summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/tenant_migration_access_blocker_registry.cpp
blob: 60b88542261ce966ffaa0971a21237215a0f8701 (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
/**
 *    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/db/repl/tenant_migration_access_blocker_registry.h"
#include "mongo/db/repl/tenant_migration_access_blocker.h"
#include "mongo/db/repl/tenant_migration_access_blocker_util.h"
#include "mongo/executor/network_interface_factory.h"
#include "mongo/executor/thread_pool_task_executor.h"
#include "mongo/logv2/log.h"
#include "mongo/util/concurrency/thread_pool.h"

namespace mongo {

namespace {

// Executor to asynchronously schedule blocking operations while the tenant migration access
// blockers are in action. This provides migrated tenants isolation from the non-migrated users.
// The executor is shared by all access blockers and the thread count goes to 0 when there is no
// migration.
std::shared_ptr<executor::TaskExecutor> _createBlockedOperationsExecutor() {
    ThreadPool::Options threadPoolOptions;
    threadPoolOptions.maxThreads = 4;
    // When there is no migration, reduce thread count to 0.
    threadPoolOptions.minThreads = 0;
    threadPoolOptions.threadNamePrefix = "TenantMigrationBlockerAsync-";
    threadPoolOptions.poolName = "TenantMigrationBlockerAsyncThreadPool";
    threadPoolOptions.onCreateThread = [](const std::string& threadName) {
        Client::initThread(threadName.c_str());
    };
    auto executor = std::make_shared<executor::ThreadPoolTaskExecutor>(
        std::make_unique<ThreadPool>(threadPoolOptions),
        executor::makeNetworkInterface("TenantMigrationBlockerNet"));

    return executor;
}
}  // namespace

using MtabType = TenantMigrationAccessBlocker::BlockerType;
using MtabPair = TenantMigrationAccessBlockerRegistry::DonorRecipientAccessBlockerPair;

const ServiceContext::Decoration<TenantMigrationAccessBlockerRegistry>
    TenantMigrationAccessBlockerRegistry::get =
        ServiceContext::declareDecoration<TenantMigrationAccessBlockerRegistry>();

void TenantMigrationAccessBlockerRegistry::add(StringData tenantId,
                                               std::shared_ptr<TenantMigrationAccessBlocker> mtab) {
    stdx::lock_guard<Latch> lg(_mutex);
    auto mtabType = mtab->getType();
    tassert(8423351,
            "add called with new-style blocker, use addShardMergeDonorAccessBlocker instead",
            mtabType != MtabType::kDonor ||
                mtab->getProtocol() != MigrationProtocolEnum::kShardMerge);

    tassert(
        8423350,
        "Adding multitenant migration donor blocker when this node has a shard merge donor blocker",
        mtabType != MtabType::kDonor || !_donorAccessBlocker);

    auto it = _tenantMigrationAccessBlockers.find(tenantId);
    if (it != _tenantMigrationAccessBlockers.end()) {
        if (it->second.getAccessBlocker(mtabType)) {
            tasserted(ErrorCodes::ConflictingOperationInProgress,
                      str::stream() << "This node is already a "
                                    << MigrationProtocol_serializer(mtab->getProtocol()) << " "
                                    << (mtabType == MtabType::kDonor ? "donor" : "recipient")
                                    << " for tenantId \"" << tenantId << "\"");
        }
        // The migration protocol guarantees that the original donor node must be garbage collected
        // before it can be chosen as a recipient under the same tenant. Therefore, we only expect
        // to have both recipient and donor access blockers in the case of back-to-back migrations
        // where the node participates first as a recipient then a donor.
        invariant(mtabType == MtabType::kDonor);
        it->second.setAccessBlocker(mtab);
        return;
    }
    MtabPair mtabPair;
    mtabPair.setAccessBlocker(mtab);
    _tenantMigrationAccessBlockers.emplace(tenantId, mtabPair);
}

void TenantMigrationAccessBlockerRegistry::addShardMergeDonorAccessBlocker(
    std::shared_ptr<TenantMigrationDonorAccessBlocker> mtab) {
    LOGV2_DEBUG(6114102, 1, "Adding shard merge donor access blocker");
    stdx::lock_guard<Latch> lg(_mutex);
    tassert(8423342,
            "addShardMergeDonorAccessBlocker called with old-style multitenant migrations blocker",
            mtab->getProtocol() == MigrationProtocolEnum::kShardMerge);
    tassert(ErrorCodes::ConflictingOperationInProgress,
            str::stream() << "This node is already a shard merge donor",
            !_donorAccessBlocker);
    tassert(6114105,
            "Adding shard merge donor blocker when this node has other donor blockers",
            std::find_if(_tenantMigrationAccessBlockers.begin(),
                         _tenantMigrationAccessBlockers.end(),
                         [](const auto& pair) {
                             return pair.second.getAccessBlocker(MtabType::kDonor).get();
                         }) == _tenantMigrationAccessBlockers.end());
    _donorAccessBlocker = mtab;
}

void TenantMigrationAccessBlockerRegistry::_remove(WithLock, StringData tenantId, MtabType type) {
    auto it = _tenantMigrationAccessBlockers.find(tenantId);

    if (it == _tenantMigrationAccessBlockers.end()) {
        return;
    }

    auto& mtabPair = it->second;
    mtabPair.clearAccessBlocker(type);
    if (!mtabPair.getAccessBlocker(MtabType::kDonor) &&
        !mtabPair.getAccessBlocker(MtabType::kRecipient)) {
        _tenantMigrationAccessBlockers.erase(it);
    }
}

void TenantMigrationAccessBlockerRegistry::remove(StringData tenantId, MtabType type) {
    stdx::lock_guard<Latch> lg(_mutex);
    if (type == MtabType::kDonor && _donorAccessBlocker) {
        tasserted(8423348, "Using remove() for new-style donor access blocker");
    }

    _remove(lg, tenantId, type);
}

void TenantMigrationAccessBlockerRegistry::removeShardMergeDonorAccessBlocker(
    const UUID& migrationId) {
    stdx::lock_guard<Latch> lg(_mutex);
    if (_donorAccessBlocker && _donorAccessBlocker->getMigrationId() == migrationId) {
        // Shard merge has one donor blocker. If it exists it must be the one we're removing.
        _donorAccessBlocker->interrupt();
        _donorAccessBlocker.reset();
    }
}

void TenantMigrationAccessBlockerRegistry::removeRecipientAccessBlockersForMigration(
    const UUID& migrationId) {
    stdx::lock_guard<Latch> lg(_mutex);
    // Clear recipient blockers for migrationId, and erase pairs with no blocker remaining.
    erase_if(_tenantMigrationAccessBlockers,
             [&](std::pair<const std::string, DonorRecipientAccessBlockerPair> it) {
                 auto& mtabPair = it.second;
                 auto recipient = checked_pointer_cast<TenantMigrationRecipientAccessBlocker>(
                     mtabPair.getAccessBlocker(MtabType::kRecipient));
                 if (!recipient || recipient->getMigrationId() != migrationId) {
                     return false;
                 }

                 mtabPair.clearAccessBlocker(MtabType::kRecipient);
                 return !mtabPair.getAccessBlocker(MtabType::kDonor);
             });
}

void TenantMigrationAccessBlockerRegistry::removeAll(MtabType type) {
    stdx::lock_guard<Latch> lg(_mutex);

    for (auto it = _tenantMigrationAccessBlockers.begin();
         it != _tenantMigrationAccessBlockers.end();) {
        _remove(lg, (it++)->first, type);
    }

    if (_donorAccessBlocker) {
        _donorAccessBlocker->interrupt();
        _donorAccessBlocker.reset();
    }
}

boost::optional<MtabPair>
TenantMigrationAccessBlockerRegistry::getTenantMigrationAccessBlockerForDbName(StringData dbName) {
    stdx::lock_guard<Latch> lg(_mutex);
    return _getTenantMigrationAccessBlockersForDbName(dbName, lg);
}

std::shared_ptr<TenantMigrationAccessBlocker>
TenantMigrationAccessBlockerRegistry::getTenantMigrationAccessBlockerForDbName(StringData dbName,
                                                                               MtabType type) {
    stdx::lock_guard<Latch> lg(_mutex);
    auto mtabPair = _getTenantMigrationAccessBlockersForDbName(dbName, lg);
    if (!mtabPair) {
        return nullptr;
    }
    return mtabPair->getAccessBlocker(type);
}

boost::optional<MtabPair>
TenantMigrationAccessBlockerRegistry::_getTenantMigrationAccessBlockersForDbName(StringData dbName,
                                                                                 WithLock lk) {
    auto tenantId = tenant_migration_access_blocker::parseTenantIdFromDB(dbName).value_or("");
    auto it = _tenantMigrationAccessBlockers.find(tenantId);
    if (it != _tenantMigrationAccessBlockers.end()) {
        auto pair = it->second;
        if (_hasDonorAccessBlocker(lk, dbName)) {
            // I still have a recipient blocker from a recent migration, now I'm a donor.
            pair.setAccessBlocker(_donorAccessBlocker);
        }
        return pair;
    } else if (_hasDonorAccessBlocker(lk, dbName)) {
        return MtabPair(_donorAccessBlocker, nullptr);
    } else {
        return boost::none;
    }
}

bool TenantMigrationAccessBlockerRegistry::_hasDonorAccessBlocker(WithLock, StringData dbName) {
    // No-op oplog entries, e.g. for linearizable reads, use namespace "".
    bool isInternal = (dbName == "" || NamespaceString(dbName).isOnInternalDb());
    return _donorAccessBlocker && !isInternal;
}

std::shared_ptr<TenantMigrationAccessBlocker>
TenantMigrationAccessBlockerRegistry::getTenantMigrationAccessBlockerForTenantId(
    StringData tenantId, MtabType type) {
    stdx::lock_guard<Latch> lg(_mutex);
    if (type == MtabType::kDonor && _donorAccessBlocker) {
        return _donorAccessBlocker;
    }

    auto it = _tenantMigrationAccessBlockers.find(tenantId);
    if (it != _tenantMigrationAccessBlockers.end()) {
        return it->second.getAccessBlocker(type);
    } else {
        return nullptr;
    }
}

void TenantMigrationAccessBlockerRegistry::applyAll(
    TenantMigrationAccessBlocker::BlockerType type,
    const std::function<void(std::shared_ptr<TenantMigrationAccessBlocker>)>& callback) {
    stdx::lock_guard<Latch> lg(_mutex);
    for (auto& [tenantId, mtabPair] : _tenantMigrationAccessBlockers) {
        if (auto mtab = mtabPair.getAccessBlocker(type)) {
            callback(mtab);
        }
    }
}

void TenantMigrationAccessBlockerRegistry::shutDown() {
    stdx::lock_guard<Latch> lg(_mutex);
    _tenantMigrationAccessBlockers.clear();
    _asyncBlockingOperationsExecutor.reset();
}

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

    if (_donorAccessBlocker) {
        BSONObjBuilder donorMtabInfoBuilder;
        _donorAccessBlocker->appendInfoForServerStatus(&donorMtabInfoBuilder);
        builder->append("donor", donorMtabInfoBuilder.obj());
    }

    for (auto& [tenantId, mtabPair] : _tenantMigrationAccessBlockers) {
        BSONObjBuilder mtabInfoBuilder;

        if (auto donorMtab = mtabPair.getAccessBlocker(MtabType::kDonor)) {
            BSONObjBuilder donorMtabInfoBuilder;
            donorMtab->appendInfoForServerStatus(&donorMtabInfoBuilder);
            mtabInfoBuilder.append("donor", donorMtabInfoBuilder.obj());
        }

        if (auto recipientMtab = mtabPair.getAccessBlocker(MtabType::kRecipient)) {
            BSONObjBuilder recipientMtabInfoBuilder;
            recipientMtab->appendInfoForServerStatus(&recipientMtabInfoBuilder);
            mtabInfoBuilder.append("recipient", recipientMtabInfoBuilder.obj());
        }

        if (mtabInfoBuilder.len()) {
            builder->append(tenantId, mtabInfoBuilder.obj());
        }
    }
}

void TenantMigrationAccessBlockerRegistry::onMajorityCommitPointUpdate(repl::OpTime opTime) {
    stdx::lock_guard<Latch> lg(_mutex);

    for (auto& [_, mtabPair] : _tenantMigrationAccessBlockers) {
        if (auto recipientMtab = mtabPair.getAccessBlocker(MtabType::kRecipient)) {
            recipientMtab->onMajorityCommitPointUpdate(opTime);
        }
        if (auto donorMtab = mtabPair.getAccessBlocker(MtabType::kDonor)) {
            donorMtab->onMajorityCommitPointUpdate(opTime);
        }
    }

    if (_donorAccessBlocker) {
        _donorAccessBlocker->onMajorityCommitPointUpdate(opTime);
    }
}

std::shared_ptr<executor::TaskExecutor>
TenantMigrationAccessBlockerRegistry::getAsyncBlockingOperationsExecutor() {
    stdx::lock_guard<Latch> lg(_mutex);
    if (!_asyncBlockingOperationsExecutor) {
        _asyncBlockingOperationsExecutor = _createBlockedOperationsExecutor();
        _asyncBlockingOperationsExecutor->startup();
    }

    return _asyncBlockingOperationsExecutor;
}

}  // namespace mongo