From a2c7f46e2cc57b25d0d334376b901ed9f3988071 Mon Sep 17 00:00:00 2001 From: Kshitij Gupta Date: Fri, 16 Dec 2022 20:18:48 +0000 Subject: SERVER-71959: Modify usages of ClusterRole:: --- src/mongo/db/cluster_role.cpp | 13 +++++++++++++ src/mongo/db/cluster_role.h | 9 +++++++++ src/mongo/db/commands/feature_compatibility_version.cpp | 2 +- .../internal_rename_if_options_and_indexes_match_cmd.cpp | 3 ++- src/mongo/db/commands/parameters.cpp | 3 +-- .../read_write_concern_defaults_server_status.cpp | 2 +- src/mongo/db/ftdc/ftdc_mongod.cpp | 2 +- src/mongo/db/s/analyze_shard_key_cmd.cpp | 3 ++- src/mongo/db/s/analyze_shard_key_cmd_util.cpp | 2 +- src/mongo/db/s/configure_query_analyzer_cmd.cpp | 2 +- src/mongo/db/s/sharding_state.cpp | 12 +++++++----- .../user_writes_recoverable_critical_section_service.cpp | 3 ++- src/mongo/db/server_options_test.cpp | 16 ++++++++++++++++ src/mongo/db/service_entry_point_common.cpp | 6 +++--- src/mongo/db/write_concern.cpp | 5 ++--- 15 files changed, 62 insertions(+), 21 deletions(-) (limited to 'src/mongo') diff --git a/src/mongo/db/cluster_role.cpp b/src/mongo/db/cluster_role.cpp index 108eff0b325..370207f7325 100644 --- a/src/mongo/db/cluster_role.cpp +++ b/src/mongo/db/cluster_role.cpp @@ -41,4 +41,17 @@ bool ClusterRole::operator==(const ClusterRole& other) const { return _value == other._value; } + +bool ClusterRole::isShardRole() { + return _value == ClusterRole::ShardServer || + (gFeatureFlagCatalogShard.isEnabledAndIgnoreFCV() && _value == ClusterRole::ConfigServer); +} + +bool ClusterRole::isExclusivelyShardRole() { + return _value == ClusterRole::ShardServer; +} + +bool ClusterRole::isExclusivelyConfigSvrRole() { + return _value == ClusterRole::ConfigServer && !gFeatureFlagCatalogShard.isEnabledAndIgnoreFCV(); +} } // namespace mongo diff --git a/src/mongo/db/cluster_role.h b/src/mongo/db/cluster_role.h index a947d0cae9a..c5fde6472f5 100644 --- a/src/mongo/db/cluster_role.h +++ b/src/mongo/db/cluster_role.h @@ -57,6 +57,15 @@ public: return !ClusterRole::operator==(other); } + // Returns true if this mongod was started with --shardsvr or --configsvr in a catalog shard + // topology, false otherwise. + bool isShardRole(); + // Returns true if this mongod was started with --shardsvr, false otherwise. + bool isExclusivelyShardRole(); + // Returns true if this mongod was started with --configServer in a non-catalog shard topology, + // false otherwise. + bool isExclusivelyConfigSvrRole(); + private: Value _value; }; diff --git a/src/mongo/db/commands/feature_compatibility_version.cpp b/src/mongo/db/commands/feature_compatibility_version.cpp index c47fc23b532..dfa4471bacf 100644 --- a/src/mongo/db/commands/feature_compatibility_version.cpp +++ b/src/mongo/db/commands/feature_compatibility_version.cpp @@ -366,7 +366,7 @@ void FeatureCompatibilityVersion::setIfCleanStartup(OperationContext* opCtx, // featureCompatibilityVersion is the downgrade version, so that it can be safely added to a // downgrade version cluster. The config server will run setFeatureCompatibilityVersion as part // of addShard. - const bool storeUpgradeVersion = serverGlobalParams.clusterRole != ClusterRole::ShardServer; + const bool storeUpgradeVersion = !serverGlobalParams.clusterRole.isExclusivelyShardRole(); UnreplicatedWritesBlock unreplicatedWritesBlock(opCtx); NamespaceString nss(NamespaceString::kServerConfigurationNamespace); diff --git a/src/mongo/db/commands/internal_rename_if_options_and_indexes_match_cmd.cpp b/src/mongo/db/commands/internal_rename_if_options_and_indexes_match_cmd.cpp index 5ef20f823f0..b9121334588 100644 --- a/src/mongo/db/commands/internal_rename_if_options_and_indexes_match_cmd.cpp +++ b/src/mongo/db/commands/internal_rename_if_options_and_indexes_match_cmd.cpp @@ -70,7 +70,8 @@ public: std::list(originalIndexes.begin(), originalIndexes.end()); const auto& collectionOptions = thisRequest.getCollectionOptions(); - if (serverGlobalParams.clusterRole != ClusterRole::ShardServer) { + if (serverGlobalParams.clusterRole == ClusterRole::None || + serverGlobalParams.clusterRole.isExclusivelyConfigSvrRole()) { // No need to acquire additional locks in a non-sharded environment _internalRun(opCtx, fromNss, toNss, indexList, collectionOptions); return; diff --git a/src/mongo/db/commands/parameters.cpp b/src/mongo/db/commands/parameters.cpp index ba905af0848..b906f79b1fd 100644 --- a/src/mongo/db/commands/parameters.cpp +++ b/src/mongo/db/commands/parameters.cpp @@ -389,8 +389,7 @@ public: str::stream() << "Cannot set parameter requireApiVersion=true on a shard or config server", parameterName != "requireApiVersion" || !parameter.trueValue() || - (serverGlobalParams.clusterRole != ClusterRole::ConfigServer && - serverGlobalParams.clusterRole != ClusterRole::ShardServer)); + (serverGlobalParams.clusterRole == ClusterRole::None)); auto oldValueObj = ([&] { BSONObjBuilder bb; diff --git a/src/mongo/db/commands/read_write_concern_defaults_server_status.cpp b/src/mongo/db/commands/read_write_concern_defaults_server_status.cpp index 16f3d6d0235..459bd2532b0 100644 --- a/src/mongo/db/commands/read_write_concern_defaults_server_status.cpp +++ b/src/mongo/db/commands/read_write_concern_defaults_server_status.cpp @@ -42,7 +42,7 @@ public: ReadWriteConcernDefaultsServerStatus() : ServerStatusSection("defaultRWConcern") {} bool includeByDefault() const override { - return serverGlobalParams.clusterRole != ClusterRole::ShardServer; + return !serverGlobalParams.clusterRole.isExclusivelyShardRole(); } BSONObj generateSection(OperationContext* opCtx, diff --git a/src/mongo/db/ftdc/ftdc_mongod.cpp b/src/mongo/db/ftdc/ftdc_mongod.cpp index 20a435ecb68..7b4b4468d25 100644 --- a/src/mongo/db/ftdc/ftdc_mongod.cpp +++ b/src/mongo/db/ftdc/ftdc_mongod.cpp @@ -124,7 +124,7 @@ void registerMongoDCollectors(FTDCController* controller) { << BSON_ARRAY(BSON("$collStats" << BSON( "storageStats" << BSON( "waitForLock" << false << "numericOnly" << true))))))); - if (serverGlobalParams.clusterRole != ClusterRole::ShardServer) { + if (!serverGlobalParams.clusterRole.isExclusivelyShardRole()) { // GetDefaultRWConcern controller->addOnRotateCollector(std::make_unique( "getDefaultRWConcern", diff --git a/src/mongo/db/s/analyze_shard_key_cmd.cpp b/src/mongo/db/s/analyze_shard_key_cmd.cpp index 79764cc01cc..1ad0442dd27 100644 --- a/src/mongo/db/s/analyze_shard_key_cmd.cpp +++ b/src/mongo/db/s/analyze_shard_key_cmd.cpp @@ -70,7 +70,8 @@ public: Response typedRun(OperationContext* opCtx) { uassert(ErrorCodes::IllegalOperation, "analyzeShardKey command is not supported on a configsvr mongod", - serverGlobalParams.clusterRole != ClusterRole::ConfigServer); + serverGlobalParams.clusterRole == ClusterRole::None || + serverGlobalParams.clusterRole.isShardRole()); const auto& nss = ns(); const auto& key = request().getKey(); diff --git a/src/mongo/db/s/analyze_shard_key_cmd_util.cpp b/src/mongo/db/s/analyze_shard_key_cmd_util.cpp index e7a46b9021a..b714d85817e 100644 --- a/src/mongo/db/s/analyze_shard_key_cmd_util.cpp +++ b/src/mongo/db/s/analyze_shard_key_cmd_util.cpp @@ -416,7 +416,7 @@ MonotonicityTypeEnum calculateMonotonicity(OperationContext* opCtx, */ boost::optional getNumOrphanDocuments(OperationContext* opCtx, const NamespaceString& nss) { - if (serverGlobalParams.clusterRole != ClusterRole::ShardServer) { + if (!serverGlobalParams.clusterRole.isShardRole()) { return boost::none; } diff --git a/src/mongo/db/s/configure_query_analyzer_cmd.cpp b/src/mongo/db/s/configure_query_analyzer_cmd.cpp index 73f8441c1db..5bcd779f1c2 100644 --- a/src/mongo/db/s/configure_query_analyzer_cmd.cpp +++ b/src/mongo/db/s/configure_query_analyzer_cmd.cpp @@ -61,7 +61,7 @@ public: Response typedRun(OperationContext* opCtx) { uassert(ErrorCodes::IllegalOperation, "configQueryAnalyzer command is not supported on a shardsvr mongod", - serverGlobalParams.clusterRole != ClusterRole::ShardServer); + !serverGlobalParams.clusterRole.isExclusivelyShardRole()); const auto& nss = ns(); const auto mode = request().getMode(); diff --git a/src/mongo/db/s/sharding_state.cpp b/src/mongo/db/s/sharding_state.cpp index 920ecb37f61..7ffcbcd2ff2 100644 --- a/src/mongo/db/s/sharding_state.cpp +++ b/src/mongo/db/s/sharding_state.cpp @@ -96,16 +96,18 @@ bool ShardingState::enabled() const { } Status ShardingState::canAcceptShardedCommands() const { - if (serverGlobalParams.clusterRole != ClusterRole::ShardServer) { + if (!serverGlobalParams.clusterRole.isShardRole()) { return {ErrorCodes::NoShardingEnabled, - "Cannot accept sharding commands if not started with --shardsvr"}; - } else if (!enabled()) { + "Cannot accept sharding commands if node does not have shard role"}; + } + + if (!enabled()) { return {ErrorCodes::ShardingStateNotInitialized, "Cannot accept sharding commands if sharding state has not " "been initialized with a shardIdentity document"}; - } else { - return Status::OK(); } + + return Status::OK(); } ShardId ShardingState::shardId() { diff --git a/src/mongo/db/s/user_writes_recoverable_critical_section_service.cpp b/src/mongo/db/s/user_writes_recoverable_critical_section_service.cpp index be1cc7c2375..798671a3f3d 100644 --- a/src/mongo/db/s/user_writes_recoverable_critical_section_service.cpp +++ b/src/mongo/db/s/user_writes_recoverable_critical_section_service.cpp @@ -172,7 +172,8 @@ const ReplicaSetAwareServiceRegistry::Registerer ExecCommandDatabase::_commandExec() { auto opCtx = _execContext->getOpCtx(); if (!opCtx->getClient()->isInDirectClient() && - serverGlobalParams.clusterRole != ClusterRole::ConfigServer && + !serverGlobalParams.clusterRole.isExclusivelyConfigSvrRole() && !_refreshedDatabase) { auto sce = s.extraInfo(); invariant(sce); @@ -1822,7 +1822,7 @@ Future ExecCommandDatabase::_commandExec() { ShardingStatistics::get(opCtx).countStaleConfigErrors.addAndFetch(1); if (!opCtx->getClient()->isInDirectClient() && - serverGlobalParams.clusterRole != ClusterRole::ConfigServer && + !serverGlobalParams.clusterRole.isExclusivelyConfigSvrRole() && !_refreshedCollection) { if (auto sce = s.extraInfo()) { bool inCriticalSection = sce->getCriticalSectionSignal().has_value(); @@ -1870,7 +1870,7 @@ Future ExecCommandDatabase::_commandExec() { .onError([this](Status s) -> Future { // This exception can never happen on the config server. Config servers can't receive // SSV either, because they never have commands with shardVersion sent. - invariant(serverGlobalParams.clusterRole != ClusterRole::ConfigServer); + invariant(!serverGlobalParams.clusterRole.isExclusivelyConfigSvrRole()); auto opCtx = _execContext->getOpCtx(); if (!opCtx->getClient()->isInDirectClient() && !_refreshedCatalogCache) { diff --git a/src/mongo/db/write_concern.cpp b/src/mongo/db/write_concern.cpp index f6caaad89d0..0c6f9f252f7 100644 --- a/src/mongo/db/write_concern.cpp +++ b/src/mongo/db/write_concern.cpp @@ -96,8 +96,7 @@ StatusWith extractWriteConcern(OperationContext* opCtx, writeConcern = ([&]() { // WriteConcern defaults can only be applied on regular replica set members. Operations // received by shard and config servers should always have WC explicitly specified. - if (serverGlobalParams.clusterRole != ClusterRole::ShardServer && - serverGlobalParams.clusterRole != ClusterRole::ConfigServer && + if (serverGlobalParams.clusterRole == ClusterRole::None && repl::ReplicationCoordinator::get(opCtx)->isReplEnabled() && (!opCtx->inMultiDocumentTransaction() || isTransactionCommand(cmdObj.firstElementFieldName())) && @@ -143,7 +142,7 @@ StatusWith extractWriteConcern(OperationContext* opCtx, } if (!clientSuppliedWriteConcern && - serverGlobalParams.clusterRole == ClusterRole::ConfigServer && + serverGlobalParams.clusterRole.isExclusivelyConfigSvrRole() && !opCtx->getClient()->isInDirectClient() && (opCtx->getClient()->session() && (opCtx->getClient()->session()->getTags() & transport::Session::kInternalClient)) && -- cgit v1.2.1