Search in sources :

Example 26 with StorageEngine

use of org.neo4j.storageengine.api.StorageEngine in project neo4j by neo4j.

the class TransactionLogsRecoveryTest method shouldRecoverExistingData.

@Test
void shouldRecoverExistingData() throws Exception {
    LogFile logFile = logFiles.getLogFile();
    Path file = logFile.getLogFileForVersion(logVersion);
    writeSomeData(file, pair -> {
        LogEntryWriter writer = pair.first();
        Consumer<LogPositionMarker> consumer = pair.other();
        LogPositionMarker marker = new LogPositionMarker();
        // last committed tx
        int previousChecksum = BASE_TX_CHECKSUM;
        consumer.accept(marker);
        LogPosition lastCommittedTxPosition = marker.newPosition();
        writer.writeStartEntry(2L, 3L, previousChecksum, new byte[0]);
        lastCommittedTxStartEntry = new LogEntryStart(2L, 3L, previousChecksum, new byte[0], lastCommittedTxPosition);
        previousChecksum = writer.writeCommitEntry(4L, 5L);
        lastCommittedTxCommitEntry = new LogEntryCommit(4L, 5L, previousChecksum);
        // check point pointing to the previously committed transaction
        var checkpointFile = logFiles.getCheckpointFile();
        var checkpointAppender = checkpointFile.getCheckpointAppender();
        checkpointAppender.checkPoint(LogCheckPointEvent.NULL, lastCommittedTxPosition, Instant.now(), "test");
        // tx committed after checkpoint
        consumer.accept(marker);
        writer.writeStartEntry(6L, 4L, previousChecksum, new byte[0]);
        expectedStartEntry = new LogEntryStart(6L, 4L, previousChecksum, new byte[0], marker.newPosition());
        previousChecksum = writer.writeCommitEntry(5L, 7L);
        expectedCommitEntry = new LogEntryCommit(5L, 7L, previousChecksum);
        return true;
    });
    LifeSupport life = new LifeSupport();
    var recoveryRequired = new AtomicBoolean();
    var recoveredTransactions = new MutableInt();
    RecoveryMonitor monitor = new RecoveryMonitor() {

        @Override
        public void recoveryRequired(LogPosition recoveryPosition) {
            recoveryRequired.set(true);
        }

        @Override
        public void recoveryCompleted(int numberOfRecoveredTransactions, long recoveryTimeInMilliseconds) {
            recoveredTransactions.setValue(numberOfRecoveredTransactions);
        }
    };
    try {
        StorageEngine storageEngine = mock(StorageEngine.class);
        final LogEntryReader reader = logEntryReader();
        TransactionMetadataCache metadataCache = new TransactionMetadataCache();
        LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFiles, metadataCache, reader, monitors, false);
        CorruptedLogsTruncator logPruner = new CorruptedLogsTruncator(storeDir, logFiles, fileSystem, INSTANCE);
        monitors.addMonitorListener(monitor);
        life.add(new TransactionLogsRecovery(new DefaultRecoveryService(storageEngine, transactionIdStore, txStore, versionRepository, logFiles, NO_MONITOR, mock(Log.class), false) {

            private int nr;

            @Override
            public RecoveryApplier getRecoveryApplier(TransactionApplicationMode mode, PageCacheTracer cacheTracer, String tracerTag) {
                RecoveryApplier actual = super.getRecoveryApplier(mode, cacheTracer, tracerTag);
                if (mode == TransactionApplicationMode.REVERSE_RECOVERY) {
                    return actual;
                }
                return new RecoveryApplier() {

                    @Override
                    public void close() throws Exception {
                        actual.close();
                    }

                    @Override
                    public boolean visit(CommittedTransactionRepresentation tx) throws Exception {
                        actual.visit(tx);
                        switch(nr++) {
                            case 0:
                                assertEquals(lastCommittedTxStartEntry, tx.getStartEntry());
                                assertEquals(lastCommittedTxCommitEntry, tx.getCommitEntry());
                                break;
                            case 1:
                                assertEquals(expectedStartEntry, tx.getStartEntry());
                                assertEquals(expectedCommitEntry, tx.getCommitEntry());
                                break;
                            default:
                                fail("Too many recovered transactions");
                        }
                        return false;
                    }
                };
            }
        }, logPruner, schemaLife, monitor, ProgressReporter.SILENT, false, EMPTY_CHECKER, NULL));
        life.start();
        assertTrue(recoveryRequired.get());
        assertEquals(2, recoveredTransactions.getValue());
    } finally {
        life.shutdown();
    }
}
Also used : LogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) StorageEngine(org.neo4j.storageengine.api.StorageEngine) LogPositionMarker(org.neo4j.kernel.impl.transaction.log.LogPositionMarker) LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) TransactionApplicationMode(org.neo4j.storageengine.api.TransactionApplicationMode) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) Path(java.nio.file.Path) LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) Log(org.neo4j.logging.Log) PageCacheTracer(org.neo4j.io.pagecache.tracing.PageCacheTracer) TransactionMetadataCache(org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with StorageEngine

use of org.neo4j.storageengine.api.StorageEngine in project neo4j by neo4j.

the class TransactionLogsRecoveryTest method shouldSeeThatACleanDatabaseShouldNotRequireRecovery.

@ParameterizedTest(name = "{0}")
@CsvSource({ "separateCheckpoints,true", "legacyCheckpoints,false" })
void shouldSeeThatACleanDatabaseShouldNotRequireRecovery(String name, boolean useSeparateLogFiles) throws Exception {
    Path file = logFiles.getLogFile().getLogFileForVersion(logVersion);
    LogPositionMarker marker = new LogPositionMarker();
    writeSomeDataWithVersion(file, pair -> {
        LogEntryWriter writer = pair.first();
        Consumer<LogPositionMarker> consumer = pair.other();
        // last committed tx
        consumer.accept(marker);
        writer.writeStartEntry(2L, 3L, BASE_TX_CHECKSUM, new byte[0]);
        writer.writeCommitEntry(4L, 5L);
        // check point
        consumer.accept(marker);
        if (useSeparateLogFiles) {
            var checkpointFile = logFiles.getCheckpointFile();
            var checkpointAppender = checkpointFile.getCheckpointAppender();
            checkpointAppender.checkPoint(LogCheckPointEvent.NULL, marker.newPosition(), Instant.now(), "test");
        } else {
            writer.writeLegacyCheckPointEntry(marker.newPosition());
        }
        return true;
    }, useSeparateLogFiles ? KernelVersion.LATEST : KernelVersion.V4_0);
    LifeSupport life = new LifeSupport();
    RecoveryMonitor monitor = mock(RecoveryMonitor.class);
    try {
        StorageEngine storageEngine = mock(StorageEngine.class);
        final LogEntryReader reader = logEntryReader();
        TransactionMetadataCache metadataCache = new TransactionMetadataCache();
        LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFiles, metadataCache, reader, monitors, false);
        CorruptedLogsTruncator logPruner = new CorruptedLogsTruncator(storeDir, logFiles, fileSystem, INSTANCE);
        monitors.addMonitorListener(new RecoveryMonitor() {

            @Override
            public void recoveryRequired(LogPosition recoveryPosition) {
                fail("Recovery should not be required");
            }
        });
        life.add(new TransactionLogsRecovery(new DefaultRecoveryService(storageEngine, transactionIdStore, txStore, versionRepository, logFiles, NO_MONITOR, mock(Log.class), false), logPruner, schemaLife, monitor, ProgressReporter.SILENT, false, EMPTY_CHECKER, NULL));
        life.start();
        verifyNoInteractions(monitor);
    } finally {
        life.shutdown();
    }
}
Also used : Path(java.nio.file.Path) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) Log(org.neo4j.logging.Log) LogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader) TransactionMetadataCache(org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) StorageEngine(org.neo4j.storageengine.api.StorageEngine) LogPositionMarker(org.neo4j.kernel.impl.transaction.log.LogPositionMarker) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with StorageEngine

