|
17 | 17 | package com.google.cloud.storage; |
18 | 18 |
|
19 | 19 | import com.google.api.gax.retrying.ResultRetryAlgorithm; |
| 20 | +import com.google.cloud.storage.Backoff.BackoffDuration; |
| 21 | +import com.google.cloud.storage.Backoff.BackoffResult; |
| 22 | +import com.google.cloud.storage.Backoff.BackoffResults; |
| 23 | +import com.google.cloud.storage.Backoff.Jitterer; |
20 | 24 | import com.google.cloud.storage.Retrying.RetryingDependencies; |
| 25 | +import com.google.common.annotations.VisibleForTesting; |
| 26 | +import java.time.Duration; |
21 | 27 | import java.util.LinkedList; |
22 | 28 | import java.util.List; |
| 29 | +import java.util.concurrent.Executors; |
| 30 | +import java.util.concurrent.ScheduledExecutorService; |
| 31 | +import java.util.concurrent.ScheduledFuture; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | +import java.util.concurrent.locks.ReentrantLock; |
| 34 | +import org.checkerframework.checker.nullness.qual.Nullable; |
23 | 35 |
|
| 36 | +@SuppressWarnings("SizeReplaceableByIsEmpty") // allow elimination of a method call and a negation |
24 | 37 | final class RetryContext { |
25 | | - |
| 38 | + private final ScheduledExecutorService scheduledExecutorService; |
26 | 39 | private final RetryingDependencies retryingDependencies; |
27 | 40 | private final ResultRetryAlgorithm<?> algorithm; |
| 41 | + private final Backoff backoff; |
| 42 | + private final ReentrantLock lock; |
| 43 | + |
28 | 44 | private List<Throwable> failures; |
| 45 | + private long lastRecordedErrorNs; |
| 46 | + @Nullable private BackoffResult lastBackoffResult; |
| 47 | + @Nullable private ScheduledFuture<?> pendingBackoff; |
29 | 48 |
|
30 | 49 | private RetryContext( |
31 | | - RetryingDependencies retryingDependencies, ResultRetryAlgorithm<?> algorithm) { |
| 50 | + ScheduledExecutorService scheduledExecutorService, |
| 51 | + RetryingDependencies retryingDependencies, |
| 52 | + ResultRetryAlgorithm<?> algorithm, |
| 53 | + Jitterer jitterer) { |
| 54 | + this.scheduledExecutorService = scheduledExecutorService; |
32 | 55 | this.retryingDependencies = retryingDependencies; |
33 | 56 | this.algorithm = algorithm; |
| 57 | + this.backoff = |
| 58 | + Backoff.from(retryingDependencies.getRetrySettings()).setJitterer(jitterer).build(); |
| 59 | + this.lock = new ReentrantLock(); |
| 60 | + this.failures = new LinkedList<>(); |
| 61 | + this.lastRecordedErrorNs = retryingDependencies.getClock().nanoTime(); |
| 62 | + this.lastBackoffResult = null; |
| 63 | + this.pendingBackoff = null; |
| 64 | + } |
| 65 | + |
| 66 | + boolean inBackoff() { |
| 67 | + lock.lock(); |
| 68 | + boolean b = pendingBackoff != null; |
| 69 | + try { |
| 70 | + return b; |
| 71 | + } finally { |
| 72 | + lock.unlock(); |
| 73 | + } |
34 | 74 | } |
35 | 75 |
|
36 | 76 | void reset() { |
37 | | - failures = new LinkedList<>(); |
| 77 | + lock.lock(); |
| 78 | + try { |
| 79 | + if (failures.size() > 0) { |
| 80 | + failures = new LinkedList<>(); |
| 81 | + } |
| 82 | + lastRecordedErrorNs = retryingDependencies.getClock().nanoTime(); |
| 83 | + clearPendingBackoff(); |
| 84 | + } finally { |
| 85 | + lock.unlock(); |
| 86 | + } |
38 | 87 | } |
39 | 88 |
|
40 | | - public <T extends Throwable> void recordError(T t, OnSuccess onSuccess, OnFailure<T> onFailure) { |
41 | | - int failureCount = failures.size() + 1 /* include t in the count*/; |
42 | | - int maxAttempts = retryingDependencies.getRetrySettings().getMaxAttempts(); |
43 | | - boolean shouldRetry = algorithm.shouldRetry(t, null); |
44 | | - String msgPrefix = null; |
45 | | - if (shouldRetry && failureCount >= maxAttempts) { |
46 | | - msgPrefix = "Operation failed to complete within retry limit"; |
47 | | - } else if (!shouldRetry) { |
48 | | - msgPrefix = "Unretryable error"; |
| 89 | + @VisibleForTesting |
| 90 | + void awaitBackoffComplete() { |
| 91 | + while (inBackoff()) { |
| 92 | + Thread.yield(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + <T extends Throwable> void recordError(T t, OnSuccess onSuccess, OnFailure<T> onFailure) { |
| 97 | + lock.lock(); |
| 98 | + try { |
| 99 | + long now = retryingDependencies.getClock().nanoTime(); |
| 100 | + Duration elapsed = Duration.ofNanos(now - lastRecordedErrorNs); |
| 101 | + if (pendingBackoff != null && pendingBackoff.isDone()) { |
| 102 | + pendingBackoff = null; |
| 103 | + lastBackoffResult = null; |
| 104 | + } else if (pendingBackoff != null) { |
| 105 | + pendingBackoff.cancel(true); |
| 106 | + backoff.backoffInterrupted(elapsed); |
| 107 | + String message = |
| 108 | + String.format( |
| 109 | + "Previous backoff interrupted by this error (previousBackoff: %s, elapsed: %s)", |
| 110 | + lastBackoffResult != null ? lastBackoffResult.errorString() : null, elapsed); |
| 111 | + t.addSuppressed(BackoffComment.of(message)); |
| 112 | + } |
| 113 | + int failureCount = failures.size() + 1 /* include t in the count*/; |
| 114 | + int maxAttempts = retryingDependencies.getRetrySettings().getMaxAttempts(); |
| 115 | + if (maxAttempts <= 0) { |
| 116 | + maxAttempts = Integer.MAX_VALUE; |
| 117 | + } |
| 118 | + boolean shouldRetry = algorithm.shouldRetry(t, null); |
| 119 | + Duration elapsedOverall = backoff.getCumulativeBackoff().plus(elapsed); |
| 120 | + BackoffResult nextBackoff = backoff.nextBackoff(elapsed); |
| 121 | + String msgPrefix = null; |
| 122 | + if (shouldRetry && failureCount >= maxAttempts) { |
| 123 | + msgPrefix = "Operation failed to complete within attempt budget"; |
| 124 | + } else if (nextBackoff == BackoffResults.EXHAUSTED) { |
| 125 | + msgPrefix = "Operation failed to complete within backoff budget"; |
| 126 | + } else if (!shouldRetry) { |
| 127 | + msgPrefix = "Unretryable error"; |
| 128 | + } |
| 129 | + |
| 130 | + if (msgPrefix == null) { |
| 131 | + t.addSuppressed(BackoffComment.fromResult(nextBackoff)); |
| 132 | + failures.add(t); |
| 133 | + |
| 134 | + BackoffDuration backoffDuration = (BackoffDuration) nextBackoff; |
| 135 | + |
| 136 | + lastBackoffResult = nextBackoff; |
| 137 | + pendingBackoff = |
| 138 | + scheduledExecutorService.schedule( |
| 139 | + () -> { |
| 140 | + try { |
| 141 | + onSuccess.onSuccess(); |
| 142 | + } finally { |
| 143 | + clearPendingBackoff(); |
| 144 | + } |
| 145 | + }, |
| 146 | + backoffDuration.getDuration().toNanos(), |
| 147 | + TimeUnit.NANOSECONDS); |
| 148 | + } else { |
| 149 | + String msg = |
| 150 | + String.format( |
| 151 | + "%s (attempts: %d%s, elapsed: %s, nextBackoff: %s%s)%s", |
| 152 | + msgPrefix, |
| 153 | + failureCount, |
| 154 | + maxAttempts == Integer.MAX_VALUE |
| 155 | + ? "" |
| 156 | + : String.format(", maxAttempts: %d", maxAttempts), |
| 157 | + elapsedOverall, |
| 158 | + nextBackoff.errorString(), |
| 159 | + Durations.eq(backoff.getTimeout(), Durations.EFFECTIVE_INFINITY) |
| 160 | + ? "" |
| 161 | + : ", timeout: " + backoff.getTimeout(), |
| 162 | + failures.isEmpty() ? "" : " previous failures follow in order of occurrence"); |
| 163 | + t.addSuppressed(new RetryBudgetExhaustedComment(msg)); |
| 164 | + for (Throwable failure : failures) { |
| 165 | + t.addSuppressed(failure); |
| 166 | + } |
| 167 | + onFailure.onFailure(t); |
| 168 | + } |
| 169 | + } finally { |
| 170 | + lock.unlock(); |
49 | 171 | } |
| 172 | + } |
50 | 173 |
|
51 | | - if (msgPrefix == null) { |
52 | | - failures.add(t); |
53 | | - onSuccess.onSuccess(); |
54 | | - } else { |
55 | | - String msg = |
56 | | - String.format( |
57 | | - "%s (attempts: %d, maxAttempts: %d)%s", |
58 | | - msgPrefix, |
59 | | - failureCount, |
60 | | - maxAttempts, |
61 | | - failures.isEmpty() ? "" : " previous failures follow in order of occurrence"); |
62 | | - t.addSuppressed(new RetryBudgetExhaustedComment(msg)); |
63 | | - for (Throwable failure : failures) { |
64 | | - t.addSuppressed(failure); |
| 174 | + private void clearPendingBackoff() { |
| 175 | + lock.lock(); |
| 176 | + try { |
| 177 | + if (pendingBackoff != null) { |
| 178 | + if (!pendingBackoff.isDone()) { |
| 179 | + pendingBackoff.cancel(true); |
| 180 | + } |
| 181 | + pendingBackoff = null; |
| 182 | + } |
| 183 | + if (lastBackoffResult != null) { |
| 184 | + lastBackoffResult = null; |
65 | 185 | } |
66 | | - onFailure.onFailure(t); |
| 186 | + } finally { |
| 187 | + lock.unlock(); |
67 | 188 | } |
68 | 189 | } |
69 | 190 |
|
70 | 191 | static RetryContext of( |
71 | | - RetryingDependencies retryingDependencies, ResultRetryAlgorithm<?> algorithm) { |
72 | | - RetryContext retryContext = new RetryContext(retryingDependencies, algorithm); |
73 | | - retryContext.reset(); |
74 | | - return retryContext; |
| 192 | + ScheduledExecutorService scheduledExecutorService, |
| 193 | + RetryingDependencies retryingDependencies, |
| 194 | + ResultRetryAlgorithm<?> algorithm, |
| 195 | + Jitterer jitterer) { |
| 196 | + return new RetryContext(scheduledExecutorService, retryingDependencies, algorithm, jitterer); |
75 | 197 | } |
76 | 198 |
|
77 | 199 | static RetryContext neverRetry() { |
78 | | - return new RetryContext(RetryingDependencies.attemptOnce(), Retrying.neverRetry()); |
| 200 | + return new RetryContext( |
| 201 | + Executors.newSingleThreadScheduledExecutor(), |
| 202 | + RetryingDependencies.attemptOnce(), |
| 203 | + Retrying.neverRetry(), |
| 204 | + Jitterer.threadLocalRandom()); |
79 | 205 | } |
80 | 206 |
|
81 | | - static RetryContextProvider providerFrom(RetryingDependencies deps, ResultRetryAlgorithm<?> alg) { |
82 | | - return () -> RetryContext.of(deps, alg); |
| 207 | + static RetryContextProvider providerFrom( |
| 208 | + ScheduledExecutorService scheduledExecutorService, |
| 209 | + RetryingDependencies deps, |
| 210 | + ResultRetryAlgorithm<?> alg) { |
| 211 | + return () -> RetryContext.of(scheduledExecutorService, deps, alg, Jitterer.threadLocalRandom()); |
83 | 212 | } |
84 | 213 |
|
85 | 214 | @FunctionalInterface |
@@ -110,4 +239,19 @@ private RetryBudgetExhaustedComment(String comment) { |
110 | 239 | super(comment, /*cause=*/ null, /*enableSuppression=*/ true, /*writableStackTrace=*/ false); |
111 | 240 | } |
112 | 241 | } |
| 242 | + |
| 243 | + private static final class BackoffComment extends Throwable { |
| 244 | + private BackoffComment(String message) { |
| 245 | + super(message, /*cause=*/ null, /*enableSuppression=*/ true, /*writableStackTrace=*/ false); |
| 246 | + } |
| 247 | + |
| 248 | + private static BackoffComment fromResult(BackoffResult result) { |
| 249 | + return new BackoffComment( |
| 250 | + String.format("backing off %s before next attempt", result.errorString())); |
| 251 | + } |
| 252 | + |
| 253 | + private static BackoffComment of(String message) { |
| 254 | + return new BackoffComment(message); |
| 255 | + } |
| 256 | + } |
113 | 257 | } |
0 commit comments