Skip to content

Commit 6db9e19

Browse files
committed
Fix retrypolicy bug, see issue #645
1 parent 56d2a8a commit 6db9e19

File tree

11 files changed

+354
-12
lines changed

11 files changed

+354
-12
lines changed

core/sdk-core/src/main/java/software/amazon/awssdk/core/retry/RetryPolicy.java

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import software.amazon.awssdk.core.retry.conditions.AndRetryCondition;
2424
import software.amazon.awssdk.core.retry.conditions.MaxNumberOfRetriesCondition;
2525
import software.amazon.awssdk.core.retry.conditions.RetryCondition;
26+
import software.amazon.awssdk.utils.ToString;
2627
import software.amazon.awssdk.utils.builder.CopyableBuilder;
2728
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
2829

@@ -54,7 +55,12 @@ private RetryPolicy(BuilderImpl builder) {
5455
this.throttlingBackoffStrategy = builder.throttlingBackoffStrategy;
5556
this.numRetries = builder.numRetries;
5657
this.retryConditionFromBuilder = builder.retryCondition;
57-
this.retryCondition = AndRetryCondition.create(MaxNumberOfRetriesCondition.create(numRetries), retryConditionFromBuilder);
58+
if (numRetries == 0) {
59+
this.retryCondition = retryConditionFromBuilder;
60+
} else {
61+
this.retryCondition = AndRetryCondition.create(MaxNumberOfRetriesCondition.create(numRetries),
62+
retryConditionFromBuilder);
63+
}
5864
}
5965

