SERVER-112792 Report existance of compound wildcard indexes prefixed by the shard key (#44877)

GitOrigin-RevId: d546ecc6ef8455df3a29f88b56a994b4fe43e53a
This commit is contained in:
Silvia Surroca 2025-12-10 16:23:01 +01:00 committed by MongoDB Bot
parent c87b7b2632
commit 84c733352b
5 changed files with 119 additions and 6 deletions

View File

@ -0,0 +1,70 @@
/**
*
* Tests that the server reports the presence of compound wildcard indexes prefixed by the shard
* key when attempting to fetch a shard key index.
*
* TODO (SERVER-112793): Remove this test once 9.0 branches out.
*
* @tags: [
* requires_fcv_82,
* ]
*/
import {ShardingTest} from "jstests/libs/shardingtest.js";
const st = new ShardingTest({
shards: 3,
});
const shardKeyPattern = {
skey: 1,
};
function checkLogAndServerStatusMetrics(shard, expectedLogCount, expectedServerStatusMetricCount) {
checkLog.containsWithCount(
shard, "Found a compound wildcard index prefixed by the shard key", 1);
checkLog.checkContainsWithCountJson(
shard,
11279201,
{
nss: coll.getFullName(),
index: {skey: 1, "a.$**": 1},
indexName: "skey_1_a.$**_1",
shardKey: shardKeyPattern
},
expectedLogCount,
);
assert.eq(
expectedServerStatusMetricCount,
shard.getDB("admin")
.serverStatus()
.shardingStatistics.countHitsOfCompoundWildcardIndexesWithShardKeyPrefix,
);
}
const db = st.s.getDB(jsTestName());
const coll = db.getCollection("coll");
const primaryShard = st.shard0;
assert.commandWorked(
st.s.adminCommand({enableSharding: db.getName(), primaryShard: primaryShard.shardName}));
coll.createIndex({skey: 1, "a.$**": 1});
coll.createIndex({skey: 1});
assert.eq(3, coll.getIndexes().length, tojson(coll.getIndexes()));
assert.commandWorked(
st.s.adminCommand({shardCollection: coll.getFullName(), key: shardKeyPattern}));
checkLogAndServerStatusMetrics(primaryShard, 1, 1);
assert.commandWorked(
st.s.adminCommand({moveChunk: coll.getFullName(), find: {skey: 0}, to: st.shard1.shardName}));
checkLogAndServerStatusMetrics(primaryShard, 1, 2);
checkLogAndServerStatusMetrics(st.shard1, 1, 1);
assert.commandWorked(
st.s.adminCommand({moveChunk: coll.getFullName(), find: {skey: 0}, to: st.shard2.shardName}));
checkLogAndServerStatusMetrics(st.shard1, 1, 2);
checkLogAndServerStatusMetrics(st.shard2, 1, 1);
st.stop();

View File

@ -34,7 +34,10 @@
#include "mongo/db/catalog/collection.h"
#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/s/sharding_api_d_params_gen.h"
#include "mongo/db/s/sharding_statistics.h"
#include "mongo/db/server_feature_flags_gen.h"
#include "mongo/logv2/log_severity_suppressor.h"
#include "mongo/util/assert_util.h"
#include <memory>
@ -168,9 +171,8 @@ bool isCompatibleWithShardKey(OperationContext* opCtx,
reasons |= kErrorCollation;
}
if (errMsg && reasons != 0) {
std::string errors = "Index " + indexEntry->descriptor()->indexName() +
" cannot be used for sharding because:";
if (reasons != 0) {
std::string errors = "Index " + desc->indexName() + " cannot be used for sharding because:";
if (reasons & kErrorPartial) {
errors += " Index key is partial.";
}
@ -189,10 +191,31 @@ bool isCompatibleWithShardKey(OperationContext* opCtx,
if (reasons & kErrorWildcard) {
errors += " Index key is a wildcard index.";
}
if (!errMsg->empty()) {
*errMsg += "\n";
if (errMsg) {
if (!errMsg->empty()) {
*errMsg += "\n";
}
*errMsg += errors;
}
// TODO (SERVER-112793) Remove metrics reporting for compound wildcard indexes prefixed by
// the shard key once v9.0 branches out.
if (reasons & kErrorWildcard && !(reasons & kErrorNotPrefix)) {
ShardingStatistics::get(opCtx)
.countHitsOfCompoundWildcardIndexesWithShardKeyPrefix.addAndFetch(1);
if (enableCompoundWildcardIndexLog.load()) {
static logv2::SeveritySuppressor logSeverity{
Hours{1}, logv2::LogSeverity::Info(), logv2::LogSeverity::Debug(5)};
LOGV2_DEBUG(11279201,
logSeverity().toInt(),
"Found a compound wildcard index prefixed by the shard key",
"index"_attr = desc->keyPattern(),
"indexName"_attr = desc->indexName(),
"shardKey"_attr = shardKey,
"nss"_attr = collection.get()->ns());
}
}
*errMsg += errors;
}
return false;
}

View File

@ -52,3 +52,14 @@ server_parameters:
cpp_varname: directConnectionChecksWithSingleShard
default: true
redact: false
# TODO (SERVER-112793) Remove once v9.0 branches out
enableCompoundWildcardIndexLog:
description: >-
Enable logging whether a compound wildcard index prefixed by the shard key exists
when a shard key index is fetched for a sharded collection.
set_at: [startup, runtime]
cpp_vartype: AtomicWord<bool>
cpp_varname: enableCompoundWildcardIndexLog
default: true
redact: false

View File

@ -90,6 +90,8 @@ void ShardingStatistics::report(BSONObjBuilder* builder) const {
countTransitionToDedicatedConfigServerCompleted.loadRelaxed());
builder->append("countTransitionFromDedicatedConfigServerCompleted",
countTransitionFromDedicatedConfigServerCompleted.loadRelaxed());
builder->append("countHitsOfCompoundWildcardIndexesWithShardKeyPrefix",
countHitsOfCompoundWildcardIndexesWithShardKeyPrefix.loadRelaxed());
}
} // namespace mongo

View File

@ -144,6 +144,13 @@ struct ShardingStatistics {
// completed.
AtomicWord<long long> countTransitionFromDedicatedConfigServerCompleted{0};
// Total number of times a compound wildcard index prefixed by shard key has been detected
// during a moveChunk, range deleter or any other operation which needs to fetch a valid shard
// key index. This will help estimate the impact of SERVER-103774.
//
// TODO (SERVER-112793) Remove once v9.0 branches out.
AtomicWord<long long> countHitsOfCompoundWildcardIndexesWithShardKeyPrefix{0};
/**
* Obtains the per-process instance of the sharding statistics object.
*/