Search in sources :

Example 6 with GridFinishedFuture

use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.

the class GridNearTxLocal method putAllAsync0.

/**
     * Internal method for all put and transform operations. Only one of {@code map}, {@code transformMap}
     * maps must be non-null.
     *
     * @param cacheCtx Context.
     * @param map Key-value map to store.
     * @param invokeMap Invoke map.
     * @param invokeArgs Optional arguments for EntryProcessor.
     * @param drMap DR map.
     * @param retval Key-transform value map to store.
     * @return Operation future.
     */
@SuppressWarnings("unchecked")
private <K, V> IgniteInternalFuture putAllAsync0(final GridCacheContext cacheCtx, @Nullable AffinityTopologyVersion entryTopVer, @Nullable Map<? extends K, ? extends V> map, @Nullable Map<? extends K, ? extends EntryProcessor<K, V, Object>> invokeMap, @Nullable final Object[] invokeArgs, @Nullable Map<KeyCacheObject, GridCacheDrInfo> drMap, final boolean retval) {
    try {
        beforePut(cacheCtx, retval);
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture(e);
    }
    final CacheOperationContext opCtx = cacheCtx.operationContextPerCall();
    final Byte dataCenterId;
    if (opCtx != null && opCtx.hasDataCenterId()) {
        assert drMap == null : drMap;
        assert map != null || invokeMap != null;
        dataCenterId = opCtx.dataCenterId();
    } else
        dataCenterId = null;
    // Cached entry may be passed only from entry wrapper.
    final Map<?, ?> map0 = map;
    final Map<?, EntryProcessor<K, V, Object>> invokeMap0 = (Map<K, EntryProcessor<K, V, Object>>) invokeMap;
    if (log.isDebugEnabled())
        log.debug("Called putAllAsync(...) [tx=" + this + ", map=" + map0 + ", retval=" + retval + "]");
    assert map0 != null || invokeMap0 != null;
    final GridCacheReturn ret = new GridCacheReturn(localResult(), false);
    if (F.isEmpty(map0) && F.isEmpty(invokeMap0)) {
        if (implicit())
            try {
                commit();
            } catch (IgniteCheckedException e) {
                return new GridFinishedFuture<>(e);
            }
        return new GridFinishedFuture<>(ret.success(true));
    }
    try {
        Set<?> keySet = map0 != null ? map0.keySet() : invokeMap0.keySet();
        final Collection<KeyCacheObject> enlisted = new ArrayList<>(keySet.size());
        final boolean keepBinary = opCtx != null && opCtx.isKeepBinary();
        final IgniteInternalFuture<Void> loadFut = enlistWrite(cacheCtx, entryTopVer, keySet, opCtx != null ? opCtx.expiry() : null, map0, invokeMap0, invokeArgs, retval, false, CU.filterArray(null), ret, enlisted, drMap, null, opCtx != null && opCtx.skipStore(), false, keepBinary, opCtx != null && opCtx.recovery(), dataCenterId);
        if (pessimistic()) {
            assert loadFut == null || loadFut.isDone() : loadFut;
            if (loadFut != null) {
                try {
                    loadFut.get();
                } catch (IgniteCheckedException e) {
                    return new GridFinishedFuture(e);
                }
            }
            if (log.isDebugEnabled())
                log.debug("Before acquiring transaction lock for put on keys: " + enlisted);
            long timeout = remainingTime();
            if (timeout == -1)
                return new GridFinishedFuture<>(timeoutException());
            IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(enlisted, timeout, this, // Needed to force load from store.
            invokeMap != null, /*read*/
            retval, isolation, isInvalidate(), -1L, -1L);
            PLC1<GridCacheReturn> plc1 = new PLC1<GridCacheReturn>(ret) {

                @Override
                public GridCacheReturn postLock(GridCacheReturn ret) throws IgniteCheckedException {
                    if (log.isDebugEnabled())
                        log.debug("Acquired transaction lock for put on keys: " + enlisted);
                    postLockWrite(cacheCtx, enlisted, ret, /*remove*/
                    false, retval, /*read*/
                    false, -1L, CU.filterArray(null), /*computeInvoke*/
                    true);
                    return ret;
                }
            };
            if (fut.isDone()) {
                try {
                    return nonInterruptable(plc1.apply(fut.get(), null));
                } catch (GridClosureException e) {
                    return new GridFinishedFuture<>(e.unwrap());
                } catch (IgniteCheckedException e) {
                    try {
                        return nonInterruptable(plc1.apply(false, e));
                    } catch (Exception e1) {
                        return new GridFinishedFuture<>(e1);
                    }
                }
            } else {
                return nonInterruptable(new GridEmbeddedFuture<>(fut, plc1));
            }
        } else
            return optimisticPutFuture(cacheCtx, loadFut, ret, keepBinary);
    } catch (RuntimeException e) {
        onException();
        throw e;
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) ArrayList(java.util.ArrayList) ROLLING_BACK(org.apache.ignite.transactions.TransactionState.ROLLING_BACK) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) EntryProcessor(javax.cache.processor.EntryProcessor) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap)

