先看下几种典型状况下的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,
"activeWindow" : {
"start" : "00:00",
"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;
}