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

一、问题描述app

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

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

 

二、思考及分析spa

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

1  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有关的,那么我就去检查一下这个collectioncode

如下是db.getCollection('postbox').stats()的输出:blog

{ 
    "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"这一个节点。这就能够说明为何这一个节点的负载高,而其它节点负载很低了。ci

经过执行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,只分布在一个节点中,因此压测就致使该节点的负载很是高。get

 

三、解决方法

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

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 } )

相关文章
相关标签/搜索