Search in sources :

Example 1 with RaftTestGroup

use of org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup in project neo4j by neo4j.

the class FollowerTest method followerReceivingHeartbeatIndicatingClusterIsAheadShouldElicitAppendResponse.

@Test
public void followerReceivingHeartbeatIndicatingClusterIsAheadShouldElicitAppendResponse() throws Exception {
    // given
    int term = 1;
    int followerAppendIndex = 9;
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    RaftState state = raftState().myself(myself).term(term).build();
    Follower follower = new Follower();
    appendSomeEntriesToLog(state, follower, followerAppendIndex - 1, term, 1);
    AppendEntries.Request heartbeat = appendEntriesRequest().from(member1).leaderTerm(term).prevLogIndex(// leader has appended 2 ahead from this follower
    followerAppendIndex + 2).prevLogTerm(// in the same term
    term).build();
    Outcome outcome = follower.handle(heartbeat, state, log());
    assertEquals(1, outcome.getOutgoingMessages().size());
    RaftMessage outgoing = outcome.getOutgoingMessages().iterator().next().message();
    assertEquals(RaftMessages.Type.APPEND_ENTRIES_RESPONSE, outgoing.type());
    RaftMessages.AppendEntries.Response response = (AppendEntries.Response) outgoing;
    assertFalse(response.success());
}
Also used : RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) RaftMessage(org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Test(org.junit.Test)

Example 2 with RaftTestGroup

use of org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup in project neo4j by neo4j.

the class FollowerTest method shouldTruncateIfTermDoesNotMatch.

@Test
public void shouldTruncateIfTermDoesNotMatch() throws Exception {
    // given
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    int term = 1;
    RaftState state = raftState().myself(myself).entryLog(entryLog).term(term).build();
    Follower follower = new Follower();
    state.update(follower.handle(new AppendEntries.Request(member1, 1, 0, 0, new RaftLogEntry[] { new RaftLogEntry(2, ContentGenerator.content()) }, 0), state, log()));
    RaftLogEntry[] entries = { new RaftLogEntry(1, new ReplicatedString("commit this!")) };
    Outcome outcome = follower.handle(new AppendEntries.Request(member1, 1, 0, 0, entries, 0), state, log());
    state.update(outcome);
    // then
    assertEquals(1, state.entryLog().appendIndex());
    assertEquals(1, state.entryLog().readEntryTerm(1));
}
Also used : RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) TestMessageBuilders.appendEntriesRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) ReplicatedString(org.neo4j.causalclustering.core.consensus.ReplicatedString) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Test(org.junit.Test)

Example 3 with RaftTestGroup

use of org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup in project neo4j by neo4j.

the class AppendEntriesMessageFlowTest method setup.

@Before
public void setup() throws IOException {
    // given
    RaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).raftLog(raftLog).outbound(outbound).build();
}
Also used : RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Before(org.junit.Before)

Example 4 with RaftTestGroup

use of org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup in project neo4j by neo4j.

the class FollowerTest method followerLearningAboutHigherCommitCausesValuesTobeAppliedToItsLog.

// TODO move this to outcome tests
@Test
public void followerLearningAboutHigherCommitCausesValuesTobeAppliedToItsLog() throws Exception {
    // 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();
    appendSomeEntriesToLog(state, follower, 3, 0, 1);
    // when receiving AppEntries with high leader commit (4)
    Outcome outcome = follower.handle(new AppendEntries.Request(myself, 0, 3, 0, new RaftLogEntry[] { new RaftLogEntry(0, ContentGenerator.content()) }, 4), state, log());
    state.update(outcome);
    // then
    assertEquals(4, 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) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) 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)

Example 5 with RaftTestGroup

use of org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup 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

InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)5 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)5 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)5 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)5 Test (org.junit.Test)4 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)4 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)4 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)3 Before (org.junit.Before)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