Skip to content

Commit

Permalink
SERVER-69445 Implement the CollectionSnapshot(s) infrastructure to ba…
Browse files Browse the repository at this point in the history
…ck CollectionPtr
  • Loading branch information
jordist authored and Evergreen Agent committed Jan 19, 2023
1 parent ef120ac commit 8c9d2a0
Show file tree
Hide file tree
Showing 13 changed files with 1,495 additions and 8 deletions.
3 changes: 2 additions & 1 deletion buildscripts/linter/simplecpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ def _check_for_tracing_support(self, linenum):
def _check_for_collection_sharding_runtime(self, linenum):
line = self.clean_lines[linenum]
if _RE_COLLECTION_SHARDING_RUNTIME.search(
line) and "/src/mongo/db/s/" not in self.file_name:
line
) and "/src/mongo/db/s/" not in self.file_name and "_test.cpp" not in self.file_name:
self._error(
linenum, 'mongodb/collection_sharding_runtime', 'Illegal use of '
'CollectionShardingRuntime outside of mongo/db/s/; use CollectionShardingState '
Expand Down
4 changes: 4 additions & 0 deletions src/mongo/db/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,8 @@ env.Library(
'catalog/catalog_helper.cpp',
'catalog/collection_uuid_mismatch.cpp',
'db_raii.cpp',
'shard_role.cpp',
'transaction_resources.cpp',
],
LIBDEPS=[
'catalog/collection',
Expand Down Expand Up @@ -2577,6 +2579,7 @@ if wiredtiger:
'session/session_catalog_mongod_test.cpp',
'session/session_catalog_test.cpp',
'shard_id_test.cpp',
'shard_role_test.cpp',
'startup_warnings_mongod_test.cpp',
'thread_client_test.cpp',
'time_proof_service_test.cpp',
Expand All @@ -2599,6 +2602,7 @@ if wiredtiger:
'$BUILD_DIR/mongo/db/auth/security_token',
'$BUILD_DIR/mongo/db/catalog/catalog_test_fixture',
'$BUILD_DIR/mongo/db/catalog/collection_crud',
'$BUILD_DIR/mongo/db/catalog/collection_uuid_mismatch_info',
'$BUILD_DIR/mongo/db/catalog/database_holder',
'$BUILD_DIR/mongo/db/catalog/index_build_entry_idl',
'$BUILD_DIR/mongo/db/change_collection_expired_change_remover',
Expand Down
14 changes: 10 additions & 4 deletions src/mongo/db/catalog/catalog_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,32 @@ void assertMatchingDbVersion(OperationContext* opCtx, const StringData& dbName)
return;
}

assertMatchingDbVersion(opCtx, dbName, *receivedVersion);
}

