Skip to content

[Bug] TPCC 1000w/1000t: lockservice 'failed to lock on remote: backend connection closed' during startup #26475

Closed

Description

@Ariznawlll

Environment

Field Value
Cluster TKE Main Nightly Regression
Namespace mo-main-commit-3c04dacaa-20260730
MO commit 6f85cb6d3c
Topology 3 CNs (tp-cn-qkwt7, tp-cn-tgcjf, tp-cn-xpp82), 1 DN, 2 Proxies
Run https://github.com/matrixorigin/mo-nightly-regression/actions/runs/30563962387/job/90984768719

Summary

TPCC 1000 warehouses / 1000 terminals fails with 2 ErrorCode: 20502 (backend connection closed) on TT_NEW_ORDER transactions within 2 seconds of test start (20:22:46 start → 20:22:48 error).

Error Chain (from Loki server-side logs)

20:22:48.053  tp-cn-qkwt7  lockservice/log.go:285   failed to lock on remote        err=backend connection closed  (×2)
20:22:48.665  tp-cn-qkwt7  frontend/session.go:2385 backend connection closed
20:22:48.666  tp-cn-qkwt7  frontend/session.go:2385 query trace status              err=backend connection closed  (×2)
20:22:49~50   tp-cn-qkwt7  lockservice/log.go:633   failed to valid txn             err=backend connection closed  (×7)
20:22:49~54   proxy pods   session.go:515            close connection failed         err=use of closed network connection

The root cause is an inter-CN MORPC connection closure during distributed lock acquisition in the lockservice. CN tp-cn-qkwt7 attempted to acquire a remote lock on another CN, but the MORPC backend connection was already closed, which cascaded into:

  1. ErrBackendClosed (20502) returned to the TPCC client via frontend
  2. 7 additional failed to valid txn errors from lockservice
  3. Proxy connection cleanup errors

TPCC Results

Spec tpmC Errors Result
tpcc_10 (10w/10t) 12,119.8 0 ✅ PASS
tpcc_10 (10w/100t) 42,927.1 0 ✅ PASS
tpcc_100 (100w/100t) 50,176.6 0 ✅ PASS
tpcc_100 (100w/1000t) 51,340.8 0 ✅ PASS
tpcc_1000 (1000w/1000t) 23,118.8 2 ❌ FAIL

Consistency check passed — no data corruption.

History

  • Same commit 6f85cb6d3c ran TPCC successfully earlier the same day (run 30472882209, 0 errors)
  • Past 7 days of TKE main nightly TPCC: all 0 errors
  • First occurrence in normal nightly workload

Related Issues

Same root cause (inter-CN MORPC connection closure during lock acquisition), but this is the first time it appears in normal nightly TPCC workload without chaos injection or merge triggers.

Expected Behavior

1000 terminals establishing connections simultaneously should not cause inter-CN MORPC connections to be closed during distributed lock acquisition.

