Search in sources :

Example 6 with EntryGetResult

use of org.apache.ignite.internal.processors.cache.EntryGetResult 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) {
        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, subjId, 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, subjId, 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.cache().configuration().isStatisticsEnabled() && !skipVals)
                    cache().metrics0().onRead(true);
                addResult(key, v, ver);
                return true;
            } else {
                boolean topStable = cctx.isReplicated() || topVer.equals(cctx.topology().topologyVersion());
                // 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 {
            if (dhtEntry != null)
                // Near cache is enabled, so near entry will be enlisted in the transaction.
                // Always touch DHT entry in this case.
                dht.context().evicts().touch(dhtEntry, topVer);
        }
    }
}
Also used : GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.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 7 with EntryGetResult

use of org.apache.ignite.internal.processors.cache.EntryGetResult 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.
     */
@SuppressWarnings("unchecked")
private Map<KeyCacheObject, GridNearCacheEntry> map(KeyCacheObject key, Map<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>> mappings, AffinityTopologyVersion topVer, 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(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, subjId, 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, subjId, null, taskName, expiryPlc, !deserializeBinary);
                }
            }
            if (v == null) {
                boolean fastLocGet = allowLocRead && cctx.allowFastLocalRead(part, affNodes, topVer);
                if (fastLocGet && localDhtGet(key, part, topVer, isNear))
                    break;
                ClusterNode affNode = affinityNode(affNodes);
                if (affNode == null) {
                    onDone(serverNotFoundError(topVer));
                    return saved;
                }
                if (cctx.cache().configuration().isStatisticsEnabled() && !skipVals && !affNode.isLocal())
                    cache().metrics0().onRead(false);
                LinkedHashMap<KeyCacheObject, Boolean> keys = mapped.get(affNode);
                if (keys != null && keys.containsKey(key)) {
                    if (REMAP_CNT_UPD.incrementAndGet(this) > MAX_REMAP_CNT) {
                        onDone(new ClusterTopologyCheckedException("Failed to remap key to a new node after " + MAX_REMAP_CNT + " attempts (key got remapped to the same node) " + "[key=" + key + ", node=" + U.toShortString(affNode) + ", mappings=" + 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)
                cctx.evicts().touch(entry, topVer);
        }
    }
    return saved;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) 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) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 8 with EntryGetResult

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

the class GridNearTxLocal method enlistRead.

/**
     * @param cacheCtx Cache context.
     * @param keys Key to enlist.
     * @param expiryPlc Explicitly specified expiry policy for entry.
     * @param map Return map.
     * @param missed Map of missed keys.
     * @param keysCnt Keys count (to avoid call to {@code Collection.size()}).
     * @param deserializeBinary Deserialize binary flag.
     * @param skipVals Skip values flag.
     * @param keepCacheObjects Keep cache objects flag.
     * @param skipStore Skip store flag.
     * @throws IgniteCheckedException If failed.
     * @return Enlisted keys.
     */