void assertMatchingDbVersion(OperationContext* opCtx,
const StringData& dbName,
const DatabaseVersion& receivedVersion) {
{
auto scopedDss = DatabaseShardingState::acquire(opCtx, dbName, DSSAcquisitionMode::kShared);
const auto critSecSignal = scopedDss->getCriticalSectionSignal(
opCtx->lockState()->isWriteLocked() ? ShardingMigrationCriticalSection::kWrite
: ShardingMigrationCriticalSection::kRead);
uassert(
StaleDbRoutingVersion(dbName.toString(), *receivedVersion, boost::none, critSecSignal),
StaleDbRoutingVersion(dbName.toString(), receivedVersion, boost::none, critSecSignal),
str::stream() << "The critical section for the database " << dbName
<< " is acquired with reason: " << scopedDss->getCriticalSectionReason(),
!critSecSignal);
}

const auto wantedVersion = DatabaseHolder::get(opCtx)->getDbVersion(opCtx, dbName);
uassert(StaleDbRoutingVersion(dbName.toString(), *receivedVersion, boost::none),
uassert(StaleDbRoutingVersion(dbName.toString(), receivedVersion, boost::none),
str::stream() << "No cached info for the database " << dbName,
wantedVersion);

uassert(StaleDbRoutingVersion(dbName.toString(), *receivedVersion, *wantedVersion),
uassert(StaleDbRoutingVersion(dbName.toString(), receivedVersion, *wantedVersion),
str::stream() << "Version mismatch for the database " << dbName,
*receivedVersion == *wantedVersion);
receivedVersion == *wantedVersion);
}

void assertIsPrimaryShardForDb(OperationContext* opCtx, const StringData& dbName) {
Expand Down
4 changes: 4 additions & 0 deletions src/mongo/db/catalog/catalog_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ namespace mongo::catalog_helper {
*/
void assertMatchingDbVersion(OperationContext* opCtx, const StringData& dbName);

void assertMatchingDbVersion(OperationContext* opCtx,
const StringData& dbName,
const DatabaseVersion& receivedVersion);

/**
* Checks that the current shard server is the primary for the given database, throwing
* `IllegalOperation` if it is not.
Expand Down
21 changes: 18 additions & 3 deletions src/mongo/db/s/collection_sharding_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ ScopedCollectionFilter CollectionShardingRuntime::getOwnershipFilter(
auto metadata =
_getMetadataWithVersionCheckAt(opCtx,
repl::ReadConcernArgs::get(opCtx).getArgsAtClusterTime(),
optReceivedShardVersion,
supportNonVersionedOperations);

if (!supportNonVersionedOperations) {
Expand All @@ -141,13 +142,19 @@ ScopedCollectionFilter CollectionShardingRuntime::getOwnershipFilter(

ScopedCollectionDescription CollectionShardingRuntime::getCollectionDescription(
OperationContext* opCtx) const {
const bool operationIsVersioned = OperationShardingState::isComingFromRouter(opCtx);
return getCollectionDescription(opCtx, operationIsVersioned);
}

ScopedCollectionDescription CollectionShardingRuntime::getCollectionDescription(
OperationContext* opCtx, bool operationIsVersioned) const {
// If the server has been started with --shardsvr, but hasn't been added to a cluster we should
// consider all collections as unsharded
if (!ShardingState::get(opCtx)->enabled())
return {kUnshardedCollection};

// Present the collection as unsharded to internal or direct commands against shards
if (!OperationShardingState::isComingFromRouter(opCtx))
if (!operationIsVersioned)
return {kUnshardedCollection};

auto& oss = OperationShardingState::get(opCtx);
Expand Down Expand Up @@ -175,7 +182,15 @@ boost::optional<CollectionMetadata> CollectionShardingRuntime::getCurrentMetadat
}

void CollectionShardingRuntime::checkShardVersionOrThrow(OperationContext* opCtx) const {
(void)_getMetadataWithVersionCheckAt(opCtx, boost::none);
const auto optReceivedShardVersion = getOperationReceivedVersion(opCtx, _nss);
if (optReceivedShardVersion) {
checkShardVersionOrThrow(opCtx, *optReceivedShardVersion);
}
}

void CollectionShardingRuntime::checkShardVersionOrThrow(
OperationContext* opCtx, const ShardVersion& receivedShardVersion) const {
(void)_getMetadataWithVersionCheckAt(opCtx, boost::none, receivedShardVersion);
}

void CollectionShardingRuntime::enterCriticalSectionCatchUpPhase(const BSONObj& reason) {
Expand Down Expand Up @@ -399,6 +414,7 @@ std::shared_ptr<ScopedCollectionDescription::Impl>
CollectionShardingRuntime::_getMetadataWithVersionCheckAt(
OperationContext* opCtx,
const boost::optional<mongo::LogicalTime>& atClusterTime,
const boost::optional<ShardVersion>& optReceivedShardVersion,
bool supportNonVersionedOperations) const {
// If the server has been started with --shardsvr, but hasn't been added to a cluster we should
// consider all collections as unsharded
Expand All @@ -409,7 +425,6 @@ CollectionShardingRuntime::_getMetadataWithVersionCheckAt(
repl::ReadConcernLevel::kAvailableReadConcern)
return kUnshardedCollection;

const auto optReceivedShardVersion = getOperationReceivedVersion(opCtx, _nss);
if (!optReceivedShardVersion && !supportNonVersionedOperations)
return kUnshardedCollection;

Expand Down
6 changes: 6 additions & 0 deletions src/mongo/db/s/collection_sharding_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ class CollectionShardingRuntime final : public CollectionShardingState,
}

ScopedCollectionDescription getCollectionDescription(OperationContext* opCtx) const override;
ScopedCollectionDescription getCollectionDescription(OperationContext* opCtx,
bool operationIsVersioned) const override;

ScopedCollectionFilter getOwnershipFilter(OperationContext* opCtx,
OrphanCleanupPolicy orphanCleanupPolicy,
bool supportNonVersionedOperations) const override;

void checkShardVersionOrThrow(OperationContext* opCtx) const override;

void checkShardVersionOrThrow(OperationContext* opCtx,
const ShardVersion& receivedShardVersion) const override;

void appendShardVersion(BSONObjBuilder* builder) const override;

size_t numberOfRangesScheduledForDeletion() const override;
Expand Down Expand Up @@ -324,6 +329,7 @@ class CollectionShardingRuntime final : public CollectionShardingState,
std::shared_ptr<ScopedCollectionDescription::Impl> _getMetadataWithVersionCheckAt(
OperationContext* opCtx,
const boost::optional<mongo::LogicalTime>& atClusterTime,
const boost::optional<ShardVersion>& optReceivedShardVersion,
bool supportNonVersionedOperations = false) const;

/**
Expand Down
6 changes: 6 additions & 0 deletions src/mongo/db/s/collection_sharding_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class CollectionShardingState {
*/
virtual ScopedCollectionDescription getCollectionDescription(OperationContext* opCtx) const = 0;

virtual ScopedCollectionDescription getCollectionDescription(
OperationContext* opCtx, bool operationIsVersioned) const = 0;

/**
* This method must be called with an OperationShardingState, which specifies an expected shard
* version for the collection and it will invariant otherwise.
Expand Down Expand Up @@ -172,6 +175,9 @@ class CollectionShardingState {
*/
virtual void checkShardVersionOrThrow(OperationContext* opCtx) const = 0;

virtual void checkShardVersionOrThrow(OperationContext* opCtx,
const ShardVersion& receivedShardVersion) const = 0;

/**
* Appends information about the shard version of the collection.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class CollectionShardingStateStandalone final : public CollectionShardingState {
return {kUnshardedCollection};
}

ScopedCollectionDescription getCollectionDescription(OperationContext* opCtx,
bool operationIsVersioned) const override {
return {kUnshardedCollection};
}

ScopedCollectionFilter getOwnershipFilter(OperationContext*,
OrphanCleanupPolicy orphanCleanupPolicy,
bool supportNonVersionedOperations) const override {
Expand All @@ -66,6 +71,8 @@ class CollectionShardingStateStandalone final : public CollectionShardingState {

void checkShardVersionOrThrow(OperationContext*) const override {}

void checkShardVersionOrThrow(OperationContext*, const ShardVersion&) const override {}

void appendShardVersion(BSONObjBuilder* builder) const override {}

size_t numberOfRangesScheduledForDeletion() const override {
Expand Down
Loading

0 comments on commit 8c9d2a0

Please sign in to comment.