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;
import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
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<String, String> state = new HashMap();
+ private final Map<String, String> state = new HashMap<>();
private final DataPersistenceProvider dataPersistenceProvider;
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);
this.dataPersistenceProvider = new PersistentDataProvider();
}
});
}
- @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++);
}
}
- @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());
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));
}
}
- private ByteString fromObject(Object snapshot) throws Exception {
+ private ByteString fromObject(final Object snapshot) throws Exception {
ByteArrayOutputStream b = null;
ObjectOutputStream o = null;
try {
}
}
- 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;
return dataPersistenceProvider;
}
- @Override public void onReceiveRecover(Object message)throws Exception {
+ @Override public void onReceiveRecover(final Object message)throws Exception {
super.onReceiveRecover(message);
}
}
@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
}
@Override
- protected void applyRecoverySnapshot(ByteString snapshot) {
+ protected void applyRecoverySnapshot(final ByteString snapshot) {
}
}
*/
package org.opendaylight.controller.cluster.raft;
-import junit.framework.Assert;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
-import static org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
-import static org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockReplicatedLogEntry;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
+import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockReplicatedLogEntry;
/**
*
*/
// now create a snapshot of 3 entries, with 1 unapplied entry left in the log
// It removes the entries which have made it to snapshot
// and updates the snapshot index and term
- Map state = takeSnapshot(3);
+ Map<Long, String> state = takeSnapshot(3);
// check the values after the snapshot.
// each index value passed in the test is the logical index (log entry index)
}
// create a snapshot for test
- public Map takeSnapshot(int numEntries) {
- Map map = new HashMap(numEntries);
+ public Map<Long, String> takeSnapshot(final int numEntries) {
+ Map<Long, String> map = new HashMap<>(numEntries);
List<ReplicatedLogEntry> entries = replicatedLogImpl.getEntriesTill(numEntries);
for (ReplicatedLogEntry entry : entries) {
map.put(entry.getIndex(), entry.getData().toString());
}
class MockAbstractReplicatedLogImpl extends AbstractReplicatedLogImpl {
@Override
- public void appendAndPersist(ReplicatedLogEntry replicatedLogEntry) {
+ public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry) {
}
@Override
- public void removeFromAndPersist(long index) {
+ public void removeFromAndPersist(final long index) {
}
- public void setSnapshotIndex(long snapshotIndex) {
+ @Override
+ public void setSnapshotIndex(final long snapshotIndex) {
this.snapshotIndex = snapshotIndex;
}
- public void setSnapshotTerm(long snapshotTerm) {
+ @Override
+ public void setSnapshotTerm(final long snapshotTerm) {
this.snapshotTerm = snapshotTerm;
}
- public List<ReplicatedLogEntry> getEntriesTill(int index) {
+ public List<ReplicatedLogEntry> getEntriesTill(final int index) {
return journal.subList(0, index);
}
}