Search in sources :

Example 1 with EntryGetResult

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

the class GridDhtGetSingleFuture method getAsync.

/**
     *
     */
@SuppressWarnings({ "unchecked", "IfMayBeConditional" })
private void getAsync() {
    assert part != -1;
    String taskName0 = cctx.kernalContext().job().currentTaskName();
    if (taskName0 == null)
        taskName0 = cctx.kernalContext().task().resolveTaskName(taskNameHash);
    final String taskName = taskName0;
    IgniteInternalFuture<Boolean> rdrFut = null;
    ClusterNode readerNode = cctx.discovery().node(reader);
    ReaderArguments readerArgs = null;
    if (readerNode != null && !readerNode.isLocal() && cctx.discovery().cacheNearNode(readerNode, cctx.name())) {
        while (true) {
            GridDhtCacheEntry e = cache().entryExx(key, topVer);
            try {
                if (e.obsolete())
                    continue;
                boolean addReader = (!e.deleted() && this.addRdr && !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.
                rdrFut = addReader ? e.addReader(reader, msgId, topVer) : null;
                break;
            } catch (IgniteCheckedException err) {
                onDone(err);
                return;
            } catch (GridCacheEntryRemovedException ignore) {
                if (log.isDebugEnabled())
                    log.debug("Got removed entry when getting a DHT value: " + e);
            } finally {
                cctx.evicts().touch(e, topVer);
            }
        }
    }
    IgniteInternalFuture<Map<KeyCacheObject, EntryGetResult>> fut;
    if (rdrFut == null || rdrFut.isDone()) {
        fut = cache().getDhtAllAsync(Collections.singleton(key), readerArgs, readThrough, subjId, taskName, expiryPlc, skipVals, /*can remap*/
        true, recovery);
    } else {
        final ReaderArguments args = readerArgs;
        rdrFut.listen(new IgniteInClosure<IgniteInternalFuture<Boolean>>() {

            @Override
            public void apply(IgniteInternalFuture<Boolean> fut) {
                Throwable e = fut.error();
                if (e != null) {
                    onDone(e);
                    return;
                }
                IgniteInternalFuture<Map<KeyCacheObject, EntryGetResult>> fut0 = cache().getDhtAllAsync(Collections.singleton(key), args, readThrough, subjId, taskName, expiryPlc, skipVals, /*can remap*/
                true, recovery);
                fut0.listen(createGetFutureListener());
            }
        });
        return;
    }
    if (fut.isDone())
        onResult(fut);
    else
        fut.listen(createGetFutureListener());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) 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) Map(java.util.Map) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 2 with EntryGetResult

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

the class GridDhtGetSingleFuture method toEntryInfo.

/**
     * @param map Map to convert.
     * @return List of infos.
     */
private GridCacheEntryInfo toEntryInfo(Map<KeyCacheObject, EntryGetResult> map) {
    if (map.isEmpty())
        return null;
    EntryGetResult val = map.get(key);
    assert val != null;
    GridCacheEntryInfo info = new GridCacheEntryInfo();
    info.cacheId(cctx.cacheId());
    info.key(key);
    info.value(skipVals ? null : (CacheObject) val.value());
    info.version(val.version());
    info.expireTime(val.expireTime());
    info.ttl(val.ttl());
    return info;
}
Also used : GridCacheEntryInfo(org.apache.ignite.internal.processors.cache.GridCacheEntryInfo) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 3 with EntryGetResult

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

the class GridDhtColocatedCache method loadAsync.

/**
     * @param keys Keys to load.
     * @param readThrough Read through flag.
     * @param forcePrimary Force get from primary node flag.
     * @param topVer Topology version.
     * @param subjId Subject ID.
     * @param taskName Task name.
     * @param deserializeBinary Deserialize binary flag.
     * @param expiryPlc Expiry policy.
     * @param skipVals Skip values flag.
     * @param canRemap Flag indicating whether future can be remapped on a newer topology version.
     * @param needVer If {@code true} returns values as tuples containing value and version.
     * @param keepCacheObj Keep cache objects flag.
     * @return Load future.
     */
