Search in sources :

Example 21 with LogHeader

use of org.neo4j.kernel.impl.transaction.log.entry.LogHeader in project neo4j by neo4j.

the class TransactionLogChannelAllocator method createLogChannel.

public PhysicalLogVersionedStoreChannel createLogChannel(long version, LongSupplier lastCommittedTransactionId) throws IOException {
    AllocatedFile allocatedFile = allocateFile(version);
    var storeChannel = allocatedFile.getStoreChannel();
    var logFile = allocatedFile.getPath();
    try (var scopedBuffer = new HeapScopedBuffer(CURRENT_FORMAT_LOG_HEADER_SIZE, logFilesContext.getMemoryTracker())) {
        var buffer = scopedBuffer.getBuffer();
        LogHeader header = readLogHeader(buffer, storeChannel, false, logFile);
        if (header == null) {
            try (LogFileCreateEvent ignored = databaseTracer.createLogFile()) {
                // we always write file header from the beginning of the file
                storeChannel.position(0);
                long lastTxId = lastCommittedTransactionId.getAsLong();
                LogHeader logHeader = new LogHeader(version, lastTxId, logFilesContext.getStoreId());
                LogHeaderWriter.writeLogHeader(storeChannel, logHeader, logFilesContext.getMemoryTracker());
                logHeaderCache.putHeader(version, logHeader);
            }
        }
        byte formatVersion = header == null ? CURRENT_LOG_FORMAT_VERSION : header.getLogFormatVersion();
        return new PhysicalLogVersionedStoreChannel(storeChannel, version, formatVersion, logFile, nativeChannelAccessor);
    }
}
Also used : HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) LogFileCreateEvent(org.neo4j.kernel.impl.transaction.tracing.LogFileCreateEvent) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader)

Example 22 with LogHeader

use of org.neo4j.kernel.impl.transaction.log.entry.LogHeader in project neo4j by neo4j.

the class TransactionLogFile method extractHeader.

private LogHeader extractHeader(long version, boolean strict) throws IOException {
    LogHeader logHeader = logHeaderCache.getLogHeader(version);
    if (logHeader == null) {
        logHeader = readLogHeader(fileSystem, getLogFileForVersion(version), strict, context.getMemoryTracker());
        if (!strict && logHeader == null) {
            return null;
        }
        logHeaderCache.putHeader(version, logHeader);
    }
    return logHeader;
}
Also used : LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader)

Example 23 with LogHeader

use of org.neo4j.kernel.impl.transaction.log.entry.LogHeader in project neo4j by neo4j.

the class TransactionLogFileInformation method getFirstEntryId.

@Override
public long getFirstEntryId(long version) throws IOException {
    LogHeader logHeader = logHeaderCache.getLogHeader(version);
    if (logHeader != null) {
        // It existed in cache
        return logHeader.getLastCommittedTxId() + 1;
    }
    // Wasn't cached, go look for it
    var logFile = logFiles.getLogFile();
    if (logFile.versionExists(version)) {
        logHeader = logFile.extractHeader(version);
        logHeaderCache.putHeader(version, logHeader);
        return logHeader.getLastCommittedTxId() + 1;
    }
    return -1;
}
Also used : LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader)

Example 24 with LogHeader

use of org.neo4j.kernel.impl.transaction.log.entry.LogHeader in project neo4j by neo4j.

the class TransactionLogInitializer method initializeExistingLogFiles.

/**
 * Make sure that any existing log files in the given transaction logs directory are initialised.
 * This is done when we migrate 3.x stores into a 4.x world.
 */
public void initializeExistingLogFiles(DatabaseLayout layout, Path transactionLogsDirectory, String checkpointReason) throws Exception {
    try (LogFilesSpan span = buildLogFiles(layout, transactionLogsDirectory)) {
        LogFiles logFiles = span.getLogFiles();
        LogFile logFile = logFiles.getLogFile();
        LogHeader logHeader = logFile.extractHeader(logFile.getLowestLogVersion());
        VersionAwareLogEntryReader entryReader = new VersionAwareLogEntryReader(commandReaderFactory, false);
        try (var readableChannel = logFile.getReader(logHeader.getStartPosition());
            var cursor = new LogEntryCursor(entryReader, readableChannel)) {
            while (cursor.next()) {
                LogEntry entry = cursor.get();
                if (entry.getType() == LogEntryTypeCodes.TX_COMMIT) {
                    // The log files already contain a transaction, so we can only append checkpoint for the end of the log files.
                    appendCheckpoint(logFiles, checkpointReason, logFile.getTransactionLogWriter().getCurrentPosition());
                    return;
                }
            }
        }
        appendEmptyTransactionAndCheckPoint(logFiles, checkpointReason);
    }
}
Also used : VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry)

Example 25 with LogHeader

use of org.neo4j.kernel.impl.transaction.log.entry.LogHeader in project neo4j by neo4j.

the class DetachedLogTailScanner method isValidCheckpoint.

/**
 * Valid checkpoint points to valid location in a file, which exists and header store id matches with checkpoint store id.
 * Otherwise checkpoint is not considered valid and we need to recover.
 */
private boolean isValidCheckpoint(LogFile logFile, CheckpointInfo checkpointInfo) throws IOException {
    LogPosition logPosition = checkpointInfo.getTransactionLogPosition();
    long logVersion = logPosition.getLogVersion();
    if (!logFile.versionExists(logVersion)) {
        return false;
    }
    Path logFileForVersion = logFile.getLogFileForVersion(logVersion);
    if (fileSystem.getFileSize(logFileForVersion) < logPosition.getByteOffset()) {
        return false;
    }
    LogHeader logHeader = logFile.extractHeader(logVersion);
    StoreId headerStoreId = logHeader.getStoreId();
    return StoreId.UNKNOWN.equals(headerStoreId) || headerStoreId.equalsIgnoringVersion(checkpointInfo.storeId());
}
Also used : Path(java.nio.file.Path) StoreId(org.neo4j.storageengine.api.StoreId) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Aggregations

LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)45 LogHeaderReader.readLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader)18 Test (org.junit.jupiter.api.Test)13 LogEntry (org.neo4j.kernel.impl.transaction.log.entry.LogEntry)12 File (java.io.File)11 StoreChannel (org.neo4j.io.fs.StoreChannel)11 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)11 LogHeaderWriter.writeLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderWriter.writeLogHeader)10 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)9 Path (java.nio.file.Path)7 Test (org.junit.Test)6 LogEntryCursor (org.neo4j.kernel.impl.transaction.log.LogEntryCursor)6 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)6 IOException (java.io.IOException)5 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)5 ReadableLogChannel (org.neo4j.kernel.impl.transaction.log.ReadableLogChannel)5 LogHeaderCache (org.neo4j.kernel.impl.transaction.log.LogHeaderCache)4 LogEntryStart (org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart)4 VersionAwareLogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader)4 ByteBuffer (java.nio.ByteBuffer)3