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()));
}
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));
}
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());
}
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();
}
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();
}
Aggregations