Search in sources :

Example 1 with SnapshotOffer

use of akka.persistence.SnapshotOffer in project controller by opendaylight.

the class RaftActorTest method testRaftActorForwardsToRaftActorRecoverySupport.

@Test
public void testRaftActorForwardsToRaftActorRecoverySupport() {
    String persistenceId = factory.generateActorId("leader-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, Collections.<String, String>emptyMap(), config), persistenceId);
    MockRaftActor mockRaftActor = mockActorRef.underlyingActor();
    // Wait for akka's recovery to complete so it doesn't interfere.
    mockRaftActor.waitForRecoveryComplete();
    RaftActorRecoverySupport mockSupport = mock(RaftActorRecoverySupport.class);
    mockRaftActor.setRaftActorRecoverySupport(mockSupport);
    Snapshot snapshot = Snapshot.create(ByteState.of(new byte[] { 1 }), Collections.<ReplicatedLogEntry>emptyList(), 3, 1, 3, 1, -1, null, null);
    SnapshotOffer snapshotOffer = new SnapshotOffer(new SnapshotMetadata("test", 6, 12345), snapshot);
    mockRaftActor.handleRecover(snapshotOffer);
    ReplicatedLogEntry logEntry = new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1", 5));
    mockRaftActor.handleRecover(logEntry);
    ApplyJournalEntries applyJournalEntries = new ApplyJournalEntries(2);
    mockRaftActor.handleRecover(applyJournalEntries);
    DeleteEntries deleteEntries = new DeleteEntries(1);
    mockRaftActor.handleRecover(deleteEntries);
    UpdateElectionTerm updateElectionTerm = new UpdateElectionTerm(5, "member2");
    mockRaftActor.handleRecover(updateElectionTerm);
    verify(mockSupport).handleRecoveryMessage(same(snapshotOffer), any(PersistentDataProvider.class));
    verify(mockSupport).handleRecoveryMessage(same(logEntry), any(PersistentDataProvider.class));
    verify(mockSupport).handleRecoveryMessage(same(applyJournalEntries), any(PersistentDataProvider.class));
    verify(mockSupport).handleRecoveryMessage(same(deleteEntries), any(PersistentDataProvider.class));
    verify(mockSupport).handleRecoveryMessage(same(updateElectionTerm), any(PersistentDataProvider.class));
}
Also used : DeleteEntries(org.opendaylight.controller.cluster.raft.persisted.DeleteEntries) FiniteDuration(scala.concurrent.duration.FiniteDuration) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) ByteString(com.google.protobuf.ByteString) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) GetSnapshot(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot) PersistentDataProvider(org.opendaylight.controller.cluster.PersistentDataProvider) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SnapshotMetadata(akka.persistence.SnapshotMetadata) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) SnapshotOffer(akka.persistence.SnapshotOffer) UpdateElectionTerm(org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm) Test(org.junit.Test)

Example 2 with SnapshotOffer

use of akka.persistence.SnapshotOffer in project controller by opendaylight.

the class RaftActorRecoverySupportTest method testDataRecoveredWithPersistenceDisabled.

