Search in sources :

Example 71 with KeyCacheObject

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

the class GridNearAtomicCache method processNearAtomicUpdateResponse.

/**
 * @param req Update request.
 * @param res Update response.
 */
public void processNearAtomicUpdateResponse(GridNearAtomicAbstractUpdateRequest req, GridNearAtomicUpdateResponse res) {
    if (F.size(res.failedKeys()) == req.size())
        return;
    /*
         * Choose value to be stored in near cache: first check key is not in failed and not in skipped list,
         * then check if value was generated on primary node, if not then use value sent in request.
         */
    Collection<KeyCacheObject> failed = res.failedKeys();
    List<Integer> nearValsIdxs = res.nearValuesIndexes();
    List<Integer> skipped = res.skippedIndexes();
    GridCacheVersion ver = res.nearVersion();
    assert ver != null : "Failed to find version [req=" + req + ", res=" + res + ']';
    int nearValIdx = 0;
    boolean needTaskName = ctx.events().isRecordable(EVT_CACHE_OBJECT_READ) || ctx.events().isRecordable(EVT_CACHE_OBJECT_PUT) || ctx.events().isRecordable(EVT_CACHE_OBJECT_REMOVED);
    String taskName = needTaskName ? ctx.kernalContext().task().resolveTaskName(req.taskNameHash()) : null;
    for (int i = 0; i < req.size(); i++) {
        if (F.contains(skipped, i))
            continue;
        KeyCacheObject key = req.key(i);
        if (F.contains(failed, key))
            continue;
        // Reader became backup.
        if (ctx.affinity().partitionBelongs(ctx.localNode(), ctx.affinity().partition(key), req.topologyVersion())) {
            GridCacheEntryEx entry = peekEx(key);
            if (entry != null && entry.markObsolete(ver))
                removeEntry(entry);
            continue;
        }
        CacheObject val = null;
        if (F.contains(nearValsIdxs, i)) {
            val = res.nearValue(nearValIdx);
            nearValIdx++;
        } else {
            assert req.operation() != TRANSFORM;
            if (req.operation() != DELETE)
                val = req.value(i);
        }
        long ttl = res.nearTtl(i);
        long expireTime = res.nearExpireTime(i);
        if (ttl != CU.TTL_NOT_CHANGED && expireTime == CU.EXPIRE_TIME_CALCULATE)
            expireTime = CU.toExpireTime(ttl);
        try {
            processNearAtomicUpdateResponse(ver, key, val, ttl, expireTime, req.keepBinary(), req.nodeId(), taskName, req.operation() == TRANSFORM);
        } catch (IgniteCheckedException e) {
            res.addFailedKey(key, new IgniteCheckedException("Failed to update key in near cache: " + key, e));
        }
    }
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 72 with KeyCacheObject

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

the class GridNearGetFuture method loadEntries.

/**
 * @param nodeId Node id.
 * @param keys Keys.
 * @param infos Entry infos.
 * @param savedEntries Saved entries.
 * @param topVer Topology version
 * @return Result map.
 */
private Map<K, V> loadEntries(UUID nodeId, Collection<KeyCacheObject> keys, Collection<GridCacheEntryInfo> infos, Map<KeyCacheObject, GridNearCacheEntry> savedEntries, AffinityTopologyVersion topVer) {
    boolean empty = F.isEmpty(keys);
    Map<K, V> map = empty ? Collections.<K, V>emptyMap() : new GridLeanMap<K, V>(keys.size());
    if (!empty) {
        boolean atomic = cctx.atomic();
        GridCacheVersion ver = atomic ? null : F.isEmpty(infos) ? null : cctx.cache().nextVersion();
        for (GridCacheEntryInfo info : infos) {
            try {
                info.unmarshalValue(cctx, cctx.deploy().globalLoader());
                // Entries available locally in DHT should not be loaded into near cache for reading.
                if (!cctx.affinity().keyLocalNode(info.key(), cctx.affinity().affinityTopologyVersion())) {
                    GridNearCacheEntry entry = savedEntries.get(info.key());
                    if (entry == null)
                        entry = cache().entryExx(info.key(), topVer);
                    // Load entry into cache.
                    entry.loadedValue(tx, nodeId, info.value(), atomic ? info.version() : ver, info.version(), info.ttl(), info.expireTime(), true, !deserializeBinary, topVer);
                }
                CacheObject val = info.value();
                KeyCacheObject key = info.key();
                assert skipVals == (info.value() == null);
                cctx.addResult(map, key, val, skipVals, keepCacheObjects, deserializeBinary, false, needVer ? info.version() : null, 0, 0, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId));
            } catch (GridCacheEntryRemovedException ignore) {
                if (log.isDebugEnabled())
                    log.debug("Got removed entry while processing get response (will not retry).");
            } catch (Exception e) {
                // Fail.
                onDone(e);
                return Collections.emptyMap();
            }
        }
    }
    return map;
}
Also used : GridCacheEntryInfo(org.apache.ignite.internal.processors.cache.GridCacheEntryInfo) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 73 with KeyCacheObject

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

