Search in sources :

Example 66 with IgnitePredicate

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

the class GridDiscoverySelfTest method testDiscoveryListener.

/**
     * @throws Exception If failed.
     */
public void testDiscoveryListener() throws Exception {
    ClusterNode node = ignite.cluster().localNode();
    assert node != null;
    final AtomicInteger cnt = new AtomicInteger();
    /** Joined nodes counter. */
    final CountDownLatch joinedCnt = new CountDownLatch(NODES_CNT);
    /** Left nodes counter. */
    final CountDownLatch leftCnt = new CountDownLatch(NODES_CNT);
    IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (EVT_NODE_JOINED == evt.type()) {
                cnt.incrementAndGet();
                joinedCnt.countDown();
            } else if (EVT_NODE_LEFT == evt.type()) {
                int i = cnt.decrementAndGet();
                assert i >= 0;
                leftCnt.countDown();
            } else
                assert false;
            return true;
        }
    };
    ignite.events().localListen(lsnr, EVT_NODE_LEFT, EVT_NODE_JOINED);
    try {
        for (int i = 0; i < NODES_CNT; i++) startGrid(i);
        joinedCnt.await(MAX_TIMEOUT_IN_MINS, MINUTES);
        assert cnt.get() == NODES_CNT;
        for (int i = 0; i < NODES_CNT; i++) stopGrid(i);
        leftCnt.await(MAX_TIMEOUT_IN_MINS, MINUTES);
        assert cnt.get() == 0;
        ignite.events().stopLocalListen(lsnr);
        assert cnt.get() == 0;
    } finally {
        for (int i = 0; i < NODES_CNT; i++) stopAndCancelGrid(i);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 67 with IgnitePredicate

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

the class GridCacheDhtPreloadDisabledSelfTest method startGrids.

/**
     * @param cnt Number of grids.
     * @param startIdx Start node index.
     * @param list List of started grids.
     * @throws Exception If failed.
     */
private void startGrids(int cnt, int startIdx, Collection<Ignite> list) throws Exception {
    for (int i = 0; i < cnt; i++) {
        final Ignite g = startGrid(startIdx++);
        if (DEBUG)
            g.events().localListen(new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event evt) {
                    info("\n>>> Preload event [igniteInstanceName=" + g.name() + ", evt=" + evt + ']');
                    return true;
                }
            }, EVTS_CACHE_REBALANCE);
        list.add(g);
    }
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite)

Example 68 with IgnitePredicate

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

the class F0 method and.

/**
     * Get a predicate (non peer-deployable) that evaluates to {@code true} if each of its component predicates
     * evaluates to {@code true}. The components are evaluated in order they are supplied.
     * Evaluation will be stopped as soon as first predicate evaluates to {@code false}.
     * Passed in predicates are NOT copied. If no predicates are passed in the returned
     * predicate will always evaluate to {@code false}.
     *
     * @param p1 Passed in predicates.
     * @param p2 Passed in predicates.
     * @param <T> Type of the free variable, i.e. the element the predicate is called on.
     * @return Predicate that evaluates to {@code true} if each of its component predicates
     *      evaluates to {@code true}.
     */
