Search in sources :

Example 51 with RaftLogEntry

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

the class ClusterSafetyViolationsTest method shouldRecogniseInconsistentCommittedContent.

@Test
public void shouldRecogniseInconsistentCommittedContent() throws Exception {
    // given
    ClusterState clusterState = new ClusterState(asSet(member(0), member(1)));
    clusterState.states.get(member(0)).entryLog.append(new RaftLogEntry(1, valueOf(1)));
    clusterState.states.get(member(1)).entryLog.append(new RaftLogEntry(1, valueOf(1)));
    clusterState.states.get(member(0)).entryLog.append(new RaftLogEntry(1, valueOf(2)));
    clusterState.states.get(member(1)).entryLog.append(new RaftLogEntry(1, valueOf(3)));
    commit(clusterState, member(0), 0);
    commit(clusterState, member(1), 0);
    // then
    assertFalse(inconsistentCommittedLogEntries(clusterState));
    // when
    commit(clusterState, member(0), 1);
    commit(clusterState, member(1), 1);
    // then
    assertTrue(inconsistentCommittedLogEntries(clusterState));
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 52 with RaftLogEntry

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

the class SegmentedRaftLogPartialEntryRecoveryTest method incompleteHeaderOfLastOfMoreThanOneLogFilesShouldNotCauseFailure.

@Test
public void incompleteHeaderOfLastOfMoreThanOneLogFilesShouldNotCauseFailure() throws Throwable {
    // Given
    // we use a RaftLog to create two log files, in order to chop the header of the second
    SegmentedRaftLog raftLog = createRaftLog(1);
    raftLog.start();
    // will cause rotation
    raftLog.append(new RaftLogEntry(4, new NewLeaderBarrier()));
    raftLog.stop();
    // We use a temporary RecoveryProtocol to get the file to chop
    RecoveryProtocol recovery = createRecoveryProtocol();
    State recoveryState = recovery.run();
    String logFilename = recoveryState.segments.last().getFilename();
    File logFile = new File(logDirectory, logFilename);
    // When
    // We remove any number of bytes from the end of the second file and try to recover
    // Then
    // No exceptions should be thrown
    truncateAndRecover(logFile, 0);
}
Also used : NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) File(java.io.File) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 53 with RaftLogEntry

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

the class SegmentedRaftLogRotationTest method shouldBeAbleToRecoverToLatestStateAfterRotation.

@Test
public void shouldBeAbleToRecoverToLatestStateAfterRotation() throws Throwable {
    // Given
    int term = 0;
    long indexToRestoreTo;
    try (Lifespan lifespan = new Lifespan()) {
        SegmentedRaftLog log = lifespan.add(createRaftLog(ROTATE_AT_SIZE_IN_BYTES));
        log.append(new RaftLogEntry(term, replicatedStringOfBytes(ROTATE_AT_SIZE_IN_BYTES - 40)));
        indexToRestoreTo = log.append(new RaftLogEntry(term, ReplicatedInteger.valueOf(1)));
    }
    // When
    SegmentedRaftLog log = life.add(createRaftLog(ROTATE_AT_SIZE_IN_BYTES));
    // Then
    assertEquals(indexToRestoreTo, log.appendIndex());
    assertEquals(term, log.readEntryTerm(indexToRestoreTo));
}
Also used : Lifespan(org.neo4j.kernel.lifecycle.Lifespan) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 54 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry 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 55 with RaftLogEntry

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

the class InFlightLogEntryReaderTest method shouldNeverUseMapAgainAfterHavingFeltBackToTheRaftLog.

@Test
public void shouldNeverUseMapAgainAfterHavingFeltBackToTheRaftLog() throws Exception {
    // given
    InFlightLogEntryReader reader = new InFlightLogEntryReader(raftLog, inFlightMap, clearCache);
    startingFromIndexReturnEntries(inFlightMap, logIndex, entry, null, mock(RaftLogEntry.class));
    RaftLogEntry[] entries = { entry, mock(RaftLogEntry.class), mock(RaftLogEntry.class) };
    startingFromIndexReturnEntries(raftLog, logIndex + 1, entries[1], entries[2]);
    for (int offset = 0; offset < 3; offset++) {
        // when
        RaftLogEntry raftLogEntry = reader.get(offset + logIndex);
        // then
        assertEquals(entries[offset], raftLogEntry);
        if (offset <= 1) {
            verify(inFlightMap).get(offset + logIndex);
        }
        if (offset == 1) {
            verify(raftLog).getEntryCursor(offset + logIndex);
        }
        assertCacheIsUpdated(inFlightMap, offset + logIndex);
    }
    verifyNoMoreInteractions(inFlightMap);
    verifyNoMoreInteractions(raftLog);
}
Also used : 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