/
BaseClientActor.java
935 lines (824 loc) · 41.7 KB
/
BaseClientActor.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
/*
* 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.services.connectivity.messaging;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import static org.eclipse.ditto.services.connectivity.messaging.BaseClientState.CONNECTED;
import static org.eclipse.ditto.services.connectivity.messaging.BaseClientState.CONNECTING;
import static org.eclipse.ditto.services.connectivity.messaging.BaseClientState.DISCONNECTED;
import static org.eclipse.ditto.services.connectivity.messaging.BaseClientState.DISCONNECTING;
import static org.eclipse.ditto.services.connectivity.messaging.BaseClientState.TESTING;
import static org.eclipse.ditto.services.connectivity.messaging.BaseClientState.UNKNOWN;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.WithDittoHeaders;
import org.eclipse.ditto.model.connectivity.Connection;
import org.eclipse.ditto.model.connectivity.ConnectionMetrics;
import org.eclipse.ditto.model.connectivity.ConnectivityModelFactory;
import org.eclipse.ditto.model.connectivity.ConnectivityStatus;
import org.eclipse.ditto.model.connectivity.MappingContext;
import org.eclipse.ditto.model.connectivity.ResourceStatus;
import org.eclipse.ditto.model.connectivity.Source;
import org.eclipse.ditto.model.connectivity.SourceMetrics;
import org.eclipse.ditto.model.connectivity.Target;
import org.eclipse.ditto.model.connectivity.TargetMetrics;
import org.eclipse.ditto.services.connectivity.messaging.internal.ClientConnected;
import org.eclipse.ditto.services.connectivity.messaging.internal.ClientDisconnected;
import org.eclipse.ditto.services.connectivity.messaging.internal.ConnectionFailure;
import org.eclipse.ditto.services.connectivity.messaging.internal.RetrieveAddressStatus;
import org.eclipse.ditto.services.connectivity.messaging.metrics.ConnectivityCounterRegistry;
import org.eclipse.ditto.services.connectivity.util.ConfigKeys;
import org.eclipse.ditto.services.models.connectivity.OutboundSignal;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import org.eclipse.ditto.services.utils.config.ConfigUtil;
import org.eclipse.ditto.services.utils.metrics.DittoMetrics;
import org.eclipse.ditto.services.utils.metrics.instruments.gauge.Gauge;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.connectivity.exceptions.ConnectionFailedException;
import org.eclipse.ditto.signals.commands.connectivity.exceptions.ConnectionSignalIllegalException;
import org.eclipse.ditto.signals.commands.connectivity.modify.CloseConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.CreateConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.OpenConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.ResetConnectionMetrics;
import org.eclipse.ditto.signals.commands.connectivity.modify.TestConnection;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionMetrics;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionMetricsResponse;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionStatus;
import com.typesafe.config.Config;
import akka.actor.AbstractFSM;
import akka.actor.ActorRef;
import akka.actor.FSM;
import akka.actor.Props;
import akka.actor.Status;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.pf.FSMStateFunctionBuilder;
import akka.routing.ConsistentHashingPool;
import akka.routing.ConsistentHashingRouter;
import akka.routing.DefaultResizer;
import akka.routing.Resizer;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
import scala.util.Either;
import scala.util.Left;
import scala.util.Right;
/**
* Base class for ClientActors which implement the connection handling for various connectivity protocols.
* <p>
* The actor expects to receive a {@link CreateConnection} command after it was started. If this command is not received
* within timeout (can be the case when this actor is remotely deployed after the command was sent) the actor requests
* the required information from ConnectionActor.
* </p>
*/
public abstract class BaseClientActor extends AbstractFSM<BaseClientState, BaseClientData> {
protected static final int CONNECTING_TIMEOUT = 10;
protected static final int TEST_CONNECTION_TIMEOUT = 10;
private static final int SOCKET_CHECK_TIMEOUT_MS = 2000;
protected final DiagnosticLoggingAdapter log = LogUtil.obtain(this);
private final ActorRef conciergeForwarder;
private final Gauge clientGauge;
@Nullable private ActorRef messageMappingProcessorActor;
// counter for all child actors ever started to disambiguate between them
private int childActorCount = 0;
protected BaseClientActor(final Connection connection, final ConnectivityStatus desiredConnectionStatus,
final ActorRef conciergeForwarder) {
checkNotNull(connection, "connection");
LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connection.getId());
final Config config = getContext().getSystem().settings().config();
final java.time.Duration javaInitTimeout = config.getDuration(ConfigKeys.Client.INIT_TIMEOUT);
this.conciergeForwarder = conciergeForwarder;
final BaseClientData startingData =
new BaseClientData(connection.getId(), connection, ConnectivityStatus.UNKNOWN,
desiredConnectionStatus, "initialized", Instant.now(), null, null);
final FiniteDuration initTimeout = Duration.create(javaInitTimeout.toMillis(), TimeUnit.MILLISECONDS);
final FiniteDuration connectingTimeout = Duration.create(CONNECTING_TIMEOUT, TimeUnit.SECONDS);
clientGauge = DittoMetrics.gauge("connection_client")
.tag("id", connection.getId())
.tag("type", connection.getConnectionType().getName());
startWith(UNKNOWN, startingData, initTimeout);
// stable states
when(UNKNOWN, inUnknownState());
when(CONNECTED, inConnectedState());
when(DISCONNECTED, inDisconnectedState());
// volatile states that time out
when(CONNECTING, connectingTimeout, inConnectingState());
when(DISCONNECTING, connectingTimeout, inDisconnectingState());
when(TESTING, connectingTimeout, inTestingState());
onTransition(this::onTransition);
whenUnhandled(inAnyState().anyEvent(this::onUnknownEvent));
ConnectivityCounterRegistry.initCountersForConnection(connection);
initialize();
}
/**
* Handles {@link TestConnection} commands by returning a CompletionState of
* {@link akka.actor.Status.Status Status} which may be {@link akka.actor.Status.Success Success} or
* {@link akka.actor.Status.Failure Failure}.
*
* @param connection the Connection to test
* @return the CompletionStage with the test result
*/
protected abstract CompletionStage<Status.Status> doTestConnection(final Connection connection);
/**
* Allocate resources once this {@code Client} connected successfully.
*
* @param clientConnected the ClientConnected message which may be subclassed and thus adding more information
*/
protected abstract void allocateResourcesOnConnection(final ClientConnected clientConnected);
/**
* Clean up everything spawned in {@code allocateResourcesOnConnection}. It should be idempotent.
*/
protected abstract void cleanupResourcesForConnection();
/**
* @return the Actor to use for publishing commandResponses/events.
*/
protected abstract ActorRef getPublisherActor();
/**
* Invoked when this {@code Client} should connect.
*
* @param connection the Connection to use for connecting.
* @param origin the ActorRef which caused the ConnectClient command.
*/
protected abstract void doConnectClient(final Connection connection, @Nullable final ActorRef origin);
/**
* Invoked when this {@code Client} should disconnect.
*
* @param connection the Connection to use for disconnecting.
* @param origin the ActorRef which caused the DisconnectClient command.
*/
protected abstract void doDisconnectClient(final Connection connection, @Nullable final ActorRef origin);
/**
* Release any temporary resources allocated during a connection operation when the operation times out.
* Do nothing by default.
*
* @param state current state of the client actor.
*/
protected void cleanupFurtherResourcesOnConnectionTimeout(final BaseClientState state) {
// do nothing by default
}
/**
* Check whether a {@code ClientConnected}, {@code ClientDisconnected} or {@code ConnectionFailed} is up to date.
* All events are interpreted as up-to-date by default.
*
* @param event an event from somewhere.
* @param state the current actor state.
* @param sender sender of the event.
* @return whether the event is up-to-date and should be interpreted.
*/
protected boolean isEventUpToDate(final Object event, final BaseClientState state, final ActorRef sender) {
return true;
}
/**
* Creates the handler for messages common to all states.
* <p>
* Overwrite and extend by additional matchers.
* </p>
*
* @return an FSM function builder
*/
protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inAnyState() {
return matchEvent(RetrieveConnectionMetrics.class, BaseClientData.class, this::retrieveConnectionMetrics)
.event(RetrieveConnectionStatus.class, BaseClientData.class, this::retrieveConnectionStatus)
.event(ResetConnectionMetrics.class, BaseClientData.class, this::resetConnectionMetrics)
.event(OutboundSignal.class, BaseClientData.class, (signal, data) -> {
handleOutboundSignal(signal);
return stay();
});
}
/**
* @return the optional MessageMappingProcessorActor.
*/
protected final Optional<ActorRef> getMessageMappingProcessorActor() {
return Optional.ofNullable(messageMappingProcessorActor);
}
/**
* Escapes the passed actorName in a actorName valid way.
* Actor name should be a valid URL with ASCII letters, see also {@code akka.actor.ActorPath#isValidPathElement},
* therefor we encode the name as an ASCII URL.
*
* @param name the actorName to escape.
* @return the escaped name.
*/
protected static String escapeActorName(final String name) {
try {
return URLEncoder.encode(name, StandardCharsets.US_ASCII.name());
} catch (final UnsupportedEncodingException e) {
// should never happen, every JDK must support US_ASCII
throw new IllegalStateException(e);
}
}
/**
* Transforms a List of CompletableFutures to a CompletableFuture of a List.
*
* @param futures the stream of futures
* @param <T> the type of the CompletableFuture and the List elements
* @return the CompletableFuture of a List
*/
protected static <T> CompletableFuture<List<T>> collectAsList(final Stream<CompletionStage<T>> futures) {
final CompletableFuture<T>[] futureArray = futures.map(CompletionStage::toCompletableFuture)
.toArray((IntFunction<CompletableFuture<T>[]>) CompletableFuture[]::new);
return CompletableFuture.allOf(futureArray).thenApply(_void ->
Arrays.stream(futureArray)
.map(CompletableFuture::join)
.collect(Collectors.toList()));
}
/**
* Starts a child actor.
*
* @param name the Actor's name
* @param props the Props
* @return the created ActorRef
*/
protected final ActorRef startChildActor(final String name, final Props props) {
log.debug("Starting child actor <{}>.", name);
final String nameEscaped = escapeActorName(name);
return getContext().actorOf(props, nameEscaped);
}
/**
* Start a child actor whose name is guaranteed to be different from all other child actors started by this method.
*
* @param prefix prefix of the child actor name.
* @param props props of the child actor.
* @return the created ActorRef.
*/
protected final ActorRef startChildActorConflictFree(final String prefix, final Props props) {
return startChildActor(nextChildActorName(prefix), props);
}
/**
* Stops a child actor.
*
* @param name the Actor's name
*/
protected final void stopChildActor(final String name) {
final String nameEscaped = escapeActorName(name);
final Optional<ActorRef> child = getContext().findChild(nameEscaped);
if (child.isPresent()) {
log.debug("Stopping child actor <{}>.", nameEscaped);
getContext().stop(child.get());
} else {
log.debug("Cannot stop child actor <{}> because it does not exist.", nameEscaped);
}
}
/**
* Stops a child actor.
*
* @param actor the ActorRef
*/
protected final void stopChildActor(final ActorRef actor) {
log.debug("Stopping child actor <{}>.", actor.path());
getContext().stop(actor);
}
/**
* @return whether this client is consuming at all
*/
protected final boolean isConsuming() {
return !connection().getSources().isEmpty();
}
/**
* @return whether this client is publishing at all
*/
protected final boolean isPublishing() {
return !connection().getTargets().isEmpty();
}
/**
* @return the currently managed Connection
*/
protected final Connection connection() {
return stateData().getConnection();
}
/**
* @return the Connection Id
*/
protected final String connectionId() {
return stateData().getConnectionId();
}
/**
* @return the sources configured for this connection or an empty list if no sources were configured.
*/
protected final List<Source> getSourcesOrEmptyList() {
return connection().getSources();
}
/**
* @return the targets configured for this connection or an empty list if no targets were configured.
*/
protected final List<Target> getTargetsOrEmptyList() {
return connection().getTargets();
}
/**
* Invoked on each transition {@code from} a {@link BaseClientState} {@code to} another.
* <p>
* May be extended to react on special transitions.
* </p>
*
* @param from the previous State
* @param to the next State
*/
private void onTransition(final BaseClientState from, final BaseClientState to) {
LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId());
log.debug("Transition: {} -> {}", from, to);
if (to == CONNECTED) {
clientGauge.increment();
}
if (to == DISCONNECTED) {
clientGauge.decrement();
}
}
private FSMStateFunctionBuilder<BaseClientState, BaseClientData> inUnknownState() {
return matchEvent(OpenConnection.class, BaseClientData.class, this::openConnection)
.event(CloseConnection.class, BaseClientData.class, this::closeConnection)
.event(TestConnection.class, BaseClientData.class, this::testConnection)
.eventEquals(StateTimeout(), BaseClientData.class, (state, data) -> {
if (ConnectivityStatus.OPEN == data.getDesiredConnectionStatus()) {
log.info("Did not receive connect command within init-timeout, connecting");
final OpenConnection openConnection = OpenConnection.of(connectionId(), DittoHeaders.empty());
getSelf().tell(openConnection, getSelf());
} else if (ConnectivityStatus.CLOSED == data.getDesiredConnectionStatus()) {
log.info(
"Did not receive connect command within init-timeout, desired state is closed, going to disconnected state.");
return goTo(DISCONNECTED);
} else {
log.info(
"Did not receive connect command within init-timeout, desired state is {}, do nothing.",
data.getDesiredConnectionStatus());
}
return stay(); // handle self-told commands later
});
}
/**
* Creates the handler for messages in disconnected state.
* Overwrite and extend by additional matchers.
*
* @return an FSM function builder
*/
protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inDisconnectedState() {
return matchEvent(OpenConnection.class, BaseClientData.class, this::openConnection)
.event(TestConnection.class, BaseClientData.class, this::testConnection);
}
/**
* Creates the handler for messages in connecting state.
* Overwrite and extend by additional matchers.
*
* @return an FSM function builder
*/
protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inConnectingState() {
return matchEventEquals(StateTimeout(), BaseClientData.class, this::connectionTimedOut)
.event(ConnectionFailure.class, BaseClientData.class, this::connectionFailure)
.event(ClientConnected.class, BaseClientData.class, this::clientConnected);
}
/**
* Creates the handler for messages in connected state.
* Overwrite and extend by additional matchers.
*
* @return an FSM function builder
*/
protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inConnectedState() {
return matchEvent(CloseConnection.class, BaseClientData.class, this::closeConnection);
}
/**
* Creates the handler for messages in disconnected state.
* Overwrite and extend by additional matchers.
*
* @return an FSM function builder
*/
protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inDisconnectingState() {
return matchEventEquals(StateTimeout(), BaseClientData.class, this::connectionTimedOut)
.event(ConnectionFailure.class, BaseClientData.class, this::connectionFailure)
.event(ClientDisconnected.class, BaseClientData.class, this::clientDisconnected);
}
/**
* Creates the handler for messages in testing state.
* Overwrite and extend by additional matchers.
*
* @return an FSM function builder
*/
protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inTestingState() {
return matchEvent(Status.Status.class, (e, d) -> Objects.equals(getSender(), getSelf()),
(status, data) -> {
final Status.Status answerToPublish = getStatusToReport(status);
data.getSessionSender().ifPresent(sender -> sender.tell(answerToPublish, getSelf()));
return stop();
})
.eventEquals(StateTimeout(), BaseClientData.class, (stats, data) -> {
log.info("test timed out.");
data.getSessionSender().ifPresent(sender -> {
final DittoRuntimeException error = ConnectionFailedException.newBuilder(connectionId())
.description(String.format("Failed to open requested connection within <%d> seconds!",
TEST_CONNECTION_TIMEOUT))
.dittoHeaders(data.getSessionHeaders())
.build();
sender.tell(new Status.Failure(error), getSelf());
});
return stop();
});
}
private State<BaseClientState, BaseClientData> onUnknownEvent(final Object event,
final BaseClientData state) {
Object message = event;
if (event instanceof Failure) {
message = ((Failure) event).cause();
}
if (event instanceof Status.Failure) {
message = ((Status.Failure) event).cause();
}
if (message instanceof Throwable) {
log.error((Throwable) message, "received Exception {} in state {} - status: {} - sender: {}",
message,
stateName(),
state.getConnectionStatus() + ": " + state.getConnectionStatusDetails().orElse(""),
getSender());
} else {
log.warning("received unknown/unsupported message {} in state {} - status: {} - sender: {}",
message,
stateName(),
state.getConnectionStatus() + ": " + state.getConnectionStatusDetails().orElse(""),
getSender());
}
final ActorRef sender = getSender();
if (!Objects.equals(sender, getSelf()) && !Objects.equals(sender, getContext().system().deadLetters())) {
sender.tell(unhandledExceptionForSignalInState(event, stateName()), getSelf());
}
return stay();
}
private FSM.State<BaseClientState, BaseClientData> closeConnection(final CloseConnection closeConnection,
final BaseClientData data) {
final ActorRef sender = getSender();
doDisconnectClient(data.getConnection(), sender);
return goTo(DISCONNECTING).using(setSession(data, sender, closeConnection.getDittoHeaders())
.setDesiredConnectionStatus(ConnectivityStatus.CLOSED)
.setConnectionStatusDetails("closing or deleting connection at " + Instant.now()));
}
private FSM.State<BaseClientState, BaseClientData> openConnection(final OpenConnection openConnection,
final BaseClientData data) {
final ActorRef sender = getSender();
final Connection connection = data.getConnection();
final DittoHeaders dittoHeaders = openConnection.getDittoHeaders();
if (canConnectViaSocket(connection)) {
doConnectClient(connection, sender);
return goTo(CONNECTING).using(setSession(data, sender, dittoHeaders));
} else {
cleanupResourcesForConnection();
final DittoRuntimeException error = newConnectionFailedException(data.getConnection(), dittoHeaders);
sender.tell(new Status.Failure(error), getSelf());
return goTo(UNKNOWN)
.using(data.setConnectionStatus(ConnectivityStatus.FAILED)
.setConnectionStatusDetails(error.getMessage())
.resetSession());
}
}
private FSM.State<BaseClientState, BaseClientData> testConnection(final TestConnection testConnection,
final BaseClientData data) {
final ActorRef self = getSelf();
final ActorRef sender = getSender();
final Connection connection = testConnection.getConnection();
if (!canConnectViaSocket(connection)) {
final ConnectionFailedException connectionFailedException =
newConnectionFailedException(connection, testConnection.getDittoHeaders());
final Status.Status failure = new Status.Failure(connectionFailedException);
getSelf().tell(failure, self);
} else {
final CompletionStage<Status.Status> connectionStatusStage = doTestConnection(connection);
final CompletionStage<Status.Status> mappingStatusStage =
testMessageMappingProcessor(connection.getMappingContext().orElse(null));
connectionStatusStage.toCompletableFuture()
.thenCombine(mappingStatusStage, (connectionStatus, mappingStatus) -> {
if (connectionStatus instanceof Status.Success &&
mappingStatus instanceof Status.Success) {
return new Status.Success("successfully connected + initialized mapper");
} else if (connectionStatus instanceof Status.Failure) {
return connectionStatus;
} else {
return mappingStatus;
}
})
.thenAccept(testStatus -> self.tell(testStatus, self))
.exceptionally(error -> {
self.tell(new Status.Failure(error), self);
return null;
});
}
return goTo(TESTING)
.using(setSession(data, sender, testConnection.getDittoHeaders())
.setConnection(connection)
.setConnectionStatusDetails("Testing connection since " + Instant.now()));
}
private FSM.State<BaseClientState, BaseClientData> connectionTimedOut(final Object event,
final BaseClientData data) {
data.getSessionSender().ifPresent(sender -> {
final DittoRuntimeException error = ConnectionFailedException.newBuilder(connectionId())
.dittoHeaders(data.getSessionHeaders())
.build();
sender.tell(new Status.Failure(error), getSelf());
});
cleanupResourcesForConnection();
cleanupFurtherResourcesOnConnectionTimeout(stateName());
return goTo(UNKNOWN).using(data.resetSession()
.setConnectionStatus(ConnectivityStatus.FAILED)
.setConnectionStatusDetails("Connection timed out at " + Instant.now() + " while " + stateName()));
}
private State<BaseClientState, BaseClientData> clientConnected(final ClientConnected clientConnected,
final BaseClientData data) {
return ifEventUpToDate(clientConnected, () -> {
LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId());
allocateResourcesOnConnection(clientConnected);
data.getSessionSender().ifPresent(origin -> origin.tell(new Status.Success(CONNECTED), getSelf()));
return goTo(CONNECTED).using(data.resetSession()
.setConnectionStatus(ConnectivityStatus.OPEN)
.setConnectionStatusDetails("Connected at " + Instant.now()));
});
}
private State<BaseClientState, BaseClientData> clientDisconnected(final ClientDisconnected event,
final BaseClientData data) {
return ifEventUpToDate(event, () -> {
LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId());
cleanupResourcesForConnection();
data.getSessionSender().ifPresent(sender -> sender.tell(new Status.Success(DISCONNECTED), getSelf()));
return goTo(DISCONNECTED).using(data.resetSession()
.setConnectionStatus(ConnectivityStatus.CLOSED)
.setConnectionStatusDetails("Disconnected at " + Instant.now()));
});
}
private State<BaseClientState, BaseClientData> connectionFailure(final ConnectionFailure event,
final BaseClientData data) {
return ifEventUpToDate(event, () -> {
LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId());
cleanupResourcesForConnection();
data.getSessionSender().ifPresent(sender -> sender.tell(getStatusToReport(event.getFailure()), getSelf()));
return goTo(UNKNOWN).using(data.resetSession()
.setConnectionStatus(ConnectivityStatus.FAILED)
.setConnectionStatusDetails(event.getFailureDescription())
.setSessionSender(getSender())
);
});
}
private State<BaseClientState, BaseClientData> ifEventUpToDate(final Object event,
final Supplier<State<BaseClientState, BaseClientData>> thenExecute) {
final BaseClientState state = stateName();
final ActorRef sender = getSender();
if (isEventUpToDate(event, state, sender)) {
return thenExecute.get();
} else {
log.warning("Received stale event <{}> at state <{}>", event, state);
return stay();
}
}
private FSM.State<BaseClientState, BaseClientData> retrieveConnectionStatus(
final RetrieveConnectionStatus command,
final BaseClientData data) {
LogUtil.enhanceLogWithCorrelationId(log, command);
log.debug("Received RetrieveConnectionStatus message from {}, forwarding to consumers and publishers.",
getSender());
// send to all children (consumers, publishers, except mapping actor)
getContext().getChildren().forEach(child -> {
if (messageMappingProcessorActor != child) {
log.debug("Forwarding RetrieveAddressStatus to child: {}", child.path());
child.tell(RetrieveAddressStatus.getInstance(), getSender());
}
});
final ResourceStatus clientStatus =
ConnectivityModelFactory.newClientStatus(ConfigUtil.instanceIdentifier(),
data.getConnectionStatus(),
"[" + stateName().name() + "] " + data.getConnectionStatusDetails().orElse(""),
getInConnectionStatusSince());
getSender().tell(clientStatus, getSelf());
return stay();
}
private FSM.State<BaseClientState, BaseClientData> retrieveConnectionMetrics(
final RetrieveConnectionMetrics command,
final BaseClientData data) {
LogUtil.enhanceLogWithCorrelationId(log, command);
log.debug("Received RetrieveConnectionMetrics message, gathering metrics.");
final DittoHeaders dittoHeaders = command.getDittoHeaders().toBuilder()
.source(ConfigUtil.instanceIdentifier())
.build();
final SourceMetrics sourceMetrics = ConnectivityCounterRegistry.aggregateSourceMetrics(connectionId());
final TargetMetrics targetMetrics = ConnectivityCounterRegistry.aggregateTargetMetrics(connectionId());
final ConnectionMetrics connectionMetrics =
ConnectivityCounterRegistry.aggregateConnectionMetrics(sourceMetrics, targetMetrics);
this.getSender().tell(
RetrieveConnectionMetricsResponse.of(connectionId(), connectionMetrics, sourceMetrics, targetMetrics,
dittoHeaders),
this.getSelf());
return stay();
}
private FSM.State<BaseClientState, BaseClientData> resetConnectionMetrics(
final ResetConnectionMetrics command,
final BaseClientData data) {
LogUtil.enhanceLogWithCorrelationId(log, command);
log.debug("Received ResetConnectionMetrics message, resetting metrics.");
ConnectivityCounterRegistry.resetCountersForConnection(data.getConnection());
ConnectivityCounterRegistry.initCountersForConnection(data.getConnection());
return stay();
}
private static ConnectionFailedException newConnectionFailedException(final Connection connection,
final DittoHeaders dittoHeaders) {
return ConnectionFailedException
.newBuilder(connection.getId())
.dittoHeaders(dittoHeaders)
.description("Could not establish a connection on '" +
connection.getHostname() + ":" + connection.getPort() + "'. Make sure the " +
"endpoint is reachable and that no firewall prevents the connection.")
.build();
}
private DittoRuntimeException unhandledExceptionForSignalInState(final Object signal,
final BaseClientState state) {
final DittoHeaders headers = signal instanceof WithDittoHeaders
? ((WithDittoHeaders) signal).getDittoHeaders()
: DittoHeaders.empty();
switch (state) {
case CONNECTING:
case DISCONNECTING:
return ConnectionSignalIllegalException.newBuilder(connectionId())
.operationName(state.name().toLowerCase())
.timeout(CONNECTING_TIMEOUT)
.dittoHeaders(headers)
.build();
default:
final String signalType = signal instanceof Signal
? ((Signal) signal).getType()
: "unknown"; // no need to disclose Java class of signal to clients
return ConnectionSignalIllegalException.newBuilder(connectionId())
.illegalSignalForState(signalType, state.name().toLowerCase())
.dittoHeaders(headers)
.build();
}
}
private boolean canConnectViaSocket(final Connection connection) {
return checkHostAndPortForAvailability(connection.getHostname(), connection.getPort());
}
private boolean checkHostAndPortForAvailability(final String host, final int port) {
try (final Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(host, port), SOCKET_CHECK_TIMEOUT_MS);
return true;
} catch (final IOException | IllegalArgumentException ex) {
LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId());
log.warning("Socket could not be opened for <{}:{}> due to <{}:{}>", host, port,
ex.getClass().getCanonicalName(), ex.getMessage());
}
return false;
}
private void handleOutboundSignal(final OutboundSignal signal) {
enhanceLogUtil(signal.getSource());
if (messageMappingProcessorActor != null) {
final Object msg = new ConsistentHashingRouter.ConsistentHashableEnvelope(signal,
signal.getSource().getId());
messageMappingProcessorActor.tell(msg, getSender());
} else {
log.info("Cannot handle <{}> signal as there is no MessageMappingProcessor available.",
signal.getSource().getType());
}
}
private void enhanceLogUtil(final WithDittoHeaders<?> signal) {
LogUtil.enhanceLogWithCorrelationId(log, signal);
LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId());
}
private Instant getInConnectionStatusSince() {
return stateData().getInConnectionStatusSince();
}
private CompletionStage<Status.Status> testMessageMappingProcessor(@Nullable final MappingContext mappingContext) {
try {
// this one throws DittoRuntimeExceptions when the mapper could not be configured
MessageMappingProcessor.of(connectionId(), mappingContext, getContext().getSystem(), log);
return CompletableFuture.completedFuture(new Status.Success("mapping"));
} catch (final DittoRuntimeException dre) {
log.info("Got DittoRuntimeException during initialization of MessageMappingProcessor: {} {} - desc: {}",
dre.getClass().getSimpleName(), dre.getMessage(), dre.getDescription().orElse(""));
getSender().tell(dre, getSelf());
return CompletableFuture.completedFuture(new Status.Failure(dre));
}
}
/**
* Starts the {@link MessageMappingProcessorActor} responsible for payload transformation/mapping as child actor
* behind a (cluster node local) RoundRobin pool and a dynamic resizer from the current mapping context.
*/
protected Either<DittoRuntimeException, ActorRef> startMessageMappingProcessor() {
final MappingContext mappingContext = stateData().getConnection().getMappingContext().orElse(null);
return startMessageMappingProcessor(mappingContext);
}
/**
* Starts the {@link MessageMappingProcessorActor} responsible for payload transformation/mapping as child actor
* behind a (cluster node local) RoundRobin pool and a dynamic resizer.
*
* @param mappingContext the MappingContext containing information about how to map external messages
*/
protected Either<DittoRuntimeException, ActorRef> startMessageMappingProcessor(
@Nullable final MappingContext mappingContext) {
if (!getMessageMappingProcessorActor().isPresent()) {
final Connection connection = connection();
final MessageMappingProcessor processor;
try {
// this one throws DittoRuntimeExceptions when the mapper could not be configured
processor = MessageMappingProcessor.of(connectionId(), mappingContext, getContext().getSystem(), log);
} catch (final DittoRuntimeException dre) {
log.info(
"Got DittoRuntimeException during initialization of MessageMappingProcessor: {} {} - desc: {}",
dre.getClass().getSimpleName(), dre.getMessage(), dre.getDescription().orElse(""));
return Left.apply(dre);
}
log.info("Configured for processing messages with the following MessageMapperRegistry: <{}>",
processor.getRegistry());
log.debug("Starting MessageMappingProcessorActor with pool size of <{}>.",
connection.getProcessorPoolSize());
final Props props =
MessageMappingProcessorActor.props(getPublisherActor(), conciergeForwarder, processor,
connectionId());
final Resizer resizer = new DefaultResizer(1, connection.getProcessorPoolSize());
/*
* By using a ConsistentHashingPool, messages sent to this actor which are wrapped into
* akka.routing.ConsistentHashingRouter.ConsistentHashableEnvelope may define which consistent hashing
* key to use.
* That way the message with the same hash are always sent to the same pooled instance of the
* MessageMappingProcessorActor.
*
* That is needed in order to guarantee message processing order. Otherwise two messages received for the
* same Thing may be processed out-of-order if the mapping of the first message takes longer than the
* mapping of the second message.
* This however will also limit throughput as the used hashing key is often connection source address based
* and does not yet "know" of the Thing ID.
*/
messageMappingProcessorActor = getContext().actorOf(new ConsistentHashingPool(1)
.withDispatcher("message-mapping-processor-dispatcher")
.withResizer(resizer)
.props(props), nextChildActorName(MessageMappingProcessorActor.ACTOR_NAME));
} else {
log.info("MessageMappingProcessor already instantiated: not initializing again.");
}
return Right.apply(messageMappingProcessorActor);
}
protected void stopMessageMappingProcessorActor() {
if (messageMappingProcessorActor != null) {
log.debug("Stopping MessageMappingProcessorActor.");
getContext().stop(messageMappingProcessorActor);
messageMappingProcessorActor = null;
}
}
private String nextChildActorName(final String prefix) {
return prefix + ++childActorCount;
}
private BaseClientData setSession(final BaseClientData data, final ActorRef sender, final DittoHeaders headers) {
if (!Objects.equals(sender, getSelf()) && !Objects.equals(sender, getContext().system().deadLetters())) {
return data.setSessionSender(sender)
.setSessionHeaders(headers);
} else {
return data.resetSession();
}
}
/**
* Add meaningful message to status for reporting.
*
* @param status status to report.
* @return status with meaningful message.
*/
private Status.Status getStatusToReport(final Status.Status status) {
final Status.Status answerToPublish;
if (status instanceof Status.Failure) {
final Status.Failure failure = (Status.Failure) status;
log.info("test failed: <{}>", failure.cause());
if (!(failure.cause() instanceof DittoRuntimeException)) {
final DittoRuntimeException error = ConnectionFailedException.newBuilder(connectionId())
.description(describeEventualCause(failure.cause()))
.dittoHeaders(stateData().getSessionHeaders())
.build();
answerToPublish = new Status.Failure(error);
} else {
answerToPublish = status;
}
} else {
answerToPublish = status;
}
return answerToPublish;
}
private static String describeEventualCause(final Throwable throwable) {
final Throwable cause = throwable.getCause();
if (cause == null || cause == throwable) {
return "Cause: " + throwable.getMessage();
} else {
return describeEventualCause(cause);
}
}
}