Search in sources :

Example 1 with GridCacheDrInfo

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

the class GridNearAtomicUpdateFuture method mapSingleUpdate.

/**
     * @param topVer Topology version.
     * @param futId Future ID.
     * @param mappingKnown {@code True} if update mapping is known locally.
     * @return Request.
     * @throws Exception If failed.
     */
private PrimaryRequestState mapSingleUpdate(AffinityTopologyVersion topVer, Long futId, boolean mappingKnown) throws Exception {
    Object key = F.first(keys);
    Object val;
    GridCacheVersion conflictVer;
    long conflictTtl;
    long conflictExpireTime;
    if (vals != null) {
        // Regular PUT.
        val = F.first(vals);
        conflictVer = null;
        conflictTtl = CU.TTL_NOT_CHANGED;
        conflictExpireTime = CU.EXPIRE_TIME_CALCULATE;
    } else if (conflictPutVals != null) {
        // Conflict PUT.
        GridCacheDrInfo conflictPutVal = F.first(conflictPutVals);
        val = conflictPutVal.valueEx();
        conflictVer = conflictPutVal.version();
        conflictTtl = conflictPutVal.ttl();
        conflictExpireTime = conflictPutVal.expireTime();
    } else if (conflictRmvVals != null) {
        // Conflict REMOVE.
        val = null;
        conflictVer = F.first(conflictRmvVals);
        conflictTtl = CU.TTL_NOT_CHANGED;
        conflictExpireTime = CU.EXPIRE_TIME_CALCULATE;
    } else {
        // Regular REMOVE.
        val = null;
        conflictVer = null;
        conflictTtl = CU.TTL_NOT_CHANGED;
        conflictExpireTime = CU.EXPIRE_TIME_CALCULATE;
    }
    // We still can get here if user pass map with single element.
    if (key == null)
        throw new NullPointerException("Null key.");
    if (val == null && op != GridCacheOperation.DELETE)
        throw new NullPointerException("Null value.");
    KeyCacheObject cacheKey = cctx.toCacheKeyObject(key);
    if (op != TRANSFORM)
        val = cctx.toCacheObject(val);
    else
        val = EntryProcessorResourceInjectorProxy.wrap(cctx.kernalContext(), (EntryProcessor) val);
    List<ClusterNode> nodes = cctx.affinity().nodesByKey(cacheKey, topVer);
    if (F.isEmpty(nodes))
        throw new ClusterTopologyServerNotFoundException("Failed to map keys for cache " + "(all partition nodes left the grid).");
    ClusterNode primary = nodes.get(0);
    boolean needPrimaryRes = !mappingKnown || primary.isLocal() || nodes.size() == 1;
    GridNearAtomicFullUpdateRequest req = new GridNearAtomicFullUpdateRequest(cctx.cacheId(), primary.id(), futId, topVer, topLocked, syncMode, op, retval, expiryPlc, invokeArgs, filter, subjId, taskNameHash, needPrimaryRes, skipStore, keepBinary, recovery, cctx.deploymentEnabled(), 1);
    req.addUpdateEntry(cacheKey, val, conflictTtl, conflictExpireTime, conflictVer);
    return new PrimaryRequestState(req, nodes, true);
}
Also used : GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 2 with GridCacheDrInfo

use of org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo 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)

Example 3 with GridCacheDrInfo

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

the class GridDhtAtomicCache method remapToNewPrimary.

/**
     * @param req Request to remap.
     */
void remapToNewPrimary(GridNearAtomicAbstractUpdateRequest req) {
    assert req.writeSynchronizationMode() == FULL_ASYNC : req;
    if (log.isDebugEnabled())
        log.debug("Remapping near update request locally: " + req);
    Collection<?> vals;
    Collection<GridCacheDrInfo> drPutVals;
    Collection<GridCacheVersion> drRmvVals;
    if (req.conflictVersions() == null) {
        vals = req.values();
        drPutVals = null;
        drRmvVals = null;
    } else if (req.operation() == UPDATE) {
        int size = req.keys().size();
        drPutVals = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            long ttl = req.conflictTtl(i);
            if (ttl == CU.TTL_NOT_CHANGED)
                drPutVals.add(new GridCacheDrInfo(req.value(i), req.conflictVersion(i)));
            else
                drPutVals.add(new GridCacheDrExpirationInfo(req.value(i), req.conflictVersion(i), ttl, req.conflictExpireTime(i)));
        }
        vals = null;
        drRmvVals = null;
    } else {
        assert req.operation() == DELETE : req;
        drRmvVals = req.conflictVersions();
        vals = null;
        drPutVals = null;
    }
    GridNearAtomicUpdateFuture updateFut = new GridNearAtomicUpdateFuture(ctx, this, ctx.config().getWriteSynchronizationMode(), req.operation(), req.keys(), vals, req.invokeArguments(), drPutVals, drRmvVals, req.returnValue(), false, req.expiry(), req.filter(), req.subjectId(), req.taskNameHash(), req.skipStore(), req.keepBinary(), req.recovery(), MAX_RETRIES);
    updateFut.map();
}
Also used : GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheDrExpirationInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrExpirationInfo) ArrayList(java.util.ArrayList)

