【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;
}
相關文章
相關標籤/搜索