/
PoliciesRootActor.java
executable file
·273 lines (236 loc) · 13.3 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
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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.policies.starter;
import static akka.http.javadsl.server.Directives.logRequest;
import static akka.http.javadsl.server.Directives.logResult;
import java.net.ConnectException;
import java.time.Duration;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletionStage;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.policies.Policy;
import org.eclipse.ditto.services.base.config.HealthConfigReader;
import org.eclipse.ditto.services.base.config.HttpConfigReader;
import org.eclipse.ditto.services.base.config.ServiceConfigReader;
import org.eclipse.ditto.services.models.policies.PoliciesMessagingConstants;
import org.eclipse.ditto.services.policies.persistence.actors.policies.PoliciesPersistenceStreamingActorCreator;
import org.eclipse.ditto.services.policies.persistence.actors.policy.PolicySupervisorActor;
import org.eclipse.ditto.services.policies.util.ConfigKeys;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import org.eclipse.ditto.services.utils.cluster.ClusterStatusSupplier;
import org.eclipse.ditto.services.utils.cluster.RetrieveStatisticsDetailsResponseSupplier;
import org.eclipse.ditto.services.utils.cluster.ShardRegionExtractor;
import org.eclipse.ditto.services.utils.config.ConfigUtil;
import org.eclipse.ditto.services.utils.config.MongoConfig;
import org.eclipse.ditto.services.utils.health.DefaultHealthCheckingActorFactory;
import org.eclipse.ditto.services.utils.health.HealthCheckingActorOptions;
import org.eclipse.ditto.services.utils.health.routes.StatusRoute;
import org.eclipse.ditto.services.utils.persistence.SnapshotAdapter;
import org.eclipse.ditto.services.utils.persistence.mongo.MongoClientActor;
import org.eclipse.ditto.signals.commands.devops.RetrieveStatisticsDetails;
import com.typesafe.config.Config;
import akka.Done;
import akka.actor.AbstractActor;
import akka.actor.ActorKilledException;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.CoordinatedShutdown;
import akka.actor.InvalidActorNameException;
import akka.actor.OneForOneStrategy;
import akka.actor.Props;
import akka.actor.Status;
import akka.actor.SupervisorStrategy;
import akka.cluster.Cluster;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.cluster.sharding.ClusterSharding;
import akka.cluster.sharding.ClusterShardingSettings;
import akka.event.DiagnosticLoggingAdapter;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.Http;
import akka.http.javadsl.ServerBinding;
import akka.http.javadsl.server.Route;
import akka.japi.Creator;
import akka.japi.pf.DeciderBuilder;
import akka.japi.pf.ReceiveBuilder;
import akka.pattern.AskTimeoutException;
import akka.pattern.PatternsCS;
import akka.stream.ActorMaterializer;
/**
* Parent Actor which takes care of supervision of all other Actors in our system.
*/
public final class PoliciesRootActor extends AbstractActor {
/**
* The name of this Actor in the ActorSystem.
*/
static final String ACTOR_NAME = "policiesRoot";
private static final String RESTARTING_CHILD_MESSAGE = "Restarting child...";
private final DiagnosticLoggingAdapter log = LogUtil.obtain(this);
private final SupervisorStrategy strategy = new OneForOneStrategy(true, DeciderBuilder
.match(NullPointerException.class, e -> {
log.error(e, "NullPointer in child actor: {}", e.getMessage());
log.info(RESTARTING_CHILD_MESSAGE);
return SupervisorStrategy.restart();
}).match(IllegalArgumentException.class, e -> {
log.warning("Illegal Argument in child actor: {}", e.getMessage());
return SupervisorStrategy.resume();
}).match(IndexOutOfBoundsException.class, e -> {
log.warning("IndexOutOfBounds in child actor: {}", e.getMessage());
return SupervisorStrategy.resume();
}).match(IllegalStateException.class, e -> {
log.warning("Illegal State in child actor: {}", e.getMessage());
return SupervisorStrategy.resume();
}).match(NoSuchElementException.class, e -> {
log.warning("NoSuchElement in child actor: {}", e.getMessage());
return SupervisorStrategy.resume();
}).match(AskTimeoutException.class, e -> {
log.warning("AskTimeoutException in child actor: {}", e.getMessage());
return SupervisorStrategy.resume();
}).match(ConnectException.class, e -> {
log.warning("ConnectException in child actor: {}", e.getMessage());
log.info(RESTARTING_CHILD_MESSAGE);
return SupervisorStrategy.restart();
}).match(InvalidActorNameException.class, e -> {
log.warning("InvalidActorNameException in child actor: {}", e.getMessage());
return SupervisorStrategy.resume();
}).match(ActorKilledException.class, e -> {
log.error(e, "ActorKilledException in child actor: {}", e.message());
log.info(RESTARTING_CHILD_MESSAGE);
return SupervisorStrategy.restart();
}).match(DittoRuntimeException.class, e -> {
log.error(e,
"DittoRuntimeException '{}' should not be escalated to PoliciesRootActor. Simply resuming Actor.",
e.getErrorCode());
return SupervisorStrategy.resume();
}).match(Throwable.class, e -> {
log.error(e, "Escalating above root actor!");
return SupervisorStrategy.escalate();
}).matchAny(e -> {
log.error("Unknown message:'{}'! Escalating above root actor!", e);
return SupervisorStrategy.escalate();
}).build());
private final RetrieveStatisticsDetailsResponseSupplier retrieveStatisticsDetailsResponseSupplier;
private PoliciesRootActor(final ServiceConfigReader configReader,
final SnapshotAdapter<Policy> snapshotAdapter,
final ActorRef pubSubMediator,
final ActorMaterializer materializer) {
final int numberOfShards = configReader.cluster().numberOfShards();
final Config config = configReader.getRawConfig();
final ClusterShardingSettings shardingSettings =
ClusterShardingSettings.create(getContext().system()).withRole(PoliciesMessagingConstants.CLUSTER_ROLE);
final Duration minBackoff = config.getDuration(ConfigKeys.Policy.SUPERVISOR_EXPONENTIAL_BACKOFF_MIN);
final Duration maxBackoff = config.getDuration(ConfigKeys.Policy.SUPERVISOR_EXPONENTIAL_BACKOFF_MAX);
final double randomFactor = config.getDouble(ConfigKeys.Policy.SUPERVISOR_EXPONENTIAL_BACKOFF_RANDOM_FACTOR);
final Props policySupervisorProps = PolicySupervisorActor.props(pubSubMediator, minBackoff, maxBackoff,
randomFactor, snapshotAdapter);
final int tagsStreamingCacheSize = config.getInt(ConfigKeys.POLICIES_TAGS_STREAMING_CACHE_SIZE);
final ActorRef persistenceStreamingActor = startChildActor(PoliciesPersistenceStreamingActorCreator.ACTOR_NAME,
PoliciesPersistenceStreamingActorCreator.props(config, tagsStreamingCacheSize));
pubSubMediator.tell(new DistributedPubSubMediator.Put(getSelf()), getSelf());
pubSubMediator.tell(new DistributedPubSubMediator.Put(persistenceStreamingActor), getSelf());
final ActorRef policiesShardRegion = ClusterSharding.get(getContext().system())
.start(PoliciesMessagingConstants.SHARD_REGION, policySupervisorProps, shardingSettings,
ShardRegionExtractor.of(numberOfShards, getContext().getSystem()));
retrieveStatisticsDetailsResponseSupplier = RetrieveStatisticsDetailsResponseSupplier.of(policiesShardRegion,
PoliciesMessagingConstants.SHARD_REGION, log);
final HealthConfigReader healthConfig = configReader.health();
final ActorRef mongoClient = startChildActor(MongoClientActor.ACTOR_NAME, MongoClientActor
.props(config.getString(ConfigKeys.MONGO_URI),
healthConfig.getPersistenceTimeout(),
MongoConfig.getSSLEnabled(config)));
final boolean healthCheckEnabled = healthConfig.enabled();
final Duration healthCheckInterval = healthConfig.getInterval();
final HealthCheckingActorOptions.Builder hcBuilder =
HealthCheckingActorOptions.getBuilder(healthCheckEnabled, healthCheckInterval);
if (healthConfig.persistenceEnabled()) {
hcBuilder.enablePersistenceCheck();
}
final HealthCheckingActorOptions healthCheckingActorOptions = hcBuilder.build();
final Props healthCheckingActorProps =
DefaultHealthCheckingActorFactory.props(healthCheckingActorOptions, mongoClient);
final ActorRef healthCheckingActor =
startChildActor(DefaultHealthCheckingActorFactory.ACTOR_NAME, healthCheckingActorProps);
final HttpConfigReader httpConfig = configReader.http();
String hostname = httpConfig.getHostname();
if (hostname.isEmpty()) {
hostname = ConfigUtil.getLocalHostAddress();
log.info("No explicit hostname configured, using HTTP hostname: {}", hostname);
}
final CompletionStage<ServerBinding> binding = Http.get(getContext().system())
.bindAndHandle(createRoute(getContext().system(), healthCheckingActor).flow(getContext().system(),
materializer), ConnectHttp.toHost(hostname, httpConfig.getPort()), materializer);
binding.thenAccept(theBinding -> CoordinatedShutdown.get(getContext().getSystem()).addTask(
CoordinatedShutdown.PhaseServiceUnbind(), "shutdown_health_http_endpoint", () -> {
log.info("Gracefully shutting down status/health HTTP endpoint..");
return theBinding.terminate(Duration.ofSeconds(1))
.handle((httpTerminated, e) -> Done.getInstance());
})
);
binding.thenAccept(this::logServerBinding)
.exceptionally(failure -> {
log.error(failure, "Something very bad happened: {}", failure.getMessage());
getContext().system().terminate();
return null;
});
}
/**
* Creates Akka configuration object Props for this PoliciesRootActor.
*
* @param configReader the configuration reader of this service.
* @param snapshotAdapter serializer and deserializer of the Policies snapshot store.
* @param pubSubMediator the PubSub mediator Actor.
* @param materializer the materializer for the akka actor system.
* @return the Akka configuration Props object.
*/
public static Props props(final ServiceConfigReader configReader,
final SnapshotAdapter<Policy> snapshotAdapter,
final ActorRef pubSubMediator,
final ActorMaterializer materializer) {
return Props.create(PoliciesRootActor.class, new Creator<PoliciesRootActor>() {
private static final long serialVersionUID = 1L;
@Override
public PoliciesRootActor create() {
return new PoliciesRootActor(configReader, snapshotAdapter, pubSubMediator, materializer);
}
});
}
@Override
public SupervisorStrategy supervisorStrategy() {
return strategy;
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RetrieveStatisticsDetails.class, this::handleRetrieveStatisticsDetails)
.match(Status.Failure.class, f -> log.error(f.cause(), "Got failure: {}", f))
.matchAny(m -> {
log.warning("Unknown message: {}", m);
unhandled(m);
}).build();
}
private void handleRetrieveStatisticsDetails(final RetrieveStatisticsDetails command) {
log.info("Sending the namespace stats of the policy shard as requested..");
PatternsCS.pipe(retrieveStatisticsDetailsResponseSupplier
.apply(command.getDittoHeaders()), getContext().dispatcher()).to(getSender());
}
private ActorRef startChildActor(final String actorName, final Props props) {
log.info("Starting child actor '{}'", actorName);
return getContext().actorOf(props, actorName);
}
private static Route createRoute(final ActorSystem actorSystem, final ActorRef healthCheckingActor) {
final StatusRoute statusRoute = new StatusRoute(new ClusterStatusSupplier(Cluster.get(actorSystem)),
healthCheckingActor, actorSystem);
return logRequest("http-request", () -> logResult("http-response", statusRoute::buildStatusRoute));
}
private void logServerBinding(final ServerBinding serverBinding) {
log.info("Bound to address {}:{}", serverBinding.localAddress().getHostString(),
serverBinding.localAddress().getPort());
}
}