6066
public RetryCondition retryCondition() {
@@ -74,7 +80,52 @@ public Integer numRetries() {
7480
}
7581

7682
public Builder toBuilder() {
77-
return builder().numRetries(numRetries).retryCondition(retryConditionFromBuilder).backoffStrategy(backoffStrategy);
83+
return builder().numRetries(numRetries)
84+
.retryCondition(retryConditionFromBuilder)
85+
.backoffStrategy(backoffStrategy)
86+
.throttlingBackoffStrategy(throttlingBackoffStrategy);
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return ToString.builder("RetryPolicy")
92+
.add("numRetries", numRetries)
93+
.add("retryCondition", retryCondition)
94+
.add("backoffStrategy", backoffStrategy)
95+
.add("throttlingBackoffStrategy", throttlingBackoffStrategy)
96+
.build();
97+
}
98+
99+
@Override
100+
public boolean equals(Object o) {
101+
if (this == o) {
102+
return true;
103+
}
104+
if (o == null || getClass() != o.getClass()) {
105+
return false;
106+
}
107+
108+
final RetryPolicy that = (RetryPolicy) o;
109+
110+
if (!retryCondition.equals(that.retryCondition)) {
111+
return false;
112+
}
113+
if (!backoffStrategy.equals(that.backoffStrategy)) {
114+
return false;
115+
}
116+
if (!throttlingBackoffStrategy.equals(that.throttlingBackoffStrategy)) {
117+
return false;
118+
}
119+
return numRetries.equals(that.numRetries);
120+
}
121+
122+
@Override
123+
public int hashCode() {
124+
int result = retryCondition.hashCode();
125+
result = 31 * result + backoffStrategy.hashCode();
126+
result = 31 * result + throttlingBackoffStrategy.hashCode();
127+
result = 31 * result + numRetries.hashCode();
128+
return result;
78129
}
79130

80131
public static Builder builder() {
@@ -84,14 +135,17 @@ public static Builder builder() {
84135
public static RetryPolicy defaultRetryPolicy() {
85136
return RetryPolicy.builder()
86137
.backoffStrategy(BackoffStrategy.defaultStrategy())
138+
.throttlingBackoffStrategy(BackoffStrategy.defaultThrottlingStrategy())
87139
.numRetries(SdkDefaultRetrySetting.DEFAULT_MAX_RETRIES)
88140
.retryCondition(RetryCondition.defaultRetryCondition())
89141
.build();
90142
}
91143

92144
public static RetryPolicy none() {
93145
return RetryPolicy.builder()
146+
.numRetries(0)
94147
.backoffStrategy(BackoffStrategy.none())
148+
.throttlingBackoffStrategy(BackoffStrategy.none())
95149
.retryCondition(RetryCondition.none())
96150
.build();
97151
}
@@ -105,6 +159,10 @@ public interface Builder extends CopyableBuilder<Builder, RetryPolicy> {
105159

106160
BackoffStrategy backoffStrategy();
107161

162+
Builder throttlingBackoffStrategy(BackoffStrategy backoffStrategy);
163+
164+
BackoffStrategy throttlingBackoffStrategy();
165+
108166
Builder retryCondition(RetryCondition retryCondition);
109167

110168
RetryCondition retryCondition();
@@ -155,13 +213,19 @@ public BackoffStrategy backoffStrategy() {
155213
return backoffStrategy;
156214
}
157215

158-
public Builder throttlingBackoffStrategy(BackoffStrategy backoffStrategy) {
159-
this.backoffStrategy = backoffStrategy;
216+
@Override
217+
public Builder throttlingBackoffStrategy(BackoffStrategy throttlingBackoffStrategy) {
218+
this.throttlingBackoffStrategy = throttlingBackoffStrategy;
160219
return this;
161220
}
162221

222+
@Override
163223
public BackoffStrategy throttlingBackoffStrategy() {
164-
return backoffStrategy;
224+
return throttlingBackoffStrategy;
225+
}
226+
227+
public void setThrottlingBackoffStrategy(BackoffStrategy throttlingBackoffStrategy) {
228+
this.throttlingBackoffStrategy = throttlingBackoffStrategy;
165229
}
166230

167231
@Override

core/sdk-core/src/main/java/software/amazon/awssdk/core/retry/backoff/EqualJitterBackoffStrategy.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Random;
2222
import software.amazon.awssdk.annotations.SdkPublicApi;
2323
import software.amazon.awssdk.core.retry.RetryPolicyContext;
24+
import software.amazon.awssdk.utils.ToString;
2425
import software.amazon.awssdk.utils.builder.CopyableBuilder;
2526
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
2627

@@ -122,4 +123,36 @@ public EqualJitterBackoffStrategy build() {
122123
return new EqualJitterBackoffStrategy(this);
123124
}
124125
}
126+
127+
@Override
128+
public boolean equals(Object o) {
129+
if (this == o) {
130+
return true;
131+
}
132+
if (o == null || getClass() != o.getClass()) {
133+
return false;
134+
}
135+
136+
final EqualJitterBackoffStrategy that = (EqualJitterBackoffStrategy) o;
137+
138+
if (!baseDelay.equals(that.baseDelay)) {
139+
return false;
140+
}
141+
return maxBackoffTime.equals(that.maxBackoffTime);
142+
}
143+
144+
@Override
145+
public int hashCode() {
146+
int result = baseDelay.hashCode();
147+
result = 31 * result + maxBackoffTime.hashCode();
148+
return result;
149+
}
150+
151+
@Override
152+
public String toString() {
153+
return ToString.builder("EqualJitterBackoffStrategy")
154+
.add("baseDelay", baseDelay)
155+
.add("maxBackoffTime", maxBackoffTime)
156+
.build();
157+
}
125158
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/retry/backoff/FixedDelayBackoffStrategy.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.time.Duration;
2121
import software.amazon.awssdk.annotations.SdkPublicApi;
2222
import software.amazon.awssdk.core.retry.RetryPolicyContext;
23+
import software.amazon.awssdk.utils.ToString;
2324

2425
/**
2526
* Simple backoff strategy that always uses a fixed delay for the delay before the next retry attempt.
@@ -41,4 +42,30 @@ public Duration computeDelayBeforeNextRetry(RetryPolicyContext context) {
4142
public static FixedDelayBackoffStrategy create(Duration fixedBackoff) {
4243
return new FixedDelayBackoffStrategy(fixedBackoff);
4344
}
45+
46+
@Override
47+
public boolean equals(Object o) {
48+
if (this == o) {
49+
return true;
50+
}
51+
if (o == null || getClass() != o.getClass()) {
52+
return false;
53+
}
54+
55+
final FixedDelayBackoffStrategy that = (FixedDelayBackoffStrategy) o;
56+
57+
return fixedBackoff.equals(that.fixedBackoff);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return fixedBackoff.hashCode();
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return ToString.builder("FixedDelayBackoffStrategy")
68+
.add("fixedBackoff", fixedBackoff)
69+
.build();
70+
}
4471
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/retry/backoff/FullJitterBackoffStrategy.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Random;
2222
import software.amazon.awssdk.annotations.SdkPublicApi;
2323
import software.amazon.awssdk.core.retry.RetryPolicyContext;
24+
import software.amazon.awssdk.utils.ToString;
2425
import software.amazon.awssdk.utils.builder.CopyableBuilder;
2526
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
2627

@@ -119,4 +120,36 @@ public FullJitterBackoffStrategy build() {
119120
return new FullJitterBackoffStrategy(this);
120121
}
121122
}
123+
124+
@Override
125+
public boolean equals(Object o) {
126+
if (this == o) {
127+
return true;
128+
}
129+
if (o == null || getClass() != o.getClass()) {
130+
return false;
131+
}
132+
133+
final FullJitterBackoffStrategy that = (FullJitterBackoffStrategy) o;
134+
135+
if (!baseDelay.equals(that.baseDelay)) {
136+
return false;
137+
}
138+
return maxBackoffTime.equals(that.maxBackoffTime);
139+
}
140+
141+
@Override
142+
public int hashCode() {
143+
int result = baseDelay.hashCode();
144+
result = 31 * result + maxBackoffTime.hashCode();
145+
return result;
146+
}
147+
148+
@Override
149+
public String toString() {
150+
return ToString.builder("FullJitterBackoffStrategy")
151+
.add("baseDelay", baseDelay)
152+
.add("maxBackoffTime", maxBackoffTime)
153+
.build();
154+
}
122155
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/retry/conditions/AndRetryCondition.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import software.amazon.awssdk.annotations.SdkPublicApi;
2222
import software.amazon.awssdk.core.retry.RetryPolicyContext;
23+
import software.amazon.awssdk.utils.ToString;
2324
import software.amazon.awssdk.utils.Validate;
2425

2526
/**
@@ -45,4 +46,30 @@ public boolean shouldRetry(RetryPolicyContext context) {
4546
public static AndRetryCondition create(RetryCondition... conditions) {
4647
return new AndRetryCondition(conditions);
4748
}
49+
50+
@Override
51+
public boolean equals(Object o) {
52+
if (this == o) {
53+
return true;
54+
}
55+
if (o == null || getClass() != o.getClass()) {
56+
return false;
57+
}
58+
59+
final AndRetryCondition that = (AndRetryCondition) o;
60+
61+
return conditions != null ? conditions.equals(that.conditions) : that.conditions == null;
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return conditions != null ? conditions.hashCode() : 0;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return ToString.builder("AndRetryCondition")
72+
.add("conditions", conditions)
73+
.build();
74+
}
4875
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/retry/conditions/MaxNumberOfRetriesCondition.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import software.amazon.awssdk.annotations.SdkPublicApi;
1919
import software.amazon.awssdk.core.retry.RetryPolicyContext;
20+
import software.amazon.awssdk.utils.ToString;
2021
import software.amazon.awssdk.utils.Validate;
2122

2223
/**
@@ -39,4 +40,30 @@ public boolean shouldRetry(RetryPolicyContext context) {
3940
public static MaxNumberOfRetriesCondition create(int maxNumberOfRetries) {
4041
return new MaxNumberOfRetriesCondition(maxNumberOfRetries);
4142
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o) {
47+
return true;
48+
}
49+
if (o == null || getClass() != o.getClass()) {
50+
return false;
51+
}
52+
53+
final MaxNumberOfRetriesCondition that = (MaxNumberOfRetriesCondition) o;
54+
55+
return maxNumberOfRetries == that.maxNumberOfRetries;
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return maxNumberOfRetries;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return ToString.builder("MaxNumberOfRetriesCondition")
66+
.add("maxNumberOfRetries", maxNumberOfRetries)
67+
.build();
68+
}
4269
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/retry/conditions/OrRetryCondition.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import software.amazon.awssdk.annotations.SdkPublicApi;
2222
import software.amazon.awssdk.core.retry.RetryPolicyContext;
23+
import software.amazon.awssdk.utils.ToString;
2324

2425
/**
2526
* Composite retry condition that evaluates to true if any containing condition evaluates to true.
@@ -44,4 +45,30 @@ public boolean shouldRetry(RetryPolicyContext context) {
4445
public static OrRetryCondition create(RetryCondition... conditions) {
4546
return new OrRetryCondition(conditions);
4647
}
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (this == o) {
52+
return true;
53+
}
54+
if (o == null || getClass() != o.getClass()) {
55+
return false;
56+
}
57+
58+
final OrRetryCondition that = (OrRetryCondition) o;
59+
60+
return conditions.equals(that.conditions);
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return conditions.hashCode();
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return ToString.builder("OrRetryCondition")
71+
.add("conditions", conditions)
72+
.build();
73+
}
4774
}

0 commit comments

Comments
 (0)