Java Code Examples for org.apache.ignite.IgniteCache#invokeAll()

The following examples show how to use org.apache.ignite.IgniteCache#invokeAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgniteCacheRandomOperationBenchmark.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Ignite cache.
 * @throws Exception If failed.
 */
private void doInvokeAll(IgniteCache<Object, Object> cache) throws Exception {
    Map<Object, EntryProcessor<Object, Object, Object>> map = new TreeMap<>();

    Class keyCls = randomKeyClass(cache.getName());

    for (int cnt = 0; cnt < args.batch(); cnt++) {
        int i = nextRandom(args.range());

        Object key = ModelUtil.create(keyCls, i);

        if (nextBoolean())
            map.put(key,
                new BenchmarkReplaceValueEntryProcessor(createRandomValue(i + 1, cache.getName())));
        else
            map.put(key, rmvEntryProc);
    }

    cache.invokeAll(map);
}
 
Example 2
Source File: GridCacheAbstractMetricsSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testInvokeAllMultipleKeysAvgTime() throws IgniteCheckedException {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    Set<Integer> keys = new TreeSet<>();
    keys.add(1);
    keys.add(2);

    assertEquals(0.0, cache.localMetrics().getEntryProcessorAverageInvocationTime(), 0.001f);

    cache.invokeAll(keys, updatingProcessor);

    U.sleep(100);

    assertTrue(cache.localMetrics().getEntryProcessorAverageInvocationTime() > 0.0);
}
 
Example 3
Source File: CacheClientStoreSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
private void doTestNoStore() throws Exception {
    factory = null;

    Ignite ignite = startClientGrid("client-1");

    IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME);

    cache.get(0);
    cache.getAll(F.asSet(0, 1));
    cache.getAndPut(0, 0);
    cache.getAndPutIfAbsent(0, 0);
    cache.getAndRemove(0);
    cache.getAndReplace(0, 0);
    cache.put(0, 0);
    cache.putAll(F.asMap(0, 0, 1, 1));
    cache.putIfAbsent(0, 0);
    cache.remove(0);
    cache.remove(0, 0);
    cache.removeAll(F.asSet(0, 1));
    cache.removeAll();
    cache.invoke(0, new EP());
    cache.invokeAll(F.asSet(0, 1), new EP());
}
 
Example 4
Source File: GridTransformSpringInjectionSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 * @throws Exception If failed.
 */
private void doTransformResourceInjection(IgniteCache<String, Integer> cache) throws Exception {
    final Collection<ResourceType> required = Arrays.asList(
        ResourceType.SPRING_APPLICATION_CONTEXT,
        ResourceType.SPRING_BEAN);

    Integer flags = cache.invoke(UUID.randomUUID().toString(), new SpringResourceInjectionEntryProcessor());

    assertTrue("Processor result is null", flags != null);

    log.info("Injection flag: " + Integer.toBinaryString(flags));

    Collection<ResourceType> notInjected = ResourceInfoSet.valueOf(flags).notInjected(required);

    if (!notInjected.isEmpty())
        fail("Can't inject resource(s): " + Arrays.toString(notInjected.toArray()));

    Set<String> keys = new HashSet<>(Arrays.asList(UUID.randomUUID().toString(),
        UUID.randomUUID().toString(),
        UUID.randomUUID().toString(),
        UUID.randomUUID().toString()));

    Map<String, EntryProcessorResult<Integer>> results = cache.invokeAll(keys,
        new SpringResourceInjectionEntryProcessor());

    assertEquals(keys.size(), results.size());

    for (EntryProcessorResult<Integer> res : results.values()) {
        Collection<ResourceType> notInjected1 = ResourceInfoSet.valueOf(res.get()).notInjected(required);

        if (!notInjected1.isEmpty())
            fail("Can't inject resource(s): " + Arrays.toString(notInjected1.toArray()));
    }
}
 
Example 5
Source File: IgniteCacheGroupsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 */
private void cacheInvokeAll(IgniteCache cache) {
    int keys = 100;
    Map<Integer, Integer> data = generateDataMap(keys);

    cache.putAll(data);

    Random rnd = ThreadLocalRandom.current();

    int one = rnd.nextInt();
    int two = rnd.nextInt();

    Map<Integer, CacheInvokeResult<Integer>> res = cache.invokeAll(data.keySet(), new CacheEntryProcessor<Integer, Integer, Integer>() {
        @Override public Integer process(MutableEntry<Integer, Integer> entry, Object... arguments) throws EntryProcessorException {
            Object expected = ((Map)arguments[0]).get(entry.getKey());

            assertEquals(expected, entry.getValue());

            // Some calculation.
            return (Integer)arguments[1] + (Integer)arguments[2];
        }
    }, data, one, two);

    assertEquals(keys, res.size());
    assertEquals(one + two, (Object)res.get(0).get());

    tearDown(cache);
}
 
Example 6
Source File: GridCacheAbstractMetricsSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testInvokeAllAvgTime() throws IgniteCheckedException {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    assertEquals(0.0, cache.localMetrics().getEntryProcessorAverageInvocationTime(), 0.001f);

    cache.invokeAll(ImmutableMap.of(0, updatingProcessor,
        1, readingProcessor,
        2, removingProcessor));

    U.sleep(100);

    assertTrue(cache.localMetrics().getEntryProcessorAverageInvocationTime() > 0.0);
}
 
Example 7
Source File: CacheClientStoreSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testCorrectStore() throws Exception {
    nearEnabled = false;
    cacheMode = CacheMode.PARTITIONED;
    factory = new Factory1();

    startGrids(2);

    Ignite ignite = startClientGrid("client-1");

    IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME);

    cache.get(0);
    cache.getAll(F.asSet(0, 1));
    cache.getAndPut(0, 0);
    cache.getAndPutIfAbsent(0, 0);
    cache.getAndRemove(0);
    cache.getAndReplace(0, 0);
    cache.put(0, 0);
    cache.putAll(F.asMap(0, 0, 1, 1));
    cache.putIfAbsent(0, 0);
    cache.remove(0);
    cache.remove(0, 0);
    cache.removeAll(F.asSet(0, 1));
    cache.removeAll();
    cache.invoke(0, new EP());
    cache.invokeAll(F.asSet(0, 1), new EP());
}
 
Example 8
Source File: IgniteCacheFailedUpdateResponseTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 */
private void testInvokeAll(final IgniteCache<Object, Object> cache) throws Exception {
    Map<Object, EntryProcessorResult<Object>> results = cache.invokeAll(Collections.singleton("1"), new UpdateProcessor());

    final EntryProcessorResult<Object> epRes = F.first(results.values());

    assertNotNull(epRes);

    // In transactions EP will be invoked locally.
    Class<? extends Exception> exp = grid("client").transactions().tx() == null || ((IgniteCacheProxy)cache).context().mvccEnabled()
        ? EntryProcessorException.class
        : NonSerializableException.class;

    //noinspection ThrowableNotThrown
    assertThrows(log, new Callable<Object>() {
        @Override public Object call() throws Exception {
            epRes.get();

            return null;
        }
    }, exp, null);

    if (ATOMIC_CACHE.equals(cache.getName())) {
        //noinspection ThrowableNotThrown
        assertThrows(log, new Callable<Object>() {
            @Override public Object call() throws Exception {
                cache.invokeAll(Collections.singleton("1"), new UpdateValueProcessor());

                return null;
            }
        }, CachePartialUpdateException.class, null);
    }
}
 
Example 9
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Tests invokeAll method for map of pairs (key, entryProcessor).
 *
 * @param cache Cache.
 * @param required Expected injected resources.
 * @param async Use async API.
 * @param oldAsync Use old async API.
 */
