jstests/sharding/shard_collection_existing_zones.js
/**
 * Test that shardCollection uses existing zone ranges to split chunks.
 */
Test that shardCollection uses existing zone ranges to split chunks.
Test that shardCollection uses existing zone ranges to split chunks .
function testChunkSplits(collectionExists) {
    var shardKey = {x: 1};
    var ranges =
        [{min: {x: 0}, max: {x: 10}}, {min: {x: 10}, max: {x: 20}}, {min: {x: 30}, max: {x: 40}}];
    var shards = configDB.shards.find().toArray();
    assert.eq(ranges.length, shards.length);
    if (collectionExists) {
        assert.commandWorked(testDB.foo.createIndex(shardKey));
    }
    for (var i = 0; i < shards.length; i++) {
        assert.commandWorked(
            st.s.adminCommand({addShardToZone: shards[i]._id, zone: zoneName + i}));
        assert.commandWorked(st.s.adminCommand(
            {updateZoneKeyRange: ns, min: ranges[i].min, max: ranges[i].max, zone: zoneName + i}));
    }
    assert.eq(
        configDB.tags.find().count(), shards.length, "failed to create tag documents correctly");
    assert.eq(configDB.chunks.find({ns: ns}).count(),
              0,
              "expect to see no chunk documents for the collection before shardCollection is run");
    assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: shardKey}));
    var expectedChunks = [
        {range: [{x: {"$minKey": 1}}, {x: 0}], shardId: st.shard0.shardName},
        {range: [{x: 0}, {x: 10}], shardId: st.shard0.shardName},  
        {range: [{x: 10}, {x: 20}], shardId: st.shard1.shardName},
        {range: [{x: 20}, {x: 30}], shardId: st.shard1.shardName},  
        {range: [{x: 30}, {x: 40}], shardId: st.shard2.shardName},  
        {range: [{x: 40}, {x: {"$maxKey": 1}}], shardId: st.shard2.shardName}
    ];
    var chunkDocs = findChunksUtil.findChunksByNs(configDB, ns).sort({min: 1}).toArray();
    assert.eq(chunkDocs.length,
              expectedChunks.length,
              "shardCollection failed to create chunk documents correctly");
    for (var i = 0; i < chunkDocs.length; i++) {
        var errMsg = "expect to see chunk " + tojson(expectedChunks[i]) + " but found chunk " +
            tojson(chunkDocs[i]);
        assert.eq(expectedChunks[i].range[0], chunkDocs[i].min, errMsg);
        assert.eq(expectedChunks[i].range[1], chunkDocs[i].max, errMsg);
        assert.eq(expectedChunks[i].shardId, chunkDocs[i].shard, errMsg);
    }
    assert.commandWorked(testDB.runCommand({drop: kCollName}));
}
function testChunkSplits ( collectionExists ) { var shardKey = { x : 1 } ; var ranges = [ { min : { x : 0 } , max : { x : 10 } } , { min : { x : 10 } , max : { x : 20 } } , { min : { x : 30 } , max : { x : 40 } } ] ; var shards = configDB . shards . find ( ) . toArray ( ) ; assert . eq ( ranges . length , shards . length ) ; if ( collectionExists ) { assert . commandWorked ( testDB . foo . createIndex ( shardKey ) ) ; } for ( var i = 0 ; i < shards . length ; i ++ ) { assert . commandWorked ( st . s . adminCommand ( { addShardToZone : shards [ i ] . _id , zone : zoneName + i } ) ) ; assert . commandWorked ( st . s . adminCommand ( { updateZoneKeyRange : ns , min : ranges [ i ] . min , max : ranges [ i ] . max , zone : zoneName + i } ) ) ; } assert . eq ( configDB . tags . find ( ) . count ( ) , shards . length , "failed to create tag documents correctly" ) ; assert . eq ( configDB . chunks . find ( { ns : ns } ) . count ( ) , 0 , "expect to see no chunk documents for the collection before shardCollection is run" ) ; assert . commandWorked ( mongos . adminCommand ( { shardCollection : ns , key : shardKey } ) ) ; var expectedChunks = [ { range : [ { x : { "$minKey" : 1 } } , { x : 0 } ] , shardId : st . shard0 . shardName } , { range : [ { x : 0 } , { x : 10 } ] , shardId : st . shard0 . shardName } , { range : [ { x : 10 } , { x : 20 } ] , shardId : st . shard1 . shardName } , { range : [ { x : 20 } , { x : 30 } ] , shardId : st . shard1 . shardName } , { range : [ { x : 30 } , { x : 40 } ] , shardId : st . shard2 . shardName } , { range : [ { x : 40 } , { x : { "$maxKey" : 1 } } ] , shardId : st . shard2 . shardName } ] ; var chunkDocs = findChunksUtil . findChunksByNs ( configDB , ns ) . sort ( { min : 1 } ) . toArray ( ) ; assert . eq ( chunkDocs . length , expectedChunks . length , "shardCollection failed to create chunk documents correctly" ) ; for ( var i = 0 ; i < chunkDocs . length ; i ++ ) { var errMsg = "expect to see chunk " + tojson ( expectedChunks [ i ] ) + " but found chunk " + tojson ( chunkDocs [ i ] ) ; assert . eq ( expectedChunks [ i ] . range [ 0 ] , chunkDocs [ i ] . min , errMsg ) ; assert . eq ( expectedChunks [ i ] . range [ 1 ] , chunkDocs [ i ] . max , errMsg ) ; assert . eq ( expectedChunks [ i ] . shardId , chunkDocs [ i ] . shard , errMsg ) ; } assert . commandWorked ( testDB . runCommand ( { drop : kCollName } ) ) ; }
Test that shardCollection uses existing zone ranges to split chunks.
Test that shardCollection uses existing zone ranges to split chunks .
// create zones: // shard0 - zonename0 - [0, 10) // shard1 - zonename0 - [10, 20) // shard2 - zonename0 - [30, 40) // shard the collection and validate the resulting chunks // pre-defined // pre-defined // pre-defined