Search in sources :

Example 1 with IgniteTxRollbackCheckedException

use of org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException in project ignite by apache.

the class GridCacheAdapter method syncOp.

/**
     * @param op Cache operation.
     * @param <T> Return type.
     * @return Operation result.
     * @throws IgniteCheckedException If operation failed.
     */
@SuppressWarnings({ "TypeMayBeWeakened", "ErrorNotRethrown", "AssignmentToCatchBlockParameter" })
@Nullable
private <T> T syncOp(SyncOp<T> op) throws IgniteCheckedException {
    checkJta();
    awaitLastFut();
    GridNearTxLocal tx = ctx.tm().threadLocalTx(ctx);
    if (tx == null || tx.implicit()) {
        TransactionConfiguration tCfg = CU.transactionConfiguration(ctx, ctx.kernalContext().config());
        CacheOperationContext opCtx = ctx.operationContextPerCall();
        int retries = opCtx != null && opCtx.noRetries() ? 1 : MAX_RETRIES;
        for (int i = 0; i < retries; i++) {
            tx = ctx.tm().newTx(true, op.single(), ctx.systemTx() ? ctx : null, OPTIMISTIC, READ_COMMITTED, tCfg.getDefaultTxTimeout(), !ctx.skipStore(), 0);
            assert tx != null;
            try {
                T t = op.op(tx);
                assert tx.done() : "Transaction is not done: " + tx;
                return t;
            } catch (IgniteInterruptedCheckedException | IgniteTxHeuristicCheckedException e) {
                throw e;
            } catch (IgniteCheckedException e) {
                if (!(e instanceof IgniteTxRollbackCheckedException)) {
                    try {
                        tx.rollback();
                        e = new IgniteTxRollbackCheckedException("Transaction has been rolled back: " + tx.xid(), e);
                    } catch (IgniteCheckedException | AssertionError | RuntimeException e1) {
                        U.error(log, "Failed to rollback transaction (cache may contain stale locks): " + tx, e1);
                        U.addLastCause(e, e1, log);
                    }
                }
                if (X.hasCause(e, ClusterTopologyCheckedException.class) && i != retries - 1) {
                    ClusterTopologyCheckedException topErr = e.getCause(ClusterTopologyCheckedException.class);
                    if (!(topErr instanceof ClusterTopologyServerNotFoundException)) {
                        AffinityTopologyVersion topVer = tx.topologyVersion();
                        assert topVer != null && topVer.topologyVersion() > 0 : tx;
                        ctx.affinity().affinityReadyFuture(topVer.topologyVersion() + 1).get();
                        continue;
                    }
                }
                throw e;
            } finally {
                ctx.tm().resetContext();
                if (ctx.isNear())
                    ctx.near().dht().context().tm().resetContext();
            }
        }
        // Should not happen.
        throw new IgniteCheckedException("Failed to perform cache operation (maximum number of retries exceeded).");
    } else
        return op.op(tx);
}
Also used : TransactionConfiguration(org.apache.ignite.configuration.TransactionConfiguration) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BROADCAST(org.apache.ignite.internal.GridClosureCallMode.BROADCAST) IGNITE_CACHE_RETRIES_COUNT(org.apache.ignite.IgniteSystemProperties.IGNITE_CACHE_RETRIES_COUNT) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with IgniteTxRollbackCheckedException

use of org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException in project ignite by apache.

the class GridNearOptimisticTxPrepareFuture method prepare0.

/**
     * Initializes future.
     *
     * @param remap Remap flag.
     * @param topLocked {@code True} if thread already acquired lock preventing topology change.
     */
@Override
protected void prepare0(boolean remap, boolean topLocked) {
    try {
        boolean txStateCheck = remap ? tx.state() == PREPARING : tx.state(PREPARING);
        if (!txStateCheck) {
            if (tx.setRollbackOnly()) {
                if (tx.remainingTime() == -1)
                    onError(new IgniteTxTimeoutCheckedException("Transaction timed out and " + "was rolled back: " + this), false);
                else
                    onError(new IgniteCheckedException("Invalid transaction state for prepare " + "[state=" + tx.state() + ", tx=" + this + ']'), false);
            } else
                onError(new IgniteTxRollbackCheckedException("Invalid transaction state for " + "prepare [state=" + tx.state() + ", tx=" + this + ']'), false);
            return;
        }
        IgniteTxEntry singleWrite = tx.singleWrite();
        if (singleWrite != null)
            prepareSingle(singleWrite, topLocked, remap);
        else
            prepare(tx.writeEntries(), topLocked, remap);
        markInitialized();
    } catch (TransactionTimeoutException e) {
        onError(e, false);
    }
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException)

Example 3 with IgniteTxRollbackCheckedException

use of org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException in project ignite by apache.

the class IgniteTxHandler method processDhtTxPrepareRequest.

/**
     * @param nodeId Sender node ID.
     * @param req Request.
     */
private void processDhtTxPrepareRequest(final UUID nodeId, final GridDhtTxPrepareRequest req) {
    if (txPrepareMsgLog.isDebugEnabled()) {
        txPrepareMsgLog.debug("Received dht prepare request [txId=" + req.nearXidVersion() + ", dhtTxId=" + req.version() + ", node=" + nodeId + ']');
    }
    assert nodeId != null;
    assert req != null;
    assert req.transactionNodes() != null;
    GridDhtTxRemote dhtTx = null;
    GridNearTxRemote nearTx = null;
    GridDhtTxPrepareResponse res;
    try {
        res = new GridDhtTxPrepareResponse(req.partition(), req.version(), req.futureId(), req.miniId(), req.deployInfo() != null);
        // Start near transaction first.
        nearTx = !F.isEmpty(req.nearWrites()) ? startNearRemoteTx(ctx.deploy().globalLoader(), nodeId, req) : null;
        dhtTx = startRemoteTx(nodeId, req, res);
        // Set evicted keys from near transaction.
        if (nearTx != null)
            res.nearEvicted(nearTx.evicted());
        if (dhtTx != null)
            req.txState(dhtTx.txState());
        else if (nearTx != null)
            req.txState(nearTx.txState());
        if (dhtTx != null && !F.isEmpty(dhtTx.invalidPartitions()))
            res.invalidPartitionsByCacheId(dhtTx.invalidPartitions());
        if (req.onePhaseCommit()) {
            assert req.last();
            if (dhtTx != null) {
                dhtTx.onePhaseCommit(true);
                dhtTx.needReturnValue(req.needReturnValue());
                finish(dhtTx, req);
            }
            if (nearTx != null) {
                nearTx.onePhaseCommit(true);
                finish(nearTx, req);
            }
        }
    } catch (IgniteCheckedException e) {
        if (e instanceof IgniteTxRollbackCheckedException)
            U.error(log, "Transaction was rolled back before prepare completed: " + req, e);
        else if (e instanceof IgniteTxOptimisticCheckedException) {
            if (log.isDebugEnabled())
                log.debug("Optimistic failure for remote transaction (will rollback): " + req);
        } else if (e instanceof IgniteTxHeuristicCheckedException) {
            U.warn(log, "Failed to commit transaction (all transaction entries were invalidated): " + CU.txString(dhtTx));
        } else
            U.error(log, "Failed to process prepare request: " + req, e);
        if (nearTx != null)
            nearTx.rollbackRemoteTx();
        res = new GridDhtTxPrepareResponse(req.partition(), req.version(), req.futureId(), req.miniId(), e, req.deployInfo() != null);
    }
    if (req.onePhaseCommit()) {
        IgniteInternalFuture completeFut;
        IgniteInternalFuture<IgniteInternalTx> dhtFin = dhtTx == null ? null : dhtTx.done() ? null : dhtTx.finishFuture();
        final IgniteInternalFuture<IgniteInternalTx> nearFin = nearTx == null ? null : nearTx.done() ? null : nearTx.finishFuture();
        if (dhtFin != null && nearFin != null) {
            GridCompoundFuture fut = new GridCompoundFuture();
            fut.add(dhtFin);
            fut.add(nearFin);
            fut.markInitialized();
            completeFut = fut;
        } else
            completeFut = dhtFin != null ? dhtFin : nearFin;
        if (completeFut != null) {
            final GridDhtTxPrepareResponse res0 = res;
            final GridDhtTxRemote dhtTx0 = dhtTx;
            final GridNearTxRemote nearTx0 = nearTx;
            completeFut.listen(new CI1<IgniteInternalFuture<IgniteInternalTx>>() {

                @Override
                public void apply(IgniteInternalFuture<IgniteInternalTx> fut) {
                    sendReply(nodeId, req, res0, dhtTx0, nearTx0);
                }
            });
        } else
            sendReply(nodeId, req, res, dhtTx, nearTx);
    } else
        sendReply(nodeId, req, res, dhtTx, nearTx);
    assert req.txState() != null || res.error() != null || (ctx.tm().tx(req.version()) == null && ctx.tm().nearTx(req.version()) == null);
}
Also used : GridDhtTxRemote(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxRemote) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridDhtTxPrepareResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareResponse) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) GridNearTxRemote(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxRemote) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 4 with IgniteTxRollbackCheckedException

