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());
}
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());
}
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));
}
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());
}
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)));
}
Aggregations