Commit ad924ad
committed
fix(kv): bound verifyLeaderEngine ReadIndex with 5s deadline
verifyLeaderEngine() called engine.VerifyLeader with context.Background(),
so any caller without an upstream context blocked indefinitely on a
ReadIndex round-trip. A single transient stall accumulated callers
permanently because they never timed out and never returned.
Production hit this on 2026-05-08: follower 192.168.0.214 lost its
network route (no route to host, ARP INCOMPLETE), the leader's
ReadIndex completion stalled intermittently, and verify-callers piled
up at roughly 9/sec without bound. After ~37 minutes the leader
(192.168.0.212) held 20,560 goroutines (20,478 in submitRead select,
oldest 39 minutes), CPU pinned at 1870% (Engine.run Ready loop walks
pendingReads O(N) per tick, so the queue feeds back on itself), and
host MemAvailable trended toward 0 until OOM. Each new leader after
failover re-entered the same death spiral.
Affected callers (all use the no-context variant):
- LeaderProxy.Commit / .Abort -- every Redis write hits this
- Coordinate.VerifyLeader / ShardedCoordinator.VerifyLeader[ForKey]
- adapter S3/SQS /healthz/leader handlers (Caddy probes)
- main_admin.go LeaderProbe (admin dashboard /admin/healthz/leader)
- adapter/sqs.go isVerifiedSQSLeader, adapter/s3.go isVerifiedS3Leader
Fix: cap the no-context path at 5s (matching leaderForwardTimeout). On
timeout, callers see context.DeadlineExceeded -- LeaderProxy falls back
to forwardWithRetry as it already does for any verify failure, healthz
handlers report not-leader, and the lock resolver skips this tick.
Self-review (5 lenses):
1. Data loss -- none. The fix only shortens a never-returning wait.
verifyLeaderEngine is a freshness check, not a write path.
2. Concurrency -- the new ctx is local to each call (defer cancel),
no shared state, no lock changes. Engine-side blocking semantics
unchanged; we just stop waiting forever.
3. Performance -- positive. Removes the unbounded goroutine pile-up
and the O(N) pendingReads walk it caused. No new allocations on
the success path beyond the WithTimeout context.
4. Data consistency -- ReadIndex still completes when quorum heartbeats
land within 5s. A timeout means the caller could not confirm
leadership freshness, which the existing "fall through to forward"
path already treats as a soft failure.
5. Test coverage -- kv/raft_engine_test.go pins the regression: a
blockingLeaderView that holds VerifyLeader on its ctx must surface
DeadlineExceeded within 2x verifyLeaderTimeout.
Test: go test -race -count=1 ./kv -- 9.3s, all green.
Future work (separate PRs): plumb real request contexts through
LeaderProxy.Commit/Abort and the healthz handlers so a client-side
deadline cascades naturally instead of relying on this fixed bound.1 parent 303ebe2 commit ad924ad
2 files changed
Lines changed: 83 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
11 | 37 | | |
12 | 38 | | |
13 | 39 | | |
| |||
41 | 67 | | |
42 | 68 | | |
43 | 69 | | |
44 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
45 | 73 | | |
46 | 74 | | |
47 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
0 commit comments