jstests/sharding/compound_hashed_shard_key_zoning.js
/**
 * Test that 'updateZoneKeyRange' works correctly by verifying 'tags' collection, after sharding the
 * collection.
 */
Test that 'updateZoneKeyRange' works correctly by verifying 'tags' collection, after sharding the
collection.
Test that ' updateZoneKeyRange ' works correctly by verifying ' tags ' collection after sharding the collection .
function testZoningAfterSharding(namespace, shardKey, NumberType) {
    assert.commandWorked(st.s.adminCommand({shardCollection: namespace, key: shardKey}));
    if (shardKey.x === "hashed") {
        assert.commandFailedWithCode(
            st.s.adminCommand(
                {updateZoneKeyRange: namespace, min: {x: 0}, max: {x: 10}, zone: 'zoneName'}),
            ErrorCodes.InvalidOptions);
    }
    assert.commandWorked(st.s.adminCommand({
        updateZoneKeyRange: namespace,
        min: {x: NumberType(0)},
        max: {x: NumberType(10)},
        zone: 'zoneName'
    }));
    let tagDoc = configDB.tags.findOne();
    assert.eq(namespace, tagDoc.ns);
    assert.eq(fillMissingShardKeyFields(shardKey, {x: NumberType(0)}), tagDoc.min);
    assert.eq(fillMissingShardKeyFields(shardKey, {x: NumberType(10)}), tagDoc.max);
    assert.eq('zoneName', tagDoc.tag);
    assert.commandFailedWithCode(st.s.adminCommand({
        updateZoneKeyRange: namespace,
        min: {x: NumberType(-10)},
        max: {x: NumberType(20)},
        zone: 'zoneName'
    }),
                                 ErrorCodes.RangeOverlapConflict);
    assert.commandFailedWithCode(st.s.adminCommand({
        updateZoneKeyRange: namespace,
        min: {newField: NumberType(-10)},
        max: {newField: NumberType(20)},
        zone: 'zoneName'
    }),
                                 ErrorCodes.ShardKeyNotFound);
    tagDoc = configDB.tags.findOne();
    assert.eq(namespace, tagDoc.ns);
    assert.eq(fillMissingShardKeyFields(shardKey, {x: NumberType(0)}), tagDoc.min);
    assert.eq(fillMissingShardKeyFields(shardKey, {x: NumberType(10)}), tagDoc.max);
    assert.eq('zoneName', tagDoc.tag);
    assert.commandWorked(st.s.adminCommand({
        updateZoneKeyRange: namespace,
        min: fillMissingShardKeyFields(shardKey, {x: NumberType(0)}, MinKey),
        max: fillMissingShardKeyFields(shardKey, {x: NumberType(10)}, MinKey),
        zone: null
    }));
    assert.eq(null, configDB.tags.findOne());
    const zone = {
        _id: 0,
        ns: namespace,
        min: fillMissingShardKeyFields(shardKey, {x: 0}, MinKey),
        max: fillMissingShardKeyFields(shardKey, {x: 10}, MinKey),
        zone: "zoneName"
    };
    assert.commandWorked(configDB.tags.update({_id: 0}, zone, {upsert: true}));
    assert.eq(zone, configDB.tags.findOne());
    assert.commandWorked(st.s.adminCommand({
        updateZoneKeyRange: namespace,
        min: fillMissingShardKeyFields(shardKey, {x: 0}, MinKey),
        max: fillMissingShardKeyFields(shardKey, {x: 10}, MinKey),
        zone: null
    }));
    assert.eq(null, configDB.tags.findOne());
}
function testZoningAfterSharding ( namespace , shardKey , NumberType ) { assert . commandWorked ( st . s . adminCommand ( { shardCollection : namespace , key : shardKey } ) ) ; if ( shardKey . x === "hashed" ) { assert . commandFailedWithCode ( st . s . adminCommand ( { updateZoneKeyRange : namespace , min : { x : 0 } , max : { x : 10 } , zone : 'zoneName' } ) , ErrorCodes . InvalidOptions ) ; } assert . commandWorked ( st . s . adminCommand ( { updateZoneKeyRange : namespace , min : { x : NumberType ( 0 ) } , max : { x : NumberType ( 10 ) } , zone : 'zoneName' } ) ) ; let tagDoc = configDB . tags . findOne ( ) ; assert . eq ( namespace , tagDoc . ns ) ; assert . eq ( fillMissingShardKeyFields ( shardKey , { x : NumberType ( 0 ) } ) , tagDoc . min ) ; assert . eq ( fillMissingShardKeyFields ( shardKey , { x : NumberType ( 10 ) } ) , tagDoc . max ) ; assert . eq ( 'zoneName' , tagDoc . tag ) ; assert . commandFailedWithCode ( st . s . adminCommand ( { updateZoneKeyRange : namespace , min : { x : NumberType ( - 10 ) } , max : { x : NumberType ( 20 ) } , zone : 'zoneName' } ) , ErrorCodes . RangeOverlapConflict ) ; assert . commandFailedWithCode ( st . s . adminCommand ( { updateZoneKeyRange : namespace , min : { newField : NumberType ( - 10 ) } , max : { newField : NumberType ( 20 ) } , zone : 'zoneName' } ) , ErrorCodes . ShardKeyNotFound ) ; tagDoc = configDB . tags . findOne ( ) ; assert . eq ( namespace , tagDoc . ns ) ; assert . eq ( fillMissingShardKeyFields ( shardKey , { x : NumberType ( 0 ) } ) , tagDoc . min ) ; assert . eq ( fillMissingShardKeyFields ( shardKey , { x : NumberType ( 10 ) } ) , tagDoc . max ) ; assert . eq ( 'zoneName' , tagDoc . tag ) ; assert . commandWorked ( st . s . adminCommand ( { updateZoneKeyRange : namespace , min : fillMissingShardKeyFields ( shardKey , { x : NumberType ( 0 ) } , MinKey ) , max : fillMissingShardKeyFields ( shardKey , { x : NumberType ( 10 ) } , MinKey ) , zone : null } ) ) ; assert . eq ( null , configDB . tags . findOne ( ) ) ; const zone = { _id : 0 , ns : namespace , min : fillMissingShardKeyFields ( shardKey , { x : 0 } , MinKey ) , max : fillMissingShardKeyFields ( shardKey , { x : 10 } , MinKey ) , zone : "zoneName" } ; assert . commandWorked ( configDB . tags . update ( { _id : 0 } , zone , { upsert : true } ) ) ; assert . eq ( zone , configDB . tags . findOne ( ) ) ; assert . commandWorked ( st . s . adminCommand ( { updateZoneKeyRange : namespace , min : fillMissingShardKeyFields ( shardKey , { x : 0 } , MinKey ) , max : fillMissingShardKeyFields ( shardKey , { x : 10 } , MinKey ) , zone : null } ) ) ; assert . eq ( null , configDB . tags . findOne ( ) ) ; }
Test that 'updateZoneKeyRange' works correctly by verifying 'tags' collection, after sharding the
 collection.
Test that ' updateZoneKeyRange ' works correctly by verifying ' tags ' collection after sharding the collection .
// Cannot assign with a non-NumberLong range value on a hashed shard key field. // Testing basic assign. // Cannot assign overlapping ranges // Cannot have non-shard key fields in tag range. // Testing basic remove. // Insert directly into the tags collection. // Remove works on entries inserted directly into the tags collection, even when those entries // do not adhere to the updateZoneKeyRange command requirement of having a NumberLong range // value for a hashed shard key field.