Search in sources :

Example 11 with GridDistributedCacheEntry

use of org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry in project ignite by apache.

the class GridDhtTransactionalCacheAdapter method startRemoteTx.

/**
     * @param nodeId Primary node ID.
     * @param req Request.
     * @param res Response.
     * @return Remote transaction.
     * @throws IgniteCheckedException If failed.
     * @throws GridDistributedLockCancelledException If lock has been cancelled.
     */
@SuppressWarnings({ "RedundantTypeArguments" })
@Nullable
private GridDhtTxRemote startRemoteTx(UUID nodeId, GridDhtLockRequest req, GridDhtLockResponse res) throws IgniteCheckedException, GridDistributedLockCancelledException {
    List<KeyCacheObject> keys = req.keys();
    GridDhtTxRemote tx = null;
    int size = F.size(keys);
    for (int i = 0; i < size; i++) {
        KeyCacheObject key = keys.get(i);
        if (key == null)
            continue;
        IgniteTxKey txKey = ctx.txKey(key);
        if (log.isDebugEnabled())
            log.debug("Unmarshalled key: " + key);
        GridDistributedCacheEntry entry = null;
        while (true) {
            try {
                int part = ctx.affinity().partition(key);
                GridDhtLocalPartition locPart = ctx.topology().localPartition(part, req.topologyVersion(), false);
                if (locPart == null || !locPart.reserve()) {
                    if (log.isDebugEnabled())
                        log.debug("Local partition for given key is already evicted (will add to invalid " + "partition list) [key=" + key + ", part=" + part + ", locPart=" + locPart + ']');
                    res.addInvalidPartition(part);
                    // Invalidate key in near cache, if any.
                    if (isNearEnabled(cacheCfg))
                        obsoleteNearEntry(key);
                    break;
                }
                try {
                    // Handle implicit locks for pessimistic transactions.
                    if (req.inTx()) {
                        if (tx == null)
                            tx = ctx.tm().tx(req.version());
                        if (tx == null) {
                            tx = new GridDhtTxRemote(ctx.shared(), req.nodeId(), req.futureId(), nodeId, req.nearXidVersion(), req.topologyVersion(), req.version(), /*commitVer*/
                            null, ctx.systemTx(), ctx.ioPolicy(), PESSIMISTIC, req.isolation(), req.isInvalidate(), req.timeout(), req.txSize(), req.subjectId(), req.taskNameHash());
                            tx = ctx.tm().onCreated(null, tx);
                            if (tx == null || !ctx.tm().onStarted(tx))
                                throw new IgniteTxRollbackCheckedException("Failed to acquire lock (transaction " + "has been completed) [ver=" + req.version() + ", tx=" + tx + ']');
                        }
                        tx.addWrite(ctx, NOOP, txKey, null, null, req.accessTtl(), req.skipStore(), req.keepBinary());
                    }
                    entry = entryExx(key, req.topologyVersion());
                    // Add remote candidate before reordering.
                    entry.addRemote(req.nodeId(), nodeId, req.threadId(), req.version(), tx != null, tx != null && tx.implicitSingle(), null);
                    // Invalidate key in near cache, if any.
                    if (isNearEnabled(cacheCfg) && req.invalidateNearEntry(i))
                        invalidateNearEntry(key, req.version());
                    // Get entry info after candidate is added.
                    if (req.needPreloadKey(i)) {
                        entry.unswap();
                        GridCacheEntryInfo info = entry.info();
                        if (info != null && !info.isNew() && !info.isDeleted())
                            res.addPreloadEntry(info);
                    }
                    // Double-check in case if sender node left the grid.
                    if (ctx.discovery().node(req.nodeId()) == null) {
                        if (log.isDebugEnabled())
                            log.debug("Node requesting lock left grid (lock request will be ignored): " + req);
                        entry.removeLock(req.version());
                        if (tx != null) {
                            tx.clearEntry(txKey);
                            // COMMITTING state, but this lock is never acquired.
                            if (tx.state() == COMMITTING)
                                tx.forceCommit();
                            else
                                tx.rollbackRemoteTx();
                        }
                        return null;
                    }
                    // Entry is legit.
                    break;
                } finally {
                    locPart.release();
                }
            } catch (GridDhtInvalidPartitionException e) {
                if (log.isDebugEnabled())
                    log.debug("Received invalid partition exception [e=" + e + ", req=" + req + ']');
                res.addInvalidPartition(e.partition());
                // Invalidate key in near cache, if any.
                if (isNearEnabled(cacheCfg))
                    obsoleteNearEntry(key);
                if (tx != null) {
                    tx.clearEntry(txKey);
                    if (log.isDebugEnabled())
                        log.debug("Cleared invalid entry from remote transaction (will skip) [entry=" + entry + ", tx=" + tx + ']');
                }
                break;
            } catch (GridCacheEntryRemovedException ignored) {
                assert entry.obsoleteVersion() != null : "Obsolete flag not set on removed entry: " + entry;
                if (log.isDebugEnabled())
                    log.debug("Received entry removed exception (will retry on renewed entry): " + entry);
                if (tx != null) {
                    tx.clearEntry(txKey);
                    if (log.isDebugEnabled())
                        log.debug("Cleared removed entry from remote transaction (will retry) [entry=" + entry + ", tx=" + tx + ']');
                }
            }
        }
    }
    if (tx != null && tx.empty()) {
        if (log.isDebugEnabled())
            log.debug("Rolling back remote DHT transaction because it is empty [req=" + req + ", res=" + res + ']');
        tx.rollbackRemoteTx();
        tx = null;
    }
    return tx;
}
Also used : GridCacheEntryInfo(org.apache.ignite.internal.processors.cache.GridCacheEntryInfo) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with GridDistributedCacheEntry

use of org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry in project ignite by apache.

the class GridDhtTransactionalCacheAdapter method clearLocks.

/**
     * @param nodeId Node ID.
     * @param req Request.
     */
@SuppressWarnings({ "RedundantTypeArguments" })
private void clearLocks(UUID nodeId, GridDistributedUnlockRequest req) {
    assert nodeId != null;
    List<KeyCacheObject> keys = req.keys();
    if (keys != null) {
        for (KeyCacheObject key : keys) {
            while (true) {
                GridDistributedCacheEntry entry = peekExx(key);
                if (entry == null)
                    // Nothing to unlock.
                    break;
                try {
                    entry.doneRemote(req.version(), req.version(), null, null, null, /*system invalidate*/
                    false);
                    // we are about to remove.
                    if (entry.removeLock(req.version())) {
                        if (log.isDebugEnabled())
                            log.debug("Removed lock [lockId=" + req.version() + ", key=" + key + ']');
                    } else {
                        if (log.isDebugEnabled())
                            log.debug("Received unlock request for unknown candidate " + "(added to cancelled locks set): " + req);
                    }
                    ctx.evicts().touch(entry, ctx.affinity().affinityTopologyVersion());
                    break;
                } catch (GridCacheEntryRemovedException ignored) {
                    if (log.isDebugEnabled())
                        log.debug("Received remove lock request for removed entry (will retry) [entry=" + entry + ", req=" + req + ']');
                }
            }
        }
    }
}
Also used : GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 13 with GridDistributedCacheEntry

use of org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry in project ignite by apache.

the class GridDhtTransactionalCacheAdapter method processDhtLockRequest0.

/**
     * @param nodeId Node ID.
     * @param req Request.
     */
private void processDhtLockRequest0(UUID nodeId, GridDhtLockRequest req) {
    assert nodeId != null;
    assert req != null;
    assert !nodeId.equals(locNodeId);
    int cnt = F.size(req.keys());
    GridDhtLockResponse res;
    GridDhtTxRemote dhtTx = null;
    GridNearTxRemote nearTx = null;
    boolean fail = false;
    boolean cancelled = false;
    try {
        res = new GridDhtLockResponse(ctx.cacheId(), req.version(), req.futureId(), req.miniId(), cnt, ctx.deploymentEnabled());
        dhtTx = startRemoteTx(nodeId, req, res);
        nearTx = isNearEnabled(cacheCfg) ? near().startRemoteTx(nodeId, req) : null;
        if (nearTx != null && !nearTx.empty())
            res.nearEvicted(nearTx.evicted());
        else {
            if (!F.isEmpty(req.nearKeys())) {
                Collection<IgniteTxKey> nearEvicted = new ArrayList<>(req.nearKeys().size());
                nearEvicted.addAll(F.viewReadOnly(req.nearKeys(), new C1<KeyCacheObject, IgniteTxKey>() {

                    @Override
                    public IgniteTxKey apply(KeyCacheObject k) {
                        return ctx.txKey(k);
                    }
                }));
                res.nearEvicted(nearEvicted);
            }
        }
    } catch (IgniteTxRollbackCheckedException e) {
        String err = "Failed processing DHT lock request (transaction has been completed): " + req;
        U.error(log, err, e);
        res = new GridDhtLockResponse(ctx.cacheId(), req.version(), req.futureId(), req.miniId(), new IgniteTxRollbackCheckedException(err, e), ctx.deploymentEnabled());
        fail = true;
    } catch (IgniteCheckedException e) {
        String err = "Failed processing DHT lock request: " + req;
        U.error(log, err, e);
        res = new GridDhtLockResponse(ctx.cacheId(), req.version(), req.futureId(), req.miniId(), new IgniteCheckedException(err, e), ctx.deploymentEnabled());
        fail = true;
    } catch (GridDistributedLockCancelledException ignored) {
        // Received lock request for cancelled lock.
        if (log.isDebugEnabled())
            log.debug("Received lock request for canceled lock (will ignore): " + req);
        res = null;
        fail = true;
        cancelled = true;
    }
    boolean releaseAll = false;
    if (res != null) {
        try {
            // Reply back to sender.
            ctx.io().send(nodeId, res, ctx.ioPolicy());
            if (txLockMsgLog.isDebugEnabled()) {
                txLockMsgLog.debug("Sent dht lock response [txId=" + req.nearXidVersion() + ", dhtTxId=" + req.version() + ", inTx=" + req.inTx() + ", node=" + nodeId + ']');
            }
        } catch (ClusterTopologyCheckedException ignored) {
            U.warn(txLockMsgLog, "Failed to send dht lock response, node failed [" + "txId=" + req.nearXidVersion() + ", dhtTxId=" + req.version() + ", inTx=" + req.inTx() + ", node=" + nodeId + ']');
            fail = true;
            releaseAll = true;
        } catch (IgniteCheckedException e) {
            U.error(txLockMsgLog, "Failed to send dht lock response (lock will not be acquired) " + "txId=" + req.nearXidVersion() + ", dhtTxId=" + req.version() + ", inTx=" + req.inTx() + ", node=" + nodeId + ']', e);
            fail = true;
        }
    }
    if (fail) {
        if (dhtTx != null)
            dhtTx.rollbackRemoteTx();
        if (// Even though this should never happen, we leave this check for consistency.
        nearTx != null)
            nearTx.rollbackRemoteTx();
        List<KeyCacheObject> keys = req.keys();
        if (keys != null) {
            for (KeyCacheObject key : keys) {
                while (true) {
                    GridDistributedCacheEntry entry = peekExx(key);
                    try {
                        if (entry != null) {
                            // Release all locks because sender node left grid.
                            if (releaseAll)
                                entry.removeExplicitNodeLocks(req.nodeId());
                            else
                                entry.removeLock(req.version());
                        }
                        break;
                    } catch (GridCacheEntryRemovedException ignore) {
                        if (log.isDebugEnabled())
                            log.debug("Attempted to remove lock on removed entity during during failure " + "handling for dht lock request (will retry): " + entry);
                    }
                }
            }
        }
        if (releaseAll && !cancelled)
            U.warn(log, "Sender node left grid in the midst of lock acquisition (locks have been released).");
    }
}
Also used : GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) GridDistributedLockCancelledException(org.apache.ignite.internal.processors.cache.distributed.GridDistributedLockCancelledException) ArrayList(java.util.ArrayList) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridNearTxRemote(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxRemote) C1(org.apache.ignite.internal.util.typedef.C1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

GridDistributedCacheEntry (org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry)13 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)10 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)9 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 GridCacheMvccCandidate (org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)5 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)4 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)3 GridDistributedUnlockRequest (org.apache.ignite.internal.processors.cache.distributed.GridDistributedUnlockRequest)3 ClusterTopologyServerNotFoundException (org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException)2 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)2 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)2 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)2 IgniteTxRollbackCheckedException (org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException)2 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1