summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/tenant_migration_access_blocker.cpp
blob: b7d95ed16d870436224eedb077be20069d494b2c (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
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
322
323
324
325
326
327
328
329
330
/**
 *    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.
 */

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTenantMigration

#include "mongo/platform/basic.h"

#include "mongo/db/client.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/repl/tenant_migration_access_blocker.h"
#include "mongo/db/repl/tenant_migration_committed_info.h"
#include "mongo/db/repl/tenant_migration_conflict_info.h"
#include "mongo/logv2/log.h"
#include "mongo/util/cancelation.h"
#include "mongo/util/fail_point.h"
#include "mongo/util/future_util.h"

namespace mongo {

namespace {

MONGO_FAIL_POINT_DEFINE(tenantMigrationBlockRead);
MONGO_FAIL_POINT_DEFINE(tenantMigrationBlockWrite);

const Backoff kExponentialBackoff(Seconds(1), Milliseconds::max());

}  // namespace

void TenantMigrationAccessBlocker::checkIfCanWriteOrThrow() {
    stdx::lock_guard<Latch> lg(_mutex);

    switch (_state) {
        case State::kAllow:
            return;
        case State::kAborted:
            return;
        case State::kBlockWrites:
        case State::kBlockWritesAndReads:
            uasserted(TenantMigrationConflictInfo(_tenantId, shared_from_this()),
                      "Write must block until this tenant migration commits or aborts");
        case State::kReject:
            uasserted(TenantMigrationCommittedInfo(_tenantId, _recipientConnString),
                      "Write must be re-routed to the new owner of this tenant");
        default:
            MONGO_UNREACHABLE;
    }
}

Status TenantMigrationAccessBlocker::waitUntilCommittedOrAborted(OperationContext* opCtx) {
    stdx::unique_lock<Latch> ul(_mutex);

    auto canWrite = [&]() { return _state == State::kAllow || _state == State::kAborted; };

    if (!canWrite()) {
        tenantMigrationBlockWrite.shouldFail();
    }

    opCtx->waitForConditionOrInterrupt(
        _transitionOutOfBlockingCV, ul, [&]() { return canWrite() || _state == State::kReject; });
    return onCompletion().getNoThrow();
}

void TenantMigrationAccessBlocker::checkIfCanDoClusterTimeReadOrBlock(
    OperationContext* opCtx, const Timestamp& readTimestamp) {
    stdx::unique_lock<Latch> ul(_mutex);

    auto canRead = [&]() {
        return _state == State::kAllow || _state == State::kAborted ||
            _state == State::kBlockWrites || readTimestamp < *_blockTimestamp;
    };

    if (!canRead()) {
        tenantMigrationBlockRead.shouldFail();
    }

    opCtx->waitForConditionOrInterrupt(
        _transitionOutOfBlockingCV, ul, [&]() { return canRead() || _state == State::kReject; });

    uassert(TenantMigrationCommittedInfo(_tenantId, _recipientConnString),
            "Read must be re-routed to the new owner of this tenant",
            canRead());
}

void TenantMigrationAccessBlocker::checkIfLinearizableReadWasAllowedOrThrow(
    OperationContext* opCtx) {
    stdx::lock_guard<Latch> lg(_mutex);
    uassert(TenantMigrationCommittedInfo(_tenantId, _recipientConnString),
            "Read must be re-routed to the new owner of this tenant",
            _state != State::kReject);
}

void TenantMigrationAccessBlocker::startBlockingWrites() {
    stdx::lock_guard<Latch> lg(_mutex);

    LOGV2(5093800, "Tenant migration starting to block writes", "tenantId"_attr = _tenantId);

    invariant(!_inShutdown);
    invariant(_state == State::kAllow);
    invariant(!_blockTimestamp);
    invariant(!_commitOrAbortOpTime);
    invariant(!_waitForCommitOrAbortToMajorityCommitOpCtx);

    _state = State::kBlockWrites;
}

void TenantMigrationAccessBlocker::startBlockingReadsAfter(const Timestamp& blockTimestamp) {
    stdx::lock_guard<Latch> lg(_mutex);

    LOGV2(5093801,
          "Tenant migration starting to block reads after blockTimestamp",
          "tenantId"_attr = _tenantId,
          "blockTimestamp"_attr = blockTimestamp);

    invariant(!_inShutdown);
    invariant(_state == State::kBlockWrites);
    invariant(!_blockTimestamp);
    invariant(!_commitOrAbortOpTime);
    invariant(!_waitForCommitOrAbortToMajorityCommitOpCtx);

    _state = State::kBlockWritesAndReads;
    _blockTimestamp = blockTimestamp;
}

void TenantMigrationAccessBlocker::rollBackStartBlocking() {
    stdx::lock_guard<Latch> lg(_mutex);

    invariant(!_inShutdown);
    invariant(_state == State::kBlockWrites || _state == State::kBlockWritesAndReads);
    invariant(!_commitOrAbortOpTime);
    invariant(!_waitForCommitOrAbortToMajorityCommitOpCtx);

    _state = State::kAllow;
    _blockTimestamp.reset();
    _transitionOutOfBlockingCV.notify_all();
}

void TenantMigrationAccessBlocker::commit(repl::OpTime commitOpTime) {
    stdx::lock_guard<Latch> lg(_mutex);

    LOGV2(5093802,
          "Tenant migration starting to wait for commit OpTime to be majority-committed",
          "tenantId"_attr = _tenantId,
          "commitOpTime"_attr = commitOpTime);

    invariant(!_inShutdown);
    invariant(_state == State::kBlockWritesAndReads);
    invariant(_blockTimestamp);
    invariant(!_commitOrAbortOpTime);
    invariant(!_waitForCommitOrAbortToMajorityCommitOpCtx);

    _commitOrAbortOpTime = commitOpTime;

    _waitForOpTimeToMajorityCommit(commitOpTime)
        .then([this, self = shared_from_this(), commitOpTime]() {
            stdx::lock_guard<Latch> lg(_mutex);

            invariant(_state == State::kBlockWritesAndReads);
            invariant(_blockTimestamp);
            invariant(_commitOrAbortOpTime == commitOpTime);
            invariant(!_waitForCommitOrAbortToMajorityCommitOpCtx);

            _state = State::kReject;
            _transitionOutOfBlockingCV.notify_all();
            _completionPromise.setError(
                {ErrorCodes::TenantMigrationCommitted,
                 "Write must be re-routed to the new owner of this tenant",
                 TenantMigrationCommittedInfo(_tenantId, _recipientConnString).toBSON()});
        })
        .getAsync([this, self = shared_from_this()](Status status) {
            stdx::lock_guard<Latch> lg(_mutex);
            LOGV2(5093803,
                  "Tenant migration finished waiting for commit OpTime to be majority-committed",
                  "tenantId"_attr = _tenantId,
                  "status"_attr = status);
        });
}

void TenantMigrationAccessBlocker::abort(repl::OpTime abortOpTime) {
    stdx::lock_guard<Latch> lg(_mutex);

    LOGV2(5093804,
          "Tenant migration starting to wait for abort OpTime to be majority-committed",
          "tenantId"_attr = _tenantId,
          "abortOpTime"_attr = abortOpTime);

    invariant(!_inShutdown);
    invariant(!_commitOrAbortOpTime);
    invariant(!_waitForCommitOrAbortToMajorityCommitOpCtx);

    _commitOrAbortOpTime = abortOpTime;

    _waitForOpTimeToMajorityCommit(abortOpTime)
        .then([this, self = shared_from_this(), abortOpTime]() {
            stdx::lock_guard<Latch> lg(_mutex);

            invariant(_commitOrAbortOpTime == abortOpTime);
            invariant(!_waitForCommitOrAbortToMajorityCommitOpCtx);

            _state = State::kAborted;
            _transitionOutOfBlockingCV.notify_all();
            _completionPromise.setError(
                {ErrorCodes::TenantMigrationAborted, "Tenant migration aborted"});
        })
        .getAsync([this, self = shared_from_this()](Status status) {
            stdx::lock_guard<Latch> lg(_mutex);
            LOGV2(5093805,
                  "Tenant migration finished waiting for abort OpTime to be majority-committed",
                  "tenantId"_attr = _tenantId,
                  "status"_attr = status);
        });
}

void TenantMigrationAccessBlocker::shutDown() {
    stdx::lock_guard<Latch> lg(_mutex);
    if (_inShutdown) {
        return;
    }

    _inShutdown = true;
    if (_waitForCommitOrAbortToMajorityCommitOpCtx) {
        stdx::lock_guard<Client> lk(*_waitForCommitOrAbortToMajorityCommitOpCtx->getClient());
        _waitForCommitOrAbortToMajorityCommitOpCtx->markKilled();
    }
}

SharedSemiFuture<void> TenantMigrationAccessBlocker::onCompletion() {
    return _completionPromise.getFuture();
}

ExecutorFuture<void> TenantMigrationAccessBlocker::_waitForOpTimeToMajorityCommit(
    repl::OpTime opTime) {
    return AsyncTry([this, self = shared_from_this(), opTime] {
               ThreadClient tc("TenantMigrationAccessBlocker", _serviceContext);
               const auto opCtxHolder = tc->makeOperationContext();
               const auto opCtx = opCtxHolder.get();

               // We will save 'opCtx' below, so make sure we clear it before 'opCtx' is destroyed.
               const auto guard = makeGuard([&] {
                   stdx::lock_guard<Latch> lg(_mutex);
                   _waitForCommitOrAbortToMajorityCommitOpCtx = nullptr;
               });

               {
                   stdx::lock_guard<Latch> lg(_mutex);

                   uassert(ErrorCodes::TenantMigrationAccessBlockerShuttingDown,
                           "TenantMigrationAccessBlocker was shut down",
                           !_inShutdown);

                   // Save 'opCtx' so that if shutDown() is called after this point, 'opCtx' will be
                   // interrupted and 'waitUntilMajorityOpTime' will return an interrupt error.
                   _waitForCommitOrAbortToMajorityCommitOpCtx = opCtx;
               }
               uassertStatusOK(repl::ReplicationCoordinator::get(opCtx)->waitUntilMajorityOpTime(
                   opCtx, opTime));
           })
        .until([this, self = shared_from_this(), opTime](Status status) {
            bool shouldStop =
                status.isOK() || status == ErrorCodes::TenantMigrationAccessBlockerShuttingDown;
            if (!shouldStop) {
                LOGV2(5093806,
                      "Tenant migration retrying waiting for OpTime to be majority-committed",
                      "tenantId"_attr = _tenantId,
                      "opTime"_attr = opTime,
                      "status"_attr = status);
            }
            return shouldStop;
        })
        .withBackoffBetweenIterations(kExponentialBackoff)
        .on(_executor, CancelationToken::uncancelable());
}

void TenantMigrationAccessBlocker::appendInfoForServerStatus(BSONObjBuilder* builder) const {
    stdx::lock_guard<Latch> lg(_mutex);

    BSONObjBuilder tenantBuilder;
    tenantBuilder.append("state", stateToString(_state));
    if (_blockTimestamp) {
        tenantBuilder.append("blockTimestamp", _blockTimestamp.get());
    }
    if (_commitOrAbortOpTime) {
        tenantBuilder.append("commitOrAbortOpTime", _commitOrAbortOpTime->toBSON());
    }
    builder->append(_tenantId, tenantBuilder.obj());
}

std::string TenantMigrationAccessBlocker::stateToString(State state) const {
    switch (state) {
        case State::kAllow:
            return "allow";
        case State::kBlockWrites:
            return "blockWrites";
        case State::kBlockWritesAndReads:
            return "blockWritesAndReads";
        case State::kReject:
            return "reject";
        case State::kAborted:
            return "aborted";
        default:
            MONGO_UNREACHABLE;
    }
}

}  // namespace mongo