use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class VoteRequestTest method shouldVoteForOnlyOneCandidatePerTerm.
@Test
public void shouldVoteForOnlyOneCandidatePerTerm() throws Exception {
// given
RaftState state = newState();
// when
final long candidateTerm = state.term() + 1;
Outcome outcome1 = role.handler.handle(voteRequest().from(member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
state.update(outcome1);
Outcome outcome2 = role.handler.handle(voteRequest().from(member2).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
// then
assertFalse(((RaftMessages.Vote.Response) messageFor(outcome2, member2)).voteGranted());
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class RaftStateTest method shouldUpdateCacheState.
@Test
public void shouldUpdateCacheState() throws Exception {
//Test that updates applied to the raft state will be refelcted in the entry cache.
//given
InFlightMap<RaftLogEntry> cache = new InFlightMap<>();
RaftState raftState = new RaftState(member(0), new InMemoryStateStorage<>(new TermState()), new FakeMembership(), new InMemoryRaftLog(), new InMemoryStateStorage<>(new VoteState()), cache, NullLogProvider.getInstance());
List<RaftLogCommand> logCommands = new LinkedList<RaftLogCommand>() {
{
add(new AppendLogEntry(1, new RaftLogEntry(0L, valueOf(0))));
add(new AppendLogEntry(2, new RaftLogEntry(0L, valueOf(1))));
add(new AppendLogEntry(3, new RaftLogEntry(0L, valueOf(2))));
add(new AppendLogEntry(4, new RaftLogEntry(0L, valueOf(4))));
add(new TruncateLogCommand(3));
add(new AppendLogEntry(3, new RaftLogEntry(0L, valueOf(5))));
}
};
Outcome raftTestMemberOutcome = new Outcome(CANDIDATE, 0, null, -1, null, emptySet(), -1, initialFollowerStates(), true, logCommands, emptyOutgoingMessages(), emptySet(), -1, emptySet());
//when
raftState.update(raftTestMemberOutcome);
//then
assertNotNull(cache.get(1L));
assertNotNull(cache.get(2L));
assertNotNull(cache.get(3L));
assertEquals(valueOf(5), cache.get(3L).content());
assertNull(cache.get(4L));
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class RaftStateTest method shouldRemoveFollowerStateAfterBecomingLeader.
@Test
public void shouldRemoveFollowerStateAfterBecomingLeader() throws Exception {
// given
RaftState raftState = new RaftState(member(0), new InMemoryStateStorage<>(new TermState()), new FakeMembership(), new InMemoryRaftLog(), new InMemoryStateStorage<>(new VoteState()), new InFlightMap<>(), NullLogProvider.getInstance());
raftState.update(new Outcome(CANDIDATE, 1, null, -1, null, emptySet(), -1, initialFollowerStates(), true, emptyLogCommands(), emptyOutgoingMessages(), emptySet(), -1, emptySet()));
// when
raftState.update(new Outcome(CANDIDATE, 1, null, -1, null, emptySet(), -1, new FollowerStates<>(), true, emptyLogCommands(), emptyOutgoingMessages(), emptySet(), -1, emptySet()));
// then
assertEquals(0, raftState.followerStates().size());
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class HeartbeatTest method shouldNotResultInCommitIfReferringToFutureEntries.
@Test
public void shouldNotResultInCommitIfReferringToFutureEntries() 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() + 1).commitIndexTerm(// And is in the same term
leaderTerm).leaderTerm(leaderTerm).build();
Outcome outcome = role.handler.handle(heartbeat, state, log());
assertThat(outcome.getLogCommands(), empty());
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class HeartbeatTest method shouldResultInCommitIfHistoryMatches.
@Test
public void shouldResultInCommitIfHistoryMatches() throws Exception {
InMemoryRaftLog raftLog = new InMemoryRaftLog();
RaftState state = raftState().myself(myself).entryLog(raftLog).build();
long leaderTerm = state.term() + leaderTermDifference;
raftLog.append(new RaftLogEntry(leaderTerm - 1, 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.getLogCommands(), empty());
}
Aggregations