Search in sources :

Example 21 with LogFile

use of org.neo4j.kernel.impl.transaction.log.files.LogFile in project neo4j by neo4j.

the class CorruptedLogsTruncatorTest method generateTransactionLogFiles.

private static void generateTransactionLogFiles(LogFiles logFiles) throws IOException {
    LogFile logFile = logFiles.getLogFile();
    FlushablePositionAwareChecksumChannel writer = logFile.getTransactionLogWriter().getChannel();
    for (byte i = 0; i < 107; i++) {
        writer.put(i);
        writer.prepareForFlush();
        if (logFile.rotationNeeded()) {
            logFile.rotate();
        }
    }
}
Also used : LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) FlushablePositionAwareChecksumChannel(org.neo4j.kernel.impl.transaction.log.FlushablePositionAwareChecksumChannel)

Example 22 with LogFile

use of org.neo4j.kernel.impl.transaction.log.files.LogFile in project neo4j by neo4j.

the class DumpCommandIT method databaseThatRequireRecoveryIsNotDumpable.

@Test
void databaseThatRequireRecoveryIsNotDumpable() throws IOException {
    LogFiles logFiles = LogFilesBuilder.builder(databaseLayout, testDirectory.getFileSystem()).withLogVersionRepository(new SimpleLogVersionRepository()).withTransactionIdStore(new SimpleTransactionIdStore()).withCommandReaderFactory(StorageEngineFactory.defaultStorageEngine().commandReaderFactory()).withStoreId(StoreId.UNKNOWN).build();
    try (Lifespan ignored = new Lifespan(logFiles)) {
        LogFile logFile = logFiles.getLogFile();
        LogEntryWriter writer = logFile.getTransactionLogWriter().getWriter();
        writer.writeStartEntry(0x123456789ABCDEFL, logFile.getLogFileInformation().getLastEntryId() + 1, BASE_TX_CHECKSUM, new byte[] { 0 });
    }
    CommandFailedException commandFailed = assertThrows(CommandFailedException.class, () -> execute("foo"));
    assertThat(commandFailed.getMessage()).startsWith("Active logical log detected, this might be a source of inconsistencies.");
}
Also used : LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) SimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) SimpleLogVersionRepository(org.neo4j.kernel.impl.transaction.SimpleLogVersionRepository) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) CommandFailedException(org.neo4j.cli.CommandFailedException) Test(org.junit.jupiter.api.Test)

Example 23 with LogFile

use of org.neo4j.kernel.impl.transaction.log.files.LogFile in project neo4j by neo4j.

the class RecoveryCorruptedTransactionLogIT method addCorruptedCommandsToLastLogFile.

private void addCorruptedCommandsToLastLogFile(LogEntryWriterWrapper logEntryWriterWrapper) throws IOException {
    PositiveLogFilesBasedLogVersionRepository versionRepository = new PositiveLogFilesBasedLogVersionRepository(logFiles);
    LogFiles internalLogFiles = LogFilesBuilder.builder(databaseLayout, fileSystem).withLogVersionRepository(versionRepository).withTransactionIdStore(new SimpleTransactionIdStore()).withStoreId(StoreId.UNKNOWN).withCommandReaderFactory(StorageEngineFactory.defaultStorageEngine().commandReaderFactory()).build();
    try (Lifespan lifespan = new Lifespan(internalLogFiles)) {
        LogFile transactionLogFile = internalLogFiles.getLogFile();
        LogEntryWriter<FlushablePositionAwareChecksumChannel> realLogEntryWriter = transactionLogFile.getTransactionLogWriter().getWriter();
        LogEntryWriter<FlushablePositionAwareChecksumChannel> wrappedLogEntryWriter = logEntryWriterWrapper.wrap(realLogEntryWriter);
        StaticLogEntryWriterFactory<FlushablePositionAwareChecksumChannel> factory = new StaticLogEntryWriterFactory<>(wrappedLogEntryWriter);
        TransactionLogWriter writer = new TransactionLogWriter(realLogEntryWriter.getChannel(), factory);
        List<StorageCommand> commands = new ArrayList<>();
        commands.add(new Command.PropertyCommand(new PropertyRecord(1), new PropertyRecord(2)));
        commands.add(new Command.NodeCommand(new NodeRecord(2), new NodeRecord(3)));
        PhysicalTransactionRepresentation transaction = new PhysicalTransactionRepresentation(commands);
        transaction.setHeader(new byte[0], 0, 0, 0, 0, ANONYMOUS);
        writer.append(transaction, 1000, BASE_TX_CHECKSUM);
    }
}
Also used : SimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore) StorageCommand(org.neo4j.storageengine.api.StorageCommand) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) ArrayList(java.util.ArrayList) FlushablePositionAwareChecksumChannel(org.neo4j.kernel.impl.transaction.log.FlushablePositionAwareChecksumChannel) LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord) Command(org.neo4j.internal.recordstorage.Command) StorageCommand(org.neo4j.storageengine.api.StorageCommand) TransactionLogWriter(org.neo4j.kernel.impl.transaction.log.TransactionLogWriter) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)

