Search in sources :

Example 21 with EntryProcessorResult

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

the class IgniteCacheEntryProcessorNodeJoinTest method checkIncrement.

/**
     * @param cacheName Cache name.
     * @param invokeAll If {@code true} tests invokeAll operation.
     * @param fut If not null then executes updates while future is not done.
     * @param latch Latch to count down when first update is done.
     * @throws Exception If failed.
     * @return Number of increments.
     */
private int checkIncrement(String cacheName, boolean invokeAll, @Nullable IgniteInternalFuture<?> fut, @Nullable CountDownLatch latch) throws Exception {
    int increments = 0;
    for (int k = 0; k < INCREMENTS || (fut != null && !fut.isDone()); k++) {
        increments++;
        if (invokeAll) {
            IgniteCache<String, Set<String>> cache = ignite(0).cache(cacheName);
            Map<String, Processor> procs = new LinkedHashMap<>();
            for (int i = 0; i < KEYS; i++) {
                String key = "set-" + i;
                String val = "value-" + k;
                procs.put(key, new Processor(val));
            }
            Map<String, EntryProcessorResult<Integer>> resMap = cache.invokeAll(procs);
            for (String key : procs.keySet()) {
                EntryProcessorResult<Integer> res = resMap.get(key);
                assertNotNull(res);
                assertEquals(k + 1, (Object) res.get());
            }
        } else {
            IgniteCache<String, Set<String>> cache = ignite(0).cache(cacheName);
            for (int i = 0; i < KEYS; i++) {
                String key = "set-" + i;
                String val = "value-" + k;
                Integer valsCnt = cache.invoke(key, new Processor(val));
                Integer exp = k + 1;
                if (!exp.equals(valsCnt))
                    log.info("Unexpected return value [valsCnt=" + valsCnt + ", exp=" + exp + ", cacheVal=" + cache.get(key) + ']');
                assertEquals(exp, valsCnt);
            }
        }
        if (latch != null && k == 0)
            latch.countDown();
    }
    return increments;
}
Also used : EntryProcessorResult(javax.cache.processor.EntryProcessorResult) HashSet(java.util.HashSet) Set(java.util.Set) EntryProcessor(javax.cache.processor.EntryProcessor) LinkedHashMap(java.util.LinkedHashMap)

Example 22 with EntryProcessorResult

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

the class IgniteCacheConfigVariationsFullApiTest method checkInvokeAll.

/**
     * @param concurrency Transaction concurrency.
     * @param isolation Transaction isolation.
     * @throws Exception If failed.
     */
private void checkInvokeAll(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    // TODO IGNITE-2664: enable tests for all modes when IGNITE-2664 will be fixed.
    if (dataMode != DataMode.EXTERNALIZABLE && gridCount() > 1)
        return;
    final Object key1 = key(1);
    final Object key2 = key(2);
    final Object key3 = key(3);
    final Object val1 = value(1);
    final Object val2 = value(2);
    final Object val3 = value(3);
    final Object val4 = value(4);
    final IgniteCache<Object, Object> cache = jcache();
    cache.put(key2, val1);
    cache.put(key3, val3);
    if (txShouldBeUsed()) {
        Map<Object, EntryProcessorResult<Object>> res;
        try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
            res = cache.invokeAll(F.asSet(key1, key2, key3), INCR_PROCESSOR, dataMode);
            tx.commit();
        }
        assertEquals(val1, cache.get(key1));
        assertEquals(val2, cache.get(key2));
        assertEquals(val4, cache.get(key3));
        assertNull(res.get(key1));
        assertEquals(val1, res.get(key2).get());
        assertEquals(val3, res.get(key3).get());
        assertEquals(2, res.size());
        cache.remove(key1);
        cache.put(key2, val1);
        cache.put(key3, val3);
    }
    Map<Object, EntryProcessorResult<Object>> 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));
    }
    assertNull(res.get(key1));
    assertEquals(val1, res.get(key2).get());
    assertEquals(val3, res.get(key3).get());
    assertEquals(2, res.size());
    cache.remove(key1);
    cache.put(key2, val1);
    cache.put(key3, val3);
    res = cache.invokeAll(F.asSet(key1, key2, key3), INCR_PROCESSOR, dataMode);
    assertEquals(val1, cache.get(key1));
    assertEquals(val2, cache.get(key2));
    assertEquals(val4, cache.get(key3));
    assertNull(res.get(key1));
    assertEquals(val1, res.get(key2).get());
    assertEquals(val3, res.get(key3).get());
    assertEquals(2, res.size());
    cache.remove(key1);
    cache.put(key2, val1);
    cache.put(key3, val3);
    res = cache.invokeAll(F.asMap(key1, INCR_PROCESSOR, key2, INCR_PROCESSOR, key3, INCR_PROCESSOR), dataMode);
    assertEquals(val1, cache.get(key1));
    assertEquals(val2, cache.get(key2));
    assertEquals(val4, cache.get(key3));
    assertNull(res.get(key1));
    assertEquals(val1, res.get(key2).get());
    assertEquals(val3, res.get(key3).get());
    assertEquals(2, res.size());
}
Also used : EntryProcessorResult(javax.cache.processor.EntryProcessorResult) Transaction(org.apache.ignite.transactions.Transaction)