Activity

  1. added
    kind/bug
    on Jul 31, 2026
  2. added
    severity/s0
    on Jul 31, 2026
  3. added theissue type on Jul 31, 2026
  4. added this to the MO-202607 milestone on Jul 31, 2026
  5. Ariznawlll commented on Jul 31, 2026

    @Ariznawlll
    CollaboratorAuthor

    详细分析

    1. 完整故障链

    错误不是孤立的,分为两个阶段:

    阶段一:TPCC 启动期 MORPC 连接中断 (20:22:48)

    TPCC 1000 terminals 同时建连
      → Proxy 分发到 3 个 CN
        → CN tp-cn-qkwt7 执行 NEW_ORDER 事务
          → lockservice 需要向另一个 CN 获取分布式锁
            → 发现 inter-CN MORPC 连接已关闭 (lockservice/log.go:285)
              → 返回 ErrBackendClosed (20502) 给 frontend
                → frontend/session.go:2385 将错误传给客户端
                  → TPCC 客户端收到 FATAL: [TT_NEW_ORDER][EXECUTION] ErrorCode: 20502
    

    阶段二:级联失败 + 后续全集群 database closed (20:22:49 ~ 20:23:50)

    20:22:49~50  tp-cn-qkwt7 lockservice: failed to valid txn err=backend connection closed (×7)
                 → 已有事务的分布式锁验证也失败了
    
    20:23:35     tp-cn-qkwt7 taskservice: task heartbeat failed err=sql: database is closed
                 tp-cn-tgcjf taskservice: failed to get tasks err=context deadline exceeded
                 tp-cn-xpp82 taskservice: fetch task failed err=context deadline exceeded
                 → 所有 CN 的 task service 开始报 HAKeeper 内部数据库关闭
    
    20:23:38~47  tp-cn-tgcjf: failed to get tasks err=sql: database is closed (×10)
                 tp-cn-xpp82: failed to get tasks err=sql: database is closed (×10)
                 log-1: query cron tasks failed err=sql: database is closed (×5)
                 → sql: database is closed 蔓延到全部组件
    
    20:23:39~59  proxy-2n7kg + proxy-dbtwd: close connection failed (×10)
                 → Proxy 开始批量清理已断开的连接
    

    2. 关键发现

    错误发生在启动后仅 2 秒:tpcc_1000 在 20:22:46 开始执行,20:22:48 就报错。1000 个 terminal 同时建立连接并发起分布式事务,造成瞬时连接风暴。

    影响范围远超 2 个事务:

    • 2 个 TPCC 客户端事务报错(表面影响)
    • 7 次 lockservice failed to valid txn(已有事务的锁验证失败)
    • 后续 ~1 分钟,全集群 3 CN + 1 log-service + 2 proxy 全部出现 sql: database is closed 错误

    CN 自身没有 crash/restart:Exec.Run 日志显示 CN 在 20:22:54 仍在正常处理查询(Exec.Run 时间 1.4s~8.2s),说明是 MORPC 连接层问题,不是 CN 进程问题。

    3. 为什么认为是 bug 而不是偶发网络抖动

    证据 说明
    错误集中在单个 CN 全部 lockservice 错误都来自 tp-cn-qkwt7,不是随机分布
    有明确的代码路径 lockservice/log.go:285 → frontend/session.go:2385,是 lockservice 获取远程锁时检测到 MORPC 连接关闭
    后续全集群级联 如果是单纯的网络抖动,不会导致 47 秒后全集群 sql: database is closed
    只在 1000t 时出现 tpcc_10/100/100 全部 0 错误,说明是高并发下的边界条件
    与 #25779 根因一致 同样是 inter-CN MORPC backend 关闭,但 #25779 只在 merge-trigger 场景出现,本次在正常负载下出现

    4. 可能原因

    1. MORPC 连接池在高并发下回收策略不当 — 1000 terminals 同时建连可能触发 MORPC 连接池的清理逻辑,误关闭了正在使用的 inter-CN 连接
    2. lockservice 缺少 MORPC 连接重试 — lockservice/log.go:285 在获取远程锁时遇到连接关闭,直接返回错误而不是重建连接重试
    3. HAKeeper 内部数据库连接泄漏 — 后续的 sql: database is closed 级联说明可能有关联的连接管理问题

    5. 建议修复方向

    • lockservice 在 failed to lock on remote 时增加 MORPC 连接重建 + 重试机制
    • 排查高并发建连场景下 MORPC 连接池的连接回收逻辑
    • 排查 sql: database is closed 级联的根因(是否与 MORPC 连接关闭有关联,还是独立问题)
  6. XuPeng-SH commented on Jul 31, 2026

    @XuPeng-SH
    Contributor

    Reference: correlated Loki/Prometheus evidence

    Conclusion — confirmed, high confidence: the two 20502: backend connection closed errors were not direct evidence that the peer CN or TCP connection had already closed. The immediate trigger was a local lockservice MORPC backend auto-create timeout at the 500 ms boundary during the TPCC startup transaction burst. The affected CN was simultaneously at critical cgroup memory usage, so the lock retry guard intentionally stopped retrying and the two transient failures escaped to SQL.

    The MatrixOne commit printed by this TPCC job is 3c04dacaa606b643e815bdf8535988d0e4576e12. The shorter commit currently mentioned in the issue is the workflow repository revision, not the MatrixOne build revision.

    Correlated timeline (UTC)

    • 20:22:46: TPCC starts the 1000-warehouse / 1000-terminal transaction phase. All 1000 terminal connections had already been created; this was a transaction-start burst, not 1000 simultaneous connection establishments.
    • 20:22:47.553: lockservice creates a remote bind.
    • 20:22:48.053: two remote-lock attempts fail almost exactly 500 ms later with backend connection closed.
    • 20:22:48.417: the same bind is closed as keeper-failed.
    • 20:22:48.665: keeping remote locks fails; both retries are stopped by the critical-memory guard and both retry budgets terminate.
    • 20:22:48.665–20:22:48.666: the two errors are returned through the frontend.
    • Another CN later shows the same bind-create-to-failure interval of approximately 500 ms, which is consistent with a deterministic local timeout rather than a random peer disconnect.

    The code at the tested revision closes the chain:

    • pkg/lockservice/rpc.go configures WithClientAutoCreateWaitTimeout(500*time.Millisecond).
    • pkg/common/morpc/client.go maps ErrBackendCreateTimeout to moerr.NewBackendClosedNoCtx(), so a local backend-creation timeout is exposed externally as error 20502.
    • The pre-fix MORPC manager has one process-wide backend-create worker. Time spent waiting behind other create requests consumes the same 500 ms caller budget.
    • MORPC metrics recorded 10 backend auto-create timeouts in the window, all from lockservice clients. There was no corresponding lockservice failed-connect increment supporting a peer connection failure.

    Memory pressure: amplifier, not the original trigger

    At the closest scrape after the event, the affected CN had:

    • cgroup memory usage: 54.93 GiB / 55.0 GiB (99.9%)
    • working set: 47.75 GiB (86.8% of the limit)
    • RSS: 40.41 GiB
    • cache: 14.09 GiB
    • OOM events: 0
    • container restart delta: 0

    The lock retry policy uses cgroup memory usage and stops retrying at the critical threshold (90%). Therefore memory pressure explains why the transient backend-create timeout was not recovered, but it does not explain the first 500 ms timeout itself. Since cgroup usage includes cache, follow-up observability should retain both usage and working-set values when evaluating this guard; the evidence here does not by itself prove that the current policy is wrong.

    First-principles causal chain

    1. Trigger: backend creation did not complete within the local 500 ms wait budget under startup/concurrency pressure.
    2. Classification gap: a local queue/create timeout was encoded as backend connection closed, conflating local scheduling delay with peer failure.
    3. Amplifier: critical cgroup memory usage caused the retry guard to stop recovery.
    4. Aftermath: keeper failure/validation logs and bind closure followed the timeout. Later proxy cleanup/connection-reset messages are secondary and should not be used as the root cause without matching request/bind identity.
    5. Ruled out by current evidence: CN restart, container OOM, and confirmed peer/TCP closure as the initiating event.

    The run continued for five minutes, completed 116,385 transactions including 52,150 NewOrders, and passed the consistency check. This bounds the observed impact to two escaped transient transaction errors; no corruption signal was observed.

    Relation to #26473

    #26473 directly addresses the primary trigger and classification gap by separating local queue budget from peer factory budget, using a bounded multi-worker create pool, rejecting stale generation results, isolating lockservice transport roles, and preventing local queue timeout/full conditions from poisoning the peer breaker.

    It should not yet be considered proven for this incident until the same TPCC 1000w/1000t startup pattern is replayed. Suggested acceptance evidence:

    • no frontend 20502 escapes during the startup transaction burst;
    • local queue timeout/full is reported separately from peer connect/close failure;
    • local queue pressure does not update the peer circuit breaker;
    • queue wait, factory/connect latency, and stale-generation rejection are independently observable;
    • retry behavior remains bounded and explicit when cgroup usage is at or above the critical-memory threshold.

    The initial backend-create problem and the memory retry policy are related but distinct control loops. Fixing the former substantially reduces the probability of this failure; the high-memory replay is still needed to verify the combined unhappy path.

  7. XuPeng-SH commented on Jul 31, 2026

    @XuPeng-SH
    Contributor

    Follow-up: why critical memory stops lock retries

    This is an intentional load-shedding / OOM-prevention policy, not evidence that memory pressure caused the original MORPC timeout.

    The policy applies to the bounded backend/bind availability retry path, rather than to every normal lock conflict:

    • below 80% memory usage: retry at roughly 1-second intervals, within a 10-second total budget;
    • from 80% to 90%: use a 5-second backoff and allow at most 16 concurrent retry waiters;
    • at or above 90%: stop retrying immediately, return the original error, and let the statement/transaction unwind.

    The first-principles rationale is that a waiting transaction is not free. It can retain pipeline state, batches/vectors, lock rows, transaction metadata, RPC state, timers, and referenced buffers. Under a backend outage or recovery delay, many concurrent retries can therefore form a positive feedback loop:

    backend unavailable -> more retained transactions -> more memory/RPC pressure -> slower recovery -> more retries

    Failing the transaction at the critical threshold attempts to break that loop and release resources before the container is OOM-killed. This is a survival-over-availability decision.

    However, the current signal is coarse. defaultLockRetryMemoryPressureLevel uses system.MemoryUsed()/MemoryTotal(), and in a container MemoryUsed() reads total cgroup usage, which includes page cache. At this incident's closest scrape, total cgroup usage was 99.9%, while working set was 86.8%, cache was 14.09 GiB, and there were no OOM events or restarts. The guard therefore behaved as implemented, but the measurement does not prove that 99.9% was all unreclaimable pressure.

    The remaining policy risks are:

    • a single instantaneous sample causes an abrupt availability change at 90%;
    • no hysteresis or sustained-pressure window;
    • no distinction among working set, anonymous/RSS memory, and reclaimable cache;
    • no use of memory PSI, OOM events, growth rate, or estimated bytes retained per retry;
    • all retries in this class are rejected at critical pressure instead of retaining a very small emergency recovery budget.

    The systemic follow-up should not simply raise the threshold. It should evaluate a pressure signal based on actual headroom and reclaimability, add hysteresis/sustained sampling, preserve a strictly bounded recovery lane where safe, and expose the pressure level and stop reason as metrics.

    #26473 addresses the initiating backend-create congestion and its incorrect peer-failure classification. It does not materially change this memory guard, so the same high-memory TPCC replay is still required to validate the combined unhappy path.

  8. aptend commented on Jul 31, 2026

    @aptend
    Contributor

    Independent re-review

    After independently checking the TPCC job log, the 5-minute Loki/Prometheus window around the failure, and the code at the tested MatrixOne revision (3c04dacaa606b643e815bdf8535988d0e4576e12), I agree with the follow-up analysis.

    The evidence supports this causal chain:

    1. The immediate trigger was a local lockservice MORPC backend auto-create timeout at the 500 ms boundary, rather than proven prior closure of the peer CN or TCP connection.
    2. ErrBackendCreateTimeout reused the ErrBackendClosed code/message, which explains the externally visible 20502 classification.
    3. Critical cgroup memory usage on the affected CN caused the bounded lock retry path to stop immediately, allowing two transient failures to escape to SQL. Memory pressure amplified the incident but did not initiate the MORPC timeout.
    4. The proxy cleanup and later taskservice sql: database is closed messages do not establish the initiating causal chain.

    The independent checks also found a useful control case: another CN hit the same approximately 500 ms lock-client auto-create timeout pattern while below the critical-memory threshold, but no additional TPCC client errors escaped. This is consistent with retry policy being the difference between recovery and frontend exposure.

    I therefore agree that #26473 addresses the primary trigger and classification/lifecycle gaps and is the appropriate solution for this incident. Final incident closure should still require replaying the same TPCC 1000w/1000t startup pattern, including the high-memory condition, and confirming that no frontend 20502 escapes and that local queue pressure is reported separately from real peer connection failures.

  9. assigned and unassigned on Jul 31, 2026
  10. Ariznawlll commented on Aug 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    最近几天没再出现,先关闭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions