use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class HeartbeatTest method shouldNotResultInCommitIfHistoryMismatches.
@Test
public void shouldNotResultInCommitIfHistoryMismatches() 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()).commitIndexTerm(// And is in the same term
leaderTerm).leaderTerm(leaderTerm).build();
Outcome outcome = role.handler.handle(heartbeat, state, log());
assertThat(outcome.getCommitIndex(), Matchers.equalTo(0L));
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class LeaderTest method leaderShouldNotRespondToSuccessResponseThatIndicatesUpToDateFollower.
@Test
public void leaderShouldNotRespondToSuccessResponseThatIndicatesUpToDateFollower() 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
*/
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(100).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> updatedFollowerStates = outcome.getFollowerStates();
assertEquals(100, updatedFollowerStates.get(instance2).getMatchIndex());
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class LeaderTest method leaderShouldRespondToSuccessResponseThatIndicatesLaggingFollowerWithJustWhatItsMissing.
@Test
public void leaderShouldRespondToSuccessResponseThatIndicatesLaggingFollowerWithJustWhatItsMissing() 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 50
*/
Leader leader = new Leader();
MemberId instance2 = member(2);
FollowerState instance2State = createArtificialFollowerState(50);
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);
// with commit requests in this test
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(231L);
// 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().success().matchIndex(89).term(231).from(instance2).build();
Outcome outcome = leader.handle(response, state, mock(Log.class));
// then
int matchCount = 0;
for (ShipCommand shipCommand : outcome.getShipCommands()) {
if (shipCommand instanceof ShipCommand.Match) {
matchCount++;
}
}
assertThat(matchCount, greaterThan(0));
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class LeaderTest method leaderShouldNotStepDownWhenReceivedQuorumOfHeartbeatResponses.
@Test
public void leaderShouldNotStepDownWhenReceivedQuorumOfHeartbeatResponses() throws Exception {
// given
RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
Leader leader = new Leader();
// when
Outcome outcome = leader.handle(new RaftMessages.HeartbeatResponse(member1), state, log());
state.update(outcome);
// we now have quorum and should not step down
outcome = leader.handle(new RaftMessages.Timeout.Election(myself), state, log());
// then
assertThat(outcome.getRole(), is(LEADER));
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class LeaderTest method leaderShouldDecideToAppendToItsLogAndSendAppendEntriesMessageOnReceiptOfClientProposal.
@Test
public void leaderShouldDecideToAppendToItsLogAndSendAppendEntriesMessageOnReceiptOfClientProposal() throws Exception {
// given
RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
Leader leader = new Leader();
RaftMessages.NewEntry.Request newEntryRequest = new RaftMessages.NewEntry.Request(member(9), CONTENT);
// when
Outcome outcome = leader.handle(newEntryRequest, state, log());
//state.update( outcome );
// then
AppendLogEntry logCommand = (AppendLogEntry) single(outcome.getLogCommands());
assertEquals(0, logCommand.index);
assertEquals(0, logCommand.entry.term());
ShipCommand.NewEntries shipCommand = (ShipCommand.NewEntries) single(outcome.getShipCommands());
assertEquals(shipCommand, new ShipCommand.NewEntries(-1, -1, new RaftLogEntry[] { new RaftLogEntry(0, CONTENT) }));
}
Aggregations