X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fexample%2FExampleActor.java;h=8e4a44cf20f7a5b9b6474424ccba8e4d99e1dee3;hb=b131db5779e46e9555aa3358c5b6aa13109ef8f5;hp=06538fd2ae157618a167ef3096f2777981f7bae3;hpb=139937c2e646894af6a9b2b8a8a1047c6ef82485;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/example/ExampleActor.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/example/ExampleActor.java index 06538fd2ae..8e4a44cf20 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/example/ExampleActor.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/example/ExampleActor.java @@ -13,6 +13,13 @@ import akka.actor.Props; import akka.japi.Creator; import com.google.common.base.Optional; import com.google.protobuf.ByteString; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.HashMap; +import java.util.Map; import org.opendaylight.controller.cluster.DataPersistenceProvider; import org.opendaylight.controller.cluster.example.messages.KeyValue; import org.opendaylight.controller.cluster.example.messages.KeyValueSaved; @@ -20,30 +27,24 @@ import org.opendaylight.controller.cluster.example.messages.PrintRole; import org.opendaylight.controller.cluster.example.messages.PrintState; import org.opendaylight.controller.cluster.raft.ConfigParams; import org.opendaylight.controller.cluster.raft.RaftActor; +import org.opendaylight.controller.cluster.raft.RaftState; import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply; +import org.opendaylight.controller.cluster.raft.behaviors.Leader; import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.HashMap; -import java.util.Map; - /** * A sample actor showing how the RaftActor is to be extended */ public class ExampleActor extends RaftActor { - private final Map state = new HashMap(); + private final Map state = new HashMap<>(); private final DataPersistenceProvider dataPersistenceProvider; private long persistIdentifier = 1; - public ExampleActor(String id, Map peerAddresses, - Optional configParams) { + public ExampleActor(final String id, final Map peerAddresses, + final Optional configParams) { super(id, peerAddresses, configParams); this.dataPersistenceProvider = new PersistentDataProvider(); } @@ -51,6 +52,7 @@ public class ExampleActor extends RaftActor { public static Props props(final String id, final Map peerAddresses, final Optional configParams){ return Props.create(new Creator(){ + private static final long serialVersionUID = 1L; @Override public ExampleActor create() throws Exception { return new ExampleActor(id, peerAddresses, configParams); @@ -58,7 +60,7 @@ public class ExampleActor extends RaftActor { }); } - @Override public void onReceiveCommand(Object message) throws Exception{ + @Override public void onReceiveCommand(final Object message) throws Exception{ if(message instanceof KeyValue){ if(isLeader()) { String persistId = Long.toString(persistIdentifier++); @@ -77,7 +79,15 @@ public class ExampleActor extends RaftActor { } else if (message instanceof PrintRole) { if(LOG.isDebugEnabled()) { - LOG.debug("{} = {}, Peers={}", getId(), getRaftState(), getPeers()); + String followers = ""; + if (getRaftState() == RaftState.Leader) { + followers = ((Leader)this.getCurrentBehavior()).printFollowerStates(); + LOG.debug("{} = {}, Peers={}, followers={}", getId(), getRaftState(), getPeers(), followers); + } else { + LOG.debug("{} = {}, Peers={}", getId(), getRaftState(), getPeers()); + } + + } } else { @@ -85,8 +95,8 @@ public class ExampleActor extends RaftActor { } } - @Override protected void applyState(ActorRef clientActor, String identifier, - Object data) { + @Override protected void applyState(final ActorRef clientActor, final String identifier, + final Object data) { if(data instanceof KeyValue){ KeyValue kv = (KeyValue) data; state.put(kv.getKey(), kv.getValue()); @@ -106,19 +116,19 @@ public class ExampleActor extends RaftActor { getSelf().tell(new CaptureSnapshotReply(bs), null); } - @Override protected void applySnapshot(ByteString snapshot) { + @Override protected void applySnapshot(final ByteString snapshot) { state.clear(); try { - state.putAll((HashMap) toObject(snapshot)); + state.putAll((Map) toObject(snapshot)); } catch (Exception e) { LOG.error(e, "Exception in applying snapshot"); } if(LOG.isDebugEnabled()) { - LOG.debug("Snapshot applied to state : {}", ((HashMap) state).size()); + LOG.debug("Snapshot applied to state : {}", ((Map) state).size()); } } - private ByteString fromObject(Object snapshot) throws Exception { + private ByteString fromObject(final Object snapshot) throws Exception { ByteArrayOutputStream b = null; ObjectOutputStream o = null; try { @@ -138,7 +148,7 @@ public class ExampleActor extends RaftActor { } } - private Object toObject(ByteString bs) throws ClassNotFoundException, IOException { + private Object toObject(final ByteString bs) throws ClassNotFoundException, IOException { Object obj = null; ByteArrayInputStream bis = null; ObjectInputStream ois = null; @@ -166,7 +176,7 @@ public class ExampleActor extends RaftActor { return dataPersistenceProvider; } - @Override public void onReceiveRecover(Object message)throws Exception { + @Override public void onReceiveRecover(final Object message)throws Exception { super.onReceiveRecover(message); } @@ -175,11 +185,11 @@ public class ExampleActor extends RaftActor { } @Override - protected void startLogRecoveryBatch(int maxBatchSize) { + protected void startLogRecoveryBatch(final int maxBatchSize) { } @Override - protected void appendRecoveredLogEntry(Payload data) { + protected void appendRecoveredLogEntry(final Payload data) { } @Override @@ -191,6 +201,6 @@ public class ExampleActor extends RaftActor { } @Override - protected void applyRecoverySnapshot(ByteString snapshot) { + protected void applyRecoverySnapshot(final ByteString snapshot) { } }