MongoDB Tag-aware sharding script

Commands : 

use dbversity
sh.status()
sh.enableSharding(“dbversity”)

db.nosql.ensureIndex({“mongodb” : 1})
db.rdbms.ensureIndex({“oracle” : 1})
db.newsql.ensureIndex({“memsql” : 1})

show collections
sh.status()
sh.shardCollection(“dbversity.nosql”, {mongodb:1});
sh.shardCollection(“dbversity.rdbms”, {oracle:1});
sh.shardCollection(“dbversity.newsql”, {memsql:1});

sh.status()
for (var i=0; i<100000; i++) { db[“nosql”].insert({mongodb: Math.random(), count: i, time: new Date()}); }
for (var i=0; i<100000; i++) { db[“rdbms”].insert({oracle: Math.random(), count: i, time: new Date()}); }
for (var i=0; i<100000; i++) { db[“newsql”].insert({memsql: Math.random(), count: i, time: new Date()}); }

sh.status()
db.nosql.count()
db.rdbms.count()
db.newsql.count()

use config
db.chunks.find({ns: “dbversity.nosql”}, {shard:1, _id:0}).sort({shard:1})
db.chunks.find({ns: “dbversity.rdbms”}, {shard:1, _id:0}).sort({shard:1})
db.chunks.find({ns: “dbversity.newsql”}, {shard:1, _id:0}).sort({shard:1})

sh.addShardTag(“rs1”, “MON”)
sh.addShardTag(“rs2”, “ORA”)
sh.addShardTag(“rs3”, “NEW”)

db.shards.find()
sh.addTagRange(“dbversity.nosql”, {mongodb:MinKey}, {mongodb:MaxKey}, “MON”)
sh.addTagRange(“dbversity.rdbms”, {oracle:MinKey}, {oracle:MaxKey}, “ORA”)
sh.addTagRange(“dbversity.newsql”, {memsql:MinKey}, {memsql:MaxKey}, “NEW”)

use config
db.chunks.find({ns: “dbversity.nosql”}, {shard:1, _id:0}).sort({shard:1})
db.chunks.find({ns: “dbversity.rdbms”}, {shard:1, _id:0}).sort({shard:1})
db.chunks.find({ns: “dbversity.newsql”}, {shard:1, _id:0}).sort({shard:1})

 

Enabling process 

 

[usr@dbversity.com bin]# ./mongo –port 10000
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:10000/test
mongos>
mongos>
mongos>
mongos> use dbversity
switched to db dbversity
mongos>
mongos>
mongos>
mongos>
mongos>
mongos>
mongos>
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“version” : 3,
“minCompatibleVersion” : 3,
“currentVersion” : 4,
“clusterId” : ObjectId(“56fb98bf20d0a81db2d20426”)
}
shards:
{ “_id” : “rs1”, “host” : “rs1/12d4-dl585-04:27010,12d4-dl585-04:27011” }
{ “_id” : “rs2”, “host” : “rs2/12d4-dl585-04:27020,12d4-dl585-04:27021” }
{ “_id” : “rs3”, “host” : “rs3/12d4-dl585-04:27030,12d4-dl585-04:27031” }
databases:
{ “_id” : “admin”, “partitioned” : false, “primary” : “config” }

mongos>
mongos>
mongos> sh.enableSharding(“dbversity”)
{ “ok” : 1 }
mongos>
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“version” : 3,
“minCompatibleVersion” : 3,
“currentVersion” : 4,
“clusterId” : ObjectId(“56fb98bf20d0a81db2d20426”)
}
shards:
{ “_id” : “rs1”, “host” : “rs1/12d4-dl585-04:27010,12d4-dl585-04:27011” }
{ “_id” : “rs2”, “host” : “rs2/12d4-dl585-04:27020,12d4-dl585-04:27021” }
{ “_id” : “rs3”, “host” : “rs3/12d4-dl585-04:27030,12d4-dl585-04:27031” }
databases:
{ “_id” : “admin”, “partitioned” : false, “primary” : “config” }
{ “_id” : “dbversity”, “partitioned” : true, “primary” : “rs2” }

