MongoDB를위한 유지 보수 스크립트를 작성하여 복제 세트의 콜렉션을 스케줄에 맞게 압축합니다. 내가 지금까지 가지고있는 스크립트는 1 차 및 2 차 노드에 대해 수행 할 것으로 기대되는 작업을 수행하는 데 적합합니다. 그러나 문제가 있습니다 :MongoDB 유지 보수
MongoDB Replica Set를 사용하는 시스템은 끊임없이 쓰고 읽는 고 가용성 웹 대기열입니다. 따라서 rs.StepDown()을 호출 할 때 몇 초의 다운 타임조차도 절대적으로 용납되지 않습니다. 수백 개의 클라이언트로부터 MongoExceptions없이 주 노드를 안전하게 강등시킬 수있는 방법이 있습니까?
감사합니다.
p.s. 여기에 한 달
// assuming we are a replica set ;-)
if(rs.isMaster().setName){
try {
//check if the script is run against a master
if(rs.isMaster().ismaster){ //if so, step down as master
print("Connected to a PRIMARY node")
print("Will try to step down as primary");
rs.stepDown();
// after stepdown connections are dropped. do an operation to cause reconnect:
rs.isMaster();
// now ready to go.
// wait for another node to become primary -- it may need data from us for the last
// small sliver of time, and if we are already compacting it cannot get it while the
// compaction is running.
var counter = 1;
while(1) {
var m = rs.isMaster();
if(m.ismaster) {
print("ERROR: no one took over during our stepDown duration. we are primary again!");
assert(false);
}
if(m.primary){ // someone else is, great
print("new master host is: "+m.primary);
break;
}
print("waiting "+counter+" seconds");
sleep(1000);
counter++;
}
}else{
print("Connected to a SECONDARY node");
print("Going into Recovery Mode and Compacting");
}
// someone else is primary, so we are ready to proceed with a compaction
print(" ");
print("Compacting...");
print("- queue");
printjson(db.runCommand({compact:"queue"}));
print(" ");
} catch(e) {
print(" ");
print("ACHTUNG! Exception:" + e);
print(" ");
}
}else{
print('This script works with replica sets only');
}