Search in sources :

Example 6 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.

the class NonFollowerVoteRequestTest method shouldRejectVoteRequestFromPreviousTerm.

@Test
public void shouldRejectVoteRequestFromPreviousTerm() throws Exception {
    RaftState state = newState();
    // when
    final long candidateTerm = state.term() - 1;
    Outcome outcome = role.handler.handle(voteRequest().from(member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
    // then
    assertFalse(((RaftMessages.Vote.Response) messageFor(outcome, member1)).voteGranted());
    assertEquals(role, outcome.getRole());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) Test(org.junit.Test)

Example 7 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.

the class NonFollowerVoteRequestTest method shouldRejectVoteRequestFromCurrentTerm.

@Test
public void shouldRejectVoteRequestFromCurrentTerm() throws Exception {
    RaftState state = newState();
    // when
    final long candidateTerm = state.term();
    Outcome outcome = role.handler.handle(voteRequest().from(member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
    // then
    assertFalse(((RaftMessages.Vote.Response) messageFor(outcome, member1)).voteGranted());
    assertEquals(role, outcome.getRole());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) Test(org.junit.Test)

Example 8 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.

the class CandidateTest method shouldUpdateTermOnReceivingVoteResponseWithLaterTerm.

@Test
public void shouldUpdateTermOnReceivingVoteResponseWithLaterTerm() throws Exception {
    // given
    RaftState state = newState();
    final long voterTerm = state.term() + 1;
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(voterTerm).from(member1).grant().build(), state, log());
    // then
    assertEquals(FOLLOWER, outcome.getRole());
    assertEquals(voterTerm, outcome.getTerm());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) Test(org.junit.Test)

Example 9 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.

the class CandidateTest method shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm.

@Test
public void shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm() throws Exception {
    // given
    RaftState state = RaftStateBuilder.raftState().term(1).myself(myself).votingMembers(member1, member2).replicationMembers(member1, member2).build();
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(state.term()).from(member1).grant().build(), state, log());
    // then
    assertEquals(LEADER, outcome.getRole());
    assertEquals(true, outcome.electionTimeoutRenewed());
    assertThat(outcome.getLogCommands(), hasItem(new AppendLogEntry(0, new RaftLogEntry(state.term(), new NewLeaderBarrier()))));
    assertThat(outcome.getOutgoingMessages(), hasItems(new RaftMessages.Directed(member1, new RaftMessages.Heartbeat(myself, state.term(), -1, -1)), new RaftMessages.Directed(member2, new RaftMessages.Heartbeat(myself, state.term(), -1, -1))));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 10 with RaftState

use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.

the class CandidateTest method shouldRejectVoteResponseWithOldTerm.

@Test
public void shouldRejectVoteResponseWithOldTerm() throws Exception {
    // given
    RaftState state = newState();
    final long voterTerm = state.term() - 1;
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(voterTerm).from(member1).grant().build(), state, log());
    // then
    assertEquals(CANDIDATE, outcome.getRole());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) 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