Search in sources :

Example 26 with LogEntryWriter

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

the class TransactionLogsRecoveryTest method shouldTruncateLogAfterSinglePartialTransaction.

@Test
void shouldTruncateLogAfterSinglePartialTransaction() throws Exception {
    // GIVEN
    Path file = logFiles.getLogFile().getLogFileForVersion(logVersion);
    final LogPositionMarker marker = new LogPositionMarker();
    writeSomeData(file, pair -> {
        LogEntryWriter writer = pair.first();
        Consumer<LogPositionMarker> consumer = pair.other();
        // incomplete tx
        // <-- marker has the last good position
        consumer.accept(marker);
        writer.writeStartEntry(5L, 4L, 0, new byte[0]);
        return true;
    });
    // WHEN
    boolean recoveryRequired = recover(storeDir, logFiles);
    // THEN
    assertTrue(recoveryRequired);
    assertEquals(marker.getByteOffset(), Files.size(file));
}
Also used : Path(java.nio.file.Path) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) LogPositionMarker(org.neo4j.kernel.impl.transaction.log.LogPositionMarker) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with LogEntryWriter

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

the class TransactionLogsRecoveryTest method shouldTellTransactionIdStoreAfterSuccessfulRecovery.

@Test
void shouldTellTransactionIdStoreAfterSuccessfulRecovery() throws Exception {
    // GIVEN
    Path file = logFiles.getLogFile().getLogFileForVersion(logVersion);
    final LogPositionMarker marker = new LogPositionMarker();
    final byte[] additionalHeaderData = new byte[0];
    final long transactionId = 4;
    final long commitTimestamp = 5;
    writeSomeData(file, pair -> {
        LogEntryWriter writer = pair.first();
        Consumer<LogPositionMarker> consumer = pair.other();
        // last committed tx
        writer.writeStartEntry(2L, 3L, BASE_TX_CHECKSUM, additionalHeaderData);
        writer.writeCommitEntry(transactionId, commitTimestamp);
        consumer.accept(marker);
        return true;
    });
    // WHEN
    boolean recoveryRequired = recover(storeDir, logFiles);
    // THEN
    assertTrue(recoveryRequired);
    long[] lastClosedTransaction = transactionIdStore.getLastClosedTransaction();
    assertEquals(transactionId, lastClosedTransaction[0]);
    assertEquals(commitTimestamp, transactionIdStore.getLastCommittedTransaction().commitTimestamp());
    assertEquals(logVersion, lastClosedTransaction[1]);
    assertEquals(marker.getByteOffset(), lastClosedTransaction[2]);
}
Also used : Path(java.nio.file.Path) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) LogPositionMarker(org.neo4j.kernel.impl.transaction.log.LogPositionMarker) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with LogEntryWriter

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

the class AbstractLogTailScannerTest method logFile.