use of org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException in project ignite by apache.

the class IgniteTxHandler method sendReply.

/**
     * Sends tx finish response to remote node, if response is requested.
     *
     * @param nodeId Node id that originated finish request.
     * @param req Request.
     * @param committed {@code True} if transaction committed on this node.
     * @param nearTxId Near tx version.
     */
private void sendReply(UUID nodeId, GridDhtTxFinishRequest req, boolean committed, GridCacheVersion nearTxId) {
    if (req.replyRequired() || req.checkCommitted()) {
        GridDhtTxFinishResponse res = new GridDhtTxFinishResponse(req.partition(), req.version(), req.futureId(), req.miniId());
        if (req.checkCommitted()) {
            res.checkCommitted(true);
            if (committed) {
                if (req.needReturnValue()) {
                    try {
                        GridCacheReturnCompletableWrapper wrapper = ctx.tm().getCommittedTxReturn(req.version());
                        if (wrapper != null)
                            res.returnValue(wrapper.fut().get());
                        else
                            assert !ctx.discovery().alive(nodeId) : nodeId;
                    } catch (IgniteCheckedException ignored) {
                        if (txFinishMsgLog.isDebugEnabled()) {
                            txFinishMsgLog.debug("Failed to gain entry processor return value. [txId=" + nearTxId + ", dhtTxId=" + req.version() + ", node=" + nodeId + ']');
                        }
                    }
                }
            } else {
                ClusterTopologyCheckedException cause = new ClusterTopologyCheckedException("Primary node left grid.");
                res.checkCommittedError(new IgniteTxRollbackCheckedException("Failed to commit transaction " + "(transaction has been rolled back on backup node): " + req.version(), cause));
            }
        }
        try {
            ctx.io().send(nodeId, res, req.policy());
            if (txFinishMsgLog.isDebugEnabled()) {
                txFinishMsgLog.debug("Sent dht tx finish response [txId=" + nearTxId + ", dhtTxId=" + req.version() + ", node=" + nodeId + ", checkCommitted=" + req.checkCommitted() + ']');
            }
        } catch (Throwable e) {
            // Double-check.
            if (ctx.discovery().node(nodeId) == null) {
                if (txFinishMsgLog.isDebugEnabled()) {
                    txFinishMsgLog.debug("Node left while send dht tx finish response [txId=" + nearTxId + ", dhtTxId=" + req.version() + ", node=" + nodeId + ']');
                }
            } else {
                U.error(log, "Failed to send finish response to node [txId=" + nearTxId + ", dhtTxId=" + req.version() + ", nodeId=" + nodeId + ", res=" + res + ']', e);
            }
            if (e instanceof Error)
                throw (Error) e;
        }
    } else {
        if (txFinishMsgLog.isDebugEnabled()) {
            txFinishMsgLog.debug("Skip send dht tx finish response [txId=" + nearTxId + ", dhtTxId=" + req.version() + ", node=" + nodeId + ']');
        }
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDhtTxFinishResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxFinishResponse) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridCacheReturnCompletableWrapper(org.apache.ignite.internal.processors.cache.GridCacheReturnCompletableWrapper) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 5 with IgniteTxRollbackCheckedException

use of org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException in project ignite by apache.

the class GridNearTxFinishFuture method checkBackup.

/**
     *
     */
private void checkBackup() {
    assert !hasFutures() : futures();
    GridDistributedTxMapping mapping = mappings.singleMapping();
    if (mapping != null) {
        UUID nodeId = mapping.primary().id();
        Collection<UUID> backups = tx.transactionNodes().get(nodeId);
        if (!F.isEmpty(backups)) {
            assert backups.size() == 1;
            UUID backupId = F.first(backups);
            ClusterNode backup = cctx.discovery().node(backupId);
            // Nothing to do if backup has left the grid.
            if (backup == null) {
                readyNearMappingFromBackup(mapping);
                ClusterTopologyCheckedException cause = new ClusterTopologyCheckedException("Backup node left grid: " + backupId);
                cause.retryReadyFuture(cctx.nextAffinityReadyFuture(tx.topologyVersion()));
                onDone(new IgniteTxRollbackCheckedException("Failed to commit transaction " + "(backup has left grid): " + tx.xidVersion(), cause));
            } else {
                final CheckBackupMiniFuture mini = new CheckBackupMiniFuture(1, backup, mapping);
                add(mini);
                if (backup.isLocal()) {
                    boolean committed = !cctx.tm().addRolledbackTx(tx);
                    readyNearMappingFromBackup(mapping);
                    if (committed) {
                        try {
                            if (tx.needReturnValue() && tx.implicit()) {
                                GridCacheReturnCompletableWrapper wrapper = cctx.tm().getCommittedTxReturn(tx.xidVersion());
                                assert wrapper != null : tx.xidVersion();
                                GridCacheReturn retVal = wrapper.fut().get();
                                assert retVal != null;
                                tx.implicitSingleResult(retVal);
                            }
                            if (tx.syncMode() == FULL_SYNC) {
                                GridCacheVersion nearXidVer = tx.nearXidVersion();
                                assert nearXidVer != null : tx;
                                IgniteInternalFuture<?> fut = cctx.tm().remoteTxFinishFuture(nearXidVer);
                                fut.listen(new CI1<IgniteInternalFuture<?>>() {

                                    @Override
                                    public void apply(IgniteInternalFuture<?> fut) {
                                        mini.onDone(tx);
                                    }
                                });
                                return;
                            }
                            mini.onDone(tx);
                        } catch (IgniteCheckedException e) {
                            if (msgLog.isDebugEnabled()) {
                                msgLog.debug("Near finish fut, failed to finish [" + "txId=" + tx.nearXidVersion() + ", node=" + backup.id() + ", err=" + e + ']');
                            }
                            mini.onDone(e);
                        }
                    } else {
                        ClusterTopologyCheckedException cause = new ClusterTopologyCheckedException("Primary node left grid: " + nodeId);
                        cause.retryReadyFuture(cctx.nextAffinityReadyFuture(tx.topologyVersion()));
                        mini.onDone(new IgniteTxRollbackCheckedException("Failed to commit transaction " + "(transaction has been rolled back on backup node): " + tx.xidVersion(), cause));
                    }
                } else {
                    GridDhtTxFinishRequest finishReq = checkCommittedRequest(mini.futureId(), false);
                    try {
                        cctx.io().send(backup, finishReq, tx.ioPolicy());
                        if (msgLog.isDebugEnabled()) {
                            msgLog.debug("Near finish fut, sent check committed request [" + "txId=" + tx.nearXidVersion() + ", node=" + backup.id() + ']');
                        }
                    } catch (ClusterTopologyCheckedException ignored) {
                        mini.onNodeLeft(backupId, false);
                    } catch (IgniteCheckedException e) {
                        if (msgLog.isDebugEnabled()) {
                            msgLog.debug("Near finish fut, failed to send check committed request [" + "txId=" + tx.nearXidVersion() + ", node=" + backup.id() + ", err=" + e + ']');
                        }
                        mini.onDone(e);
                    }
                }
            }
        } else
            readyNearMappingFromBackup(mapping);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) GridDhtTxFinishRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxFinishRequest) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UUID(java.util.UUID) GridCacheReturnCompletableWrapper(org.apache.ignite.internal.processors.cache.GridCacheReturnCompletableWrapper) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

IgniteTxRollbackCheckedException (org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)4 Nullable (org.jetbrains.annotations.Nullable)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)3 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)3 GridNearTxRemote (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxRemote)3 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)3 IgniteTxOptimisticCheckedException (org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException)3 IgniteTxTimeoutCheckedException (org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException)3 IGNITE_CACHE_RETRIES_COUNT (org.apache.ignite.IgniteSystemProperties.IGNITE_CACHE_RETRIES_COUNT)2 BROADCAST (org.apache.ignite.internal.GridClosureCallMode.BROADCAST)2 GridCacheReturnCompletableWrapper (org.apache.ignite.internal.processors.cache.GridCacheReturnCompletableWrapper)2 GridDistributedCacheEntry (org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry)2 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)2 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)2 IgniteTxHeuristicCheckedException (org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException)2 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)2 ArrayList (java.util.ArrayList)1