Database Manual / Reference / Database Commands / Sharding

balancerCollectionStatus (database command数据库命令)

Definition定义

balancerCollectionStatus

Returns a document that contains information about whether the chunks of a sharded collection are balanced (i.e. do not need to be moved) as of the time the command is run or need to be moved because of draining shards, zone violation or imbalance of chunks across shards.返回一个文档,其中包含有关分片集合的块在运行命令时是否平衡(即不需要移动)的信息,或者由于分片耗尽、区域冲突或分片间块不平衡而需要移动的信息。

You can only issue the balancerCollectionStatus against the admin database.您只能对admin数据库发出balancerCollectionStatus

Tip

In mongosh, this command can also be run through the sh.balancerCollectionStatus() helper method.mongosh中,此命令也可以通过sh.balancerCollectionStatus()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. 助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要便利性或需要额外的返回字段,请使用database命令。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
balancerCollectionStatus: "<db>.<collection>"
}
)

Specify the full namespace ("<db>.<collection>") of the sharded collection.指定分片集合的完整命名空间("<db>.<collection>")。

mongosh provides a wrapper method sh.balancerCollectionStatus().提供了一个包装器方法sh.balancerCollectionStatus()

Access Control访问控制

When running with access control, the user must have the enableSharding privilege actions on database and/or collection to run the command. 当使用访问控制运行时,用户必须对数据库和/或集合具有enableSharding(启用分片)权限操作才能运行该命令。That is, a user must have a role that grants the following privilege:也就是说,用户必须具有授予以下权限角色

{ resource: { db: <database>, collection: <collection> }, actions: [ "enableSharding" ] }

The built-in clusterManager role provides the appropriate privileges.内置的clusterManager角色提供适当的权限。

Output Document输出文档

The following is an example of a document returned by the command:以下是命令返回的文档示例:

{
"chunkSize": Long("128"),
"balancerCompliant" : false,
"firstComplianceViolation" : "chunksImbalance",
"ok" : 1,
"operationTime" : Timestamp(1583192967, 16),
"$clusterTime" : {
"clusterTime" : Timestamp(1583192967, 16),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : Long(0)
}
}
}
Field字段Description描述
"chunkSize"

New in version 5.3.在版本5.3中新增。

An integer that indicates the chunk size in megabytes.一个整数,表示块大小(以兆字节为单位)。

"balancerCompliant"A boolean that indicates whether the chunks do not need to be moved (true) or need to be moved (false).一个布尔值,指示块是不需要移动(true)还是需要移动(false)。
"firstComplianceViolation"

A string that indicates the reason chunks for this namespace need to be moved. The field is only available if "balancerCompliant" is false.一个字符串,指示需要移动此命名空间的块的原因。仅当"balancerCompliant"false时,该字段才可用。

Possible values are:

"chunksImbalance"
The difference in the number of chunks between the shard with the most chunks for the collection and the shard with the fewest chunks for the collection exceed the migration threshold.集合中块最多的分片和集合中块最少的分片之间的块数差异超过了迁移阈值
"defragmentingChunks"
The queried namespace is currently going through the chunk defragmentation process. Defragmentation can be triggered by the configureCollectionBalancing command.查询的命名空间当前正在进行块分片整理过程。分片整理可以通过configureCollectionBalancing命令触发。
"draining"
A remove shard operation is in progress and MongoDB must drain chunks off the removed shard to other shards.正在进行删除分片操作,MongoDB必须将删除的分片中的块排放到其他分片中。
"zoneViolation"
Chunks violate the defined zone ranges for a shard.块违反了为分片定义的区域范围

This field only returns information on the first violation observed by MongoDB. There may be additional pending chunk migrations due to a different reason than the one reported in firstComplianceViolation.此字段仅返回MongoDB观察到的第一次违规的信息。由于与firstComplianceViolation中报告的原因不同的原因,可能还有其他未决的块迁移。

"details"

An object containing information on the ongoing defragmentation process. This object indicates the current phase of the defragmentation and how many chunks are left to process in that phase. 包含正在进行的分片整理过程信息的对象。此对象指示分片整理的当前阶段以及在该阶段中还有多少块要处理。For example output, see Ongoing Defragmentation Process.例如输出,请参阅正在进行的分片整理过程

This field is only returned when firstComplianceViolation is defragmentingChunks.仅当firstComplianceViolation对区块进行分片整理(defragmentingChunks)时,才会返回此字段。

In addition to the command-specific return fields, the command also returns the ok status field, the operationTime field, and the $clusterTime field for the operation. 除了特定于命令的返回字段外,该命令还返回操作的ok状态字段、operationTime字段和$clusterTime字段。For details on these fields, see Response.有关这些字段的详细信息,请参阅响应

Examples示例

To check whether the chunks of a sharded collection test.contacts is currently in balance, connect to a mongos instance and issue the following command:要检查分片集合test.contacts的块当前是否平衡,请连接到mongos实例并发出以下命令:

db.adminCommand( { balancerCollectionStatus: "test.contacts" } )

If the chunks for the collection do not need to be moved, the command returns an output similar to the following:如果不需要移动集合的块,则该命令返回类似于以下内容的输出:

{
"chunkSize": Long("128"),
"balancerCompliant" : true,
"ok" : 1,
"operationTime" : Timestamp(1583193238, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1583193238, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : Long(0)
}
}
}

Ongoing Defragmentation Process正在进行的分片整理过程

If the queried namespace is going through chunk defragmentation, the balancerCollectionStatus command returns output similar to the following:如果查询的命名空间正在进行块分片整理,balancerCollectionStatus命令将返回类似于以下内容的输出:

{
"chunkSize": Long("128"),
"balancerCompliant": false,
"firstComplianceViolation": "defragmentingChunks",
"details": {
"currentPhase": "moveAndMergeChunks",
"progress": { "remainingChunksToProcess": 1 }
}
}

Note

Chunk defragmentation occurs in multiple phases. The progress field only pertains to the current phase.块分片整理分多个阶段进行。progress字段仅适用于当前阶段。

To learn more about:了解更多信息: