Skip to content

Commit a80cd88

Browse files
committed
Update methods names and javadoc
1 parent 16fd5e0 commit a80cd88

File tree

5 files changed

+78
-73
lines changed

5 files changed

+78
-73
lines changed

common/src/main/java/tech/ydb/common/retry/RetryConfig.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,36 @@ public interface RetryConfig {
1818
* @param code status code to check
1919
* @return policy of retries or {@code null} if the status code is not retryable
2020
*/
21-
RetryPolicy isStatusRetryable(StatusCode code);
21+
RetryPolicy getStatusCodeRetryPolicy(StatusCode code);
2222

2323
/**
2424
* Returns retry policy for the given exception and {@code null} if that exception is not retryable
2525
*
2626
* @param th exception to check
2727
* @return policy of retries or {@code null} if the exception is not retryable
2828
*/
29-
default RetryPolicy isThrowableRetryable(Throwable th) {
29+
default RetryPolicy getThrowableRetryPolicy(Throwable th) {
3030
for (Throwable ex = th; ex != null; ex = ex.getCause()) {
3131
if (ex instanceof UnexpectedResultException) {
32-
return isStatusRetryable(((UnexpectedResultException) ex).getStatus().getCode());
32+
return getStatusCodeRetryPolicy(((UnexpectedResultException) ex).getStatus().getCode());
3333
}
3434
}
3535
return null;
3636
}
3737

3838
/**
39-
* Retries a non idempotent operation forever with default exponential delay
39+
* Infinity retries with default exponential delay.<br>That policy <b>does not</b> retries <i>conditionally</i>
40+
* retryable errors so it can be used for both as idempotent and non idempotent operations
41+
*
4042
* @return retry configuration object
4143
*/
4244
static RetryConfig retryForever() {
4345
return newConfig().retryForever();
4446
}
4547

4648
/**
47-
* Retries a non idempotent operation with default exponential until the specified elapsed milliseconds expire
49+
* Retries until the specified elapsed milliseconds expire.<br>That policy <b>does not</b> retries
50+
* <i>conditionally</i> retryable errors so it can be used for both as idempotent and non idempotent operations
4851
* @param maxElapsedMs maximum timeout for retries
4952
* @return retry configuration object
5053
*/
@@ -53,20 +56,22 @@ static RetryConfig retryUntilElapsed(long maxElapsedMs) {
5356
}
5457

5558
/**
56-
* Retries an idempotent operation forever with default exponential delay
59+
* Infinity retries with default exponential delay.<br>That policy <b>does</b> retries <i>conditionally</i>
60+
* retryable errors so it can be used <b>ONLY</b> for idempotent operations
5761
* @return retry configuration object
5862
*/
5963
static RetryConfig idempotentRetryForever() {
60-
return newConfig().retryIdempotent(true).retryForever();
64+
return newConfig().retryConditionallyRetryableErrors(true).retryForever();
6165
}
6266

6367
/**
64-
* Retries an idempotent operation with default exponential until the specified elapsed milliseconds expire
68+
* Retries until the specified elapsed milliseconds expire.<br>That policy <b>does</b> retries
69+
* <i>conditionally</i> retryable errors so it can be used <b>ONLY</b> for idempotent operations
6570
* @param maxElapsedMs maximum timeout for retries
6671
* @return retry configuration object
6772
*/
6873
static RetryConfig idempotentRetryUntilElapsed(long maxElapsedMs) {
69-
return newConfig().retryIdempotent(true).retryUntilElapsed(maxElapsedMs);
74+
return newConfig().retryConditionallyRetryableErrors(true).retryUntilElapsed(maxElapsedMs);
7075
}
7176

7277
/**
@@ -86,7 +91,7 @@ static Builder newConfig() {
8691
}
8792

8893
interface Builder {
89-
Builder retryIdempotent(boolean retry);
94+
Builder retryConditionallyRetryableErrors(boolean retry);
9095
Builder retryNotFound(boolean retry);
9196
Builder withSlowBackoff(long backoff, int ceiling);
9297
Builder withFastBackoff(long backoff, int ceiling);

common/src/main/java/tech/ydb/common/retry/YdbRetryBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class YdbRetryBuilder implements RetryConfig.Builder {
1515
private int slowCeiling = 6;
1616

1717
@Override
18-
public YdbRetryBuilder retryIdempotent(boolean retry) {
18+
public YdbRetryBuilder retryConditionallyRetryableErrors(boolean retry) {
1919
this.idempotent = retry;
2020
return this;
2121
}

common/src/main/java/tech/ydb/common/retry/YdbRetryConfig.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
* @author Aleksandr Gorshenin
88
*/
99
class YdbRetryConfig implements RetryConfig {
10-
private final boolean idempotent;
10+
private final boolean retryConditionally;
1111
private final boolean retryNotFound;
1212
private final RetryPolicy immediatelly;
1313
private final RetryPolicy fast;
1414
private final RetryPolicy slow;
1515

16-
YdbRetryConfig(boolean idempotent, boolean notFound, RetryPolicy immediatelly, RetryPolicy fast, RetryPolicy slow) {
17-
this.idempotent = idempotent;
16+
YdbRetryConfig(boolean conditionally, boolean notFound, RetryPolicy instant, RetryPolicy fast, RetryPolicy slow) {
17+
this.retryConditionally = conditionally;
1818
this.retryNotFound = notFound;
19-
this.immediatelly = immediatelly;
19+
this.immediatelly = instant;
2020
this.fast = fast;
2121
this.slow = slow;
2222
}
2323

2424
@Override
25-
public RetryPolicy isStatusRetryable(StatusCode code) {
25+
public RetryPolicy getStatusCodeRetryPolicy(StatusCode code) {
2626
if (code == null) {
2727
return null;
2828
}
@@ -43,14 +43,14 @@ public RetryPolicy isStatusRetryable(StatusCode code) {
4343
case CLIENT_RESOURCE_EXHAUSTED:
4444
return slow;
4545

46-
// Conditionally retry
46+
// Conditionally retryable statuses
4747
case CLIENT_CANCELLED:
4848
case CLIENT_INTERNAL_ERROR:
4949
case TRANSPORT_UNAVAILABLE:
5050
case UNAVAILABLE:
51-
return idempotent ? fast : null;
51+
return retryConditionally ? fast : null;
5252

53-
// Not found retry
53+
// Not found has special flag for retries
5454
case NOT_FOUND:
5555
return retryNotFound ? fast : null;
5656

common/src/test/java/tech/ydb/common/retry/RetryConfigTest.java

+50-50
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ private void assertDuration(long from, long to, long ms) {
2121
public void nullStatusesTest() {
2222
RetryConfig config = RetryConfig.retryForever();
2323

24-
Assert.assertNull(config.isThrowableRetryable(null));
25-
Assert.assertNull(config.isStatusRetryable(null));
24+
Assert.assertNull(config.getThrowableRetryPolicy(null));
25+
Assert.assertNull(config.getStatusCodeRetryPolicy(null));
2626
}
2727

2828
@Test
2929
public void throwableRetriesTest() {
3030
RetryConfig config = RetryConfig.retryUntilElapsed(1000);
3131

32-
Assert.assertNull(config.isThrowableRetryable(new RuntimeException("test message")));
33-
Assert.assertNull(config.isThrowableRetryable(new Exception("1", new RuntimeException("2"))));
32+
Assert.assertNull(config.getThrowableRetryPolicy(new RuntimeException("test message")));
33+
Assert.assertNull(config.getThrowableRetryPolicy(new Exception("1", new RuntimeException("2"))));
3434

35-
RetryPolicy immediatelly = config.isStatusRetryable(StatusCode.BAD_SESSION);
36-
RetryPolicy fast = config.isStatusRetryable(StatusCode.NOT_FOUND);
35+
RetryPolicy immediatelly = config.getStatusCodeRetryPolicy(StatusCode.BAD_SESSION);
36+
RetryPolicy fast = config.getStatusCodeRetryPolicy(StatusCode.NOT_FOUND);
3737

38-
Assert.assertEquals(immediatelly, config.isThrowableRetryable(
38+
Assert.assertEquals(immediatelly, config.getThrowableRetryPolicy(
3939
new UnexpectedResultException("base", Status.of(StatusCode.BAD_SESSION)))
4040
);
41-
Assert.assertEquals(immediatelly, config.isThrowableRetryable(new Exception("base",
41+
Assert.assertEquals(immediatelly, config.getThrowableRetryPolicy(new Exception("base",
4242
new UnexpectedResultException("cause", Status.of(StatusCode.SESSION_BUSY)))
4343
));
44-
Assert.assertEquals(fast, config.isThrowableRetryable(new Exception("base",
44+
Assert.assertEquals(fast, config.getThrowableRetryPolicy(new Exception("base",
4545
new UnexpectedResultException("cause", Status.of(StatusCode.NOT_FOUND)))
4646
));
4747
}
@@ -51,7 +51,7 @@ public void noRetryPolicyTest() {
5151
RetryConfig config = RetryConfig.noRetries();
5252
// unretrayable
5353
for (StatusCode code: StatusCode.values()) {
54-
Assert.assertNull(config.isStatusRetryable(code));
54+
Assert.assertNull(config.getStatusCodeRetryPolicy(code));
5555
}
5656
}
5757

@@ -60,80 +60,80 @@ public void nonIdempotentRetryPolicyTest() {
6060
RetryConfig config = RetryConfig.retryForever();
6161

6262
// unretrayable
63-
Assert.assertNull(config.isStatusRetryable(StatusCode.SCHEME_ERROR));
64-
Assert.assertNull(config.isStatusRetryable(StatusCode.ALREADY_EXISTS));
65-
Assert.assertNull(config.isStatusRetryable(StatusCode.UNAUTHORIZED));
66-
Assert.assertNull(config.isStatusRetryable(StatusCode.UNAVAILABLE));
67-
Assert.assertNull(config.isStatusRetryable(StatusCode.TRANSPORT_UNAVAILABLE));
68-
Assert.assertNull(config.isStatusRetryable(StatusCode.CLIENT_CANCELLED));
69-
Assert.assertNull(config.isStatusRetryable(StatusCode.CLIENT_INTERNAL_ERROR));
70-
Assert.assertNull(config.isStatusRetryable(StatusCode.NOT_FOUND));
71-
72-
RetryPolicy immediatelly = config.isStatusRetryable(StatusCode.BAD_SESSION);
63+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.SCHEME_ERROR));
64+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.ALREADY_EXISTS));
65+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.UNAUTHORIZED));
66+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.UNAVAILABLE));
67+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.TRANSPORT_UNAVAILABLE));
68+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.CLIENT_CANCELLED));
69+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.CLIENT_INTERNAL_ERROR));
70+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.NOT_FOUND));
71+
72+
RetryPolicy immediatelly = config.getStatusCodeRetryPolicy(StatusCode.BAD_SESSION);
7373
Assert.assertNotNull(immediatelly);
74-
Assert.assertEquals(immediatelly, config.isStatusRetryable(StatusCode.SESSION_BUSY));
74+
Assert.assertEquals(immediatelly, config.getStatusCodeRetryPolicy(StatusCode.SESSION_BUSY));
7575

76-
RetryPolicy fast = config.isStatusRetryable(StatusCode.ABORTED);
76+
RetryPolicy fast = config.getStatusCodeRetryPolicy(StatusCode.ABORTED);
7777
Assert.assertNotNull(fast);
78-
Assert.assertEquals(fast, config.isStatusRetryable(StatusCode.UNDETERMINED));
78+
Assert.assertEquals(fast, config.getStatusCodeRetryPolicy(StatusCode.UNDETERMINED));
7979

80-
RetryPolicy slow = config.isStatusRetryable(StatusCode.OVERLOADED);
80+
RetryPolicy slow = config.getStatusCodeRetryPolicy(StatusCode.OVERLOADED);
8181
Assert.assertNotNull(slow);
82-
Assert.assertEquals(slow, config.isStatusRetryable(StatusCode.CLIENT_RESOURCE_EXHAUSTED));
82+
Assert.assertEquals(slow, config.getStatusCodeRetryPolicy(StatusCode.CLIENT_RESOURCE_EXHAUSTED));
8383
}
8484

