Search in sources :

Example 41 with CacheObject

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

the class IgniteTxLocalAdapter method postLockWrite.

/**
 * Post lock processing for put or remove.
 *
 * @param cacheCtx Context.
 * @param keys Keys.
 * @param ret Return value.
 * @param rmv {@code True} if remove.
 * @param retval Flag to return value or not.
 * @param read {@code True} if read.
 * @param accessTtl TTL for read operation.
 * @param filter Filter to check entries.
 * @throws IgniteCheckedException If error.
 * @param computeInvoke If {@code true} computes return value for invoke operation.
 */
@SuppressWarnings("unchecked")
protected final void postLockWrite(GridCacheContext cacheCtx, Iterable<KeyCacheObject> keys, GridCacheReturn ret, boolean rmv, boolean retval, boolean read, long accessTtl, CacheEntryPredicate[] filter, boolean computeInvoke) throws IgniteCheckedException {
    for (KeyCacheObject k : keys) {
        IgniteTxEntry txEntry = entry(cacheCtx.txKey(k));
        if (txEntry == null)
            throw new IgniteCheckedException("Transaction entry is null (most likely collection of keys passed into cache " + "operation was changed before operation completed) [missingKey=" + k + ", tx=" + this + ']');
        while (true) {
            GridCacheEntryEx cached = txEntry.cached();
            try {
                assert cached.detached() || cached.lockedByThread(threadId) || isRollbackOnly() : "Transaction lock is not acquired [entry=" + cached + ", tx=" + this + ", nodeId=" + cctx.localNodeId() + ", threadId=" + threadId + ']';
                if (log.isDebugEnabled())
                    log.debug("Post lock write entry: " + cached);
                CacheObject v = txEntry.previousValue();
                boolean hasPrevVal = txEntry.hasPreviousValue();
                if (onePhaseCommit())
                    filter = txEntry.filters();
                // If we have user-passed filter, we must read value into entry for peek().
                if (!F.isEmptyOrNulls(filter) && !F.isAlwaysTrue(filter))
                    retval = true;
                boolean invoke = txEntry.op() == TRANSFORM;
                if (retval || invoke) {
                    if (!cacheCtx.isNear()) {
                        if (!hasPrevVal) {
                            // For non-local cache should read from store after lock on primary.
                            boolean readThrough = cacheCtx.isLocal() && (invoke || cacheCtx.loadPreviousValue()) && !txEntry.skipStore();
                            v = cached.innerGet(null, this, readThrough, /*metrics*/
                            !invoke, /*event*/
                            !invoke && !dht(), CU.subjectId(this, cctx), null, resolveTaskName(), null, txEntry.keepBinary());
                        }
                    } else {
                        if (!hasPrevVal)
                            v = cached.rawGet();
                    }
                    if (txEntry.op() == TRANSFORM) {
                        if (computeInvoke) {
                            GridCacheVersion ver;
                            try {
                                ver = cached.version();
                            } catch (GridCacheEntryRemovedException e) {
                                assert optimistic() : txEntry;
                                if (log.isDebugEnabled())
                                    log.debug("Failed to get entry version: [msg=" + e.getMessage() + ']');
                                ver = null;
                            }
                            addInvokeResult(txEntry, v, ret, ver);
                        }
                    } else
                        ret.value(cacheCtx, v, txEntry.keepBinary());
                }
                boolean pass = F.isEmpty(filter) || cacheCtx.isAll(cached, filter);
                // For remove operation we return true only if we are removing s/t,
                // i.e. cached value is not null.
                ret.success(pass && (!retval ? !rmv || cached.hasValue() || v != null : !rmv || v != null));
                if (onePhaseCommit())
                    txEntry.filtersPassed(pass);
                boolean updateTtl = read;
                if (pass) {
                    txEntry.markValid();
                    if (log.isDebugEnabled())
                        log.debug("Filter passed in post lock for key: " + k);
                } else {
                    // Revert operation to previous. (if no - NOOP, so entry will be unlocked).
                    txEntry.setAndMarkValid(txEntry.previousOperation(), cacheCtx.toCacheObject(ret.value()));
                    txEntry.filters(CU.empty0());
                    txEntry.filtersSet(false);
                    updateTtl = !cacheCtx.putIfAbsentFilter(filter);
                }
                if (updateTtl) {
                    if (!read) {
                        ExpiryPolicy expiryPlc = cacheCtx.expiryForTxEntry(txEntry);
                        if (expiryPlc != null)
                            txEntry.ttl(CU.toTtl(expiryPlc.getExpiryForAccess()));
                    } else
                        txEntry.ttl(accessTtl);
                }
                // While.
                break;
            }// If entry cached within transaction got removed before lock.
             catch (GridCacheEntryRemovedException ignore) {
                if (log.isDebugEnabled())
                    log.debug("Got removed entry in putAllAsync method (will retry): " + cached);
                txEntry.cached(entryEx(cached.context(), txEntry.txKey(), topologyVersion()));
            }
        }
    }
}
Also used : GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) 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 42 with CacheObject

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

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

