Search in sources :

Example 16 with IgniteTxKey

use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.

the class GridDhtColocatedCache method removeLocks.

/**
     * Removes locks regardless of whether they are owned or not for given
     * version and keys.
     *
     * @param threadId Thread ID.
     * @param ver Lock version.
     * @param keys Keys.
     */
public void removeLocks(long threadId, GridCacheVersion ver, Collection<KeyCacheObject> keys) {
    if (keys.isEmpty())
        return;
    try {
        int keyCnt = -1;
        Map<ClusterNode, GridNearUnlockRequest> map = null;
        Collection<KeyCacheObject> locKeys = new LinkedList<>();
        for (KeyCacheObject key : keys) {
            IgniteTxKey txKey = ctx.txKey(key);
            GridCacheMvccCandidate lock = ctx.mvcc().removeExplicitLock(threadId, txKey, ver);
            if (lock != null) {
                AffinityTopologyVersion topVer = lock.topologyVersion();
                if (map == null) {
                    Collection<ClusterNode> affNodes = CU.allNodes(ctx, topVer);
                    keyCnt = (int) Math.ceil((double) keys.size() / affNodes.size());
                    map = U.newHashMap(affNodes.size());
                }
                ClusterNode primary = ctx.affinity().primaryByKey(key, topVer);
                if (primary == null) {
                    if (log.isDebugEnabled())
                        log.debug("Failed to remove locks (all partition nodes left the grid).");
                    continue;
                }
                if (!primary.isLocal()) {
                    // Send request to remove from remote nodes.
                    GridNearUnlockRequest req = map.get(primary);
                    if (req == null) {
                        map.put(primary, req = new GridNearUnlockRequest(ctx.cacheId(), keyCnt, ctx.deploymentEnabled()));
                        req.version(ver);
                    }
                    GridCacheEntryEx entry = peekEx(key);
                    KeyCacheObject key0 = entry != null ? entry.key() : key;
                    req.addKey(key0, ctx);
                } else
                    locKeys.add(key);
            }
        }
        if (!locKeys.isEmpty())
            removeLocks(ctx.localNodeId(), ver, locKeys, true);
        if (map == null || map.isEmpty())
            return;
        IgnitePair<Collection<GridCacheVersion>> versPair = ctx.tm().versions(ver);
        Collection<GridCacheVersion> committed = versPair.get1();
        Collection<GridCacheVersion> rolledback = versPair.get2();
        for (Map.Entry<ClusterNode, GridNearUnlockRequest> mapping : map.entrySet()) {
            ClusterNode n = mapping.getKey();
            GridDistributedUnlockRequest req = mapping.getValue();
            if (!F.isEmpty(req.keys())) {
                req.completedVersions(committed, rolledback);
                try {
                    // We don't wait for reply to this message.
                    ctx.io().send(n, req, ctx.ioPolicy());
                } catch (ClusterTopologyCheckedException e) {
                    if (log.isDebugEnabled())
                        log.debug("Failed to send unlock request (node has left the grid) [keys=" + req.keys() + ", n=" + n + ", e=" + e + ']');
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to send unlock request [keys=" + req.keys() + ", n=" + n + ']', e);
                }
            }
        }
    } catch (IgniteCheckedException ex) {
        U.error(log, "Failed to unlock the lock for keys: " + keys, ex);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearUnlockRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearUnlockRequest) LinkedList(java.util.LinkedList) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDistributedUnlockRequest(org.apache.ignite.internal.processors.cache.distributed.GridDistributedUnlockRequest) Collection(java.util.Collection) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) Map(java.util.Map) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 17 with IgniteTxKey

use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.

the class GridDhtColocatedLockFuture method requestedKeys0.

/**
     * @return Keys for which locks requested from remote nodes but response isn't received.
     */
private Set<IgniteTxKey> requestedKeys0() {
    for (IgniteInternalFuture<Boolean> miniFut : futures()) {
        if (isMini(miniFut) && !miniFut.isDone()) {
            MiniFuture mini = (MiniFuture) miniFut;
            Set<IgniteTxKey> requestedKeys = U.newHashSet(mini.keys.size());
            for (KeyCacheObject key : mini.keys) requestedKeys.add(new IgniteTxKey(key, cctx.cacheId()));
            return requestedKeys;
        }
    }
    return null;
}
Also used : IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 18 with IgniteTxKey

use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.

the class GridDhtColocatedLockFuture method addEntry.

/**
     * Adds entry to future.
     *
     * @param entry Entry to add.
     * @return Non-reentry candidate if lock should be acquired on remote node,
     *      reentry candidate if locks has been already acquired and {@code null} if explicit locks is held and
     *      implicit transaction accesses locked entry.
     * @throws IgniteCheckedException If failed to add entry due to external locking.
     */
@Nullable
private GridCacheMvccCandidate addEntry(GridDistributedCacheEntry entry) throws IgniteCheckedException {
    IgniteTxKey txKey = entry.txKey();
    GridCacheMvccCandidate cand = cctx.mvcc().explicitLock(threadId, txKey);
    if (inTx()) {
        if (cand != null) {
            if (!tx.implicit())
                throw new IgniteCheckedException("Cannot access key within transaction if lock is " + "externally held [key=" + entry.key() + ", entry=" + entry + ']');
            else
                return null;
        } else {
            IgniteTxEntry txEntry = tx.entry(txKey);
            txEntry.cached(entry);
            // Check transaction entries (corresponding tx entries must be enlisted in transaction).
            cand = new GridCacheMvccCandidate(entry, cctx.localNodeId(), null, null, threadId, lockVer, true, tx.entry(txKey).locked(), inTx(), inTx() && tx.implicitSingle(), false, false, null, false);
            cand.topologyVersion(topVer);
        }
    } else {
        if (cand == null) {
            cand = new GridCacheMvccCandidate(entry, cctx.localNodeId(), null, null, threadId, lockVer, true, false, inTx(), inTx() && tx.implicitSingle(), false, false, null, false);
            cand.topologyVersion(topVer);
        } else
            cand = cand.reenter();
        cctx.mvcc().addExplicitLock(threadId, cand, topVer);
    }
    return cand;
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with IgniteTxKey

use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.

the class GridDhtTxPrepareRequest method prepareMarshal.

/**
     * {@inheritDoc}
     *
     * @param ctx
     */
@Override
public void prepareMarshal(GridCacheSharedContext ctx) throws IgniteCheckedException {
    super.prepareMarshal(ctx);
    if (owned != null && ownedKeys == null) {
        ownedKeys = owned.keySet();
        ownedVals = owned.values();
        for (IgniteTxKey key : ownedKeys) {
            GridCacheContext cctx = ctx.cacheContext(key.cacheId());
            key.prepareMarshal(cctx);
            if (addDepInfo)
                prepareObject(key, cctx);
        }
    }
    if (nearWrites != null)
        marshalTx(nearWrites, ctx);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)

Example 20 with IgniteTxKey

use of org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey in project ignite by apache.

the class GridDhtTxPrepareResponse method prepareMarshal.

/** {@inheritDoc} */
@Override
public void prepareMarshal(GridCacheSharedContext ctx) throws IgniteCheckedException {
    super.prepareMarshal(ctx);
    if (nearEvicted != null) {
        for (IgniteTxKey key : nearEvicted) {
            GridCacheContext cctx = ctx.cacheContext(key.cacheId());
            key.prepareMarshal(cctx);
        }
    }
    if (preloadEntries != null) {
        for (GridCacheEntryInfo info : preloadEntries) {
            GridCacheContext cctx = ctx.cacheContext(info.cacheId());
            info.marshal(cctx);
        }
    }
}
Also used : GridCacheEntryInfo(org.apache.ignite.internal.processors.cache.GridCacheEntryInfo) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)

Aggregations

IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)31 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)16 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)14 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)14 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)11 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)11 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)9 Map (java.util.Map)7 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)7 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)7 IgniteTxRollbackCheckedException (org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException)6 ArrayList (java.util.ArrayList)5 GridCacheMvccCandidate (org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)5 HashMap (java.util.HashMap)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 GridDistributedCacheEntry (org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry)4 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)3 GridCacheEntryInfo (org.apache.ignite.internal.processors.cache.GridCacheEntryInfo)3