LogCreator logFile(Entry... entries) {
    return (logVersion, positions) -> {
        try {
            AtomicLong lastTxId = new AtomicLong();
            logVersionRepository.setCurrentLogVersion(logVersion, NULL);
            logVersionRepository.setCheckpointLogVersion(logVersion, NULL);
            LifeSupport logFileLife = new LifeSupport();
            logFileLife.start();
            logFileLife.add(logFiles);
            LogFile logFile = logFiles.getLogFile();
            var checkpointFile = logFiles.getCheckpointFile();
            int previousChecksum = BASE_TX_CHECKSUM;
            try {
                TransactionLogWriter logWriter = logFile.getTransactionLogWriter();
                LogEntryWriter writer = logWriter.getWriter();
                for (Entry entry : entries) {
                    LogPosition currentPosition = logWriter.getCurrentPosition();
                    positions.put(entry, currentPosition);
                    if (entry instanceof StartEntry) {
                        writer.writeStartEntry(0, 0, previousChecksum, new byte[0]);
                    } else if (entry instanceof CommitEntry) {
                        CommitEntry commitEntry = (CommitEntry) entry;
                        previousChecksum = writer.writeCommitEntry(commitEntry.txId, 0);
                        lastTxId.set(commitEntry.txId);
                    } else if (entry instanceof CheckPointEntry) {
                        CheckPointEntry checkPointEntry = (CheckPointEntry) entry;
                        Entry target = checkPointEntry.withPositionOfEntry;
                        LogPosition logPosition = target != null ? positions.get(target) : currentPosition;
                        assert logPosition != null : "No registered log position for " + target;
                        writeCheckpoint(writer, checkpointFile, logPosition);
                    } else if (entry instanceof PositionEntry) {
                    // Don't write anything, this entry is just for registering a position so that
                    // another CheckPointEntry can refer to it
                    } else {
                        throw new IllegalArgumentException("Unknown entry " + entry);
                    }
                }
            } finally {
                logFileLife.shutdown();
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    };
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) LogAssertions.assertThat(org.neo4j.logging.LogAssertions.assertThat) HashMap(java.util.HashMap) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) Inject(org.neo4j.test.extension.Inject) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Monitors(org.neo4j.monitoring.Monitors) BASE_TX_CHECKSUM(org.neo4j.storageengine.api.TransactionIdStore.BASE_TX_CHECKSUM) Map(java.util.Map) LogVersionRepository(org.neo4j.storageengine.api.LogVersionRepository) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) INFO(org.neo4j.logging.AssertableLogProvider.Level.INFO) StoreId(org.neo4j.storageengine.api.StoreId) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) Path(java.nio.file.Path) MethodSource(org.junit.jupiter.params.provider.MethodSource) PageCache(org.neo4j.io.pagecache.PageCache) LogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader) IOException(java.io.IOException) LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) NO_TRANSACTION_ID(org.neo4j.kernel.impl.transaction.log.files.checkpoint.InlinedLogTailScanner.NO_TRANSACTION_ID) Arguments(org.junit.jupiter.params.provider.Arguments) SimpleLogVersionRepository(org.neo4j.kernel.impl.transaction.SimpleLogVersionRepository) EphemeralPageCacheExtension(org.neo4j.test.extension.pagecache.EphemeralPageCacheExtension) Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EphemeralNeo4jLayoutExtension(org.neo4j.test.extension.EphemeralNeo4jLayoutExtension) INSTANCE(org.neo4j.memory.EmptyMemoryTracker.INSTANCE) Stream(java.util.stream.Stream) TestLogEntryReader.logEntryReader(org.neo4j.kernel.impl.transaction.log.TestLogEntryReader.logEntryReader) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) LogTailInformation(org.neo4j.kernel.impl.transaction.log.files.LogTailInformation) TransactionLogWriter(org.neo4j.kernel.impl.transaction.log.TransactionLogWriter) TransactionIdStore(org.neo4j.storageengine.api.TransactionIdStore) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) SimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) TransactionLogWriter(org.neo4j.kernel.impl.transaction.log.TransactionLogWriter) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Aggregations

LogEntryWriter (org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter)28 LogPositionMarker (org.neo4j.kernel.impl.transaction.log.LogPositionMarker)16 IOException (java.io.IOException)11 PhysicalLogFile (org.neo4j.kernel.impl.transaction.log.PhysicalLogFile)11 PhysicalLogFiles (org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles)11 File (java.io.File)10 Path (java.nio.file.Path)10 Test (org.junit.jupiter.api.Test)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)10 Test (org.junit.Test)9 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)9 Consumer (java.util.function.Consumer)7 LogFile (org.neo4j.kernel.impl.transaction.log.LogFile)7 LogHeaderCache (org.neo4j.kernel.impl.transaction.log.LogHeaderCache)7 LogEntryStart (org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart)7 Pair (org.neo4j.helpers.collection.Pair)6 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)6 LogicalTransactionStore (org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore)6 PhysicalLogicalTransactionStore (org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore)6