summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2021-05-07 15:47:23 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-05-11 07:12:58 +0000
commit9261befabc2f3e922eec0f15e091e9881deb1960 (patch)
tree18865614ac63e55a203dbcaf72bdd41f72c3fc1e
parentfa92dae8174f68fec1018d572f14e490e5467eac (diff)
downloadmongo-9261befabc2f3e922eec0f15e091e9881deb1960.tar.gz
SERVER-56515 Make mergeChunks consider the 'incrementChunkMajorVersionOnChunkSplits' parameter
-rw-r--r--jstests/sharding/major_version_check.js15
-rw-r--r--src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp37
2 files changed, 41 insertions, 11 deletions
diff --git a/jstests/sharding/major_version_check.js b/jstests/sharding/major_version_check.js
index 2ae32fe6cae..0626074abe2 100644
--- a/jstests/sharding/major_version_check.js
+++ b/jstests/sharding/major_version_check.js
@@ -4,8 +4,7 @@
(function() {
'use strict';
- // Test with default value of incrementChunkMajorVersionOnChunkSplits, which is
- // false.
+ // Test with default value of incrementChunkMajorVersionOnChunkSplits, which is false.
(() => {
var st = new ShardingTest({shards: 1, mongos: 2});
@@ -39,7 +38,6 @@
staleMongos.getCollection(coll + "").findOne();
printjson(staleMongos.getDB("admin").runCommand({getShardVersion: coll + ""}));
-
assert.eq(Timestamp(1, 0),
staleMongos.getDB("admin").runCommand({getShardVersion: coll + ""}).version);
@@ -48,10 +46,14 @@
staleMongos.getCollection(coll + "").findOne();
printjson(staleMongos.getDB("admin").runCommand({getShardVersion: coll + ""}));
-
assert.eq(Timestamp(1, 0),
staleMongos.getDB("admin").runCommand({getShardVersion: coll + ""}).version);
+ // Merge the just-split chunks and ensure the major version didn't go up
+ assert.commandWorked(
+ admin.runCommand({mergeChunks: coll + "", bounds: [{_id: MinKey}, {_id: MaxKey}]}));
+ assert.eq(Timestamp(1, 3), admin.runCommand({getShardVersion: coll + ""}).version);
+
st.stop();
})();
@@ -123,6 +125,11 @@
assert.eq(Timestamp(2, 2),
staleMongos.getDB("admin").runCommand({getShardVersion: coll + ""}).version);
+ // Merge the just-split chunks and ensure the major version went up
+ assert.commandWorked(
+ admin.runCommand({mergeChunks: coll + "", bounds: [{_id: MinKey}, {_id: MaxKey}]}));
+ assert.eq(Timestamp(3, 0), admin.runCommand({getShardVersion: coll + ""}).version);
+
st.stop();
})();
})();
diff --git a/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp b/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp
index 033a96c6a25..453ae919b72 100644
--- a/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp
+++ b/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp
@@ -370,13 +370,12 @@ Status ShardingCatalogManager::commitChunkSplit(OperationContext* opCtx,
std::vector<ChunkType> newChunks;
ChunkVersion currentMaxVersion = collVersion;
- // Increment the major version only if the shard that owns the chunk being split has version ==
- // collection version. See SERVER-41480 for details.
+ // Increment the major version only if the shard that owns the chunk being split has
+ // shardVersion == collection version. See SERVER-41480 for details.
if (incrementChunkMajorVersionOnChunkSplits.load() && shardVersion == collVersion) {
currentMaxVersion.incMajor();
}
-
auto startKey = range.getMin();
auto newChunkBounds(splitPoints);
newChunkBounds.push_back(range.getMax());
@@ -583,8 +582,7 @@ Status ShardingCatalogManager::commitChunkMerge(OperationContext* opCtx,
1)); // Limit 1.
if (!swCollVersion.isOK()) {
- return swCollVersion.getStatus().withContext(str::stream()
- << "mergeChunk cannot merge chunks.");
+ return swCollVersion.getStatus().withContext("mergeChunk cannot merge chunks.");
}
auto collVersion = swCollVersion.getValue();
@@ -596,6 +594,25 @@ Status ShardingCatalogManager::commitChunkMerge(OperationContext* opCtx,
"that the collection was dropped and re-created."};
}
+ // Get the shard version (max chunk version) for the shard requesting the merge.
+ auto swShardVersion = getMaxChunkVersionFromQueryResponse(
+ nss,
+ Grid::get(opCtx)->shardRegistry()->getConfigShard()->exhaustiveFindOnConfig(
+ opCtx,
+ ReadPreferenceSetting{ReadPreference::PrimaryOnly},
+ repl::ReadConcernLevel::kLocalReadConcern,
+ ChunkType::ConfigNS,
+ BSON("ns" << nss.ns() << "shard"
+ << shardName), // Query all chunks for this namespace and shard.
+ BSON(ChunkType::lastmod << -1), // Sort by version.
+ 1)); // Limit 1.
+
+ if (!swShardVersion.isOK()) {
+ return swShardVersion.getStatus().withContext("mergeChunk cannot merge chunks.");
+ }
+
+ auto shardVersion = swShardVersion.getValue();
+
// Build chunks to be merged
std::vector<ChunkType> chunksToMerge;
@@ -636,7 +653,13 @@ Status ShardingCatalogManager::commitChunkMerge(OperationContext* opCtx,
}
ChunkVersion mergeVersion = collVersion;
- mergeVersion.incMinor();
+ // Increment the major version only if the shard that owns the chunks being merged has
+ // shardVersion == collection version. See SERVER-41480 for details.
+ if (incrementChunkMajorVersionOnChunkSplits.load() && shardVersion == collVersion) {
+ mergeVersion.incMajor();
+ } else {
+ mergeVersion.incMinor();
+ }
auto updates = buildMergeChunksTransactionUpdates(chunksToMerge, mergeVersion, validAfter);
auto preCond = buildMergeChunksTransactionPrecond(chunksToMerge, collVersion);
@@ -668,7 +691,7 @@ Status ShardingCatalogManager::commitChunkMerge(OperationContext* opCtx,
Grid::get(opCtx)
->catalogClient()
->logChange(opCtx, "merge", nss.ns(), logDetail.obj(), WriteConcernOptions())
- .transitional_ignore();
+ .ignore();
return applyOpsStatus;
}