-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingsAggregatorProxyActor.java
349 lines (302 loc) · 14.9 KB
/
ThingsAggregatorProxyActor.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
/*
* Copyright (c) 2017 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.aggregator;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.base.model.signals.commands.WithEntity;
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.metrics.DittoMetrics;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.StartedTimer;
import org.eclipse.ditto.internal.utils.tracing.DittoTracing;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThingResponse;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThings;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThingsResponse;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThings;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingsResponse;
import akka.NotUsed;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.japi.pf.PFBuilder;
import akka.japi.pf.ReceiveBuilder;
import akka.pattern.Patterns;
import akka.stream.Materializer;
import akka.stream.SourceRef;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
/**
* Acts as a client for {@code ThingsAggregatorActor} which responds
* to a {@link RetrieveThings} command via a {@link SourceRef} which is a pointer in the cluster emitting the retrieved
* {@link Thing}s one after one in a stream. That ensures that the cluster messages size must not be increased when
* streaming a larger amount of Things in the cluster.
*/
public final class ThingsAggregatorProxyActor extends AbstractActor {
/**
* The name of this Actor in the ActorSystem.
*/
public static final String ACTOR_NAME = "aggregatorProxy";
private static final String TRACE_AGGREGATOR_RETRIEVE_THINGS = "aggregatorproxy_retrievethings";
private static final int ASK_TIMEOUT = 60;
private final DittoDiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final ActorRef targetActor;
private final Materializer materializer;
@SuppressWarnings("unused")
private ThingsAggregatorProxyActor(final ActorRef targetActor) {
this.targetActor = targetActor;
materializer = Materializer.createMaterializer(this::getContext);
}
/**
* Creates Akka configuration object Props for this ThingsAggregatorProxyActor.
*
* @param targetActor the Actor to delegate "asks" for the aggregation to.
* @return the Akka configuration Props object
*/
public static Props props(final ActorRef targetActor) {
return Props.create(ThingsAggregatorProxyActor.class, targetActor);
}
private static void stopTimer(final StartedTimer timer) {
timer.stop(); // stop timer
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RetrieveThings.class, rt -> handleRetrieveThings(rt, rt))
.match(SudoRetrieveThings.class, srt -> handleSudoRetrieveThings(srt, srt))
.match(DistributedPubSubMediator.Send.class, send -> {
final Object msg = send.msg();
if (msg instanceof RetrieveThings) {
handleRetrieveThings((RetrieveThings) msg, send);
} else if (msg instanceof SudoRetrieveThings) {
handleSudoRetrieveThings((SudoRetrieveThings) msg, send);
} else {
log.warning("Got unknown message: {}", send);
unhandled(send);
}
})
.matchAny(m -> {
log.warning("Got unknown message: {}", m);
unhandled(m);
})
.build();
}
private void handleRetrieveThings(final RetrieveThings rt, final Object msgToAsk) {
final List<ThingId> thingIds = rt.getEntityIds();
log.withCorrelationId(rt)
.info("Got '{}' message. Retrieving requested '{}' Things..",
RetrieveThings.class.getSimpleName(), thingIds.size());
final ActorRef sender = getSender();
askTargetActor(rt, thingIds, msgToAsk, sender);
}
private void handleSudoRetrieveThings(final SudoRetrieveThings srt, final Object msgToAsk) {
final List<ThingId> thingIds = srt.getThingIds();
log.withCorrelationId(srt)
.info("Got '{}' message. Retrieving requested '{}' Things..",
SudoRetrieveThings.class.getSimpleName(), thingIds.size());
final ActorRef sender = getSender();
askTargetActor(srt, thingIds, msgToAsk, sender);
}
private void askTargetActor(final Command<?> command, final List<ThingId> thingIds,
final Object msgToAsk, final ActorRef sender) {
Patterns.ask(targetActor, msgToAsk, Duration.ofSeconds(ASK_TIMEOUT))
.thenAccept(response -> {
if (response instanceof SourceRef) {
handleSourceRef((SourceRef<?>) response, thingIds, command, sender);
} else if (response instanceof DittoRuntimeException) {
sender.tell(response, getSelf());
} else {
log.error("Unexpected non-DittoRuntimeException error - responding with " +
"GatewayInternalErrorException. Cause: {} - {}",
response.getClass().getSimpleName(), response);
final DittoInternalErrorException responseEx =
DittoInternalErrorException.newBuilder()
.dittoHeaders(command.getDittoHeaders())
.build();
sender.tell(responseEx, getSelf());
}
});
}
private void handleSourceRef(final SourceRef<?> sourceRef, final List<ThingId> thingIds,
final Command<?> originatingCommand, final ActorRef originatingSender) {
final Function<Jsonifiable<?>, PlainJson> thingPlainJsonSupplier;
final Function<List<PlainJson>, CommandResponse<?>> overallResponseSupplier;
final Function<List<PlainJson>, List<PlainJson>> plainJsonSorter = supplyPlainJsonSorter(thingIds);
if (originatingCommand instanceof SudoRetrieveThings) {
thingPlainJsonSupplier = supplyPlainJsonFromSudoRetrieveThingResponse();
overallResponseSupplier = supplySudoRetrieveThingsResponse(originatingCommand.getDittoHeaders());
} else {
thingPlainJsonSupplier = supplyPlainJsonFromRetrieveThingResponse();
final String namespace = ((RetrieveThings) originatingCommand).getNamespace().orElse(null);
overallResponseSupplier = supplyRetrieveThingsResponse(originatingCommand.getDittoHeaders(), namespace);
}
final StartedTimer timer = DittoMetrics.timer(TRACE_AGGREGATOR_RETRIEVE_THINGS)
.tag("size", Integer.toString(thingIds.size()))
.start();
DittoTracing.wrapTimer(DittoTracing.extractTraceContext(originatingCommand), timer);
final Source<Jsonifiable<?>, NotUsed> thingNotAccessibleExceptionSource = Source.single(
ThingNotAccessibleException.fromMessage("Thing could not be accessed.", DittoHeaders.empty())
);
final CompletionStage<List<PlainJson>> o =
sourceRef.getSource()
.<Jsonifiable<?>>map(Jsonifiable.class::cast)
.orElse(thingNotAccessibleExceptionSource)
.filterNot(DittoRuntimeException.class::isInstance)
.map(thingPlainJsonSupplier::apply)
.log("retrieve-thing-response", log)
.recoverWithRetries(1, new PFBuilder<Throwable, Source<PlainJson, NotUsed>>()
.match(NoSuchElementException.class, nsee -> Source.single(PlainJson.empty()))
.build()
)
.runWith(Sink.seq(), materializer);
final CompletionStage<? extends CommandResponse<?>> commandResponseCompletionStage = o
.thenApply(plainJsonSorter)
.thenApply(overallResponseSupplier::apply)
.thenApply(list -> {
stopTimer(timer);
return list;
});
Patterns.pipe(commandResponseCompletionStage, getContext().dispatcher()).to(originatingSender);
}
private Function<Jsonifiable<?>, PlainJson> supplyPlainJsonFromRetrieveThingResponse() {
return jsonifiable -> {
if (jsonifiable instanceof RetrieveThingResponse) {
final RetrieveThingResponse response = (RetrieveThingResponse) jsonifiable;
final String json = response.getEntityPlainString().orElseGet(() ->
response.getEntity(response.getImplementedSchemaVersion()).toString());
return PlainJson.of(response.getEntityId(), json);
} else if (jsonifiable instanceof WithEntity && jsonifiable instanceof WithEntityId) {
final String json = ((WithEntity<?>) jsonifiable).getEntityPlainString().orElseGet(() ->
((WithEntity<?>) jsonifiable).getEntity(jsonifiable.getImplementedSchemaVersion()).toString());
return PlainJson.of(((WithEntityId) jsonifiable).getEntityId(), json);
} else {
return PlainJson.empty();
}
};
}
private Function<Jsonifiable<?>, PlainJson> supplyPlainJsonFromSudoRetrieveThingResponse() {
return jsonifiable -> {
if (jsonifiable instanceof SudoRetrieveThingResponse) {
final SudoRetrieveThingResponse response = (SudoRetrieveThingResponse) jsonifiable;
final String json = response.getEntityPlainString().orElseGet(() ->
response.getEntity(response.getImplementedSchemaVersion()).toString());
return response.getThing().getEntityId()
.map(thingId -> PlainJson.of(thingId, json))
.orElse(null);
} else {
return null;
}
};
}
private Function<List<PlainJson>, List<PlainJson>> supplyPlainJsonSorter(final List<ThingId> thingIds) {
return plainJsonThings -> {
final Comparator<PlainJson> comparator = (pj1, pj2) -> {
if (!pj1.isEmpty() && !pj2.isEmpty()) {
final ThingId thingId1 = ThingId.of(pj1.getId());
final ThingId thingId2 = ThingId.of(pj2.getId());
return Integer.compare(thingIds.indexOf(thingId1), thingIds.indexOf(thingId2));
} else {
return 0;
}
};
final List<PlainJson> sortedList = new ArrayList<>(plainJsonThings);
sortedList.sort(comparator);
return sortedList;
};
}
private Function<List<PlainJson>, CommandResponse<?>> supplyRetrieveThingsResponse(
final DittoHeaders dittoHeaders,
@Nullable final String namespace) {
return plainJsonThings -> RetrieveThingsResponse.of(plainJsonThings.stream()
.map(PlainJson::getJson)
.filter(Predicate.not(String::isEmpty))
.collect(Collectors.toList()), namespace, dittoHeaders);
}
private Function<List<PlainJson>, CommandResponse<?>> supplySudoRetrieveThingsResponse(
final DittoHeaders dittoHeaders) {
return plainJsonThings -> SudoRetrieveThingsResponse.of(plainJsonThings.stream()
.map(PlainJson::getJson)
.filter(Predicate.not(String::isEmpty))
.collect(Collectors.toList()), dittoHeaders);
}
/**
* Internal representation of an entity's JSON string.
*/
private static final class PlainJson {
private final String id;
private final String json;
private PlainJson(final CharSequence id, final String json) {
this.id = checkNotNull(id, "ID").toString();
this.json = checkNotNull(json, "JSON");
}
static PlainJson empty() {
return new PlainJson("", "");
}
static PlainJson of(final CharSequence id, final String json) {
return new PlainJson(id, json);
}
boolean isEmpty() {
return id.isEmpty() && json.isEmpty();
}
String getId() {
return id;
}
String getJson() {
return json;
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final PlainJson plainJson = (PlainJson) o;
return Objects.equals(id, plainJson.id) &&
Objects.equals(json, plainJson.json);
}
@Override
public int hashCode() {
return Objects.hash(id, json);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"id=" + id +
", json=" + json +
"]";
}
}
}