简体   繁体   中英

Mongo shard collection not distributed if shard key is a small int

We did mock test on the sharded mongo environment, but if the shard key value is small int, the collection didn't distribute, but if the shard key is big int, its working fine. Please read on...

Code used to insert records from mongos shell.

var shId = 15;
for (var i = 0; i < 100; i++) {
  if(i%50 == 0){
      shId = shId + 1;
  }
  db.Foo.insert( { shKeyId : shId , text:"this is a test" } );
}

With shId = 15, Foo collection split to two shards didn't work.

Environment : Two shards, each shard with Primary1 and two secondary mongod instances. Mongo config is running on one of the shard.

Sharding enabled on 'Foo' collection by shKeyId as hashed shard key. db.runCommand({ shardcollection : "test.Foo", key : {shKeyId : "hashed"}});

sh.status() output

mongos> sh.status();
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "version" : 3,
    "minCompatibleVersion" : 3,
    "currentVersion" : 4,
    "clusterId" : ObjectId("516ea48e979736fd306973c9")
}
  shards:
    {  "_id" : "mongo-perf-shrd1",  "host" : "mongo-perf-shrd1/sh1-prim-ip:27017,sh1-sec1-ip:27017,sh1-sec2-ip:27017" }
    {  "_id" : "mongo-perf-shrd2",  "host" : "mongo-perf-shrd2/sh2-prim-ip:27017,sh2-sec1-ip:27017,sh2-sec2-ip:27017" }
  databases:
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

    {  "_id" : "test",  "partitioned" : true,  "primary" : "mongo-perf-shrd2" }
        test.Foo
            shard key: { "shKeyId" : "hashed" }
            chunks:
                mongo-perf-shrd2    2
                mongo-perf-shrd1    2
            { "shKeyId" : { "$minKey" : 1 } } -->> { "shKeyId" : NumberLong("-4611686018427387902") } on : mongo-perf-shrd2 { "t" : 2, "i" : 2 } 
            { "shKeyId" : NumberLong("-4611686018427387902") } -->> { "shKeyId" : NumberLong(0) } on : mongo-perf-shrd2 { "t" : 2, "i" : 3 } 
            { "shKeyId" : NumberLong(0) } -->> { "shKeyId" : NumberLong("4611686018427387902") } on : mongo-perf-shrd1 { "t" : 2, "i" : 4 } 
            { "shKeyId" : NumberLong("4611686018427387902") } -->> { "shKeyId" : { "$maxKey" : 1 } } on : mongo-perf-shrd1 { "t" : 2, "i" : 5 } 

Shard Distribution output

   mongos> db.Foo.getShardDistribution();

Shard mongo-perf-shrd1 at mongo-perf-shrd1/ip1:27017,ip2,ip3:27017
 data : 6KiB docs : 100 chunks : 2
 estimated data per chunk : 3KiB
 estimated docs per chunk : 50

Shard mongo-perf-shrd2 at mongo-perf-shrd2/ip4:27017,ip5:27017,ip6:27017
 data : 0B docs : 0 chunks : 2
 estimated data per chunk : 0B
 estimated docs per chunk : 0

Totals
 data : 6KiB docs : 100 chunks : 4
 Shard mongo-perf-shrd1 contains 100% data, 100% docs in cluster, avg obj size on shard : 64B
 Shard mongo-perf-shrd2 contains 0% data, 0% docs in cluster, avg obj size on shard : NaNGiB

you have just monotonically shard key, this is not good solution: http://docs.mongodb.org/manual/core/sharded-cluster-internals/

for just mock tested, enough to select shard key by default _id field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM