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-19 11:56:14 +0000 |
commit | 014381421c79a4d24bed1ebf82b7075db2ce7bd9 (patch) | |
tree | ec24c6ff8858516bcaa3bcf49c4a50715192b5ec | |
parent | d25e0fcfe72f2a39f744e3e1e085361ff73184ad (diff) | |
download | mongo-014381421c79a4d24bed1ebf82b7075db2ce7bd9.tar.gz |
SERVER-71614 Add case to hide the index when is not compatible with shard key.
-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 4359d731dbb..c55f1c6bafa 100644 --- a/src/mongo/db/s/shard_key_index_util.cpp +++ b/src/mongo/db/s/shard_key_index_util.cpp @@ -188,6 +188,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, true /* requireSingleKey */) .has_value(); diff --git a/src/mongo/db/s/shard_key_index_util.h b/src/mongo/db/s/shard_key_index_util.h index c15840db109..34fa25c6ec8 100644 --- a/src/mongo/db/s/shard_key_index_util.h +++ b/src/mongo/db/s/shard_key_index_util.h @@ -98,8 +98,8 @@ boost::optional<ShardKeyIndex> findShardKeyPrefixedIndex(OperationContext* opCtx std::string* errMsg = nullptr); /** - * 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 |