8585
@Test
8686
public void idempotentRetryPolicyTest() {
8787
RetryConfig config = RetryConfig.idempotentRetryForever();
8888

8989
// unretrayable
90-
Assert.assertNull(config.isStatusRetryable(StatusCode.SCHEME_ERROR));
91-
Assert.assertNull(config.isStatusRetryable(StatusCode.ALREADY_EXISTS));
92-
Assert.assertNull(config.isStatusRetryable(StatusCode.UNAUTHORIZED));
93-
Assert.assertNull(config.isStatusRetryable(StatusCode.NOT_FOUND));
90+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.SCHEME_ERROR));
91+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.ALREADY_EXISTS));
92+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.UNAUTHORIZED));
93+
Assert.assertNull(config.getStatusCodeRetryPolicy(StatusCode.NOT_FOUND));
9494

95-
RetryPolicy immediatelly = config.isStatusRetryable(StatusCode.BAD_SESSION);
95+
RetryPolicy immediatelly = config.getStatusCodeRetryPolicy(StatusCode.BAD_SESSION);
9696
Assert.assertNotNull(immediatelly);
97-
Assert.assertEquals(immediatelly, config.isStatusRetryable(StatusCode.SESSION_BUSY));
97+
Assert.assertEquals(immediatelly, config.getStatusCodeRetryPolicy(StatusCode.SESSION_BUSY));
9898

