Search in sources :

Example 31 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class WeightedRandomLoadBalancingSpi method onContextInitialized0.

/**
 * {@inheritDoc}
 */
@Override
protected void onContextInitialized0(IgniteSpiContext spiCtx) throws IgniteSpiException {
    getSpiContext().addLocalEventListener(evtLsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            assert evt instanceof TaskEvent || evt instanceof JobEvent;
            if (evt.type() == EVT_TASK_FINISHED || evt.type() == EVT_TASK_FAILED) {
                IgniteUuid sesId = ((TaskEvent) evt).taskSessionId();
                taskTops.remove(sesId);
                if (log.isDebugEnabled())
                    log.debug("Removed task topology from topology cache for session: " + sesId);
            } else // Here we set mapped property and later cache will be ignored
            if (evt.type() == EVT_JOB_MAPPED) {
                IgniteUuid sesId = ((JobEvent) evt).taskSessionId();
                IgniteBiTuple<Boolean, WeightedTopology> weightedTop = taskTops.get(sesId);
                if (weightedTop != null)
                    weightedTop.set1(true);
                if (log.isDebugEnabled())
                    log.debug("Job has been mapped. Ignore cache for session: " + sesId);
            }
        }
    }, EVT_TASK_FAILED, EVT_TASK_FINISHED, EVT_JOB_MAPPED);
}
Also used : JobEvent(org.apache.ignite.events.JobEvent) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IgniteUuid(org.apache.ignite.lang.IgniteUuid) TaskEvent(org.apache.ignite.events.TaskEvent) JobEvent(org.apache.ignite.events.JobEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event)

Example 32 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridEventStorageRuntimeConfigurationSelfTest method testEnableWithIncludes.

/**
 * @throws Exception If failed.
 */
