Skip to content

Commit

Permalink
fix: mixed retry
Browse files Browse the repository at this point in the history
  • Loading branch information
YangruiEmma committed Aug 27, 2024
1 parent c2a5bf6 commit a97cdde
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/retry/mixed_retryer.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ func (r *mixedRetryer) Do(ctx context.Context, rpcCall RPCCallFunc, firstRI rpci
}
doneCount++
isFinishErr := res.err != nil && errors.Is(res.err, kerrors.ErrRPCFinish)
if nonFinishedErrRes == nil || !isFinishErr {
nonFinishedErrRes = res
}
if doneCount < retryTimes+1 {
if isFinishErr {
// There will be only one request (goroutine) pass the `checkRPCState`, others will skip decoding
Expand All @@ -191,9 +194,6 @@ func (r *mixedRetryer) Do(ctx context.Context, rpcCall RPCCallFunc, firstRI rpci
continue
}
}
if nonFinishedErrRes == nil || !isFinishErr {
nonFinishedErrRes = res
}
atomic.StoreInt32(&abort, 1)
recordRetryInfo(nonFinishedErrRes.ri, atomic.LoadInt32(&callTimes), callCosts.String())
return nonFinishedErrRes.ri, false, nonFinishedErrRes.err
Expand Down
28 changes: 28 additions & 0 deletions pkg/retry/mixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ func TestMixedRetry(t *testing.T) {
test.Assert(t, ok)
test.Assert(t, v == remoteTagValue)
})

// case4: RPCFinishErr
t.Run("RPCFinishErr", func(t *testing.T) {
mockErr := errors.New("mock")
retryWithRPCFinishErr := func(callCount int32) RPCCallFunc {
// fails for the first call if callTimes is initialized to 0
return func(ctx context.Context, r Retryer) (rpcinfo.RPCInfo, interface{}, error) {
time.Sleep(50 * time.Millisecond)
ct := atomic.AddInt32(&callCount, 1)
if ct == 1 || ct == 2 {
// first call retry TransErr with specified errCode
return genRPCInfo(), nil, mockErr
} else {
return genRPCInfo(), nil, kerrors.ErrRPCFinish
}
}
}

rc := NewRetryContainer()
mp := NewMixedPolicyWithResultRetry(10, AllErrorRetry())
mp.WithMaxRetryTimes(3)
p := BuildMixedPolicy(mp)
ri = genRPCInfo()
_, ok, err := rc.WithRetryIfNeeded(ctx, &p, retryWithRPCFinishErr(0), ri, nil)
test.Assert(t, err != nil, err)
test.Assert(t, err == mockErr, err)
test.Assert(t, !ok)
})
}

// Assuming the first request returns at 300ms, the second request costs 150ms
Expand Down

0 comments on commit a97cdde

Please sign in to comment.