summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/checkpointer.cpp
blob: b3d249e18376f28858490a2f419721144b28d91f (plain)
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
/**
 *    Copyright (C) 2020-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */


#include "mongo/platform/basic.h"

#include "mongo/db/storage/checkpointer.h"

#include "mongo/db/operation_context.h"
#include "mongo/db/service_context.h"
#include "mongo/db/storage/kv/kv_engine.h"
#include "mongo/logv2/log.h"
#include "mongo/util/concurrency/idle_thread_block.h"
#include "mongo/util/fail_point.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kStorage


namespace mongo {

namespace {

const auto getCheckpointer = ServiceContext::declareDecoration<std::unique_ptr<Checkpointer>>();

MONGO_FAIL_POINT_DEFINE(pauseCheckpointThread);

}  // namespace

Checkpointer* Checkpointer::get(ServiceContext* serviceCtx) {
    return getCheckpointer(serviceCtx).get();
}

Checkpointer* Checkpointer::get(OperationContext* opCtx) {
    return get(opCtx->getServiceContext());
}

void Checkpointer::set(ServiceContext* serviceCtx, std::unique_ptr<Checkpointer> newCheckpointer) {
    auto& checkpointer = getCheckpointer(serviceCtx);
    if (checkpointer) {
        invariant(!checkpointer->running(),
                  "Tried to reset the Checkpointer without shutting down the original instance.");
    }
    checkpointer = std::move(newCheckpointer);
}

void Checkpointer::run() {
    ThreadClient tc(name(), getGlobalServiceContext());
    LOGV2_DEBUG(22307, 1, "Starting thread", "threadName"_attr = name());

    {
        stdx::lock_guard<Client> lk(*tc.get());
        tc.get()->setSystemOperationUnkillableByStepdown(lk);
    }

    while (true) {
        auto opCtx = tc->makeOperationContext();

        {
            stdx::unique_lock<Latch> lock(_mutex);
            MONGO_IDLE_THREAD_BLOCK;

            // Wait for 'storageGlobalParams.syncdelay' seconds; or until either shutdown is
            // signaled or a checkpoint is triggered.
            LOGV2_DEBUG(7702900,
                        1,
                        "Checkpoint thread sleeping",
                        "duration"_attr = static_cast<std::int64_t>(storageGlobalParams.syncdelay));
            _sleepCV.wait_for(
                lock,
                stdx::chrono::seconds(static_cast<std::int64_t>(storageGlobalParams.syncdelay)),
                [&] { return _shuttingDown || _triggerCheckpoint; });

            // If the syncdelay is set to 0, that means we should skip checkpointing. However,
            // syncdelay is adjustable by a runtime server parameter, so we need to wake up to check
            // periodically. The wakeup to check period is arbitrary.
            while (storageGlobalParams.syncdelay == 0 && !_shuttingDown && !_triggerCheckpoint) {
                _sleepCV.wait_for(lock, stdx::chrono::seconds(static_cast<std::int64_t>(3)), [&] {
                    return _shuttingDown || _triggerCheckpoint;
                });
            }

            if (_shuttingDown) {
                invariant(!_shutdownReason.isOK());
                LOGV2_DEBUG(22309,
                            1,
                            "Stopping thread",
                            "threadName"_attr = name(),
                            "reason"_attr = _shutdownReason);
                return;
            }

            // Clear the trigger so we do not immediately checkpoint again after this.
            _triggerCheckpoint = false;
        }

        pauseCheckpointThread.pauseWhileSet();

        const Date_t startTime = Date_t::now();
        opCtx->getServiceContext()->getStorageEngine()->checkpoint(opCtx.get());

        const auto secondsElapsed = durationCount<Seconds>(Date_t::now() - startTime);
        if (secondsElapsed >= 30) {
            LOGV2_DEBUG(22308,
                        1,
                        "Checkpoint was slow to complete",
                        "secondsElapsed"_attr = secondsElapsed);
        }
    }
}

void Checkpointer::triggerFirstStableCheckpoint(Timestamp prevStable,
                                                Timestamp initialData,
                                                Timestamp currStable) {
    stdx::unique_lock<Latch> lock(_mutex);
    invariant(!_hasTriggeredFirstStableCheckpoint);
    if (prevStable < initialData && currStable >= initialData) {
        LOGV2(22310,
              "Triggering the first stable checkpoint",
              "initialDataTimestamp"_attr = initialData,
              "prevStableTimestamp"_attr = prevStable,
              "currStableTimestamp"_attr = currStable);
        _hasTriggeredFirstStableCheckpoint = true;
        _triggerCheckpoint = true;
        _sleepCV.notify_one();
    }
}

bool Checkpointer::hasTriggeredFirstStableCheckpoint() {
    stdx::unique_lock<Latch> lock(_mutex);
    return _hasTriggeredFirstStableCheckpoint;
}

void Checkpointer::shutdown(const Status& reason) {
    LOGV2(22322, "Shutting down checkpoint thread");

    {
        stdx::unique_lock<Latch> lock(_mutex);
        _shuttingDown = true;
        _shutdownReason = reason;

        // Wake up the checkpoint thread early, to take a final checkpoint before shutting down, if
        // one has not coincidentally just been taken.
        _sleepCV.notify_one();
    }

    wait();
    LOGV2(22323, "Finished shutting down checkpoint thread");
}

}  // namespace mongo