Search in sources :

Example 11 with EntryGetResult

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

the class GridLocalAtomicCache method getAllInternal.

/**
     * Entry point to all public API get methods.
     *
     * @param keys Keys to remove.
     * @param storeEnabled Store enabled flag.
     * @param taskName Task name.
     * @param deserializeBinary Deserialize binary .
     * @param skipVals Skip value flag.
     * @param needVer Need version.
     * @return Key-value map.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("ConstantConditions")
private Map<K, V> getAllInternal(@Nullable Collection<? extends K> keys, boolean storeEnabled, String taskName, boolean deserializeBinary, boolean skipVals, boolean needVer) throws IgniteCheckedException {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (F.isEmpty(keys))
        return Collections.emptyMap();
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    UUID subjId = ctx.subjectIdPerCall(null, opCtx);
    Map<K, V> vals = U.newHashMap(keys.size());
    if (keyCheck)
        validateCacheKeys(keys);
    final IgniteCacheExpiryPolicy expiry = expiryPolicy(opCtx != null ? opCtx.expiry() : null);
    boolean success = true;
    boolean readNoEntry = ctx.readNoEntry(expiry, false);
    final boolean evt = !skipVals;
    for (K key : keys) {
        if (key == null)
            throw new NullPointerException("Null key.");
        KeyCacheObject cacheKey = ctx.toCacheKeyObject(key);
        boolean skipEntry = readNoEntry;
        if (readNoEntry) {
            CacheDataRow row = ctx.offheap().read(cacheKey);
            if (row != null) {
                long expireTime = row.expireTime();
                if (expireTime == 0 || expireTime > U.currentTimeMillis()) {
                    ctx.addResult(vals, cacheKey, row.value(), skipVals, false, deserializeBinary, true, null, row.version(), 0, 0, needVer);
                    if (configuration().isStatisticsEnabled() && !skipVals)
                        metrics0().onRead(true);
                    if (evt) {
                        ctx.events().readEvent(cacheKey, null, row.value(), subjId, taskName, !deserializeBinary);
                    }
                } else
                    skipEntry = false;
            } else
                success = false;
        }
        if (!skipEntry) {
            GridCacheEntryEx entry = null;
            while (true) {
                try {
                    entry = entryEx(cacheKey);
                    if (entry != null) {
                        CacheObject v;
                        if (needVer) {
                            EntryGetResult res = entry.innerGetVersioned(null, null, /*update-metrics*/
                            false, /*event*/
                            evt, subjId, null, taskName, expiry, !deserializeBinary, null);
                            if (res != null) {
                                ctx.addResult(vals, cacheKey, res, skipVals, false, deserializeBinary, true, needVer);
                            } else
                                success = false;
                        } else {
                            v = entry.innerGet(null, null, /*read-through*/
                            false, /*update-metrics*/
                            true, /*event*/
                            evt, subjId, null, taskName, expiry, !deserializeBinary);
                            if (v != null) {
                                ctx.addResult(vals, cacheKey, v, skipVals, false, deserializeBinary, true, null, 0, 0);
                            } else
                                success = false;
                        }
                    } else {
                        if (!storeEnabled && configuration().isStatisticsEnabled() && !skipVals)
                            metrics0().onRead(false);
                        success = false;
                    }
                    // While.
                    break;
                } catch (GridCacheEntryRemovedException ignored) {
                // No-op, retry.
                } finally {
                    if (entry != null)
                        ctx.evicts().touch(entry, ctx.affinity().affinityTopologyVersion());
                }
                if (!success && storeEnabled)
                    break;
            }
        }
    }
    if (success || !storeEnabled)
        return vals;
    return getAllAsync(keys, null, opCtx == null || !opCtx.skipStore(), false, subjId, taskName, deserializeBinary, opCtx != null && opCtx.recovery(), /*force primary*/
    false, expiry, skipVals, /*can remap*/
    true, needVer).get();
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) 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) UUID(java.util.UUID) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 12 with EntryGetResult

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

the class GridDhtGetFuture method getAsync.

/**
     * @param keys Keys to get.
     * @return Future for local get.
     */
@SuppressWarnings({ "unchecked", "IfMayBeConditional" })
private IgniteInternalFuture<Collection<GridCacheEntryInfo>> getAsync(final Map<KeyCacheObject, Boolean> keys) {
    if (F.isEmpty(keys))
        return new GridFinishedFuture<Collection<GridCacheEntryInfo>>(Collections.<GridCacheEntryInfo>emptyList());
    String taskName0 = cctx.kernalContext().job().currentTaskName();
    if (taskName0 == null)
        taskName0 = cctx.kernalContext().task().resolveTaskName(taskNameHash);
    final String taskName = taskName0;
    GridCompoundFuture<Boolean, Boolean> txFut = null;
    ClusterNode readerNode = cctx.discovery().node(reader);
    ReaderArguments readerArgs = null;
    if (readerNode != null && !readerNode.isLocal() && cctx.discovery().cacheNearNode(readerNode, cctx.name())) {
        for (Map.Entry<KeyCacheObject, Boolean> k : keys.entrySet()) {
            while (true) {
                GridDhtCacheEntry e = cache().entryExx(k.getKey(), topVer);
                try {
                    if (e.obsolete())
                        continue;
                    boolean addReader = (!e.deleted() && k.getValue() && !skipVals);
                    if (addReader) {
                        e.unswap(false);
                        // we have to add reader again later.
                        if (readerArgs == null)
                            readerArgs = new ReaderArguments(reader, msgId, topVer);
                    }
                    // Register reader. If there are active transactions for this entry,
                    // then will wait for their completion before proceeding.
                    // TODO: IGNITE-3498:
                    // TODO: What if any transaction we wait for actually removes this entry?
                    // TODO: In this case seems like we will be stuck with untracked near entry.
                    // TODO: To fix, check that reader is contained in the list of readers once
                    // TODO: again after the returned future completes - if not, try again.
                    IgniteInternalFuture<Boolean> f = addReader ? e.addReader(reader, msgId, topVer) : null;
                    if (f != null) {
                        if (txFut == null)
                            txFut = new GridCompoundFuture<>(CU.boolReducer());
                        txFut.add(f);
                    }
                    break;
                } catch (IgniteCheckedException err) {
                    return new GridFinishedFuture<>(err);
                } catch (GridCacheEntryRemovedException ignore) {
                    if (log.isDebugEnabled())
                        log.debug("Got removed entry when getting a DHT value: " + e);
                } finally {
                    cctx.evicts().touch(e, topVer);
                }
            }
        }
        if (txFut != null)
            txFut.markInitialized();
    }
    IgniteInternalFuture<Map<KeyCacheObject, EntryGetResult>> fut;
    if (txFut == null || txFut.isDone()) {
        fut = cache().getDhtAllAsync(keys.keySet(), readerArgs, readThrough, subjId, taskName, expiryPlc, skipVals, /*can remap*/
        true, recovery);
    } else {
        final ReaderArguments args = readerArgs;
        // If we are here, then there were active transactions for some entries
        // when we were adding the reader. In that case we must wait for those
        // transactions to complete.
        fut = new GridEmbeddedFuture<>(txFut, new C2<Boolean, Exception, IgniteInternalFuture<Map<KeyCacheObject, EntryGetResult>>>() {

            @Override
            public IgniteInternalFuture<Map<KeyCacheObject, EntryGetResult>> apply(Boolean b, Exception e) {
                if (e != null)
                    throw new GridClosureException(e);
                return cache().getDhtAllAsync(keys.keySet(), args, readThrough, subjId, taskName, expiryPlc, skipVals, /*can remap*/
                true, recovery);
            }
        });
    }
    if (fut.isDone()) {
        if (fut.error() != null)
            onDone(fut.error());
        else
            return new GridFinishedFuture<>(toEntryInfos(fut.result()));
    }
    return new GridEmbeddedFuture<>(new C2<Map<KeyCacheObject, EntryGetResult>, Exception, Collection<GridCacheEntryInfo>>() {

        @Override
        public Collection<GridCacheEntryInfo> apply(Map<KeyCacheObject, EntryGetResult> map, Exception e) {
            if (e != null) {
                onDone(e);
                return Collections.emptyList();
            } else
                return toEntryInfos(map);
        }
    }, fut);
}
Also used : GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) C2(org.apache.ignite.internal.util.typedef.C2) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) ReaderArguments(org.apache.ignite.internal.processors.cache.ReaderArguments) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheEntryInfo(org.apache.ignite.internal.processors.cache.GridCacheEntryInfo) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) Collection(java.util.Collection) Map(java.util.Map)

Example 13 with EntryGetResult

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

the class GridDhtGetFuture method toEntryInfos.

/**
     * @param map Map to convert.
     * @return List of infos.
     */
