-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingsRootActor.java
executable file
·195 lines (165 loc) · 9.95 KB
/
ThingsRootActor.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
/*
* 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.things.service.starter;
import static org.eclipse.ditto.things.api.ThingsMessagingConstants.CLUSTER_ROLE;
import org.eclipse.ditto.base.api.devops.signals.commands.RetrieveStatisticsDetails;
import org.eclipse.ditto.base.service.RootChildActorStarter;
import org.eclipse.ditto.base.service.actors.DittoRootActor;
import org.eclipse.ditto.things.service.aggregation.DefaultThingsAggregatorConfig;
import org.eclipse.ditto.things.service.aggregation.ThingsAggregatorActor;
import org.eclipse.ditto.things.service.aggregation.ThingsAggregatorConfig;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.cluster.RetrieveStatisticsDetailsResponseSupplier;
import org.eclipse.ditto.internal.utils.cluster.ShardRegionExtractor;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
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.namespaces.BlockedNamespaces;
import org.eclipse.ditto.internal.utils.persistence.mongo.MongoClientWrapper;
import org.eclipse.ditto.internal.utils.persistence.mongo.MongoHealthChecker;
import org.eclipse.ditto.internal.utils.persistence.mongo.config.MongoDbConfig;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal;
import org.eclipse.ditto.internal.utils.persistentactors.cleanup.PersistenceCleanupActor;
import org.eclipse.ditto.internal.utils.pubsub.DistributedAcks;
import org.eclipse.ditto.internal.utils.pubsub.DistributedPub;
import org.eclipse.ditto.internal.utils.pubsubthings.LiveSignalPub;
import org.eclipse.ditto.internal.utils.pubsubthings.ThingEventPubSubFactory;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcerProvider;
import org.eclipse.ditto.things.api.ThingsMessagingConstants;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.service.common.config.ThingsConfig;
import org.eclipse.ditto.things.service.persistence.actors.ThingPersistenceActorPropsFactory;
import org.eclipse.ditto.things.service.persistence.actors.ThingPersistenceOperationsActor;
import org.eclipse.ditto.things.service.persistence.actors.ThingSupervisorActor;
import org.eclipse.ditto.things.service.persistence.actors.ThingsPersistenceStreamingActorCreator;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.cluster.sharding.ClusterSharding;
import akka.cluster.sharding.ClusterShardingSettings;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.pf.ReceiveBuilder;
import akka.pattern.Patterns;
/**
* Our "Parent" Actor which takes care of supervision of all other Actors in our system.
*/
public final class ThingsRootActor extends DittoRootActor {
/**
* The name of this Actor in the ActorSystem.
*/
public static final String ACTOR_NAME = "thingsRoot";
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final RetrieveStatisticsDetailsResponseSupplier retrieveStatisticsDetailsResponseSupplier;
@SuppressWarnings("unused")
private ThingsRootActor(final ThingsConfig thingsConfig,
final ActorRef pubSubMediator,
final ThingPersistenceActorPropsFactory propsFactory) {
final var actorSystem = getContext().system();
final var clusterConfig = thingsConfig.getClusterConfig();
final var shardRegionExtractor =
ShardRegionExtractor.of(clusterConfig.getNumberOfShards(), actorSystem);
final var distributedAcks = DistributedAcks.lookup(actorSystem);
final var pubSubFactory =
ThingEventPubSubFactory.of(getContext(), shardRegionExtractor, distributedAcks);
final DistributedPub<ThingEvent<?>> distributedPubThingEventsForTwin = pubSubFactory.startDistributedPub();
final LiveSignalPub liveSignalPub = LiveSignalPub.of(getContext(), distributedAcks);
final BlockedNamespaces blockedNamespaces = BlockedNamespaces.of(actorSystem);
final PolicyEnforcerProvider policyEnforcerProvider = PolicyEnforcerProvider.getInstance(actorSystem);
final Props thingSupervisorActorProps = getThingSupervisorActorProps(pubSubMediator,
distributedPubThingEventsForTwin,
liveSignalPub,
propsFactory,
blockedNamespaces,
policyEnforcerProvider
);
final ActorRef thingsShardRegion = ClusterSharding.get(actorSystem)
.start(ThingsMessagingConstants.SHARD_REGION,
thingSupervisorActorProps,
ClusterShardingSettings.create(actorSystem).withRole(CLUSTER_ROLE),
shardRegionExtractor
);
startChildActor(ThingPersistenceOperationsActor.ACTOR_NAME,
ThingPersistenceOperationsActor.props(pubSubMediator, thingsConfig.getMongoDbConfig(),
actorSystem.settings().config(), thingsConfig.getPersistenceOperationsConfig()));
final ThingsAggregatorConfig thingsAggregatorConfig = DefaultThingsAggregatorConfig.of(
DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config())
);
final Props props = ThingsAggregatorActor.props(thingsShardRegion, thingsAggregatorConfig);
final ActorRef thingsAggregatorActor = getContext().actorOf(props, ThingsAggregatorActor.ACTOR_NAME);
retrieveStatisticsDetailsResponseSupplier = RetrieveStatisticsDetailsResponseSupplier.of(thingsShardRegion,
ThingsMessagingConstants.SHARD_REGION, log);
final var healthCheckConfig = thingsConfig.getHealthCheckConfig();
final var hcBuilder =
HealthCheckingActorOptions.getBuilder(healthCheckConfig.isEnabled(), healthCheckConfig.getInterval());
if (healthCheckConfig.getPersistenceConfig().isEnabled()) {
hcBuilder.enablePersistenceCheck();
}
final var healthCheckingActorOptions = hcBuilder.build();
final var metricsReporterConfig =
healthCheckConfig.getPersistenceConfig().getMetricsReporterConfig();
final ActorRef healthCheckingActor = startChildActor(DefaultHealthCheckingActorFactory.ACTOR_NAME,
DefaultHealthCheckingActorFactory.props(healthCheckingActorOptions, MongoHealthChecker.props()));
final ActorRef snapshotStreamingActor =
ThingsPersistenceStreamingActorCreator.startSnapshotStreamingActor(this::startChildActor);
final var cleanupConfig = thingsConfig.getThingConfig().getCleanupConfig();
final var mongoReadJournal = newMongoReadJournal(thingsConfig.getMongoDbConfig(), actorSystem);
final Props cleanupActorProps = PersistenceCleanupActor.props(cleanupConfig, mongoReadJournal, CLUSTER_ROLE);
startChildActor(PersistenceCleanupActor.NAME, cleanupActorProps);
pubSubMediator.tell(DistPubSubAccess.put(getSelf()), getSelf());
pubSubMediator.tell(DistPubSubAccess.put(snapshotStreamingActor), getSelf());
bindHttpStatusRoute(thingsConfig.getHttpConfig(), healthCheckingActor);
RootChildActorStarter.get(actorSystem, ScopedConfig.dittoExtension(actorSystem.settings().config()))
.execute(getContext());
}
/**
* Creates Akka configuration object Props for this ThingsRootActor.
*
* @param thingsConfig the configuration settings of the Things service.
* @param pubSubMediator the PubSub mediator Actor.
* @param propsFactory factory of Props of thing-persistence-actor.
* @return the Akka configuration Props object.
*/
public static Props props(final ThingsConfig thingsConfig,
final ActorRef pubSubMediator,
final ThingPersistenceActorPropsFactory propsFactory) {
return Props.create(ThingsRootActor.class, thingsConfig, pubSubMediator, propsFactory);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RetrieveStatisticsDetails.class, this::handleRetrieveStatisticsDetails)
.build().orElse(super.createReceive());
}
private void handleRetrieveStatisticsDetails(final RetrieveStatisticsDetails command) {
log.info("Sending the namespace stats of the things shard as requested ...");
Patterns.pipe(retrieveStatisticsDetailsResponseSupplier
.apply(command.getDittoHeaders()), getContext().dispatcher()).to(getSender());
}
private static Props getThingSupervisorActorProps(final ActorRef pubSubMediator,
final DistributedPub<ThingEvent<?>> distributedPubThingEventsForTwin,
final LiveSignalPub liveSignalPub,
final ThingPersistenceActorPropsFactory propsFactory,
final BlockedNamespaces blockedNamespaces,
final PolicyEnforcerProvider policyEnforcerProvider) {
return ThingSupervisorActor.props(pubSubMediator, distributedPubThingEventsForTwin,
liveSignalPub, propsFactory, blockedNamespaces, policyEnforcerProvider);
}
private static MongoReadJournal newMongoReadJournal(final MongoDbConfig mongoDbConfig,
final ActorSystem actorSystem) {
final var config = actorSystem.settings().config();
final var mongoClient = MongoClientWrapper.newInstance(mongoDbConfig);
return MongoReadJournal.newInstance(config, mongoClient, actorSystem);
}
}