Search in sources :

Example 11 with RaftLogEntry

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

the class SegmentedRaftLogRotationTruncationPruneTest method shouldPruneAwaySingleEntriesIfRotationHappenedEveryEntry.

@Test
public void shouldPruneAwaySingleEntriesIfRotationHappenedEveryEntry() throws Exception {
    /**
         * If you have a raft log which rotates after every append, therefore having a single entry in every segment,
         * we assert that every sequential prune attempt will result in the prevIndex incrementing by one.
         */
    // given
    RaftLog log = createRaftLog();
    long term = 0;
    for (int i = 0; i < 10; i++) {
        log.append(new RaftLogEntry(term, valueOf(i)));
    }
    assertEquals(-1, log.prevIndex());
    for (int i = 0; i < 9; i++) {
        log.prune(i);
        assertEquals(i, log.prevIndex());
    }
    log.prune(9);
    assertEquals(8, log.prevIndex());
}
Also used : RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 12 with RaftLogEntry

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

the class SegmentedRaftLogRotationTruncationPruneTest method shouldPruneAwaySingleEntriesAfterTruncationIfRotationHappenedEveryEntry.

@Test
public void shouldPruneAwaySingleEntriesAfterTruncationIfRotationHappenedEveryEntry() throws Exception {
    /**
         * Given a log with many single-entry segments, a series of truncations at decending values followed by
         * pruning at more previous segments will maintain the correct prevIndex for the log.
         *
         * Initial Scenario:    [0][1][2][3][4][5][6][7][8][9]              prevIndex = 0
         * Truncate segment 9 : [0][1][2][3][4][5][6][7][8]   (9)           prevIndex = 0
         * Truncate segment 8 : [0][1][2][3][4][5][6][7]      (8)(9)        prevIndex = 0
         * Truncate segment 7 : [0][1][2][3][4][5][6]         (7)(8)(9)     prevIndex = 0
         * Prune segment 0    :    [1][2][3][4][5][6]         (7)(8)(9)     prevIndex = 1
         * Prune segment 1    :       [2][3][4][5][6]         (7)(8)(9)     prevIndex = 2
         * Prune segment 2    :          [3][4][5][6]         (7)(8)(9)     prevIndex = 3
         * Prune segment 3    :             [4][5][6]         (7)(8)(9)     prevIndex = 4
         * Prune segment 4    :                [5][6]         (7)(8)(9)     prevIndex = 5
         * Prune segment 5    :                [5][6]         (7)(8)(9)     prevIndex = 5
         * Prune segment 6    :                [5][6]         (7)(8)(9)     prevIndex = 5
         * Etc...
         *
         * The prevIndex should not become corrupt and become greater than 5 in this example.
         */
    // given
    RaftLog log = createRaftLog();
    long term = 0;
    for (int i = 0; i < 10; i++) {
        log.append(new RaftLogEntry(term, valueOf(i)));
    }
    log.truncate(9);
    log.truncate(8);
    log.truncate(7);
    assertEquals(-1, log.prevIndex());
    for (int i = 0; i <= 5; i++) {
        log.prune(i);
        assertEquals(i, log.prevIndex());
    }
    for (int i = 5; i < 10; i++) {
        log.prune(i);
        assertEquals(5, log.prevIndex());
    }
}
Also used : RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 13 with RaftLogEntry

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

the class MembershipWaiterTest method shouldWaitUntilLeaderCommitIsAvailable.

@Test
public void shouldWaitUntilLeaderCommitIsAvailable() 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();
    future.get(1, TimeUnit.SECONDS);
}
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 14 with RaftLogEntry

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

the class SegmentedRaftLogCursorIT method shouldReturnFalseOnCursorForEntryThatWasPruned.

@Test
public void shouldReturnFalseOnCursorForEntryThatWasPruned() throws Exception {
    //given
    SegmentedRaftLog segmentedRaftLog = createRaftLog(1, "keep_none");
    long firstIndex = segmentedRaftLog.append(new RaftLogEntry(1, ReplicatedInteger.valueOf(1)));
    segmentedRaftLog.append(new RaftLogEntry(2, ReplicatedInteger.valueOf(2)));
    long lastIndex = segmentedRaftLog.append(new RaftLogEntry(3, ReplicatedInteger.valueOf(3)));
    //when
    segmentedRaftLog.prune(firstIndex);
    RaftLogCursor entryCursor = segmentedRaftLog.getEntryCursor(firstIndex);
    boolean next = entryCursor.next();
    //then
    assertFalse(next);
}
Also used : RaftLogCursor(org.neo4j.causalclustering.core.consensus.log.RaftLogCursor) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 15 with RaftLogEntry

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

the class SegmentedRaftLogCursorIT method shouldReturnFalseOnCursorForEntryThatDoesntExist.

@Test
public void shouldReturnFalseOnCursorForEntryThatDoesntExist() throws Exception {
    //given
    SegmentedRaftLog segmentedRaftLog = createRaftLog(1);
    segmentedRaftLog.append(new RaftLogEntry(1, ReplicatedInteger.valueOf(1)));
    segmentedRaftLog.append(new RaftLogEntry(2, ReplicatedInteger.valueOf(2)));
    long lastIndex = segmentedRaftLog.append(new RaftLogEntry(3, ReplicatedInteger.valueOf(3)));
    //when
    boolean next;
    try (RaftLogCursor entryCursor = segmentedRaftLog.getEntryCursor(lastIndex + 1)) {
        next = entryCursor.next();
    }
    //then
    assertFalse(next);
}
Also used : RaftLogCursor(org.neo4j.causalclustering.core.consensus.log.RaftLogCursor) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)87 Test (org.junit.Test)69 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)27 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)25 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)20 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)12 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)10 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)10 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)8 NewLeaderBarrier (org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)7 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)7 MemberId (org.neo4j.causalclustering.identity.MemberId)7 InOrder (org.mockito.InOrder)6 RaftLogCursor (org.neo4j.causalclustering.core.consensus.log.RaftLogCursor)6 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)5 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)5 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)5 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)5 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)5 File (java.io.File)4