Search in sources :

Example 26 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridNearTxLocal method loadMissing.

/**
     * @param cacheCtx  Cache context.
     * @param readThrough Read through flag.
     * @param async if {@code True}, then loading will happen in a separate thread.
     * @param keys Keys.
     * @param skipVals Skip values flag.
     * @param needVer If {@code true} version is required for loaded values.
     * @param c Closure to be applied for loaded values.
     * @param expiryPlc Expiry policy.
     * @return Future with {@code True} value if loading took place.
     */
public IgniteInternalFuture<Void> loadMissing(final GridCacheContext cacheCtx, AffinityTopologyVersion topVer, boolean readThrough, boolean async, final Collection<KeyCacheObject> keys, final boolean skipVals, final boolean needVer, boolean keepBinary, boolean recovery, final ExpiryPolicy expiryPlc, final GridInClosure3<KeyCacheObject, Object, GridCacheVersion> c) {
    IgniteCacheExpiryPolicy expiryPlc0 = optimistic() ? accessPolicy(cacheCtx, keys) : cacheCtx.cache().expiryPolicy(expiryPlc);
    if (cacheCtx.isNear()) {
        return cacheCtx.nearTx().txLoadAsync(this, topVer, keys, readThrough, /*deserializeBinary*/
        false, recovery, expiryPlc0, skipVals, needVer).chain(new C1<IgniteInternalFuture<Map<Object, Object>>, Void>() {

            @Override
            public Void apply(IgniteInternalFuture<Map<Object, Object>> f) {
                try {
                    Map<Object, Object> map = f.get();
                    processLoaded(map, keys, needVer, c);
                    return null;
                } catch (Exception e) {
                    setRollbackOnly();
                    throw new GridClosureException(e);
                }
            }
        });
    } else if (cacheCtx.isColocated()) {
        if (keys.size() == 1) {
            final KeyCacheObject key = F.first(keys);
            return cacheCtx.colocated().loadAsync(key, readThrough, /*force primary*/
            needVer || !cacheCtx.config().isReadFromBackup(), topVer, CU.subjectId(this, cctx), resolveTaskName(), /*deserializeBinary*/
            false, expiryPlc0, skipVals, /*can remap*/
            true, needVer, /*keepCacheObject*/
            true, recovery).chain(new C1<IgniteInternalFuture<Object>, Void>() {

                @Override
                public Void apply(IgniteInternalFuture<Object> f) {
                    try {
                        Object val = f.get();
                        processLoaded(key, val, needVer, skipVals, c);
                        return null;
                    } catch (Exception e) {
                        setRollbackOnly();
                        throw new GridClosureException(e);
                    }
                }
            });
        } else {
            return cacheCtx.colocated().loadAsync(keys, readThrough, /*force primary*/
            needVer || !cacheCtx.config().isReadFromBackup(), topVer, CU.subjectId(this, cctx), resolveTaskName(), /*deserializeBinary*/
            false, recovery, expiryPlc0, skipVals, /*can remap*/
            true, needVer, /*keepCacheObject*/
            true).chain(new C1<IgniteInternalFuture<Map<Object, Object>>, Void>() {

                @Override
                public Void apply(IgniteInternalFuture<Map<Object, Object>> f) {
                    try {
                        Map<Object, Object> map = f.get();
                        processLoaded(map, keys, needVer, c);
                        return null;
                    } catch (Exception e) {
                        setRollbackOnly();
                        throw new GridClosureException(e);
                    }
                }
            });
        }
    } else {
        assert cacheCtx.isLocal();
        return localCacheLoadMissing(cacheCtx, topVer, readThrough, async, keys, skipVals, needVer, keepBinary, recovery, expiryPlc, c);
    }
}
Also used : GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) 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) C1(org.apache.ignite.internal.util.typedef.C1) 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) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 27 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture 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 28 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridCacheQueryManager method start0.