mongos>
mongos> db.nosql.ensureIndex({“mongodb” : 1})
mongos> db.rdbms.ensureIndex({“oracle” : 1})
mongos> db.newsql.ensureIndex({“memsql” : 1})
mongos>
mongos> show collections
newsql
nosql
rdbms
system.indexes
mongos>
mongos> db.newsql.count()
0
mongos> db.rdbms.count()
0
mongos> db.nosql.count()
0
mongos>
mongos>
mongos> sh.shardCollection(“dbversity.nosql”, {mongodb:1});
mongos> sh.shardCollection(“dbversity.nosql”, {mongodb:1});
mongos> sh.shardCollection(“dbversity.rdbms”, {oracle:1});
mongos>
mongos>
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“version” : 3,
“minCompatibleVersion” : 3,
“currentVersion” : 4,
“clusterId” : ObjectId(“56fb98bf20d0a81db2d20426”)
}
shards:
{ “_id” : “rs1”, “host” : “rs1/12d4-dl585-04:27010,12d4-dl585-04:27011” }
{ “_id” : “rs2”, “host” : “rs2/12d4-dl585-04:27020,12d4-dl585-04:27021” }
{ “_id” : “rs3”, “host” : “rs3/12d4-dl585-04:27030,12d4-dl585-04:27031” }
databases:
{ “_id” : “admin”, “partitioned” : false, “primary” : “config” }
{ “_id” : “dbversity”, “partitioned” : true, “primary” : “rs2” }
dbversity.newsql
shard key: { “memsql” : 1 }
chunks:
rs2 1
{ “memsql” : { “$minKey” : 1 } } –>> { “memsql” : { “$maxKey” : 1 } } on : rs2 Timestamp(1, 0)
dbversity.nosql
shard key: { “mongodb” : 1 }
chunks:
rs2 1
{ “mongodb” : { “$minKey” : 1 } } –>> { “mongodb” : { “$maxKey” : 1 } } on : rs2 Timestamp(1, 0)
dbversity.rdbms
shard key: { “oracle” : 1 }
chunks:
rs2 1
{ “oracle” : { “$minKey” : 1 } } –>> { “oracle” : { “$maxKey” : 1 } } on : rs2 Timestamp(1, 0)

mongos>
mongos> for (var i=0; i<100000; i++) { db[“nosql”].insert({mongodb: Math.random(), count: i, time: new Date()}); }
mongos> for (var i=0; i<100000; i++) { db[“rdbms”].insert({oracle: Math.random(), count: i, time: new Date()}); }
mongos> for (var i=0; i<100000; i++) { db[“newsql”].insert({memsql: Math.random(), count: i, time: new Date()}); }

mongos> db.nosql.count()
100000
mongos> db.rdbms.count()
100000
mongos> db.newsql.count()
100000
mongos>
mongos> use config
switched to db config
mongos>
mongos> db.chunks.find({ns: “dbversity.nosql”}, {shard:1, _id:0}).sort({shard:1})
{ “shard” : “rs1” }
{ “shard” : “rs2” }
{ “shard” : “rs3” }
mongos>
mongos> db.chunks.find({ns: “dbversity.rdbms”}, {shard:1, _id:0}).sort({shard:1})
{ “shard” : “rs1” }
{ “shard” : “rs2” }
{ “shard” : “rs3” }
mongos>
mongos> db.chunks.find({ns: “dbversity.newsql”}, {shard:1, _id:0}).sort({shard:1})
{ “shard” : “rs1” }
{ “shard” : “rs2” }
{ “shard” : “rs3” }
mongos>

mongos> use dbversity
switched to db dbversity
mongos>
mongos> db.nosql.findOne()
{
“_id” : ObjectId(“56fb9fd139bd1e73b034e05a”),
“mongodb” : 0.6616894330363721,
“count” : 0,
“time” : ISODate(“2016-03-30T09:43:45.346Z”)
}
mongos>
mongos>
mongos> db.rdbms.findOne()
{
“_id” : ObjectId(“56fba00839bd1e73b03666fa”),
“oracle” : 0.6739588007330894,
“count” : 0,
“time” : ISODate(“2016-03-30T09:44:40.197Z”)
}
mongos>
mongos> db.newsql.findOne()
{
“_id” : ObjectId(“56fba0ce39bd1e73b037ed9a”),
“memsql” : 0.8853254450950772,
“count” : 0,
“time” : ISODate(“2016-03-30T09:47:58.836Z”)
}
mongos>
mongos> db.nosql.find({“mongodb” : 0.6616894330363721}).explain()
{
“clusteredType” : “ParallelSort”,
“shards” : {
“rs2/12d4-dl585-04:27020,12d4-dl585-04:27021” : [
{
“cursor” : “BtreeCursor mongodb_1”,
“isMultiKey” : false,
“n” : 1,
“nscannedObjects” : 1,
“nscanned” : 1,
“nscannedObjectsAllPlans” : 1,
“nscannedAllPlans” : 1,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 0,
“nChunkSkips” : 0,
“millis” : 0,
“indexBounds” : {
“mongodb” : [
[
0.6616894330363721,
0.6616894330363721
]
]
},
“server” : “12d4-dl585-04:27020”
}
]
},
“cursor” : “BtreeCursor mongodb_1”,
“n” : 1,
“nChunkSkips” : 0,
“nYields” : 0,
“nscanned” : 1,
“nscannedAllPlans” : 1,
“nscannedObjects” : 1,
“nscannedObjectsAllPlans” : 1,
“millisShardTotal” : 0,
“millisShardAvg” : 0,
“numQueries” : 1,
“numShards” : 1,
“indexBounds” : {
“mongodb” : [
[
0.6616894330363721,
0.6616894330363721
]
]
},
“millis” : 2
}
mongos>

