【Mongodb】Sharding 手工迁移chunk

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 }
再次查看:
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       4
                                shard0000       4
                                shard0002       4
                        { "id" : { $minKey : 1 } } -->> { "id" : 0 } on : shard0001 { "t" : 2000, "i" : 0 }
                        { "id" : 0 } -->> { "id" : 11595 } on : shard0000 { "t" : 11000, "i" : 0 }
                        { "id" : 11595 } -->> { "id" : 23191 } on : shard0001 { "t" : 4000, "i" : 0 }
                        { "id" : 23191 } -->> { "id" : 31929 } on : shard0002 { "t" : 11000, "i" : 1 }
                        { "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 : 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" : 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 : shard0002 { "t" : 10000, "i" : 0 }
                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> 
从结果看出:并非所有id>82842的数据所在的chunk都迁移到了shard0002上面,[82842,102100] 和[305812 ,+∞]迁移到了shard0002.这个有点不明白??
日志记录:
##发命令
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
上一篇:我的博客pdf文档集合


下一篇:MaCfee导致Asp.net/Jmail无法发送邮件的解决办法