Reshard a Collection重分片集合

On this page本页内容

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

The ideal shard key allows MongoDB to distribute documents evenly throughout the cluster while facilitating common query patterns. 理想的分片键允许MongoDB在整个集群中均匀分布文档,同时促进常见的查询模式。A suboptimal shard key can lead to performance or scaling issues due to uneven data distribution. 由于数据分布不均匀,次优的分片键可能会导致性能或缩放问题。Starting in MongoDB 5.0, you can change the shard key for a collection to change the distribution of your data across a cluster.从MongoDB 5.0开始,您可以更改集合的shard键,以更改数据在集群中的分布。

Note注意

Before resharding your collection, read Troubleshoot Shard Keys for information on common performance and scaling issues and advice on how to fix them.在重新分片集合之前,请阅读分片键疑难解答,了解常见性能和扩展问题的信息,以及如何修复这些问题的建议。

Requirements要求

Before you reshard your collection, ensure that you meet the following requirements:在重新存储集合之前,请确保满足以下要求:

  • Your application can tolerate a period of two seconds where the collection that is being resharded blocks writes. 应用程序可以容忍两秒钟的时间来阻止正在重新硬存储的集合写入。During the time period where writes are blocked your application experiences an increase in latency. 在写操作被阻止的时间段内,应用程序的延迟会增加。If your workload cannot tolerate this requirement, consider refining your shard key instead.如果您的工作负载无法满足此要求,请考虑改进您的共享密钥
  • Your database meets these resource requirements:您的数据库满足以下资源要求:

    • Available storage space: Ensure that your available storage space is at least 1.2x the size of the collection that you want to reshard. 可用存储空间:确保可用存储空间至少是要重新硬存储的集合大小的1.2倍。For example, if the size of the collection you want to reshard is 1 TB, you should have at least 1.2 TB of free storage when starting the sharding operation.例如,如果要重新硬存储的集合大小为1 TB,则在启动分片操作时,应至少有1.2 TB的可用存储空间。
    • I/O: Ensure that your I/O capacity is below 50%.输入/输出:确保您的输入/输出容量低于50%。
    • CPU load: Ensure your CPU load is below 80%.CPU负载:确保您的CPU负载低于80%。
    Important重要

    These requirements are not enforced by the database. 数据库不强制执行这些要求。A failure to allocate enough resources can result in:未能分配足够的资源可能导致:

    • the database running out of space and shutting down数据库空间不足,正在关闭
    • decreased performance性能下降
    • the resharding operation taking longer than expected重新分片操作的时间比预期的要长

    If your application has time periods with less traffic, reshard your collection during that time if possible.如果您的应用程序具有流量较少的时间段,请尽可能在该时间段内重新硬存储您的集合。

  • You have rewritten your application code to update your queries to use both the current shard key and the new shard key.您已经重写了应用程序代码,以更新查询,使其同时使用当前的分片键和新的分片键。

    The following queries return an error if the query filter does not include both the current shard key or a unique field (like _id):如果查询筛选器不同时包含当前分片键或唯一字段(如_id),则以下查询将返回错误:

    For optimal performance, we recommend that you also rewrite other queries to include the new shard key.为了获得最佳性能,我们建议您也重写其他查询以包含新的shard键。

    Once the resharding operation completes, you can remove the old shard key from the queries.一旦重新加密操作完成,就可以从查询中删除旧的shard密钥。

  • No index builds are in progress. 没有正在进行的索引生成。Use db.currentOp() to check for any running index builds:使用db.currentOp()检查是否有任何正在运行的索引生成:

     db.adminCommand(
        {
          currentOp: true,
          $or: [
            { op: "command", "command.createIndexes": { $exists: true }  },
            { op: "none", "msg" : /^Index Build/ }
          ]
        }
    )

    In the result document, if the inprog field value is an empty array, there are no index builds in progress:在结果文档中,如果inprog字段值为空数组,则没有正在进行的索引生成:

    {
       inprog: [],
       ok: 1,
       '$clusterTime': { ... },
       operationTime: <timestamp>
    }
Warning警告

We strongly recommend that you check the Limitations and read the resharding process section in full before resharding your collection.我们强烈建议您在重新分片您的集合之前,检查限制并阅读完整的重分片过程部分。

