Search in sources :

Example 1 with ApplyState

use of org.opendaylight.controller.cluster.raft.base.messages.ApplyState in project controller by opendaylight.

the class RaftActor method persistData.

/**
 * Persists the given Payload in the journal and replicates to any followers. After successful completion,
 * {@link #applyState(ActorRef, Identifier, Object)} is notified.
 *
 * @param clientActor optional ActorRef that is provided via the applyState callback
 * @param identifier the payload identifier
 * @param data the payload data to persist
 * @param batchHint if true, an attempt is made to delay immediate replication and batch the payload with
 *        subsequent payloads for efficiency. Otherwise the payload is immediately replicated.
 */
protected final void persistData(final ActorRef clientActor, final Identifier identifier, final Payload data, final boolean batchHint) {
    ReplicatedLogEntry replicatedLogEntry = new SimpleReplicatedLogEntry(context.getReplicatedLog().lastIndex() + 1, context.getTermInformation().getCurrentTerm(), data);
    replicatedLogEntry.setPersistencePending(true);
    LOG.debug("{}: Persist data {}", persistenceId(), replicatedLogEntry);
    final RaftActorContext raftContext = getRaftActorContext();
    boolean wasAppended = replicatedLog().appendAndPersist(replicatedLogEntry, persistedLogEntry -> {
        // Clear the persistence pending flag in the log entry.
        persistedLogEntry.setPersistencePending(false);
        if (!hasFollowers()) {
            // Increment the Commit Index and the Last Applied values
            raftContext.setCommitIndex(persistedLogEntry.getIndex());
            raftContext.setLastApplied(persistedLogEntry.getIndex());
            // Apply the state immediately.
            handleApplyState(new ApplyState(clientActor, identifier, persistedLogEntry));
            // Send a ApplyJournalEntries message so that we write the fact that we applied
            // the state to durable storage
            self().tell(new ApplyJournalEntries(persistedLogEntry.getIndex()), self());
        } else {
            context.getReplicatedLog().captureSnapshotIfReady(replicatedLogEntry);
            // Local persistence is complete so send the CheckConsensusReached message to the behavior (which
            // normally should still be the leader) to check if consensus has now been reached in conjunction with
            // follower replication.
            getCurrentBehavior().handleMessage(getSelf(), CheckConsensusReached.INSTANCE);
        }
    }, true);
    if (wasAppended && hasFollowers()) {
        // Send log entry for replication.
        getCurrentBehavior().handleMessage(getSelf(), new Replicate(clientActor, identifier, replicatedLogEntry, !batchHint));
    }
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) Replicate(org.opendaylight.controller.cluster.raft.base.messages.Replicate) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState)

Example 2 with ApplyState

use of org.opendaylight.controller.cluster.raft.base.messages.ApplyState in project controller by opendaylight.

the class LeaderTest method testHandleAppendEntriesReplyFailureWithFollowersLogTermDifferent.

