Search in sources :

Example 51 with Event

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

the class GridEventConsumeSelfTest method testEventsByType.

/**
 * @throws Exception If failed.
 */
public void testEventsByType() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT);
    UUID consumeId = grid(0).events().remoteListen(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    }, null, EVT_JOB_STARTED);
    try {
        assertNotNull(consumeId);
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT, nodeIds.size());
        assertEquals(GRID_CNT, cnt.get());
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
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) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 52 with Event

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

the class GridEventConsumeSelfTest method testProjectionWithLocalNode.

/**
 * @throws Exception If failed.
 */
public void testProjectionWithLocalNode() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT - 1);
    UUID consumeId = events(grid(0).cluster().forAttribute("include", null)).remoteListen(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    }, null, EVT_JOB_STARTED);
    try {
        assertNotNull(consumeId);
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT - 1, nodeIds.size());
        assertEquals(GRID_CNT - 1, cnt.get());
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
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) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 53 with Event

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

the class GridTaskCancelSingleNodeSelfTest method checkCancellation.

/**
 * @param timeoutBeforeCancel Timeout.
 * @throws Exception If failed.
 */
@SuppressWarnings("ErrorNotRethrown")
private void checkCancellation(long timeoutBeforeCancel) throws Exception {
    final AtomicInteger finished = new AtomicInteger();
    final AtomicInteger cancelled = new AtomicInteger();
    final AtomicInteger rejected = new AtomicInteger();
    final AtomicBoolean jobStarted = new AtomicBoolean();
    grid().events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            info("Received event: " + evt);
            switch(evt.type()) {
                case EVT_JOB_STARTED:
                    jobStarted.set(true);
                    break;
                case EVT_JOB_FINISHED:
                    finished.incrementAndGet();
                    break;
                case EVT_JOB_CANCELLED:
                    cancelled.incrementAndGet();
                    break;
                case EVT_JOB_REJECTED:
                    rejected.incrementAndGet();
                    break;
                default:
                    assert false : "Unexpected event: " + evt;
            }
            return true;
        }
    }, EVT_JOB_STARTED, EVT_JOB_FINISHED, EVT_JOB_CANCELLED, EVT_JOB_REJECTED);
    ComputeTaskFuture<?> fut = grid().compute().executeAsync(TestTask.class, null);
    if (timeoutBeforeCancel > 0L)
        Thread.sleep(timeoutBeforeCancel);
    assert fut.cancel();
    for (int i = 0; i < 3; i++) {
        try {
            if (timeoutBeforeCancel == 0L) {
                if (jobStarted.get())
                    assertTrue("Failed on iteration [i=" + i + ", finished=" + finished.get() + ", cancelled=" + cancelled.get() + ", rejected=" + rejected.get() + ']', finished.get() == 1 && cancelled.get() == 1 && rejected.get() == 0);
                else {
                    // job can be rejected if was concurrently cancelled before started
                    assertTrue("Failed on iteration [i=" + i + ", finished=" + finished.get() + ", cancelled=" + cancelled.get() + ", rejected=" + rejected.get() + ']', finished.get() == 0 && cancelled.get() == 0 && rejected.get() <= 1);
                }
            } else
                assertTrue("Failed on iteration [i=" + i + ", finished=" + finished.get() + ", cancelled=" + cancelled.get() + ", rejected=" + rejected.get() + ']', finished.get() == 1 && cancelled.get() == 1 && rejected.get() == 0);
        } catch (AssertionError e) {
            info("Check failed: " + e.getMessage());
            if (timeoutBeforeCancel == 0L && i == 2)
                throw e;
        }
        if (i < 2)
            U.sleep(500);
    }
    try {
        fut.get();
        fail();
    } catch (IgniteFutureCancelledException e) {
        info("Caught expected exception: " + e);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.apache.ignite.events.Event) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException)

Example 54 with Event

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

the class GridTaskJobRejectSelfTest method testReject.

/**
 * @throws Exception If failed.
 */
