Skip to content

admission: add local token reservation to SQLCPUHandle#168231

Merged
trunk-io[bot] merged 2 commits intocockroachdb:masterfrom
wenyihu6:noheuristic
Apr 13, 2026
Merged

admission: add local token reservation to SQLCPUHandle#168231
trunk-io[bot] merged 2 commits intocockroachdb:masterfrom
wenyihu6:noheuristic

Conversation

@wenyihu6
Copy link
Copy Markdown
Contributor

Part of: #166749
Epic: none
Release note: None


admission: extract reportCPU helper

This commit extracts the cumulative CPU counter update into a
standalone reportCPU helper on SQLCPUHandle.
reportAndAcquireConsumedCPU now delegates to reportCPU for the
counter update before proceeding with CTT WorkQueue admission.

Epic: none
Release note: None

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com


admission: add local token reservation to SQLCPUHandle

Each measureAndAdmit call on a SQL goroutine calls WorkQueue.Admit,
acquiring the WorkQueue mutex on every checkpoint (~1024 rows). For
statements with many goroutines, this creates contention on
WorkQueue.mu.

This change introduces a local token reservation in SQLCPUHandle.
When a goroutine consumes CPU, it first attempts to deduct from the
reservation via a lock-free CAS. If the reservation covers the full
deficit, the goroutine returns immediately without calling Admit.
When the reservation is insufficient, the goroutine enters the slow
path: it acquires a turn via a capacity-1 channel (serializing blocking
Admit calls across goroutines), calls Admit for the deficit plus a
buffer (see refillHeuristic), and stores the buffer in the
reservation for future fast-path deductions.

Epic: none
Release note: None

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com

This commit extracts the cumulative CPU counter update into a
standalone `reportCPU` helper on `SQLCPUHandle`.
`reportAndAcquireConsumedCPU` now delegates to `reportCPU` for the
counter update before proceeding with CTT `WorkQueue` admission.

Epic: none
Release note: None

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented Apr 13, 2026

😎 Merged directly without going through the merge queue, as the queue was empty and the PR was up to date with the target branch - details.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@wenyihu6 wenyihu6 requested review from sumeerbhola and tbg April 13, 2026 04:38
@wenyihu6 wenyihu6 marked this pull request as ready for review April 13, 2026 04:39
@wenyihu6 wenyihu6 requested a review from a team as a code owner April 13, 2026 04:39
@wenyihu6 wenyihu6 removed the request for review from a team April 13, 2026 04:39
Copy link
Copy Markdown
Member

@tbg tbg left a comment

Choose a reason for hiding this comment

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

:lgtm:

@tbg reviewed 4 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on sumeerbhola).

@wenyihu6
Copy link
Copy Markdown
Contributor Author

Gonna go ahead and merge this. Lmk if you have comments, and I will address in a follow up @sumeerbhola.

/trunk merge

@wenyihu6
Copy link
Copy Markdown
Contributor Author

/trunk cancel

Copy link
Copy Markdown
Collaborator

@sumeerbhola sumeerbhola left a comment

Choose a reason for hiding this comment

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

:lgtm:

@sumeerbhola reviewed 2 files and made 5 comments.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on wenyihu6).


pkg/util/admission/sql_cpu_handle.go line 230 at r3 (raw file):

// cpuTimeTokenEstimator (see callerSetRequestedCount in Admit). This is
// required for SQL CPU admission, which already knows the exact CPU consumed
// from grunning and does not need/want estimation.

should this say
// REQUIRES: reqCount > 0


pkg/util/admission/sql_cpu_handle.go line 270 at r3 (raw file):

//
// When SQLCPUHandle is closed, any remaining reservation should be
// returned via AdmittedSQLWorkDone. That said, small token leaks are

If this code is never leaking tokens, it seems unnecessary to have multiple sentences about leaks.
Or at least we should make it clear to the reader that this code doesn't leak, even though small leaks are harmless.


pkg/util/admission/sql_cpu_handle.go line 328 at r3 (raw file):

	resp, err := h.wq.Admit(ctx, h.constructWorkInfo(h.refillHeuristic(remaining), false /*noWait*/))
	if err != nil {
		return err

error will only happen on context cancellation, iirc. Please confirm.

So this should be handled also by accounting for the deficit without blocking.


pkg/util/admission/sql_cpu_handle.go line 347 at r3 (raw file):

		// Close already ran and drained reservation. Return the buffer
		// directly.
		if closed && buffer > 0 {

This buffer > 0 could be hoisted before calling the preceding closure.

Each `measureAndAdmit` call on a SQL goroutine calls `WorkQueue.Admit`,
acquiring the `WorkQueue` mutex on every checkpoint (~1024 rows). For
statements with many goroutines, this creates contention on
`WorkQueue.mu`.

This change introduces a local token reservation in `SQLCPUHandle`.
When a goroutine consumes CPU, it first attempts to deduct from the
reservation via a lock-free CAS. If the reservation covers the full
deficit, the goroutine returns immediately without calling `Admit`.
When the reservation is insufficient, the goroutine enters the slow
path: it acquires a turn via a capacity-1 channel (serializing blocking
`Admit` calls across goroutines), calls `Admit` for the deficit plus a
buffer (see `refillHeuristic`), and stores the buffer in the
reservation for future fast-path deductions.

Epic: none
Release note: None

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
Copy link
Copy Markdown
Contributor Author

@wenyihu6 wenyihu6 left a comment

Choose a reason for hiding this comment

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

TFTRs!

/trunk merge

@wenyihu6 made 5 comments.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on sumeerbhola and tbg).


pkg/util/admission/sql_cpu_handle.go line 230 at r3 (raw file):

Previously, sumeerbhola wrote…

should this say
// REQUIRES: reqCount > 0

Done.


pkg/util/admission/sql_cpu_handle.go line 270 at r3 (raw file):

Previously, sumeerbhola wrote…

If this code is never leaking tokens, it seems unnecessary to have multiple sentences about leaks.
Or at least we should make it clear to the reader that this code doesn't leak, even though small leaks are harmless.

Done.


pkg/util/admission/sql_cpu_handle.go line 328 at r3 (raw file):

Previously, sumeerbhola wrote…

error will only happen on context cancellation, iirc. Please confirm.

So this should be handled also by accounting for the deficit without blocking.

Good catch, done.


pkg/util/admission/sql_cpu_handle.go line 347 at r3 (raw file):

Previously, sumeerbhola wrote…

This buffer > 0 could be hoisted before calling the preceding closure.

Done.

@trunk-io trunk-io Bot merged commit 872d1c4 into cockroachdb:master Apr 13, 2026
25 checks passed
@wenyihu6 wenyihu6 deleted the noheuristic branch April 14, 2026 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants