Shard Tagging with overlapping/Same ranges ?!!

 

What will happen when you create a Shard Tagging with overlapping ranges ?!! See below.

Any given shard key range may only have one assigned tag.
You cannot overlap defined ranges, or tag the same range more than once – it’ll not throw any errors but won’t effect anything if you try so.

When you do overlapping ranges, it’s just ignore the same range tags such way it covers all the ranges as below.

 

sh.addShardTag(“rs1”, “DBVERSITY”)
sh.addShardTag(“rs2”, “DBFRY”)
sh.addShardTag(“rs3”, “DBFRY”)

sh.addTagRange(“dbversity.dbfry”, { id: 1 }, { id: 200 }, “DBVERSITY”)
sh.addTagRange(“dbversity.dbfry”, { id: 150 }, { id: 200 }, “DBFRY”)
sh.addTagRange(“dbversity.dbfry”, { id: 1 }, { id: 150 }, “DBFRY”)

mongos> use config
switched to db config
mongos>
mongos>
mongos> db.tags.find()
mongos>
mongos>
mongos> sh.addTagRange(“dbversity.dbfry”, { id: 1 }, { id: 200 }, “DBVERSITY”)
mongos> db.tags.find()
{ “_id” : { “ns” : “dbversity.dbfry”, “min” : { “id” : 1 } }, “ns” : “dbversity.dbfry”, “min” : { “id” : 1 }, “max” : { “id” : 200 }, “tag” : “DBVERSITY” }
mongos>
mongos> sh.addTagRange(“dbversity.dbfry”, { id: 150 }, { id: 200 }, “DBFRY”)
mongos>
mongos> db.tags.find()
{ “_id” : { “ns” : “dbversity.dbfry”, “min” : { “id” : 1 } }, “ns” : “dbversity.dbfry”, “min” : { “id” : 1 }, “max” : { “id” : 200 }, “tag” : “DBVERSITY” }
{ “_id” : { “ns” : “dbversity.dbfry”, “min” : { “id” : 150 } }, “ns” : “dbversity.dbfry”, “min” : { “id” : 150 }, “max” : { “id” : 200 }, “tag” : “DBFRY” }
mongos>
mongos>
mongos> sh.addTagRange(“dbversity.dbfry”, { id: 1 }, { id: 150 }, “DBFRY”)
mongos>
mongos> db.tags.find()
{ “_id” : { “ns” : “dbversity.dbfry”, “min” : { “id” : 1 } }, “ns” : “dbversity.dbfry”, “min” : { “id” : 1 }, “max” : { “id” : 150 }, “tag” : “DBFRY” }
{ “_id” : { “ns” : “dbversity.dbfry”, “min” : { “id” : 150 } }, “ns” : “dbversity.dbfry”, “min” : { “id” : 150 }, “max” : { “id” : 200 }, “tag” : “DBFRY” }
mongos>
mongos>

 

This is what happen when you create same tags.
use dbversity
db.dbfry.ensureIndex( { id : 1 }, { unique : true })
sh.enableSharding(“dbversity”)
sh.shardCollection(“dbversity.dbfry”, {“id”:1})
sh.startBalancer()
sh.getBalancerState()
sh.isBalancerRunning()
use dbversity
for(var i = 1; i <= 301 ; i++){db.dbfry.insert({“id” : i , “name” : “bulk-inserts”, ” Iteration: ” : i });}

shards:
{ “_id” : “rs1”, “host” : “rs1/srinivas-HP-G62-Notebook-PC:27010,srinivas-HP-G62-Notebook-PC:27011” }
{ “_id” : “rs2”, “host” : “rs2/srinivas-HP-G62-Notebook-PC:27020,srinivas-HP-G62-Notebook-PC:27021” }
{ “_id” : “rs3”, “host” : “rs3/srinivas-HP-G62-Notebook-PC:27030,srinivas-HP-G62-Notebook-PC:27031” }
#mongo localhost:10000/dbversity
MongoDB shell version: 3.2.0
connecting to: localhost:10000/dbversity

Tag a Shard

Associate tags with a particular shard using the sh.addShardTag() method when connected to a mongos instance. A single shard may have multiple tags, and multiple shards may also have the same tag.
sh.addShardTag(“rs1”, “DBVERSITY”)
sh.addShardTag(“rs2”, “DBFRY”)
sh.addShardTag(“rs3”, “DBFRY”)

Tag a Shard Key Range

To assign a tag to a range of shard keys use the sh.addTagRange() method when connected to a mongos instance.

