22
22
23
23
import java .time .Duration ;
24
24
25
- import org .apache .accumulo .core .util .time .NanoTime ;
26
25
import org .slf4j .Logger ;
27
26
28
27
import com .google .common .annotations .VisibleForTesting ;
@@ -42,9 +41,8 @@ public class Retry {
42
41
private Duration currentWait ;
43
42
private Duration initialWait ;
44
43
45
- private boolean hasNeverLogged ;
46
44
private boolean hasLoggedWarn = false ;
47
- private NanoTime lastRetryLog ;
45
+ private Timer lastRetryLogTimer ;
48
46
private double currentBackOffFactor ;
49
47
private boolean doTimeJitter = true ;
50
48
@@ -64,8 +62,7 @@ private Retry(long maxRetries, Duration startWait, Duration waitIncrement, Durat
64
62
this .currentWait = startWait ;
65
63
this .initialWait = startWait ;
66
64
this .logInterval = logInterval ;
67
- this .hasNeverLogged = true ;
68
- this .lastRetryLog = null ;
65
+ this .lastRetryLogTimer = null ;
69
66
this .backOffFactor = backOffFactor ;
70
67
this .currentBackOffFactor = this .backOffFactor ;
71
68
@@ -202,16 +199,14 @@ protected void sleep(Duration wait) throws InterruptedException {
202
199
203
200
public void logRetry (Logger log , String message , Throwable t ) {
204
201
// log the first time as debug, and then after every logInterval as a warning
205
- NanoTime now = NanoTime .now ();
206
- if (hasNeverLogged ) {
202
+ if (lastRetryLogTimer == null ) {
207
203
if (log .isDebugEnabled ()) {
208
204
log .debug (getMessage (message , t ));
209
205
}
210
- hasNeverLogged = false ;
211
- lastRetryLog = now ;
212
- } else if (now .subtract (lastRetryLog ).compareTo (logInterval ) > 0 ) {
206
+ lastRetryLogTimer = Timer .startNew ();
207
+ } else if (lastRetryLogTimer .hasElapsed (logInterval )) {
213
208
log .warn (getMessage (message ), t );
214
- lastRetryLog = now ;
209
+ lastRetryLogTimer . restart () ;
215
210
hasLoggedWarn = true ;
216
211
} else {
217
212
if (log .isTraceEnabled ()) {
@@ -222,16 +217,14 @@ public void logRetry(Logger log, String message, Throwable t) {
222
217
223
218
public void logRetry (Logger log , String message ) {
224
219
// log the first time as debug, and then after every logInterval as a warning
225
- NanoTime now = NanoTime .now ();
226
- if (hasNeverLogged ) {
220
+ if (lastRetryLogTimer == null ) {
227
221
if (log .isDebugEnabled ()) {
228
222
log .debug (getMessage (message ));
229
223
}
230
- hasNeverLogged = false ;
231
- lastRetryLog = now ;
232
- } else if (now .subtract (lastRetryLog ).compareTo (logInterval ) > 0 ) {
224
+ lastRetryLogTimer = Timer .startNew ();
225
+ } else if (lastRetryLogTimer .hasElapsed (logInterval )) {
233
226
log .warn (getMessage (message ));
234
- lastRetryLog = now ;
227
+ lastRetryLogTimer . restart () ;
235
228
hasLoggedWarn = true ;
236
229
} else {
237
230
if (log .isTraceEnabled ()) {
@@ -251,14 +244,15 @@ private String getMessage(String message, Throwable t) {
251
244
}
252
245
253
246
public void logCompletion (Logger log , String operationDescription ) {
254
- if (!hasNeverLogged ) {
255
- var message = operationDescription + " completed after " + (retriesDone + 1 )
256
- + " retries and is no longer retrying." ;
257
- if (hasLoggedWarn ) {
258
- log .info (message );
259
- } else {
260
- log .debug (message );
261
- }
247
+ if (lastRetryLogTimer == null ) { // have never logged a retry
248
+ return ;
249
+ }
250
+ var message = operationDescription + " completed after " + (retriesDone + 1 )
251
+ + " retries and is no longer retrying." ;
252
+ if (hasLoggedWarn ) {
253
+ log .info (message );
254
+ } else {
255
+ log .debug (message );
262
256
}
263
257
}
264
258
0 commit comments