@SuppressWarnings({ "RedundantTypeArguments" })
private <K, V> Collection<KeyCacheObject> enlistRead(final GridCacheContext cacheCtx, @Nullable AffinityTopologyVersion entryTopVer, Collection<KeyCacheObject> keys, @Nullable ExpiryPolicy expiryPlc, Map<K, V> map, Map<KeyCacheObject, GridCacheVersion> missed, int keysCnt, boolean deserializeBinary, boolean skipVals, boolean keepCacheObjects, boolean skipStore, boolean recovery, final boolean needVer) throws IgniteCheckedException {
    assert !F.isEmpty(keys);
    assert keysCnt == keys.size();
    cacheCtx.checkSecurity(SecurityPermission.CACHE_READ);
    boolean single = keysCnt == 1;
    Collection<KeyCacheObject> lockKeys = null;
    AffinityTopologyVersion topVer = entryTopVer != null ? entryTopVer : topologyVersion();
    boolean needReadVer = (serializable() && optimistic()) || needVer;
    // outside of this loop.
    for (KeyCacheObject key : keys) {
        if ((pessimistic() || needReadVer) && !readCommitted() && !skipVals)
            addActiveCache(cacheCtx, recovery);
        IgniteTxKey txKey = cacheCtx.txKey(key);
        // Check write map (always check writes first).
        IgniteTxEntry txEntry = entry(txKey);
        // Either non-read-committed or there was a previous write.
        if (txEntry != null) {
            CacheObject val = txEntry.value();
            if (txEntry.hasValue()) {
                if (!F.isEmpty(txEntry.entryProcessors()))
                    val = txEntry.applyEntryProcessors(val);
                if (val != null) {
                    GridCacheVersion ver = null;
                    if (needVer) {
                        if (txEntry.op() != READ)
                            ver = IgniteTxEntry.GET_ENTRY_INVALID_VER_UPDATED;
                        else {
                            ver = txEntry.entryReadVersion();
                            if (ver == null && pessimistic()) {
                                while (true) {
                                    try {
                                        GridCacheEntryEx cached = txEntry.cached();
                                        ver = cached.isNear() ? ((GridNearCacheEntry) cached).dhtVersion() : cached.version();
                                        break;
                                    } catch (GridCacheEntryRemovedException ignored) {
                                        txEntry.cached(entryEx(cacheCtx, txEntry.txKey(), topVer));
                                    }
                                }
                            }
                            if (ver == null) {
                                assert optimistic() && repeatableRead() : this;
                                ver = IgniteTxEntry.GET_ENTRY_INVALID_VER_AFTER_GET;
                            }
                        }
                        assert ver != null;
                    }
                    cacheCtx.addResult(map, key, val, skipVals, keepCacheObjects, deserializeBinary, false, ver, 0, 0);
                }
            } else {
                assert txEntry.op() == TRANSFORM;
                while (true) {
                    try {
                        GridCacheVersion readVer = null;
                        EntryGetResult getRes = null;
                        Object transformClo = (txEntry.op() == TRANSFORM && cctx.gridEvents().isRecordable(EVT_CACHE_OBJECT_READ)) ? F.first(txEntry.entryProcessors()) : null;
                        if (needVer) {
                            getRes = txEntry.cached().innerGetVersioned(null, this, /*update-metrics*/
                            true, /*event*/
                            !skipVals, CU.subjectId(this, cctx), transformClo, resolveTaskName(), null, txEntry.keepBinary(), null);
                            if (getRes != null) {
                                val = getRes.value();
                                readVer = getRes.version();
                            }
                        } else {
                            val = txEntry.cached().innerGet(null, this, /*read-through*/
                            false, /*metrics*/
                            true, /*event*/
                            !skipVals, CU.subjectId(this, cctx), transformClo, resolveTaskName(), null, txEntry.keepBinary());
                        }
                        if (val != null) {
                            if (!readCommitted() && !skipVals)
                                txEntry.readValue(val);
                            if (!F.isEmpty(txEntry.entryProcessors()))
                                val = txEntry.applyEntryProcessors(val);
                            cacheCtx.addResult(map, key, val, skipVals, keepCacheObjects, deserializeBinary, false, getRes, readVer, 0, 0, needVer);
                        } else
                            missed.put(key, txEntry.cached().version());
                        break;
                    } catch (GridCacheEntryRemovedException ignored) {
                        txEntry.cached(entryEx(cacheCtx, txEntry.txKey(), topVer));
                    }
                }
            }
        } else // First time access within transaction.
        {
            if (lockKeys == null && !skipVals)
                lockKeys = single ? Collections.singleton(key) : new ArrayList<KeyCacheObject>(keysCnt);
            if (!single && !skipVals)
                lockKeys.add(key);
            while (true) {
                GridCacheEntryEx entry = entryEx(cacheCtx, txKey, topVer);
                try {
                    GridCacheVersion ver = entry.version();
                    CacheObject val = null;
                    GridCacheVersion readVer = null;
                    EntryGetResult getRes = null;
                    if (!pessimistic() || readCommitted() && !skipVals) {
                        IgniteCacheExpiryPolicy accessPlc = optimistic() ? accessPolicy(cacheCtx, txKey, expiryPlc) : null;
                        if (needReadVer) {
                            getRes = primaryLocal(entry) ? entry.innerGetVersioned(null, this, /*metrics*/
                            true, /*event*/
                            true, CU.subjectId(this, cctx), null, resolveTaskName(), accessPlc, !deserializeBinary, null) : null;
                            if (getRes != null) {
                                val = getRes.value();
                                readVer = getRes.version();
                            }
                        } else {
                            val = entry.innerGet(null, this, /*read-through*/
                            false, /*metrics*/
                            true, /*event*/
                            !skipVals, CU.subjectId(this, cctx), null, resolveTaskName(), accessPlc, !deserializeBinary);
                        }
                        if (val != null) {
                            cacheCtx.addResult(map, key, val, skipVals, keepCacheObjects, deserializeBinary, false, getRes, readVer, 0, 0, needVer);
                        } else
                            missed.put(key, ver);
                    } else
                        // We must wait for the lock in pessimistic mode.
                        missed.put(key, ver);
                    if (!readCommitted() && !skipVals) {
                        txEntry = addEntry(READ, val, null, null, entry, expiryPlc, null, true, -1L, -1L, null, skipStore, !deserializeBinary);
                        // for non-pessimistic if value is not null.
                        if (val != null && !pessimistic()) {
                            txEntry.markValid();
                            if (needReadVer) {
                                assert readVer != null;
                                txEntry.entryReadVersion(readVer);
                            }
                        }
                    }
                    // While.
                    break;
                } catch (GridCacheEntryRemovedException ignored) {
                    if (log.isDebugEnabled())
                        log.debug("Got removed entry in transaction getAllAsync(..) (will retry): " + key);
                } finally {
                    if (entry != null && readCommitted()) {
                        if (cacheCtx.isNear()) {
                            if (cacheCtx.affinity().partitionBelongs(cacheCtx.localNode(), entry.partition(), topVer)) {
                                if (entry.markObsolete(xidVer))
                                    cacheCtx.cache().removeEntry(entry);
                            }
                        } else
                            entry.context().evicts().touch(entry, topVer);
                    }
                }
            }
        }
    }
    return lockKeys != null ? lockKeys : Collections.<KeyCacheObject>emptyList();
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) 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) 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) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)

Example 9 with EntryGetResult

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

the class GridNearTxLocal method getAllAsync.

/**
     * @param cacheCtx Cache context.
     * @param keys Keys to get.
     * @param deserializeBinary Deserialize binary flag.
     * @param skipVals Skip values flag.
     * @param keepCacheObjects Keep cache objects
     * @param skipStore Skip store flag.
     * @return Future for this get.
     */
@SuppressWarnings("unchecked")
public <K, V> IgniteInternalFuture<Map<K, V>> getAllAsync(final GridCacheContext cacheCtx, @Nullable final AffinityTopologyVersion entryTopVer, Collection<KeyCacheObject> keys, final boolean deserializeBinary, final boolean skipVals, final boolean keepCacheObjects, final boolean skipStore, final boolean recovery, final boolean needVer) {
    if (F.isEmpty(keys))
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    init();
    int keysCnt = keys.size();
    boolean single = keysCnt == 1;
    try {
        checkValid();
        final Map<K, V> retMap = new GridLeanMap<>(keysCnt);
        final Map<KeyCacheObject, GridCacheVersion> missed = new GridLeanMap<>(pessimistic() ? keysCnt : 0);
        CacheOperationContext opCtx = cacheCtx.operationContextPerCall();
        ExpiryPolicy expiryPlc = opCtx != null ? opCtx.expiry() : null;
        final Collection<KeyCacheObject> lockKeys = enlistRead(cacheCtx, entryTopVer, keys, expiryPlc, retMap, missed, keysCnt, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, needVer);
        if (single && missed.isEmpty())
            return new GridFinishedFuture<>(retMap);
        // Handle locks.
        if (pessimistic() && !readCommitted() && !skipVals) {
            if (expiryPlc == null)
                expiryPlc = cacheCtx.expiry();
            long accessTtl = expiryPlc != null ? CU.toTtl(expiryPlc.getExpiryForAccess()) : CU.TTL_NOT_CHANGED;
            long createTtl = expiryPlc != null ? CU.toTtl(expiryPlc.getExpiryForCreation()) : CU.TTL_NOT_CHANGED;
            long timeout = remainingTime();
            if (timeout == -1)
                return new GridFinishedFuture<>(timeoutException());
            IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(lockKeys, timeout, this, true, true, isolation, isInvalidate(), createTtl, accessTtl);
            final ExpiryPolicy expiryPlc0 = expiryPlc;
            PLC2<Map<K, V>> plc2 = new PLC2<Map<K, V>>() {

                @Override
                public IgniteInternalFuture<Map<K, V>> postLock() throws IgniteCheckedException {
                    if (log.isDebugEnabled())
                        log.debug("Acquired transaction lock for read on keys: " + lockKeys);
                    // Load keys only after the locks have been acquired.
                    for (KeyCacheObject cacheKey : lockKeys) {
                        K keyVal = (K) (keepCacheObjects ? cacheKey : cacheCtx.cacheObjectContext().unwrapBinaryIfNeeded(cacheKey, !deserializeBinary, true));
                        if (retMap.containsKey(keyVal))
                            // We already have a return value.
                            continue;
                        IgniteTxKey txKey = cacheCtx.txKey(cacheKey);
                        IgniteTxEntry txEntry = entry(txKey);
                        assert txEntry != null;
                        // Check if there is cached value.
                        while (true) {
                            GridCacheEntryEx cached = txEntry.cached();
                            CacheObject val = null;
                            GridCacheVersion readVer = null;
                            EntryGetResult getRes = null;
                            try {
                                Object transformClo = (!F.isEmpty(txEntry.entryProcessors()) && cctx.gridEvents().isRecordable(EVT_CACHE_OBJECT_READ)) ? F.first(txEntry.entryProcessors()) : null;
                                if (needVer) {
                                    getRes = cached.innerGetVersioned(null, GridNearTxLocal.this, /*update-metrics*/
                                    true, /*event*/
                                    !skipVals, CU.subjectId(GridNearTxLocal.this, cctx), transformClo, resolveTaskName(), null, txEntry.keepBinary(), null);
                                    if (getRes != null) {
                                        val = getRes.value();
                                        readVer = getRes.version();
                                    }
                                } else {
                                    val = cached.innerGet(null, GridNearTxLocal.this, /*read through*/
                                    false, /*metrics*/
                                    true, /*events*/
                                    !skipVals, CU.subjectId(GridNearTxLocal.this, cctx), transformClo, resolveTaskName(), null, txEntry.keepBinary());
                                }
                                // If value is in cache and passed the filter.
                                if (val != null) {
                                    missed.remove(cacheKey);
                                    txEntry.setAndMarkValid(val);
                                    if (!F.isEmpty(txEntry.entryProcessors()))
                                        val = txEntry.applyEntryProcessors(val);
                                    cacheCtx.addResult(retMap, cacheKey, val, skipVals, keepCacheObjects, deserializeBinary, false, getRes, readVer, 0, 0, needVer);
                                    if (readVer != null)
                                        txEntry.entryReadVersion(readVer);
                                }
                                // While.
                                break;
                            } catch (GridCacheEntryRemovedException ignore) {
                                if (log.isDebugEnabled())
                                    log.debug("Got removed exception in get postLock (will retry): " + cached);
                                txEntry.cached(entryEx(cacheCtx, txKey, topologyVersion()));
                            }
                        }
                    }
                    if (!missed.isEmpty() && cacheCtx.isLocal()) {
                        AffinityTopologyVersion topVer = topologyVersionSnapshot();
                        if (topVer == null)
                            topVer = entryTopVer;
                        return checkMissed(cacheCtx, topVer != null ? topVer : topologyVersion(), retMap, missed, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, needVer, expiryPlc0);
                    }
                    return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
                }
            };
            FinishClosure<Map<K, V>> finClos = new FinishClosure<Map<K, V>>() {

                @Override
                Map<K, V> finish(Map<K, V> loaded) {
                    retMap.putAll(loaded);
                    return retMap;
                }
            };
            if (fut.isDone()) {
                try {
                    IgniteInternalFuture<Map<K, V>> fut1 = plc2.apply(fut.get(), null);
                    return fut1.isDone() ? new GridFinishedFuture<>(finClos.apply(fut1.get(), null)) : new GridEmbeddedFuture<>(finClos, fut1);
                } catch (GridClosureException e) {
                    return new GridFinishedFuture<>(e.unwrap());
                } catch (IgniteCheckedException e) {
                    try {
                        return plc2.apply(false, e);
                    } catch (Exception e1) {
                        return new GridFinishedFuture<>(e1);
                    }
                }
            } else {
                return new GridEmbeddedFuture<>(fut, plc2, finClos);
            }
        } else {
            assert optimistic() || readCommitted() || skipVals;
            if (!missed.isEmpty()) {
                if (!readCommitted())
                    for (Iterator<KeyCacheObject> it = missed.keySet().iterator(); it.hasNext(); ) {
                        KeyCacheObject cacheKey = it.next();
                        K keyVal = (K) (keepCacheObjects ? cacheKey : cacheCtx.cacheObjectContext().unwrapBinaryIfNeeded(cacheKey, !deserializeBinary, false));
                        if (retMap.containsKey(keyVal))
                            it.remove();
                    }
                if (missed.isEmpty())
                    return new GridFinishedFuture<>(retMap);
                AffinityTopologyVersion topVer = topologyVersionSnapshot();
                if (topVer == null)
                    topVer = entryTopVer;
                return checkMissed(cacheCtx, topVer != null ? topVer : topologyVersion(), retMap, missed, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, needVer, expiryPlc);
            }
            return new GridFinishedFuture<>(retMap);
        }
    } catch (IgniteCheckedException e) {
        setRollbackOnly();
        return new GridFinishedFuture<>(e);
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) ROLLING_BACK(org.apache.ignite.transactions.TransactionState.ROLLING_BACK) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Iterator(java.util.Iterator) 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) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap)

