Manage Sharded Cluster Balancer管理分片化群集平衡器
On this page本页内容
Check the Balancer State检查平衡器状态Check if Balancer is Running检查平衡器是否正在运行Configure Default Range Size配置默认范围大小Schedule the Balancing Window安排平衡窗口Remove a Balancing Window Schedule删除平衡窗口计划Disable the Balancer禁用平衡器Enable the Balancer启用平衡器Disable Balancing During Backups在备份期间禁用平衡Disable Balancing on a Collection禁用集合上的平衡Enable Balancing on a Collection对集合启用平衡Confirm Balancing is Enabled or Disabled确认已启用或禁用平衡Change Replication Behavior for Chunk Migration更改块迁移的复制行为Change the Maximum Storage Size for a Given Shard更改给定分片的最大存储大小
Changed in version 6.16.1版更改.
This page describes common administrative procedures related to balancing. 本页介绍与平衡相关的常见管理程序。For an introduction to balancing, see Sharded Cluster Balancer. 有关平衡的介绍,请参阅分片集群平衡器。For lower level information on balancing, see Balancer Internals.有关平衡的较低级别信息,请参阅平衡器内部。
The balancer process has moved from the 平衡器进程已从mongos
instances to the primary member of the config server replica set.mongos
实例移动到配置服务器副本集的主要成员。
Check the Balancer State检查平衡器状态
sh.getBalancerState()
checks if the balancer is enabled (i.e. that the balancer is permitted to run). sh.getBalancerState()
does not check if the balancer is actively migrating data.sh.getBalancerState()
检查平衡器是否已启用(即是否允许平衡器运行)。sh.getBalancerState()
不检查平衡器是否正在主动迁移数据。
To see if the balancer is enabled in your sharded cluster, issue the following command, which returns a boolean:要查看您的分片集群中是否启用了平衡器,请发出以下命令,该命令返回一个布尔值:
sh.getBalancerState()
You can also see if the balancer is enabled using 您还可以使用sh.status()
. sh.status()
查看平衡器是否已启用。The currently-enabled
field indicates whether the balancer is enabled, while the currently-running
field indicates if the balancer is currently running.currently-enabled
用字段指示平衡器是否已启用,而currently-running
字段指示平衡器当前是否正在运行。
Check if Balancer is Running检查平衡器是否正在运行
To see if the balancer process is active in your cluster:要查看平衡器进程在集群中是否处于活动状态,请执行以下操作:
Connect to any使用mongos
in the cluster using themongosh
shell.mongosh
shell连接到集群中的任何mongos
。Use the following operation to determine if the balancer is running:使用以下操作来确定平衡器是否正在运行:sh.isBalancerRunning()
Configure Default Range Size配置默认范围大小
The default range size for a sharded cluster is 128 megabytes. 分片集群的默认范围大小为128兆字节。In most situations, the default size is appropriate for splitting and migrating chunks. 在大多数情况下,默认大小适用于分割和迁移块。For information on how range size affects deployments, see details, see Range Size.有关范围大小如何影响部署的信息,请参阅详细信息,请参阅范围大小。
Changing the default range size affects ranges that are processes during migrations and auto-splits but does not retroactively affect all ranges.更改默认范围大小会影响迁移和自动拆分过程中的进程范围,但不会追溯到所有范围。
Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改。
To configure default range size, see Modify Range Size in a Sharded Cluster.要配置默认范围大小,请参阅修改分片群集中的范围大小。
Schedule the Balancing Window安排平衡窗口
In some situations, particularly when your data set grows slowly and a migration can impact performance, it is useful to ensure that the balancer is active only at certain times. 在某些情况下,特别是当数据集增长缓慢且迁移可能影响性能时,确保平衡器仅在特定时间处于活动状态是非常有用的。The following procedure specifies the 以下过程指定activeWindow
, which is the timeframe during which the balancer will be able to migrate chunks:activeWindow
,这是平衡器能够迁移块的时间段:
Switch to the Config Database.切换到配置数据库。
Issue the following command to switch to the config database.发出以下命令以切换到配置数据库。
use config
Ensure that the balancer is not stopped
.确保平衡器没有停止。
stopped
.The balancer will not activate in the 平衡器在stopped
state. stopped
状态下不会激活。To ensure that the balancer is not 要确保平衡器没有停止,请使用stopped
, use sh.startBalancer()
, as in the following:sh.startBalancer()
,如下所示:
sh.startBalancer()
The balancer will not start if you are outside of the 如果您在activeWindow
timeframe.activeWindow
时间范围之外,平衡器将不会启动。
Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改。
In MongoDB versions earlier than 6.1, 在6.1之前的MongoDB版本中,sh.startBalancer()
also enables auto-splitting for the sharded cluster.sh.startBalancer()
还支持对分片集群进行自动拆分。
Modify the balancer's window.修改平衡器的窗口。
Set the 使用activeWindow
using updateOne()
, as in the following:updateOne()
设置activeWindow
,如下所示:
db.settings.updateOne(
{ _id: "balancer" },
{ $set: { activeWindow : { start : "<start-time>", stop : "<stop-time>" } } },
{ upsert: true }
)
Replace 使用指定平衡窗口的开始和结束边界的两位小时和分钟值(即<start-time>
and <end-time>
with time values using two digit hour and minute values (i.e. HH:MM
) that specify the beginning and end boundaries of the balancing window.HH:MM
)将<start-time>
和<end-time>
替换为时间值。
For对于HH
values, use hour values ranging from00
-23
.HH
值,使用00
-23
之间的小时值。For对于MM
value, use minute values ranging from00
-59
.MM
值,请使用00
-59
之间的分钟值。
For on-premises or self-managed sharded clusters, MongoDB evaluates the start and stop times relative to the time zone of the primary member in the config server replica set.对于内部部署或自管理的分片集群,MongoDB评估相对于配置服务器副本集中主要成员的时区的开始和停止时间。
For Atlas clusters, MongoDB evaluates the start and stop times relative to the UTC timezone.对于Atlas集群,MongoDB评估相对于UTC时区的开始和停止时间。
The balancer window must be sufficient to complete the migration of all data inserted during the day.平衡器窗口必须足以完成当天插入的所有数据的迁移。
As data insert rates can change based on activity and usage patterns, it is important to ensure that the balancing window you select will be sufficient to support the needs of your deployment.由于数据插入率可能会根据活动和使用模式而变化,因此确保您选择的平衡窗口足以满足您的部署需求非常重要。
Remove a Balancing Window Schedule删除平衡窗口计划
If you have set the balancing window and wish to remove the schedule so that the balancer is always running, use 如果您已经设置了平衡窗口,并希望删除时间表,以便平衡器始终运行,请使用$unset
to clear the activeWindow
, as in the following:$unset
清除activeWindow
,如下所示:
use config
db.settings.updateOne( { _id : "balancer" }, { $unset : { activeWindow : true } } )
Disable the Balancer禁用平衡器
By default, the balancer may run at any time and only moves chunks as needed. 默认情况下,平衡器可以在任何时候运行,并且只根据需要移动块。To disable the balancer for a short period of time and prevent all migration, use the following procedure:要在短时间内禁用平衡器并阻止所有迁移,请使用以下过程:
Connect to any使用mongos
in the cluster using themongosh
shell.mongosh
shell连接到集群中的任何mongos
。Issue the following operation to disable the balancer:发出以下操作以禁用平衡器:sh.stopBalancer()
If a migration is in progress, the system will complete the in-progress migration before stopping.如果正在进行迁移,系统将在停止之前完成正在进行的迁移。Starting in MongoDB 6.1, automatic chunk splitting is not performed. This is because of balancing policy improvements.从MongoDB 6.1开始,不执行自动区块分割。这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation.自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改。In MongoDB versions earlier than 6.1,在6.1之前的MongoDB版本中,sh.stopBalancer()
also disables auto-splitting for the sharded cluster.sh.stopBalancer()
还禁用了分片集群的自动拆分。To verify that the balancer will not start, issue the following command, which returns要验证平衡器是否不会启动,请发出以下命令,如果平衡器被禁用,则返回false
if the balancer is disabled:false
:sh.getBalancerState()
Optionally, to verify no migrations are in progress after disabling, issue the following operation in the(可选)要验证禁用后没有迁移正在进行,请在mongosh
shell:mongosh
shell中发出以下操作:use config
while( sh.isBalancerRunning() ) {
print("waiting...");
sleep(1000);
}
To disable the balancer from a driver, use the balancerStop command against the 要从驱动程序禁用平衡器,请对admin
database, as in the following:admin
数据库使用balancerStop
命令,如下所示:
db.adminCommand( { balancerStop: 1 } )
Enable the Balancer启用平衡器
Use this procedure if you have disabled the balancer and are ready to re-enable it:如果已禁用平衡器并准备重新启用,请使用以下步骤:
Connect to any使用mongos
in the cluster using themongosh
shell.mongosh
shell连接到集群中的任何mongos
。Issue one of the following operations to enable the balancer:发出以下操作之一以启用平衡器:From the从mongosh
shell, issue:mongosh
shell发布:sh.startBalancer()
NoteTo enable the balancer from a driver, use the balancerStart command against the要从驱动程序启用平衡器,请对admin
database, as in the following:admin
数据库使用balancerStart
命令,如下所示:db.adminCommand( { balancerStart: 1 } )
Starting in MongoDB 6.1, automatic chunk splitting is not performed.从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements.这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation.自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改。In MongoDB versions earlier than 6.1,在6.1之前的MongoDB版本中,sh.startBalancer()
also enables auto-splitting for the sharded cluster.sh.startBalancer()
还支持对分片集群进行自动拆分。
Disable Balancing During Backups在备份期间禁用平衡
Disabling the balancer is only necessary when manually taking backups, either by calling 只有在手动进行备份时才需要禁用平衡器,无论是通过调用mongodump
or scheduling a task that calls mongodump
at a specific time.mongodump
还是安排在特定时间调用mongoump
的任务。
You do not have to disable the balancer when using coordinated backup and restore processes:在使用协调的备份和恢复过程时,您不必禁用平衡器:
If MongoDB migrates a chunk during a backup, you can end with an inconsistent snapshot of your sharded cluster. 如果MongoDB在备份过程中迁移了一个区块,那么您可能会得到一个不一致的分片集群快照。Never run a backup while the balancer is active. 平衡器处于活动状态时,切勿运行备份。To ensure that the balancer is inactive during your backup operation:要确保平衡器在备份操作期间处于非活动状态,请执行以下操作:
Set the balancing window so that the balancer is inactive during the backup. Ensure that the backup can complete while you have the balancer disabled.设置平衡窗口,使平衡器在备份期间处于非活动状态。请确保在禁用平衡器的情况下可以完成备份。manually disable the balancer for the duration of the backup procedure.在备份过程期间手动禁用平衡器。
If you turn the balancer off while it is in the middle of a balancing round, the shut down is not instantaneous. 如果在平衡循环进行到一半时关闭平衡器,则关闭不是即时的。The balancer completes the chunk move in-progress and then ceases all further balancing rounds.平衡器完成正在进行的区块移动,然后停止所有进一步的平衡回合。
Before starting a backup operation, confirm that the balancer is not active. 在启动备份操作之前,请确认平衡器未处于活动状态。You can use the following command to determine if the balancer is active:您可以使用以下命令来确定平衡器是否处于活动状态:
!sh.getBalancerState() && !sh.isBalancerRunning()
When the backup procedure is complete you can reactivate the balancer process.备份过程完成后,您可以重新激活平衡器进程。
Disable Balancing on a Collection禁用集合上的平衡
You can disable balancing for a specific collection with the 您可以使用sh.disableBalancing()
method. sh.disableBalancing()
方法禁用特定集合的平衡。You may want to disable the balancer for a specific collection to support maintenance operations or atypical workloads, for example, during data ingestions or data exports.您可能希望禁用特定集合的平衡器,以支持维护操作或非典型工作负载,例如,在数据摄取或数据导出期间。
When you disable balancing on a collection, MongoDB will not interrupt in progress migrations.当您在集合上禁用平衡时,MongoDB不会中断正在进行的迁移。
To disable balancing on a collection, connect to a 要禁用集合上的平衡,请使用mongos
with the mongosh
shell and call the sh.disableBalancing()
method.mongosh
shell连接到mongos
并调用sh.disableBalancing()
方法。
For example:例如:
sh.disableBalancing("students.grades")
The sh.disableBalancing()
method accepts as its parameter the full namespace of the collection.sh.disableBalancing()
方法接受集合的完整命名空间作为其参数。
Enable Balancing on a Collection对集合启用平衡
You can enable balancing for a specific collection with the 您可以使用sh.enableBalancing()
method.sh.enableBalancing()
方法为特定集合启用平衡。
When you enable balancing for a collection, MongoDB will not immediately begin balancing data. 当您为集合启用平衡时,MongoDB不会立即开始平衡数据。However, if the data in your sharded collection is not balanced, MongoDB will be able to begin distributing the data more evenly.然而,如果您的分片集合中的数据不平衡,MongoDB将能够开始更均匀地分配数据。
To enable balancing on a collection, connect to a 要在集合上启用平衡,请使用mongos
with the mongosh
shell and call the sh.enableBalancing()
method.mongosh
shell连接到mongos
并调用sh.enableBalancing()
方法。
For example:例如:
sh.enableBalancing("students.grades")
The sh.enableBalancing()
method accepts as its parameter the full namespace of the collection.sh.enableBalancing()
方法接受集合的完整命名空间作为其参数。
Confirm Balancing is Enabled or Disabled确认已启用或禁用平衡
To confirm whether balancing for a collection is enabled or disabled, query the 要确认集合的平衡是启用还是禁用,请在collections
collection in the config
database for the collection namespace and check the noBalance
field. config
数据库中查询集合命名空间的collections
集合,并检查noBalance
字段。For example:例如:
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;
This operation will return a null error, 此操作将返回true
, false
, or no output:null
错误、true
、false
或无输出:
A null error indicates the collection namespace is incorrect.null
错误表示集合命名空间不正确。If the result is如果结果为true
, balancing is disabled.true
,则禁用平衡。If the result is如果结果为false
, balancing is enabled currently but has been disabled in the past for the collection.false
,则当前已启用平衡,但过去已禁用集合的平衡。Balancing of this collection will begin the next time the balancer runs.此集合的平衡将在平衡器下次运行时开始。If the operation returns no output, balancing is enabled currently and has never been disabled in the past for this collection.如果操作未返回任何输出,则当前已启用平衡,并且以前从未对此集合禁用过平衡。Balancing of this collection will begin the next time the balancer runs.此集合的平衡将在平衡器下次运行时开始。
You can also see if the balancer is enabled using 您还可以使用sh.status()
. sh.status()
查看平衡器是否已启用。The currently-enabled
field indicates if the balancer is enabled.currently-enabled
字段指示平衡器是否已启用。
Change Replication Behavior for Chunk Migration更改块迁移的复制行为
Secondary Throttle辅助节流阀
During chunk migration, the 在块迁移过程中,_secondaryThrottle
value determines when the migration proceeds with next document in the chunk._secondaryThrottle
值确定迁移何时进行块中的下一个文档。
In the 在config.settings
collection:config.settings
集合中:
If the如果平衡器的_secondaryThrottle
setting for the balancer is set to a write concern, each document move during chunk migration must receive the requested acknowledgement before proceeding with the next document._secondaryThrottle
设置设置为写入问题,则块迁移期间的每个文档移动都必须在继续下一个文档之前收到请求的确认。If the如果平衡器的_secondaryThrottle
setting for the balancer is set totrue
, each document move during chunk migration must receive acknowledgement from at least one secondary before the migration proceeds with the next document in the chunk._secondaryThrottle
设置设置为true
,则块迁移期间的每个文档移动都必须至少从一个辅助文档接收到确认,然后才能继续迁移块中的下一个文档。This is equivalent to a write concern of这相当于{ w: 2 }
.{ w: 2 }
的写入问题。If the如果未设置_secondaryThrottle
setting is unset, the migration process does not wait for replication to a secondary and instead continues with the next document._secondaryThrottle
设置,则迁移过程不会等待复制到辅助文档,而是继续执行下一个文档。This is the default behavior for WiredTiger.这是WiredTiger的默认行为。
To change the 要更改_secondaryThrottle
setting, connect to a mongos
instance and directly update the _secondaryThrottle
value in the settings
collection of the config database. _secondaryThrottle
设置,请连接到mongos
实例,然后直接更新config
数据库的settings
集合中的_secondaryThrottle
值。For example, from a 例如,从连接到mongosh
shell connected to a mongos
, issue the following command:mongos
的mongosh
shell中,发出以下命令:
use config
db.settings.updateOne(
{ "_id" : "balancer" },
{ $set : { "_secondaryThrottle" : { "w": "majority" } } },
{ upsert : true }
)
The effects of changing the 更改_secondaryThrottle
setting may not be immediate. _secondaryThrottle
设置可能不会立即产生影响。To ensure an immediate effect, stop and restart the balancer to enable the selected value of 要确保立即生效,请停止并重新启动平衡器,以启用选定的_secondaryThrottle
._secondaryThrottle
值。
For more information on the replication behavior during various steps of chunk migration, see Range Migration and Replication.有关区块迁移各个步骤期间的复制行为的更多信息,请参阅范围迁移和复制。
Use the使用moveRange
command'ssecondaryThrottle
andwriteConcern
options to specify the behavior during the command.moveRange
命令的secondaryThrottle
和writeConcern
选项可以指定命令执行期间的行为。Use the使用moveChunk
command's_secondaryThrottle
andwriteConcern
options to specify the behavior during the command.moveChunk
命令的_secondaryThrottle
和writeConcern
选项可以指定命令执行期间的行为。
For details, see 有关详细信息,请参阅moveRange
and moveChunk
.moveRange
和moveChunk
。
Wait for Delete等待删除
The 平衡器的_waitForDelete
setting of the balancer and the moveChunk
command affects how the balancer migrates multiple chunks from a shard. _waitForDelete
设置和moveChunk
命令会影响平衡器如何从分片迁移多个块。Similarly, the 类似地,平衡器的_waitForDelete
setting of the balancer and the moveRange
command also affect how the balancer migrates multiple chunks from a shard. _waitForDelete
设置和moveRange
命令也会影响平衡器如何从分片迁移多个块。By default, the balancer does not wait for the on-going migration's delete phase to complete before starting the next chunk migration. 默认情况下,平衡器不等待正在进行的迁移的删除阶段完成,然后再开始下一个块迁移。To have the delete phase block the start of the next chunk migration, you can set the 要使删除阶段阻止下一个块迁移的开始,可以将_waitForDelete
to true._waitForDelete
设置为true
。
For details on chunk migration, see Range Migration. 有关区块迁移的详细信息,请参阅范围迁移。For details on the chunk migration queuing behavior, see Asynchronous Range Migration Cleanup.有关区块迁移排队行为的详细信息,请参阅异步范围迁移清理。
The _waitForDelete
is generally for internal testing purposes. _waitForDelete
通常用于内部测试。To change the balancer's 要更改平衡器的_waitForDelete
value:_waitForDelete
值,请执行以下操作:
Connect to a连接到mongos
instance.mongos
实例。Update the更新_waitForDelete
value in thesettings
collection of the config database.config
数据库的settings
集合中的_waitForDelete
值。For example:例如:use config
db.settings.updateOne(
{ "_id" : "balancer" },
{ $set : { "_waitForDelete" : true } },
{ upsert : true }
)
Once set to 一旦设置为true
, to revert to the default behavior:true
,要恢复到默认行为:
Connect to a连接到mongos
instance.mongos
实例。Update or unset the更新或取消设置_waitForDelete
field in thesettings
collection of the config database:config
数据库的settings
集合中的_waitForDelete
字段:use config
db.settings.updateOne(
{ "_id" : "balancer", "_waitForDelete": true },
{ $unset : { "_waitForDelete" : "" } }
)
Balance Ranges that Exceed Size Limit超出大小限制的平衡范围
By default, MongoDB cannot move a range if the number of documents in the range is greater than 2 times the result of dividing the configured range size by the average document size.默认情况下,如果范围中的文档数量大于配置的范围大小除以平均文档大小的结果的2倍,MongoDB将无法移动该范围。
Starting in MongoDB 4.4, by specifying the balancer setting 从MongoDB 4.4开始,通过指定平衡器设置attemptToBalanceJumboChunks
to true
, the balancer can migrate these large ranges as long as they have not been labeled as jumbo.attemptToBalanceJumboChunks
为true
,平衡器可以迁移这些大范围,只要它们没有被标记为jumbo。
To set the balancer's 要设置平衡器的attemptToBalanceJumboChunks
setting, connect to a mongos
instance and directly update the config.settings
collection. attemptToBalanceJumboChunks
设置,请连接到mongos
实例并直接更新config.settings
集合。For example, from a 例如,从连接到mongosh
shell connected to a mongos
instance, issue the following command:mongos
实例的mongosh
shell中,发出以下命令:
db.getSiblingDB("config").settings.updateOne(
{ _id: "balancer" },
{ $set: { attemptToBalanceJumboChunks : true } },
{ upsert: true }
)
When the balancer attempts to move the range, if the queue of writes that modify any documents being migrated surpasses 500MB of memory the migration will fail. 当平衡器尝试移动范围时,如果修改要迁移的任何文档的写入队列超过500MB内存,则迁移将失败。For details on the migration procedure, see Range Migration Procedure.有关迁移过程的详细信息,请参阅范围迁移过程。
If the range you want to move is labeled 如果要移动的范围标记为jumbo
, you can manually clear the jumbo flag to have the balancer attempt to migrate the range.jumbo
,则可以手动清除jumbo
标志,让平衡器尝试迁移该范围。
You can also manually migrate ranges that exceed the size limit (with or without the 您还可以手动迁移超过大小限制的范围(使用或不使用jumbo
label) using either:jumbo
标签),方法如下:
the带有moveRange
command with the forceJumbo: true optionforceJumbo: true
选项的moveRange
命令the带有moveChunk
command with the forceJumbo: true optionforceJumbo: true
选项的moveChunk
命令
However, when you run 但是,当您使用moveRange
or moveChunk
with forceJumbo: true
, write operations to the collection may block for a long period of time during the migration.forceJumbo: true
运行moveRange
或moveChunk
时,对集合的写入操作可能会在迁移期间阻塞很长一段时间。
Change the Maximum Storage Size for a Given Shard更改给定分片的最大存储大小
Changing a shard's maximum storage size is deprecated. 不赞成更改分片的最大存储大小。Starting in version 4.2, MongoDB uses the WiredTiger Storage Engine to manage how data is stored. WiredTiger compresses data to minimize storage use. 从4.2版本开始,MongoDB使用WiredTiger存储引擎来管理数据的存储方式。WiredTiger压缩数据以最大限度地减少存储使用。To learn more, see Compression.要了解更多信息,请参阅压缩。
For MongoDB versions 6.1 and earlier, you can still set a maximum storage size for a given shard in the sharded cluster by using the 对于MongoDB 6.1及更早版本,您仍然可以使用maxSize
field. maxSize
字段为分片集群中的给定分片设置最大存储大小。You can find configuration data related to shards in the 您可以在config.shards
collection.config.shards
集合中找到与分片相关的配置数据。
Starting in version 6.2, MongoDB removes the 从6.2版本开始,MongoDB从maxSize
field from the addShard
command. As a result:addShard
命令中删除了maxSize
字段。因此: