Database Manual / Sharding / Reference / Defragment Sharded Collections

Monitor Defragmentation of a Sharded Collection监控分片化集合的分片整理

To monitor defragmentation of a sharded collection, use the balancerCollectionStatus command.要监视分片集合的分片整理,请使用balancerCollectionStatus命令。

You can see the current defragmentation state and the number of remaining chunks to process. This shows you the defragmentation progress.您可以看到当前的分片整理状态和要处理的剩余块数。这将显示分片整理进度。

About this Task关于此任务

Defragmentation uses the following phases to reduce the number of chunks in a collection and improve performance:分片整理使用以下阶段来减少集合中的块数并提高性能:

  1. Merge chunks on the same shard that can be merged.合并可以合并的同一分片上的块。
  2. Migrate smaller chunks to other shards. A small chunk is one that contains data less than 25% of the chunkSize setting.将较小的块迁移到其他分片。小块是指包含的数据小于chunkSize设置的25%。
  3. Merge remaining chunks on the same shard that can be merged.合并同一分片上可以合并的剩余块。

The procedure in this task uses an example sharded collection named ordersShardedCollection in a database named test.此任务中的过程在名为test的数据库中使用名为ordersShardedCollection的示例分片集合。

You can use your own sharded collection and database in the procedure.您可以在过程中使用自己的分片集合和数据库。

In the procedure for this task, you monitor the phases and see the defragmentation progress.在执行此任务的过程中,您将监视各个阶段并查看分片整理进度。

Before you Begin开始之前

Procedure过程

1

Monitor defragmentation监视器分片整理

Run:运行:

db.adminCommand(
{
balancerCollectionStatus: "test.ordersShardedCollection"
}
)
2

Examine output document检查输出文档

The previous command returns a document with information about defragmentation status, current phase, and the defragmentation work remaining. For example:上一个命令返回一个文档,其中包含有关分片整理状态、当前阶段和剩余分片整理工作的信息。例如:

{
"balancerCompliant": false,
"firstComplianceViolation": "defragmentingChunks",
"details": {
"currentPhase": "moveAndMergeChunks",
"progress": { "remainingChunksToProcess": 1 }
}
}

The following table describes the document fields.下表描述了文档字段。

Field字段Type类型Description描述
balancerCompliantBoolean布尔值false if collection chunks must be moved. Otherwise, true.如果必须移动集合块,则为false。否则,这是真的。
firstComplianceViolationString字符串Indicates the reason that chunks for the namespace must be moved or merged. Only returned if balancerCompliant is false.指示必须移动或合并命名空间块的原因。仅当balancerCompliantfalse时才返回。
detailsObjectAddtional information about the current defragmentation state. Only returned if firstComplianceViolation is defragmentingChunks.有关当前分片整理状态的附加信息。仅当firstComplianceViolationdefragmentingChunks时才返回。
currentPhaseString字符串

Current defragmentation phase:当前分片整理阶段:

  • For phase one, currentPhase is mergeAndMeasureChunks.

    Phase one merges contiguous chunks located on the same shard and calculates the data size for those chunks.第一阶段合并位于同一分片上的连续块,并计算这些块的数据大小。

  • For phase two, currentPhase is moveAndMergeChunks.对于第二阶段,currentPhasemoveAndMergeChunks

    After phase one is complete, there might be some small chunks remaining. Phase two migrates those small chunks to other shards and merges the chunks on those shards.第一阶段完成后,可能还会剩下一些小块。第二阶段将这些小块迁移到其他分片,并合并这些分片上的块。

remainingChunksToProcessInteger整数Number of remaining chunks to process in the current phase.当前阶段中要处理的剩余块数。

For additional information about the returned document fields, see the balancer collection status output document.有关返回的文档字段的更多信息,请参阅平衡器集合状态输出文档

3

Confirm that defragmentation is complete确认分片整理已完成

After defragmentation completes, the command returns either:分片整理完成后,该命令返回以下任一结果:

  • balancerCompliant: true if your collection is balanced.如果集合是平衡的。
  • balancerCompliant: false with firstComplianceViolation set to a string other than defragmentingChunks if your collection is not balanced.如果集合不平衡,则balancerCompliant: false,并且将firstComplianceViolation设置为除defragmentingChunks之外的字符串。

Example output for a balanced collection after defragmentation completes:分片整理完成后平衡集合的示例输出:

{
chunkSize: 0.2,
balancerCompliant: true,
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1677543079, i: 1 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1677543079, i: 1 })
}

Next Steps后续步骤

If defragmentation has not yet completed, you can stop it. For details, see Stop Defragmenting a Sharded Collection.如果分片整理尚未完成,您可以停止它。有关详细信息,请参阅停止对分片集合进行分片整理

Learn More了解更多