Search in sources :

Example 6 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class PlatformEvents method startWaitForLocalAsync.

/**
     * Starts the waitForLocal asynchronously.
     *
     * @param reader Reader
     * @param events Events.
     * @return Result.
     */
private IgniteFuture<EventAdapter> startWaitForLocalAsync(BinaryRawReaderEx reader, IgniteEvents events) {
    Long filterHnd = reader.readObject();
    IgnitePredicate filter = filterHnd != null ? localFilter(filterHnd) : null;
    int[] eventTypes = readEventTypes(reader);
    return events.waitForLocalAsync(filter, eventTypes);
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate)

Example 7 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class VisorTaskUtils method collectEvents.

/**
     * Grabs local events and detects if events was lost since last poll.
     *
     * @param ignite Target grid.
     * @param evtOrderKey Unique key to take last order key from node local map.
     * @param evtThrottleCntrKey Unique key to take throttle count from node local map.
     * @param evtTypes Event types to collect.
     * @param evtMapper Closure to map grid events to Visor data transfer objects.
     * @return Collections of node events
     */
public static List<VisorGridEvent> collectEvents(Ignite ignite, String evtOrderKey, String evtThrottleCntrKey, int[] evtTypes, IgniteClosure<Event, VisorGridEvent> evtMapper) {
    assert ignite != null;
    assert evtTypes != null && evtTypes.length > 0;
    ConcurrentMap<String, Long> nl = ignite.cluster().nodeLocalMap();
    final long lastOrder = getOrElse(nl, evtOrderKey, -1L);
    final long throttle = getOrElse(nl, evtThrottleCntrKey, 0L);
    // When we first time arrive onto a node to get its local events,
    // we'll grab only last those events that not older than given period to make sure we are
    // not grabbing GBs of data accidentally.
    final long notOlderThan = System.currentTimeMillis() - EVENTS_COLLECT_TIME_WINDOW;
    // Flag for detecting gaps between events.
    final AtomicBoolean lastFound = new AtomicBoolean(lastOrder < 0);
    IgnitePredicate<Event> p = new IgnitePredicate<Event>() {

        /** */
        private static final long serialVersionUID = 0L;

        @Override
        public boolean apply(Event e) {
            // Detects that events were lost.
            if (!lastFound.get() && (lastOrder == e.localOrder()))
                lastFound.set(true);
            // Retains events by lastOrder, period and type.
            return e.localOrder() > lastOrder && e.timestamp() > notOlderThan;
        }
    };
    Collection<Event> evts = ignite.configuration().getEventStorageSpi() instanceof NoopEventStorageSpi ? Collections.<Event>emptyList() : ignite.events().localQuery(p, evtTypes);
    // Update latest order in node local, if not empty.
    if (!evts.isEmpty()) {
        Event maxEvt = Collections.max(evts, EVTS_ORDER_COMPARATOR);
        nl.put(evtOrderKey, maxEvt.localOrder());
    }
    // Update throttle counter.
    if (!lastFound.get())
        nl.put(evtThrottleCntrKey, throttle == 0 ? EVENTS_LOST_THROTTLE : throttle - 1);
    boolean lost = !lastFound.get() && throttle == 0;
    List<VisorGridEvent> res = new ArrayList<>(evts.size() + (lost ? 1 : 0));
    if (lost)
        res.add(new VisorGridEventsLost(ignite.cluster().localNode().id()));
    for (Event e : evts) {
        VisorGridEvent visorEvt = evtMapper.apply(e);
        if (visorEvt != null)
            res.add(visorEvt);
    }
    return res;
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) VisorGridEventsLost(org.apache.ignite.internal.visor.event.VisorGridEventsLost) NoopEventStorageSpi(org.apache.ignite.spi.eventstorage.NoopEventStorageSpi) VisorGridEvent(org.apache.ignite.internal.visor.event.VisorGridEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Event(org.apache.ignite.events.Event) VisorGridEvent(org.apache.ignite.internal.visor.event.VisorGridEvent)

Example 8 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class ClusterGroupSelfTest method testForPredicate.

/**
     * @throws Exception If failed.
     */
public void testForPredicate() throws Exception {
    IgnitePredicate<ClusterNode> evenP = new IgnitePredicate<ClusterNode>() {

        @Override
        public boolean apply(ClusterNode node) {
            return node.order() % 2 == 0;
        }
    };
    IgnitePredicate<ClusterNode> oddP = new IgnitePredicate<ClusterNode>() {

        @Override
        public boolean apply(ClusterNode node) {
            return node.order() % 2 == 1;
        }
    };
    ClusterGroup remotes = ignite.cluster().forRemotes();
    ClusterGroup evenYoungest = remotes.forPredicate(evenP).forYoungest();
    ClusterGroup evenOldest = remotes.forPredicate(evenP).forOldest();
    ClusterGroup oddYoungest = remotes.forPredicate(oddP).forYoungest();
    ClusterGroup oddOldest = remotes.forPredicate(oddP).forOldest();
    int clusterSize = ignite.cluster().nodes().size();
    assertEquals(grid(gridMaxOrder(clusterSize, true)).localNode().id(), evenYoungest.node().id());
    assertEquals(grid(1).localNode().id(), evenOldest.node().id());
    assertEquals(grid(gridMaxOrder(clusterSize, false)).localNode().id(), oddYoungest.node().id());
    assertEquals(grid(2).localNode().id(), oddOldest.node().id());
    try (Ignite g4 = startGrid(NODES_CNT);
        Ignite g5 = startGrid(NODES_CNT + 1)) {
        clusterSize = g4.cluster().nodes().size();
        assertEquals(grid(gridMaxOrder(clusterSize, true)).localNode().id(), evenYoungest.node().id());
        assertEquals(grid(1).localNode().id(), evenOldest.node().id());
        assertEquals(grid(gridMaxOrder(clusterSize, false)).localNode().id(), oddYoungest.node().id());
        assertEquals(grid(2).localNode().id(), oddOldest.node().id());
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Example 9 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgfsEventsAbstractSelfTest method testNestedEmptyDirs.

/**
     * Checks events on CRUD operations with multiple
     * empty directories.
     *
     * @throws Exception If failed.
     */
public void testNestedEmptyDirs() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 2 + 1;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    IgfsPath dir = new IgfsPath("/dir1/dir2");
    assertFalse(igfs.exists(dir.parent()));
    // Will generate 2 EVT_IGFS_DIR_RENAMED events.
    igfs.mkdirs(dir);
    // Will generate EVT_IGFS_DIR_DELETED event.
    assertTrue(igfs.delete(dir.parent(), true));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_DIR_DELETED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgfsEventsAbstractSelfTest method testMoveFile.

/**
     * Checks events on CRUD operations on file move.
     *
     * @throws Exception If failed.
     */
public void testMoveFile() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 5 + 1;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    IgfsPath dir = new IgfsPath("/dir1/dir2");
    IgfsPath file = new IgfsPath(dir, "file1");
    // Will generate 2 EVT_IGFS_DIR_CREATED events + EVT_IGFS_FILE_CREATED_EVENT + OPEN + CLOSE.
    igfs.create(file, true).close();
    // Will generate 1 EVT_IGFS_FILE_RENAMED.
    igfs.rename(file, dir.parent());
    assertTrue(igfs.exists(new IgfsPath(dir.parent(), file.name())));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(3);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(4);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/file1"), evt.path());
    assertEquals(0, evt.dataSize());
    IgfsEvent evt4 = (IgfsEvent) evtList.get(5);
    assertEquals(EVT_IGFS_FILE_RENAMED, evt4.type());
    assertEquals(new IgfsPath("/dir1/dir2/file1"), evt4.path());
    assertEquals(new IgfsPath("/dir1/file1"), evt4.newPath());
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)75 Event (org.apache.ignite.events.Event)43 CountDownLatch (java.util.concurrent.CountDownLatch)33 Ignite (org.apache.ignite.Ignite)27 ArrayList (java.util.ArrayList)15 UUID (java.util.UUID)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 IgfsEvent (org.apache.ignite.events.IgfsEvent)9 HashMap (java.util.HashMap)8 CacheEvent (org.apache.ignite.events.CacheEvent)7 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)7 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)7 Map (java.util.Map)6 IgniteException (org.apache.ignite.IgniteException)6 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 CacheQueryReadEvent (org.apache.ignite.events.CacheQueryReadEvent)6 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)6 Ignition (org.apache.ignite.Ignition)5