Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SERVER-45389 Add metrics tracking how often shards have inconsistent …
…indexes create mode 100644 jstests/noPassthrough/sharded_index_consistency_metrics.js create mode 100644 src/mongo/db/commands/sharded_index_consistency_server_status.cpp create mode 100644 src/mongo/db/s/periodic_sharded_index_consistency_checker.cpp create mode 100644 src/mongo/db/s/periodic_sharded_index_consistency_checker.h
- Loading branch information
1 parent
d6a8ae5
commit ea696eb
Showing
9 changed files
with
533 additions
and
2 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
jstests/noPassthrough/sharded_index_consistency_metrics.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Tests index consistency metrics in the serverStatus output. | ||
* @tags: [requires_fcv_44, requires_sharding] | ||
*/ | ||
(function() { | ||
"use strict"; | ||
|
||
// This test creates inconsistent indexes. | ||
TestData.skipCheckingIndexesConsistentAcrossCluster = true; | ||
|
||
/* | ||
* Asserts that the serverStatus output does not contain the index consistency metrics | ||
* both by default and when 'shardedIndexConsistency' is explicitly included. | ||
*/ | ||
function assertServerStatusNotContainIndexMetrics(conn) { | ||
let res = assert.commandWorked(conn.adminCommand({serverStatus: 1})); | ||
assert.eq(undefined, res.shardedIndexConsistency, tojson(res.shardedIndexConsistency)); | ||
|
||
res = assert.commandWorked(conn.adminCommand({serverStatus: 1, shardedIndexConsistency: 1})); | ||
assert.eq(undefined, res.shardedIndexConsistency, tojson(res.shardedIndexConsistency)); | ||
} | ||
|
||
/* | ||
* Asserts that eventually the number of sharded collections with inconsistent indexes in the | ||
* serverStatus output is equal to the expected count. | ||
*/ | ||
function checkServerStatusNumCollsWithInconsistentIndexes(conn, expectedCount) { | ||
assert.soon( | ||
() => { | ||
const res = assert.commandWorked(conn.adminCommand({serverStatus: 1})); | ||
assert.hasFields(res, ["shardedIndexConsistency"]); | ||
assert.hasFields(res.shardedIndexConsistency, | ||
["numShardedCollectionsWithInconsistentIndexes"]); | ||
return expectedCount == | ||
res.shardedIndexConsistency.numShardedCollectionsWithInconsistentIndexes; | ||
}, | ||
`expect the count of sharded collections with inconsistent indexes to eventually be equal to ${ | ||
expectedCount}`, | ||
undefined /* timeout */, | ||
1000 /* interval */); | ||
} | ||
|
||
/* | ||
* For each mongod in 'connsWithIndexConsistencyMetrics', asserts that its serverStatus | ||
* output has the expected number of collections with inconsistent indexes. For each mongod | ||
* in 'connsWithoutIndexConsistencyMetrics', asserts that its serverStatus output does | ||
* not contain the index consistency metrics. | ||
*/ | ||
function checkServerStatus(connsWithIndexConsistencyMetrics, | ||
connsWithoutIndexConsistencyMetrics, | ||
expectedNumCollsWithInconsistentIndexes) { | ||
for (const conn of connsWithIndexConsistencyMetrics) { | ||
checkServerStatusNumCollsWithInconsistentIndexes(conn, | ||
expectedNumCollsWithInconsistentIndexes); | ||
} | ||
for (const conn of connsWithoutIndexConsistencyMetrics) { | ||
assertServerStatusNotContainIndexMetrics(conn); | ||
} | ||
} | ||
|
||
const intervalMS = 3000; | ||
const st = new ShardingTest({ | ||
shards: 2, | ||
config: 2, | ||
configOptions: {setParameter: {"shardedIndexConsistencyCheckIntervalMS": intervalMS}} | ||
}); | ||
const dbName = "testDb"; | ||
const ns1 = dbName + ".testColl1"; | ||
const ns2 = dbName + ".testColl2"; | ||
const ns3 = dbName + ".testColl3"; | ||
const expiration = 1000000; | ||
const filterExpr = { | ||
x: {$gt: 50} | ||
}; | ||
|
||
assert.commandWorked(st.s.adminCommand({enableSharding: dbName})); | ||
st.ensurePrimaryShard(dbName, st.shard0.shardName); | ||
assert.commandWorked(st.s.adminCommand({shardCollection: ns1, key: {_id: "hashed"}})); | ||
assert.commandWorked(st.s.adminCommand({shardCollection: ns2, key: {_id: "hashed"}})); | ||
assert.commandWorked(st.s.adminCommand({shardCollection: ns3, key: {_id: "hashed"}})); | ||
|
||
st.config1.getDB("admin").runCommand({setParameter: 1, enableShardedIndexConsistencyCheck: false}); | ||
const connsWithIndexConsistencyMetrics = [st.config0]; | ||
const connsWithoutIndexConsistencyMetrics = [st.config1, st.shard0, st.shard1, st.s]; | ||
|
||
checkServerStatus(connsWithIndexConsistencyMetrics, connsWithoutIndexConsistencyMetrics, 0); | ||
|
||
// Create an inconsistent index for ns1. | ||
assert.commandWorked(st.shard0.getCollection(ns1).createIndex({x: 1})); | ||
checkServerStatus(connsWithIndexConsistencyMetrics, connsWithoutIndexConsistencyMetrics, 1); | ||
|
||
// Create another inconsistent index for ns1. | ||
assert.commandWorked(st.shard1.getCollection(ns1).createIndexes([{y: 1}])); | ||
checkServerStatus(connsWithIndexConsistencyMetrics, connsWithoutIndexConsistencyMetrics, 1); | ||
|
||
// Create an inconsistent index for ns2. | ||
assert.commandWorked(st.shard0.getCollection(ns2).createIndex({x: 1})); | ||
checkServerStatus(connsWithIndexConsistencyMetrics, connsWithoutIndexConsistencyMetrics, 2); | ||
|
||
// Resolve the index inconsistency for ns2. | ||
assert.commandWorked(st.shard1.getCollection(ns2).createIndex({x: 1})); | ||
checkServerStatus(connsWithIndexConsistencyMetrics, connsWithoutIndexConsistencyMetrics, 1); | ||
|
||
// Create indexes for n3 with the same options but in different orders on each shard, and verify | ||
// that it is not considered as inconsistent. | ||
assert.commandWorked(st.shard0.getCollection(ns3).createIndex({x: 1}, { | ||
name: "indexWithOptionsOrderedDifferently", | ||
partialFilterExpression: filterExpr, | ||
expireAfterSeconds: expiration | ||
})); | ||
assert.commandWorked(st.shard1.getCollection(ns3).createIndex({x: 1}, { | ||
name: "indexWithOptionsOrderedDifferently", | ||
expireAfterSeconds: expiration, | ||
partialFilterExpression: filterExpr | ||
})); | ||
checkServerStatus(connsWithIndexConsistencyMetrics, connsWithoutIndexConsistencyMetrics, 1); | ||
|
||
// Create indexes for n3 with the same key but different options on each shard, and verify that | ||
// it is considered as inconsistent. | ||
assert.commandWorked(st.shard0.getCollection(ns3).createIndex( | ||
{y: 1}, {name: "indexWithDifferentOptions", expireAfterSeconds: expiration})); | ||
assert.commandWorked( | ||
st.shard1.getCollection(ns3).createIndex({y: 1}, {name: "indexWithDifferentOptions"})); | ||
checkServerStatus(connsWithIndexConsistencyMetrics, connsWithoutIndexConsistencyMetrics, 2); | ||
|
||
st.stop(); | ||
|
||
// Verify that the serverStatus output for standalones and non-sharded repilca set servers does | ||
// not contain the index consistency metrics. | ||
const standaloneMongod = MongoRunner.runMongod(); | ||
assertServerStatusNotContainIndexMetrics(standaloneMongod); | ||
MongoRunner.stopMongod(standaloneMongod); | ||
|
||
const rst = ReplSetTest({nodes: 1}); | ||
rst.startSet(); | ||
rst.initiate(); | ||
assertServerStatusNotContainIndexMetrics(rst.getPrimary()); | ||
rst.stopSet(); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/mongo/db/commands/sharded_index_consistency_server_status.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright (C) 2020-present MongoDB, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
* | ||
* As a special exception, the copyright holders give permission to link the | ||
* code of portions of this program with the OpenSSL library under certain | ||
* conditions as described in each individual source file and distribute | ||
* linked combinations including the program with the OpenSSL library. You | ||
* must comply with the Server Side Public License in all respects for | ||
* all of the code used other than as permitted herein. If you modify file(s) | ||
* with this exception, you may extend this exception to your version of the | ||
* file(s), but you are not obligated to do so. If you do not wish to do so, | ||
* delete this exception statement from your version. If you delete this | ||
* exception statement from all source files in the program, then also delete | ||
* it in the license file. | ||
*/ | ||
|
||
#include "mongo/platform/basic.h" | ||
|
||
#include "mongo/db/commands/server_status.h" | ||
#include "mongo/db/s/periodic_sharded_index_consistency_checker.h" | ||
#include "mongo/db/s/sharding_runtime_d_params_gen.h" | ||
|
||
namespace mongo { | ||
namespace { | ||
|
||
bool isConfigServerWithShardedIndexConsistencyCheckEnabled() { | ||
return serverGlobalParams.clusterRole == ClusterRole::ConfigServer && | ||
enableShardedIndexConsistencyCheck.load(); | ||
} | ||
|
||
class ShardedIndexConsistencyServerStatus final : public ServerStatusSection { | ||
public: | ||
ShardedIndexConsistencyServerStatus() : ServerStatusSection("shardedIndexConsistency") {} | ||
|
||
bool includeByDefault() const override { | ||
return isConfigServerWithShardedIndexConsistencyCheckEnabled(); | ||
} | ||
|
||
BSONObj generateSection(OperationContext* opCtx, | ||
const BSONElement& configElement) const override { | ||
if (!isConfigServerWithShardedIndexConsistencyCheckEnabled()) { | ||
return {}; | ||
} | ||
|
||
BSONObjBuilder builder; | ||
builder.append("numShardedCollectionsWithInconsistentIndexes", | ||
PeriodicShardedIndexConsistencyChecker::get(opCtx->getServiceContext()) | ||
.getNumShardedCollsWithInconsistentIndexes()); | ||
return builder.obj(); | ||
} | ||
|
||
} indexConsistencyServerStatus; | ||
|
||
} // namespace | ||
} // namespace mongo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.