Search in sources :

Example 11 with RaftLog

use of org.neo4j.causalclustering.core.consensus.log.RaftLog in project neo4j by neo4j.

the class FollowerTest method shouldUpdateCommitIndexIfNecessary.

@Test
public void shouldUpdateCommitIndexIfNecessary() throws Exception {
    //  If leaderCommit > commitIndex, set commitIndex = min( leaderCommit, index of last new entry )
    // given
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    RaftState state = raftState().myself(myself).entryLog(entryLog).build();
    Follower follower = new Follower();
    int localAppendIndex = 3;
    int localCommitIndex = localAppendIndex - 1;
    int term = 0;
    // append index is 0 based
    appendSomeEntriesToLog(state, follower, localAppendIndex, term, 1);
    // the next when-then simply verifies that the test is setup properly, with commit and append index as expected
    // when
    Outcome raftTestMemberOutcome = new Outcome(FOLLOWER, state);
    raftTestMemberOutcome.setCommitIndex(localCommitIndex);
    state.update(raftTestMemberOutcome);
    // then
    assertEquals(localAppendIndex, state.entryLog().appendIndex());
    assertEquals(localCommitIndex, state.commitIndex());
    // when
    // an append req comes in with leader commit index > localAppendIndex but localCommitIndex < localAppendIndex
    Outcome outcome = follower.handle(appendEntriesRequest().leaderTerm(term).prevLogIndex(3).prevLogTerm(term).leaderCommit(localCommitIndex + 4).build(), state, log());
    state.update(outcome);
    // then
    // The local commit index must be brought as far along as possible
    assertEquals(3, state.commitIndex());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)11 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)10 Test (org.junit.Test)9 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)9 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)7 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)6 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)5 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)4 Before (org.junit.Before)2 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)2 Log (org.neo4j.logging.Log)2 OutboundMessageCollector (org.neo4j.causalclustering.core.consensus.OutboundMessageCollector)1 RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)1 RaftMessage (org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage)1 ReplicatedString (org.neo4j.causalclustering.core.consensus.ReplicatedString)1 TestMessageBuilders.appendEntriesRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest)1 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)1 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)1 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)1 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)1