use of org.apache.ignite.lang.IgniteBiInClosure in project ignite by apache.
the class GridCacheIoManager method addHandler.
/**
* Adds message handler.
*
* @param cacheId Cache ID.
* @param type Type of message.
* @param c Handler.
*/
@SuppressWarnings({ "unchecked" })
public void addHandler(int cacheId, Class<? extends GridCacheMessage> type, IgniteBiInClosure<UUID, ? extends GridCacheMessage> c) {
int msgIdx = messageIndex(type);
if (msgIdx != -1) {
Map<Integer, IgniteBiInClosure[]> idxClsHandlers0 = idxClsHandlers;
IgniteBiInClosure[] cacheClsHandlers = idxClsHandlers0.get(cacheId);
if (cacheClsHandlers == null) {
cacheClsHandlers = new IgniteBiInClosure[GridCacheMessage.MAX_CACHE_MSG_LOOKUP_INDEX];
idxClsHandlers0.put(cacheId, cacheClsHandlers);
}
if (cacheClsHandlers[msgIdx] != null)
throw new IgniteException("Duplicate cache message ID found [cacheId=" + cacheId + ", type=" + type + ']');
cacheClsHandlers[msgIdx] = c;
idxClsHandlers = idxClsHandlers0;
return;
} else {
ListenerKey key = new ListenerKey(cacheId, type);
if (clsHandlers.putIfAbsent(key, (IgniteBiInClosure<UUID, GridCacheMessage>) c) != null)
assert false : "Handler for class already registered [cacheId=" + cacheId + ", cls=" + type + ", old=" + clsHandlers.get(key) + ", new=" + c + ']';
}
IgniteLogger log0 = log;
if (log0 != null && log0.isTraceEnabled())
log0.trace("Registered cache communication handler [cacheId=" + cacheId + ", type=" + type + ", msgIdx=" + msgIdx + ", handler=" + c + ']');
}
use of org.apache.ignite.lang.IgniteBiInClosure in project ignite by apache.
the class CacheAffinitySharedManager method initCoordinatorCaches.
/**
* @param fut Exchange future.
* @throws IgniteCheckedException If failed.
* @return Future completed when caches initialization is done.
*/
private IgniteInternalFuture<?> initCoordinatorCaches(final GridDhtPartitionsExchangeFuture fut) throws IgniteCheckedException {
final List<IgniteInternalFuture<AffinityTopologyVersion>> futs = new ArrayList<>();
forAllRegisteredCaches(new IgniteInClosureX<DynamicCacheDescriptor>() {
@Override
public void applyx(DynamicCacheDescriptor desc) throws IgniteCheckedException {
CacheHolder cache = caches.get(desc.cacheId());
if (cache != null) {
if (cache.client())
cache.affinity().calculate(fut.topologyVersion(), fut.discoveryEvent(), fut.discoCache());
return;
}
final Integer cacheId = desc.cacheId();
GridCacheContext cacheCtx = cctx.cacheContext(cacheId);
if (cacheCtx == null) {
cctx.io().addHandler(desc.cacheId(), GridDhtAffinityAssignmentResponse.class, new IgniteBiInClosure<UUID, GridDhtAffinityAssignmentResponse>() {
@Override
public void apply(UUID nodeId, GridDhtAffinityAssignmentResponse res) {
processAffinityAssignmentResponse(nodeId, res);
}
});
cache = CacheHolder2.create(cctx, desc, fut, null);
final GridAffinityAssignmentCache aff = cache.affinity();
List<GridDhtPartitionsExchangeFuture> exchFuts = cctx.exchange().exchangeFutures();
int idx = exchFuts.indexOf(fut);
assert idx >= 0 && idx < exchFuts.size() - 1 : "Invalid exchange futures state [cur=" + idx + ", total=" + exchFuts.size() + ']';
final GridDhtPartitionsExchangeFuture prev = exchFuts.get(idx + 1);
if (log.isDebugEnabled()) {
log.debug("Need initialize affinity on coordinator [" + "cache=" + desc.cacheConfiguration().getName() + "prevAff=" + prev.topologyVersion() + ']');
}
assert prev.topologyVersion().compareTo(fut.topologyVersion()) < 0 : prev;
GridDhtAssignmentFetchFuture fetchFut = new GridDhtAssignmentFetchFuture(cctx, desc, prev.topologyVersion(), prev.discoCache());
fetchFut.init();
final GridFutureAdapter<AffinityTopologyVersion> affFut = new GridFutureAdapter<>();
fetchFut.listen(new IgniteInClosureX<IgniteInternalFuture<GridDhtAffinityAssignmentResponse>>() {
@Override
public void applyx(IgniteInternalFuture<GridDhtAffinityAssignmentResponse> fetchFut) throws IgniteCheckedException {
fetchAffinity(prev, aff, (GridDhtAssignmentFetchFuture) fetchFut);
aff.calculate(fut.topologyVersion(), fut.discoveryEvent(), fut.discoCache());
affFut.onDone(fut.topologyVersion());
}
});
futs.add(affFut);
} else
cache = new CacheHolder1(cacheCtx, null);
CacheHolder old = caches.put(cache.cacheId(), cache);
assert old == null : old;
}
});
if (!futs.isEmpty()) {
GridCompoundFuture<AffinityTopologyVersion, ?> affFut = new GridCompoundFuture<>();
for (IgniteInternalFuture<AffinityTopologyVersion> f : futs) affFut.add(f);
affFut.markInitialized();
return affFut;
}
return null;
}
use of org.apache.ignite.lang.IgniteBiInClosure in project ignite by apache.
the class IgniteCacheStoreValueAbstractTest method cacheConfiguration.
/** {@inheritDoc} */
@Override
protected CacheConfiguration cacheConfiguration(String igniteInstanceName) throws Exception {
CacheConfiguration ccfg = super.cacheConfiguration(igniteInstanceName);
if (ccfg.getCacheMode() != CacheMode.LOCAL)
assertEquals(1, ccfg.getBackups());
assertTrue(ccfg.isCopyOnRead());
ccfg.setCopyOnRead(cpyOnRead);
assertEquals(FULL_SYNC, ccfg.getWriteSynchronizationMode());
ccfg.setCacheStoreFactory(singletonFactory(new CacheStoreAdapter() {
@Override
public void loadCache(IgniteBiInClosure clo, Object... args) {
clo.apply(new TestKey(100_000), new TestValue(30_000));
}
@Override
public Object load(Object key) throws CacheLoaderException {
return null;
}
@Override
public void write(Cache.Entry entry) throws CacheWriterException {
// No-op.
}
@Override
public void delete(Object key) throws CacheWriterException {
// No-op.
}
}));
ccfg.setInterceptor(new TestInterceptor());
return ccfg;
}
use of org.apache.ignite.lang.IgniteBiInClosure 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;
Transaction tx = null;
try {
if (cache0.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL)
tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
assertEquals(val, val0);
cache0.put(key, newVal);
if (tx != null)
tx.commit();
latch.countDown();
} catch (Exception exp) {
log.error("Failed: ", exp);
throw new IgniteException(exp);
} finally {
if (tx != null)
tx.close();
}
}
};
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 org.apache.ignite.lang.IgniteBiInClosure 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;
Transaction tx = null;
try {
if (cache0.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL)
tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
assertEquals(val, val0);
cache0.put(key, newVal);
if (tx != null)
tx.commit();
latch.countDown();
} catch (Exception exp) {
log.error("Failed: ", exp);
throw new IgniteException(exp);
} finally {
if (tx != null)
tx.close();
}
}
};
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());
}
}
Aggregations