Search in sources :

Example 36 with LogFile

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

the class LogPruningTest method setUp.

@BeforeEach
void setUp() {
    fs = mock(FileSystemAbstraction.class);
    logFiles = mock(LogFiles.class);
    LogFile logFile = mock(LogFile.class);
    when(logFiles.getLogFile()).thenReturn(logFile);
    when(logFiles.getCheckpointFile()).thenReturn(mock(CheckpointFile.class));
    doAnswer(inv -> Path.of(String.valueOf(inv.getArguments()[0]))).when(logFile).getLogFileForVersion(anyLong());
    logProvider = new AssertableLogProvider();
    clock = mock(SystemNanoClock.class);
    factory = mock(LogPruneStrategyFactory.class);
}
Also used : LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) SystemNanoClock(org.neo4j.time.SystemNanoClock) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) CheckpointFile(org.neo4j.kernel.impl.transaction.log.files.checkpoint.CheckpointFile) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 37 with LogFile

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

the class TransactionLogFileTest method shouldReadOlderLogs.

@Test
void shouldReadOlderLogs() throws Exception {
    // GIVEN
    LogFiles logFiles = buildLogFiles();
    life.start();
    life.add(logFiles);
    // WHEN
    LogFile logFile = logFiles.getLogFile();
    TransactionLogWriter logWriter = logFile.getTransactionLogWriter();
    var writer = logWriter.getChannel();
    LogPosition position1 = logWriter.getCurrentPosition();
    int intValue = 45;
    long longValue = 4854587;
    byte[] someBytes = someBytes(40);
    writer.putInt(intValue);
    writer.putLong(longValue);
    writer.put(someBytes, someBytes.length);
    logFile.flush();
    LogPosition position2 = logWriter.getCurrentPosition();
    long longValue2 = 123456789L;
    writer.putLong(longValue2);
    writer.put(someBytes, someBytes.length);
    logFile.flush();
    // THEN
    try (ReadableChannel reader = logFile.getReader(position1)) {
        assertEquals(intValue, reader.getInt());
        assertEquals(longValue, reader.getLong());
        assertArrayEquals(someBytes, readBytes(reader, 40));
    }
    try (ReadableChannel reader = logFile.getReader(position2)) {
        assertEquals(longValue2, reader.getLong());
        assertArrayEquals(someBytes, readBytes(reader, 40));
    }
}
Also used : LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) ReadableChannel(org.neo4j.io.fs.ReadableChannel) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) Test(org.junit.jupiter.api.Test)

Example 38 with LogFile

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

the class TransactionLogFileTest method shouldWriteSomeDataIntoTheLog.

@Test
void shouldWriteSomeDataIntoTheLog() throws Exception {
    // GIVEN
    LogFiles logFiles = buildLogFiles();
    life.start();
    life.add(logFiles);
    // WHEN
    LogFile logFile = logFiles.getLogFile();
    TransactionLogWriter transactionLogWriter = logFile.getTransactionLogWriter();
    var channel = transactionLogWriter.getChannel();
    LogPosition currentPosition = transactionLogWriter.getCurrentPosition();
    int intValue = 45;
    long longValue = 4854587;
    channel.putInt(intValue);
    channel.putLong(longValue);
    logFile.flush();
    // THEN
    try (ReadableChannel reader = logFile.getReader(currentPosition)) {
        assertEquals(intValue, reader.getInt());
        assertEquals(longValue, reader.getLong());
    }
}
Also used : LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) ReadableChannel(org.neo4j.io.fs.ReadableChannel) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) Test(org.junit.jupiter.api.Test)

Example 39 with LogFile

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

the class TransactionLogFileTest method shouldBatchUpMultipleWaitingForceRequests.

