Search in sources :

Example 6 with TransactionCursor

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

the class ReversedMultiFileTransactionCursorTest method log.

private static ThrowingFunction<LogPosition, TransactionCursor, IOException> log(int... transactionCounts) throws IOException {
    long baseOffset = start().getByteOffset();
    @SuppressWarnings("unchecked") ThrowingFunction<LogPosition, TransactionCursor, IOException> result = mock(ThrowingFunction.class);
    AtomicLong txId = new AtomicLong(0);
    CommittedTransactionRepresentation[][] logs = new CommittedTransactionRepresentation[transactionCounts.length][];
    for (int logVersion = 0; logVersion < transactionCounts.length; logVersion++) {
        logs[logVersion] = transactions(transactionCounts[logVersion], txId);
    }
    when(result.apply(any(LogPosition.class))).thenAnswer(invocation -> {
        LogPosition position = invocation.getArgument(0);
        if (position == null) {
            // A mockito issue when calling the "when" methods, I believe
            return null;
        }
        // For simplicity the offset means, in this test, the array offset
        CommittedTransactionRepresentation[] transactions = logs[toIntExact(position.getLogVersion())];
        CommittedTransactionRepresentation[] subset = copyOfRange(transactions, toIntExact(position.getByteOffset() - baseOffset), transactions.length);
        ArrayUtil.reverse(subset);
        return given(subset);
    });
    return result;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) 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 7 with TransactionCursor

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

the class ReversedMultiFileTransactionCursorTest method shouldHandleEmptyLogsMidStream.

@Test
void shouldHandleEmptyLogsMidStream() throws Exception {
    // GIVEN
    TransactionCursor cursor = new ReversedMultiFileTransactionCursor(logFile, log(5, 0, 2, 0, 3), 4, start());
    // WHEN
    CommittedTransactionRepresentation[] reversed = exhaust(cursor);
    // THEN
    assertTransactionRange(reversed, 5 + 2 + 3, 0);
}
Also used : CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) TransactionCursor(org.neo4j.kernel.impl.transaction.log.TransactionCursor) Test(org.junit.jupiter.api.Test)

Example 8 with TransactionCursor

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

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

the class ResponsePackerTest method shouldHaveFixedTargetTransactionIdEvenIfLastTransactionIdIsMoving.

@Test
public void shouldHaveFixedTargetTransactionIdEvenIfLastTransactionIdIsMoving() throws Exception {
    // GIVEN
    LogicalTransactionStore transactionStore = mock(LogicalTransactionStore.class);
    long lastAppliedTransactionId = 5L;
    TransactionCursor endlessCursor = new EndlessCursor(lastAppliedTransactionId + 1);
    when(transactionStore.getTransactions(anyLong())).thenReturn(endlessCursor);
    final long targetTransactionId = 8L;
    final TransactionIdStore transactionIdStore = new DeadSimpleTransactionIdStore(targetTransactionId, 0, BASE_TX_COMMIT_TIMESTAMP, 0, 0);
    ResponsePacker packer = new ResponsePacker(transactionStore, transactionIdStore, Suppliers.singleton(StoreIdTestFactory.newStoreIdForCurrentVersion()));
    // WHEN
    Response<Object> response = packer.packTransactionStreamResponse(requestContextStartingAt(5L), null);
    final AtomicLong nextExpectedVisit = new AtomicLong(lastAppliedTransactionId);
    response.accept(new Response.Handler() {

        @Override
        public void obligation(long txId) throws IOException {
            fail("Should not be called");
        }

        @Override
        public Visitor<CommittedTransactionRepresentation, Exception> transactions() {
            return new Visitor<CommittedTransactionRepresentation, Exception>() {

                @Override
                public boolean visit(CommittedTransactionRepresentation element) {
                    // THEN
                    long txId = element.getCommitEntry().getTxId();
                    assertThat(txId, lessThanOrEqualTo(targetTransactionId));
                    assertEquals(nextExpectedVisit.incrementAndGet(), txId);
                    // Move the target transaction id forward one step, effectively always keeping it out of reach
                    transactionIdStore.setLastCommittedAndClosedTransactionId(transactionIdStore.getLastCommittedTransactionId() + 1, 0, BASE_TX_COMMIT_TIMESTAMP, 3, 4);
                    return true;
                }
            };
        }
    });
}
Also used : CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) DeadSimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.DeadSimpleTransactionIdStore) Visitor(org.neo4j.helpers.collection.Visitor) DeadSimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.DeadSimpleTransactionIdStore) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) IOException(java.io.IOException) IOException(java.io.IOException) Response(org.neo4j.com.Response) AtomicLong(java.util.concurrent.atomic.AtomicLong) TransactionCursor(org.neo4j.kernel.impl.transaction.log.TransactionCursor) Test(org.junit.Test)

