summaryrefslogtreecommitdiff
path: root/src/mongo/db/shard_role.cpp
blob: 39e289c9096209f6db7bc6fc44ec0780364548b2 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
 *    Copyright (C) 2022-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/db/shard_role.h"

#include <exception>
#include <map>

#include "mongo/db/catalog/catalog_helper.h"
#include "mongo/db/catalog/collection_uuid_mismatch.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/repl/read_concern_args.h"
#include "mongo/db/s/collection_sharding_runtime.h"
#include "mongo/db/s/collection_sharding_state.h"
#include "mongo/db/s/database_sharding_state.h"
#include "mongo/db/s/operation_sharding_state.h"
#include "mongo/db/s/scoped_collection_metadata.h"
#include "mongo/db/transaction_resources.h"
#include "mongo/logv2/log.h"
#include "mongo/util/decorable.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding

namespace mongo {
namespace {

auto getTransactionResources = OperationContext::declareDecoration<
    boost::optional<shard_role_details::TransactionResources>>();

shard_role_details::TransactionResources& getOrMakeTransactionResources(OperationContext* opCtx) {
    auto& readConcern = repl::ReadConcernArgs::get(opCtx);
    auto& optTransactionResources = getTransactionResources(opCtx);
    if (!optTransactionResources) {
        optTransactionResources.emplace(readConcern);
    }

    return *optTransactionResources;
}

NamespaceOrViewAcquisitionRequest::PlacementConcern getPlacementConcernFromOSS(
    OperationContext* opCtx, const NamespaceString& nss) {
    const auto optDbVersion = OperationShardingState::get(opCtx).getDbVersion(nss.db());
    const auto optShardVersion = OperationShardingState::get(opCtx).getShardVersion(nss);
    return {optDbVersion, optShardVersion};
}

struct ResolvedNamespaceOrViewAcquisitionRequest {
    // Populated in the first phase of collection(s) acquisition
    NamespaceString nss;

    boost::optional<UUID> uuid;

    NamespaceOrViewAcquisitionRequest::PlacementConcern tsPlacement;
    NamespaceOrViewAcquisitionRequest::ViewMode viewMode;

    // Populated optionally in the second phase of collection(s) acquisition
    boost::optional<Lock::DBLock> dbLock;
    boost::optional<Lock::CollectionLock> collLock;

