20
20
21
21
package io .temporal .serviceclient ;
22
22
23
+ import static io .temporal .serviceclient .rpcretry .DefaultStubServiceOperationRpcRetryOptions .*;
24
+
23
25
import com .google .common .base .MoreObjects ;
24
26
import com .google .common .base .Preconditions ;
25
27
import com .google .protobuf .GeneratedMessageV3 ;
26
28
import io .grpc .Status ;
27
29
import io .temporal .internal .common .OptionsUtils ;
28
- import io .temporal .serviceclient .rpcretry .DefaultStubServiceOperationRpcRetryOptions ;
29
30
import java .time .Duration ;
30
31
import java .util .ArrayList ;
31
32
import java .util .Collections ;
@@ -119,20 +120,16 @@ private Builder(RpcRetryOptions options) {
119
120
120
121
/**
121
122
* Interval of the first retry, on regular failures. If coefficient is 1.0 then it is used for
122
- * all retries. Defaults to 50ms .
123
+ * all retries. Defaults to 100ms .
123
124
*
124
125
* @param initialInterval Interval to wait on first retry. Default will be used if set to {@code
125
126
* null}.
126
127
*/
127
128
public Builder setInitialInterval (Duration initialInterval ) {
128
- if (initialInterval != null ) {
129
- if (initialInterval .isNegative () || initialInterval .isZero ()) {
130
- throw new IllegalArgumentException ("Invalid interval: " + initialInterval );
131
- }
132
- this .initialInterval = initialInterval ;
133
- } else {
134
- this .initialInterval = null ;
129
+ if (isInvalidDuration (initialInterval )) {
130
+ throw new IllegalArgumentException ("invalid interval: " + initialInterval );
135
131
}
132
+ this .initialInterval = initialInterval ;
136
133
return this ;
137
134
}
138
135
@@ -144,14 +141,10 @@ public Builder setInitialInterval(Duration initialInterval) {
144
141
* Defaults to 1000ms, which is used if set to {@code null}.
145
142
*/
146
143
public Builder setCongestionInitialInterval (Duration congestionInitialInterval ) {
147
- if (initialInterval != null ) {
148
- if (congestionInitialInterval .isNegative () || congestionInitialInterval .isZero ()) {
149
- throw new IllegalArgumentException ("Invalid interval: " + congestionInitialInterval );
150
- }
151
- this .congestionInitialInterval = congestionInitialInterval ;
152
- } else {
153
- this .congestionInitialInterval = null ;
144
+ if (isInvalidDuration (congestionInitialInterval )) {
145
+ throw new IllegalArgumentException ("invalid interval: " + congestionInitialInterval );
154
146
}
147
+ this .congestionInitialInterval = congestionInitialInterval ;
155
148
return this ;
156
149
}
157
150
@@ -165,33 +158,26 @@ public Builder setCongestionInitialInterval(Duration congestionInitialInterval)
165
158
* null}.
166
159
*/
167
160
public Builder setExpiration (Duration expiration ) {
168
- if (expiration != null ) {
169
- if (expiration .isNegative () || expiration .isZero ()) {
170
- throw new IllegalArgumentException ("Invalid interval: " + expiration );
171
- }
172
- this .expiration = expiration ;
173
- } else {
174
- this .expiration = null ;
161
+ if (isInvalidDuration (expiration )) {
162
+ throw new IllegalArgumentException ("invalid interval: " + expiration );
175
163
}
164
+ this .expiration = expiration ;
176
165
return this ;
177
166
}
178
167
179
168
/**
180
169
* Coefficient used to calculate the next retry interval. The next retry interval is previous
181
- * interval multiplied by this coefficient. Must be 1 or larger. Default is 2.0 .
170
+ * interval multiplied by this coefficient. Must be 1 or larger. Default is 1.5 .
182
171
*
183
172
* @param backoffCoefficient Coefficient used to calculate the next retry interval. Defaults to
184
173
* 2.0, which is used if set to {@code 0}.
185
174
*/
186
175
public Builder setBackoffCoefficient (double backoffCoefficient ) {
187
- if (backoffCoefficient != 0.0 ) {
188
- if (!Double .isFinite (backoffCoefficient ) || (backoffCoefficient < 1.0 )) {
189
- throw new IllegalArgumentException ("coefficient has to be >= 1.0: " + backoffCoefficient );
190
- }
191
- this .backoffCoefficient = backoffCoefficient ;
192
- } else {
193
- this .backoffCoefficient = 0.0 ;
176
+ if (isInvalidBackoffCoefficient (backoffCoefficient )) {
177
+ throw new IllegalArgumentException (
178
+ "coefficient must be >= 1.0 and finite: " + backoffCoefficient );
194
179
}
180
+ this .backoffCoefficient = backoffCoefficient ;
195
181
return this ;
196
182
}
197
183
@@ -206,7 +192,7 @@ public Builder setBackoffCoefficient(double backoffCoefficient) {
206
192
* set to {@code 0}.
207
193
*/
208
194
public Builder setMaximumAttempts (int maximumAttempts ) {
209
- if (maximumAttempts < 0 ) {
195
+ if (isInvalidMaxAttempts ( maximumAttempts ) ) {
210
196
throw new IllegalArgumentException ("Invalid maximumAttempts: " + maximumAttempts );
211
197
}
212
198
this .maximumAttempts = maximumAttempts ;
@@ -216,41 +202,31 @@ public Builder setMaximumAttempts(int maximumAttempts) {
216
202
/**
217
203
* Maximum interval between retries. Exponential backoff leads to interval increase. This value
218
204
* is the cap of the increase. <br>
219
- * Default is 100x of initial interval. Can't be less than {@link #setInitialInterval(Duration)}
205
+ * Default is 50x of initial interval. Can't be less than {@link #setInitialInterval(Duration)}
220
206
*
221
- * @param maximumInterval the maximum interval value. Defaults to 100x initial interval, which
222
- * is used if set to {@code null}.
207
+ * @param maximumInterval the maximum interval value. Defaults to 50x initial interval, which is
208
+ * used if set to {@code null}.
223
209
*/
224
210
public Builder setMaximumInterval (Duration maximumInterval ) {
225
- if (maximumInterval != null ) {
226
- if ((maximumInterval .isNegative () || maximumInterval .isZero ())) {
227
- throw new IllegalArgumentException ("Invalid interval: " + maximumInterval );
228
- }
229
- this .maximumInterval = maximumInterval ;
230
- } else {
231
- this .maximumInterval = null ;
211
+ if (isInvalidDuration (maximumInterval )) {
212
+ throw new IllegalArgumentException ("invalid interval: " + maximumInterval );
232
213
}
214
+ this .maximumInterval = maximumInterval ;
233
215
return this ;
234
216
}
235
217
236
218
/**
237
219
* Maximum amount of jitter to apply. 0.2 means that actual retry time can be +/- 20% of the
238
- * calculated time. Set to 0 to disable jitter. Must be lower than 1. Default is 0.1 .
220
+ * calculated time. Set to 0 to disable jitter. Must be lower than 1. Default is 0.2 .
239
221
*
240
222
* @param maximumJitterCoefficient Maximum amount of jitter. Default will be used if set to -1.
241
223
*/
242
224
public Builder setMaximumJitterCoefficient (double maximumJitterCoefficient ) {
243
- if (maximumJitterCoefficient != -1.0 ) {
244
- if (!Double .isFinite (maximumJitterCoefficient )
245
- || maximumJitterCoefficient < 0
246
- || maximumJitterCoefficient >= 1.0 ) {
247
- throw new IllegalArgumentException (
248
- "maximumJitterCoefficient has to be >= 0 and < 1.0: " + maximumJitterCoefficient );
249
- }
250
- this .maximumJitterCoefficient = maximumJitterCoefficient ;
251
- } else {
252
- this .maximumJitterCoefficient = -1.0 ;
225
+ if (isInvalidJitterCoefficient (maximumJitterCoefficient )) {
226
+ throw new IllegalArgumentException (
227
+ "coefficient must be >= 0 and < 1.0: " + maximumJitterCoefficient );
253
228
}
229
+ this .maximumJitterCoefficient = maximumJitterCoefficient ;
254
230
return this ;
255
231
}
256
232
@@ -334,7 +310,7 @@ private List<DoNotRetryItem> merge(List<DoNotRetryItem> o1, List<DoNotRetryItem>
334
310
if (o2 != null ) {
335
311
return new ArrayList <>(o2 );
336
312
}
337
- if (o1 . size () > 0 ) {
313
+ if (o1 != null && ! o1 . isEmpty () ) {
338
314
return new ArrayList <>(o1 );
339
315
}
340
316
return null ;
@@ -364,51 +340,63 @@ public RpcRetryOptions buildWithDefaultsFrom(RpcRetryOptions rpcRetryOptions) {
364
340
365
341
/** Validates property values and builds RetryOptions with default values. */
366
342
public RpcRetryOptions validateBuildWithDefaults () {
367
- double backoff = backoffCoefficient ;
368
- if (backoff == 0d ) {
369
- backoff = DefaultStubServiceOperationRpcRetryOptions . BACKOFF ;
343
+ double backoffCoefficient = this . backoffCoefficient ;
344
+ if (backoffCoefficient < 1 ) {
345
+ backoffCoefficient = BACKOFF ;
370
346
}
371
- if (initialInterval == null || initialInterval .isZero () || initialInterval .isNegative ()) {
372
- initialInterval = DefaultStubServiceOperationRpcRetryOptions .INITIAL_INTERVAL ;
347
+ Duration initialInterval = this .initialInterval ;
348
+ if (initialInterval == null ) {
349
+ initialInterval = INITIAL_INTERVAL ;
373
350
}
374
- if (congestionInitialInterval == null
375
- || congestionInitialInterval .isZero ()
376
- || congestionInitialInterval .isNegative ()) {
377
- congestionInitialInterval =
378
- DefaultStubServiceOperationRpcRetryOptions .CONGESTION_INITIAL_INTERVAL ;
351
+ Duration congestionInitialInterval = this .congestionInitialInterval ;
352
+ if (congestionInitialInterval == null ) {
353
+ congestionInitialInterval = CONGESTION_INITIAL_INTERVAL ;
379
354
}
380
- if (expiration == null || expiration .isZero () || expiration .isNegative ()) {
381
- expiration = DefaultStubServiceOperationRpcRetryOptions .EXPIRATION_INTERVAL ;
355
+ Duration expiration = this .expiration ;
356
+ if (expiration == null ) {
357
+ expiration = EXPIRATION_INTERVAL ;
382
358
}
383
-
384
- Duration maxInterval = this .maximumInterval ;
385
-
386
- if (maxInterval == null || maxInterval .isZero () || maxInterval .isNegative ()) {
387
- if (maximumAttempts == 0 ) {
388
- maxInterval = DefaultStubServiceOperationRpcRetryOptions .MAXIMUM_INTERVAL ;
389
- } else {
390
- maxInterval = null ;
391
- }
359
+ Duration maximumInterval = this .maximumInterval ;
360
+ if (maximumInterval == null && maximumAttempts == 0 ) {
361
+ maximumInterval = initialInterval .multipliedBy (MAXIMUM_INTERVAL_MULTIPLIER );
392
362
}
393
-
394
- if (maximumJitterCoefficient == -1.0 ) {
395
- maximumJitterCoefficient =
396
- DefaultStubServiceOperationRpcRetryOptions .MAXIMUM_JITTER_COEFFICIENT ;
363
+ double maximumJitterCoefficient = this .maximumJitterCoefficient ;
364
+ if (maximumJitterCoefficient < 0 ) {
365
+ maximumJitterCoefficient = MAXIMUM_JITTER_COEFFICIENT ;
397
366
}
398
367
399
368
RpcRetryOptions result =
400
369
new RpcRetryOptions (
401
370
initialInterval ,
402
371
congestionInitialInterval ,
403
- backoff ,
372
+ backoffCoefficient ,
404
373
expiration ,
405
374
maximumAttempts ,
406
- maxInterval ,
375
+ maximumInterval ,
407
376
maximumJitterCoefficient ,
408
377
MoreObjects .firstNonNull (doNotRetry , Collections .emptyList ()));
409
378
result .validate ();
410
379
return result ;
411
380
}
381
+
382
+ private static boolean isInvalidDuration (Duration d ) {
383
+ if (d == null ) {
384
+ return false ;
385
+ }
386
+ return d .isNegative () || d .isZero ();
387
+ }
388
+
389
+ private static boolean isInvalidMaxAttempts (int i ) {
390
+ return i < 0 ;
391
+ }
392
+
393
+ private static boolean isInvalidBackoffCoefficient (double v ) {
394
+ return !Double .isFinite (v ) || (v != 0.0 && v < 1.0 );
395
+ }
396
+
397
+ private static boolean isInvalidJitterCoefficient (double v ) {
398
+ return !Double .isFinite (v ) || (v != -1.0 && (v < 0.0 || v >= 1.0 ));
399
+ }
412
400
}
413
401
414
402
private final Duration initialInterval ;
0 commit comments