use of org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup 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.membership.RaftTestGroup 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.membership.RaftTestGroup in project neo4j by neo4j.
the class AppendEntriesMessageFlowTest method setup.
@Before
public void setup() throws IOException {
// given
RaftLog raftLog = new InMemoryRaftLog();
raftLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).raftLog(raftLog).outbound(outbound).build();
}
use of org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup in project neo4j by neo4j.
the class FollowerTest method followerLearningAboutHigherCommitCausesValuesTobeAppliedToItsLog.
// TODO move this to outcome tests
@Test
public void followerLearningAboutHigherCommitCausesValuesTobeAppliedToItsLog() throws Exception {
// given
RaftLog entryLog = new InMemoryRaftLog();
entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
RaftState state = raftState().myself(myself).entryLog(entryLog).build();
Follower follower = new Follower();
appendSomeEntriesToLog(state, follower, 3, 0, 1);
// when receiving AppEntries with high leader commit (4)
Outcome outcome = follower.handle(new AppendEntries.Request(myself, 0, 3, 0, new RaftLogEntry[] { new RaftLogEntry(0, ContentGenerator.content()) }, 4), state, log());
state.update(outcome);
// then
assertEquals(4, state.commitIndex());
}
use of org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup in project neo4j by neo4j.
the class FollowerTest method shouldUpdateCommitIndexIfNecessary.
@Test
public void shouldUpdateCommitIndexIfNecessary() throws Exception {
// If leaderCommit > commitIndex, set commitIndex = min( leaderCommit, index of last new entry )
// given
RaftLog entryLog = new InMemoryRaftLog();
entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
RaftState state = raftState().myself(myself).entryLog(entryLog).build();
Follower follower = new Follower();
int localAppendIndex = 3;
int localCommitIndex = localAppendIndex - 1;
int term = 0;
// append index is 0 based
appendSomeEntriesToLog(state, follower, localAppendIndex, term, 1);
// the next when-then simply verifies that the test is setup properly, with commit and append index as expected
// when
Outcome raftTestMemberOutcome = new Outcome(FOLLOWER, state);
raftTestMemberOutcome.setCommitIndex(localCommitIndex);
state.update(raftTestMemberOutcome);
// then
assertEquals(localAppendIndex, state.entryLog().appendIndex());
assertEquals(localCommitIndex, state.commitIndex());
// when
// an append req comes in with leader commit index > localAppendIndex but localCommitIndex < localAppendIndex
Outcome outcome = follower.handle(appendEntriesRequest().leaderTerm(term).prevLogIndex(3).prevLogTerm(term).leaderCommit(localCommitIndex + 4).build(), state, log());
state.update(outcome);
// then
// The local commit index must be brought as far along as possible
assertEquals(3, state.commitIndex());
}
Aggregations