diff options
author | Jonathan Lee <jonathan.lee@mongodb.com> | 2021-09-17 16:10:19 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-10-07 22:05:53 +0000 |
commit | db0d66c2fe43edffeb3aa72f6f51aa23e5882036 (patch) | |
tree | 3a385b34cd03905367d84215f819b8bebf02f4b8 | |
parent | b59d2547c281c42e61b52d44291d500f4dad21e4 (diff) | |
download | mongo-db0d66c2fe43edffeb3aa72f6f51aa23e5882036.tar.gz |
SERVER-58104 Prevent the balancer from calculating split points in sessions collection if shard key is not _id
(cherry picked from commit 9a12a55346e0da77acc94797bf5a558b001188e0)
3 files changed, 64 insertions, 0 deletions
diff --git a/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml b/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml index a63bac55d57..49e017e2514 100644 --- a/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml +++ b/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml @@ -134,6 +134,8 @@ selector: - jstests/sharding/time_zone_info_mongos.js # Requires behavior that does not and will never exist in 3.6. - jstests/sharding/retryable_mongos_write_errors.js + # Requires the fix for SERVER-58104 which was not backported to 3.6. + - jstests/sharding/balancing_sessions_collection_reshard.js executor: config: shell_options: diff --git a/jstests/sharding/balancing_sessions_collection_reshard.js b/jstests/sharding/balancing_sessions_collection_reshard.js new file mode 100644 index 00000000000..fe34ed34116 --- /dev/null +++ b/jstests/sharding/balancing_sessions_collection_reshard.js @@ -0,0 +1,55 @@ +/* + * Tests that the balancer does not split the chunks for the sessions collection + * if the shard key is not _id. + */ +(function() { + "use strict"; + + let numShards = 2; + const kMinNumChunks = 2; + const clusterName = jsTest.name(); + const st = new ShardingTest({ + name: clusterName, + shards: numShards, + other: + {configOptions: {setParameter: {minNumChunksForSessionsCollection: kMinNumChunks}}} + }); + + let waitForBalancerToRun = function() { + let lastRoundNumber = + assert.commandWorked(st.s.adminCommand({balancerStatus: 1})).numBalancerRounds; + st.startBalancer(); + + assert.soon(function() { + let res = assert.commandWorked(st.s.adminCommand({balancerStatus: 1})); + return res.mode == "full" && res.numBalancerRounds - lastRoundNumber > 1; + }); + + st.stopBalancer(); + }; + + const kSessionsNs = "config.system.sessions"; + let configDB = st.s.getDB("config"); + + jsTest.log("Verify that the sessions collection is successfully dropped."); + + assert.commandWorked(configDB.runCommand({drop: "system.sessions"})); + + jsTest.log("Verify that the sessions collection is successfully recreated and resharded."); + + assert.commandWorked(st.s.adminCommand({enableSharding: "config"})); + assert.commandWorked(st.s.adminCommand({shardCollection: kSessionsNs, key: {oldRoles: 1}})); + + jsTest.log("Verify that balancer does not fail after resharding."); + waitForBalancerToRun(); + + jsTest.log("Verify that there is a single chunk for the collection " + + "and the bounds are the resharded key."); + assert.eq(1, configDB.chunks.count({ns: kSessionsNs})); + + let doc = configDB.chunks.findOne({ns: kSessionsNs}); + assert(doc.min.hasOwnProperty("oldRoles")); + assert(doc.max.hasOwnProperty("oldRoles")); + + st.stop(); +}()); diff --git a/src/mongo/db/s/balancer/balancer_chunk_selection_policy_impl.cpp b/src/mongo/db/s/balancer/balancer_chunk_selection_policy_impl.cpp index b113735802f..15bf7093fbf 100644 --- a/src/mongo/db/s/balancer/balancer_chunk_selection_policy_impl.cpp +++ b/src/mongo/db/s/balancer/balancer_chunk_selection_policy_impl.cpp @@ -234,6 +234,13 @@ void getSplitCandidatesToEnforceTagRanges(const ChunkManager* cm, void getSplitCandidatesForSessionsCollection(OperationContext* opCtx, const ChunkManager* cm, SplitCandidatesBuffer* splitCandidates) { + // Do not compute split points if shard key is not _id, + // since the split points depend on _id.id. + const auto& keyPattern = cm->getShardKeyPattern().getKeyPattern(); + if (!KeyPattern::isIdKeyPattern(keyPattern.toBSON())) { + return; + } + const auto minNumChunks = minNumChunksForSessionsCollection.load(); if (cm->numChunks() >= minNumChunks) { |