Search in sources :

Example 81 with RaftLogEntry

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

the class CommandApplicationProcessTest method shouldApplyCommittedCommand.

@Test
public void shouldApplyCommittedCommand() throws Throwable {
    // given
    RaftLogCommitIndexMonitor listener = mock(RaftLogCommitIndexMonitor.class);
    monitors.addMonitorListener(listener);
    applicationProcess.start();
    InOrder inOrder = inOrder(coreStateMachines, commandDispatcher);
    // when
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    applicationProcess.notifyCommitted(2);
    applier.sync(false);
    // then
    inOrder.verify(coreStateMachines).commandDispatcher();
    inOrder.verify(commandDispatcher).dispatch(eq(nullTx), eq(0L), anyCallback());
    inOrder.verify(commandDispatcher).dispatch(eq(nullTx), eq(1L), anyCallback());
    inOrder.verify(commandDispatcher).dispatch(eq(nullTx), eq(2L), anyCallback());
    inOrder.verify(commandDispatcher).close();
    verify(listener).commitIndex(2);
}
Also used : RaftLogCommitIndexMonitor(org.neo4j.causalclustering.core.consensus.log.monitoring.RaftLogCommitIndexMonitor) InOrder(org.mockito.InOrder) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 82 with RaftLogEntry

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

the class CommandApplicationProcessTest method shouldNotApplyUncommittedCommands.

@Test
public void shouldNotApplyUncommittedCommands() throws Throwable {
    // given
    applicationProcess.start();
    // when
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    applicationProcess.notifyCommitted(-1);
    applier.sync(false);
    // then
    verifyZeroInteractions(commandDispatcher);
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 83 with RaftLogEntry

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

the class CommandApplicationProcessTest method duplicatesShouldBeIgnoredButStillIncreaseCommandIndex.

@Test
public void duplicatesShouldBeIgnoredButStillIncreaseCommandIndex() throws Exception {
    // given
    applicationProcess.start();
    // when
    raftLog.append(new RaftLogEntry(0, new NewLeaderBarrier()));
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(nullTx, globalSession, new LocalOperationId(0, 0))));
    // duplicate
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(nullTx, globalSession, new LocalOperationId(0, 0))));
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(nullTx, globalSession, new LocalOperationId(0, 1))));
    applicationProcess.notifyCommitted(3);
    applier.sync(false);
    InOrder inOrder = inOrder(coreStateMachines, commandDispatcher);
    // then
    inOrder.verify(coreStateMachines).commandDispatcher();
    inOrder.verify(commandDispatcher).dispatch(eq(nullTx), eq(1L), anyCallback());
    // duplicate not dispatched
    inOrder.verify(commandDispatcher).dispatch(eq(nullTx), eq(3L), anyCallback());
    inOrder.verify(commandDispatcher).close();
    verifyNoMoreInteractions(commandDispatcher);
}
Also used : InOrder(org.mockito.InOrder) LocalOperationId(org.neo4j.causalclustering.core.replication.session.LocalOperationId) DistributedOperation(org.neo4j.causalclustering.core.replication.DistributedOperation) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 84 with RaftLogEntry

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

the class CommandApplicationProcessTest method shouldFallbackToLogCursorOnCacheMiss.

@Test
public void shouldFallbackToLogCursorOnCacheMiss() throws Throwable {
    // if the cache does not contain all things to be applied, make sure we fall back to the log
    // should only happen in recovery, otherwise this is probably a bug.
    applicationProcess.start();
    //given cache with missing entry
    ReplicatedContent operation0 = operation(nullTx);
    ReplicatedContent operation1 = operation(nullTx);
    ReplicatedContent operation2 = operation(nullTx);
    inFlightMap.put(0L, new RaftLogEntry(0, operation0));
    inFlightMap.put(2L, new RaftLogEntry(2, operation2));
    raftLog.append(new RaftLogEntry(0, operation0));
    raftLog.append(new RaftLogEntry(1, operation1));
    raftLog.append(new RaftLogEntry(2, operation2));
    //when
    applicationProcess.notifyCommitted(2);
    applier.sync(false);
    //then the cache stops being used after it finds 1 is missing
    verify(inFlightMap, times(1)).get(0L);
    verify(inFlightMap, times(1)).get(1L);
    verify(inFlightMap, times(0)).get(2L);
    // we only look up 1 so let's remove that from the cache
    verify(inFlightMap, times(1)).remove(0L);
    verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(0L), anyCallback());
    verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(1L), anyCallback());
    verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(2L), anyCallback());
    verify(raftLog, times(1)).getEntryCursor(1);
}
Also used : CoreReplicatedContent(org.neo4j.causalclustering.core.state.machines.tx.CoreReplicatedContent) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 85 with RaftLogEntry

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

the class CommandApplicationProcess method submitApplyJob.

private void submitApplyJob(long lastToApply) {
    boolean success = applier.submit((status) -> () -> {
        final long snapshotLastSeenCommitIndex = this.lastSeenCommitIndex;
        try (InFlightLogEntryReader logEntrySupplier = new InFlightLogEntryReader(raftLog, inFlightMap, true)) {
            for (long logIndex = lastApplied + 1; !status.isCancelled() && logIndex <= snapshotLastSeenCommitIndex; logIndex++) {
                RaftLogEntry entry = logEntrySupplier.get(logIndex);
                if (entry == null) {
                    throw new IllegalStateException(format("Committed log %d entry must exist.", logIndex));
                }
                if (entry.content() instanceof DistributedOperation) {
                    DistributedOperation distributedOperation = (DistributedOperation) entry.content();
                    progressTracker.trackReplication(distributedOperation);
                    batcher.add(logIndex, distributedOperation);
                } else {
                    batcher.flush();
                    // since this last entry didn't get in the batcher we need to update the lastApplied:
                    lastApplied = logIndex;
                }
            }
            batcher.flush();
        } catch (Throwable e) {
            log.error("Failed to apply up to index " + lastToApply, e);
            dbHealth.get().panic(e);
            applier.panic();
        }
    });
    if (!success) {
        log.error("Applier has entered a state of panic, no more jobs can be submitted.");
        try {
            // Let's sleep a while so that the log does not get flooded in this state.
            // TODO: Consider triggering a shutdown of the database on panic.
            Thread.sleep(1000);
        } catch (InterruptedException ignored) {
        }
    }
}
Also used : DistributedOperation(org.neo4j.causalclustering.core.replication.DistributedOperation) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

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