Skip to content

cherry-pick: add locks functions - #25094

Merged
XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
daviszhen:0823-pick-4.0-to-main-389fd3a
Jun 24, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
daviszhen:0823-pick-4.0-to-main-389fd3a

Conversation

@daviszhen

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #24486

What this PR does / why we need it:

增加user level locks函数: is_used_lock, release_all_locks.

• IS_USED_LOCK:

  • 新增 lockservice 点查接口 GetLockHolder(tableID, row, options)
  • 复用现有 GetTxnLock 远程 RPC,并在 response 中增加 Holder / Found,让 holder 查询能路由到真正持有 lock table 的 CN,支持跨 CN。
  • user lock 的 row key 保持为 \0<lock_name>;synthetic txn id 中编码 \0<connection_id>\0<lock_name>。
  • IS_USED_LOCK 从 holder 的 synthetic txn id 中解析 connection_id;如果锁不存在或 holder txn id 不是 user lock 格式,则返回 NULL。

RELEASE_ALL_LOCKS:

  • 只维护本 session 的本地状态:counts 记录可重入计数,byOwner 记录当前 session 持有的 lock name。
  • 只释放当前 session 自己持有的 user locks,通过当前 session 的 owner 和 connection id 生成 synthetic txn id 执行 unlock。
  • 返回释放的本地可重入 lock 次数。
  • 不再维护本地全局 holders map,避免错误报告或释放其他 session / 其他 CN 持有的锁。

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added the size/XXL Denotes a PR that changes 2000+ lines label Jun 23, 2026
@daviszhen

Copy link
Copy Markdown
Contributor Author

Code Review: User-Level Lock Functions (GET_LOCK / RELEASE_LOCK / IS_FREE_LOCK / IS_USED_LOCK / RELEASE_ALL_LOCKS)

Overall this is a well-structured PR with solid test coverage (unit tests + BVT). The implementation cleanly separates local session state from distributed lock service state. Below are detailed findings.

🔴 Correctness Issue: releaseAllUserLevelLocks error semantics hide partial success

In releaseAllUserLevelLocks (func_unary.go), when an unlock fails for one lock, the code records firstErr and continues. But if the first lock fails, released stays 0 and all subsequent successful unlocks do increment released. The final return is return released, firstErr.

However, looking at the test TestReleaseAllUserLevelLocksReturnsCountWhenUnlockFails: the expectation is released=0 when unlock fails. This happens because untrackAllUserLevelLock is in the else branch of the error check — wait, actually re-reading the code:

if err := unlockUserLevelLockTxnIDs(...); err != nil {
    // firstErr = err; continue  -- does NOT untrack, does NOT increment released
}
if _, held := untrackAllUserLevelLock(owner, name); held {
    released++
}

So if unlock fails, released is NOT incremented for that lock, and subsequent locks also fail (because unlockErr is set on the mock). This means the test only covers "all unlocks fail." There is no test for partial failure (lock A fails, lock B succeeds). In a partial failure scenario, the function would return (1, err) — is that the intended MySQL-compatible behavior? MySQL's RELEASE_ALL_LOCKS doesn't return errors this way. Consider clarifying the semantics.

🟡 Design Observation: Package-level global userLevelLocks mutex

The userLevelLocks variable is a package-level global with its own sync.Mutex. All sessions on a CN share one mutex. Under high concurrency with many sessions using user-level locks, this could become a contention point. This is acceptable for now but consider noting in a comment that this state is per-process (per-CN), not per-session.

🟡 userLevelLockConnectionIDFromTxnID parsing relies on \x00 delimiter

