Skip to content

[Bug]: CN can keep stale lock table bind after TN allocator restart until COMMIT fails #24896

Closed

Description

@iamlinjunhong

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searches for "lock table bind changed", ErrLockTableBindChanged, lockservice allocator bind epoch, and stale lock table bind allocator found several historical closed issues, but I did not find an open issue for this specific allocator-epoch stale-bind scenario.

Branch Name

  • Affected production image: registry.jst.com.cn/moi/matrixone:3.0.0-5e4210029-hotfix-20260603
  • Affected local branch/repro: 3.0-dev

Commit ID

  • Production image commit: 5e4210029
  • Local reproduction commit: 1b4aa0a7a458d69c834c1292a9628056eed86cae

Other Environment Information

  • Deployment mode: distributed MO with CN and TN/DN pods
  • Component: lockservice, lockTableAllocator, pessimistic transaction commit validation
  • Observed during a data import workflow. The failing SQL observed in MO logs was COMMIT, not the preceding DML.

Actual Behavior

A CN can keep local lock table binds created by an older TN/DN lock allocator epoch after the TN/DN allocator pod is rebuilt. If the CN does not restart, and it later accesses another table first, the new allocator registers the same CN service ID again. After that, normal keepalive returns OK=true, but it does not validate the CN's already cached table binds.

As a result, stale table binds can remain on the CN for a long time and are only detected at transaction commit time by allocator.Valid(). The user-visible result is:

Error 20702 (HY000): lock table bind changed

Evidence from one incident:

old allocator start:
  2026-06-05 15:13:42 +0800
  version=1780643622856501201

old binds assigned:
  2026-06-08 14:07:25 +0800
  table=272592 / 272593
  bind version=1780643622856501202

new allocator start after DN pod rebuild:
  2026-06-08 14:11:38 +0800
  version=1780899098803397307

commit failure:
  2026-06-09 09:39:55 +0800
  error=lock table bind changed
  allocator current=0-0(0)--0
  received old bind=0-272592(272592)-...-1780643622856501202
  received old bind=0-272593(272593)-...-1780643622856501202

The important part is that the later keepalive can be healthy (OK=true) even though the CN still has stale per-table binds from the old allocator epoch.

Expected Behavior

After the TN/DN lock allocator restarts and gets a new allocator epoch, CNs that still cache binds from an older allocator epoch should proactively fence and remove those stale local lock tables before a user transaction reaches COMMIT.

allocator.Valid() should remain as the final safety check, but normal operation should not allow an old allocator epoch bind to sit on a live CN until a business transaction commit fails.

Steps to Reproduce

A minimal reproduction can be modeled in pkg/lockservice/service_test.go:

  1. Start one CN lock service and one lock table allocator.
  2. Let the CN lock and unlock table A so that it caches table A's local lock table bind.
  3. Simulate a TN/DN allocator pod rebuild while the CN stays alive:
    • clear allocator services
    • clear allocator lockTables
    • move allocator version forward to represent a new allocator epoch
  4. Let the same CN access table B. This calls GetBind and registers the same CN service ID in the new allocator.
  5. KeepLockTableBind(serviceID) can now return OK=true.
  6. The old table A bind still exists in the CN's local tableGroups.
  7. Call allocator.Valid(serviceID, txnID, []LockTable{oldTableABind}) to simulate commit validation.
  8. The allocator returns table A as invalid, matching the production ErrLockTableBindChanged path.

A local UT was added to demonstrate this behavior:

go test ./pkg/lockservice -run TestCommitDetectsStaleLocalBindAfterAllocatorRestart -count=1

The test demonstrates the current bug shape: keepalive can be OK after the CN re-registers through another table, while the older table bind is still only detected by allocator.Valid().

Additional information

