虚位以待(AD)
虚位以待(AD)
首页 > 数据库 > MongoDB数据库 > MongoDB诡异问题之sh.stopBalancer卡住的解决方法

MongoDB诡异问题之sh.stopBalancer卡住的解决方法
类别:MongoDB数据库   作者:码皇   来源:互联网   点击:

这篇文章主要给大家介绍了关于MongoDB诡异问题之sh stopBalancer卡住解决的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

背景

Part1:写在最前

我们在使用MongoDB sharding集群时,会使用如下命令来管理启停Balancer:

    >sh.stopBalancer() 停止Balancer>sh.startBalancer() 开启Balancer

Part2:背景

开启balancer后,客户反馈前端应用写入缓慢,查询超时。因此我们尝试关闭balancer,来避免chunk迁移对集群性能带来的影响。

但是在调用sh.stopBalancer的时候,发现却停不下来,sh.stopBalancer会处于卡住的状态:

    mongos>sh.stopBalancer()Waiting for active hosts...Waiting for the balancer lock...assert.soon failed,msg:Waited too long for lock balancer to unlockdoassert@src/mongo/shell/assert.js:18:14assert.soon@src/mongo/shell/assert.js:202:13sh.waitForDLock@src/mongo/shell/utils_sh.js:198:1sh.waitForBalancerOff@src/mongo/shell/utils_sh.js:264:9sh.waitForBalancer@src/mongo/shell/utils_sh.js:294:9sh.stopBalancer@src/mongo/shell/utils_sh.js:161:5@(shell):1:1Balancer still may be active, you must manually verify this is not the case using theconfig.changelog collection.2018-02-11T16:28:29.753+0800E QUERY [thread1] Error: Error:assert.soon failed, msg:Waited too long for lock balancer to unlock :sh.waitForBalancerOff@src/mongo/shell/utils_sh.js:268:15sh.waitForBalancer@src/mongo/shell/utils_sh.js:294:9sh.stopBalancer@src/mongo/shell/utils_sh.js:161:5@(shell):1:1

从上述报错能够看出,是由于目前balancer正在运行导致的,

Warning:警告 在3.4版本中,balancer运行在config server 的主节点上,在早期的版本中,balancer是运行在mongos上的。 当balancer进程处于活动状态时,config server副本集的主服务器通过修改config数据库的lock集合中的文档,来获取“平衡器锁”。 这个“平衡器锁”只能自己主动释放。

Part3:排查方法

我们调用sh.status()命令能够看到当前balancer已经关闭了,但是running还是yes,这说明有迁移正在运行。 

    balancer:Currently enabled: noCurrently running: yes

我们查看发现migrations集合下为空,说明没有集合在迁移

    mongos> db.migrations.find()

我们查看locks集合下的信息,处于2状态的说明正持有锁

    mongos> db.locks.find(){
    "_id" : "balancer", "state" : 2, "ts" : ObjectId("5a324c42329457086086da07"), "who" : "ConfigServer:Balancer", "process" : "ConfigServer", "when" : ISODate("2018-01-31T08:33:43.346Z"), "why" : "CSRS Balancer" }

Warning:警告

locks集合中的why列告诉我们持有锁的原因,如果有正在迁移的文档,其状态应该是2,why中的原因会显示Migrating chunk(s) in collection db.collationname.

从3.4版本起,balancer的状态字段将始终为值2,以防止老版本的mongos实例执行平衡操作。 when字段指config server 成员成为主节点的时间。

解决办法

Part1:写在最前

sh.stopBalancer停不下来,常见的可能原因有以下几个:

  • 正在做chunk迁移,必须等待chunk迁移完成后,才能够正常停止;
  • 后端的server时间不同步;
  • mongo客户端版本低于server端,本文就是第3种情况。mongo客户端的版本是3.2版本,config server和mongod都是3.4版本的mongo。

Part2:解决办法

替换老版本的mongo客户端,使用3.4版本的客户端

    mongos> sh.stopBalancer(){
    "ok" : 1 }
    config:PRIMARY> db.version()3.4.9-2.9

Part3:原因分析

卡住的原因是由于客户端mongo是3.2版本,而config节点是3.4版本,3.2版本的mongos在执行stopBalancer()时,stopBalancer代码假定如果balancerStop命令没有找到,那么它会使用旧版本的逻辑,等待锁被释放。从3.4版本起,Balance进程从mongos移动之configer server的primary节点上。

总结

通过这个案例,我们能够了解到mongo客户端版本带来的问题,以及有哪些常见原因导致sh.stopBalancer停不下来。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关热词搜索: mongodb balancer mongodb stop mongodb s