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:可以应用区域的一些常见部署模式如下:
Isolate a specific subset of data on a specific set of shards.隔离特定分片集上的特定数据子集。Ensure that the most relevant data reside on shards that are geographically closest to the application servers.确保最相关的数据位于地理位置上离应用程序服务器最近的分片上。Route data to shards based on the hardware / performance of the shard hardware.根据分片硬件的硬件/性能将数据路由到分片。
The following image illustrates a sharded cluster with three shards and two zones. 下图显示了一个具有三个分片和两个区域的分片集群。The A区域表示一个范围,其下限为A
zone represents a range with a lower boundary of 1
and an upper bound of 10
. 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. Alpha
和Beta
有A
区。Shard 分片贝塔也有Beta
also has the B
zone. Shard Charlie
has no zones associated with it. B
区。分片Charlie
没有与之相关的区域。The cluster is in a steady state and no chunks violate any of the zones.集群处于稳定状态,没有任何区块违反任何区域。
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
:5
到10
之间的散列范围:
{ "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 of1
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 of15
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 of8
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 为了定义范围,MongoDB提供了updateZoneKeyRange
command and the associated helper methods sh.updateZoneKeyRange()
and sh.addShardTag()
.updateZoneKeyRange
命令和相关的辅助方法sh.updateZoneKeyRange()
和sh.addShardTag()
。
Starting in MongoDB 4.0.2, you can run 从MongoDB 4.0.2开始,您可以在未分片的集合或不存在的集合上运行updateZoneKeyRange
database command and its helpers sh.updateZoneKeyRange()
and sh.addTagRange()
on an unsharded collection or a non-existing collection.updateZoneKeyRange
数据库命令及其助手sh.updateZoneKeyRange()
和sh.addTagRange()
。
Dropping a collection deletes its associated zone/tag ranges.删除集合将删除其关联的区域/标记范围。
Initial Chunk Distribution初始区块分布
Changed in version 4.0.34.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.有关示例,请参阅空集合或不存在集合的预定义分区和分区范围。
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.配置后,平衡器在未来的平衡回合中尊重区域。
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. Creating or updating a range to covers values of c
requires including a
and b
as the prefix.{ a : 1, b : 1, c : 1 }
,创建或更新一个范围以覆盖b
的值需要包含a
作为前缀。创建或更新覆盖c
值的范围需要包含a
和b
作为前缀。
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.不能使用未包含在分片键中的字段创建范围。例如,如果您想使用zone根据地理位置对数据进行分区,那么分片键需要第一个字段来包含地理数据。
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.区域范围始终包括下边界,不包括上边界。