Search in sources :

Example 1 with GridDistributedUnlockRequest

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

the class GridDhtColocatedCache method unlockAll.

/**
 * {@inheritDoc}
 */
@Override
public void unlockAll(Collection<? extends K> keys) {
    if (keys.isEmpty())
        return;
    try {
        GridCacheVersion ver = null;
        int keyCnt = -1;
        Map<ClusterNode, GridNearUnlockRequest> map = null;
        Collection<KeyCacheObject> locKeys = new ArrayList<>();
        for (K key : keys) {
            KeyCacheObject cacheKey = ctx.toCacheKeyObject(key);
            IgniteTxKey txKey = ctx.txKey(cacheKey);
            GridDistributedCacheEntry entry = peekExx(cacheKey);
            GridCacheMvccCandidate lock = ctx.mvcc().removeExplicitLock(Thread.currentThread().getId(), txKey, null);
            if (lock != null) {
                final AffinityTopologyVersion topVer = lock.topologyVersion();
                assert topVer.compareTo(AffinityTopologyVersion.ZERO) > 0;
                // Send request to remove from remote nodes.
                ClusterNode primary = ctx.affinity().primaryByKey(key, topVer);
                if (primary == null) {
                    if (log.isDebugEnabled())
                        log.debug("Failed to unlock keys (all partition nodes left the grid).");
                    continue;
                }
                if (map == null) {
                    Collection<ClusterNode> affNodes = CU.affinityNodes(ctx, topVer);
                    keyCnt = (int) Math.ceil((double) keys.size() / affNodes.size());
                    map = U.newHashMap(affNodes.size());
                }
                if (ver == null)
                    ver = lock.version();
                if (!lock.reentry()) {
                    if (!ver.equals(lock.version()))
                        throw new IgniteCheckedException("Failed to unlock (if keys were locked separately, " + "then they need to be unlocked separately): " + keys);
                    if (!primary.isLocal()) {
                        GridNearUnlockRequest req = map.get(primary);
                        if (req == null) {
                            map.put(primary, req = new GridNearUnlockRequest(ctx.cacheId(), keyCnt, ctx.deploymentEnabled()));
                            req.version(ver);
                        }
                        KeyCacheObject key0 = entry != null ? entry.key() : cacheKey;
                        req.addKey(key0, ctx);
                    } else
                        locKeys.add(cacheKey);
                    if (log.isDebugEnabled())
                        log.debug("Removed lock (will distribute): " + lock);
                } else if (log.isDebugEnabled())
                    log.debug("Current thread still owns lock (or there are no other nodes)" + " [lock=" + lock + ", curThreadId=" + Thread.currentThread().getId() + ']');
            }
        }
        if (ver == null)
            return;
        if (!locKeys.isEmpty())
            removeLocks(ctx.localNodeId(), ver, locKeys, true);
        for (Map.Entry<ClusterNode, GridNearUnlockRequest> mapping : map.entrySet()) {
            ClusterNode n = mapping.getKey();
            GridDistributedUnlockRequest req = mapping.getValue();
            assert !n.isLocal();
            if (!F.isEmpty(req.keys())) {
                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) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) GridNearUnlockRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearUnlockRequest) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDistributedUnlockRequest(org.apache.ignite.internal.processors.cache.distributed.GridDistributedUnlockRequest) 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 2 with GridDistributedUnlockRequest

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

the class GridNearTransactionalCache method unlockAll.

/**
 * {@inheritDoc}
 */
@Override
public void unlockAll(Collection<? extends K> keys) {
    if (keys.isEmpty())
        return;
    try {
        GridCacheVersion ver = null;
        int keyCnt = -1;
        Map<ClusterNode, GridNearUnlockRequest> map = null;
        Collection<KeyCacheObject> locKeys = new LinkedList<>();
        for (K key : keys) {
            while (true) {
                KeyCacheObject cacheKey = ctx.toCacheKeyObject(key);
                GridDistributedCacheEntry entry = peekExx(cacheKey);
                if (entry == null)
                    // While.
                    break;
                try {
                    GridCacheMvccCandidate cand = entry.candidate(ctx.nodeId(), Thread.currentThread().getId());
                    AffinityTopologyVersion topVer = AffinityTopologyVersion.NONE;
                    if (cand != null) {
                        assert cand.nearLocal() : "Got non-near-local candidate in near cache: " + cand;
                        ver = cand.version();
                        if (map == null) {
                            Collection<ClusterNode> affNodes = CU.affinityNodes(ctx, cand.topologyVersion());
                            if (F.isEmpty(affNodes))
                                return;
                            keyCnt = (int) Math.ceil((double) keys.size() / affNodes.size());
                            map = U.newHashMap(affNodes.size());
                        }
                        topVer = cand.topologyVersion();
                        // Send request to remove from remote nodes.
                        ClusterNode primary = ctx.affinity().primaryByKey(key, topVer);
                        if (primary == null) {
                            if (log.isDebugEnabled())
                                log.debug("Failed to unlock key (all partition nodes left the grid).");
                            break;
                        }
                        GridNearUnlockRequest req = map.get(primary);
                        if (req == null) {
                            map.put(primary, req = new GridNearUnlockRequest(ctx.cacheId(), keyCnt, ctx.deploymentEnabled()));
                            req.version(ver);
                        }
                        // Remove candidate from local node first.
                        GridCacheMvccCandidate rmv = entry.removeLock();
                        if (rmv != null) {
                            if (!rmv.reentry()) {
                                if (ver != null && !ver.equals(rmv.version()))
                                    throw new IgniteCheckedException("Failed to unlock (if keys were locked separately, " + "then they need to be unlocked separately): " + keys);
                                if (!primary.isLocal()) {
                                    assert req != null;
                                    req.addKey(entry.key(), ctx);
                                } else
                                    locKeys.add(cacheKey);
                                if (log.isDebugEnabled())
                                    log.debug("Removed lock (will distribute): " + rmv);
                            } else if (log.isDebugEnabled())
                                log.debug("Current thread still owns lock (or there are no other nodes)" + " [lock=" + rmv + ", curThreadId=" + Thread.currentThread().getId() + ']');
                        }
                    }
                    assert !topVer.equals(AffinityTopologyVersion.NONE) || cand == null;
                    if (topVer.equals(AffinityTopologyVersion.NONE))
                        topVer = ctx.affinity().affinityTopologyVersion();
                    entry.touch();
                    break;
                } catch (GridCacheEntryRemovedException ignore) {
                    if (log.isDebugEnabled())
                        log.debug("Attempted to unlock removed entry (will retry): " + entry);
                }
            }
        }
        if (ver == null)
            return;
        for (Map.Entry<ClusterNode, GridNearUnlockRequest> mapping : map.entrySet()) {
            ClusterNode n = mapping.getKey();
            GridDistributedUnlockRequest req = mapping.getValue();
            if (n.isLocal())
                dht.removeLocks(ctx.nodeId(), req.version(), locKeys, true);
            else if (!F.isEmpty(req.keys()))
                // We don't wait for reply to this message.
                ctx.io().send(n, req, ctx.ioPolicy());
        }
    } catch (IgniteCheckedException ex) {
        U.error(log, "Failed to unlock the lock for keys: " + keys, ex);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) LinkedList(java.util.LinkedList) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDistributedUnlockRequest(org.apache.ignite.internal.processors.cache.distributed.GridDistributedUnlockRequest) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) Map(java.util.Map) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)

