Skip to content

perf(txn): drop redundant client.mu lock in closeTxn success path - #25490

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
VioletQwQ-0:violet/perf-close-txn-lock
Jul 10, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
VioletQwQ-0:violet/perf-close-txn-lock

Conversation

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator

What type of PR is this?

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

Which issue(s) this PR fixes:

Fixes #25489

What this PR does / why we need it:

txnClient.closeTxn acquired client.mu twice (client.go:676 and 689). The first Lock/Unlock only "protected" markAllActiveTxnAborted, but that function just sends a non-blocking signal to abortC (client.go:838) and does not touch client.mu — so the first Lock/Unlock was dead weight that every normal txn close paid for.

Under high-concurrency autocommit workloads (sysbench oltp_point_select 1000vu), this redundant lock pair accounted for 706s of mutex wait and 51.62% of closeLocked cum. Drop it; removeActiveTxn uses its own sharded lock, and the mark-before-remove ordering is preserved.

Verification

sysbench oltp_point_select, 10 tables × 1M rows, 1000 vusers, 120s, --skip_trx=on --db-ps-mode=disable, mo-srv-129 standalone (64-core, GOMEMLIMIT=40000MiB), main commit 2a008285e. Before = stock binary, After = this PR.

metric before after Δ
TPS 18,555 18,867 +1.7%
p95 latency 173ms 142ms -18%
mutex total wait (30s) 5,909s 2,030s -66%
sync.Mutex.Unlock flat 3,918s 769s -80%
closeTxn mutex cum 51.6% 25.7% -50%

TPS gain is modest because at 1000vu the CPU is already saturated (~50 cores); the win is in tail latency and lock overhead. Newly surfaced bottleneck: fileservice.IOMerger (25% mutex cum) — next target.

Repro: mo-srv-129:/data/mo-prof/{run_bench.sh,fix_verify.sh}, profiles at /data/mo-prof/pprof/{1000vu,1000vu_fix}/.

Non-goals

  • plan-build DeepCopy allocation (~150GB/30s at 1000vu) — separate issue, needs plan-cache hit-rate check first.
  • fileservice.IOMerger contention — next iteration.

@aptend aptend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the multi-CN / invalid-CN unhappy path as well. I do not see a correctness dependency on the removed client.mu lock: client.mu is local to the txnClient, markAllActiveTxnAborted only does a non-blocking send to abortC, active txn traversal/removal is protected by the sharded active-txn locks, and new txn creation is gated by waitMarkAllActiveAbortedC in the async handler rather than by this close-path lock.

One small non-blocking note: the new comment says the ordering ensures this txn is still marked while it remains in the active map. Since the abort signal is asynchronous, this preserves the original signal-before-remove ordering, but it does not strictly guarantee the closing txn will be included in the later traversal. That is fine for correctness here, but the wording could be softened to avoid overclaiming.

pkg/txn/client tests passed locally: go test ./pkg/txn/client

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed latest head a7d7a38.

I do not see a correctness dependency on the removed first client.mu lock. markAllActiveTxnAborted is only a non-blocking abortC signal; active txn add/remove/iteration are protected by the sharded active-txn locks; user counters and wait queues still stay under client.mu; new txn creation is gated by waitMarkAllActiveAbortedC in the async abort-all handler. The mark-before-remove ordering is preserved, and the old lock did not make the async traversal include the closing txn either.

CI is green. Non-blocking: the new comment could be softened around "this txn is still marked" because the abort signal is asynchronous, but I do not think this blocks the PR.

@VioletQwQ-0
VioletQwQ-0 force-pushed the violet/perf-close-txn-lock branch from a7d7a38 to 10344ad Compare July 9, 2026 11:03
closeTxn acquired client.mu twice (client.go:676 and 689). The first
Lock/Unlock only "protected" markAllActiveTxnAborted, but that function
just sends a non-blocking signal to abortC and does not touch client.mu.
Every normal txn close paid for this redundant lock.