the class GridDhtAtomicCache method updatePartialBatch.

/**
 * @param hasNear {@code True} if originating node has near cache.
 * @param firstEntryIdx Index of the first entry in the request keys collection.
 * @param entries Entries to update.
 * @param ver Version to set.
 * @param nearNode Originating node.
 * @param writeVals Write values.
 * @param putMap Values to put.
 * @param rmvKeys Keys to remove.
 * @param entryProcessorMap Entry processors.
 * @param dhtFut DHT update future if has backups.
 * @param req Request.
 * @param res Response.
 * @param replicate Whether replication is enabled.
 * @param batchRes Batch update result.
 * @param taskName Task name.
 * @param expiry Expiry policy.
 * @param sndPrevVal If {@code true} sends previous value to backups.
 * @return Deleted entries.
 */
@SuppressWarnings("ForLoopReplaceableByForEach")
@Nullable
private GridDhtAtomicAbstractUpdateFuture updatePartialBatch(final boolean hasNear, final int firstEntryIdx, final List<GridDhtCacheEntry> entries, final GridCacheVersion ver, final ClusterNode nearNode, @Nullable final List<CacheObject> writeVals, @Nullable final Map<KeyCacheObject, CacheObject> putMap, @Nullable final Collection<KeyCacheObject> rmvKeys, @Nullable final Map<KeyCacheObject, EntryProcessor<Object, Object, Object>> entryProcessorMap, @Nullable GridDhtAtomicAbstractUpdateFuture dhtFut, final GridNearAtomicAbstractUpdateRequest req, final GridNearAtomicUpdateResponse res, final boolean replicate, final DhtAtomicUpdateResult batchRes, final String taskName, @Nullable final IgniteCacheExpiryPolicy expiry, final boolean sndPrevVal) {
    assert putMap == null ^ rmvKeys == null;
    assert req.conflictVersions() == null : "Cannot be called when there are conflict entries in the batch.";
    AffinityTopologyVersion topVer = req.topologyVersion();
    CacheStorePartialUpdateException storeErr = null;
    try {
        GridCacheOperation op;
        if (putMap != null) {
            try {
                Map<? extends KeyCacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>> view = F.viewReadOnly(putMap, new C1<CacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>>() {

                    @Override
                    public IgniteBiTuple<? extends CacheObject, GridCacheVersion> apply(CacheObject val) {
                        return F.t(val, ver);
                    }
                });
                ctx.store().putAll(null, view);
            } catch (CacheStorePartialUpdateException e) {
                storeErr = e;
            }
            op = UPDATE;
        } else {
            try {
                ctx.store().removeAll(null, rmvKeys);
            } catch (CacheStorePartialUpdateException e) {
                storeErr = e;
            }
            op = DELETE;
        }
        boolean intercept = ctx.config().getInterceptor() != null;
        AffinityAssignment affAssignment = ctx.affinity().assignment(topVer);
        // Avoid iterator creation.
        for (int i = 0; i < entries.size(); i++) {
            GridDhtCacheEntry entry = entries.get(i);
            assert entry.lockedByCurrentThread();
            if (entry.obsolete()) {
                assert req.operation() == DELETE : "Entry can become obsolete only after remove: " + entry;
                continue;
            }
            if (storeErr != null && storeErr.failedKeys().contains(entry.key().value(ctx.cacheObjectContext(), false)))
                continue;
            try {
                // We are holding java-level locks on entries at this point.
                CacheObject writeVal = op == UPDATE ? writeVals.get(i) : null;
                assert writeVal != null || op == DELETE : "null write value found.";
                // Get readers before innerUpdate (reader cleared after remove).
                GridDhtCacheEntry.ReaderId[] readers = entry.readersLocked();
                GridCacheUpdateAtomicResult updRes = entry.innerUpdate(ver, nearNode.id(), locNodeId, op, writeVal, null, /*write-through*/
                false, /*read-through*/
                false, /*retval*/
                sndPrevVal, req.keepBinary(), expiry, /*event*/
                true, /*metrics*/
                true, /*primary*/
                true, /*verCheck*/
                false, topVer, null, replicate ? DR_PRIMARY : DR_NONE, CU.TTL_NOT_CHANGED, CU.EXPIRE_TIME_CALCULATE, null, /*conflict resolve*/
                false, /*intercept*/
                false, req.subjectId(), taskName, null, null, dhtFut);
                assert !updRes.success() || updRes.newTtl() == CU.TTL_NOT_CHANGED || expiry != null : "success=" + updRes.success() + ", newTtl=" + updRes.newTtl() + ", expiry=" + expiry;
                if (intercept) {
                    if (op == UPDATE) {
                        ctx.config().getInterceptor().onAfterPut(new CacheLazyEntry(ctx, entry.key(), updRes.newValue(), req.keepBinary()));
                    } else {
                        assert op == DELETE : op;
                        // Old value should be already loaded for 'CacheInterceptor.onBeforeRemove'.
                        ctx.config().getInterceptor().onAfterRemove(new CacheLazyEntry(ctx, entry.key(), updRes.oldValue(), req.keepBinary()));
                    }
                }
                batchRes.addDeleted(entry, updRes, entries);
                if (dhtFut != null) {
                    EntryProcessor<Object, Object, Object> entryProcessor = entryProcessorMap == null ? null : entryProcessorMap.get(entry.key());
                    dhtFut.addWriteEntry(affAssignment, entry, writeVal, entryProcessor, updRes.newTtl(), CU.EXPIRE_TIME_CALCULATE, null, sndPrevVal, updRes.oldValue(), updRes.updateCounter());
                    if (readers != null)
                        dhtFut.addNearWriteEntries(nearNode, readers, entry, writeVal, entryProcessor, updRes.newTtl(), CU.EXPIRE_TIME_CALCULATE);
                }
                if (hasNear) {
                    if (!ctx.affinity().partitionBelongs(nearNode, entry.partition(), topVer)) {
                        int idx = firstEntryIdx + i;
                        if (req.operation() == TRANSFORM) {
                            res.addNearValue(idx, writeVal, updRes.newTtl(), CU.EXPIRE_TIME_CALCULATE);
                        } else
                            res.addNearTtl(idx, updRes.newTtl(), CU.EXPIRE_TIME_CALCULATE);
                        if (writeVal != null || entry.hasValue()) {
                            IgniteInternalFuture<Boolean> f = entry.addReader(nearNode.id(), req.messageId(), topVer);
                            assert f == null : f;
                        }
                    } else if (GridDhtCacheEntry.ReaderId.contains(readers, nearNode.id())) {
                        // Reader became primary or backup.
                        entry.removeReader(nearNode.id(), req.messageId());
                    } else
                        res.addSkippedIndex(firstEntryIdx + i);
                }
            } catch (GridCacheEntryRemovedException e) {
                assert false : "Entry cannot become obsolete while holding lock.";
                e.printStackTrace();
            }
        }
    } catch (IgniteCheckedException e) {
        res.addFailedKeys(putMap != null ? putMap.keySet() : rmvKeys, e);
    }
    if (storeErr != null) {
        ArrayList<KeyCacheObject> failed = new ArrayList<>(storeErr.failedKeys().size());
        for (Object failedKey : storeErr.failedKeys()) failed.add(ctx.toCacheKeyObject(failedKey));
        res.addFailedKeys(failed, storeErr.getCause());
    }
    return dhtFut;
}
Also used : CacheLazyEntry(org.apache.ignite.internal.processors.cache.CacheLazyEntry) AffinityAssignment(org.apache.ignite.internal.processors.affinity.AffinityAssignment) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) 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) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) GridCacheUpdateAtomicResult(org.apache.ignite.internal.processors.cache.GridCacheUpdateAtomicResult) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with CacheObject

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

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