[usr@dbversity.com bin]# ./mongo –port 27010
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27010/test
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY> use dbversity
switched to db dbversity
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY> show collections
newsql
nosql
rdbms
system.indexes
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY> db.nosql.count()
7430
rs1:PRIMARY> db.rdbms.count()
14462
rs1:PRIMARY> db.newsql.count()
5961
rs1:PRIMARY>
rs1:PRIMARY>
bye
[usr@dbversity.com bin]#
[usr@dbversity.com bin]# ./mongo –port 27020
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27020/test
rs2:PRIMARY>
rs2:PRIMARY> use dbversity
switched to db dbversity
rs2:PRIMARY> db.nosql.count()
92557
rs2:PRIMARY>
rs2:PRIMARY> db.rdbms.count()
85537
rs2:PRIMARY>
rs2:PRIMARY> db.newsql.count()
94036
rs2:PRIMARY>
rs2:PRIMARY>
bye
[usr@dbversity.com bin]#
[usr@dbversity.com bin]# ./mongo –port 27030
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27030/test
rs3:PRIMARY>
rs3:PRIMARY> use dbversity
switched to db dbversity
rs3:PRIMARY>
rs3:PRIMARY> db.nosql.count()
13
rs3:PRIMARY> db.rdbms.count()
1
rs3:PRIMARY> db.newsql.count()
3
rs3:PRIMARY>
rs3:PRIMARY>
bye

mongos> use dbversity
switched to db dbversity
mongos>
mongos>
mongos>
mongos> sh.addShardTag(“rs1”, “MON”)
mongos> sh.addShardTag(“rs2”, “ORA”)
mongos> sh.addShardTag(“rs3”, “NEW”)
mongos>
mongos> use config
switched to db config
mongos> db.shards.find()
{ “_id” : “rs1”, “host” : “rs1/12d4-dl585-04:27010,12d4-dl585-04:27011”, “tags” : [ “MON” ] }
{ “_id” : “rs2”, “host” : “rs2/12d4-dl585-04:27020,12d4-dl585-04:27021”, “tags” : [ “ORA” ] }
{ “_id” : “rs3”, “host” : “rs3/12d4-dl585-04:27030,12d4-dl585-04:27031”, “tags” : [ “NEW” ] }
mongos>
mongos> use dbversity
switched to db dbversity
mongos>
mongos>
mongos>
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“version” : 3,
“minCompatibleVersion” : 3,
“currentVersion” : 4,
“clusterId” : ObjectId(“56fb98bf20d0a81db2d20426”)
}
shards:
{ “_id” : “rs1”, “host” : “rs1/12d4-dl585-04:27010,12d4-dl585-04:27011”, “tags” : [ “MON” ] }
{ “_id” : “rs2”, “host” : “rs2/12d4-dl585-04:27020,12d4-dl585-04:27021”, “tags” : [ “ORA” ] }
{ “_id” : “rs3”, “host” : “rs3/12d4-dl585-04:27030,12d4-dl585-04:27031”, “tags” : [ “NEW” ] }
databases:
{ “_id” : “admin”, “partitioned” : false, “primary” : “config” }
{ “_id” : “dbversity”, “partitioned” : true, “primary” : “rs2” }
dbversity.newsql
shard key: { “memsql” : 1 }
chunks:
rs3 3
{ “memsql” : { “$minKey” : 1 } } –>> { “memsql” : 0.06100128311663866 } on : rs3 Timestamp(4, 0)
{ “memsql” : 0.06100128311663866 } –>> { “memsql” : 0.9999848995357752 } on : rs3 Timestamp(5, 0)
{ “memsql” : 0.9999848995357752 } –>> { “memsql” : { “$maxKey” : 1 } } on : rs3 Timestamp(2, 0)
tag: NEW { “memsql” : { “$minKey” : 1 } } –>> { “memsql” : { “$maxKey” : 1 } }
dbversity.nosql
shard key: { “mongodb” : 1 }
chunks:
rs1 3
{ “mongodb” : { “$minKey” : 1 } } –>> { “mongodb” : 0.07499531609937549 } on : rs1 Timestamp(3, 0)
{ “mongodb” : 0.07499531609937549 } –>> { “mongodb” : 0.9999116831459105 } on : rs1 Timestamp(4, 0)
{ “mongodb” : 0.9999116831459105 } –>> { “mongodb” : { “$maxKey” : 1 } } on : rs1 Timestamp(5, 0)
tag: MON { “mongodb” : { “$minKey” : 1 } } –>> { “mongodb” : { “$maxKey” : 1 } }
dbversity.rdbms
shard key: { “oracle” : 1 }
chunks:
rs2 3
{ “oracle” : { “$minKey” : 1 } } –>> { “oracle” : 0.1431133581791073 } on : rs2 Timestamp(4, 0)
{ “oracle” : 0.1431133581791073 } –>> { “oracle” : 0.9999876511283219 } on : rs2 Timestamp(3, 1)
{ “oracle” : 0.9999876511283219 } –>> { “oracle” : { “$maxKey” : 1 } } on : rs2 Timestamp(5, 0)
tag: ORA { “oracle” : { “$minKey” : 1 } } –>> { “oracle” : { “$maxKey” : 1 } }

