Search in sources :

Example 26 with LogPosition

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

the class LatestCheckPointFinder method latestCheckPoint.

private LatestCheckPoint latestCheckPoint(long fromVersionBackwards, long version, LogEntryStart latestStartEntry, long oldestVersionFound, CheckPoint latestCheckPoint) throws IOException {
    // Is the latest start entry in this log file version later than what the latest check point targets?
    LogPosition target = latestCheckPoint.getLogPosition();
    boolean startEntryAfterCheckPoint = latestStartEntry != null && latestStartEntry.getStartPosition().compareTo(target) >= 0;
    if (!startEntryAfterCheckPoint) {
        if (target.getLogVersion() < version) {
            // This check point entry targets a previous log file.
            // Go there and see if there's a transaction. Reader is capped to that log version.
            startEntryAfterCheckPoint = extractFirstTxIdAfterPosition(target, version) != LatestCheckPoint.NO_TRANSACTION_ID;
        }
    }
    // Extract first transaction id after check point target position.
    // Reader may continue into log files after the initial version.
    long firstTxIdAfterCheckPoint = startEntryAfterCheckPoint ? extractFirstTxIdAfterPosition(target, fromVersionBackwards) : LatestCheckPoint.NO_TRANSACTION_ID;
    return new LatestCheckPoint(latestCheckPoint, startEntryAfterCheckPoint, firstTxIdAfterCheckPoint, oldestVersionFound);
}
Also used : LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Example 27 with LogPosition

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

the class Recovery method init.

@Override
public void init() throws Throwable {
    LogPosition recoveryFromPosition = spi.getPositionToRecoverFrom();
    if (LogPosition.UNSPECIFIED.equals(recoveryFromPosition)) {
        return;
    }
    monitor.recoveryRequired(recoveryFromPosition);
    LogPosition recoveryToPosition;
    CommittedTransactionRepresentation lastTransaction = null;
    Visitor<CommittedTransactionRepresentation, Exception> recoveryVisitor = spi.startRecovery();
    try (TransactionCursor transactionsToRecover = spi.getTransactions(recoveryFromPosition)) {
        while (transactionsToRecover.next()) {
            lastTransaction = transactionsToRecover.get();
            long txId = lastTransaction.getCommitEntry().getTxId();
            recoveryVisitor.visit(lastTransaction);
            monitor.transactionRecovered(txId);
            numberOfRecoveredTransactions++;
        }
        recoveryToPosition = transactionsToRecover.position();
    }
    if (recoveryToPosition.equals(LogPosition.UNSPECIFIED)) {
        recoveryToPosition = recoveryFromPosition;
    }
    spi.allTransactionsRecovered(lastTransaction, recoveryToPosition);
    recoveredLog = true;
    spi.forceEverything();
}
Also used : CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) TransactionCursor(org.neo4j.kernel.impl.transaction.log.TransactionCursor) IOException(java.io.IOException) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Example 28 with LogPosition

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

the class RecoveryTest method shouldRecoverExistingData.