Example 7 with GridFinishedFuture

use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.

the class GridNearTxLocal method putAsync0.

/**
     * Internal method for single update operation.
     *
     * @param cacheCtx Cache context.
     * @param key Key.
     * @param val Value.
     * @param entryProcessor Entry processor.
     * @param invokeArgs Optional arguments for EntryProcessor.
     * @param retval Return value flag.
     * @param filter Filter.
     * @return Operation future.
     */
private <K, V> IgniteInternalFuture putAsync0(final GridCacheContext cacheCtx, @Nullable AffinityTopologyVersion entryTopVer, K key, @Nullable V val, @Nullable EntryProcessor<K, V, Object> entryProcessor, @Nullable final Object[] invokeArgs, final boolean retval, @Nullable final CacheEntryPredicate filter) {
    assert key != null;
    try {
        beforePut(cacheCtx, retval);
        final GridCacheReturn ret = new GridCacheReturn(localResult(), false);
        CacheOperationContext opCtx = cacheCtx.operationContextPerCall();
        final Byte dataCenterId = opCtx != null ? opCtx.dataCenterId() : null;
        KeyCacheObject cacheKey = cacheCtx.toCacheKeyObject(key);
        boolean keepBinary = opCtx != null && opCtx.isKeepBinary();
        final CacheEntryPredicate[] filters = CU.filterArray(filter);
        final IgniteInternalFuture<Void> loadFut = enlistWrite(cacheCtx, entryTopVer, cacheKey, val, opCtx != null ? opCtx.expiry() : null, entryProcessor, invokeArgs, retval, /*lockOnly*/
        false, filters, ret, opCtx != null && opCtx.skipStore(), /*singleRmv*/
        false, keepBinary, opCtx != null && opCtx.recovery(), dataCenterId);
        if (pessimistic()) {
            assert loadFut == null || loadFut.isDone() : loadFut;
            if (loadFut != null)
                loadFut.get();
            final Collection<KeyCacheObject> enlisted = Collections.singleton(cacheKey);
            if (log.isDebugEnabled())
                log.debug("Before acquiring transaction lock for put on key: " + enlisted);
            long timeout = remainingTime();
            if (timeout == -1)
                return new GridFinishedFuture<>(timeoutException());
            IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(enlisted, timeout, this, // Needed to force load from store.
            entryProcessor != null, /*read*/
            retval, isolation, isInvalidate(), -1L, -1L);
            PLC1<GridCacheReturn> plc1 = new PLC1<GridCacheReturn>(ret) {

                @Override
                public GridCacheReturn postLock(GridCacheReturn ret) throws IgniteCheckedException {
                    if (log.isDebugEnabled())
                        log.debug("Acquired transaction lock for put on keys: " + enlisted);
                    postLockWrite(cacheCtx, enlisted, ret, /*remove*/
                    false, retval, /*read*/
                    false, -1L, filters, /*computeInvoke*/
                    true);
                    return ret;
                }
            };
            if (fut.isDone()) {
                try {
                    return nonInterruptable(plc1.apply(fut.get(), null));
                } catch (GridClosureException e) {
                    return new GridFinishedFuture<>(e.unwrap());
                } catch (IgniteCheckedException e) {
                    try {
                        return nonInterruptable(plc1.apply(false, e));
                    } catch (Exception e1) {
                        return new GridFinishedFuture<>(e1);
                    }
                }
            } else {
                return nonInterruptable(new GridEmbeddedFuture<>(fut, plc1));
            }
        } else
            return optimisticPutFuture(cacheCtx, loadFut, ret, keepBinary);
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture(e);
    } catch (RuntimeException e) {
        onException();
        throw e;
    }
}
Also used : GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheEntryPredicate(org.apache.ignite.internal.processors.cache.CacheEntryPredicate) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 8 with GridFinishedFuture

