updateZoneKeyRange

On this page本页内容

Definition定义

updateZoneKeyRange

The updateZoneKeyRange administrative command can either create or remove the association between a range of shard key values and a zone.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()

To run updateZoneKeyRange, use the db.runCommand( { <command> } ) method.要运行updateZoneKeyRange,请使用 db.runCommand( { <command> } )方法。

You must run addShardToZone on the admin database.您必须在管理数据库上运行addShardToZone

The updateZoneKeyRange command has the following syntax:updateZoneKeyRange命令具有以下语法:

{
  updateZoneKeyRange: <string>,
  min: <document>,
  max: <document>,
  zone: <string> | <null>
}

The command takes the following fields:该命令接受以下字段:

Parameter参数Type类型Description描述
updateZoneKeyRangestring

The namespace of the collection to associate with the range.要与范围关联的集合的命名空间。

The collection must be sharded for the command to succeed.必须对集合进行分片化,命令才能成功。

mindocument

The inclusive lower bound of the range of shard key values.分片键值范围的包含下限。

Specify each field of the shard key in the form of <fieldname> : <value>. <fieldname> : <value>的形式指定分片键的每个字段。The value must be of the same BSON type or types as the shard key.该值必须与分片键具有相同的BSON类型。

Note注意

To use hashed sharding, the field value needs to be of type NumberLong.要使用哈希分片,字段值需要为NumberLong类型。

maxdocument

The exclusive upper bound of the range of shard key values.分片键值范围的独占上限。

Specify each field of the shard key in the form of <fieldname> : <value>. <fieldname> : <value>的形式指定分片键的每个字段。The value must be of the same BSON type or types as the shard key.该值必须与分片键具有相同的BSON类型。

Note注意

To use hashed sharding, the field value needs to be of type NumberLong.要使用哈希分片,字段值需要为NumberLong类型。

zonestring

The name of the zone to associate with the range bounded by the min and max.要与由minmax限定的范围关联的分区的名称。

If the value does not match an existing zone, the command fails.如果该值与现有分区不匹配,则命令将失败。

Specify null to remove the association between the range with lower bounds of min and upper bound of max and the updateZoneKeyRange collection. 指定null以删除下限为min、上限为max的范围与updateZoneKeyRange集合之间的关联。The values of min and max must match exactly the target range.minmax必须与目标范围完全匹配。

If no zone range matches the minimum and maximum bounds passed to updateZoneKeyRange, nothing is removed.如果没有区域范围与传递给updateZoneKeyRange的最小和最大边界匹配,则不会删除任何内容。

Only issue updateZoneKeyRange when connected to a mongos instance.仅在连接到mongos实例时发出updateZoneKeyRange

mongosh provides two helper methods:提供了两种帮助器方法:

  • sh.updateZoneKeyRange() for adding a range of shard key values to a zone.用于向区域添加一系列分片键值。
  • sh.removeRangeFromZone() for removing a range of shard key values from a zone.用于从区域中删除一系列分片键值。

Behavior行为

You cannot create a range of shard key values whose lower and upper boundaries overlap with an existing range for the sharded collection. 不能创建一个分片关键值范围,其上下边界与分片集合的现有范围重叠。For example, given an existing range of 1 to 10, you cannot create a new range of 5 to 20, as the new range would overlap with the existing range.例如,如果现有范围为110,则无法创建520的新范围,因为新范围将与现有范围重叠。

A zone can have multiple ranges of data associated with it, but a range can at most be associated with a single zone.一个区域可以有多个数据范围与之关联,但一个范围最多可以与一个区域关联。

When removing the association between a range and a zone, updateZoneKeyRange does not remove the zone. 删除区域和区域之间的关联时,updateZoneKeyRange不会删除区域。Use the removeShardFromZone command to remove the association between a zone and a shard.使用removeShardFromZone命令删除分区和分片之间的关联。

See the zone manual page for more information on zones in sharded clusters.有关分片化集群中的区域的更多信息,请参阅区域手册页面。

