Search in sources :

Example 31 with PA

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

the class IgniteCacheGroupsTest method continuousQuery.

/**
 * @param cacheMode Cache mode.
 * @param atomicityMode Cache atomicity mode.
 * @throws Exception If failed.
 */
private void continuousQuery(CacheMode cacheMode, CacheAtomicityMode atomicityMode) throws Exception {
    final int keys = 10_000;
    Integer[] data1 = generateData(keys);
    Integer[] data2 = generateData(keys);
    boolean loc = cacheMode == LOCAL;
    if (loc)
        startGrid(0);
    else
        startGridsMultiThreaded(4);
    Ignite srv0 = ignite(0);
    srv0.createCache(cacheConfiguration(GROUP1, CACHE1, cacheMode, atomicityMode, 2, false));
    srv0.createCache(cacheConfiguration(GROUP1, CACHE2, cacheMode, atomicityMode, 2, false));
    final AtomicInteger cntr1 = new AtomicInteger();
    final AtomicInteger cntr2 = new AtomicInteger();
    CacheEntryUpdatedListener lsnr1 = new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> ignored : evts) cntr1.incrementAndGet();
        }
    };
    CacheEntryUpdatedListener lsnr2 = new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> ignored : evts) cntr2.incrementAndGet();
        }
    };
    QueryCursor qry1 = ignite(loc ? 0 : 2).cache(CACHE1).query(new ContinuousQuery<>().setLocalListener(lsnr1));
    QueryCursor qry2 = ignite(loc ? 0 : 3).cache(CACHE2).query(new ContinuousQuery<>().setLocalListener(lsnr2));
    if (atomicityMode == TRANSACTIONAL) {
        Ignite ignite = ignite(loc ? 0 : 1);
        IgniteCache<Integer, Integer> cache1 = ignite.cache(CACHE1);
        IgniteCache<Integer, Integer> cache2 = ignite.cache(CACHE2);
        try (Transaction tx = ignite.transactions().txStart()) {
            for (int i = 0; i < keys; i++) {
                cache1.put(i, data1[i]);
                cache2.put(i, data2[i]);
            }
            tx.commit();
        }
    } else {
        int ldrs = 4;
        List<Callable<?>> cls = new ArrayList<>(ldrs * 2);
        for (int i = 0; i < ldrs; i++) {
            cls.add(putOperation(loc ? 0 : 1, ldrs, i, CACHE1, data1));
            cls.add(putOperation(loc ? 0 : 2, ldrs, i, CACHE2, data2));
        }
        GridTestUtils.runMultiThreaded(cls, "loaders");
    }
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return cntr1.get() == keys && cntr2.get() == keys;
        }
    }, 2000);
    assertEquals(cntr1.get(), keys);
    assertEquals(cntr2.get(), keys);
    qry1.close();
    Map<Integer, Integer> map = generateDataMap(10);
    srv0.cache(CACHE1).putAll(map);
    srv0.cache(CACHE2).putAll(map);
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return cntr2.get() == keys + 10;
        }
    }, 2000);
    assertEquals(keys + 10, cntr2.get());
    assertEquals(keys, cntr1.get());
    qry2.close();
}
Also used : ArrayList(java.util.ArrayList) GridPlainCallable(org.apache.ignite.internal.util.lang.GridPlainCallable) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 32 with PA

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

the class IgniteCacheReadThroughEvictionSelfTest method destroyCacheSafe.

/**
 * @param ig Ignite.
 * @param cacheName Cache name to destroy.
 * @throws IgniteCheckedException If failed.
 */
private void destroyCacheSafe(Ignite ig, final String cacheName) throws IgniteCheckedException {
    ig.destroyCache(cacheName);
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            for (Ignite g : G.allGrids()) {
                IgniteInternalCache<Object, Object> cache = ((IgniteEx) g).context().cache().cache(cacheName);
                if (cache != null)
                    return false;
            }
            return true;
        }
    }, getTestTimeout());
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) Ignite(org.apache.ignite.Ignite)

