Skip to content
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

handle mismatch peer id region error #885 #891

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/locate/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,11 @@ func regionErrorToLabel(e *errorpb.Error) string {
return "flashback_in_progress"
} else if e.GetFlashbackNotPrepared() != nil {
return "flashback_not_prepared"
} else if strings.Contains(e.Message, "mismatch peer id") {
// the error message is like "[components/raftstore/src/store/util.rs:428]: mismatch peer id ? != ?"
// the `mismatch peer id` error does not has a specific error type, so we have to match the error message.
// TODO: add a specific error type for `mismatch peer id`.
return "mismatch_peer_id"
}
return "unknown"
}
Expand All @@ -1411,7 +1416,8 @@ func (s *RegionRequestSender) onRegionError(bo *retry.Backoffer, ctx *RPCContext
}

// NOTE: Please add the region error handler in the same order of errorpb.Error.
metrics.TiKVRegionErrorCounter.WithLabelValues(regionErrorToLabel(regionErr)).Inc()
errLabel := regionErrorToLabel(regionErr)
metrics.TiKVRegionErrorCounter.WithLabelValues(errLabel).Inc()

if notLeader := regionErr.GetNotLeader(); notLeader != nil {
// Retry if error is `NotLeader`.
Expand Down Expand Up @@ -1612,6 +1618,10 @@ func (s *RegionRequestSender) onRegionError(bo *retry.Backoffer, ctx *RPCContext
zap.Stringer("ctx", ctx))

if s.replicaSelector != nil {
if errLabel == "mismatch_peer_id" {
s.replicaSelector.invalidateRegion()
return false, nil
}
// Try the next replica.
return true, nil
}
Expand Down
45 changes: 45 additions & 0 deletions internal/locate/region_request3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,3 +873,48 @@ func (s *testRegionRequestToThreeStoresSuite) TestSendReqWithReplicaSelector() {
s.True(totalAttempts <= 2)
}
}

func (s *testRegionRequestToThreeStoresSuite) TestReplicaReadFallbackToLeaderRegionError() {
regionLoc, err := s.cache.LocateRegionByID(s.bo, s.regionID)
s.Nil(err)
s.NotNil(regionLoc)

s.regionRequestSender.client = &fnClient{fn: func(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (response *tikvrpc.Response, err error) {
select {
case <-ctx.Done():
return nil, errors.New("timeout")
default:
}
// Return `mismatch peer id` when accesses the leader.
if addr == s.cluster.GetStore(s.storeIDs[0]).Address {
return &tikvrpc.Response{Resp: &kvrpcpb.GetResponse{RegionError: &errorpb.Error{
Message: `"[components/raftstore/src/store/util.rs:428]: mismatch peer id 1 != 2"`,
}}}, nil
}
return &tikvrpc.Response{Resp: &kvrpcpb.GetResponse{RegionError: &errorpb.Error{
DataIsNotReady: &errorpb.DataIsNotReady{},
}}}, nil
}}

region := s.cache.getRegionByIDFromCache(regionLoc.Region.GetID())
s.True(region.isValid())

req := tikvrpc.NewReplicaReadRequest(tikvrpc.CmdGet, &kvrpcpb.GetRequest{Key: []byte("key")}, kv.ReplicaReadLeader, nil)
req.ReadReplicaScope = oracle.GlobalTxnScope
req.TxnScope = oracle.GlobalTxnScope
req.EnableStaleRead()
req.ReplicaReadType = kv.ReplicaReadFollower

ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
bo := retry.NewBackoffer(ctx, -1)
s.Nil(err)
resp, err := s.regionRequestSender.SendReq(bo, req, regionLoc.Region, time.Second)
s.Nil(err)
regionErr, err := resp.GetRegionError()
s.Nil(err)
s.Equal(regionErrorToLabel(regionErr), "mismatch_peer_id")
// return non-epoch-not-match region error and the upper layer can auto retry.
s.Nil(regionErr.GetEpochNotMatch())
// after region error returned, the region should be invalidated.
s.False(region.isValid())
}