private Collection<GridCacheEntryInfo> toEntryInfos(Map<KeyCacheObject, EntryGetResult> map) {
    if (map.isEmpty())
        return Collections.emptyList();
    Collection<GridCacheEntryInfo> infos = new ArrayList<>(map.size());
    for (Map.Entry<KeyCacheObject, EntryGetResult> entry : map.entrySet()) {
        EntryGetResult val = entry.getValue();
        assert val != null;
        GridCacheEntryInfo info = new GridCacheEntryInfo();
        info.cacheId(cctx.cacheId());
        info.key(entry.getKey());
        info.value(skipVals ? null : (CacheObject) val.value());
        info.version(val.version());
        info.expireTime(val.expireTime());
        info.ttl(val.ttl());
        infos.add(info);
    }
    return infos;
}
Also used : GridCacheEntryInfo(org.apache.ignite.internal.processors.cache.GridCacheEntryInfo) ArrayList(java.util.ArrayList) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 14 with EntryGetResult

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

the class GridPartitionedSingleGetFuture method localGet.

/**
     * @param topVer Topology version.
     * @param part Partition.
     * @return {@code True} if future completed.
     */
private boolean localGet(AffinityTopologyVersion topVer, int part) {
    assert cctx.affinityNode() : this;
    GridDhtCacheAdapter colocated = cctx.dht();
    boolean readNoEntry = cctx.readNoEntry(expiryPlc, false);
    boolean evt = !skipVals;
    while (true) {
        try {
            CacheObject v = null;
            GridCacheVersion ver = null;
            boolean skipEntry = readNoEntry;
            if (readNoEntry) {
                CacheDataRow row = cctx.offheap().read(key);
                if (row != null) {
                    long expireTime = row.expireTime();
                    if (expireTime == 0 || expireTime > U.currentTimeMillis()) {
                        v = row.value();
                        if (needVer)
                            ver = row.version();
                        if (evt) {
                            cctx.events().readEvent(key, null, row.value(), subjId, taskName, !deserializeBinary);
                        }
                    } else
                        skipEntry = false;
                }
            }
            if (!skipEntry) {
                GridCacheEntryEx entry = colocated.entryEx(key);
                // If our DHT cache do has value, then we peek it.
                if (entry != null) {
                    boolean isNew = entry.isNewLocked();
                    if (needVer) {
                        EntryGetResult res = entry.innerGetVersioned(null, null, /*update-metrics*/
                        false, /*event*/
                        evt, subjId, null, taskName, expiryPlc, true, null);
                        if (res != null) {
                            v = res.value();
                            ver = res.version();
                        }
                    } else {
                        v = entry.innerGet(null, null, /*read-through*/
                        false, /*update-metrics*/
                        false, /*event*/
                        evt, subjId, null, taskName, expiryPlc, true);
                    }
                    colocated.context().evicts().touch(entry, topVer);
                    // Entry was not in memory or in swap, so we remove it from cache.
                    if (v == null) {
                        if (isNew && entry.markObsoleteIfEmpty(ver))
                            colocated.removeEntry(entry);
                    }
                }
            }
            if (v != null) {
                if (!skipVals && cctx.config().isStatisticsEnabled())
                    cctx.cache().metrics0().onRead(true);
                if (!skipVals)
                    setResult(v, ver);
                else
                    setSkipValueResult(true, ver);
                return true;
            }
            boolean topStable = cctx.isReplicated() || topVer.equals(cctx.topology().topologyVersion());
            // Entry not found, complete future with null result if topology did not change and there is no store.
            if (!cctx.readThroughConfigured() && (topStable || partitionOwned(part))) {
                if (!skipVals && cctx.config().isStatisticsEnabled())
                    colocated.metrics0().onRead(false);
                if (skipVals)
                    setSkipValueResult(false, null);
                else
                    setResult(null, null);
                return true;
            }
            return false;
        } catch (GridCacheEntryRemovedException ignored) {
        // No-op, will retry.
        } catch (GridDhtInvalidPartitionException ignored) {
            return false;
        } catch (IgniteCheckedException e) {
            onDone(e);
            return true;
        }
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) 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)

Example 15 with EntryGetResult

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

the class GridPartitionedSingleGetFuture method setResult.

/**
     * @param val Value.
     * @param ver Version.
     */
private void setResult(@Nullable CacheObject val, @Nullable GridCacheVersion ver) {
    try {
        assert !skipVals;
        if (val != null) {
            if (!keepCacheObjects) {
                Object res = cctx.unwrapBinaryIfNeeded(val, !deserializeBinary);
                onDone(needVer ? new EntryGetResult(res, ver) : res);
            } else
                onDone(needVer ? new EntryGetResult(val, ver) : val);
        } else
            onDone(null);
    } catch (Exception e) {
        onDone(e);
    }
}
Also used : EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) 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) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)

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