Search in sources :

Example 11 with EntryProcessorResult

use of javax.cache.processor.EntryProcessorResult in project hazelcast by hazelcast.

the class ClientCacheWriteQuorumTest method testInvokeAllOperationSuccessfulWhenQuorumSizeMet.

@Test
public void testInvokeAllOperationSuccessfulWhenQuorumSizeMet() {
    HashSet<Integer> hashSet = new HashSet<Integer>();
    hashSet.add(123);
    EntryProcessorResult epr = cache1.invokeAll(hashSet, new SimpleEntryProcessor()).get(123);
    assertNull(epr);
}
Also used : EntryProcessorResult(javax.cache.processor.EntryProcessorResult) HashSet(java.util.HashSet) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 12 with EntryProcessorResult

use of javax.cache.processor.EntryProcessorResult 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 13 with EntryProcessorResult

use of javax.cache.processor.EntryProcessorResult in project ignite by apache.

the class GridCacheReturn method addEntryProcessResult.

/**
     * @param cctx Context.
     * @param key Key.
     * @param key0 Key value.
     * @param res Result.
     * @param err Error.
     * @param keepBinary Keep binary.
     */
@SuppressWarnings("unchecked")
public synchronized void addEntryProcessResult(GridCacheContext cctx, KeyCacheObject key, @Nullable Object key0, @Nullable Object res, @Nullable Exception err, boolean keepBinary) {
    assert v == null || v instanceof Map : v;
    assert key != null;
    assert res != null || err != null;
    invokeRes = true;
    if (loc) {
        HashMap<Object, EntryProcessorResult> resMap = (HashMap<Object, EntryProcessorResult>) v;
        if (resMap == null) {
            resMap = new HashMap<>();
            v = resMap;
        }
        CacheInvokeResult res0 = err == null ? CacheInvokeResult.fromResult(res) : CacheInvokeResult.fromError(err);
        Object resKey = key0 != null ? key0 : ((keepBinary && key instanceof BinaryObject) ? key : CU.value(key, cctx, true));
        resMap.put(resKey, res0);
    } else {
        assert v == null;
        assert cacheId == 0 || cacheId == cctx.cacheId();
        cacheId = cctx.cacheId();
        if (invokeResCol == null)
            invokeResCol = new ArrayList<>();
        CacheInvokeDirectResult res0 = err == null ? new CacheInvokeDirectResult(key, cctx.toCacheObject(res)) : new CacheInvokeDirectResult(key, err);
        invokeResCol.add(res0);
    }
}
Also used : EntryProcessorResult(javax.cache.processor.EntryProcessorResult) BinaryObject(org.apache.ignite.binary.BinaryObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BinaryObject(org.apache.ignite.binary.BinaryObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with EntryProcessorResult

use of javax.cache.processor.EntryProcessorResult in project ignite by apache.

the class GridLocalAtomicCache method updateWithBatch.

/**
     * Updates entries using batched write-through.
     *
     * @param op Operation.
     * @param keys Keys.
     * @param vals Values.
     * @param invokeArgs Optional arguments for EntryProcessor.
     * @param expiryPlc Expiry policy.
     * @param ver Cache version.
     * @param filter Optional filter.
     * @param subjId Subject ID.
     * @param taskName Task name.
     * @return Results map for invoke operation.
     * @throws CachePartialUpdateCheckedException If update failed.
     */
@SuppressWarnings({ "ForLoopReplaceableByForEach", "unchecked" })
private Map<K, EntryProcessorResult> updateWithBatch(GridCacheOperation op, Collection<? extends K> keys, @Nullable Iterable<?> vals, @Nullable Object[] invokeArgs, @Nullable ExpiryPolicy expiryPlc, GridCacheVersion ver, @Nullable CacheEntryPredicate[] filter, boolean keepBinary, UUID subjId, String taskName) throws IgniteCheckedException {
    List<GridCacheEntryEx> locked = lockEntries(keys);
    try {
        int size = locked.size();
        Map<KeyCacheObject, CacheObject> putMap = null;
        Collection<KeyCacheObject> rmvKeys = null;
        List<CacheObject> writeVals = null;
        Map<K, EntryProcessorResult> invokeResMap = op == TRANSFORM ? U.<K, EntryProcessorResult>newHashMap(size) : null;
        List<GridCacheEntryEx> filtered = new ArrayList<>(size);
        CachePartialUpdateCheckedException err = null;
        Iterator<?> valsIter = vals != null ? vals.iterator() : null;
        boolean intercept = ctx.config().getInterceptor() != null;
        for (int i = 0; i < size; i++) {
            GridCacheEntryEx entry = locked.get(i);
            Object val = valsIter != null ? valsIter.next() : null;
            if (val == null && op != DELETE)
                throw new NullPointerException("Null value.");
            try {
                try {
                    if (!ctx.isAllLocked(entry, filter)) {
                        if (log.isDebugEnabled())
                            log.debug("Entry did not pass the filter (will skip write) [entry=" + entry + ", filter=" + Arrays.toString(filter) + ']');
                        continue;
                    }
                } catch (IgniteCheckedException e) {
                    if (err == null)
                        err = partialUpdateException();
                    err.add(F.asList(entry.key()), e);
                    continue;
                }
                if (op == TRANSFORM) {
                    ctx.kernalContext().resource().inject(val, GridResourceIoc.AnnotationSet.ENTRY_PROCESSOR, ctx.name());
                    EntryProcessor<Object, Object, Object> entryProcessor = (EntryProcessor<Object, Object, Object>) val;
                    CacheObject old = entry.innerGet(null, null, /*read-through*/
                    true, /*update-metrics*/
                    true, /*event*/
                    true, subjId, entryProcessor, taskName, null, keepBinary);
                    Object oldVal = null;
                    CacheInvokeEntry<Object, Object> invokeEntry = new CacheInvokeEntry<>(entry.key(), old, entry.version(), keepBinary, entry);
                    CacheObject updated;
                    Object updatedVal = null;
                    CacheInvokeResult invokeRes = null;
                    try {
                        Object computed = entryProcessor.process(invokeEntry, invokeArgs);
                        updatedVal = ctx.unwrapTemporary(invokeEntry.getValue());
                        updated = ctx.toCacheObject(updatedVal);
                        if (computed != null)
                            invokeRes = CacheInvokeResult.fromResult(ctx.unwrapTemporary(computed));
                    } catch (Exception e) {
                        invokeRes = CacheInvokeResult.fromError(e);
                        updated = old;
                    }
                    if (invokeRes != null)
                        invokeResMap.put((K) entry.key().value(ctx.cacheObjectContext(), false), invokeRes);
                    if (updated == null) {
                        if (intercept) {
                            IgniteBiTuple<Boolean, ?> interceptorRes = ctx.config().getInterceptor().onBeforeRemove(new CacheLazyEntry(ctx, entry.key(), invokeEntry.key(), old, oldVal, keepBinary));
                            if (ctx.cancelRemove(interceptorRes))
                                continue;
                        }
                        // Update previous batch.
                        if (putMap != null) {
                            err = updatePartialBatch(filtered, ver, writeVals, putMap, null, expiryPlc, keepBinary, err, subjId, taskName);
                            putMap = null;
                            writeVals = null;
                            filtered = new ArrayList<>();
                        }
                        // Start collecting new batch.
                        if (rmvKeys == null)
                            rmvKeys = new ArrayList<>(size);
                        rmvKeys.add(entry.key());
                    } else {
                        if (intercept) {
                            Object interceptorVal = ctx.config().getInterceptor().onBeforePut(new CacheLazyEntry(ctx, entry.key(), invokeEntry.getKey(), old, oldVal, keepBinary), updatedVal);
                            if (interceptorVal == null)
                                continue;
                            updated = ctx.toCacheObject(ctx.unwrapTemporary(interceptorVal));
                        }
                        // Update previous batch.
                        if (rmvKeys != null) {
                            err = updatePartialBatch(filtered, ver, null, null, rmvKeys, expiryPlc, keepBinary, err, subjId, taskName);
                            rmvKeys = null;
                            filtered = new ArrayList<>();
                        }
                        if (putMap == null) {
                            putMap = new LinkedHashMap<>(size, 1.0f);
                            writeVals = new ArrayList<>(size);
                        }
                        putMap.put(entry.key(), updated);
                        writeVals.add(updated);
                    }
                } else if (op == UPDATE) {
                    CacheObject cacheVal = ctx.toCacheObject(val);
                    if (intercept) {
                        CacheObject old = entry.innerGet(null, null, /*read-through*/
                        ctx.loadPreviousValue(), /**update-metrics*/
                        true, /**event*/
                        true, subjId, null, taskName, null, keepBinary);
                        Object interceptorVal = ctx.config().getInterceptor().onBeforePut(new CacheLazyEntry(ctx, entry.key(), old, keepBinary), val);
                        if (interceptorVal == null)
                            continue;
                        cacheVal = ctx.toCacheObject(ctx.unwrapTemporary(interceptorVal));
                    }
                    if (putMap == null) {
                        putMap = new LinkedHashMap<>(size, 1.0f);
                        writeVals = new ArrayList<>(size);
                    }
                    putMap.put(entry.key(), cacheVal);
                    writeVals.add(cacheVal);
                } else {
                    assert op == DELETE;
                    if (intercept) {
                        CacheObject old = entry.innerGet(null, null, /*read-through*/
                        ctx.loadPreviousValue(), /**update-metrics*/
                        true, /**event*/
                        true, subjId, null, taskName, null, keepBinary);
                        IgniteBiTuple<Boolean, ?> interceptorRes = ctx.config().getInterceptor().onBeforeRemove(new CacheLazyEntry(ctx, entry.key(), old, keepBinary));
                        if (ctx.cancelRemove(interceptorRes))
                            continue;
                    }
                    if (rmvKeys == null)
                        rmvKeys = new ArrayList<>(size);
                    rmvKeys.add(entry.key());
                }
                filtered.add(entry);
            } catch (IgniteCheckedException e) {
                if (err == null)
                    err = partialUpdateException();
                err.add(F.asList(entry.key()), e);
            } catch (GridCacheEntryRemovedException ignore) {
                assert false : "Entry cannot become obsolete while holding lock.";
            }
        }
        // Store final batch.
        if (putMap != null || rmvKeys != null) {
            err = updatePartialBatch(filtered, ver, writeVals, putMap, rmvKeys, expiryPlc, keepBinary, err, subjId, taskName);
        } else
            assert filtered.isEmpty();
        if (err != null)
            throw err;
        return invokeResMap;
    } finally {
        unlockEntries(locked);
    }
}
Also used : CacheLazyEntry(org.apache.ignite.internal.processors.cache.CacheLazyEntry) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) CacheInvokeResult(org.apache.ignite.internal.processors.cache.CacheInvokeResult) LinkedHashMap(java.util.LinkedHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CachePartialUpdateCheckedException(org.apache.ignite.internal.processors.cache.CachePartialUpdateCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CachePartialUpdateCheckedException(org.apache.ignite.internal.processors.cache.CachePartialUpdateCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) EntryProcessor(javax.cache.processor.EntryProcessor) CacheInvokeEntry(org.apache.ignite.internal.processors.cache.CacheInvokeEntry) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 15 with EntryProcessorResult

use of javax.cache.processor.EntryProcessorResult in project ignite by apache.

the class GridLocalAtomicCache method updateAllInternal.

/**
     * Entry point for all public update methods (put, remove, invoke).
     *
     * @param op Operation.
     * @param keys Keys.
     * @param vals Values.
     * @param invokeArgs Optional arguments for EntryProcessor.
     * @param expiryPlc Expiry policy.
     * @param retval Return value required flag.
     * @param rawRetval Return {@code GridCacheReturn} instance.
     * @param filter Cache entry filter.
     * @param writeThrough Write through.
     * @param readThrough Read through.
     * @return Update result.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
private Object updateAllInternal(GridCacheOperation op, Collection<? extends K> keys, @Nullable Iterable<?> vals, @Nullable Object[] invokeArgs, @Nullable ExpiryPolicy expiryPlc, boolean retval, boolean rawRetval, CacheEntryPredicate filter, boolean writeThrough, boolean readThrough, boolean keepBinary) throws IgniteCheckedException {
    if (keyCheck)
        validateCacheKeys(keys);
    if (op == DELETE)
        ctx.checkSecurity(SecurityPermission.CACHE_REMOVE);
    else
        ctx.checkSecurity(SecurityPermission.CACHE_PUT);
    String taskName = ctx.kernalContext().job().currentTaskName();
    GridCacheVersion ver = ctx.versions().next();
    UUID subjId = ctx.subjectIdPerCall(null);
    CacheEntryPredicate[] filters = CU.filterArray(filter);
    ctx.shared().database().ensureFreeSpace(ctx.memoryPolicy());
    if (writeThrough && keys.size() > 1) {
        return updateWithBatch(op, keys, vals, invokeArgs, expiryPlc, ver, filters, keepBinary, subjId, taskName);
    }
    Iterator<?> valsIter = vals != null ? vals.iterator() : null;
    IgniteBiTuple<Boolean, ?> res = null;
    CachePartialUpdateCheckedException err = null;
    boolean intercept = ctx.config().getInterceptor() != null;
    for (K key : keys) {
        if (key == null)
            throw new NullPointerException("Null key.");
        Object val = valsIter != null ? valsIter.next() : null;
        if (val == null && op != DELETE)
            throw new NullPointerException("Null value.");
        KeyCacheObject cacheKey = ctx.toCacheKeyObject(key);
        if (op == UPDATE)
            val = ctx.toCacheObject(val);
        else if (op == TRANSFORM)
            ctx.kernalContext().resource().inject(val, GridResourceIoc.AnnotationSet.ENTRY_PROCESSOR, ctx.name());
        while (true) {
            GridCacheEntryEx entry = null;
            try {
                entry = entryEx(cacheKey);
                GridTuple3<Boolean, Object, EntryProcessorResult<Object>> t = entry.innerUpdateLocal(ver, val == null ? DELETE : op, val, invokeArgs, writeThrough, readThrough, retval, keepBinary, expiryPlc, true, true, filters, intercept, subjId, taskName);
                if (op == TRANSFORM) {
                    if (t.get3() != null) {
                        Map<K, EntryProcessorResult> computedMap;
                        if (res == null) {
                            computedMap = U.newHashMap(keys.size());
                            res = new IgniteBiTuple<>(true, computedMap);
                        } else
                            computedMap = (Map<K, EntryProcessorResult>) res.get2();
                        computedMap.put(key, t.get3());
                    }
                } else if (res == null)
                    res = new T2(t.get1(), t.get2());
                // While.
                break;
            } catch (GridCacheEntryRemovedException ignored) {
                if (log.isDebugEnabled())
                    log.debug("Got removed entry while updating (will retry): " + key);
                entry = null;
            } catch (IgniteCheckedException e) {
                if (err == null)
                    err = partialUpdateException();
                err.add(F.asList(key), e);
                U.error(log, "Failed to update key : " + key, e);
                break;
            } finally {
                if (entry != null)
                    ctx.evicts().touch(entry, ctx.affinity().affinityTopologyVersion());
            }
        }
    }
    if (err != null)
        throw err;
    Object ret = res == null ? null : rawRetval ? new GridCacheReturn(ctx, true, keepBinary, res.get2(), res.get1()) : (retval || op == TRANSFORM) ? res.get2() : res.get1();
    if (op == TRANSFORM && ret == null)
        ret = Collections.emptyMap();
    return ret;
}
Also used : GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) CachePartialUpdateCheckedException(org.apache.ignite.internal.processors.cache.CachePartialUpdateCheckedException) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheEntryPredicate(org.apache.ignite.internal.processors.cache.CacheEntryPredicate) UUID(java.util.UUID) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) T2(org.apache.ignite.internal.util.typedef.T2) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Aggregations

EntryProcessorResult (javax.cache.processor.EntryProcessorResult)26 Map (java.util.Map)11 LinkedHashMap (java.util.LinkedHashMap)10 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 EntryProcessor (javax.cache.processor.EntryProcessor)5 EntryProcessorException (javax.cache.processor.EntryProcessorException)5 Transaction (org.apache.ignite.transactions.Transaction)5 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Ignite (org.apache.ignite.Ignite)4 BinaryObject (org.apache.ignite.binary.BinaryObject)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)3 CachePartialUpdateCheckedException (org.apache.ignite.internal.processors.cache.CachePartialUpdateCheckedException)3