Initial Chunk Distribution for Empty or Non-Existing Collections空集合或不存在集合的初始区块分布

If you are considering performing zone sharding on an empty or non-existent collection, use updateZoneKeyRange to create the zones and zone ranges before sharding the collection (since 4.0.2). 如果您正在考虑对一个空的或不存在的集合执行区域分片,请在分片集合之前使用updateZoneKeyRange创建区域和区域范围(从4.0.2开始)。Starting in version 4.0.3, creating zones and zone ranges on empty or non-existing collections allows MongoDB to optimize the initial chunk creation and distribution process when sharding the collection. 从4.0.3版开始,在空的或不存在的集合上创建区域和区域范围允许MongoDB在对集合进行分片时优化初始块创建和分发过程。This optimized process supports faster setup of zoned sharding with less balancer overhead than creating zones after sharding. 这种优化的过程支持更快的分区分片设置,与分片后创建分区相比,平衡器开销更少。The balancer performs all chunk management after the optimized initial chunk creation and distribution.平衡器在优化的初始块创建和分发之后执行所有块管理。

For an example of defining zones and zone ranges for initial chunk distribution, see Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection.有关为初始区块分布定义区域和区域范围的示例,请参阅为空集合或非现有集合预先定义区域和分区范围

Initial Chunk Distribution with Compound Hashed Shard Keys具有复合哈希分片键的初始块分布

Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. 从4.4版开始,MongoDB支持在复合哈希索引上对集合进行分片。MongoDB can perform optimized initial chunk creation and distribution when sharding the empty or non-existing collection on a compound hashed shard key.MongoDB可以在将空的或不存在的集合分片到复合哈希分片键上时,执行优化的初始块创建和分发。

If the hashed field is the prefix of the shard key (i.e. the first field in the shard key), all of the following must be true for MongoDB to perform initial chunk creation and distribution:如果哈希字段是分片键的前缀(即分片键中的第一个字段),则MongoDB必须满足以下所有条件才能执行初始块创建和分发:

If the hashed field is not the prefix of the shard key (i.e. the shard key has one or more non-hashed leading fields), all of the following must be true for MongoDB to perform initial chunk creation and distribution:如果散列字段不是分片键的前缀(即分片键具有一个或多个非散列前导字段),则MongoDB必须满足以下所有条件才能执行初始块创建和分发:

  • The collection has one zone range for each combination of distinct prefix field values (i.e. all fields preceding the hashed field).集合对于不同前缀字段值的每个组合都有一个区域范围(即哈希字段之前的所有字段)。

  • For the lower-bound of each zone range, specify MinKey for the hashed field and all subsequent fields.对于每个区域范围的下限,为哈希字段和所有后续字段指定MinKey

  • For each zone range, at least one upper-bound prefix field must differ from its lower-bound counterpart.对于每个区域范围,至少有一个上限前缀字段必须与其下限对应字段不同。

  • sh.shardCollection() specifies the presplitHashedZones: true option.指定presplitHashedZones: true选项。

For a more complete example of defining zones and zone ranges for initial chunk distribution on a compound hashed shard key, see Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection.有关为复合哈希分片键上的初始块分布定义区域和区域范围的更完整示例,请参阅为空集合或非现有集合预先定义区域和分区范围

Balancer平衡器

After successfully running updateZoneKeyRange, there may be chunk migrations during the next balancer round.成功运行updateZoneKeyRange后,在下一轮平衡器期间可能会发生块迁移。

After adding a range to a zone, the balancer must first run in order to migrate any chunks whose ranges are covered by the zone to shards inside of that zone. 在向区域添加范围后,平衡器必须首先运行,以便将区域覆盖范围的任何块迁移到该区域内的分片。Until balancing completes, some chunks may reside on the wrong shard given the configured zones for the sharded cluster.在平衡完成之前,考虑到分片集群的配置区域,一些块可能会驻留在错误的分片上。

