/**
* Test that mongos always ensures the sharded output collection of a mapReduce is written to the
* sharding catalog (even if the mongos was stale and believed the sharded output collection
* already existed, but the collection had been dropped from another mongos).
*/
(function() {
"use strict";
const dbName = "test";
const inputCollName = "inputColl";
const outputCollName = "outputColl";
const outputNs = dbName + "." + outputCollName;
const st = new ShardingTest({mongos: 2, config: 1, shards: 1, rs: {nodes: 1}});
const staleMongos = st.s0;
const freshMongos = st.s1;
jsTest.log("Insert some data into the input collection");
var bulk = staleMongos.getDB(dbName).getCollection(inputCollName).initializeUnorderedBulkOp();
for (let j = 0; j < 100; j++) {
for (let i = 0; i < 512; i++) {
bulk.insert({j: j, i: i});
}
}
assert.writeOK(bulk.execute());
jsTest.log("Use the stale mongos to initially shard the output collection.");
assert.commandWorked(staleMongos.adminCommand({enableSharding: dbName}));
assert.commandWorked(staleMongos.adminCommand({shardCollection: outputNs, key: {_id: 1}}));
jsTest.log("Force the stale mongos to refresh its routing table cache for the output coll");
assert.commandWorked(staleMongos.getDB(dbName).runCommand({find: outputCollName}));
jsTest.log("Drop the sharded output collection from the fresh mongos.");
assert.commandWorked(freshMongos.getDB(dbName).runCommand({drop: outputCollName}));
jsTest.log("Run a mapReduce with the input coll and sharded output coll from the staleMongos.");
const map = function() {
emit(this.i, 1);
};
const reduce = function(key, values) {
return Array.sum(values);
};
assert.commandWorked(staleMongos.getDB(dbName).runCommand({
mapReduce: inputCollName,
map: map,
reduce: reduce,
out: {replace: outputCollName, sharded: true}
}));
jsTest.log("Check that the output collection was re-sharded by the stale mongos.");
const resArr =
staleMongos.getDB("config").getCollection("collections").find({_id: outputNs}).toArray();
assert.eq(1, resArr.length);
st.stop();
})();