Search in sources :

Example 1 with ReaderLogVersionBridge

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

the class DumpLogicalLog method dump.

public void dump(String filenameOrDirectory, PrintStream out, Predicate<LogEntry[]> filter, Function<LogEntry, String> serializer) throws IOException {
    File file = new File(filenameOrDirectory);
    printFile(file, out);
    File firstFile;
    LogVersionBridge bridge;
    if (file.isDirectory()) {
        // Use natural log version bridging if a directory is supplied
        final PhysicalLogFiles logFiles = new PhysicalLogFiles(file, fileSystem);
        bridge = new ReaderLogVersionBridge(fileSystem, logFiles) {

            @Override
            public LogVersionedStoreChannel next(LogVersionedStoreChannel channel) throws IOException {
                LogVersionedStoreChannel next = super.next(channel);
                if (next != channel) {
                    printFile(logFiles.getLogFileForVersion(next.getVersion()), out);
                }
                return next;
            }
        };
        firstFile = logFiles.getLogFileForVersion(logFiles.getLowestLogVersion());
    } else {
        // Use no bridging, simple reading this single log file if a file is supplied
        firstFile = file;
        bridge = NO_MORE_CHANNELS;
    }
    StoreChannel fileChannel = fileSystem.open(firstFile, "r");
    ByteBuffer buffer = ByteBuffer.allocateDirect(LOG_HEADER_SIZE);
    LogHeader logHeader;
    try {
        logHeader = readLogHeader(buffer, fileChannel, false, firstFile);
    } catch (IOException ex) {
        out.println("Unable to read timestamp information, no records in logical log.");
        out.println(ex.getMessage());
        fileChannel.close();
        throw ex;
    }
    out.println("Logical log format: " + logHeader.logFormatVersion + " version: " + logHeader.logVersion + " with prev committed tx[" + logHeader.lastCommittedTxId + "]");
    PhysicalLogVersionedStoreChannel channel = new PhysicalLogVersionedStoreChannel(fileChannel, logHeader.logVersion, logHeader.logFormatVersion);
    ReadableClosablePositionAwareChannel logChannel = new ReadAheadLogChannel(channel, bridge, DEFAULT_READ_AHEAD_SIZE);
    LogEntryReader<ReadableClosablePositionAwareChannel> entryReader = new VersionAwareLogEntryReader<>();
    IOCursor<LogEntry> entryCursor = new LogEntryCursor(entryReader, logChannel);
    TransactionLogEntryCursor transactionCursor = new TransactionLogEntryCursor(entryCursor);
    try (IOCursor<LogEntry[]> cursor = filter == null ? transactionCursor : new FilteringIOCursor<>(transactionCursor, filter)) {
        while (cursor.next()) {
            for (LogEntry entry : cursor.get()) {
                out.println(serializer.apply(entry));
            }
        }
    }
}
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) ReaderLogVersionBridge(org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge) LogVersionBridge(org.neo4j.kernel.impl.transaction.log.LogVersionBridge) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) TransactionLogEntryCursor(org.neo4j.kernel.impl.transaction.log.TransactionLogEntryCursor) TransactionLogEntryCursor(org.neo4j.kernel.impl.transaction.log.TransactionLogEntryCursor) PhysicalLogFiles(org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles) ReadableClosablePositionAwareChannel(org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel) ReaderLogVersionBridge(org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) File(java.io.File) ReadAheadLogChannel(org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry)

Example 2 with ReaderLogVersionBridge

use of org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge 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 3 with ReaderLogVersionBridge

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

Example 4 with ReaderLogVersionBridge

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

the class ReaderLogVersionBridgeTest method shouldReturnOldChannelWhenNextChannelHasntGottenCompleteHeaderYet.

@Test
public void shouldReturnOldChannelWhenNextChannelHasntGottenCompleteHeaderYet() throws Exception {
    // given
    final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge(fs, logFiles);
    final StoreChannel nextVersionWithIncompleteHeader = mock(StoreChannel.class);
    when(nextVersionWithIncompleteHeader.read(any(ByteBuffer.class))).thenReturn(LOG_HEADER_SIZE / 2);
    when(channel.getVersion()).thenReturn(version);
    when(fs.fileExists(file)).thenReturn(true);
    when(logFiles.getLogFileForVersion(version + 1)).thenReturn(file);
    when(fs.open(file, "r")).thenReturn(nextVersionWithIncompleteHeader);
    // when
    final LogVersionedStoreChannel result = bridge.next(channel);
    // then
    assertEquals(channel, result);
    verify(channel, never()).close();
}
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) ReaderLogVersionBridge(org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with ReaderLogVersionBridge

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

the class ReaderLogVersionBridgeTest method shouldOpenTheNextChannelWhenItExists.

@Test
void shouldOpenTheNextChannelWhenItExists() throws IOException {
    // given
    final StoreChannel newStoreChannel = mock(StoreChannel.class);
    final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge(logFiles.getLogFile());
    when(channel.getVersion()).thenReturn(version);
    when(channel.getLogFormatVersion()).thenReturn(CURRENT_LOG_FORMAT_VERSION);
    when(fs.fileExists(any(Path.class))).thenReturn(true);
    when(fs.read(any(Path.class))).thenReturn(newStoreChannel);
    when(newStoreChannel.read(ArgumentMatchers.<ByteBuffer>any())).then(new Answer<>() {

        private int count;

        @Override
        public Integer answer(InvocationOnMock invocation) {
            count++;
            ByteBuffer buffer = invocation.getArgument(0);
            if (count == 1) {
                buffer.putLong(encodeLogVersion(version + 1, CURRENT_LOG_FORMAT_VERSION));
                return Long.BYTES;
            }
            if (count == 2) {
                buffer.putLong(42);
                buffer.putLong(1);
                buffer.putLong(2);
                buffer.putLong(3);
                buffer.putLong(4);
                buffer.putLong(5);
                // reserved
                buffer.putLong(0);
                return Long.BYTES * 7;
            }
            throw new AssertionError("Should only be called twice.");
        }
    });
    // when
    final LogVersionedStoreChannel result = bridge.next(channel, false);
    // then
    PhysicalLogVersionedStoreChannel expected = new PhysicalLogVersionedStoreChannel(newStoreChannel, version + 1, CURRENT_LOG_FORMAT_VERSION, Path.of("log.file"), ChannelNativeAccessor.EMPTY_ACCESSOR);
    assertEquals(expected, result);
    verify(channel).close();
}
Also used : Path(java.nio.file.Path) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)5 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)5 ReaderLogVersionBridge (org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge)5 ByteBuffer (java.nio.ByteBuffer)4 StoreChannel (org.neo4j.io.fs.StoreChannel)4 Path (java.nio.file.Path)3 Test (org.junit.jupiter.api.Test)3 File (java.io.File)1 IOException (java.io.IOException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 LogEntryCursor (org.neo4j.kernel.impl.transaction.log.LogEntryCursor)1 LogVersionBridge (org.neo4j.kernel.impl.transaction.log.LogVersionBridge)1 PhysicalLogFiles (org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles)1 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)1 ReadableClosablePositionAwareChannel (org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel)1 TransactionLogEntryCursor (org.neo4j.kernel.impl.transaction.log.TransactionLogEntryCursor)1 LogEntry (org.neo4j.kernel.impl.transaction.log.entry.LogEntry)1 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)1