mongos>
mongos>
mongos> use config
switched to db config
mongos>
mongos>
mongos> db.chunks.find({ns: “dbversity.nosql”}, {shard:1, _id:0}).sort({shard:1})
{ “shard” : “rs1” }
{ “shard” : “rs1” }
{ “shard” : “rs1” }
mongos> db.chunks.find({ns: “dbversity.rdbms”}, {shard:1, _id:0}).sort({shard:1})
{ “shard” : “rs2” }
{ “shard” : “rs2” }
{ “shard” : “rs2” }
mongos> db.chunks.find({ns: “dbversity.newsql”}, {shard:1, _id:0}).sort({shard:1})
{ “shard” : “rs3” }
{ “shard” : “rs3” }
{ “shard” : “rs3” }
mongos>
mongos>
mongos>
bye
[usr@dbversity.com bin]#
[usr@dbversity.com bin]#
[usr@dbversity.com bin]# ./mongo 12d4-dl585-04:27010/dbversity
MongoDB shell version: 2.4.11
connecting to: 12d4-dl585-04:27010/dbversity
rs1:PRIMARY>
rs1:PRIMARY> db.nosql.count()
100000
rs1:PRIMARY> db.rdbms.count()
0
rs1:PRIMARY> db.newsql.count()
0
rs1:PRIMARY>
bye
[usr@dbversity.com bin]# ./mongo 12d4-dl585-04:27020/dbversity
MongoDB shell version: 2.4.11
connecting to: 12d4-dl585-04:27020/dbversity
rs2:PRIMARY>
rs2:PRIMARY>
rs2:PRIMARY> db.nosql.count()
0
rs2:PRIMARY> db.rdbms.count()
100000
rs2:PRIMARY> db.newsql.count()
0
rs2:PRIMARY>
bye
[usr@dbversity.com bin]#
[usr@dbversity.com bin]#
[usr@dbversity.com bin]# ./mongo 12d4-dl585-04:27030/dbversity
MongoDB shell version: 2.4.11
connecting to: 12d4-dl585-04:27030/dbversity
rs3:PRIMARY>
rs3:PRIMARY> db.nosql.count()
0
rs3:PRIMARY> db.rdbms.count()
0
rs3:PRIMARY> db.newsql.count()
100000
rs3:PRIMARY>
bye
[usr@dbversity.com bin]#
[usr@dbversity.com bin]#
mongos> db.nosql.find({“mongo” : 1 }).explain()
{
“clusteredType” : “ParallelSort”,
“shards” : {
“rs1/12d4-dl585-04:27010,12d4-dl585-04:27011” : [
{
“cursor” : “BasicCursor”,
“isMultiKey” : false,
“n” : 0,
“nscannedObjects” : 100000,
“nscanned” : 100000,
“nscannedObjectsAllPlans” : 100000,
“nscannedAllPlans” : 100000,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 1,
“nChunkSkips” : 0,
“millis” : 199,
“indexBounds” : {

},
“server” : “12d4-dl585-04:27010”
}
]
},
“cursor” : “BasicCursor”,
“n” : 0,
“nChunkSkips” : 0,
“nYields” : 1,
“nscanned” : 100000,
“nscannedAllPlans” : 100000,
“nscannedObjects” : 100000,
“nscannedObjectsAllPlans” : 100000,
“millisShardTotal” : 199,
“millisShardAvg” : 199,
“numQueries” : 1,
“numShards” : 1,
“indexBounds” : {

},
“millis” : 201
}
mongos> db.rdbms.find({“oracle” : 1 }).explain()
{
“clusteredType” : “ParallelSort”,
“shards” : {
“rs2/12d4-dl585-04:27020,12d4-dl585-04:27021” : [
{
“cursor” : “BtreeCursor oracle_1”,
“isMultiKey” : false,
“n” : 0,
“nscannedObjects” : 0,
“nscanned” : 0,
“nscannedObjectsAllPlans” : 0,
“nscannedAllPlans” : 0,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 0,
“nChunkSkips” : 0,
“millis” : 0,
“indexBounds” : {
“oracle” : [
[
1,
1
]
]
},
“server” : “12d4-dl585-04:27020”
}
]
},
“cursor” : “BtreeCursor oracle_1”,
“n” : 0,
“nChunkSkips” : 0,
“nYields” : 0,
“nscanned” : 0,
“nscannedAllPlans” : 0,
“nscannedObjects” : 0,
“nscannedObjectsAllPlans” : 0,
“millisShardTotal” : 0,
“millisShardAvg” : 0,
“numQueries” : 1,
“numShards” : 1,
“indexBounds” : {
“oracle” : [
[
1,
1
]
]
},
“millis” : 1
}
mongos>
mongos> db.newsql.find({“memsql” : 1 }).explain()
{
“clusteredType” : “ParallelSort”,
“shards” : {
“rs3/12d4-dl585-04:27030,12d4-dl585-04:27031” : [
{
“cursor” : “BtreeCursor memsql_1”,
“isMultiKey” : false,
“n” : 0,
“nscannedObjects” : 0,
“nscanned” : 0,
“nscannedObjectsAllPlans” : 0,
“nscannedAllPlans” : 0,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 0,
“nChunkSkips” : 0,
“millis” : 0,
“indexBounds” : {
“memsql” : [
[
1,
1
]
]
},
“server” : “12d4-dl585-04:27030”
}
]
},
“cursor” : “BtreeCursor memsql_1”,
“n” : 0,
“nChunkSkips” : 0,
“nYields” : 0,
“nscanned” : 0,
“nscannedAllPlans” : 0,
“nscannedObjects” : 0,
“nscannedObjectsAllPlans” : 0,
“millisShardTotal” : 0,
“millisShardAvg” : 0,
“numQueries” : 1,
“numShards” : 1,
“indexBounds” : {
“memsql” : [
[
1,
1
]
]
},
“millis” : 5
}
mongos>

