【MongoDB学习笔记十四】MongoDB分片自动均衡

假如MongoDB分片集群采用单调递增的字段作为片键,例如系统时间,那么随着插入的增多,越来越多的文档将插入到区间到正无穷的分片上,这样,分片将不均衡,MongoDB提供了自动均衡的方法来将数据量进行均衡。



 

均衡进程的运行机制

The balancer is a background process that manages chunk migrations. The balancer runs in all of the query routers in a cluster.

When the distribution of a sharded collection in a cluster is uneven, the balancer process migrates chunks from the shard that has the largest number of chunks to the shard with the least number of chunks until the collection balances. For example: if collection users has 100 chunks on shard 1 and 50 chunks on shard 2, the balancer will migrate chunks from shard 1 to shard 2 until the collection achieves balance.

The shards manage chunk migrations as a background operation between an origin shard and a destination shard. During a chunk migration, the destination shard is sent all the current documents in the chunk from the origin shard. Next, the destination shard captures and applies all changes made to the data during the migration process. Finally, the metadata regarding the location of the chunk on config server is updated.

If there’s an error during the migration, the balancer aborts the process leaving the chunk unchanged on the origin shard. MongoDB removes the chunk’s data from the origin shard after the migration completes successfully.

扫描二维码关注公众号,回复: 1166978 查看本文章

在实际中,通常会把MongoDB自动均衡这个选项禁掉,也就是不允许MongoDB做自动均衡,那么问题来了,当新增一个分片时,如果把原来分片中的数据移动到新分片,做到数据均衡

猜你喜欢

转载自bit1129.iteye.com/blog/2158718