Skip to content

loadshed: add priority-aware shedding and query wiring - #947

Open
rcohen2000 wants to merge 28 commits into
bwines/snake-22-02-codelfrom
bwines/snake-22-03-priority-wiring
Open

rcohen2000 wants to merge 28 commits into
bwines/snake-22-02-codelfrom
bwines/snake-22-03-priority-wiring

Conversation

@rcohen2000

@rcohen2000 rcohen2000 commented Sep 18, 2026 •

Copy link
Copy Markdown
Collaborator

👋🏻 This PR description is human-written.

Background / Why?

Snake must preserve Vitess query priorities so overload sheds less important work before latency-sensitive work. Direct vttablet callers can bypass VTGate validation, and malformed priorities previously reached a fixed 100-bucket index, so this PR validates values at the tablet boundary and falls back to each pool's configured default if an invalid value nevertheless reaches Snake. Configured defaults are checked at startup even when the transaction throttler is disabled, preventing a later runtime enablement from exposing an invalid priority. Internal 2PC metadata transactions need to complete during overload to resolve and clean up prepared transactions, so those specific acquisitions explicitly use the existing undroppable priority. Equal-priority requests remain FIFO, while the bucketed index selects the highest numeric droppable priority in constant time.

Testing

new unit tests covering priority parsing, bounds, configured-default fallback, propagation, ordering, bucket-index behavior, undroppable requests, and metrics

AI assisted with development. Every line of code was either written by or carefully reviewed by me :)

@rcohen2000
rcohen2000 requested a review from a team as a code owner September 18, 2026 06:14
@github-actions github-actions Bot added this to the v22.0.4 milestone Sep 18, 2026
@rcohen2000 rcohen2000 changed the title loadshed: wire query priorities and metrics loadshed: add priority-aware shedding and query wiring Sep 18, 2026
@rcohen2000
rcohen2000 added this pull request to stack #950 September 18, 2026 18:07
@bgwines
bgwines removed this pull request from stack #950 September 29, 2026 00:15
@bgwines
bgwines added this pull request to stack #958 September 29, 2026 00:16
@bgwines
bgwines removed this pull request from stack #958 September 29, 2026 23:14
@bgwines
bgwines added this pull request to stack #961 September 29, 2026 23:16
@bgwines
bgwines force-pushed the bwines/snake-22-03-priority-wiring branch from c891eb2 to 4868c9a Compare September 30, 2026 23:06
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

This PR is being marked as stale because it has been open for 30 days with no activity. To rectify, you may do any of the following:

  • Push additional commits to the associated branch.
  • Remove the stale label.
  • Add a comment indicating why it is not stale.

If no action is taken within 7 days, this PR will be closed.

@github-actions github-actions Bot added the Stale label Nov 1, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

This PR was closed because it has been stale for 7 days with no activity.

@github-actions github-actions Bot closed this Nov 8, 2026
@rcohen2000 rcohen2000 reopened this Nov 19, 2026
@bgwines
bgwines force-pushed the bwines/snake-22-03-priority-wiring branch from 7a68a26 to ccf3403 Compare November 19, 2026 23:01
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.01961% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 70.08%. Comparing base (67487e0) to head (ccf3403).

Files with missing lines Patch % Lines
.../vttablet/tabletserver/loadshed/droppable_index.go 96.29% 1 Missing ⚠️
Additional details and impacted files
@@                    Coverage Diff                    @@
##           bwines/snake-22-02-codel     #947   +/-   ##
=========================================================
  Coverage                     70.07%   70.08%           
=========================================================
  Files                          1614     1615    +1     
  Lines                        217187   217250   +63     
=========================================================
+ Hits                         152197   152251   +54     
- Misses                        64990    64999    +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot removed the Stale label Nov 20, 2026
@bgwines
bgwines force-pushed the bwines/snake-22-03-priority-wiring branch from 27ef558 to 0ee2076 Compare November 20, 2026 02:02
@bgwines
bgwines force-pushed the bwines/snake-22-03-priority-wiring branch from 0ee2076 to ad7cc1c Compare November 20, 2026 04:05
rcohen2000 and others added 5 commits November 20, 2026 13:39
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
rcohen2000 and others added 23 commits November 20, 2026 13:39
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Remove transient formatting churn and keep the stress test on the private priority-aware pool path.

Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>