public final IgniteInternalFuture<Map<K, V>> loadAsync(@Nullable Collection<KeyCacheObject> keys, boolean readThrough, boolean forcePrimary, AffinityTopologyVersion topVer, @Nullable UUID subjId, String taskName, boolean deserializeBinary, boolean recovery, @Nullable IgniteCacheExpiryPolicy expiryPlc, boolean skipVals, boolean canRemap, boolean needVer, boolean keepCacheObj) {
    if (keys == null || keys.isEmpty())
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    if (expiryPlc == null)
        expiryPlc = expiryPolicy(null);
    // Optimisation: try to resolve value locally and escape 'get future' creation.
    if (!forcePrimary && ctx.affinityNode()) {
        try {
            Map<K, V> locVals = null;
            boolean success = true;
            boolean readNoEntry = ctx.readNoEntry(expiryPlc, false);
            boolean evt = !skipVals;
            for (KeyCacheObject key : keys) {
                if (readNoEntry) {
                    CacheDataRow row = ctx.offheap().read(key);
                    if (row != null) {
                        long expireTime = row.expireTime();
                        if (expireTime == 0 || expireTime > U.currentTimeMillis()) {
                            if (locVals == null)
                                locVals = U.newHashMap(keys.size());
                            ctx.addResult(locVals, key, row.value(), skipVals, keepCacheObj, deserializeBinary, true, null, row.version(), 0, 0, needVer);
                            if (evt) {
                                ctx.events().readEvent(key, null, row.value(), subjId, taskName, !deserializeBinary);
                            }
                        } else
                            success = false;
                    } else
                        success = false;
                } else {
                    GridCacheEntryEx entry = null;
                    while (true) {
                        try {
                            entry = entryEx(key);
                            // If our DHT cache do has value, then we peek it.
                            if (entry != null) {
                                boolean isNew = entry.isNewLocked();
                                EntryGetResult getRes = null;
                                CacheObject v = null;
                                GridCacheVersion ver = null;
                                if (needVer) {
                                    getRes = entry.innerGetVersioned(null, null, /*update-metrics*/
                                    false, /*event*/
                                    evt, subjId, null, taskName, expiryPlc, !deserializeBinary, null);
                                    if (getRes != null) {
                                        v = getRes.value();
                                        ver = getRes.version();
                                    }
                                } else {
                                    v = entry.innerGet(null, null, /*read-through*/
                                    false, /*update-metrics*/
                                    false, /*event*/
                                    evt, subjId, null, taskName, expiryPlc, !deserializeBinary);
                                }
                                // Entry was not in memory or in swap, so we remove it from cache.
                                if (v == null) {
                                    GridCacheVersion obsoleteVer = context().versions().next();
                                    if (isNew && entry.markObsoleteIfEmpty(obsoleteVer))
                                        removeEntry(entry);
                                    success = false;
                                } else {
                                    if (locVals == null)
                                        locVals = U.newHashMap(keys.size());
                                    ctx.addResult(locVals, key, v, skipVals, keepCacheObj, deserializeBinary, true, getRes, ver, 0, 0, needVer);
                                }
                            } else
                                success = false;
                            // While.
                            break;
                        } catch (GridCacheEntryRemovedException ignored) {
                        // No-op, retry.
                        } catch (GridDhtInvalidPartitionException ignored) {
                            success = false;
                            // While.
                            break;
                        } finally {
                            if (entry != null)
                                context().evicts().touch(entry, topVer);
                        }
                    }
                }
                if (!success)
                    break;
                else if (!skipVals && ctx.config().isStatisticsEnabled())
                    ctx.cache().metrics0().onRead(true);
            }
            if (success) {
                sendTtlUpdateRequest(expiryPlc);
                return new GridFinishedFuture<>(locVals);
            }
        } catch (IgniteCheckedException e) {
            return new GridFinishedFuture<>(e);
        }
    }
    if (expiryPlc != null)
        expiryPlc.reset();
    // Either reload or not all values are available locally.
    GridPartitionedGetFuture<K, V> fut = new GridPartitionedGetFuture<>(ctx, keys, topVer, readThrough, forcePrimary, subjId, taskName, deserializeBinary, recovery, expiryPlc, skipVals, canRemap, needVer, keepCacheObj);
    fut.init();
    return fut;
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) GridPartitionedGetFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedGetFuture) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) 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)

Example 4 with EntryGetResult

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

the class GridDhtAtomicCache method getAllAsync0.

/**
     * Entry point to all public API get methods.
     *
     * @param keys Keys.
     * @param forcePrimary Force primary flag.
     * @param subjId Subject ID.
     * @param taskName Task name.
     * @param deserializeBinary Deserialize binary flag.
     * @param expiryPlc Expiry policy.
     * @param skipVals Skip values flag.
     * @param skipStore Skip store flag.
     * @param needVer Need version.
     * @return Get future.
     */
