Search in sources :

Example 31 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class GridSelfTest method testAsyncListen.

/**
 * @throws Exception If failed.
 */
@Test
public void testAsyncListen() throws Exception {
    final String hello = "HELLO!";
    final String bye = "BYE!";
    final Ignite g = grid(0);
    final UUID locNodeId = g.cluster().localNode().id();
    g.message().remoteListen(null, new MessagingListenActor<String>() {

        @Override
        protected void receive(UUID nodeId, String rcvMsg) throws Throwable {
            if (hello.equals(rcvMsg)) {
                assertEquals(locNodeId, nodeId);
                assertEquals(hello, rcvMsg);
                stop(bye);
            }
        }
    });
    final AtomicInteger cnt = new AtomicInteger();
    g.message().localListen(null, new P2<UUID, String>() {

        @Override
        public boolean apply(UUID nodeId, String msg) {
            if (msg.equals(bye))
                cnt.incrementAndGet();
            return true;
        }
    });
    g.message().send(null, hello);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return cnt.get() == g.cluster().nodes().size();
        }
    }, 5000);
    assertEquals(cnt.get(), g.cluster().nodes().size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) Test(org.junit.Test) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest)

Example 32 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class GridProjectionForCachesSelfTest method testProjectionForNamedCache.

/**
 * @throws Exception If failed.
 */
@Test
public void testProjectionForNamedCache() throws Exception {
    final ClusterGroup prj = ignite.cluster().forCacheNodes(CACHE_NAME);
    assertNotNull(prj);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return prj.nodes().size() == 3;
        }
    }, 5000);
    assertEquals("Invalid projection: " + prj.nodes(), 3, prj.nodes().size());
    assert !prj.nodes().contains(grid(0).localNode());
    assert prj.nodes().contains(grid(1).localNode());
    assert prj.nodes().contains(grid(2).localNode());
    assert prj.nodes().contains(grid(3).localNode());
    assert !prj.nodes().contains(grid(4).localNode());
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 33 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class GridCacheAbstractFullApiSelfTest method testPeekExpiredTx.

/**
 * @throws Exception If failed.
 */
@Test
public void testPeekExpiredTx() throws Exception {
    if (txShouldBeUsed()) {
        final IgniteCache<String, Integer> c = jcache();
        final String key = "1";
        int ttl = 500;
        try (Transaction tx = grid(0).transactions().txStart()) {
            final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
            grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
            tx.commit();
        }
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return peek(c, key) == null;
            }
        }, 2000);
        assertNull(peek(c, key));
        assert c.localSize() == 0;
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) Test(org.junit.Test)

Example 34 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class IgniteClientCacheStartFailoverTest method testRebalanceStateConcurrentStart.

/**
 * @throws Exception If failed.
 */
