X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft-example%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fexample%2FExampleActor.java;h=11278ede10012698b4bc1950be1bd0d76f61291a;hp=663d400b53b3e3f7a589a08b819f930572d4c753;hb=1984e660ade65e692708722d21cbbbf76701151c;hpb=2faf656bf68dd3843fd59520b27a7ec2abbdcc68 diff --git a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/ExampleActor.java b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/ExampleActor.java index 663d400b53..11278ede10 100644 --- a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/ExampleActor.java +++ b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/ExampleActor.java @@ -38,7 +38,7 @@ import org.opendaylight.yangtools.concepts.Identifier; import org.opendaylight.yangtools.util.AbstractStringIdentifier; /** - * A sample actor showing how the RaftActor is to be extended + * A sample actor showing how the RaftActor is to be extended. */ public class ExampleActor extends RaftActor implements RaftActorRecoveryCohort, RaftActorSnapshotCohort { private static final class PayloadIdentifier extends AbstractStringIdentifier { @@ -69,23 +69,23 @@ public class ExampleActor extends RaftActor implements RaftActorRecoveryCohort, @Override protected void handleNonRaftCommand(Object message) { - if(message instanceof KeyValue){ - if(isLeader()) { + if (message instanceof KeyValue) { + if (isLeader()) { persistData(getSender(), new PayloadIdentifier(persistIdentifier++), (Payload) message, false); } else { - if(getLeader() != null) { + if (getLeader() != null) { getLeader().forward(message, getContext()); } } } else if (message instanceof PrintState) { - if(LOG.isDebugEnabled()) { + if (LOG.isDebugEnabled()) { LOG.debug("State of the node:{} has entries={}, {}", getId(), state.size(), getReplicatedLogState()); } } else if (message instanceof PrintRole) { - if(LOG.isDebugEnabled()) { + if (LOG.isDebugEnabled()) { if (getRaftState() == RaftState.Leader || getRaftState() == RaftState.IsolatedLeader) { final String followers = ((Leader)this.getCurrentBehavior()).printFollowerStates(); LOG.debug("{} = {}, Peers={}, followers={}", getId(), getRaftState(), @@ -122,22 +122,23 @@ public class ExampleActor extends RaftActor implements RaftActorRecoveryCohort, @Override protected void applyState(final ActorRef clientActor, final Identifier identifier, final Object data) { - if(data instanceof KeyValue){ + if (data instanceof KeyValue) { KeyValue kv = (KeyValue) data; state.put(kv.getKey(), kv.getValue()); - if(clientActor != null) { + if (clientActor != null) { clientActor.tell(new KeyValueSaved(), getSelf()); } } } @Override + @SuppressWarnings("checkstyle:IllegalCatch") public void createSnapshot(ActorRef actorRef, java.util.Optional installSnapshotStream) { try { if (installSnapshotStream.isPresent()) { SerializationUtils.serialize((Serializable) state, installSnapshotStream.get()); } - } catch (Exception e) { + } catch (RuntimeException e) { LOG.error("Exception in creating snapshot", e); } @@ -147,12 +148,9 @@ public class ExampleActor extends RaftActor implements RaftActorRecoveryCohort, @Override public void applySnapshot(Snapshot.State snapshotState) { state.clear(); - try { - state.putAll(((MapState)snapshotState).state); - } catch (Exception e) { - LOG.error("Exception in applying snapshot", e); - } - if(LOG.isDebugEnabled()) { + state.putAll(((MapState)snapshotState).state); + + if (LOG.isDebugEnabled()) { LOG.debug("Snapshot applied to state : {}", ((HashMap) state).size()); } } @@ -197,25 +195,20 @@ public class ExampleActor extends RaftActor implements RaftActorRecoveryCohort, } @Override - public byte[] getRestoreFromSnapshot() { + public Snapshot getRestoreFromSnapshot() { return null; } + @SuppressWarnings("unchecked") @Override public Snapshot.State deserializeSnapshot(ByteSource snapshotBytes) { try { - return deserializePreCarbonSnapshot(snapshotBytes.read()); + return new MapState((Map) SerializationUtils.deserialize(snapshotBytes.read())); } catch (IOException e) { throw Throwables.propagate(e); } } - @SuppressWarnings("unchecked") - @Override - public Snapshot.State deserializePreCarbonSnapshot(byte[] from) { - return new MapState((Map) SerializationUtils.deserialize(from)); - } - private static class MapState implements Snapshot.State { private static final long serialVersionUID = 1L;