Example 3 with GridDistributedUnlockRequest

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

the class GridNearTransactionalCache method removeLocks.

/**
 * Removes locks regardless of whether they are owned or not for given
 * version and keys.
 *
 * @param ver Lock version.
 * @param keys Keys.
 */
public void removeLocks(GridCacheVersion ver, Collection<KeyCacheObject> keys) {
    if (keys.isEmpty())
        return;
    try {
        int keyCnt = -1;
        Map<ClusterNode, GridNearUnlockRequest> map = null;
        for (KeyCacheObject key : keys) {
            // Send request to remove from remote nodes.
            GridNearUnlockRequest req = null;
            while (true) {
                GridDistributedCacheEntry entry = peekExx(key);
                try {
                    if (entry != null) {
                        GridCacheMvccCandidate cand = entry.candidate(ver);
                        if (cand != null) {
                            if (map == null) {
                                Collection<ClusterNode> affNodes = CU.affinityNodes(ctx, cand.topologyVersion());
                                if (F.isEmpty(affNodes))
                                    return;
                                keyCnt = (int) Math.ceil((double) keys.size() / affNodes.size());
                                map = U.newHashMap(affNodes.size());
                            }
                            ClusterNode primary = ctx.affinity().primaryByKey(key, cand.topologyVersion());
                            if (primary == null) {
                                if (log.isDebugEnabled())
                                    log.debug("Failed to unlock key (all partition nodes left the grid).");
                                break;
                            }
                            if (!primary.isLocal()) {
                                req = map.get(primary);
                                if (req == null) {
                                    map.put(primary, req = new GridNearUnlockRequest(ctx.cacheId(), keyCnt, ctx.deploymentEnabled()));
                                    req.version(ver);
                                }
                            }
                            // Remove candidate from local node first.
                            if (entry.removeLock(cand.version())) {
                                if (primary.isLocal()) {
                                    dht.removeLocks(primary.id(), ver, F.asList(key), true);
                                    assert req == null;
                                    continue;
                                }
                                req.addKey(entry.key(), ctx);
                            }
                        }
                    }
                    break;
                } catch (GridCacheEntryRemovedException ignored) {
                    if (log.isDebugEnabled())
                        log.debug("Attempted to remove lock from removed entry (will retry) [rmvVer=" + ver + ", entry=" + entry + ']');
                }
            }
        }
        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);
                // We don't wait for reply to this message.
                ctx.io().send(n, req, ctx.ioPolicy());
            }
        }
    } catch (IgniteCheckedException ex) {
        U.error(log, "Failed to unlock the lock for keys: " + keys, ex);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) 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) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) Map(java.util.Map) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)

Example 4 with GridDistributedUnlockRequest

use of org.apache.ignite.internal.processors.cache.distributed.GridDistributedUnlockRequest 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.affinityNodes(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)

Aggregations

Map (java.util.Map)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 GridCacheMvccCandidate (org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)4 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)4 GridDistributedUnlockRequest (org.apache.ignite.internal.processors.cache.distributed.GridDistributedUnlockRequest)4 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)4 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)3 GridDistributedCacheEntry (org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry)3 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)2 GridCacheConcurrentMap (org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)2 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)2 GridNearUnlockRequest (org.apache.ignite.internal.processors.cache.distributed.near.GridNearUnlockRequest)2 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)2 ArrayList (java.util.ArrayList)1 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)1