【NoSQL】mongo_detail.py中均衡器信息的处理思路

【ToolsForMongo】mongo_detail.py中均衡器信息的处理思路

先看下几种典型情况下的db.settings.find({'_id':'balancer'})输出:javascript

1.建立mongos以后,从未设置balancer时:java

mongos> var x = db.settings.findOne({'_id':'balancer'})
mongos> x == null
true
mongos> sh.getBalancerState()
true

2.建立了mongos以后,因故手动关闭了balancershell

mongos> db.settings.findOne({'_id':'balancer'})
{ "_id" : "balancer", "mode" : "off", "stopped" : true }
mongos> sh.getBalancerState()
false

3.设置了balancer的运行时间段,但当前时间不在其中ide

mongos>  var x = db.settings.findOne({'_id':'balancer'})
mongos> x
{
    "_id" : "balancer",
    "stopped" : true,
    "activeWindow" : {
        "start" : "00:00",
        "stop" : "06:00"
    }
}
mongos> sh.getBalancerState()
false

4.设置了balancer的运行时间段,当前时间在其中code

mongos> var x = db.settings.findOne({'_id':'balancer'})
mongos> x
{
    "_id" : "balancer",
    "stopped" : false,
    "activeWindow" : {
        "start" : "00:00",
        "stop" : "22:00"
    }
}
mongos> sh.getBalancerState()
true

再看下官方mongo shell中的js代码ip

mongos> sh.getBalancerState
function (configDB) {
    if (configDB === undefined)
        configDB = sh._getConfigDB();
    var x = configDB.settings.findOne({_id: "balancer"});
    if (x == null)
        return true;
    return !x.stopped;
}

1.先处理了configDB不是默认的config库的状况get

2.x == null表明了上面的从未设置balancer,默认开启的情况it

3.对返回值中的.stopped项进行取反,获得是否正在运行io

mongos> sh.isBalancerRunning
function (configDB) {
    if (configDB === undefined)
        configDB = sh._getConfigDB();
    var x = configDB.locks.findOne({_id: "balancer"});
    if (x == null) {
        print("config.locks collection empty or missing. be sure you are connected to a mongos");
        return false;
    }
    return x.state > 0;
}
相关文章
相关标签/搜索