Search in sources :

Example 71 with DiscoveryEvent

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

the class TcpDiscoverySpiReconnectDelayTest method checkServerJoinAtStart.

/**
 * Check that client restores connection after the given time, with the expected number of messages sent
 * and expected time elapsed.
 *  @param numOfFailedRequests number of TcpDiscoveryJoinRequestMessage to be failed before succeed to connect.
 * @param reconnectDelay argument for {@link TcpDiscoverySpi#setReconnectDelay(int)}
 */
private void checkServerJoinAtStart(int numOfFailedRequests, int reconnectDelay) throws Exception {
    try (Ignite ignite1 = G.start(getConfiguration("server", false, reconnectDelay))) {
        final CountDownLatch joinLatch = new CountDownLatch(1);
        final AtomicInteger failJoinReqRes = ((FailingTcpDiscoverySpi) ignite1.configuration().getDiscoverySpi()).failJoinReqRes;
        failJoinReqRes.set(numOfFailedRequests);
        ignite1.events().localListen(new IgnitePredicate<DiscoveryEvent>() {

            @Override
            public boolean apply(DiscoveryEvent evt) {
                info("Node1 event: " + evt);
                if (evt.type() == EVT_NODE_JOINED)
                    joinLatch.countDown();
                return true;
            }
        }, EVT_NODE_JOINED);
        final long startTime = System.currentTimeMillis();
        try (Ignite ignite2 = G.start(getConfiguration("server-2", false, reconnectDelay))) {
            assertTrue(joinLatch.await(EVT_TIMEOUT, MILLISECONDS));
            long endTime = System.currentTimeMillis();
            // Check topology.
            assertEquals(1L, ignite1.cluster().localNode().order());
            assertEquals(2L, ignite2.cluster().localNode().order());
            assertEquals(2L, ignite2.cluster().topologyVersion());
            // Check connection time.
            // Total time should be at least the sum of all delays.
            long totalTime = endTime - startTime;
            long expTotalTime = numOfFailedRequests * reconnectDelay;
            assertTrue(totalTime >= expTotalTime);
            // Check number of messages.
            // If exactly numOfFailedRequests fail, counter will be at -1.
            // If unexpected additional requests are sent, counter will be <= -2.
            int cntr = failJoinReqRes.get();
            int numOfMessages = numOfFailedRequests - cntr;
            int expNumOfMessages = numOfFailedRequests + 1;
            assertEquals("Unexpected number of messages", expNumOfMessages, numOfMessages);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 72 with DiscoveryEvent

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

the class HadoopJobTracker method onKernalStart.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("deprecation")
@Override
public void onKernalStart() throws IgniteCheckedException {
    super.onKernalStart();
    jobMetaCache().context().continuousQueries().executeInternalQuery(new CacheEntryUpdatedListener<HadoopJobId, HadoopJobMetadata>() {

        @Override
        public void onUpdated(final Iterable<CacheEntryEvent<? extends HadoopJobId, ? extends HadoopJobMetadata>> evts) {
            if (!busyLock.tryReadLock())
                return;
            try {
                // Must process query callback in a separate thread to avoid deadlocks.
                evtProcSvc.execute(new EventHandler() {

                    @Override
                    protected void body() throws IgniteCheckedException {
                        processJobMetadataUpdates(evts);
                    }
                });
            } finally {
                busyLock.readUnlock();
            }
        }
    }, null, true, true, false);
    ctx.kernalContext().event().addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(final Event evt) {
            if (!busyLock.tryReadLock())
                return;
            try {
                // Must process discovery callback in a separate thread to avoid deadlock.
                evtProcSvc.execute(new EventHandler() {

                    @Override
                    protected void body() {
                        processNodeLeft((DiscoveryEvent) evt);
                    }
                });
            } finally {
                busyLock.readUnlock();
            }
        }
    }, EventType.EVT_NODE_FAILED, EventType.EVT_NODE_LEFT);
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryEvent(javax.cache.event.CacheEntryEvent) HadoopJobId(org.apache.ignite.internal.processors.hadoop.HadoopJobId)

Example 73 with DiscoveryEvent

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

the class GridMapQueryExecutor method start.

/**
 * @param ctx Context.
 * @param h2 H2 Indexing.
 * @throws IgniteCheckedException If failed.
 */
public void start(final GridKernalContext ctx, IgniteH2Indexing h2) throws IgniteCheckedException {
    this.ctx = ctx;
    this.h2 = h2;
    log = ctx.log(GridMapQueryExecutor.class);
    final UUID locNodeId = ctx.localNodeId();
    ctx.event().addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(final Event evt) {
            UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
            GridH2QueryContext.clearAfterDeadNode(locNodeId, nodeId);
            MapNodeResults nodeRess = qryRess.remove(nodeId);
            if (nodeRess == null)
                return;
            nodeRess.cancelAll();
        }
    }, EventType.EVT_NODE_FAILED, EventType.EVT_NODE_LEFT);
    ctx.io().addMessageListener(GridTopic.TOPIC_QUERY, new GridMessageListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            if (!busyLock.enterBusy())
                return;
            try {
                if (msg instanceof GridCacheQueryMarshallable)
                    ((GridCacheQueryMarshallable) msg).unmarshall(ctx.config().getMarshaller(), ctx);
                GridMapQueryExecutor.this.onMessage(nodeId, msg);
            } finally {
                busyLock.leaveBusy();
            }
        }
    });
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridCacheQueryMarshallable(org.apache.ignite.internal.processors.cache.query.GridCacheQueryMarshallable) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) UUID(java.util.UUID)

