【问题标题】:Mongo shard collection not distributed if shard key is a small int如果分片键是小整数,则不会分发 Mongo 分片集合
【发布时间】:2013-04-17 09:35:47
【问题描述】:

我们在 sharded mongo 环境下进行了模拟测试,但是如果 shard key 值为 small int,则集合没有分发,但如果 shard key 为 big int,则工作正常。请继续阅读...

用于从 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" } );
}

当 shId = 15 时,Foo 集合拆分为两个分片不起作用。

环境:两个分片,每个分片都有 Primary1 和两个辅助 mongod 实例。 Mongo 配置正在其中一个分片上运行。

通过 shKeyId 对 'Foo' 集合启用分片作为散列分片键。 db.runCommand({ shardcollection : "test.Foo", key : {shKeyId : "hashed"}});

sh.status() 输出

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 } 

分片分布输出

   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

【问题讨论】:

  • 你能描述一下“分片不起作用”是什么意思吗?
  • 我期待,50 条记录进入 shard1,其他 50 条记录进入 shard2。但是所有 100 条记录都保存到 shard1
  • 你能把sh.status()在mongos上运行的输出贴出来吗?
  • 谢谢谢尔曼。我已将 sh.status 输出添加到问题中
  • 为什么你认为所有 100 条记录都保存在 shard1 中?看起来,给定输出,块均匀分布在两个分片中(每个分片中有 2 个块)

标签: mongodb sharding mongo-java


【解决方案1】:

你只有单调的分片键, 这不是好的解决方案:http://docs.mongodb.org/manual/core/sharded-cluster-internals/

对于只是模拟测试,足以默认选择分片键_id字段

【讨论】:

  • docs.mongodb.org/manual/core/sharded-clusters/#shard-keys "散列键适用于单调增加的字段"
  • 1 这里要考虑的重要因素也是query pattern。我认为单调递增的分片键没有任何问题。如果范围是均匀分布的,这可以通过使用称为pre-sharding 的过程来实现。如果事先知道可能的范围,则使用 python(或任何其他语言)我们可以计算否。预先分配分片,并为分片分配键范围。这在我们有范围查询时很有用from id1 to idN
  • 2 散列分片肯定会导致均匀分布,但在这里提到initial Chunks 以减少块迁移的负载也很重要。但是,如果查询涉及范围,这将没有用。例如id1 将是 shard 1 的一部分,id 2 可能是 shard n 的一部分,获取这将是昂贵的操作。
猜你喜欢
  • 2016-10-10
  • 1970-01-01
  • 2020-10-14
  • 2021-07-14
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
相关资源
最近更新 更多