Search in sources :

Example 21 with DiscoveryEvent

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

the class IgniteCacheClientNodeChangingTopologyTest method findKeys.

/**
     * Tries to find keys for two partitions: for one partition assignment should not change after node join,
     * for another primary node should change.
     *
     * @param ignite Ignite.
     * @param nodes Current nodes.
     * @return Found keys.
     */
private IgniteBiTuple<Integer, Integer> findKeys(Ignite ignite, ClusterNode... nodes) {
    ClusterNode newNode = new TcpDiscoveryNode();
    GridTestUtils.setFieldValue(newNode, "consistentId", getTestIgniteInstanceName(4));
    GridTestUtils.setFieldValue(newNode, "id", UUID.randomUUID());
    List<ClusterNode> topNodes = new ArrayList<>();
    Collections.addAll(topNodes, nodes);
    topNodes.add(newNode);
    DiscoveryEvent discoEvt = new DiscoveryEvent(newNode, "", EventType.EVT_NODE_JOINED, newNode);
    final long topVer = ignite.cluster().topologyVersion();
    GridAffinityFunctionContextImpl ctx = new GridAffinityFunctionContextImpl(topNodes, null, discoEvt, new AffinityTopologyVersion(topVer + 1), 1);
    AffinityFunction affFunc = ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getAffinity();
    List<List<ClusterNode>> newAff = affFunc.assignPartitions(ctx);
    List<List<ClusterNode>> curAff = ((IgniteKernal) ignite).context().cache().internalCache(DEFAULT_CACHE_NAME).context().affinity().assignments(new AffinityTopologyVersion(topVer));
    Integer key1 = null;
    Integer key2 = null;
    Affinity<Integer> aff = ignite.affinity(DEFAULT_CACHE_NAME);
    for (int i = 0; i < curAff.size(); i++) {
        if (key1 == null) {
            List<ClusterNode> oldNodes = curAff.get(i);
            List<ClusterNode> newNodes = newAff.get(i);
            if (oldNodes.equals(newNodes))
                key1 = findKey(aff, i);
        }
        if (key2 == null) {
            ClusterNode oldPrimary = F.first(curAff.get(i));
            ClusterNode newPrimary = F.first(newAff.get(i));
            if (!oldPrimary.equals(newPrimary))
                key2 = findKey(aff, i);
        }
        if (key1 != null && key2 != null)
            break;
    }
    if (key1 == null || key2 == null)
        fail("Failed to find nodes required for test.");
    return new IgniteBiTuple<>(key1, key2);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) List(java.util.List) ArrayList(java.util.ArrayList) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 22 with DiscoveryEvent

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

the class GridEventConsumeSelfTest method testApi.

/**
     * @throws Exception If failed.
     */
public void testApi() throws Exception {
    try {
        grid(0).events().stopRemoteListen(null);
    } catch (NullPointerException ignored) {
    // No-op.
    }
    grid(0).events().stopRemoteListen(UUID.randomUUID());
    UUID consumeId = null;
    try {
        consumeId = grid(0).events().remoteListen(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);
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
    try {
        consumeId = grid(0).events().remoteListen(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;
            }
        });
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
    try {
        consumeId = grid(0).events().remoteListen(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;
            }
        });
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) P2(org.apache.ignite.internal.util.typedef.P2) 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)

Example 23 with DiscoveryEvent

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

the class GridEventConsumeSelfTest method testApiAsyncOld.

/**
     * @throws Exception If failed.
     */
public void testApiAsyncOld() throws Exception {
    IgniteEvents evtAsync = grid(0).events().withAsync();
    try {
        evtAsync.stopRemoteListen(null);
        evtAsync.future().get();
    } catch (NullPointerException ignored) {
    // No-op.
    }
    evtAsync.stopRemoteListen(UUID.randomUUID());
    evtAsync.future().get();
    UUID consumeId = null;
    try {
        evtAsync.remoteListen(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);
        consumeId = (UUID) evtAsync.future().get();
        assertNotNull(consumeId);
    } finally {
        evtAsync.stopRemoteListen(consumeId);
        evtAsync.future().get();
    }
    try {
        evtAsync.remoteListen(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;
            }
        });
        consumeId = (UUID) evtAsync.future().get();
        assertNotNull(consumeId);
    } finally {
        evtAsync.stopRemoteListen(consumeId);
        evtAsync.future().get();
    }
    try {
        evtAsync.remoteListen(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;
            }
        });
        consumeId = (UUID) evtAsync.future().get();
        assertNotNull(consumeId);
    } finally {
        evtAsync.stopRemoteListen(consumeId);
        evtAsync.future().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)