@Test
void shouldBatchUpMultipleWaitingForceRequests() throws Throwable {
    LogFiles logFiles = buildLogFiles();
    life.start();
    life.add(logFiles);
    LogFile logFile = logFiles.getLogFile();
    var capturingChannel = wrappingFileSystem.getCapturingChannel();
    var flushesBefore = capturingChannel.getFlushCounter().get();
    var writesBefore = capturingChannel.getWriteAllCounter().get();
    ReentrantLock writeAllLock = capturingChannel.getWriteAllLock();
    writeAllLock.lock();
    int executors = 10;
    var executorService = Executors.newFixedThreadPool(executors);
    try {
        List<Future<?>> futures = Stream.iterate(0, i -> i + 1).limit(executors).map(v -> executorService.submit(() -> logFile.forceAfterAppend(LogAppendEvent.NULL))).collect(toList());
        while (!writeAllLock.hasQueuedThreads()) {
            parkNanos(100);
        }
        writeAllLock.unlock();
        assertThat(futures).hasSize(executors);
        Futures.getAll(futures);
    } finally {
        if (writeAllLock.isLocked()) {
            writeAllLock.unlock();
        }
        executorService.shutdownNow();
    }
    assertThat(capturingChannel.getFlushCounter().get() - flushesBefore).isLessThanOrEqualTo(executors);
    assertThat(capturingChannel.getWriteAllCounter().get() - writesBefore).isLessThanOrEqualTo(executors);
}
Also used : LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) ReentrantLock(java.util.concurrent.locks.ReentrantLock) BeforeEach(org.junit.jupiter.api.BeforeEach) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) NativeCallResult(org.neo4j.internal.nativeimpl.NativeCallResult) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LogFilesBuilder(org.neo4j.kernel.impl.transaction.log.files.LogFilesBuilder) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) ByteBuffer(java.nio.ByteBuffer) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) Mockito.doThrow(org.mockito.Mockito.doThrow) Future(java.util.concurrent.Future) StoreChannel(org.neo4j.io.fs.StoreChannel) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader) LogVersionRepository(org.neo4j.storageengine.api.LogVersionRepository) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) Path(java.nio.file.Path) Futures(org.neo4j.util.concurrent.Futures) TransactionLogFilesHelper(org.neo4j.kernel.impl.transaction.log.files.TransactionLogFilesHelper) BASE_TX_COMMIT_TIMESTAMP(org.neo4j.storageengine.api.TransactionIdStore.BASE_TX_COMMIT_TIMESTAMP) Executors(java.util.concurrent.Executors) Neo4jLayoutExtension(org.neo4j.test.extension.Neo4jLayoutExtension) Test(org.junit.jupiter.api.Test) List(java.util.List) 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) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LockSupport.parkNanos(java.util.concurrent.locks.LockSupport.parkNanos) LogFile(org.neo4j.kernel.impl.transaction.log.files.LogFile) Inject(org.neo4j.test.extension.Inject) StoreId(org.neo4j.storageengine.api.StoreId) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteUnit(org.neo4j.io.ByteUnit) LifeExtension(org.neo4j.test.extension.LifeExtension) ReentrantLock(java.util.concurrent.locks.ReentrantLock) NativeAccess(org.neo4j.internal.nativeimpl.NativeAccess) IOException(java.io.IOException) LogAppendEvent(org.neo4j.kernel.impl.transaction.tracing.LogAppendEvent) Mockito.when(org.mockito.Mockito.when) SimpleLogVersionRepository(org.neo4j.kernel.impl.transaction.SimpleLogVersionRepository) Mockito.verify(org.mockito.Mockito.verify) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) IncompleteLogHeaderException(org.neo4j.kernel.impl.transaction.log.entry.IncompleteLogHeaderException) Collectors.toList(java.util.stream.Collectors.toList) CURRENT_FORMAT_LOG_HEADER_SIZE(org.neo4j.kernel.impl.transaction.log.entry.LogVersions.CURRENT_FORMAT_LOG_HEADER_SIZE) ReadableChannel(org.neo4j.io.fs.ReadableChannel) TransactionIdStore(org.neo4j.storageengine.api.TransactionIdStore) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) SimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) Future(java.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 40 with LogFile

use of org.neo4j.kernel.impl.transaction.log.files.LogFile 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

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