Search in sources :

Example 36 with SimpleReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.

the class AbstractReplicatedLogImplTest method testSnapshotPreCommit.

@Test
public void testSnapshotPreCommit() {
    // add 4 more entries
    replicatedLogImpl.append(new SimpleReplicatedLogEntry(4, 2, new MockPayload("E")));
    replicatedLogImpl.append(new SimpleReplicatedLogEntry(5, 2, new MockPayload("F")));
    replicatedLogImpl.append(new SimpleReplicatedLogEntry(6, 3, new MockPayload("G")));
    replicatedLogImpl.append(new SimpleReplicatedLogEntry(7, 3, new MockPayload("H")));
    // sending negative values should not cause any changes
    replicatedLogImpl.snapshotPreCommit(-1, -1);
    assertEquals(8, replicatedLogImpl.size());
    assertEquals(-1, replicatedLogImpl.getSnapshotIndex());
    assertEquals(-1, replicatedLogImpl.getSnapshotTerm());
    replicatedLogImpl.snapshotPreCommit(4, 2);
    assertEquals(3, replicatedLogImpl.size());
    assertEquals(4, replicatedLogImpl.getSnapshotIndex());
    assertEquals(2, replicatedLogImpl.getSnapshotTerm());
    replicatedLogImpl.snapshotPreCommit(6, 3);
    assertEquals(1, replicatedLogImpl.size());
    assertEquals(6, replicatedLogImpl.getSnapshotIndex());
    assertEquals(3, replicatedLogImpl.getSnapshotTerm());
    replicatedLogImpl.snapshotPreCommit(7, 3);
    assertEquals(0, replicatedLogImpl.size());
    assertEquals(7, replicatedLogImpl.getSnapshotIndex());
    assertEquals(3, replicatedLogImpl.getSnapshotTerm());
    // running it again on an empty list should not throw exception
    replicatedLogImpl.snapshotPreCommit(7, 3);
    assertEquals(0, replicatedLogImpl.size());
    assertEquals(7, replicatedLogImpl.getSnapshotIndex());
    assertEquals(3, replicatedLogImpl.getSnapshotTerm());
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Example 37 with SimpleReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.

the class AbstractReplicatedLogImplTest method setUp.

@Before
public void setUp() {
    replicatedLogImpl = new MockAbstractReplicatedLogImpl();
    // create a set of initial entries in the in-memory log
    replicatedLogImpl.append(new SimpleReplicatedLogEntry(0, 1, new MockPayload("A")));
    replicatedLogImpl.append(new SimpleReplicatedLogEntry(1, 1, new MockPayload("B")));
    replicatedLogImpl.append(new SimpleReplicatedLogEntry(2, 1, new MockPayload("C")));
    replicatedLogImpl.append(new SimpleReplicatedLogEntry(3, 2, new MockPayload("D")));
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Before(org.junit.Before)

Example 38 with SimpleReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.

the class MigratedMessagesTest method testNoSnapshotAfterStartupWithNoMigratedMessages.

@Test
public void testNoSnapshotAfterStartupWithNoMigratedMessages() {
    TEST_LOG.info("testNoSnapshotAfterStartupWithNoMigratedMessages starting");
    String id = factory.generateActorId("test-actor-");
    InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, id));
    InMemoryJournal.addEntry(id, 2, new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("A")));
    InMemoryJournal.addEntry(id, 3, new ApplyJournalEntries(0));
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    RaftActorSnapshotCohort snapshotCohort = new RaftActorSnapshotCohort() {

        @Override
        public void createSnapshot(ActorRef actorRef, java.util.Optional<OutputStream> installSnapshotStream) {
            actorRef.tell(new CaptureSnapshotReply(ByteState.empty(), installSnapshotStream), actorRef);
        }

        @Override
        public void applySnapshot(Snapshot.State snapshotState) {
        }

        @Override
        public State deserializeSnapshot(ByteSource snapshotBytes) {
            throw new UnsupportedOperationException();
        }
    };
    TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(id).config(config).snapshotCohort(snapshotCohort).persistent(Optional.of(true)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), id);
    MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
    mockRaftActor.waitForRecoveryComplete();
    Uninterruptibles.sleepUninterruptibly(750, TimeUnit.MILLISECONDS);
    List<Snapshot> snapshots = InMemorySnapshotStore.getSnapshots(id, Snapshot.class);
    assertEquals("Snapshots", 0, snapshots.size());
    TEST_LOG.info("testNoSnapshotAfterStartupWithNoMigratedMessages ending");
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) Optional(com.google.common.base.Optional) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) State(org.opendaylight.controller.cluster.raft.persisted.Snapshot.State) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) ByteSource(com.google.common.io.ByteSource) UpdateElectionTerm(org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm) Test(org.junit.Test)

