zoukankan      html  css  js  c++  java
  • MongoDB集群负载不均衡问题定位及解决

    1、问题描述

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

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

    2、思考及分析

    首先,我查看慢日志,发现大量与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有关的,那么我就去检查一下这个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 } )

  • 相关阅读:
    NSRunloop-基本概念
    GCD—NSThread-多线程的基本用法
    NSURLConnection-网络访问(同步异步)
    ASIHttpRequest网络使用框架
    XML与JSON解析
    iOS 设置系统音量和监听系统音量变化
    iOS_字典数组 按key分组和排序
    iOS 自定义字体设置
    日期选择和输入弹框
    iOS 耳机线控
  • 原文地址:https://www.cnblogs.com/dba-john/p/12193465.html
Copyright © 2011-2022 走看看