parts := strings.Split(string(txnID), "\x00")
if len(parts) < 3 || len(parts) > 4 || parts[0] != "mo-user-level-lock" {

The txnID format mo-user-level-lock\x00<owner>\x00<name>\x00<connID> is parsed by splitting on NUL. normalizeUserLevelLockName correctly rejects names with NUL bytes (good), but owner (session ID string) is not validated for NUL bytes. In practice, UUIDs and connection IDs never contain NUL, so this is low-risk. Consider adding a defensive comment.

The probe txnID format (mo-user-level-lock-probe\x00...) is correctly rejected by parts[0] != "mo-user-level-lock" — good defensive coding.

🟢 Positive: lockTableLookupInputsFromRequest is clean refactoring

The extraction of lockTableLookupInputsFromRequest properly handles the sharding lookup for both Method_GetLockHolder and Method_GetTxnLock without duplicating logic.

🟢 Positive: Bind-change retry loop in service-level GetLockHolder

The retry loop on ErrLockTableBindChanged correctly reacquires the lock table and retries. Test TestGetLockHolderReacquiresLockTableAfterBindChanged validates this. The bindChangeMu.RLock() is held across the local lookup, and TestGetLockHolderHoldsBindChangeLockAcrossLocalLookup explicitly verifies this with TryLock. Well done.

🟢 Positive: Legacy txnID compatibility

unlockUserLevelLockTxnIDs unlocks both the new format and old format (userLevelLockTxnIDOld). Backward compatibility during upgrades is covered by TestReleaseLockLegacyTxnIDCompatible.

🟢 Positive: Context-aware retry backoff

The new waitRemoteRetryBackoffWithContext properly handles context cancellation during backoff, unlike the existing waitRemoteRetryBackoff which uses time.Sleep. Good improvement. Test TestGetLockHolderRemoteStopsRetryWhenContextCanceled covers this.

🟢 Positive: Comprehensive test coverage

The test suite covers: basic lock/unlock, reentrant locks, contention, timeout, cancellation, concurrent sessions, case insensitivity, multibyte name boundaries, legacy compatibility, null inputs, error paths. This is thorough.

🔵 Merge Note

The branch is currently BEHIND main and needs a rebase before merge.

已经修改:

  • 调整了 RELEASE_ALL_LOCKS 的语义实现:与mysql./mariadb 对齐后 调整了行为

    • 成功时返回 refcount 总数
    • 不再按唯一锁名计数
    • 遇到 unlock 失败时直接返回 error,不继续释放后续锁
    • 为了让这种 fail-fast 语义稳定,先对锁名排序再释放
  • 对应补了测试:

    • 原有 release_all UT 从唯一锁名数改成 refcount 总数
    • 新增 partial-failure UT,覆盖“前面已释放一部分,后面某把锁失败”的情况
    • test/distributed/cases/function/user_lock.result 已和新语义对齐,例如 a3 + b1 => 4
  • 补了两条 reviewer 提到的非阻塞说明性改动:

    • 注释 userLevelLocks 是 per-process / per-CN 状态
    • 注释 userLevelLockConnectionIDFromTxnID 依赖 NUL 分隔解析的前提

@daviszhen

Copy link
Copy Markdown
Contributor Author

@mergify refresh

@mergify

mergify Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

refresh

✅ Pull request refreshed

@XuPeng-SH
XuPeng-SH merged commit 88dd910 into matrixorigin:main Jun 24, 2026
42 of 43 checks passed
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [x] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#24486

## What this PR does / why we need it:

增加user level locks函数: is_used_lock, release_all_locks.

• IS_USED_LOCK:

- 新增 lockservice 点查接口 GetLockHolder(tableID, row, options)
- 复用现有 GetTxnLock 远程 RPC,并在 response 中增加 Holder / Found,让 holder
查询能路由到真正持有 lock table 的 CN,支持跨 CN。
- user lock 的 row key 保持为 <account>\0<lock_name>;synthetic txn id 中编码
<owner>\0<connection_id>\0<lock_name>。
- IS_USED_LOCK 从 holder 的 synthetic txn id 中解析 connection_id;如果锁不存在或
holder txn id 不是 user lock 格式,则返回 NULL。

RELEASE_ALL_LOCKS:

- 只维护本 session 的本地状态:counts 记录可重入计数,byOwner 记录当前 session 持有的 lock name。
- 只释放当前 session 自己持有的 user locks,通过当前 session 的 owner 和 connection id 生成
synthetic txn id 执行 unlock。
- 返回释放的本地可重入 lock 次数。
- 不再维护本地全局 holders map,避免错误报告或释放其他 session / 其他 CN 持有的锁。
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [x] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#24486

## What this PR does / why we need it:

增加user level locks函数: is_used_lock, release_all_locks.

• IS_USED_LOCK:

- 新增 lockservice 点查接口 GetLockHolder(tableID, row, options)
- 复用现有 GetTxnLock 远程 RPC,并在 response 中增加 Holder / Found,让 holder
查询能路由到真正持有 lock table 的 CN,支持跨 CN。
- user lock 的 row key 保持为 <account>\0<lock_name>;synthetic txn id 中编码
<owner>\0<connection_id>\0<lock_name>。
- IS_USED_LOCK 从 holder 的 synthetic txn id 中解析 connection_id;如果锁不存在或
holder txn id 不是 user lock 格式,则返回 NULL。

RELEASE_ALL_LOCKS:

- 只维护本 session 的本地状态:counts 记录可重入计数,byOwner 记录当前 session 持有的 lock name。
- 只释放当前 session 自己持有的 user locks,通过当前 session 的 owner 和 connection id 生成
synthetic txn id 执行 unlock。
- 返回释放的本地可重入 lock 次数。
- 不再维护本地全局 holders map,避免错误报告或释放其他 session / 其他 CN 持有的锁。
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [x] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#24486

## What this PR does / why we need it:

增加user level locks函数: is_used_lock, release_all_locks.

• IS_USED_LOCK:

- 新增 lockservice 点查接口 GetLockHolder(tableID, row, options)
- 复用现有 GetTxnLock 远程 RPC,并在 response 中增加 Holder / Found,让 holder
查询能路由到真正持有 lock table 的 CN,支持跨 CN。
- user lock 的 row key 保持为 <account>\0<lock_name>;synthetic txn id 中编码
<owner>\0<connection_id>\0<lock_name>。
- IS_USED_LOCK 从 holder 的 synthetic txn id 中解析 connection_id;如果锁不存在或
holder txn id 不是 user lock 格式,则返回 NULL。

RELEASE_ALL_LOCKS:

- 只维护本 session 的本地状态:counts 记录可重入计数,byOwner 记录当前 session 持有的 lock name。
- 只释放当前 session 自己持有的 user locks,通过当前 session 的 owner 和 connection id 生成
synthetic txn id 执行 unlock。
- 返回释放的本地可重入 lock 次数。
- 不再维护本地全局 holders map,避免错误报告或释放其他 session / 其他 CN 持有的锁。

This branch was previously deployed

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

Labels

kind/enhancement kind/feature size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants