Search in sources :

Example 6 with TransactionFailureException

use of org.neo4j.kernel.api.exceptions.TransactionFailureException in project neo4j by neo4j.

the class KernelTransactionsTest method shouldBeAbleToSnapshotDuringHeavyLoad.

@Test
public void shouldBeAbleToSnapshotDuringHeavyLoad() throws Throwable {
    // GIVEN
    final KernelTransactions transactions = newKernelTransactions();
    Race race = new Race();
    final int threads = 50;
    final AtomicBoolean end = new AtomicBoolean();
    final AtomicReferenceArray<KernelTransactionsSnapshot> snapshots = new AtomicReferenceArray<>(threads);
    // Representing "transaction" threads
    for (int i = 0; i < threads; i++) {
        final int threadIndex = i;
        race.addContestant(() -> {
            ThreadLocalRandom random = ThreadLocalRandom.current();
            while (!end.get()) {
                try (KernelTransaction transaction = getKernelTransaction(transactions)) {
                    parkNanos(MILLISECONDS.toNanos(random.nextInt(3)));
                    if (snapshots.get(threadIndex) == null) {
                        snapshots.set(threadIndex, transactions.get());
                        parkNanos(MILLISECONDS.toNanos(random.nextInt(3)));
                    }
                } catch (TransactionFailureException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    // Just checks snapshots
    race.addContestant(() -> {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        int snapshotsLeft = 1_000;
        while (snapshotsLeft > 0) {
            int threadIndex = random.nextInt(threads);
            KernelTransactionsSnapshot snapshot = snapshots.get(threadIndex);
            if (snapshot != null && snapshot.allClosed()) {
                snapshotsLeft--;
                snapshots.set(threadIndex, null);
            }
        }
        // End condition of this test can be described as:
        //   when 1000 snapshots have been seen as closed.
        // setting this boolean to true will have all other threads end as well so that race.go() will end
        end.set(true);
    });
    // WHEN
    race.go();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) Race(org.neo4j.test.Race) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Test(org.junit.Test)

Example 7 with TransactionFailureException

use of org.neo4j.kernel.api.exceptions.TransactionFailureException in project neo4j by neo4j.

the class KernelTransactionImplementationTest method shouldRollbackAndThrowOnFailedAndSuccess.

@Test
public void shouldRollbackAndThrowOnFailedAndSuccess() throws Exception {
    // GIVEN
    boolean exceptionReceived = false;
    try (KernelTransaction transaction = newTransaction(securityContext())) {
        // WHEN
        transactionInitializer.accept(transaction);
        transaction.failure();
        transaction.success();
    } catch (TransactionFailureException e) {
        // Expected.
        exceptionReceived = true;
    }
    // THEN
    assertTrue(exceptionReceived);
    verify(transactionMonitor, times(1)).transactionFinished(false, isWriteTx);
    verifyExtraInteractionWithTheMonitor(transactionMonitor, isWriteTx);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) Test(org.junit.Test)

Example 8 with TransactionFailureException

use of org.neo4j.kernel.api.exceptions.TransactionFailureException in project neo4j by neo4j.

the class MasterImpl method newLockSession.

@Override
public Response<Void> newLockSession(RequestContext context) throws TransactionFailureException {
    monitor.initializeTx(context);
    if (!spi.isAccessible()) {
        throw new TransactionFailureException(Status.General.DatabaseUnavailable, "Database is currently not available");
    }
    assertCorrectEpoch(context);
    try {
        conversationManager.begin(context);
    } catch (ConcurrentAccessException e) {
        throw new TransactionFailureException(Status.Transaction.TransactionAccessedConcurrently, e, "The lock session requested to start is already in use. " + "Please retry your request in a few seconds.");
    }
    return spi.packTransactionObligationResponse(context, null);
}
Also used : TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) ConcurrentAccessException(org.neo4j.kernel.impl.util.collection.ConcurrentAccessException)

Example 9 with TransactionFailureException

use of org.neo4j.kernel.api.exceptions.TransactionFailureException in project neo4j by neo4j.

the class ReplicatedTransactionStateMachineTest method shouldFailFutureForTransactionCommittedUnderWrongLockSession.

@Test
public void shouldFailFutureForTransactionCommittedUnderWrongLockSession() throws Exception {
    // given
    int txLockSessionId = 23;
    int currentLockSessionId = 24;
    ReplicatedTransaction tx = ReplicatedTransactionFactory.createImmutableReplicatedTransaction(physicalTx(txLockSessionId));
    TransactionCommitProcess localCommitProcess = mock(TransactionCommitProcess.class);
    final ReplicatedTransactionStateMachine stateMachine = new ReplicatedTransactionStateMachine(lockState(currentLockSessionId), batchSize, logProvider);
    stateMachine.installCommitProcess(localCommitProcess, -1L);
    AtomicBoolean called = new AtomicBoolean();
    // when
    stateMachine.applyCommand(tx, 0, result -> {
        called.set(true);
        try {
            result.consume();
            fail("should have thrown");
        } catch (TransactionFailureException tfe) {
            assertEquals(Status.Transaction.LockSessionExpired, tfe.status());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    stateMachine.ensuredApplied();
    assertTrue(called.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) TransactionCommitProcess(org.neo4j.kernel.impl.api.TransactionCommitProcess) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) Test(org.junit.Test)

Example 10 with TransactionFailureException

use of org.neo4j.kernel.api.exceptions.TransactionFailureException in project neo4j by neo4j.

the class ReplicatedTransactionCommitProcess method commit.

@Override
public long commit(final TransactionToApply tx, final CommitEvent commitEvent, TransactionApplicationMode mode) throws TransactionFailureException {
    ReplicatedTransaction transaction = createImmutableReplicatedTransaction(tx.transactionRepresentation());
    Future<Object> futureTxId;
    try {
        futureTxId = replicator.replicate(transaction, true);
    } catch (InterruptedException e) {
        throw new TransactionFailureException("Interrupted replicating transaction.", e);
    } catch (NoLeaderFoundException e) {
        throw new TransactionFailureException("No leader found while replicating transaction.", e);
    }
    try {
        return (long) futureTxId.get();
    } catch (ExecutionException e) {
        if (e.getCause() instanceof TransactionFailureException) {
            throw (TransactionFailureException) e.getCause();
        }
        // TODO: Panic?
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        // TODO Wait for the transaction to possibly finish within a user configurable time, before aborting.
        throw new TransactionFailureException("Interrupted while waiting for txId", e);
    }
}
Also used : TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) ReplicatedTransactionFactory.createImmutableReplicatedTransaction(org.neo4j.causalclustering.core.state.machines.tx.ReplicatedTransactionFactory.createImmutableReplicatedTransaction) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)26 Test (org.junit.Test)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 IOException (java.io.IOException)5 TransientTransactionFailureException (org.neo4j.graphdb.TransientTransactionFailureException)4 TransactionToApply (org.neo4j.kernel.impl.api.TransactionToApply)4 CreateConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)3 StorageEngine (org.neo4j.storageengine.api.StorageEngine)3 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 RequestContext (org.neo4j.com.RequestContext)2 Statement (org.neo4j.kernel.api.Statement)2 ConstraintValidationException (org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException)2 TransactionCommitProcess (org.neo4j.kernel.impl.api.TransactionCommitProcess)2 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)2 StorageCommand (org.neo4j.storageengine.api.StorageCommand)2 ExecutionException (java.util.concurrent.ExecutionException)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicReferenceArray (java.util.concurrent.atomic.AtomicReferenceArray)1 CatchupPollingProcess (org.neo4j.causalclustering.catchup.tx.CatchupPollingProcess)1