use of org.neo4j.storageengine.api.StorageEngine in project neo4j by neo4j.

the class TransactionLogsRecoveryTest method recover.

private boolean recover(Path storeDir, LogFiles logFiles, RecoveryStartupChecker startupChecker) {
    LifeSupport life = new LifeSupport();
    final AtomicBoolean recoveryRequired = new AtomicBoolean();
    RecoveryMonitor monitor = new RecoveryMonitor() {

        @Override
        public void recoveryRequired(LogPosition recoveryPosition) {
            recoveryRequired.set(true);
        }
    };
    try {
        StorageEngine storageEngine = mock(StorageEngine.class);
        final LogEntryReader reader = logEntryReader();
        TransactionMetadataCache metadataCache = new TransactionMetadataCache();
        LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFiles, metadataCache, reader, monitors, false);
        CorruptedLogsTruncator logPruner = new CorruptedLogsTruncator(storeDir, logFiles, fileSystem, INSTANCE);
        monitors.addMonitorListener(monitor);
        life.add(new TransactionLogsRecovery(new DefaultRecoveryService(storageEngine, transactionIdStore, txStore, versionRepository, logFiles, NO_MONITOR, mock(Log.class), false), logPruner, schemaLife, monitor, ProgressReporter.SILENT, false, startupChecker, NULL));
        life.start();
    } finally {
        life.shutdown();
    }
    return recoveryRequired.get();
}
Also used : PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) Log(org.neo4j.logging.Log) LogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader) TransactionMetadataCache(org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) StorageEngine(org.neo4j.storageengine.api.StorageEngine) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Example 29 with StorageEngine

