-
Notifications
You must be signed in to change notification settings - Fork 2
fix(lease-read): only invalidate lease on leadership-loss errors #558
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package hashicorp | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/bootjp/elastickv/internal/raftengine" | ||
| "github.com/cockroachdb/errors" | ||
| "github.com/hashicorp/raft" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestTranslateLeadershipErrMatchesRaftEngineSentinel pins the invariant | ||
| // that hashicorp/raft leadership-loss errors are marked against the | ||
| // shared raftengine sentinels. The lease-read fast path in package kv | ||
| // relies on a single cross-backend errors.Is(err, raftengine.ErrNotLeader) | ||
| // check; a future refactor that forgets to mark these errors would | ||
| // silently force every read onto the slow LinearizableRead path. | ||
| func TestTranslateLeadershipErrMatchesRaftEngineSentinel(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| in error | ||
| want error | ||
| }{ | ||
| {"not leader", raft.ErrNotLeader, raftengine.ErrNotLeader}, | ||
| {"leadership lost", raft.ErrLeadershipLost, raftengine.ErrLeadershipLost}, | ||
| {"leadership transfer in progress", raft.ErrLeadershipTransferInProgress, raftengine.ErrLeadershipTransferInProgress}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| out := translateLeadershipErr(tc.in) | ||
| require.True(t, errors.Is(out, tc.want), | ||
| "translated error must errors.Is-match the raftengine sentinel") | ||
| require.True(t, errors.Is(out, tc.in), | ||
| "translated error must retain the original raft sentinel for debugging") | ||
| }) | ||
| } | ||
|
|
||
| t.Run("unrelated error is passed through", func(t *testing.T) { | ||
| t.Parallel() | ||
| orig := errors.New("write conflict") | ||
| out := translateLeadershipErr(orig) | ||
| require.False(t, errors.Is(out, raftengine.ErrNotLeader)) | ||
| require.False(t, errors.Is(out, raftengine.ErrLeadershipLost)) | ||
| require.False(t, errors.Is(out, raftengine.ErrLeadershipTransferInProgress)) | ||
| }) | ||
|
|
||
| t.Run("nil stays nil", func(t *testing.T) { | ||
| t.Parallel() | ||
| require.NoError(t, translateLeadershipErr(nil)) | ||
| }) | ||
| } |
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
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.
この
isLeadershipLossErrorの導入により、DispatchやCommit/Abortにおける不要な lease invalidation を防げるようになりますが、Coordinate.LeaseReadやShardedCoordinator.groupLeaseReadにおいても同様の対応が必要ではないでしょうか。現状、それらのメソッド内でのLinearizableRead失敗時は無条件でinvalidate()が呼ばれています。タイムアウト等のリーダー交代を伴わない一時的なエラーで lease が破棄されると、後続の読み取りが不必要に slow path へ誘導されるため、本 PR の目的である「擬似的な invalidation による悪循環」を完全に解消するためには、読み取りパスの修正も検討すべきと考えます。