{
  "id": "mongodb/transaction-abort-on-shard-migration",
  "signature": "MongoServerError: Transaction aborted due to shard chunk migration in progress",
  "signature_zh": "MongoServerError: 由于分片块迁移进行中，事务已中止",
  "regex": "Transaction aborted due to shard chunk migration in progress",
  "domain": "mongodb",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A multi-document transaction accessed a chunk that was being migrated between shards, causing the balancer to abort the transaction.",
  "root_cause_type": "generic",
  "root_cause_zh": "多文档事务访问了一个正在分片之间迁移的块，导致平衡器中止了该事务。",
  "versions": [
    {
      "version": "MongoDB 5.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MongoDB 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MongoDB 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Stopping the balancer is not a fix for the transaction; it only avoids the symptom but can cause performance issues.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The abort is triggered by the migration process, not by timeout.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The migration is asynchronous and may take seconds to minutes to complete.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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; } }",
      "success_rate": 0.9,
      "how": "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; } }",
      "condition": "",
      "sources": []
    },
    {
      "action": "Temporarily disable the balancer during critical transaction windows: sh.stopBalancer(); // run transactions; sh.startBalancer()",
      "success_rate": 0.85,
      "how": "Temporarily disable the balancer during critical transaction windows: sh.stopBalancer(); // run transactions; sh.startBalancer()",
      "condition": "",
      "sources": []
    },
    {
      "action": "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",
      "success_rate": 0.75,
      "how": "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",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "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"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.mongodb.com/docs/manual/core/sharded-cluster-balancer/",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-11-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}