Example 33 with PA

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

the class IgniteCacheReadThroughEvictionSelfTest method testReadThroughEvictionPolicy.

/**
 * @throws Exception if failed.
 */
public void testReadThroughEvictionPolicy() throws Exception {
    Ignite ig = testedGrid();
    CacheConfiguration<Object, Object> cc = variationConfig("eviction");
    cc.setEvictionPolicy(new FifoEvictionPolicy(1));
    cc.setOnheapCacheEnabled(true);
    final IgniteCache<Object, Object> cache = ig.createCache(cc);
    try {
        for (int i = 0; i < KEYS; i++) cache.put(key(i), value(i));
        assertTrue(GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                int size = cache.size(CachePeekMode.ONHEAP);
                int offheapSize = cache.size(CachePeekMode.OFFHEAP);
                System.out.println("Cache [onHeap=" + size + ", offHeap=" + offheapSize + ']');
                return size <= testsCfg.gridCount();
            }
        }, 30_000));
        for (int i = 0; i < KEYS; i++) assertEquals(value(i), cache.get(key(i)));
    } finally {
        destroyCacheSafe(ig, cc.getName());
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) Ignite(org.apache.ignite.Ignite) FifoEvictionPolicy(org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicy)

Example 34 with PA

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

the class IgniteCacheReadThroughEvictionSelfTest method waitEmpty.

/**
 * @throws Exception if failed.
 */
private void waitEmpty(final String name) throws Exception {
    boolean success = GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            for (Ignite g : G.allGrids()) {
                GridCacheAdapter<Object, Object> cache = ((IgniteEx) g).context().cache().internalCache(name);
                if (cache == null)
                    return true;
                if (!cache.isEmpty())
                    return false;
            }
            return true;
        }
    }, getTestTimeout());
    assertTrue("Failed to wait for the cache to be empty", success);
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) Ignite(org.apache.ignite.Ignite)

Example 35 with PA

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

the class GridCacheNearEvictionSelfTest method testNearEnabledThreeNodes.

/**
 * @throws Exception If failed.
 */
public void testNearEnabledThreeNodes() throws Exception {
    gridCnt = 3;
    startGridsMultiThreaded(gridCnt);
    try {
        final int cnt = 100;
        grid(0).compute().broadcast(new IgniteCallable<Object>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public Object call() throws Exception {
                IgniteCache<Integer, String> c = ignite.cache(DEFAULT_CACHE_NAME);
                for (int i = 0; i < cnt; i++) c.put(i, Integer.toString(i));
                return true;
            }
        });
        for (int i = 0; i < gridCnt; i++) {
            final GridCacheAdapter cache = internalCache(i);
            final GridCacheAdapter near = near(i);
            // Repeatedly check cache sizes because of concurrent cache updates.
            assertTrue(GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    // Every node contains either near, backup, or primary.
                    return cnt == cache.size() + near.nearSize();
                }
            }, getTestTimeout()));
            int keySize = near(i).nearSize();
            assert keySize < cnt : "Key size is not less than count [cnt=" + cnt + ", size=" + keySize + ']';
        }
    } finally {
        stopAllGrids();
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite)

Aggregations

PA (org.apache.ignite.internal.util.typedef.PA)55 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 Ignite (org.apache.ignite.Ignite)20 ArrayList (java.util.ArrayList)14 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)13 UUID (java.util.UUID)9 CacheEntryEvent (javax.cache.event.CacheEntryEvent)9 IgniteException (org.apache.ignite.IgniteException)7 QueryCursor (org.apache.ignite.cache.query.QueryCursor)7 List (java.util.List)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 IgniteCache (org.apache.ignite.IgniteCache)5 IgniteEx (org.apache.ignite.internal.IgniteEx)5 T2 (org.apache.ignite.internal.util.typedef.T2)5 Transaction (org.apache.ignite.transactions.Transaction)5 Callable (java.util.concurrent.Callable)4