Search in sources :

Example 16 with InMemoryRaftLog

use of org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog in project neo4j by neo4j.

the class BatchAppendLogEntriesTest method shouldApplyFromOffsetOnly.

@Test
public void shouldApplyFromOffsetOnly() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    BatchAppendLogEntries batchAppendLogEntries = new BatchAppendLogEntries(0, 2, new RaftLogEntry[] { entryA, entryB, entryC, entryD });
    // when
    batchAppendLogEntries.applyTo(raftLog, log);
    // then
    assertEquals(entryC, readLogEntry(raftLog, 0));
    assertEquals(entryD, readLogEntry(raftLog, 1));
    assertEquals(1, raftLog.appendIndex());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Test(org.junit.Test)

Example 17 with InMemoryRaftLog

use of org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog in project neo4j by neo4j.

the class MembershipWaiterTest method shouldReturnImmediatelyIfMemberAndCaughtUp.

@Test
public void shouldReturnImmediatelyIfMemberAndCaughtUp() throws Exception {
    OnDemandJobScheduler jobScheduler = new OnDemandJobScheduler();
    MembershipWaiter waiter = new MembershipWaiter(member(0), jobScheduler, () -> dbHealth, 500, NullLogProvider.getInstance());
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, valueOf(0)));
    ExposedRaftState raftState = RaftStateBuilder.raftState().votingMembers(member(0)).leaderCommit(0).entryLog(raftLog).commitIndex(0L).build().copy();
    RaftMachine raft = mock(RaftMachine.class);
    when(raft.state()).thenReturn(raftState);
    CompletableFuture<Boolean> future = waiter.waitUntilCaughtUpMember(raft);
    jobScheduler.runJob();
    jobScheduler.runJob();
    future.get(0, NANOSECONDS);
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) ExposedRaftState(org.neo4j.causalclustering.core.consensus.state.ExposedRaftState) OnDemandJobScheduler(org.neo4j.test.OnDemandJobScheduler) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 18 with InMemoryRaftLog

use of org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog in project neo4j by neo4j.

the class AppendEntriesMessageFlowTest method setup.

@Before
public void setup() throws IOException {
    // given
    RaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).raftLog(raftLog).outbound(outbound).build();
}
Also used : RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Before(org.junit.Before)

Example 19 with InMemoryRaftLog

use of org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog 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());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 20 with InMemoryRaftLog

use of org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog 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 })));
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)30 Test (org.junit.Test)27 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)23 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)18 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)15 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)7 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)6 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)5 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)4 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)4 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)4 ReplicatedString (org.neo4j.causalclustering.core.consensus.ReplicatedString)3 RaftLogCommand (org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand)3 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)3 Before (org.junit.Before)2 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)2 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)2 ExposedRaftState (org.neo4j.causalclustering.core.consensus.state.ExposedRaftState)2 TermState (org.neo4j.causalclustering.core.consensus.term.TermState)2 VoteState (org.neo4j.causalclustering.core.consensus.vote.VoteState)2