private IgniteInternalFuture<Map<K, V>> getAllAsync0(@Nullable Collection<KeyCacheObject> keys, boolean forcePrimary, UUID subjId, String taskName, boolean deserializeBinary, boolean recovery, @Nullable ExpiryPolicy expiryPlc, boolean skipVals, boolean skipStore, boolean canRemap, boolean needVer) {
    AffinityTopologyVersion topVer = canRemap ? ctx.affinity().affinityTopologyVersion() : ctx.shared().exchange().readyAffinityVersion();
    final IgniteCacheExpiryPolicy expiry = skipVals ? null : expiryPolicy(expiryPlc);
    final boolean evt = !skipVals;
    // Optimisation: try to resolve value locally and escape 'get future' creation.
    if (!forcePrimary && ctx.affinityNode()) {
        try {
            Map<K, V> locVals = U.newHashMap(keys.size());
            boolean success = true;
            boolean readNoEntry = ctx.readNoEntry(expiry, false);
            // Optimistically expect that all keys are available locally (avoid creation of get future).
            for (KeyCacheObject key : keys) {
                if (readNoEntry) {
                    CacheDataRow row = ctx.offheap().read(key);
                    if (row != null) {
                        long expireTime = row.expireTime();
                        if (expireTime == 0 || expireTime > U.currentTimeMillis()) {
                            ctx.addResult(locVals, key, row.value(), skipVals, false, deserializeBinary, true, null, row.version(), 0, 0, needVer);
                            if (evt) {
                                ctx.events().readEvent(key, null, row.value(), subjId, taskName, !deserializeBinary);
                            }
                        } else
                            success = false;
                    } else
                        success = false;
                } else {
                    GridCacheEntryEx entry = null;
                    while (true) {
                        try {
                            entry = entryEx(key);
                            // If our DHT cache do has value, then we peek it.
                            if (entry != null) {
                                boolean isNew = entry.isNewLocked();
                                EntryGetResult getRes = null;
                                CacheObject v = null;
                                GridCacheVersion ver = null;
                                if (needVer) {
                                    getRes = entry.innerGetVersioned(null, null, /*update-metrics*/
                                    false, /*event*/
                                    evt, subjId, null, taskName, expiry, true, null);
                                    if (getRes != null) {
                                        v = getRes.value();
                                        ver = getRes.version();
                                    }
                                } else {
                                    v = entry.innerGet(null, null, /*read-through*/
                                    false, /*update-metrics*/
                                    false, /*event*/
                                    evt, subjId, null, taskName, expiry, !deserializeBinary);
                                }
                                // Entry was not in memory or in swap, so we remove it from cache.
                                if (v == null) {
                                    if (isNew && entry.markObsoleteIfEmpty(context().versions().next()))
                                        removeEntry(entry);
                                    success = false;
                                } else {
                                    ctx.addResult(locVals, key, v, skipVals, false, deserializeBinary, true, getRes, ver, 0, 0, needVer);
                                }
                            } else
                                success = false;
                            // While.
                            break;
                        } catch (GridCacheEntryRemovedException ignored) {
                        // No-op, retry.
                        } catch (GridDhtInvalidPartitionException ignored) {
                            success = false;
                            // While.
                            break;
                        } finally {
                            if (entry != null)
                                ctx.evicts().touch(entry, topVer);
                        }
                    }
                }
                if (!success)
                    break;
                else if (!skipVals && ctx.config().isStatisticsEnabled())
                    metrics0().onRead(true);
            }
            if (success) {
                sendTtlUpdateRequest(expiry);
                return new GridFinishedFuture<>(locVals);
            }
        } catch (IgniteCheckedException e) {
            return new GridFinishedFuture<>(e);
        }
    }
    if (expiry != null)
        expiry.reset();
    // Either reload or not all values are available locally.
    GridPartitionedGetFuture<K, V> fut = new GridPartitionedGetFuture<>(ctx, keys, topVer, !skipStore, forcePrimary, subjId, taskName, deserializeBinary, recovery, expiry, skipVals, canRemap, needVer, false);
    fut.init();
    return fut;
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) GridPartitionedGetFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedGetFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) 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) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 5 with EntryGetResult

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

the class GridPartitionedGetFuture method localGet.

/**
     * @param key Key.
     * @param part Partition.
     * @param locVals Local values.
     * @return {@code True} if there is no need to further search value.
     */
private boolean localGet(KeyCacheObject key, int part, Map<K, V> locVals) {
    assert cctx.affinityNode() : this;
    GridDhtCacheAdapter<K, V> cache = cache();
    boolean readNoEntry = cctx.readNoEntry(expiryPlc, false);
    boolean evt = !skipVals;
    while (true) {
        try {
            boolean skipEntry = readNoEntry;
            EntryGetResult getRes = null;
            CacheObject v = null;
            GridCacheVersion ver = null;
            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 = cache.entryEx(key);
                // If our DHT cache do has value, then we peek it.
                if (entry != null) {
                    boolean isNew = entry.isNewLocked();
                    if (needVer) {
                        getRes = entry.innerGetVersioned(null, null, /*update-metrics*/
                        false, /*event*/
                        evt, subjId, null, taskName, expiryPlc, !deserializeBinary, null);
                        if (getRes != null) {
                            v = getRes.value();
                            ver = getRes.version();
                        }
                    } else {
                        v = entry.innerGet(null, null, /*read-through*/
                        false, /*update-metrics*/
                        false, /*event*/
                        evt, subjId, null, taskName, expiryPlc, !deserializeBinary);
                    }
                    cache.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))
                            cache.removeEntry(entry);
                    }
                }
            }
            if (v != null) {
                cctx.addResult(locVals, key, v, skipVals, keepCacheObjects, deserializeBinary, true, getRes, ver, 0, 0, needVer);
                return true;
            }
            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.
            if (!cctx.readThroughConfigured() && (topStable || partitionOwned(part))) {
                if (!skipVals && cctx.config().isStatisticsEnabled())
                    cache.metrics0().onRead(false);
                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)

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