Limitations局限性

  • Only one collection can be resharded at a time.一次只能重新存储一个集合。
  • writeConcernMajorityJournalDefault must be true.必须是真的。
  • Resharding a collection that has a uniqueness constraint is not supported.不支持对具有唯一性约束的集合进行重新硬划分。
  • The new shard key cannot have a uniqueness constraint.新的分片键不能有唯一性约束。
  • The following commands and corresponding shell methods are not supported on the collection that is being resharded while the resharding operation is in progress:正在进行重新存储操作时,正在重新存储的集合不支持以下命令和相应的shell方法:

  • The following commands and methods are not supported on the cluster while the resharding operation is in progress:正在进行重新存储操作时,群集上不支持以下命令和方法:

    Warning警告

    Using any of the preceding commands during a resharding operation causes the resharding operation to fail.在重新存储操作期间使用上述任何命令都会导致重新存储操作失败。

  • If the collection to be resharded uses Atlas Search, the search index will become unavailable when the resharding operation completes. 如果要重新存储的集合使用Atlas Search,则当重新存储操作完成时,搜索索引将不可用。You need to manually rebuild the search index once the resharding operation completes.重新存储操作完成后,需要手动重建搜索索引。

Resharding Process重分片过程

1

Start the resharding operation.开始重新装货操作。

While connected to the mongos, issue a reshardCollection command that specifies the collection to be resharded and the new shard key:连接到mongos时,发出reshardCollection命令,指定要重新存储的集合和新的分片键:

db.adminCommand({
  reshardCollection: "<database>.<collection>",
  key: <shardkey>
})

MongoDB sets the max number of seconds to block writes to two seconds and begins the resharding operation.MongoDB将阻塞写入的最大秒数设置为2秒,并开始重新存储操作。

2

Monitor the resharding operation.监控重新装货操作。

To monitor the resharding operation, you can use the $currentOp pipeline stage:要监视重新存储操作,可以使用$currentOp管道阶段:

db.getSiblingDB("admin").aggregate([
  { $currentOp: { allUsers: true, localOps: false } },
  {
    $match: {
      type: "op",
      "originatingCommand.reshardCollection": "<database>.<collection>"
    }
  }
])
Note注意

To see updated values, you need to continuously run the preceeding pipeline.要查看更新的值,需要连续运行前面的管道。

The $currentOp pipeline outputs:$currentOp管道输出:

  • totalOperationTimeElapsedSecs: elapsed operation time in seconds:运行时间(秒)
  • remainingOperationTimeEstimatedSecs: estimate of the remaining time to complete the resharding operation:估计完成重新装货操作的剩余时间
[
  {
    shard: '<shard>',
    type: 'op',
    desc: 'ReshardingRecipientService | ReshardingDonorService | ReshardingCoordinatorService <reshardingUUID>',
    op: 'command',
    ns: '<database>.<collection>',
    originatingCommand: {
      reshardCollection: '<database>.<collection>',
      key: <shardkey>,
      unique: <boolean>,
      collation: { locale: 'simple' }
    },
    totalOperationTimeElapsedSecs: <number>,
    remainingOperationTimeEstimatedSecs: <number>,
    ...
  },
  ...
]
3

Finish the resharding operation.完成重新装货操作。

Throughout the resharding process, the estimated time to complete the resharding operation (remainingOperationTimeEstimatedSecs) decreases. 在整个重新装载过程中,完成重新装载操作的估计时间(剩余remainingOperationTimeEstimatedSecs)减少。When the estimated time is below two seconds, MongoDB blocks writes and completes the resharding operation. 当估计时间低于2秒时,MongoDB阻塞写入并完成重新存储操作。Until the estimated time to complete the resharing operation is below two seconds, the resharding operation does not block writes by default. 在完成重新存储操作的估计时间低于两秒之前,默认情况下,重新存储操作不会阻止写入。During the time period where writes are blocked your application experiences an increase in latency.在写操作被阻止的时间段内,应用程序的延迟会增加。

Once the resharding process has completed, the resharding command returns ok: 1.重分片过程完成后,重分片命令返回ok:1