@Test
public void testRebalanceStateConcurrentStart() throws Exception {
    final int SRVS1 = 3;
    final int CLIENTS = 5;
    final int SRVS2 = 5;
    startGrids(SRVS1);
    Ignite srv0 = ignite(0);
    final int KEYS = 1000;
    final List<String> cacheNames = startCaches(srv0, KEYS);
    final List<Ignite> clients = new ArrayList<>();
    for (int i = 0; i < CLIENTS; i++) clients.add(startClientGrid(SRVS1 + i));
    final CyclicBarrier barrier = new CyclicBarrier(clients.size() + SRVS2);
    final AtomicInteger clientIdx = new AtomicInteger();
    final Set<Integer> keys = new HashSet<>();
    for (int i = 0; i < KEYS; i++) keys.add(i);
    IgniteInternalFuture<?> fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            barrier.await();
            Ignite client = clients.get(clientIdx.getAndIncrement());
            for (String cacheName : cacheNames) client.cache(cacheName);
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            for (int i = 0; i < 10; i++) {
                for (String cacheName : cacheNames) {
                    IgniteCache<Object, Object> cache = client.cache(cacheName);
                    Map<Object, Object> map0 = cache.getAll(keys);
                    assertEquals("[cache=" + cacheName + ", expected=" + KEYS + ", actual=" + map0.size() + ']', KEYS, map0.size());
                    int key = rnd.nextInt(KEYS);
                    try {
                        cache.put(key, i);
                    } catch (CacheException e) {
                        log.error("It couldn't put a value [cache=" + cacheName + ", key=" + key + ", val=" + i + ']', e);
                        CacheConfiguration ccfg = cache.getConfiguration(CacheConfiguration.class);
                        TransactionSerializationException txEx = X.cause(e, TransactionSerializationException.class);
                        boolean notContains = !txEx.getMessage().contains("Cannot serialize transaction due to write conflict (transaction is marked for rollback)");
                        if (txEx == null || ccfg.getAtomicityMode() != TRANSACTIONAL_SNAPSHOT || notContains)
                            fail("Assert violated because exception was thrown [e=" + e.getMessage() + ']');
                    }
                }
            }
            return null;
        }
    }, clients.size(), "client-cache-start");
    final AtomicInteger srvIdx = new AtomicInteger(SRVS1 + CLIENTS);
    IgniteInternalFuture<?> fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            barrier.await();
            startGrid(srvIdx.incrementAndGet());
            return null;
        }
    }, SRVS2, "node-start");
    fut1.get();
    fut2.get();
    final AffinityTopologyVersion topVer = new AffinityTopologyVersion(SRVS1 + SRVS2 + CLIENTS, 1);
    for (Ignite client : clients) {
        for (String cacheName : cacheNames) {
            final GridDhtPartitionTopology top = ((IgniteKernal) client).context().cache().internalCache(cacheName).context().topology();
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    return top.rebalanceFinished(topVer);
                }
            }, 5000);
            assertTrue(top.rebalanceFinished(topVer));
        }
    }
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CacheException(javax.cache.CacheException) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) ArrayList(java.util.ArrayList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) HashSet(java.util.HashSet) IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) CacheException(javax.cache.CacheException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TreeMap(java.util.TreeMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 35 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class IgniteCacheStoreValueAbstractTest method testValueNotStored.

/**
 * @throws Exception If failed.
 */
@Test
public void testValueNotStored() throws Exception {
    cpyOnRead = true;
    startGrids();
    IgniteCache<TestKey, TestValue> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    Affinity<Object> aff = grid(0).affinity(DEFAULT_CACHE_NAME);
    final List<WeakReference<Object>> refs = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        TestKey key = new TestKey(i);
        TestValue val = new TestValue(i);
        refs.add(new WeakReference<Object>(val));
        cache.put(key, val);
        checkNoValue(aff, key);
        for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
        checkNoValue(aff, key);
        cache.invoke(key, new CacheEntryProcessor<TestKey, TestValue, Object>() {

            @Override
            public Object process(MutableEntry<TestKey, TestValue> entry, Object... args) {
                assertNotNull(entry.getValue());
                entry.setValue(new TestValue(10_000));
                return new TestValue(20_000);
            }
        });
        checkNoValue(aff, key);
        for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
        checkNoValue(aff, key);
        cache.remove(key);
        for (int g = 0; g < gridCount(); g++) assertNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
        try (IgniteDataStreamer<TestKey, TestValue> streamer = grid(0).dataStreamer(DEFAULT_CACHE_NAME)) {
            streamer.addData(key, val);
        }
        checkNoValue(aff, key);
        cache.remove(key);
        try (IgniteDataStreamer<TestKey, TestValue> streamer = grid(0).dataStreamer(DEFAULT_CACHE_NAME)) {
            streamer.allowOverwrite(true);
            streamer.addData(key, val);
        }
        checkNoValue(aff, key);
        if (aff.isPrimaryOrBackup(grid(0).localNode(), key)) {
            cache.localEvict(Collections.singleton(key));
            assertNull(cache.localPeek(key, CachePeekMode.ONHEAP));
            cache.get(key);
            assertNotNull(cache.localPeek(key, CachePeekMode.ONHEAP));
            checkNoValue(aff, key);
        }
    }
    // Should load TestKey(100_000).
    cache.loadCache(null);
    TestKey key = new TestKey(100_000);
    checkNoValue(aff, key);
    for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
    checkNoValue(aff, key);
    boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            System.gc();
            boolean pass = true;
            for (Iterator<WeakReference<Object>> it = refs.iterator(); it.hasNext(); ) {
                WeakReference<Object> ref = it.next();
                if (ref.get() == null)
                    it.remove();
                else {
                    pass = false;
                    log.info("Not collected value: " + ref.get());
                }
            }
            return pass;
        }
    }, 60_000);
    assertTrue("Failed to wait for when values are collected", wait);
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ArrayList(java.util.ArrayList) WeakReference(java.lang.ref.WeakReference) Iterator(java.util.Iterator) Test(org.junit.Test)

Aggregations

GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)229 Ignite (org.apache.ignite.Ignite)109 Test (org.junit.Test)102 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)65 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)52 ClusterNode (org.apache.ignite.cluster.ClusterNode)37 IgniteEx (org.apache.ignite.internal.IgniteEx)34 IgniteException (org.apache.ignite.IgniteException)31 ArrayList (java.util.ArrayList)29 CountDownLatch (java.util.concurrent.CountDownLatch)28 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)28 Transaction (org.apache.ignite.transactions.Transaction)25 Map (java.util.Map)22 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)21 IgniteCache (org.apache.ignite.IgniteCache)19 IgniteKernal (org.apache.ignite.internal.IgniteKernal)19 HashMap (java.util.HashMap)17 Duration (javax.cache.expiry.Duration)15 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)13 UUID (java.util.UUID)12