use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.

the class GridNearTxLocal method removeAllAsync0.

/**
     * @param cacheCtx Cache context.
     * @param keys Keys to remove.
     * @param drMap DR map.
     * @param retval Flag indicating whether a value should be returned.
     * @param filter Filter.
     * @param singleRmv {@code True} for single key remove operation ({@link Cache#remove(Object)}.
     * @return Future for asynchronous remove.
     */
@SuppressWarnings("unchecked")
private <K, V> IgniteInternalFuture<GridCacheReturn> removeAllAsync0(final GridCacheContext cacheCtx, @Nullable AffinityTopologyVersion entryTopVer, @Nullable final Collection<? extends K> keys, @Nullable Map<KeyCacheObject, GridCacheVersion> drMap, final boolean retval, @Nullable final CacheEntryPredicate filter, boolean singleRmv) {
    try {
        checkUpdatesAllowed(cacheCtx);
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture(e);
    }
    cacheCtx.checkSecurity(SecurityPermission.CACHE_REMOVE);
    if (retval)
        needReturnValue(true);
    final Collection<?> keys0;
    if (drMap != null) {
        assert keys == null;
        keys0 = drMap.keySet();
    } else
        keys0 = keys;
    CacheOperationContext opCtx = cacheCtx.operationContextPerCall();
    final Byte dataCenterId;
    if (opCtx != null && opCtx.hasDataCenterId()) {
        assert drMap == null : drMap;
        dataCenterId = opCtx.dataCenterId();
    } else
        dataCenterId = null;
    assert keys0 != null;
    if (log.isDebugEnabled())
        log.debug(S.toString("Called removeAllAsync(...)", "tx", this, false, "keys", keys0, true, "implicit", implicit, false, "retval", retval, false));
    try {
        checkValid();
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture<>(e);
    }
    final GridCacheReturn ret = new GridCacheReturn(localResult(), false);
    if (F.isEmpty(keys0)) {
        if (implicit()) {
            try {
                commit();
            } catch (IgniteCheckedException e) {
                return new GridFinishedFuture<>(e);
            }
        }
        return new GridFinishedFuture<>(ret.success(true));
    }
    init();
    final Collection<KeyCacheObject> enlisted = new ArrayList<>();
    ExpiryPolicy plc;
    final CacheEntryPredicate[] filters = CU.filterArray(filter);
    if (!F.isEmpty(filters))
        plc = opCtx != null ? opCtx.expiry() : null;
    else
        plc = null;
    final boolean keepBinary = opCtx != null && opCtx.isKeepBinary();
    final IgniteInternalFuture<Void> loadFut = enlistWrite(cacheCtx, entryTopVer, keys0, plc, /*lookup map*/
    null, /*invoke map*/
    null, /*invoke arguments*/
    null, retval, /*lock only*/
    false, filters, ret, enlisted, null, drMap, opCtx != null && opCtx.skipStore(), singleRmv, keepBinary, opCtx != null && opCtx.recovery(), dataCenterId);
    if (log.isDebugEnabled())
        log.debug("Remove keys: " + enlisted);
    // to be rolled back.
    if (pessimistic()) {
        assert loadFut == null || loadFut.isDone() : loadFut;
        if (loadFut != null) {
            try {
                loadFut.get();
            } catch (IgniteCheckedException e) {
                return new GridFinishedFuture<>(e);
            }
        }
        if (log.isDebugEnabled())
            log.debug("Before acquiring transaction lock for remove on keys: " + enlisted);
        long timeout = remainingTime();
        if (timeout == -1)
            return new GridFinishedFuture<>(timeoutException());
        IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(enlisted, timeout, this, false, retval, isolation, isInvalidate(), -1L, -1L);
        PLC1<GridCacheReturn> plc1 = new PLC1<GridCacheReturn>(ret) {

            @Override
            protected GridCacheReturn postLock(GridCacheReturn ret) throws IgniteCheckedException {
                if (log.isDebugEnabled())
                    log.debug("Acquired transaction lock for remove on keys: " + enlisted);
                postLockWrite(cacheCtx, enlisted, ret, /*remove*/
                true, retval, /*read*/
                false, -1L, filters, /*computeInvoke*/
                false);
                return ret;
            }
        };
        if (fut.isDone()) {
            try {
                return nonInterruptable(plc1.apply(fut.get(), null));
            } catch (GridClosureException e) {
                return new GridFinishedFuture<>(e.unwrap());
            } catch (IgniteCheckedException e) {
                try {
                    return nonInterruptable(plc1.apply(false, e));
                } catch (Exception e1) {
                    return new GridFinishedFuture<>(e1);
                }
            }
        } else
            return nonInterruptable(new GridEmbeddedFuture<>(fut, plc1));
    } else {
        if (implicit()) {
            // with prepare response, if required.
            assert loadFut.isDone();
            return nonInterruptable(commitNearTxLocalAsync().chain(new CX1<IgniteInternalFuture<IgniteInternalTx>, GridCacheReturn>() {

                @Override
                public GridCacheReturn applyx(IgniteInternalFuture<IgniteInternalTx> txFut) throws IgniteCheckedException {
                    try {
                        txFut.get();
                        return new GridCacheReturn(cacheCtx, true, keepBinary, implicitRes.value(), implicitRes.success());
                    } catch (IgniteCheckedException | RuntimeException e) {
                        rollbackNearTxLocalAsync();
                        throw e;
                    }
                }
            }));
        } else {
            return nonInterruptable(loadFut.chain(new CX1<IgniteInternalFuture<Void>, GridCacheReturn>() {

                @Override
                public GridCacheReturn applyx(IgniteInternalFuture<Void> f) throws IgniteCheckedException {
                    f.get();
                    return ret;
                }
            }));
        }
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) CX1(org.apache.ignite.internal.util.typedef.CX1) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) CacheEntryPredicate(org.apache.ignite.internal.processors.cache.CacheEntryPredicate)

