Search in sources :

Example 96 with IgniteInternalFuture

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

the class GridDhtAtomicCache method invokeAll0.

/**
     * @param async Async operation flag.
     * @param keys Keys.
     * @param entryProcessor Entry processor.
     * @param args Entry processor arguments.
     * @return Future.
     */
private <T> IgniteInternalFuture<Map<K, EntryProcessorResult<T>>> invokeAll0(boolean async, Set<? extends K> keys, final EntryProcessor<K, V, T> entryProcessor, Object... args) {
    A.notNull(keys, "keys", entryProcessor, "entryProcessor");
    if (keyCheck)
        validateCacheKeys(keys);
    Map<? extends K, EntryProcessor> invokeMap = F.viewAsMap(keys, new C1<K, EntryProcessor>() {

        @Override
        public EntryProcessor apply(K k) {
            return entryProcessor;
        }
    });
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    final boolean keepBinary = opCtx != null && opCtx.isKeepBinary();
    IgniteInternalFuture<Map<K, EntryProcessorResult<T>>> resFut = updateAll0(null, invokeMap, args, null, null, false, false, TRANSFORM, async);
    return resFut.chain(new CX1<IgniteInternalFuture<Map<K, EntryProcessorResult<T>>>, Map<K, EntryProcessorResult<T>>>() {

        @Override
        public Map<K, EntryProcessorResult<T>> applyx(IgniteInternalFuture<Map<K, EntryProcessorResult<T>>> fut) throws IgniteCheckedException {
            Map<Object, EntryProcessorResult> resMap = (Map) fut.get();
            return ctx.unwrapInvokeResult(resMap, keepBinary);
        }
    });
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) EntryProcessor(javax.cache.processor.EntryProcessor) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IGNITE_ATOMIC_DEFERRED_ACK_TIMEOUT(org.apache.ignite.IgniteSystemProperties.IGNITE_ATOMIC_DEFERRED_ACK_TIMEOUT) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap) HashMap(java.util.HashMap)

Example 97 with IgniteInternalFuture

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

the class GridDhtAtomicCache method updateAllAsyncInternal0.

/**
     * Executes local update after preloader fetched values.
     *
     * @param nodeId Node ID.
     * @param req Update request.
     * @param completionCb Completion callback.
     */
