use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class AppendEntriesRequestTest method shouldTruncateOnReceiptOfConflictingEntry.
@Test
public void shouldTruncateOnReceiptOfConflictingEntry() throws Exception {
// given
InMemoryRaftLog raftLog = new InMemoryRaftLog();
RaftState state = raftState().myself(myself).term(5).entryLog(raftLog).build();
long leaderTerm = state.term() + leaderTermDifference;
raftLog.append(new RaftLogEntry(state.term() - 1, content()));
raftLog.append(new RaftLogEntry(state.term() - 1, content()));
// when
long previousIndex = raftLog.appendIndex() - 1;
Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(previousIndex).prevLogTerm(raftLog.readEntryTerm(previousIndex)).logEntry(new RaftLogEntry(leaderTerm, content())).build(), state, log());
// then
assertTrue(((Response) messageFor(outcome, leader)).success());
assertThat(outcome.getLogCommands(), hasItem(new TruncateLogCommand(1)));
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome 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.Outcome in project neo4j by neo4j.
the class AppendingTest method shouldNotAllowTruncationAtCommit.
@Test
public void shouldNotAllowTruncationAtCommit() throws Exception {
// given
long commitIndex = 5;
long localTermForAllEntries = 1L;
Outcome outcome = mock(Outcome.class);
ReadableRaftLog logMock = mock(ReadableRaftLog.class);
// for simplicity, all entries are at term 1
when(logMock.readEntryTerm(anyLong())).thenReturn(localTermForAllEntries);
when(logMock.appendIndex()).thenReturn(commitIndex);
ReadableRaftState state = mock(ReadableRaftState.class);
when(state.entryLog()).thenReturn(logMock);
when(state.commitIndex()).thenReturn(commitIndex);
// when - then
try {
Appending.handleAppendEntriesRequest(state, outcome, new RaftMessages.AppendEntries.Request(aMember, localTermForAllEntries, commitIndex - 1, localTermForAllEntries, new RaftLogEntry[] { new RaftLogEntry(localTermForAllEntries + 1, ReplicatedInteger.valueOf(2)) }, commitIndex + 3), NullLog.getInstance());
fail("Appending should not allow truncation at or before the commit index");
} catch (IllegalStateException expected) {
// ok
}
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.
the class RaftStateBuilder method build.
public RaftState build() throws IOException {
StateStorage<TermState> termStore = new InMemoryStateStorage<>(new TermState());
StateStorage<VoteState> voteStore = new InMemoryStateStorage<>(new VoteState());
StubMembership membership = new StubMembership(votingMembers, replicationMembers);
RaftState state = new RaftState(myself, termStore, membership, entryLog, voteStore, new InFlightMap<>(), NullLogProvider.getInstance());
Collection<RaftMessages.Directed> noMessages = Collections.emptyList();
List<RaftLogCommand> noLogCommands = Collections.emptyList();
state.update(new Outcome(null, term, leader, leaderCommit, votedFor, votesForMe, lastLogIndexBeforeWeBecameLeader, followerStates, false, noLogCommands, noMessages, emptySet(), commitIndex, emptySet()));
return state;
}
use of org.neo4j.causalclustering.core.consensus.outcome.Outcome 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));
}
Aggregations