use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class IgniteDynamicCacheStartSelfTest method testServerNodesLeftEvent.
/**
* @throws Exception If failed.
*/
public void testServerNodesLeftEvent() throws Exception {
testAttribute = false;
startGrid(nodeCount());
CacheConfiguration cfg = new CacheConfiguration(DYNAMIC_CACHE_NAME);
cfg.setNodeFilter(F.not(NODE_FILTER));
IgniteCache<Object, Object> cache = ignite(0).createCache(cfg);
final CountDownLatch[] latches = new CountDownLatch[nodeCount()];
IgnitePredicate[] lsnrs = new IgnitePredicate[nodeCount()];
for (int i = 0; i < nodeCount(); i++) {
final int idx = i;
latches[i] = new CountDownLatch(1);
lsnrs[i] = new IgnitePredicate<CacheEvent>() {
@Override
public boolean apply(CacheEvent e) {
switch(e.type()) {
case EventType.EVT_CACHE_NODES_LEFT:
latches[idx].countDown();
break;
default:
assert false;
}
assertEquals(DYNAMIC_CACHE_NAME, e.cacheName());
return true;
}
};
ignite(i).events().localListen(lsnrs[i], EventType.EVTS_CACHE_LIFECYCLE);
}
stopGrid(nodeCount());
for (CountDownLatch latch : latches) latch.await();
for (int i = 0; i < nodeCount(); i++) ignite(i).events().stopLocalListen(lsnrs[i]);
cache.destroy();
}
use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class IgniteDynamicCacheStartSelfTest method testEvents.
/**
* @throws Exception If failed.
*/
public void testEvents() throws Exception {
CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
cfg.setName(DYNAMIC_CACHE_NAME);
cfg.setCacheMode(CacheMode.REPLICATED);
final CountDownLatch[] starts = new CountDownLatch[nodeCount()];
final CountDownLatch[] stops = new CountDownLatch[nodeCount()];
IgnitePredicate[] lsnrs = new IgnitePredicate[nodeCount()];
for (int i = 0; i < nodeCount(); i++) {
final int idx = i;
starts[i] = new CountDownLatch(1);
stops[i] = new CountDownLatch(1);
lsnrs[i] = new IgnitePredicate<CacheEvent>() {
@Override
public boolean apply(CacheEvent e) {
switch(e.type()) {
case EventType.EVT_CACHE_STARTED:
starts[idx].countDown();
break;
case EventType.EVT_CACHE_STOPPED:
stops[idx].countDown();
break;
default:
assert false;
}
assertEquals(DYNAMIC_CACHE_NAME, e.cacheName());
return true;
}
};
ignite(i).events().localListen(lsnrs[i], EventType.EVTS_CACHE_LIFECYCLE);
}
IgniteCache<Object, Object> cache = ignite(0).createCache(cfg);
try {
for (CountDownLatch start : starts) start.await();
} finally {
cache.destroy();
}
for (CountDownLatch stop : stops) stop.await();
for (int i = 0; i < nodeCount(); i++) ignite(i).events().stopLocalListen(lsnrs[i]);
}
use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class GridCacheEvictionEventAbstractTest method testEvictionEvent.
/**
* @throws Exception If failed.
*/
public void testEvictionEvent() throws Exception {
Ignite g = grid();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> oldVal = new AtomicReference<>();
g.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
CacheEvent e = (CacheEvent) evt;
oldVal.set((String) e.oldValue());
latch.countDown();
return true;
}
}, EventType.EVT_CACHE_ENTRY_EVICTED);
IgniteCache<String, String> c = g.cache(DEFAULT_CACHE_NAME);
c.put("1", "val1");
c.localEvict(Collections.singleton("1"));
assertTrue("Failed to wait for eviction event", latch.await(10, TimeUnit.SECONDS));
}
use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class GridCachePreloadEventsAbstractSelfTest method checkPreloadEvents.
/**
* @param evts Events.
* @param g Grid.
* @param keys Keys.
*/
protected void checkPreloadEvents(Collection<Event> evts, Ignite g, Collection<? extends Object> keys) {
assertEquals(keys.size(), evts.size());
for (Event evt : evts) {
CacheEvent cacheEvt = (CacheEvent) evt;
assertEquals(EVT_CACHE_REBALANCE_OBJECT_LOADED, cacheEvt.type());
assertEquals(g.cache(DEFAULT_CACHE_NAME).getName(), cacheEvt.cacheName());
assertEquals(g.cluster().localNode().id(), cacheEvt.node().id());
assertEquals(g.cluster().localNode().id(), cacheEvt.eventNode().id());
assertTrue(cacheEvt.hasNewValue());
assertNotNull(cacheEvt.newValue());
assertTrue("Unexpected key: " + cacheEvt.key(), keys.contains(cacheEvt.key()));
}
}
use of org.apache.ignite.events.CacheEvent in project ignite by apache.
the class GridCacheTransformEventSelfTest method checkEventNodeIdsStrict.
/**
* Ensure that events were recorded on the given nodes.
*
* @param cClsName Entry processor class name.
* @param ids Event IDs.
*/
private void checkEventNodeIdsStrict(String cClsName, UUID... ids) {
if (ids == null)
assertTrue(evts.isEmpty());
else {
assertEquals(ids.length, evts.size());
for (UUID id : ids) {
CacheEvent foundEvt = null;
for (CacheEvent evt : evts) {
if (F.eq(id, evt.node().id())) {
assertEquals(cClsName, evt.closureClassName());
foundEvt = evt;
break;
}
}
if (foundEvt == null) {
int gridIdx = -1;
for (int i = 0; i < GRID_CNT; i++) {
if (F.eq(this.ids[i], id)) {
gridIdx = i;
break;
}
}
fail("Expected transform event was not triggered on the node [nodeId=" + id + ", key1Primary=" + primary(gridIdx, key1) + ", key1Backup=" + backup(gridIdx, key1) + ", key2Primary=" + primary(gridIdx, key2) + ", key2Backup=" + backup(gridIdx, key2) + ']');
} else
evts.remove(foundEvt);
}
}
}
Aggregations