/
PoliciesRootActor.java
executable file
·149 lines (125 loc) · 7.4 KB
/
PoliciesRootActor.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
/*
* 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.policies.starter;
import static org.eclipse.ditto.services.models.policies.PoliciesMessagingConstants.CLUSTER_ROLE;
import org.eclipse.ditto.model.policies.Policy;
import org.eclipse.ditto.services.base.actors.DittoRootActor;
import org.eclipse.ditto.services.models.policies.PoliciesMessagingConstants;
import org.eclipse.ditto.services.policies.common.config.PoliciesConfig;
import org.eclipse.ditto.services.policies.persistence.actors.PoliciesPersistenceStreamingActorCreator;
import org.eclipse.ditto.services.policies.persistence.actors.PolicyPersistenceOperationsActor;
import org.eclipse.ditto.services.policies.persistence.actors.PolicySupervisorActor;
import org.eclipse.ditto.services.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.services.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.services.utils.cluster.RetrieveStatisticsDetailsResponseSupplier;
import org.eclipse.ditto.services.utils.cluster.ShardRegionExtractor;
import org.eclipse.ditto.services.utils.cluster.config.ClusterConfig;
import org.eclipse.ditto.services.utils.health.DefaultHealthCheckingActorFactory;
import org.eclipse.ditto.services.utils.health.HealthCheckingActorOptions;
import org.eclipse.ditto.services.utils.health.config.HealthCheckConfig;
import org.eclipse.ditto.services.utils.health.config.MetricsReporterConfig;
import org.eclipse.ditto.services.utils.persistence.SnapshotAdapter;
import org.eclipse.ditto.services.utils.persistence.mongo.MongoHealthChecker;
import org.eclipse.ditto.services.utils.persistence.mongo.MongoMetricsReporter;
import org.eclipse.ditto.services.utils.persistence.mongo.config.TagsConfig;
import org.eclipse.ditto.services.utils.pubsub.PolicyNotificationPubSubFactory;
import org.eclipse.ditto.signals.commands.devops.RetrieveStatisticsDetails;
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;
/**
* Parent Actor which takes care of supervision of all other Actors in our system.
*/
public final class PoliciesRootActor extends DittoRootActor {
/**
* The name of this Actor in the ActorSystem.
*/
public static final String ACTOR_NAME = "policiesRoot";
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final RetrieveStatisticsDetailsResponseSupplier retrieveStatisticsDetailsResponseSupplier;
@SuppressWarnings("unused")
private PoliciesRootActor(final PoliciesConfig policiesConfig,
final SnapshotAdapter<Policy> snapshotAdapter,
final ActorRef pubSubMediator) {
final ActorSystem actorSystem = getContext().system();
final ClusterShardingSettings shardingSettings =
ClusterShardingSettings.create(actorSystem).withRole(CLUSTER_ROLE);
// Start distributed data replicator even though it is not used for now.
// TODO: use the DistributedSub for notification publication
PolicyNotificationPubSubFactory.of(getContext(), actorSystem).startDistributedPub();
final Props policySupervisorProps = PolicySupervisorActor.props(pubSubMediator, snapshotAdapter);
final TagsConfig tagsConfig = policiesConfig.getTagsConfig();
final ActorRef persistenceStreamingActor = startChildActor(PoliciesPersistenceStreamingActorCreator.ACTOR_NAME,
PoliciesPersistenceStreamingActorCreator.props(tagsConfig.getStreamingCacheSize()));
pubSubMediator.tell(DistPubSubAccess.put(getSelf()), getSelf());
pubSubMediator.tell(DistPubSubAccess.put(persistenceStreamingActor), getSelf());
final ClusterConfig clusterConfig = policiesConfig.getClusterConfig();
final ActorRef policiesShardRegion = ClusterSharding.get(actorSystem)
.start(PoliciesMessagingConstants.SHARD_REGION, policySupervisorProps, shardingSettings,
ShardRegionExtractor.of(clusterConfig.getNumberOfShards(), actorSystem));
startChildActor(PolicyPersistenceOperationsActor.ACTOR_NAME,
PolicyPersistenceOperationsActor.props(pubSubMediator, policiesConfig.getMongoDbConfig(),
actorSystem.settings().config(), policiesConfig.getPersistenceOperationsConfig()));
retrieveStatisticsDetailsResponseSupplier = RetrieveStatisticsDetailsResponseSupplier.of(policiesShardRegion,
PoliciesMessagingConstants.SHARD_REGION, log);
final HealthCheckConfig healthCheckConfig = policiesConfig.getHealthCheckConfig();
final HealthCheckingActorOptions.Builder hcBuilder =
HealthCheckingActorOptions.getBuilder(healthCheckConfig.isEnabled(), healthCheckConfig.getInterval());
if (healthCheckConfig.getPersistenceConfig().isEnabled()) {
hcBuilder.enablePersistenceCheck();
}
final HealthCheckingActorOptions healthCheckingActorOptions = hcBuilder.build();
final MetricsReporterConfig metricsReporterConfig =
healthCheckConfig.getPersistenceConfig().getMetricsReporterConfig();
final Props healthCheckingActorProps = DefaultHealthCheckingActorFactory.props(healthCheckingActorOptions,
MongoHealthChecker.props(),
MongoMetricsReporter.props(
metricsReporterConfig.getResolution(),
metricsReporterConfig.getHistory(),
pubSubMediator
)
);
final ActorRef healthCheckingActor =
startChildActor(DefaultHealthCheckingActorFactory.ACTOR_NAME, healthCheckingActorProps);
bindHttpStatusRoute(policiesConfig.getHttpConfig(), healthCheckingActor);
}
/**
* Creates Akka configuration object Props for this PoliciesRootActor.
*
* @param policiesConfig the configuration reader of this service.
* @param snapshotAdapter serializer and deserializer of the Policies snapshot store.
* @param pubSubMediator the PubSub mediator Actor.
* @return the Akka configuration Props object.
*/
public static Props props(final PoliciesConfig policiesConfig,
final SnapshotAdapter<Policy> snapshotAdapter,
final ActorRef pubSubMediator) {
return Props.create(PoliciesRootActor.class, policiesConfig, snapshotAdapter, pubSubMediator);
}
@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 policy shard as requested ...");
Patterns.pipe(retrieveStatisticsDetailsResponseSupplier
.apply(command.getDittoHeaders()), getContext().dispatcher()).to(getSender());
}
}