use of org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries in project neo4j by neo4j.
the class Appending method appendNewEntries.
static void appendNewEntries(ReadableRaftState ctx, Outcome outcome, List<ReplicatedContent> contents) throws IOException {
long prevLogIndex = ctx.entryLog().appendIndex();
long prevLogTerm = prevLogIndex == -1 ? -1 : prevLogIndex > ctx.lastLogIndexBeforeWeBecameLeader() ? ctx.term() : ctx.entryLog().readEntryTerm(prevLogIndex);
RaftLogEntry[] raftLogEntries = contents.stream().map(content -> new RaftLogEntry(ctx.term(), content)).toArray(RaftLogEntry[]::new);
outcome.addShipCommand(new ShipCommand.NewEntries(prevLogIndex, prevLogTerm, raftLogEntries));
outcome.addLogCommand(new BatchAppendLogEntries(prevLogIndex + 1, 0, raftLogEntries));
}
use of org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries 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.BatchAppendLogEntries in project neo4j by neo4j.
the class AppendEntriesRequestTest method shouldAcceptInitialEntriesAfterBootstrap.
@Test
public void shouldAcceptInitialEntriesAfterBootstrap() throws Exception {
RaftLog raftLog = bootstrappedLog();
RaftState state = raftState().entryLog(raftLog).myself(myself).build();
long leaderTerm = state.term() + leaderTermDifference;
RaftLogEntry logEntry1 = new RaftLogEntry(leaderTerm, content());
RaftLogEntry logEntry2 = new RaftLogEntry(leaderTerm, content());
// when
Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(0).prevLogTerm(0).logEntry(logEntry1).logEntry(logEntry2).build(), state, log());
// then
assertTrue(((Response) messageFor(outcome, leader)).success());
assertThat(outcome.getLogCommands(), hasItem(new BatchAppendLogEntries(1, 0, new RaftLogEntry[] { logEntry1, logEntry2 })));
}
use of org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries in project neo4j by neo4j.
the class AppendEntriesRequestTest method shouldAcceptInitialEntryAfterBootstrap.
@Test
public void shouldAcceptInitialEntryAfterBootstrap() throws Exception {
RaftLog raftLog = bootstrappedLog();
RaftState state = raftState().entryLog(raftLog).myself(myself).build();
long leaderTerm = state.term() + leaderTermDifference;
RaftLogEntry logEntry = new RaftLogEntry(leaderTerm, content());
// when
Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(0).prevLogTerm(0).logEntry(logEntry).build(), state, log());
// then
assertTrue(((Response) messageFor(outcome, leader)).success());
assertThat(outcome.getLogCommands(), hasItem(new BatchAppendLogEntries(1, 0, new RaftLogEntry[] { logEntry })));
}
use of org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries in project neo4j by neo4j.
the class AppendEntriesRequestTest method shouldAppendNewEntryAndCommitPreviouslyAppendedEntry.
@Test
public void shouldAppendNewEntryAndCommitPreviouslyAppendedEntry() throws Exception {
// given
InMemoryRaftLog raftLog = new InMemoryRaftLog();
RaftState state = raftState().entryLog(raftLog).myself(myself).build();
long leaderTerm = state.term() + leaderTermDifference;
RaftLogEntry previouslyAppendedEntry = new RaftLogEntry(leaderTerm, content());
raftLog.append(previouslyAppendedEntry);
RaftLogEntry newLogEntry = new RaftLogEntry(leaderTerm, content());
// when
Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(raftLog.appendIndex()).prevLogTerm(leaderTerm).logEntry(newLogEntry).leaderCommit(0).build(), state, log());
// then
assertTrue(((Response) messageFor(outcome, leader)).success());
assertThat(outcome.getCommitIndex(), Matchers.equalTo(0L));
assertThat(outcome.getLogCommands(), hasItem(new BatchAppendLogEntries(1, 0, new RaftLogEntry[] { newLogEntry })));
}
Aggregations