Zones区域

On this page本页内容

In sharded clusters, you can create zones of sharded data based on the shard key. 在分片集群中,可以基于分片密钥创建分片数据区域You can associate each zone with one or more shards in the cluster. 可以将每个分区与群集中的一个或多个分片相关联。A shard can associate with any number of zones. 分片可以与任意数量的区域关联。In a balanced cluster, MongoDB migrates chunks covered by a zone only to those shards associated with the zone.在平衡集群中,MongoDB只将区域覆盖的迁移到与该区域相关联的分片。

Some common deployment patterns where zones can be applied are as follows:可以应用区域的一些常见部署模式如下:

The following image illustrates a sharded cluster with three shards and two zones. 下图显示了一个包含三个分片和两个区域的分片簇。The A zone represents a range with a lower boundary of 1 and an upper bound of 10. A区域表示下限为1、上限为10的范围。The B zone represents a range with a lower boundary of 10 and an upper boundary of 20. B区代表一个下限为10、上限为20的范围。Shards Alpha and Beta have the A zone. 分片AlphaBeta具有A区域。Shard Beta also has the B zone. 分片Beta也有B区。Shard Charlie has no zones associated with it. 分片Charlie没有与之相关的区域。The cluster is in a steady state and no chunks violate any of the zones.集群处于稳定状态,没有块体违反任何区域。

Diagram of data distribution based on zones in a sharded cluster

Behavior and Operations行为和操作

Ranges范围

Each zone covers one or more ranges of shard key values for a collection. 每个分区覆盖一个集合的一个或多个分片键值范围。Each range a zone covers is always inclusive of its lower boundary and exclusive of its upper boundary. 区域覆盖的每个范围始终包括其下边界,不包括其上边界。Zones cannot share ranges, nor can they have overlapping ranges.分区不能共享范围,也不能有重叠的范围。

For example, consider a shard key on {"x": 1}. 例如,考虑{"x": 1}上的分片键。The cluster has the following zone ranges:集群具有以下区域范围:

{ "x" : 5 } --> { "x" : 10 } // Zone A
{ "x" : 10} --> { "x" : 20 } // Zone B
  • A document with a shard key value of 7 is routed to zone A.分片键值为7的文档被路由到区域A。
  • A document with shard key value of 10 is routed to zone B.分片键值为10的文档被路由到区域B。

Hashed Shard Keys and Zone Ranges散列分片键和区域范围

For collections whose shard key includes a hashed field, zone ranges and data distribution on that field are on hashed values. 对于分片键包含散列字段的集合,该字段上的区域范围和数据分布基于散列值。The zone contains documents whose hashed shard key value falls into the defined range. 该区域包含哈希分片键值在定义范围内的文档。A zone range on a hashed field does not have the same predictable document routing behavior as a zone range on an unhashed field.散列字段上的区域范围与未散列字段上的区域范围不具有相同的可预测文档路由行为。

For example, consider a shard key on {"x" : "hashed"}. 例如,考虑{"x" : "hashed"}上的分片键。The following range represents the hashed range between 5 and 10:以下范围表示510之间的散列范围:

{ "x": NumberLong("4470791281878691347") } --> { "x": NumberLong("7766103514953448109") } // Zone A
  • A document with a shard key value of 1 is routed to Zone A since the hashed value of 1 falls into the defined range.分片键值为1的文档被路由到区域A,因为散列值为1属于定义的范围。
  • A document with a shard key value of 15 is routed to Zone A since the hashed value of 15 falls into the defined range.分片键值为15的文档被路由到区域A,因为散列值为15属于定义的范围。
  • A document with a shard key value of 8 is not routed to Zone A since the hashed value of 8 does not fall into the defined range.分片键值为8的文档不会被路由到区域A,因为散列值为8不属于定义的范围。

mongosh provides the convertShardKeyToHashed() for computing the post-hash value of the specified parameter.提供convertShardKeyToHashed(),用于计算指定参数的后哈希值。

One valid use of zone ranges on a hashed field is to restrict the data for a collection to the shard or shards in a single zone. 哈希字段上区域范围的一个有效用法是将集合的数据限制为单个区域中的一个或多个分片。Create a zone range that covers the entire range of possible hashed shard key values using minKey as the lower bound and maxkey as the upper bound.使用minKey作为下限,maxkey作为上限,创建一个区域范围,覆盖可能的散列分片键值的整个范围。

To define ranges, MongoDB provides the updateZoneKeyRange command and the associated helper methods sh.updateZoneKeyRange() and sh.addShardTag().为了定义范围,MongoDB提供了updateZoneKeyRange命令和相关的助手方法sh.updateZoneKeyRange()sh.updateZoneKeyRange()

