From fd849606b1548ba0d47c01216d6fc1f7961f6e90 Mon Sep 17 00:00:00 2001 From: Pol Pinol Castuera Date: Mon, 19 Dec 2022 11:25:51 +0000 Subject: SERVER-71614 Add case to hide the index when is not compatible with shard key. (cherry picked from commit 014381421c79a4d24bed1ebf82b7075db2ce7bd9) --- src/mongo/db/s/shard_key_index_util.cpp | 7 +++++++ src/mongo/db/s/shard_key_index_util.h | 4 ++-- src/mongo/db/s/shard_key_index_util_test.cpp | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/mongo/db/s/shard_key_index_util.cpp b/src/mongo/db/s/shard_key_index_util.cpp index 542da1e129c..a97ee5167c1 100644 --- a/src/mongo/db/s/shard_key_index_util.cpp +++ b/src/mongo/db/s/shard_key_index_util.cpp @@ -137,6 +137,13 @@ bool isLastNonHiddenShardKeyIndex(OperationContext* opCtx, const IndexCatalog* indexCatalog, const std::string& indexName, const BSONObj& shardKey) { + const auto index = indexCatalog->findIndexByName(opCtx, indexName); + if (!index || + !isCompatibleWithShardKey( + opCtx, collection, index->getEntry(), shardKey, false /* requireSingleKey */)) { + return false; + } + return !_findShardKeyPrefixedIndex( opCtx, collection, indexCatalog, indexName, shardKey, false /* requireSingleKey */) .is_initialized(); diff --git a/src/mongo/db/s/shard_key_index_util.h b/src/mongo/db/s/shard_key_index_util.h index 1fefa4af87e..c033e88be6c 100644 --- a/src/mongo/db/s/shard_key_index_util.h +++ b/src/mongo/db/s/shard_key_index_util.h @@ -93,8 +93,8 @@ const boost::optional findShardKeyPrefixedIndex(OperationContext* bool requireSingleKey); /** - * Returns true if the given index name is the last remaining not hidden index that is compatible - * with the shard key index. + * Returns true if the given index exists and it is the last non-hidden index compatible with the + * shard key. False otherwise. */ bool isLastNonHiddenShardKeyIndex(OperationContext* opCtx, const CollectionPtr& collection, diff --git a/src/mongo/db/s/shard_key_index_util_test.cpp b/src/mongo/db/s/shard_key_index_util_test.cpp index bf1502b1ff3..73e8f78779e 100644 --- a/src/mongo/db/s/shard_key_index_util_test.cpp +++ b/src/mongo/db/s/shard_key_index_util_test.cpp @@ -226,5 +226,26 @@ TEST_F(ShardKeyIndexUtilTest, LastShardIndexWithMultipleCandidates) { opCtx(), coll(), coll()->getIndexCatalog(), "x", BSON("x" << 1))); } +TEST_F(ShardKeyIndexUtilTest, LastShardIndexWithIncompatibleIndex) { + createIndex(BSON("key" << BSON("y" << 1) << "name" + << "y" + << "v" << kIndexVersion)); + createIndex(BSON("key" << BSON("x" << 1) << "name" + << "x" + << "v" << kIndexVersion)); + + ASSERT_FALSE(isLastNonHiddenShardKeyIndex( + opCtx(), coll(), coll()->getIndexCatalog(), "y", BSON("x" << 1))); +} + +TEST_F(ShardKeyIndexUtilTest, LastShardIndexWithNonExistingIndex) { + createIndex(BSON("key" << BSON("x" << 1) << "name" + << "x" + << "v" << kIndexVersion)); + + ASSERT_FALSE(isLastNonHiddenShardKeyIndex( + opCtx(), coll(), coll()->getIndexCatalog(), "y", BSON("x" << 1))); +} + } // namespace } // namespace mongo -- cgit v1.2.1