Example 9 with GridFinishedFuture

use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.

the class GridNearTxLocal method getAllAsync.

/**
     * @param cacheCtx Cache context.
     * @param keys Keys to get.
     * @param deserializeBinary Deserialize binary flag.
     * @param skipVals Skip values flag.
     * @param keepCacheObjects Keep cache objects
     * @param skipStore Skip store flag.
     * @return Future for this get.
     */
@SuppressWarnings("unchecked")
public <K, V> IgniteInternalFuture<Map<K, V>> getAllAsync(final GridCacheContext cacheCtx, @Nullable final AffinityTopologyVersion entryTopVer, Collection<KeyCacheObject> keys, final boolean deserializeBinary, final boolean skipVals, final boolean keepCacheObjects, final boolean skipStore, final boolean recovery, final boolean needVer) {
    if (F.isEmpty(keys))
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    init();
    int keysCnt = keys.size();
    boolean single = keysCnt == 1;
    try {
        checkValid();
        final Map<K, V> retMap = new GridLeanMap<>(keysCnt);
        final Map<KeyCacheObject, GridCacheVersion> missed = new GridLeanMap<>(pessimistic() ? keysCnt : 0);
        CacheOperationContext opCtx = cacheCtx.operationContextPerCall();
        ExpiryPolicy expiryPlc = opCtx != null ? opCtx.expiry() : null;
        final Collection<KeyCacheObject> lockKeys = enlistRead(cacheCtx, entryTopVer, keys, expiryPlc, retMap, missed, keysCnt, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, needVer);
        if (single && missed.isEmpty())
            return new GridFinishedFuture<>(retMap);
        // Handle locks.
        if (pessimistic() && !readCommitted() && !skipVals) {
            if (expiryPlc == null)
                expiryPlc = cacheCtx.expiry();
            long accessTtl = expiryPlc != null ? CU.toTtl(expiryPlc.getExpiryForAccess()) : CU.TTL_NOT_CHANGED;
            long createTtl = expiryPlc != null ? CU.toTtl(expiryPlc.getExpiryForCreation()) : CU.TTL_NOT_CHANGED;
            long timeout = remainingTime();
            if (timeout == -1)
                return new GridFinishedFuture<>(timeoutException());
            IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(lockKeys, timeout, this, true, true, isolation, isInvalidate(), createTtl, accessTtl);
            final ExpiryPolicy expiryPlc0 = expiryPlc;
            PLC2<Map<K, V>> plc2 = new PLC2<Map<K, V>>() {

                @Override
                public IgniteInternalFuture<Map<K, V>> postLock() throws IgniteCheckedException {
                    if (log.isDebugEnabled())
                        log.debug("Acquired transaction lock for read on keys: " + lockKeys);
                    // Load keys only after the locks have been acquired.
                    for (KeyCacheObject cacheKey : lockKeys) {
                        K keyVal = (K) (keepCacheObjects ? cacheKey : cacheCtx.cacheObjectContext().unwrapBinaryIfNeeded(cacheKey, !deserializeBinary, true));
                        if (retMap.containsKey(keyVal))
                            // We already have a return value.
                            continue;
                        IgniteTxKey txKey = cacheCtx.txKey(cacheKey);
                        IgniteTxEntry txEntry = entry(txKey);
                        assert txEntry != null;
                        // Check if there is cached value.
                        while (true) {
                            GridCacheEntryEx cached = txEntry.cached();
                            CacheObject val = null;
                            GridCacheVersion readVer = null;
                            EntryGetResult getRes = null;
                            try {
                                Object transformClo = (!F.isEmpty(txEntry.entryProcessors()) && cctx.gridEvents().isRecordable(EVT_CACHE_OBJECT_READ)) ? F.first(txEntry.entryProcessors()) : null;
                                if (needVer) {
                                    getRes = cached.innerGetVersioned(null, GridNearTxLocal.this, /*update-metrics*/
                                    true, /*event*/
                                    !skipVals, CU.subjectId(GridNearTxLocal.this, cctx), transformClo, resolveTaskName(), null, txEntry.keepBinary(), null);
                                    if (getRes != null) {
                                        val = getRes.value();
                                        readVer = getRes.version();
                                    }
                                } else {
                                    val = cached.innerGet(null, GridNearTxLocal.this, /*read through*/
                                    false, /*metrics*/
                                    true, /*events*/
                                    !skipVals, CU.subjectId(GridNearTxLocal.this, cctx), transformClo, resolveTaskName(), null, txEntry.keepBinary());
                                }
                                // If value is in cache and passed the filter.
                                if (val != null) {
                                    missed.remove(cacheKey);
                                    txEntry.setAndMarkValid(val);
                                    if (!F.isEmpty(txEntry.entryProcessors()))
                                        val = txEntry.applyEntryProcessors(val);
                                    cacheCtx.addResult(retMap, cacheKey, val, skipVals, keepCacheObjects, deserializeBinary, false, getRes, readVer, 0, 0, needVer);
                                    if (readVer != null)
                                        txEntry.entryReadVersion(readVer);
                                }
                                // While.
                                break;
                            } catch (GridCacheEntryRemovedException ignore) {
                                if (log.isDebugEnabled())
                                    log.debug("Got removed exception in get postLock (will retry): " + cached);
                                txEntry.cached(entryEx(cacheCtx, txKey, topologyVersion()));
                            }
                        }
                    }
                    if (!missed.isEmpty() && cacheCtx.isLocal()) {
                        AffinityTopologyVersion topVer = topologyVersionSnapshot();
                        if (topVer == null)
                            topVer = entryTopVer;
                        return checkMissed(cacheCtx, topVer != null ? topVer : topologyVersion(), retMap, missed, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, needVer, expiryPlc0);
                    }
                    return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
                }
            };
            FinishClosure<Map<K, V>> finClos = new FinishClosure<Map<K, V>>() {

                @Override
                Map<K, V> finish(Map<K, V> loaded) {
                    retMap.putAll(loaded);
                    return retMap;
                }
            };
            if (fut.isDone()) {
                try {
                    IgniteInternalFuture<Map<K, V>> fut1 = plc2.apply(fut.get(), null);
                    return fut1.isDone() ? new GridFinishedFuture<>(finClos.apply(fut1.get(), null)) : new GridEmbeddedFuture<>(finClos, fut1);
                } catch (GridClosureException e) {
                    return new GridFinishedFuture<>(e.unwrap());
                } catch (IgniteCheckedException e) {
                    try {
                        return plc2.apply(false, e);
                    } catch (Exception e1) {
                        return new GridFinishedFuture<>(e1);
                    }
                }
            } else {
                return new GridEmbeddedFuture<>(fut, plc2, finClos);
            }
        } else {
            assert optimistic() || readCommitted() || skipVals;
            if (!missed.isEmpty()) {
                if (!readCommitted())
                    for (Iterator<KeyCacheObject> it = missed.keySet().iterator(); it.hasNext(); ) {
                        KeyCacheObject cacheKey = it.next();
                        K keyVal = (K) (keepCacheObjects ? cacheKey : cacheCtx.cacheObjectContext().unwrapBinaryIfNeeded(cacheKey, !deserializeBinary, false));
                        if (retMap.containsKey(keyVal))
                            it.remove();
                    }
                if (missed.isEmpty())
                    return new GridFinishedFuture<>(retMap);
                AffinityTopologyVersion topVer = topologyVersionSnapshot();
                if (topVer == null)
                    topVer = entryTopVer;
                return checkMissed(cacheCtx, topVer != null ? topVer : topologyVersion(), retMap, missed, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, needVer, expiryPlc);
            }
            return new GridFinishedFuture<>(retMap);
        }
    } catch (IgniteCheckedException e) {
        setRollbackOnly();
        return new GridFinishedFuture<>(e);
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) ROLLING_BACK(org.apache.ignite.transactions.TransactionState.ROLLING_BACK) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) 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) Iterator(java.util.Iterator) 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) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) 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) LinkedHashMap(java.util.LinkedHashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap)