Example 24 with DiscoveryEvent

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

the class GridEventConsumeSelfTest method testMasterNodeLeaveNoAutoUnsubscribe.

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

                @Override
                public boolean apply(Event evt) {
                    if (nodeId.equals(((DiscoveryEvent) evt).eventNode().id()))
                        discoLatch.countDown();
                    return true;
                }
            }, EVT_NODE_LEFT);
        }
        consumeLatch = new CountDownLatch(GRID_CNT * 2 + 1);
        consumeCnt = new AtomicInteger();
        noAutoUnsubscribe = true;
        g.events().remoteListen(1, 0, false, null, new P1<Event>() {

            @Override
            public boolean apply(Event evt) {
                consumeLatch.countDown();
                consumeCnt.incrementAndGet();
                return true;
            }
        }, EVT_JOB_STARTED);
        grid(0).compute().broadcast(F.noop());
    } finally {
        stopGrid("anotherGrid");
    }
    discoLatch.await(3000, MILLISECONDS);
    grid(0).compute().broadcast(F.noop());
    assert consumeLatch.await(2, SECONDS);
    assertEquals(GRID_CNT * 2 + 1, consumeCnt.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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)

Example 25 with DiscoveryEvent

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

the class TcpDiscoverySelfTest method testPingInterruptedOnNodeFailed.

/**
     * @throws Exception If any error occurs.
     */
public void testPingInterruptedOnNodeFailed() throws Exception {
    try {
        final Ignite pingingNode = startGrid("testPingInterruptedOnNodeFailedPingingNode");
        final Ignite failedNode = startGrid("testPingInterruptedOnNodeFailedFailingNode");
        startGrid("testPingInterruptedOnNodeFailedSimpleNode");
        ((TestTcpDiscoverySpi) failedNode.configuration().getDiscoverySpi()).ignorePingResponse = true;
        final UUID failedNodeId = failedNode.cluster().localNode().id();
        final CountDownLatch pingLatch = new CountDownLatch(1);
        final CountDownLatch eventLatch = new CountDownLatch(1);
        final AtomicBoolean pingRes = new AtomicBoolean(true);
        final AtomicBoolean failRes = new AtomicBoolean(false);
        long startTs = System.currentTimeMillis();
        pingingNode.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event event) {
                if (((DiscoveryEvent) event).eventNode().id().equals(failedNodeId)) {
                    failRes.set(true);
                    eventLatch.countDown();
                }
                return true;
            }
        }, EventType.EVT_NODE_FAILED);
        IgniteInternalFuture<?> pingFut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                pingLatch.countDown();
                pingRes.set(pingingNode.configuration().getDiscoverySpi().pingNode(failedNodeId));
                return null;
            }
        }, 1);
        IgniteInternalFuture<?> failingFut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                pingLatch.await();
                Thread.sleep(3000);
                ((TestTcpDiscoverySpi) failedNode.configuration().getDiscoverySpi()).simulateNodeFailure();
                return null;
            }
        }, 1);
        failingFut.get();
        pingFut.get();
        assertFalse(pingRes.get());
        assertTrue(System.currentTimeMillis() - startTs < pingingNode.configuration().getFailureDetectionTimeout() / 2);
        assertTrue(eventLatch.await(7, TimeUnit.SECONDS));
        assertTrue(failRes.get());
    } finally {
        stopAllGrids();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteIllegalStateException(org.apache.ignite.IgniteIllegalStateException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Aggregations

DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)68 Event (org.apache.ignite.events.Event)49 UUID (java.util.UUID)36 ClusterNode (org.apache.ignite.cluster.ClusterNode)27 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)22 Ignite (org.apache.ignite.Ignite)20 CountDownLatch (java.util.concurrent.CountDownLatch)19 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)14 ArrayList (java.util.ArrayList)11 Collection (java.util.Collection)10 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 List (java.util.List)9 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 JobEvent (org.apache.ignite.events.JobEvent)8 GridAffinityFunctionContextImpl (org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 IgniteException (org.apache.ignite.IgniteException)5 HashMap (java.util.HashMap)4