-
Notifications
You must be signed in to change notification settings - Fork 215
/
ConnectivityRootActor.java
192 lines (163 loc) · 10 KB
/
ConnectivityRootActor.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
/*
* 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.connectivity.service;
import javax.jms.JMSRuntimeException;
import javax.naming.NamingException;
import org.eclipse.ditto.base.service.RootChildActorStarter;
import org.eclipse.ditto.base.service.actors.DittoRootActor;
import org.eclipse.ditto.connectivity.api.ConnectivityMessagingConstants;
import org.eclipse.ditto.connectivity.service.config.ConnectionIdsRetrievalConfig;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.enforcement.ConnectionEnforcerActorPropsFactory;
import org.eclipse.ditto.connectivity.service.messaging.ConnectionIdsRetrievalActor;
import org.eclipse.ditto.connectivity.service.messaging.persistence.ConnectionPersistenceOperationsActor;
import org.eclipse.ditto.connectivity.service.messaging.persistence.ConnectionPersistenceStreamingActorCreator;
import org.eclipse.ditto.connectivity.service.messaging.persistence.ConnectionSupervisorActor;
import org.eclipse.ditto.edge.service.dispatching.EdgeCommandForwarderActor;
import org.eclipse.ditto.edge.service.dispatching.ShardRegions;
import org.eclipse.ditto.base.service.signaltransformer.SignalTransformer;
import org.eclipse.ditto.base.service.signaltransformer.SignalTransformers;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.cluster.ClusterUtil;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.cluster.ShardRegionExtractor;
import org.eclipse.ditto.internal.utils.cluster.config.ClusterConfig;
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import org.eclipse.ditto.internal.utils.health.DefaultHealthCheckingActorFactory;
import org.eclipse.ditto.internal.utils.health.HealthCheckingActorOptions;
import org.eclipse.ditto.internal.utils.health.config.HealthCheckConfig;
import org.eclipse.ditto.internal.utils.health.config.PersistenceConfig;
import org.eclipse.ditto.internal.utils.persistence.mongo.MongoHealthChecker;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal;
import org.eclipse.ditto.internal.utils.persistentactors.PersistencePingActor;
import org.eclipse.ditto.internal.utils.persistentactors.cleanup.PersistenceCleanupActor;
import org.eclipse.ditto.internal.utils.pubsubthings.DittoProtocolSub;
import com.typesafe.config.Config;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.SupervisorStrategy;
import akka.cluster.sharding.ClusterSharding;
import akka.cluster.sharding.ClusterShardingSettings;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.pf.DeciderBuilder;
import scala.PartialFunction;
/**
* Parent Actor which takes care of supervision of all other Actors in our system.
*/
public final class ConnectivityRootActor extends DittoRootActor {
/**
* The name of this Actor in the ActorSystem.
*/
public static final String ACTOR_NAME = "connectivityRoot";
private static final String CLUSTER_ROLE = "connectivity";
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
@SuppressWarnings("unused")
private ConnectivityRootActor(final ConnectivityConfig connectivityConfig,
final ActorRef pubSubMediator) {
final ClusterConfig clusterConfig = connectivityConfig.getClusterConfig();
final ActorSystem actorSystem = getContext().system();
final ActorRef commandForwarder = getCommandForwarder(clusterConfig, pubSubMediator);
final var dittoExtensionsConfig = ScopedConfig.dittoExtension(actorSystem.settings().config());
final var enforcerActorPropsFactory =
ConnectionEnforcerActorPropsFactory.get(actorSystem, dittoExtensionsConfig);
final var connectionSupervisorProps =
ConnectionSupervisorActor.props(commandForwarder, pubSubMediator, enforcerActorPropsFactory);
// Create persistence streaming actor (with no cache) and make it known to pubSubMediator.
final ActorRef persistenceStreamingActor =
startChildActor(ConnectionPersistenceStreamingActorCreator.ACTOR_NAME,
ConnectionPersistenceStreamingActorCreator.props());
pubSubMediator.tell(DistPubSubAccess.put(persistenceStreamingActor), getSelf());
// start DittoProtocolSub extension, even if not passed to connections via reference
// because of serialization issues the single BaseClientActors "get" the extension themselves
// it must however be started here in order to already participate in Ditto pub/sub, even if no connection is
// available!
DittoProtocolSub.get(actorSystem);
final MongoReadJournal mongoReadJournal = MongoReadJournal.newInstance(actorSystem);
startClusterSingletonActor(
PersistencePingActor.props(
startConnectionShardRegion(actorSystem, connectionSupervisorProps, clusterConfig),
connectivityConfig.getPingConfig(), mongoReadJournal),
PersistencePingActor.ACTOR_NAME);
final Config config = actorSystem.settings().config();
final ConnectionIdsRetrievalConfig connectionIdsRetrievalConfig =
connectivityConfig.getConnectionIdsRetrievalConfig();
final ActorRef connectionIdsRetrievalActor = startChildActor(
ConnectionIdsRetrievalActor.ACTOR_NAME,
ConnectionIdsRetrievalActor.props(mongoReadJournal, connectionIdsRetrievalConfig));
startChildActor(ConnectionPersistenceOperationsActor.ACTOR_NAME,
ConnectionPersistenceOperationsActor.props(pubSubMediator, connectivityConfig.getMongoDbConfig(),
config, connectivityConfig.getPersistenceOperationsConfig()));
RootChildActorStarter.get(actorSystem, ScopedConfig.dittoExtension(config)).execute(getContext());
final var cleanupConfig = connectivityConfig.getConnectionConfig().getCleanupConfig();
final var cleanupActorProps = PersistenceCleanupActor.props(cleanupConfig, mongoReadJournal, CLUSTER_ROLE);
startChildActor(PersistenceCleanupActor.NAME, cleanupActorProps);
final ActorRef healthCheckingActor = getHealthCheckingActor(connectivityConfig);
bindHttpStatusRoute(connectivityConfig.getHttpConfig(), healthCheckingActor);
}
/**
* Creates Akka configuration object Props for this ConnectivityRootActor.
*
* @param connectivityConfig the configuration of the Connectivity service.
* @param pubSubMediator the PubSub mediator Actor.
* @return the Akka configuration Props object.
*/
public static Props props(final ConnectivityConfig connectivityConfig, final ActorRef pubSubMediator) {
return Props.create(ConnectivityRootActor.class, connectivityConfig, pubSubMediator);
}
@Override
protected PartialFunction<Throwable, SupervisorStrategy.Directive> getSupervisionDecider() {
return DeciderBuilder.match(JMSRuntimeException.class, e -> {
log.warning("JMSRuntimeException '{}' occurred.", e.getMessage());
return restartChild();
}).match(NamingException.class, e -> {
log.warning("NamingException '{}' occurred.", e.getMessage());
return restartChild();
}).build().orElse(super.getSupervisionDecider());
}
private void startClusterSingletonActor(final Props props, final String name) {
ClusterUtil.startSingleton(getContext(), CLUSTER_ROLE, name, props);
}
private ActorRef getHealthCheckingActor(final ConnectivityConfig connectivityConfig) {
final HealthCheckConfig healthCheckConfig = connectivityConfig.getHealthCheckConfig();
final HealthCheckingActorOptions.Builder hcBuilder =
HealthCheckingActorOptions.getBuilder(healthCheckConfig.isEnabled(), healthCheckConfig.getInterval());
final PersistenceConfig persistenceConfig = healthCheckConfig.getPersistenceConfig();
if (persistenceConfig.isEnabled()) {
hcBuilder.enablePersistenceCheck();
}
final HealthCheckingActorOptions healthCheckingActorOptions = hcBuilder.build();
return startChildActor(DefaultHealthCheckingActorFactory.ACTOR_NAME,
DefaultHealthCheckingActorFactory.props(healthCheckingActorOptions,
MongoHealthChecker.props()
));
}
private ActorRef getCommandForwarder(final ClusterConfig clusterConfig, final ActorRef pubSubMediator) {
final ActorSystem system = getContext().system();
final Config dittoExtensionConfig = ScopedConfig.dittoExtension(system.settings().config());
final SignalTransformer signalTransformer = SignalTransformers.get(system, dittoExtensionConfig);
return startChildActor(EdgeCommandForwarderActor.ACTOR_NAME,
EdgeCommandForwarderActor.props(pubSubMediator,
ShardRegions.of(getContext().getSystem(), clusterConfig), signalTransformer));
}
private static ActorRef startConnectionShardRegion(final ActorSystem actorSystem,
final Props connectionSupervisorProps, final ClusterConfig clusterConfig) {
final ClusterShardingSettings shardingSettings = ClusterShardingSettings.create(actorSystem)
.withRole(ConnectivityMessagingConstants.CLUSTER_ROLE);
return ClusterSharding.get(actorSystem)
.start(ConnectivityMessagingConstants.SHARD_REGION,
connectionSupervisorProps,
shardingSettings,
ShardRegionExtractor.of(clusterConfig.getNumberOfShards(), actorSystem));
}
}