Search in sources :

Example 21 with IgnitePredicate

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

the class GridLocalEventListenerSelfTest method getConfiguration.

/** {@inheritDoc} */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    int idx = getTestIgniteInstanceIndex(igniteInstanceName);
    if (idx == 0) {
        Map<IgnitePredicate<? extends Event>, int[]> lsnrs = new HashMap<>();
        lsnrs.put(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                fired.countDown();
                return true;
            }
        }, new int[] { EventType.EVT_NODE_JOINED });
        cfg.setLocalEventListeners(lsnrs);
    }
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) HashMap(java.util.HashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event)

Example 22 with IgnitePredicate

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

the class IgniteClientReconnectAbstractTest method reconnectClientNodes.

/**
     * Reconnect client node.
     *
     * @param log  Logger.
     * @param clients Clients.
     * @param srv Server.
     * @param disconnectedC Closure which will be run when client node disconnected.
     * @throws Exception If failed.
     */
protected static void reconnectClientNodes(final IgniteLogger log, List<Ignite> clients, Ignite srv, @Nullable Runnable disconnectedC) throws Exception {
    final TestTcpDiscoverySpi srvSpi = spi(srv);
    final CountDownLatch disconnectLatch = new CountDownLatch(clients.size());
    final CountDownLatch reconnectLatch = new CountDownLatch(clients.size());
    log.info("Block reconnect.");
    for (Ignite client : clients) spi(client).writeLatch = new CountDownLatch(1);
    IgnitePredicate<Event> p = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
                log.info("Disconnected: " + evt);
                disconnectLatch.countDown();
            } else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                log.info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    };
    for (Ignite client : clients) client.events().localListen(p, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
    for (Ignite client : clients) srvSpi.failNode(client.cluster().localNode().id(), null);
    waitReconnectEvent(log, disconnectLatch);
    if (disconnectedC != null)
        disconnectedC.run();
    log.info("Allow reconnect.");
    for (Ignite client : clients) spi(client).writeLatch.countDown();
    waitReconnectEvent(log, reconnectLatch);
    for (Ignite client : clients) client.events().stopLocalListen(p);
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 23 with IgnitePredicate

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

the class GridTestUtils method assertOneToOne.

/**
     * Asserts that each element in iterable has one-to-one correspondence with a
     * predicate from list.
     *
     * @param it Input iterable of elements.
     * @param ps Array of predicates (by number of elements in iterable).
     */
@SuppressWarnings("ConstantConditions")
public static <T> void assertOneToOne(Iterable<T> it, IgnitePredicate<T>... ps) {
    Collection<IgnitePredicate<T>> ps0 = new ArrayList<>(Arrays.asList(ps));
    Collection<T2<IgnitePredicate<T>, T>> passed = new ArrayList<>();
    for (T elem : it) {
        for (T2<IgnitePredicate<T>, T> p : passed) {
            if (p.get1().apply(elem))
                throw new AssertionError("Two elements match one predicate [elem1=" + p.get2() + ", elem2=" + elem + ", pred=" + p.get1() + ']');
        }
        IgnitePredicate<T> matched = null;
        for (IgnitePredicate<T> p : ps0) {
            if (p.apply(elem)) {
                if (matched != null)
                    throw new AssertionError("Element matches more than one predicate [elem=" + elem + ", pred1=" + p + ", pred2=" + matched + ']');
                matched = p;
            }
        }
        if (// None matched.
        matched == null)
            throw new AssertionError("The element does not match [elem=" + elem + ", numRemainingPreds=" + ps0.size() + ']');
        ps0.remove(matched);
        passed.add(new T2<>(matched, elem));
    }
}
Also used : LT(org.apache.ignite.internal.util.typedef.internal.LT) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) T2(org.apache.ignite.internal.util.typedef.T2)

Example 24 with IgnitePredicate

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

the class GridJobStealingSelfTest method testProjectionPredicateDifferentClassLoaders.

