From 7208ec2715d1726451d9aaf730d2bc9b3969e15d Mon Sep 17 00:00:00 2001 From: Pierlauro Sciarelli Date: Tue, 7 Mar 2023 11:10:28 +0000 Subject: [PATCH] SERVER-73028 Add index supporting automerger query on `config.chunks` --- jstests/sharding/query_config.js | 33 +++++++------------ .../db/s/config/sharding_catalog_manager.cpp | 17 ++++++++-- ...rding_catalog_manager_chunk_operations.cpp | 24 +++++++------- ...log_manager_config_initialization_test.cpp | 5 ++- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/jstests/sharding/query_config.js b/jstests/sharding/query_config.js index 9553d100322..66bbdb3d7e3 100644 --- a/jstests/sharding/query_config.js +++ b/jstests/sharding/query_config.js @@ -115,29 +115,18 @@ var testListConfigChunksIndexes = function(st) { // This test depends on all the indexes in the configChunksIndexes being the exact indexes // in the config chunks collection. var configDB = st.s.getDB("config"); - var configChunksIndexes = (function() { - if (configDB.collections.findOne({_id: "config.system.sessions"}).timestamp) { - return ["_id_", "uuid_1_lastmod_1", "uuid_1_min_1", "uuid_1_shard_1_min_1"]; - } else { - return ["_id_", "ns_1_lastmod_1", "ns_1_min_1", "ns_1_shard_1_min_1"]; - } - }()); - var cursor; - var cursorArray = []; + var expectedConfigChunksIndexes = + ["_id_", "uuid_1_lastmod_1", "uuid_1_min_1", "uuid_1_shard_1_min_1"]; + const foundIndexesArray = getListIndexesCursor(configDB.chunks).toArray(); + // TODO SERVER-74573 always consider new index once 7.0 branches out + if (foundIndexesArray.length == expectedConfigChunksIndexes.length + 1) { + // CSRS nodes in v7.0 create a new index on config.chunks. Since the creation is not + // FCV-gated, this code is handling mixed binaries scenarios + expectedConfigChunksIndexes.push("uuid_1_shard_1_onCurrentShardSince_1"); + } - cursor = getListIndexesCursor(configDB.chunks); - assert.eq(cursorGetIndexNames(cursor), configChunksIndexes); - - cursor = getListIndexesCursor(configDB.chunks, {cursor: {batchSize: 2}}, 2); - assert.eq(cursor.objsLeftInBatch(), 2); - cursorArray.push(cursor.next()); - cursorArray.push(cursor.next()); - assert(cursor.hasNext()); - assert.eq(cursor.objsLeftInBatch(), 2); - cursorArray.push(cursor.next()); - cursorArray.push(cursor.next()); - assert(!cursor.hasNext()); - assert.eq(arrayGetNames(sortArrayByName(cursorArray)), configChunksIndexes); + assert.eq(foundIndexesArray.length, expectedConfigChunksIndexes.length); + assert.eq(arrayGetNames(sortArrayByName(foundIndexesArray)), expectedConfigChunksIndexes); }; /** diff --git a/src/mongo/db/s/config/sharding_catalog_manager.cpp b/src/mongo/db/s/config/sharding_catalog_manager.cpp index 73978ff57bd..7eb99d31a0a 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager.cpp @@ -217,7 +217,7 @@ Status createIndexesForConfigChunks(OperationContext* opCtx) { BSON(ChunkType::collectionUUID() << 1 << ChunkType::min() << 1), unique); if (!result.isOK()) { - return result.withContext("couldn't create uuid_1_min_1 index on config db"); + return result.withContext("couldn't create uuid_1_min_1 index on config.chunks"); } result = createIndexOnConfigCollection( @@ -226,7 +226,7 @@ Status createIndexesForConfigChunks(OperationContext* opCtx) { BSON(ChunkType::collectionUUID() << 1 << ChunkType::shard() << 1 << ChunkType::min() << 1), unique); if (!result.isOK()) { - return result.withContext("couldn't create uuid_1_shard_1_min_1 index on config db"); + return result.withContext("couldn't create uuid_1_shard_1_min_1 index on config.chunks"); } result = createIndexOnConfigCollection( @@ -235,7 +235,18 @@ Status createIndexesForConfigChunks(OperationContext* opCtx) { BSON(ChunkType::collectionUUID() << 1 << ChunkType::lastmod() << 1), unique); if (!result.isOK()) { - return result.withContext("couldn't create uuid_1_lastmod_1 index on config db"); + return result.withContext("couldn't create uuid_1_lastmod_1 index on config.chunks"); + } + + result = createIndexOnConfigCollection(opCtx, + ChunkType::ConfigNS, + BSON(ChunkType::collectionUUID() + << 1 << ChunkType::shard() << 1 + << ChunkType::onCurrentShardSince() << 1), + false /* unique */); + if (!result.isOK()) { + return result.withContext( + "couldn't create uuid_1_shard_1_onCurrentShardSince_1 index on config.chunks"); } return Status::OK(); 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 ad009d109bf..03fdcb670f4 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 @@ -1080,18 +1080,18 @@ ShardingCatalogManager::commitMergeAllChunksOnShard(OperationContext* opCtx, const auto oldestTimestampSupportedForHistory = getOldestTimestampSupportedForSnapshotHistory(opCtx); const auto chunksBelongingToShard = - uassertStatusOK( - _localConfigShard->exhaustiveFindOnConfig( - opCtx, - ReadPreferenceSetting{ReadPreference::PrimaryOnly}, - repl::ReadConcernLevel::kLocalReadConcern, - ChunkType::ConfigNS, - BSON(ChunkType::collectionUUID - << collUuid << ChunkType::shard(shardId.toString()) << ChunkType::jumbo - << BSON("$ne" << true) << ChunkType::onCurrentShardSince - << BSON("$lt" << oldestTimestampSupportedForHistory)), - BSON(ChunkType::min << 1) /* sort */, - boost::none /* limit */)) + uassertStatusOK(_localConfigShard->exhaustiveFindOnConfig( + opCtx, + ReadPreferenceSetting{ReadPreference::PrimaryOnly}, + repl::ReadConcernLevel::kLocalReadConcern, + ChunkType::ConfigNS, + BSON(ChunkType::collectionUUID + << collUuid << ChunkType::shard(shardId.toString()) + << ChunkType::onCurrentShardSince + << BSON("$lt" << oldestTimestampSupportedForHistory) + << ChunkType::jumbo << BSON("$ne" << true)), + BSON(ChunkType::min << 1) /* sort */, + boost::none /* limit */)) .docs; // 3. Prepare the data for the merge. diff --git a/src/mongo/db/s/config/sharding_catalog_manager_config_initialization_test.cpp b/src/mongo/db/s/config/sharding_catalog_manager_config_initialization_test.cpp index 9fcdf428a83..a0dfbd77b4b 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager_config_initialization_test.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager_config_initialization_test.cpp @@ -275,7 +275,10 @@ TEST_F(ConfigInitializationTest, BuildsNecessaryIndexes) { << "unique" << true), BSON("v" << 2 << "key" << BSON("uuid" << 1 << "lastmod" << 1) << "name" << "uuid_1_lastmod_1" - << "unique" << true)}; + << "unique" << true), + BSON("v" << 2 << "key" << BSON("uuid" << 1 << "shard" << 1 << "onCurrentShardSince" << 1) + << "name" + << "uuid_1_shard_1_onCurrentShardSince_1")}; auto expectedShardsIndexes = std::vector{ BSON("v" << 2 << "key" << BSON("_id" << 1) << "name"