mongodb的資料由monogd伺服器存儲,由mongos對寫入的資料根據片鍵進行路由,整個過程對用戶端完全透明。對chunk的移動是由“平衡器”來決定的,當然加入chunk分布不均勻了,我們也可以手工來操作
db.runcommand( { movechunk : "" ,
find : {查詢條件} ,
to : "" } )
注釋:
movechunk:一個集合的全字要加上資料庫的名稱:比如test.yql
find:一個查詢語句,對于指定集合中的符合查詢的資料或者chunk,系統自動查出from 的shard
to: 指向chunk的目的shard
隻要目的shard和源sharad同意指定的chunk由目的shard接管,指令就傳回。遷移chunk是一個比較複雜的過程,它包括兩個内部通信協定:
1 複制資料,包括在複制過程中的變化的資料
2 確定所有參與遷移的組成部分:目的shard ,源shard ,config server都确定遷移已經完成!
比如我們要将 yql.momo中的id>82842的部分由shard0001遷移到shard0002:
mongos> db.printshardingstatus()
--- sharding status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "10.250.7.225:27018" }
{ "_id" : "shard0001", "host" : "10.250.7.249:27019" }
{ "_id" : "shard0002", "host" : "10.250.7.241:27020" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
test.momo chunks:
shard0001 3
shard0002 3
shard0000 6
{ "id" : { $minkey : 1 } } -->> { "id" : 0 } on : shard0001 { "t" : 2000, "i" : 0 }
{ "id" : 0 } -->> { "id" : 11595 } on : shard0002 { "t" : 3000, "i" : 0 }
{ "id" : 11595 } -->> { "id" : 23191 } on : shard0001 { "t" : 4000, "i" : 0 }
{ "id" : 23191 } -->> { "id" : 31929 } on : shard0002 { "t" : 5000, "i" : 0 }
{ "id" : 31929 } -->> { "id" : 42392 } on : shard0001 { "t" : 6000, "i" : 0 }
{ "id" : 42392 } -->> { "id" : 62952 } on : shard0002 { "t" : 7000, "i" : 0 }
{ "id" : 62952 } -->> { "id" : 82842 } on : shard0000 { "t" : 7000, "i" : 1 }
{ "id" : 82842 } -->> { "id" : 102100 } on : shard0000 { "t" : 1000, "i" : 11 }
{ "id" : 102100 } -->> { "id" : 120602 } on : shard0000 { "t" : 1000, "i" : 13 }
{ "id" : 120602 } -->> { "id" : 287873 } on : shard0000 { "t" : 2000, "i" : 2 }
{ "id" : 287873 } -->> { "id" : 305812 } on : shard0000 { "t" : 2000, "i" : 6 }
{ "id" : 305812 } -->> { "id" : { $maxkey : 1 } } on : shard0000 { "t" : 2000, "i" : 7 }
test.yql chunks:
shard0001 2
shard0000 1
shard0002 1
{ "_id" : { $minkey : 1 } } -->> { "_id" : objectid("4eb298b3adbd9673afee95e3") } on : shard0001 { "t" : 4000, "i" : 0 }
{ "_id" : objectid("4eb298b3adbd9673afee95e3") } -->> { "_id" : objectid("4eb2a64640643e5bb60072f7") } on : shard0000 { "t" : 4000, "i" : 1 }
{ "_id" : objectid("4eb2a64640643e5bb60072f7") } -->> { "_id" : objectid("4eb2a65340643e5bb600e084") } on : shard0002 { "t" : 3000, "i" : 1 }
{ "_id" : objectid("4eb2a65340643e5bb600e084") } -->> { "_id" : { $maxkey : 1 } } on : shard0001 { "t" : 3000, "i" : 0 }
{ "_id" : "mongos", "partitioned" : false, "primary" : "shard0000" }
執行遷移指令:
mongos> db.admincommand({movechunk : "test.momo", find : {id:{$gt:82842}}, to : "shard0002"});
{ "millis" : 1474, "ok" : 1 }
再次檢視:
shard0001 4
shard0000 4
shard0002 4
{ "id" : 0 } -->> { "id" : 11595 } on : shard0000 { "t" : 11000, "i" : 0 }
{ "id" : 23191 } -->> { "id" : 31929 } on : shard0002 { "t" : 11000, "i" : 1 }
{ "id" : 62952 } -->> { "id" : 82842 } on : shard0001 { "t" : 8000, "i" : 0 }
{ "id" : 82842 } -->> { "id" : 102100 } on : shard0002 { "t" : 9000, "i" : 0 }
{ "id" : 102100 } -->> { "id" : 120602 } on : shard0000 { "t" : 10000, "i" : 1 }
{ "id" : 305812 } -->> { "id" : { $maxkey : 1 } } on : shard0002 { "t" : 10000, "i" : 0 }
mongos>
<b>從結果看出:并非所有id>82842的資料所在的chunk都遷移到了shard0002上面,[82842,102100] 和[305812 ,+∞]遷移到了shard0002.這個有點不明白??</b>
日志記錄:
##發指令
sat nov 5 16:15:35 [conn1] cmd: movechunk: { movechunk: "test.momo", find: { id: { $gt: 82842.0 } }, to: "shard0002" }
##遷移資料
sat nov 5 16:15:35 [conn1] moving chunk ns: test.momo moving ( ns:test.momo at: shard0000:10.250.7.225:27018 lastmod: 2|7 min: { id: 305812 } max: { id: maxkey }) shard0000:10.250.7.225:27018 -> shard0002:10.250.7.241:27020
sat nov 5 16:15:36 [balancer] distributed lock 'balancer/rac4:27017:1320477786:1804289383' acquired, ts : 4eb4f0a818ed672581e262dd
sat nov 5 16:15:36 [balancer] distributed lock 'balancer/rac4:27017:1320477786:1804289383' unlocked.
sat nov 5 16:15:37 [conn1] created new distributed lock for test.momo on rac1:28001,rac2:28002,rac3:28003 ( lock timeout : 900000, ping interval : 30000, process : 0 )
sat nov 5 16:15:37 [conn1] chunkmanager: time to load chunks for test.momo: 0ms sequencenumber: 10 version: 10|1
sat nov 5 16:15:41 [balancer] distributed lock 'balancer/rac4:27017:1320477786:1804289383' acquired, ts : 4eb4f0ad18ed672581e262de
sat nov 5 16:15:41 [balancer] chose [shard0002] to [shard0000] { _id: "test.momo-id_0.0", lastmod: timestamp 3000|0, ns: "test.momo", min: { id: 0.0 }, max: { id: 11595 }, shard: "shard0002" }
sat nov 5 16:15:41 [balancer] moving chunk ns: test.momo moving ( ns:test.momo at: shard0002:10.250.7.241:27020 lastmod: 3|0 min: { id: 0.0 } max: { id: 11595 }) shard0002:10.250.7.241:27020 -> shard0000:10.250.7.225:27018
sat nov 5 16:15:43 [balancer] created new distributed lock for test.momo on rac1:28001,rac2:28002,rac3:28003 ( lock timeout : 900000, ping interval : 30000, process : 0 )
sat nov 5 16:15:43 [balancer] chunkmanager: time to load chunks for test.momo: 0ms sequencenumber: 11 version: 11|1