private void checkResourceInjectionOnInvokeAllMap(IgniteCache<String, Integer> cache,
    Collection<ResourceType> required, boolean async, boolean oldAsync) {
    Map<String, EntryProcessorResult<Integer>> results;

    Map<String, EntryProcessor<String, Integer, Integer>> map = new HashMap<>();

    map.put(UUID.randomUUID().toString(), new ResourceInjectionEntryProcessor());
    map.put(UUID.randomUUID().toString(), new ResourceInjectionEntryProcessor());
    map.put(UUID.randomUUID().toString(), new ResourceInjectionEntryProcessor());
    map.put(UUID.randomUUID().toString(), new ResourceInjectionEntryProcessor());

    if (async) {
        if (oldAsync) {
            IgniteCache<String, Integer> acache = cache.withAsync();

            acache.invokeAll(map);

            results = acache.<Map<String, EntryProcessorResult<Integer>>>future().get();
        }
        else
            results = cache.invokeAllAsync(map).get();
    }
    else
        results = cache.invokeAll(map);

    assertEquals(map.size(), results.size());

    for (EntryProcessorResult<Integer> res : results.values()) {
        Collection<ResourceType> notInjected = ResourceInfoSet.valueOf(res.get()).notInjected(required);

        if (!notInjected.isEmpty())
            fail("Can't inject resource(s): " + Arrays.toString(notInjected.toArray()));
    }
}
 
Example 10
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Tests invokeAll method for set of keys.
 *
 * @param cache Cache.
 * @param required Expected injected resources.
 * @param async Use async API.
 * @param oldAsync Use old async API.
 */
private void checkResourceInjectionOnInvokeAll(IgniteCache<String, Integer> cache,
    Collection<ResourceType> required, boolean async, boolean oldAsync) {
    Set<String> keys = new HashSet<>(Arrays.asList(UUID.randomUUID().toString(),
        UUID.randomUUID().toString(),
        UUID.randomUUID().toString(),
        UUID.randomUUID().toString()));

    Map<String, EntryProcessorResult<Integer>> results;

    if (async) {
        if (oldAsync) {
            IgniteCache<String, Integer> acache = cache.withAsync();

            acache.invokeAll(keys, new ResourceInjectionEntryProcessor());

            results = acache.<Map<String, EntryProcessorResult<Integer>>>future().get();
        }
        else
            results = cache.invokeAllAsync(keys, new ResourceInjectionEntryProcessor()).get();
    }
    else
        results = cache.invokeAll(keys, new ResourceInjectionEntryProcessor());

    assertEquals(keys.size(), results.size());

    for (EntryProcessorResult<Integer> res : results.values()) {
        Collection<ResourceType> notInjected1 = ResourceInfoSet.valueOf(res.get()).notInjected(required);

        if (!notInjected1.isEmpty())
            fail("Can't inject resource(s): " + Arrays.toString(notInjected1.toArray()));
    }
}
 
Example 11
Source File: CacheEntryProcessorExample.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Increments values of entries stored in the cache using {@link IgniteCache#invokeAll(Set, EntryProcessor,
 * Object...)} method.
 *
 * @param cache Cache instance.
 */
private static void incrementEntriesWithInvokeAll(IgniteCache<Integer, Integer> cache) {
    System.out.println("");
    System.out.println(">> Incrementing values in the cache using EntryProcessor.");

    // Using EntryProcessor.invokeAll to increment every value in place.
    cache.invokeAll(KEYS_SET, (entry, object) -> {
        entry.setValue(entry.getValue() + 5);

        return null;
    });

    // Print outs entries that are incremented using the EntryProcessor above.
    printCacheEntries(cache);
}
 
Example 12
Source File: GridCacheHashMapPutAllWarningsTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testHashMapInvokeAllLocal() throws Exception {
    List<String> messages = Collections.synchronizedList(new ArrayList<>());

    testLog = new ListeningTestLogger(false, log());

    testLog.registerListener((s) -> {
        if (s.contains("deadlock"))
            messages.add(s);
    });

    Ignite ignite = startGrid(0);

    IgniteCache<Integer, String> c = ignite.getOrCreateCache(new CacheConfiguration<Integer, String>("invoke")
        .setCacheMode(CacheMode.LOCAL));

    c.put(1, "foo");
    c.put(2, "bar");

    Map<Integer, EntryProcessorResult<String>> result = c.invokeAll(new HashSet<>(Arrays.asList(1, 2)),
        new EntryProcessor<Integer, String, String>() {
            @Override public String process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
                String newVal = entry.getValue() + "2";

                entry.setValue(newVal);

                return newVal;
            }
        });

    assertEquals(2, result.size());
    assertEquals("bar2", c.get(2));

    int found = 0;

    for (String message : messages) {
        if (message.contains("Unordered collection java.util.HashSet is used for invokeAll operation on cache invoke. "))
            found++;
    }

    assertEquals(1, found);
}
 
