-
Notifications
You must be signed in to change notification settings - Fork 222
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
add region cache state test & fix some issues of replica selector #910
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 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 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 |
---|---|---|
|
@@ -427,6 +427,8 @@ func (state *tryFollower) next(bo *retry.Backoffer, selector *replicaSelector) ( | |
return rpcCtx, err | ||
} | ||
if state.fallbackFromLeader { | ||
staleRead := false | ||
rpcCtx.contextPatcher.staleRead = &staleRead | ||
replicaRead := true | ||
rpcCtx.contextPatcher.replicaRead = &replicaRead | ||
} | ||
|
@@ -562,6 +564,10 @@ type accessFollower struct { | |
lastIdx AccessIndex | ||
} | ||
|
||
// Follower read will try followers first, if no follower is available, it will fallback to leader. | ||
// Specially, for stale read, it tries local peer(can be either leader or follower), then use snapshot read in the leader, | ||
// if the leader read receive server-is-busy and connection errors, the region cache is still valid, | ||
// and the state will be changed to tryFollower, which will read by replica read. | ||
func (state *accessFollower) next(bo *retry.Backoffer, selector *replicaSelector) (*RPCContext, error) { | ||
resetStaleRead := false | ||
if state.lastIdx < 0 { | ||
|
@@ -609,14 +615,30 @@ func (state *accessFollower) next(bo *retry.Backoffer, selector *replicaSelector | |
// If there is no candidate, fallback to the leader. | ||
if selector.targetIdx < 0 { | ||
leader := selector.replicas[state.leaderIdx] | ||
leaderInvalid := leader.isEpochStale() || state.IsLeaderExhausted(leader) | ||
leaderEpochStale := leader.isEpochStale() | ||
leaderInvalid := leaderEpochStale || state.IsLeaderExhausted(leader) | ||
if len(state.option.labels) > 0 { | ||
logutil.BgLogger().Warn("unable to find stores with given labels", | ||
zap.Uint64("region", selector.region.GetID()), | ||
zap.Bool("leader-invalid", leaderInvalid), | ||
zap.Any("labels", state.option.labels)) | ||
} | ||
if leaderInvalid { | ||
// In stale-read, the request will fallback to leader after the local follower failure. | ||
// If the leader is also unavailable, we can fallback to the follower and use replica-read flag again, | ||
// The remote follower not tried yet, and the local follower can retry without stale-read flag. | ||
if state.isStaleRead { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be printed in the above log so we know if |
||
selector.state = &tryFollower{ | ||
fallbackFromLeader: true, | ||
leaderIdx: state.leaderIdx, | ||
lastIdx: state.leaderIdx, | ||
labels: state.option.labels, | ||
} | ||
if leaderEpochStale { | ||
selector.regionCache.scheduleReloadRegion(selector.region) | ||
} | ||
return nil, stateChanged{} | ||
} | ||
metrics.TiKVReplicaSelectorFailureCounter.WithLabelValues("exhausted").Inc() | ||
selector.invalidateRegion() | ||
return nil, nil | ||
|
@@ -655,13 +677,17 @@ func (state *accessFollower) onSendFailure(bo *retry.Backoffer, selector *replic | |
} | ||
|
||
func (state *accessFollower) isCandidate(idx AccessIndex, replica *replica) bool { | ||
return !replica.isEpochStale() && !replica.isExhausted(1) && | ||
if replica.isEpochStale() || replica.isExhausted(1) || replica.store.getLivenessState() == unreachable { | ||
return false | ||
} | ||
if state.option.leaderOnly && idx == state.leaderIdx { | ||
// The request can only be sent to the leader. | ||
((state.option.leaderOnly && idx == state.leaderIdx) || | ||
// Choose a replica with matched labels. | ||
(!state.option.leaderOnly && (state.tryLeader || idx != state.leaderIdx) && replica.store.IsLabelsMatch(state.option.labels))) && | ||
// Make sure the replica is not unreachable. | ||
replica.store.getLivenessState() != unreachable | ||
return true | ||
} else if !state.tryLeader && idx == state.leaderIdx { | ||
// The request cannot be sent to leader. | ||
return false | ||
} | ||
return replica.store.IsLabelsMatch(state.option.labels) | ||
} | ||
|
||
type invalidStore struct { | ||
|
@@ -930,25 +956,21 @@ func (s *replicaSelector) updateLeader(leader *metapb.Peer) { | |
s.region.invalidate(StoreNotFound) | ||
} | ||
|
||
// For some reason, the leader is unreachable by now, try followers instead. | ||
func (s *replicaSelector) fallback2Follower(ctx *RPCContext) bool { | ||
if ctx == nil || s == nil || s.state == nil { | ||
// For some reasons, the leader is unreachable by now, try followers instead. | ||
// the state is changed in accessFollower.next when leader is unavailable. | ||
func (s *replicaSelector) canFallback2Follower() bool { | ||
if s == nil || s.state == nil { | ||
return false | ||
} | ||
state, ok := s.state.(*accessFollower) | ||
if !ok { | ||
return false | ||
} | ||
if state.lastIdx != state.leaderIdx { | ||
if !state.isStaleRead { | ||
return false | ||
} | ||
s.state = &tryFollower{ | ||
fallbackFromLeader: true, | ||
leaderIdx: state.leaderIdx, | ||
lastIdx: state.leaderIdx, | ||
labels: state.option.labels, | ||
} | ||
return true | ||
// can fallback to follower only when the leader is exhausted. | ||
return state.lastIdx == state.leaderIdx && state.IsLeaderExhausted(s.replicas[state.leaderIdx]) | ||
} | ||
|
||
func (s *replicaSelector) invalidateRegion() { | ||
|
@@ -1680,6 +1702,7 @@ func (s *RegionRequestSender) onRegionError(bo *retry.Backoffer, ctx *RPCContext | |
} | ||
|
||
// This peer is removed from the region. Invalidate the region since it's too stale. | ||
// if the region error is from follower, can we mark the peer unavailable and reload region asynchronously? | ||
if regionErr.GetRegionNotFound() != nil { | ||
s.regionCache.InvalidateCachedRegion(ctx.Region) | ||
return false, nil | ||
|
@@ -1706,7 +1729,7 @@ func (s *RegionRequestSender) onRegionError(bo *retry.Backoffer, ctx *RPCContext | |
logutil.BgLogger().Warn("tikv reports `ServerIsBusy` retry later", | ||
zap.String("reason", regionErr.GetServerIsBusy().GetReason()), | ||
zap.Stringer("ctx", ctx)) | ||
if s.replicaSelector.fallback2Follower(ctx) { | ||
if s.replicaSelector.canFallback2Follower() { | ||
// immediately retry on followers. | ||
return true, nil | ||
} | ||
|
Oops, something went wrong.
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.
Maybe we could refactor the implememtation of
state.IsLeaderExhausted
, cheking if the leader is exausted by the current states instead ofleader.isExhausted(2)
.