On this page本页内容
The mergeChunks
command allows you to combine continuous chunks on the same shard into a single chunk. This tutorial explains how to merge neighboring chunks in a sharded cluster.mergeChunks
命令允许您将同一分片上的连续块合并为单个块。本教程解释了如何在分片集群中合并相邻的块。
Examples in this procedure use a 本过程中的示例使用members
collection in the test
database, using the username
field as the shard key.test
数据库中的members
集合,将username
字段用作分片键。
In 在mongosh
, identify the chunk ranges with the following operation:mongosh
中,使用以下操作标识区块范围:
sh.status()
In the output, the chunk ranges appear after the chunk counts for each sharded collection, as in the following example:在输出中,区块范围显示在每个分片集合的区块计数之后,如以下示例所示:
--- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5ebf0bfd3eeb6037ec7cbba9") } shards: { "_id" : "shardA", "host" : "shardA/shardA-m1.example.net:27018,shardA-m2.example.net:27018,shardA-m3.example.net:27018", "state" : 1 } { "_id" : "shardB", "host" : "shardB/shardB-m1.example.net:27018,shardB-m2.example.net:27018,shardB-m3.example.net:27018", "state" : 1 } active mongoses: "4.4.0" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 519 : Success databases: { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: shardA 512 shardB 512 too many chunks to print, use verbose if you want to force print { "_id" : "test", "primary" : "shardA", "partitioned" : true, "version" : { "uuid" : UUID("22c042fc-7e3d-4c6d-992d-f3d714759781"), "lastMod" : 1 } } test.members shard key: { "username" : 1 } unique: false balancing: true chunks: shardA 7 shardB 7 { "username" : { "$minKey" : 1 } } -->> { "username" : "user16643" } on : shardB Timestamp(13, 0) { "username" : "user16643" } -->> { "username" : "user2329" } on : shardB Timestamp(16, 0) { "username" : "user2329" } -->> { "username" : "user29937" } on : shardB Timestamp(17, 0) { "username" : "user29937" } -->> { "username" : "user36583" } on : shardB Timestamp(18, 0) { "username" : "user36583" } -->> { "username" : "user43229" } on : shardB Timestamp(19, 0) { "username" : "user43229" } -->> { "username" : "user49877" } on : shardB Timestamp(20, 0) { "username" : "user49877" } -->> { "username" : "user56522" } on : shardB Timestamp(21, 0) { "username" : "user56522" } -->> { "username" : "user63169" } on : shardA Timestamp(21, 1) { "username" : "user63169" } -->> { "username" : "user69816" } on : shardA Timestamp(10, 1) { "username" : "user69816" } -->> { "username" : "user76462" } on : shardA Timestamp(11, 1) { "username" : "user76462" } -->> { "username" : "user83108" } on : shardA Timestamp(12, 1) { "username" : "user83108" } -->> { "username" : "user89756" } on : shardA Timestamp(14, 1) { "username" : "user89756" } -->> { "username" : "user96401" } on : shardA Timestamp(15, 1) { "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shardA Timestamp(15, 2)
The chunk ranges appear after the chunk counts for each sharded collection. 区块范围显示在每个分片集合的区块计数之后。For example, the following are the chunk ranges for the 例如,以下是test.members
collection:test.members
集合的区块范围:
{ "username" : { "$minKey" : 1 } } -->> { "username" : "user16643" } on : shardB Timestamp(13, 0) { "username" : "user16643" } -->> { "username" : "user2329" } on : shardB Timestamp(16, 0) { "username" : "user2329" } -->> { "username" : "user29937" } on : shardB Timestamp(17, 0) { "username" : "user29937" } -->> { "username" : "user36583" } on : shardB Timestamp(18, 0) { "username" : "user36583" } -->> { "username" : "user43229" } on : shardB Timestamp(19, 0) { "username" : "user43229" } -->> { "username" : "user49877" } on : shardB Timestamp(20, 0) { "username" : "user49877" } -->> { "username" : "user56522" } on : shardB Timestamp(21, 0) { "username" : "user56522" } -->> { "username" : "user63169" } on : shardA Timestamp(21, 1) { "username" : "user63169" } -->> { "username" : "user69816" } on : shardA Timestamp(10, 1) { "username" : "user69816" } -->> { "username" : "user76462" } on : shardA Timestamp(11, 1) { "username" : "user76462" } -->> { "username" : "user83108" } on : shardA Timestamp(12, 1) { "username" : "user83108" } -->> { "username" : "user89756" } on : shardA Timestamp(14, 1) { "username" : "user89756" } -->> { "username" : "user96401" } on : shardA Timestamp(15, 1) { "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shardA Timestamp(15, 2)
Merge contiguous chunks on the same shard.合并同一分片上的连续块。
For example, consider the following chunk ranges on 例如,考虑shardA
:shardA
上的以下区块范围:
The chunks to be merged are highlighted.将突出显示要合并的块。
{ "username" : "user56522" } -->> { "username" : "user63169" } on : shardA Timestamp(21, 1) { "username" : "user63169" } -->> { "username" : "user69816" } on : shardA Timestamp(10, 1) { "username" : "user69816" } -->> { "username" : "user76462" } on : shardA Timestamp(11, 1) { "username" : "user76462" } -->> { "username" : "user83108" } on : shardA Timestamp(12, 1) { "username" : "user83108" } -->> { "username" : "user89756" } on : shardA Timestamp(14, 1) { "username" : "user89756" } -->> { "username" : "user96401" } on : shardA Timestamp(15, 1) { "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shardA Timestamp(15, 2)
To merge the highlighted contiguous chunks, issue the 要合并突出显示的连续块,请对管理数据库发出mergeChunks
command against the admin
database:mergeChunks
命令:
db.adminCommand( { mergeChunks: "test.members", bounds: [ { "username" : "user69816" }, { "username" : "user96401" } ] } )
On success, 成功后,mergeChunks
produces the following output:mergeChunks
生成以下输出:
{ "ok" : 1, "operationTime" : Timestamp(1589580356, 14), "$clusterTime" : { "clusterTime" : Timestamp(1589580356, 14), "signature" : { "hash" : BinData(0,"up5VKd49G/uPCq1iger2nOtfIHw="), "keyId" : NumberLong("6827188741371592725") } } }
On any failure condition, 在任何失败条件下,mergeChunks
returns a document where the value of the ok
field is 0
.mergeChunks
都会返回ok
字段值为0
的文档。
After merging the identified chunks, confirm the new chunk, as follows:合并识别的块后,按如下方式确认新块:
sh.status()
The output of sh.status()
should resemble:sh.status()
的输出应该类似于:
--- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5ebef5447fa151d4bd79dd72") } shards: { "_id" : "shardA", "host" : "shardA/shardA-m1.example.net:27018,shardA-m2.example.net:27018,shardA-m3.example.net:27018", "state" : 1 } { "_id" : "shardB", "host" : "shardB/shardB-m1.example.net:27018,shardB-m2.example.net:27018,shardB-m3.example.net:27018", "state" : 1 } active mongoses: "4.4.0" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 519 : Success databases: { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: shardA 512 shardB 512 too many chunks to print, use verbose if you want to force print { "_id" : "test", "primary" : "shardA", "partitioned" : true, "version" : { "uuid" : UUID("22c042fc-7e3d-4c6d-992d-f3d714759781"), "lastMod" : 1 } } test.members shard key: { "username" : 1 } unique: false balancing: true chunks: shardA 5 shardB 6 { "username" : { "$minKey" : 1 } } -->> { "username" : "user16643" } on : shardA Timestamp(22, 0) { "username" : "user16643" } -->> { "username" : "user2329" } on : shardB Timestamp(22, 1) { "username" : "user2329" } -->> { "username" : "user29937" } on : shardB Timestamp(17, 0) { "username" : "user29937" } -->> { "username" : "user36583" } on : shardB Timestamp(18, 0) { "username" : "user36583" } -->> { "username" : "user43229" } on : shardB Timestamp(19, 0) { "username" : "user43229" } -->> { "username" : "user49877" } on : shardB Timestamp(20, 0) { "username" : "user49877" } -->> { "username" : "user56522" } on : shardB Timestamp(21, 0) { "username" : "user56522" } -->> { "username" : "user63169" } on : shardA Timestamp(21, 1) { "username" : "user63169" } -->> { "username" : "user69816" } on : shardA Timestamp(10, 1) { "username" : "user69816" } -->> { "username" : "user96401" } on : shardA Timestamp(21, 2) { "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shardA Timestamp(15, 2)
After the merge, the balancer may migrate chunks across shards to ensure a more even distribution of chunks.合并后,平衡器可以跨分片迁移块,以确保块的分布更加均匀。