Search in sources :

Example 46 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.

the class LeaderTest method leaderShouldIgnoreSuccessResponseThatIndicatesLaggingWhileLocalStateIndicatesFollowerIsCaughtUp.

@Test
public void leaderShouldIgnoreSuccessResponseThatIndicatesLaggingWhileLocalStateIndicatesFollowerIsCaughtUp() 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);
    int j = 100;
    FollowerState instance2State = createArtificialFollowerState(j);
    ReadableRaftState state = mock(ReadableRaftState.class);
    FollowerStates<MemberId> followerState = new FollowerStates<>();
    followerState = new FollowerStates<>(followerState, instance2, instance2State);
    ReadableRaftLog logMock = mock(ReadableRaftLog.class);
    when(logMock.appendIndex()).thenReturn(100L);
    //  with commit requests in this test
    when(state.commitIndex()).thenReturn(-1L);
    when(state.entryLog()).thenReturn(logMock);
    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().success().matchIndex(80).term(4).from(instance2).build();
    Outcome outcome = leader.handle(response, state, mock(Log.class));
    // then the leader should not send anything, since this is a delayed, out of order response to a previous append
    // request
    assertTrue(outcome.getOutgoingMessages().isEmpty());
    // The follower state should not be touched
    FollowerStates<MemberId> updatedFollowerStates = outcome.getFollowerStates();
    assertEquals(100, updatedFollowerStates.get(instance2).getMatchIndex());
}
Also used : 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) MemberId(org.neo4j.causalclustering.identity.MemberId) ReadableRaftLog(org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog) 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) Test(org.junit.Test)

Example 47 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.

the class LeaderTest method leaderShouldRejectAppendEntriesResponseWithNewerTermAndBecomeAFollower.

@Test
public void leaderShouldRejectAppendEntriesResponseWithNewerTermAndBecomeAFollower() throws Exception {
    // given
    RaftState state = raftState().myself(myself).build();
    Leader leader = new Leader();
    // when
    AppendEntries.Response message = appendEntriesResponse().from(member1).term(state.term() + 1).build();
    Outcome outcome = leader.handle(message, state, log());
    // then
    assertEquals(0, count(outcome.getOutgoingMessages()));
    assertEquals(FOLLOWER, outcome.getRole());
    assertEquals(0, count(outcome.getLogCommands()));
    assertEquals(state.term() + 1, outcome.getTerm());
}
Also used : 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) Test(org.junit.Test)

Example 48 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.

the class LeaderTest method leaderShouldSendHeartbeatsToAllClusterMembersOnReceiptOfHeartbeatTick.

// TODO: test that shows we don't commit for previous terms
@Test
public void leaderShouldSendHeartbeatsToAllClusterMembersOnReceiptOfHeartbeatTick() throws Exception {
    // given
    RaftState state = raftState().votingMembers(myself, member1, member2).replicationMembers(myself, member1, member2).build();
    Leader leader = new Leader();
    // make sure it has quorum.
    leader.handle(new RaftMessages.HeartbeatResponse(member1), state, log());
    // when
    Outcome outcome = leader.handle(new Heartbeat(myself), state, log());
    // then
    assertTrue(messageFor(outcome, member1) instanceof RaftMessages.Heartbeat);
    assertTrue(messageFor(outcome, member2) instanceof RaftMessages.Heartbeat);
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) Heartbeat(org.neo4j.causalclustering.core.consensus.RaftMessages.Timeout.Heartbeat) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) Test(org.junit.Test)

Example 49 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome 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 50 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome 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)

Aggregations

Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)58 Test (org.junit.Test)53 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)42 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)27 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)24 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)19 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)15 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)11 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)11 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)9 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)6 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)6 Log (org.neo4j.logging.Log)6 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)5 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)5 MemberId (org.neo4j.causalclustering.identity.MemberId)5 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)4 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)4 ReplicatedString (org.neo4j.causalclustering.core.consensus.ReplicatedString)3 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)3