Mongodb – Update tag range on MongoDB range shard

mongodbsharding

Let say I set a min and max key for shards using range tags. Later I find out that the tag range I defined will not balance. Then I have to change the tag range value.

I have searched MongoDB's documentation, which only mentions add and remove tags, nothing about updating or changing the tag range.

Then I experimented by updating the range values in the config database tag collection.

mongos> db.tags.find()
{ "_id" : { "ns" : "test.lab.range", "min" : { "_id" : 1 } }, "ns" : "test.lab.range", "min" : { "_id" : 1 }, "max" : { "_id" : 100 }, "tag" : "rangeTime1" }
{ "_id" : { "ns" : "test.lab.range", "min" : { "_id" : 100 } }, "ns" : "test.lab.range", "min" : { "_id" : 100 }, "max" : { "_id" : 200 }, "tag" : "rangeTime2" }
mongos> db.tags.update({_id:{ "ns" : "test.lab.range", "min" : { "_id" : 100 } }},{$set:{min : {_id : 150}}})
mongos> db.tags.update({_id:{ "ns" : "test.lab.range", "min" : { "_id" : 1 } }},{$set:{max : {_id : 150}}})
mongos> db.tags.find()
{ "_id" : { "ns" : "test.lab.range", "min" : { "_id" : 1 } }, "ns" : "test.lab.range", "min" : { "_id" : 1 }, "max" : { "_id" : 150 }, "tag" : "rangeTime1" }
{ "_id" : { "ns" : "test.lab.range", "min" : { "_id" : 100 } }, "max" : { "_id" : 200 }, "min" : { "_id" : 150 }, "ns" : "test.lab.range", "tag" : "rangeTime2" }

That worked well, but the tags id wasn't updated. Notice that even when min is updated to 150 at rangeTime2, id is not updated, which makes the tags collection seem inconsistent.

It is kind of dirty but sadly it is works.

Is this the right way to update tag range? Is there a better way?

Best Answer

New to MongoDB, but this is how I learned how to do the same thing following the MongoDB 202 class. There are other ways to balance traffic, such as moving, splitting and merging chunks. I haven't read anywhere that directly updating the tags collection in the config database was dirty or incorrect.

The code below is untested, and you will have to replace certain values like nameofShard and the minimum, maximum values. You will notice that I stop the balancer before modifying the tag ranges

// stop the balancer
sh.stopBalancer()

// remove the tags from the shards
sh.removeShardTag("nameofshardN", "rangeTime1")
sh.removeShardTag("nameofshardN+1", "rangeTime2")

// remove the existing tags
db.tags.remove({ "_id" : { "ns" : "testlab.range", "min" : { "_id" : 1 } }, "ns" : "testlab.range", "min" : { "_id" : 1 }, "max" : { "_id" : 100 }, "tag" : "rangeTime1" })
db.tags.remove({ "_id" : { "ns" : "testlab.range", "min" : { "_id" : 100 } }, "ns" : "testlab.range", "min" : { "_id" : 100 }, "max" : { "_id" : 200 }, "tag" : "rangeTime2" })

// Add new tags
sh.addTagRange("testlab.range", {_id : new_minimum_value}, {_id : new_maximum_value}, "rangeTime1");

sh.addTagRange("testlab.range", {_id : new_minimum_value}, {_id : new_maximum_value}, "rangeTime2");


// Add shardTag
sh.addShardTag("nameofshardN", "rangeTime1")
sh.addShardTag("nameofshardN+1", "RangeTime2")


// start balancer
sh.startBalancer()

sh.status() or mongostat --port 27017 --discover to verify balancing

test.lab is not a valid database name, so I changed my database name to testlab.

One additional remark about the _id field not being updated. When I query the tags collection like so

mongos> db.tags.find({}, {_id:1})
{ "_id" : { "ns" : "testlab.range", "min" : { "_id" : 1 } } }
{ "_id" : { "ns" : "testlab.range", "min" : { "_id" : 100 } } }

It shows that the _id field contains the min value or the tag range. If you are updating the tags collection and modifying the tags min field value, then wouldn't it also be reasonable to update the _id field's min value as well?