use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.
the class AppendEntriesRequestTest method shouldNotCommitAheadOfMatchingHistory.
@Test
public void shouldNotCommitAheadOfMatchingHistory() 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);
// when
Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(raftLog.appendIndex() + 1).prevLogTerm(leaderTerm).leaderCommit(0).build(), state, log());
// then
assertFalse(((Response) messageFor(outcome, leader)).success());
assertThat(outcome.getLogCommands(), empty());
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry 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 })));
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.
the class AppendEntriesRequestTest method shouldCommitEntry.
@Test
public void shouldCommitEntry() throws Exception {
// given
InMemoryRaftLog raftLog = new InMemoryRaftLog();
RaftState state = raftState().entryLog(raftLog).myself(myself).build();
long leaderTerm = state.term() + leaderTermDifference;
raftLog.append(new RaftLogEntry(leaderTerm, content()));
// when
Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(raftLog.appendIndex()).prevLogTerm(leaderTerm).leaderCommit(0).build(), state, log());
// then
assertTrue(((Response) messageFor(outcome, leader)).success());
assertThat(outcome.getCommitIndex(), Matchers.equalTo(0L));
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.
the class AppendingTest method shouldPerformTruncation.
@Test
public void shouldPerformTruncation() throws Exception {
// when
// we have a log appended up to appendIndex, and committed somewhere before that
long appendIndex = 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(appendIndex);
ReadableRaftState state = mock(ReadableRaftState.class);
when(state.entryLog()).thenReturn(logMock);
when(state.commitIndex()).thenReturn(appendIndex - 3);
// when
// the leader asks to append after the commit index an entry that mismatches on term
Appending.handleAppendEntriesRequest(state, outcome, new RaftMessages.AppendEntries.Request(aMember, localTermForAllEntries, appendIndex - 2, localTermForAllEntries, new RaftLogEntry[] { new RaftLogEntry(localTermForAllEntries + 1, ReplicatedInteger.valueOf(2)) }, appendIndex + 3), NullLog.getInstance());
// then
// we must produce a TruncateLogCommand at the earliest mismatching index
verify(outcome, times(1)).addLogCommand(argThat(new LogCommandMatcher(appendIndex - 1)));
}
use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.
the class AppendingTest method shouldNotAllowTruncationBeforeCommit.
@Test
public void shouldNotAllowTruncationBeforeCommit() 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 - 2, 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) {
// fine
}
}
Aggregations