您可以完成迁移,并使用mongosync切换进程将应用程序工作负载从源集群转移到目标集群。
mongosync 应保持活动状态,直到达到 COMMITTED 状态。这允许 mongosync同步迁移期间发生的任何其他写入。
注意
在将应用程序工作负载切换到目标集群之前,应始终验证同步是否成功。 有关详细信息,请参阅验证数据传输。
步骤
验证 mongosync 的状态。
在开始切换进程之前,调用进度端点以确定mongosync的状态。 确保mongosync进程状态显示以下值:
canCommit是true。lagTimeSeconds很小(接近0)。如果在转换开始时
lagTimeSeconds不接近0,则转换可能需要很长时间。使用嵌入式验证程序时,请检查
verification.source和verification.destination返回文档。两个文档中的lagTimeSeconds字段应接近0,并且phase字段应显示"stream hashing"。如果验证者未处于流哈希阶段,则切换进程可能需要很长时间。
以下示例返回同步进程的状态。
请求
curl localhost:27182/api/v1/progress -XGET
响应
{ "progress": { "state":"RUNNING", "canCommit":true, "canWrite":false, "info":"change event application", "lagTimeSeconds":0, "collectionCopy": { "estimatedTotalBytes":694, "estimatedCopiedBytes":694 }, "directionMapping": { "Source":"cluster0: localhost:27017", "Destination":"cluster1: localhost:27018" }, "verification": { "source": { "estimatedDocumentCount": 42, "hashedDocumentCount": 42, "lagTimeSeconds": 2, "totalCollectionCount": 42, "scannedCollectionCount": 10, "phase": "stream hashing" }, "destination": { "estimatedDocumentCount": 42, "hashedDocumentCount": 42, "lagTimeSeconds": 2, "totalCollectionCount": 42, "scannedCollectionCount": 10, "phase": "stream hashing" } } }, "success": true }
停止对源上已同步集合的任何写入操作。
等待源集群上的所有事务提交或中止。
mongosync默认启用仅默认写入阻塞。 您可以通过启动mongosync并将enableUserWriteBlocking设立为"destinationOnly"来显式启用此功能。mongosync仅阻止对目标的写入,并在canWrite被设立为true之前解除阻止。如果在
enableUserWriteBlocking设立为"sourceAndDestination"的情况下启动mongosync,则mongosync会阻止目标集群上的所有写入操作,并在将canWrite设立为true之前解除对这些操作的阻止。 调用/commit后,mongosync会阻止对源进行写入。If you start
mongosyncwithenableUserWriteBlockingset to"none", ensure that you disable writes. For example, run thesetUserWriteBlockModecommand on the source cluster:db.adminCommand( { setUserWriteBlockMode: 1, global: true } ) 如果
mongosync使用筛选同步,则无需禁用对整个源集群的写入。 但是,您必须确保停止对过滤包含的集合的写入操作。
向 mongosync 发送提交请求。
如果为迁移启动多个 mongosync 实例,则必须为每个 mongosync实例发出提交请求。
请求
curl localhost:27182/api/v1/commit -XPOST --data '{ }'
响应
{"success":true}
注意
提交commit请求后,调用progress端点以确保mongosync状态为COMMITTING或COMMITTED 。
如果源集群包含持久查询设置 (PQS),则必须手动将 PQS迁移到目标集群。
如果您之前将 enableUserWriteBlocking设立为 sourceAndDestination,则完成此步骤后,mongosync 将阻止对源集群的写入。
If you previously set enableUserWriteBlocking to sourceAndDestination and mongosync crashes during this step, you must manually unblock writes on the source cluster by using setUserWriteBlockMode.
等待,直到可以对目标集群执行写入。
调用 progress 终结点以确定 canWrite 是否为 true。如果 canWrite 为 false,请等到 progress 显示 canWrite 为 true。如果您正在运行多个 mongosync 实例,则仅检查第一个 mongosync进程上的 canWrite 是否为 true。任何后续 mongosync 进程的 canWrite 状态均无效。
curl -sS localhost:27182/api/v1/progress -XGET | jq ".progress.canWrite"
true
If you manually blocked writes on the destination cluster by using setUserWriteBlockMode, enable application writes on the destination cluster.
To enable writes, update setUserWriteBlockMode:
db.adminCommand( { setUserWriteBlockMode: 1, global: false } )
然后,将应用程序工作负载传输到目标集群。
如果您通过在/start端点上使用 enableUserWriteBlocking 选项来启动具有写入阻塞功能的 mongosync,则无需完成此步骤。
行为
canWrite 和 COMMITTED
mongosync 允许在 COMMITTED 状态之前的阶段写入目标集群。
如果将 enableUserWriteBlocking设立为 "sourceAndDestination" 或 "destinationOnly",或者验证器已开启,则可以在 /progress 报告 canWrite: true 后写入目标。否则,请等到省/市/自治区为 COMMITTED。如果正在运行mongosync 的多个实例,请仅使用第一个 mongosync进程中的 canWrite 状态。
在初始同步中,mongosync 将源集群上的唯一索引复制为目标集群上的非唯一索引。在提交期间,目标集群上的相关非唯一索引将被设立为 prepareUnique。完成后,/progress 端点开始返回 canWrite: true。具有 prepareUnique 索引的集合会拒绝违反唯一索引约束的新文档。然后,mongosync 将 prepareUnique 索引转换为唯一索引。完成后,mongosync 会将其状态更改为 COMMITTED。
注意
同步大型集合时,将 prepareUnique 索引转换为唯一索引可能会占用大量资源。这可能会导致 /progress 端点返回 canWrite: true 和 mongosync 达到 COMMITTED 状态之间需要很长时间。
虽然应用程序可以在返回 canWrite: true 后发出写入操作,但需要独占锁的操作(例如索引构建)只能在索引转换完成后才能继续。此外,由于竞争资源争用,写入操作在此期间可能会出现延迟增加。