package org.opendaylight.controller.cluster.notifications;
-import akka.actor.Actor;
import akka.actor.ActorPath;
import akka.actor.ActorRef;
import akka.actor.Props;
-import akka.japi.Creator;
import akka.serialization.Serialization;
import com.google.common.collect.Maps;
import java.util.Map;
import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
/**
- * The RoleChangeNotifier is responsible for receiving Raft role change messages and notifying
+ * The RoleChangeNotifier is responsible for receiving Raft role and leader state change messages and notifying
* the listeners (within the same node), which are registered with it.
- * <p/>
+ *
+ * <p>
* The RoleChangeNotifier is instantiated by the Shard and injected into the RaftActor.
*/
public class RoleChangeNotifier extends AbstractUntypedActor implements AutoCloseable {
- private String memberId;
- private Map<ActorPath, ActorRef> registeredListeners = Maps.newHashMap();
+ private final String memberId;
+ private final Map<ActorPath, ActorRef> registeredListeners = Maps.newHashMap();
private RoleChangeNotification latestRoleChangeNotification = null;
+ private LeaderStateChanged latestLeaderStateChanged;
public RoleChangeNotifier(String memberId) {
this.memberId = memberId;
}
public static Props getProps(final String memberId) {
- return Props.create(new Creator<Actor>() {
- @Override
- public Actor create() throws Exception {
- return new RoleChangeNotifier(memberId);
- }
- });
+ return Props.create(RoleChangeNotifier.class, memberId);
}
@Override
}
@Override
- protected void handleReceive(Object message) throws Exception {
+ protected void handleReceive(Object message) {
if (message instanceof RegisterRoleChangeListener) {
// register listeners for this shard
getSender().tell(new RegisterRoleChangeListenerReply(), getSelf());
+ if (latestLeaderStateChanged != null) {
+ getSender().tell(latestLeaderStateChanged, getSelf());
+ }
+
if (latestRoleChangeNotification != null) {
getSender().tell(latestRoleChangeNotification, getSelf());
}
for (ActorRef listener: registeredListeners.values()) {
listener.tell(latestRoleChangeNotification, getSelf());
}
+ } else if (message instanceof LeaderStateChanged) {
+ latestLeaderStateChanged = (LeaderStateChanged)message;
+
+ for (ActorRef listener: registeredListeners.values()) {
+ listener.tell(latestLeaderStateChanged, getSelf());
+ }
+ } else {
+ unknownMessage(message);
}
}
@Override
- public void close() throws Exception {
+ public void close() {
registeredListeners.clear();
}
}