aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/frameworkimpl/thread/deadlockdetector.cpp
blob: 7b3ab16790c6187ffc0a6a2ad9a402ee16cb2b85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "deadlockdetector.h"
#include "htmltable.h"
#include <vespa/storage/bucketdb/storbucketdb.h>
#include <vespa/storage/common/content_bucket_space_repo.h>
#include <vespa/storageframework/generic/thread/thread.h>
#include <vespa/storageframework/generic/clock/clock.h>
#include <vespa/vespalib/stllike/asciistream.h>

#include <vespa/log/bufferedlogger.h>
LOG_SETUP(".deadlock.detector");

using document::BucketSpace;

namespace storage {

DeadLockDetector::DeadLockDetector(StorageComponentRegister& compReg, AppKiller::UP killer)
    : framework::HtmlStatusReporter("deadlockdetector", "Dead lock detector"),
      _killer(std::move(killer)),
      _states(),
      _lock(),
      _cond(),
      _enableWarning(true),
      _enableShutdown(false),
      _processSlack(30s),
      _waitSlack(5s)
{
    auto* dComp(dynamic_cast<DistributorComponentRegister*>(&compReg));
    if (dComp) {
        _dComponent = std::make_unique<DistributorComponent>(*dComp, "deadlockdetector");
        _component = _dComponent.get();
    } else {
        auto* slComp(dynamic_cast<ServiceLayerComponentRegister*>(&compReg));
        assert(slComp != nullptr);
        _slComponent = std::make_unique<ServiceLayerComponent>(*slComp, "deadlockdetector");
        _component = _slComponent.get();
    }
    _component->registerStatusPage(*this);
    _thread = _component->startThread(*this);
}

DeadLockDetector::~DeadLockDetector()
{
    if (_thread) {
        _thread->interruptAndJoin(_cond);
    }
}

void
DeadLockDetector::enableWarning(bool enable)
{
    if (enable == warning_enabled_relaxed()) {
        return;
    }
    LOG(debug, "%s dead lock detection warnings", enable ? "Enabling" : "Disabling");
    _enableWarning.store(enable, std::memory_order_relaxed);
}

void
DeadLockDetector::enableShutdown(bool enable)
{
    if (enable == shutdown_enabled_relaxed()) {
        return;
    }
    LOG(debug, "%s dead lock detection", enable ? "Enabling" : "Disabling");
    _enableShutdown.store(enable, std::memory_order_relaxed);
}

namespace {
    struct VisitorWrapper : public framework::ThreadVisitor {
        std::map<vespalib::string, DeadLockDetector::State>& _states;
        DeadLockDetector::ThreadVisitor& _visitor;

        VisitorWrapper(std::map<vespalib::string, DeadLockDetector::State>& s,
                       DeadLockDetector::ThreadVisitor& visitor)
            : _states(s),
              _visitor(visitor)
        {
        }

        void visitThread(const framework::Thread& thread) override {
            if (_states.find(thread.getId()) == _states.end()) {
                _states[thread.getId()] = DeadLockDetector::OK;
            }
            _visitor.visitThread(thread, _states[thread.getId()]);
        }
    };
}

void
DeadLockDetector::visitThreads(ThreadVisitor& visitor) const
{
    VisitorWrapper wrapper(_states, visitor);
    _component->getThreadPool().visitThreads(wrapper);
}

bool
DeadLockDetector::isAboveFailThreshold(vespalib::steady_time time,
                                       const framework::ThreadProperties& tp,
                                       const framework::ThreadTickData& tick) const
{
    if (tp.getMaxCycleTime() == vespalib::duration::zero()) {
        return false;
    }
    vespalib::duration slack(tick._lastTickType == framework::WAIT_CYCLE
            ? getWaitSlack() : getProcessSlack());
    return (tick._lastTick + tp.getMaxCycleTime() + slack < time);
}

bool
DeadLockDetector::isAboveWarnThreshold(vespalib::steady_time time,
                                       const framework::ThreadProperties& tp,
                                       const framework::ThreadTickData& tick) const
{
    if (tp.getMaxCycleTime() == vespalib::duration::zero()) return false;
    vespalib::duration slack = tick._lastTickType == framework::WAIT_CYCLE
            ? getWaitSlack() : getProcessSlack();
    return (tick._lastTick + tp.getMaxCycleTime() + slack / 4 < time);
}

vespalib::string
DeadLockDetector::getBucketLockInfo() const
{
    vespalib::asciistream ost;
    if (_dComponent.get() != nullptr) {
        ost << "No bucket lock information available for distributor\n";
    } else {
        for (const auto &elem : _slComponent->getBucketSpaceRepo()) {
            const auto &bucketDatabase = elem.second->bucketDatabase();
            if (bucketDatabase.size() > 0) {
                bucketDatabase.showLockClients(ost);
            }
        }
    }
    return ost.str();
}

namespace {
    struct ThreadChecker : public DeadLockDetector::ThreadVisitor
    {
        DeadLockDetector& _detector;
        vespalib::steady_time _currentTime;

