Search in sources :

Example 11 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 12 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 13 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)

Example 14 with TransactionFailureException

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

the class ReadReplicaReplicationIT method transactionsShouldNotAppearOnTheReadReplicaWhilePollingIsPaused.

@Test
public void transactionsShouldNotAppearOnTheReadReplicaWhilePollingIsPaused() throws Throwable {
    // given
    Cluster cluster = clusterRule.startCluster();
    ReadReplicaGraphDatabase readReplicaGraphDatabase = cluster.findAnyReadReplica().database();
    CatchupPollingProcess pollingClient = readReplicaGraphDatabase.getDependencyResolver().resolveDependency(CatchupPollingProcess.class);
    pollingClient.stop();
    cluster.coreTx((coreGraphDatabase, transaction) -> {
        coreGraphDatabase.createNode();
        transaction.success();
    });
    CoreGraphDatabase leaderDatabase = cluster.awaitLeader().database();
    long transactionVisibleOnLeader = transactionIdTracker(leaderDatabase).newestEncounteredTxId();
    // when the poller is paused, transaction doesn't make it to the read replica
    try {
        transactionIdTracker(readReplicaGraphDatabase).awaitUpToDate(transactionVisibleOnLeader, ofSeconds(3));
        fail("should have thrown exception");
    } catch (TransactionFailureException e) {
    // expected timeout
    }
    // when the poller is resumed, it does make it to the read replica
    pollingClient.start();
    transactionIdTracker(readReplicaGraphDatabase).awaitUpToDate(transactionVisibleOnLeader, ofSeconds(3));
}
Also used : TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) CatchupPollingProcess(org.neo4j.causalclustering.catchup.tx.CatchupPollingProcess) Cluster(org.neo4j.causalclustering.discovery.Cluster) ReadReplicaGraphDatabase(org.neo4j.causalclustering.readreplica.ReadReplicaGraphDatabase) CoreGraphDatabase(org.neo4j.causalclustering.core.CoreGraphDatabase) Test(org.junit.Test)

Example 15 with TransactionFailureException

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

the class AbstractConstraintCreationIT method changedConstraintsShouldResultInTransientFailure.

@Test
public void changedConstraintsShouldResultInTransientFailure() throws InterruptedException {
    // Given
    Runnable constraintCreation = () -> {
        try (Transaction tx = db.beginTx()) {
            createConstraintInRunningTx(db, KEY, PROP);
            tx.success();
        }
    };
    // When
    try {
        try (Transaction tx = db.beginTx()) {
            Executors.newSingleThreadExecutor().submit(constraintCreation).get();
            db.createNode();
            tx.success();
        }
        fail("Exception expected");
    } catch (Exception e) {
        // Then
        assertThat(e, instanceOf(TransientTransactionFailureException.class));
        assertThat(e.getCause(), instanceOf(TransactionFailureException.class));
        TransactionFailureException cause = (TransactionFailureException) e.getCause();
        assertEquals(Status.Transaction.ConstraintsChanged, cause.status());
    }
}
Also used : TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) DropConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException) TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) NoSuchConstraintException(org.neo4j.kernel.api.exceptions.schema.NoSuchConstraintException) KernelException(org.neo4j.kernel.api.exceptions.KernelException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) Test(org.junit.Test)

Aggregations

TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)26 Test (org.junit.Test)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 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 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)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 StorageCommand (org.neo4j.storageengine.api.StorageCommand)2 ExecutionException (java.util.concurrent.ExecutionException)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicReferenceArray (java.util.concurrent.atomic.AtomicReferenceArray)1 CatchupPollingProcess (org.neo4j.causalclustering.catchup.tx.CatchupPollingProcess)1