[usr@dbversity.com bin]# ./mongo –port 27010
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27010/test
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY> use admin
switched to db admin
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY> db.shutdownServer()
Wed Mar 30 10:38:42.832 DBClientCursor::init call() failed
server should be down…
Wed Mar 30 10:38:42.837 trying reconnect to 127.0.0.1:27010
Wed Mar 30 10:38:42.837 reconnect 127.0.0.1:27010 failed couldn’t connect to server 127.0.0.1:27010
>
bye
[usr@dbversity.com bin]#
[usr@dbversity.com bin]# ./mongo –port 27011
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27011/test
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY>
bye
[usr@dbversity.com bin]#
[usr@dbversity.com bin]# ./mongo –port 10000
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:10000/test
mongos>
mongos> use dbversity
switched to db dbversity
mongos>
mongos> db.nosql.find({“mongo” : 1 }).explain()
{
“clusteredType” : “ParallelSort”,
“shards” : {
“rs1/12d4-dl585-04:27010,12d4-dl585-04:27011” : [
{
“cursor” : “BasicCursor”,
“isMultiKey” : false,
“n” : 0,
“nscannedObjects” : 100000,
“nscanned” : 100000,
“nscannedObjectsAllPlans” : 100000,
“nscannedAllPlans” : 100000,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 0,
“nChunkSkips” : 0,
“millis” : 186,
“indexBounds” : {

},
“server” : “12d4-dl585-04:27011”
}
]
},
“cursor” : “BasicCursor”,
“n” : 0,
“nChunkSkips” : 0,
“nYields” : 0,
“nscanned” : 100000,
“nscannedAllPlans” : 100000,
“nscannedObjects” : 100000,
“nscannedObjectsAllPlans” : 100000,
“millisShardTotal” : 186,
“millisShardAvg” : 186,
“numQueries” : 1,
“numShards” : 1,
“indexBounds” : {

},
“millis” : 195
}
mongos>

  • Ask Question