the class GridDhtColocatedLockFuture method map0.

/**
 * @param keys Keys to map.
 * @param remap Remap flag.
 * @param topLocked Topology locked flag.
 * @throws IgniteCheckedException If mapping failed.
 */
private synchronized void map0(Collection<KeyCacheObject> keys, boolean remap, boolean topLocked) throws IgniteCheckedException {
    AffinityTopologyVersion topVer = this.topVer;
    assert topVer != null;
    assert topVer.topologyVersion() > 0;
    if (CU.affinityNodes(cctx, topVer).isEmpty()) {
        onDone(new ClusterTopologyServerNotFoundException("Failed to map keys for cache " + "(all partition nodes left the grid): " + cctx.name()));
        return;
    }
    boolean clientNode = cctx.kernalContext().clientNode();
    assert !remap || (clientNode && (tx == null || !tx.hasRemoteLocks()));
    // First assume this node is primary for all keys passed in.
    if (!clientNode && mapAsPrimary(keys, topVer))
        return;
    mappings = new ArrayDeque<>();
    // Assign keys to primary nodes.
    GridNearLockMapping map = null;
    for (KeyCacheObject key : keys) {
        GridNearLockMapping updated = map(key, map, topVer);
        // If new mapping was created, add to collection.
        if (updated != map) {
            mappings.add(updated);
            if (tx != null && updated.node().isLocal())
                tx.colocatedLocallyMapped(true);
        }
        map = updated;
    }
    if (isDone()) {
        if (log.isDebugEnabled())
            log.debug("Abandoning (re)map because future is done: " + this);
        return;
    }
    if (log.isDebugEnabled())
        log.debug("Starting (re)map for mappings [mappings=" + mappings + ", fut=" + this + ']');
    boolean hasRmtNodes = false;
    boolean first = true;
    // Create mini futures.
    for (Iterator<GridNearLockMapping> iter = mappings.iterator(); iter.hasNext(); ) {
        GridNearLockMapping mapping = iter.next();
        ClusterNode node = mapping.node();
        Collection<KeyCacheObject> mappedKeys = mapping.mappedKeys();
        boolean loc = node.equals(cctx.localNode());
        assert !mappedKeys.isEmpty();
        GridNearLockRequest req = null;
        Collection<KeyCacheObject> distributedKeys = new ArrayList<>(mappedKeys.size());
        for (KeyCacheObject key : mappedKeys) {
            IgniteTxKey txKey = cctx.txKey(key);
            GridDistributedCacheEntry entry = null;
            if (tx != null) {
                IgniteTxEntry txEntry = tx.entry(txKey);
                if (txEntry != null) {
                    entry = (GridDistributedCacheEntry) txEntry.cached();
                    if (entry != null && loc == entry.detached()) {
                        entry = cctx.colocated().entryExx(key, topVer, true);
                        txEntry.cached(entry);
                    }
                }
            }
            boolean explicit;
            while (true) {
                try {
                    if (entry == null)
                        entry = cctx.colocated().entryExx(key, topVer, true);
                    if (!cctx.isAll(entry, filter)) {
                        if (log.isDebugEnabled())
                            log.debug("Entry being locked did not pass filter (will not lock): " + entry);
                        onComplete(false, false, true);
                        return;
                    }
                    assert loc ^ entry.detached() : "Invalid entry [loc=" + loc + ", entry=" + entry + ']';
                    GridCacheMvccCandidate cand = addEntry(entry);
                    // Will either return value from dht cache or null if this is a miss.
                    IgniteBiTuple<GridCacheVersion, CacheObject> val = entry.detached() ? null : ((GridDhtCacheEntry) entry).versionedValue(topVer);
                    GridCacheVersion dhtVer = null;
                    if (val != null) {
                        dhtVer = val.get1();
                        valMap.put(key, val);
                    }
                    if (cand != null && !cand.reentry()) {
                        if (req == null) {
                            boolean clientFirst = false;
                            if (first) {
                                clientFirst = clientNode && !topLocked && (tx == null || !tx.hasRemoteLocks());
                                first = false;
                            }
                            assert !implicitTx() && !implicitSingleTx() : tx;
                            req = new GridNearLockRequest(cctx.cacheId(), topVer, cctx.nodeId(), threadId, futId, lockVer, inTx(), read, retval, isolation(), isInvalidate(), timeout, mappedKeys.size(), inTx() ? tx.size() : mappedKeys.size(), inTx() && tx.syncMode() == FULL_SYNC, inTx() ? tx.subjectId() : null, inTx() ? tx.taskNameHash() : 0, read ? createTtl : -1L, read ? accessTtl : -1L, skipStore, keepBinary, clientFirst, false, cctx.deploymentEnabled());
                            mapping.request(req);
                        }
                        distributedKeys.add(key);
                        if (tx != null)
                            tx.addKeyMapping(txKey, mapping.node());
                        req.addKeyBytes(key, retval, // Include DHT version to match remote DHT entry.
                        dhtVer, cctx);
                    }
                    explicit = inTx() && cand == null;
                    if (explicit)
                        tx.addKeyMapping(txKey, mapping.node());
                    break;
                } catch (GridCacheEntryRemovedException ignored) {
                    if (log.isDebugEnabled())
                        log.debug("Got removed entry in lockAsync(..) method (will retry): " + entry);
                    entry = null;
                }
            }
            // Mark mapping explicit lock flag.
            if (explicit) {
                boolean marked = tx != null && tx.markExplicit(node.id());
                assert tx == null || marked;
            }
        }
        if (!distributedKeys.isEmpty()) {
            mapping.distributedKeys(distributedKeys);
            hasRmtNodes |= !mapping.node().isLocal();
        } else {
            assert mapping.request() == null;
            iter.remove();
        }
    }
    if (hasRmtNodes) {
        trackable = true;
        if (!remap && !cctx.mvcc().addFuture(this))
            throw new IllegalStateException("Duplicate future ID: " + this);
    } else
        trackable = false;
    proceedMapping();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) GridNearLockMapping(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockMapping) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearLockRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockRequest) ArrayList(java.util.ArrayList) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)

Aggregations

CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)96 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)86 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)46 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)44 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)38 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)26 CacheLockCandidates (org.apache.ignite.internal.processors.cache.CacheLockCandidates)17 GridCacheMvcc (org.apache.ignite.internal.processors.cache.GridCacheMvcc)17 ArrayList (java.util.ArrayList)15 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)15 GridCacheOperation (org.apache.ignite.internal.processors.cache.GridCacheOperation)15 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)13 Nullable (org.jetbrains.annotations.Nullable)13 UUID (java.util.UUID)11 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)11 GridCacheMvccCandidate (org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)11 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)11 IgniteException (org.apache.ignite.IgniteException)10 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)10 LinkedHashMap (java.util.LinkedHashMap)9