{
  ok: 1,
  '$clusterTime': {
    clusterTime: <timestamp>,
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: <number>
    }
  },
  operationTime: <timestamp>
}

To see whether the resharding operation completed successfully, check the output of the sh.status() method:要查看重新存储操作是否成功完成,请检查sh.status()方法的输出:

sh.status()

The sh.status() method output contains a subsection for the databases. sh.status()方法输出包含数据库的子部分。If resharding has completed successfully, the output lists the new shard key for the collection:如果重新存储成功完成,输出将列出集合的新分片键:

databases
[
  {
     database: {
     _id: '<database>',
     primary: '<shard>',
     partitioned: true,
     version: {
       uuid: <uuid>,
       timestamp: <timestamp>,
       lastMod: <number>
     }
   },
   collections: {
     '<database>.<collection>': {
       shardKey: <shardkey>,
       unique: <boolean>,
       balancing: <boolean>,
       chunks: [],
       tags: []
     }
   }
  }
  ...
]
Note注意

If the resharded collection uses Atlas Search, the search index will become unavailable when the resharding operation completes. 如果重新存储的集合使用Atlas Search,则当重新存储操作完成时,搜索索引将不可用。You need to manually rebuild the search index once the resharding operation completes.重新存储操作完成后,需要手动重建搜索索引。

Block writes early to force resharding to complete提前写块以强制重新存储完成

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命令会阻止提前写入,并强制完成重新存储操作。The command has the following syntax:该命令具有以下语法:

db.adminCommand({
  commitReshardCollection: "<database>.<collection>"
})

Abort resharding operation中止重新装载操作

You can abort the resharding operation during any stage of the resharding operation, even after running the commitReshardCollection, until shards have fully caught up.您可以在重新存储操作的任何阶段中止重新存储操作,即使在运行commitReshardCollection之后,直到分片完全被捕获。

For example, if the write unavailability duration estimate does not decrease, you can abort the resharding operation with the abortReshardCollection command:例如,如果写入不可用持续时间估计值没有减少,可以使用abortReshardCollection命令中止重新存储操作:

db.adminCommand({
  abortReshardCollection: "<database>.<collection>"
})

After canceling the operation, you can retry the resharding operation during a time window with lower write volume. 取消操作后,可以在写入量较低的时间窗口内重试重新存储操作。If this is not possible, add more shards before retrying.如果不可能,请在重试之前添加更多分片

Behavior行为

Minimum Duration of a Resharding Operation重新分片操作的最短持续时间

The minimum duration of a resharding operation is always 5 minutes.重新装货操作的最短持续时间始终为5分钟。

Retryable Writes可重试写入

Retryable writes initiated before or during resharding can be retried during and after the collection has been resharded for up to 5 minutes. 在重新存储之前或期间启动的可重试写入操作可以在集合重新存储期间和之后重试,最多5分钟。After 5 minutes you may be unable to find the definitive result of the write and subsequent attempts to retry the write fail with an IncompleteTransactionHistory error.5分钟后,您可能无法找到写入的最终结果,随后尝试重试写入失败,并出现IncompleteTransactionHistory错误。

Error Cases错误案例

Primary Failover主故障切换s

If a primary failover on a replica set shard or config server occurs, the resharding operation aborts.如果副本集分片或配置服务器上发生主故障转移,则重新存储操作将中止。

If a resharding operation aborts due to a primary failover, run the cleanupReshardCollection command before starting a new resharding operation:如果重新存储操作由于主故障转移而中止,请在启动新的重新存储操作之前运行cleanupReshardCollection命令:

db.runCommand({
  cleanupReshardCollection: "<database>.<collection>"
})

Duplicate _id Values重复的_id

The resharding operation fails if _id values are not globally unique to avoid corrupting collection data. 如果_id值不是全局唯一的,则重新存储操作失败,以避免损坏集合数据。Duplicate _id values can also prevent successful chunk migration. 重复的_id值也会阻止成功的块迁移。If you have documents with duplicate _id values, copy the data from each into a new document, and then delete the duplicate documents.如果有具有重复_id值的文档,请将每个文档中的数据复制到新文档中,然后删除重复的文档。

←  Refine a Shard KeyChange a Document's Shard Key Value →