com.arpnetworking.clusteraggregator.GracefulShutdownActor Maven / Gradle / Ivy
/*
* Copyright 2015 Groupon.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arpnetworking.clusteraggregator;
import com.arpnetworking.clusteraggregator.client.HttpSourceActor;
import com.arpnetworking.clusteraggregator.http.Routes;
import com.arpnetworking.steno.Logger;
import com.arpnetworking.steno.LoggerFactory;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.cluster.Cluster;
import org.apache.pekko.cluster.sharding.ShardRegion;
import org.apache.pekko.pattern.Patterns;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
/**
* Shuts down the Pekko cluster gracefully.
*
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
*/
public class GracefulShutdownActor extends AbstractActor {
/**
* Public constructor.
*
* @param shardRegion aggregator shard region
* @param hostEmitter host emitter
* @param clusterEmitter cluster emitter
* @param routes routes
* @param healthcheckShutdownDelay delay after shutting down healthcheck before shutting down emitters
* @param ingestActor ingest actor
*/
@Inject
@SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Context is safe to use in constructor.")
public GracefulShutdownActor(
@Named("aggregator-shard-region") final ActorRef shardRegion,
@Named("host-emitter") final ActorRef hostEmitter,
@Named("cluster-emitter") final ActorRef clusterEmitter,
final Routes routes,
@Named("healthcheck-shutdown-delay") final Duration healthcheckShutdownDelay,
@Named("http-ingest-v1") final ActorRef ingestActor) {
_shardRegion = shardRegion;
_hostEmitter = hostEmitter;
_clusterEmitter = clusterEmitter;
_routes = routes;
_healthcheckShutdownDelay = healthcheckShutdownDelay;
_ingestActor = ingestActor;
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Shutdown.class, message -> {
LOGGER.info()
.setMessage("Initiating graceful shutdown")
.addData("actor", self())
.log();
self().tell(ShutdownHealthcheck.getInstance(), sender());
})
.match(ShutdownHealthcheck.class, message -> {
LOGGER.info()
.setMessage("Shutting down healthcheck")
.addData("actor", self())
.log();
_routes.shutdownHealthcheck();
_ingestActor.tell(HttpSourceActor.Shutdown.getInstance(), self());
LOGGER.info()
.setMessage("Waiting before proceeding with shutdown of emitters")
.addData("delay", _healthcheckShutdownDelay)
.addData("actor", self())
.log();
context().system().scheduler().scheduleOnce(
_healthcheckShutdownDelay,
self(),
ShutdownEmitter.getInstance(),
context().dispatcher(),
sender());
})
.match(ShutdownEmitter.class, message -> {
LOGGER.info()
.setMessage("Shutting down emitters")
.addData("actor", self())
.log();
final CompletionStage
© 2015 - 2025 Weber Informatics LLC | Privacy Policy