@Test
public void testEnableWithIncludes() throws Exception {
    inclEvtTypes = new int[] { EVT_TASK_STARTED, EVT_TASK_FINISHED };
    try {
        Ignite g = startGrid();
        final AtomicInteger cnt = new AtomicInteger();
        g.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                cnt.incrementAndGet();
                return true;
            }
        }, EVT_TASK_STARTED, EVT_TASK_FINISHED, EVT_JOB_STARTED);
        g.compute().run(F.noop());
        assertEquals(2, cnt.get());
        g.events().enableLocal(EVT_TASK_FINISHED, EVT_JOB_STARTED);
        g.compute().run(F.noop());
        assertEquals(5, cnt.get());
    } finally {
        stopAllGrids();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 33 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridDiscoveryEventSelfTest method testMixedSequenceEvents.

/**
 * @throws Exception If failed.
 */
@Test
public void testMixedSequenceEvents() throws Exception {
    try {
        Ignite g0 = startGrid(0);
        UUID id0 = g0.cluster().localNode().id();
        final ConcurrentMap<Integer, Collection<ClusterNode>> evts = new ConcurrentHashMap<>();
        final CountDownLatch latch = new CountDownLatch(8);
        g0.events().localListen(new IgnitePredicate<Event>() {

            private AtomicInteger cnt = new AtomicInteger();

            @Override
            public boolean apply(Event evt) {
                assert evt.type() == EVT_NODE_JOINED || evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED : evt;
                evts.put(cnt.getAndIncrement(), ((DiscoveryEvent) evt).topologyNodes());
                latch.countDown();
                return true;
            }
        }, EVT_NODE_JOINED, EVT_NODE_LEFT, EVT_NODE_FAILED);
        UUID id1 = startGrid(1).cluster().localNode().id();
        UUID id2 = startGrid(2).cluster().localNode().id();
        UUID id3 = startGrid(3).cluster().localNode().id();
        stopGrid(3);
        stopGrid(2);
        stopGrid(1);
        UUID id4 = startGrid(4).cluster().localNode().id();
        stopGrid(4);
        assertTrue("Wrong count of events received [cnt= " + evts.size() + ", evts=" + evts + ']', latch.await(3000, MILLISECONDS));
        Collection<ClusterNode> top0 = evts.get(0);
        assertNotNull(top0);
        assertEquals(2, top0.size());
        assertTrue(F.viewReadOnly(top0, NODE_2ID).contains(id0));
        assertTrue(F.viewReadOnly(top0, NODE_2ID).contains(id1));
        Collection<ClusterNode> top1 = evts.get(1);
        assertNotNull(top1);
        assertEquals(3, top1.size());
        assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id0));
        assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id1));
        assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id2));
        Collection<ClusterNode> top2 = evts.get(2);
        assertNotNull(top2);
        assertEquals(4, top2.size());
        assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id0));
        assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id1));
        assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id2));
        assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id3));
        Collection<ClusterNode> top3 = evts.get(3);
        assertNotNull(top3);
        assertEquals(3, top3.size());
        assertTrue(F.viewReadOnly(top3, NODE_2ID).contains(id0));
        assertTrue(F.viewReadOnly(top3, NODE_2ID).contains(id1));
        assertTrue(F.viewReadOnly(top3, NODE_2ID).contains(id2));
        assertFalse(F.viewReadOnly(top3, NODE_2ID).contains(id3));
        Collection<ClusterNode> top4 = evts.get(4);
        assertNotNull(top4);
        assertEquals(2, top4.size());
        assertTrue(F.viewReadOnly(top4, NODE_2ID).contains(id0));
        assertTrue(F.viewReadOnly(top4, NODE_2ID).contains(id1));
        assertFalse(F.viewReadOnly(top4, NODE_2ID).contains(id2));
        assertFalse(F.viewReadOnly(top4, NODE_2ID).contains(id3));
        Collection<ClusterNode> top5 = evts.get(5);
        assertNotNull(top5);
        assertEquals(1, top5.size());
        assertTrue(F.viewReadOnly(top5, NODE_2ID).contains(id0));
        assertFalse(F.viewReadOnly(top5, NODE_2ID).contains(id1));
        assertFalse(F.viewReadOnly(top5, NODE_2ID).contains(id2));
        assertFalse(F.viewReadOnly(top5, NODE_2ID).contains(id3));
        Collection<ClusterNode> top6 = evts.get(6);
        assertNotNull(top6);
        assertEquals(2, top6.size());
        assertTrue(F.viewReadOnly(top6, NODE_2ID).contains(id0));
        assertTrue(F.viewReadOnly(top6, NODE_2ID).contains(id4));
        assertFalse(F.viewReadOnly(top6, NODE_2ID).contains(id1));
        assertFalse(F.viewReadOnly(top6, NODE_2ID).contains(id2));
        assertFalse(F.viewReadOnly(top6, NODE_2ID).contains(id3));
        Collection<ClusterNode> top7 = evts.get(7);
        assertNotNull(top7);
        assertEquals(1, top7.size());
        assertTrue(F.viewReadOnly(top7, NODE_2ID).contains(id0));
        assertFalse(F.viewReadOnly(top7, NODE_2ID).contains(id1));
        assertFalse(F.viewReadOnly(top7, NODE_2ID).contains(id2));
        assertFalse(F.viewReadOnly(top7, NODE_2ID).contains(id3));
        assertFalse(F.viewReadOnly(top7, NODE_2ID).contains(id4));
    } finally {
        stopAllGrids();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 34 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridDiscoveryEventSelfTest method testLeaveSequenceEvents.

/**
 * @throws Exception If failed.
 */
@Test
public void testLeaveSequenceEvents() throws Exception {
    try {
        Ignite g0 = startGrid(0);
        UUID id0 = g0.cluster().localNode().id();
        UUID id1 = startGrid(1).cluster().localNode().id();
        UUID id2 = startGrid(2).cluster().localNode().id();
        UUID id3 = startGrid(3).cluster().localNode().id();
        final ConcurrentMap<Integer, Collection<ClusterNode>> evts = new ConcurrentHashMap<>();
        final CountDownLatch latch = new CountDownLatch(3);
        g0.events().localListen(new IgnitePredicate<Event>() {

            private AtomicInteger cnt = new AtomicInteger();

            @Override
            public boolean apply(Event evt) {
                assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED : evt;
                evts.put(cnt.getAndIncrement(), ((DiscoveryEvent) evt).topologyNodes());
                latch.countDown();
                return true;
            }
        }, EVT_NODE_LEFT, EVT_NODE_FAILED);
        stopGrid(3);
        stopGrid(2);
        stopGrid(1);
        assertTrue("Wrong count of events received: " + evts, latch.await(3000, MILLISECONDS));
        Collection<ClusterNode> top2 = evts.get(0);
        assertNotNull(top2);
        assertEquals(3, top2.size());
        assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id0));
        assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id1));
        assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id2));
        assertFalse(F.viewReadOnly(top2, NODE_2ID).contains(id3));
        Collection<ClusterNode> top1 = evts.get(1);
        assertNotNull(top1);
        assertEquals(2, top1.size());
        assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id0));
        assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id1));
        assertFalse(F.viewReadOnly(top1, NODE_2ID).contains(id2));
        assertFalse(F.viewReadOnly(top1, NODE_2ID).contains(id3));
        Collection<ClusterNode> top0 = evts.get(2);
        assertNotNull(top0);
        assertEquals(1, top0.size());
        assertTrue(F.viewReadOnly(top0, NODE_2ID).contains(id0));
        assertFalse(F.viewReadOnly(top0, NODE_2ID).contains(id1));
        assertFalse(F.viewReadOnly(top0, NODE_2ID).contains(id2));
        assertFalse(F.viewReadOnly(top0, NODE_2ID).contains(id3));
    } finally {
        stopAllGrids();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 35 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridDiscoveryEventSelfTest method testConcurrentJoinEvents.

/**
 * @throws Exception If failed.
 */
@Test
public void testConcurrentJoinEvents() throws Exception {
    try {
        Ignite g0 = startGrid(0);
        UUID id0 = g0.cluster().localNode().id();
        final ConcurrentMap<Integer, Collection<ClusterNode>> evts = new ConcurrentHashMap<>();
        g0.events().localListen(new IgnitePredicate<Event>() {

            private AtomicInteger cnt = new AtomicInteger();

            @Override
            public boolean apply(Event evt) {
                assert evt.type() == EVT_NODE_JOINED : evt;
                X.println(">>>>>>> Joined " + F.viewReadOnly(((DiscoveryEvent) evt).topologyNodes(), NODE_2ID));
                evts.put(cnt.getAndIncrement(), ((DiscoveryEvent) evt).topologyNodes());
                return true;
            }
        }, EVT_NODE_JOINED);
        U.sleep(100);
        startGridsMultiThreaded(1, 10);
        U.sleep(100);
        assertEquals(10, evts.size());
        for (int i = 0; i < 10; i++) {
            Collection<ClusterNode> snapshot = evts.get(i);
            assertEquals(2 + i, snapshot.size());
            assertTrue(F.viewReadOnly(snapshot, NODE_2ID).contains(id0));
            for (ClusterNode n : snapshot) assertTrue("Wrong node order in snapshot [i=" + i + ", node=" + n + ']', n.order() <= 2 + i);
        }
        Collection<UUID> ids = F.viewReadOnly(evts.get(9), NODE_2ID);
        for (int i = 1; i <= 10; i++) assertTrue(ids.contains(grid(i).localNode().id()));
    } finally {
        stopAllGrids();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

Event (org.apache.ignite.events.Event)273 Test (org.junit.Test)148 CountDownLatch (java.util.concurrent.CountDownLatch)146 Ignite (org.apache.ignite.Ignite)144 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)125 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)103 UUID (java.util.UUID)87 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)76 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)50 ClusterNode (org.apache.ignite.cluster.ClusterNode)48 IgniteException (org.apache.ignite.IgniteException)34 ArrayList (java.util.ArrayList)33 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)31 JobEvent (org.apache.ignite.events.JobEvent)28 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)28 HashMap (java.util.HashMap)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)26 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 Collection (java.util.Collection)23 Map (java.util.Map)23