Example 74 with DiscoveryEvent

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

the class IgniteCacheReplicatedQuerySelfTest method testNodeLeft.

/**
 * @throws Exception If failed.
 */
@IgniteIgnore(value = "https://issues.apache.org/jira/browse/IGNITE-613", forceFailure = true)
public void testNodeLeft() throws Exception {
    Ignite g = startGrid("client");
    try {
        assertTrue(g.configuration().isClientMode());
        IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
        for (int i = 0; i < 1000; i++) cache.put(i, i);
        // Client cache should be empty.
        assertEquals(0, cache.localSize());
        QueryCursor<Cache.Entry<Integer, Integer>> q = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "_key >= 0 order by _key").setPageSize(10));
        assertEquals(0, (int) q.iterator().next().getKey());
        // Query for replicated cache was run on one of nodes.
        ConcurrentMap<?, ?> mapNode1 = queryResultMap(0);
        ConcurrentMap<?, ?> mapNode2 = queryResultMap(1);
        ConcurrentMap<?, ?> mapNode3 = queryResultMap(2);
        assertEquals(1, mapNode1.size() + mapNode2.size() + mapNode3.size());
        final UUID nodeId = g.cluster().localNode().id();
        final CountDownLatch latch = new CountDownLatch(1);
        grid(0).events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                if (((DiscoveryEvent) evt).eventNode().id().equals(nodeId))
                    latch.countDown();
                return true;
            }
        }, EVT_NODE_LEFT);
        stopGrid("client");
        latch.await();
        assertEquals(0, mapNode1.size());
        assertEquals(0, mapNode2.size());
        assertEquals(0, mapNode3.size());
    } finally {
        stopGrid("client");
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CountDownLatch(java.util.concurrent.CountDownLatch) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) IgniteIgnore(org.apache.ignite.testsuites.IgniteIgnore)

Example 75 with DiscoveryEvent

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

the class GridMemoryEventStorageMultiThreadedSelfTest method testMultiThreaded.

/**
 * @throws Exception If test failed
 */
public void testMultiThreaded() throws Exception {
    GridTestUtils.runMultiThreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < 100000; i++) getSpi().record(new DiscoveryEvent(null, "Test event", 1, null));
            return null;
        }
    }, 10, "event-thread");
    Collection<Event> evts = getSpi().localEvents(F.<Event>alwaysTrue());
    info("Events count in memory: " + evts.size());
    assert evts.size() <= 10000 : "Incorrect number of events: " + evts.size();
}
Also used : DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event)

Aggregations

DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)83 Event (org.apache.ignite.events.Event)54 UUID (java.util.UUID)39 ClusterNode (org.apache.ignite.cluster.ClusterNode)32 CountDownLatch (java.util.concurrent.CountDownLatch)24 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)24 Ignite (org.apache.ignite.Ignite)23 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)18 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 ArrayList (java.util.ArrayList)12 Collection (java.util.Collection)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)11 List (java.util.List)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 JobEvent (org.apache.ignite.events.JobEvent)8 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)7 GridAffinityFunctionContextImpl (org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl)7 IgniteException (org.apache.ignite.IgniteException)6 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)6