jstests/sharding/timeseries_multiple_mongos.js
/**
 * The test runs drop, create and shardCollection commands using mongos0, then validates the given
 * command by running against mongos1 with stale config.
 */
The test runs drop, create and shardCollection commands using mongos0, then validates the given
command by running against mongos1 with stale config.
The test runs drop create and shardCollection commands using mongos0 then validates the given command by running against mongos1 with stale config .
function runTest({shardKey, cmdObj, numProfilerEntries}) {
    const isDelete = cmdObj["delete"] !== undefined;
    const isUpdate = cmdObj["update"] !== undefined;
    const isCollMod = cmdObj["collMod"] !== undefined;
    const cmdCollName = cmdObj[Object.keys(cmdObj)[0]];
    const shardKeyHasMetaField = shardKey[metaField] !== undefined;
    assert.commandWorked(mongos1.getCollection(collName).insert({[timeField]: ISODate()}));
    assert(mongos0.getCollection(collName).drop());
    assert.commandWorked(mongos0.createCollection(
        collName, {timeseries: {timeField: timeField, metaField: metaField}}));
    assert.commandWorked(st.s0.adminCommand({
        shardCollection: `${dbName}.${collName}`,
        key: shardKey,
    }));
    const middle = shardKeyHasMetaField ? {meta: 1} : {"meta.a": 1};
    assert.commandWorked(mongos0.adminCommand({split: `${dbName}.${bucketsCollName}`, middle}));
    const primaryShard = st.getPrimaryShard(dbName);
    const otherShard = st.getOther(primaryShard);
    assert.commandWorked(mongos0.adminCommand({
        movechunk: `${dbName}.${bucketsCollName}`,
        find: middle,
        to: otherShard.name,
        _waitForDelete: true
    }));
    function validateCommand(cmdCollName, numEntries, unVersioned) {
        for (let shardDB of [shard0DB, shard1DB]) {
            shardDB.setProfilingLevel(0);
            shardDB.system.profile.drop();
            shardDB.setProfilingLevel(2);
        }
        const res = mongos1.runCommand(cmdObj);
        if (numEntries > 0) {
            assert.commandWorked(res);
        } else {
            assert.commandFailed(res);
        }
        const queryField = `command.${Object.keys(cmdObj)[0]}`;
        let filter = {
            [queryField]: cmdCollName,
            "$or": [
                {
                    "$and": [
                        {"command.shardVersion.0": {"$exists": true}},
                        {
                            "command.shardVersion.0":
                                {$ne: ShardVersioningUtil.kIgnoredShardVersion[0]}
                        },
                    ]
                },
                {
                    "$and": [
                        {"command.shardVersion.v": {"$exists": true}},
                        {
                            "command.shardVersion.v":
                                {$ne: ShardVersioningUtil.kIgnoredShardVersion[0]}
                        },
                    ]
                },
            ]
        };
        if (isUpdate) {
            filter = {"op": "update", "ns": `${dbName}.${cmdCollName}`, "ok": {$ne: 0}};
        } else if (isDelete) {
            filter = {"op": "remove", "ns": `${dbName}.${cmdCollName}`, "ok": {$ne: 0}};
        } else if (isCollMod) {
            const command = unVersioned ? "_shardsvrCollMod" : "_shardsvrCollModParticipant";
            filter = {[`command.${command}`]: cmdCollName, "ok": {$ne: 0}};
        } else if (unVersioned && !isCollMod) {
            filter = {
                [queryField]: cmdCollName,
                "$or": [
                    {"command.shardVersion.0": ShardVersioningUtil.kIgnoredShardVersion[0]},
                    {"command.shardVersion.v": ShardVersioningUtil.kIgnoredShardVersion[0]},
                ]
            };
        }
        filter["command.pipeline.0.$indexStats"] = {$exists: false};
        const shard0Entries = shard0DB.system.profile.find(filter).toArray();
        const shard1Entries = shard1DB.system.profile.find(filter).toArray();
        assert.eq(shard0Entries.length + shard1Entries.length,
                  numEntries,
                  {shard0Entries: shard0Entries, shard1Entries: shard1Entries});
    }
    validateCommand((isUpdate || isDelete || isCollMod) ? cmdCollName : bucketsCollName,
                    numProfilerEntries.sharded);
    assert.commandWorked(mongos1.getCollection(collName).insert({[timeField]: ISODate()}));
    assert(mongos0.getCollection(collName).drop());
    assert.commandWorked(mongos0.createCollection(
        collName, {timeseries: {timeField: timeField, metaField: metaField}}));
    validateCommand(cmdCollName, numProfilerEntries.unsharded, true);
}
function runTest ( { shardKey , cmdObj , numProfilerEntries } ) { const isDelete = cmdObj [ "delete" ] !== undefined ; const isUpdate = cmdObj [ "update" ] !== undefined ; const isCollMod = cmdObj [ "collMod" ] !== undefined ; const cmdCollName = cmdObj [ Object . keys ( cmdObj ) [ 0 ] ] ; const shardKeyHasMetaField = shardKey [ metaField ] !== undefined ; assert . commandWorked ( mongos1 . getCollection ( collName ) . insert ( { [ timeField ] : ISODate ( ) } ) ) ; assert ( mongos0 . getCollection ( collName ) . drop ( ) ) ; assert . commandWorked ( mongos0 . createCollection ( collName , { timeseries : { timeField : timeField , metaField : metaField } } ) ) ; assert . commandWorked ( st . s0 . adminCommand ( { shardCollection : ` ${ dbName } ${ collName } ` , key : shardKey , } ) ) ; const middle = shardKeyHasMetaField ? { meta : 1 } : { "meta.a" : 1 } ; assert . commandWorked ( mongos0 . adminCommand ( { split : ` ${ dbName } ${ bucketsCollName } ` , middle } ) ) ; const primaryShard = st . getPrimaryShard ( dbName ) ; const otherShard = st . getOther ( primaryShard ) ; assert . commandWorked ( mongos0 . adminCommand ( { movechunk : ` ${ dbName } ${ bucketsCollName } ` , find : middle , to : otherShard . name , _waitForDelete : true } ) ) ; function validateCommand ( cmdCollName , numEntries , unVersioned ) { for ( let shardDB of [ shard0DB , shard1DB ] ) { shardDB . setProfilingLevel ( 0 ) ; shardDB . system . profile . drop ( ) ; shardDB . setProfilingLevel ( 2 ) ; } const res = mongos1 . runCommand ( cmdObj ) ; if ( numEntries > 0 ) { assert . commandWorked ( res ) ; } else { assert . commandFailed ( res ) ; } const queryField = ` ${ Object . keys ( cmdObj ) [ 0 ] } ` ; let filter = { [ queryField ] : cmdCollName , "$or" : [ { "$and" : [ { "command.shardVersion.0" : { "$exists" : true } } , { "command.shardVersion.0" : { $ne : ShardVersioningUtil . kIgnoredShardVersion [ 0 ] } } , ] } , { "$and" : [ { "command.shardVersion.v" : { "$exists" : true } } , { "command.shardVersion.v" : { $ne : ShardVersioningUtil . kIgnoredShardVersion [ 0 ] } } , ] } , ] } ; if ( isUpdate ) { filter = { "op" : "update" , "ns" : ` ${ dbName } ${ cmdCollName } ` , "ok" : { $ne : 0 } } ; } else if ( isDelete ) { filter = { "op" : "remove" , "ns" : ` ${ dbName } ${ cmdCollName } ` , "ok" : { $ne : 0 } } ; } else if ( isCollMod ) { const command = unVersioned ? "_shardsvrCollMod" : "_shardsvrCollModParticipant" ; filter = { [ ` ${ command } ` ] : cmdCollName , "ok" : { $ne : 0 } } ; } else if ( unVersioned && ! isCollMod ) { filter = { [ queryField ] : cmdCollName , "$or" : [ { "command.shardVersion.0" : ShardVersioningUtil . kIgnoredShardVersion [ 0 ] } , { "command.shardVersion.v" : ShardVersioningUtil . kIgnoredShardVersion [ 0 ] } , ] } ; } filter [ "command.pipeline.0.$indexStats" ] = { $exists : false } ; const shard0Entries = shard0DB . system . profile . find ( filter ) . toArray ( ) ; const shard1Entries = shard1DB . system . profile . find ( filter ) . toArray ( ) ; assert . eq ( shard0Entries . length + shard1Entries . length , numEntries , { shard0Entries : shard0Entries , shard1Entries : shard1Entries } ) ; } validateCommand ( ( isUpdate || isDelete || isCollMod ) ? cmdCollName : bucketsCollName , numProfilerEntries . sharded ) ; assert . commandWorked ( mongos1 . getCollection ( collName ) . insert ( { [ timeField ] : ISODate ( ) } ) ) ; assert ( mongos0 . getCollection ( collName ) . drop ( ) ) ; assert . commandWorked ( mongos0 . createCollection ( collName , { timeseries : { timeField : timeField , metaField : metaField } } ) ) ; validateCommand ( cmdCollName , numProfilerEntries . unsharded , true ) ; }
The test runs drop, create and shardCollection commands using mongos0, then validates the given
 command by running against mongos1 with stale config.
The test runs drop create and shardCollection commands using mongos0 then validates the given command by running against mongos1 with stale config .
// Insert some dummy data using 'mongos1' as the router, so that the cache is initialized on the // mongos while the collection is unsharded. // Drop and shard the collection with 'mongos0' as the router. // Move one of the chunks into the second shard. Note that we can only do this if the meta field // is part of the shard key. // Validate the command by running against 'mongos1' as the router. // Restart profiler. // We currently do not log 'shardVersion' for updates. See SERVER-60354 for details. // Filter out the profiler entries with $indexStats pipeline stage, as the // PeriodicShardedIndexConsistencyChecker can issue an aggregate command with // '[$indexStats: {}]' pipeline periodically. We don't want these entries to be accounted // here. // The update command is always logged as being on the user-provided namespace. // Insert dummy data so that the 'mongos1' sees the collection as sharded. // Drop and recreate an unsharded collection with 'mongos0' as the router. // When unsharded, the command should be run against the user requested namespace.