-
Notifications
You must be signed in to change notification settings - Fork 214
/
AskWithRetry.java
311 lines (273 loc) · 13.1 KB
/
AskWithRetry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.cacheloaders;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.exceptions.AskException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.cacheloaders.config.AskWithRetryConfig;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Scheduler;
import org.apache.pekko.pattern.AskTimeoutException;
import org.apache.pekko.pattern.Patterns;
import scala.compat.java8.FutureConverters;
/**
* Helper/Pattern class providing an "ask with retry" pattern based on a provided {@link AskWithRetryConfig}.
*/
public final class AskWithRetry {
private static final DittoRuntimeException DUMMY_DRE = new DittoRuntimeException("dummy",
HttpStatus.INTERNAL_SERVER_ERROR, DittoHeaders.empty(), null, null, null, null) {
@Override
public DittoRuntimeException setDittoHeaders(final DittoHeaders dittoHeaders) {
return this;
}
};
private static final ThreadSafeDittoLogger LOGGER = DittoLoggerFactory.getThreadSafeLogger(AskWithRetry.class);
private AskWithRetry() {
throw new AssertionError();
}
/**
* Dispatcher name to use for AskWithRetry.
*/
public static final String ASK_WITH_RETRY_DISPATCHER = "ask-with-retry-dispatcher";
/**
* Performs the "ask with retry" pattern by asking the passed in {@code actorToAsk} the passed in {@code message},
* mapping a successful response with the provided {@code responseMapper} and retrying the operation on Exceptions
* which are not {@link DittoRuntimeException}s based on the given {@code config}.
*
* @param actorToAsk the actor to ask the message.
* @param message the message to ask.
* @param config the "ask with retry" configuration to apply, e.g. whether to do retries at all,
* with which timeouts, with how many retries and delays, etc.
* @param actorSystem the actorSystem for looking up the scheduler and dispatcher to use.
* @param responseMapper a function converting the response of the asked message.
* @param <M> the type of the message to ask.
* @param <A> the type of the answer.
* @return a CompletionStage which is completed by applying the passed in {@code responseMapper} function on the
* response of the asked message or which is completed exceptionally with the Exception.
*/
public static <M, A> CompletionStage<A> askWithRetry(final ActorRef actorToAsk,
final M message,
final AskWithRetryConfig config,
final ActorSystem actorSystem,
final Function<Object, A> responseMapper) {
return askWithRetry(actorToAsk,
message,
config,
actorSystem.getScheduler(),
actorSystem.dispatchers().lookup(ASK_WITH_RETRY_DISPATCHER),
responseMapper
);
}
/**
* Performs the "ask with retry" pattern by asking the passed in {@code actorToAsk} the passed in {@code message},
* mapping a successful response with the provided {@code responseMapper} and retrying the operation on Exceptions
* which are not {@link DittoRuntimeException}s based on the given {@code config}.
*
* @param actorToAsk the actor to ask the message.
* @param message the message to ask.
* @param config the "ask with retry" configuration to apply, e.g. whether to do retries at all,
* with which timeouts, with how many retries and delays, etc.
* @param scheduler the scheduler to use for retrying the ask.
* @param executor the executor to use for retrying the ask.
* @param responseMapper a function converting the response of the asked message.
* @param <M> the type of the message to ask.
* @param <A> the type of the answer.
* @return a CompletionStage which is completed by applying the passed in {@code responseMapper} function on the
* response of the asked message or which is completed exceptionally with the Exception.
* @throws java.lang.NullPointerException if any of the passed arguments was {@code null}.
*/
public static <M, A> CompletionStage<A> askWithRetry(final ActorRef actorToAsk,
final M message,
final AskWithRetryConfig config,
final Scheduler scheduler,
final Executor executor,
final Function<Object, A> responseMapper) {
checkNotNull(actorToAsk, "actorToAsk");
checkNotNull(message, "message");
checkNotNull(config, "config");
checkNotNull(scheduler, "scheduler");
checkNotNull(executor, "executor");
checkNotNull(responseMapper, "responseMapper");
final DittoHeaders dittoHeaders;
if (message instanceof WithDittoHeaders withDittoHeaders) {
dittoHeaders = withDittoHeaders.getDittoHeaders();
} else {
dittoHeaders = null;
}
final int retryAttempts = config.getRetryAttempts();
final Callable<CompletionStage<AskResult<A>>> askHandleCallable = () ->
createAskHandle(actorToAsk, message, dittoHeaders, responseMapper, config.getAskTimeout());
final CompletionStage<AskResult<A>> stage;
if (retryAttempts == 0) {
stage = createAskHandle(actorToAsk, message, dittoHeaders, responseMapper, config.getAskTimeout());
} else {
switch (config.getRetryStrategy()) {
case BACKOFF_DELAY:
stage = Patterns.retry(askHandleCallable,
retryAttempts,
config.getBackoffDelayMin(),
config.getBackoffDelayMax(),
config.getBackoffDelayRandomFactor(),
scheduler,
FutureConverters.fromExecutor(executor)
);
break;
case FIXED_DELAY:
stage = Patterns.retry(askHandleCallable,
retryAttempts,
config.getFixedDelay(),
scheduler,
FutureConverters.fromExecutor(executor)
);
break;
case NO_DELAY:
stage = Patterns.retry(askHandleCallable,
retryAttempts,
FutureConverters.fromExecutor(executor)
);
break;
case OFF:
default:
stage = createAskHandle(actorToAsk, message, dittoHeaders, responseMapper, config.getAskTimeout());
}
}
return stage.handle(handleRetryResult(dittoHeaders));
}
private static <M, A> CompletionStage<AskResult<A>> createAskHandle(final ActorRef actorToAsk,
final M message,
@Nullable final DittoHeaders dittoHeaders,
final Function<Object, A> responseMapper,
final Duration askTimeout) {
return Patterns.ask(actorToAsk, message, askTimeout)
.handle((response, throwable) -> {
if (null != throwable) {
final var dre = DittoRuntimeException.asDittoRuntimeException(throwable,
cause -> DUMMY_DRE); // throwable was no DittoRuntimeException when DUMMY_DRE is used
if (dre != DUMMY_DRE) {
// we have a real DittoRuntimeException:
return new AskFailure<>(dre);
}
if (throwable instanceof AskTimeoutException) {
ThreadSafeDittoLogger l = LOGGER;
if (null != dittoHeaders) {
l = LOGGER.withCorrelationId(dittoHeaders);
} else if (message instanceof WithDittoHeaders withDittoHeaders) {
l = LOGGER.withCorrelationId(withDittoHeaders.getDittoHeaders());
}
final EntityId entityId;
if (message instanceof WithEntityId withEntityId) {
entityId = withEntityId.getEntityId();
} else {
entityId = null;
}
l.warn("Got AskTimeout during ask for message <{}> and entityId <{} / {}> - retrying.. : <{}>",
message.getClass().getSimpleName(),
entityId,
entityId != null ? entityId.getEntityType() : null,
throwable.getMessage()
);
}
// all non-known RuntimeException should be handled by the "Patterns.retry" with a retry:
throw new UnknownAskRuntimeException(throwable);
} else {
try {
return new AskSuccess<>(responseMapper.apply(response));
} catch (final DittoRuntimeException dre) {
return new AskFailure<>(dre);
}
}
});
}
private static <A> BiFunction<AskResult<A>, Throwable, A> handleRetryResult(
@Nullable final DittoHeaders dittoHeaders) {
return (askResult, throwable) -> {
if (null != throwable) {
final Throwable cause;
if (throwable instanceof UnknownAskRuntimeException) {
cause = throwable.getCause();
} else {
cause = throwable;
}
throw DittoRuntimeException.asDittoRuntimeException(cause,
t -> {
final DittoRuntimeExceptionBuilder<AskException> exceptionBuilder =
AskException.newBuilder().cause(t);
if (null != dittoHeaders) {
exceptionBuilder.dittoHeaders(dittoHeaders);
}
return exceptionBuilder.build();
});
} else {
if (askResult.getDittoRuntimeException().isPresent()) {
throw askResult.getDittoRuntimeException().get();
} else {
return askResult.getAnswer().orElse(null);
}
}
};
}
private interface AskResult<A> {
Optional<A> getAnswer();
Optional<DittoRuntimeException> getDittoRuntimeException();
}
private static final class AskSuccess<A> implements AskResult<A> {
private final A answer;
AskSuccess(final A answer) {
this.answer = answer;
}
@Override
public Optional<A> getAnswer() {
return Optional.of(answer);
}
@Override
public Optional<DittoRuntimeException> getDittoRuntimeException() {
return Optional.empty();
}
}
private static final class AskFailure<A> implements AskResult<A> {
private final DittoRuntimeException dittoRuntimeException;
AskFailure(final DittoRuntimeException dittoRuntimeException) {
this.dittoRuntimeException = dittoRuntimeException;
}
@Override
public Optional<A> getAnswer() {
return Optional.empty();
}
@Override
public Optional<DittoRuntimeException> getDittoRuntimeException() {
return Optional.of(dittoRuntimeException);
}
}
private static final class UnknownAskRuntimeException extends RuntimeException {
UnknownAskRuntimeException(final Throwable cause) {
super(cause);
}
}
}