@Test
public void shouldRecoverExistingData() throws Exception {
    final PhysicalLogFiles logFiles = new PhysicalLogFiles(directory.directory(), "log", fileSystemRule.get());
    File file = logFiles.getLogFileForVersion(logVersion);
    writeSomeData(file, new Visitor<Pair<LogEntryWriter, Consumer<LogPositionMarker>>, IOException>() {

        @Override
        public boolean visit(Pair<LogEntryWriter, Consumer<LogPositionMarker>> pair) throws IOException {
            LogEntryWriter writer = pair.first();
            Consumer<LogPositionMarker> consumer = pair.other();
            LogPositionMarker marker = new LogPositionMarker();
            // last committed tx
            consumer.accept(marker);
            LogPosition lastCommittedTxPosition = marker.newPosition();
            writer.writeStartEntry(0, 1, 2L, 3L, new byte[0]);
            lastCommittedTxStartEntry = new LogEntryStart(0, 1, 2L, 3L, new byte[0], lastCommittedTxPosition);
            writer.writeCommitEntry(4L, 5L);
            lastCommittedTxCommitEntry = new OnePhaseCommit(4L, 5L);
            // check point pointing to the previously committed transaction
            writer.writeCheckPointEntry(lastCommittedTxPosition);
            expectedCheckPointEntry = new CheckPoint(lastCommittedTxPosition);
            // tx committed after checkpoint
            consumer.accept(marker);
            writer.writeStartEntry(0, 1, 6L, 4L, new byte[0]);
            expectedStartEntry = new LogEntryStart(0, 1, 6L, 4L, new byte[0], marker.newPosition());
            writer.writeCommitEntry(5L, 7L);
            expectedCommitEntry = new OnePhaseCommit(5L, 7L);
            return true;
        }
    });
    LifeSupport life = new LifeSupport();
    Recovery.Monitor monitor = mock(Recovery.Monitor.class);
    final AtomicBoolean recoveryRequired = new AtomicBoolean();
    try {
        StorageEngine storageEngine = mock(StorageEngine.class);
        final LogEntryReader<ReadableClosablePositionAwareChannel> reader = new VersionAwareLogEntryReader<>();
        LatestCheckPointFinder finder = new LatestCheckPointFinder(logFiles, fileSystemRule.get(), reader);
        LogHeaderCache logHeaderCache = new LogHeaderCache(10);
        TransactionMetadataCache metadataCache = new TransactionMetadataCache(100);
        LogFile logFile = life.add(new PhysicalLogFile(fileSystemRule.get(), logFiles, 50, () -> transactionIdStore.getLastCommittedTransactionId(), logVersionRepository, mock(PhysicalLogFile.Monitor.class), logHeaderCache));
        LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFile, metadataCache, reader);
        life.add(new Recovery(new DefaultRecoverySPI(storageEngine, logFiles, fileSystemRule.get(), logVersionRepository, finder, transactionIdStore, txStore, NO_MONITOR) {

            private int nr = 0;

            @Override
            public Visitor<CommittedTransactionRepresentation, Exception> startRecovery() {
                recoveryRequired.set(true);
                final Visitor<CommittedTransactionRepresentation, Exception> actual = super.startRecovery();
                return new Visitor<CommittedTransactionRepresentation, Exception>() {

                    @Override
                    public boolean visit(CommittedTransactionRepresentation tx) throws Exception {
                        actual.visit(tx);
                        switch(nr++) {
                            case 0:
                                assertEquals(lastCommittedTxStartEntry, tx.getStartEntry());
                                assertEquals(lastCommittedTxCommitEntry, tx.getCommitEntry());
                                break;
                            case 1:
                                assertEquals(expectedStartEntry, tx.getStartEntry());
                                assertEquals(expectedCommitEntry, tx.getCommitEntry());
                                break;
                            default:
                                fail("Too many recovered transactions");
                        }
                        return false;
                    }
                };
            }
        }, monitor));
        life.start();
        InOrder order = inOrder(monitor);
        order.verify(monitor, times(1)).recoveryRequired(any(LogPosition.class));
        order.verify(monitor, times(1)).recoveryCompleted(2);
        assertTrue(recoveryRequired.get());
    } finally {
        life.shutdown();
    }
}
Also used : DefaultRecoverySPI(org.neo4j.kernel.recovery.DefaultRecoverySPI) Visitor(org.neo4j.helpers.collection.Visitor) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) StorageEngine(org.neo4j.storageengine.api.StorageEngine) Recovery(org.neo4j.kernel.recovery.Recovery) PhysicalLogFiles(org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles) LogPositionMarker(org.neo4j.kernel.impl.transaction.log.LogPositionMarker) ReadableClosablePositionAwareChannel(org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel) LogFile(org.neo4j.kernel.impl.transaction.log.LogFile) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) Consumer(java.util.function.Consumer) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) OnePhaseCommit(org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit) Pair(org.neo4j.helpers.collection.Pair) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) CheckPoint(org.neo4j.kernel.impl.transaction.log.entry.CheckPoint) LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LatestCheckPointFinder(org.neo4j.kernel.recovery.LatestCheckPointFinder) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) InOrder(org.mockito.InOrder) TransactionMetadataCache(org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache) IOException(java.io.IOException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LogFile(org.neo4j.kernel.impl.transaction.log.LogFile) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) File(java.io.File) LogHeaderCache(org.neo4j.kernel.impl.transaction.log.LogHeaderCache) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) Test(org.junit.Test)

