Search in sources :

Example 1 with RaftState

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

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

the class LeaderTest method leaderShouldNotStepDownWhenReceivedQuorumOfHeartbeatResponses.

@Test
public void leaderShouldNotStepDownWhenReceivedQuorumOfHeartbeatResponses() throws Exception {
    // given
    RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
    Leader leader = new Leader();
    // when
    Outcome outcome = leader.handle(new RaftMessages.HeartbeatResponse(member1), state, log());
    state.update(outcome);
    // we now have quorum and should not step down
    outcome = leader.handle(new RaftMessages.Timeout.Election(myself), state, log());
    // then
    assertThat(outcome.getRole(), is(LEADER));
}
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) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) Test(org.junit.Test)

Example 3 with RaftState

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

the class LeaderTest method leaderShouldDecideToAppendToItsLogAndSendAppendEntriesMessageOnReceiptOfClientProposal.

@Test
public void leaderShouldDecideToAppendToItsLogAndSendAppendEntriesMessageOnReceiptOfClientProposal() throws Exception {
    // given
    RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
    Leader leader = new Leader();
    RaftMessages.NewEntry.Request newEntryRequest = new RaftMessages.NewEntry.Request(member(9), CONTENT);
    // when
    Outcome outcome = leader.handle(newEntryRequest, state, log());
    //state.update( outcome );
    // then
    AppendLogEntry logCommand = (AppendLogEntry) single(outcome.getLogCommands());
    assertEquals(0, logCommand.index);
    assertEquals(0, logCommand.entry.term());
    ShipCommand.NewEntries shipCommand = (ShipCommand.NewEntries) single(outcome.getShipCommands());
    assertEquals(shipCommand, new ShipCommand.NewEntries(-1, -1, new RaftLogEntry[] { new RaftLogEntry(0, CONTENT) }));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 4 with RaftState

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

the class LeaderTest method leaderShouldHandleBatch.

@Test
public void leaderShouldHandleBatch() throws Exception {
    // given
    RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
    Leader leader = new Leader();
    int BATCH_SIZE = 3;
    RaftMessages.NewEntry.BatchRequest batchRequest = new RaftMessages.NewEntry.BatchRequest(BATCH_SIZE);
    batchRequest.add(valueOf(0));
    batchRequest.add(valueOf(1));
    batchRequest.add(valueOf(2));
    // when
    Outcome outcome = leader.handle(batchRequest, state, log());
    // then
    BatchAppendLogEntries logCommand = (BatchAppendLogEntries) single(outcome.getLogCommands());
    assertEquals(0, logCommand.baseIndex);
    for (int i = 0; i < BATCH_SIZE; i++) {
        assertEquals(0, logCommand.entries[i].term());
        assertEquals(i, ((ReplicatedInteger) logCommand.entries[i].content()).get());
    }
    ShipCommand.NewEntries shipCommand = (ShipCommand.NewEntries) single(outcome.getShipCommands());
    assertEquals(shipCommand, new ShipCommand.NewEntries(-1, -1, new RaftLogEntry[] { new RaftLogEntry(0, valueOf(0)), new RaftLogEntry(0, valueOf(1)), new RaftLogEntry(0, valueOf(2)) }));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) Test(org.junit.Test)

Example 5 with RaftState

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

the class LeaderTest method leaderShouldStepDownWhenLackingHeartbeatResponses.

@Test
public void leaderShouldStepDownWhenLackingHeartbeatResponses() throws Exception {
    // given
    RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).leader(myself).build();
    Leader leader = new Leader();
    leader.handle(new RaftMessages.Timeout.Election(myself), state, log());
    // when
    Outcome outcome = leader.handle(new RaftMessages.Timeout.Election(myself), state, log());
    // then
    assertThat(outcome.getRole(), not(LEADER));
    assertNull(outcome.getLeader());
}
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) 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