@Test
public void testHandleAppendEntriesReplyFailureWithFollowersLogTermDifferent() {
    logStart("testHandleAppendEntriesReplyFailureWithFollowersLogTermDifferent");
    MockRaftActorContext leaderActorContext = createActorContextWithFollower();
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 2, 2).build());
    long leaderCommitIndex = 1;
    leaderActorContext.setCommitIndex(leaderCommitIndex);
    leaderActorContext.setLastApplied(leaderCommitIndex);
    final ReplicatedLogEntry leadersFirstLogEntry = leaderActorContext.getReplicatedLog().get(0);
    final ReplicatedLogEntry leadersSecondLogEntry = leaderActorContext.getReplicatedLog().get(1);
    MockRaftActorContext followerActorContext = createFollowerActorContextWithLeader();
    followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 1, 1).build());
    followerActorContext.setCommitIndex(-1);
    followerActorContext.setLastApplied(-1);
    Follower follower = new Follower(followerActorContext);
    followerActor.underlyingActor().setBehavior(follower);
    followerActorContext.setCurrentBehavior(follower);
    leader = new Leader(leaderActorContext);
    AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    final AppendEntriesReply appendEntriesReply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
    MessageCollectorActor.clearMessages(followerActor);
    MessageCollectorActor.clearMessages(leaderActor);
    // Verify initial AppendEntries sent with the leader's current commit index.
    assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
    assertEquals("Log entries size", 0, appendEntries.getEntries().size());
    assertEquals("getPrevLogIndex", 0, appendEntries.getPrevLogIndex());
    leaderActor.underlyingActor().setBehavior(leader);
    leaderActorContext.setCurrentBehavior(leader);
    leader.handleMessage(followerActor, appendEntriesReply);
    MessageCollectorActor.expectMatching(leaderActor, AppendEntriesReply.class, 1);
    appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals("getLeaderCommit", leaderCommitIndex, appendEntries.getLeaderCommit());
    assertEquals("getPrevLogIndex", -1, appendEntries.getPrevLogIndex());
    assertEquals("Log entries size", 2, appendEntries.getEntries().size());
    assertEquals("First entry index", 0, appendEntries.getEntries().get(0).getIndex());
    assertEquals("First entry term", 2, appendEntries.getEntries().get(0).getTerm());
    assertEquals("First entry data", leadersFirstLogEntry.getData(), appendEntries.getEntries().get(0).getData());
    assertEquals("Second entry index", 1, appendEntries.getEntries().get(1).getIndex());
    assertEquals("Second entry term", 2, appendEntries.getEntries().get(1).getTerm());
    assertEquals("Second entry data", leadersSecondLogEntry.getData(), appendEntries.getEntries().get(1).getData());
    FollowerLogInformation followerInfo = leader.getFollower(FOLLOWER_ID);
    assertEquals("getNextIndex", 2, followerInfo.getNextIndex());
    List<ApplyState> applyStateList = MessageCollectorActor.expectMatching(followerActor, ApplyState.class, 2);
    ApplyState applyState = applyStateList.get(0);
    assertEquals("Follower's first ApplyState index", 0, applyState.getReplicatedLogEntry().getIndex());
    assertEquals("Follower's first ApplyState term", 2, applyState.getReplicatedLogEntry().getTerm());
    assertEquals("Follower's first ApplyState data", leadersFirstLogEntry.getData(), applyState.getReplicatedLogEntry().getData());
    applyState = applyStateList.get(1);
    assertEquals("Follower's second ApplyState index", 1, applyState.getReplicatedLogEntry().getIndex());
    assertEquals("Follower's second ApplyState term", 2, applyState.getReplicatedLogEntry().getTerm());
    assertEquals("Follower's second ApplyState data", leadersSecondLogEntry.getData(), applyState.getReplicatedLogEntry().getData());
    assertEquals("Follower's commit index", 1, followerActorContext.getCommitIndex());
    assertEquals("Follower's lastIndex", 1, followerActorContext.getReplicatedLog().lastIndex());
    assertEquals("Follower's lastTerm", 2, followerActorContext.getReplicatedLog().lastTerm());
}
Also used : FollowerLogInformation(org.opendaylight.controller.cluster.raft.FollowerLogInformation) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) Test(org.junit.Test)

Example 3 with ApplyState

use of org.opendaylight.controller.cluster.raft.base.messages.ApplyState in project controller by opendaylight.

the class LeaderTest method testHandleAppendEntriesReplySuccess.

@Test
public void testHandleAppendEntriesReplySuccess() throws Exception {
    logStart("testHandleAppendEntriesReplySuccess");
    MockRaftActorContext leaderActorContext = createActorContextWithFollower();
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 3, 1).build());
    leaderActorContext.setCommitIndex(1);
    leaderActorContext.setLastApplied(1);
    leaderActorContext.getTermInformation().update(1, "leader");
    leader = new Leader(leaderActorContext);
    FollowerLogInformation followerInfo = leader.getFollower(FOLLOWER_ID);
    assertEquals(payloadVersion, leader.getLeaderPayloadVersion());
    assertEquals(RaftVersions.HELIUM_VERSION, followerInfo.getRaftVersion());
    AppendEntriesReply reply = new AppendEntriesReply(FOLLOWER_ID, 1, true, 2, 1, payloadVersion);
    RaftActorBehavior raftActorBehavior = leader.handleAppendEntriesReply(followerActor, reply);
    assertEquals(RaftState.Leader, raftActorBehavior.state());
    assertEquals(2, leaderActorContext.getCommitIndex());
    ApplyJournalEntries applyJournalEntries = MessageCollectorActor.expectFirstMatching(leaderActor, ApplyJournalEntries.class);
    assertEquals(2, leaderActorContext.getLastApplied());
    assertEquals(2, applyJournalEntries.getToIndex());
    List<ApplyState> applyStateList = MessageCollectorActor.getAllMatching(leaderActor, ApplyState.class);
    assertEquals(1, applyStateList.size());
    ApplyState applyState = applyStateList.get(0);
    assertEquals(2, applyState.getReplicatedLogEntry().getIndex());
    assertEquals(2, followerInfo.getMatchIndex());
    assertEquals(3, followerInfo.getNextIndex());
    assertEquals(payloadVersion, followerInfo.getPayloadVersion());
    assertEquals(RaftVersions.CURRENT_VERSION, followerInfo.getRaftVersion());
}
Also used : FollowerLogInformation(org.opendaylight.controller.cluster.raft.FollowerLogInformation) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) Test(org.junit.Test)