Example 29 with LogPosition

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

the class VersionAwareLogEntryReaderTest method shouldParseOldStartEntry.

@Test
public void shouldParseOldStartEntry() throws IOException {
    // given
    LogEntryVersion version = LogEntryVersion.V2_1;
    final InMemoryClosableChannel channel = new InMemoryClosableChannel();
    final LogEntryStart start = new LogEntryStart(1, 2, 3, 4, new byte[] {}, new LogPosition(0, 37));
    channel.put(version.byteCode());
    channel.put(LogEntryByteCodes.TX_START);
    // ignored data
    // globalId length
    channel.put((byte) 1);
    // branchId length
    channel.put((byte) 0);
    // globalId
    channel.put(new byte[] { 7 }, 1);
    // branchId
    channel.put(new byte[] {}, 0);
    // identifier
    channel.putInt(123);
    // formatId
    channel.putInt(456);
    // actually used data
    channel.putInt(start.getMasterId());
    channel.putInt(start.getLocalId());
    channel.putLong(start.getTimeWritten());
    channel.putLong(start.getLastCommittedTxWhenTransactionStarted());
    // when
    final LogEntry logEntry = logEntryReader.readLogEntry(channel);
    // then
    assertTrue(logEntry instanceof IdentifiableLogEntry);
    assertEquals(start, ((IdentifiableLogEntry) logEntry).getEntry());
}
Also used : InMemoryClosableChannel(org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) Test(org.junit.Test)

Example 30 with LogPosition

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

the class VersionAwareLogEntryReaderTest method shouldReadACheckPointLogEntry.

@Test
public void shouldReadACheckPointLogEntry() throws IOException {
    // given
    LogEntryVersion version = LogEntryVersion.CURRENT;
    final LogPosition logPosition = new LogPosition(42, 43);
    final CheckPoint checkPoint = new CheckPoint(version, logPosition);
    final InMemoryClosableChannel channel = new InMemoryClosableChannel();
    channel.put(version.byteCode());
    channel.put(LogEntryByteCodes.CHECK_POINT);
    channel.putLong(logPosition.getLogVersion());
    channel.putLong(logPosition.getByteOffset());
    // when
    final LogEntry logEntry = logEntryReader.readLogEntry(channel);
    // then
    assertEquals(checkPoint, logEntry);
}
Also used : InMemoryClosableChannel(org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) Test(org.junit.Test)

Aggregations

LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)42 Test (org.junit.Test)26 File (java.io.File)11 PhysicalLogFile (org.neo4j.kernel.impl.transaction.log.PhysicalLogFile)9 PhysicalLogFiles (org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles)9 IOException (java.io.IOException)6 LogPositionMarker (org.neo4j.kernel.impl.transaction.log.LogPositionMarker)6 ReadableClosablePositionAwareChannel (org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel)5 TransactionMetadataCache (org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache)5 VersionAwareLogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader)5 InMemoryClosableChannel (org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel)4 LogHeaderCache (org.neo4j.kernel.impl.transaction.log.LogHeaderCache)4 CheckPoint (org.neo4j.kernel.impl.transaction.log.entry.CheckPoint)4 LogEntry (org.neo4j.kernel.impl.transaction.log.entry.LogEntry)4 LogEntryWriter (org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter)4 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)4 LatestCheckPoint (org.neo4j.kernel.recovery.LatestCheckPointFinder.LatestCheckPoint)4 LogFile (org.neo4j.kernel.impl.transaction.log.LogFile)3 PhysicalLogicalTransactionStore (org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore)3 HashMap (java.util.HashMap)2