the class GridNearGetFuture method localDhtGet.

/**
 * @param key Key.
 * @param part Partition.
 * @param topVer Topology version.
 * @param nearRead {@code True} if already tried to read from near cache.
 * @return {@code True} if there is no need to further search value.
 */
private boolean localDhtGet(KeyCacheObject key, int part, AffinityTopologyVersion topVer, boolean nearRead) {
    GridDhtCacheAdapter<K, V> dht = cache().dht();
    assert dht.context().affinityNode() : this;
    while (true) {
        cctx.shared().database().checkpointReadLock();
        GridCacheEntryEx dhtEntry = null;
        try {
            dhtEntry = dht.entryEx(key);
            CacheObject v = null;
            // If near cache does not have value, then we peek DHT cache.
            if (dhtEntry != null) {
                boolean isNew = dhtEntry.isNewLocked() || !dhtEntry.valid(topVer);
                if (needVer) {
                    EntryGetResult res = dhtEntry.innerGetVersioned(null, null, /*update-metrics*/
                    false, /*event*/
                    !nearRead && !skipVals, null, taskName, expiryPlc, !deserializeBinary, null);
                    if (res != null) {
                        v = res.value();
                        ver = res.version();
                    }
                } else {
                    v = dhtEntry.innerGet(null, tx, /*read-through*/
                    false, /*update-metrics*/
                    false, /*events*/
                    !nearRead && !skipVals, null, taskName, expiryPlc, !deserializeBinary);
                }
                // Entry was not in memory or in swap, so we remove it from cache.
                if (v == null && isNew && dhtEntry.markObsoleteIfEmpty(ver))
                    dht.removeEntry(dhtEntry);
            }
            if (v != null) {
                if (cctx.statisticsEnabled() && !skipVals)
                    cache().metrics0().onRead(true);
                addResult(key, v, ver);
                return true;
            } else {
                boolean topStable = cctx.isReplicated() || topVer.equals(cctx.topology().lastTopologyChangeVersion());
                // Entry not found, do not continue search if topology did not change and there is no store.
                return !cctx.readThroughConfigured() && (topStable || partitionOwned(part));
            }
        } catch (GridCacheEntryRemovedException ignored) {
        // Retry.
        } catch (GridDhtInvalidPartitionException ignored) {
            return false;
        } catch (IgniteCheckedException e) {
            onDone(e);
            return false;
        } finally {
            cctx.shared().database().checkpointReadUnlock();
            if (dhtEntry != null)
                // Near cache is enabled, so near entry will be enlisted in the transaction.
                // Always touch DHT entry in this case.
                dhtEntry.touch();
        }
    }
}
Also used : GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 74 with KeyCacheObject

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

the class GridNearGetFuture method map.

/**
 * @param mappings Mappings.
 * @param key Key to map.
 * @param topVer Topology version
 * @param mapped Previously mapped.
 * @param saved Reserved near cache entries.
 * @return Map.
 */
