-
Notifications
You must be signed in to change notification settings - Fork 2
fix(kv): bound verifyLeaderEngine ReadIndex with 5s deadline #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+94
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package kv | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/bootjp/elastickv/internal/raftengine" | ||
| "github.com/cockroachdb/errors" | ||
| ) | ||
|
|
||
| // blockingLeaderView is a LeaderView whose VerifyLeader blocks until ctx is | ||
| // cancelled, modelling the production pathology where ReadIndex stalls | ||
| // because heartbeat acks fail to land. LinearizableRead is similarly | ||
| // well-behaved on cancel; State / Leader are stamped enough to satisfy the | ||
| // callers under test. | ||
| type blockingLeaderView struct{} | ||
|
|
||
| func (blockingLeaderView) State() raftengine.State { return raftengine.StateLeader } | ||
| func (blockingLeaderView) Leader() raftengine.LeaderInfo { return raftengine.LeaderInfo{ID: "self"} } | ||
| func (blockingLeaderView) VerifyLeader(ctx context.Context) error { | ||
| <-ctx.Done() | ||
| return ctx.Err() | ||
| } | ||
| func (blockingLeaderView) LinearizableRead(ctx context.Context) (uint64, error) { | ||
| <-ctx.Done() | ||
| return 0, ctx.Err() | ||
| } | ||
|
|
||
| // TestVerifyLeaderEngine_BoundsBlockingReadIndex pins the regression: if a | ||
| // stalled ReadIndex used to return only when the underlying ctx fired, but | ||
| // callers passed context.Background(), the goroutine pinned forever. After | ||
| // the 2026-05-08 incident this must complete within roughly | ||
| // verifyLeaderTimeout, surfacing context.DeadlineExceeded. | ||
| // | ||
| // Skipped under -short because the whole point is to wait for the deadline | ||
| // to fire; the no-skip path adds verifyLeaderTimeout (5s) to every default | ||
| // `make test` run. | ||
| func TestVerifyLeaderEngine_BoundsBlockingReadIndex(t *testing.T) { | ||
| t.Parallel() | ||
| if testing.Short() { | ||
| t.Skip("skipping: blocks for verifyLeaderTimeout (5s)") | ||
| } | ||
|
|
||
| start := time.Now() | ||
| err := verifyLeaderEngine(blockingLeaderView{}) | ||
| elapsed := time.Since(start) | ||
|
|
||
| if err == nil { | ||
| t.Fatalf("verifyLeaderEngine(blocking) returned nil; expected DeadlineExceeded") | ||
| } | ||
| if !errors.Is(err, context.DeadlineExceeded) { | ||
| t.Fatalf("verifyLeaderEngine(blocking) err = %v; want DeadlineExceeded", err) | ||
| } | ||
| // Lower bound: confirm the engine actually held the call until the | ||
| // deadline fired, not that some other error path returned | ||
| // immediately. Without this, a future regression that returned | ||
| // DeadlineExceeded before doing any work (e.g. a misplaced ctx | ||
| // check before the engine call) would silently pass. | ||
| // | ||
| // Tolerate a 200ms early-return slack so a slow CI scheduler that | ||
| // trips ctx.Done() a hair before the wall clock catches up does | ||
| // not flake. | ||
| const slack = 200 * time.Millisecond | ||
| if elapsed+slack < verifyLeaderTimeout { | ||
| t.Fatalf("verifyLeaderEngine(blocking) returned too early after %s; want >= %s (-%s slack)", elapsed, verifyLeaderTimeout, slack) | ||
| } | ||
| // Upper bound: prove the call returned at all. Generous so a slow | ||
| // CI host does not flake. | ||
| if elapsed > 2*verifyLeaderTimeout { | ||
| t.Fatalf("verifyLeaderEngine(blocking) returned after %s; want <= 2x verifyLeaderTimeout (%s)", elapsed, verifyLeaderTimeout) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure that the test is actually exercising the timeout logic and not returning early for some other reason, consider asserting a minimum elapsed time. This confirms that the blockingLeaderView mock correctly held the call until the context deadline was reached.