Search in sources :

Example 96 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridNearAtomicUpdateFuture method onPrimaryResponse.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked", "ThrowableResultOfMethodCallIgnored" })
@Override
public void onPrimaryResponse(UUID nodeId, GridNearAtomicUpdateResponse res, boolean nodeErr) {
    GridNearAtomicAbstractUpdateRequest req;
    AffinityTopologyVersion remapTopVer0 = null;
    GridCacheReturn opRes0 = null;
    CachePartialUpdateCheckedException err0 = null;
    boolean rcvAll;
    synchronized (this) {
        if (!checkFutureId(res.futureId()))
            return;
        if (singleReq != null) {
            req = singleReq.processPrimaryResponse(nodeId, res);
            if (req == null)
                return;
            rcvAll = singleReq.onPrimaryResponse(res, cctx);
        } else {
            if (mappings == null)
                return;
            PrimaryRequestState reqState = mappings.get(nodeId);
            if (reqState == null)
                return;
            req = reqState.processPrimaryResponse(nodeId, res);
            if (req != null) {
                if (reqState.onPrimaryResponse(res, cctx)) {
                    assert mappings.size() > resCnt : "[mappings=" + mappings.size() + ", cnt=" + resCnt + ']';
                    resCnt++;
                    rcvAll = mappings.size() == resCnt;
                } else {
                    assert mappings.size() > resCnt : "[mappings=" + mappings.size() + ", cnt=" + resCnt + ']';
                    rcvAll = false;
                }
            } else
                return;
        }
        assert req.topologyVersion().equals(topVer) : req;
        if (res.remapTopologyVersion() != null) {
            assert !req.topologyVersion().equals(res.remapTopologyVersion());
            if (remapKeys == null)
                remapKeys = U.newHashSet(req.size());
            remapKeys.addAll(req.keys());
            if (remapTopVer == null || remapTopVer.compareTo(res.remapTopologyVersion()) < 0)
                remapTopVer = req.topologyVersion();
        } else if (res.error() != null)
            onPrimaryError(req, res);
        else {
            GridCacheReturn ret = res.returnValue();
            if (op == TRANSFORM) {
                if (ret != null) {
                    assert ret.value() == null || ret.value() instanceof Map : ret.value();
                    if (ret.value() != null) {
                        if (opRes != null)
                            opRes.mergeEntryProcessResults(ret);
                        else
                            opRes = ret;
                    }
                }
            } else
                opRes = ret;
        }
        if (rcvAll) {
            remapTopVer0 = onAllReceived();
            if (remapTopVer0 == null) {
                err0 = err;
                opRes0 = opRes;
            }
        }
    }
    if (res.error() != null && res.failedKeys() == null) {
        completeFuture(null, res.error(), res.futureId());
        return;
    }
    if (rcvAll && nearEnabled) {
        if (mappings != null) {
            for (PrimaryRequestState reqState : mappings.values()) {
                GridNearAtomicUpdateResponse res0 = reqState.req.response();
                assert res0 != null : reqState;
                updateNear(reqState.req, res0);
            }
        } else if (!nodeErr)
            updateNear(req, res);
    }
    if (remapTopVer0 != null) {
        waitAndRemap(remapTopVer0);
        return;
    }
    if (rcvAll)
        completeFuture(opRes0, err0, res.futureId());
}
Also used : GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) CachePartialUpdateCheckedException(org.apache.ignite.internal.processors.cache.CachePartialUpdateCheckedException) Map(java.util.Map)

Example 97 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion 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 98 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridDhtColocatedCache method getAsync.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteInternalFuture<V> getAsync(final K key, boolean forcePrimary, boolean skipTx, @Nullable UUID subjId, String taskName, final boolean deserializeBinary, final boolean skipVals, final boolean needVer) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (keyCheck)
        validateCacheKey(key);
    GridNearTxLocal tx = ctx.tm().threadLocalTx(ctx);
    final CacheOperationContext opCtx = ctx.operationContextPerCall();
    final boolean recovery = opCtx != null && opCtx.recovery();
    if (tx != null && !tx.implicit() && !skipTx) {
        return asyncOp(tx, new AsyncOp<V>() {

            @Override
            public IgniteInternalFuture<V> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
                IgniteInternalFuture<Map<Object, Object>> fut = tx.getAllAsync(ctx, readyTopVer, Collections.singleton(ctx.toCacheKeyObject(key)), deserializeBinary, skipVals, false, opCtx != null && opCtx.skipStore(), recovery, needVer);
                return fut.chain(new CX1<IgniteInternalFuture<Map<Object, Object>>, V>() {

                    @SuppressWarnings("unchecked")
                    @Override
                    public V applyx(IgniteInternalFuture<Map<Object, Object>> e) throws IgniteCheckedException {
                        Map<Object, Object> map = e.get();
                        assert map.isEmpty() || map.size() == 1 : map.size();
                        if (skipVals) {
                            Boolean val = map.isEmpty() ? false : (Boolean) F.firstValue(map);
                            return (V) (val);
                        }
                        return (V) F.firstValue(map);
                    }
                });
            }
        }, opCtx, /*retry*/
        false);
    }
    AffinityTopologyVersion topVer = tx == null ? ctx.affinity().affinityTopologyVersion() : tx.topologyVersion();
    subjId = ctx.subjectIdPerCall(subjId, opCtx);
    GridPartitionedSingleGetFuture fut = new GridPartitionedSingleGetFuture(ctx, ctx.toCacheKeyObject(key), topVer, opCtx == null || !opCtx.skipStore(), forcePrimary, subjId, taskName, deserializeBinary, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, /*keepCacheObjects*/
    false, opCtx != null && opCtx.recovery());
    fut.init();
    return (IgniteInternalFuture<V>) fut;
}
Also used : GridPartitionedSingleGetFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedSingleGetFuture) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CX1(org.apache.ignite.internal.util.typedef.CX1) Map(java.util.Map) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)

Example 99 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridDhtColocatedCache method getAllAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<Map<K, V>> getAllAsync(@Nullable final Collection<? extends K> keys, boolean forcePrimary, boolean skipTx, @Nullable UUID subjId, String taskName, final boolean deserializeBinary, final boolean recovery, final boolean skipVals, final boolean needVer) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (F.isEmpty(keys))
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    if (keyCheck)
        validateCacheKeys(keys);
    GridNearTxLocal tx = ctx.tm().threadLocalTx(ctx);
    final CacheOperationContext opCtx = ctx.operationContextPerCall();
    if (tx != null && !tx.implicit() && !skipTx) {
        return asyncOp(tx, new AsyncOp<Map<K, V>>(keys) {

            @Override
            public IgniteInternalFuture<Map<K, V>> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
                return tx.getAllAsync(ctx, readyTopVer, ctx.cacheKeysView(keys), deserializeBinary, skipVals, false, opCtx != null && opCtx.skipStore(), recovery, needVer);
            }
        }, opCtx, /*retry*/
        false);
    }
    AffinityTopologyVersion topVer = tx == null ? ctx.affinity().affinityTopologyVersion() : tx.topologyVersion();
    subjId = ctx.subjectIdPerCall(subjId, opCtx);
    return loadAsync(ctx.cacheKeysView(keys), opCtx == null || !opCtx.skipStore(), forcePrimary, topVer, subjId, taskName, deserializeBinary, recovery, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer);
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Map(java.util.Map) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)

Example 100 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridDhtColocatedLockFuture method map0.

/**
 * @param keys Keys to map.
 * @param remap Remap flag.
 * @param topLocked Topology locked flag.
 * @throws IgniteCheckedException If mapping failed.
 */