private Map<KeyCacheObject, GridNearCacheEntry> map(KeyCacheObject key, AffinityTopologyVersion topVer, Map<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>> mappings, Map<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>> mapped, Map<KeyCacheObject, GridNearCacheEntry> saved) {
    int part = cctx.affinity().partition(key);
    List<ClusterNode> affNodes = cctx.affinity().nodesByPartition(part, topVer);
    if (affNodes.isEmpty()) {
        onDone(serverNotFoundError(part, topVer));
        return null;
    }
    final GridNearCacheAdapter near = cache();
    // Allow to get cached value from the local node.
    boolean allowLocRead = !forcePrimary || cctx.localNode().equals(affNodes.get(0));
    while (true) {
        GridNearCacheEntry entry = allowLocRead ? (GridNearCacheEntry) near.peekEx(key) : null;
        try {
            CacheObject v = null;
            GridCacheVersion ver = null;
            boolean isNear = entry != null;
            // First we peek into near cache.
            if (isNear) {
                if (needVer) {
                    EntryGetResult res = entry.innerGetVersioned(null, null, /*update-metrics*/
                    true, /*event*/
                    !skipVals, null, taskName, expiryPlc, !deserializeBinary, null);
                    if (res != null) {
                        v = res.value();
                        ver = res.version();
                    }
                } else {
                    v = entry.innerGet(null, tx, /*read-through*/
                    false, /*metrics*/
                    true, /*events*/
                    !skipVals, null, taskName, expiryPlc, !deserializeBinary);
                }
            }
            if (v == null) {
                boolean fastLocGet = allowLocRead && cctx.reserveForFastLocalGet(part, topVer);
                if (fastLocGet) {
                    try {
                        if (localDhtGet(key, part, topVer, isNear))
                            break;
                    } catch (IgniteException ex) {
                        onDone(ex);
                        return saved;
                    } finally {
                        cctx.releaseForFastLocalGet(part, topVer);
                    }
                }
                Set<ClusterNode> invalidNodesSet = getInvalidNodes(part, topVer);
                ClusterNode affNode = cctx.selectAffinityNodeBalanced(affNodes, invalidNodesSet, part, canRemap, forcePrimary);
                if (affNode == null) {
                    onDone(serverNotFoundError(part, topVer));
                    return saved;
                }
                if (cctx.statisticsEnabled() && !skipVals && !affNode.isLocal() && !isNear)
                    cache().metrics0().onRead(false);
                if (!checkRetryPermits(key, affNode, mapped))
                    return saved;
                if (!affNodes.contains(cctx.localNode())) {
                    GridNearCacheEntry nearEntry = entry != null ? entry : near.entryExx(key, topVer);
                    nearEntry.reserveEviction();
                    entry = null;
                    if (saved == null)
                        saved = U.newHashMap(3);
                    saved.put(key, nearEntry);
                }
                // Don't add reader if transaction acquires lock anyway to avoid deadlock.
                boolean addRdr = tx == null || tx.optimistic();
                if (!addRdr && tx.readCommitted() && !tx.writeSet().contains(cctx.txKey(key)))
                    addRdr = true;
                LinkedHashMap<KeyCacheObject, Boolean> old = mappings.get(affNode);
                if (old == null)
                    mappings.put(affNode, old = new LinkedHashMap<>(3, 1f));
                old.put(key, addRdr);
            } else
                addResult(key, v, ver);
            break;
        } catch (IgniteCheckedException e) {
            onDone(e);
            break;
        } catch (GridCacheEntryRemovedException ignored) {
        // Retry.
        } finally {
            if (entry != null && tx == null)
                entry.touch();
        }
    }
    return saved;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 75 with KeyCacheObject

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

the class GridNearTransactionalCache method clearLocks.

/**
 * @param nodeId Node ID.
 * @param req Request.
 */
public void clearLocks(UUID nodeId, GridDhtUnlockRequest req) {
    assert nodeId != null;
    GridCacheVersion obsoleteVer = nextVersion();
    List<KeyCacheObject> keys = req.nearKeys();
    if (keys != null) {
        AffinityTopologyVersion topVer = ctx.affinity().affinityTopologyVersion();
        for (KeyCacheObject key : keys) {
            while (true) {
                GridDistributedCacheEntry entry = peekExx(key);
                try {
                    if (entry != null) {
                        entry.doneRemote(req.version(), req.version(), null, req.committedVersions(), req.rolledbackVersions(), /*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 + ']');
                            // Try to evict near entry dht-mapped locally.
                            evictNearEntry(entry, obsoleteVer, topVer);
                        } else {
                            if (log.isDebugEnabled())
                                log.debug("Received unlock request for unknown candidate " + "(added to cancelled locks set): " + req);
                        }
                        entry.touch();
                    } else if (log.isDebugEnabled())
                        log.debug("Received unlock request for entry that could not be found: " + req);
                    break;
                } catch (GridCacheEntryRemovedException ignored) {
                    if (log.isDebugEnabled())
                        log.debug("Received remove lock request for removed entry (will retry) [entry=" + entry + ", req=" + req + ']');
                }
            }
        }
    }
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Aggregations

KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)171 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)99 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)92 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)73 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)58 ArrayList (java.util.ArrayList)50 Map (java.util.Map)39 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)37 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)34 ClusterNode (org.apache.ignite.cluster.ClusterNode)33 HashMap (java.util.HashMap)32 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)31 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)29 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)27 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)27 IgniteException (org.apache.ignite.IgniteException)25 LinkedHashMap (java.util.LinkedHashMap)24 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)24 GridDhtInvalidPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException)21 GridCacheOperation (org.apache.ignite.internal.processors.cache.GridCacheOperation)20