Search in sources :

Example 1 with CommitEvent

use of org.neo4j.kernel.impl.transaction.tracing.CommitEvent in project neo4j by neo4j.

the class KernelTransactionImplementation method commit.

private long commit() throws TransactionFailureException {
    boolean success = false;
    long txId = READ_ONLY;
    try (CommitEvent commitEvent = transactionEvent.beginCommitEvent()) {
        // Trigger transaction "before" hooks.
        if (hasDataChanges()) {
            try {
                hooksState = hooks.beforeCommit(txState, this, storageEngine.storeReadLayer(), storageStatement);
                if (hooksState != null && hooksState.failed()) {
                    TransactionHookException cause = hooksState.failure();
                    throw new TransactionFailureException(Status.Transaction.TransactionHookFailed, cause, "");
                }
            } finally {
                beforeHookInvoked = true;
            }
        }
        // Convert changes into commands and commit
        if (hasChanges()) {
            // grab all optimistic locks now, locks can't be deferred any further
            statementLocks.prepareForCommit();
            // use pessimistic locks for the rest of the commit process, locks can't be deferred any further
            Locks.Client commitLocks = statementLocks.pessimistic();
            // Gather up commands from the various sources
            Collection<StorageCommand> extractedCommands = new ArrayList<>();
            storageEngine.createCommands(extractedCommands, txState, storageStatement, commitLocks, lastTransactionIdWhenStarted);
            if (hasLegacyIndexChanges()) {
                legacyIndexTransactionState.extractCommands(extractedCommands);
            }
            /* Here's the deal: we track a quick-to-access hasChanges in transaction state which is true
                 * if there are any changes imposed by this transaction. Some changes made inside a transaction undo
                 * previously made changes in that same transaction, and so at some point a transaction may have
                 * changes and at another point, after more changes seemingly,
                 * the transaction may not have any changes.
                 * However, to track that "undoing" of the changes is a bit tedious, intrusive and hard to maintain
                 * and get right.... So to really make sure the transaction has changes we re-check by looking if we
                 * have produced any commands to add to the logical log.
                 */
            if (!extractedCommands.isEmpty()) {
                // Finish up the whole transaction representation
                PhysicalTransactionRepresentation transactionRepresentation = new PhysicalTransactionRepresentation(extractedCommands);
                TransactionHeaderInformation headerInformation = headerInformationFactory.create();
                long timeCommitted = clock.millis();
                transactionRepresentation.setHeader(headerInformation.getAdditionalHeader(), headerInformation.getMasterId(), headerInformation.getAuthorId(), startTimeMillis, lastTransactionIdWhenStarted, timeCommitted, commitLocks.getLockSessionId());
                // Commit the transaction
                success = true;
                TransactionToApply batch = new TransactionToApply(transactionRepresentation);
                txId = transactionId = commitProcess.commit(batch, commitEvent, INTERNAL);
                commitTime = timeCommitted;
            }
        }
        success = true;
        return txId;
    } catch (ConstraintValidationException | CreateConstraintFailureException e) {
        throw new ConstraintViolationTransactionFailureException(e.getUserMessage(new KeyReadTokenNameLookup(currentTransactionOperations.keyReadOperations())), e);
    } finally {
        if (!success) {
            rollback();
        } else {
            afterCommit(txId);
        }
    }
}
Also used : ConstraintViolationTransactionFailureException(org.neo4j.kernel.api.exceptions.ConstraintViolationTransactionFailureException) KeyReadTokenNameLookup(org.neo4j.kernel.api.KeyReadTokenNameLookup) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException) ArrayList(java.util.ArrayList) Locks(org.neo4j.kernel.impl.locking.Locks) StatementLocks(org.neo4j.kernel.impl.locking.StatementLocks) TransactionHookException(org.neo4j.kernel.api.exceptions.TransactionHookException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) ConstraintViolationTransactionFailureException(org.neo4j.kernel.api.exceptions.ConstraintViolationTransactionFailureException) CommitEvent(org.neo4j.kernel.impl.transaction.tracing.CommitEvent) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 2 with CommitEvent

use of org.neo4j.kernel.impl.transaction.tracing.CommitEvent in project neo4j by neo4j.

the class KernelTransactionImplementation method commitTransaction.

