-
Notifications
You must be signed in to change notification settings - Fork 215
/
AcknowledgementAggregatorActorStarter.java
361 lines (319 loc) · 17.5 KB
/
AcknowledgementAggregatorActorStarter.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright (c) 2020 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.edge.service.acknowledgements;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Collection;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.acks.AbstractCommandAckRequestSetter;
import org.eclipse.ditto.base.model.acks.AcknowledgementRequest;
import org.eclipse.ditto.base.model.acks.CommandResponseAcknowledgementProvider;
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.DittoHeaderInvalidException;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.translator.HeaderTranslator;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.internal.models.signal.correlation.MatchingValidationResult;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import akka.actor.ActorRef;
import akka.actor.ActorRefFactory;
import akka.actor.Address;
import akka.cluster.Cluster;
import akka.japi.pf.PFBuilder;
import scala.PartialFunction;
/**
* Starting an acknowledgement aggregator actor is more complex than simply call {@code actorOf}.
* Thus starting logic is worth to be handled within its own class.
*
* @since 1.1.0
*/
public final class AcknowledgementAggregatorActorStarter {
private static final ThreadSafeDittoLogger LOGGER =
DittoLoggerFactory.getThreadSafeLogger(AcknowledgementAggregatorActorStarter.class);
private final ActorRefFactory actorRefFactory;
private final Duration maxTimeout;
private final HeaderTranslator headerTranslator;
private final PartialFunction<Signal<?>, Signal<?>> ackRequestSetter;
private final Collection<CommandResponseAcknowledgementProvider<?>> responseAcknowledgementProviders;
@Nullable private final Consumer<MatchingValidationResult.Failure> matchingValidationFailureConsumer;
private final Address selfRemoteAddress;
private int childCounter;
private AcknowledgementAggregatorActorStarter(final ActorRefFactory actorRefFactory,
final Duration maxTimeout,
final HeaderTranslator headerTranslator,
@Nullable final Consumer<MatchingValidationResult.Failure> matchingValidationFailureConsumer,
final PartialFunction<Signal<?>, Signal<?>> ackRequestSetter,
final Collection<CommandResponseAcknowledgementProvider<?>> responseAcknowledgementProviders) {
this.actorRefFactory = checkNotNull(actorRefFactory, "actorRefFactory");
this.maxTimeout = checkNotNull(maxTimeout, "maxTimeout");
this.headerTranslator = checkNotNull(headerTranslator, "headerTranslator");
this.matchingValidationFailureConsumer = matchingValidationFailureConsumer;
this.ackRequestSetter = ackRequestSetter;
this.responseAcknowledgementProviders = responseAcknowledgementProviders;
selfRemoteAddress = Cluster.get(actorRefFactory.systemImpl()).selfUniqueAddress().address();
childCounter = 0;
}
/**
* Returns an instance of {@code AcknowledgementAggregatorActorStarter}.
*
* @param actorRefFactory the actorRefFactory to start the aggregator actor in.
* @param acknowledgementConfig provides configuration setting regarding acknowledgement handling.
* @param headerTranslator translates headers from external sources or to external sources.
* response over a channel to the user.
* @param matchingValidationFailureConsumer optional handler for response validation failures.
* @param ackRequestSetters a collection of {@code ackRequestSetters} which are used to enhance inbound signals with
* (implicit) acknowledgement requests.
* @param responseAcknowledgementProviders a collection of Acknowledgement providers which provide Acks based on
* processed command responses.
* @return a means to start an acknowledgement forwarder actor.
* @throws NullPointerException if any argument but {@code responseValidationFailureContextConsumer} is {@code null}.
*/
public static AcknowledgementAggregatorActorStarter of(final ActorRefFactory actorRefFactory,
final AcknowledgementConfig acknowledgementConfig,
final HeaderTranslator headerTranslator,
@Nullable final Consumer<MatchingValidationResult.Failure> matchingValidationFailureConsumer,
final Collection<AbstractCommandAckRequestSetter<?>> ackRequestSetters,
final Collection<CommandResponseAcknowledgementProvider<?>> responseAcknowledgementProviders) {
return of(actorRefFactory,
acknowledgementConfig.getForwarderFallbackTimeout(),
headerTranslator,
matchingValidationFailureConsumer,
ackRequestSetters,
responseAcknowledgementProviders);
}
/**
* Returns an instance of {@code AcknowledgementAggregatorActorStarter}.
*
* @param actorRefFactory the actorRefFactory to start the aggregator actor in.
* @param maxTimeout the maximum timeout.
* @param headerTranslator translates headers from external sources or to external sources.
* response over a channel to the user.
* @param matchingValidationFailureConsumer optional handler for response validation failures.
* @param ackRequestSetters a collection of {@code ackRequestSetters} which are used to enhance inbound signals with
* (implicit) acknowledgement requests.
* @param responseAcknowledgementProviders a collection of Acknowledgement providers which provide Acks based on
* processed command responses.
* @return a means to start an acknowledgement forwarder actor.
* @throws NullPointerException if any argument but {@code responseValidationFailureContextConsumer} is {@code null}.
*/
public static AcknowledgementAggregatorActorStarter of(final ActorRefFactory actorRefFactory,
final Duration maxTimeout,
final HeaderTranslator headerTranslator,
@Nullable final Consumer<MatchingValidationResult.Failure> matchingValidationFailureConsumer,
final Collection<AbstractCommandAckRequestSetter<?>> ackRequestSetters,
final Collection<CommandResponseAcknowledgementProvider<?>> responseAcknowledgementProviders) {
return new AcknowledgementAggregatorActorStarter(actorRefFactory,
maxTimeout,
headerTranslator,
matchingValidationFailureConsumer,
buildAckRequestSetter(ackRequestSetters),
responseAcknowledgementProviders);
}
/**
* Checks if the {@code AcknowledgementAggregatorActor} should be started for the passed {@code signal}.
* Public because this is also used in unit tests.
*
* @param signal the signal to check for whether the ack aggregator actor should be started.
* @return whether the ack aggregator actor should be started.
*/
public static boolean shouldStartForIncoming(final Signal<?> signal) {
final boolean result;
final var isLiveSignal = Signal.isChannelLive(signal);
final var isChannelSmart = Signal.isChannelSmart(signal);
final Collection<AcknowledgementRequest> ackRequests = signal.getDittoHeaders().getAcknowledgementRequests();
if (signal instanceof Command<?> command &&
Command.Category.isEntityModifyingCommand(command.getCategory()) &&
Command.isThingCommand(signal) &&
!isLiveSignal) {
result = ackRequests.stream().anyMatch(AcknowledgementForwarderActorStarter::isNotLiveResponse);
} else if (Command.isMessageCommand(signal) || isLiveSignal && Command.isThingCommand(signal)) {
result = ackRequests.stream().anyMatch(AcknowledgementForwarderActorStarter::isNotTwinPersisted);
} else {
result = isChannelSmart;
}
return result;
}
/**
* Start an acknowledgement aggregator actor if needed.
*
* @param <T> type of the result.
* @param signal the signal to start the aggregator actor for.
* @param timeoutOverride duration to override the timeout of the signal.
* @param responseSignalConsumer consumer of the aggregated response or error.
* @param ackregatorStartedFunction what to do if the aggregator actor started. The first argument is
* the signal after setting requested-acks and response-required.
* @param ackregatorNotStartedFunction what to do if the aggregator actor did not start.
* @return the result.
*/
public <T> T start(final Signal<?> signal,
@Nullable final Duration timeoutOverride,
final Function<Object, T> responseSignalConsumer,
final BiFunction<Signal<?>, ActorRef, T> ackregatorStartedFunction,
final Function<Signal<?>, T> ackregatorNotStartedFunction) {
return preprocess(signal,
(originatingSignal, shouldStart) -> {
final var entityIdOptional = WithEntityId.getEntityId(signal);
if (shouldStart && entityIdOptional.isPresent() &&
originatingSignal instanceof Command<?> originatingCommand) {
return doStart(entityIdOptional.get(),
originatingCommand,
timeoutOverride,
responseSignalConsumer::apply,
(ackregator, adjustedSignal) ->
ackregatorStartedFunction.apply(adjustedSignal, ackregator)
);
} else {
return ackregatorNotStartedFunction.apply(originatingSignal);
}
},
responseSignalConsumer
);
}
/**
* Preprocess a signal for starting acknowledgement aggregator actors.
*
* @param signal the signal for which an acknowledgement aggregator should start.
* @param preprocessor what to do. The first parameter is the signal with requested-acks and response-required
* set. The second parameter is whether an acknowledgement aggregator should start.
* @param onInvalidHeader what to do if the headers are invalid.
* @param <T> the type of results.
* @return the result.
*/
public <T> T preprocess(final Signal<?> signal,
final BiFunction<Signal<?>, Boolean, T> preprocessor,
final Function<? super DittoHeaderInvalidException, T> onInvalidHeader) {
final var signalToForward = ackRequestSetter.apply(signal);
final var headerInvalid = getDittoHeaderInvalidException(signalToForward);
return headerInvalid.map(onInvalidHeader)
.orElseGet(() -> preprocessor.apply(signalToForward, shouldStartForIncoming(signalToForward)));
}
/**
* Start an acknowledgement aggregator actor for a signal with acknowledgement requests.
*
* @param <T> type of results.
* @param entityId the entity ID of the originating signal.
* @param command the originating command. Must have nonempty acknowledgement requests.
* @param timeoutOverride override timeout of the signal by another duration.
* @param responseSignalConsumer consumer of the aggregated response or error.
* @param forwarderStartedFunction what to do after the aggregator actor started.
* @return the result.
*/
public <T> T doStart(final EntityId entityId,
final Command<?> command,
@Nullable final Duration timeoutOverride,
final Consumer<Object> responseSignalConsumer,
final BiFunction<ActorRef, Signal<?>, T> forwarderStartedFunction) {
final ActorRef ackgregator =
startAckAggregatorActor(entityId, command, timeoutOverride, responseSignalConsumer);
final String addressSerializationFormat = ackgregator.path()
.toSerializationFormatWithAddress(selfRemoteAddress);
final Signal<?> adjustedSignal = command.setDittoHeaders(command.getDittoHeaders().toBuilder()
.putHeader(DittoHeaderDefinition.DITTO_ACKREGATOR_ADDRESS.getKey(), addressSerializationFormat)
.build());
return forwarderStartedFunction.apply(ackgregator, adjustedSignal);
}
private <C extends Command<?>> ActorRef startAckAggregatorActor(final EntityId entityId,
final C command,
@Nullable final Duration timeoutOverride,
final Consumer<Object> responseSignalConsumer) {
final CommandResponseAcknowledgementProvider<C> acknowledgementProvider =
findRelevantAcknowledgementProvider(command)
.orElseThrow(() -> {
LOGGER.withCorrelationId(command)
.error("Tried to start acknowledgement aggregator for command <{}> but don't " +
"know any applicable acknowledgement providers for this.", command);
return DittoInternalErrorException.newBuilder()
.dittoHeaders(command.getDittoHeaders())
.build();
});
final var props = AcknowledgementAggregatorActor.props(entityId,
command,
timeoutOverride,
maxTimeout,
headerTranslator,
responseSignalConsumer,
matchingValidationFailureConsumer,
acknowledgementProvider);
return actorRefFactory.actorOf(props, getNextActorName(command.getDittoHeaders()));
}
@SuppressWarnings("unchecked")
private <C extends Command<?>> Optional<CommandResponseAcknowledgementProvider<C>> findRelevantAcknowledgementProvider(
final C signal) {
return responseAcknowledgementProviders.stream()
.filter(provider -> provider.getCommandClass().isAssignableFrom(signal.getClass()))
.map(provider -> (CommandResponseAcknowledgementProvider<C>) provider)
.findAny();
}
private String getNextActorName(final DittoHeaders dittoHeaders) {
final var correlationId = dittoHeaders.getCorrelationId()
.map(cid -> URLEncoder.encode(cid, StandardCharsets.UTF_8))
.orElse("_");
return String.format("ackr%x-%s", childCounter++, correlationId);
}
@SuppressWarnings({"unchecked", "rawtypes", "java:S3740"})
private static PartialFunction<Signal<?>, Signal<?>> buildAckRequestSetter(
final Collection<AbstractCommandAckRequestSetter<?>> ackRequestSetters) {
PFBuilder<Signal<?>, Signal<?>> pfBuilder = new PFBuilder<>();
// unavoidable raw type due to the lack of existential type
for (final AbstractCommandAckRequestSetter ackRequestSetter : ackRequestSetters) {
pfBuilder = pfBuilder.match(ackRequestSetter.getMatchedClass(),
ackRequestSetter::isApplicable,
s -> (Signal<?>) ackRequestSetter.apply(s));
}
return pfBuilder.matchAny(x -> x).build();
}
private static Optional<DittoHeaderInvalidException> getDittoHeaderInvalidException(final Signal<?> signal) {
final Optional<DittoHeaderInvalidException> result;
final var dittoHeaders = signal.getDittoHeaders();
if (isTimeoutHeaderInvalid(dittoHeaders)) {
final var invalidHeaderKey = DittoHeaderDefinition.TIMEOUT.getKey();
final var message = String.format("The value of the header '%s' must not be zero if " +
"response or acknowledgements are requested.", invalidHeaderKey);
result = Optional.of(DittoHeaderInvalidException.newBuilder()
.withInvalidHeaderKey(invalidHeaderKey)
.message(message)
.description("Please provide a positive timeout.")
.dittoHeaders(dittoHeaders)
.build());
} else {
result = Optional.empty();
}
return result;
}
private static boolean isTimeoutHeaderInvalid(final DittoHeaders dittoHeaders) {
final boolean result;
final var isTimeoutZero = dittoHeaders.getTimeout().filter(Duration::isZero).isPresent();
if (isTimeoutZero) {
if (dittoHeaders.isResponseRequired()) {
result = true;
} else {
final var acknowledgementRequests = dittoHeaders.getAcknowledgementRequests();
result = !acknowledgementRequests.isEmpty();
}
} else {
result = false;
}
return result;
}
}