summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllison Easton <allison.easton@mongodb.com>2023-04-03 08:01:28 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-04-03 09:12:56 +0000
commitaf10b407ed0ddcc50395e046a296551ef3ba8bb2 (patch)
tree248272accd9338e29604917a9fa4f490fd2fb69f
parent3845fac4c113bf576f07ae406043b58e5c47f81f (diff)
downloadmongo-af10b407ed0ddcc50395e046a296551ef3ba8bb2.tar.gz
SERVER-75368 Set cluster parameter during FCV upgrade
-rw-r--r--jstests/sharding/check_for_direct_shard_ops_upgrade_downgrade.js49
-rw-r--r--src/mongo/db/commands/set_feature_compatibility_version_command.cpp45
2 files changed, 94 insertions, 0 deletions
diff --git a/jstests/sharding/check_for_direct_shard_ops_upgrade_downgrade.js b/jstests/sharding/check_for_direct_shard_ops_upgrade_downgrade.js
new file mode 100644
index 00000000000..36de3f7f1a7
--- /dev/null
+++ b/jstests/sharding/check_for_direct_shard_ops_upgrade_downgrade.js
@@ -0,0 +1,49 @@
+/**
+ * Tests that the cluster parameter "shardedClusterCardinalityForDirectConns" has the correct value
+ * after upgrade and downgrade.
+ *
+ * @tags: [multiversion_incompatible, featureFlagCheckForDirectShardOperations,
+ * temporary_catalog_shard_incompatible]
+ */
+
+(function() {
+'use strict';
+
+load("jstests/sharding/libs/remove_shard_util.js");
+
+const st = new ShardingTest({shards: 1, useHostname: false});
+
+const additionalShard = new ReplSetTest({name: "additionalShard", host: 'localhost', nodes: 1});
+additionalShard.startSet({shardsvr: ""});
+additionalShard.initiate();
+
+let checkClusterParameter = function(conn, expectedValue) {
+ let resp = assert.commandWorked(
+ conn.adminCommand({getClusterParameter: "shardedClusterCardinalityForDirectConns"}));
+ assert.eq(resp.clusterParameters[0].hasTwoOrMoreShards, expectedValue);
+};
+
+// There is only one shard in the cluster, so the cluster parameter should be false
+checkClusterParameter(st.configRS.getPrimary(), false);
+checkClusterParameter(st.shard0, false);
+
+assert.commandWorked(st.s.adminCommand({setFeatureCompatibilityVersion: lastLTSFCV}));
+assert.commandWorked(st.s.adminCommand({addShard: additionalShard.getURL(), name: "shard02"}));
+assert.commandWorked(st.s.adminCommand({setFeatureCompatibilityVersion: latestFCV}));
+
+// There are two shards in the cluster while upgrading, so the cluster parameter should be true
+checkClusterParameter(st.configRS.getPrimary(), true);
+checkClusterParameter(st.shard0, true);
+checkClusterParameter(additionalShard.getPrimary(), true);
+
+assert.commandWorked(st.s.adminCommand({setFeatureCompatibilityVersion: lastLTSFCV}));
+removeShard(st, "shard02");
+assert.commandWorked(st.s.adminCommand({setFeatureCompatibilityVersion: latestFCV}));
+
+// There is one shard in the cluster while upgrading, so the cluster parameter should be false
+checkClusterParameter(st.configRS.getPrimary(), false);
+checkClusterParameter(st.shard0, false);
+
+additionalShard.stopSet();
+st.stop();
+})();
diff --git a/src/mongo/db/commands/set_feature_compatibility_version_command.cpp b/src/mongo/db/commands/set_feature_compatibility_version_command.cpp
index b7b4b8a38c9..c8c7a2a33ef 100644
--- a/src/mongo/db/commands/set_feature_compatibility_version_command.cpp
+++ b/src/mongo/db/commands/set_feature_compatibility_version_command.cpp
@@ -74,6 +74,7 @@
#include "mongo/db/s/resharding/resharding_coordinator_service.h"
#include "mongo/db/s/resharding/resharding_donor_recipient_common.h"
#include "mongo/db/s/shard_authoritative_catalog_gen.h"
+#include "mongo/db/s/sharding_cluster_parameters_gen.h"
#include "mongo/db/s/sharding_ddl_coordinator_service.h"
#include "mongo/db/s/sharding_index_catalog_ddl_util.h"
#include "mongo/db/s/sharding_state.h"
@@ -91,6 +92,8 @@
#include "mongo/rpc/get_status_from_command_result.h"
#include "mongo/s/catalog/type_config_version.h"
#include "mongo/s/catalog/type_index_catalog.h"
+#include "mongo/s/grid.h"
+#include "mongo/s/request_types/sharded_ddl_commands_gen.h"
#include "mongo/s/sharding_feature_flags_gen.h"
#include "mongo/stdx/unordered_set.h"
#include "mongo/util/exit.h"
@@ -690,6 +693,7 @@ private:
// Depends on _setOnCurrentShardSinceFieldOnChunks()
_initializePlacementHistory(opCtx, requestedVersion, actualVersion);
_dropConfigMigrationsCollection(opCtx);
+ _setShardedClusterCardinalityParam(opCtx, requestedVersion);
}
_removeRecordPreImagesCollectionOption(opCtx);
@@ -804,6 +808,47 @@ private:
}
}
+ void _setShardedClusterCardinalityParam(
+ OperationContext* opCtx, const multiversion::FeatureCompatibilityVersion requestedVersion) {
+ if (feature_flags::gCheckForDirectShardOperations.isEnabledOnVersion(requestedVersion)) {
+ // Get current cluster parameter value so that we don't run SetClusterParameter
+ // extraneously
+ auto* clusterParameters = ServerParameterSet::getClusterParameterSet();
+ auto* clusterCardinalityParam =
+ clusterParameters->get<ClusterParameterWithStorage<ShardedClusterCardinalityParam>>(
+ "shardedClusterCardinalityForDirectConns");
+ auto currentValue =
+ clusterCardinalityParam->getValue(boost::none).getHasTwoOrMoreShards();
+
+ // config.shards is stable during FCV changes, so query that to discover the current
+ // number of shards.
+ DBDirectClient client(opCtx);
+ FindCommandRequest findRequest{NamespaceString::kConfigsvrShardsNamespace};
+ findRequest.setLimit(2);
+ auto numShards = client.find(std::move(findRequest))->itcount();
+ bool expectedValue = numShards >= 2;
+
+ if (expectedValue == currentValue) {
+ return;
+ }
+
+ ConfigsvrSetClusterParameter configsvrSetClusterParameter(
+ BSON("shardedClusterCardinalityForDirectConns"
+ << BSON("hasTwoOrMoreShards" << expectedValue)));
+ configsvrSetClusterParameter.setDbName(DatabaseName(boost::none, "admin"));
+
+ const auto shardRegistry = Grid::get(opCtx)->shardRegistry();
+ const auto cmdResponse =
+ uassertStatusOK(shardRegistry->getConfigShard()->runCommandWithFixedRetryAttempts(
+ opCtx,
+ ReadPreferenceSetting(ReadPreference::PrimaryOnly),
+ DatabaseName::kAdmin.toString(),
+ configsvrSetClusterParameter.toBSON({}),
+ Shard::RetryPolicy::kIdempotent));
+ uassertStatusOK(Shard::CommandResponse::getEffectiveStatus(std::move(cmdResponse)));
+ }
+ }
+
// TODO (SERVER-72791): Remove once FCV 7.0 becomes last-lts.
void _setOnCurrentShardSinceFieldOnChunks(
OperationContext* opCtx,