Search in sources :

Example 26 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class TcpDiscoverySelfTest method testNoExtraNodeFailedMessage.

/**
     * @throws Exception If failed.
     */
public void testNoExtraNodeFailedMessage() throws Exception {
    try {
        final int NODES = 10;
        startGridsMultiThreaded(NODES);
        int stopIdx = 5;
        Ignite failIgnite = ignite(stopIdx);
        ((TcpDiscoverySpi) failIgnite.configuration().getDiscoverySpi()).simulateNodeFailure();
        for (int i = 0; i < NODES; i++) {
            if (i != stopIdx) {
                final Ignite ignite = ignite(i);
                GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return ignite.cluster().topologyVersion() >= NODES + 1;
                    }
                }, 10_000);
                TcpDiscoverySpi spi = (TcpDiscoverySpi) ignite.configuration().getDiscoverySpi();
                TcpDiscoveryStatistics stats = GridTestUtils.getFieldValue(spi, "stats");
                Integer cnt = stats.sentMessages().get(TcpDiscoveryNodeFailedMessage.class.getSimpleName());
                log.info("Count1: " + cnt);
                assertTrue("Invalid message count: " + cnt, cnt == null || cnt <= 2);
                cnt = stats.receivedMessages().get(TcpDiscoveryNodeFailedMessage.class.getSimpleName());
                log.info("Count2: " + cnt);
                assertTrue("Invalid message count: " + cnt, cnt == null || cnt <= 2);
            }
        }
    } finally {
        stopAllGrids();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA) TcpDiscoveryStatistics(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryStatistics) Ignite(org.apache.ignite.Ignite) TcpDiscoveryNodeFailedMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryNodeFailedMessage)

Example 27 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class GridMessagingSelfTest method testStopLocalListen.

/**
     * @throws Exception If error occurs.
     */
@SuppressWarnings("TooBroadScope")
public void testStopLocalListen() throws Exception {
    final AtomicInteger msgCnt1 = new AtomicInteger();
    final AtomicInteger msgCnt2 = new AtomicInteger();
    final AtomicInteger msgCnt3 = new AtomicInteger();
    P2<UUID, Object> lsnr1 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener1 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt1.incrementAndGet();
            return true;
        }
    };
    P2<UUID, Object> lsnr2 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener2 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt2.incrementAndGet();
            return true;
        }
    };
    P2<UUID, Object> lsnr3 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener3 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt3.incrementAndGet();
            return true;
        }
    };
    final String topic1 = null;
    final String topic2 = "top1";
    final String topic3 = "top3";
    ignite1.message().localListen(topic1, lsnr1);
    ignite1.message().localListen(topic2, lsnr2);
    ignite1.message().localListen(topic3, lsnr3);
    ClusterGroup rNode1 = ignite2.cluster().forRemotes();
    message(rNode1).send(topic1, "msg1-1");
    message(rNode1).send(topic2, "msg1-2");
    message(rNode1).send(topic3, "msg1-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 0 && msgCnt2.get() > 0 && msgCnt3.get() > 0;
        }
    }, 5000);
    assertEquals(1, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(1, msgCnt3.get());
    ignite1.message().stopLocalListen(topic2, lsnr2);
    message(rNode1).send(topic1, "msg2-1");
    message(rNode1).send(topic2, "msg2-2");
    message(rNode1).send(topic3, "msg2-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 1 && msgCnt3.get() > 1;
        }
    }, 5000);
    assertEquals(2, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(2, msgCnt3.get());
    // Try to use wrong topic for lsnr1 removing.
    ignite1.message().stopLocalListen(topic2, lsnr1);
    message(rNode1).send(topic1, "msg3-1");
    message(rNode1).send(topic2, "msg3-2");
    message(rNode1).send(topic3, "msg3-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 2 && msgCnt3.get() > 2;
        }
    }, 5000);
    assertEquals(3, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(3, msgCnt3.get());
    ignite1.message().stopLocalListen(topic1, lsnr1);
    ignite1.message().stopLocalListen(topic3, lsnr3);
    message(rNode1).send(topic1, "msg4-1");
    message(rNode1).send(topic2, "msg4-2");
    message(rNode1).send(topic3, "msg4-3");
    U.sleep(1000);
    assertEquals(3, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(3, msgCnt3.get());
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) P2(org.apache.ignite.internal.util.typedef.P2) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) UUID(java.util.UUID)