Example 4 with ApplyState

use of org.opendaylight.controller.cluster.raft.base.messages.ApplyState in project controller by opendaylight.

the class LeaderTest method testHandleReplicateMessageWhenThereAreNoFollowers.

@Test
public void testHandleReplicateMessageWhenThereAreNoFollowers() throws Exception {
    logStart("testHandleReplicateMessageWhenThereAreNoFollowers");
    MockRaftActorContext actorContext = createActorContext();
    leader = new Leader(actorContext);
    actorContext.setLastApplied(0);
    long newLogIndex = actorContext.getReplicatedLog().lastIndex() + 1;
    long term = actorContext.getTermInformation().getCurrentTerm();
    ReplicatedLogEntry newEntry = new SimpleReplicatedLogEntry(newLogIndex, term, new MockRaftActorContext.MockPayload("foo"));
    actorContext.getReplicatedLog().append(newEntry);
    final Identifier id = new MockIdentifier("state-id");
    RaftActorBehavior raftBehavior = leader.handleMessage(leaderActor, new Replicate(leaderActor, id, newEntry, true));
    // State should not change
    assertTrue(raftBehavior instanceof Leader);
    assertEquals("getCommitIndex", newLogIndex, actorContext.getCommitIndex());
    // We should get 2 ApplyState messages - 1 for new log entry and 1 for the previous
    // one since lastApplied state is 0.
    List<ApplyState> applyStateList = MessageCollectorActor.getAllMatching(leaderActor, ApplyState.class);
    assertEquals("ApplyState count", newLogIndex, applyStateList.size());
    for (int i = 0; i <= newLogIndex - 1; i++) {
        ApplyState applyState = applyStateList.get(i);
        assertEquals("getIndex", i + 1, applyState.getReplicatedLogEntry().getIndex());
        assertEquals("getTerm", term, applyState.getReplicatedLogEntry().getTerm());
    }
    ApplyState last = applyStateList.get((int) newLogIndex - 1);
    assertEquals("getData", newEntry.getData(), last.getReplicatedLogEntry().getData());
    assertEquals("getIdentifier", id, last.getIdentifier());
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) Identifier(org.opendaylight.yangtools.concepts.Identifier) Replicate(org.opendaylight.controller.cluster.raft.base.messages.Replicate) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) Test(org.junit.Test)

Example 5 with ApplyState

use of org.opendaylight.controller.cluster.raft.base.messages.ApplyState in project controller by opendaylight.

the class RaftActorTest method testApplyState.

@Test
public void testApplyState() throws Exception {
    String persistenceId = factory.generateActorId("leader-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    DataPersistenceProvider dataPersistenceProvider = mock(DataPersistenceProvider.class);
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, Collections.<String, String>emptyMap(), config, dataPersistenceProvider), persistenceId);
    MockRaftActor mockRaftActor = mockActorRef.underlyingActor();
    mockRaftActor.waitForInitializeBehaviorComplete();
    ReplicatedLogEntry entry = new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("F"));
    final Identifier id = new MockIdentifier("apply-state");
    mockRaftActor.getRaftActorContext().getApplyStateConsumer().accept(new ApplyState(mockActorRef, id, entry));
    verify(mockRaftActor.actorDelegate).applyState(eq(mockActorRef), eq(id), anyObject());
}
Also used : FiniteDuration(scala.concurrent.duration.FiniteDuration) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) ByteString(com.google.protobuf.ByteString) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) Identifier(org.opendaylight.yangtools.concepts.Identifier) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) Test(org.junit.Test)

Aggregations

ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)26 Test (org.junit.Test)17 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)13 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)11 FiniteDuration (scala.concurrent.duration.FiniteDuration)9 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)6 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)5 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)5 CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)5 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)5 ApplyJournalEntries (org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries)5 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)4 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)4 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)4 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)4 ActorRef (akka.actor.ActorRef)3 TestActorRef (akka.testkit.TestActorRef)3 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)3 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)3 LeaderTransitioning (org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning)2