private void updateAllAsyncInternal0(UUID nodeId, GridNearAtomicAbstractUpdateRequest req, UpdateReplyClosure completionCb) {
    ClusterNode node = ctx.discovery().node(nodeId);
    if (node == null) {
        U.warn(msgLog, "Skip near update request, node originated update request left [" + "futId=" + req.futureId() + ", node=" + nodeId + ']');
        return;
    }
    GridNearAtomicUpdateResponse res = new GridNearAtomicUpdateResponse(ctx.cacheId(), nodeId, req.futureId(), req.partition(), false, ctx.deploymentEnabled());
    assert !req.returnValue() || (req.operation() == TRANSFORM || req.size() == 1);
    GridDhtAtomicAbstractUpdateFuture dhtFut = null;
    boolean remap = false;
    String taskName = ctx.kernalContext().task().resolveTaskName(req.taskNameHash());
    IgniteCacheExpiryPolicy expiry = null;
    try {
        // If batch store update is enabled, we need to lock all entries.
        // First, need to acquire locks on cache entries, then check filter.
        List<GridDhtCacheEntry> locked = null;
        Collection<IgniteBiTuple<GridDhtCacheEntry, GridCacheVersion>> deleted = null;
        try {
            GridDhtPartitionTopology top = topology();
            top.readLock();
            try {
                if (top.stopping()) {
                    res.addFailedKeys(req.keys(), new IgniteCheckedException("Failed to perform cache operation " + "(cache is stopped): " + name()));
                    completionCb.apply(req, res);
                    return;
                }
                // external transaction or explicit lock.
                if (req.topologyLocked() || !needRemap(req.topologyVersion(), top.topologyVersion())) {
                    ctx.shared().database().ensureFreeSpace(ctx.memoryPolicy());
                    locked = lockEntries(req, req.topologyVersion());
                    boolean hasNear = ctx.discovery().cacheNearNode(node, name());
                    // Assign next version for update inside entries lock.
                    GridCacheVersion ver = ctx.versions().next(top.topologyVersion());
                    if (hasNear)
                        res.nearVersion(ver);
                    if (msgLog.isDebugEnabled()) {
                        msgLog.debug("Assigned update version [futId=" + req.futureId() + ", writeVer=" + ver + ']');
                    }
                    assert ver != null : "Got null version for update request: " + req;
                    boolean sndPrevVal = !top.rebalanceFinished(req.topologyVersion());
                    dhtFut = createDhtFuture(ver, req);
                    expiry = expiryPolicy(req.expiry());
                    GridCacheReturn retVal = null;
                    if (// Several keys ...
                    req.size() > 1 && writeThrough() && // and store is enabled ...
                    !req.skipStore() && // and this is not local store ...
                    !ctx.store().isLocal() && // (conflict resolver should be used for local store)
                    !// and no DR.
                    ctx.dr().receiveEnabled()) {
                        // This method can only be used when there are no replicated entries in the batch.
                        UpdateBatchResult updRes = updateWithBatch(node, hasNear, req, res, locked, ver, dhtFut, ctx.isDrEnabled(), taskName, expiry, sndPrevVal);
                        deleted = updRes.deleted();
                        dhtFut = updRes.dhtFuture();
                        if (req.operation() == TRANSFORM)
                            retVal = updRes.invokeResults();
                    } else {
                        UpdateSingleResult updRes = updateSingle(node, hasNear, req, res, locked, ver, dhtFut, ctx.isDrEnabled(), taskName, expiry, sndPrevVal);
                        retVal = updRes.returnValue();
                        deleted = updRes.deleted();
                        dhtFut = updRes.dhtFuture();
                    }
                    if (retVal == null)
                        retVal = new GridCacheReturn(ctx, node.isLocal(), true, null, true);
                    res.returnValue(retVal);
                    if (dhtFut != null) {
                        if (req.writeSynchronizationMode() == PRIMARY_SYNC && // To avoid deadlock disable back-pressure for sender data node.
                        !ctx.discovery().cacheAffinityNode(ctx.discovery().node(nodeId), ctx.name()) && !dhtFut.isDone()) {
                            final IgniteRunnable tracker = GridNioBackPressureControl.threadTracker();
                            if (tracker != null && tracker instanceof GridNioMessageTracker) {
                                ((GridNioMessageTracker) tracker).onMessageReceived();
                                dhtFut.listen(new IgniteInClosure<IgniteInternalFuture<Void>>() {

                                    @Override
                                    public void apply(IgniteInternalFuture<Void> fut) {
                                        ((GridNioMessageTracker) tracker).onMessageProcessed();
                                    }
                                });
                            }
                        }
                        ctx.mvcc().addAtomicFuture(dhtFut.id(), dhtFut);
                    }
                } else {
                    // Should remap all keys.
                    remap = true;
                    res.remapTopologyVersion(top.topologyVersion());
                }
            } finally {
                top.readUnlock();
            }
        } catch (GridCacheEntryRemovedException e) {
            assert false : "Entry should not become obsolete while holding lock.";
            e.printStackTrace();
        } finally {
            if (locked != null)
                unlockEntries(locked, req.topologyVersion());
            // Enqueue if necessary after locks release.
            if (deleted != null) {
                assert !deleted.isEmpty();
                assert ctx.deferredDelete() : this;
                for (IgniteBiTuple<GridDhtCacheEntry, GridCacheVersion> e : deleted) ctx.onDeferredDelete(e.get1(), e.get2());
            }
            // TODO fire events only after successful fsync
            if (ctx.shared().wal() != null)
                ctx.shared().wal().fsync(null);
        }
    } catch (GridDhtInvalidPartitionException ignore) {
        if (log.isDebugEnabled())
            log.debug("Caught invalid partition exception for cache entry (will remap update request): " + req);
        remap = true;
        res.remapTopologyVersion(ctx.topology().topologyVersion());
    } catch (Throwable e) {
        // At least RuntimeException can be thrown by the code above when GridCacheContext is cleaned and there is
        // an attempt to use cleaned resources.
        U.error(log, "Unexpected exception during cache update", e);
        res.addFailedKeys(req.keys(), e);
        completionCb.apply(req, res);
        if (e instanceof Error)
            throw (Error) e;
        return;
    }
    if (remap) {
        assert dhtFut == null;
        completionCb.apply(req, res);
    } else if (dhtFut != null)
        dhtFut.map(node, res.returnValue(), res, completionCb);
    if (req.writeSynchronizationMode() != FULL_ASYNC)
        req.cleanup(!node.isLocal());
    sendTtlUpdateRequest(expiry);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridNioMessageTracker(org.apache.ignite.internal.util.nio.GridNioMessageTracker) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)

