Search in sources :

Example 11 with KeyCacheObject

use of org.apache.ignite.internal.processors.cache.KeyCacheObject 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 12 with KeyCacheObject

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

the class GridDhtAtomicCache method unlockEntries.

/**
     * Releases java-level locks on cache entries.
     *
     * @param locked Locked entries.
     * @param topVer Topology version.
     */
private void unlockEntries(List<GridDhtCacheEntry> locked, AffinityTopologyVersion topVer) {
    // Process deleted entries before locks release.
    assert ctx.deferredDelete() : this;
    // Entries to skip eviction manager notification for.
    // Enqueue entries while holding locks.
    Collection<KeyCacheObject> skip = null;
    int size = locked.size();
    try {
        for (int i = 0; i < size; i++) {
            GridCacheMapEntry entry = locked.get(i);
            if (entry != null && entry.deleted()) {
                if (skip == null)
                    skip = U.newHashSet(locked.size());
                skip.add(entry.key());
            }
        }
    } finally {
        // That's why releasing locks in the finally block..
        for (int i = 0; i < size; i++) {
            GridCacheMapEntry entry = locked.get(i);
            if (entry != null)
                GridUnsafe.monitorExit(entry);
        }
    }
    // Try evict partitions.
    for (int i = 0; i < size; i++) {
        GridDhtCacheEntry entry = locked.get(i);
        if (entry != null)
            entry.onUnlock();
    }
    ctx.shared().database().checkpointReadUnlock();
    if (skip != null && skip.size() == size)
        // Optimization.
        return;
    // Eviction manager will remove empty entries.
    for (int i = 0; i < size; i++) {
        GridCacheMapEntry entry = locked.get(i);
        if (entry != null && (skip == null || !skip.contains(entry.key())))
            ctx.evicts().touch(entry, topVer);
    }
}
Also used : GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 13 with KeyCacheObject

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

the class GridDhtAtomicCache method lockEntries.

/**
     * Acquires java-level locks on cache entries. Returns collection of locked entries.
     *
     * @param req Request with keys to lock.
     * @param topVer Topology version to lock on.
     * @return Collection of locked entries.
     * @throws GridDhtInvalidPartitionException If entry does not belong to local node. If exception is thrown, locks
     * are released.
     */
@SuppressWarnings("ForLoopReplaceableByForEach")
private List<GridDhtCacheEntry> lockEntries(GridNearAtomicAbstractUpdateRequest req, AffinityTopologyVersion topVer) throws GridDhtInvalidPartitionException {
    ctx.shared().database().checkpointReadLock();
    if (req.size() == 1) {
        KeyCacheObject key = req.key(0);
        while (true) {
            GridDhtCacheEntry entry = entryExx(key, topVer);
            GridUnsafe.monitorEnter(entry);
            if (entry.obsolete())
                GridUnsafe.monitorExit(entry);
            else
                return Collections.singletonList(entry);
        }
    } else {
        List<GridDhtCacheEntry> locked = new ArrayList<>(req.size());
        while (true) {
            for (int i = 0; i < req.size(); i++) {
                GridDhtCacheEntry entry = entryExx(req.key(i), topVer);
                locked.add(entry);
            }
            boolean retry = false;
            for (int i = 0; i < locked.size(); i++) {
                GridCacheMapEntry entry = locked.get(i);
                if (entry == null)
                    continue;
                GridUnsafe.monitorEnter(entry);
                if (entry.obsolete()) {
                    // Unlock all locked.
                    for (int j = 0; j <= i; j++) {
                        if (locked.get(j) != null)
                            GridUnsafe.monitorExit(locked.get(j));
                    }
                    // Clear entries.
                    locked.clear();
                    // Retry.
                    retry = true;
                    break;
                }
            }
            if (!retry)
                return locked;
        }
    }
}
Also used : GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) ArrayList(java.util.ArrayList) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 14 with KeyCacheObject

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

the class GridNearAtomicUpdateFuture method mapSingleUpdate.

/**
     * @param topVer Topology version.
     * @param futId Future ID.
     * @param mappingKnown {@code True} if update mapping is known locally.
     * @return Request.
     * @throws Exception If failed.
     */
private PrimaryRequestState mapSingleUpdate(AffinityTopologyVersion topVer, Long futId, boolean mappingKnown) throws Exception {
    Object key = F.first(keys);
    Object val;
    GridCacheVersion conflictVer;
    long conflictTtl;
    long conflictExpireTime;
    if (vals != null) {
        // Regular PUT.
        val = F.first(vals);
        conflictVer = null;
        conflictTtl = CU.TTL_NOT_CHANGED;
        conflictExpireTime = CU.EXPIRE_TIME_CALCULATE;
    } else if (conflictPutVals != null) {
        // Conflict PUT.
        GridCacheDrInfo conflictPutVal = F.first(conflictPutVals);
        val = conflictPutVal.valueEx();
        conflictVer = conflictPutVal.version();
        conflictTtl = conflictPutVal.ttl();
        conflictExpireTime = conflictPutVal.expireTime();
    } else if (conflictRmvVals != null) {
        // Conflict REMOVE.
        val = null;
        conflictVer = F.first(conflictRmvVals);
        conflictTtl = CU.TTL_NOT_CHANGED;
        conflictExpireTime = CU.EXPIRE_TIME_CALCULATE;
    } else {
        // Regular REMOVE.
        val = null;
        conflictVer = null;
        conflictTtl = CU.TTL_NOT_CHANGED;
        conflictExpireTime = CU.EXPIRE_TIME_CALCULATE;
    }
    // We still can get here if user pass map with single element.
    if (key == null)
        throw new NullPointerException("Null key.");
    if (val == null && op != GridCacheOperation.DELETE)
        throw new NullPointerException("Null value.");
    KeyCacheObject cacheKey = cctx.toCacheKeyObject(key);
    if (op != TRANSFORM)
        val = cctx.toCacheObject(val);
    else
        val = EntryProcessorResourceInjectorProxy.wrap(cctx.kernalContext(), (EntryProcessor) val);
    List<ClusterNode> nodes = cctx.affinity().nodesByKey(cacheKey, topVer);
    if (F.isEmpty(nodes))
        throw new ClusterTopologyServerNotFoundException("Failed to map keys for cache " + "(all partition nodes left the grid).");
    ClusterNode primary = nodes.get(0);
    boolean needPrimaryRes = !mappingKnown || primary.isLocal() || nodes.size() == 1;
    GridNearAtomicFullUpdateRequest req = new GridNearAtomicFullUpdateRequest(cctx.cacheId(), primary.id(), futId, topVer, topLocked, syncMode, op, retval, expiryPlc, invokeArgs, filter, subjId, taskNameHash, needPrimaryRes, skipStore, keepBinary, recovery, cctx.deploymentEnabled(), 1);
    req.addUpdateEntry(cacheKey, val, conflictTtl, conflictExpireTime, conflictVer);
    return new PrimaryRequestState(req, nodes, true);
}
Also used : GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 15 with KeyCacheObject

use of org.apache.ignite.internal.processors.cache.KeyCacheObject 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

KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)105 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)65 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)56 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)54 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)41 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)28 ArrayList (java.util.ArrayList)27 ClusterNode (org.apache.ignite.cluster.ClusterNode)27 Map (java.util.Map)25 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)23 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)23 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)15 HashMap (java.util.HashMap)14 IgniteException (org.apache.ignite.IgniteException)14 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)14 LinkedHashMap (java.util.LinkedHashMap)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)13 ClusterTopologyServerNotFoundException (org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException)13 CacheStorePartialUpdateException (org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException)12 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)11