Search in sources :

Example 16 with EntryProcessorResult

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

the class GridLocalAtomicCache method updatePartialBatch.

/**
     * @param entries Entries to update.
     * @param ver Cache version.
     * @param writeVals Cache values.
     * @param putMap Values to put.
     * @param rmvKeys Keys to remove.
     * @param expiryPlc Expiry policy.
     * @param err Optional partial update exception.
     * @param subjId Subject ID.
     * @param taskName Task name.
     * @return Partial update exception.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions", "ForLoopReplaceableByForEach" })
@Nullable
private CachePartialUpdateCheckedException updatePartialBatch(List<GridCacheEntryEx> entries, final GridCacheVersion ver, @Nullable List<CacheObject> writeVals, @Nullable Map<KeyCacheObject, CacheObject> putMap, @Nullable Collection<KeyCacheObject> rmvKeys, @Nullable ExpiryPolicy expiryPlc, boolean keepBinary, @Nullable CachePartialUpdateCheckedException err, UUID subjId, String taskName) {
    assert putMap == null ^ rmvKeys == null;
    GridCacheOperation op;
    CacheStorePartialUpdateException storeErr = null;
    try {
        if (putMap != null) {
            try {
                Map<? extends KeyCacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>> view = F.viewReadOnly(putMap, new C1<CacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>>() {

                    @Override
                    public IgniteBiTuple<? extends CacheObject, GridCacheVersion> apply(CacheObject val) {
                        return F.t(val, ver);
                    }
                });
                ctx.store().putAll(null, view);
            } catch (CacheStorePartialUpdateException e) {
                storeErr = e;
            }
            op = UPDATE;
        } else {
            try {
                ctx.store().removeAll(null, rmvKeys);
            } catch (CacheStorePartialUpdateException e) {
                storeErr = e;
            }
            op = DELETE;
        }
    } catch (IgniteCheckedException e) {
        if (err == null)
            err = partialUpdateException();
        err.add(putMap != null ? putMap.keySet() : rmvKeys, e);
        return err;
    }
    boolean intercept = ctx.config().getInterceptor() != null;
    for (int i = 0; i < entries.size(); i++) {
        GridCacheEntryEx entry = entries.get(i);
        assert Thread.holdsLock(entry);
        if (entry.obsolete() || (storeErr != null && storeErr.failedKeys().contains(entry.key().value(ctx.cacheObjectContext(), false))))
            continue;
        try {
            // We are holding java-level locks on entries at this point.
            CacheObject writeVal = op == UPDATE ? writeVals.get(i) : null;
            assert writeVal != null || op == DELETE : "null write value found.";
            GridTuple3<Boolean, Object, EntryProcessorResult<Object>> t = entry.innerUpdateLocal(ver, op, writeVal, null, false, false, false, keepBinary, expiryPlc, true, true, null, false, subjId, taskName);
            if (intercept) {
                if (op == UPDATE)
                    ctx.config().getInterceptor().onAfterPut(new CacheLazyEntry(ctx, entry.key(), writeVal, keepBinary));
                else
                    ctx.config().getInterceptor().onAfterRemove(new CacheLazyEntry(ctx, entry.key(), t.get2(), keepBinary));
            }
        } catch (GridCacheEntryRemovedException ignore) {
            assert false : "Entry cannot become obsolete while holding lock.";
        } catch (IgniteCheckedException e) {
            if (err == null)
                err = partialUpdateException();
            err.add(Collections.singleton(entry.key()), e);
        }
    }
    return err;
}
Also used : CacheLazyEntry(org.apache.ignite.internal.processors.cache.CacheLazyEntry) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with EntryProcessorResult

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

the class DmlStatementsProcessor method splitErrors.

/**
     * Process errors of entry processor - split the keys into duplicated/concurrently modified and those whose
     * processing yielded an exception.
     *
     * @param res Result of {@link GridCacheAdapter#invokeAll)}
     * @return pair [array of duplicated/concurrently modified keys, SQL exception for erroneous keys] (exception is
     * null if all keys are duplicates/concurrently modified ones).
     */