Example 98 with IgniteInternalFuture

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

the class GridDhtAtomicCache method getAsync.

/** {@inheritDoc} */
@Override
protected IgniteInternalFuture<V> getAsync(final K key, final boolean forcePrimary, final boolean skipTx, @Nullable UUID subjId, final String taskName, final boolean deserializeBinary, final boolean skipVals, final boolean canRemap, final boolean needVer) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (keyCheck)
        validateCacheKey(key);
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    subjId = ctx.subjectIdPerCall(null, opCtx);
    final UUID subjId0 = subjId;
    final ExpiryPolicy expiryPlc = skipVals ? null : opCtx != null ? opCtx.expiry() : null;
    final boolean skipStore = opCtx != null && opCtx.skipStore();
    final boolean recovery = opCtx != null && opCtx.recovery();
    return asyncOp(new CO<IgniteInternalFuture<V>>() {

        @Override
        public IgniteInternalFuture<V> apply() {
            return getAsync0(ctx.toCacheKeyObject(key), forcePrimary, subjId0, taskName, deserializeBinary, recovery, expiryPlc, skipVals, skipStore, canRemap, needVer);
        }
    });
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) UUID(java.util.UUID) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 99 with IgniteInternalFuture

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

the class GridDhtAtomicCache method updateAll0.

/**
     * Entry point for all public API put/transform methods.
     *
     * @param map Put map. Either {@code map}, {@code invokeMap} or {@code conflictPutMap} should be passed.
     * @param invokeMap Invoke map. Either {@code map}, {@code invokeMap} or {@code conflictPutMap} should be passed.
     * @param invokeArgs Optional arguments for EntryProcessor.
     * @param conflictPutMap Conflict put map.
     * @param conflictRmvMap Conflict remove map.
     * @param retval Return value required flag.
     * @param rawRetval Return {@code GridCacheReturn} instance.
     * @param async Async operation flag.
     * @return Completion future.
     */
