mongo分片集群 balance问题
阿里文档:https://help.aliyun.com/document_detail/100658.html?scm=20140722.184.2.173
1、mongodb Cannot move chunk
问题描述
对应mongodb集群,客户可能反馈集群分片中每个分片的存储使用量差异比较大。此时需要关注:
-
客户是否对库,表开启分片,如果没开启分片,那么对没开启分片的库会存在某个shard上;
sh.status(true) 可以确认
-
如果库表基本都开启了分片,那么此时需要看下
是否存在move chunk失败的情况 (sh.status(true) 可以确认)
Failed with error ‘aborted’, from d-bp1d9b8c781f1094 to d-bp1f84176683d8c4
是否存在jumbo chunk的情况 (sh.status(true) 可以确认,也可以切换到config数据库下,执行db.chunks.find({“jumbo”:true})确认)
3、看下索引是否一致,可能是索引不一致导致写入冲突
Chunk move failed :: caused by :: OperationFailed: Data transfer error: migrate failed: Location51008: operation was interrupted
参考这里解决方案:https://jira.mongodb.org/browse/SERVER-46363
4. 排除上面三种情况,那需要关注每个*是否碎片率比较高
可以执行下面命令查看*可回收空间大小
db.<collection_name>.stats().wiredTiger[“block-manager”][“file bytes available for reuse”]
回收碎片率参考:https://help.aliyun.com/document_detail/96530.html?spm=a2c4g.26556.0.i1
这里针对第二种move chunk失败讲下:
通过mongo 运行日志看到move chunk失败的信息如下:
Cannot move chunk: the maximum number of documents for a chunk is xxx, the maxinum chunk size is 67108864, average document size is yyy. Found zzz documents in chunk ns:
此处以上面截图报错说明下几个数字的含义:
1417:mongodb内部限制的一个chunk最多可以存放的文档数据,这个并不是固定的,和写入的平均文档大小有关系,具体计算公式是: 1.3* (max chunk size / average document size), 所以此处1417 = 1.3*(67108864/61552)
67108864: mongodb默认定义的最大chunk大小,也就是64M
1517:当前chunk存在的文档数。
这里报错就是因为1417 < 1517,所以内核限制的chunk数小于了已存入文档数,导致move chunk失败,如果mongodb版本大于4.4,那么如文档描述可以通过设置参数 attemptToBalanceJumboChunks=true来迁移还没有成为jumbo chunk的块。
官方文档说明:
https://www.mongodb.com/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-balance-chunks-that-exceed-size-limit
对于已经是jumbo chunk的块,可以按下面文档方式处理:
-
对于可以拆分的,可以 sh.splitAt() or sh.splitFind() 来拆分,拆分后会自动清理jumbo标识
-
不可拆分的,看mongodb版本,如果大于等于 4.4版本,那么可以用refineCollectionShardKey
重新定义分区键,refineCollectionShardKey命令给现有的Shard Key增加一个或多个Suffix Field来改善现有的文档在Chunk上的分布问题,此命令性能开销非常低,仅更改Config Server节点上的元数据,不需要任何形式的数据迁移,数据的打散仍然在后续正常的Chunk自动分裂和迁移的流程中逐步进行。此外,Shard Key需要有对应的Index来支撑,因此refineCollectionShardKey命令要求提前创建新Shard Key所对应的Index。
详细参考:
https://www.mongodb.com/docs/manual/tutorial/clear-jumbo-flag/
示例:【问题】 杭州可用区,实例 id:dds-bp1fc80c2d6fba34 mongodb 分片集群,有个表的在 shard 之间的 chunks 数很不均匀,其它表就比较均匀(balancing 都是开启了的)
【进展】1、分别 执行这两个命令在看下sh.status() 和 while( sh.isBalancerRunning() ) { print(“waiting…”); sleep(1000); }
sh.status()
while( sh.isBalancerRunning() ) { print(“waiting…”); sleep(1000); }
https://help.aliyun.com/document_detail/64561.html
caused by :: OperationFailed: Data transfer error: migrate failed: Location51008: operation was interrupted