Starting in MongoDB 4.0.2, you can run updateZoneKeyRange database command and its helpers sh.updateZoneKeyRange() and sh.addTagRange() on an unsharded collection or a non-existing collection.从MongoDB 4.0.2开始,您可以在未共享的集合或不存在的集合上运行updateZoneKeyRange数据库命令及其助手sh.updateZoneKeyRange()sh.addTagRange()

Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.从MongoDB 4.0.2开始,删除集合将删除其关联的区域/标记范围。

Initial Chunk Distribution初始组块分布

Changed in version 4.0.3.在版本4.0.3中更改

By defining the zones and the zone ranges before sharding an empty or a non-existing collection, the shard collection operation creates chunks for the defined zone ranges as well as any additional chunks to cover the entire range of the shard key values and performs an initial chunk distribution based on the zone ranges. 通过在对空集合或不存在的集合进行分片之前定义分区和分区范围,分片集合操作将为定义的分区范围以及任何其他分片创建分块,以覆盖整个分片键值范围,并基于分区范围执行初始分块分布。This initial creation and distribution of chunks allows for faster setup of zoned sharding. 块的初始创建和分布允许更快地设置分区分片。After the initial distribution, the balancer manages the chunk distribution going forward.在初始分发之后,均衡器继续管理区块分发。

Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. 从4.4版开始,MongoDB支持在复合散列索引上分片集合。When sharding an empty or non-existing collection using a compound hashed shard key, additional requirements apply in order for MongoDB to perform initial chunk creation and distribution.当使用复合散列分片键分片空的或不存在的集合时,MongoDB执行初始块创建和分发需要满足额外的要求。

See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for for an example.有关示例,请参阅空集合或不存在集合的预定义分区和分区范围

Tip提示
See also: 参阅:

Balancer均衡器

The balancer attempts to evenly distribute a sharded collection's chunks across all shards in the cluster.均衡器尝试在集群中的所有分片上均匀分布分片集合的块。

For each chunk marked for migration, the balancer checks each possible destination shard for any configured zones. 对于标记为迁移的每个,平衡器检查每个可能的目标分片是否有任何配置的区域。If the chunk range falls into a zone, the balancer migrates the chunk into a shard inside that zone. 如果块范围落入某个区域,则平衡器将块迁移到该区域内的分片中。Chunks that do not fall into a zone can exist on any shard in the cluster and are migrated normally.不属于区域的块可以存在于集群中的任何分片上,并正常迁移。

During balancing rounds, if the balancer detects that any chunks violate the configured zones for a given shard, the balancer migrates those chunks to a shard where no conflict exists.在平衡循环期间,如果平衡器检测到任何块违反给定分片的配置区域,则平衡器将这些块迁移到不存在冲突的分片。

After associating a zone with a shard or shards and configuring the zone with a shard key range for a sharded collection, the cluster may take some time to migrate the affected data for the sharded collection. 将分区与一个或多个分片关联并为分区集合配置分区密钥范围后,集群可能需要一些时间来迁移分区集合的受影响数据。This depends on the division of chunks and the current distribution of data in the cluster. 这取决于数据块的划分和集群中数据的当前分布。When balancing is complete, reads and writes for documents in a given zone are routed only to the shard or shards inside that zone.平衡完成后,对给定区域中文档的读取和写入只路由到该区域内的一个或多个分片。

Once configured, the balancer respects zones during future balancing rounds.一旦配置完毕,均衡器将在未来的平衡回合中尊重区域。

Tip提示
See also: 参阅:

Shard Key分片键

You must use fields contained in the shard key when defining a new range for a zone to cover. 定义分区要覆盖的新范围时,必须使用分片键中包含的字段。If using a compound shard key, the range must include the prefix of the shard key.如果使用复合分片键,则范围必须包括分片键的前缀。

For example, given a shard key { a : 1, b : 1, c : 1 }, creating or updating a range to cover values of b requires including a as the prefix. 例如,给定一个分片键{ a : 1, b : 1, c : 1 },创建或更新覆盖b值的范围需要包含a作为前缀。Creating or updating a range to covers values of c requires including a and b as the prefix.创建或更新覆盖c值的范围需要将ab作为前缀。

You cannot create ranges using fields not included in the shard key. 不能使用未包含在分片键中的字段创建范围。For example, if you wanted to use zones to partition data based on geographic location, the shard key would need the first field to contain geographic data.例如,如果希望使用分区根据地理位置对数据进行分区,则shard key需要第一个字段来包含地理数据。

When choosing a shard key for a collection, consider what fields you might want to use for configuring zones. 为集合选择分片键时,请考虑您可能希望使用哪些字段来配置区域。See Choose a Shard Key for considerations in choosing a shard key.有关选择分片键时的注意事项,请参阅选择分片键

Shard Zone Boundaries分片区边界

Zone ranges are always inclusive of the lower boundary and exclusive of the upper boundary.分区范围始终包括下边界,不包括上边界。

Time Series Collections时间序列集合

MongoDB does not support creating zones for sharded time series collections.MongoDB不支持为分片时间序列集合创建区域。

Tip提示
←  Deploy a Sharded ClusterManage Shard Zones →