您好,登錄后才能下訂單哦!
先看下幾種典型狀況下的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;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。