use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry 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.log.RaftLogEntry 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.log.RaftLogEntry in project neo4j by neo4j.
the class CandidateTest method shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm.
@Test
public void shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm() throws Exception {
// given
RaftState state = RaftStateBuilder.raftState().term(1).myself(myself).votingMembers(member1, member2).replicationMembers(member1, member2).build();
// when
Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(state.term()).from(member1).grant().build(), state, log());
// then
assertEquals(LEADER, outcome.getRole());
assertEquals(true, outcome.electionTimeoutRenewed());
assertThat(outcome.getLogCommands(), hasItem(new AppendLogEntry(0, new RaftLogEntry(state.term(), new NewLeaderBarrier()))));
assertThat(outcome.getOutgoingMessages(), hasItems(new RaftMessages.Directed(member1, new RaftMessages.Heartbeat(myself, state.term(), -1, -1)), new RaftMessages.Directed(member2, new RaftMessages.Heartbeat(myself, state.term(), -1, -1))));
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.
the class FollowerTest method appendSomeEntriesToLog.
private void appendSomeEntriesToLog(RaftState raft, Follower follower, int numberOfEntriesToAppend, int term, int firstIndex) throws IOException {
for (int i = 0; i < numberOfEntriesToAppend; i++) {
int prevLogIndex = (firstIndex + i) - 1;
raft.update(follower.handle(new AppendEntries.Request(myself, term, prevLogIndex, term, new RaftLogEntry[] { new RaftLogEntry(term, ContentGenerator.content()) }, -1), raft, log()));
}
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry 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());
}
Aggregations