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);
}
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);
}
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);
}
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();
}
}
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();
}
}
Aggregations