# Conflicts:
#	go/pools/smartconnpool/waitlist.go
#	go/pools/smartconnpool/waitlist_test.go
Signed-off-by: Brett Wines <bwines@slack-corp.com>
…ity-wiring

# Conflicts:
#	go/pools/smartconnpool/stress_test.go
#	go/vt/vttablet/tabletserver/loadshed/snake_stats_test.go
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
…ity-wiring

Signed-off-by: Brett Wines <bwines@slack-corp.com>

# Conflicts:
#	go/vt/vttablet/tabletserver/loadshed/snake_stats_test.go
…ity-wiring

Signed-off-by: Brett Wines <bwines@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
…ity-wiring

Signed-off-by: Brett Wines <bwines@slack-corp.com>
Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
…ity-wiring

Signed-off-by: Brett Wines <bwines@slack-corp.com>

# Conflicts:
#	go/vt/vttablet/tabletserver/loadshed/codelq_test.go
#	go/vt/vttablet/tabletserver/loadshed/dequeue_shed_test.go
#	go/vt/vttablet/tabletserver/loadshed/snake.go
…ity-wiring

Signed-off-by: Brett Wines <bwines@slack-corp.com>
…ity-wiring

Signed-off-by: Brett Wines <bwines@slack-corp.com>
…ity-wiring

Signed-off-by: Brett Wines <bwines@slack-corp.com>
Signed-off-by: Brett Wines <bwines@slack-corp.com>
Signed-off-by: Brett Wines <bwines@slack-corp.com>
…ity-wiring

Co-Authored-By: Codex <svc-devxp-Codex@slack-corp.com>
Signed-off-by: Ross Cohen <rcohen@slack-corp.com>
Signed-off-by: Brett Wines <bwines@slack-corp.com>
@bgwines
bgwines force-pushed the bwines/snake-22-03-priority-wiring branch from ad7cc1c to f1e51cd Compare November 20, 2026 21:48

@tanjinx tanjinx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Priority plumbing and the O(1) droppable index look correct; three design and observability points to weigh, nothing blocking.

0 Should fix, 3 Consider, 2 Nit.

What I verified, by reading the diff and the head files at f1e51cd (no tests run locally):

  • Every queue removal path (Dequeue, Cancel, CancelMatching, Drain, enqueue and timer drops) goes through lockedRemove, so the index stays in lockstep with droppableLen.
  • On the query path, only getConn and StatefulConnectionPool.NewConn reach Snake-enabled pools; the other Get callers (priority 0) are startup-only (inferred from upstream call sites).
  • The expire-worker early return only skips the scan when tryReturnAnyConn would have had nothing to hand over.
  • This replaces the O(n) lockedFindDroppable scan from my #943 nit. The backstop re-arm and cfg.Mode() under wl.mu threads on #943 still apply here.

CI at f1e51cd: the required Unit Test (mysql80) failed in go/vt/vtgate/schema (TestTrackerNoLock) and vttablet/tabletmanager/vreplication (vplayer tests), packages this PR doesn't touch. Unit Test (mysql84) and Unit Test (Race) passed at the same head, so it looks unrelated (inferred). Optional Code Coverage and the Query Serving (Queries) upgrade/downgrade job (vtgate_queries_timeout) also failed.