Example 23 with EntryProcessorResult

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

the class IgniteCacheConfigVariationsFullApiTest method checkInvokeAllAsync.

/**
     * @param concurrency Transaction concurrency.
     * @param isolation Transaction isolation.
     * @throws Exception If failed.
     */
private void checkInvokeAllAsync(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    // TODO IGNITE-2664: enable tests for all modes when IGNITE-2664 will be fixed.
    if (dataMode != DataMode.EXTERNALIZABLE && gridCount() > 1)
        return;
    final Object key1 = key(1);
    final Object key2 = key(2);
    final Object key3 = key(3);
    final Object val1 = value(1);
    final Object val2 = value(2);
    final Object val3 = value(3);
    final Object val4 = value(4);
    final IgniteCache<Object, Object> cache = jcache();
    cache.put(key2, val1);
    cache.put(key3, val3);
    if (txShouldBeUsed()) {
        Map<Object, EntryProcessorResult<Object>> res;
        try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
            res = cache.invokeAllAsync(F.asSet(key1, key2, key3), INCR_PROCESSOR, dataMode).get();
            tx.commit();
        }
        assertEquals(val1, cache.get(key1));
        assertEquals(val2, cache.get(key2));
        assertEquals(val4, cache.get(key3));
        assertNull(res.get(key1));
        assertEquals(val1, res.get(key2).get());
        assertEquals(val3, res.get(key3).get());
        assertEquals(2, res.size());
        cache.remove(key1);
        cache.put(key2, val1);
        cache.put(key3, val3);
    }
    Map<Object, EntryProcessorResult<Object>> res = cache.invokeAllAsync(F.asSet(key1, key2, key3), RMV_PROCESSOR).get();
    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));
    }
    assertNull(res.get(key1));
    assertEquals(val1, res.get(key2).get());
    assertEquals(val3, res.get(key3).get());
    assertEquals(2, res.size());
    cache.remove(key1);
    cache.put(key2, val1);
    cache.put(key3, val3);
    res = cache.invokeAllAsync(F.asSet(key1, key2, key3), INCR_PROCESSOR, dataMode).get();
    assertEquals(val1, cache.get(key1));
    assertEquals(val2, cache.get(key2));
    assertEquals(val4, cache.get(key3));
    assertNull(res.get(key1));
    assertEquals(val1, res.get(key2).get());
    assertEquals(val3, res.get(key3).get());
    assertEquals(2, res.size());
    cache.remove(key1);
    cache.put(key2, val1);
    cache.put(key3, val3);
    res = cache.invokeAllAsync(F.asMap(key1, INCR_PROCESSOR, key2, INCR_PROCESSOR, key3, INCR_PROCESSOR), dataMode).get();
    assertEquals(val1, cache.get(key1));
    assertEquals(val2, cache.get(key2));
    assertEquals(val4, cache.get(key3));
    assertNull(res.get(key1));
    assertEquals(val1, res.get(key2).get());
    assertEquals(val3, res.get(key3).get());
    assertEquals(2, res.size());
}
Also used : EntryProcessorResult(javax.cache.processor.EntryProcessorResult) Transaction(org.apache.ignite.transactions.Transaction)

Example 24 with EntryProcessorResult

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

the class IgniteCachePutRetryAbstractSelfTest method checkRetry.

/**
     * @param test Test type.
     * @param evict If {@code true} uses eviction policy
     * @param store If {@code true} uses cache with store.
     * @throws Exception If failed.
     */
