X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FAbstractReplicatedLogImplTest.java;h=c99f253657734cee8112f42e85b93e7feeebf215;hp=ae8e525233cbe96f74418ce0fa45d70a2266df34;hb=8360177d8f021df9078ac54919a816a73fbee0a0;hpb=b78e7cc266d3540634ccc5b996b67b68365edbd8 diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImplTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImplTest.java index ae8e525233..c99f253657 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImplTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImplTest.java @@ -7,20 +7,20 @@ */ package org.opendaylight.controller.cluster.raft; -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 akka.japi.Procedure; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +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; + /** * */ @@ -31,24 +31,17 @@ public class AbstractReplicatedLogImplTest { @Before public void setUp() { replicatedLogImpl = new MockAbstractReplicatedLogImpl(); - } - - @After - public void tearDown() { - replicatedLogImpl.journal.clear(); - replicatedLogImpl.setSnapshotIndex(-1); - replicatedLogImpl.setSnapshotTerm(-1); - replicatedLogImpl = null; - } - - @Test - public void testIndexOperations() { // create a set of initial entries in the in-memory log replicatedLogImpl.append(new MockReplicatedLogEntry(1, 0, new MockPayload("A"))); replicatedLogImpl.append(new MockReplicatedLogEntry(1, 1, new MockPayload("B"))); replicatedLogImpl.append(new MockReplicatedLogEntry(1, 2, new MockPayload("C"))); replicatedLogImpl.append(new MockReplicatedLogEntry(2, 3, new MockPayload("D"))); + } + + @Test + public void testIndexOperations() { + // check if the values returned are correct, with snapshotIndex = -1 assertEquals("B", replicatedLogImpl.get(1).getData().toString()); assertEquals("D", replicatedLogImpl.last().getData().toString()); @@ -63,7 +56,7 @@ public class AbstractReplicatedLogImplTest { // 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); + takeSnapshot(3); // check the values after the snapshot. // each index value passed in the test is the logical index (log entry index) @@ -99,7 +92,7 @@ public class AbstractReplicatedLogImplTest { assertEquals(2, replicatedLogImpl.getFrom(6).size()); // take a second snapshot with 5 entries with 0 unapplied entries left in the log - state = takeSnapshot(5); + takeSnapshot(5); assertEquals(0, replicatedLogImpl.size()); assertNull(replicatedLogImpl.last()); @@ -112,42 +105,133 @@ public class AbstractReplicatedLogImplTest { } + @Test + public void testGetFromWithMax(){ + List from = replicatedLogImpl.getFrom(0, 1); + Assert.assertEquals(1, from.size()); + Assert.assertEquals(1, from.get(0).getTerm()); + + from = replicatedLogImpl.getFrom(0, 20); + Assert.assertEquals(4, from.size()); + Assert.assertEquals(2, from.get(3).getTerm()); + + from = replicatedLogImpl.getFrom(1, 2); + Assert.assertEquals(2, from.size()); + Assert.assertEquals(1, from.get(1).getTerm()); + + } + + @Test + public void testSnapshotPreCommit() { + //add 4 more entries + replicatedLogImpl.append(new MockReplicatedLogEntry(2, 4, new MockPayload("E"))); + replicatedLogImpl.append(new MockReplicatedLogEntry(2, 5, new MockPayload("F"))); + replicatedLogImpl.append(new MockReplicatedLogEntry(3, 6, new MockPayload("G"))); + replicatedLogImpl.append(new MockReplicatedLogEntry(3, 7, new MockPayload("H"))); + + //sending negative values should not cause any changes + replicatedLogImpl.snapshotPreCommit(-1, -1); + assertEquals(8, replicatedLogImpl.size()); + assertEquals(-1, replicatedLogImpl.getSnapshotIndex()); + + replicatedLogImpl.snapshotPreCommit(4, 3); + assertEquals(3, replicatedLogImpl.size()); + assertEquals(4, replicatedLogImpl.getSnapshotIndex()); + + replicatedLogImpl.snapshotPreCommit(6, 3); + assertEquals(1, replicatedLogImpl.size()); + assertEquals(6, replicatedLogImpl.getSnapshotIndex()); + + replicatedLogImpl.snapshotPreCommit(7, 3); + assertEquals(0, replicatedLogImpl.size()); + assertEquals(7, replicatedLogImpl.getSnapshotIndex()); + + //running it again on an empty list should not throw exception + replicatedLogImpl.snapshotPreCommit(7, 3); + assertEquals(0, replicatedLogImpl.size()); + assertEquals(7, replicatedLogImpl.getSnapshotIndex()); + + } + + @Test + public void testIsPresent() { + assertTrue(replicatedLogImpl.isPresent(0)); + assertTrue(replicatedLogImpl.isPresent(1)); + assertTrue(replicatedLogImpl.isPresent(2)); + assertTrue(replicatedLogImpl.isPresent(3)); + + replicatedLogImpl.append(new MockReplicatedLogEntry(2, 4, new MockPayload("D"))); + replicatedLogImpl.snapshotPreCommit(3, 2); //snapshot on 3 + replicatedLogImpl.snapshotCommit(); + + assertFalse(replicatedLogImpl.isPresent(0)); + assertFalse(replicatedLogImpl.isPresent(1)); + assertFalse(replicatedLogImpl.isPresent(2)); + assertFalse(replicatedLogImpl.isPresent(3)); + assertTrue(replicatedLogImpl.isPresent(4)); + + replicatedLogImpl.snapshotPreCommit(4, 2); //snapshot on 4 + replicatedLogImpl.snapshotCommit(); + assertFalse(replicatedLogImpl.isPresent(4)); + + replicatedLogImpl.append(new MockReplicatedLogEntry(2, 5, new MockPayload("D"))); + assertTrue(replicatedLogImpl.isPresent(5)); + } + + @Test + public void testRemoveFrom() { + + replicatedLogImpl.append(new MockReplicatedLogEntry(2, 4, new MockPayload("E", 2))); + replicatedLogImpl.append(new MockReplicatedLogEntry(2, 5, new MockPayload("F", 3))); + + assertEquals("dataSize", 9, replicatedLogImpl.dataSize()); + + long adjusted = replicatedLogImpl.removeFrom(4); + assertEquals("removeFrom - adjusted", 4, adjusted); + assertEquals("size", 4, replicatedLogImpl.size()); + assertEquals("dataSize", 4, replicatedLogImpl.dataSize()); + + takeSnapshot(1); + + adjusted = replicatedLogImpl.removeFrom(2); + assertEquals("removeFrom - adjusted", 1, adjusted); + assertEquals("size", 1, replicatedLogImpl.size()); + assertEquals("dataSize", 1, replicatedLogImpl.dataSize()); + + assertEquals("removeFrom - adjusted", -1, replicatedLogImpl.removeFrom(0)); + assertEquals("removeFrom - adjusted", -1, replicatedLogImpl.removeFrom(100)); + } + // create a snapshot for test - public Map takeSnapshot(int numEntries) { - Map map = new HashMap(numEntries); - List entries = replicatedLogImpl.getEntriesTill(numEntries); - for (ReplicatedLogEntry entry : entries) { + public Map takeSnapshot(final int numEntries) { + Map map = new HashMap<>(numEntries); + + long lastIndex = 0; + long lastTerm = 0; + for(int i = 0; i < numEntries; i++) { + ReplicatedLogEntry entry = replicatedLogImpl.getAtPhysicalIndex(i); map.put(entry.getIndex(), entry.getData().toString()); + lastIndex = entry.getIndex(); + lastTerm = entry.getTerm(); } - int term = (int) replicatedLogImpl.lastTerm(); - int lastIndex = (int) entries.get(entries.size() - 1).getIndex(); - entries.clear(); - replicatedLogImpl.setSnapshotTerm(term); - replicatedLogImpl.setSnapshotIndex(lastIndex); + replicatedLogImpl.snapshotPreCommit(lastIndex, lastTerm); + replicatedLogImpl.snapshotCommit(); return map; } 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) { - this.snapshotIndex = snapshotIndex; - } - - public void setSnapshotTerm(long snapshotTerm) { - this.snapshotTerm = snapshotTerm; - } - - public List getEntriesTill(int index) { - return journal.subList(0, index); + @Override + public void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure callback) { } } }