Search in sources :

Example 41 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 needVer) {
    AffinityTopologyVersion topVer = ctx.affinity().affinityTopologyVersion();
    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(ctx, 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.statisticsEnabled())
                    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, !skipStore, forcePrimary, subjId, taskName, deserializeBinary, recovery, expiry, skipVals, needVer, false);
    fut.init(topVer);
    return fut;
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.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 42 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 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 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(ctx, 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.statisticsEnabled())
                    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, readThrough, forcePrimary, subjId, taskName, deserializeBinary, recovery, expiryPlc, skipVals, needVer, keepCacheObj);
    fut.init(topVer);
    return fut;
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.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 43 with GridCacheEntryEx

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

the class GridNearTxFinishFuture method onDone.

/**
 * {@inheritDoc}
 */
@Override
public boolean onDone(IgniteInternalTx tx0, Throwable err) {
    if (isDone())
        return false;
    synchronized (this) {
        if (isDone())
            return false;
        boolean nodeStop = false;
        if (err != null) {
            tx.setRollbackOnly();
            nodeStop = err instanceof NodeStoppingException;
        }
        if (commit) {
            if (tx.commitError() != null)
                err = tx.commitError();
            else if (err != null)
                tx.commitError(err);
        }
        if (initialized() || err != null) {
            if (tx.needCheckBackup()) {
                assert tx.onePhaseCommit();
                if (err != null)
                    err = new TransactionRollbackException("Failed to commit transaction.", err);
                try {
                    tx.localFinish(err == null, true);
                } catch (IgniteCheckedException e) {
                    if (err != null)
                        err.addSuppressed(e);
                    else
                        err = e;
                }
            }
            if (tx.onePhaseCommit()) {
                boolean commit = this.commit && err == null;
                if (!nodeStop)
                    finishOnePhase(commit);
                try {
                    tx.tmFinish(commit, nodeStop, true);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to finish tx: " + tx, e);
                    if (err == null)
                        err = e;
                }
            }
            if (super.onDone(tx0, err)) {
                if (error() instanceof IgniteTxHeuristicCheckedException && !nodeStop) {
                    AffinityTopologyVersion topVer = tx.topologyVersion();
                    for (IgniteTxEntry e : tx.writeMap().values()) {
                        GridCacheContext cacheCtx = e.context();
                        try {
                            if (e.op() != NOOP && !cacheCtx.affinity().keyLocalNode(e.key(), topVer)) {
                                GridCacheEntryEx entry = cacheCtx.cache().peekEx(e.key());
                                if (entry != null)
                                    entry.invalidate(tx.xidVersion());
                            }
                        } catch (Throwable t) {
                            U.error(log, "Failed to invalidate entry.", t);
                            if (t instanceof Error)
                                throw (Error) t;
                        }
                    }
                }
                // Don't forget to clean up.
                cctx.mvcc().removeFuture(futId);
                return true;
            }
        }
    }
    return false;
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException)

Example 44 with GridCacheEntryEx

use of org.apache.ignite.internal.processors.cache.GridCacheEntryEx 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(ctx, 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 (ctx.statisticsEnabled() && !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;
                        }
                    }
                    // 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) {
            if (!storeEnabled && ctx.statisticsEnabled() && !skipVals)
                metrics0().onRead(false);
        }
    }
    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, needVer).get();
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.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 45 with GridCacheEntryEx

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

the class PageAbstractEvictionTracker method evictDataPage.

/**
     * @param pageIdx Page index.
     * @return true if at least one data row has been evicted
     * @throws IgniteCheckedException If failed.
     */
final boolean evictDataPage(int pageIdx) throws IgniteCheckedException {
    long fakePageId = PageIdUtils.pageId(0, (byte) 0, pageIdx);
    long page = pageMem.acquirePage(0, fakePageId);
    List<CacheDataRowAdapter> rowsToEvict;
    try {
        long pageAddr = pageMem.readLockForce(0, fakePageId, page);
        try {
            if (PageIO.getType(pageAddr) != PageIO.T_DATA)
                // Can't evict: page has been recycled into non-data page.
                return false;
            DataPageIO io = DataPageIO.VERSIONS.forPage(pageAddr);
            long realPageId = PageIO.getPageId(pageAddr);
            if (!checkTouch(realPageId))
                // Can't evict: another thread concurrently invoked forgetPage()
                return false;
            rowsToEvict = io.forAllItems(pageAddr, new DataPageIO.CC<CacheDataRowAdapter>() {

                @Override
                public CacheDataRowAdapter apply(long link) throws IgniteCheckedException {
                    CacheDataRowAdapter row = new CacheDataRowAdapter(link);
                    row.initFromLink(null, sharedCtx, pageMem, CacheDataRowAdapter.RowData.KEY_ONLY);
                    assert row.cacheId() != 0 : "Cache ID should be stored in rows of evictable cache";
                    return row;
                }
            });
        } finally {
            pageMem.readUnlock(0, fakePageId, page);
        }
    } finally {
        pageMem.releasePage(0, fakePageId, page);
    }
    boolean evictionDone = false;
    for (CacheDataRowAdapter dataRow : rowsToEvict) {
        GridCacheContext<?, ?> cacheCtx = sharedCtx.cacheContext(dataRow.cacheId());
        if (!cacheCtx.userCache())
            continue;
        GridCacheEntryEx entryEx = cacheCtx.isNear() ? cacheCtx.near().dht().entryEx(dataRow.key()) : cacheCtx.cache().entryEx(dataRow.key());
        evictionDone |= entryEx.evictInternal(GridCacheVersionManager.EVICT_VER, null, true);
    }
    return evictionDone;
}
Also used : DataPageIO(org.apache.ignite.internal.processors.cache.database.tree.io.DataPageIO) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) CacheDataRowAdapter(org.apache.ignite.internal.processors.cache.database.CacheDataRowAdapter)

Aggregations

GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)70 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)42 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)33 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)32 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)26 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)23 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)15 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)14 GridDhtInvalidPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException)12 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)11 IgniteKernal (org.apache.ignite.internal.IgniteKernal)10 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)10 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 UUID (java.util.UUID)8 IgniteCacheExpiryPolicy (org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)7 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)7 Ignite (org.apache.ignite.Ignite)6