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