-
Notifications
You must be signed in to change notification settings - Fork 215
/
DispatcherActor.java
327 lines (277 loc) · 13.5 KB
/
DispatcherActor.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
/*
* 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.concierge.service.starter.actors;
import static org.eclipse.ditto.concierge.api.ConciergeMessagingConstants.DISPATCHER_ACTOR_PATH;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.headers.DittoHeadersSettable;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.concierge.service.common.DittoConciergeConfig;
import org.eclipse.ditto.concierge.service.common.EnforcementConfig;
import org.eclipse.ditto.concierge.service.enforcement.PreEnforcer;
import org.eclipse.ditto.internal.utils.akka.controlflow.AbstractGraphActor;
import org.eclipse.ditto.internal.utils.akka.controlflow.Filter;
import org.eclipse.ditto.internal.utils.akka.controlflow.WithSender;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThings;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThings;
import org.eclipse.ditto.thingsearch.api.commands.sudo.ThingSearchSudoCommand;
import org.eclipse.ditto.thingsearch.model.signals.commands.ThingSearchCommand;
import org.eclipse.ditto.thingsearch.model.signals.commands.query.ThingSearchQueryCommand;
import akka.Done;
import akka.NotUsed;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.stream.FanOutShape2;
import akka.stream.FlowShape;
import akka.stream.Graph;
import akka.stream.SinkShape;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.GraphDSL;
import akka.stream.javadsl.Sink;
/**
* Actor that dispatches signals not authorized by any entity meaning signals without entityId.
*/
public final class DispatcherActor
extends AbstractGraphActor<DispatcherActor.ImmutableDispatch, DittoHeadersSettable<?>> {
/**
* The name of this actor.
*/
public static final String ACTOR_NAME = "dispatcherActor";
private static final Map<String, ThreadSafeDittoLogger> NAMESPACE_INSPECTION_LOGGERS = new HashMap<>();
private final Flow<ImmutableDispatch, ImmutableDispatch, NotUsed> handler;
private final ActorRef thingsAggregatorActor;
private final EnforcementConfig enforcementConfig;
@SuppressWarnings("unused")
private DispatcherActor(final ActorRef enforcerActor,
final ActorRef pubSubMediator,
final Flow<ImmutableDispatch, ImmutableDispatch, NotUsed> handler) {
super(WithDittoHeaders.class, UnaryOperator.identity());
enforcementConfig = DittoConciergeConfig.of(
DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config())
).getEnforcementConfig();
enforcementConfig.getSpecialLoggingInspectedNamespaces()
.forEach(loggedNamespace -> NAMESPACE_INSPECTION_LOGGERS.put(
loggedNamespace,
DittoLoggerFactory.getThreadSafeLogger(DispatcherActor.class.getName() +
".namespace." + loggedNamespace)));
this.handler = handler;
final Props props = ThingsAggregatorActor.props(enforcerActor);
thingsAggregatorActor = getContext().actorOf(props, ThingsAggregatorActor.ACTOR_NAME);
initActor(getSelf(), pubSubMediator);
}
@Override
protected ImmutableDispatch mapMessage(final DittoHeadersSettable<?> message) {
return new ImmutableDispatch(message, getSender(), thingsAggregatorActor);
}
@Override
protected Sink<ImmutableDispatch, ?> createSink() {
return handler.to(
Sink.foreach(dispatch -> logger.withCorrelationId(dispatch.getMessage())
.warning("Unhandled Message in DispatcherActor: <{}>", dispatch))
);
}
@Override
protected int getBufferSize() {
return enforcementConfig.getBufferSize();
}
/**
* Create Akka actor configuration Props object without pre-enforcer.
*
* @param pubSubMediator Akka pub-sub mediator.
* @param enforcerActor address of the enforcer actor.
* @return the Props object.
*/
public static Props props(final String searchActorPath, final ActorRef pubSubMediator,
final ActorRef enforcerActor) {
return props(searchActorPath, pubSubMediator, enforcerActor, CompletableFuture::completedFuture);
}
/**
* Create Akka actor configuration Props object with pre-enforcer.
*
* @param pubSubMediator Akka pub-sub mediator.
* @param enforcerActor the address of the enforcer actor.
* @param preEnforcer the pre-enforcer as graph.
* @return the Props object.
*/
public static Props props(final String searchActorPath, final ActorRef pubSubMediator,
final ActorRef enforcerActor, final PreEnforcer preEnforcer) {
final Flow<ImmutableDispatch, ImmutableDispatch, NotUsed> dispatchFlow =
Flow.fromGraph(createDispatchFlow(searchActorPath, pubSubMediator, preEnforcer));
return Props.create(DispatcherActor.class, enforcerActor, pubSubMediator, dispatchFlow);
}
/**
* Create a stream to dispatch search and things commands.
*
* @param pubSubMediator Akka pub-sub-mediator.
* @return stream to dispatch search and thing commands.
*/
private static Graph<FlowShape<ImmutableDispatch, ImmutableDispatch>, NotUsed> createDispatchFlow(
final String searchActorPath, final ActorRef pubSubMediator,
final PreEnforcer preEnforcer) {
return GraphDSL.create(builder -> {
final FanOutShape2<ImmutableDispatch, ImmutableDispatch, ImmutableDispatch> multiplexSearch =
builder.add(multiplexBy(ThingSearchCommand.class, ThingSearchSudoCommand.class));
final FanOutShape2<ImmutableDispatch, ImmutableDispatch, ImmutableDispatch> multiplexRetrieveThings =
builder.add(multiplexBy(RetrieveThings.class, SudoRetrieveThings.class));
final SinkShape<ImmutableDispatch> forwardToSearchActor =
builder.add(searchActorSink(searchActorPath, pubSubMediator, preEnforcer));
final SinkShape<ImmutableDispatch> forwardToThingsAggregator =
builder.add(thingsAggregatorSink(preEnforcer));
builder.from(multiplexSearch.out0()).to(forwardToSearchActor);
builder.from(multiplexRetrieveThings.out0()).to(forwardToThingsAggregator);
builder.from(multiplexSearch.out1()).toInlet(multiplexRetrieveThings.in());
return FlowShape.of(multiplexSearch.in(), multiplexRetrieveThings.out1());
});
}
private static Graph<FanOutShape2<ImmutableDispatch, ImmutableDispatch, ImmutableDispatch>, NotUsed> multiplexBy(
final Class<?>... classes) {
return Filter.multiplexBy(dispatch ->
Arrays.stream(classes).anyMatch(clazz -> clazz.isInstance(dispatch.getMessage()))
? Optional.of(dispatch)
: Optional.empty());
}
private static Sink<ImmutableDispatch, ?> searchActorSink(final String searchActorPath,
final ActorRef pubSubMediator, final PreEnforcer preEnforcer) {
return Sink.foreach(dispatchToPreEnforce ->
preEnforce(dispatchToPreEnforce, preEnforcer, dispatch -> {
final DittoHeadersSettable<?> command = dispatch.message;
if (command instanceof ThingSearchCommand) {
final ThingSearchCommand<?> searchCommand = (ThingSearchCommand<?>) command;
final Set<String> namespaces = searchCommand.getNamespaces().orElseGet(Set::of);
NAMESPACE_INSPECTION_LOGGERS.entrySet().stream()
.filter(entry -> namespaces.contains(entry.getKey()))
.map(Map.Entry::getValue)
.forEach(l -> {
if (searchCommand instanceof ThingSearchQueryCommand) {
final String filter = ((ThingSearchQueryCommand<?>) searchCommand)
.getFilter().orElse(null);
l.withCorrelationId(command)
.info("Forwarding search query command type <{}> with filter <{}> and fields <{}>",
searchCommand.getType(), filter,
searchCommand.getSelectedFields().orElse(null));
}
});
}
pubSubMediator.tell(DistPubSubAccess.send(searchActorPath, dispatch.getMessage()),
dispatch.getSender());
})
);
}
private static Sink<ImmutableDispatch, ?> thingsAggregatorSink(final PreEnforcer preEnforcer) {
return Sink.foreach(dispatchToPreEnforce ->
preEnforce(dispatchToPreEnforce, preEnforcer, dispatch ->
dispatch.thingsAggregatorActor.tell(dispatch.getMessage(), dispatch.getSender())
)
);
}
private static void preEnforce(final ImmutableDispatch dispatch,
final PreEnforcer preEnforcer,
final Consumer<ImmutableDispatch> andThen) {
preEnforcer.withErrorHandlingAsync(dispatch, Done.done(), newDispatch -> {
andThen.accept(newDispatch);
return CompletableFuture.completedStage(Done.done());
});
}
private static void initActor(final ActorRef self, final ActorRef pubSubMediator) {
sanityCheck(self);
putSelfToPubSubMediator(self, pubSubMediator);
}
/**
* Verify the actor path of self agrees with what is advertised in ditto-concierge-api.
*
* @param self ActorRef of this actor.
*/
private static void sanityCheck(final ActorRef self) {
final String selfPath = self.path().toStringWithoutAddress();
if (!Objects.equals(DISPATCHER_ACTOR_PATH, selfPath)) {
final String message =
String.format("Path of <%s> is <%s>, which does not agree with the advertised path <%s>",
ACTOR_NAME, selfPath, DISPATCHER_ACTOR_PATH);
throw new IllegalStateException(message);
}
}
/**
* Tell PubSubMediator about self so that other actors may send messages here from other cluster nodes.
*
* @param self ActorRef of this actor.
* @param pubSubMediator Akka PubSub mediator.
*/
private static void putSelfToPubSubMediator(final ActorRef self, final ActorRef pubSubMediator) {
pubSubMediator.tell(DistPubSubAccess.put(self), self);
}
/**
* Local immutable implementation of {@link WithSender} containing an additional {@code thingsAggregatorActor}
* reference.
*/
@Immutable
static final class ImmutableDispatch implements WithSender<DittoHeadersSettable<?>> {
private final DittoHeadersSettable<?> message;
private final ActorRef sender;
private final ActorRef thingsAggregatorActor;
private ImmutableDispatch(final DittoHeadersSettable<?> message, final ActorRef sender,
final ActorRef thingsAggregatorActor) {
this.message = message;
this.sender = sender;
this.thingsAggregatorActor = thingsAggregatorActor;
}
@Override
public DittoHeadersSettable<?> getMessage() {
return message;
}
@Override
public ActorRef getSender() {
return sender;
}
@Override
public ImmutableDispatch withMessage(final DittoHeadersSettable<?> newMessage) {
return new ImmutableDispatch(newMessage, sender, thingsAggregatorActor);
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ImmutableDispatch)) {
return false;
}
final ImmutableDispatch that = (ImmutableDispatch) o;
return Objects.equals(message, that.message) &&
Objects.equals(sender, that.sender) &&
Objects.equals(thingsAggregatorActor, that.thingsAggregatorActor);
}
@Override
public int hashCode() {
return Objects.hash(message, sender, thingsAggregatorActor);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"message=" + message +
", sender=" + sender +
", thingsAggregatorActor=" + thingsAggregatorActor +
"]";
}
}
}