-
Notifications
You must be signed in to change notification settings - Fork 214
/
SupervisorSmartChannelDispatching.java
279 lines (234 loc) · 13.3 KB
/
SupervisorSmartChannelDispatching.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
/*
* Copyright (c) 2022 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.things.service.persistence.actors;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import org.eclipse.ditto.base.model.exceptions.AskException;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.DittoHeadersBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeadersSettable;
import org.eclipse.ditto.base.model.headers.LiveChannelTimeoutStrategy;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.ErrorResponse;
import org.eclipse.ditto.base.model.signals.commands.exceptions.CommandTimeoutException;
import org.eclipse.ditto.internal.models.signal.CommandHeaderRestoration;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLoggingAdapter;
import org.eclipse.ditto.internal.utils.persistentactors.TargetActorWithMessage;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommand;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommandResponse;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSelection;
import org.apache.pekko.pattern.AskTimeoutException;
import org.apache.pekko.pattern.Patterns;
/**
* Functionality used in {@link ThingSupervisorActor} for the "smart channel selection"
* (a.k.a. "live-channel-condition" parameter handling).
*/
final class SupervisorSmartChannelDispatching {
private static final Duration DEFAULT_LIVE_TIMEOUT = Duration.ofSeconds(60L);
private static final Duration DEFAULT_LOCAL_ASK_TIMEOUT = Duration.ofSeconds(5);
private final ThreadSafeDittoLoggingAdapter log;
private final ActorSelection thingsPersistenceActor;
private final SupervisorLiveChannelDispatching liveChannelDispatching;
SupervisorSmartChannelDispatching(final ThreadSafeDittoLoggingAdapter log,
final ActorSelection thingsPersistenceActor,
final SupervisorLiveChannelDispatching liveChannelDispatching) {
this.log = log;
this.thingsPersistenceActor = thingsPersistenceActor;
this.liveChannelDispatching = liveChannelDispatching;
}
/**
* Initiate the "smart channel selection" for the passed in {@code thingQueryCommand}, meaning the following steps
* are performed until the returned CompletionStage is completed:
* <ul>
* <li>The thing persistence actor is asked for the {@code thingQueryCommand} in order to determine the persisted
* state and perform potentially other conditions</li>
* <li>Determines whether based on the {@code live-channel-condition} contained in the headers and the returned
* response from the twin, the query command should be converted to a "live" command as well</li>
* <li>Dispatches the live command via the {@link SupervisorLiveChannelDispatching}</li>
* <li>Applies a fallback (if configured for the command) if no "live" response arrives in time</li>
* </ul>
*
* @param thingQueryCommand the command to handle as "smart channel selection" query command
* @param sender the original sender of the command required for responding the live response to
* @return a CompletionStage which will be completed with a target actor and a message to send to this target actor
*/
CompletionStage<TargetActorWithMessage> dispatchSmartChannelThingQueryCommand(
final ThingQueryCommand<?> thingQueryCommand,
final ActorRef sender) {
return initSmartChannelSelection(thingQueryCommand)
.thenCompose(twinQueryCommandResponse -> {
if (shouldAttemptLiveChannel(thingQueryCommand, twinQueryCommandResponse)) {
// perform conversion + publishing of live command
final ThingQueryCommand<?> liveCommand = toLiveCommand(thingQueryCommand);
return liveChannelDispatching.dispatchLiveChannelThingQueryCommand(liveCommand,
(command, receiver) ->
liveChannelDispatching.prepareForPubSubPublishing(command, receiver,
response ->
getFallbackResponseCaster(liveCommand, twinQueryCommandResponse)
.apply(response)
)
);
} else {
// directly respond with twin response to sender
return CompletableFuture.completedStage(new TargetActorWithMessage(
sender,
twinQueryCommandResponse,
Duration.ZERO,
Function.identity()
));
}
});
}
private CompletionStage<ThingQueryCommandResponse<?>> initSmartChannelSelection(
final ThingQueryCommand<?> thingQueryCommand) {
final ThingQueryCommand<?> twinQueryCommand = ensureTwinChannel(thingQueryCommand);
return Patterns.ask(thingsPersistenceActor, twinQueryCommand, DEFAULT_LOCAL_ASK_TIMEOUT)
.thenApply(response -> handleSmartChannelTwinResponse(thingQueryCommand, response));
}
private static ThingQueryCommandResponse<?> handleSmartChannelTwinResponse(
final ThingQueryCommand<?> enforcedThingQueryCommand,
final Object response) {
if (response instanceof ThingQueryCommandResponse<?> thingQueryCommandResponse) {
final ThingQueryCommandResponse<?> twinResponseWithTwinChannel = setTwinChannel(thingQueryCommandResponse);
return CommandHeaderRestoration.restoreCommandConnectivityHeaders(
twinResponseWithTwinChannel, enforcedThingQueryCommand.getDittoHeaders()
);
} else {
throw new IllegalArgumentException("Response was not as expected a thing query command response!");
}
}
private Function<Object, ThingQueryCommandResponse<?>> getFallbackResponseCaster(
final ThingQueryCommand<?> liveCommand,
final ThingQueryCommandResponse<?> twinResponse) {
return response -> {
if (response instanceof ThingQueryCommandResponse) {
return setAdditionalHeaders((ThingQueryCommandResponse<?>) response, liveCommand.getDittoHeaders());
} else if (response instanceof ErrorResponse) {
throw setAdditionalHeaders(((ErrorResponse<?>) response),
liveCommand.getDittoHeaders()).getDittoRuntimeException();
} else if (response instanceof Throwable t) {
Throwable throwable = t;
if (response instanceof CompletionException completionException) {
throwable = completionException.getCause();
}
if (throwable instanceof AskException || throwable instanceof AskTimeoutException) {
return applyTimeoutStrategy(liveCommand, twinResponse);
} else {
log.withCorrelationId(liveCommand)
.error("Unknown throwable during smart channel response casting: <{}: {}>",
throwable.getClass().getSimpleName(), throwable.getMessage());
throw DittoRuntimeException.asDittoRuntimeException(throwable, cause ->
DittoInternalErrorException.newBuilder()
.cause(cause)
.dittoHeaders(liveCommand.getDittoHeaders())
.build()
);
}
}
log.withCorrelationId(liveCommand)
.error("Unknown response during smart channel response casting: {}", response);
throw DittoInternalErrorException.newBuilder()
.dittoHeaders(liveCommand.getDittoHeaders())
.build();
};
}
private static boolean shouldAttemptLiveChannel(final ThingQueryCommand<?> command,
final ThingQueryCommandResponse<?> twinResponse) {
return isLiveChannelConditionMatched(command, twinResponse) || isLiveQueryCommandWithTimeoutStrategy(command);
}
private static ThingQueryCommand<?> toLiveCommand(final ThingQueryCommand<?> command) {
return command.setDittoHeaders(command.getDittoHeaders().toBuilder()
.liveChannelCondition(null)
.channel(Signal.CHANNEL_LIVE)
.build());
}
private static ThingQueryCommand<?> ensureTwinChannel(final ThingQueryCommand<?> command) {
if (Signal.isChannelLive(command)) {
return command.setDittoHeaders(command.getDittoHeaders()
.toBuilder()
.channel(Signal.CHANNEL_TWIN)
.build());
} else {
return command;
}
}
private static ThingQueryCommandResponse<?> setTwinChannel(final ThingQueryCommandResponse<?> response) {
return response.setDittoHeaders(response.getDittoHeaders()
.toBuilder()
.channel(Signal.CHANNEL_TWIN)
.putHeaders(getAdditionalLiveResponseHeaders(response.getDittoHeaders()))
.build());
}
private static boolean isLiveChannelConditionMatched(final ThingQueryCommand<?> command,
final ThingQueryCommandResponse<?> twinResponse) {
return command.getDittoHeaders().getLiveChannelCondition().isPresent() &&
twinResponse.getDittoHeaders().didLiveChannelConditionMatch();
}
private static boolean isLiveQueryCommandWithTimeoutStrategy(final Signal<?> command) {
return command instanceof ThingQueryCommand &&
command.getDittoHeaders().getLiveChannelTimeoutStrategy().isPresent() &&
Signal.isChannelLive(command);
}
@SuppressWarnings("unchecked")
private static <T extends DittoHeadersSettable<?>> T setAdditionalHeaders(final T settable,
final DittoHeaders commandHeaders) {
final DittoHeaders dittoHeaders = settable.getDittoHeaders();
final DittoHeadersSettable<?> theSettable = settable.setDittoHeaders(dittoHeaders
.toBuilder()
.putHeaders(getAdditionalLiveResponseHeaders(dittoHeaders))
.build());
return (T) CommandHeaderRestoration.restoreCommandConnectivityHeaders(theSettable, commandHeaders);
}
private static ThingQueryCommandResponse<?> applyTimeoutStrategy(
final ThingCommand<?> command,
final ThingQueryCommandResponse<?> twinResponse) {
if (isTwinFallbackEnabled(twinResponse)) {
return twinResponse;
} else {
final var timeout = getLiveSignalTimeout(command);
final CommandTimeoutException timeoutException = CommandTimeoutException.newBuilder(timeout)
.dittoHeaders(twinResponse.getDittoHeaders()
.toBuilder()
.channel(Signal.CHANNEL_LIVE)
.putHeaders(getAdditionalLiveResponseHeaders(twinResponse.getDittoHeaders()))
.build())
.build();
throw CommandHeaderRestoration.restoreCommandConnectivityHeaders(timeoutException,
command.getDittoHeaders());
}
}
private static Duration getLiveSignalTimeout(final Signal<?> signal) {
return signal.getDittoHeaders().getTimeout().orElse(DEFAULT_LIVE_TIMEOUT);
}
private static boolean isTwinFallbackEnabled(final Signal<?> signal) {
final var liveChannelFallbackStrategy =
signal.getDittoHeaders().getLiveChannelTimeoutStrategy().orElse(LiveChannelTimeoutStrategy.FAIL);
return LiveChannelTimeoutStrategy.USE_TWIN == liveChannelFallbackStrategy;
}
private static DittoHeaders getAdditionalLiveResponseHeaders(final DittoHeaders responseHeaders) {
final var liveChannelConditionMatched = responseHeaders.getOrDefault(
DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(), Boolean.TRUE.toString());
final DittoHeadersBuilder<?, ?> dittoHeadersBuilder = DittoHeaders.newBuilder()
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(), liveChannelConditionMatched)
.responseRequired(false);
return dittoHeadersBuilder.build();
}
}