2012-01-17 14 views
4

MongoDBのメンテナンススクリプトを作成しています。これは、スケジュールに基づいてレプリカセットからコレクションを圧縮します。私が今までに持っていたスクリプトは、プライマリとセカンダリのノードに対して行うことが期待される仕事をしているように見えます。ただし、問題があります。MongoDBメンテナンス

MongoDBレプリカセットを使用しているシステムは、可用性の高いWebキューです。このキューは常に書き込みと読み取りが行われます。したがって、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

いったん低負荷の回でのcronジョブ経由で実行する必要があり、スクリプトの実際のバージョンは、安全の数百からMongoExceptionsせずに、プライマリノードを降格する方法はありますされますクライアント?

いいえ。 MongoDBには、"予期的な切り替え"または"メンテナンスの切り替え"という形式はありません。

コンパクションのこのサイクルを行うことは、正しいことです。しかし、プライマリを圧縮するためにメンテナンスウィンドウを待たなければなりません。

...は高可用性のWebキューです。

これにはCapped Collectionsを使用していますか?キャッピングされたコレクションは圧縮する必要はありません。キャップ付きコレクション内のオブジェクトのサイズを変更することはできませんが、通常はキューオブジェクトには問題ありません。

あなたの問題を解決するのに理想的な解決策ではありませんが、

関連する問題