tjtjtjのメモ

自分のためのメモです

mongodb で複数シャードを立ち上げる。

前回まで

前回は、シャードを2つ登録し、10万件のドキュメントを登録した。ドキュメントは全てshard0000に登録された。

データを確認する。

> db.emails.count();
100000

モデル的にはユーザー:10 の受信箱には 4000件のメールがあり、送信箱に1000件のメールがある状態。

> db.emails.find({to:10}).count();
4000
> db.emails.find({from:10}).count();
1000

ユーザー:10が最後に受信したメールはこうやって得る。

> db.emails.find({to:10}).sort({id:-1}).limit(1);
{ "_id" : ObjectId("4d651216ba6e0000000322df"), "id" : 99908, "from" : 9, "to" :
 [ 10, 11, 12, 13 ], "subject" : "subject99908", "body" : "body99908" }
>

Chunkの分割と移動

普通はchunkの分割は自動で行われます。現状では、分割はinsertingに関して副作用を持っています。将来的にchunkの自動分割は他の事例にも適用される予定です。(Chunkの分割は透過的です)

Chunkの分割

普通は自動だそうだが、手動でやってみる。

Chunkの移動

> use admin
switched to db admin
> db.runCommand( { moveChunk : "test.emails" , find : { id : 50000 } , to : "shard0001" } )
{ "millis" : 11762, "ok" : 1 }
>

確認

> db.printShardingStatus()
--- Sharding Status ---
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
      { "_id" : "shard0000", "host" : "localhost:27101" }
      { "_id" : "shard0001", "host" : "localhost:27102" }
  databases:
        { "_id" : "admin", "partitioned" : false, "primary" : "config" }
        { "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
                test.emails chunks:
                        { "id" : { $minKey : 1 } } -->> { "id" : { $maxKey : 1 }
 } on : shard0001 { "t" : 2000, "i" : 0 }
        { "_id" : "local", "partitioned" : false, "primary" : "shard0001" }

>

これだと、minKey から maxKey まで shard0001 へ移動してしまっている。ああ、moveChunk は移動だ。分割はsplitだ。

> db.runCommand( { split : "test.emails" , middle : { id : 50000 } } );
{ "ok" : 1 }
> db.printShardingStatus()
--- Sharding Status ---
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
      { "_id" : "shard0000", "host" : "localhost:27101" }
      { "_id" : "shard0001", "host" : "localhost:27102" }
  databases:
        { "_id" : "admin", "partitioned" : false, "primary" : "config" }
        { "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
                test.emails chunks:
                        { "id" : { $minKey : 1 } } -->> { "id" : 50000 } on : sh
ard0001 { "t" : 2000, "i" : 1 }
                        { "id" : 50000 } -->> { "id" : { $maxKey : 1 } } on : sh
ard0001 { "t" : 2000, "i" : 2 }
        { "_id" : "local", "partitioned" : false, "primary" : "shard0001" }

>

50000でchunkが割れたので、最初のchunkをshard0000へ移動する。

> db.runCommand( { moveChunk : "test.emails" , find : { id : 1 } , to : "shard0000" } )
{ "millis" : 6456, "ok" : 1 }
> db.printShardingStatus()
--- Sharding Status ---
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
      { "_id" : "shard0000", "host" : "localhost:27101" }
      { "_id" : "shard0001", "host" : "localhost:27102" }
  databases:
        { "_id" : "admin", "partitioned" : false, "primary" : "config" }
        { "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
                test.emails chunks:
                        { "id" : { $minKey : 1 } } -->> { "id" : 50000 } on : sh
ard0000 { "t" : 3000, "i" : 0 }
                        { "id" : 50000 } -->> { "id" : { $maxKey : 1 } } on : sh
ard0001 { "t" : 3000, "i" : 1 }
        { "_id" : "local", "partitioned" : false, "primary" : "shard0001" }

>

1つ目のチャンクがshard0000、2つ目のチャンクがshard0001になっている。

データ確認

localhost:27104(mongos) で確認

C:\> \mongo\bin\mongo localhost:27104/test
MongoDB shell version: 1.6.5
connecting to: localhost:27104/test
> db.emails.count();
100000
> db.emails.find({to:10}).count();
4000
> db.emails.find({from:10}).count();
1000
> db.emails.find({to:10}).sort({id:-1}).limit(1);
{ "_id" : ObjectId("4d651216ba6e0000000322df"), "id" : 99908, "from" : 9, "to" :
 [ 10, 11, 12, 13 ], "subject" : "subject99908", "body" : "body99908" }
>

localhost:27101(shard0000) で確認

C:\> \mongo\bin\mongo localhost:27101/test
MongoDB shell version: 1.6.5
connecting to: localhost:27101/test
> db.emails.count();
50000
> db.emails.find({to:10}).count();
2000
> db.emails.find({from:10}).count();
500
> db.emails.find({to:10}).sort({id:-1}).limit(1);
{ "_id" : ObjectId("4d65120bba6e000000025f8f"), "id" : 49908, "from" : 9, "to" :
 [ 10, 11, 12, 13 ], "subject" : "subject49908", "body" : "body49908" }
>


localhost:27102(shard0001) で確認

C:\> \mongo\bin\mongo localhost:27102/test
MongoDB shell version: 1.6.5
connecting to: localhost:27102/test
> db.emails.count();
50000
> db.emails.find({to:10}).count();
2000
> db.emails.find({from:10}).count();
500
> db.emails.find({to:10}).sort({id:-1}).limit(1);
{ "_id" : ObjectId("4d651216ba6e0000000322df"), "id" : 99908, "from" : 9, "to" :
 [ 10, 11, 12, 13 ], "subject" : "subject99908", "body" : "body99908" }
>

おお、localhost:27104(mongos)でshard0000とshard0001をあわせた結果が取得できている。