2012-01-17 74 views
4

我正在爲MongoDB編寫一個維護腳本,它將按照計劃壓縮來自副本集的集合。迄今爲止,我所看到的腳本看起來不錯,可以完成它針對主節點和次節點所做的工作。然而,存在一個問題:MongoDB維護

使用MongoDB副本集的系統是一個高可用性Web隊列,它不斷被寫入和讀取。因此,即使調用rs.StepDown()幾秒鐘的停機時間也絕對不可接受。有沒有一種方法可以安全降級數百個客戶端的沒有MongoException的主節點?

謝謝!

p.s.這裏是一個應該通過一個cron作業,在低負載時每月一次

// 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沒有任何形式的「預期轉換」「維護轉換」

做這個壓縮循環是正確的事情。但是你將不得不等待維護窗口來壓縮主服務器。

...是一個高可用性Web隊列。

您使用的是Capped Collections嗎?上限收藏不需​​要壓縮。封頂集合中的對象無法調整大小,但對於Queue對象通常不是問題。

雖然不是一個理想的解決方案,但它可能會解決您的問題。