Example 4 with GridCacheDrInfo

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

the class GridDhtAtomicCache method createSingleUpdateFuture.

/**
     * Craete future for single key-val pair update.
     *
     * @param key Key.
     * @param val Value.
     * @param proc Processor.
     * @param invokeArgs Invoke arguments.
     * @param retval Return value flag.
     * @param filter Filter.
     * @return Future.
     */
private GridNearAtomicAbstractUpdateFuture createSingleUpdateFuture(K key, @Nullable V val, @Nullable EntryProcessor proc, @Nullable Object[] invokeArgs, boolean retval, @Nullable CacheEntryPredicate filter) {
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    GridCacheOperation op;
    Object val0;
    if (val != null) {
        op = UPDATE;
        val0 = val;
    } else if (proc != null) {
        op = TRANSFORM;
        val0 = proc;
    } else {
        op = DELETE;
        val0 = null;
    }
    GridCacheDrInfo conflictPutVal = null;
    GridCacheVersion conflictRmvVer = null;
    if (opCtx != null && opCtx.hasDataCenterId()) {
        Byte dcId = opCtx.dataCenterId();
        assert dcId != null;
        if (op == UPDATE) {
            conflictPutVal = new GridCacheDrInfo(ctx.toCacheObject(val), ctx.versions().next(dcId));
            val0 = null;
        } else if (op == GridCacheOperation.TRANSFORM) {
            conflictPutVal = new GridCacheDrInfo(proc, ctx.versions().next(dcId));
            val0 = null;
        } else
            conflictRmvVer = ctx.versions().next(dcId);
    }
    CacheEntryPredicate[] filters = CU.filterArray(filter);
    if (conflictPutVal == null && conflictRmvVer == null) {
        return new GridNearAtomicSingleUpdateFuture(ctx, this, ctx.config().getWriteSynchronizationMode(), op, key, val0, invokeArgs, retval, false, opCtx != null ? opCtx.expiry() : null, filters, ctx.subjectIdPerCall(null, opCtx), ctx.kernalContext().job().currentTaskNameHash(), opCtx != null && opCtx.skipStore(), opCtx != null && opCtx.isKeepBinary(), opCtx != null && opCtx.recovery(), opCtx != null && opCtx.noRetries() ? 1 : MAX_RETRIES);
    } else {
        return new GridNearAtomicUpdateFuture(ctx, this, ctx.config().getWriteSynchronizationMode(), op, Collections.singletonList(key), val0 != null ? Collections.singletonList(val0) : null, invokeArgs, conflictPutVal != null ? Collections.singleton(conflictPutVal) : null, conflictRmvVer != null ? Collections.singleton(conflictRmvVer) : null, retval, false, opCtx != null ? opCtx.expiry() : null, filters, ctx.subjectIdPerCall(null, opCtx), ctx.kernalContext().job().currentTaskNameHash(), opCtx != null && opCtx.skipStore(), opCtx != null && opCtx.isKeepBinary(), opCtx != null && opCtx.recovery(), opCtx != null && opCtx.noRetries() ? 1 : MAX_RETRIES);
    }
}
Also used : GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) CacheEntryPredicate(org.apache.ignite.internal.processors.cache.CacheEntryPredicate) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation)

Example 5 with GridCacheDrInfo

use of org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo 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)

Aggregations

GridCacheDrInfo (org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo)7 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)6 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)5 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)3 CacheOperationContext (org.apache.ignite.internal.processors.cache.CacheOperationContext)3 Map (java.util.Map)2 UUID (java.util.UUID)2 EntryProcessor (javax.cache.processor.EntryProcessor)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 ClusterTopologyServerNotFoundException (org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException)2 GridCacheDrExpirationInfo (org.apache.ignite.internal.processors.cache.dr.GridCacheDrExpirationInfo)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Ignite (org.apache.ignite.Ignite)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1