Search in sources :

Example 11 with IgniteEvents

use of org.apache.ignite.IgniteEvents in project ignite by apache.

the class GridMarshallerAbstractTest method testEvents.

/**
 * @throws Exception If failed.
 */
public void testEvents() throws Exception {
    IgniteConfiguration cfg = optimize(getConfiguration("g1"));
    try (Ignite g1 = G.start(cfg)) {
        IgniteEvents evts = events(grid().cluster().forNode(g1.cluster().localNode()));
        evts.localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event gridEvt) {
                return true;
            }
        }, EVTS_CACHE);
        grid().cache(DEFAULT_CACHE_NAME).put(1, 1);
        GridMarshallerTestBean inBean = newTestBean(evts);
        byte[] buf = marshal(inBean);
        GridMarshallerTestBean outBean = unmarshal(buf);
        assert inBean.getObjectField() != null;
        assert outBean.getObjectField() != null;
        assert inBean.getObjectField().getClass().equals(IgniteEventsImpl.class);
        assert outBean.getObjectField().getClass().equals(IgniteEventsImpl.class);
        assert inBean != outBean;
        assert inBean.equals(outBean);
        ClusterGroup inPrj = evts.clusterGroup();
        ClusterGroup outPrj = ((IgniteEvents) outBean.getObjectField()).clusterGroup();
        assert inPrj.getClass().equals(outPrj.getClass());
        assert F.eqNotOrdered(inPrj.nodes(), outPrj.nodes());
        outBean.checkNullResources();
    }
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteEvents(org.apache.ignite.IgniteEvents) Event(org.apache.ignite.events.Event) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Example 12 with IgniteEvents

use of org.apache.ignite.IgniteEvents in project ignite by apache.

the class GridEventConsumeSelfTest method testApiAsync.

/**
 * @throws Exception If failed.
 */
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)

Example 13 with IgniteEvents

use of org.apache.ignite.IgniteEvents in project ignite by apache.

the class GridEventConsumeSelfTest method testMultithreadedWithNodeRestart.

/**
 * @throws Exception If failed.
 */
public void testMultithreadedWithNodeRestart() throws Exception {
    final AtomicBoolean stop = new AtomicBoolean();
    final BlockingQueue<IgniteBiTuple<Integer, UUID>> queue = new LinkedBlockingQueue<>();
    final Collection<UUID> started = new GridConcurrentHashSet<>();
    final Collection<UUID> stopped = new GridConcurrentHashSet<>();
    final Random rnd = new Random();
    IgniteInternalFuture<?> starterFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < CONSUME_CNT; i++) {
                int idx = rnd.nextInt(GRID_CNT);
                try {
                    IgniteEvents evts = grid(idx).events();
                    UUID consumeId = evts.remoteListenAsync(new P2<UUID, Event>() {

                        @Override
                        public boolean apply(UUID uuid, Event evt) {
                            return true;
                        }
                    }, null, EVT_JOB_STARTED).get(3000);
                    started.add(consumeId);
                    queue.add(F.t(idx, consumeId));
                } catch (ClusterTopologyException ignored) {
                // No-op.
                }
                U.sleep(10);
            }
            stop.set(true);
            return null;
        }
    }, 8, "consume-starter");
    IgniteInternalFuture<?> stopperFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                IgniteBiTuple<Integer, UUID> t = queue.poll(1, SECONDS);
                if (t == null)
                    continue;
                int idx = t.get1();
                UUID consumeId = t.get2();
                try {
                    IgniteEvents evts = grid(idx).events();
                    evts.stopRemoteListenAsync(consumeId).get(3000);
                    stopped.add(consumeId);
                } catch (ClusterTopologyException ignored) {
                // No-op.
                }
            }
            return null;
        }
    }, 4, "consume-stopper");
    IgniteInternalFuture<?> nodeRestarterFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                startGrid("anotherGrid");
                stopGrid("anotherGrid");
            }
            return null;
        }
    }, 1, "node-restarter");
    IgniteInternalFuture<?> jobRunnerFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                int idx = rnd.nextInt(GRID_CNT);
                try {
                    grid(idx).compute().runAsync(F.noop()).get(3000);
                } catch (IgniteException ignored) {
                // Ignore all job execution related errors.
                }
            }
            return null;
        }
    }, 1, "job-runner");
    starterFut.get();
    stopperFut.get();
    nodeRestarterFut.get();
    jobRunnerFut.get();
    IgniteBiTuple<Integer, UUID> t;
    while ((t = queue.poll()) != null) {
        int idx = t.get1();
        UUID consumeId = t.get2();
        grid(idx).events().stopRemoteListenAsync(consumeId).get(3000);
        stopped.add(consumeId);
    }
    Collection<UUID> notStopped = F.lose(started, true, stopped);
    assertEquals("Not stopped IDs: " + notStopped, 0, notStopped.size());
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteException(org.apache.ignite.IgniteException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) IgniteEvents(org.apache.ignite.IgniteEvents) IgniteException(org.apache.ignite.IgniteException) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) UUID(java.util.UUID)

Aggregations

IgniteEvents (org.apache.ignite.IgniteEvents)13 Event (org.apache.ignite.events.Event)8 Ignite (org.apache.ignite.Ignite)7 UUID (java.util.UUID)4 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 JobEvent (org.apache.ignite.events.JobEvent)3 WalSegmentArchivedEvent (org.apache.ignite.events.WalSegmentArchivedEvent)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)2 HashSet (java.util.HashSet)1 Random (java.util.Random)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 IgniteException (org.apache.ignite.IgniteException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)1 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)1 IgniteWalIteratorFactory (org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory)1 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)1