专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

MongoDB集群负载不均衡问题定位及解决

1、问题描述

这是一套运行在腾讯云上的MongoDB 3.6版本集群,共5个分片,每片规格是6核16GB。

在压测的过程中,发现第3个分片的CPU使用率长时间高达96%,其它4个分片的CPU使用率都没有超过10%。

2、思考及分析

首先,我查看慢日志,发现大量与postbox相关的query,半个小时内出现9000多次,每次请求平均耗时200ms左右,planSummary为IXSCAN,每次扫描和返回的文档数都很少,锁也很少。

  planSummary: IXSCAN { serviceUserId: 1, updatedDate: -1, messageType: 1 } keysExamined:0 docsExamined:0 cursorExhausted:1 numYields:0 nreturned:0 reslen:340 locks:{ Global: { acquireCount: { r: 2 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { r: 1 } } }

到此,还不能说明问题,但是既然慢日志里面都是与postbox有关的,那么我就去检查一下这个collection

以下是db.getCollection(‘postbox’).stats()的输出:

{ 
    "sharded" : true, 
    "capped" : false, 
    "ns" : "postbox.postbox", 
    "count" : 1020.0, 
    "size" : 301694.0, 
    "storageSize" : 163840.0, 
    "totalIndexSize" : 184320.0, 
    "indexSizes" : {
        "_id_" : 69632.0, 
        "expireAtTtlIndex" : 53248.0, 
        "serviceUserIdMsgTypeSearchIdx" : 61440.0
    }, 
    "avgObjSize" : 295.0, 
    "nindexes" : 3.0, 
    "nchunks" : 1.0, 
    "shards" : {
        "cmgo-280eoxk3_2" : {
   ……
   省略

}

可以看出,整个文档只有294KB,包含一个chunk,只分布在”cmgo-280eoxk3_2″这一个节点。这就可以说明为什么这一个节点的负载高,而其它节点负载很低了。

通过执行sh.status(),可以看到该collection的分片方式为range:

                postbox.postbox
                        shard key: { "serviceUserId" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                cmgo-280eoxk3_2    1
                        { "serviceUserId" : { "$minKey" : 1 } } -->> { "serviceUserId" : { "$maxKey" : 1 } } on : cmgo-280eoxk3_2 Timestamp(1, 0) 

所以,这个问题的根本原因是:该collection目前数据非常少,只有一个chunk,只分布在一个节点中,所以压测就导致该节点的负载非常高。

3、解决方法

查阅官方文档,其中有如下说明:

If you shard an empty collection using a hashed shard key, MongoDB automatically creates two empty chunks per shard, to cover the entire range of the hashed shard key value across the cluster. 
You can control how many chunks MongoDB creates with the numInitialChunks parameter to shardCollection or by manually creating chunks on the empty collection using the split command.

意思是使用hashed分片方式,MongoDB会自动为每个片创建2个空的chunks,你也可以在设置该集合的分片时,使用numInitialChunks参数来指定空chunks的数量。

通过与研发沟通,结合我们的实际情况评估,认为该collection可以使用hashed分片方式。

所以,备份该集合的数据,然后使用如下方重新指定分片方式,最后导入数据。

db.runCommand( { shardCollection: “postbox.userPostIndex”, key: {serviceUserId:”hashed”}, numInitialChunks: 3 } )

文章永久链接:https://tech.souyunku.com/48034

未经允许不得转载:搜云库技术团队 » MongoDB集群负载不均衡问题定位及解决

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们