2012-01-17 5 views
4

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'); 
} 

답변

5

일단 저 부하 시간에 크론 작업을 통해 실행해야 스크립트의 실제 버전은 안전의 수백 MongoExceptions없이 기본 노드를 내릴 수있는 방법이 있습니다 고객?

아니오. MongoDB는 "예상 전환" 또는 "유지 관리 변경"의 형식이 아닙니다.

이 압축주기를 수행하는 것이 올바른 일입니다. 하지만 기본 유지를 위해 유지 관리 기간을 기다려야합니다.

...은 고 가용성 웹 대기열입니다.

여기에 Capped Collections을 사용하고 있습니까? 캐핑 한 콜렉션은 압축 할 필요가 없습니다. 캡 된 컬렉션의 개체는 크기를 조정할 수 없지만 일반적으로 큐 개체에는 문제가되지 않습니다.

이상적인 해결책은 아니지만 문제를 해결할 수는 있습니다.