jstests/sharding/compound_hashed_shard_key_zoning.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() {
    const shardKey = {x: 1, y: "hashed"};
    const shards = configDB.shards.find().toArray();
    const testColl = testDB.getCollection(kCollName);
    const ranges = [
        {min: {x: 0, y: MinKey}, max: {x: 10, y: MaxKey}},
        {min: {x: 10, y: MaxKey}, max: {x: 20, y: MinKey}},
        {min: {x: 20, y: MinKey}, max: {x: 40, y: MaxKey}}
    ];
    for (let i = 0; i < 40; i++) {
        assert.commandWorked(testColl.insert({x: 1, y: Math.random()}));
    }
    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(() => findChunksUtil.countChunksForNs(configDB, ns) === 5,
                'balancer never ran',
                5 * 60 * 1000,
                1000);
    assert.commandWorked(testDB.runCommand({drop: kCollName}));
}
function testNonemptyZonedCollection ( ) { const shardKey = { x : 1 , y : "hashed" } ; const shards = configDB . shards . find ( ) . toArray ( ) ; const testColl = testDB . getCollection ( kCollName ) ; const ranges = [ { min : { x : 0 , y : MinKey } , max : { x : 10 , y : MaxKey } } , { min : { x : 10 , y : MaxKey } , max : { x : 20 , y : MinKey } } , { min : { x : 20 , y : MinKey } , max : { x : 40 , y : MaxKey } } ] ; for ( let i = 0 ; i < 40 ; i ++ ) { assert . commandWorked ( testColl . insert ( { x : 1 , y : Math . random ( ) } ) ) ; } 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 ( ( ) => findChunksUtil . countChunksForNs ( configDB , ns ) === 5 , 'balancer never ran' , 5 * 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.