gpt4 book ai didi

MongoDB维护

转载 作者:可可西里 更新时间:2023-11-01 09:12:37 25 4
gpt4 key购买 nike

我正在为 MongoDB 编写一个维护脚本,它将按计划压缩副本集中的集合。到目前为止,我的脚本看起来不错,可以针对主节点和辅助节点完成预期的工作。但是有一个问题:

使用 MongoDB 副本集的系统是一个高可用性网络队列,它不断地被写入和读取。因此,即使调用 rs.StepDown() 时出现几秒钟的停机时间也是绝对不能接受的。有没有办法安全地降级主节点,而不会出现来自数百个客户端的 MongoException?

谢谢!

附注这是脚本的实际版本,应该每月在低负载时间通过 cron-job 运行一次

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

最佳答案

Is there a way to safely demote a Primary node without MongoExceptions from the hundreds of clients?

。 MongoDB 没有任何形式的“预期转换”“维护转换”

执行此压缩循环是正确的做法。但是您将不得不等待维护窗口来压缩主数据库。

... is a high-availability web-queue.

你在使用 Capped Collections为了这?上限集合不需要压缩。上限集合中的对象不能调整大小,但这通常不是队列对象的问题。

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

关于MongoDB维护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8895256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com