Zone Sharding Not Working As Configured

29 views
Skip to first unread message

Akashi Seih

unread,
Jul 20, 2017, 6:22:42 PM7/20/17
to mongodb-user
I'm trying to set up Zone Sharding on two MongoDB replica-sets.

I tried to follow the mongoDB docs and follow their guide, but I've unfortunately hit a wall and need some guidance:

This is what I've done so far:

I first created a test database called test1 (on the query router. After that I created my shard tags, one for each replica set.

    sh.addShardTag("r0", "AS")
    sh.addShardTag("r1", "EU")

Then I went on and enabled the sharding for my database and collection:

    sh.enableSharding("test1")
    sh.shardCollection("test1.users", { "code" : 1 } )

Finally I created my sharding rules. I want "code" values between 1->20 to be saved in shard1 (r0) and values between 20->40 to be saved in shard2(r2).

    sh.addTagRange("test1.users", { code: "1" }, { code: "20" }, "AS")
    sh.addTagRange("test1.users", { code: "20" }, { code: "40" }, "EU")

sh.status returns this and from my point of view it all looks fine:

    {  "_id" : "test1",  "primary" : "rs1",  "partitioned" : true }
    test1.users
    shard key: { "code" : 1 }
    unique: false
    balancing: true
    chunks:
    rs0 1
    rs1 3
    { "code" : { "$minKey" : 1 } } -->> { "code" : "100" } on : rs1 Timestamp(2, 1)
    { "code" : "1" } -->> { "code" : "20" } on : rs0 Timestamp(2, 0)
    { "code" : "20" } -->> { "code" : "40" } on : rs1 Timestamp(2, 2)
    { "code" : "40" } -->> { "code" : { "$maxKey" : 1 } } on : rs1 Timestamp(2, 3)
    tag: AS  { "code" : "1" } -->> { "code" : "20" }
    tag: EU  { "code" : "20" } -->> { "code" : "40" }

After I've set this up I went on and tried to push in some data to see if the data is being split up as I want. 

    for (i = 1; i < 40; i++) { db.users.insert({"code":i})}

However, all the data is being saved in to one shard, and the rules are totally ignored. I know I messed up somewhere, I just don't know where so all the help I can get is appreciated.

Thanks.

Weishan Ang

unread,
Jul 21, 2017, 10:56:38 AM7/21/17
to mongodb-user
The trouble is this:

{ "code" : { "$minKey" : 1 } } -->> { "code" : "100" } on : rs1 Timestamp(2, 1)
{ "code" : "40" } -->> { "code" : { "$maxKey" : 1 } } on : rs1 Timestamp(2, 3)

anything below 100 will go to rs1. I'm not sure how overlapping shard tag will behave though.

Kevin Adistambha

unread,
Aug 7, 2017, 2:59:54 AM8/7/17
to mongodb-user

Hi,

I noticed a small discrepancy in the code you posted.

Your addTagRange command specified the code to be string:

sh.addTagRange(“test1.users”, { code: “1” }, { code: “20” }, “AS”)

while your test code specified the code to be numbers:

for (i = 1; i < 40; i++) { db.users.insert({“code”:i})}

Using this test code, all your test documents will be deposited in a single chunk.

You might want to re-specify your tag ranges to be in numbers, e.g.:

sh.addTagRange("test1.users", { code: 1 }, { code: 20 }, "AS")

and see if your tag ranges now behaves as designed.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages