Search in sources :

Example 1 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 2 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)

Example 3 with TransactionFailureException

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);
        }
    }
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) IOException(java.io.IOException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException)

Example 4 with TransactionFailureException

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);
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) StorageStatement(org.neo4j.storageengine.api.StorageStatement) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException) ArrayList(java.util.ArrayList) StorageEngine(org.neo4j.storageengine.api.StorageEngine) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) TxState(org.neo4j.kernel.impl.api.state.TxState) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 5 with TransactionFailureException

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));
    }
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)25 Test (org.junit.Test)10 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)9 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 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 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)2 StorageCommand (org.neo4j.storageengine.api.StorageCommand)2 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CatchupPollingProcess (org.neo4j.causalclustering.catchup.tx.CatchupPollingProcess)1 CoreGraphDatabase (org.neo4j.causalclustering.core.CoreGraphDatabase)1