99-
RetryPolicy fast = config.isStatusRetryable(StatusCode.ABORTED);
99+
RetryPolicy fast = config.getStatusCodeRetryPolicy(StatusCode.ABORTED);
100100
Assert.assertNotNull(fast);
101-
Assert.assertEquals(fast, config.isStatusRetryable(StatusCode.UNDETERMINED));
102-
Assert.assertEquals(fast, config.isStatusRetryable(StatusCode.UNAVAILABLE));
103-
Assert.assertEquals(fast, config.isStatusRetryable(StatusCode.TRANSPORT_UNAVAILABLE));
104-
Assert.assertEquals(fast, config.isStatusRetryable(StatusCode.CLIENT_CANCELLED));
105-
Assert.assertEquals(fast, config.isStatusRetryable(StatusCode.CLIENT_INTERNAL_ERROR));
101+
Assert.assertEquals(fast, config.getStatusCodeRetryPolicy(StatusCode.UNDETERMINED));
102+
Assert.assertEquals(fast, config.getStatusCodeRetryPolicy(StatusCode.UNAVAILABLE));
103+
Assert.assertEquals(fast, config.getStatusCodeRetryPolicy(StatusCode.TRANSPORT_UNAVAILABLE));
104+
Assert.assertEquals(fast, config.getStatusCodeRetryPolicy(StatusCode.CLIENT_CANCELLED));
105+
Assert.assertEquals(fast, config.getStatusCodeRetryPolicy(StatusCode.CLIENT_INTERNAL_ERROR));
106106