Under sysbench oltp_point_select 1000vu, this pair accounted for 706s
of mutex wait (51.62% of closeLocked cum). Removing it:
- mutex total wait: 5909s -> 2030s (-66%)
- sync.Mutex.Unlock: 3918s -> 769s (-80%)
- p95 latency: 173ms -> 142ms (-18%)
- TPS: 18555 -> 18867 (+1.7%, CPU already saturated)

Closes matrixorigin#25489

Co-Authored-By: Claude <[email protected]>
@VioletQwQ-0
VioletQwQ-0 force-pushed the violet/perf-close-txn-lock branch from 10344ad to 7d666b0 Compare July 9, 2026 11:03
@VioletQwQ-0

VioletQwQ-0 commented Jul 9, 2026 •

Copy link
Copy Markdown
Collaborator Author

Updated the closeTxn comment to avoid overclaiming the asynchronous abort traversal. It now says we preserve the original signal-before-remove ordering, but correctness does not rely on the closing txn being observed by the later active-txn traversal.
Validation: go test ./pkg/txn/client

@mergify

mergify Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-10 05:08 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-07-10 05:08 UTC · at b89e36538323e52483bc942c23b9aff068c1ec65 · squash

This pull request spent 13 seconds in the queue, including 1 second running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • github-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64

@mergify
mergify Bot merged commit 0fcd615 into matrixorigin:main Jul 10, 2026
23 of 24 checks passed
@mergify mergify Bot removed the queued label Jul 10, 2026
@VioletQwQ-0
VioletQwQ-0 deleted the violet/perf-close-txn-lock branch July 13, 2026 03:06
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…trixorigin#25490)

`txnClient.closeTxn` acquired `client.mu` twice (client.go:676 and 689). The first Lock/Unlock only "protected" `markAllActiveTxnAborted`, but that function just sends a non-blocking signal to `abortC` (client.go:838) and does not touch `client.mu` — so the first Lock/Unlock was dead weight that every normal txn close paid for.

Under high-concurrency autocommit workloads (sysbench `oltp_point_select` 1000vu), this redundant lock pair accounted for 706s of mutex wait and 51.62% of `closeLocked` cum. Drop it; `removeActiveTxn` uses its own sharded lock, and the mark-before-remove ordering is preserved.

Approved by: @aptend, @XuPeng-SH, @iamlinjunhong
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…trixorigin#25490)

`txnClient.closeTxn` acquired `client.mu` twice (client.go:676 and 689). The first Lock/Unlock only "protected" `markAllActiveTxnAborted`, but that function just sends a non-blocking signal to `abortC` (client.go:838) and does not touch `client.mu` — so the first Lock/Unlock was dead weight that every normal txn close paid for.

Under high-concurrency autocommit workloads (sysbench `oltp_point_select` 1000vu), this redundant lock pair accounted for 706s of mutex wait and 51.62% of `closeLocked` cum. Drop it; `removeActiveTxn` uses its own sharded lock, and the mark-before-remove ordering is preserved.

Approved by: @aptend, @XuPeng-SH, @iamlinjunhong
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…trixorigin#25490)

`txnClient.closeTxn` acquired `client.mu` twice (client.go:676 and 689). The first Lock/Unlock only "protected" `markAllActiveTxnAborted`, but that function just sends a non-blocking signal to `abortC` (client.go:838) and does not touch `client.mu` — so the first Lock/Unlock was dead weight that every normal txn close paid for.

Under high-concurrency autocommit workloads (sysbench `oltp_point_select` 1000vu), this redundant lock pair accounted for 706s of mutex wait and 51.62% of `closeLocked` cum. Drop it; `removeActiveTxn` uses its own sharded lock, and the mark-before-remove ordering is preserved.

Approved by: @aptend, @XuPeng-SH, @iamlinjunhong

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 size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance] High txn close lock contention under sysbench point_select 1000vu

5 participants