mongodb
runtime_error
ai_generated
true
MongoServerError: 由于分片块迁移进行中,事务已中止
MongoServerError: Transaction aborted due to shard chunk migration in progress
ID: mongodb/transaction-abort-on-shard-migration
85%修复率
84%置信度
1证据数
2023-11-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| MongoDB 5.0 | active | — | — | — |
| MongoDB 6.0 | active | — | — | — |
| MongoDB 7.0 | active | — | — | — |
根因分析
多文档事务访问了一个正在分片之间迁移的块,导致平衡器中止了该事务。
English
A multi-document transaction accessed a chunk that was being migrated between shards, causing the balancer to abort the transaction.
官方文档
https://www.mongodb.com/docs/manual/core/sharded-cluster-balancer/解决方案
-
Implement a retry loop with exponential backoff to re-run the transaction after a delay: while (true) { try { session.startTransaction(); ... session.commitTransaction(); break; } catch (e) { if (e.code === 251) { sleep(1000 * Math.pow(2, retries)); retries++; } else throw e; } } -
Temporarily disable the balancer during critical transaction windows: sh.stopBalancer(); // run transactions; sh.startBalancer()
-
Avoid accessing chunks that are being migrated by querying the config.changelog collection to check for recent migrations and routing transactions to stable chunks: use config.changelog to identify migration patterns
无效尝试
常见但无效的做法:
-
50% 失败
Stopping the balancer is not a fix for the transaction; it only avoids the symptom but can cause performance issues.
-
90% 失败
The abort is triggered by the migration process, not by timeout.
-
70% 失败
The migration is asynchronous and may take seconds to minutes to complete.