Likely root cause:

  • lockTableAllocator.version is initialized from time.Now().UnixNano() when the allocator is constructed.
  • Normal ServiceLockEnable keepalive does not send all local LockTables.
  • KeepLockTableBind(serviceID) only checks whether the service exists and marks it active.
  • If the service has already been re-registered in the new allocator epoch by another GetBind, keepalive returns OK and does not remove stale per-table binds.

Suggested fix direction:

  • Add an explicit allocator epoch/version field to allocator responses, at least:
    • GetBindResponse.AllocatorVersion
    • KeepLockTableBindResponse.AllocatorVersion
  • CN should observe allocator epoch from those responses through a serialized observeAllocatorVersion(...) path.
  • When a newer allocator epoch is observed, CN should purge local lock tables whose bind version belongs to an older allocator epoch.
  • Do not infer allocator epoch from an arbitrary LockTable.Version: createBindLocked sets b.Version = allocator.version, but tryRebindLocked does old.Version++, so LockTable.Version is allocator_epoch + per-table increment, not a pure allocator epoch.
  • Keep allocator.Valid() as the final commit-time safety check.

A design note and task breakdown were prepared locally under docs/lockservice/lock-table-bind-epoch-fencing-fix-design.md.

Activity

  1. added
    severity/s0
    on Jun 30, 2026
  2. added this to the MO-202607 milestone on Jun 30, 2026
  3. added
    ai-medium
    and removed
    ai-medium
    on Jul 1, 2026
  4. Repository owner unassigned on Jul 13, 2026
  5. Ariznawlll commented on Jul 13, 2026

    @Ariznawlll
    Collaborator

    已在最新 main 完成回归和扩展白盒验证,当前问题无法复现,修复有效。

    验证环境

    原始故障链路回归

    执行 TestCommitDetectsStaleLocalBindAfterAllocatorRestart,用例完整模拟了 issue 中的现场路径:

    1. CN 在旧 allocator epoch 下访问并缓存表 272592 的 local lock table bind;
    2. 清空 allocator 的 services 和 lockTables,推进 allocator epoch,模拟 TN/DN allocator pod 重建,而 CN 不重启;
    3. 重启后、刷新前,旧 bind 交给 allocator.Valid() 会被判为 invalid,证明测试确实构造出了原始故障前提;
    4. 同一个 CN 再访问另一张表 50011829,触发新 allocator 的 GetBind;
    5. CN 从响应中的 allocator identity/epoch 识别 allocator 已更换,主动删除表 272592 的旧本地 bind;
    6. 新表 bind 使用新 epoch,KeepLockTableBind 正常;
    7. CN 再次访问表 272592 时重新获取新 bind,新旧 bind 的 identity 不同,后续 Lock/Unlock 成功。

    该原始用例连续执行 100 轮,全部通过:

    go test ./pkg/lockservice \
      -run '^TestCommitDetectsStaleLocalBindAfterAllocatorRestart$' \
      -count=100
    
    ok github.com/matrixorigin/matrixone/pkg/lockservice 8.071s
    

    扩展白盒测试矩阵

    以下 21 个测试均开启 -race,每个连续执行 10 轮,共 210 次,全部通过且未发现数据竞争:

    go test -race ./pkg/lockservice \
      -run '^(TestCommitDetectsStaleLocalBindAfterAllocatorRestart|TestAllocatorVersionZeroKeepsLocalBinds|TestAllocatorObserverDoesNotPurgeSameEpochBindVersions|TestKeepBindFailedSkipsBindPublishedAfterSnapshot|TestGetBindPurgesStaleBindWhenAllocatorIDChangesWithRegressedVersion|TestKeepaliveEpochPurgeKeepsGroupMovePop|TestKeepaliveOKFalseWithNewAllocatorVersionPurgesStaleBinds|TestKeepaliveOKFalseFencesActiveTxn|TestAllocatorObserverRejectsSupersededGetBindResponse|TestAllocatorPublishRejectsStaleBindAfterNewAllocatorObserved|TestAllocatorPublishRejectsOverwriteAfterConcurrentBindChanged|TestGetLatestLockTableBindRejectsSupersededAllocator|TestAllocatorObserverBoundsSupersededAllocatorIDs|TestAllocatorIDDistinguishesBindIdentity|TestAllocatorObserverPurgesRemoteAndProxyLockTables|TestAllocatorObserverConcurrentKeepaliveAndGetBind|TestAllocatorObserverCloseWaitersOnStaleLocalBind|TestRemoteNewBindFromNewAllocatorPurgesStaleBinds|TestRemoteBindRefreshRejectsSupersededAllocatorBind|TestKeepRemoteLockRefreshRejectsSupersededAllocatorBind|TestGetLockHolderRemoteDoesNotBlockAllocatorEpochPurge)$' \
      -count=10
    
    ok github.com/matrixorigin/matrixone/pkg/lockservice 13.957s
    

    覆盖点和结果如下:

    1. allocator 重启与主动清理:新 allocator epoch 通过 GetBind/keepalive 被 CN 观察到后,旧 local bind 会在业务事务提交前被清理。
    2. allocator ID 更换但 version 回退:不依赖时间戳/version 单调递增;即使新 allocator version 比旧值小,也能依靠 allocator ID 判定实例已经更换并删除旧 bind。
    3. 兼容响应:allocator version 为 0 时不误删本地 bind;同一 epoch 内因单表 rebind 产生的 bind version 增量不会被误认为 allocator 重启。
    4. keepalive 两种返回:OK=true 和 OK=false 路径都能观察新 allocator;旧 local/remote/proxy bind 被清理,新 epoch 的 proxy bind 被保留。
    5. 活跃事务 fencing:旧 bind 被清理时,持有旧 bind 的活跃事务被正确 fence;不会继续以旧 bind 提交。
    6. 等待者唤醒:删除 stale local lock table 时会关闭/通知等待者,等待中的事务返回 ErrLockTableNotFound,不会永久挂起。
    7. 并发 keepalive/GetBind:allocator observer、keepalive 和新表 Lock/GetBind 并发执行时,最终 allocator 状态一致,旧表 bind 必然清除,race detector 无告警。
    8. 乱序旧响应:已经观察到新 allocator 后,延迟到达的旧 GetBind/refresh 响应会被拒绝,不能重新发布旧 bind,也不能覆盖并发产生的新 bind。
    9. remote/proxy 路径:远端 lock table、local proxy、remote refresh、keeper refresh 和 GetLockHolder 均遵守 allocator fencing;epoch 清理不会被远端查询长时间阻塞。
    10. 连续多次 allocator 替换:superseded allocator ID 集合有上限,旧实例响应仍被拒绝,不会无限增长。
    11. group move/pop:epoch 清理同步移除 group move 队列中的 stale table,不遗留二次发布入口。
    12. 快照并发保护:keepalive 清理只处理快照中的旧 bind,不会误删快照之后刚发布的新 bind。

    全包回归

    另外执行了 lockservice 全包测试:

    go test ./pkg/lockservice -count=1
    
    ok github.com/matrixorigin/matrixone/pkg/lockservice 124.983s
    

    代码结论

    当前实现已把 allocator identity/epoch 放入 bind/keepalive 响应,并通过串行化的 allocator state observer 统一处理状态切换。发现新 allocator 时会在 CN 侧主动 purge 旧 local/remote/proxy lock table,同时保留 allocator.Valid() 作为最终安全校验;旧 allocator 的延迟响应也无法把 stale bind 重新写回。

    因此,issue 所描述的“CN keepalive 健康,但旧 epoch bind 一直保留到业务事务 COMMIT 才报 lock table bind changed”路径在最新 main 已被阻断。本轮未发现新的回归缺陷,可以关闭。

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

Metadata

Metadata

Labels

kind/bugphase/testingseverity/s0

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions