Repository navigation
txthrottler: don't cancel the state context when restarting the healthcheck stream - #20561
mightbeanshuu wants to merge 2 commits into
Conversation
…hcheck stream closeHealthCheckStream cancelled ts.cancel(), the state context that healthChecksProcessor runs on. That is fine on the deallocate path, but updateHealthCheckCells also calls closeHealthCheckStream to restart the stream when the topology cell list changes. So the first cell-list change cancelled the state context: the re-created healthcheck was bound to a dead context and healthChecksProcessor returned on ts.ctx.Done(), leaving the throttler unable to see replication lag for the life of the tablet. Give the healthcheck stream its own child context so restarting it no longer tears down the state. deallocateResources now cancels the state context explicitly, which closeHealthCheckStream used to do implicitly. Fixes vitessio#20560 Signed-off-by: Anshu <amananshu2004@gmail.com>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7b744d48db
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ts.healthCheck = nil | ||
|
|
||
| // Stop healthChecksProcessor, which runs for the lifetime of ts.ctx. | ||
| ts.cancel() |
There was a problem hiding this comment.
Cancel state before closing healthcheck
When Close() runs with the real HealthCheckImpl, closeHealthCheckStream() closes the subscriber channel before this line cancels ts.ctx; healthChecksProcessor can then wake on ts.healthCheckChan, receive the zero nil value from the closed channel, and panic in StatsUpdate while the tablet is shutting down. Previously the state context was canceled before the channel was closed, so the processor had a chance to exit first; cancel the state context before closing the stream or make the receive handle a closed channel.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, this is a real one I introduced. Since closeHealthCheckStream no longer cancels the state context, the teardown path could close the real HealthCheckImpl subscriber channel while healthChecksProcessor was still selecting on it, and it would then receive a nil TabletHealth and pass it to StatsUpdate.
Fixed in 31f3430 two ways: deallocateResources now cancels the state context before closing the stream, and healthChecksProcessor returns if the receive from healthCheckChan reports the channel is closed. Added TestHealthChecksProcessorReturnsOnClosedChannel, which spins up the processor and closes its channel; it hangs on the old code (nil StatsUpdate loop) and returns cleanly now.
There was a problem hiding this comment.
Pull request overview
Fixes a reliability bug in the vttablet transaction throttler where restarting the healthcheck stream (triggered by topology-discovered cell list changes) accidentally canceled the throttler’s long-lived state context, causing replication-lag monitoring to stop permanently.
Changes:
- Introduces a dedicated per-healthcheck-stream child context/cancel so stream restarts don’t cancel the throttler’s state context.
- Moves state-context cancellation to
deallocateResources()to keep shutdown semantics explicit. - Adds a regression test ensuring stream restart does not cancel the state context.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| go/vt/vttablet/tabletserver/txthrottler/tx_throttler.go | Splits stream lifetime from state lifetime by adding a per-stream cancel; adjusts teardown to cancel state context explicitly. |
| go/vt/vttablet/tabletserver/txthrottler/tx_throttler_test.go | Adds a test to verify restarting the healthcheck stream does not cancel the state context. |
| hcCtx, hcCancel := context.WithCancel(ts.ctx) | ||
| ts.healthCheck, err = healthCheckFactory(hcCtx, topoServer, target.Cell, target.Keyspace, target.Shard, ts.healthCheckCells) | ||
| if err != nil { | ||
| hcCancel() | ||
| return err |
There was a problem hiding this comment.
Thanks. The StatsUpdate(nil) panic you describe is handled by 31f3430 (pushed after this review): healthChecksProcessor now does th, ok := <-ts.healthCheckChan and returns on !ok, so a receive from a closed channel can't reach StatsUpdate.
On the retry point: initHealthCheckStream can only fail if healthCheckFactory fails, and the factory only errors when discovery.NewFilterByShard rejects the keyspace|shard filter. That filter is built from the tablet's keyspace/shard, which don't change between topo cell refreshes, so once the throttler has opened successfully a re-init during a cell refresh won't fail in practice. Worth noting main was actually worse on this path: there closeHealthCheckStream cancelled the state context, so a failed re-init killed the processor outright.
| func (ts *txThrottlerStateImpl) closeHealthCheckStream() { | ||
| if ts.healthCheck == nil { | ||
| return | ||
| } | ||
| ts.cancel() | ||
| ts.healthCheckCancel() | ||
| ts.healthCheck.Close() |
There was a problem hiding this comment.
Same as the other thread: 31f3430 makes healthChecksProcessor return when the receive reports the channel is closed, so reading the previous (closed) channel after Close() no longer risks a nil StatsUpdate. healthCheckCancel is only ever called when healthCheck != nil (the guard above it), and both are set together in initHealthCheckStream, so it can't be nil there.
…ardown Reordered deallocateResources to cancel ts.ctx before closeHealthCheckStream closes the subscriber channel, and made healthChecksProcessor return when that channel is closed. Without this, teardown could close the real HealthCheckImpl channel before the state context was cancelled, so the processor would receive a nil TabletHealth and forward it to StatsUpdate. Signed-off-by: Anshu <amananshu2004@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #20561 +/- ##
===========================================
+ Coverage 69.67% 78.14% +8.47%
===========================================
Files 1614 1 -1613
Lines 216793 183 -216610
===========================================
- Hits 151044 143 -150901
+ Misses 65749 40 -65709
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
mattlord
left a comment
There was a problem hiding this comment.
The core cancellation fix looks correct, but I think two issues remain:
- Wait for
healthChecksProcessorbefore closing its resources
go/vt/vttablet/tabletserver/txthrottler/tx_throttler.go:408
ts.cancel() only signals the processor; waitForTermination currently tracks only updateMaxLag. If the processor already selected the topology-refresh case, it can continue after cancellation. fetchKnownCells converts the canceled request into the local-cell fallback, which can trigger another close/reinitialize cycle. I reproduced this on the current head: updateHealthCheckCells installed a new healthcheck after ts.ctx was canceled.
During teardown that can race with HealthCheck.Close, leak the replacement healthcheck, or let StatsUpdate call RecordReplicationLag after throttler.Close has closed its input channel. I think we should track and join healthChecksProcessor: cancel the state, stop both processors, wait for them, and only then close the current healthcheck and throttler. A regression test could block a refresh during teardown and verify that no replacement stream is created.
- Move healthcheck observability to the replacement instance
go/vt/vttablet/tabletserver/txthrottler/tx_throttler.go:315
The restart replaces ts.healthCheck, but RegisterStats runs only for the initial instance. Those metric callbacks remain bound to the first healthcheck after it has been closed. NewHealthCheck similarly registers /debug/gateway through a package-level sync.Once, so that handler remains bound to the closed instance too. Replication-lag events now continue correctly, but HealthcheckConnections, topology-watcher metrics, and the debug endpoint become empty or stale after the first cell change.
I think we should either restart the watchers without replacing the registered healthcheck or make the published callbacks dereference the current instance, with a test confirming observability follows the replacement. No?
|
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:
If no action is taken within 7 days, this PR will be closed. |
|
This PR was closed because it has been stale for 7 days with no activity. |
Description
When the transaction throttler discovers its cells from the topology (no
--tx-throttler-healthcheck-cells), the first change to the topology cell list silently stops it from ever monitoring replication lag again.healthChecksProcessorruns on the state contextts.ctxand periodically callsupdateHealthCheckCells. When the cell list changes it restarts the healthcheck stream viacloseHealthCheckStream+initHealthCheckStream. The trouble iscloseHealthCheckStreamcalledts.cancel(), which cancels the state context, not a per-stream one. So after the first refresh:initHealthCheckStreamre-created the healthcheck bound to the already-cancelledts.ctx, andhealthChecksProcessorreturned oncase <-ts.ctx.Done()and never ran again.From then on
StatsUpdate/RecordReplicationLagstop, the cached lag goes stale, and the throttler fails open. No panic, no error logged.The fix gives the healthcheck stream its own child context derived from
ts.ctx, so restarting it only tears down the stream and leaves the state context alive.deallocateResourcesnow cancels the state context explicitly, whichcloseHealthCheckStreampreviously did as a side effect on the teardown path.I think this is worth backporting: it's a silent reliability regression on the topology-cells path (introduced in #12477), the fix is small and self-contained, and there's no behavior change for anyone who is not on that path.
Related Issue(s)
Fixes #20560
Checklist
Deployment Notes
None. Behavior only changes for tablets running
--enable-tx-throttlerwithout--tx-throttler-healthcheck-cells, which will now keep monitoring lag across topology cell changes instead of silently going quiet.AI Disclosure
This PR was written with Claude Code; I reviewed the change, the reasoning, and the test, and I can defend them.