Search in sources :

Example 26 with InMemoryRaftLog

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

the class HeartbeatTest method shouldResultInCommitIfHistoryMatches.

@Test
public void shouldResultInCommitIfHistoryMatches() throws Exception {
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftState state = raftState().myself(myself).entryLog(raftLog).build();
    long leaderTerm = state.term() + leaderTermDifference;
    raftLog.append(new RaftLogEntry(leaderTerm - 1, content()));
    RaftMessages.Heartbeat heartbeat = heartbeat().from(leader).commitIndex(// The leader is talking about committing stuff we don't know about
    raftLog.appendIndex()).commitIndexTerm(// And is in the same term
    leaderTerm).leaderTerm(leaderTerm).build();
    Outcome outcome = role.handler.handle(heartbeat, state, log());
    assertThat(outcome.getLogCommands(), empty());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 27 with InMemoryRaftLog

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

the class LeaderTest method leaderShouldCommitOnMajorityResponse.

@Test
public void leaderShouldCommitOnMajorityResponse() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, new ReplicatedString("lalalala")));
    RaftState state = raftState().votingMembers(member1, member2).term(0).lastLogIndexBeforeWeBecameLeader(-1).leader(myself).leaderCommit(-1).entryLog(raftLog).messagesSentToFollower(member1, raftLog.appendIndex() + 1).messagesSentToFollower(member2, raftLog.appendIndex() + 1).build();
    Leader leader = new Leader();
    // when a single instance responds (plus self == 2 out of 3 instances)
    Outcome outcome = leader.handle(new RaftMessages.AppendEntries.Response(member1, 0, true, 0, 0), state, log());
    // then
    assertEquals(0L, outcome.getCommitIndex());
    assertEquals(0L, outcome.getLeaderCommit());
}
Also used : ReplicatedString(org.neo4j.causalclustering.core.consensus.ReplicatedString) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 28 with InMemoryRaftLog

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

the class LeaderTest method leaderShouldCommitAllPreviouslyAppendedEntriesWhenCommittingLaterEntryInSameTerm.

// TODO move this someplace else, since log no longer holds the commit
@Test
public void leaderShouldCommitAllPreviouslyAppendedEntriesWhenCommittingLaterEntryInSameTerm() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, new ReplicatedString("first!")));
    raftLog.append(new RaftLogEntry(0, new ReplicatedString("second")));
    raftLog.append(new RaftLogEntry(0, new ReplicatedString("third")));
    RaftState state = raftState().votingMembers(myself, member1, member2).term(0).entryLog(raftLog).messagesSentToFollower(member1, raftLog.appendIndex() + 1).messagesSentToFollower(member2, raftLog.appendIndex() + 1).build();
    Leader leader = new Leader();
    // when
    Outcome outcome = leader.handle(new AppendEntries.Response(member1, 0, true, 2, 2), state, log());
    state.update(outcome);
    // then
    assertEquals(2, state.commitIndex());
}
Also used : ReplicatedString(org.neo4j.causalclustering.core.consensus.ReplicatedString) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 29 with InMemoryRaftLog

use of org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog 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 30 with InMemoryRaftLog

use of org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog 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)30 Test (org.junit.Test)27 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)23 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)18 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)15 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)7 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)6 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)5 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)4 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)4 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)4 ReplicatedString (org.neo4j.causalclustering.core.consensus.ReplicatedString)3 RaftLogCommand (org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand)3 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)3 Before (org.junit.Before)2 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)2 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)2 ExposedRaftState (org.neo4j.causalclustering.core.consensus.state.ExposedRaftState)2 TermState (org.neo4j.causalclustering.core.consensus.term.TermState)2 VoteState (org.neo4j.causalclustering.core.consensus.vote.VoteState)2