diff options
author | Tommaso Tocci <tommaso.tocci@mongodb.com> | 2023-04-24 15:46:56 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-04-24 17:10:29 +0000 |
commit | fc7d81d0f3cad9978bf69097b2a9dc3bbe3a0026 (patch) | |
tree | e736e2bf16d0a80d4b052abf9214b20858caed9e /src/mongo | |
parent | 9bdae80d0e77cd0c582185df528e4c356ebfa9bd (diff) | |
download | mongo-fc7d81d0f3cad9978bf69097b2a9dc3bbe3a0026.tar.gz |
SERVER-76396 Refactor special database constant handling
Diffstat (limited to 'src/mongo')
21 files changed, 38 insertions, 51 deletions
diff --git a/src/mongo/db/auth/authorization_session_impl.cpp b/src/mongo/db/auth/authorization_session_impl.cpp index 8e6e49300ea..5551d7e92ab 100644 --- a/src/mongo/db/auth/authorization_session_impl.cpp +++ b/src/mongo/db/auth/authorization_session_impl.cpp @@ -617,7 +617,7 @@ static int buildResourceSearchList(const ResourcePattern& target, // ResourcePattern::forAnyNormalResource. 'local' and 'config' are // used to store special system collections, which user level // administrators should not be able to manipulate. - if (target.ns().db() != "local" && target.ns().db() != "config") { + if (!target.ns().isLocalDB() && !target.ns().isConfigDB()) { resourceSearchList[size++] = ResourcePattern::forAnyNormalResource(); } resourceSearchList[size++] = ResourcePattern::forDatabaseName(target.ns().db()); @@ -636,7 +636,7 @@ static int buildResourceSearchList(const ResourcePattern& target, // All collections can be matched by a collection resource for their name resourceSearchList[size++] = ResourcePattern::forCollectionName(target.ns().coll()); } else if (target.isDatabasePattern()) { - if (target.ns().db() != "local" && target.ns().db() != "config") { + if (!target.ns().isLocalDB() && !target.ns().isConfigDB()) { resourceSearchList[size++] = ResourcePattern::forAnyNormalResource(); } } @@ -851,7 +851,7 @@ bool AuthorizationSessionImpl::isAuthorizedForAnyActionOnAnyResourceInDB(StringD // If the user is authorized for anyNormalResource, then they implicitly have access // to most databases. - if (db != "local" && db != "config" && + if (db != DatabaseName::kLocal.db() && db != DatabaseName::kConfig.db() && user->hasActionsForResource(ResourcePattern::forAnyNormalResource())) { return true; } diff --git a/src/mongo/db/auth/privilege_parser.cpp b/src/mongo/db/auth/privilege_parser.cpp index 801e731af08..552d7504102 100644 --- a/src/mongo/db/auth/privilege_parser.cpp +++ b/src/mongo/db/auth/privilege_parser.cpp @@ -110,7 +110,7 @@ bool ParsedResource::isValid(std::string* errMsg) const { // local.oplog.$main is a real collection that the server will create. But, collection // names with a '$' character are illegal. We must make an exception for this collection // here so we can grant users access to it. - if (!(getDb() == "local" && getCollection() == "oplog.$main")) { + if (!(getDb() == DatabaseName::kLocal.db() && getCollection() == "oplog.$main")) { *errMsg = stream() << getCollection() << " is not a valid collection name"; return false; } diff --git a/src/mongo/db/commands/authentication_commands.cpp b/src/mongo/db/commands/authentication_commands.cpp index f1c02c967a1..0e7ea5f6a90 100644 --- a/src/mongo/db/commands/authentication_commands.cpp +++ b/src/mongo/db/commands/authentication_commands.cpp @@ -105,8 +105,6 @@ public: void doCheckAuthorization(OperationContext*) const final {} - static constexpr auto kAdminDB = "admin"_sd; - static constexpr auto kLocalDB = "local"_sd; void typedRun(OperationContext* opCtx) { auto& logoutState = getLogoutCommandState(opCtx->getServiceContext()); auto hasBeenInvoked = logoutState.markAsInvoked(); @@ -121,14 +119,14 @@ public: as->logoutDatabase( opCtx->getClient(), dbname.toStringWithTenantId(), "Logging out on user request"); - if (getTestCommandsEnabled() && (dbname.db() == kAdminDB)) { + if (getTestCommandsEnabled() && (dbname.db() == DatabaseName::kAdmin.db())) { // Allows logging out as the internal user against the admin database, however // this actually logs out of the local database as well. This is to // support the auth passthrough test framework on mongos (since you can't use the // local database on a mongos, so you can't logout as the internal user // without this). as->logoutDatabase(opCtx->getClient(), - kLocalDB, + DatabaseName::kLocal.db(), "Logging out from local database for test purposes"); } } diff --git a/src/mongo/db/commands/dbcheck.cpp b/src/mongo/db/commands/dbcheck.cpp index 25cbbf43817..312431536a2 100644 --- a/src/mongo/db/commands/dbcheck.cpp +++ b/src/mongo/db/commands/dbcheck.cpp @@ -199,7 +199,7 @@ std::unique_ptr<DbCheckRun> fullDatabaseRun(OperationContext* opCtx, const DbCheckAllInvocation& invocation) { uassert(ErrorCodes::InvalidNamespace, "Cannot run dbCheck on local database", - dbName.db() != "local"); + dbName.db() != DatabaseName::kLocal.db()); AutoGetDb agd(opCtx, dbName, MODE_IS); uassert(ErrorCodes::NamespaceNotFound, diff --git a/src/mongo/db/namespace_string.cpp b/src/mongo/db/namespace_string.cpp index eeb77b19da3..e274e3bb7b7 100644 --- a/src/mongo/db/namespace_string.cpp +++ b/src/mongo/db/namespace_string.cpp @@ -90,9 +90,8 @@ bool NamespaceString::isCollectionlessAggregateNS() const { bool NamespaceString::isLegalClientSystemNS( const ServerGlobalParams::FeatureCompatibility& currentFCV) const { - auto dbname = dbName().db(); - if (dbname == DatabaseName::kAdmin.db()) { + if (isAdminDB()) { if (coll() == "system.roles") return true; if (coll() == kServerConfigurationNamespace.coll()) @@ -101,7 +100,7 @@ bool NamespaceString::isLegalClientSystemNS( return true; if (coll() == "system.backup_users") return true; - } else if (dbname == DatabaseName::kConfig.db()) { + } else if (isConfigDB()) { if (coll() == "system.sessions") return true; if (coll() == kIndexBuildEntryNamespace.coll()) @@ -112,7 +111,7 @@ bool NamespaceString::isLegalClientSystemNS( return true; if (coll() == kConfigsvrCoordinatorsNamespace.coll()) return true; - } else if (dbname == DatabaseName::kLocal.db()) { + } else if (isLocalDB()) { if (coll() == kSystemReplSetNamespace.coll()) return true; if (coll() == kLocalHealthLogNamespace.coll()) @@ -355,11 +354,11 @@ StatusWith<repl::OpTime> NamespaceString::getDropPendingNamespaceOpTime() const bool NamespaceString::isNamespaceAlwaysUnsharded() const { // Local and admin never have sharded collections - if (db() == DatabaseName::kLocal.db() || db() == DatabaseName::kAdmin.db()) + if (isLocalDB() || isAdminDB()) return true; // Config can only have the system.sessions as sharded - if (db() == DatabaseName::kConfig.db()) + if (isConfigDB()) return *this != NamespaceString::kLogicalSessionsNamespace; if (isSystem()) { @@ -393,11 +392,11 @@ bool NamespaceString::isTimeseriesBucketsCollection() const { } bool NamespaceString::isChangeStreamPreImagesCollection() const { - return _dbName.db() == DatabaseName::kConfig.db() && coll() == kPreImagesCollectionName; + return isConfigDB() && coll() == kPreImagesCollectionName; } bool NamespaceString::isChangeCollection() const { - return _dbName.db() == DatabaseName::kConfig.db() && coll() == kChangeCollectionName; + return isConfigDB() && coll() == kChangeCollectionName; } bool NamespaceString::isConfigImagesCollection() const { @@ -443,7 +442,7 @@ bool NamespaceString::isImplicitlyReplicated() const { } bool NamespaceString::isReplicated() const { - if (isLocal()) { + if (isLocalDB()) { return false; } diff --git a/src/mongo/db/namespace_string.h b/src/mongo/db/namespace_string.h index 7ff360eee42..026f6f941ed 100644 --- a/src/mongo/db/namespace_string.h +++ b/src/mongo/db/namespace_string.h @@ -165,7 +165,6 @@ public: // Prefix for orphan collections static constexpr StringData kOrphanCollectionPrefix = "orphan."_sd; - static constexpr StringData kOrphanCollectionDb = "local"_sd; // Prefix for collections that store the local resharding oplog buffer. static constexpr StringData kReshardingLocalOplogBufferPrefix = @@ -468,13 +467,13 @@ public: // bool isHealthlog() const { - return isLocal() && coll() == "system.healthlog"; + return isLocalDB() && coll() == "system.healthlog"; } bool isSystem() const { return coll().startsWith("system."); } bool isNormalCollection() const { - return !isSystem() && !(isLocal() && coll().startsWith("replset.")); + return !isSystem() && !(isLocalDB() && coll().startsWith("replset.")); } bool isGlobalIndex() const { return coll().startsWith(kGlobalIndexCollectionPrefix); @@ -482,7 +481,7 @@ public: bool isAdminDB() const { return db() == DatabaseName::kAdmin.db(); } - bool isLocal() const { + bool isLocalDB() const { return db() == DatabaseName::kLocal.db(); } bool isSystemDotProfile() const { @@ -499,7 +498,7 @@ public: return coll() == kSystemDotJavascriptCollectionName; } bool isServerConfigurationCollection() const { - return (db() == DatabaseName::kAdmin.db()) && (coll() == "system.version"); + return isAdminDB() && (coll() == "system.version"); } bool isPrivilegeCollection() const { if (!isAdminDB()) { @@ -517,17 +516,11 @@ public: return oplog(_ns); } bool isOnInternalDb() const { - if (db() == DatabaseName::kAdmin.db()) - return true; - if (db() == DatabaseName::kLocal.db()) - return true; - if (db() == DatabaseName::kConfig.db()) - return true; - return false; + return isAdminDB() || isLocalDB() || isConfigDB(); } bool isOrphanCollection() const { - return db() == kOrphanCollectionDb && coll().startsWith(kOrphanCollectionPrefix); + return isLocalDB() && coll().startsWith(kOrphanCollectionPrefix); } /** diff --git a/src/mongo/db/op_observer/op_observer_impl.cpp b/src/mongo/db/op_observer/op_observer_impl.cpp index fd45c05c412..aace82b6511 100644 --- a/src/mongo/db/op_observer/op_observer_impl.cpp +++ b/src/mongo/db/op_observer/op_observer_impl.cpp @@ -312,7 +312,7 @@ bool shouldTimestampIndexBuildSinglePhase(OperationContext* opCtx, const Namespa return false; // 3. If the index build is on the local database, do not timestamp. - if (nss.isLocal()) + if (nss.isLocalDB()) return false; // 4. All other cases, we generate a timestamp by writing a no-op oplog entry. This is diff --git a/src/mongo/db/ops/write_ops_exec_util.cpp b/src/mongo/db/ops/write_ops_exec_util.cpp index 2f1592f77f1..4facc775a60 100644 --- a/src/mongo/db/ops/write_ops_exec_util.cpp +++ b/src/mongo/db/ops/write_ops_exec_util.cpp @@ -56,7 +56,7 @@ LastOpFixer::~LastOpFixer() { void LastOpFixer::startingOp(const NamespaceString& ns) { // Operations on the local DB aren't replicated, so they don't need to bump the lastOp. - _needToFixLastOp = !ns.isLocal(); + _needToFixLastOp = !ns.isLocalDB(); _opTimeAtLastOpStart = replClientInfo().getLastOp(); } diff --git a/src/mongo/db/pipeline/document_source_change_stream.cpp b/src/mongo/db/pipeline/document_source_change_stream.cpp index 0f17c1be40c..55f2443d25d 100644 --- a/src/mongo/db/pipeline/document_source_change_stream.cpp +++ b/src/mongo/db/pipeline/document_source_change_stream.cpp @@ -367,7 +367,7 @@ void DocumentSourceChangeStream::assertIsLegalSpecification( // 'admin' database iff 'allChangesForCluster' is true. A stream may run against the 'config' // database iff 'allowToRunOnConfigDB' is true. const bool isNotBannedInternalDB = - !expCtx->ns.isLocal() && (!expCtx->ns.isConfigDB() || spec.getAllowToRunOnConfigDB()); + !expCtx->ns.isLocalDB() && (!expCtx->ns.isConfigDB() || spec.getAllowToRunOnConfigDB()); uassert(ErrorCodes::InvalidNamespace, str::stream() << "$changeStream may not be opened on the internal " << expCtx->ns.dbName().toStringForErrorMsg() << " database", diff --git a/src/mongo/db/pipeline/sharded_agg_helpers.cpp b/src/mongo/db/pipeline/sharded_agg_helpers.cpp index 8a7f835c05e..bc045deb2f8 100644 --- a/src/mongo/db/pipeline/sharded_agg_helpers.cpp +++ b/src/mongo/db/pipeline/sharded_agg_helpers.cpp @@ -1594,7 +1594,7 @@ std::unique_ptr<Pipeline, PipelineDeleter> attachCursorToPipeline( // these namespaces, a local cursor should always be used. // TODO SERVER-59957: use NamespaceString::isPerShardNamespace instead. auto shouldAlwaysAttachLocalCursorForNamespace = [](const NamespaceString& ns) { - return (ns.isLocal() || ns.isConfigDotCacheDotChunks() || + return (ns.isLocalDB() || ns.isConfigDotCacheDotChunks() || ns.isReshardingLocalOplogBufferCollection() || ns == NamespaceString::kConfigImagesNamespace || ns.isChangeStreamPreImagesCollection()); diff --git a/src/mongo/db/repl/all_database_cloner.cpp b/src/mongo/db/repl/all_database_cloner.cpp index 7a5bb8c5093..a9831451875 100644 --- a/src/mongo/db/repl/all_database_cloner.cpp +++ b/src/mongo/db/repl/all_database_cloner.cpp @@ -173,7 +173,7 @@ BaseCloner::AfterStageBehavior AllDatabaseCloner::listDatabasesStage() { : boost::none; DatabaseName dbName = DatabaseNameUtil::deserialize(tenantId, dbBSON["name"].str()); - if (dbName.db() == "local") { + if (dbName.db() == DatabaseName::kLocal.db()) { LOGV2_DEBUG(21056, 1, "Excluding database from the 'listDatabases' response: {db}", diff --git a/src/mongo/db/repl/idempotency_test_fixture.cpp b/src/mongo/db/repl/idempotency_test_fixture.cpp index e14db65a6ca..b4a528213b7 100644 --- a/src/mongo/db/repl/idempotency_test_fixture.cpp +++ b/src/mongo/db/repl/idempotency_test_fixture.cpp @@ -360,7 +360,7 @@ std::vector<CollectionState> IdempotencyTest::validateAllCollections() { auto dbNames = catalog->getAllDbNames(); for (auto& dbName : dbNames) { // Skip local database. - if (dbName.db() != "local") { + if (dbName.db() != DatabaseName::kLocal.db()) { std::vector<NamespaceString> collectionNames; { Lock::DBLock lk(_opCtx.get(), dbName, MODE_S); diff --git a/src/mongo/db/repl/repl_set_commands.cpp b/src/mongo/db/repl/repl_set_commands.cpp index 0392c393141..7b229f858dd 100644 --- a/src/mongo/db/repl/repl_set_commands.cpp +++ b/src/mongo/db/repl/repl_set_commands.cpp @@ -733,7 +733,7 @@ bool replHasDatabases(OperationContext* opCtx) { if (dbNames.size() >= 2) return true; if (dbNames.size() == 1) { - if (dbNames[0].db() != "local") + if (dbNames[0].db() != DatabaseName::kLocal.db()) return true; // we have a local database. return true if oplog isn't empty diff --git a/src/mongo/db/repl/replication_coordinator.cpp b/src/mongo/db/repl/replication_coordinator.cpp index bfccf8a98be..57ed080cdb8 100644 --- a/src/mongo/db/repl/replication_coordinator.cpp +++ b/src/mongo/db/repl/replication_coordinator.cpp @@ -90,7 +90,7 @@ bool ReplicationCoordinator::isOplogDisabledFor(OperationContext* opCtx, } bool ReplicationCoordinator::isOplogDisabledForNS(const NamespaceString& nss) { - if (nss.isLocal()) { + if (nss.isLocalDB()) { return true; } diff --git a/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp b/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp index 25a95706c1f..c15ed1b3702 100644 --- a/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp +++ b/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp @@ -131,8 +131,6 @@ namespace mongo { namespace repl { namespace { -const char kLocalDbName[] = "local"; - MONGO_FAIL_POINT_DEFINE(dropPendingCollectionReaperHang); // The count of items in the buffer diff --git a/src/mongo/db/repl/replication_coordinator_impl.cpp b/src/mongo/db/repl/replication_coordinator_impl.cpp index c7e68ecb84a..b0396fd61af 100644 --- a/src/mongo/db/repl/replication_coordinator_impl.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl.cpp @@ -188,7 +188,6 @@ using NextAction = Fetcher::NextAction; namespace { -const char kLocalDB[] = "local"; void lockAndCall(stdx::unique_lock<Latch>* lk, const std::function<void()>& fn) { if (!lk->owns_lock()) { @@ -3020,7 +3019,7 @@ bool ReplicationCoordinatorImpl::canAcceptWritesForDatabase_UNSAFE(OperationCont if (_readWriteAbility->canAcceptNonLocalWrites_UNSAFE() || alwaysAllowNonLocalWrites(opCtx)) { return true; } - if (dbName == kLocalDB) { + if (dbName == DatabaseName::kLocal.db()) { return true; } return false; @@ -3048,7 +3047,7 @@ bool isSystemDotProfile(OperationContext* opCtx, const NamespaceStringOrUUID& ns bool ReplicationCoordinatorImpl::canAcceptWritesFor(OperationContext* opCtx, const NamespaceStringOrUUID& nsOrUUID) { - if (!isReplEnabled() || nsOrUUID.db() == kLocalDB) { + if (!isReplEnabled() || nsOrUUID.db() == DatabaseName::kLocal.db()) { // Writes on stand-alone nodes or "local" database are always permitted. return true; } @@ -3132,7 +3131,7 @@ Status ReplicationCoordinatorImpl::checkCanServeReadsFor_UNSAFE(OperationContext // Non-oplog local reads from the user are not allowed during initial sync when the initial // sync method disallows it. "isFromUserConnection" means DBDirectClient reads are not blocked; // "isInternalClient" means reads from other cluster members are not blocked. - if (!isPrimaryOrSecondary && getReplicationMode() == modeReplSet && ns.db() == kLocalDB && + if (!isPrimaryOrSecondary && getReplicationMode() == modeReplSet && ns.isLocalDB() && client->isFromUserConnection()) { stdx::lock_guard<Latch> lock(_mutex); auto isInternalClient = !client->session() || diff --git a/src/mongo/db/repl/replication_coordinator_mock.cpp b/src/mongo/db/repl/replication_coordinator_mock.cpp index 2352767e596..e7b818bd8fe 100644 --- a/src/mongo/db/repl/replication_coordinator_mock.cpp +++ b/src/mongo/db/repl/replication_coordinator_mock.cpp @@ -198,7 +198,7 @@ bool ReplicationCoordinatorMock::canAcceptWritesForDatabase(OperationContext* op if (_alwaysAllowWrites) { return true; } - return dbName == "local" || _memberState.primary(); + return dbName == DatabaseName::kLocal.db() || _memberState.primary(); } bool ReplicationCoordinatorMock::canAcceptWritesForDatabase_UNSAFE(OperationContext* opCtx, diff --git a/src/mongo/db/repl/storage_interface_impl.cpp b/src/mongo/db/repl/storage_interface_impl.cpp index 4cf1fdb421b..e544b8b4aa4 100644 --- a/src/mongo/db/repl/storage_interface_impl.cpp +++ b/src/mongo/db/repl/storage_interface_impl.cpp @@ -394,7 +394,7 @@ Status StorageInterfaceImpl::dropReplicatedDatabases(OperationContext* opCtx) { auto databaseHolder = DatabaseHolder::get(opCtx); auto hasLocalDatabase = false; for (const auto& dbName : dbNames) { - if (dbName.db() == "local") { + if (dbName.db() == DatabaseName::kLocal.db()) { hasLocalDatabase = true; continue; } diff --git a/src/mongo/db/s/move_primary/move_primary_recipient_service.cpp b/src/mongo/db/s/move_primary/move_primary_recipient_service.cpp index 0b5f78054d9..8879b3719e3 100644 --- a/src/mongo/db/s/move_primary/move_primary_recipient_service.cpp +++ b/src/mongo/db/s/move_primary/move_primary_recipient_service.cpp @@ -898,7 +898,7 @@ repl::OpTime MovePrimaryRecipientService::MovePrimaryRecipient::_getStartApplyin auto rawResp = _movePrimaryRecipientExternalState->sendCommandToShards( opCtx, - "local"_sd, + DatabaseName::kLocal.db(), findCmd.toBSON({}), {ShardId(_metadata.getFromShardName().toString())}, **executor); diff --git a/src/mongo/db/storage/durable_catalog_impl.cpp b/src/mongo/db/storage/durable_catalog_impl.cpp index a94471106c0..148cb461321 100644 --- a/src/mongo/db/storage/durable_catalog_impl.cpp +++ b/src/mongo/db/storage/durable_catalog_impl.cpp @@ -631,8 +631,8 @@ StatusWith<std::string> DurableCatalogImpl::newOrphanedIdent( // The collection will be named local.orphan.xxxxx. std::string identNs = ident; std::replace(identNs.begin(), identNs.end(), '-', '_'); - NamespaceString nss(NamespaceString(NamespaceString::kOrphanCollectionDb, - NamespaceString::kOrphanCollectionPrefix + identNs)); + NamespaceString nss{DatabaseName::kLocal.db(), + NamespaceString::kOrphanCollectionPrefix + identNs}; BSONObj obj; { diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp index 212b6dda382..c3999d0e07e 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp @@ -969,7 +969,7 @@ bool WiredTigerUtil::useTableLogging(const NamespaceString& nss) { invariant(nss.size() > 0); // Of the replica set configurations: - if (!nss.isLocal()) { + if (!nss.isLocalDB()) { // All replicated collections are not logged. return false; } |