Search in sources :

Example 16 with CacheEvent

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();
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) CountDownLatch(java.util.concurrent.CountDownLatch) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 17 with CacheEvent

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]);
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) CountDownLatch(java.util.concurrent.CountDownLatch) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 18 with CacheEvent

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));
}
Also used : CacheEvent(org.apache.ignite.events.CacheEvent) CacheEvent(org.apache.ignite.events.CacheEvent) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 19 with CacheEvent

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()));
    }
}
Also used : CacheEvent(org.apache.ignite.events.CacheEvent) CacheEvent(org.apache.ignite.events.CacheEvent) Event(org.apache.ignite.events.Event)

Example 20 with CacheEvent

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);
        }
    }
}
Also used : CacheEvent(org.apache.ignite.events.CacheEvent) UUID(java.util.UUID)

Aggregations

CacheEvent (org.apache.ignite.events.CacheEvent)22 CountDownLatch (java.util.concurrent.CountDownLatch)11 UUID (java.util.UUID)10 Ignite (org.apache.ignite.Ignite)8 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)8 Event (org.apache.ignite.events.Event)5 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)5 HashMap (java.util.HashMap)3 IgniteException (org.apache.ignite.IgniteException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)2 CacheRebalancingEvent (org.apache.ignite.events.CacheRebalancingEvent)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 TimeoutException (java.util.concurrent.TimeoutException)1