Search in sources :

Example 6 with LogVersionedStoreChannel

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

the class LatestCheckPointFinder method extractFirstTxIdAfterPosition.

/**
     * Extracts txId from first commit entry, when starting reading at the given {@code position}.
     * If no commit entry found in the version, the reader will continue into next version(s) up till
     * {@code maxLogVersion} until finding one.
     *
     * @param initialPosition {@link LogPosition} to start scan from.
     * @param maxLogVersion max log version to scan.
     * @return txId of closes commit entry to {@code initialPosition}, or {@link LatestCheckPoint#NO_TRANSACTION_ID}
     * if not found.
     * @throws IOException on I/O error.
     */
private long extractFirstTxIdAfterPosition(LogPosition initialPosition, long maxLogVersion) throws IOException {
    LogPosition currentPosition = initialPosition;
    while (currentPosition.getLogVersion() <= maxLogVersion) {
        LogVersionedStoreChannel storeChannel = PhysicalLogFile.tryOpenForVersion(logFiles, fileSystem, currentPosition.getLogVersion(), false);
        if (storeChannel != null) {
            try {
                storeChannel.position(currentPosition.getByteOffset());
                try (ReadAheadLogChannel logChannel = new ReadAheadLogChannel(storeChannel, NO_MORE_CHANNELS);
                    LogEntryCursor cursor = new LogEntryCursor(logEntryReader, logChannel)) {
                    while (cursor.next()) {
                        LogEntry entry = cursor.get();
                        if (entry instanceof LogEntryCommit) {
                            return ((LogEntryCommit) entry).getTxId();
                        }
                    }
                }
            } finally {
                storeChannel.close();
            }
        }
        currentPosition = LogPosition.start(currentPosition.getLogVersion() + 1);
    }
    return LatestCheckPoint.NO_TRANSACTION_ID;
}
Also used : LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) ReadAheadLogChannel(org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Example 7 with LogVersionedStoreChannel

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

the class TransactionLogCatchUpWriterTest method verifyTransactionsInLog.

private void verifyTransactionsInLog(long fromTxId, long endTxId) throws IOException {
    long expectedTxId = fromTxId;
    PhysicalLogFiles logFiles = new PhysicalLogFiles(storeDir, fs);
    LogVersionedStoreChannel versionedStoreChannel = PhysicalLogFile.openForVersion(logFiles, fs, 0, false);
    try (ReadableLogChannel channel = new ReadAheadLogChannel(versionedStoreChannel, LogVersionBridge.NO_MORE_CHANNELS, 1024)) {
        try (PhysicalTransactionCursor<ReadableLogChannel> txCursor = new PhysicalTransactionCursor<>(channel, new VersionAwareLogEntryReader<>())) {
            while (txCursor.next()) {
                CommittedTransactionRepresentation tx = txCursor.get();
                long txId = tx.getCommitEntry().getTxId();
                assertThat(expectedTxId, lessThanOrEqualTo(endTxId));
                assertEquals(expectedTxId, txId);
                expectedTxId++;
            }
        }
    }
}
Also used : PhysicalTransactionCursor(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionCursor) ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) ReadAheadLogChannel(org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel) PhysicalLogFiles(org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles)

Example 8 with LogVersionedStoreChannel

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

the class ReadAheadLogChannelTest method shouldReadFromMultipleChannels.

@Test
void shouldReadFromMultipleChannels() throws Exception {
    // GIVEN
    writeSomeData(file(0), element -> {
        for (int i = 0; i < 10; i++) {
            element.putLong(i);
        }
        return true;
    });
    writeSomeData(file(1), element -> {
        for (int i = 10; i < 20; i++) {
            element.putLong(i);
        }
        return true;
    });
    StoreChannel storeChannel = fileSystem.read(file(0));
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, -1, /* ignored */
    (byte) -1, file(0), nativeChannelAccessor);
    try (ReadAheadLogChannel channel = new ReadAheadLogChannel(versionedStoreChannel, new LogVersionBridge() {

        private boolean returned;

        @Override
        public LogVersionedStoreChannel next(LogVersionedStoreChannel channel, boolean raw) throws IOException {
            if (!returned) {
                returned = true;
                channel.close();
                return new PhysicalLogVersionedStoreChannel(fileSystem.read(file(1)), -1, /* ignored */
                (byte) -1, file(1), nativeChannelAccessor);
            }
            return channel;
        }
    }, INSTANCE)) {
        // THEN
        for (long i = 0; i < 20; i++) {
            assertEquals(i, channel.getLong());
        }
    }
}
Also used : LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) LogVersionBridge(org.neo4j.kernel.impl.transaction.log.LogVersionBridge) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) IOException(java.io.IOException) ReadAheadLogChannel(org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel) Test(org.junit.jupiter.api.Test)

Example 9 with LogVersionedStoreChannel

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

the class ReaderLogVersionBridgeTest method shouldReturnOldChannelWhenThereIsNoNextChannel.

@Test
void shouldReturnOldChannelWhenThereIsNoNextChannel() throws IOException {
    // given
    final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge(logFiles.getLogFile());
    when(channel.getVersion()).thenReturn(version);
    when(fs.read(any(Path.class))).thenThrow(new NoSuchFileException("mock"));
    // when
    final LogVersionedStoreChannel result = bridge.next(channel, false);
    // then
    assertEquals(channel, result);
    verify(channel, never()).close();
}
Also used : Path(java.nio.file.Path) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) ReaderLogVersionBridge(org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge) NoSuchFileException(java.nio.file.NoSuchFileException) Test(org.junit.jupiter.api.Test)

Example 10 with LogVersionedStoreChannel

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

the class ReaderLogVersionBridgeTest method shouldReturnOldChannelWhenNextChannelHasNotGottenCompleteHeaderYet.

@Test
void shouldReturnOldChannelWhenNextChannelHasNotGottenCompleteHeaderYet() throws Exception {
    // given
    final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge(logFiles.getLogFile());
    final StoreChannel nextVersionWithIncompleteHeader = mock(StoreChannel.class);
    when(nextVersionWithIncompleteHeader.read(any(ByteBuffer.class))).thenReturn(CURRENT_FORMAT_LOG_HEADER_SIZE / 2);
    when(channel.getVersion()).thenReturn(version);
    when(fs.fileExists(any(Path.class))).thenReturn(true);
    when(fs.read(any(Path.class))).thenReturn(nextVersionWithIncompleteHeader);
    // when
    final LogVersionedStoreChannel result = bridge.next(channel, false);
    // then
    assertEquals(channel, result);
    verify(channel, never()).close();
}
Also used : Path(java.nio.file.Path) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) ReaderLogVersionBridge(org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)22 LogEntry (org.neo4j.kernel.impl.transaction.log.entry.LogEntry)11 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)10 StoreChannel (org.neo4j.io.fs.StoreChannel)8 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)8 File (java.io.File)6 Test (org.junit.Test)6 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)6 LogEntryCommit (org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit)6 LogEntryStart (org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart)6 LogEntryWriter (org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter)6 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)6 Test (org.junit.jupiter.api.Test)5 LogEntryCursor (org.neo4j.kernel.impl.transaction.log.LogEntryCursor)5 ReaderLogVersionBridge (org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge)5 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)4 Path (java.nio.file.Path)4 CommittedTransactionRepresentation (org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation)3 LogVersionBridge (org.neo4j.kernel.impl.transaction.log.LogVersionBridge)3