jstests/sharding/shard_collection_existing_zones.js
/**
 * Tests that a non-empty collection associated with zones can be sharded.
 */
Tests that a non-empty collection associated with zones can be sharded.
Tests that a non - empty collection associated with zones can be sharded .
function testNonemptyZonedCollection() {
    var shardKey = {x: 1};
    var shards = configDB.shards.find().toArray();
    var testColl = testDB.getCollection(kCollName);
    var ranges =
        [{min: {x: 0}, max: {x: 10}}, {min: {x: 10}, max: {x: 20}}, {min: {x: 20}, max: {x: 40}}];
    for (let i = 0; i < 40; i++) {
        assert.commandWorked(testColl.insert({x: i}));
    }
    assert.commandWorked(testColl.createIndex(shardKey));
    for (let i = 0; i < shards.length; i++) {
        assert.commandWorked(
            mongos.adminCommand({addShardToZone: shards[i]._id, zone: zoneName + i}));
        assert.commandWorked(mongos.adminCommand(
            {updateZoneKeyRange: ns, min: ranges[i].min, max: ranges[i].max, zone: zoneName + i}));
    }
    assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: shardKey}));
    assert.eq(1, findChunksUtil.countChunksForNs(configDB, ns));
    st.startBalancer();
    assert.soon(() => {
        let res = findChunksUtil.countChunksForNs(configDB, ns);
        return res === 5;
    }, 'balancer never ran', 10 * 60 * 1000, 1000);
    assert.commandWorked(testDB.runCommand({drop: kCollName}));
}
function testNonemptyZonedCollection ( ) { var shardKey = { x : 1 } ; var shards = configDB . shards . find ( ) . toArray ( ) ; var testColl = testDB . getCollection ( kCollName ) ; var ranges = [ { min : { x : 0 } , max : { x : 10 } } , { min : { x : 10 } , max : { x : 20 } } , { min : { x : 20 } , max : { x : 40 } } ] ; for ( let i = 0 ; i < 40 ; i ++ ) { assert . commandWorked ( testColl . insert ( { x : i } ) ) ; } assert . commandWorked ( testColl . createIndex ( shardKey ) ) ; for ( let i = 0 ; i < shards . length ; i ++ ) { assert . commandWorked ( mongos . adminCommand ( { addShardToZone : shards [ i ] . _id , zone : zoneName + i } ) ) ; assert . commandWorked ( mongos . adminCommand ( { updateZoneKeyRange : ns , min : ranges [ i ] . min , max : ranges [ i ] . max , zone : zoneName + i } ) ) ; } assert . commandWorked ( mongos . adminCommand ( { shardCollection : ns , key : shardKey } ) ) ; assert . eq ( 1 , findChunksUtil . countChunksForNs ( configDB , ns ) ) ; st . startBalancer ( ) ; assert . soon ( ( ) => { let res = findChunksUtil . countChunksForNs ( configDB , ns ) ; return res === 5 ; } , 'balancer never ran' , 10 * 60 * 1000 , 1000 ) ; assert . commandWorked ( testDB . runCommand ( { drop : kCollName } ) ) ; }
Tests that a non-empty collection associated with zones can be sharded.
Tests that a non - empty collection associated with zones can be sharded .
// Check that there is initially 1 chunk. // Check that the chunks were moved properly.