Search in sources :

Example 1 with TransientFailureException

use of org.neo4j.graphdb.TransientFailureException in project neo4j by neo4j.

the class TopLevelTransactionTest method shouldLetThroughTransientFailureException.

@Test
public void shouldLetThroughTransientFailureException() throws Exception {
    // GIVEN
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    when(kernelTransaction.isOpen()).thenReturn(true);
    doThrow(new TransientDatabaseFailureException("Just a random failure")).when(kernelTransaction).close();
    ThreadToStatementContextBridge bridge = new ThreadToStatementContextBridge();
    TopLevelTransaction transaction = new TopLevelTransaction(kernelTransaction, bridge);
    // WHEN
    transaction.success();
    try {
        transaction.close();
        fail("Should have failed");
    } catch (TransientFailureException e) {
    // THEN Good
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransientFailureException(org.neo4j.graphdb.TransientFailureException) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) TransientDatabaseFailureException(org.neo4j.graphdb.TransientDatabaseFailureException) TopLevelTransaction(org.neo4j.kernel.impl.coreapi.TopLevelTransaction) Test(org.junit.Test)

Example 2 with TransientFailureException

use of org.neo4j.graphdb.TransientFailureException 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)

Example 3 with TransientFailureException

use of org.neo4j.graphdb.TransientFailureException in project neo4j by neo4j.

the class ConcurrentCreateDropIndexIT method concurrentCreatingUniquenessConstraintOnNonUniqueData.

@Test
void concurrentCreatingUniquenessConstraintOnNonUniqueData() throws Throwable {
    // given
    Label label = label(0);
    try (Transaction tx = db.beginTx()) {
        for (int i = 0; i < 2; i++) {
            tx.createNode(label).setProperty(KEY, "A");
        }
        tx.commit();
    }
    Race race = new Race().withMaxDuration(10, SECONDS);
    race.addContestants(3, () -> {
        try (Transaction tx = db.beginTx()) {
            tx.schema().constraintFor(label).assertPropertyIsUnique(KEY).create();
            tx.commit();
        } catch (TransientFailureException | ConstraintViolationException e) {
        // It's OK
        }
    }, 100);
    // when
    race.go();
    try (Transaction tx = db.beginTx()) {
        // then
        ConstraintDefinition constraint = singleOrNull(tx.schema().getConstraints(label));
        assertNull(constraint);
        IndexDefinition index = singleOrNull(tx.schema().getIndexes(label));
        assertNull(index);
        tx.commit();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) TransientFailureException(org.neo4j.graphdb.TransientFailureException) Race(org.neo4j.test.Race) Label(org.neo4j.graphdb.Label) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.jupiter.api.Test)

Example 4 with TransientFailureException

use of org.neo4j.graphdb.TransientFailureException in project neo4j by neo4j.

the class TransactionImplTest method shouldLetThroughTransientFailureException.

@Test
void shouldLetThroughTransientFailureException() throws Exception {
    // GIVEN
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    when(kernelTransaction.isOpen()).thenReturn(true);
    doThrow(new TransientFailureException("Just a random failure") {

        // @Override
        public Status status() {
            return null;
        }
    }).when(kernelTransaction).close();
    TransactionImpl transaction = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null);
    // WHEN
    transaction.commit();
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransientFailureException(org.neo4j.graphdb.TransientFailureException) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) Test(org.junit.jupiter.api.Test)

Example 5 with TransientFailureException

use of org.neo4j.graphdb.TransientFailureException in project neo4j by neo4j.

the class ConcurrentCreateDropIndexIT method concurrentCreatingUniquenessConstraint.

@Test
void concurrentCreatingUniquenessConstraint() throws Throwable {
    // given
    Race race = new Race().withMaxDuration(10, SECONDS);
    Label label = label(0);
    race.addContestants(10, () -> {
        try (Transaction tx = db.beginTx()) {
            tx.schema().constraintFor(label).assertPropertyIsUnique(KEY).create();
            tx.commit();
        } catch (TransientFailureException | ConstraintViolationException e) {
        // It's OK
        }
    }, 300);
    // when
    race.go();
    try (Transaction tx = db.beginTx()) {
        // then
        ConstraintDefinition constraint = single(tx.schema().getConstraints(label));
        assertNotNull(constraint);
        IndexDefinition index = single(tx.schema().getIndexes(label));
        assertNotNull(index);
        tx.commit();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) TransientFailureException(org.neo4j.graphdb.TransientFailureException) Race(org.neo4j.test.Race) Label(org.neo4j.graphdb.Label) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.jupiter.api.Test)

Aggregations

TransientFailureException (org.neo4j.graphdb.TransientFailureException)5 Test (org.junit.jupiter.api.Test)3 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)2 Label (org.neo4j.graphdb.Label)2 Transaction (org.neo4j.graphdb.Transaction)2 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 Status (org.neo4j.kernel.api.exceptions.Status)2 Race (org.neo4j.test.Race)2 Test (org.junit.Test)1 TransientDatabaseFailureException (org.neo4j.graphdb.TransientDatabaseFailureException)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 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)1 TopLevelTransaction (org.neo4j.kernel.impl.coreapi.TopLevelTransaction)1 TransactionImpl (org.neo4j.kernel.impl.coreapi.TransactionImpl)1 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)1 CommitEvent (org.neo4j.kernel.impl.transaction.tracing.CommitEvent)1 TransactionListenersState (org.neo4j.kernel.internal.event.TransactionListenersState)1