/** {@inheritDoc} */
@Override
public void start0() throws IgniteCheckedException {
    CacheConfiguration ccfg = cctx.config();
    qryProcEnabled = QueryUtils.isEnabled(ccfg);
    qryProc = cctx.kernalContext().query();
    cacheName = cctx.name();
    enabled = qryProcEnabled || (isIndexingSpiEnabled() && !CU.isSystemCache(cacheName));
    maxIterCnt = ccfg.getMaxQueryIteratorsCount();
    detailMetricsSz = ccfg.getQueryDetailMetricsSize();
    if (detailMetricsSz > 0)
        detailMetrics = new ConcurrentHashMap8<>(detailMetricsSz);
    lsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
            Map<Long, GridFutureAdapter<QueryResult<K, V>>> futs = qryIters.remove(nodeId);
            if (futs != null) {
                for (Map.Entry<Long, GridFutureAdapter<QueryResult<K, V>>> entry : futs.entrySet()) {
                    final Object rcpt = recipient(nodeId, entry.getKey());
                    entry.getValue().listen(new CIX1<IgniteInternalFuture<QueryResult<K, V>>>() {

                        @Override
                        public void applyx(IgniteInternalFuture<QueryResult<K, V>> f) throws IgniteCheckedException {
                            f.get().closeIfNotShared(rcpt);
                        }
                    });
                }
            }
            Map<Long, GridFutureAdapter<FieldsResult>> fieldsFuts = fieldsQryRes.remove(nodeId);
            if (fieldsFuts != null) {
                for (Map.Entry<Long, GridFutureAdapter<FieldsResult>> entry : fieldsFuts.entrySet()) {
                    final Object rcpt = recipient(nodeId, entry.getKey());
                    entry.getValue().listen(new CIX1<IgniteInternalFuture<FieldsResult>>() {

                        @Override
                        public void applyx(IgniteInternalFuture<FieldsResult> f) throws IgniteCheckedException {
                            f.get().closeIfNotShared(rcpt);
                        }
                    });
                }
            }
        }
    };
    cctx.events().addListener(lsnr, EVT_NODE_LEFT, EVT_NODE_FAILED);
    qryTopVer = cctx.startTopologyVersion();
    assert qryTopVer != null : cctx.name();
}
Also used : CIX1(org.apache.ignite.internal.util.typedef.CIX1) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) UUID(java.util.UUID) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 29 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridNearTxLocal method commitAsyncLocal.

/**
     * Commits local part of colocated transaction.
     *
     * @return Commit future.
     */
public IgniteInternalFuture<IgniteInternalTx> commitAsyncLocal() {
    if (log.isDebugEnabled())
        log.debug("Committing colocated tx locally: " + this);
    IgniteInternalFuture<?> prep = prepFut;
    // Do not create finish future if there are no remote nodes.
    if (F.isEmpty(dhtMap) && F.isEmpty(nearMap)) {
        if (prep != null)
            return (IgniteInternalFuture<IgniteInternalTx>) prep;
        return new GridFinishedFuture<IgniteInternalTx>(this);
    }
    final GridDhtTxFinishFuture fut = new GridDhtTxFinishFuture<>(cctx, this, true);
    cctx.mvcc().addFuture(fut, fut.futureId());
    if (prep == null || prep.isDone()) {
        assert prep != null || optimistic();
        IgniteCheckedException err = null;
        try {
            if (prep != null)
                // Check for errors of a parent future.
                prep.get();
        } catch (IgniteCheckedException e) {
            err = e;
            U.error(log, "Failed to prepare transaction: " + this, e);
        }
        if (err != null)
            fut.rollbackOnError(err);
        else
            fut.finish(true);
    } else
        prep.listen(new CI1<IgniteInternalFuture<?>>() {

            @Override
            public void apply(IgniteInternalFuture<?> f) {
                IgniteCheckedException err = null;
                try {
                    // Check for errors of a parent future.
                    f.get();
                } catch (IgniteCheckedException e) {
                    err = e;
                    U.error(log, "Failed to prepare transaction: " + this, e);
                }
                if (err != null)
                    fut.rollbackOnError(err);
                else
                    fut.finish(true);
            }
        });
    return fut;
}
Also used : GridDhtTxFinishFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxFinishFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 30 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridLocalAtomicCache method asyncOp.

/**
     * @param op Operation closure.
     * @return Future.
     */
@SuppressWarnings("unchecked")
private IgniteInternalFuture asyncOp(final Callable<?> op) {
    IgniteInternalFuture fail = asyncOpAcquire(/*retry*/
    false);
    if (fail != null)
        return fail;
    IgniteInternalFuture f = ctx.closures().callLocalSafe(op);
    f.listen(new CI1<IgniteInternalFuture<?>>() {

        @Override
        public void apply(IgniteInternalFuture<?> f) {
            asyncOpRelease(false);
        }
    });
    return f;
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Aggregations

IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)178 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)88 Ignite (org.apache.ignite.Ignite)43 ArrayList (java.util.ArrayList)39 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)34 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 IgniteException (org.apache.ignite.IgniteException)23 Callable (java.util.concurrent.Callable)22 ClusterNode (org.apache.ignite.cluster.ClusterNode)22 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)22 Map (java.util.Map)21 IgniteCache (org.apache.ignite.IgniteCache)21 HashMap (java.util.HashMap)19 UUID (java.util.UUID)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)16 CacheException (javax.cache.CacheException)14 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)14 IgniteKernal (org.apache.ignite.internal.IgniteKernal)14