Search in sources :

Example 6 with TransactionFailureException

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

the class InternalTransactionCommitProcessTest method shouldFailWithProperMessageOnAppendException.

@Test
void shouldFailWithProperMessageOnAppendException() throws Exception {
    // GIVEN
    TransactionAppender appender = mock(TransactionAppender.class);
    IOException rootCause = new IOException("Mock exception");
    doThrow(new IOException(rootCause)).when(appender).append(any(TransactionToApply.class), any(LogAppendEvent.class));
    StorageEngine storageEngine = mock(StorageEngine.class);
    TransactionCommitProcess commitProcess = new InternalTransactionCommitProcess(appender, storageEngine);
    // WHEN
    TransactionFailureException exception = assertThrows(TransactionFailureException.class, () -> commitProcess.commit(mockedTransaction(), commitEvent, INTERNAL));
    assertThat(exception.getMessage()).contains("Could not append transaction representation to log");
    assertTrue(contains(exception, rootCause.getMessage(), rootCause.getClass()));
}
Also used : TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) TestableTransactionAppender(org.neo4j.kernel.impl.transaction.log.TestableTransactionAppender) TransactionAppender(org.neo4j.kernel.impl.transaction.log.TransactionAppender) IOException(java.io.IOException) LogAppendEvent(org.neo4j.kernel.impl.transaction.tracing.LogAppendEvent) StorageEngine(org.neo4j.storageengine.api.StorageEngine) Test(org.junit.jupiter.api.Test)

Example 7 with TransactionFailureException

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

the class InternalTransactionCommitProcessTest method shouldCloseTransactionRegardlessOfWhetherOrNotItAppliedCorrectly.

@Test
void shouldCloseTransactionRegardlessOfWhetherOrNotItAppliedCorrectly() throws Exception {
    // GIVEN
    TransactionIdStore transactionIdStore = mock(TransactionIdStore.class);
    TransactionAppender appender = new TestableTransactionAppender(transactionIdStore);
    long txId = 11;
    when(transactionIdStore.nextCommittingTransactionId()).thenReturn(txId);
    IOException rootCause = new IOException("Mock exception");
    StorageEngine storageEngine = mock(StorageEngine.class);
    doThrow(new IOException(rootCause)).when(storageEngine).apply(any(TransactionToApply.class), any(TransactionApplicationMode.class));
    TransactionCommitProcess commitProcess = new InternalTransactionCommitProcess(appender, storageEngine);
    TransactionToApply transaction = mockedTransaction();
    // WHEN
    TransactionFailureException exception = assertThrows(TransactionFailureException.class, () -> commitProcess.commit(transaction, commitEvent, INTERNAL));
    assertThat(exception.getMessage()).contains("Could not apply the transaction to the store");
    assertTrue(contains(exception, rootCause.getMessage(), rootCause.getClass()));
    // THEN
    // we can't verify transactionCommitted since that's part of the TransactionAppender, which we have mocked
    verify(transactionIdStore).transactionClosed(eq(txId), anyLong(), anyLong(), any(CursorContext.class));
}
Also used : TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) TransactionIdStore(org.neo4j.storageengine.api.TransactionIdStore) TestableTransactionAppender(org.neo4j.kernel.impl.transaction.log.TestableTransactionAppender) TestableTransactionAppender(org.neo4j.kernel.impl.transaction.log.TestableTransactionAppender) TransactionAppender(org.neo4j.kernel.impl.transaction.log.TransactionAppender) TransactionApplicationMode(org.neo4j.storageengine.api.TransactionApplicationMode) IOException(java.io.IOException) CursorContext(org.neo4j.io.pagecache.context.CursorContext) StorageEngine(org.neo4j.storageengine.api.StorageEngine) Test(org.junit.jupiter.api.Test)

Example 8 with TransactionFailureException

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

the class KernelTransactionsTest method trackNumberOfActiveTransactionFromMultipleThreads.

@Test
void trackNumberOfActiveTransactionFromMultipleThreads() throws Throwable {
    KernelTransactions kernelTransactions = newKernelTransactions();
    int expectedTransactions = 100;
    Phaser phaser = new Phaser(expectedTransactions);
    List<Future<Void>> transactionFutures = new ArrayList<>();
    for (int i = 0; i < expectedTransactions; i++) {
        Future transactionFuture = executorService.submit(() -> {
            try (KernelTransaction ignored = kernelTransactions.newInstance(EXPLICIT, access(), EMBEDDED_CONNECTION, 0L)) {
                phaser.arriveAndAwaitAdvance();
                assertEquals(expectedTransactions, kernelTransactions.getNumberOfActiveTransactions());
                phaser.arriveAndAwaitAdvance();
            } catch (TransactionFailureException e) {
                throw new RuntimeException(e);
            }
            phaser.arriveAndDeregister();
        });
        transactionFutures.add(transactionFuture);
    }
    combine(transactionFutures).get();
    assertEquals(0, kernelTransactions.getNumberOfActiveTransactions());
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Phaser(java.util.concurrent.Phaser) Test(org.junit.jupiter.api.Test)

Example 9 with TransactionFailureException

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

the class KernelTransactionsTest method shouldBeAbleToSnapshotDuringHeavyLoad.

@Test
void shouldBeAbleToSnapshotDuringHeavyLoad() throws Throwable {
    // GIVEN
    final KernelTransactions transactions = newKernelTransactions();
    Race race = new Race();
    final int threads = 50;
    final AtomicBoolean end = new AtomicBoolean();
    final AtomicReferenceArray<IdController.ConditionSnapshot> 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 ignored = 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);
            IdController.ConditionSnapshot snapshot = snapshots.get(threadIndex);
            if (snapshot != null && snapshot.conditionMet()) {
                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 : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) Race(org.neo4j.test.Race) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IdController(org.neo4j.internal.id.IdController) Test(org.junit.jupiter.api.Test)

Example 10 with TransactionFailureException

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

the class TransactionImplTest method shouldThrowTransientExceptionOnTransientKernelException.

@Test
void shouldThrowTransientExceptionOnTransientKernelException() throws Exception {
    // GIVEN
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    when(kernelTransaction.isOpen()).thenReturn(true);
    doThrow(new TransactionFailureException(Status.Transaction.ConstraintsChanged, "Proving that transaction does the right thing")).when(kernelTransaction).close();
    TransactionImpl transaction = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null);
    // WHEN
    transaction.commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)11 Test (org.junit.jupiter.api.Test)5 IOException (java.io.IOException)3 KernelException (org.neo4j.exceptions.KernelException)3 ConstraintViolationTransactionFailureException (org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException)3 CreateConstraintFailureException (org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)3 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)3 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)3 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)2 UniquePropertyValueValidationException (org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException)2 TestableTransactionAppender (org.neo4j.kernel.impl.transaction.log.TestableTransactionAppender)2 TransactionAppender (org.neo4j.kernel.impl.transaction.log.TransactionAppender)2 StorageEngine (org.neo4j.storageengine.api.StorageEngine)2 ArrayList (java.util.ArrayList)1 Future (java.util.concurrent.Future)1 Phaser (java.util.concurrent.Phaser)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReferenceArray (java.util.concurrent.atomic.AtomicReferenceArray)1 BoltTransaction (org.neo4j.bolt.dbapi.BoltTransaction)1