Search in sources :

Example 1 with IgniteTxOptimisticCheckedException

use of org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException 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) {
    try (TraceSurroundings ignored = MTC.support(ctx.kernalContext().tracing().create(TX_PROCESS_DHT_PREPARE_REQ, MTC.span()))) {
        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());
            List<IgniteTxKey> writesCacheMissed = req.nearWritesCacheMissed();
            if (writesCacheMissed != null) {
                Collection<IgniteTxKey> evicted0 = res.nearEvicted();
                if (evicted0 != null)
                    writesCacheMissed.addAll(evicted0);
                res.nearEvicted(writesCacheMissed);
            }
            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
                U.error(log, "Failed to process prepare request: " + req, e);
            if (nearTx != null)
                try {
                    nearTx.rollbackRemoteTx();
                } catch (Throwable e1) {
                    e.addSuppressed(e1);
                }
            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 || (dhtTx == null && nearTx == null) : req + " tx=" + dhtTx + " nearTx=" + nearTx;
    }
}
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) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) 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 2 with IgniteTxOptimisticCheckedException

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

the class GridDhtTxLocal method prepareAsync.

/**
 * Prepares next batch of entries in dht transaction.
 *
 * @param req Prepare request.
 * @return Future that will be completed when locks are acquired.
 */
public final IgniteInternalFuture<GridNearTxPrepareResponse> prepareAsync(GridNearTxPrepareRequest req) {
    // In optimistic mode prepare still can be called explicitly from salvageTx.
    GridDhtTxPrepareFuture fut = prepFut;
    long timeout = remainingTime();
    if (fut == null) {
        init();
        // Future must be created before any exception can be thrown.
        if (!PREP_FUT_UPD.compareAndSet(this, null, fut = new GridDhtTxPrepareFuture(cctx, this, timeout, req.miniId(), req.dhtVersions(), req.last(), needReturnValue()))) {
            GridDhtTxPrepareFuture f = prepFut;
            assert f.nearMiniId() == req.miniId() : "Wrong near mini id on existing future " + "[futMiniId=" + f.nearMiniId() + ", miniId=" + req.miniId() + ", fut=" + f + ']';
            if (timeout == -1)
                f.onError(timeoutException());
            return chainOnePhasePrepare(f);
        }
    } else {
        assert fut.nearMiniId() == req.miniId() : "Wrong near mini id on existing future " + "[futMiniId=" + fut.nearMiniId() + ", miniId=" + req.miniId() + ", fut=" + fut + ']';
        // Prepare was called explicitly.
        return chainOnePhasePrepare(fut);
    }
    if (state() != PREPARING) {
        if (!state(PREPARING)) {
            if (state() == PREPARED && isSystemInvalidate())
                fut.complete();
            if (setRollbackOnly()) {
                if (timeout == -1)
                    fut.onError(new IgniteTxTimeoutCheckedException("Transaction timed out and was rolled back: " + this));
                else
                    fut.onError(new IgniteCheckedException("Invalid transaction state for prepare [state=" + state() + ", tx=" + this + ']'));
            } else
                fut.onError(new IgniteTxRollbackCheckedException("Invalid transaction state for prepare [state=" + state() + ", tx=" + this + ']'));
            return fut;
        }
    }
    try {
        if (req.reads() != null) {
            for (IgniteTxEntry e : req.reads()) addEntry(req.messageId(), e);
        }
        if (req.writes() != null) {
            for (IgniteTxEntry e : req.writes()) addEntry(req.messageId(), e);
        }
        userPrepare(null);
        // Make sure to add future before calling prepare on it.
        cctx.mvcc().addFuture(fut);
        if (isSystemInvalidate())
            fut.complete();
        else
            fut.prepare(req);
    } catch (IgniteTxTimeoutCheckedException | IgniteTxRollbackCheckedException | IgniteTxOptimisticCheckedException e) {
        fut.onError(e);
    } catch (IgniteCheckedException e) {
        setRollbackOnly();
        fut.onError(new IgniteTxRollbackCheckedException("Failed to prepare transaction: " + CU.txString(this), e));
    }
    return chainOnePhasePrepare(fut);
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException)

Example 3 with IgniteTxOptimisticCheckedException

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

the class GridDhtTxPrepareFuture method versionCheckError.

/**
 * @param entry Entry.
 * @return Optimistic version check error.
 */
private IgniteTxOptimisticCheckedException versionCheckError(IgniteTxEntry entry) {
    StringBuilder msg = new StringBuilder("Failed to prepare transaction, read/write conflict [");
    GridCacheContext cctx = entry.context();
    try {
        Object key = cctx.unwrapBinaryIfNeeded(entry.key(), entry.keepBinary(), false, null);
        assert key != null : entry.key();
        if (S.includeSensitive())
            msg.append("key=").append(key.toString()).append(", keyCls=").append(key.getClass().getName());
    } catch (Exception e) {
        msg.append("key=<failed to get key: ").append(e.toString()).append(">");
    }
    try {
        GridCacheEntryEx entryEx = entry.cached();
        CacheObject cacheVal = entryEx != null ? entryEx.rawGet() : null;
        Object val = cacheVal != null ? cctx.unwrapBinaryIfNeeded(cacheVal, entry.keepBinary(), false, null) : null;
        if (val != null) {
            if (S.includeSensitive())
                msg.append(", val=").append(val.toString()).append(", valCls=").append(val.getClass().getName());
        } else
            msg.append(", val=null");
    } catch (Exception e) {
        msg.append(", val=<failed to get value: ").append(e.toString()).append(">");
    }
    msg.append(", cache=").append(cctx.name()).append(", thread=").append(Thread.currentThread()).append("]");
    return new IgniteTxOptimisticCheckedException(msg.toString());
}
Also used : GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException)

Example 4 with IgniteTxOptimisticCheckedException

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

the class GridNearTxLocal method prepareAsyncLocal.

/**
 * Prepares next batch of entries in dht transaction.
 *
 * @param req Prepare request.
 * @return Future that will be completed when locks are acquired.
 */
public IgniteInternalFuture<GridNearTxPrepareResponse> prepareAsyncLocal(GridNearTxPrepareRequest req) {
    long timeout = remainingTime();
    if (state() != PREPARING) {
        if (timeout == -1)
            return new GridFinishedFuture<>(timeoutException());
        setRollbackOnly();
        return new GridFinishedFuture<>(rollbackException());
    }
    if (timeout == -1)
        return new GridFinishedFuture<>(timeoutException());
    init();
    GridDhtTxPrepareFuture fut = new GridDhtTxPrepareFuture(cctx, this, timeout, 0, Collections.<IgniteTxKey, GridCacheVersion>emptyMap(), req.last(), needReturnValue() && implicit());
    try {
        userPrepare((serializable() && optimistic()) ? F.concat(false, req.writes(), req.reads()) : req.writes());
        // Make sure to add future before calling prepare on it.
        cctx.mvcc().addFuture(fut);
        if (isSystemInvalidate())
            fut.complete();
        else
            fut.prepare(req);
    } catch (IgniteTxTimeoutCheckedException | IgniteTxOptimisticCheckedException e) {
        fut.onError(e);
    } catch (IgniteCheckedException e) {
        setRollbackOnly();
        fut.onError(new IgniteTxRollbackCheckedException("Failed to prepare transaction: " + this, e));
    }
    return chainOnePhasePrepare(fut);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 5 with IgniteTxOptimisticCheckedException

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

the class GridNearOptimisticSerializableTxPrepareFuture method onError.

/**
 * @param m Failed mapping.
 * @param e Error.
 */
private void onError(@Nullable GridDistributedTxMapping m, Throwable e) {
    try (TraceSurroundings ignored = MTC.support(span)) {
        if (X.hasCause(e, ClusterTopologyCheckedException.class) || X.hasCause(e, ClusterTopologyException.class)) {
            if (tx.onePhaseCommit()) {
                tx.markForBackupCheck();
                onComplete();
                return;
            }
        }
        if (e instanceof IgniteTxOptimisticCheckedException) {
            if (m != null)
                tx.removeMapping(m.primary().id());
        }
        prepareError(e);
    }
}
Also used : ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

IgniteTxOptimisticCheckedException (org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteTxRollbackCheckedException (org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException)6 IgniteTxTimeoutCheckedException (org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException)4 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)3 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)2 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)2 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)2 GridDhtTxPrepareFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture)2 TraceSurroundings (org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)2 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)2 Lock (java.util.concurrent.locks.Lock)1 Ignite (org.apache.ignite.Ignite)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)1 Event (org.apache.ignite.events.Event)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)1