Search in sources :

Example 31 with IgnitePredicate

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

the class IgfsEventsAbstractSelfTest method testDirWithFiles.

/**
     * Checks events on CRUD operations on a single directory
     * with some files.
     *
     * @throws Exception If failed.
     */
public void testDirWithFiles() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 4 + 3 + 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");
    IgfsPath file1 = new IgfsPath(dir, "file1");
    IgfsPath file2 = new IgfsPath(dir, "file2");
    // Will generate EVT_IGFS_DIR_CREATED + EVT_IGFS_FILE_CREATED + EVT_IGFS_FILE_OPENED_WRITE +
    // EVT_IGFS_FILE_CLOSED_WRITE.
    igfs.create(file1, true).close();
    // Will generate EVT_IGFS_FILE_CREATED + EVT_IGFS_FILE_OPENED_WRITE +
    // EVT_IGFS_FILE_CLOSED.
    igfs.create(file2, true).close();
    // Will generate EVT_IGFS_DIR_DELETED event.
    assertTrue(igfs.delete(dir, 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());
    assertTrue(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    assertFalse(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(3);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(4);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    assertFalse(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(5);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    evt = (IgfsEvent) evtList.get(6);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    evt = (IgfsEvent) evtList.get(7);
    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 32 with IgnitePredicate

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

the class TcpDiscoverySelfTest method testMetricsSending.

/**
     * @throws Exception If any error occurs.
     */
public void testMetricsSending() throws Exception {
    final AtomicBoolean stopping = new AtomicBoolean();
    try {
        final CountDownLatch latch1 = new CountDownLatch(1);
        final Ignite g1 = startGrid(1);
        IgnitePredicate<Event> lsnr1 = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                info(evt.message());
                latch1.countDown();
                return true;
            }
        };
        g1.events().localListen(lsnr1, EVT_NODE_METRICS_UPDATED);
        assert latch1.await(10, SECONDS);
        g1.events().stopLocalListen(lsnr1);
        final CountDownLatch latch1_1 = new CountDownLatch(1);
        final CountDownLatch latch1_2 = new CountDownLatch(1);
        final CountDownLatch latch2_1 = new CountDownLatch(1);
        final CountDownLatch latch2_2 = new CountDownLatch(1);
        final Ignite g2 = startGrid(2);
        g2.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                if (stopping.get())
                    return true;
                info(evt.message());
                UUID id = ((DiscoveryEvent) evt).eventNode().id();
                if (id.equals(g1.cluster().localNode().id()))
                    latch2_1.countDown();
                else if (id.equals(g2.cluster().localNode().id()))
                    latch2_2.countDown();
                else
                    assert false : "Event fired for unknown node.";
                return true;
            }
        }, EVT_NODE_METRICS_UPDATED);
        g1.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                if (stopping.get())
                    return true;
                info(evt.message());
                UUID id = ((DiscoveryEvent) evt).eventNode().id();
                if (id.equals(g1.cluster().localNode().id()))
                    latch1_1.countDown();
                else if (id.equals(g2.cluster().localNode().id()))
                    latch1_2.countDown();
                else
                    assert false : "Event fired for unknown node.";
                return true;
            }
        }, EVT_NODE_METRICS_UPDATED);
        assert latch1_1.await(10, SECONDS);
        assert latch1_2.await(10, SECONDS);
        assert latch2_1.await(10, SECONDS);
        assert latch2_2.await(10, SECONDS);
    } finally {
        stopping.set(true);
        stopAllGrids();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 33 with IgnitePredicate

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

the class TcpDiscoveryMultiThreadedTest method getConfiguration.

/** {@inheritDoc} */
@SuppressWarnings({ "IfMayBeConditional" })
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    UUID id = nodeId.get();
    if (id != null) {
        cfg.setNodeId(id);
        nodeId.set(null);
    }
    if (client())
        cfg.setClientMode(true);
    cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder).setJoinTimeout(60_000).setNetworkTimeout(10_000));
    int[] evts = { EVT_NODE_FAILED, EVT_NODE_LEFT };
    Map<IgnitePredicate<? extends Event>, int[]> lsnrs = new HashMap<>();
    lsnrs.put(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            DiscoveryEvent discoveryEvt = (DiscoveryEvent) evt;
            failedNodes.add(discoveryEvt.eventNode().id());
            return true;
        }
    }, evts);
    cfg.setLocalEventListeners(lsnrs);
    cfg.setCacheConfiguration();
    cfg.setIncludeEventTypes(EVT_TASK_FAILED, EVT_TASK_FINISHED, EVT_JOB_MAPPED);
    cfg.setIncludeProperties();
    ((TcpCommunicationSpi) cfg.getCommunicationSpi()).setSharedMemoryPort(-1);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) HashMap(java.util.HashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) UUID(java.util.UUID) TcpCommunicationSpi(org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi)

