-
Notifications
You must be signed in to change notification settings - Fork 214
/
PoliciesRootActor.java
executable file
·173 lines (142 loc) · 9.08 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* 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.policies.service.starter;
import static org.eclipse.ditto.policies.api.PoliciesMessagingConstants.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.internal.utils.pekko.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.RetrieveStatisticsDetailsResponseSupplier;
import org.eclipse.ditto.internal.utils.cluster.ShardRegionCreator;
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.namespaces.BlockedNamespacesUpdater;
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.pubsub.DistributedPub;
import org.eclipse.ditto.internal.utils.pubsubpolicies.PolicyAnnouncementPubSubFactory;
import org.eclipse.ditto.policies.api.PoliciesMessagingConstants;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcerProvider;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcerProviderExtension;
import org.eclipse.ditto.policies.model.signals.announcements.PolicyAnnouncement;
import org.eclipse.ditto.policies.service.common.config.PoliciesConfig;
import org.eclipse.ditto.policies.service.persistence.actors.PoliciesPersistenceStreamingActorCreator;
import org.eclipse.ditto.policies.service.persistence.actors.PolicyPersistenceOperationsActor;
import org.eclipse.ditto.policies.service.persistence.actors.PolicySupervisorActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Props;
import org.apache.pekko.cluster.sharding.ClusterShardingSettings;
import org.apache.pekko.event.DiagnosticLoggingAdapter;
import org.apache.pekko.japi.pf.ReceiveBuilder;
import org.apache.pekko.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 ActorRef pubSubMediator) {
final var actorSystem = getContext().system();
final ClusterShardingSettings shardingSettings =
ClusterShardingSettings.create(actorSystem).withRole(CLUSTER_ROLE);
final DistributedPub<PolicyAnnouncement<?>> policyAnnouncementPub =
PolicyAnnouncementPubSubFactory.of(getContext(), actorSystem).startDistributedPub();
final ActorRef persistenceStreamingActor = startChildActor(PoliciesPersistenceStreamingActorCreator.ACTOR_NAME,
PoliciesPersistenceStreamingActorCreator.props());
pubSubMediator.tell(DistPubSubAccess.put(getSelf()), getSelf());
pubSubMediator.tell(DistPubSubAccess.put(persistenceStreamingActor), getSelf());
final BlockedNamespaces blockedNamespaces = BlockedNamespaces.of(actorSystem);
// start cluster singleton that writes to the distributed cache of blocked namespaces
// this must only be started for one service in the cluster - which "policies" was chosen for ...
// this BlockedNamespacesUpdater updates the DData "BlockedNamespaces" data structure
final Props blockedNamespacesUpdaterProps = BlockedNamespacesUpdater.props(blockedNamespaces, pubSubMediator);
ClusterUtil.startSingleton(actorSystem, getContext(), PoliciesMessagingConstants.CLUSTER_ROLE,
BlockedNamespacesUpdater.ACTOR_NAME, blockedNamespacesUpdaterProps);
final PolicyEnforcerProvider policyEnforcerProvider = PolicyEnforcerProviderExtension.get(actorSystem).getPolicyEnforcerProvider();
final var mongoReadJournal = MongoReadJournal.newInstance(actorSystem);
final var policySupervisorProps =
getPolicySupervisorActorProps(pubSubMediator, policyAnnouncementPub, blockedNamespaces,
policyEnforcerProvider, mongoReadJournal);
final ActorRef policiesShardRegion =
ShardRegionCreator.start(actorSystem, PoliciesMessagingConstants.SHARD_REGION, policySupervisorProps,
policiesConfig.getClusterConfig().getNumberOfShards(), CLUSTER_ROLE);
startClusterSingletonActor(
PersistencePingActor.props(policiesShardRegion, policiesConfig.getPingConfig(), mongoReadJournal),
PersistencePingActor.ACTOR_NAME);
startChildActor(PolicyPersistenceOperationsActor.ACTOR_NAME,
PolicyPersistenceOperationsActor.props(pubSubMediator, policiesConfig.getMongoDbConfig(),
actorSystem.settings().config(), policiesConfig.getPersistenceOperationsConfig()));
retrieveStatisticsDetailsResponseSupplier = RetrieveStatisticsDetailsResponseSupplier.of(policiesShardRegion,
PoliciesMessagingConstants.SHARD_REGION, log);
final var cleanupConfig = policiesConfig.getPolicyConfig().getCleanupConfig();
final var cleanupActorProps = PersistenceCleanupActor.props(cleanupConfig, mongoReadJournal, CLUSTER_ROLE);
startChildActor(PersistenceCleanupActor.ACTOR_NAME, cleanupActorProps);
final var healthCheckConfig = policiesConfig.getHealthCheckConfig();
final var hcBuilder =
HealthCheckingActorOptions.getBuilder(healthCheckConfig.isEnabled(), healthCheckConfig.getInterval());
if (healthCheckConfig.getPersistenceConfig().isEnabled()) {
hcBuilder.enablePersistenceCheck();
}
final var healthCheckingActorOptions = hcBuilder.build();
final var healthCheckingActorProps =
DefaultHealthCheckingActorFactory.props(healthCheckingActorOptions, MongoHealthChecker.props());
final ActorRef healthCheckingActor =
startChildActor(DefaultHealthCheckingActorFactory.ACTOR_NAME, healthCheckingActorProps);
bindHttpStatusRoute(policiesConfig.getHttpConfig(), healthCheckingActor);
RootChildActorStarter.get(actorSystem, ScopedConfig.dittoExtension(actorSystem.settings().config()))
.execute(getContext());
}
private static Props getPolicySupervisorActorProps(final ActorRef pubSubMediator,
final DistributedPub<PolicyAnnouncement<?>> policyAnnouncementPub,
final BlockedNamespaces blockedNamespaces,
final PolicyEnforcerProvider policyEnforcerProvider,
final MongoReadJournal mongoReadJournal) {
return PolicySupervisorActor.props(pubSubMediator, policyAnnouncementPub, blockedNamespaces,
policyEnforcerProvider, mongoReadJournal);
}
/**
* Creates Akka configuration object Props for this PoliciesRootActor.
*
* @param policiesConfig the configuration reader of this service.
* @param pubSubMediator the PubSub mediator Actor.
* @return the Akka configuration Props object.
*/
public static Props props(final PoliciesConfig policiesConfig, final ActorRef pubSubMediator) {
return Props.create(PoliciesRootActor.class, policiesConfig, pubSubMediator);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RetrieveStatisticsDetails.class, this::handleRetrieveStatisticsDetails)
.build().orElse(super.createReceive());
}
private void startClusterSingletonActor(final Props props, final String name) {
ClusterUtil.startSingleton(getContext(), CLUSTER_ROLE, name, props);
}
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());
}
}