protected final void checkRetry(Test test, boolean evict, boolean store) throws Exception {
    ignite(0).createCache(cacheConfiguration(evict, store));
    final AtomicBoolean finished = new AtomicBoolean();
    int keysCnt = keysCount();
    IgniteInternalFuture<Object> fut = runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Random rnd = new Random();
            while (!finished.get()) {
                stopGrid(3);
                U.sleep(300);
                startGrid(3);
                if (// OPC possible only when there is no migration from one backup to another.
                rnd.nextBoolean())
                    awaitPartitionMapExchange();
            }
            return null;
        }
    });
    final IgniteCache<Integer, Integer> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
    int iter = 0;
    try {
        long stopTime = System.currentTimeMillis() + DURATION;
        switch(test) {
            case PUT:
                {
                    while (System.currentTimeMillis() < stopTime) {
                        Integer val = ++iter;
                        for (int i = 0; i < keysCnt; i++) cache.put(i, val);
                        for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
                    }
                    break;
                }
            case GET_AND_PUT:
                {
                    for (int i = 0; i < keysCnt; i++) cache.put(i, 0);
                    while (System.currentTimeMillis() < stopTime) {
                        Integer expOld = iter;
                        Integer val = ++iter;
                        for (int i = 0; i < keysCnt; i++) {
                            Integer old = cache.getAndPut(i, val);
                            assertTrue("Unexpected old value [old=" + old + ", exp=" + expOld + ']', expOld.equals(old) || val.equals(old));
                        }
                        for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
                    }
                    break;
                }
            case TX_PUT:
                {
                    while (System.currentTimeMillis() < stopTime) {
                        final Integer val = ++iter;
                        Ignite ignite = ignite(0);
                        for (int i = 0; i < keysCnt; i++) {
                            final Integer key = i;
                            doInTransaction(ignite, new Callable<Void>() {

                                @Override
                                public Void call() throws Exception {
                                    cache.put(key, val);
                                    return null;
                                }
                            });
                        }
                        for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
                    }
                    break;
                }
            case PUT_ALL:
                {
                    while (System.currentTimeMillis() < stopTime) {
                        Integer val = ++iter;
                        Map<Integer, Integer> map = new LinkedHashMap<>();
                        for (int i = 0; i < keysCnt; i++) {
                            map.put(i, val);
                            if (map.size() == 100 || i == keysCnt - 1) {
                                cache.putAll(map);
                                map.clear();
                            }
                        }
                        for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
                    }
                }
            case PUT_ASYNC:
                {
                    while (System.currentTimeMillis() < stopTime) {
                        Integer val = ++iter;
                        for (int i = 0; i < keysCnt; i++) cache.putAsync(i, val).get();
                        for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.getAsync(i).get());
                    }
                    break;
                }
            case INVOKE:
                {
                    while (System.currentTimeMillis() < stopTime) {
                        Integer val = ++iter;
                        Integer expOld = iter - 1;
                        for (int i = 0; i < keysCnt; i++) {
                            Integer old = cache.invoke(i, new SetEntryProcessor(val));
                            assertNotNull(old);
                            assertTrue(old.equals(expOld) || old.equals(val));
                        }
                        for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
                    }
                    break;
                }
            case INVOKE_ALL:
                {
                    while (System.currentTimeMillis() < stopTime) {
                        Integer val = ++iter;
                        Integer expOld = iter - 1;
                        Set<Integer> keys = new LinkedHashSet<>();
                        for (int i = 0; i < keysCnt; i++) {
                            keys.add(i);
                            if (keys.size() == 100 || i == keysCnt - 1) {
                                Map<Integer, EntryProcessorResult<Integer>> resMap = cache.invokeAll(keys, new SetEntryProcessor(val));
                                for (Integer key : keys) {
                                    EntryProcessorResult<Integer> res = resMap.get(key);
                                    assertNotNull(res);
                                    Integer old = res.get();
                                    assertTrue(old.equals(expOld) || old.equals(val));
                                }
                                assertEquals(keys.size(), resMap.size());
                                keys.clear();
                            }
                        }
                        for (int i = 0; i < keysCnt; i++) assertEquals(val, cache.get(i));
                    }
                    break;
                }
            default:
                assert false : test;
        }
    } finally {
        finished.set(true);
        fut.get();
    }
    for (int i = 0; i < keysCnt; i++) assertEquals((Integer) iter, cache.get(i));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) CacheLoaderException(javax.cache.integration.CacheLoaderException) CachePartialUpdateException(org.apache.ignite.cache.CachePartialUpdateException) CacheWriterException(javax.cache.integration.CacheWriterException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 25 with EntryProcessorResult

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

the class IgfsMetaManager method createFileOrDirectory.

/**
     * Create file or directory.
     *
     * @param dir Directory flag.
     * @param pathIds Path IDs.
     * @param lockInfos Lock infos.
     * @param dirProps Directory properties.
     * @param fileProps File properties.
     * @param blockSize Block size.
     * @param affKey Affinity key.
     * @param evictExclude Evict exclude flag.
     * @param secondaryCtx Secondary file system create context.
     * @param secondaryOutHolder Secondary output stream holder.
     * @return Result.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
private IgfsPathsCreateResult createFileOrDirectory(boolean dir, IgfsPathIds pathIds, Map<IgniteUuid, IgfsEntryInfo> lockInfos, Map<String, String> dirProps, Map<String, String> fileProps, int blockSize, @Nullable IgniteUuid affKey, boolean evictExclude, @Nullable IgfsSecondaryFileSystemCreateContext secondaryCtx, @Nullable T1<OutputStream> secondaryOutHolder) throws IgniteCheckedException {
    // This is our starting point.
    int lastExistingIdx = pathIds.lastExistingIndex();
    IgfsEntryInfo lastExistingInfo = lockInfos.get(pathIds.lastExistingId());
    // If current info already contains entry with the same name as it's child, then something
    // has changed concurrently. We must re-try because we cannot get info of this unexpected
    // element due to possible deadlocks.
    int curIdx = lastExistingIdx + 1;
    String curPart = pathIds.part(curIdx);
    IgniteUuid curId = pathIds.surrogateId(curIdx);
    if (lastExistingInfo.hasChild(curPart))
        return null;
    // Create entry in the secondary file system if needed.
    if (secondaryCtx != null) {
        assert secondaryOutHolder != null;
        secondaryOutHolder.set(secondaryCtx.create());
    }
    Map<IgniteUuid, EntryProcessor> procMap = new HashMap<>();
    // First step: add new entry to the last existing element.
    procMap.put(lastExistingInfo.id(), new IgfsMetaDirectoryListingAddProcessor(curPart, new IgfsListingEntry(curId, dir || !pathIds.isLastIndex(curIdx))));
    // Events support.
    IgfsPath lastCreatedPath = pathIds.lastExistingPath();
    List<IgfsPath> createdPaths = new ArrayList<>(pathIds.count() - curIdx);
    // Second step: create middle directories.
    long curTime = System.currentTimeMillis();
    while (curIdx < pathIds.count() - 1) {
        lastCreatedPath = new IgfsPath(lastCreatedPath, curPart);
        int nextIdx = curIdx + 1;
        String nextPart = pathIds.part(nextIdx);
        IgniteUuid nextId = pathIds.surrogateId(nextIdx);
        long accessTime;
        long modificationTime;
        Map<String, String> props;
        if (secondaryCtx != null) {
            accessTime = 0L;
            modificationTime = 0L;
            props = null;
        } else {
            accessTime = curTime;
            modificationTime = curTime;
            props = dirProps;
        }
        procMap.put(curId, new IgfsMetaDirectoryCreateProcessor(accessTime, modificationTime, props, nextPart, new IgfsListingEntry(nextId, dir || !pathIds.isLastIndex(nextIdx))));
        // Save event.
        createdPaths.add(lastCreatedPath);
        // Advance things further.
        curIdx++;
        curPart = nextPart;
        curId = nextId;
    }
    // Third step: create leaf.
    if (dir) {
        long accessTime;
        long modificationTime;
        Map<String, String> props;
        if (secondaryCtx != null) {
            accessTime = 0L;
            modificationTime = 0L;
            props = null;
        } else {
            accessTime = curTime;
            modificationTime = curTime;
            props = dirProps;
        }
        procMap.put(curId, new IgfsMetaDirectoryCreateProcessor(accessTime, modificationTime, props));
    } else {
        long newAccessTime;
        long newModificationTime;
        Map<String, String> newProps;
        long newLen;
        int newBlockSize;
        if (secondaryCtx != null) {
            newAccessTime = 0L;
            newModificationTime = 0L;
            newProps = null;
        } else {
            newAccessTime = curTime;
            newModificationTime = curTime;
            newProps = fileProps;
        }
        newLen = 0L;
        newBlockSize = blockSize;
        procMap.put(curId, new IgfsMetaFileCreateProcessor(newAccessTime, newModificationTime, newProps, newBlockSize, affKey, createFileLockId(false), evictExclude, newLen));
    }
    createdPaths.add(pathIds.path());
    // Execute cache operations.
    Map<Object, EntryProcessorResult> invokeRes = ((IgniteInternalCache) id2InfoPrj).invokeAll(procMap);
    IgfsEntryInfo info = (IgfsEntryInfo) invokeRes.get(curId).get();
    return new IgfsPathsCreateResult(createdPaths, info);
}
Also used : IgfsMetaDirectoryListingAddProcessor(org.apache.ignite.internal.processors.igfs.meta.IgfsMetaDirectoryListingAddProcessor) HashMap(java.util.HashMap) IgfsMetaFileCreateProcessor(org.apache.ignite.internal.processors.igfs.meta.IgfsMetaFileCreateProcessor) ArrayList(java.util.ArrayList) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) IgfsPath(org.apache.ignite.igfs.IgfsPath) EntryProcessor(javax.cache.processor.EntryProcessor) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgfsMetaDirectoryCreateProcessor(org.apache.ignite.internal.processors.igfs.meta.IgfsMetaDirectoryCreateProcessor)

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