private static PageProcessingErrorResult splitErrors(Map<Object, EntryProcessorResult<Boolean>> res) {
    Set<Object> errKeys = new LinkedHashSet<>(res.keySet());
    SQLException currSqlEx = null;
    SQLException firstSqlEx = null;
    int errors = 0;
    // Let's form a chain of SQL exceptions
    for (Map.Entry<Object, EntryProcessorResult<Boolean>> e : res.entrySet()) {
        try {
            e.getValue().get();
        } catch (EntryProcessorException ex) {
            SQLException next = createJdbcSqlException("Failed to process key '" + e.getKey() + '\'', IgniteQueryErrorCode.ENTRY_PROCESSING);
            next.initCause(ex);
            if (currSqlEx != null)
                currSqlEx.setNextException(next);
            else
                firstSqlEx = next;
            currSqlEx = next;
            errKeys.remove(e.getKey());
            errors++;
        }
    }
    return new PageProcessingErrorResult(errKeys.toArray(), firstSqlEx, errors);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) EntryProcessorException(javax.cache.processor.EntryProcessorException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BinaryObject(org.apache.ignite.binary.BinaryObject) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap)

Example 18 with EntryProcessorResult

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

the class IgniteTxPreloadAbstractTest method testRemoteTxPreloading.

/**
     * @throws Exception If failed.
     */
