やり方ここに書いてありますがとりあえず試してみる。
moveChunk — MongoDB Manual 2.4.8
とりあえず現在のchunkの状態を確認。
> db.printShardingStatus() test2.table01 chunks: set02 19 set03 19 set01 18
migrationの後なので偏ってませんが、試しにchunk数19のところからひとつ18のところに移動してみます。
まずはどのchunkを移動するか決めるためにconfigサーバで該当collectionのchunkの一覧を確認。
db.chunks.find({ns:"test2.table01"}).limit(10) { "_id" : "test2.table01-userId_\"hoge\"entryId_118415", "lastmod" : { "t" : 35000, "i" : 0 }, "ns" : "test2.table01", "min" : { "userId" : "hoge", "entryId" : NumberLong(118415) }, "max" : { "userId" : "hoge", "entryId" : NumberLong(120960) }, "shard" : "set03" } { "_id" : "test2.table01-userId_\"hoge\"entryId_123505", "lastmod" : { "t" : 37000, "i" : 0 }, "ns" : "test2.table01", "min" : { "userId" : "hoge", "entryId" : NumberLong(123505) }, "max" : { "userId" : "hoge", "entryId" : NumberLong(126050) }, "shard" : "set03" } { "_id" : "test2.table01-userId_\"hoge\"entryId_128582", "lastmod" : { "t" : 41000, "i" : 0 }, "ns" : "test2.table01", "min" : { "userId" : "hoge", "entryId" : NumberLong(128582) }, "max" : { "userId" : "hoge", "entryId" : NumberLong(131114) }, "shard" : "set03" } (略)
ひとつだけ特定するために条件を絞ります。
> db.chunks.find({ns:"test2.table01",min : { userId : "hoge", entryId : NumberLong(128582) }}) { "_id" : "test2.table01-userId_\"hoge\"entryId_128582", "lastmod" : { "t" : 41000, "i" : 0 }, "ns" : "test2.table01", "min" : { "userId" : "hoge", "entryId" : NumberLong(128582) }, "max" : { "userId" : "hoge", "entryId" : NumberLong(131114) }, "shard" : "set03" }
chunkのレンジは被らないはずなので、min〜maxの間の値を指定してやればそのレンジにあるchunkのみが移動できるはず。
以下の構文でchunkの移動を実行します。
db.adminCommand({moveChunk : $ns$, find : {$findquery$}, to : "$shardname$"})
- $ns$:db.chunks.find()したときの「ns」の値を指定します。
- $findquery$:普通にfindするときと同じ。今回でいうとdb.chunks.find()したときのmin〜maxの間の値を指定します。
- $shardname$:シャード名。db.chunks.find()したときの「shard」の値を指定します。
これに従って書くと今回はこんなかんじのはず!
いざ実行!
> db.adminCommand({moveChunk:"test2.table01", find:{userId:"hoge", entryId:NumberLong(128582)}, to:"set01"}) { "assertion" : "invalid parameter: expected an object ()", "assertionCode" : 10065, "errmsg" : "db assertion failure", "ok" : 0 }
あれ!
なんぞ!
ああconfigサーバでやってた!mongosにきりかえます!
> db.adminCommand({moveChunk:"test2.table01", find:{userId:"hoge", entryId:NumberLong(128582)}, to:"set01"}) { "millis" : 1501, "ok" : 1 }
できた!
確認!
> db.printShardingStatus() test2.table01 chunks: set02 19 set03 18 set01 19
いけてる!
折角なのでもっかい同じクエリを流してみます。
> db.adminCommand({moveChunk:"test2.table01", find:{userId:"hoge", entryId:NumberLong(128582)}, to:"set01"}) { "ok" : 0, "errmsg" : "that chunk is already on that shard" }
おこられた!すみません!