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. The cluster is in a steady state and no chunks violate any of the zones.Beta也有B区。分片Charlie没有与之关联的区域。集群处于稳定状态,没有任何块违反任何区域。
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}. The cluster has the following zone ranges:{"x": 1}上的分片键。该集群具有以下区域范围:
{ "x" : 5 } --> { "x" : 10 } // Zone A
{ "x" : 10} --> { "x" : 20 } // Zone B
A document with a shard key value of分片键值为7is routed to zone A.7的文档被路由到区域A。A document with shard key value of分片键值为10is 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"}. The following range represents the hashed range between 5 and 10:{"x" : "hashed"}上的分片键。以下范围表示5到10之间的哈希范围:
{ "x": Long("4470791281878691347") } --> { "x": Long("7766103514953448109") } // Zone A
A document with a shard key value of分片键值为1is routed to Zone A since the hashed value of1falls into the defined range.1的文档被路由到区域A,因为哈希值1落入定义的范围内。A document with a shard key value of分片键值为15is routed to Zone A since the hashed value of15falls into the defined range.15的文档被路由到区域A,因为哈希值为15落入定义的范围内。A document with a shard key value of分片键值为8is not routed to Zone A since the hashed value of8does 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()。
You can run 您可以在未记录的集合或不存在的集合上运行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初始块分布
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.这种块的初始创建和分发允许更快地设置分区分片。在初始分发之后,平衡器管理接下来的块分发。
MongoDB supports sharding collections on compound hashed indexes. 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.有关示例,请参阅空集合或不存在集合的预定义分区和分区范围。
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.配置后,平衡器在未来的平衡回合中会尊重区域。
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的值需要包含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.您无法使用分片键中未包含的字段创建范围。例如,如果你想使用区域根据地理位置对数据进行分区,分片键需要第一个字段包含地理数据。
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时间序列集合
Zone sharding does not support time series collections. The balancer always distributes data in sharded time series collections evenly across all shards in the cluster.区域分片不支持时间序列集合。平衡器总是将分片时间序列集合中的数据均匀地分布在集群中的所有分片上。