reshardCollection

On this page本页内容

Definition定义

reshardCollection

New in version 5.0.在版本5.0中新增

The reshardCollection command changes the shard key for a collection and changes the distribution of your data.reshardCollection命令更改集合的分片键,并更改数据的分布。

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

{
  reshardCollection: "<database>.<collection>",
  key: <shardkey>,
  unique: <boolean>,
  numInitialChunks: <integer>,
  collation: { locale: "simple" },
  zones: [
      {
          min: <document with same shape as shardkey>,
          max: <document with same shape as shardkey>,
          zone: <string> | null
      },
      ...
  ]
}

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

Field字段Type类型Description描述
reshardCollectionstringThe namespace of the collection to be resharded. 要重新保护的集合的命名空间Takes the form <database>.<collection>. 形式为<database>.<collection>
keydocument

The document that specifies the new field or fields to use as the shard key.指定要用作分片键的一个或多个新字段的文档。

{ <field1>: <1|"hashed">, ... }

Set the field values to either:将字段值设置为:

uniquebooleanOptional. 可选。Specify whether there is a uniqueness constraint on the shard key. Only false is supported. 指定分片键上是否存在唯一性约束。仅支持falseDefaults to false. 默认为false
numInitialChunksintegerOptional. 可选。Specifies the initial number of chunks to create across all shards in the cluster when resharding a collection. 指定重新分配集合时要在集群中的所有分片上创建的块的初始数量。The default is the number of chunks that exist for the collection under the current shard key pattern. 默认值是当前分片键模式下集合存在的块数。MongoDB will then create and balance chunks across the cluster. 然后MongoDB将在集群中创建和平衡区块。The numInitialChunks must result in less than 8192 per shard. numInitialChunks必须导致每个分片少于8192个。
collationdocumentOptional. 可选。If the collection specified to reshardCollection has a default collation, you must include a collation document with { locale : "simple" }, or the reshardCollection command fails. 如果指定给reshardCollection的集合具有默认排序规则,则必须包含带有{ locale : "simple" }的排序规则文档,否则reshardCollection命令将失败。
zonesarrayOptional. 可选。To maintain or add zones, specify the zones for your collection in an array. 要维护或添加分区,请在数组中指定集合的分区。

The mongosh provides a wrapper method sh.reshardCollection().mongosh提供了一个包装方法sh.reshardCollection()

Resharding Process重新分片过程

During the resharding process, there are two roles a shard may play:在重新分类过程中,分片可以扮演两个角色:

  • Donors are shards which currently own chunks of the sharded collection.捐赠方是目前拥有分片集合的分片。
  • Recipients are shards which would own chunks of the sharded collection according to the new shard key and zones.接收方是分片,根据新的分片键和区域,这些分片将拥有分片集合的块。

A shard may play both the role of a donor and a recipient concurrently. 分片可以同时扮演捐赠者和接受者的角色。Unless zones are being used, the set of donor shards is the same as the set of recipient shards.除非使用区域,否则施主分片集与接收方分片集相同。

The config server primary is always chosen as the resharding coordinator, responsible for initiating each phase of the process.始终选择配置服务器主服务器作为重新分发协调器,负责启动流程的每个阶段。

Initialization Phase初始化阶段

During the initialization phase:在初始化阶段:

  • The balancer determines the new data distribution for the sharded collection.平衡器确定分片集合的新数据分布。

Index Phase索引阶段

During the index phase:在索引阶段:

  • Each shard recipient creates a new, empty sharded collection with the same collection options as the existing sharded collection. 每个分片接收者创建一个新的空分片集合,其集合选项与现有分片集合相同。This new sharded collection is the target for where recipient shards write the new data.这个新的分片集合是接收方分片写入新数据的目标。
  • Each shard recipient builds the necessary new indexes. 每个分片接收者构建必要的新索引。These include all existing indexes on the sharded collection and an index compatible with the new shard key pattern if such an index doesn’t already exist on the sharded collection.其中包括分片集合上的所有现有索引,以及与新分片键模式兼容的索引(如果分片集合上不存在此类索引)。

Clone, Apply, and Catch-up Phase克隆、应用和追赶阶段

During the clone, apply, and catch-up phase:在克隆、应用和追赶阶段:

  • Each shard recipient clones an initial copy of the documents it would own under the new shard key.每个分片接收者都会在新的分片键下克隆其所拥有的文档的初始副本。
  • Each shard recipient begins applying oplog entries from operations that happened after the recipient cloned the data.每个分片接收者都开始从接收者克隆数据后发生的操作中应用oplog条目。
  • When the estimate for the time remaining to complete the resharding operation is under two seconds, the resharding coordinator blocks writes for the collection.当完成重新分配操作的剩余时间估计值不足两秒时,重新分配协调器将阻止对集合的写入。

    Note注意

    If desired, you can manually force the resharding operation to complete by issuing the commitReshardCollection command. 如果需要,可以通过发出commitReshardCollection命令手动强制完成重新发送操作。This is useful if the current time estimate to complete the resharding operation is an acceptable duration for your collection to block writes. 如果完成重新整理操作的当前时间估计值是集合阻止写入的可接受持续时间,则这非常有用。The commitReshardCollection command blocks writes early and forces the resharding operation to complete. commitReshardCollection命令阻止早期写入,并强制重新加载操作完成。During the time period where writes are blocked your application experiences an increase in latency.在写入被阻止的时间段内,应用程序的延迟会增加。

Commit Phase提交阶段

  • Once the resharding process reaches the commit phase, it may no longer be aborted with abortReshardCollection.一旦重新提交过程到达提交阶段,它可能不再使用abortReshardCollection中止。
  • When all shards have reached strict consistency, the resharding coordinator commits the resharding operation and installs the new routing table.当所有分片都达到严格一致性时,重新分配协调器将提交重新分配操作并安装新的路由表。
  • The resharding coordinator instructs each donor and recipient shard primary, independently, to rename the temporary sharded collection. 重新分配协调器指示每个捐赠者和接收者分片主,独立地重命名临时分片集合。The temporary collection becomes the new resharded collection.临时集合将成为新的重新保护集合。
  • Each donor shard drops the old sharded collection.每个捐赠者分片都会丢弃旧分片集合。

    Tip提示

Example示例

Reshard a Collection重新分片集合

The following example reshards the sales.orders collection with the new shard key { order_id: 1 }:以下示例使用新的分片键{ order_id: 1 }重新包装sales.orders集合:

db.adminCommand({
  reshardCollection: "sales.orders",
  key: { order_id: 1 }
})

MongoDB returns the following:MongoDB返回以下内容:

{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp(1, 1624887954),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: 0
    }
  },
  operationTime: Timestamp(1, 1624887947)
}
Tip提示
←  removeShardFromZonesetAllowMigrations →