>>>>>>> cn
翻译或纠错本页面

开启分片集合的均衡

Balancing is the process MongoDB uses to distribute data of a sharded collection evenly across a sharded cluster. When a shard has too many of a sharded collection’s chunks compared to other shards, MongoDB automatically balances the chunks across the shards. The balancing procedure for sharded clusters is entirely transparent to the user and application layer.

集群均衡器

The balancer process is responsible for redistributing the chunks of a sharded collection evenly among the shards for every sharded collection. By default, the balancer process is always enabled.

集群中任意一个 mongos 都可以开始一个均衡过程.当一个均衡过程处于活动状态时,开始这个过程的 mongos 会通过修改 Config Database 中的 lock 集合获得一个 “lock” .

在 3.2 版更改: With replica set config servers, clock skew does not affect distributed lock management. If you are using mirrored config servers, large differences in timekeeping can lead to failed distributed locks. With mirrored config servers, minimize clock skew by running the network time protocol (NTP) ntpd on your servers.

To address uneven chunk distribution for a sharded collection, the balancer migrates chunks from shards with more chunks to shards with a fewer number of chunks. The balancer migrates the chunks, one at a time, until there is an even distribution of chunks for the collection across the shards. For details about chunk migration, see Chunk Migration Procedure.

在 2.6 版更改: 为了解决分片集合数据块不均衡的问题,均衡器会从数据块较多的分片 migrates chunks 到数据块较少的分片,均衡器一次迁移一个数据块,直到不同分片之间的数据块数目变得均衡.

由于占用带宽以及系统负载,数据块的迁移带来的开销会影响数据库的性能, balancer 尽可能减少带来的性能影响.

  • 每次只迁移一个数据块,参见 数据块迁移排队 .

  • Starting a balancing round only when the difference in the number of chunks between the shard with the greatest number of chunks for a sharded collection and the shard with the lowest number of chunks for that collection reaches the migration threshold.

你可以暂时停掉均衡器以进行运维,参见 禁用均衡器 以获取更多细节

你也可以限制均衡进行的时间窗口来防止在生产环境中造成性能问题,参见 Schedule the Balancing Window 以获取更多细节.

注解

指定的均衡时间窗口依赖于每个 mongos 的本地时间.

参见

文档 管理集群均衡过程.

迁移阈值

为了使均衡对集群性能的影响减小到最低,在不同分片之间的数据块数量差异达到阈值时, balancer 不会开始工作,阈值是指集群中 chunks 最多的分片与数据块最少的分片之间数据块数量的差,均衡器有以下阈值:

数据块的数量

迁移阈值

少于20个

2
20-79 4
80 and greater 8

一旦一个均衡过程开始,除非集群中,这个集合在不同分片的数据块数目差 少于2个 或者数据块迁移失败,均衡过程不会停止.

分片数据量大小

默认情况下,随着数据文件的增长,MongoDB会使用每个分片上所有的可用磁盘空间.为了保证MongoDB能够持续容纳数据增长,要监控磁盘使用率以及其他性能指标.

When adding a shard, you may set a “maximum size” for that shard. This prevents the balancer from migrating chunks to the shard when the value of mem.mapped exceeds the “maximum size”. Use the maxSize parameter of the addShard command to set the “maximum size” for the shard.