domenica 28 febbraio 2016

Mongodb shard test (versione 3.0.7)

In questo esempio creiamo un ambiente con:
  • 3 config server;
  • 2 shard;
  • 2 router(mongos)
Per prima cosa mi sono creato una directory shardTest con all'interno le seguenti directory:
  • configServ;
  • configServ2;
  • configServ3;
  • shard1;
  • shard2

Quindiho fatto partire da shell prima i config server poi i router e poi gli shard


mongod --configsvr --port 30000 --dbpath /home/csciandrone//eserciziMongo/shardTest/configServ --logpath /home/csciandrone/eserciziMongo/shardTest/configServ/log.txt --fork



mongod --configsvr --port 30001 --dbpath /home/csciandrone//eserciziMongo/shardTest/configServ2 --logpath /home/csciandrone/eserciziMongo/shardTest/configServ2/log.txt --fork



mongod --configsvr --port 30002 --dbpath /home/csciandrone//eserciziMongo/shardTest/configServ3 --logpath /home/csciandrone/eserciziMongo/shardTest/configServ3/log.txt --fork


Di seguito i comandi per attivare i mongos:

mongos --port 27018 --configdb 127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:30002
mongos --port 27019 --configdb 127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:30002



Ora facciamo partire i 2 mongod (i 2 shard):

mongod --port 27020 --dbpath /home/csciandrone/eserciziMongo/shardTest/shard1 --logpath /home/csciandrone/eserciziMongo/shardTest/shard1/log.txt --fork
mongod --port 27021 --dbpath /home/csciandrone/eserciziMongo/shardTest/shard2 --logpath /home/csciandrone/eserciziMongo/shardTest/shard2/log.txt --fork


Ora possiamo connetterci ad uno dei due mongos e aggiungere i 2 shard appena creati:

mongo --port 27018
MongoDB shell version: 3.0.9
connecting to: 127.0.0.1:27018/test
mongos> sh.addShard("127.0.0.1:27020")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("127.0.0.1:27021")
{ "shardAdded" : "shard0001", "ok" : 1 }



A questo punto il nostro sharding cluster è configurato correttamente.
Occorre quindi operare nel seguente modo:
1) scegliere un db e creare la collection (es db test collection randomNumbers);
2) abilitare lo sharding per il db;
3) scegliere chiave di sharding per la collection;
4) creare la shard key

Quindi supponiamo di inserire un record in una collection:

db.randomNumbers.insert({a:1,b:2,c:3}) 

Creaiamo un indice sulla chiave a:
db.randomNumbers.createIndex({a:1})

Quindi abilitiamo lo shard:

sh.enableSharding("test")

Ora effettuiamo lo shard della collection sulla chiave a:

sh.shardCollection("test.randomNumbers",{a:1})
Con il comando sh.status() possiamo vedere lo stato del cluster:

sh.status()
--- Sharding Status --- 
  sharding version: {
 "_id" : 1,
 "minCompatibleVersion" : 5,
 "currentVersion" : 6,
 "clusterId" : ObjectId("56d305ccad2dda9a7c735322")
}
  shards:
 {  "_id" : "shard0000",  "host" : "127.0.0.1:27020" }
 {  "_id" : "shard0001",  "host" : "127.0.0.1:27021" }
  balancer:
 Currently enabled:  yes
 Currently running:  no
 Failed balancer rounds in last 5 attempts:  0
 Migration Results for the last 24 hours: 
  No recent migrations
  databases:
 {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
 {  "_id" : "test",  "partitioned" : true,  "primary" : "shard0000" }
  test.randomNumbers
   shard key: { "a" : 1 }
   chunks:
    shard0000 1
   { "a" : { "$minKey" : 1 } } -->> { "a" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)

Ora proviamo ad inserire un pò di record nella collection:

for(var i=0;i<100000;i++){db.randomNumbers.insert({a:Math.floor(Math.random()*100),b:Math.floor(Math.random()*100),c:Math.floor(Math.random()*100)})}

Ora possiamo rieseguire il comando sh.status() e vediamo che il balancer è entrato in azione:

sh.status(true)
--- Sharding Status --- 
  sharding version: {
 "_id" : 1,
 "minCompatibleVersion" : 5,
 "currentVersion" : 6,
 "clusterId" : ObjectId("56d305ccad2dda9a7c735322")
}
  shards:
 {  "_id" : "shard0000",  "host" : "127.0.0.1:27020" }
 {  "_id" : "shard0001",  "host" : "127.0.0.1:27021" }
  balancer:
 Currently enabled:  yes
 Currently running:  no
 Failed balancer rounds in last 5 attempts:  0
 Migration Results for the last 24 hours: 
  1 : Success
  1 : Failed with error 'could not acquire collection lock for test.randomNumbers to migrate chunk [{ : MinKey },{ : MaxKey }) :: caused by :: Lock for migrating chunk [{ : MinKey }, { : MaxKey }) in test.randomNumbers is taken.', from shard0000 to shard0001
  databases:
 {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
 {  "_id" : "test",  "partitioned" : true,  "primary" : "shard0000" }
  test.randomNumbers
   shard key: { "a" : 1 }
   chunks:
    shard0000 2
    shard0001 1
   { "a" : { "$minKey" : 1 } } -->> { "a" : 9 } on : shard0000 Timestamp(2, 1) 
   { "a" : 9 } -->> { "a" : 81 } on : shard0000 Timestamp(1, 2) 
   { "a" : 81 } -->> { "a" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 0)

Nessun commento:

Posta un commento