public void testRemoteTxPreloading() throws Exception {
    IgniteCache<String, Integer> cache = jcache(0);
    for (int i = 0; i < 10_000; i++) cache.put(String.valueOf(i), 0);
    final AtomicInteger gridIdx = new AtomicInteger(1);
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            int idx = gridIdx.getAndIncrement();
            startGrid(idx);
            return null;
        }
    }, GRID_CNT - 1, "grid-starter-" + getName());
    waitForRemoteNodes(grid(0), 2);
    Set<String> keys = new HashSet<>();
    for (int i = 0; i < 10; i++) keys.add(String.valueOf(i * 1000));
    Map<String, EntryProcessorResult<Integer>> resMap = cache.invokeAll(keys, new EntryProcessor<String, Integer, Integer>() {

        @Override
        public Integer process(MutableEntry<String, Integer> e, Object... args) {
            Integer val = e.getValue();
            if (val == null) {
                keyNotLoaded = true;
                e.setValue(1);
                return null;
            }
            e.setValue(val + 1);
            return val;
        }
    });
    assertFalse(keyNotLoaded);
    for (String key : keys) {
        EntryProcessorResult<Integer> res = resMap.get(key);
        assertNotNull(res);
        assertEquals(0, (Object) res.get());
    }
    fut.get();
    for (int i = 0; i < GRID_CNT; i++) {
        for (String key : keys) assertEquals("Unexpected value for cache " + i, (Integer) 1, jcache(i).get(key));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nullable(org.jetbrains.annotations.Nullable) HashSet(java.util.HashSet)

Example 19 with EntryProcessorResult

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

the class GridCacheCommandHandler method appendOrPrepend.

/**
     * Handles append and prepend commands.
     *
     * @param ctx Kernal context.
     * @param cache Cache.
     * @param key Key.
     * @param req Request.
     * @param prepend Whether to prepend.
     * @return Future of operation result.
     * @throws IgniteCheckedException In case of any exception.
     */
private static IgniteInternalFuture<?> appendOrPrepend(final GridKernalContext ctx, final IgniteInternalCache<Object, Object> cache, final Object key, GridRestCacheRequest req, final boolean prepend) throws IgniteCheckedException {
    assert cache != null;
    assert key != null;
    assert req != null;
    final Object val = req.value();
    if (val == null)
        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("val"));
    return ctx.closure().callLocalSafe(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            EntryProcessorResult<Boolean> res = cache.invoke(key, new EntryProcessor<Object, Object, Boolean>() {

                @Override
                public Boolean process(MutableEntry<Object, Object> entry, Object... objects) throws EntryProcessorException {
                    try {
                        Object curVal = entry.getValue();
                        if (curVal == null)
                            return false;
                        // Modify current value with appendix one.
                        Object newVal = appendOrPrepend(curVal, val, !prepend);
                        // Put new value asynchronously.
                        entry.setValue(newVal);
                        return true;
                    } catch (IgniteCheckedException e) {
                        throw new EntryProcessorException(e);
                    }
                }
            });
            try {
                return res.get();
            } catch (EntryProcessorException e) {
                throw new IgniteCheckedException(e.getCause());
            }
        }
    }, false);
}
Also used : EntryProcessor(javax.cache.processor.EntryProcessor) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) MutableEntry(javax.cache.processor.MutableEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryProcessorException(javax.cache.processor.EntryProcessorException)

Example 20 with EntryProcessorResult

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

the class GridCacheAbstractFullApiSelfTest method checkTransformAll.

/**
     * @param concurrency Transaction concurrency.
     * @param isolation Transaction isolation.
     * @throws Exception If failed.
     */
private void checkTransformAll(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    final IgniteCache<String, Integer> cache = jcache();
    cache.put("key2", 1);
    cache.put("key3", 3);
    if (txShouldBeUsed()) {
        Map<String, EntryProcessorResult<String>> res;
        try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
            res = cache.invokeAll(F.asSet("key1", "key2", "key3"), INCR_PROCESSOR);
            tx.commit();
        }
        assertEquals((Integer) 1, cache.get("key1"));
        assertEquals((Integer) 2, cache.get("key2"));
        assertEquals((Integer) 4, cache.get("key3"));
        assertEquals("null", res.get("key1").get());
        assertEquals("1", res.get("key2").get());
        assertEquals("3", res.get("key3").get());
        assertEquals(3, res.size());
        cache.remove("key1");
        cache.put("key2", 1);
        cache.put("key3", 3);
    }
    Map<String, EntryProcessorResult<String>> res = cache.invokeAll(F.asSet("key1", "key2", "key3"), RMV_PROCESSOR);
    for (int i = 0; i < gridCount(); i++) {
        assertNull(jcache(i).localPeek("key1", ONHEAP));
        assertNull(jcache(i).localPeek("key2", ONHEAP));
        assertNull(jcache(i).localPeek("key3", ONHEAP));
    }
    assertEquals("null", res.get("key1").get());
    assertEquals("1", res.get("key2").get());
    assertEquals("3", res.get("key3").get());
    assertEquals(3, res.size());
    cache.remove("key1");
    cache.put("key2", 1);
    cache.put("key3", 3);
    res = cache.invokeAll(F.asSet("key1", "key2", "key3"), INCR_PROCESSOR);
    assertEquals((Integer) 1, cache.get("key1"));
    assertEquals((Integer) 2, cache.get("key2"));
    assertEquals((Integer) 4, cache.get("key3"));
    assertEquals("null", res.get("key1").get());
    assertEquals("1", res.get("key2").get());
    assertEquals("3", res.get("key3").get());
    assertEquals(3, res.size());
    cache.remove("key1");
    cache.put("key2", 1);
    cache.put("key3", 3);
    res = cache.invokeAll(F.asMap("key1", INCR_PROCESSOR, "key2", INCR_PROCESSOR, "key3", INCR_PROCESSOR));
    assertEquals((Integer) 1, cache.get("key1"));
    assertEquals((Integer) 2, cache.get("key2"));
    assertEquals((Integer) 4, cache.get("key3"));
    assertEquals("null", res.get("key1").get());
    assertEquals("1", res.get("key2").get());
    assertEquals("3", res.get("key3").get());
    assertEquals(3, res.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) Transaction(org.apache.ignite.transactions.Transaction)

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