@SuppressWarnings("ConstantConditions")
private IgniteInternalFuture updateAll0(@Nullable Map<? extends K, ? extends V> map, @Nullable Map<? extends K, ? extends EntryProcessor> invokeMap, @Nullable Object[] invokeArgs, @Nullable Map<KeyCacheObject, GridCacheDrInfo> conflictPutMap, @Nullable Map<KeyCacheObject, GridCacheVersion> conflictRmvMap, final boolean retval, final boolean rawRetval, final GridCacheOperation op, boolean async) {
    assert ctx.updatesAllowed();
    if (map != null && keyCheck)
        validateCacheKeys(map.keySet());
    ctx.checkSecurity(SecurityPermission.CACHE_PUT);
    final CacheOperationContext opCtx = ctx.operationContextPerCall();
    if (opCtx != null && opCtx.hasDataCenterId()) {
        assert conflictPutMap == null : conflictPutMap;
        assert conflictRmvMap == null : conflictRmvMap;
        if (op == GridCacheOperation.TRANSFORM) {
            assert invokeMap != null : invokeMap;
            conflictPutMap = F.viewReadOnly((Map) invokeMap, new IgniteClosure<EntryProcessor, GridCacheDrInfo>() {

                @Override
                public GridCacheDrInfo apply(EntryProcessor o) {
                    return new GridCacheDrInfo(o, ctx.versions().next(opCtx.dataCenterId()));
                }
            });
            invokeMap = null;
        } else if (op == GridCacheOperation.DELETE) {
            assert map != null : map;
            conflictRmvMap = F.viewReadOnly((Map) map, new IgniteClosure<V, GridCacheVersion>() {

                @Override
                public GridCacheVersion apply(V o) {
                    return ctx.versions().next(opCtx.dataCenterId());
                }
            });
            map = null;
        } else {
            assert map != null : map;
            conflictPutMap = F.viewReadOnly((Map) map, new IgniteClosure<V, GridCacheDrInfo>() {

                @Override
                public GridCacheDrInfo apply(V o) {
                    return new GridCacheDrInfo(ctx.toCacheObject(o), ctx.versions().next(opCtx.dataCenterId()));
                }
            });
            map = null;
        }
    }
    UUID subjId = ctx.subjectIdPerCall(null, opCtx);
    int taskNameHash = ctx.kernalContext().job().currentTaskNameHash();
    final GridNearAtomicUpdateFuture updateFut = new GridNearAtomicUpdateFuture(ctx, this, ctx.config().getWriteSynchronizationMode(), op, map != null ? map.keySet() : invokeMap != null ? invokeMap.keySet() : conflictPutMap != null ? conflictPutMap.keySet() : conflictRmvMap.keySet(), map != null ? map.values() : invokeMap != null ? invokeMap.values() : null, invokeArgs, (Collection) (conflictPutMap != null ? conflictPutMap.values() : null), conflictRmvMap != null ? conflictRmvMap.values() : null, retval, rawRetval, opCtx != null ? opCtx.expiry() : null, CU.filterArray(null), subjId, taskNameHash, opCtx != null && opCtx.skipStore(), opCtx != null && opCtx.isKeepBinary(), opCtx != null && opCtx.recovery(), opCtx != null && opCtx.noRetries() ? 1 : MAX_RETRIES);
    if (async) {
        return asyncOp(new CO<IgniteInternalFuture<Object>>() {

            @Override
            public IgniteInternalFuture<Object> apply() {
                updateFut.map();
                return updateFut;
            }
        });
    } else {
        updateFut.map();
        return updateFut;
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) EntryProcessor(javax.cache.processor.EntryProcessor) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) UUID(java.util.UUID) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap) HashMap(java.util.HashMap)

Example 100 with IgniteInternalFuture

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

the class GridDhtAtomicCache method getAllAsyncInternal.

/**
     * @param keys Keys.
     * @param forcePrimary Force primary flag.
     * @param subjId Subject ID.
     * @param taskName Task name.
     * @param deserializeBinary Deserialize binary flag.
     * @param skipVals Skip values flag.
     * @param canRemap Can remap flag.
     * @param needVer Need version flag.
     * @param asyncOp Async operation flag.
     * @return Future.
     */
private IgniteInternalFuture<Map<K, V>> getAllAsyncInternal(@Nullable final Collection<? extends K> keys, final boolean forcePrimary, @Nullable UUID subjId, final String taskName, final boolean deserializeBinary, final boolean recovery, final boolean skipVals, final boolean canRemap, final boolean needVer, boolean asyncOp) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (F.isEmpty(keys))
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    if (keyCheck)
        validateCacheKeys(keys);
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    subjId = ctx.subjectIdPerCall(subjId, opCtx);
    final UUID subjId0 = subjId;
    final ExpiryPolicy expiryPlc = skipVals ? null : opCtx != null ? opCtx.expiry() : null;
    final boolean skipStore = opCtx != null && opCtx.skipStore();
    if (asyncOp) {
        return asyncOp(new CO<IgniteInternalFuture<Map<K, V>>>() {

            @Override
            public IgniteInternalFuture<Map<K, V>> apply() {
                return getAllAsync0(ctx.cacheKeysView(keys), forcePrimary, subjId0, taskName, deserializeBinary, recovery, expiryPlc, skipVals, skipStore, canRemap, needVer);
            }
        });
    } else {
        return getAllAsync0(ctx.cacheKeysView(keys), forcePrimary, subjId0, taskName, deserializeBinary, recovery, expiryPlc, skipVals, skipStore, canRemap, needVer);
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) UUID(java.util.UUID) 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