        ThreadChecker(DeadLockDetector& d, vespalib::steady_time time)
            : _detector(d), _currentTime(time) {}

        void visitThread(const framework::Thread& thread,
                         DeadLockDetector::State& state) override
        {
            const auto& id  = thread.getId();
            const auto& tp  = thread.getProperties();
            const auto tick = thread.getTickData();
            // In case we just got a new tick, ignore the thread
            if (tick._lastTick > _currentTime) return;
            // If thread is already in halted state, ignore it.
            if (state == DeadLockDetector::HALTED) return;

            if (_detector.isAboveFailThreshold(_currentTime, tp, tick)) {
                state = DeadLockDetector::HALTED;
                _detector.handleDeadlock(_currentTime, thread, id, tp, tick, false);
            } else if (_detector.isAboveWarnThreshold(_currentTime, tp, tick)) {
                state = DeadLockDetector::WARNED;
                _detector.handleDeadlock(_currentTime, thread, id, tp, tick, true);
            } else if (state != DeadLockDetector::OK) {
                vespalib::asciistream ost;
                ost << "Thread " << id << " has registered tick again.";
                LOGBP(info, "%s", ost.str().data());
                state = DeadLockDetector::OK;
            }
        }
    };
}

void
DeadLockDetector::handleDeadlock(vespalib::steady_time currentTime,
                                 const framework::Thread& deadlocked_thread,
                                 const vespalib::string& id,
                                 const framework::ThreadProperties&,
                                 const framework::ThreadTickData& tick,
                                 bool warnOnly)
{
    vespalib::asciistream error;
    error << "Thread " << id << " has gone "
          << vespalib::count_ms(currentTime - tick._lastTick)
          << " milliseconds without registering a tick.";
    const bool shutdown_enabled = shutdown_enabled_relaxed();
    const bool warning_enabled  = warning_enabled_relaxed();
    if (!warnOnly) {
        if (shutdown_enabled) {
            error << " Restarting process due to presumed internal deadlock.";
        } else {
            error << " Would have restarted process due to deadlock if shutdown had been enabled.";
        }
    } else {
        // TODO would ideally print thread ID here, but it's not well-defined how to print a pthread_t...
        error << " Global slack not expended yet. Warning for now. Attempting to dump stack of thread...\n";
        error << deadlocked_thread.get_live_thread_stack_trace();
    }
    if (warnOnly) {
        if (warning_enabled) {
            LOGBT(warning, "deadlockw-" + id, "%s", vespalib::string(error.str()).c_str());
        }
        return;
    } else {
        if (shutdown_enabled || warning_enabled) {
            LOGBT(error, "deadlock-" + id, "%s", vespalib::string(error.str()).c_str());
        }
    }
    if (shutdown_enabled) {
        _killer->kill();
    }
}

void
DeadLockDetector::run(framework::ThreadHandle& thread)
{
    std::unique_lock sync(_lock);
    while (!thread.interrupted()) {
        ThreadChecker checker(*this, _component->getClock().getMonotonicTime());
        visitThreads(checker);
        _cond.wait_for(sync, 1s);
        thread.registerTick(framework::WAIT_CYCLE);
    }
}

namespace {
    struct ThreadTable {
        HtmlTable _table;
        LongColumn _msSinceLastTick;
        LongColumn _maxProcTickTime;
        LongColumn _maxWaitTickTime;
        LongColumn _maxProcTickTimeSeen;
        LongColumn _maxWaitTickTimeSeen;