Example 28 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class GridCommonAbstractTest method findKeys.

/**
     * @param cache Cache.
     * @param cnt Keys count.
     * @param startFrom Start value for keys search.
     * @return Collection of keys for which given cache is primary.
     */
@SuppressWarnings("unchecked")
protected List<Integer> findKeys(IgniteCache<?, ?> cache, final int cnt, final int startFrom, final int type) {
    assert cnt > 0 : cnt;
    final List<Integer> found = new ArrayList<>(cnt);
    final ClusterNode locNode = localNode(cache);
    final Affinity<Integer> aff = (Affinity<Integer>) affinity(cache);
    try {
        GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                for (int i = startFrom; i < startFrom + 100_000; i++) {
                    Integer key = i;
                    boolean ok;
                    if (type == 0)
                        ok = aff.isPrimary(locNode, key);
                    else if (type == 1)
                        ok = aff.isBackup(locNode, key);
                    else if (type == 2)
                        ok = !aff.isPrimaryOrBackup(locNode, key);
                    else {
                        fail();
                        return false;
                    }
                    if (ok) {
                        if (!found.contains(key))
                            found.add(key);
                        if (found.size() == cnt)
                            return true;
                    }
                }
                return false;
            }
        }, 5000);
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
    if (found.size() != cnt)
        throw new IgniteException("Unable to find " + cnt + " requied keys.");
    return found;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterNode(org.apache.ignite.cluster.ClusterNode) PA(org.apache.ignite.internal.util.typedef.PA) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ArrayList(java.util.ArrayList) Affinity(org.apache.ignite.cache.affinity.Affinity)

Example 29 with PA

use of org.apache.ignite.internal.util.typedef.PA 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)

Example 30 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class IgniteTxExceptionAbstractSelfTest method checkUnlocked.

/**
     * @param key Key.
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
private void checkUnlocked(final Integer key) throws Exception {
    TestIndexingSpi.forceFail(false);
    awaitPartitionMapExchange();
    info("Check key: " + key);
    for (int i = 0; i < gridCount(); i++) {
        final int idx = i;
        GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                IgniteKernal grid = (IgniteKernal) grid(idx);
                GridCacheAdapter cache = grid.internalCache(DEFAULT_CACHE_NAME);
                GridCacheEntryEx entry = cache.peekEx(key);
                log.info("Entry: " + entry);
                if (entry != null) {
                    try {
                        boolean locked = entry.lockedByAny();
                        if (locked) {
                            info("Unexpected entry for grid [i=" + idx + ", entry=" + entry + ']');
                            return false;
                        }
                    } catch (GridCacheEntryRemovedException ignore) {
                    // Obsolete entry cannot be locked.
                    }
                }
                if (cache.isNear()) {
                    entry = ((GridNearCacheAdapter) cache).dht().peekEx(key);
                    log.info("Dht entry: " + entry);
                    if (entry != null) {
                        try {
                            boolean locked = entry.lockedByAny();
                            if (locked) {
                                info("Unexpected entry for grid [i=" + idx + ", entry=" + entry + ']');
                                return false;
                            }
                        } catch (GridCacheEntryRemovedException ignore) {
                        // Obsolete entry cannot be locked.
                        }
                    }
                }
                return true;
            }
        }, getTestTimeout());
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteKernal(org.apache.ignite.internal.IgniteKernal)

Aggregations

PA (org.apache.ignite.internal.util.typedef.PA)48 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 Ignite (org.apache.ignite.Ignite)17 ArrayList (java.util.ArrayList)12 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)12 UUID (java.util.UUID)9 CacheEntryEvent (javax.cache.event.CacheEntryEvent)9 IgniteException (org.apache.ignite.IgniteException)6 HashMap (java.util.HashMap)5 List (java.util.List)5 Map (java.util.Map)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 IgniteCache (org.apache.ignite.IgniteCache)5 QueryCursor (org.apache.ignite.cache.query.QueryCursor)5 T2 (org.apache.ignite.internal.util.typedef.T2)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 Transaction (org.apache.ignite.transactions.Transaction)4