/
ConnectionActor.java
731 lines (638 loc) · 33.7 KB
/
ConnectionActor.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.connectivity.messaging;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import static org.eclipse.ditto.services.models.connectivity.ConnectivityMessagingConstants.CLUSTER_ROLE;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
import org.eclipse.ditto.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.connectivity.Connection;
import org.eclipse.ditto.model.connectivity.ConnectionStatus;
import org.eclipse.ditto.model.connectivity.MappingContext;
import org.eclipse.ditto.services.connectivity.messaging.persistence.ConnectionData;
import org.eclipse.ditto.services.connectivity.messaging.persistence.ConnectionMongoSnapshotAdapter;
import org.eclipse.ditto.services.connectivity.util.ConfigKeys;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import org.eclipse.ditto.services.utils.persistence.SnapshotAdapter;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.base.Command;
import org.eclipse.ditto.signals.commands.base.CommandResponse;
import org.eclipse.ditto.signals.commands.connectivity.AggregatedConnectivityCommandResponse;
import org.eclipse.ditto.signals.commands.connectivity.ConnectivityCommand;
import org.eclipse.ditto.signals.commands.connectivity.exceptions.ConnectionConflictException;
import org.eclipse.ditto.signals.commands.connectivity.exceptions.ConnectionFailedException;
import org.eclipse.ditto.signals.commands.connectivity.exceptions.ConnectionNotAccessibleException;
import org.eclipse.ditto.signals.commands.connectivity.modify.CloseConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.CloseConnectionResponse;
import org.eclipse.ditto.signals.commands.connectivity.modify.CreateConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.CreateConnectionResponse;
import org.eclipse.ditto.signals.commands.connectivity.modify.DeleteConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.DeleteConnectionResponse;
import org.eclipse.ditto.signals.commands.connectivity.modify.OpenConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.OpenConnectionResponse;
import org.eclipse.ditto.signals.commands.connectivity.modify.TestConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.TestConnectionResponse;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnection;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionMetrics;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionResponse;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionStatus;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionStatusResponse;
import org.eclipse.ditto.signals.events.base.Event;
import org.eclipse.ditto.signals.events.connectivity.ConnectionClosed;
import org.eclipse.ditto.signals.events.connectivity.ConnectionCreated;
import org.eclipse.ditto.signals.events.connectivity.ConnectionDeleted;
import org.eclipse.ditto.signals.events.connectivity.ConnectionOpened;
import org.eclipse.ditto.signals.events.things.ThingEvent;
import com.typesafe.config.Config;
import akka.ConfigurationException;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Cancellable;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.actor.ReceiveTimeout;
import akka.actor.Status;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.cluster.routing.ClusterRouterPool;
import akka.cluster.routing.ClusterRouterPoolSettings;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.Creator;
import akka.japi.pf.ReceiveBuilder;
import akka.pattern.PatternsCS;
import akka.persistence.AbstractPersistentActor;
import akka.persistence.RecoveryCompleted;
import akka.persistence.SaveSnapshotSuccess;
import akka.persistence.SnapshotOffer;
import akka.routing.Broadcast;
import akka.routing.RoundRobinPool;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
/**
* Handles {@code *Connection} commands and manages the persistence of connection. The actual connection handling to the
* remote server is delegated to a child actor that uses a specific client (AMQP 1.0 or 0.9.1).
*/
final class ConnectionActor extends AbstractPersistentActor {
private static final String PERSISTENCE_ID_PREFIX = "connection:";
private static final String JOURNAL_PLUGIN_ID = "akka-contrib-mongodb-persistence-connection-journal";
private static final String SNAPSHOT_PLUGIN_ID = "akka-contrib-mongodb-persistence-connection-snapshots";
private static final int SHUTDOWN_DELAY_SECONDS = 10;
private static final FiniteDuration SHUTDOWN_DELAY = Duration.apply(SHUTDOWN_DELAY_SECONDS, TimeUnit.SECONDS);
private static final long DEFAULT_TIMEOUT_MS = 5000;
private static final String PUB_SUB_GROUP_PREFIX = "connection:";
private final DiagnosticLoggingAdapter log = LogUtil.obtain(this);
private final String connectionId;
private final ActorRef pubSubMediator;
private final long snapshotThreshold;
private final SnapshotAdapter<ConnectionData> snapshotAdapter;
private final ConnectionActorPropsFactory propsFactory;
private final Receive connectionCreatedBehaviour;
private ConnectionStatus connectionStatus;
@Nullable private ActorRef clientActor;
@Nullable private Connection connection;
@Nullable private List<MappingContext> mappingContexts;
@Nullable private Cancellable shutdownCancellable;
private long lastSnapshotSequenceNr = -1L;
private boolean snapshotInProgress = false;
private Set<String> uniqueTopicPaths;
private ConnectionActor(final String connectionId, final ActorRef pubSubMediator,
final ConnectionActorPropsFactory propsFactory) {
this.connectionId = connectionId;
this.pubSubMediator = pubSubMediator;
this.propsFactory = propsFactory;
final Config config = getContext().system().settings().config();
snapshotThreshold = config.getLong(ConfigKeys.Connection.SNAPSHOT_THRESHOLD);
if (snapshotThreshold < 0) {
throw new ConfigurationException(String.format("Config setting '%s' must be positive, but is: %d.",
ConfigKeys.Connection.SNAPSHOT_THRESHOLD, snapshotThreshold));
}
snapshotAdapter = new ConnectionMongoSnapshotAdapter();
connectionStatus = ConnectionStatus.CLOSED;
mappingContexts = Collections.emptyList();
connectionCreatedBehaviour = createConnectionCreatedBehaviour();
}
/**
* Creates Akka configuration object for this actor.
*
* @return the Akka configuration Props object
*/
public static Props props(final String connectionId, final ActorRef pubSubMediator,
final ConnectionActorPropsFactory propsFactory) {
return Props.create(ConnectionActor.class, new Creator<ConnectionActor>() {
private static final long serialVersionUID = 1L;
@Override
public ConnectionActor create() {
return new ConnectionActor(connectionId, pubSubMediator, propsFactory);
}
});
}
@Override
public String persistenceId() {
return PERSISTENCE_ID_PREFIX + connectionId;
}
@Override
public String journalPluginId() {
return JOURNAL_PLUGIN_ID;
}
@Override
public String snapshotPluginId() {
return SNAPSHOT_PLUGIN_ID;
}
@Override
public void postStop() {
super.postStop();
if (shutdownCancellable != null) {
shutdownCancellable.cancel();
}
log.info("stopped connection <{}>", connectionId);
}
@Override
public Receive createReceiveRecover() {
return ReceiveBuilder.create()
// # Snapshot handling
.match(SnapshotOffer.class, ss -> {
final ConnectionData fromSnapshotStore = snapshotAdapter.fromSnapshotStore(ss);
log.info("Received SnapshotOffer containing connectionStatus: <{}>", fromSnapshotStore);
if (fromSnapshotStore != null) {
connection = fromSnapshotStore.getConnection();
mappingContexts = fromSnapshotStore.getMappingContexts();
connectionStatus = fromSnapshotStore.getConnectionStatus();
}
lastSnapshotSequenceNr = ss.metadata().sequenceNr();
})
.match(ConnectionCreated.class, event -> {
connection = event.getConnection();
mappingContexts = event.getMappingContexts();
connectionStatus = ConnectionStatus.OPEN;
})
.match(ConnectionOpened.class, event -> connectionStatus = ConnectionStatus.OPEN)
.match(ConnectionClosed.class, event -> connectionStatus = ConnectionStatus.CLOSED)
.match(ConnectionDeleted.class, event -> {
connection = null;
mappingContexts = Collections.emptyList();
connectionStatus = ConnectionStatus.CLOSED;
})
.match(RecoveryCompleted.class, rc -> {
log.info("Connection '{}' was recovered: {}", connectionId, connection);
if (connection != null) {
if (ConnectionStatus.OPEN.equals(connectionStatus)) {
log.debug("Opening connection {} after recovery.", connectionId);
final CreateConnection connect;
if (mappingContexts != null) {
connect = CreateConnection.of(connection, mappingContexts, DittoHeaders.empty());
} else {
connect = CreateConnection.of(connection, DittoHeaders.empty());
}
final ActorRef origin = getSender();
askClientActor("recovery-connect", connect, origin,
response -> log.info("CreateConnection result: {}", response));
subscribeForEvents();
}
getContext().become(connectionCreatedBehaviour);
}
scheduleShutdown();
getContext().getParent().tell(ConnectionSupervisorActor.ManualReset.getInstance(), getSelf());
})
.matchAny(m -> log.warning("Unknown recover message: {}", m))
.build();
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(TestConnection.class, this::testConnection)
.match(CreateConnection.class, this::createConnection)
.match(ConnectivityCommand.class, this::handleCommandDuringInitialization)
.match(Shutdown.class, shutdown -> stopSelf())
.match(Status.Failure.class, f -> log.warning("Got failure in initial behaviour with cause {}: {}",
f.cause().getClass().getSimpleName(), f.cause().getMessage()))
.matchAny(m -> {
log.warning("Unknown message: {}", m);
unhandled(m);
}).build();
}
private Receive createConnectionCreatedBehaviour() {
return ReceiveBuilder.create()
.match(TestConnection.class, testConnection ->
getSender().tell(
TestConnectionResponse.of(testConnection.getConnectionId(),
"Connection was already created - no test possible",
testConnection.getDittoHeaders()),
getSelf()))
.match(CreateConnection.class, createConnection -> {
LogUtil.enhanceLogWithCorrelationId(log, createConnection);
log.info("Connection <{}> already exists, responding with conflict", createConnection.getId());
final ConnectionConflictException conflictException =
ConnectionConflictException.newBuilder(createConnection.getId())
.dittoHeaders(createConnection.getDittoHeaders())
.build();
getSender().tell(conflictException, getSelf());
})
.match(OpenConnection.class, this::openConnection)
.match(CloseConnection.class, this::closeConnection)
.match(DeleteConnection.class, this::deleteConnection)
.match(RetrieveConnection.class, this::retrieveConnection)
.match(RetrieveConnectionStatus.class, this::retrieveConnectionStatus)
.match(RetrieveConnectionMetrics.class, this::retrieveConnectionMetrics)
.match(Signal.class, this::handleSignal)
.match(DistributedPubSubMediator.SubscribeAck.class, this::handleSubscribeAck)
.match(DistributedPubSubMediator.UnsubscribeAck.class, this::handleUnsubscribeAck)
.match(SaveSnapshotSuccess.class, this::handleSnapshotSuccess)
.match(Shutdown.class, shutdown -> log.debug("Dropping Shutdown in created behaviour state."))
.match(Status.Failure.class, f -> log.warning("Got failure in connectionCreated behaviour with " +
"cause {}: {}", f.cause().getClass().getSimpleName(), f.cause().getMessage()))
.matchAny(m -> {
log.warning("Unknown message: {}", m);
unhandled(m);
}).build();
}
private void handleSignal(final Signal<?> signal) {
if (clientActor == null) {
log.debug("Cannot forward thing event, client actor not ready.");
return;
}
if (connection == null) {
log.debug("No Connection configuration available.");
return;
}
if (uniqueTopicPaths.isEmpty()) {
log.debug("Not forwarding anything.");
return;
}
if (connectionId.equals(signal.getDittoHeaders().get("origin"))) {
log.debug("Dropping signal, was sent by myself.");
return;
}
final String topicPath = TopicPathMapper.mapSignalToTopicPath(signal);
// forward to client actor if topic was subscribed and connection is authorized to read
if (uniqueTopicPaths.contains(topicPath) && isAuthorized(signal, connection.getAuthorizationContext())) {
log.debug("Forwarding signal <{}> to client actor.", signal.getType());
clientActor.tell(signal, getSelf());
}
}
private boolean isAuthorized(final Signal<?> signal, final AuthorizationContext authorizationContext) {
final Set<String> authorizedReadSubjects = signal.getDittoHeaders().getReadSubjects();
final List<String> connectionSubjects = authorizationContext.getAuthorizationSubjectIds();
return !Collections.disjoint(authorizedReadSubjects, connectionSubjects);
}
private void testConnection(final TestConnection command) {
final ActorRef origin = getSender();
connection = command.getConnection();
mappingContexts = command.getMappingContexts();
askClientActor("test", command, origin, response -> {
origin.tell(
TestConnectionResponse.of(command.getConnectionId(), response.toString(),
command.getDittoHeaders()),
getSelf());
});
// terminate this actor's supervisor after a connection test again:
final ActorRef parent = getContext().getParent();
getContext().getSystem().scheduler().scheduleOnce(FiniteDuration.apply(5, TimeUnit.SECONDS), parent,
PoisonPill.getInstance(), getContext().dispatcher(), getSelf());
}
private void createConnection(final CreateConnection command) {
final ConnectionCreated connectionCreated =
ConnectionCreated.of(command.getConnection(), command.getMappingContexts(),
command.getDittoHeaders());
final ActorRef origin = getSender();
persistEvent(connectionCreated, persistedEvent -> {
connection = persistedEvent.getConnection();
mappingContexts = persistedEvent.getMappingContexts();
connectionStatus = ConnectionStatus.OPEN;
askClientActor("connect", command, origin, response -> {
getContext().become(connectionCreatedBehaviour);
subscribeForEvents();
origin.tell(
CreateConnectionResponse.of(connection, mappingContexts, command.getDittoHeaders()),
getSelf());
getContext().getParent().tell(ConnectionSupervisorActor.ManualReset.getInstance(), getSelf());
});
});
}
private void openConnection(final OpenConnection command) {
checkNotNull(connection, "Connection");
final ConnectionOpened connectionOpened =
ConnectionOpened.of(command.getConnectionId(), command.getDittoHeaders());
final ActorRef origin = getSender();
persistEvent(connectionOpened, persistedEvent -> {
connectionStatus = ConnectionStatus.OPEN;
askClientActor("open-connection", command, origin, response -> {
subscribeForEvents();
origin.tell(OpenConnectionResponse.of(connectionId, command.getDittoHeaders()), getSelf());
});
});
}
private void closeConnection(final CloseConnection command) {
final ConnectionClosed connectionClosed =
ConnectionClosed.of(command.getConnectionId(), command.getDittoHeaders());
final ActorRef origin = getSender();
persistEvent(connectionClosed, persistedEvent -> {
connectionStatus = ConnectionStatus.CLOSED;
askClientActor("disconnect", command, origin, response -> {
origin.tell(CloseConnectionResponse.of(connectionId, command.getDittoHeaders()),
getSelf());
unsubscribeFromEvents();
});
});
}
private void deleteConnection(final DeleteConnection command) {
final ConnectionDeleted connectionDeleted =
ConnectionDeleted.of(command.getConnectionId(), command.getDittoHeaders());
final ActorRef origin = getSender();
persistEvent(connectionDeleted,
persistedEvent -> {
connectionStatus = ConnectionStatus.CLOSED;
askClientActor("disconnect", command, origin, response -> {
unsubscribeFromEvents();
stopClientActor();
origin.tell(DeleteConnectionResponse.of(connectionId, command.getDittoHeaders()),
getSelf());
stopSelf();
});
});
}
private void askClientActor(final String action, final Command<?> cmd, final ActorRef origin,
final Consumer<Object> onSuccess) {
startClientActorIfRequired();
final long timeout = Optional.ofNullable(cmd.getDittoHeaders().get("timeout"))
.map(Long::parseLong)
.orElse(DEFAULT_TIMEOUT_MS);
// wrap in Broadcast message because these management messages must be delivered to each client actor
if (clientActor != null && connection != null) {
final ActorRef aggregationActor = getContext().actorOf(
AggregateActor.props(connectionId, clientActor, connection.getClientCount(), timeout));
PatternsCS.ask(aggregationActor, cmd, timeout)
.whenComplete((response, exception) -> {
log.debug("Got response to {}: {}", cmd.getType(), exception == null ? response : exception);
if (exception != null) {
handleException(action, origin, exception);
} else if (response instanceof Status.Failure) {
handleException(action, origin, ((Status.Failure) response).cause());
} else if (response instanceof DittoRuntimeException) {
handleException(action, origin, (DittoRuntimeException) response);
} else {
onSuccess.accept(response);
}
});
}
}
private void handleException(final String action, final ActorRef origin, final Throwable exception) {
final DittoRuntimeException dre;
if (exception instanceof DittoRuntimeException) {
dre = (DittoRuntimeException) exception;
} else {
dre = ConnectionFailedException.newBuilder(connectionId)
.description(exception.getMessage())
.cause(exception)
.build();
}
origin.tell(dre, getSelf());
log.warning("Operation <{}> on connection <{}> failed due to {}: {}.", action, connectionId,
dre.getClass().getSimpleName(), dre.getMessage());
}
private void retrieveConnection(final RetrieveConnection command) {
checkNotNull(connection, "Connection");
checkNotNull(mappingContexts, "MappingContexts");
getSender().tell(RetrieveConnectionResponse.of(connection, mappingContexts, command.getDittoHeaders()),
getSelf());
}
private void retrieveConnectionStatus(final RetrieveConnectionStatus command) {
checkNotNull(connection, "Connection");
checkNotNull(mappingContexts, "MappingContexts");
getSender().tell(RetrieveConnectionStatusResponse.of(connectionId, connectionStatus,
"the status as persisted / desired status", command.getDittoHeaders()), getSelf());
}
private void retrieveConnectionMetrics(final RetrieveConnectionMetrics command) {
checkNotNull(connection, "Connection");
checkNotNull(mappingContexts, "MappingContexts");
final ActorRef origin = getSender();
askClientActor("retrieve-metrics", command, origin, response -> {
origin.tell(response, getSelf());
});
}
private void subscribeForEvents() {
checkNotNull(connection, "Connection");
uniqueTopicPaths = connection.getTargets().stream()
.flatMap(target -> target.getTopics().stream())
.collect(Collectors.toSet());
final Set<String> pubSubTopics = uniqueTopicPaths.stream()
.map(TopicPathMapper::mapToPubSubTopic)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toSet());
pubSubTopics.forEach(pubSubTopic -> {
final DistributedPubSubMediator.Subscribe subscribe =
new DistributedPubSubMediator.Subscribe(pubSubTopic, PUB_SUB_GROUP_PREFIX + connectionId,
getSelf());
log.info("Subscribing to pubsub topic '{}' for connection '{}'.", pubSubTopic, connectionId);
pubSubMediator.tell(subscribe, getSelf());
});
}
private void unsubscribeFromEvents() {
pubSubMediator.tell(
new DistributedPubSubMediator.Unsubscribe(ThingEvent.TYPE_PREFIX, PUB_SUB_GROUP_PREFIX + connectionId,
getSelf()), getSelf());
}
private void handleCommandDuringInitialization(final ConnectivityCommand command) {
log.debug("Unexpected command during initialization of actor received: {} - "
+ "Terminating this actor and sending 'ConnectionNotAccessibleException' to requester..",
command.getType());
getSender().tell(ConnectionNotAccessibleException.newBuilder(command.getId())
.dittoHeaders(command.getDittoHeaders())
.build(), getSelf());
}
private <E extends Event> void persistEvent(final E event, final Consumer<E> consumer) {
persist(event, persistedEvent -> {
log.debug("Successfully persisted Event '{}'", persistedEvent.getType());
consumer.accept(persistedEvent);
pubSubMediator.tell(new DistributedPubSubMediator.Publish(event.getType(), event, true), getSelf());
// save a snapshot if there were too many changes since the last snapshot
if ((lastSequenceNr() - lastSnapshotSequenceNr) > snapshotThreshold) {
doSaveSnapshot();
}
});
}
private void doSaveSnapshot() {
if (snapshotInProgress) {
log.debug("Already requested taking a Snapshot - not doing it again");
} else if (connection != null && mappingContexts != null) {
snapshotInProgress = true;
final ConnectionData connectionData = new ConnectionData(connection, connectionStatus, mappingContexts);
log.info("Attempting to save Snapshot for '{}' ..", connectionData);
// save a snapshot
final Object snapshotToStore = snapshotAdapter.toSnapshotStore(connectionData);
saveSnapshot(snapshotToStore);
} else {
log.warning("Connection and MappingContext must not be null when taking snapshot.");
}
}
private void startClientActorIfRequired() {
checkNotNull(connectionId, "connectionId");
checkNotNull(connection, "connection");
if (clientActor == null) {
final int clientCount = connection.getClientCount();
log.info("Starting ClientActor for connection <{}> with <{}> clients.", connectionId, clientCount);
final Props props = propsFactory.getActorPropsForType(connection, connectionStatus);
final ClusterRouterPoolSettings clusterRouterPoolSettings =
new ClusterRouterPoolSettings(clientCount, 1, true,
Collections.singleton(CLUSTER_ROLE));
final RoundRobinPool roundRobinPool = new RoundRobinPool(clientCount);
final Props clusterRouterPoolProps =
new ClusterRouterPool(roundRobinPool, clusterRouterPoolSettings).props(props);
clientActor = getContext().actorOf(clusterRouterPoolProps, "client-router");
} else {
log.debug("ClientActor already started.");
}
}
private void stopClientActor() {
if (clientActor != null) {
log.debug("Stopping the client actor.");
stopChildActor(clientActor);
clientActor = null;
}
}
private void stopChildActor(final ActorRef actor) {
log.debug("Stopping child actor '{}'", actor.path());
getContext().stop(actor);
}
private void stopSelf() {
log.debug("Shutting down");
// stop the supervisor (otherwise it'd restart this actor) which causes this actor to stop, too.
getContext().getParent().tell(PoisonPill.getInstance(), getSelf());
}
private void scheduleShutdown() {
shutdownCancellable = getContext().getSystem().scheduler()
.scheduleOnce(SHUTDOWN_DELAY,
getSelf(),
Shutdown.getInstance(),
getContext().dispatcher(),
ActorRef.noSender());
}
private void handleSubscribeAck(final DistributedPubSubMediator.SubscribeAck subscribeAck) {
log.debug("Successfully subscribed to distributed pub/sub on topic '{}'", subscribeAck.subscribe().topic());
}
private void handleUnsubscribeAck(final DistributedPubSubMediator.UnsubscribeAck unsubscribeAck) {
log.debug("Successfully unsubscribed from distributed pub/sub on topic '{}'",
unsubscribeAck.unsubscribe().topic());
}
private void handleSnapshotSuccess(final SaveSnapshotSuccess sss) {
log.debug("Snapshot was saved successfully: {}", sss);
}
private static class Shutdown {
private Shutdown() {
// no-op
}
private static Shutdown getInstance() {
return new Shutdown();
}
}
/**
* Local helper-actor which is started for aggregating several CommandResponses sent back by potentially several
* {@code clientActors} (behind a cluster Router running on different cluster nodes).
*/
private static class AggregateActor extends AbstractActor {
private final DiagnosticLoggingAdapter log = LogUtil.obtain(this);
private final List<CommandResponse<?>> aggregatedResults;
private final Map<String, Status.Status> aggregatedStatus;
private final String connectionId;
private final ActorRef clientActor;
private final int expectedResponses;
private final long timeout;
private int responseCount = 0;
@Nullable private ActorRef origin;
@Nullable private DittoHeaders originHeaders;
/**
* Creates Akka configuration object for this actor.
*
* @return the Akka configuration Props object
*/
static Props props(final String connectionId, final ActorRef clientActor, final int expectedResponses,
final long timeout) {
return Props.create(AggregateActor.class, connectionId, clientActor, expectedResponses, timeout);
}
private AggregateActor(final String connectionId, final ActorRef clientActor, final int expectedResponses,
final long timeout) {
this.connectionId = connectionId;
this.clientActor = clientActor;
this.expectedResponses = expectedResponses;
this.timeout = timeout;
aggregatedResults = new ArrayList<>();
aggregatedStatus = new HashMap<>();
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(Command.class, command -> {
clientActor.tell(new Broadcast(command), getSelf());
originHeaders = command.getDittoHeaders();
origin = getSender();
getContext().setReceiveTimeout(Duration.create(timeout / 2.0, TimeUnit.MILLISECONDS));
})
.match(ReceiveTimeout.class, timeout -> {
// send back (partially) gathered responses
sendBackAggregatedResults();
})
.matchAny(any -> {
if (any instanceof CommandResponse) {
aggregatedResults.add((CommandResponse<?>) any);
} else if (any instanceof Status.Status) {
aggregatedStatus.put(getSender().path().address().hostPort(), (Status.Status) any);
} else {
log.error("Could not handle non-Jsonifiable non-Status response: {}", any);
}
responseCount++;
if (expectedResponses == responseCount) {
// send back all gathered responses
sendBackAggregatedResults();
}
})
.build();
}
private void sendBackAggregatedResults() {
if (origin != null && originHeaders != null && !aggregatedResults.isEmpty()) {
final String responseType = aggregatedResults.get(0).getType();
final AggregatedConnectivityCommandResponse response =
AggregatedConnectivityCommandResponse.of(connectionId, aggregatedResults, responseType,
HttpStatusCode.OK, originHeaders);
log.debug("Aggregated response: {}", response);
origin.tell(response, getSelf());
} else if (origin != null && originHeaders != null && !aggregatedStatus.isEmpty()) {
log.debug("Aggregated stati: {}", this.aggregatedStatus);
final Optional<Status.Status> failure = this.aggregatedStatus.entrySet().stream()
.filter(s -> s.getValue() instanceof Status.Failure)
.map(Map.Entry::getValue)
.findFirst();
if (failure.isPresent()) {
origin.tell(failure.get(), getSelf());
} else {
final String aggregatedStatusStr = this.aggregatedStatus.entrySet().stream()
.map(Object::toString)
.collect(Collectors.joining(","));
origin.tell(new Status.Success(aggregatedStatusStr), getSelf());
}
} else {
log.warning("No origin was present or results were empty in order to send back aggregated results to");
}
getContext().stop(getSelf());
}
}
}