Skip to content

Commit 29f8c18

Browse files
toto-devMongoDB Bot
authored andcommitted
SERVER-76483 relax check shard filtering metadata (#31778)
GitOrigin-RevId: f11094fb1ea337ae03434a560eb98470b727e88b
1 parent 32c5814 commit 29f8c18

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

jstests/libs/check_shard_filtering_metadata_helpers.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,35 @@ var CheckShardFilteringMetadataHelpers = (function() {
3232
print(`CheckShardFilteringMetadata: Database '${dbName}' on '${nodeConn.host}' OK`);
3333
}
3434

35+
function getPrimaryShardForDB(dbName) {
36+
if (dbName == 'config') {
37+
return 'config';
38+
}
39+
40+
const configDB = mongosConn.getDB('config');
41+
42+
const dbEntry = configDB.databases.findOne({_id: dbName});
43+
assert(dbEntry, `Couldn't find database '${dbName}' in 'config.databases'`);
44+
assert(dbEntry.primary,
45+
`Database entry for db '${dbName}' does not contain primary shard: ${
46+
tojson(dbEntry)}`);
47+
return dbEntry.primary;
48+
}
49+
3550
function checkShardedCollection(coll, nodeShardingState) {
3651
const ns = coll._id;
3752
print(`CheckShardFilteringMetadata: checking collection '${ns} ' on node '${
3853
nodeConn.host}' of shard '${shardId}'`);
3954

4055
const configDB = mongosConn.getDB('config');
4156

57+
const dbName = mongosConn.getCollection(ns).getDB().getName();
58+
const primaryShardId = getPrimaryShardForDB(dbName);
4259
const highestChunkOnShard = configDB.chunks.find({uuid: coll.uuid, shard: shardId})
4360
.sort({lastmod: -1})
4461
.limit(1)
4562
.toArray()[0];
4663

47-
const expectedShardVersion =
48-
highestChunkOnShard ? highestChunkOnShard.lastmod : Timestamp(0, 0);
4964
const expectedTimestamp = coll.timestamp;
5065

5166
const collectionMetadataOnNode = nodeShardingState.versions[ns];
@@ -62,18 +77,25 @@ var CheckShardFilteringMetadataHelpers = (function() {
6277
return;
6378
}
6479

65-
if (timestampCmp(collectionMetadataOnNode.timestamp, Timestamp(0, 0)) === 0) {
66-
// The metadata reflects an unsharded collection. It is okay for a node to have this
67-
// stale metadata, as long as the node knows the correct dbVersion.
80+
if ((shardId != getPrimaryShardForDB(dbName) && !highestChunkOnShard)) {
81+
// The shard is neither primary for database nor owns some chunks for this
82+
// collection.
83+
// In this case the shard is allow to have a stale/wrong collection
84+
// metadata as long as it has the correct db version.
6885
return;
6986
}
7087

71-
// If the node knows its filtering info, then assert that it is correct.
88+
// Check that timestamp is correct
7289
assert.eq(timestampCmp(collectionMetadataOnNode.timestamp, expectedTimestamp),
7390
0,
7491
`Unexpected timestamp for ns '${ns}' on node '${nodeConn.host}'. Found '${
7592
tojson(collectionMetadataOnNode.timestamp)}', expected '${
7693
tojson(expectedTimestamp)}'`);
94+
95+
// Check that placement version is correct
96+
const expectedShardVersion =
97+
highestChunkOnShard ? highestChunkOnShard.lastmod : Timestamp(0, 0);
98+
7799
// Only check the major version because some operations (such as resharding or
78100
// setAllowMigrations) bump the minor version without the shards knowing. This does not
79101
// affect placement, so it is okay.

0 commit comments

Comments
 (0)