Search in sources :

Example 1 with CacheEntryProcessor

use of org.apache.ignite.cache.CacheEntryProcessor in project ignite by apache.

the class IgniteCacheGroupsTest method cacheInvokeAllAsync.

/**
 * @param cache Cache.
 */
private void cacheInvokeAllAsync(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();
    Object res0 = cache.invokeAllAsync(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).get(ASYNC_TIMEOUT);
    Map<Integer, CacheInvokeResult<Integer>> res = (Map<Integer, CacheInvokeResult<Integer>>) res0;
    assertEquals(keys, res.size());
    assertEquals(one + two, (Object) res.get(0).get());
    tearDown(cache);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) BinaryObject(org.apache.ignite.binary.BinaryObject) MutableEntry(javax.cache.processor.MutableEntry) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) IntMap(org.apache.ignite.internal.util.collection.IntMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with CacheEntryProcessor

use of org.apache.ignite.cache.CacheEntryProcessor in project ignite by apache.

the class IgniteCacheGroupsTest method cacheInvokeAsync.

/**
 * @param cache Cache.
 */
private void cacheInvokeAsync(IgniteCache cache) {
    Random rnd = ThreadLocalRandom.current();
    Integer key = rnd.nextInt();
    Integer val = rnd.nextInt();
    cache.put(key, val);
    int one = rnd.nextInt();
    int two = rnd.nextInt();
    Object res = cache.invokeAsync(key, new CacheEntryProcessor<Integer, Integer, Integer>() {

        @Override
        public Integer process(MutableEntry<Integer, Integer> entry, Object... arguments) throws EntryProcessorException {
            assertEquals(arguments[0], entry.getValue());
            // Some calculation.
            return (Integer) arguments[1] + (Integer) arguments[2];
        }
    }, val, one, two).get(ASYNC_TIMEOUT);
    assertEquals(one + two, res);
    tearDown(cache);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) BinaryObject(org.apache.ignite.binary.BinaryObject) MutableEntry(javax.cache.processor.MutableEntry)

Example 3 with CacheEntryProcessor

use of org.apache.ignite.cache.CacheEntryProcessor in project ignite by apache.

the class BinaryMetadataRegistrationInsideEntryProcessorTest method testContinuousQueryAndBinaryObjectBuilder.

/**
 * Continuously execute multiple EntryProcessors with having continuous queries in parallel.
 * This used to lead to several deadlocks.
 *
 * @throws Exception If failed.
 */
@Test
public void testContinuousQueryAndBinaryObjectBuilder() throws Exception {
    startGrids(3).cluster().active(true);
    grid(0).createCache(new CacheConfiguration<>().setName(CACHE_NAME).setAtomicityMode(ATOMIC).setBackups(2).setCacheMode(PARTITIONED).setWriteSynchronizationMode(FULL_SYNC).setPartitionLossPolicy(READ_WRITE_SAFE));
    IgniteEx client1 = startClientGrid(getConfiguration().setIgniteInstanceName("client1"));
    IgniteEx client2 = startClientGrid(getConfiguration().setIgniteInstanceName("client2"));
    AtomicBoolean stop = new AtomicBoolean();
    AtomicInteger keyCntr = new AtomicInteger();
    AtomicInteger binaryTypeCntr = new AtomicInteger();
    /**
     */
    class MyEntryProcessor implements CacheEntryProcessor<Object, Object, Object> {

        /**
         * Cached int value retrieved from {@code binaryTypeCntr} variable.
         */
        private int i;

        /**
         */
        public MyEntryProcessor(int i) {
            this.i = i;
        }

        /**
         */
        @IgniteInstanceResource
        Ignite ignite;

        /**
         * {@inheritDoc}
         */
        @Override
        public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
            BinaryObjectBuilder builder = ignite.binary().builder("my_type");
            builder.setField("new_field" + i, i);
            entry.setValue(builder.build());
            return null;
        }
    }
    IgniteInternalFuture fut1 = GridTestUtils.runMultiThreadedAsync(() -> {
        IgniteCache<Object, Object> cache = client1.cache(CACHE_NAME).withKeepBinary();
        while (!stop.get()) {
            Integer key = keyCntr.getAndIncrement();
            cache.put(key, key);
            cache.invoke(key, new MyEntryProcessor(binaryTypeCntr.get()));
            binaryTypeCntr.incrementAndGet();
        }
    }, 8, "writer-thread");
    IgniteInternalFuture fut2 = GridTestUtils.runAsync(() -> {
        IgniteCache<Object, Object> cache = client2.cache(CACHE_NAME).withKeepBinary();
        while (!stop.get()) {
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>((key, val) -> true));
            qry.setLocalListener(evts -> {
            });
            // noinspection EmptyTryBlock
            try (QueryCursor<Cache.Entry<Object, Object>> cursor = cache.query(qry)) {
            // No-op.
            }
        }
    });
    doSleep(10_000);
    stop.set(true);
    fut1.get(10, TimeUnit.SECONDS);
    fut2.get(10, TimeUnit.SECONDS);
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) READ_WRITE_SAFE(org.apache.ignite.cache.PartitionLossPolicy.READ_WRITE_SAFE) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) IgniteEx(org.apache.ignite.internal.IgniteEx) EntryProcessorException(javax.cache.processor.EntryProcessorException) EntryProcessor(javax.cache.processor.EntryProcessor) MutableEntry(javax.cache.processor.MutableEntry) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) After(org.junit.After) Cache(javax.cache.Cache) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) TcpDiscoveryVmIpFinder(org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) ATOMIC(org.apache.ignite.cache.CacheAtomicityMode.ATOMIC) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MutableEntry(javax.cache.processor.MutableEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 4 with CacheEntryProcessor

use of org.apache.ignite.cache.CacheEntryProcessor in project ignite by apache.

the class GridCacheAtomicEntryProcessorDeploymentSelfTest method doTestInvokeAll.

/**
 * @throws Exception In case of error.
 */
private void doTestInvokeAll() throws Exception {
    try {
        startGrid(0);
        startClientGrid(1);
        Class procCls = grid(1).configuration().getClassLoader().loadClass(getEntryProcessor());
        Class valCls = grid(1).configuration().getClassLoader().loadClass(TEST_VALUE);
        assertTrue(grid(1).configuration().isClientMode());
        assertFalse(grid(0).configuration().isClientMode());
        IgniteCache cache = getCache();
        TreeSet keys = new TreeSet();
        for (int i = 0; i < 3; i++) {
            String key = "key" + i;
            cache.put(key, valCls.newInstance());
            keys.add(key);
        }
        Map<String, EntryProcessorResult> res = (Map<String, EntryProcessorResult>) cache.invokeAll(keys, (CacheEntryProcessor) procCls.newInstance());
        assertEquals(3, res.size());
        for (EntryProcessorResult result : res.values()) assertTrue((Boolean) result.get());
        // Checks that get produces no exceptions.
        for (int i = 0; i < 3; i++) {
            String key = "key" + i;
            cache.get(key);
        }
    } finally {
        stopAllGrids();
    }
}
Also used : EntryProcessorResult(javax.cache.processor.EntryProcessorResult) TreeSet(java.util.TreeSet) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) IgniteCache(org.apache.ignite.IgniteCache) Map(java.util.Map)

