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

前端之家收集整理的这篇文章主要介绍了【NoSQL】mongo_detail.py中均衡器信息的处理思路前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

1.创建mongos之后,从未设置balancer时:

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

2.创建了mongos之后,因故手动关闭了balancer

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

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

  1. mongos> var x = db.settings.findOne({'_id':'balancer'})
  2. mongos> x
  3. {
  4. "_id" : "balancer","stopped" : true,"activeWindow" : {
  5. "start" : "00:00","stop" : "06:00"
  6. }
  7. }
  8. mongos> sh.getBalancerState()
  9. false

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

  1. mongos> var x = db.settings.findOne({'_id':'balancer'})
  2. mongos> x
  3. {
  4. "_id" : "balancer","stopped" : false,"stop" : "22:00"
  5. }
  6. }
  7. mongos> sh.getBalancerState()
  8. true

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

  1. mongos> sh.getBalancerState
  2. function (configDB) {
  3. if (configDB === undefined)
  4. configDB = sh._getConfigDB();
  5. var x = configDB.settings.findOne({_id: "balancer"});
  6. if (x == null)
  7. return true;
  8. return !x.stopped;
  9. }

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

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

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

  1. mongos> sh.isBalancerRunning
  2. function (configDB) {
  3. if (configDB === undefined)
  4. configDB = sh._getConfigDB();
  5. var x = configDB.locks.findOne({_id: "balancer"});
  6. if (x == null) {
  7. print("config.locks collection empty or missing. be sure you are connected to a mongos");
  8. return false;
  9. }
  10. return x.state > 0;
  11. }

猜你在找的NoSQL相关文章