Example 10 with GridFinishedFuture

use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.

the class GridNearTxLocal method enlistWrite.

/**
     * Internal routine for <tt>putAll(..)</tt>
     *
     * @param cacheCtx Cache context.
     * @param keys Keys to enlist.
     * @param expiryPlc Explicitly specified expiry policy for entry.
     * @param lookup Value lookup map ({@code null} for remove).
     * @param invokeMap Map with entry processors for invoke operation.
     * @param invokeArgs Optional arguments for EntryProcessor.
     * @param retval Flag indicating whether a value should be returned.
     * @param lockOnly If {@code true}, then entry will be enlisted as noop.
     * @param filter User filters.
     * @param ret Return value.
     * @param enlisted Collection of keys enlisted into this transaction.
     * @param drPutMap DR put map (optional).
     * @param drRmvMap DR remove map (optional).
     * @param skipStore Skip store flag.
     * @param singleRmv {@code True} for single key remove operation ({@link Cache#remove(Object)}.
     * @param keepBinary Keep binary flag.
     * @param dataCenterId Optional data center ID.
     * @return Future for missing values loading.
     */
private <K, V> IgniteInternalFuture<Void> enlistWrite(final GridCacheContext cacheCtx, @Nullable AffinityTopologyVersion entryTopVer, Collection<?> keys, @Nullable ExpiryPolicy expiryPlc, @Nullable Map<?, ?> lookup, @Nullable Map<?, EntryProcessor<K, V, Object>> invokeMap, @Nullable Object[] invokeArgs, final boolean retval, boolean lockOnly, final CacheEntryPredicate[] filter, final GridCacheReturn ret, Collection<KeyCacheObject> enlisted, @Nullable Map<KeyCacheObject, GridCacheDrInfo> drPutMap, @Nullable Map<KeyCacheObject, GridCacheVersion> drRmvMap, boolean skipStore, final boolean singleRmv, final boolean keepBinary, final boolean recovery, Byte dataCenterId) {
    assert retval || invokeMap == null;
    try {
        addActiveCache(cacheCtx, recovery);
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture<>(e);
    }
    boolean rmv = lookup == null && invokeMap == null;
    final boolean hasFilters = !F.isEmptyOrNulls(filter) && !F.isAlwaysTrue(filter);
    final boolean needVal = singleRmv || retval || hasFilters;
    final boolean needReadVer = needVal && (serializable() && optimistic());
    try {
        // Set transform flag for transaction.
        if (invokeMap != null)
            transform = true;
        Set<KeyCacheObject> missedForLoad = null;
        for (Object key : keys) {
            if (key == null) {
                rollback();
                throw new NullPointerException("Null key.");
            }
            Object val = rmv || lookup == null ? null : lookup.get(key);
            EntryProcessor entryProcessor = invokeMap == null ? null : invokeMap.get(key);
            GridCacheVersion drVer;
            long drTtl;
            long drExpireTime;
            if (drPutMap != null) {
                GridCacheDrInfo info = drPutMap.get(key);
                assert info != null;
                drVer = info.version();
                drTtl = info.ttl();
                drExpireTime = info.expireTime();
            } else if (drRmvMap != null) {
                assert drRmvMap.get(key) != null;
                drVer = drRmvMap.get(key);
                drTtl = -1L;
                drExpireTime = -1L;
            } else if (dataCenterId != null) {
                drVer = cctx.versions().next(dataCenterId);
                drTtl = -1L;
                drExpireTime = -1L;
            } else {
                drVer = null;
                drTtl = -1L;
                drExpireTime = -1L;
            }
            if (!rmv && val == null && entryProcessor == null) {
                setRollbackOnly();
                throw new NullPointerException("Null value.");
            }
            KeyCacheObject cacheKey = cacheCtx.toCacheKeyObject(key);
            boolean loadMissed = enlistWriteEntry(cacheCtx, entryTopVer, cacheKey, val, entryProcessor, invokeArgs, expiryPlc, retval, lockOnly, filter, drVer, drTtl, drExpireTime, ret, enlisted, skipStore, singleRmv, hasFilters, needVal, needReadVer, keepBinary, recovery);
            if (loadMissed) {
                if (missedForLoad == null)
                    missedForLoad = new HashSet<>();
                missedForLoad.add(cacheKey);
            }
        }
        if (missedForLoad != null) {
            AffinityTopologyVersion topVer = topologyVersionSnapshot();
            if (topVer == null)
                topVer = entryTopVer;
            return loadMissing(cacheCtx, topVer != null ? topVer : topologyVersion(), missedForLoad, filter, ret, needReadVer, singleRmv, hasFilters, /*read through*/
            (invokeMap != null || cacheCtx.config().isLoadPreviousValue()) && !skipStore, retval, keepBinary, recovery, expiryPlc);
        }
        return new GridFinishedFuture<>();
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture<>(e);
    }
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) EntryProcessor(javax.cache.processor.EntryProcessor) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) HashSet(java.util.HashSet)

Aggregations

GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)29 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)24 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)12 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)11 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)10 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)10 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)9 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)8 IgniteTxRollbackCheckedException (org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException)8 GridClosureException (org.apache.ignite.internal.util.lang.GridClosureException)7 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)6 IgniteTxOptimisticCheckedException (org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException)6 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)5 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)5 ArrayList (java.util.ArrayList)4 CacheException (javax.cache.CacheException)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 CacheOperationContext (org.apache.ignite.internal.processors.cache.CacheOperationContext)4