2012-01-17 62 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对象通常不是问题。

虽然不是一个理想的解决方案,但它可能会解决您的问题。