Search in sources :

Example 1 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class ReplayableCommitProcessTest method shouldCommitTransactions.

@Test
public void shouldCommitTransactions() throws Exception {
    // given
    TransactionToApply newTx1 = mock(TransactionToApply.class);
    TransactionToApply newTx2 = mock(TransactionToApply.class);
    TransactionToApply newTx3 = mock(TransactionToApply.class);
    StubLocalDatabase localDatabase = new StubLocalDatabase(1);
    final ReplayableCommitProcess txListener = new ReplayableCommitProcess(localDatabase, localDatabase);
    // when
    txListener.commit(newTx1, NULL, EXTERNAL);
    txListener.commit(newTx2, NULL, EXTERNAL);
    txListener.commit(newTx3, NULL, EXTERNAL);
    // then
    verify(localDatabase.commitProcess, times(3)).commit(any(TransactionToApply.class), any(CommitEvent.class), any(TransactionApplicationMode.class));
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) TransactionApplicationMode(org.neo4j.storageengine.api.TransactionApplicationMode) CommitEvent(org.neo4j.kernel.impl.transaction.tracing.CommitEvent) Test(org.junit.Test)

Example 2 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class ReplicatedTransactionStateMachineTest method createFakeTransactionCommitProcess.

private TransactionCommitProcess createFakeTransactionCommitProcess(long txId) throws TransactionFailureException {
    TransactionCommitProcess localCommitProcess = mock(TransactionCommitProcess.class);
    when(localCommitProcess.commit(any(TransactionToApply.class), any(CommitEvent.class), any(TransactionApplicationMode.class))).thenAnswer(invocation -> {
        TransactionToApply txToApply = (TransactionToApply) invocation.getArguments()[0];
        txToApply.commitment(new FakeCommitment(txId, mock(TransactionIdStore.class)), txId);
        txToApply.commitment().publishAsCommitted();
        txToApply.commitment().publishAsClosed();
        txToApply.close();
        return txId;
    });
    return localCommitProcess;
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) TransactionCommitProcess(org.neo4j.kernel.impl.api.TransactionCommitProcess) TransactionApplicationMode(org.neo4j.storageengine.api.TransactionApplicationMode) CommitEvent(org.neo4j.kernel.impl.transaction.tracing.CommitEvent) FakeCommitment(org.neo4j.kernel.impl.transaction.log.FakeCommitment)

Example 3 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class ApplyTransactionsCommand method applyTransactions.

private long applyTransactions(File fromPath, GraphDatabaseAPI toDb, long fromTxExclusive, long toTxInclusive, PrintStream out) throws IOException, TransactionFailureException {
    DependencyResolver resolver = toDb.getDependencyResolver();
    TransactionRepresentationCommitProcess commitProcess = new TransactionRepresentationCommitProcess(resolver.resolveDependency(TransactionAppender.class), resolver.resolveDependency(StorageEngine.class));
    LifeSupport life = new LifeSupport();
    try (DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
        PageCache pageCache = StandalonePageCacheFactory.createPageCache(fileSystem)) {
        LogicalTransactionStore source = life.add(new ReadOnlyTransactionStore(pageCache, fileSystem, fromPath, new Monitors()));
        life.start();
        long lastAppliedTx = fromTxExclusive;
        // Some progress if there are more than a couple of transactions to apply
        ProgressListener progress = toTxInclusive - fromTxExclusive >= 100 ? textual(out).singlePart("Application progress", toTxInclusive - fromTxExclusive) : ProgressListener.NONE;
        try (IOCursor<CommittedTransactionRepresentation> cursor = source.getTransactions(fromTxExclusive + 1)) {
            while (cursor.next()) {
                CommittedTransactionRepresentation transaction = cursor.get();
                TransactionRepresentation transactionRepresentation = transaction.getTransactionRepresentation();
                try {
                    commitProcess.commit(new TransactionToApply(transactionRepresentation), NULL, EXTERNAL);
                    progress.add(1);
                } catch (final Throwable e) {
                    System.err.println("ERROR applying transaction " + transaction.getCommitEntry().getTxId());
                    throw e;
                }
                lastAppliedTx = transaction.getCommitEntry().getTxId();
                if (lastAppliedTx == toTxInclusive) {
                    break;
                }
            }
        }
        return lastAppliedTx;
    } finally {
        life.shutdown();
    }
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) TransactionAppender(org.neo4j.kernel.impl.transaction.log.TransactionAppender) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) StorageEngine(org.neo4j.storageengine.api.StorageEngine) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore) DependencyResolver(org.neo4j.graphdb.DependencyResolver) ProgressListener(org.neo4j.helpers.progress.ProgressListener) TransactionRepresentationCommitProcess(org.neo4j.kernel.impl.api.TransactionRepresentationCommitProcess) Monitors(org.neo4j.kernel.monitoring.Monitors) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) PageCache(org.neo4j.io.pagecache.PageCache)