private synchronized void map0(Collection<KeyCacheObject> keys, boolean remap, boolean topLocked) throws IgniteCheckedException {
    AffinityTopologyVersion topVer = this.topVer;
    assert topVer != null;
    assert topVer.topologyVersion() > 0;
    if (CU.affinityNodes(cctx, topVer).isEmpty()) {
        onDone(new ClusterTopologyServerNotFoundException("Failed to map keys for cache " + "(all partition nodes left the grid): " + cctx.name()));
        return;
    }
    boolean clientNode = cctx.kernalContext().clientNode();
    assert !remap || (clientNode && (tx == null || !tx.hasRemoteLocks()));
    // First assume this node is primary for all keys passed in.
    if (!clientNode && mapAsPrimary(keys, topVer))
        return;
    mappings = new ArrayDeque<>();
    // Assign keys to primary nodes.
    GridNearLockMapping map = null;
    for (KeyCacheObject key : keys) {
        GridNearLockMapping updated = map(key, map, topVer);
        // If new mapping was created, add to collection.
        if (updated != map) {
            mappings.add(updated);
            if (tx != null && updated.node().isLocal())
                tx.colocatedLocallyMapped(true);
        }
        map = updated;
    }
    if (isDone()) {
        if (log.isDebugEnabled())
            log.debug("Abandoning (re)map because future is done: " + this);
        return;
    }
    if (log.isDebugEnabled())
        log.debug("Starting (re)map for mappings [mappings=" + mappings + ", fut=" + this + ']');
    boolean hasRmtNodes = false;
    boolean first = true;
    // Create mini futures.
    for (Iterator<GridNearLockMapping> iter = mappings.iterator(); iter.hasNext(); ) {
        GridNearLockMapping mapping = iter.next();
        ClusterNode node = mapping.node();
        Collection<KeyCacheObject> mappedKeys = mapping.mappedKeys();
        boolean loc = node.equals(cctx.localNode());
        assert !mappedKeys.isEmpty();
        GridNearLockRequest req = null;
        Collection<KeyCacheObject> distributedKeys = new ArrayList<>(mappedKeys.size());
        for (KeyCacheObject key : mappedKeys) {
            IgniteTxKey txKey = cctx.txKey(key);
            GridDistributedCacheEntry entry = null;
            if (tx != null) {
                IgniteTxEntry txEntry = tx.entry(txKey);
                if (txEntry != null) {
                    entry = (GridDistributedCacheEntry) txEntry.cached();
                    if (entry != null && loc == entry.detached()) {
                        entry = cctx.colocated().entryExx(key, topVer, true);
                        txEntry.cached(entry);
                    }
                }
            }
            boolean explicit;
            while (true) {
                try {
                    if (entry == null)
                        entry = cctx.colocated().entryExx(key, topVer, true);
                    if (!cctx.isAll(entry, filter)) {
                        if (log.isDebugEnabled())
                            log.debug("Entry being locked did not pass filter (will not lock): " + entry);
                        onComplete(false, false, true);
                        return;
                    }
                    assert loc ^ entry.detached() : "Invalid entry [loc=" + loc + ", entry=" + entry + ']';
                    GridCacheMvccCandidate cand = addEntry(entry);
                    // Will either return value from dht cache or null if this is a miss.
                    IgniteBiTuple<GridCacheVersion, CacheObject> val = entry.detached() ? null : ((GridDhtCacheEntry) entry).versionedValue(topVer);
                    GridCacheVersion dhtVer = null;
                    if (val != null) {
                        dhtVer = val.get1();
                        valMap.put(key, val);
                    }
                    if (cand != null && !cand.reentry()) {
                        if (req == null) {
                            boolean clientFirst = false;
                            if (first) {
                                clientFirst = clientNode && !topLocked && (tx == null || !tx.hasRemoteLocks());
                                first = false;
                            }
                            assert !implicitTx() && !implicitSingleTx() : tx;
                            req = new GridNearLockRequest(cctx.cacheId(), topVer, cctx.nodeId(), threadId, futId, lockVer, inTx(), read, retval, isolation(), isInvalidate(), timeout, mappedKeys.size(), inTx() ? tx.size() : mappedKeys.size(), inTx() && tx.syncMode() == FULL_SYNC, inTx() ? tx.subjectId() : null, inTx() ? tx.taskNameHash() : 0, read ? createTtl : -1L, read ? accessTtl : -1L, skipStore, keepBinary, clientFirst, false, cctx.deploymentEnabled());
                            mapping.request(req);
                        }
                        distributedKeys.add(key);
                        if (tx != null)
                            tx.addKeyMapping(txKey, mapping.node());
                        req.addKeyBytes(key, retval, // Include DHT version to match remote DHT entry.
                        dhtVer, cctx);
                    }
                    explicit = inTx() && cand == null;
                    if (explicit)
                        tx.addKeyMapping(txKey, mapping.node());
                    break;
                } catch (GridCacheEntryRemovedException ignored) {
                    if (log.isDebugEnabled())
                        log.debug("Got removed entry in lockAsync(..) method (will retry): " + entry);
                    entry = null;
                }
            }
            // Mark mapping explicit lock flag.
            if (explicit) {
                boolean marked = tx != null && tx.markExplicit(node.id());
                assert tx == null || marked;
            }
        }
        if (!distributedKeys.isEmpty()) {
            mapping.distributedKeys(distributedKeys);
            hasRmtNodes |= !mapping.node().isLocal();
        } else {
            assert mapping.request() == null;
            iter.remove();
        }
    }
    if (hasRmtNodes) {
        trackable = true;
        if (!remap && !cctx.mvcc().addFuture(this))
            throw new IllegalStateException("Duplicate future ID: " + this);
    } else
        trackable = false;
    proceedMapping();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) GridNearLockMapping(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockMapping) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearLockRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockRequest) ArrayList(java.util.ArrayList) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)

Aggregations

AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)242 ClusterNode (org.apache.ignite.cluster.ClusterNode)87 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)75 ArrayList (java.util.ArrayList)60 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)46 List (java.util.List)36 UUID (java.util.UUID)35 Ignite (org.apache.ignite.Ignite)33 Map (java.util.Map)32 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)31 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)30 HashMap (java.util.HashMap)27 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)22 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)20 IgniteKernal (org.apache.ignite.internal.IgniteKernal)20 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)20 IgniteException (org.apache.ignite.IgniteException)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)17 GridAffinityFunctionContextImpl (org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl)15