Search code examples
mongodbnosqlscalabilitysharding

MongoDB shard collecton in primary shard only


I'm trying to shard a collection (db: mql, collection name: teste) in my sharded cluster with three shards servers, however, the only shard containing my data is the primary one.

--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("60907ccc078cdbdf9b66c6d0")
  }
  shards:
        {  "_id" : "sh1",  "host" : "sh1/localhost:27022",  "state" : 1,  "tags" : [ "NYC", "SF" ] }
        {  "_id" : "sh2",  "host" : "sh2/localhost:27023",  "state" : 1 }
        {  "_id" : "sh3",  "host" : "sh3/localhost:27026",  "state" : 1 }
  active mongoses:
        "4.2.13" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                853 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                sh1     341
                                sh2     342
                                sh3     341
                        too many chunks to print, use verbose if you want to force print
        {  "_id" : "mql",  "primary" : "sh3",  "partitioned" : true,  "version" : {  "uuid" : UUID("c2c07a45-b8a7-4468-a3ef-cd5bafd8999c"),  "lastMod" : 1 } }
                mql.teste
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                sh3     1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : sh3 Timestamp(1, 0)

This is what I get when I check if the collection is sharded or not:

mongos> db.collections.find( {_id: "mql.teste" , dropped : false } )
{ 
  "_id" : "mql.teste", 
  "lastmodEpoch" : ObjectId("6090c18062e39651b4cd267c"), 
  "lastmod" : ISODate("1970-02-19T17:02:47.296Z"), 
  "dropped" : false, 
  "key" : { "_id" : 1 }, 
  "unique" : false, 
  "uuid" : UUID("91d1fdd2-5062-404b-b2ec-28a1f6e15b3e") 
}

Solution

  • The mongodb sharded cluster balancer tries to place the same number of chunks on each shard.

    That collection only has 1 chunk, and therefore can't be divided among the shards.

    The default maximum size for a chunk is 64MB.

    Depending on the version of MongoDB either the mongos or the mongod will call for a split when it realizes that a significant fraction of the maximum chunk size has been written to a chunk.

    Or you can manually split the chunk with sh.splitAt.

    You can also run db.getSiblingDB("mql").teste.getShardDistribution() to get information about the number and size of documents/chunks on each shard.