Commit 5f12d5d
authored
fix(kv): bound verifyLeaderEngine ReadIndex with 5s deadline (#745)
## Summary
`verifyLeaderEngine()` called `engine.VerifyLeader` with
`context.Background()`, so callers without an upstream context blocked
indefinitely on a ReadIndex round-trip. A single transient stall
accumulated callers permanently. This caps the no-context path at 5s.
## Production incident — 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 ~9/sec without bound.
After ~37 minutes the leader (192.168.0.212) showed:
- **20,560 goroutines**, 20,478 of them in `etcd.(*Engine).submitRead`
`[select, 35-39 minutes]`
- **CPU 1870%** (`Engine.run` Ready loop walks `pendingReads` O(N) per
tick → queue feeds back on itself)
- **Host MemAvailable** trending toward 0 → OOM
- Each new leader after failover re-entered the same death spiral
Mitigation: `docker restart elastickv` on 212 dropped it to 74% CPU /
163 MiB. 214 was hardware-rebooted and is REACHABLE again. This PR
prevents the next leader from re-entering the spiral.
## Affected callers
All use the no-context `verifyLeaderEngine` variant:
- `kv/leader_proxy.go` — `LeaderProxy.Commit` / `.Abort` (every Redis
write)
- `kv/coordinator.go` — `Coordinate.VerifyLeader`
- `kv/sharded_coordinator.go` — `ShardedCoordinator.VerifyLeader` /
`VerifyLeaderForKey`
- `adapter/s3.go` — `isVerifiedS3Leader` / inline VerifyLeader at line
2291 (healthz)
- `adapter/sqs.go` — `isVerifiedSQSLeader` (healthz)
- `main_admin.go` — `LeaderProbe` callback for `/admin/healthz/leader`
## Failure mode on timeout
`context.DeadlineExceeded` surfaces to the caller. `LeaderProxy` falls
back to `forwardWithRetry` (the existing path for any verify failure).
Healthz handlers report 503 not-leader. Background loops (lock resolver,
HLC lease) skip this tick.
No new infinite loop: even when this node *is* the leader, a
verify-failure → forward path already exists in `LeaderProxy.Commit`;
that path is bounded by `leaderProxyRetryBudget = 5s` and
`maxForwardRetries = 3`.
## 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.
Already-committed proposals are unaffected.
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** — net 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::TestVerifyLeaderEngine_BoundsBlockingReadIndex`
pins the regression: a `blockingLeaderView` that holds `VerifyLeader` on
its ctx must surface `DeadlineExceeded` within `2 *
verifyLeaderTimeout`.
## Test plan
- [x] `go test -race -count=1 ./kv` — 9.3s, all green
- [x] New regression test
`TestVerifyLeaderEngine_BoundsBlockingReadIndex` covers the blocking
case
- [ ] Roll out to 192.168.0.x cluster after merge, watch CPU/Mem panel
for the next 4-6h to confirm no more OOM cascade
## Future work (separate PRs)
Plumb real request contexts through `LeaderProxy.Commit/Abort` and the
healthz handlers so client-side deadlines cascade naturally instead of
relying on this fixed bound. Today the Redis adapter's per-command
deadline doesn't reach `LeaderProxy`; the proxy interface takes
`[]*pb.Request` only.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Implemented timeout bounds for leader verification operations with a
5-second limit to prevent indefinite blocking.
* **Tests**
* Added test to verify leader verification properly handles timeout
scenarios and completes within the expected timeframe under stalled
conditions.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->2 files changed
Lines changed: 94 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 | + | |
11 | 29 | | |
12 | 30 | | |
13 | 31 | | |
| |||
41 | 59 | | |
42 | 60 | | |
43 | 61 | | |
44 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
45 | 65 | | |
46 | 66 | | |
47 | 67 | | |
| |||
| 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 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
0 commit comments