我们在 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