Example 13
Source File: GridCacheReturnValueTransferSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param mode Atomicity mode.
 * @param b Number of backups.
 * @throws Exception If failed.
 */
private void checkTransform(CacheAtomicityMode mode, int b)
    throws Exception {
    try {
        atomicityMode = mode;

        backups = b;

        startGrids(2);
        startClientGrid(2);

        failDeserialization = false;

        // Get client grid.
        IgniteCache<Integer, TestObject> cache = grid(2).cache(DEFAULT_CACHE_NAME);

        for (int i = 0; i < 100; i++)
            cache.put(i, new TestObject());

        failDeserialization = true;

        info(">>>>>> Transforming");

        // Transform (check non-existent keys also).
        for (int i = 0; i < 200; i++)
            cache.invoke(i, new Transform());

        Set<Integer> keys = new HashSet<>();

        // Check transformAll.
        for (int i = 0; i < 300; i++)
            keys.add(i);

        cache.invokeAll(keys, new Transform());

        // Avoid errors during stop.
        failDeserialization = false;
    }
    finally {
        stopAllGrids();
    }
}
 
Example 14
Source File: IgniteCacheAtomicProtocolTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param backups Number of backups.
 * @throws Exception If failed.
 */
private void cacheOperations(int backups) throws Exception {
    ccfg = cacheConfiguration(backups, FULL_SYNC);

    final int SRVS = 4;

    startServers(SRVS);

    Ignite clientNode = startClientGrid(SRVS);

    final IgniteCache<Integer, Integer> nearCache = clientNode.cache(TEST_CACHE);

    Integer key = primaryKey(ignite(0).cache(TEST_CACHE));

    nearCache.replace(key, 1);

    nearCache.remove(key);

    nearCache.invoke(key, new SetValueEntryProcessor(null));

    Map<Integer, SetValueEntryProcessor> map = new HashMap<>();

    List<Integer> keys = primaryKeys(ignite(0).cache(TEST_CACHE), 2);

    map.put(keys.get(0), new SetValueEntryProcessor(1));
    map.put(keys.get(1), new SetValueEntryProcessor(null));

    nearCache.invokeAll(map);

    Set<Integer> rmvAllKeys = new HashSet<>();

    for (int i = 0; i < 100; i++) {
        nearCache.put(i, i);

        if (i % 2 == 0)
            rmvAllKeys.add(i);
    }

    nearCache.removeAll(rmvAllKeys);
}
 
Example 15
Source File: IgniteCacheWriteBehindNoUpdateSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testEntryProcessorNoUpdate() throws Exception {
    IgniteCache<Object, Object> cache = ignite(0).cache(THROTTLES_CACHE_NAME);

    IgniteCache<Object, Object> skipStore = cache.withSkipStore();

    int entryCnt = 500;

    Set<String> keys = new HashSet<>();

    for (int i = 0; i < entryCnt; i++) {
        skipStore.put(String.valueOf(i), i);

        keys.add(String.valueOf(i));
    }

    TestCacheStore testStore = (TestCacheStore)grid(0).context().cache().cache(THROTTLES_CACHE_NAME).context()
        .store().configuredStore();

    assertEquals(0, testStore.writeCnt.get());

    cache.invokeAll(keys, new NoOpEntryProcessor());

    assertEquals(0, testStore.writeCnt.get());

    cache.invokeAll(keys, new OpEntryProcessor());

    assertEquals(1, testStore.writeCnt.get());
}