@Test
public void testDataRecoveredWithPersistenceDisabled() {
    doNothing().when(mockCohort).applyRecoverySnapshot(anyObject());
    doReturn(false).when(mockPersistence).isRecoveryApplicable();
    doReturn(10L).when(mockPersistentProvider).getLastSequenceNumber();
    Snapshot snapshot = Snapshot.create(new MockSnapshotState(Arrays.asList(new MockPayload("1"))), Collections.<ReplicatedLogEntry>emptyList(), 3, 1, 3, 1, -1, null, null);
    SnapshotOffer snapshotOffer = new SnapshotOffer(new SnapshotMetadata("test", 6, 12345), snapshot);
    sendMessageToSupport(snapshotOffer);
    sendMessageToSupport(new UpdateElectionTerm(5, "member2"));
    sendMessageToSupport(new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("4")));
    sendMessageToSupport(new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("5")));
    sendMessageToSupport(new ApplyJournalEntries(4));
    sendMessageToSupport(new DeleteEntries(5));
    assertEquals("Journal log size", 0, context.getReplicatedLog().size());
    assertEquals("Last index", -1, context.getReplicatedLog().lastIndex());
    assertEquals("Last applied", -1, context.getLastApplied());
    assertEquals("Commit index", -1, context.getCommitIndex());
    assertEquals("Snapshot term", -1, context.getReplicatedLog().getSnapshotTerm());
    assertEquals("Snapshot index", -1, context.getReplicatedLog().getSnapshotIndex());
    assertEquals("Current term", 5, context.getTermInformation().getCurrentTerm());
    assertEquals("Voted For", "member2", context.getTermInformation().getVotedFor());
    sendMessageToSupport(RecoveryCompleted.getInstance(), true);
    verify(mockCohort, never()).applyRecoverySnapshot(anyObject());
    verify(mockCohort, never()).getRestoreFromSnapshot();
    verifyNoMoreInteractions(mockCohort);
    verify(mockPersistentProvider).deleteMessages(10L);
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SnapshotMetadata(akka.persistence.SnapshotMetadata) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) SnapshotOffer(akka.persistence.SnapshotOffer) DeleteEntries(org.opendaylight.controller.cluster.raft.persisted.DeleteEntries) UpdateElectionTerm(org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Example 3 with SnapshotOffer

use of akka.persistence.SnapshotOffer in project controller by opendaylight.

the class RaftActorRecoverySupportTest method testOnSnapshotOfferWithServerConfiguration.

@Test
public void testOnSnapshotOfferWithServerConfiguration() {
    long electionTerm = 2;
    String electionVotedFor = "member-2";
    ServerConfigurationPayload serverPayload = new ServerConfigurationPayload(Arrays.asList(new ServerInfo(localId, true), new ServerInfo("follower1", true), new ServerInfo("follower2", true)));
    MockSnapshotState snapshotState = new MockSnapshotState(Arrays.asList(new MockPayload("1")));
    Snapshot snapshot = Snapshot.create(snapshotState, Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1, electionTerm, electionVotedFor, serverPayload);
    SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
    SnapshotOffer snapshotOffer = new SnapshotOffer(metadata, snapshot);
    sendMessageToSupport(snapshotOffer);
    assertEquals("Journal log size", 0, context.getReplicatedLog().size());
    assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
    assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
    assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
    assertEquals("Peer List", Sets.newHashSet("follower1", "follower2"), Sets.newHashSet(context.getPeerIds()));
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) ServerConfigurationPayload(org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload) SnapshotMetadata(akka.persistence.SnapshotMetadata) ServerInfo(org.opendaylight.controller.cluster.raft.persisted.ServerInfo) SnapshotOffer(akka.persistence.SnapshotOffer) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Example 4 with SnapshotOffer

use of akka.persistence.SnapshotOffer in project controller by opendaylight.

the class RaftActorRecoverySupportTest method testOnSnapshotOffer.

@Test
public void testOnSnapshotOffer() {
    ReplicatedLog replicatedLog = context.getReplicatedLog();
    replicatedLog.append(new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1")));
    replicatedLog.append(new SimpleReplicatedLogEntry(2, 1, new MockRaftActorContext.MockPayload("2")));
    replicatedLog.append(new SimpleReplicatedLogEntry(3, 1, new MockRaftActorContext.MockPayload("3")));
    ReplicatedLogEntry unAppliedEntry1 = new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("4", 4));
    ReplicatedLogEntry unAppliedEntry2 = new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("5", 5));
    long lastAppliedDuringSnapshotCapture = 3;
    long lastIndexDuringSnapshotCapture = 5;
    long electionTerm = 2;
    String electionVotedFor = "member-2";
    MockSnapshotState snapshotState = new MockSnapshotState(Arrays.asList(new MockPayload("1")));
    Snapshot snapshot = Snapshot.create(snapshotState, Arrays.asList(unAppliedEntry1, unAppliedEntry2), lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1, electionTerm, electionVotedFor, null);
    SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
    SnapshotOffer snapshotOffer = new SnapshotOffer(metadata, snapshot);
    sendMessageToSupport(snapshotOffer);
    assertEquals("Journal log size", 2, context.getReplicatedLog().size());
    assertEquals("Journal data size", 9, context.getReplicatedLog().dataSize());
    assertEquals("Last index", lastIndexDuringSnapshotCapture, context.getReplicatedLog().lastIndex());
    assertEquals("Last applied", lastAppliedDuringSnapshotCapture, context.getLastApplied());
    assertEquals("Commit index", lastAppliedDuringSnapshotCapture, context.getCommitIndex());
    assertEquals("Snapshot term", 1, context.getReplicatedLog().getSnapshotTerm());
    assertEquals("Snapshot index", lastAppliedDuringSnapshotCapture, context.getReplicatedLog().getSnapshotIndex());
    assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
    assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
    assertFalse("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
    verify(mockCohort).applyRecoverySnapshot(snapshotState);
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SnapshotMetadata(akka.persistence.SnapshotMetadata) SnapshotOffer(akka.persistence.SnapshotOffer) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Example 5 with SnapshotOffer

use of akka.persistence.SnapshotOffer in project controller by opendaylight.

the class RecoveringClientActorBehavior method onReceiveRecover.

@Override
AbstractClientActorBehavior<?> onReceiveRecover(final Object recover) {
    if (recover instanceof RecoveryCompleted) {
        final ClientIdentifier nextId;
        if (lastId != null) {
            if (!currentFrontend.equals(lastId.getFrontendId())) {
                LOG.error("{}: Mismatched frontend identifier, shutting down. Current: {} Saved: {}", persistenceId(), currentFrontend, lastId.getFrontendId());
                return null;
            }
            nextId = ClientIdentifier.create(currentFrontend, lastId.getGeneration() + 1);
        } else {
            nextId = ClientIdentifier.create(currentFrontend, 0);
        }
        LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId);
        context().saveSnapshot(nextId);
        return new SavingClientActorBehavior(context(), nextId);
    } else if (recover instanceof SnapshotOffer) {
        lastId = (ClientIdentifier) ((SnapshotOffer) recover).snapshot();
        LOG.debug("{}: recovered identifier {}", persistenceId(), lastId);
    } else {
        LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
    }
    return this;
}
Also used : RecoveryCompleted(akka.persistence.RecoveryCompleted) ClientIdentifier(org.opendaylight.controller.cluster.access.concepts.ClientIdentifier) SnapshotOffer(akka.persistence.SnapshotOffer)

Aggregations

SnapshotOffer (akka.persistence.SnapshotOffer)5 SnapshotMetadata (akka.persistence.SnapshotMetadata)4 Test (org.junit.Test)4 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)4 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)4 MockSnapshotState (org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState)3 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)3 ApplyJournalEntries (org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries)2 DeleteEntries (org.opendaylight.controller.cluster.raft.persisted.DeleteEntries)2 UpdateElectionTerm (org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm)2 RecoveryCompleted (akka.persistence.RecoveryCompleted)1 ByteString (com.google.protobuf.ByteString)1 NonPersistentDataProvider (org.opendaylight.controller.cluster.NonPersistentDataProvider)1 PersistentDataProvider (org.opendaylight.controller.cluster.PersistentDataProvider)1 ClientIdentifier (org.opendaylight.controller.cluster.access.concepts.ClientIdentifier)1 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)1 GetSnapshot (org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot)1 ServerConfigurationPayload (org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload)1 ServerInfo (org.opendaylight.controller.cluster.raft.persisted.ServerInfo)1 FiniteDuration (scala.concurrent.duration.FiniteDuration)1