Example 5 with CacheEntryProcessor

use of org.apache.ignite.cache.CacheEntryProcessor in project ignite by apache.

the class GridCacheAtomicEntryProcessorDeploymentSelfTest method doTestInvoke.

/**
 * @throws Exception In case of error.
 */
private void doTestInvoke() throws Exception {
    try {
        startGrid(0);
        startClientGrid(1);
        Class procCls = grid(1).configuration().getClassLoader().loadClass(getEntryProcessor());
        Class valCls = grid(1).configuration().getClassLoader().loadClass(TEST_VALUE);
        assertTrue(grid(1).configuration().isClientMode());
        assertFalse(grid(0).configuration().isClientMode());
        IgniteCache cache = getCache();
        cache.put("key", valCls.newInstance());
        Boolean res = (Boolean) cache.invoke("key", (CacheEntryProcessor) procCls.newInstance());
        assertTrue(res);
        // Checks that get produces no exceptions.
        cache.get("key");
    } finally {
        stopAllGrids();
    }
}
Also used : CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

CacheEntryProcessor (org.apache.ignite.cache.CacheEntryProcessor)13 MutableEntry (javax.cache.processor.MutableEntry)9 IgniteCache (org.apache.ignite.IgniteCache)8 Ignite (org.apache.ignite.Ignite)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 EntryProcessorException (javax.cache.processor.EntryProcessorException)4 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)4 QueryCursor (org.apache.ignite.cache.query.QueryCursor)4 Map (java.util.Map)3 CacheEntryEvent (javax.cache.event.CacheEntryEvent)3 Transaction (org.apache.ignite.transactions.Transaction)3 TransactionSerializationException (org.apache.ignite.transactions.TransactionSerializationException)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Random (java.util.Random)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CacheException (javax.cache.CacheException)2 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)2 IgniteException (org.apache.ignite.IgniteException)2