diff options
author | Paolo Polato <paolo.polato@mongodb.com> | 2022-10-17 10:16:26 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-10-17 10:46:28 +0000 |
commit | 613df409e8e545cefb494c140565bdbe85838ce2 (patch) | |
tree | 00144667efe57fad422de899b68610edbbc4459b /jstests/sharding | |
parent | 4826dded41f235887cd472845227926e4c86acd5 (diff) | |
download | mongo-613df409e8e545cefb494c140565bdbe85838ce2.tar.gz |
SERVER-68927 Store placement changes into config.placementHistory when a shardCollection() command gets committed on the config server
Diffstat (limited to 'jstests/sharding')
-rw-r--r-- | jstests/sharding/sessions_collection_auto_healing.js | 24 | ||||
-rw-r--r-- | jstests/sharding/store_historical_placement_data.js | 95 |
2 files changed, 91 insertions, 28 deletions
diff --git a/jstests/sharding/sessions_collection_auto_healing.js b/jstests/sharding/sessions_collection_auto_healing.js index 5689c4fd3ec..109b8fe96d0 100644 --- a/jstests/sharding/sessions_collection_auto_healing.js +++ b/jstests/sharding/sessions_collection_auto_healing.js @@ -3,6 +3,7 @@ * ] */ load('jstests/libs/sessions_collection.js'); +load("jstests/libs/feature_flag_util.js"); (function() { "use strict"; @@ -119,19 +120,30 @@ var shardConfig = shard.getDB("config"); validateSessionsCollection(shard, true, true); - // We will have two sessions because of the session used in the shardCollection's retryable - // write to shard the sessions collection. It will disappear after we run the refresh - // function on the shard. - assert.eq(shardConfig.system.sessions.countDocuments({}), 2, "did not flush config's sessions"); + // TODO SERVER-69106 adapt the test assuming that the flag will be always enabled. + const historicalPlacementDataFeatureFlag = FeatureFlagUtil.isEnabled( + st.configRS.getPrimary().getDB('admin'), "HistoricalPlacementShardingCatalog"); + const sessionsOpenedByShardCollectionCmd = historicalPlacementDataFeatureFlag ? 3 : 2; + + // We will have sessionsOpenedByShardCollectionCmd sessions because of the sessions used in the + // shardCollection's retryable write to shard the sessions collection. It will disappear after + // we run the refresh function on the shard. + assert.eq(shardConfig.system.sessions.countDocuments({}), + sessionsOpenedByShardCollectionCmd, + "did not flush config's sessions"); // Now, if we do refreshes on the other servers, their in-mem records will // be written to the collection. assert.commandWorked(shard.adminCommand({refreshLogicalSessionCacheNow: 1})); - assert.eq(shardConfig.system.sessions.countDocuments({}), 3, "did not flush shard's sessions"); + assert.eq(shardConfig.system.sessions.countDocuments({}), + sessionsOpenedByShardCollectionCmd + 1, + "did not flush shard's sessions"); rs.awaitLastOpCommitted(); assert.commandWorked(mongos.adminCommand({refreshLogicalSessionCacheNow: 1})); - assert.eq(shardConfig.system.sessions.countDocuments({}), 5, "did not flush mongos' sessions"); + assert.eq(shardConfig.system.sessions.countDocuments({}), + sessionsOpenedByShardCollectionCmd + 3, + "did not flush mongos' sessions"); } // Test that if we drop the index on the sessions collection, only a refresh on the config diff --git a/jstests/sharding/store_historical_placement_data.js b/jstests/sharding/store_historical_placement_data.js index 844d46cf54e..f4b2831df02 100644 --- a/jstests/sharding/store_historical_placement_data.js +++ b/jstests/sharding/store_historical_placement_data.js @@ -1,14 +1,73 @@ -/* - * The test verifies that each Sharding DDL operation that gets successfully completed - * also produces a document detailing the changes to the placement of the targeted nss. - * - * - */ + (function() { "use strict"; load("jstests/libs/feature_flag_util.js"); const st = new ShardingTest({shards: 2}); +const configDB = st.s.getDB('config'); + +function getAndValidateLatestPlacementInfoForDB(dbName) { + const placementQueryResults = + configDB.placementHistory.find({nss: dbName}).sort({timestamp: -1}).limit(1).toArray(); + assert.eq(placementQueryResults.length, 1); + const dbPlacementDetails = placementQueryResults[0]; + + // Verify that the placementHistory document matches the related content stored in + // config.databases. + const configDBsQueryResults = configDB.databases.find({_id: dbPlacementDetails.nss}).toArray(); + assert.eq(1, configDBsQueryResults.length); + const databaseDetails = configDBsQueryResults[0]; + + assert(timestampCmp(databaseDetails.version.timestamp, dbPlacementDetails.timestamp) === 0); + assert.eq(1, dbPlacementDetails.shards.length); + assert.eq(databaseDetails.primary, dbPlacementDetails.shards[0]); + assert.eq(undefined, dbPlacementDetails.uuid); + return dbPlacementDetails; +} + +function getAndValidateLatestPlacementInfoForCollection(fullCollName) { + const placementQueryResults = configDB.placementHistory.find({nss: fullCollName}) + .sort({timestamp: -1}) + .limit(1) + .toArray(); + assert.eq(placementQueryResults.length, 1); + const placementDetails = placementQueryResults[0]; + + // Verify that the placementHistory document matches the related content stored in + // config.collections. + const configCollsQueryResults = + configDB.collections.find({_id: placementDetails.nss}).toArray(); + assert.eq(configCollsQueryResults.length, 1); + const collectionEntry = configCollsQueryResults[0]; + + assert.eq(collectionEntry.uuid, placementDetails.uuid); + assert(timestampCmp(collectionEntry.timestamp, placementDetails.timestamp) === 0); + return placementDetails; +} + +function testEnableSharding(dbName, primaryShardName) { + assert.commandWorked( + st.s.adminCommand({enableSharding: dbName, primaryShard: primaryShardName})); + getAndValidateLatestPlacementInfoForDB(dbName); +} + +function testShardCollection(dbName, collName) { + let nss = dbName + '.' + collName; + + // Shard the collection. Ensure enough chunks to cover all shards. + assert.commandWorked( + st.s.adminCommand({shardCollection: nss, key: {_id: "hashed"}, numInitialChunks: 20})); + // Verify that there is consistent placement info on the shared collection and its parent DB. + const dbPlacementInfo = getAndValidateLatestPlacementInfoForDB(dbName); + const collPlacementInfo = getAndValidateLatestPlacementInfoForCollection(nss); + assert(timestampCmp(dbPlacementInfo.timestamp, collPlacementInfo.timestamp) < 0); + + // Verify that the placementHistory document matches the related content stored in + // config.shards. + const entriesInConfigShards = configDB.shards.find({}, {_id: 1}).toArray().map((s) => s._id); + assert.sameMembers(entriesInConfigShards, collPlacementInfo.shards); +} + // TODO SERVER-69106 remove the logic to skip the test execution const historicalPlacementDataFeatureFlag = FeatureFlagUtil.isEnabled( st.configRS.getPrimary().getDB('admin'), "HistoricalPlacementShardingCatalog"); @@ -18,23 +77,15 @@ if (!historicalPlacementDataFeatureFlag) { return; } -const dbName = 'test'; -const configDB = st.s.getDB('config'); +jsTest.log('Testing placement entries added by explicit DB creation'); +testEnableSharding('explicitlyCreatedDB', st.shard0.shardName); + +jsTest.log( + 'Testing placement entries added by shardCollection() over an existing sharding-enabled DB'); +testShardCollection('explicitlyCreatedDB', 'coll1'); -jsTest.log('Verifying placement data generated by createDatabase()'); -assert.commandWorked( - st.s.adminCommand({enableSharding: dbName, primaryShard: st.shard0.shardName})); - -const placementHistoryEntries = configDB.placementHistory.find().toArray(); -assert.eq(placementHistoryEntries.length, 1); -const placementDetails = placementHistoryEntries[0]; -const databaseEntries = configDB.databases.find({_id: placementDetails.nss}).toArray(); -assert.eq(1, databaseEntries.length); -const databaseDetails = databaseEntries[0]; -assert(timestampCmp(databaseDetails.version.timestamp, placementDetails.timestamp) == 0); -assert.eq(1, placementDetails.shards.length); -assert.eq(databaseDetails.primary, placementDetails.shards[0]); -assert.eq(undefined, placementDetails.uuid); +jsTest.log('Testing placement entries added by shardCollection() over a non-existing db (& coll)'); +testShardCollection('implicitlyCreatedDB', 'coll1'); st.stop(); }()); |