@@ -32,20 +32,35 @@ var CheckShardFilteringMetadataHelpers = (function() {
32
32
print ( `CheckShardFilteringMetadata: Database '${ dbName } ' on '${ nodeConn . host } ' OK` ) ;
33
33
}
34
34
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
+
35
50
function checkShardedCollection ( coll , nodeShardingState ) {
36
51
const ns = coll . _id ;
37
52
print ( `CheckShardFilteringMetadata: checking collection '${ ns } ' on node '${
38
53
nodeConn . host } ' of shard '${ shardId } '`) ;
39
54
40
55
const configDB = mongosConn . getDB ( 'config' ) ;
41
56
57
+ const dbName = mongosConn . getCollection ( ns ) . getDB ( ) . getName ( ) ;
58
+ const primaryShardId = getPrimaryShardForDB ( dbName ) ;
42
59
const highestChunkOnShard = configDB . chunks . find ( { uuid : coll . uuid , shard : shardId } )
43
60
. sort ( { lastmod : - 1 } )
44
61
. limit ( 1 )
45
62
. toArray ( ) [ 0 ] ;
46
63
47
- const expectedShardVersion =
48
- highestChunkOnShard ? highestChunkOnShard . lastmod : Timestamp ( 0 , 0 ) ;
49
64
const expectedTimestamp = coll . timestamp ;
50
65
51
66
const collectionMetadataOnNode = nodeShardingState . versions [ ns ] ;
@@ -62,18 +77,25 @@ var CheckShardFilteringMetadataHelpers = (function() {
62
77
return ;
63
78
}
64
79
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.
68
85
return ;
69
86
}
70
87
71
- // If the node knows its filtering info, then assert that it is correct.
88
+ // Check that timestamp is correct
72
89
assert . eq ( timestampCmp ( collectionMetadataOnNode . timestamp , expectedTimestamp ) ,
73
90
0 ,
74
91
`Unexpected timestamp for ns '${ ns } ' on node '${ nodeConn . host } '. Found '${
75
92
tojson ( collectionMetadataOnNode . timestamp ) } ', expected '${
76
93
tojson ( expectedTimestamp ) } '`) ;
94
+
95
+ // Check that placement version is correct
96
+ const expectedShardVersion =
97
+ highestChunkOnShard ? highestChunkOnShard . lastmod : Timestamp ( 0 , 0 ) ;
98
+
77
99
// Only check the major version because some operations (such as resharding or
78
100
// setAllowMigrations) bump the minor version without the shards knowing. This does not
79
101
// affect placement, so it is okay.
0 commit comments