Search in sources :

Example 1 with InMemoryRaftLog

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

the class HeartbeatTest method shouldNotResultInCommitIfHistoryMismatches.

@Test
public void shouldNotResultInCommitIfHistoryMismatches() throws Exception {
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftState state = raftState().myself(myself).entryLog(raftLog).build();
    long leaderTerm = state.term() + leaderTermDifference;
    raftLog.append(new RaftLogEntry(leaderTerm, 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.getCommitIndex(), Matchers.equalTo(0L));
}
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 2 with InMemoryRaftLog

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

the class LeaderTest method leaderShouldSpawnMismatchCommandOnFailure.

// TODO: rethink this test, it does too much
@Test
public void leaderShouldSpawnMismatchCommandOnFailure() throws Exception {
    // given
    /*
         * A leader who
         * - has an append index of 100
         * - knows about instance 2
         * - assumes that instance 2 is fully caught up
         */
    Leader leader = new Leader();
    MemberId instance2 = member(2);
    FollowerState instance2State = createArtificialFollowerState(100);
    ReadableRaftState state = mock(ReadableRaftState.class);
    FollowerStates<MemberId> followerState = new FollowerStates<>();
    followerState = new FollowerStates<>(followerState, instance2, instance2State);
    RaftLog log = new InMemoryRaftLog();
    for (int i = 0; i <= 100; i++) {
        log.append(new RaftLogEntry(0, valueOf(i)));
    }
    when(state.commitIndex()).thenReturn(-1L);
    when(state.entryLog()).thenReturn(log);
    when(state.followerStates()).thenReturn(followerState);
    // both leader and follower are in the same term
    when(state.term()).thenReturn(4L);
    // when
    // that leader is asked to handle a response from that follower that says that the follower is still missing
    // things
    RaftMessages.AppendEntries.Response response = appendEntriesResponse().failure().appendIndex(0).matchIndex(-1).term(4).from(instance2).build();
    Outcome outcome = leader.handle(response, state, mock(Log.class));
    // then
    int mismatchCount = 0;
    for (ShipCommand shipCommand : outcome.getShipCommands()) {
        if (shipCommand instanceof ShipCommand.Mismatch) {
            mismatchCount++;
        }
    }
    assertThat(mismatchCount, greaterThan(0));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Log(org.neo4j.logging.Log) ReadableRaftLog(org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) MemberId(org.neo4j.causalclustering.identity.MemberId) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) FollowerStates(org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates) FollowerState(org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) ReadableRaftLog(org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Test(org.junit.Test)

Example 3 with InMemoryRaftLog

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

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

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

the class PruningTest method shouldGeneratePruneCommandsOnRequest.

@Test
public void shouldGeneratePruneCommandsOnRequest() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftState state = raftState().myself(myself).entryLog(raftLog).build();
    // when
    RaftMessages.PruneRequest pruneRequest = new RaftMessages.PruneRequest(1000);
    Outcome outcome = role.handler.handle(pruneRequest, state, log());
    // then
    assertThat(outcome.getLogCommands(), hasItem(new PruneLogCommand(1000)));
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) PruneLogCommand(org.neo4j.causalclustering.core.consensus.outcome.PruneLogCommand) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) 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