2 * Copyright (c) 2015 Huawei Technologies Co., Ltd. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.common.actor;
11 import akka.actor.Props;
12 import akka.actor.UntypedActor;
13 import akka.japi.Creator;
14 import akka.japi.Effect;
15 import akka.remote.AssociationErrorEvent;
16 import akka.remote.InvalidAssociation;
17 import akka.remote.RemotingLifecycleEvent;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * This class listens to Akka RemotingLifecycleEvent events to detect when this node has been
23 * quarantined by another. Once this node gets quarantined, restart the ActorSystem to allow this
24 * node to rejoin the cluster.
26 * @author Gary Wu <gary.wu1@huawei.com>
29 public class QuarantinedMonitorActor extends UntypedActor {
31 private final Logger LOG = LoggerFactory.getLogger(QuarantinedMonitorActor.class);
33 public static final String ADDRESS = "quarantined-monitor";
35 private final Effect callback;
37 protected QuarantinedMonitorActor(Effect callback) {
38 this.callback = callback;
40 LOG.debug("Created QuarantinedMonitorActor");
42 getContext().system().eventStream().subscribe(getSelf(), RemotingLifecycleEvent.class);
46 public void postStop() {
47 LOG.debug("Stopping QuarantinedMonitorActor");
51 public void onReceive(Object message) throws Exception {
52 final String messageType = message.getClass().getSimpleName();
53 LOG.trace("onReceive {} {}", messageType, message);
55 // check to see if we got quarantined by another node
57 // TODO: follow https://github.com/akka/akka/issues/18758 to see if Akka adds a named
58 // exception for quarantine detection
59 if (message instanceof AssociationErrorEvent) {
60 AssociationErrorEvent event = (AssociationErrorEvent) message;
61 Throwable cause = event.getCause();
62 if (cause instanceof InvalidAssociation) {
63 Throwable cause2 = ((InvalidAssociation) cause).getCause();
64 if (cause2.getMessage().contains("quarantined this system")) {
65 LOG.warn("Got quarantined by {}", event.getRemoteAddress());
67 // execute the callback
70 LOG.debug("received AssociationErrorEvent, cause: InvalidAssociation", cause2);
73 LOG.debug("received AssociationErrorEvent", cause);
78 public static Props props(final Effect callback) {
79 return Props.create(new Creator<QuarantinedMonitorActor>() {
80 private static final long serialVersionUID = 1L;
83 public QuarantinedMonitorActor create() throws Exception {
84 return new QuarantinedMonitorActor(callback);