Example 39 with SimpleReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.

the class SnapshotManagerTest method testPersistWhenReplicatedToAllIndexNotMinus.

@Test
public void testPersistWhenReplicatedToAllIndexNotMinus() throws Exception {
    doReturn(45L).when(mockReplicatedLog).getSnapshotIndex();
    doReturn(6L).when(mockReplicatedLog).getSnapshotTerm();
    ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class);
    doReturn(replicatedLogEntry).when(mockReplicatedLog).get(9);
    doReturn(6L).when(replicatedLogEntry).getTerm();
    doReturn(9L).when(replicatedLogEntry).getIndex();
    // when replicatedToAllIndex != -1
    snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), 9);
    ByteState snapshotState = ByteState.of(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    snapshotManager.persist(snapshotState, Optional.empty(), Runtime.getRuntime().totalMemory());
    ArgumentCaptor<Snapshot> snapshotArgumentCaptor = ArgumentCaptor.forClass(Snapshot.class);
    verify(mockDataPersistenceProvider).saveSnapshot(snapshotArgumentCaptor.capture());
    Snapshot snapshot = snapshotArgumentCaptor.getValue();
    assertEquals("getLastTerm", 6L, snapshot.getLastTerm());
    assertEquals("getLastIndex", 9L, snapshot.getLastIndex());
    assertEquals("getLastAppliedTerm", 6L, snapshot.getLastAppliedTerm());
    assertEquals("getLastAppliedIndex", 9L, snapshot.getLastAppliedIndex());
    assertEquals("getState", snapshotState, snapshot.getState());
    assertEquals("getUnAppliedEntries size", 0, snapshot.getUnAppliedEntries().size());
    verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
    verify(mockRaftActorBehavior).setReplicatedToAllIndex(9);
}
Also used : CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) Test(org.junit.Test)

Example 40 with SimpleReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry in project controller by opendaylight.

the class SnapshotManagerTest method testCallingCommitMultipleTimesCausesNoHarm.

@Test
public void testCallingCommitMultipleTimesCausesNoHarm() {
    doReturn(50L).when(mockDataPersistenceProvider).getLastSequenceNumber();
    // when replicatedToAllIndex = -1
    snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
    snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
    snapshotManager.commit(100L, 0);
    snapshotManager.commit(100L, 0);
    verify(mockReplicatedLog, times(1)).snapshotCommit();
    verify(mockDataPersistenceProvider, times(1)).deleteMessages(50L);
    verify(mockDataPersistenceProvider, times(1)).deleteSnapshots(any(SnapshotSelectionCriteria.class));
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SnapshotSelectionCriteria(akka.persistence.SnapshotSelectionCriteria) Test(org.junit.Test)

Aggregations

SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)75 Test (org.junit.Test)64 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)32 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)17 ApplyJournalEntries (org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries)15 UpdateElectionTerm (org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm)12 FiniteDuration (scala.concurrent.duration.FiniteDuration)12 ByteString (com.google.protobuf.ByteString)11 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)11 CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)11 ServerConfigurationPayload (org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload)11 ServerInfo (org.opendaylight.controller.cluster.raft.persisted.ServerInfo)11 ActorRef (akka.actor.ActorRef)10 TestActorRef (akka.testkit.TestActorRef)9 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)8 ByteState (org.opendaylight.controller.cluster.raft.persisted.ByteState)8 ArrayList (java.util.ArrayList)7 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)7 CaptureSnapshotReply (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply)7 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)7