107-
RetryPolicy slow = config.isStatusRetryable(StatusCode.OVERLOADED);
107+
RetryPolicy slow = config.getStatusCodeRetryPolicy(StatusCode.OVERLOADED);
108108
Assert.assertNotNull(slow);
109-
Assert.assertEquals(slow, config.isStatusRetryable(StatusCode.CLIENT_RESOURCE_EXHAUSTED));
109+
Assert.assertEquals(slow, config.getStatusCodeRetryPolicy(StatusCode.CLIENT_RESOURCE_EXHAUSTED));
110110
}
111111

112112
@Test
113113
public void notFoundRetryPolicyTest() {
114114
RetryConfig config = RetryConfig.newConfig().retryNotFound(true).retryForever();
115115

116-
RetryPolicy fast = config.isStatusRetryable(StatusCode.ABORTED);
117-
Assert.assertEquals(fast, config.isStatusRetryable(StatusCode.NOT_FOUND));
116+
RetryPolicy fast = config.getStatusCodeRetryPolicy(StatusCode.ABORTED);
117+
Assert.assertEquals(fast, config.getStatusCodeRetryPolicy(StatusCode.NOT_FOUND));
118118
}
119119

120120
@Test
121121
public void foreverRetryTest() {
122122
RetryConfig config = RetryConfig.newConfig().withSlowBackoff(100, 5).withFastBackoff(10, 10).retryForever();
123123

124-
RetryPolicy immediatelly = config.isStatusRetryable(StatusCode.BAD_SESSION);
124+
RetryPolicy immediatelly = config.getStatusCodeRetryPolicy(StatusCode.BAD_SESSION);
125125
Assert.assertEquals(0, immediatelly.nextRetryMs(0, 0));
126126
Assert.assertEquals(0, immediatelly.nextRetryMs(0, Integer.MAX_VALUE));
127127
Assert.assertEquals(0, immediatelly.nextRetryMs(Integer.MAX_VALUE, 0));
128128
Assert.assertEquals(0, immediatelly.nextRetryMs(Integer.MAX_VALUE, Integer.MAX_VALUE));
129129

130-
RetryPolicy fast = config.isStatusRetryable(StatusCode.ABORTED);
130+
RetryPolicy fast = config.getStatusCodeRetryPolicy(StatusCode.ABORTED);
131131
assertDuration(10, 20, fast.nextRetryMs(0, 0));
132132
assertDuration(10, 20, fast.nextRetryMs(0, Integer.MAX_VALUE));
133133
assertDuration(10240, 20480, fast.nextRetryMs(Integer.MAX_VALUE, 0));
134134
assertDuration(10240, 20480, fast.nextRetryMs(Integer.MAX_VALUE, Integer.MAX_VALUE));
135135

136-
RetryPolicy slow = config.isStatusRetryable(StatusCode.OVERLOADED);
136+
RetryPolicy slow = config.getStatusCodeRetryPolicy(StatusCode.OVERLOADED);
137137
assertDuration(100, 200, slow.nextRetryMs(0, 0));
138138
assertDuration(100, 200, slow.nextRetryMs(0, Integer.MAX_VALUE));
139139
assertDuration(3200, 6400, slow.nextRetryMs(Integer.MAX_VALUE, 0));
@@ -144,22 +144,22 @@ public void foreverRetryTest() {
144144
public void untilElapsedRetryTest() {
145145
RetryConfig config = RetryConfig.idempotentRetryUntilElapsed(5000);
146146

147-
RetryPolicy immediatelly = config.isStatusRetryable(StatusCode.BAD_SESSION);
147+
RetryPolicy immediatelly = config.getStatusCodeRetryPolicy(StatusCode.BAD_SESSION);
148148
Assert.assertEquals(0, immediatelly.nextRetryMs(0, 0));
149149
Assert.assertEquals(0, immediatelly.nextRetryMs(0, 5000));
150150
Assert.assertEquals(0, immediatelly.nextRetryMs(Integer.MAX_VALUE, 0));
151151
Assert.assertEquals(0, immediatelly.nextRetryMs(Integer.MAX_VALUE, 5000));
152152
Assert.assertEquals(-1, immediatelly.nextRetryMs(0, 5001));
153153
Assert.assertEquals(-1, immediatelly.nextRetryMs(Integer.MAX_VALUE, 5001));
154154

155-
RetryPolicy fast = config.isStatusRetryable(StatusCode.ABORTED);
155+
RetryPolicy fast = config.getStatusCodeRetryPolicy(StatusCode.ABORTED);
156156
assertDuration(5, 10, fast.nextRetryMs(0, 0));
157157
Assert.assertEquals(3, fast.nextRetryMs(0, 4997));
158158
Assert.assertEquals(5000, fast.nextRetryMs(Integer.MAX_VALUE, 0));
159159
Assert.assertEquals(1, fast.nextRetryMs(Integer.MAX_VALUE, 4999));
160160
Assert.assertEquals(-1, fast.nextRetryMs(Integer.MAX_VALUE, 5000));
161161

162-
RetryPolicy slow = config.isStatusRetryable(StatusCode.OVERLOADED);
162+
RetryPolicy slow = config.getStatusCodeRetryPolicy(StatusCode.OVERLOADED);
163163
assertDuration(500, 1000, slow.nextRetryMs(0, 0));
164164
Assert.assertEquals(3, slow.nextRetryMs(0, 4997));
165165
Assert.assertEquals(5000, slow.nextRetryMs(Integer.MAX_VALUE, 0));
@@ -171,23 +171,23 @@ public void untilElapsedRetryTest() {
171171
public void nTimesRetryTest() {
172172
RetryConfig config = RetryConfig.newConfig().retryNTimes(8);
173173

174-
RetryPolicy immediatelly = config.isStatusRetryable(StatusCode.BAD_SESSION);
174+
RetryPolicy immediatelly = config.getStatusCodeRetryPolicy(StatusCode.BAD_SESSION);
175175
Assert.assertEquals(0, immediatelly.nextRetryMs(0, 0));
176176
Assert.assertEquals(0, immediatelly.nextRetryMs(0, Integer.MAX_VALUE));
177177
Assert.assertEquals(0, immediatelly.nextRetryMs(7, 0));
178178
Assert.assertEquals(0, immediatelly.nextRetryMs(7, Integer.MAX_VALUE));
179179
Assert.assertEquals(-1, immediatelly.nextRetryMs(8, 0));
180180
Assert.assertEquals(-1, immediatelly.nextRetryMs(8, Integer.MAX_VALUE));
181181

182-
RetryPolicy fast = config.isStatusRetryable(StatusCode.ABORTED);
182+
RetryPolicy fast = config.getStatusCodeRetryPolicy(StatusCode.ABORTED);
183183
assertDuration(5, 10, fast.nextRetryMs(0, 0));
184184
assertDuration(5, 10, fast.nextRetryMs(0, Integer.MAX_VALUE));
185185
assertDuration(5 * 128, 5 * 256, fast.nextRetryMs(7, 0));
186186
assertDuration(5 * 128, 5 * 256, fast.nextRetryMs(7, Integer.MAX_VALUE));
187187
Assert.assertEquals(-1, fast.nextRetryMs(8, 0));
188188
Assert.assertEquals(-1, fast.nextRetryMs(8, Integer.MAX_VALUE));
189189

190-
RetryPolicy slow = config.isStatusRetryable(StatusCode.OVERLOADED);
190+
RetryPolicy slow = config.getStatusCodeRetryPolicy(StatusCode.OVERLOADED);
191191
assertDuration(500, 1000, slow.nextRetryMs(0, 0));
192192
assertDuration(500, 1000, slow.nextRetryMs(0, Integer.MAX_VALUE));
193193
assertDuration(500 * 64, 500 * 128, slow.nextRetryMs(7, 0));

0 commit comments

Comments
 (0)