/**
     * Tests job stealing with peer deployment and different class loaders.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
public void testProjectionPredicateDifferentClassLoaders() throws Exception {
    final Ignite ignite3 = startGrid(3);
    URL[] clsLdrUrls;
    try {
        clsLdrUrls = new URL[] { new URL(GridTestProperties.getProperty("p2p.uri.cls")) };
    } catch (MalformedURLException e) {
        throw new RuntimeException("Define property p2p.uri.cls", e);
    }
    ClassLoader ldr1 = new URLClassLoader(clsLdrUrls, getClass().getClassLoader());
    Class taskCls = ldr1.loadClass("org.apache.ignite.tests.p2p.JobStealingTask");
    Class nodeFilterCls = ldr1.loadClass("org.apache.ignite.tests.p2p.ExcludeNodeFilter");
    IgnitePredicate<ClusterNode> nodeFilter = (IgnitePredicate<ClusterNode>) nodeFilterCls.getConstructor(UUID.class).newInstance(ignite2.cluster().localNode().id());
    Map<UUID, Integer> ret = (Map<UUID, Integer>) executeAsync(compute(ignite1.cluster().forPredicate(nodeFilter)), taskCls, null).get(TASK_EXEC_TIMEOUT_MS);
    assert ret != null;
    assert ret.get(ignite1.cluster().localNode().id()) != null && ret.get(ignite1.cluster().localNode().id()) == 2 : ret.get(ignite1.cluster().localNode().id());
    assert ret.get(ignite3.cluster().localNode().id()) != null && ret.get(ignite3.cluster().localNode().id()) == 2 : ret.get(ignite3.cluster().localNode().id());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) MalformedURLException(java.net.MalformedURLException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with IgnitePredicate

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

the class GridCacheContinuousQueryAbstractSelfTest method testEvents.

/**
     * @throws Exception If failed.
     */
public void testEvents() throws Exception {
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(50);
    final CountDownLatch execLatch = new CountDownLatch(cacheMode() == REPLICATED ? 1 : gridCount());
    IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            assert evt instanceof CacheQueryReadEvent;
            CacheQueryReadEvent qe = (CacheQueryReadEvent) evt;
            assertEquals(CONTINUOUS.name(), qe.queryType());
            assertEquals(DEFAULT_CACHE_NAME, qe.cacheName());
            assertEquals(grid(0).localNode().id(), qe.subjectId());
            assertNull(qe.className());
            assertNull(qe.clause());
            assertNull(qe.scanQueryFilter());
            assertNotNull(qe.continuousQueryFilter());
            assertNull(qe.arguments());
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    };
    IgnitePredicate<Event> execLsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            assert evt instanceof CacheQueryExecutedEvent;
            CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
            assertEquals(CONTINUOUS.name(), qe.queryType());
            assertEquals(DEFAULT_CACHE_NAME, qe.cacheName());
            assertEquals(grid(0).localNode().id(), qe.subjectId());
            assertNull(qe.className());
            assertNull(qe.clause());
            assertNull(qe.scanQueryFilter());
            assertNotNull(qe.continuousQueryFilter());
            assertNull(qe.arguments());
            execLatch.countDown();
            return true;
        }
    };
    try {
        for (int i = 0; i < gridCount(); i++) {
            grid(i).events().localListen(lsnr, EVT_CACHE_QUERY_OBJECT_READ);
            grid(i).events().localListen(execLsnr, EVT_CACHE_QUERY_EXECUTED);
        }
        IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
        ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
        qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            // No-op.
            }
        });
        qry.setRemoteFilter(new CacheEntryEventSerializableFilter<Integer, Integer>() {

            @Override
            public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
                return evt.getValue() >= 50;
            }
        });
        try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
            for (int i = 0; i < 100; i++) cache.put(i, i);
            assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
            assert execLatch.await(LATCH_TIMEOUT, MILLISECONDS);
            assertEquals(50, cnt.get());
        }
    } finally {
        for (int i = 0; i < gridCount(); i++) {
            grid(i).events().stopLocalListen(lsnr, EVT_CACHE_QUERY_OBJECT_READ);
            grid(i).events().stopLocalListen(execLsnr, EVT_CACHE_QUERY_EXECUTED);
        }
    }
}
Also used : CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent)

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