Search in sources :

Example 6 with GridCacheEntryEx

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

the class GridDhtTxLocalAdapter method lockAllAsync.

/**
     * @param cacheCtx Cache context.
     * @param entries Entries to lock.
     * @param msgId Message ID.
     * @param read Read flag.
     * @param createTtl TTL for create operation.
     * @param accessTtl TTL for read operation.
     * @param needRetVal Return value flag.
     * @param skipStore Skip store flag.
     * @return Lock future.
     */
@SuppressWarnings("ForLoopReplaceableByForEach")
IgniteInternalFuture<GridCacheReturn> lockAllAsync(GridCacheContext cacheCtx, List<GridCacheEntryEx> entries, long msgId, final boolean read, final boolean needRetVal, long createTtl, long accessTtl, boolean skipStore, boolean keepBinary) {
    try {
        checkValid();
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture<>(e);
    }
    final GridCacheReturn ret = new GridCacheReturn(localResult(), false);
    if (F.isEmpty(entries))
        return new GridFinishedFuture<>(ret);
    init();
    onePhaseCommit(onePhaseCommit);
    try {
        Set<KeyCacheObject> skipped = null;
        AffinityTopologyVersion topVer = topologyVersion();
        GridDhtCacheAdapter dhtCache = cacheCtx.isNear() ? cacheCtx.near().dht() : cacheCtx.dht();
        // Enlist locks into transaction.
        for (int i = 0; i < entries.size(); i++) {
            GridCacheEntryEx entry = entries.get(i);
            KeyCacheObject key = entry.key();
            IgniteTxEntry txEntry = entry(entry.txKey());
            // First time access.
            if (txEntry == null) {
                GridDhtCacheEntry cached;
                while (true) {
                    try {
                        cached = dhtCache.entryExx(key, topVer);
                        cached.unswap(read);
                        break;
                    } catch (GridCacheEntryRemovedException ignore) {
                        if (log.isDebugEnabled())
                            log.debug("Get removed entry: " + key);
                    }
                }
                addActiveCache(dhtCache.context(), false);
                txEntry = addEntry(NOOP, null, null, null, cached, null, CU.empty0(), false, -1L, -1L, null, skipStore, keepBinary);
                if (read)
                    txEntry.ttl(accessTtl);
                txEntry.cached(cached);
                addReader(msgId, cached, txEntry, topVer);
            } else {
                if (skipped == null)
                    skipped = new GridLeanSet<>();
                skipped.add(key);
            }
        }
        assert pessimistic();
        Collection<KeyCacheObject> keys = F.viewReadOnly(entries, CU.entry2Key());
        // Acquire locks only after having added operation to the write set.
        // Otherwise, during rollback we will not know whether locks need
        // to be rolled back.
        // Loose all skipped and previously locked (we cannot reenter locks here).
        final Collection<KeyCacheObject> passedKeys = skipped != null ? F.view(keys, F0.notIn(skipped)) : keys;
        if (log.isDebugEnabled())
            log.debug("Lock keys: " + passedKeys);
        return obtainLockAsync(cacheCtx, ret, passedKeys, read, needRetVal, createTtl, accessTtl, skipStore, keepBinary);
    } catch (IgniteCheckedException e) {
        setRollbackOnly();
        return new GridFinishedFuture<>(e);
    }
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridLeanSet(org.apache.ignite.internal.util.GridLeanSet) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 7 with GridCacheEntryEx

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

the class GridDhtTxPrepareFuture method addDhtValues.

/**
     * @param res Response being sent.
     */
private void addDhtValues(GridNearTxPrepareResponse res) {
    // Interceptor on near node needs old values to execute callbacks.
    if (!F.isEmpty(writes)) {
        for (IgniteTxEntry e : writes) {
            IgniteTxEntry txEntry = tx.entry(e.txKey());
            assert txEntry != null : "Missing tx entry for key [tx=" + tx + ", key=" + e.txKey() + ']';
            GridCacheContext cacheCtx = txEntry.context();
            while (true) {
                try {
                    GridCacheEntryEx entry = txEntry.cached();
                    GridCacheVersion dhtVer = entry.version();
                    CacheObject val0 = entry.valueBytes();
                    if (val0 != null)
                        res.addOwnedValue(txEntry.txKey(), dhtVer, val0);
                    break;
                } catch (GridCacheEntryRemovedException ignored) {
                    // Retry.
                    txEntry.cached(cacheCtx.cache().entryEx(txEntry.key(), tx.topologyVersion()));
                }
            }
        }
    }
    for (Map.Entry<IgniteTxKey, GridCacheVersion> ver : dhtVerMap.entrySet()) {
        IgniteTxEntry txEntry = tx.entry(ver.getKey());
        if (res.hasOwnedValue(ver.getKey()))
            continue;
        assert txEntry != null : ver;
        GridCacheContext cacheCtx = txEntry.context();
        while (true) {
            try {
                GridCacheEntryEx entry = txEntry.cached();
                GridCacheVersion dhtVer = entry.version();
                if (ver.getValue() == null || !ver.getValue().equals(dhtVer)) {
                    CacheObject val0 = entry.valueBytes();
                    res.addOwnedValue(txEntry.txKey(), dhtVer, val0);
                }
                break;
            } catch (GridCacheEntryRemovedException ignored) {
                // Retry.
                txEntry.cached(cacheCtx.cache().entryEx(txEntry.key(), tx.topologyVersion()));
            }
        }
    }
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) 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) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with GridCacheEntryEx

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

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

the class GridDhtTxPrepareFuture method versionCheckError.

/**
     * @param entry Entry.
     * @return Optimistic version check error.
     */
private IgniteTxOptimisticCheckedException versionCheckError(IgniteTxEntry entry) {
    StringBuilder msg = new StringBuilder("Failed to prepare transaction, read/write conflict [");
    GridCacheContext cctx = entry.context();
    try {
        Object key = cctx.unwrapBinaryIfNeeded(entry.key(), entry.keepBinary(), false);
        assert key != null : entry.key();
        if (S.INCLUDE_SENSITIVE)
            msg.append("key=").append(key.toString()).append(", keyCls=").append(key.getClass().getName());
    } catch (Exception e) {
        msg.append("key=<failed to get key: ").append(e.toString()).append(">");
    }
    try {
        GridCacheEntryEx entryEx = entry.cached();
        CacheObject cacheVal = entryEx != null ? entryEx.rawGet() : null;
        Object val = cacheVal != null ? cctx.unwrapBinaryIfNeeded(cacheVal, entry.keepBinary(), false) : null;
        if (val != null) {
            if (S.INCLUDE_SENSITIVE)
                msg.append(", val=").append(val.toString()).append(", valCls=").append(val.getClass().getName());
        } else
            msg.append(", val=null");
    } catch (Exception e) {
        msg.append(", val=<failed to get value: ").append(e.toString()).append(">");
    }
    msg.append(", cache=").append(cctx.name()).append(", thread=").append(Thread.currentThread()).append("]");
    return new IgniteTxOptimisticCheckedException(msg.toString());
}
Also used : GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) 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) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException)

Example 10 with GridCacheEntryEx

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

Aggregations

GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)66 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)38 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)31 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)30 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)25 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)22 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)15 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)14 ClusterNode (org.apache.ignite.cluster.ClusterNode)10 IgniteKernal (org.apache.ignite.internal.IgniteKernal)10 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)10 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)10 GridDhtInvalidPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 UUID (java.util.UUID)8 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)8 IgniteException (org.apache.ignite.IgniteException)7 IgniteCacheExpiryPolicy (org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)7 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)7