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);
}
}
use of org.neo4j.kernel.api.exceptions.TransactionFailureException in project neo4j by neo4j.
the class ReplicatedTransactionStateMachine method applyCommand.
@Override
public synchronized void applyCommand(ReplicatedTransaction replicatedTx, long commandIndex, Consumer<Result> callback) {
if (commandIndex <= lastCommittedIndex) {
log.debug("Ignoring transaction at log index %d since already committed up to %d", commandIndex, lastCommittedIndex);
return;
}
TransactionRepresentation tx;
byte[] extraHeader = encodeLogIndexAsTxHeader(commandIndex);
tx = ReplicatedTransactionFactory.extractTransactionRepresentation(replicatedTx, extraHeader);
int currentTokenId = lockTokenStateMachine.currentToken().id();
int txLockSessionId = tx.getLockSessionId();
if (currentTokenId != txLockSessionId && txLockSessionId != Locks.Client.NO_LOCK_SESSION_ID) {
callback.accept(Result.of(new TransactionFailureException(LockSessionExpired, "The lock session in the cluster has changed: [current lock session id:%d, tx lock session id:%d]", currentTokenId, txLockSessionId)));
} else {
try {
TransactionToApply transaction = new TransactionToApply(tx);
transaction.onClose(txId -> callback.accept(Result.of(txId)));
queue.queue(transaction);
} catch (Exception e) {
throw panicException(e);
}
}
}
use of org.neo4j.kernel.api.exceptions.TransactionFailureException in project neo4j by neo4j.
the class ReplicatedTokenHolder method createCommands.
private byte[] createCommands(String tokenName) {
StorageEngine storageEngine = dependencies.resolveDependency(StorageEngine.class);
Collection<StorageCommand> commands = new ArrayList<>();
TransactionState txState = new TxState();
int tokenId = Math.toIntExact(idGeneratorFactory.get(tokenIdType).nextId());
createToken(txState, tokenName, tokenId);
try (StorageStatement statement = storageEngine.storeReadLayer().newStatement()) {
storageEngine.createCommands(commands, txState, statement, ResourceLocker.NONE, Long.MAX_VALUE);
} catch (CreateConstraintFailureException | TransactionFailureException | ConstraintValidationException e) {
throw new RuntimeException("Unable to create token '" + tokenName + "'", e);
}
return ReplicatedTokenRequestSerializer.commandBytes(commands);
}
use of org.neo4j.kernel.api.exceptions.TransactionFailureException in project neo4j by neo4j.
the class SlaveTransactionCommitProcessTest method mustTranslateIOExceptionsToKernelTransactionFailures.
@Test
public void mustTranslateIOExceptionsToKernelTransactionFailures() throws Exception {
when(master.commit(requestContext, tx)).thenThrow(new IOException());
try {
commitProcess.commit(new TransactionToApply(tx), CommitEvent.NULL, TransactionApplicationMode.INTERNAL);
fail("commit should have thrown");
} catch (TransactionFailureException e) {
assertThat(e.status(), is((Status) Status.Transaction.TransactionCommitFailed));
}
}
Aggregations