Search in sources :

Example 86 with DiscoveryEvent

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

the class GridDiscoveryEventSelfTest method testJoinSequenceEvents.

/**
 * @throws Exception If failed.
 */
@Test
public void testJoinSequenceEvents() 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(3);
        g0.events().localListen(new IgnitePredicate<Event>() {

            private AtomicInteger cnt = new AtomicInteger();

            @Override
            public boolean apply(Event evt) {
                assert evt.type() == EVT_NODE_JOINED : evt;
                evts.put(cnt.getAndIncrement(), ((DiscoveryEvent) evt).topologyNodes());
                latch.countDown();
                return true;
            }
        }, EVT_NODE_JOINED);
        UUID id1 = startGrid(1).cluster().localNode().id();
        UUID id2 = startGrid(2).cluster().localNode().id();
        UUID id3 = startGrid(3).cluster().localNode().id();
        assertTrue("Wrong count of events received: " + 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));
    } 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 87 with DiscoveryEvent

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

the class GridSameVmStartupSelfTest method testSameVmStartup.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testSameVmStartup() throws Exception {
    Ignite ignite1 = startGrid(1);
    Collection<ClusterNode> top1 = ignite1.cluster().forRemotes().nodes();
    try {
        assert top1.isEmpty() : "Grid1 topology is not empty: " + top1;
        // Start another grid.
        Ignite ignite2 = startGrid(2);
        final CountDownLatch latch = new CountDownLatch(1);
        int size1 = ignite1.cluster().forRemotes().nodes().size();
        int size2 = ignite2.cluster().forRemotes().nodes().size();
        assert size1 == 1 : "Invalid number of remote nodes discovered: " + size1;
        assert size2 == 1 : "Invalid number of remote nodes discovered: " + size2;
        final UUID grid1LocNodeId = ignite1.cluster().localNode().id();
        ignite2.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                boolean tcpDiscovery = tcpDiscovery();
                if (tcpDiscovery)
                    assert evt.type() != EVT_NODE_FAILED : "Node1 did not exit gracefully.";
                if (evt instanceof DiscoveryEvent) {
                    // Local node can send METRICS_UPDATED event.
                    assert ((DiscoveryEvent) evt).eventNode().id().equals(grid1LocNodeId) || evt.type() == EVT_NODE_METRICS_UPDATED : "Received event about invalid node [received=" + ((DiscoveryEvent) evt).eventNode().id() + ", expected=" + grid1LocNodeId + ", type=" + evt.type() + ']';
                    if (tcpDiscovery) {
                        if (evt.type() == EVT_NODE_LEFT)
                            latch.countDown();
                    } else {
                        if (evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED)
                            latch.countDown();
                    }
                }
                return true;
            }
        }, EVTS_DISCOVERY);
        stopGrid(1);
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        Collection<ClusterNode> top2 = ignite2.cluster().forRemotes().nodes();
        assert top2.isEmpty() : "Grid2 topology is not empty: " + top2;
    } finally {
        stopGrid(1);
        stopGrid(2);
    }
    assert G.allGrids().isEmpty();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest)

Example 88 with DiscoveryEvent

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

the class GridEventConsumeSelfTest method testApiAsync.

/**
 * @throws Exception If failed.
 */
@Test
public void testApiAsync() throws Exception {
    IgniteEvents evt = grid(0).events();
    try {
        evt.stopRemoteListenAsync(null).get();
    } catch (NullPointerException ignored) {
    // No-op.
    }
    evt.stopRemoteListenAsync(UUID.randomUUID()).get();
    UUID consumeId = null;
    try {
        consumeId = evt.remoteListenAsync(new P2<UUID, DiscoveryEvent>() {

            @Override
            public boolean apply(UUID uuid, DiscoveryEvent evt) {
                return false;
            }
        }, new P1<DiscoveryEvent>() {

            @Override
            public boolean apply(DiscoveryEvent e) {
                return false;
            }
        }, EVTS_DISCOVERY).get();
        assertNotNull(consumeId);
    } finally {
        evt.stopRemoteListenAsync(consumeId).get();
    }
    try {
        consumeId = evt.remoteListenAsync(new P2<UUID, DiscoveryEvent>() {

            @Override
            public boolean apply(UUID uuid, DiscoveryEvent evt) {
                return false;
            }
        }, new P1<DiscoveryEvent>() {

            @Override
            public boolean apply(DiscoveryEvent e) {
                return false;
            }
        }).get();
        assertNotNull(consumeId);
    } finally {
        evt.stopRemoteListenAsync(consumeId).get();
    }
    try {
        consumeId = evt.remoteListenAsync(new P2<UUID, Event>() {

            @Override
            public boolean apply(UUID uuid, Event evt) {
                return false;
            }
        }, new P1<Event>() {

            @Override
            public boolean apply(Event e) {
                return false;
            }
        }).get();
        assertNotNull(consumeId);
    } finally {
        evt.stopRemoteListenAsync(consumeId).get();
    }
}
Also used : IgniteEvents(org.apache.ignite.IgniteEvents) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 89 with DiscoveryEvent

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

the class GridEventConsumeSelfTest method testMasterNodeLeave.

/**
 * @throws Exception If failed.
 */
@Test
public void testMasterNodeLeave() throws Exception {
    final CountDownLatch latch = new CountDownLatch(GRID_CNT);
    Ignite g = startGrid("anotherGridMasterNodeLeave");
    try {
        final UUID nodeId = g.cluster().localNode().id();
        for (int i = 0; i < GRID_CNT; i++) {
            grid(i).events().localListen(new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event evt) {
                    if (nodeId.equals(((DiscoveryEvent) evt).eventNode().id()))
                        latch.countDown();
                    return true;
                }
            }, EVT_NODE_LEFT, EVT_NODE_FAILED);
        }
        g.events().remoteListen(null, new P1<Event>() {

            @Override
            public boolean apply(Event evt) {
                return true;
            }
        }, EVTS_ALL);
    } finally {
        stopGrid("anotherGridMasterNodeLeave");
    }
    assert latch.await(3000, MILLISECONDS);
}
Also used : DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 90 with DiscoveryEvent

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

the class CommunicationDiscoveryEventListener method onEvent.

/**
 * {@inheritDoc}
 */
@Override
public void onEvent(Event evt) {
    assert evt instanceof DiscoveryEvent : evt;
    assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
    ClusterNode node = ((DiscoveryEvent) evt).eventNode();
    onNodeLeft(node.consistentId(), node.id());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent)

Aggregations

DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)111 Event (org.apache.ignite.events.Event)56 UUID (java.util.UUID)49 ClusterNode (org.apache.ignite.cluster.ClusterNode)45 CountDownLatch (java.util.concurrent.CountDownLatch)30 Ignite (org.apache.ignite.Ignite)29 Test (org.junit.Test)27 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)25 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)24 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)23 ArrayList (java.util.ArrayList)20 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 List (java.util.List)16 Collection (java.util.Collection)12 HashMap (java.util.HashMap)12 Map (java.util.Map)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)11 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)10