@SuppressWarnings({ "unchecked", "ConfusingArgumentToVarargsMethod" })
public static <T> IgnitePredicate<T> and(@Nullable final IgnitePredicate<? super T>[] p1, @Nullable final IgnitePredicate<? super T>... p2) {
    if (F.isAlwaysFalse(p1) || F.isAlwaysFalse(p2))
        return F.alwaysFalse();
    if (F.isAlwaysTrue(p1) && F.isAlwaysTrue(p2))
        return F.alwaysTrue();
    final boolean e1 = F.isEmpty(p1);
    final boolean e2 = F.isEmpty(p2);
    if (e1 && e2)
        return F.alwaysTrue();
    if (e1) {
        if (p2.length == 1)
            return (IgnitePredicate<T>) p2[0];
    }
    if (!e1 && e2) {
        if (p1.length == 1)
            return (IgnitePredicate<T>) p1[0];
    }
    if ((e1 || isAllNodePredicates(p1)) && (e2 || isAllNodePredicates(p2))) {
        Set<UUID> ids = new GridLeanSet<>();
        if (!e1) {
            for (IgnitePredicate<? super T> p : p1) ids.addAll(((GridNodePredicate) p).nodeIds());
        }
        if (!e2) {
            for (IgnitePredicate<? super T> p : p2) ids.addAll(((GridNodePredicate) p).nodeIds());
        }
        // T must be <T extends ClusterNode>.
        return (IgnitePredicate<T>) new GridNodePredicate(ids);
    } else {
        return new P1<T>() {

            @Override
            public boolean apply(T t) {
                if (!e1) {
                    for (IgnitePredicate<? super T> p : p1) if (p != null && !p.apply(t))
                        return false;
                }
                if (!e2) {
                    for (IgnitePredicate<? super T> p : p2) if (p != null && !p.apply(t))
                        return false;
                }
                return true;
            }
        };
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) GridNodePredicate(org.apache.ignite.internal.util.lang.GridNodePredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) UUID(java.util.UUID)

Example 69 with IgnitePredicate

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

the class F0 method and.

/**
     * Get a predicate (not peer-deployable) that evaluates to {@code true} if each of its component predicates
     * evaluates to {@code true}. The components are evaluated in order they are supplied.
     * Evaluation will be stopped as soon as first predicate evaluates to {@code false}.
     * Passed in predicates are NOT copied. If no predicates are passed in the returned
     * predicate will always evaluate to {@code false}.
     *
     * @param ps Passed in predicate. If none provided - always-{@code false} predicate is
     *      returned.
     * @param <T> Type of the free variable, i.e. the element the predicate is called on.
     * @return Predicate that evaluates to {@code true} if each of its component predicates
     *      evaluates to {@code true}.
     */
@SuppressWarnings({ "unchecked", "ConfusingArgumentToVarargsMethod", "ConstantConditions" })
public static <T> IgnitePredicate<T> and(@Nullable final IgnitePredicate<? super T> p, @Nullable final IgnitePredicate<? super T>... ps) {
    if (p == null && F.isEmptyOrNulls(ps))
        return F.alwaysTrue();
    if (F.isAlwaysFalse(p) && F.isAlwaysFalse(ps))
        return F.alwaysFalse();
    if (F.isAlwaysTrue(p) && F.isAlwaysTrue(ps))
        return F.alwaysTrue();
    if (isAllNodePredicates(p) && isAllNodePredicates(ps)) {
        assert ps != null;
        Set<UUID> ids = new GridLeanSet<>();
        for (IgnitePredicate<? super T> p0 : ps) {
            Collection<UUID> list = ((GridNodePredicate) p0).nodeIds();
            if (ids.isEmpty())
                ids.addAll(list);
            else
                ids.retainAll(list);
        }
        Collection<UUID> list = ((GridNodePredicate) p).nodeIds();
        if (ids.isEmpty())
            ids.addAll(list);
        else
            ids.retainAll(list);
        // T must be <T extends ClusterNode>.
        return (IgnitePredicate<T>) new GridNodePredicate(ids);
    } else {
        return new P1<T>() {

            @Override
            public boolean apply(T t) {
                assert ps != null;
                if (p != null && !p.apply(t))
                    return false;
                for (IgnitePredicate<? super T> p : ps) if (p != null && !p.apply(t))
                    return false;
                return true;
            }
        };
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) GridNodePredicate(org.apache.ignite.internal.util.lang.GridNodePredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) UUID(java.util.UUID)

Example 70 with IgnitePredicate

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

the class IgniteCacheConfigVariationsFullApiTest method checkGetOutTx.

/**
     * @param async Use async API.
     * @param oldAsync Uase old style async API.
     * @throws Exception If failed.
     */
private void checkGetOutTx(boolean async, boolean oldAsync) throws Exception {
    final AtomicInteger lockEvtCnt = new AtomicInteger();
    IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            lockEvtCnt.incrementAndGet();
            return true;
        }
    };
    try {
        IgniteCache<String, Integer> cache = grid(0).cache(cacheName());
        List<String> keys = primaryKeysForCache(0, 2, 1);
        assertEquals(2, keys.size());
        cache.put(keys.get(0), 0);
        cache.put(keys.get(1), 1);
        grid(0).events().localListen(lsnr, EVT_CACHE_OBJECT_LOCKED, EVT_CACHE_OBJECT_UNLOCKED);
        if (async && oldAsync)
            cache = cache.withAsync();
        try (Transaction tx = transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            Integer val0;
            if (async) {
                if (oldAsync) {
                    cache.get(keys.get(0));
                    val0 = cache.<Integer>future().get();
                } else
                    val0 = cache.getAsync(keys.get(0)).get();
            } else
                val0 = cache.get(keys.get(0));
            assertEquals(0, val0.intValue());
            Map<String, Integer> allOutTx;
            if (async) {
                if (oldAsync) {
                    cache.getAllOutTx(F.asSet(keys.get(1)));
                    allOutTx = cache.<Map<String, Integer>>future().get();
                } else
                    allOutTx = cache.getAllOutTxAsync(F.asSet(keys.get(1))).get();
            } else
                allOutTx = cache.getAllOutTx(F.asSet(keys.get(1)));
            assertEquals(1, allOutTx.size());
            assertTrue(allOutTx.containsKey(keys.get(1)));
            assertEquals(1, allOutTx.get(keys.get(1)).intValue());
        }
        assertTrue(GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                info("Lock event count: " + lockEvtCnt.get());
                if (atomicityMode() == ATOMIC)
                    return lockEvtCnt.get() == 0;
                if (cacheMode() == PARTITIONED && nearEnabled()) {
                    if (!grid(0).configuration().isClientMode())
                        return lockEvtCnt.get() == 4;
                }
                return lockEvtCnt.get() == 2;
            }
        }, 15000));
    } finally {
        grid(0).events().stopLocalListen(lsnr, EVT_CACHE_OBJECT_LOCKED, EVT_CACHE_OBJECT_UNLOCKED);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

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