Any given shard key range may only have one assigned tag.
You cannot overlap defined ranges, or tag the same range more than once – it’ll not throw any errors but won’t effect anything if you try so.
sh.addTagRange(“dbversity.dbfry”, { id: 1 }, { id: 100 }, “DBVERSITY”)
sh.addTagRange(“dbversity.dbfry”, { id: 101 }, { id: 200 }, “DBFRY”)
sh.addTagRange(“dbversity.dbfry”, { id: 201 }, { id: 301 }, “DBFRY”)

mongos> show dbs
config 0.001GB
dbversity 0.000GB

#mongo localhost:10000/dbversity
MongoDB shell version: 3.2.0
connecting to: localhost:10000/dbversity
mongos>
mongos> db.dbfry.count()
301
mongos>
mongos>
mongos>
mongos> db.dbfry.find().limit(2)
{ “_id” : ObjectId(“568ba25ebecb5e1918a71524”), “id” : 1, “name” : “bulk-inserts”, ” Iteration: ” : 1 }
{ “_id” : ObjectId(“568ba25ebecb5e1918a71525”), “id” : 2, “name” : “bulk-inserts”, ” Iteration: ” : 2 }
mongos>
mongos> db.dbfry.find({ id: { $gt : 299} })
{ “_id” : ObjectId(“568ba25ebecb5e1918a7164f”), “id” : 300, “name” : “bulk-inserts”, ” Iteration: ” : 300 }
{ “_id” : ObjectId(“568ba25ebecb5e1918a71650”), “id” : 301, “name” : “bulk-inserts”, ” Iteration: ” : 301 }
mongos>
mongos>

#./mongo localhost:27010/dbversity
MongoDB shell version: 3.2.0
connecting to: localhost:27010/dbversity
rs1:PRIMARY>
rs1:PRIMARY> db.dbfry.find().limit(2)
{ “_id” : ObjectId(“568ba25ebecb5e1918a71524”), “id” : 1, “name” : “bulk-inserts”, ” Iteration: ” : 1 }
{ “_id” : ObjectId(“568ba25ebecb5e1918a71525”), “id” : 2, “name” : “bulk-inserts”, ” Iteration: ” : 2 }
rs1:PRIMARY>
rs1:PRIMARY> db.dbfry.find({ id: { $gt : 99} })
{ “_id” : ObjectId(“568ba25ebecb5e1918a71587”), “id” : 100, “name” : “bulk-inserts”, ” Iteration: ” : 100 }
rs1:PRIMARY>
rs1:PRIMARY> db.dbfry.count()
100
rs1:PRIMARY>
bye
#
#
#./mongo localhost:27020/dbversity
MongoDB shell version: 3.2.0
connecting to: localhost:27020/dbversity
rs2:PRIMARY>
rs2:PRIMARY> db.dbfry.count()
100
rs2:PRIMARY> db.dbfry.find().limit(2)
{ “_id” : ObjectId(“568ba25ebecb5e1918a71588”), “id” : 101, “name” : “bulk-inserts”, ” Iteration: ” : 101 }
{ “_id” : ObjectId(“568ba25ebecb5e1918a71589”), “id” : 102, “name” : “bulk-inserts”, ” Iteration: ” : 102 }
rs2:PRIMARY>
rs2:PRIMARY> db.dbfry.find({ id: { $gt : 199} })
{ “_id” : ObjectId(“568ba25ebecb5e1918a715eb”), “id” : 200, “name” : “bulk-inserts”, ” Iteration: ” : 200 }
rs2:PRIMARY>
rs2:PRIMARY>
bye
#
#
#./mongo localhost:27030/dbversity
MongoDB shell version: 3.2.0
connecting to: localhost:27030/dbversity
rs3:PRIMARY>
rs3:PRIMARY> db.dbfry.count()
101
rs3:PRIMARY>
rs3:PRIMARY> db.dbfry.find().limit(2)
{ “_id” : ObjectId(“568ba25ebecb5e1918a715ec”), “id” : 201, “name” : “bulk-inserts”, ” Iteration: ” : 201 }
{ “_id” : ObjectId(“568ba25ebecb5e1918a715ed”), “id” : 202, “name” : “bulk-inserts”, ” Iteration: ” : 202 }
rs3:PRIMARY>
rs3:PRIMARY> db.dbfry.find({ id: { $gt : 299} })
{ “_id” : ObjectId(“568ba25ebecb5e1918a7164f”), “id” : 300, “name” : “bulk-inserts”, ” Iteration: ” : 300 }
{ “_id” : ObjectId(“568ba25ebecb5e1918a71650”), “id” : 301, “name” : “bulk-inserts”, ” Iteration: ” : 301 }
rs3:PRIMARY>
rs3:PRIMARY>
rs3:PRIMARY>
bye
#

  • Ask Question