Reputation: 602
Let say I set a min and max key for shards using range tag. Later than, I find out that tag range I defined will not balance. Then I have to change tag range value.
I have search in MongoDB docs, it only mention add and remove tags, no mention on update/change tag range value.
Then I do some experiment by updating value in db config collection shard
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" }
It works well, but tags id is not updated. Notice that even when min is updated to 150 at rangeTime2, id is not updated, which makes tags collection seems inconsistent.
It is kind of dirty but sadly it is works.
Is this the right way to update tag range? Is there any other better ways?
Upvotes: 1
Views: 1187
Reputation: 21682
It's not obvious, but if you run sh.addTagRange()
with the same minimum (in your case, 1 or 100) and a different maximum, then the existing tag range with that minimum value will change, essentially functioning as an in-place update (the addTagRange()
helper basically runs an upsert based on the _id field and with some sanity checking) which can be seen by running it without parentheses:
mongos> sh.addTagRange
function ( ns, min, max, tag ) {
if ( bsonWoCompare( min, max ) == 0 ) {
throw new Error("min and max cannot be the same");
}
var config = db.getSisterDB( "config" );
config.tags.update( {_id: { ns : ns , min : min } } ,
{_id: { ns : ns , min : min }, ns : ns , min : min , max : max , tag : tag } ,
true );
sh._checkLastError( config );
}
To alter a tag range minimum, this won't work - you need to remove and re-add, or you need to manually update the _id.min
field in the sub-document as well as the min
field in the top level document to avoid the inconsistency you are seeing. I would recommend the remove and re-add option currently to avoid confusion. These commands are due to be refactored in a later release (not yet scheduled), and I personally added a new remove helper as part of SERVER-6352 to make this a bit less onerous in the future.
Upvotes: 3