public void testReject() throws Exception {
    grid(1).events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            X.println("Task event: " + evt);
            return true;
        }
    }, EVTS_TASK_EXECUTION);
    grid(1).events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            X.println("Job event: " + evt);
            return true;
        }
    }, EVTS_JOB_EXECUTION);
    final CountDownLatch startedLatch = new CountDownLatch(1);
    grid(1).events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            startedLatch.countDown();
            return true;
        }
    }, EVT_JOB_STARTED);
    final AtomicInteger failedOver = new AtomicInteger(0);
    grid(1).events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            failedOver.incrementAndGet();
            return true;
        }
    }, EVT_JOB_FAILED_OVER);
    final CountDownLatch finishedLatch = new CountDownLatch(1);
    grid(1).events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            finishedLatch.countDown();
            return true;
        }
    }, EVT_TASK_FINISHED, EVT_TASK_FAILED);
    final ClusterNode node = grid(1).localNode();
    ComputeTaskFuture<?> fut = grid(1).compute().executeAsync(new ComputeTaskAdapter<Void, Void>() {

        @Override
        public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable Void arg) {
            return F.asMap(new SleepJob(), node, new SleepJob(), node);
        }

        /**
         * {@inheritDoc}
         */
        @Nullable
        @Override
        public Void reduce(List<ComputeJobResult> results) {
            return null;
        }
    }, null);
    assert startedLatch.await(2, SECONDS);
    fut.cancel();
    assert finishedLatch.await(2, SECONDS);
    assert failedOver.get() == 0;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CountDownLatch(java.util.concurrent.CountDownLatch) ComputeJob(org.apache.ignite.compute.ComputeJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.apache.ignite.events.Event) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult)

Example 55 with Event

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

the class GridContinuousProcessor method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() throws IgniteCheckedException {
    if (ctx.config().isDaemon())
        return;
    retryDelay = ctx.config().getNetworkSendRetryDelay();
    retryCnt = ctx.config().getNetworkSendRetryCount();
    marsh = ctx.config().getMarshaller();
    ctx.event().addLocalEventListener(new DiscoveryListener(), EVT_NODE_LEFT, EVT_NODE_FAILED);
    ctx.event().addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            cancelFutures(new IgniteCheckedException("Topology segmented"));
        }
    }, EVT_NODE_SEGMENTED);
    ctx.discovery().setCustomEventListener(StartRoutineDiscoveryMessage.class, new CustomEventListener<StartRoutineDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineDiscoveryMessage msg) {
            if (ctx.isStopping())
                return;
            processStartRequest(snd, msg);
        }
    });
    ctx.discovery().setCustomEventListener(StartRoutineAckDiscoveryMessage.class, new CustomEventListener<StartRoutineAckDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineAckDiscoveryMessage msg) {
            if (ctx.isStopping())
                return;
            processStartAckRequest(topVer, msg);
        }
    });
    ctx.discovery().setCustomEventListener(StopRoutineDiscoveryMessage.class, new CustomEventListener<StopRoutineDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineDiscoveryMessage msg) {
            if (ctx.isStopping())
                return;
            processStopRequest(snd, msg);
        }
    });
    ctx.discovery().setCustomEventListener(StopRoutineAckDiscoveryMessage.class, new CustomEventListener<StopRoutineAckDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineAckDiscoveryMessage msg) {
            if (ctx.isStopping())
                return;
            processStopAckRequest(msg);
        }
    });
    ctx.io().addMessageListener(TOPIC_CONTINUOUS, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object obj, byte plc) {
            GridContinuousMessage msg = (GridContinuousMessage) obj;
            if (msg.data() == null && msg.dataBytes() != null) {
                try {
                    msg.data(U.unmarshal(marsh, msg.dataBytes(), U.resolveClassLoader(ctx.config())));
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to process message (ignoring): " + msg, e);
                    return;
                }
            }
            switch(msg.type()) {
                case MSG_EVT_NOTIFICATION:
                    processNotification(nodeId, msg);
                    break;
                case MSG_EVT_ACK:
                    processMessageAck(msg);
                    break;
                default:
                    assert false : "Unexpected message received: " + msg.type();
            }
        }
    });
    ctx.cacheObjects().onContinuousProcessorStarted(ctx);
    ctx.service().onContinuousProcessorStarted(ctx);
    if (log.isDebugEnabled())
        log.debug("Continuous processor started.");
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) UUID(java.util.UUID)

Aggregations

Event (org.apache.ignite.events.Event)226 CountDownLatch (java.util.concurrent.CountDownLatch)127 Ignite (org.apache.ignite.Ignite)112 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)96 UUID (java.util.UUID)71 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)43 ClusterNode (org.apache.ignite.cluster.ClusterNode)38 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)28 ArrayList (java.util.ArrayList)27 JobEvent (org.apache.ignite.events.JobEvent)27 IgniteException (org.apache.ignite.IgniteException)20 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 HashSet (java.util.HashSet)15 TaskEvent (org.apache.ignite.events.TaskEvent)15 Collection (java.util.Collection)13 HashMap (java.util.HashMap)12 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)12 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)12