1919
2020import org .apache .ratis .util .JavaUtils ;
2121import org .apache .ratis .util .TimeDuration ;
22- import org .slf4j .Logger ;
23- import org .slf4j .LoggerFactory ;
2422
2523import java .util .ArrayList ;
2624import java .util .Collections ;
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 */
4341public 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}
0 commit comments