Example 4 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class BatchingTxApplier method queue.

/**
     * Queues a transaction for application.
     *
     * @param tx The transaction to be queued for application.
     */
public void queue(CommittedTransactionRepresentation tx) throws Exception {
    long receivedTxId = tx.getCommitEntry().getTxId();
    long expectedTxId = lastQueuedTxId + 1;
    if (receivedTxId != expectedTxId) {
        log.warn("Out of order transaction. Received: %d Expected: %d", receivedTxId, expectedTxId);
        return;
    }
    txQueue.queue(new TransactionToApply(tx.getTransactionRepresentation(), receivedTxId));
    if (!stopped) {
        lastQueuedTxId = receivedTxId;
        monitor.txPullResponse(receivedTxId);
    }
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply)

Example 5 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class ReplicatedTransactionStateMachine method applyCommand.

@Override
public synchronized void applyCommand(ReplicatedTransaction replicatedTx, long commandIndex, Consumer<Result> callback) {
    if (commandIndex <= lastCommittedIndex) {
        log.debug("Ignoring transaction at log index %d since already committed up to %d", commandIndex, lastCommittedIndex);
        return;
    }
    TransactionRepresentation tx;
    byte[] extraHeader = encodeLogIndexAsTxHeader(commandIndex);
    tx = ReplicatedTransactionFactory.extractTransactionRepresentation(replicatedTx, extraHeader);
    int currentTokenId = lockTokenStateMachine.currentToken().id();
    int txLockSessionId = tx.getLockSessionId();
    if (currentTokenId != txLockSessionId && txLockSessionId != Locks.Client.NO_LOCK_SESSION_ID) {
        callback.accept(Result.of(new TransactionFailureException(LockSessionExpired, "The lock session in the cluster has changed: [current lock session id:%d, tx lock session id:%d]", currentTokenId, txLockSessionId)));
    } else {
        try {
            TransactionToApply transaction = new TransactionToApply(tx);
            transaction.onClose(txId -> callback.accept(Result.of(txId)));
            queue.queue(transaction);
        } catch (Exception e) {
            throw panicException(e);
        }
    }
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) IOException(java.io.IOException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException)

Aggregations

TransactionToApply (org.neo4j.kernel.impl.api.TransactionToApply)46 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)14 Test (org.junit.jupiter.api.Test)11 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)11 CommittedTransactionRepresentation (org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation)9 Test (org.junit.Test)7 IOException (java.io.IOException)6 CursorContext (org.neo4j.io.pagecache.context.CursorContext)6 DbmsLogEntryWriterFactory (org.neo4j.kernel.database.DbmsLogEntryWriterFactory)6 TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)4 TransactionId (org.neo4j.storageengine.api.TransactionId)4 TransactionCommitProcess (org.neo4j.kernel.impl.api.TransactionCommitProcess)3 CommitEvent (org.neo4j.kernel.impl.transaction.tracing.CommitEvent)3 ArrayList (java.util.ArrayList)2 DefaultPageCacheTracer (org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer)2 TransactionApplier (org.neo4j.kernel.impl.api.TransactionApplier)2 LockGroup (org.neo4j.kernel.impl.locking.LockGroup)2 FakeCommitment (org.neo4j.kernel.impl.transaction.log.FakeCommitment)2 LogEntryCommit (org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit)2 LogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader)2