use of org.neo4j.storageengine.api.StorageEngine in project neo4j by neo4j.

the class InternalTransactionCommitProcessTest method shouldSuccessfullyCommitTransactionWithNoCommands.

@Test
void shouldSuccessfullyCommitTransactionWithNoCommands() throws Exception {
    // GIVEN
    long txId = 11;
    TransactionIdStore transactionIdStore = mock(TransactionIdStore.class);
    TransactionAppender appender = new TestableTransactionAppender(transactionIdStore);
    when(transactionIdStore.nextCommittingTransactionId()).thenReturn(txId);
    StorageEngine storageEngine = mock(StorageEngine.class);
    TransactionCommitProcess commitProcess = new InternalTransactionCommitProcess(appender, storageEngine);
    PhysicalTransactionRepresentation noCommandTx = new PhysicalTransactionRepresentation(Collections.emptyList());
    noCommandTx.setHeader(new byte[0], -1, -1, -1, -1, ANONYMOUS);
    // WHEN
    commitProcess.commit(new TransactionToApply(noCommandTx, NULL), commitEvent, INTERNAL);
    verify(transactionIdStore).transactionCommitted(txId, FakeCommitment.CHECKSUM, FakeCommitment.TIMESTAMP, NULL);
}
Also used : TransactionIdStore(org.neo4j.storageengine.api.TransactionIdStore) TestableTransactionAppender(org.neo4j.kernel.impl.transaction.log.TestableTransactionAppender) TestableTransactionAppender(org.neo4j.kernel.impl.transaction.log.TestableTransactionAppender) TransactionAppender(org.neo4j.kernel.impl.transaction.log.TransactionAppender) StorageEngine(org.neo4j.storageengine.api.StorageEngine) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) Test(org.junit.jupiter.api.Test)

Example 30 with StorageEngine

use of org.neo4j.storageengine.api.StorageEngine in project neo4j by neo4j.

the class DbmsDiagnosticsManagerTest method setUp.

@BeforeEach
@SuppressWarnings("unchecked")
void setUp() throws IOException {
    logProvider = new AssertableLogProvider();
    databaseManager = mock(DatabaseManager.class);
    storageEngine = mock(StorageEngine.class);
    storageEngineFactory = mock(StorageEngineFactory.class);
    defaultContext = mock(StandaloneDatabaseContext.class);
    defaultDatabase = prepareDatabase();
    when(storageEngineFactory.listStorageFiles(any(), any())).thenReturn(Collections.emptyList());
    dependencies = new Dependencies();
    dependencies.satisfyDependency(Config.defaults());
    dependencies.satisfyDependency(databaseManager);
    when(defaultContext.database()).thenReturn(defaultDatabase);
    when(databaseManager.getDatabaseContext(DEFAULT_DATABASE_ID)).thenReturn(Optional.of(defaultContext));
    when(databaseManager.registeredDatabases()).thenReturn(new TreeMap<>(singletonMap(DEFAULT_DATABASE_ID, defaultContext)));
    diagnosticsManager = new DbmsDiagnosticsManager(dependencies, new SimpleLogService(logProvider));
}
Also used : DatabaseManager(org.neo4j.dbms.database.DatabaseManager) SimpleLogService(org.neo4j.logging.internal.SimpleLogService) StorageEngineFactory(org.neo4j.storageengine.api.StorageEngineFactory) Dependencies(org.neo4j.collection.Dependencies) StorageEngine(org.neo4j.storageengine.api.StorageEngine) StandaloneDatabaseContext(org.neo4j.dbms.database.StandaloneDatabaseContext) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

StorageEngine (org.neo4j.storageengine.api.StorageEngine)30 IOException (java.io.IOException)9 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)9 PhysicalLogicalTransactionStore (org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore)8 TransactionMetadataCache (org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache)8 Test (org.junit.Test)7 IndexingService (org.neo4j.kernel.impl.api.index.IndexingService)7 LogicalTransactionStore (org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore)7 TransactionAppender (org.neo4j.kernel.impl.transaction.log.TransactionAppender)7 Test (org.junit.jupiter.api.Test)6 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)6 TestableTransactionAppender (org.neo4j.kernel.impl.transaction.log.TestableTransactionAppender)6 VersionAwareLogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader)6 Dependencies (org.neo4j.collection.Dependencies)5 ReadableClosablePositionAwareChannel (org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 CommittedTransactionRepresentation (org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation)4 PhysicalLogFile (org.neo4j.kernel.impl.transaction.log.PhysicalLogFile)4 Log (org.neo4j.logging.Log)4 File (java.io.File)3