diff options
author | Pol Pinol Castuera <pol.pinol@mongodb.com> | 2022-12-19 11:25:51 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-12-23 11:25:06 +0000 |
commit | fd849606b1548ba0d47c01216d6fc1f7961f6e90 (patch) | |
tree | 96d7d497bb69f2fe60e4973c4b4a72b36a56800e | |
parent | 55d44655f9437c411ed1ef3edfddcee56b2e9aae (diff) | |
download | mongo-fd849606b1548ba0d47c01216d6fc1f7961f6e90.tar.gz |
SERVER-71614 Add case to hide the index when is not compatible with shard key.
(cherry picked from commit 014381421c79a4d24bed1ebf82b7075db2ce7bd9)
-rw-r--r-- | src/mongo/db/s/shard_key_index_util.cpp | 7 | ||||
-rw-r--r-- | src/mongo/db/s/shard_key_index_util.h | 4 | ||||
-rw-r--r-- | 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<ShardKeyIndex> 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 |