🤖 Written by Speck, Slack's coding agent, on the author's behalf
💬 Code Channel: Private channel

} else {
var newlyDropped []*list.Element[waiter[C]]
request, newlyDropped = wl.snake.Enqueue(elem)
request, newlyDropped = wl.snake.Enqueue(elem, priority)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider: Priority 0 now reaches Snake as undroppable, and in ModeEnabled the waiter cap is off (L107, L127). Before this PR every new waiter was droppable, so CoDel bounded the queue once the cap was lifted. Now any PRIORITY=0 query, and every tx-pool waiter on a tablet running --tx-throttler-default-priority=0 (the tx pools inherit that flag at connpool/pool.go L54), queues with neither a cap nor shedding, bounded only by its context deadline. 0 is also the tx throttler's never-throttle value, so callers who set it to avoid throttling now skip shedding and the cap too (inferred). Consider keeping the waiter cap for undroppable waiters in enabled mode, or giving the tx pools their own default flag like --loadshed-oltp-read-default-priority.

🤖 Written by Speck, Slack's coding agent, on the author's behalf
💬 Code Channel: Private channel

priority = s.cfg.DefaultPriority
}
if recordAcquire && s.acquireByPriority != nil {
s.acquireByPriority.Add([]string{strconv.Itoa(priority)}, 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider: This runs under wl.mu on every Snake enqueue, and CountersWithMultiLabels.Add takes the counter's own mutex and writes a map (go/stats/counters.go L50-L54). Every scrape's Counts() copies that map under the same mutex, so a scrape can stall enqueues during overload. strconv.Itoa also allocates for 100, the OLTP-read default, since Go only caches 0-99. A [sqlparser.MaxPriorityValue + 1]atomic.Int64 per metric, exported with NewCountersFuncWithMultiLabels, keeps this lock-free; the same applies to shedByPriority at L307. Not benchmarked (inferred).

🤖 Written by Speck, Slack's coding agent, on the author's behalf
💬 Code Channel: Private channel

return s.ShedCount()
})
s.shedByPriority = exporter.NewCountersWithMultiLabels(prefix+"ShedByPriority", "Cumulative requests shed by the Snake load shedder, labeled by query priority (\"0\" undroppable, \"1\" most important .. \"100\" least); sum equals ShedCount", []string{"priority"})
s.acquireByPriority = exporter.NewCountersWithMultiLabels(prefix+"AcquireByPriority", "Cumulative Acquire attempts (offered load) labeled by query priority (\"0\" undroppable, \"1\" most important .. \"100\" least); ShedByPriority/AcquireByPriority is the per-priority shed rate", []string{"priority"})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider: This counter isn't offered load. It's bumped only in Snake.enqueue (snake.go L130-L131), which runs after get/getWithSetting found no idle or new connection, and only while the pool is in shadow or enabled mode (waitlist.go L137-L141). Requests served on the fast path never count, so ShedByPriority / AcquireByPriority is the shed rate among requests that queued, which overstates the per-priority shed rate of traffic. Rewording this help text and the field comment at snake.go L60-L63 (or renaming to something like QueuedByPriority) keeps dashboards honest; if offered load is the goal, count in ConnPool.GetWithPriority instead.

🤖 Written by Speck, Slack's coding agent, on the author's behalf
💬 Code Channel: Private channel

// bucket and list node on the request for O(1) removal. Must not be called for
// an undroppable request.
func (idx *droppableIndex[T]) insert(req *Request[T]) {
b := req.priority - 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: insert trusts that the priority is 1-100. Snake.enqueue validates the caller's value but substitutes s.cfg.DefaultPriority unchecked (snake.go L128). Verify covers today's two flags, but a SnakeConfig built anywhere else with an out-of-range default panics here while wl.mu is held, and a negative one also counts as droppable because isDroppable is != 0. Separately, occ [2]uint64 covers only 128 buckets. A range check in NewSnake plus a compile-time guard such as const _ = uint(128 - numPriorityBuckets) would pin both.

🤖 Written by Speck, Slack's coding agent, on the author's behalf
💬 Code Channel: Private channel

return wl.numWaiting() == 7
}, time.Second, time.Millisecond)

time.Sleep(2 * time.Millisecond)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: This test passes only if the waiters all enqueue within testPoolConfig's 1ms interval. Once five droppable waiters are queued, any enqueue more than 1ms after the first droppable one makes lockedEnqueueAdvance shed on the spot (whichever is lowest at that moment, possibly 60 or 70), and the numWaiting() == 7 wait above never succeeds. Race CI passed at this head, so it's rare (inferred). CLAUDE.md asks for assert.Eventually over sleeps, but polling runDropTimer() doesn't work today: a fire before dropNextNs disarms the backstop without re-arming (the #943 re-arm thread). A per-test interval well above goroutine start skew, for example a field on testPoolConfig, would close the window.

🤖 Written by Speck, Slack's coding agent, on the author's behalf
💬 Code Channel: Private channel

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