【ToolsForMongo】mongo_detail.py中均衡器信息的处理思路
先看下几种典型状况下的db.settings.find({'_id':'balancer'})
输出:
1.创建mongos之后,从未设置balancer时:
mongos> var x = db.settings.findOne({'_id':'balancer'}) mongos> x == null true mongos> sh.getBalancerState() true
2.创建了mongos之后,因故手动关闭了balancer
mongos> db.settings.findOne({'_id':'balancer'}) { "_id" : "balancer","mode" : "off","stopped" : true } mongos> sh.getBalancerState() false
3.设置了balancer的运行时间段,但当前时间不在其中
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的运行时间段,当前时间在其中
mongos> var x = db.settings.findOne({'_id':'balancer'}) mongos> x { "_id" : "balancer","stopped" : false,"stop" : "22:00" } } mongos> sh.getBalancerState() true
再看下官方mongo shell中的js代码
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库的情况
2.x == null
代表了上面的从未设置balancer,默认开启的状况
3.对返回值中的.stopped项进行取反,得到是否正在运行
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; }