Removing the association between a range and a zone removes the constraints keeping chunks covered by the range on the shards inside that zone. 移除范围和区域之间的关联将移除该区域内分片上保持范围覆盖块的约束。During the next balancer round, the balancer may migrate chunks that were previously covered by the zone.在下一轮平衡器期间,平衡器可以迁移先前由区域覆盖的块。

See the documentation for the sharded cluster balancer for more information on how migrations work in a sharded cluster.有关如何在分片集群中进行迁移的更多信息,请参阅分片集群平衡器的文档。

Bounds边界

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

Dropped Collections丢弃的集合

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

In earlier versions, MongoDB does not remove the tag associations for a dropped collection, and if you later create a new collection with the same name, the old tag associations will apply to the new collection.在早期版本中,MongoDB不会删除已删除集合的标记关联,如果稍后创建同名的新集合,则旧标记关联将应用于新集合。

Security安全

For sharded clusters running with authentication, you must authenticate as either:对于通过身份验证运行的分片集群,您必须通过以下任一身份验证:

  • a user whose privileges include the specified actions on various collections in the config database:其权限包括对config数据库中的各种集合执行指定操作的用户:

    or, alternatively,或者,

  • a user whose privileges include enableSharding on the cluster resource (available starting in version 4.2.2, 4.0.14, 3.6.16).其权限包括在群集资源上enableSharding的用户(从版本4.2.2、4.0.14、3.6.16开始提供)。

The clusterAdmin or clusterManager built-in roles have the appropriate permissions for issuing updateZoneKeyRange. clusterAdminclusterManager内置角色具有发出updateZoneKeyRange的适当权限。See the documentation page for Role-Based Access Control for more information.有关详细信息,请参阅基于角色的访问控制的文档页。

Example示例

Given a sharded collection exampledb.collection with a shard key of { a : 1 }, the following operation creates a range with a lower bound of 1 and an upper bound of 10 on the alpha zone:给定一个分片键为{ a : 1 }的分片集合exampledb.collection,以下操作在alpha区域上创建一个下限为1、上限为10的范围:

admin = db.getSiblingDB("admin")
admin.runCommand(
   {
      updateZoneKeyRange : "exampledb.collection",
      min : { a : 1 },
      max : { a : 10 },
      zone : "alpha"
   }
)

The following operation removes the previously created range by passing null to the zone field.以下操作通过向区域字段传递null来删除先前创建的范围。

admin = db.getSiblingDB("admin")
admin.runCommand(
   {
      updateZoneKeyRange : "exampledb.collection",
      min : { a : 1 },
      max : { a : 10 },
      zone : null
   }
)

The min and max must match exactly the bounds of the target range. minmax必须与目标范围的边界完全匹配。The following operation attempts to remove the previously created range, but specifies { a : 0 } as the min bound:以下操作尝试删除以前创建的范围,但将{ a : 0 }指定为min范围:

admin = db.getSiblingDB("admin")
admin.runCommand(
   {
      updateZoneKeyRange : "exampledb.collection",
      min : { a : 0 },
      max : { a : 10 },
      zone : null
   }
)

While the range of { a : 0 } and { a : 10 } encompasses the existing range, it is not an exact match and therefore updateZoneKeyRange does not remove anything.虽然{ a : 0 }{ a : 10 }的范围包含现有范围,但它不是完全匹配的,因此updateZoneKeyRange不会删除任何内容。

Compound Shard Key复合分片键

Given a sharded collection exampledb.collection with a shard key of { a : 1, b : 1 }, the following operation creates a range covering the lower bound of { a: 1, b : 1 } and an upper bound of { a : 10, b : 10} and associates it with the alpha zone:给定一个分片集合exampledb.collection,其分片键为{ a : 1, b : 1 },以下操作将创建一个范围,该范围覆盖下限{ a: 1, b : 1 }和上限{ a : 10, b : 10},并将其与alpha区域相关联:

admin = db.getSiblingDB("admin")
admin.runCommand(
   {
      updateZoneKeyRange : "exampledb.collection",
      min : { a : 1, b : 1 },
      max : { a : 10, b : 10 },
      zone : "alpha"
   }
)
←  unsetShardingSessions Commands →