use of javax.cache.processor.MutableEntry in project ignite by apache.
the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInFilter.
/**
* @param ccfg Cache configuration.
* @param asyncFilter Async filter.
* @param asyncLsnr Async listener.
* @param jcacheApi Use JCache api for start update listener.
* @throws Exception If failed.
*/
private void testNonDeadLockInFilter(CacheConfiguration ccfg, final boolean asyncFilter, final boolean asyncLsnr, boolean jcacheApi) throws Exception {
ignite(0).createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
try {
for (int i = 0; i < ITERATION_CNT; i++) {
log.info("Start iteration: " + i);
int nodeIdx = i % NODES;
final String cacheName = ccfg.getName();
final IgniteCache cache = grid(nodeIdx).cache(cacheName);
final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
final QueryTestValue val0 = new QueryTestValue(1);
final QueryTestValue newVal = new QueryTestValue(2);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
if (asyncFilter) {
assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
}
IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
QueryTestValue val = e.getValue();
if (val == null)
return;
else if (val.equals(newVal)) {
evtFromLsnrLatch.countDown();
return;
} else if (!val.equals(val0))
return;
try {
assertEquals(val, val0);
if (atomicityMode(cache0) != ATOMIC) {
boolean committed = false;
while (!committed && !Thread.currentThread().isInterrupted()) {
try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache0.put(key, newVal);
tx.commit();
committed = true;
} catch (Exception ex) {
assertTrue(ex.toString(), X.hasCause(ex, TransactionSerializationException.class));
assertEquals(atomicityMode(cache0), TRANSACTIONAL_SNAPSHOT);
}
}
} else
cache0.put(key, newVal);
latch.countDown();
} catch (Exception exp) {
log.error("Failed: ", exp);
throw new IgniteException(exp);
}
}
};
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
if (asyncLsnr) {
assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
}
QueryTestValue val = e.getValue();
if (val == null || !val.equals(new QueryTestValue(1)))
return;
assertEquals(val, val0);
latch.countDown();
}
};
QueryCursor qry = null;
MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFilter ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
if (jcacheApi) {
lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
cache.registerCacheEntryListener(lsnrCfg);
} else {
ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
conQry.setLocalListener(locLsnr);
conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
qry = cache.query(conQry);
}
try {
if (rnd.nextBoolean())
cache.put(key, val0);
else
cache.invoke(key, new CacheEntryProcessor() {
@Override
public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
entry.setValue(val0);
return null;
}
});
assert U.await(latch, 3, SECONDS) : "Failed to waiting event.";
assertEquals(cache.get(key), new QueryTestValue(2));
assertTrue("Failed to waiting event from filter.", U.await(latch, 3, SECONDS));
} finally {
if (qry != null)
qry.close();
if (lsnrCfg != null)
cache.deregisterCacheEntryListener(lsnrCfg);
}
log.info("Iteration finished: " + i);
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of javax.cache.processor.MutableEntry in project ignite by apache.
the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInListener.
/**
* @param ccfg Cache configuration.
* @param asyncFltr Async filter.
* @param asyncLsnr Async listener.
* @param jcacheApi Use JCache api for registration entry update listener.
* @throws Exception If failed.
*/
private void testNonDeadLockInListener(CacheConfiguration ccfg, final boolean asyncFltr, boolean asyncLsnr, boolean jcacheApi) throws Exception {
ignite(0).createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
try {
for (int i = 0; i < ITERATION_CNT; i++) {
log.info("Start iteration: " + i);
int nodeIdx = i % NODES;
final String cacheName = ccfg.getName();
final IgniteCache cache = grid(nodeIdx).cache(cacheName);
final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
final QueryTestValue val0 = new QueryTestValue(1);
final QueryTestValue newVal = new QueryTestValue(2);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
if (asyncFltr) {
assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
}
}
};
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
QueryTestValue val = e.getValue();
if (val == null)
return;
else if (val.equals(newVal)) {
evtFromLsnrLatch.countDown();
return;
} else if (!val.equals(val0))
return;
// several ms to wait - mvcc coordinator need some time to register tx as finished.
if (ccfg.getAtomicityMode() == TRANSACTIONAL_SNAPSHOT) {
Object v = null;
while (v == null && !Thread.currentThread().isInterrupted()) {
v = cache0.get(key);
if (v == null)
doSleep(50);
}
}
try {
assertEquals(val, val0);
if (atomicityMode(cache0) != ATOMIC) {
boolean committed = false;
while (!committed && !Thread.currentThread().isInterrupted()) {
try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache0.put(key, newVal);
tx.commit();
committed = true;
} catch (Exception ex) {
assertTrue(ex.getCause() instanceof TransactionSerializationException);
assertEquals(atomicityMode(cache0), TRANSACTIONAL_SNAPSHOT);
}
}
} else
cache0.put(key, newVal);
latch.countDown();
} catch (Exception exp) {
log.error("Failed: ", exp);
throw new IgniteException(exp);
}
}
};
QueryCursor qry = null;
MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFltr ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
if (jcacheApi) {
lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
cache.registerCacheEntryListener(lsnrCfg);
} else {
ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
conQry.setLocalListener(locLsnr);
conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
qry = cache.query(conQry);
}
try {
if (rnd.nextBoolean())
cache.put(key, val0);
else {
cache.invoke(key, new CacheEntryProcessor() {
@Override
public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
entry.setValue(val0);
return null;
}
});
}
assertTrue("Failed to waiting event.", U.await(latch, 3, SECONDS));
assertEquals(cache.get(key), new QueryTestValue(2));
assertTrue("Failed to waiting event from listener.", U.await(latch, 3, SECONDS));
} finally {
if (qry != null)
qry.close();
if (lsnrCfg != null)
cache.deregisterCacheEntryListener(lsnrCfg);
}
log.info("Iteration finished: " + i);
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of javax.cache.processor.MutableEntry 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 javax.cache.processor.MutableEntry in project ignite by apache.
the class GridCacheHashMapPutAllWarningsTest method testHashMapInvokeAllLocal.
/**
* @throws Exception If failed.
*/
@Test
public void testHashMapInvokeAllLocal() throws Exception {
Assume.assumeFalse("Local transactional caches not supported by MVCC", IgniteSystemProperties.getBoolean(IgniteSystemProperties.IGNITE_FORCE_MVCC_MODE_IN_TESTS, false));
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).setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
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);
}
use of javax.cache.processor.MutableEntry 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);
}
Aggregations