use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class LeaderTest method leaderShouldHandleBatch.
@Test
public void leaderShouldHandleBatch() throws Exception {
// given
RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
Leader leader = new Leader();
int BATCH_SIZE = 3;
RaftMessages.NewEntry.BatchRequest batchRequest = new RaftMessages.NewEntry.BatchRequest(BATCH_SIZE);
batchRequest.add(valueOf(0));
batchRequest.add(valueOf(1));
batchRequest.add(valueOf(2));
// when
Outcome outcome = leader.handle(batchRequest, state, log());
// then
BatchAppendLogEntries logCommand = (BatchAppendLogEntries) single(outcome.getLogCommands());
assertEquals(0, logCommand.baseIndex);
for (int i = 0; i < BATCH_SIZE; i++) {
assertEquals(0, logCommand.entries[i].term());
assertEquals(i, ((ReplicatedInteger) logCommand.entries[i].content()).get());
}
ShipCommand.NewEntries shipCommand = (ShipCommand.NewEntries) single(outcome.getShipCommands());
assertEquals(shipCommand, new ShipCommand.NewEntries(-1, -1, new RaftLogEntry[] { new RaftLogEntry(0, valueOf(0)), new RaftLogEntry(0, valueOf(1)), new RaftLogEntry(0, valueOf(2)) }));
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class LeaderTest method leaderShouldSpawnMismatchCommandOnFailure.
// TODO: rethink this test, it does too much
@Test
public void leaderShouldSpawnMismatchCommandOnFailure() throws Exception {
// given
/*
* A leader who
* - has an append index of 100
* - knows about instance 2
* - assumes that instance 2 is fully caught up
*/
Leader leader = new Leader();
MemberId instance2 = member(2);
FollowerState instance2State = createArtificialFollowerState(100);
ReadableRaftState state = mock(ReadableRaftState.class);
FollowerStates<MemberId> followerState = new FollowerStates<>();
followerState = new FollowerStates<>(followerState, instance2, instance2State);
RaftLog log = new InMemoryRaftLog();
for (int i = 0; i <= 100; i++) {
log.append(new RaftLogEntry(0, valueOf(i)));
}
when(state.commitIndex()).thenReturn(-1L);
when(state.entryLog()).thenReturn(log);
when(state.followerStates()).thenReturn(followerState);
// both leader and follower are in the same term
when(state.term()).thenReturn(4L);
// when
// that leader is asked to handle a response from that follower that says that the follower is still missing
// things
RaftMessages.AppendEntries.Response response = appendEntriesResponse().failure().appendIndex(0).matchIndex(-1).term(4).from(instance2).build();
Outcome outcome = leader.handle(response, state, mock(Log.class));
// then
int mismatchCount = 0;
for (ShipCommand shipCommand : outcome.getShipCommands()) {
if (shipCommand instanceof ShipCommand.Mismatch) {
mismatchCount++;
}
}
assertThat(mismatchCount, greaterThan(0));
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class LeaderTest method leaderShouldNotRespondToSuccessResponseFromFollowerThatWillSoonUpToDateViaInFlightMessages.
@Test
public void leaderShouldNotRespondToSuccessResponseFromFollowerThatWillSoonUpToDateViaInFlightMessages() throws Exception {
// given
/*
* A leader who
* - has an append index of 100
* - knows about instance 2
* - assumes that instance 2 is at an index less than 100 -say 84 but it has already been sent up to 100
*/
Leader leader = new Leader();
MemberId instance2 = member(2);
FollowerState instance2State = createArtificialFollowerState(84);
ReadableRaftState state = mock(ReadableRaftState.class);
FollowerStates<MemberId> followerState = new FollowerStates<>();
followerState = new FollowerStates<>(followerState, instance2, instance2State);
ReadableRaftLog logMock = mock(ReadableRaftLog.class);
when(logMock.appendIndex()).thenReturn(100L);
when(state.commitIndex()).thenReturn(-1L);
when(state.entryLog()).thenReturn(logMock);
when(state.followerStates()).thenReturn(followerState);
// both leader and follower are in the same term
when(state.term()).thenReturn(4L);
// when
// that leader is asked to handle a response from that follower that says that the follower is up to date
RaftMessages.AppendEntries.Response response = appendEntriesResponse().success().matchIndex(90).term(4).from(instance2).build();
Outcome outcome = leader.handle(response, state, mock(Log.class));
// then
// The leader should not be trying to send any messages to that instance
assertTrue(outcome.getOutgoingMessages().isEmpty());
// And the follower state should be updated
FollowerStates<MemberId> leadersViewOfFollowerStates = outcome.getFollowerStates();
assertEquals(90, leadersViewOfFollowerStates.get(instance2).getMatchIndex());
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class LeaderTest method leaderShouldStepDownWhenLackingHeartbeatResponses.
@Test
public void leaderShouldStepDownWhenLackingHeartbeatResponses() throws Exception {
// given
RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).leader(myself).build();
Leader leader = new Leader();
leader.handle(new RaftMessages.Timeout.Election(myself), state, log());
// when
Outcome outcome = leader.handle(new RaftMessages.Timeout.Election(myself), state, log());
// then
assertThat(outcome.getRole(), not(LEADER));
assertNull(outcome.getLeader());
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class NonFollowerVoteRequestTest method shouldRejectVoteRequestFromPreviousTerm.
@Test
public void shouldRejectVoteRequestFromPreviousTerm() throws Exception {
RaftState state = newState();
// when
final long candidateTerm = state.term() - 1;
Outcome outcome = role.handler.handle(voteRequest().from(member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
// then
assertFalse(((RaftMessages.Vote.Response) messageFor(outcome, member1)).voteGranted());
assertEquals(role, outcome.getRole());
}
Aggregations