use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.
the class AppendEntriesRequestTest method shouldAcceptContinuousEntries.
@Test
public void shouldAcceptContinuousEntries() 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()));
// when
Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(raftLog.appendIndex()).prevLogTerm(leaderTerm).logEntry(new RaftLogEntry(leaderTerm, content())).build(), state, log());
// then
assertTrue(((Response) messageFor(outcome, leader)).success());
}
use of org.neo4j.causalclustering.core.consensus.state.RaftState 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.state.RaftState 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.state.RaftState 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.state.RaftState 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