private long commitTransaction() throws KernelException {
    boolean success = false;
    long txId = READ_ONLY_ID;
    TransactionListenersState listenersState = null;
    try (CommitEvent commitEvent = transactionEvent.beginCommitEvent()) {
        listenersState = eventListeners.beforeCommit(txState, this, storageReader);
        if (listenersState != null && listenersState.isFailed()) {
            Throwable cause = listenersState.failure();
            if (cause instanceof TransientFailureException) {
                throw (TransientFailureException) cause;
            }
            if (cause instanceof Status.HasStatus) {
                throw new TransactionFailureException(((Status.HasStatus) cause).status(), cause, cause.getMessage());
            }
            throw new TransactionFailureException(Status.Transaction.TransactionHookFailed, cause, cause.getMessage());
        }
        // Convert changes into commands and commit
        if (hasChanges()) {
            forceThawLocks();
            lockClient.prepareForCommit();
            // Gather up commands from the various sources
            HeapTrackingArrayList<StorageCommand> extractedCommands = HeapTrackingCollections.newArrayList(memoryTracker);
            storageEngine.createCommands(extractedCommands, txState, storageReader, commandCreationContext, lockClient, lockTracer(), lastTransactionIdWhenStarted, this::enforceConstraints, cursorContext, memoryTracker);
            /* Here's the deal: we track a quick-to-access hasChanges in transaction state which is true
                 * if there are any changes imposed by this transaction. Some changes made inside a transaction undo
                 * previously made changes in that same transaction, and so at some point a transaction may have
                 * changes and at another point, after more changes seemingly,
                 * the transaction may not have any changes.
                 * However, to track that "undoing" of the changes is a bit tedious, intrusive and hard to maintain
                 * and get right.... So to really make sure the transaction has changes we re-check by looking if we
                 * have produced any commands to add to the logical log.
                 */
            if (!extractedCommands.isEmpty()) {
                // Finish up the whole transaction representation
                PhysicalTransactionRepresentation transactionRepresentation = new PhysicalTransactionRepresentation(extractedCommands);
                long timeCommitted = clocks.systemClock().millis();
                transactionRepresentation.setHeader(EMPTY_BYTE_ARRAY, startTimeMillis, lastTransactionIdWhenStarted, timeCommitted, leaseClient.leaseId(), securityContext.subject());
                // Commit the transaction
                success = true;
                TransactionToApply batch = new TransactionToApply(transactionRepresentation, cursorContext);
                kernelTransactionMonitor.beforeApply();
                txId = commitProcess.commit(batch, commitEvent, INTERNAL);
                commitTime = timeCommitted;
            }
        }
        success = true;
        return txId;
    } catch (ConstraintValidationException | CreateConstraintFailureException e) {
        throw new ConstraintViolationTransactionFailureException(e.getUserMessage(tokenRead()), e);
    } finally {
        if (!success) {
            rollback(listenersState);
        } else {
            transactionId = txId;
            afterCommit(listenersState);
        }
        transactionMonitor.addHeapTransactionSize(memoryTracker.heapHighWaterMark());
        transactionMonitor.addNativeTransactionSize(memoryTracker.usedNativeMemory());
    }
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) ConstraintViolationTransactionFailureException(org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException) TransientFailureException(org.neo4j.graphdb.TransientFailureException) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ConstraintValidationException(org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) ConstraintViolationTransactionFailureException(org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException) CommitEvent(org.neo4j.kernel.impl.transaction.tracing.CommitEvent) TransactionListenersState(org.neo4j.kernel.internal.event.TransactionListenersState) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) CreateConstraintFailureException(org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)

Aggregations

PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)2 CommitEvent (org.neo4j.kernel.impl.transaction.tracing.CommitEvent)2 StorageCommand (org.neo4j.storageengine.api.StorageCommand)2 ArrayList (java.util.ArrayList)1 TransientFailureException (org.neo4j.graphdb.TransientFailureException)1 ConstraintViolationTransactionFailureException (org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException)1 TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)1 ConstraintValidationException (org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException)1 CreateConstraintFailureException (org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)1 KeyReadTokenNameLookup (org.neo4j.kernel.api.KeyReadTokenNameLookup)1 ConstraintViolationTransactionFailureException (org.neo4j.kernel.api.exceptions.ConstraintViolationTransactionFailureException)1 Status (org.neo4j.kernel.api.exceptions.Status)1 TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)1 TransactionHookException (org.neo4j.kernel.api.exceptions.TransactionHookException)1 ConstraintValidationException (org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException)1 CreateConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)1 Locks (org.neo4j.kernel.impl.locking.Locks)1 StatementLocks (org.neo4j.kernel.impl.locking.StatementLocks)1 TransactionListenersState (org.neo4j.kernel.internal.event.TransactionListenersState)1