* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.cluster.example;
import akka.actor.ActorRef;
import akka.actor.Props;
-import com.google.common.base.Optional;
-import com.google.common.base.Throwables;
import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
import org.apache.commons.lang3.SerializationUtils;
import org.opendaylight.controller.cluster.example.messages.KeyValue;
import org.opendaylight.controller.cluster.example.messages.KeyValueSaved;
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.messages.Payload;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
-import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
import org.opendaylight.yangtools.concepts.Identifier;
import org.opendaylight.yangtools.util.AbstractStringIdentifier;
private long persistIdentifier = 1;
- public ExampleActor(String id, Map<String, String> peerAddresses,
- Optional<ConfigParams> configParams) {
+ public ExampleActor(final String id, final Map<String, String> peerAddresses,
+ final Optional<ConfigParams> configParams) {
super(id, peerAddresses, configParams, (short)0);
setPersistence(true);
roleChangeNotifier = createRoleChangeNotifier(id);
}
@Override
- protected void handleNonRaftCommand(Object message) {
+ protected void handleNonRaftCommand(final Object message) {
if (message instanceof KeyValue) {
if (isLeader()) {
persistData(getSender(), new PayloadIdentifier(persistIdentifier++), (Payload) message, false);
- } else {
- if (getLeader() != null) {
- getLeader().forward(message, getContext());
- }
+ } else if (getLeader() != null) {
+ getLeader().forward(message, getContext());
}
} else if (message instanceof PrintState) {
} else if (message instanceof PrintRole) {
if (LOG.isDebugEnabled()) {
if (getRaftState() == RaftState.Leader || getRaftState() == RaftState.IsolatedLeader) {
- final String followers = ((Leader)this.getCurrentBehavior()).printFollowerStates();
+ final String followers = ((Leader)getCurrentBehavior()).printFollowerStates();
LOG.debug("{} = {}, Peers={}, followers={}", getId(), getRaftState(),
getRaftActorContext().getPeerIds(), followers);
} else {
+ ", im-mem journal size=" + getRaftActorContext().getReplicatedLog().size();
}
- public Optional<ActorRef> createRoleChangeNotifier(String actorId) {
- ActorRef exampleRoleChangeNotifier = this.getContext().actorOf(
+ public Optional<ActorRef> createRoleChangeNotifier(final String actorId) {
+ ActorRef exampleRoleChangeNotifier = getContext().actorOf(
RoleChangeNotifier.getProps(actorId), actorId + "-notifier");
return Optional.<ActorRef>of(exampleRoleChangeNotifier);
}
@Override
protected void applyState(final ActorRef clientActor, final Identifier identifier, final Object data) {
- if (data instanceof KeyValue) {
- KeyValue kv = (KeyValue) data;
+ if (data instanceof KeyValue kv) {
state.put(kv.getKey(), kv.getValue());
if (clientActor != null) {
clientActor.tell(new KeyValueSaved(), getSelf());
@Override
@SuppressWarnings("checkstyle:IllegalCatch")
- public void createSnapshot(ActorRef actorRef, java.util.Optional<OutputStream> installSnapshotStream) {
+ public void createSnapshot(final ActorRef actorRef, final Optional<OutputStream> installSnapshotStream) {
try {
if (installSnapshotStream.isPresent()) {
SerializationUtils.serialize((Serializable) state, installSnapshotStream.get());
}
@Override
- public void applySnapshot(Snapshot.State snapshotState) {
+ public void applySnapshot(final Snapshot.State snapshotState) {
state.clear();
state.putAll(((MapState)snapshotState).state);
}
@Override
- public void startLogRecoveryBatch(int maxBatchSize) {
+ public void startLogRecoveryBatch(final int maxBatchSize) {
}
@Override
- public void appendRecoveredLogEntry(Payload data) {
+ public void appendRecoveredLogEntry(final Payload data) {
}
@Override
}
@Override
- public void applyRecoverySnapshot(Snapshot.State snapshotState) {
+ public void applyRecoverySnapshot(final Snapshot.State snapshotState) {
}
@Override
@SuppressWarnings("unchecked")
@Override
- public Snapshot.State deserializeSnapshot(ByteSource snapshotBytes) {
+ public Snapshot.State deserializeSnapshot(final ByteSource snapshotBytes) {
try {
return new MapState((Map<String, String>) SerializationUtils.deserialize(snapshotBytes.read()));
} catch (IOException e) {
- throw Throwables.propagate(e);
+ throw new IllegalStateException(e);
}
}
Map<String, String> state;
- MapState(Map<String, String> state) {
+ MapState(final Map<String, String> state) {
this.state = state;
}
}