Search in sources :

Example 16 with RaftState

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

the class VoteRequestTest method shouldStayInCurrentRoleOnRequestFromCurrentTerm.

@Test
public void shouldStayInCurrentRoleOnRequestFromCurrentTerm() throws Exception {
    // given
    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
    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 17 with RaftState

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

the class VoteRequestTest method shouldUpdateTermIfRequestIsFromLaterTerm.

@Test
public void shouldUpdateTermIfRequestIsFromLaterTerm() throws Exception {
    // given
    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
    assertEquals(candidateTerm, 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 18 with RaftState

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

the class VoteRequestTest method shouldMoveToFollowerIfRequestIsFromLaterTerm.

@Test
public void shouldMoveToFollowerIfRequestIsFromLaterTerm() throws Exception {
    // given
    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
    assertEquals(Role.FOLLOWER, 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 19 with RaftState

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

the class AppendEntriesRequestTest method shouldAcceptInitialEntriesAfterBootstrap.

@Test
public void shouldAcceptInitialEntriesAfterBootstrap() throws Exception {
    RaftLog raftLog = bootstrappedLog();
    RaftState state = raftState().entryLog(raftLog).myself(myself).build();
    long leaderTerm = state.term() + leaderTermDifference;
    RaftLogEntry logEntry1 = new RaftLogEntry(leaderTerm, content());
    RaftLogEntry logEntry2 = new RaftLogEntry(leaderTerm, content());
    // when
    Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(0).prevLogTerm(0).logEntry(logEntry1).logEntry(logEntry2).build(), state, log());
    // then
    assertTrue(((Response) messageFor(outcome, leader)).success());
    assertThat(outcome.getLogCommands(), hasItem(new BatchAppendLogEntries(1, 0, new RaftLogEntry[] { logEntry1, logEntry2 })));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) 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 20 with RaftState

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

the class AppendEntriesRequestTest method shouldRejectDiscontinuousEntries.

@Test
public void shouldRejectDiscontinuousEntries() throws Exception {
    // given
    RaftState state = raftState().myself(myself).build();
    long leaderTerm = state.term() + leaderTermDifference;
    // when
    Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(state.entryLog().appendIndex() + 1).prevLogTerm(leaderTerm).logEntry(new RaftLogEntry(leaderTerm, content())).build(), state, log());
    // then
    Response response = (Response) messageFor(outcome, leader);
    assertEquals(state.entryLog().appendIndex(), response.appendIndex());
    assertFalse(response.success());
}
Also used : Response(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries.Response) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) 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