MongoDB仅在选择标记分片键时将数据存储到主分片

时间:2015-05-23 17:04:09

标签: mongodb sharding database nosql

我正在使用mongodb v3.0并尝试使用标记分片进行分片。 文档mongodb v3.0 documentation显示了如何配置它。

我有2个分片:

mongos> sh.addShardTag("shard0000", "USA")
mongos> sh.addShardTag("shard0001", "EU")

和分片集合是test_collection:

mongos> db.printShardingStatus()
--- Sharding Status --- 
  sharding version: {
 "_id" : 1,
 "version" : 3,
 "minCompatibleVersion" : 3,
 "currentVersion" : 4,
 "clusterId" : ObjectId("555df7f4f6506e6ba07e1f20")
}
  shards:
 {  "_id" : "shard0000",  "host" : "127.0.0.1:27017",  "tags" : [  "USA" ] }
 {  "_id" : "shard0001",  "host" : "127.0.0.1:27018",  "tags" : [  "EU" ] }
  databases:
 {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
 {  "_id" : "test_collection",  "partitioned" : true,  "primary" : "shard0000" }

正如您在本案中所看到的,它选择shard0000作为主要。 当我标记集合并启用集合进行分片时:

mongos> sh.addTagRange("test_collection.items", {country: "USA"}, {country: "USA"}, "USA")
mongos> sh.addTagRange("test_collection.items", {country: "EU"}, {country: "EU"}, "EU")
mongos> db.runCommand({shardCollection: "test_collection.items", key: {"_id": "hashed"}})

并插入数据(约20K项),我已检查状态

mongos> db.printShardingStatus()
--- Sharding Status --- 
  sharding version: {
 "_id" : 1,
 "version" : 3,
 "minCompatibleVersion" : 3,
 "currentVersion" : 4,
 "clusterId" : ObjectId("555df7f4f6506e6ba07e1f20")
}
  shards:
 {  "_id" : "shard0000",  "host" : "127.0.0.1:27017",  "tags" : [  "USA" ] }
 {  "_id" : "shard0001",  "host" : "127.0.0.1:27018",  "tags" : [  "EU" ] }
  databases:
 {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
 {  "_id" : "test_collection",  "partitioned" : true,  "primary" : "shard0000" }
  test_collection.items
   shard key: { "_id" : "hashed" }
   chunks:
    shard0000 1
   { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 
    tag: EU  { "country" : "EU" } -->> { "country" : "EU" }
    tag: USA  { "country" : "USA" } -->> { "country" : "USA" }

因此,应用了分片标记,但数据仅写入主分片。

我按照手册制作的每一步,有人能告诉我配置错过了什么吗? 仅在选择分片标记时才会显示相同的行为。没有标签,例如只有通过分片键才能正常工作,数据存储到两个分片。

2 个答案:

答案 0 :(得分:1)

问题是您的分片键是散列的“_id”列,但您在“country”列上标记范围。

根据文档,这是不可能的:

  

MongoDB支持标记一系列分片键

http://docs.mongodb.org/manual/core/tag-aware-sharding/

您需要将“国家/地区”列设为分片键的一部分。

答案 1 :(得分:0)

谢谢,博鲁特!

我实际上已尝试将“country”作为分片键,但它也无效。也许我在配置上犯了另一个错误。但经过上述评论后,我重复了所有步骤,并使分片配置正常工作。

因此,我逐步发布所有流程以启动配置:

  • 使用标签标记碎片(已添加碎片)后,检查状态
shardingversion: {
  "_id": 1,
  "version": 3,
  "minCompatibleVersion": 3,
  "currentVersion": 4,
  "clusterId": ObjectId("555df7f4f6506e6ba07e1f20")
}shards: {
  "_id": "shard0000",
  "host": "127.0.0.1:27000",
  "tags": [
    "USA"
  ]
}{
  "_id": "shard0001",
  "host": "127.0.0.1:270001",
  "tags": [
    "EU"
  ]
}databases: {
  "_id": "admin",
  "partitioned": false,
  "primary": "config"
}{
  "_id": "test_db",
  "partitioned": true,
  "primary": "shard0000"
}
  • 然后将shard键添加到我们将使用的集合

db.runCommand({shardCollection: "test_db.items", key: {"country": 1}})

请注意,在这种情况下,我选择的键尽可能简单,但在实际系统中,您应该更仔细地选择它。

为我们的分片添加标记范围: sh.addTagRange("test_db.items", {country: "MX"}, {country: "USA"}, "USA") sh.addTagRange("test_db.items", {country: "ES"}, {country: "GER"}, "EU")

  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("556376557b13cb3ac8b90b49")
}
  shards:
        {  "_id" : "shard0000",  "host" : "127.0.0.1:27000",  "tags" : [ "USA" ]
}
        {  "_id" : "shard0001",  "host" : "127.0.0.1:27001",  "tags" : [ "EU" ] }

  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test_db",  "partitioned" : true,  "primary" : "shard0000" }
                test_db.items
                        shard key: { "country" : 1 }
                        chunks:
                                shard0000       2
                                shard0001       1
                        { "country" : { "$minKey" : 1 } } -->> { "country" : "ES
" } on : shard0000 Timestamp(2, 1)
                        { "country" : "ES" } -->> { "country" : "MX" } on : shar
d0001 Timestamp(2, 0)
                        { "country" : "MX" } -->> { "country" : { "$maxKey" : 1
} } on : shard0000 Timestamp(1, 2)
                         tag: EU  { "country" : "ES" } -->> { "country" : "GER"
}
                         tag: USA  { "country" : "MX" } -->> { "country" : "USA"
 }
  • 好的,我们将元素添加到集合
for(var i=0; i<1000; i++)
{ 
  db.items.insert({"model": "One", "country":"EU"}); 
  db.items.insert({"model": "iPhone", "country": "US"});
}

for(var i=0; i<10;++i)
{
  db.items.insert({"model":"X", "country":"EU"});
}

我们期望在一个碎片上获得1K元素,在第二个碎片上获得1K + 10个元素。

查看结果:

db.items.stats()
{
        "sharded" : true,
        "userFlags" : 1,
        "ns" : "test_db.items",
        "count" : 2010,
        "numExtents" : 6,
        "size" : 225120,
        "storageSize" : 344064,
        "totalIndexSize" : 155344,
        "indexSizes" : {
                "_id_" : 81760,
                "country_1" : 73584
        },
        "avgObjSize" : 112,
        "nindexes" : 2,
        "nchunks" : 3,
        "shards" : {
                "shard0000" : {
                        "ns" : "test_db.items",
                        "count" : 1000,
                        "size" : 112000,
                        "avgObjSize" : 112,
                        "numExtents" : 3,
                        "storageSize" : 172032,
                        "nindexes" : 2,
                        "lastExtentSize" : 131072,
                        "paddingFactor" : 1,
                        "userFlags" : 1,
                        "totalIndexSize" : 81760,
                        "indexSizes" : {
                                "_id_" : 40880,
                                "country_1" : 40880
                        },
                        "ok" : 1
                },
                "shard0001" : {
                        "ns" : "test_db.items",
                        "count" : 1010,
                        "size" : 113120,
                        "avgObjSize" : 112,
                        "numExtents" : 3,
                        "storageSize" : 172032,
                        "nindexes" : 2,
                        "lastExtentSize" : 131072,
                        "paddingFactor" : 1,
                        "userFlags" : 1,
                        "totalIndexSize" : 73584,
                        "indexSizes" : {
                                "_id_" : 40880,
                                "country_1" : 32704
                        },
                        "ok" : 1
                }
        },
        "ok" : 1
}

检查每个分片统计信息的“计数”字段。

我希望上述信息对于那些正在与同一问题作斗争的人来说是有用的,并节省您的时间来找到解决方案。