    boost::optional<ScopedCollectionFilter> filter;
};

using ResolvedNamespaceOrViewAcquisitionRequestsMap =
    std::map<ResourceId, ResolvedNamespaceOrViewAcquisitionRequest>;

ResolvedNamespaceOrViewAcquisitionRequestsMap resolveNamespaceOrViewAcquisitionRequests(
    OperationContext* opCtx,
    std::initializer_list<NamespaceOrViewAcquisitionRequest> acquisitionRequests) {
    auto catalog = CollectionCatalog::get(opCtx);

    ResolvedNamespaceOrViewAcquisitionRequestsMap sortedAcquisitionRequests;

    for (const auto& ar : acquisitionRequests) {
        if (ar.nss) {
            auto coll = catalog->lookupCollectionByNamespace(opCtx, *ar.nss);
            if (ar.uuid) {
                checkCollectionUUIDMismatch(opCtx, *ar.nss, coll, *ar.uuid);
            }

            ResolvedNamespaceOrViewAcquisitionRequest resolvedAcquisitionRequest{
                *ar.nss,
                ar.uuid,
                ar.placementConcern,
                ar.viewMode,
                boost::none,
                boost::none,
                boost::none};
            sortedAcquisitionRequests.emplace(ResourceId(RESOURCE_COLLECTION, *ar.nss),
                                              std::move(resolvedAcquisitionRequest));
        } else if (ar.dbname) {
            invariant(ar.uuid);
            auto coll = catalog->lookupCollectionByUUID(opCtx, *ar.uuid);
            uassert(ErrorCodes::NamespaceNotFound,
                    str::stream() << "Namespace " << *ar.dbname << ":" << *ar.uuid << " not found",
                    coll);
            uassert(ErrorCodes::NamespaceNotFound,
                    str::stream() << "Database name mismatch for " << *ar.dbname << ":" << *ar.uuid
                                  << ". Expected: " << *ar.dbname
                                  << " Actual: " << coll->ns().dbName(),
                    coll->ns().dbName() == *ar.dbname);
            if (ar.nss) {
                checkCollectionUUIDMismatch(opCtx, *ar.nss, coll, *ar.uuid);
            }

            ResolvedNamespaceOrViewAcquisitionRequest resolvedAcquisitionRequest{
                coll->ns(),
                coll->uuid(),
                ar.placementConcern,
                ar.viewMode,
                boost::none,
                boost::none,
                boost::none};

            sortedAcquisitionRequests.emplace(ResourceId(RESOURCE_COLLECTION, coll->ns()),
                                              std::move(resolvedAcquisitionRequest));
        }
    }

    return sortedAcquisitionRequests;
}

void verifyDbAndCollection(OperationContext* opCtx,
                           const NamespaceString& nss,
                           CollectionPtr& coll) {
    invariant(coll);

    // In most cases we expect modifications for system.views to upgrade MODE_IX to MODE_X
    // before taking the lock. One exception is a query by UUID of system.views in a
    // transaction. Usual queries of system.views (by name, not UUID) within a transaction are
    // rejected. However, if the query is by UUID we can't determine whether the namespace is
    // actually system.views until we take the lock here. So we have this one last assertion.
    uassert(6944500,
            "Modifications to system.views must take an exclusive lock",
            !nss.isSystemDotViews() || opCtx->lockState()->isCollectionLockedForMode(nss, MODE_X));

    // If we are in a transaction, we cannot yield and wait when there are pending catalog changes.
    // Instead, we must return an error in such situations. We ignore this restriction for the
    // oplog, since it never has pending catalog changes.
    if (opCtx->inMultiDocumentTransaction() && nss != NamespaceString::kRsOplogNamespace) {
        if (auto minSnapshot = coll->getMinimumVisibleSnapshot()) {
            auto mySnapshot =
                opCtx->recoveryUnit()->getPointInTimeReadTimestamp(opCtx).get_value_or(
                    opCtx->recoveryUnit()->getCatalogConflictingTimestamp());

            uassert(
                ErrorCodes::SnapshotUnavailable,
                str::stream() << "Unable to read from a snapshot due to pending collection catalog "
                                 "changes; please retry the operation. Snapshot timestamp is "
                              << mySnapshot.toString() << ". Collection minimum is "
                              << minSnapshot->toString(),
                mySnapshot.isNull() || mySnapshot >= minSnapshot.value());
        }
    }
}

void checkPlacementVersion(
    OperationContext* opCtx,
    const ResolvedNamespaceOrViewAcquisitionRequest& resolvedAcquisitionRequest) {
    const auto& nss = resolvedAcquisitionRequest.nss;

    const auto& receivedDbVersion = resolvedAcquisitionRequest.tsPlacement.dbVersion;
    if (receivedDbVersion) {
        DatabaseShardingState::assertMatchingDbVersion(opCtx, nss.db(), *receivedDbVersion);
    }

    const auto& receivedShardVersion = resolvedAcquisitionRequest.tsPlacement.shardVersion;
    if (receivedShardVersion) {
        const auto scopedCSS = CollectionShardingState::acquire(opCtx, nss);
        scopedCSS->checkShardVersionOrThrow(opCtx, *receivedShardVersion);
    }
}

ScopedCollectionOrViewAcquisition acquireResolvedCollectionOrView(
    OperationContext* opCtx,
    ResolvedNamespaceOrViewAcquisitionRequest& resolvedAcquisitionRequest) {
    const auto& nss = resolvedAcquisitionRequest.nss;

    // Check placement version before acquiring the catalog snapshot
    checkPlacementVersion(opCtx, resolvedAcquisitionRequest);

    const auto catalog = CollectionCatalog::get(opCtx);
    auto coll = catalog->lookupCollectionByNamespace(opCtx, nss);

    // Checks after having established the storage catalog snapshot
    if (resolvedAcquisitionRequest.uuid) {
        checkCollectionUUIDMismatch(opCtx, nss, coll, resolvedAcquisitionRequest.uuid);
    }

    if (coll) {
        verifyDbAndCollection(opCtx, nss, coll);
    } else if (catalog->lookupView(opCtx, nss)) {
        uassert(ErrorCodes::CommandNotSupportedOnView,
                str::stream() << "Namespace " << nss << " is a view, not a collection",
                resolvedAcquisitionRequest.viewMode ==
                    NamespaceOrViewAcquisitionRequest::ViewMode::kCanBeView);
    } else {
        uasserted(ErrorCodes::NamespaceNotFound,
                  str::stream() << "Namespace " << nss << "does not exist.");
    }

    const bool isPlacementConcernVersioned = resolvedAcquisitionRequest.tsPlacement.dbVersion ||
        resolvedAcquisitionRequest.tsPlacement.shardVersion;

    const auto scopedCSS = CollectionShardingState::acquire(opCtx, nss);
    auto collectionDescription =
        scopedCSS->getCollectionDescription(opCtx, isPlacementConcernVersioned);

    // Recheck the placement version after having acquired the catalog snapshot. If the placement
    // version still matches, then the catalog we snapshoted is consistent with the placement
    // concern too.
    checkPlacementVersion(opCtx, resolvedAcquisitionRequest);

    return ScopedCollectionOrViewAcquisition::make(opCtx,
                                                   std::move(coll),
                                                   std::move(collectionDescription),
                                                   std::move(resolvedAcquisitionRequest.dbLock),
                                                   std::move(resolvedAcquisitionRequest.collLock));
}

std::vector<ScopedCollectionOrViewAcquisition> acquireResolvedCollectionsOrViewsWithoutTakingLocks(
    OperationContext* opCtx,
    ResolvedNamespaceOrViewAcquisitionRequestsMap sortedAcquisitionRequests) {
    std::vector<ScopedCollectionOrViewAcquisition> acquisitions;
    for (auto& acquisitionRequest : sortedAcquisitionRequests) {
        auto acquisition = acquireResolvedCollectionOrView(opCtx, acquisitionRequest.second);
        acquisitions.emplace_back(std::move(acquisition));
    }

    return acquisitions;
}

}  // namespace

const NamespaceOrViewAcquisitionRequest::PlacementConcern
    NamespaceOrViewAcquisitionRequest::kPretendUnshardedDueToDirectConnection{boost::none,
                                                                              boost::none};

NamespaceOrViewAcquisitionRequest::NamespaceOrViewAcquisitionRequest(
    OperationContext* opCtx,
    NamespaceString nss,
    repl::ReadConcernArgs readConcern,
    ViewMode viewMode)
    : nss(nss),
      placementConcern(getPlacementConcernFromOSS(opCtx, nss)),
      readConcern(readConcern),
      viewMode(viewMode) {}

ScopedCollectionOrViewAcquisition::~ScopedCollectionOrViewAcquisition() {
    if (_opCtx) {
        auto& transactionResources = getOrMakeTransactionResources(_opCtx);
        transactionResources.acquiredCollections.remove_if(
            [& acquiredCollection = _acquiredCollection](
                const shard_role_details::TransactionResources::AcquiredCollection&
                    txnResourceAcquiredColl) {
                return &txnResourceAcquiredColl == &acquiredCollection;
            });
    }
}

ScopedCollectionOrViewAcquisition ScopedCollectionOrViewAcquisition::make(
    OperationContext* opCtx,
    CollectionPtr&& collectionPtr,
    ScopedCollectionDescription&& collectionDescription,
    boost::optional<Lock::DBLock>&& dbLock,
    boost::optional<Lock::CollectionLock>&& collectionLock) {
    invariant(collectionPtr);

    const auto nss = collectionPtr->ns();
    const auto uuid = collectionPtr->uuid();
    const shard_role_details::TransactionResources::AcquiredCollection& acquiredCollection =
        getOrMakeTransactionResources(opCtx).addAcquiredCollection({nss,
                                                                    uuid,
                                                                    std::move(collectionPtr),
                                                                    collectionDescription,
                                                                    std::move(dbLock),
                                                                    std::move(collectionLock)});

    return ScopedCollectionOrViewAcquisition(opCtx, std::move(acquiredCollection));
}

std::vector<ScopedCollectionOrViewAcquisition> acquireCollectionsOrViews(
    OperationContext* opCtx,
    std::initializer_list<NamespaceOrViewAcquisitionRequest> acquisitionRequests,
    LockMode mode) {
    // Optimistically populate the nss and uuid parts of the resolved acquisition requests and sort
    // them
    while (true) {
        auto sortedAcquisitionRequests =
            resolveNamespaceOrViewAcquisitionRequests(opCtx, acquisitionRequests);

        // At this point, sortedAcquisitionRequests contains fully resolved (both nss and uuid)
        // namespace or view requests in sorted order. However, there is still no guarantee that the
        // nss <-> uuid mapping won't change from underneath.
        //
        // Lock the collection locks in the sorted order and pass the resolved namespaces to
        // acquireCollectionsOrViewsWithoutTakingLocks. If it throws CollectionUUIDMismatch, we
        // need to start over.
        for (auto& ar : sortedAcquisitionRequests) {
            // TODO: SERVER-73004 When acquiring multiple collections, avoid recursively locking the
            // dbLock because that causes recursive locking of the globalLock which prevents
            // yielding.
            ar.second.dbLock.emplace(
                opCtx, ar.second.nss.db(), isSharedLockMode(mode) ? MODE_IS : MODE_IX);
            ar.second.collLock.emplace(opCtx, ar.second.nss, mode);
        }

        try {
            return acquireResolvedCollectionsOrViewsWithoutTakingLocks(
                opCtx, std::move(sortedAcquisitionRequests));
        } catch (const ExceptionFor<ErrorCodes::CollectionUUIDMismatch>&) {
            continue;
        }
    }
}

std::vector<ScopedCollectionOrViewAcquisition> acquireCollectionsOrViewsWithoutTakingLocks(
    OperationContext* opCtx,
    std::initializer_list<NamespaceOrViewAcquisitionRequest> acquisitionRequests) {
    while (true) {
        auto sortedAcquisitionRequests =
            resolveNamespaceOrViewAcquisitionRequests(opCtx, acquisitionRequests);

        try {
            return acquireResolvedCollectionsOrViewsWithoutTakingLocks(
                opCtx, std::move(sortedAcquisitionRequests));
        } catch (const ExceptionFor<ErrorCodes::CollectionUUIDMismatch>&) {
            continue;
        }
    }
}

YieldedTransactionResources yieldTransactionResources(OperationContext* opCtx);

void restoreTransactionResources(OperationContext* opCtx,
                                 YieldedTransactionResources&& yieldedResources);

}  // namespace mongo