Search in sources :

Example 31 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState 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 32 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState 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 33 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState 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 34 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState 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 35 with RaftState

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

Test (org.junit.Test)42 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)42 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)42 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)20 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)17 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)13 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)9 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)6 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)6 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)4 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)4 ReplicatedString (org.neo4j.causalclustering.core.consensus.ReplicatedString)3 Election (org.neo4j.causalclustering.core.consensus.RaftMessages.Timeout.Election)2 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)2 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)2 NewLeaderBarrier (org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)1 Response (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries.Response)1 RaftMessage (org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage)1 Heartbeat (org.neo4j.causalclustering.core.consensus.RaftMessages.Timeout.Heartbeat)1 TestMessageBuilders.appendEntriesRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest)1