Example 34 with IgnitePredicate

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

the class TcpDiscoveryRestartTest method getConfiguration.

/** {@inheritDoc} */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    TcpDiscoverySpi spi = new TcpDiscoverySpi();
    spi.setIpFinder(ipFinder);
    cfg.setDiscoverySpi(spi);
    int[] evts = { EVT_NODE_JOINED, EVT_NODE_FAILED, EVT_NODE_LEFT };
    cfg.setIncludeEventTypes(evts);
    Map<IgnitePredicate<? extends Event>, int[]> lsnrs = new HashMap<>();
    lsnrs.put(new TestEventListener(), evts);
    cfg.setLocalEventListeners(lsnrs);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) HashMap(java.util.HashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event)

Example 35 with IgnitePredicate

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

the class IgniteSourceConnectorTest method doTest.

/**
     * Tests the source with the specified source configurations.
     *
     * @param srcProps Source properties.
     * @param conditioned Flag indicating whether filtering is enabled.
     * @throws Exception Fails if error.
     */
private void doTest(Map<String, String> srcProps, boolean conditioned) throws Exception {
    FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>(new Callback<Herder.Created<ConnectorInfo>>() {

        @Override
        public void onCompletion(Throwable error, Herder.Created<ConnectorInfo> info) {
            if (error != null)
                throw new RuntimeException("Failed to create a job!", error);
        }
    });
    herder.putConnectorConfig(srcProps.get(ConnectorConfig.NAME_CONFIG), srcProps, true, cb);
    cb.get();
    // Ugh! To be sure Kafka Connect's worker thread is properly started...
    Thread.sleep(5000);
    final CountDownLatch latch = new CountDownLatch(EVENT_CNT);
    final IgnitePredicate<CacheEvent> locLsnr = new IgnitePredicate<CacheEvent>() {

        @Override
        public boolean apply(CacheEvent evt) {
            assert evt != null;
            latch.countDown();
            return true;
        }
    };
    grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).localListen(locLsnr, EVT_CACHE_OBJECT_PUT);
    IgniteCache<String, String> cache = grid.cache(CACHE_NAME);
    assertEquals(0, cache.size(CachePeekMode.PRIMARY));
    Map<String, String> keyValMap = new HashMap<>(EVENT_CNT);
    keyValMap.putAll(sendData());
    // Checks all events are processed.
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).stopLocalListen(locLsnr);
    assertEquals(EVENT_CNT, cache.size(CachePeekMode.PRIMARY));
    // Checks the events are transferred to Kafka broker.
    if (conditioned)
        checkDataDelivered(EVENT_CNT * TOPICS.length / 2);
    else
        checkDataDelivered(EVENT_CNT * TOPICS.length);
}
Also used : ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) HashMap(java.util.HashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEvent(org.apache.ignite.events.CacheEvent) Herder(org.apache.kafka.connect.runtime.Herder) StandaloneHerder(org.apache.kafka.connect.runtime.standalone.StandaloneHerder) FutureCallback(org.apache.kafka.connect.util.FutureCallback)

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