Docs HomeMongoDB Manual

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描述
balancerCompliantBooleanfalse if collection chunks must be moved. Otherwise, true.如果必须移动集合块,则为false。否则,是true
firstComplianceViolationStringIndicates 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时返回。
currentPhaseStringCurrent defragmentation phase: 当前分片整理阶段:
  • For phase one, currentPhase is mergeAndMeasureChunks.对于第一阶段,currentPhasemergeAndMeasureChucks
    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.第二阶段将这些小块迁移到其他分片,并合并这些分片上的块。
remainingChunksToProcessIntegerNumber 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: true
  • 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了解更多信息