Skip to content

Commit

Permalink
try replica read if leader is exhausted
Browse files Browse the repository at this point in the history
Signed-off-by: rishabh_mittal <[email protected]>
  • Loading branch information
mittalrishabh committed Dec 20, 2024
1 parent ee1cfd2 commit e7babf8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
18 changes: 10 additions & 8 deletions internal/locate/region_request_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func TestRegionCacheStaleRead(t *testing.T) {
leaderRegionValid: true,
leaderAsyncReload: util.Some(true),
leaderSuccessReplica: []string{"z2", "z3"},
leaderSuccessReadType: SuccessStaleRead,
leaderSuccessReadType: SuccessFollowerRead,
followerRegionValid: true,
followerAsyncReload: util.Some(false),
followerSuccessReplica: []string{"z2"},
Expand All @@ -346,7 +346,7 @@ func TestRegionCacheStaleRead(t *testing.T) {
leaderRegionValid: true,
leaderAsyncReload: util.Some(true),
leaderSuccessReplica: []string{"z2", "z3"},
leaderSuccessReadType: SuccessStaleRead,
leaderSuccessReadType: SuccessFollowerRead,
followerRegionValid: true,
followerAsyncReload: util.None[bool](),
followerSuccessReplica: []string{"z2"},
Expand Down Expand Up @@ -434,11 +434,11 @@ func TestRegionCacheStaleRead(t *testing.T) {
leaderRegionValid: true,
leaderAsyncReload: util.Some(true),
leaderSuccessReplica: []string{"z2", "z3"},
leaderSuccessReadType: SuccessStaleRead,
leaderSuccessReadType: SuccessFollowerRead,
followerRegionValid: true,
followerAsyncReload: util.Some(true),
followerSuccessReplica: []string{"z2", "z3"},
followerSuccessReadType: SuccessStaleRead,
followerSuccessReadType: SuccessFollowerRead,
},
{
do: leaderDown,
Expand All @@ -447,11 +447,11 @@ func TestRegionCacheStaleRead(t *testing.T) {
leaderRegionValid: true,
leaderAsyncReload: util.Some(true),
leaderSuccessReplica: []string{"z3"},
leaderSuccessReadType: SuccessStaleRead,
leaderSuccessReadType: SuccessFollowerRead,
followerRegionValid: true,
followerAsyncReload: util.Some(true),
followerSuccessReplica: []string{"z3"},
followerSuccessReadType: SuccessStaleRead,
followerSuccessReadType: SuccessFollowerRead,
},
{
do: leaderDown,
Expand All @@ -460,11 +460,11 @@ func TestRegionCacheStaleRead(t *testing.T) {
leaderRegionValid: true,
leaderAsyncReload: util.Some(true),
leaderSuccessReplica: []string{"z3"},
leaderSuccessReadType: SuccessStaleRead,
leaderSuccessReadType: SuccessFollowerRead,
followerRegionValid: true,
followerAsyncReload: util.Some(true),
followerSuccessReplica: []string{"z3"},
followerSuccessReadType: SuccessStaleRead,
followerSuccessReadType: SuccessFollowerRead,
},
}
tests := []func(*testRegionCacheStaleReadSuite, *RegionCacheTestCase){
Expand Down Expand Up @@ -556,6 +556,8 @@ func testStaleRead(s *testRegionCacheStaleReadSuite, r *RegionCacheTestCase, zon
_, successZone, successReadType := s.extractResp(resp)
find := false
if leaderZone {
funcName := runtime.FuncForPC(reflect.ValueOf(r.do).Pointer()).Name()
fmt.Printf("Function name of r.do(): %s\n", funcName)
s.Equal(r.leaderSuccessReadType, successReadType, msg)
for _, z := range r.leaderSuccessReplica {
if z == successZone {
Expand Down
7 changes: 3 additions & 4 deletions internal/locate/replica_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,10 @@ func (s *ReplicaSelectMixedStrategy) next(selector *replicaSelector) *replica {
func (s *ReplicaSelectMixedStrategy) canSendReplicaRead(selector *replicaSelector) bool {
replicas := selector.replicas
replica := replicas[s.leaderIdx]
liveness := replica.store.getLivenessState()
epochStale := replica.isEpochStale()

if liveness != reachable || epochStale || replica.hasFlag(deadlineErrUsingConfTimeoutFlag) || replica.hasFlag(serverIsBusyFlag) {
// don't overwhelm the leader is busy, timeout, not accessible or stale epoch.
maxAttempt := 1
if !replica.isExhausted(maxAttempt, 0) || replica.hasFlag(deadlineErrUsingConfTimeoutFlag) || replica.hasFlag(serverIsBusyFlag) {
// don't do the replica read if leader is not exhausted or leader has timeout or server busy error.
return false
}
return true
Expand Down
6 changes: 3 additions & 3 deletions internal/locate/replica_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,8 @@ func TestReplicaReadAccessPathByCase2(t *testing.T) {
expect: &accessPathResult{
accessPath: []string{
"{addr: store1, replica-read: false, stale-read: true}",
"{addr: store2, replica-read: false, stale-read: true}",
"{addr: store3, replica-read: false, stale-read: true}"},
"{addr: store2, replica-read: true, stale-read: false}",
"{addr: store3, replica-read: true, stale-read: false}"},
respErr: "",
respRegionError: fakeEpochNotMatch,
backoffCnt: 3,
Expand Down Expand Up @@ -2125,7 +2125,7 @@ func TestReplicaReadAccessPathByStaleReadCase(t *testing.T) {
accessPath: []string{
"{addr: store2, replica-read: false, stale-read: true}",
"{addr: store1, replica-read: false, stale-read: false}",
"{addr: store3, replica-read: false, stale-read: true}",
"{addr: store3, replica-read: true, stale-read: false}",
},
respErr: "",
respRegionError: nil,
Expand Down

0 comments on commit e7babf8

Please sign in to comment.