在分片集群中合并数据段
Overview
mergeChunks
命令允许您将同一分片上的连续分片合并为单个数据块。 本教程介绍如何合并分分片集群中的相邻数据段。
步骤
识别数据块范围
sh.status()
在输出中,数据段范围显示在每个分片集合的数据段计数之后,如以下示例所示:
--- 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)
数据块范围显示在每个分片collection的数据块计数之后。例如,以下是test.members
collection的数据块范围:
{ "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)
mergeChunks
合并同一分数据块上的连续数据段。
例如,考虑shardA
上的以下数据块范围:
注意
要合并的数据块突出显示。
{ "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)
要合并突出显示的连续数据段,请对admin
数据库发出mergeChunks
命令:
db.adminCommand( { mergeChunks: "test.members", bounds: [ { "username" : "user69816" }, { "username" : "user96401" } ] } )
成功后, mergeChunks
会生成以下输出:
{ "ok" : 1, "operationTime" : Timestamp(1589580356, 14), "$clusterTime" : { "clusterTime" : Timestamp(1589580356, 14), "signature" : { "hash" : BinData(0,"up5VKd49G/uPCq1iger2nOtfIHw="), "keyId" : NumberLong("6827188741371592725") } } }
在任何失败情况下, mergeChunks
都会返回一个文档,其中ok
字段的值为0
。
查看合并的数据块范围
合并已识别的数据块后,确认新数据块,如下所示:
sh.status()
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)
合并后,负载均衡器可能会跨分片迁移数据段,以确保数据段的分布更均匀。