Example 10 with TransactionCursor

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

the class RemoteStore method getPullIndex.

/**
     * Later stages of the startup process require at least one transaction to
     * figure out the mapping between the transaction log and the consensus log.
     *
     * If there are no transaction logs then we can pull from and including
     * the index which the metadata store points to. This would be the case
     * for example with a backup taken during an idle period of the system.
     *
     * However, if there are transaction logs then we want to find out where
     * they end and pull from there, excluding the last one so that we do not
     * get duplicate entries.
     */
private long getPullIndex(File storeDir) throws IOException {
    /* this is the metadata store */
    ReadOnlyTransactionIdStore txIdStore = new ReadOnlyTransactionIdStore(pageCache, storeDir);
    /* Clean as in clean shutdown. Without transaction logs this should be the truth,
        * but otherwise it can be used as a starting point for scanning the logs. */
    long lastCleanTxId = txIdStore.getLastCommittedTransactionId();
    /* these are the transaction logs */
    ReadOnlyTransactionStore txStore = new ReadOnlyTransactionStore(pageCache, fs, storeDir, new Monitors());
    long lastTxId = BASE_TX_ID;
    try (Lifespan ignored = new Lifespan(txStore)) {
        TransactionCursor cursor;
        try {
            cursor = txStore.getTransactions(lastCleanTxId);
        } catch (NoSuchTransactionException e) {
            log.info("No transaction logs found. Will use metadata store as base for pull request.");
            return lastCleanTxId;
        }
        while (cursor.next()) {
            CommittedTransactionRepresentation tx = cursor.get();
            lastTxId = tx.getCommitEntry().getTxId();
        }
        if (lastTxId < lastCleanTxId) {
            throw new IllegalStateException("Metadata index was higher than transaction log index.");
        }
        // we don't want to pull a transaction we already have in the log, hence +1
        return lastTxId + 1;
    }
}
Also used : CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) ReadOnlyTransactionIdStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore) TransactionCursor(org.neo4j.kernel.impl.transaction.log.TransactionCursor) Monitors(org.neo4j.kernel.monitoring.Monitors) NoSuchTransactionException(org.neo4j.kernel.impl.transaction.log.NoSuchTransactionException) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore)

Aggregations

TransactionCursor (org.neo4j.kernel.impl.transaction.log.TransactionCursor)15 CommittedTransactionRepresentation (org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation)13 Test (org.junit.jupiter.api.Test)6 LogicalTransactionStore (org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore)6 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Test (org.junit.Test)1 AfterEach (org.junit.jupiter.api.AfterEach)1 Response (org.neo4j.com.Response)1 Visitor (org.neo4j.helpers.collection.Visitor)1 Command (org.neo4j.internal.recordstorage.Command)1 KernelVersion (org.neo4j.kernel.KernelVersion)1 DeadSimpleTransactionIdStore (org.neo4j.kernel.impl.transaction.DeadSimpleTransactionIdStore)1 NoSuchTransactionException (org.neo4j.kernel.impl.transaction.log.NoSuchTransactionException)1 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)1 ReadOnlyTransactionIdStore (org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore)1 ReadOnlyTransactionStore (org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore)1