jstests/sharding/move_chunk_allowMigrations.js
// collBSetParams specify the field(s) that will be set on the collB in config.collections.
collBSetParams specify the field(s) that will be set on the collB in config.collections.
collBSetParams specify the field ( s ) that will be set on the collB in config . collections .
function testAllowMigrationsFalseDisablesBalancer(allowMigrations, collBSetNoBalanceParam) {
    jsTestLog(`Running AllowMigrationsFalseDisablesBalancer(${allowMigrations}, ${
        tojson(collBSetNoBalanceParam)})`);
    const dbName = setUpDatabaseAndEnableSharding('AllowMigrationsFalseDisablesBalancer');
    const collAName = "collA";
    const collBName = "collB";
    const collA = st.s.getCollection(`${dbName}.${collAName}`);
    const collB = st.s.getCollection(`${dbName}.${collBName}`);
    assert.commandWorked(st.s.adminCommand({shardCollection: collA.getFullName(), key: {_id: 1}}));
    assert.commandWorked(st.s.adminCommand({shardCollection: collB.getFullName(), key: {_id: 1}}));
    for (let coll of [collA, collB]) {
        coll.insert({_id: 1});
        coll.insert({_id: 10});
        coll.insert({_id: 20});
        coll.insert({_id: 30});
        assert.commandWorked(st.splitAt(coll.getFullName(), {_id: 10}));
        assert.commandWorked(st.splitAt(coll.getFullName(), {_id: 20}));
        assert.commandWorked(st.splitAt(coll.getFullName(), {_id: 30}));
        const balancerStatus = assert.commandWorked(
            st.s0.adminCommand({balancerCollectionStatus: coll.getFullName()}));
        assert.eq(balancerStatus.balancerCompliant, false);
        assert.eq(balancerStatus.firstComplianceViolation, 'chunksImbalance');
        assert.eq(4,
                  findChunksUtil
                      .findChunksByNs(configDB, coll.getFullName(), {shard: st.shard0.shardName})
                      .count());
    }
    assert.commandWorked(
        configDB.collections.update({_id: collB.getFullName()}, {$set: collBSetNoBalanceParam}));
    assert.commandWorked(st.configRS.getPrimary().adminCommand({
        _configsvrSetAllowMigrations: collB.getFullName(),
        allowMigrations: allowMigrations,
        writeConcern: {w: "majority"}
    }));
    st.startBalancer();
    assert.soon(() => {
        st.awaitBalancerRound();
        const shard0Chunks =
            findChunksUtil
                .findChunksByNs(configDB, collA.getFullName(), {shard: st.shard0.shardName})
                .itcount();
        const shard1Chunks =
            findChunksUtil
                .findChunksByNs(configDB, collA.getFullName(), {shard: st.shard1.shardName})
                .itcount();
        jsTestLog(`shard0 chunks ${shard0Chunks}, shard1 chunks ${shard1Chunks}`);
        return shard0Chunks == 2 && shard1Chunks == 2;
    }, `Balancer failed to balance ${collA.getFullName()}`, 1000 * 60 * 10);
    st.stopBalancer();
    const collABalanceStatus =
        assert.commandWorked(st.s.adminCommand({balancerCollectionStatus: collA.getFullName()}));
    assert.eq(collABalanceStatus.balancerCompliant, true);
    const collBBalanceStatus =
        assert.commandWorked(st.s.adminCommand({balancerCollectionStatus: collB.getFullName()}));
    assert.eq(collBBalanceStatus.balancerCompliant, false);
    assert.eq(collBBalanceStatus.firstComplianceViolation, 'chunksImbalance');
    assert.eq(
        4,
        findChunksUtil.findChunksByNs(configDB, collB.getFullName(), {shard: st.shard0.shardName})
            .count());
}
function testAllowMigrationsFalseDisablesBalancer ( allowMigrations , collBSetNoBalanceParam ) { jsTestLog ( ` ${ allowMigrations } ${ tojson ( collBSetNoBalanceParam ) } ` ) ; const dbName = setUpDatabaseAndEnableSharding ( 'AllowMigrationsFalseDisablesBalancer' ) ; const collAName = "collA" ; const collBName = "collB" ; const collA = st . s . getCollection ( ` ${ dbName } ${ collAName } ` ) ; const collB = st . s . getCollection ( ` ${ dbName } ${ collBName } ` ) ; assert . commandWorked ( st . s . adminCommand ( { shardCollection : collA . getFullName ( ) , key : { _id : 1 } } ) ) ; assert . commandWorked ( st . s . adminCommand ( { shardCollection : collB . getFullName ( ) , key : { _id : 1 } } ) ) ; for ( let coll of [ collA , collB ] ) { coll . insert ( { _id : 1 } ) ; coll . insert ( { _id : 10 } ) ; coll . insert ( { _id : 20 } ) ; coll . insert ( { _id : 30 } ) ; assert . commandWorked ( st . splitAt ( coll . getFullName ( ) , { _id : 10 } ) ) ; assert . commandWorked ( st . splitAt ( coll . getFullName ( ) , { _id : 20 } ) ) ; assert . commandWorked ( st . splitAt ( coll . getFullName ( ) , { _id : 30 } ) ) ; const balancerStatus = assert . commandWorked ( st . s0 . adminCommand ( { balancerCollectionStatus : coll . getFullName ( ) } ) ) ; assert . eq ( balancerStatus . balancerCompliant , false ) ; assert . eq ( balancerStatus . firstComplianceViolation , 'chunksImbalance' ) ; assert . eq ( 4 , findChunksUtil . findChunksByNs ( configDB , coll . getFullName ( ) , { shard : st . shard0 . shardName } ) . count ( ) ) ; } assert . commandWorked ( configDB . collections . update ( { _id : collB . getFullName ( ) } , { $set : collBSetNoBalanceParam } ) ) ; assert . commandWorked ( st . configRS . getPrimary ( ) . adminCommand ( { _configsvrSetAllowMigrations : collB . getFullName ( ) , allowMigrations : allowMigrations , writeConcern : { w : "majority" } } ) ) ; st . startBalancer ( ) ; assert . soon ( ( ) => { st . awaitBalancerRound ( ) ; const shard0Chunks = findChunksUtil . findChunksByNs ( configDB , collA . getFullName ( ) , { shard : st . shard0 . shardName } ) . itcount ( ) ; const shard1Chunks = findChunksUtil . findChunksByNs ( configDB , collA . getFullName ( ) , { shard : st . shard1 . shardName } ) . itcount ( ) ; jsTestLog ( ` ${ shard0Chunks } ${ shard1Chunks } ` ) ; return shard0Chunks == 2 && shard1Chunks == 2 ; } , ` ${ collA . getFullName ( ) } ` , 1000 * 60 * 10 ) ; st . stopBalancer ( ) ; const collABalanceStatus = assert . commandWorked ( st . s . adminCommand ( { balancerCollectionStatus : collA . getFullName ( ) } ) ) ; assert . eq ( collABalanceStatus . balancerCompliant , true ) ; const collBBalanceStatus = assert . commandWorked ( st . s . adminCommand ( { balancerCollectionStatus : collB . getFullName ( ) } ) ) ; assert . eq ( collBBalanceStatus . balancerCompliant , false ) ; assert . eq ( collBBalanceStatus . firstComplianceViolation , 'chunksImbalance' ) ; assert . eq ( 4 , findChunksUtil . findChunksByNs ( configDB , collB . getFullName ( ) , { shard : st . shard0 . shardName } ) . count ( ) ) ; }
collBSetParams specify the field(s) that will be set on the collB in config.collections.
collBSetParams specify the field ( s ) that will be set on the collB in config . collections .
// Split both collections into 4 chunks so balancing can occur. // Confirm the chunks are initially unbalanced. All chunks should start out on shard0 // (primary shard for the database). // Test that collB remains unbalanced.