Example 10 with EntryGetResult

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

the class GridNearTxLocal method localCacheLoadMissing.

/**
     * @param cacheCtx  Cache context.
     * @param readThrough Read through flag.
     * @param async if {@code True}, then loading will happen in a separate thread.
     * @param keys Keys.
     * @param skipVals Skip values flag.
     * @param needVer If {@code true} version is required for loaded values.
     * @param c Closure to be applied for loaded values.
     * @param expiryPlc Expiry policy.
     * @return Future with {@code True} value if loading took place.
     */
private IgniteInternalFuture<Void> localCacheLoadMissing(final GridCacheContext cacheCtx, final AffinityTopologyVersion topVer, final boolean readThrough, boolean async, final Collection<KeyCacheObject> keys, boolean skipVals, boolean needVer, boolean keepBinary, boolean recovery, final ExpiryPolicy expiryPlc, final GridInClosure3<KeyCacheObject, Object, GridCacheVersion> c) {
    assert cacheCtx.isLocal() : cacheCtx.name();
    if (!readThrough || !cacheCtx.readThrough()) {
        for (KeyCacheObject key : keys) c.apply(key, null, SER_READ_EMPTY_ENTRY_VER);
        return new GridFinishedFuture<>();
    }
    try {
        IgniteCacheExpiryPolicy expiryPlc0 = optimistic() ? accessPolicy(cacheCtx, keys) : cacheCtx.cache().expiryPolicy(expiryPlc);
        Map<KeyCacheObject, GridCacheVersion> misses = null;
        for (KeyCacheObject key : keys) {
            while (true) {
                IgniteTxEntry txEntry = entry(cacheCtx.txKey(key));
                GridCacheEntryEx entry = txEntry == null ? cacheCtx.cache().entryEx(key) : txEntry.cached();
                if (entry == null)
                    continue;
                try {
                    EntryGetResult res = entry.innerGetVersioned(null, this, /*update-metrics*/
                    !skipVals, /*event*/
                    !skipVals, CU.subjectId(this, cctx), null, resolveTaskName(), expiryPlc0, txEntry == null ? keepBinary : txEntry.keepBinary(), null);
                    if (res == null) {
                        if (misses == null)
                            misses = new LinkedHashMap<>();
                        misses.put(key, entry.version());
                    } else
                        c.apply(key, skipVals ? true : res.value(), res.version());
                    break;
                } catch (GridCacheEntryRemovedException ignore) {
                    if (log.isDebugEnabled())
                        log.debug("Got removed entry, will retry: " + key);
                    if (txEntry != null)
                        txEntry.cached(cacheCtx.cache().entryEx(key, topologyVersion()));
                }
            }
        }
        if (misses != null) {
            final Map<KeyCacheObject, GridCacheVersion> misses0 = misses;
            cacheCtx.store().loadAll(this, misses.keySet(), new CI2<KeyCacheObject, Object>() {

                @Override
                public void apply(KeyCacheObject key, Object val) {
                    GridCacheVersion ver = misses0.remove(key);
                    assert ver != null : key;
                    if (val != null) {
                        CacheObject cacheVal = cacheCtx.toCacheObject(val);
                        while (true) {
                            GridCacheEntryEx entry = cacheCtx.cache().entryEx(key, topVer);
                            try {
                                cacheCtx.shared().database().ensureFreeSpace(cacheCtx.memoryPolicy());
                                EntryGetResult verVal = entry.versionedValue(cacheVal, ver, null, null, null);
                                if (log.isDebugEnabled()) {
                                    log.debug("Set value loaded from store into entry [" + "oldVer=" + ver + ", newVer=" + verVal.version() + ", entry=" + entry + ']');
                                }
                                ver = verVal.version();
                                break;
                            } catch (GridCacheEntryRemovedException ignore) {
                                if (log.isDebugEnabled())
                                    log.debug("Got removed entry, (will retry): " + entry);
                            } catch (IgniteCheckedException e) {
                                // Wrap errors (will be unwrapped).
                                throw new GridClosureException(e);
                            }
                        }
                    } else
                        ver = SER_READ_EMPTY_ENTRY_VER;
                    c.apply(key, val, ver);
                }
            });
            for (KeyCacheObject key : misses0.keySet()) c.apply(key, null, SER_READ_EMPTY_ENTRY_VER);
        }
        return new GridFinishedFuture<>();
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture<>(e);
    }
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) LinkedHashMap(java.util.LinkedHashMap) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) 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) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)

Aggregations

EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)17 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)17 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)15 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)14 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)10 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)10 IgniteCacheExpiryPolicy (org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)5 CacheDataRow (org.apache.ignite.internal.processors.cache.database.CacheDataRow)5 Map (java.util.Map)4 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)4 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)4 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)3 GridCacheEntryInfo (org.apache.ignite.internal.processors.cache.GridCacheEntryInfo)3 GridDhtInvalidPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException)3 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)3 GridClosureException (org.apache.ignite.internal.util.lang.GridClosureException)3 LinkedHashMap (java.util.LinkedHashMap)2