Skip to content

Commit

Permalink
Fix retry policy max attempts (#4752)
Browse files Browse the repository at this point in the history
  • Loading branch information
yycptt authored Aug 10, 2023
1 parent 31eada0 commit 97dbe09
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion common/backoff/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (s *RetrySuite) TestThrottleRetryContext() {
throttleRetryPolicy = testThrottleRetryPolicy

policy := NewExponentialRetryPolicy(10 * time.Millisecond).
WithMaximumAttempts(1)
WithMaximumAttempts(2)

// test if throttle retry policy is used on resource exhausted error
attempt := 1
Expand Down
3 changes: 2 additions & 1 deletion common/backoff/retrypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ func (p *ExponentialRetryPolicy) WithMaximumAttempts(maximumAttempts int) *Expon
// ComputeNextDelay returns the next delay interval. This is used by Retrier to delay calling the operation again
func (p *ExponentialRetryPolicy) ComputeNextDelay(elapsedTime time.Duration, numAttempts int) time.Duration {
// Check to see if we ran out of maximum number of attempts
if p.maximumAttempts != noMaximumAttempts && numAttempts > p.maximumAttempts {
// NOTE: if maxAttempts is X, return done when numAttempts == X, otherwise there will be attempt X+1
if p.maximumAttempts != noMaximumAttempts && numAttempts >= p.maximumAttempts {
return done
}

Expand Down
8 changes: 5 additions & 3 deletions common/backoff/retrypolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,18 @@ func (s *RetryPolicySuite) TestExponentialBackoff() {
}

func (s *RetryPolicySuite) TestNumberOfAttempts() {
maxAttempts := 5
policy := createPolicy(time.Second).
WithMaximumAttempts(5)
WithMaximumAttempts(maxAttempts)

r, _ := createRetrier(policy)
var next time.Duration
for i := 0; i < 6; i++ {
for i := 0; i < maxAttempts-1; i++ {
next = r.NextBackOff()
s.NotEqual(done, next)
}

s.Equal(done, next)
s.Equal(done, r.NextBackOff())
}

// Test to make sure relative maximum interval for each retry is honoured
Expand Down
3 changes: 1 addition & 2 deletions service/history/shard/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,8 @@ func (s *contextSuite) TestAcquireShardNonOwnershipLostErrorIsRetried() {
s.mockShard.state = contextStateAcquiring
s.mockShard.acquireShardRetryPolicy = backoff.NewExponentialRetryPolicy(time.Nanosecond).
WithMaximumAttempts(5)
// TODO: make this 5 times instead of 6 when retry policy is fixed
s.mockShardManager.EXPECT().UpdateShard(gomock.Any(), gomock.Any()).
Return(fmt.Errorf("temp error")).Times(6)
Return(fmt.Errorf("temp error")).Times(5)

s.mockShard.acquireShard()

Expand Down

0 comments on commit 97dbe09

Please # to comment.