sh.startBalancer(超时,间隔)参数的含义?

时间:2016-06-30 11:51:55

标签: mongodb

在mongo db命令中sh.startBalancer(超时,间隔) 根据文件: timeout整数等待的毫秒数。 interval integer每个等待休眠周期的毫秒数。 有人可以详细解释参数含义。运行sh.startBalancer()时默认值是什么?

1 个答案:

答案 0 :(得分:0)

sh.startBalancer(超时,间隔)将做两件事:

  1. 更新config.settings {_id:'balancer'}文档,以允许平衡器运行。
  2. 检查每个“间隔”毫秒是否已获取分布式锁定,表明平衡器正在运行。它将执行此操作,直到指定'timeout'毫秒为止,此时此方法将断言。
  3. 即使您达到超时,config.settings更新仍会在启用平衡器的情况下保持不变。

    如果您对查看代码感兴趣,可以在没有括号和参数的情况下运行sh.startBalancer,对它调用的函数执行相同的操作。以下是MongoDB 3.2.6:

    > sh.startBalancer
    function (timeout, interval) {
        sh.setBalancerState(true);
        sh.waitForBalancer(true, timeout, interval);
    }
    
    > sh.setBalancerState
    function (onOrNot) {
        sh._getConfigDB().settings.update(
        {_id: "balancer"}, {$set: {stopped: onOrNot ? false : true}}, true);
    }
    
    > sh.waitForBalancer
    function (onOrNot, timeout, interval) {
    
        // If we're waiting for the balancer to turn on or switch state or
        // go to a particular state
        if (onOrNot) {
            // Just wait for the balancer lock to change, can't ensure we'll ever see it
            // actually locked
            sh.waitForDLock("balancer", undefined, timeout, interval);
        } else {
            // Otherwise we need to wait until we're sure balancing stops
            sh.waitForBalancerOff(timeout, interval);
        }
    
    }
    
    > sh.waitForDLock
    function (lockId, onOrNot, timeout, interval) {
        // Wait for balancer to be on or off
        // Can also wait for particular balancer state
        var state = onOrNot;
        var configDB = sh._getConfigDB();
    
        var beginTS = undefined;
        if (state == undefined) {
            var currLock = configDB.locks.findOne({_id: lockId});
            if (currLock != null)
                beginTS = currLock.ts;
        }
    
        var lockStateOk = function() {
            var lock = configDB.locks.findOne({_id: lockId});
    
            if (state == false)
                return !lock || lock.state == 0;
            if (state == true)
                return lock && lock.state == 2;
            if (state == undefined)
                return (beginTS == undefined && lock) ||
                    (beginTS != undefined && (!lock || lock.ts + "" != beginTS + ""));
            else
                return lock && lock.state == state;
        };
    
        assert.soon(
            lockStateOk,
            "Waited too long for lock " + lockId + " to " +
                (state == true ? "lock" : (state == false ? "unlock" : "change to state " + state)),
            timeout,
            interval);
    }