Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / akka / impl / ActorSystemProviderImpl.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.akka.impl;
9
10 import akka.actor.ActorSystem;
11 import akka.actor.Props;
12 import akka.actor.Terminated;
13 import akka.dispatch.OnComplete;
14 import com.typesafe.config.Config;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.ActorSystemProvider;
19 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
20 import org.opendaylight.controller.cluster.common.actor.QuarantinedMonitorActor;
21 import org.opendaylight.controller.cluster.datastore.TerminationMonitor;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.util.ListenerRegistry;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.Await;
27 import scala.concurrent.ExecutionContext;
28 import scala.concurrent.Future;
29 import scala.concurrent.duration.FiniteDuration;
30
31 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
32     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
33     private static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
34
35     private final @NonNull ActorSystem actorSystem;
36     private final ListenerRegistry<ActorSystemProviderListener> listeners = ListenerRegistry.create();
37
38     public ActorSystemProviderImpl(
39             final ClassLoader classLoader, final Props quarantinedMonitorActorProps, final Config akkaConfig) {
40         LOG.info("Creating new ActorSystem");
41
42         actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
43         actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
44         actorSystem.actorOf(quarantinedMonitorActorProps, QuarantinedMonitorActor.ADDRESS);
45     }
46
47     @Override
48     public ActorSystem getActorSystem() {
49         return actorSystem;
50     }
51
52     @Override
53     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
54             final ActorSystemProviderListener listener) {
55         return listeners.register(listener);
56     }
57
58     public Future<Terminated> asyncClose() {
59         LOG.info("Shutting down ActorSystem");
60
61         final Future<Terminated> ret = actorSystem.terminate();
62         ret.onComplete(new OnComplete<Terminated>() {
63             @Override
64             public void onComplete(final Throwable failure, final Terminated success) throws Throwable {
65                 if (failure != null) {
66                     LOG.warn("ActorSystem failed to shut down", failure);
67                 } else {
68                     LOG.info("ActorSystem shut down");
69                 }
70             }
71         }, ExecutionContext.global());
72         return ret;
73     }
74
75     public void close(final FiniteDuration wait) throws TimeoutException, InterruptedException {
76         Await.result(asyncClose(), wait);
77     }
78
79     @Override
80     public void close() throws TimeoutException, InterruptedException {
81         close(FiniteDuration.create(10, TimeUnit.SECONDS));
82     }
83 }