-
Notifications
You must be signed in to change notification settings - Fork 2
perf(lua): verify leader once at startTS, use snapshot reads thereafter #546
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
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
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
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
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,96 @@ | ||
| package adapter | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/bootjp/elastickv/store" | ||
| "github.com/cockroachdb/errors" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/tidwall/redcon" | ||
| ) | ||
|
|
||
| // sentinelErr is the error injected by the stub coordinator to simulate a | ||
| // LinearizableRead failure (e.g. leadership lost, context timeout). | ||
| var sentinelLinearizableErr = errors.New("linearizable read: not leader") | ||
|
|
||
| func newLuaTestServer(linearizableErr error) *RedisServer { | ||
| return &RedisServer{ | ||
| store: store.NewMVCCStore(), | ||
| coordinator: &stubAdapterCoordinator{verifyLeaderErr: linearizableErr}, | ||
| scriptCache: map[string]string{}, | ||
| } | ||
| } | ||
|
|
||
| // TestEval_LinearizableReadFailure verifies that EVAL propagates a | ||
| // LinearizableRead error to the client as a Redis error reply. | ||
| func TestEval_LinearizableReadFailure(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newLuaTestServer(sentinelLinearizableErr) | ||
| conn := &recordingConn{} | ||
| r.eval(conn, redcon.Command{ | ||
| Args: [][]byte{ | ||
| []byte("EVAL"), | ||
| []byte("return 1"), | ||
| []byte("0"), | ||
| }, | ||
| }) | ||
|
|
||
| require.NotEmpty(t, conn.err, "EVAL must write an error when LinearizableRead fails") | ||
| require.Contains(t, conn.err, sentinelLinearizableErr.Error()) | ||
| } | ||
|
|
||
| // TestEvalSHA_LinearizableReadFailure verifies that EVALSHA propagates a | ||
| // LinearizableRead error when the script is cached. | ||
| func TestEvalSHA_LinearizableReadFailure(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newLuaTestServer(sentinelLinearizableErr) | ||
| script := "return 1" | ||
| sha := luaScriptSHA(script) | ||
| r.scriptCache[sha] = script | ||
|
|
||
| conn := &recordingConn{} | ||
| r.evalsha(conn, redcon.Command{ | ||
| Args: [][]byte{ | ||
| []byte("EVALSHA"), | ||
| []byte(sha), | ||
| []byte("0"), | ||
| }, | ||
| }) | ||
|
|
||
| require.NotEmpty(t, conn.err, "EVALSHA must write an error when LinearizableRead fails") | ||
| require.Contains(t, conn.err, sentinelLinearizableErr.Error()) | ||
| } | ||
|
|
||
| // TestExecLuaCompat_LinearizableReadFailure verifies that the compat path | ||
| // (used by RENAME, RPOPLPUSH, LLEN, etc.) propagates LinearizableRead errors. | ||
| func TestExecLuaCompat_LinearizableReadFailure(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newLuaTestServer(sentinelLinearizableErr) | ||
| conn := &recordingConn{} | ||
| r.execLuaCompat(conn, cmdSet, [][]byte{[]byte("k"), []byte("v")}) | ||
|
|
||
| require.NotEmpty(t, conn.err, "execLuaCompat must write an error when LinearizableRead fails") | ||
| require.Contains(t, conn.err, sentinelLinearizableErr.Error()) | ||
| } | ||
|
|
||
| // TestEval_LinearizableReadSuccess verifies the happy path: when | ||
| // LinearizableRead succeeds, EVAL executes the script and returns a result. | ||
| func TestEval_LinearizableReadSuccess(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newLuaTestServer(nil) // no error | ||
| conn := &recordingConn{} | ||
| r.eval(conn, redcon.Command{ | ||
| Args: [][]byte{ | ||
| []byte("EVAL"), | ||
| []byte("return 42"), | ||
| []byte("0"), | ||
| }, | ||
| }) | ||
|
|
||
| require.Empty(t, conn.err, "EVAL must not write an error when LinearizableRead succeeds") | ||
| require.Equal(t, int64(42), conn.int) | ||
| } |
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
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
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.
Update the snapshot-read precondition comments.
The Lua path now gates snapshot reads with
LinearizableRead(ctx), notcoordinator.VerifyLeader(). Keeping the old wording makes the safety contract ambiguous for future callers.📝 Proposed comment update
Also applies to: 467-469
🤖 Prompt for AI Agents