X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fexample%2FExampleActor.java;h=641ec0582c3790cac20a6b7102ad5e12134e72e1;hp=3c8e12b8b11e0c5fd977ceedbda2fa65ffb1b71a;hb=d097d70beab05f7fcb028ff12b2c47e35570fb55;hpb=cf5be659d906cc80d52647cb516bbab435156742 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 3c8e12b8b1..641ec0582c 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 @@ -8,27 +8,88 @@ package org.opendaylight.controller.cluster.example; +import akka.actor.ActorRef; +import akka.actor.Props; +import akka.japi.Creator; +import org.opendaylight.controller.cluster.example.messages.KeyValue; +import org.opendaylight.controller.cluster.example.messages.KeyValueSaved; +import org.opendaylight.controller.cluster.example.messages.PrintRole; +import org.opendaylight.controller.cluster.example.messages.PrintState; import org.opendaylight.controller.cluster.raft.RaftActor; +import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload; + +import java.util.HashMap; +import java.util.Map; /** * A sample actor showing how the RaftActor is to be extended */ public class ExampleActor extends RaftActor { - public ExampleActor(String id) { - super(id); + + private final Map state = new HashMap(); + + private long persistIdentifier = 1; + + + public ExampleActor(String id, Map peerAddresses) { + super(id, peerAddresses); + } + + public static Props props(final String id, final Map peerAddresses){ + return Props.create(new Creator(){ + + @Override public ExampleActor create() throws Exception { + return new ExampleActor(id, peerAddresses); + } + }); } @Override public void onReceiveCommand(Object message){ - /* - Here the extended class does whatever it needs to do. - If it cannot handle a message then it passes it on to the super - class for handling - */ - super.onReceiveCommand(message); + if(message instanceof KeyValue){ + if(isLeader()) { + String persistId = Long.toString(persistIdentifier++); + persistData(getSender(), persistId, (Payload) message); + } else { + if(getLeader() != null) { + getLeader().forward(message, getContext()); + } + } + + } else if (message instanceof PrintState) { + LOG.debug("State of the node:"+getId() + " has = "+state.size() + " entries"); + + } else if (message instanceof PrintRole) { + LOG.debug(getId() + " = " + getRaftState()); + } else { + super.onReceiveCommand(message); + } + } + + @Override protected void applyState(ActorRef clientActor, String identifier, + Object data) { + if(data instanceof KeyValue){ + KeyValue kv = (KeyValue) data; + state.put(kv.getKey(), kv.getValue()); + if(clientActor != null) { + clientActor.tell(new KeyValueSaved(), getSelf()); + } + } + } + + @Override protected Object createSnapshot() { + return state; + } + + @Override protected void applySnapshot(Object snapshot) { + state.clear(); + state.putAll((HashMap) snapshot); } @Override public void onReceiveRecover(Object message) { super.onReceiveRecover(message); } + @Override public String persistenceId() { + return getId(); + } }