Example 24 with LogFile

use of org.neo4j.kernel.impl.transaction.log.files.LogFile in project neo4j by neo4j.

the class RecoveryCorruptedTransactionLogIT method getLastReadablePosition.

private LogPosition getLastReadablePosition(Path logFile) throws IOException {
    VersionAwareLogEntryReader entryReader = new VersionAwareLogEntryReader(storageEngineFactory.commandReaderFactory());
    LogFile txLogFile = logFiles.getLogFile();
    long logVersion = txLogFile.getLogVersion(logFile);
    LogPosition startPosition = txLogFile.extractHeader(logVersion).getStartPosition();
    try (ReadableLogChannel reader = openTransactionFileChannel(logVersion, startPosition)) {
        while (entryReader.readLogEntry(reader) != null) {
        // scroll to the end of readable entries
        }
    } catch (IncompleteLogHeaderException e) {
        return new LogPosition(logVersion, 0);
    }
    return entryReader.lastPosition();
}
Also used : LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) IncompleteLogHeaderException(org.neo4j.kernel.impl.transaction.log.entry.IncompleteLogHeaderException) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Example 25 with LogFile

use of org.neo4j.kernel.impl.transaction.log.files.LogFile in project neo4j by neo4j.

the class RecoveryCorruptedTransactionLogIT method startWithNotLastTransactionLogHavingZerosInTheEndAndCorruptedLogRecoveryEnabled.

@Test
void startWithNotLastTransactionLogHavingZerosInTheEndAndCorruptedLogRecoveryEnabled() throws IOException {
    DatabaseManagementService managementService = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    logFiles = buildDefaultLogFiles(getStoreId(database));
    generateTransaction(database);
    managementService.shutdown();
    long originalLogDataLength;
    Path firstLogFile;
    try (Lifespan lifespan = new Lifespan(logFiles)) {
        LogFile logFile = logFiles.getLogFile();
        LogPosition readablePosition = getLastReadablePosition(logFile);
        firstLogFile = logFiles.getLogFile().getHighestLogFile();
        originalLogDataLength = readablePosition.getByteOffset();
        logFile.rotate();
        // append zeros in the end of previous file causing illegal suffix
        try (StoreFileChannel writeChannel = fileSystem.write(firstLogFile)) {
            writeChannel.position(writeChannel.size());
            for (int i = 0; i < 10; i++) {
                writeChannel.writeAll(ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0 }));
            }
        }
    }
    startStopDbRecoveryOfCorruptedLogs();
    assertEquals(originalLogDataLength, fileSystem.getFileSize(firstLogFile));
    assertThat(logProvider).containsMessages("Recovery required from position LogPosition{logVersion=0, byteOffset=" + (996 + txOffsetAfterStart) + "}").assertExceptionForLogMessage("Fail to read transaction log version 0.").hasMessage("Transaction log files with version 0 has 50 unreadable bytes. Was able to read upto " + (996 + txOffsetAfterStart) + " but " + (1046 + txOffsetAfterStart) + " is available.");
}
Also used : Path(java.nio.file.Path) LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) StoreFileChannel(org.neo4j.io.fs.StoreFileChannel) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

LogFile (org.neo4j.kernel.impl.transaction.log.files.LogFile)42 Test (org.junit.jupiter.api.Test)25 LogFiles (org.neo4j.kernel.impl.transaction.log.files.LogFiles)21 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)15 Path (java.nio.file.Path)12 CheckpointFile (org.neo4j.kernel.impl.transaction.log.files.checkpoint.CheckpointFile)9 SimpleTransactionIdStore (org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore)7 Lifespan (org.neo4j.kernel.lifecycle.Lifespan)7 File (java.io.File)5 ZipFile (java.util.zip.ZipFile)5 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)5 SimpleLogVersionRepository (org.neo4j.kernel.impl.transaction.SimpleLogVersionRepository)5 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)5 IOException (java.io.IOException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)4 FlushablePositionAwareChecksumChannel (org.neo4j.kernel.impl.transaction.log.FlushablePositionAwareChecksumChannel)4 LogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader)4 Assertions.assertArrayEquals (org.junit.jupiter.api.Assertions.assertArrayEquals)3 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)3