您好,登錄后才能下訂單哦!
先看下幾種典型狀況下的db.settings.find({'_id':'balancer'})
輸出:
1.創(chuàng)建mongos之后,從未設(shè)置balancer時(shí):
mongos> var x = db.settings.findOne({'_id':'balancer'})
mongos> x == null
true
mongos> sh.getBalancerState()
true
2.創(chuàng)建了mongos之后,因故手動(dòng)關(guān)閉了balancer
mongos> db.settings.findOne({'_id':'balancer'})
{ "_id" : "balancer", "mode" : "off", "stopped" : true }
mongos> sh.getBalancerState()
false
3.設(shè)置了balancer的運(yùn)行時(shí)間段,但當(dāng)前時(shí)間不在其中
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.設(shè)置了balancer的運(yùn)行時(shí)間段,當(dāng)前時(shí)間在其中
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不是默認(rèn)的config庫(kù)的情況
2.x == null
代表了上面的從未設(shè)置balancer,默認(rèn)開啟的狀況
3.對(duì)返回值中的.stopped項(xiàng)進(jìn)行取反,得到是否正在運(yùn)行
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;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。