The MongoDB balancer is a background process that monitors the amount of data on each shard for each sharded collection. MongoDB平衡器是一个后台进程,用于监控每个分片集合的每个分片上的数据量。When the amount of data for a sharded collection on a given shard reaches specific migration thresholds, the balancer attempts to automatically migrate data between shards and reach an even amount of data per shard while respecting the zones. 当给定分片上的分片集合的数据量达到特定的迁移阈值时,平衡器会尝试在分片之间自动迁移数据,并在尊重区域的同时达到每个分片的平均数据量。By default, the balancer process is always enabled.默认情况下,平衡器进程始终处于启用状态。
The balancing procedure for sharded clusters is entirely transparent to the user and application layer, though there may be some performance impact while the procedure takes place.分片集群的平衡过程对用户和应用层完全透明,尽管在过程中可能会对性能产生一些影响。
The balancer runs on the primary of the config server replica set (CSRS).平衡器在配置服务器副本集(CSRS)的主服务器上运行。
To configure collection balancing for a single collection, see 要为单个集合配置集合平衡,请参阅configureCollectionBalancing.configureCollectionBalancing。
To manage the sharded cluster balancer, see Manage Sharded Cluster Balancer.要管理分片集群平衡器,请参阅管理分片群集平衡器。
Balancer Internals平衡器内部
Range migrations carry some overhead in terms of bandwidth and workload, both of which can impact database performance. 范围迁移在带宽和工作负载方面会带来一些开销,这两者都会影响数据库性能。The balancer attempts to minimize the impact by:
Restricting a shard to at most one migration at any given time. Specifically, a shard cannot participate in multiple data migrations at the same time. The balancer migrates ranges one at a time.在任何给定时间将一个分片限制为最多一次迁移。具体来说,一个分片不能同时参与多个数据迁移。平衡器一次迁移一个范围。MongoDB can perform parallel data migrations, but a shard can participate in at most one migration at a time. For a sharded cluster with n shards, MongoDB can perform at most n/2 (rounded down) simultaneous migrations.MongoDB可以执行并行数据迁移,但一个分片一次最多只能参与一次迁移。对于具有n个分片的分片集群,MongoDB最多可以执行n/2(四舍五入)个同时迁移。See also Asynchronous Range Migration Cleanup.另请参见异步范围迁移清理。Starting a balancing round only when the difference in the amount of data between the shard with the most data for a sharded collection and the shard with the least data for that collection reaches the migration threshold.仅当分片集合中数据最多的分片与该集合中数据最少的分片之间的数据量差达到迁移阈值时,才开始一轮平衡。
You can disable the balancer temporarily for maintenance, but leaving the balancer disabled for extended periods of time can degrade cluster performance. For more information, see Disable the Balancer.您可以暂时禁用平衡器进行维护,但长时间禁用平衡器可能会降低集群性能。有关更多信息,请参阅禁用平衡器。
You can also limit the window during which the balancer runs to prevent it from impacting production traffic. See Schedule the Balancing Window for details.您还可以限制平衡器运行的窗口,以防止其影响生产流量。有关详细信息,请参阅计划平衡窗口。
Note
The specification of the balancing window is relative to the local time zone of the primary of the config server replica set.平衡窗口的规格与配置服务器副本集的主服务器的本地时区有关。
Adding and Removing Shards from the Cluster在群集中添加和删除分片
Adding a shard to a cluster creates an imbalance, since the new shard has no data. While MongoDB begins migrating data to the new shard immediately, it can take some time before the cluster balances. 将分片添加到集群会造成不平衡,因为新的分片没有数据。虽然MongoDB立即开始将数据迁移到新的分片,但集群平衡可能需要一些时间。See the Add Shards to a Cluster tutorial for instructions on adding a shard to a cluster.有关将分片添加到集群的说明,请参阅向集群添加分片教程。
Tip
Starting in MongoDB 8.0, you can reshard to the same key to move data. 从MongoDB 8.0开始,您可以重新标记到相同的键来移动数据。If your application meets the resharding requirements, you can use the 如果应用程序满足重新分片要求,则可以使用reshardCollection command to redistribute data across the cluster to include the new shards. reshardCollection命令在整个集群中重新分发数据,以包含新的分片。For more information, see Reshard to the Same Shard Key. 有关更多信息,请参阅重新硬化相同的分片键。This process is much faster than the alternative Range Migration Procedure.此过程比替代的范围迁移过程快得多。
Removing a shard from a cluster creates a similar imbalance, since data residing on that shard must be redistributed throughout the cluster. 从集群中删除分片会造成类似的不平衡,因为驻留在该分片上的数据必须在整个集群中重新分布。While MongoDB begins draining a removed shard immediately, it can take some time before the cluster balances. 虽然MongoDB会立即开始耗尽已删除的分片,但集群平衡可能需要一些时间。Do not shutdown the servers associated to the removed shard during this process.在此过程中,不要关闭与已删除分片关联的服务器。
When you remove a shard in a cluster with an uneven chunk distribution, the balancer first removes the chunks from the draining shard and then balances the remaining uneven chunk distribution.当你在一个块分布不均匀的集群中删除一个分片时,平衡器首先从耗尽的分片中删除块,然后平衡剩余的不均匀块分布。
See the Remove Shards from a Cluster tutorial for instructions on safely removing a shard from a cluster.有关从集群中安全删除分片的说明,请参阅从集群中删除分片教程。
Range Migration Procedure范围迁移程序
All range migrations use the following procedure:所有范围迁移都使用以下过程:
The balancer process sends the平衡器进程将moveRangecommand to the source shard.moveRange命令发送到源分片。The source starts the move when it receives an internal源在收到内部moveRangecommand. During the migration process, operations to the range are sent to the source shard. The source shard is responsible for incoming write operations for the range.moveRange命令时开始移动。在迁移过程中,对范围的操作被发送到源分片。源分片负责该范围的传入写入操作。The destination shard builds any indexes required by the source that do not exist on the destination.目标分片构建源所需的、目标上不存在的任何索引。The destination shard begins requesting documents in the range and starts receiving copies of the data. See also Range Migration and Replication.目标分片开始请求该范围内的文档,并开始接收数据的副本。另请参见范围迁移和复制。After receiving the final document in the range, the destination shard starts a synchronization process to ensure that it has the changes to the migrated documents that occurred during the migration.在收到范围内的最终文档后,目标分片会启动同步过程,以确保它对迁移过程中发生的迁移文档进行了更改。When fully synchronized, the source shard connects to the config database and updates the cluster metadata with the new location for the range.完全同步后,源分片连接到配置数据库,并使用范围的新位置更新集群元数据。After the source shard completes the update of the metadata, and once there are no open cursors on the range, the source shard deletes its copy of the documents.在源分片完成元数据的更新后,一旦范围上没有打开的游标,源分片就会删除其文档副本。Note
If the balancer needs to perform additional chunk migrations from the source shard, the balancer can start the next chunk migration without waiting for the current migration process to finish this deletion step. See Asynchronous Range Migration Cleanup.如果平衡器需要从源分片执行额外的块迁移,则平衡器可以开始下一个块迁移,而无需等待当前迁移过程完成此删除步骤。请参阅异步范围迁移清理。
Warning
Starting in MongoDB version 8.2, long-running secondary reads in a sharded cluster may automatically terminate before orphaned document deletion following a chunk migration.从MongoDB 8.2版本开始,分片集群中长时间运行的辅助读取可能会在块迁移后删除孤立文档之前自动终止。
The terminateSecondaryReadsOnOrphanCleanup parameter controls this behavior. To learn more about handling long-running secondary reads, see Long-Running Secondary Reads in Sharded Clusters.terminateSecondaryReadsOnOrphanCleanup参数控制此行为。要了解有关处理长时间运行的辅助读取的更多信息,请参阅分片集群中的长时间运行辅助读取。
Migration Thresholds迁移阈值
To minimize the impact of balancing on the cluster, the balancer only begins balancing after the distribution of data for a sharded collection has reached certain thresholds.为了尽量减少平衡对集群的影响,平衡器只有在分片集合的数据分布达到一定阈值后才开始平衡。
A collection is considered balanced if the difference in data between shards (for that collection) is less than three times the configured range size for the collection. For the default range size of 如果(该集合的)分片之间的数据差异小于集合配置范围大小的三倍,则认为集合是平衡的。对于128MB, two shards must have a data size difference for a given collection of at least 384MB for a migration to occur.128MB的默认范围大小,对于给定的集合,两个分片必须具有至少384MB的数据大小差才能进行迁移。
Asynchronous Range Migration Cleanup异步范围迁移清理
To migrate data from a shard, the balancer migrates the data one range at a time. However, the balancer does not wait for the current migration's delete phase to complete before starting the next range migration. 为了从分片迁移数据,平衡器一次迁移一个范围的数据。但是,平衡器在开始下一次范围迁移之前不会等待当前迁移的删除阶段完成。See Range Migration for the range migration process and the delete phase.有关范围迁移过程和删除阶段,请参阅范围迁移。
This queuing behavior allows shards to unload data more quickly in cases of heavily imbalanced cluster, such as when performing initial data loads without pre-splitting and when adding new shards.这种排队行为允许分片在集群严重不平衡的情况下更快地卸载数据,例如在不进行预拆分的情况下执行初始数据加载以及添加新分片时。
This behavior also affects the 此行为也会影响moveRange command, and migration scripts that use the moveRange command may proceed more quickly.moveRange命令,使用moveRange命令的迁移脚本可能会进行得更快。
In some cases, the delete phases may persist longer. Range migrations are enhanced to be more resilient in the event of a failover during the delete phase. Orphaned documents are cleaned up even if a replica set's primary crashes or restarts during this phase.在某些情况下,删除阶段可能会持续更长时间。范围迁移得到了增强,以便在删除阶段发生故障转移时更具弹性。即使副本集的主文档在此阶段崩溃或重新启动,孤立文档也会被清理。
Important
When the 当设置_waitForDelete field is set, MongoDB does not wait on the orphanCleanupDelaySecs delay before performing the range deletion. _waitForDelete字段时,MongoDB在执行范围删除之前不会等待orphanCleanupDelaySecs延迟。If you use the 如果使用_waitForDelete parameter and have any read operations occurring on secondaries, the read might terminate due to the migration's delete phase. _waitForDelete参数并在次服务器上进行任何读取操作,则读取可能会因迁移的删除阶段而终止。To learn more, see 要了解更多信息,请参阅terminateSecondaryReadsOnOrphanCleanup.terminateSecondaryReadsOnOrphanCleanup。
For more information, see Wait for Delete.有关详细信息,请参阅等待删除。
Note
Range deletion is a resource intensive operation that can result in significant cache and I/O stress as the cluster deletes the documents.范围删除是一项资源密集型操作,当集群删除文档时,可能会导致严重的缓存和I/O压力。
In cases where you plan to move a large amount of data, such as when adding shards to a cluster or during the initial distribution of a sharded collection across multiple shards, consider resharding the collection instead. Resharding operations don't require range cleanup, which makes them much less stressful on the cluster.如果您计划移动大量数据,例如在向集群添加分片时,或者在分片集合跨多个分片的初始分发期间,请考虑对集合进行重新分片。重新硬装操作不需要范围清理,这使得它们对集群的压力要小得多。
For more information, see Reshard a Collection.有关更多信息,请参阅重新分片集合。
Range Migration and Replication范围迁移和复制
During range migration, the 在范围迁移过程中,_secondaryThrottle value determines when the migration proceeds with next document in the range._seconditionThrottle值决定迁移何时继续进行范围内的下一个文档。
In the 在config.settings collection:config.settings集合中:
If the如果平衡器的_secondaryThrottlesetting for the balancer is set to a write concern, each document moved during range migration must receive the requested acknowledgment before proceeding with the next document._seconditionThrottle设置设置为写入关注,则在范围迁移期间移动的每个文档在继续处理下一个文档之前都必须收到请求的确认。If the如果未设置_secondaryThrottlesetting is unset, the migration process does not wait for replication to a secondary and instead continues with the next document._seconditionThrottle设置,迁移过程不会等待复制到辅助文档,而是继续处理下一个文档。
To update the 要更新平衡器的_secondaryThrottle parameter for the balancer, see Secondary Throttle for an example._secondaryThrottle参数,请参阅Secondary Throttle以获取示例。
Independent of any 独立于任何_secondaryThrottle setting, certain phases of the range migration have the following replication policy:_seconditionThrottle设置,范围迁移的某些阶段具有以下复制策略:
MongoDB briefly pauses all application reads and writes to the collection being migrated to on the source shard before updating the config servers with the range location. MongoDB resumes application reads and writes after the update.MongoDB在用范围位置更新配置服务器之前,会短暂暂停对源分片上迁移到的集合的所有应用程序读写。MongoDB在更新后恢复应用程序读写。The range move requires all writes to be acknowledged by majority of the members of the replica set both before and after committing the range move to config servers.范围移动要求在将范围移动提交到配置服务器之前和之后,所有写入都要得到副本集大多数成员的确认。When an outgoing migration finishes and cleanup occurs, all writes must be replicated to a majority of servers before further cleanup (from other outgoing migrations) or new incoming migrations can proceed.当传出迁移完成并进行清理时,在进一步清理(来自其他传出迁移)或新的传入迁移之前,必须将所有写入复制到大多数服务器。
To update the 要更新_secondaryThrottle setting in the config.settings collection, see Secondary Throttle for an example.config.settings集合中的_seconditionThrottle设置,请参阅Secondary Throttle以获取示例。
Maximum Number of Documents Per Range to Migrate每个区域要迁移的最大文档数
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无法移动范围。If MongoDB can move a sub-range of a chunk and reduce the size to less than that, the balancer does so by migrating a range. 如果MongoDB可以移动块的子范围并将大小减小到小于该范围,则平衡器会通过迁移范围来实现。db.collection.stats() includes the avgObjSize field, which represents the average document size in the collection.db.collection.stats()包含avgObjSize字段,该字段表示集合中的平均文档大小。
For chunks that are too large to migrate:对于太大而无法迁移的块:
The balancer setting平衡器设置attemptToBalanceJumboChunksallows the balancer to migrate chunks too large to move as long as the chunks are not labeled jumbo.attemptToBalanceJumboChunks允许平衡器迁移太大而无法移动的块,只要这些块没有标记为jumbo。See Balance Ranges that Exceed Size Limit for details.有关详细信息,请参阅超过大小限制的平衡范围。When issuing在发出moveRangeandmoveChunkcommands, it's possible to specify the forceJumbo option to allow for the migration of ranges that are too large to move.moveRange和moveChunk命令时,可以指定forceJumbo选项,以允许迁移太大而无法移动的范围。The ranges may or may not be labeled jumbo.这些范围可能被标记为巨型,也可能没有。
Range Deletion Performance Tuning范围删除性能调整
You can tune the performance impact of range deletions with 您可以使用rangeDeleterBatchSize and rangeDeleterBatchDelayMS.rangeDeleterBatchSize和rangeDeleterBatchDelayMS调整范围删除对性能的影响。
For example:例如:
To limit the number of documents deleted per batch, you can set要限制每批删除的文档数量,可以将rangeDeleterBatchSizeto a small value such as32.rangeDeleterBatchSize设置为较小的值,如32。To add an additional delay between batch deletions, you can set要在批删除之间添加额外的延迟,可以将rangeDeleterBatchDelayMSabove the current default of20milliseconds.rangeDeleterBatchDelayMS设置为高于当前默认值20毫秒。
Note
If there are ongoing read operations or open cursors on the collection targeted for deletes, range deletion processes may not proceed.如果要删除的集合上有正在进行的读取操作或打开的游标,则范围删除过程可能无法继续。
Change Streams and Orphan Documents更改流和孤立文档
Starting in MongoDB 5.3, during range migration, change stream events are not generated for updates to orphaned documents.从MongoDB 5.3开始,在范围迁移期间,不会为孤立文档的更新生成更改流事件。
Shard Size分片大小
By default, MongoDB attempts to fill all available disk space with data on every shard as the data set grows. To ensure that the cluster always has the capacity to handle data growth, monitor disk usage as well as other performance metrics.默认情况下,随着数据集的增长,MongoDB会尝试用每个分片上的数据填充所有可用磁盘空间。为了确保集群始终有能力处理数据增长,请监视磁盘使用情况以及其他性能指标。
Chunk Size and Balancing块大小和平衡
For an introduction to 有关chunkSize, see Modify Range Size in a Sharded Cluster.chunkSize的介绍,请参阅修改分片群集中的范围大小。
When the collection data shared between two shards differs by three or more times the configured 当两个分片之间共享的集合数据与配置的chunkSize setting, the balancer migrates chunks between the shards.chunkSize设置相差三倍或更多时,平衡器会在分片之间迁移块。
For example, if 例如,如果chunkSize is 128 MB and the collection data differs by 384 MB or more, the balancer migrates chunks between the shards.chunkSize为128 MB,而集合数据相差384 MB或更多,则平衡器会在分片之间迁移块。
When chunks are moved, split, or merged, the shard metadata is updated after the chunk operation is committed by a config server. Shards not involved in the chunk operation are also updated with new metadata.当块被移动、拆分或合并时,分片元数据会在配置服务器提交块操作后更新。未参与块操作的分片也会用新的元数据进行更新。
The time for the shard metadata update is proportional to the size of the routing table. CRUD operations on the collection are temporarily blocked while the shard metadata is updated, and a smaller routing table means shorter CRUD operation delays.分片元数据更新的时间与路由表的大小成正比。在更新分片元数据时,集合上的CRUD操作会被临时阻止,较小的路由表意味着更短的CRUD操作延迟。
Defragmenting a collection reduces the number of chunks and the time to update the chunk metadata.对集合进行分片整理可以减少块的数量和更新块元数据的时间。
To reduce the system workload, configure the balancer to run only at a specific time using a shard balancing window. Defragmentation runs during the balancing window time period.为了减少系统工作负载,请使用分片平衡窗口将平衡器配置为仅在特定时间运行。分片整理在平衡窗口时间段内运行。
You can use the 您可以使用chunkDefragmentationThrottlingMS parameter to limit the rate of split and merge commands run by the balancer.chunkDefragmentationThrottlingMS参数来限制平衡器运行的拆分和合并命令的速率。
You can start and stop defragmentation at any time.您可以随时启动和停止分片整理。
You can also set a shard zone. A shard zone is based on the shard key, and you can associate each zone with one or more shards in a cluster.您还可以设置一个分片区域。分片区域基于分片键,您可以将每个区域与集群中的一个或多个分片相关联。
A sharded cluster only splits chunks when chunks must be migrated. This means the chunk size may exceed 分片集群仅在必须迁移块时才分割块。这意味着块大小可能超过chunkSize. Larger chunks reduce the number of chunks on a shard and improve performance because the time to update the shard metadata is reduced. chunkSize。较大的块减少了分片上的块数,并提高了性能,因为更新分片元数据的时间减少了。For example, you might see a 1 TB chunk on a shard even though you have set 例如,即使您已将chunkSize to 256 MB.chunkSize设置为256MB,您也可能在分片上看到1TB的块。
chunkSize affects the following:chunkSize会影响以下内容:
Maximum amount of data the balancer attempts to migrate between two shards in a single chunk migration operation.平衡器在单个块迁移操作中尝试在两个分片之间迁移的最大数据量。Amount of data migrated during defragmentation.分片整理期间迁移的数据量。
For details about defragmenting sharded collections, see Defragment Sharded Collections.有关对分片集合进行分片整理的详细信息,请参阅分片集合分片整理。