Skip to content

Commit 0443685

Browse files
authored
RATIS-2364. MultipleLinearRandomRetry should throw exceptions for illegal arguments. (#1319)
1 parent 8fd97a3 commit 0443685

File tree

3 files changed

+24
-50
lines changed

3 files changed

+24
-50
lines changed

ratis-common/dev-support/findbugsExcludeFile.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
<Class name="org.apache.ratis.protocol.GroupInfoReply" />
2424
<Bug pattern="EI_EXPOSE_REP2" />
2525
</Match>
26-
<Match>
27-
<Class name="org.apache.ratis.retry.MultipleLinearRandomRetry" />
28-
<Bug pattern="CT_CONSTRUCTOR_THROW" />
29-
</Match>
3026
<Match>
3127
<Class name="org.apache.ratis.util.AtomicFileOutputStream" />
3228
<Bug pattern="CT_CONSTRUCTOR_THROW" />
@@ -35,10 +31,6 @@
3531
<Class name="org.apache.ratis.util.Daemon" />
3632
<Bug pattern="EI_EXPOSE_REP2" />
3733
</Match>
38-
<Match>
39-
<Class name="org.apache.ratis.retry.MultipleLinearRandomRetry$Pair" />
40-
<Bug pattern="CT_CONSTRUCTOR_THROW" />
41-
</Match>
4234
<Match>
4335
<Class name="org.apache.ratis.util.Daemon$Builder" />
4436
<Bug pattern="EI_EXPOSE_REP2" />

ratis-common/src/main/java/org/apache/ratis/retry/MultipleLinearRandomRetry.java

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import org.apache.ratis.util.JavaUtils;
2121
import org.apache.ratis.util.TimeDuration;
22-
import org.slf4j.Logger;
23-
import org.slf4j.LoggerFactory;
2422

2523
import java.util.ArrayList;
2624
import java.util.Collections;
@@ -34,28 +32,19 @@
3432
* Given pairs of number of retries and sleep time (n0, t0), (n1, t1), ...,
3533
* the first n0 retries sleep t0 milliseconds on average,
3634
* the following n1 retries sleep t1 milliseconds on average, and so on.
37-
*
35+
* <p>
3836
* For all the sleep, the actual sleep time is randomly uniform distributed
3937
* in the close interval [0.5t, 1.5t], where t is the sleep time specified.
40-
*
38+
* <p>
4139
* The objects of this class are immutable.
4240
*/
4341
public final class MultipleLinearRandomRetry implements RetryPolicy {
44-
static final Logger LOG = LoggerFactory.getLogger(MultipleLinearRandomRetry.class);
45-
4642
/** Pairs of numRetries and sleepSeconds */
47-
private static class Pair {
43+
private static final class Pair {
4844
private final int numRetries;
4945
private final TimeDuration sleepTime;
5046

5147
Pair(int numRetries, TimeDuration sleepTime) {
52-
if (numRetries < 0) {
53-
throw new IllegalArgumentException("numRetries = " + numRetries+" < 0");
54-
}
55-
if (sleepTime.isNegative()) {
56-
throw new IllegalArgumentException("sleepTime = " + sleepTime + " < 0");
57-
}
58-
5948
this.numRetries = numRetries;
6049
this.sleepTime = sleepTime;
6150
}
@@ -76,9 +65,6 @@ public String toString() {
7665
private final Supplier<String> myString;
7766

7867
private MultipleLinearRandomRetry(List<Pair> pairs) {
79-
if (pairs == null || pairs.isEmpty()) {
80-
throw new IllegalArgumentException("pairs must be neither null nor empty.");
81-
}
8268
this.pairs = Collections.unmodifiableList(pairs);
8369
this.myString = JavaUtils.memoize(() -> JavaUtils.getClassSimpleName(getClass()) + pairs);
8470
}
@@ -131,30 +117,22 @@ public String toString() {
131117
* @return the parsed object, or null if the parsing fails.
132118
*/
133119
public static MultipleLinearRandomRetry parseCommaSeparated(String input) {
134-
final String[] elements = input.split(",");
135-
if (elements.length == 0) {
136-
LOG.warn("Illegal value: there is no element in \"{}\".", input);
137-
return null;
120+
input = input.trim();
121+
if (input.isEmpty()) {
122+
throw new IllegalArgumentException("Failed to parse \"" + input + "\": no elements found");
138123
}
124+
final String[] elements = input.split(",");
139125
if (elements.length % 2 != 0) {
140-
LOG.warn("Illegal value: the number of elements in \"{}\" is {} but an even number of elements is expected.",
141-
input, elements.length);
142-
return null;
126+
throw new IllegalArgumentException("Failed to parse \"" + input
127+
+ "\": number of elements (" + elements.length + ") is old");
143128
}
144129

145130
final List<Pair> pairs = new ArrayList<>();
146131
for(int i = 0; i < elements.length; ) {
147-
//parse the i-th sleep-time
148-
final TimeDuration sleep = parseElement(elements, i++, input, MultipleLinearRandomRetry::parsePositiveTime);
149-
if (sleep == null) {
150-
return null; //parse fails
151-
}
152-
//parse the i-th number-of-retries
153-
final Integer retries = parseElement(elements, i++, input, MultipleLinearRandomRetry::parsePositiveInt);
154-
if (retries == null) {
155-
return null; //parse fails
156-
}
157-
132+
final TimeDuration sleep = parseElement("sleep-time", elements, i++, input,
133+
MultipleLinearRandomRetry::parsePositiveTime);
134+
final Integer retries = parseElement("retries", elements, i++, input,
135+
MultipleLinearRandomRetry::parsePositiveInt);
158136
pairs.add(new Pair(retries, sleep));
159137
}
160138
return new MultipleLinearRandomRetry(pairs);
@@ -176,13 +154,13 @@ private static int parsePositiveInt(String trimmed) {
176154
return n;
177155
}
178156

179-
private static <E> E parseElement(String[] elements, int i, String input, Function<String, E> parser) {
157+
private static <E> E parseElement(String name, String[] elements, int i, String input, Function<String, E> parser) {
180158
final String s = elements[i].trim().replace("_", "");
181159
try {
182160
return parser.apply(s);
183-
} catch(Exception t) {
184-
LOG.warn("Failed to parse \"{}\", which is the index {} element in \"{}\"", s, i, input, t);
185-
return null;
161+
} catch (Exception e) {
162+
throw new IllegalArgumentException(
163+
"Failed to parse \"" + s + "\" as " + name + " (element " + i + " in \"" + input + "\")", e);
186164
}
187165
}
188166
}

ratis-test/src/test/java/org/apache/ratis/retry/TestMultipleLinearRandomRetry.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ public void testParseCommaSeparated() {
4444
assertLegalInput("[10x100ms, 20x1s, 30x5s]", "100,10, 1s,20, 5s,30");
4545
}
4646

47-
private static void assertIllegalInput(String input) {
48-
final MultipleLinearRandomRetry computed = MultipleLinearRandomRetry.parseCommaSeparated(input);
49-
Assertions.assertNull(computed);
47+
private void assertIllegalInput(String input) {
48+
try {
49+
MultipleLinearRandomRetry.parseCommaSeparated(input);
50+
} catch (IllegalArgumentException e) {
51+
LOG.info("Expected to catch: {}", String.valueOf(e));
52+
}
5053
}
54+
5155
private static MultipleLinearRandomRetry assertLegalInput(String expected, String input) {
5256
final MultipleLinearRandomRetry computed = MultipleLinearRandomRetry.parseCommaSeparated(input);
5357
Assertions.assertNotNull(computed);

0 commit comments

Comments
 (0)