Search in sources :

Example 11 with RaftState

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

the class FollowerTest method shouldBecomeCandidateOnReceivingElectionTimeoutMessage.

@Test
public void shouldBecomeCandidateOnReceivingElectionTimeoutMessage() throws Exception {
    // given
    RaftState state = raftState().myself(myself).votingMembers(asSet(myself, member1, member2)).build();
    Follower follower = new Follower();
    // when
    Outcome outcome = follower.handle(new Election(myself), 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) Election(org.neo4j.causalclustering.core.consensus.RaftMessages.Timeout.Election) Test(org.junit.Test)

Example 12 with RaftState

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

the class FollowerTest method followerReceivingHeartbeatIndicatingClusterIsAheadShouldElicitAppendResponse.

@Test
public void followerReceivingHeartbeatIndicatingClusterIsAheadShouldElicitAppendResponse() throws Exception {
    // given
    int term = 1;
    int followerAppendIndex = 9;
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    RaftState state = raftState().myself(myself).term(term).build();
    Follower follower = new Follower();
    appendSomeEntriesToLog(state, follower, followerAppendIndex - 1, term, 1);
    AppendEntries.Request heartbeat = appendEntriesRequest().from(member1).leaderTerm(term).prevLogIndex(// leader has appended 2 ahead from this follower
    followerAppendIndex + 2).prevLogTerm(// in the same term
    term).build();
    Outcome outcome = follower.handle(heartbeat, state, log());
    assertEquals(1, outcome.getOutgoingMessages().size());
    RaftMessage outgoing = outcome.getOutgoingMessages().iterator().next().message();
    assertEquals(RaftMessages.Type.APPEND_ENTRIES_RESPONSE, outgoing.type());
    RaftMessages.AppendEntries.Response response = (AppendEntries.Response) outgoing;
    assertFalse(response.success());
}
Also used : RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) RaftMessage(org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Test(org.junit.Test)

Example 13 with RaftState

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

the class FollowerTest method shouldTruncateIfTermDoesNotMatch.

@Test
public void shouldTruncateIfTermDoesNotMatch() throws Exception {
    // given
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    int term = 1;
    RaftState state = raftState().myself(myself).entryLog(entryLog).term(term).build();
    Follower follower = new Follower();
    state.update(follower.handle(new AppendEntries.Request(member1, 1, 0, 0, new RaftLogEntry[] { new RaftLogEntry(2, ContentGenerator.content()) }, 0), state, log()));
    RaftLogEntry[] entries = { new RaftLogEntry(1, new ReplicatedString("commit this!")) };
    Outcome outcome = follower.handle(new AppendEntries.Request(member1, 1, 0, 0, entries, 0), state, log());
    state.update(outcome);
    // then
    assertEquals(1, state.entryLog().appendIndex());
    assertEquals(1, state.entryLog().readEntryTerm(1));
}
Also used : RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) TestMessageBuilders.appendEntriesRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) ReplicatedString(org.neo4j.causalclustering.core.consensus.ReplicatedString) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Test(org.junit.Test)

Example 14 with RaftState

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

the class FollowerTest method shouldRenewElectionTimeoutOnReceiptOfHeartbeatInCurrentOrHigherTerm.

@Test
public void shouldRenewElectionTimeoutOnReceiptOfHeartbeatInCurrentOrHigherTerm() throws Exception {
    // given
    RaftState state = raftState().myself(myself).term(0).build();
    Follower follower = new Follower();
    Outcome outcome = follower.handle(new RaftMessages.Heartbeat(myself, 1, 1, 1), state, log());
    // then
    assertTrue(outcome.electionTimeoutRenewed());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) Test(org.junit.Test)

Example 15 with RaftState

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

the class PruningTest method shouldGeneratePruneCommandsOnRequest.

@Test
public void shouldGeneratePruneCommandsOnRequest() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftState state = raftState().myself(myself).entryLog(raftLog).build();
    // when
    RaftMessages.PruneRequest pruneRequest = new RaftMessages.PruneRequest(1000);
    Outcome outcome = role.handler.handle(pruneRequest, state, log());
    // then
    assertThat(outcome.getLogCommands(), hasItem(new PruneLogCommand(1000)));
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) PruneLogCommand(org.neo4j.causalclustering.core.consensus.outcome.PruneLogCommand) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) 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