use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.
the class LeaderTest method leaderShouldCommitOnMajorityResponse.
@Test
public void leaderShouldCommitOnMajorityResponse() throws Exception {
// given
InMemoryRaftLog raftLog = new InMemoryRaftLog();
raftLog.append(new RaftLogEntry(0, new ReplicatedString("lalalala")));
RaftState state = raftState().votingMembers(member1, member2).term(0).lastLogIndexBeforeWeBecameLeader(-1).leader(myself).leaderCommit(-1).entryLog(raftLog).messagesSentToFollower(member1, raftLog.appendIndex() + 1).messagesSentToFollower(member2, raftLog.appendIndex() + 1).build();
Leader leader = new Leader();
// when a single instance responds (plus self == 2 out of 3 instances)
Outcome outcome = leader.handle(new RaftMessages.AppendEntries.Response(member1, 0, true, 0, 0), state, log());
// then
assertEquals(0L, outcome.getCommitIndex());
assertEquals(0L, outcome.getLeaderCommit());
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.
the class LeaderTest method leaderShouldCommitAllPreviouslyAppendedEntriesWhenCommittingLaterEntryInSameTerm.
// TODO move this someplace else, since log no longer holds the commit
@Test
public void leaderShouldCommitAllPreviouslyAppendedEntriesWhenCommittingLaterEntryInSameTerm() throws Exception {
// given
InMemoryRaftLog raftLog = new InMemoryRaftLog();
raftLog.append(new RaftLogEntry(0, new ReplicatedString("first!")));
raftLog.append(new RaftLogEntry(0, new ReplicatedString("second")));
raftLog.append(new RaftLogEntry(0, new ReplicatedString("third")));
RaftState state = raftState().votingMembers(myself, member1, member2).term(0).entryLog(raftLog).messagesSentToFollower(member1, raftLog.appendIndex() + 1).messagesSentToFollower(member2, raftLog.appendIndex() + 1).build();
Leader leader = new Leader();
// when
Outcome outcome = leader.handle(new AppendEntries.Response(member1, 0, true, 2, 2), state, log());
state.update(outcome);
// then
assertEquals(2, state.commitIndex());
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry 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.log.RaftLogEntry 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());
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.
the class CommandApplicationProcessTest method shouldPeriodicallyFlushState.
// TODO: Test recovery, see CoreState#start().
@Test
public void shouldPeriodicallyFlushState() throws Throwable {
// given
applicationProcess.start();
int interactions = flushEvery * 5;
for (int i = 0; i < interactions; i++) {
raftLog.append(new RaftLogEntry(0, operation(nullTx)));
}
// when
applicationProcess.notifyCommitted(interactions);
applier.sync(false);
// then
verify(coreStateMachines, times(interactions / batchSize)).flush();
assertEquals(interactions - (interactions % batchSize) - 1, (long) lastFlushedStorage.getInitialState());
}
Aggregations