        ThreadTable()
            : _table("Thread name"),
              _msSinceLastTick("Milliseconds since last tick", " ms", &_table),
              _maxProcTickTime("Max milliseconds before wait tick", " ms", &_table),
              _maxWaitTickTime("Max milliseconds before wait tick", " ms", &_table),
              _maxProcTickTimeSeen("Max processing tick time observed", " ms", &_table),
              _maxWaitTickTimeSeen("Max wait tick time observed", " ms", &_table)
        {
            _maxProcTickTime._alignment = Column::LEFT;
            _maxProcTickTimeSeen._alignment = Column::LEFT;
            _maxWaitTickTimeSeen._alignment = Column::LEFT;
        }
        ~ThreadTable();
    };

    ThreadTable::~ThreadTable() = default;

    struct ThreadStatusWriter : public DeadLockDetector::ThreadVisitor {
        ThreadTable& _table;
        vespalib::steady_time _time;

        ThreadStatusWriter(ThreadTable& table, vespalib::steady_time time)
            : _table(table), _time(time) {}

        template<typename T>
        vespalib::string toS(const T& val) {
            vespalib::asciistream ost;
            ost << val;
            return ost.str();
        }

        void visitThread(const framework::Thread& thread,
                         DeadLockDetector::State& /*state*/) override
        {
            _table._table.addRow(thread.getId());
            uint32_t i = _table._table.getRowCount() - 1;
            const auto& tp = thread.getProperties();
            const auto tick = thread.getTickData();
            _table._msSinceLastTick[i] = vespalib::count_ms(_time - tick._lastTick);
            _table._maxProcTickTime[i] = vespalib::count_ms(tp.getMaxProcessTime());
            _table._maxWaitTickTime[i] = vespalib::count_ms(tp.getWaitTime());
            _table._maxProcTickTimeSeen[i] = vespalib::count_ms(tick._maxProcessingTimeSeen);
            _table._maxWaitTickTimeSeen[i] = vespalib::count_ms(tick._maxWaitTimeSeen);
        }
    };
}

void
DeadLockDetector::reportHtmlStatus(std::ostream& os,
                                   const framework::HttpUrlPath&) const
{
    vespalib::asciistream out;
    out << "<h2>Overview of latest thread ticks</h2>\n";
    ThreadTable threads;
    std::lock_guard guard(_lock);
    ThreadStatusWriter writer(threads, _component->getClock().getMonotonicTime());
    visitThreads(writer);
    std::ostringstream ost;
    threads._table.print(ost);
    out << ost.str();
    out << "<p>\n"
        << "Note that there is a global slack period of " << vespalib::count_ms(getProcessSlack())
        << " ms for processing ticks and " << vespalib::count_ms(getWaitSlack())
        << " ms for wait ticks. Actual shutdown or warning logs will not"
        << " appear before this slack time is expendede on top of the per"
        << " thread value.\n"
        << "</p>\n";
    if (shutdown_enabled_relaxed()) {
        out << "<p>The deadlock detector is enabled and will kill the process "
            << "if a deadlock is detected</p>\n";
    } else {
        out << "<p>The deadlock detector is disabled and will only monitor "
            << "tick times.</p>\n";
    }
    out << "<h2>Current locks in the bucket database</h2>\n"
        << "<p>In case of a software bug causing a deadlock in the code, bucket"
        << " database locks are a likely reason. Thus, we list current locks "
        << "here in hopes that it will simplify debugging.</p>\n"
        << "<p>Bucket database</p>\n"
        << "<pre>\n"
        << getBucketLockInfo()
        << "</pre>\n";
    os << out.str();
}

} // storage