Search in sources :

Example 1 with GridAbsPredicate

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

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatesAndQueryStart.

/**
     * @param atomicityMode Cache atomicity mode.
     * @throws Exception If failed.
     */
private void concurrentUpdatesAndQueryStart(CacheAtomicityMode atomicityMode) throws Exception {
    Ignite srv = startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setAtomicityMode(atomicityMode);
    IgniteCache clientCache = client.createCache(ccfg);
    Affinity<Integer> aff = srv.affinity(DEFAULT_CACHE_NAME);
    final List<Integer> keys = new ArrayList<>();
    final int KEYS = 10;
    for (int i = 0; i < 100_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() == KEYS)
                break;
        }
    }
    assertEquals(KEYS, keys.size());
    final int THREADS = 10;
    final int UPDATES = 1000;
    for (int i = 0; i < 5; i++) {
        log.info("Iteration: " + i);
        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
        final AtomicInteger evtCnt = new AtomicInteger();
        qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                for (CacheEntryEvent evt : evts) {
                    assertNotNull(evt.getKey());
                    assertNotNull(evt.getValue());
                    if ((Integer) evt.getValue() >= 0)
                        evtCnt.incrementAndGet();
                }
            }
        });
        QueryCursor cur;
        final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
        final AtomicBoolean stop = new AtomicBoolean();
        try {
            IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (!stop.get()) srvCache.put(keys.get(rnd.nextInt(KEYS)), rnd.nextInt(100) - 200);
                    return null;
                }
            }, THREADS, "update");
            U.sleep(1000);
            cur = clientCache.query(qry);
            U.sleep(1000);
            stop.set(true);
            fut.get();
        } finally {
            stop.set(true);
        }
        GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
                return null;
            }
        }, THREADS, "update");
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                log.info("Events: " + evtCnt.get());
                return evtCnt.get() >= THREADS * UPDATES;
            }
        }, 5000);
        assertEquals(THREADS * UPDATES, evtCnt.get());
        cur.close();
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with GridAbsPredicate

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

the class CacheMetricsEnableRuntimeTest method testCacheManagerStatisticsEnable.

/**
 * @throws Exception If failed.
 */
public void testCacheManagerStatisticsEnable() throws Exception {
    final CacheManager mgr1 = Caching.getCachingProvider().getCacheManager();
    final CacheManager mgr2 = Caching.getCachingProvider().getCacheManager();
    CacheConfiguration cfg1 = new CacheConfiguration().setName(CACHE1).setGroupName(GROUP).setCacheMode(CacheMode.PARTITIONED).setAtomicityMode(CacheAtomicityMode.ATOMIC);
    mgr1.createCache(CACHE1, cfg1);
    CacheConfiguration cfg2 = new CacheConfiguration(cfg1).setName(CACHE2).setStatisticsEnabled(true);
    mgr1.createCache(CACHE2, cfg2);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return !isStatisticsEnabled(mgr1, CACHE1) && !isStatisticsEnabled(mgr2, CACHE1) && isStatisticsEnabled(mgr1, CACHE2) && isStatisticsEnabled(mgr2, CACHE2);
        }
    }, WAIT_CONDITION_TIMEOUT));
    mgr1.enableStatistics(CACHE1, true);
    mgr2.enableStatistics(CACHE2, false);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return isStatisticsEnabled(mgr1, CACHE1) && isStatisticsEnabled(mgr2, CACHE1) && !isStatisticsEnabled(mgr1, CACHE2) && !isStatisticsEnabled(mgr2, CACHE2);
        }
    }, WAIT_CONDITION_TIMEOUT));
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheManager(javax.cache.CacheManager) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 3 with GridAbsPredicate

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

the class CacheMetricsEnableRuntimeTest method testJmxStatisticsEnable.

/**
 * @param persistence Persistence.
 */
private void testJmxStatisticsEnable(boolean persistence) throws Exception {
    this.persistence = persistence;
    Ignite ig1 = startGrid(1);
    Ignite ig2 = startGrid(2);
    ig1.active(true);
    CacheConfiguration ccfg = ig1.cache(CACHE1).getConfiguration(CacheConfiguration.class);
    CacheConfiguration cacheCfg2 = new CacheConfiguration(ccfg);
    cacheCfg2.setName(CACHE2);
    cacheCfg2.setStatisticsEnabled(true);
    ig2.getOrCreateCache(cacheCfg2);
    CacheMetricsMXBean mxBeanCache1 = mxBean(2, CACHE1, CacheClusterMetricsMXBeanImpl.class);
    CacheMetricsMXBean mxBeanCache2 = mxBean(2, CACHE2, CacheClusterMetricsMXBeanImpl.class);
    CacheMetricsMXBean mxBeanCache1loc = mxBean(2, CACHE1, CacheLocalMetricsMXBeanImpl.class);
    mxBeanCache1.enableStatistics();
    mxBeanCache2.disableStatistics();
    assertCachesStatisticsMode(true, false);
    stopGrid(1);
    startGrid(3);
    assertCachesStatisticsMode(true, false);
    mxBeanCache1loc.disableStatistics();
    assertCachesStatisticsMode(false, false);
    mxBeanCache1.enableStatistics();
    mxBeanCache2.enableStatistics();
    // Start node 1 again.
    startGrid(1);
    assertCachesStatisticsMode(true, true);
    int gridIdx = 0;
    for (Ignite ignite : G.allGrids()) {
        gridIdx++;
        ignite.cache(CACHE1).put(gridIdx, gridIdx);
        ignite.cache(CACHE2).put(gridIdx, gridIdx);
        ignite.cache(CACHE1).get(gridIdx);
        ignite.cache(CACHE2).get(gridIdx);
    }
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            int cnt = G.allGrids().size();
            for (Ignite ignite : G.allGrids()) {
                CacheMetrics metrics1 = ignite.cache(CACHE1).metrics();
                CacheMetrics metrics2 = ignite.cache(CACHE2).metrics();
                if (metrics1.getCacheGets() < cnt || metrics2.getCacheGets() < cnt || metrics1.getCachePuts() < cnt || metrics2.getCachePuts() < cnt)
                    return false;
            }
            return true;
        }
    }, 10_000L));
    stopAllGrids();
    ig1 = startGrid(1);
    ig1.active(true);
    ig1.getOrCreateCache(cacheCfg2.setStatisticsEnabled(false));
    if (persistence)
        // cache1 - from local configuration, cache2 - restored from pds
        assertCachesStatisticsMode(false, true);
    else
        assertCachesStatisticsMode(false, false);
    mxBeanCache1 = mxBean(1, CACHE1, CacheLocalMetricsMXBeanImpl.class);
    mxBeanCache2 = mxBean(1, CACHE2, CacheLocalMetricsMXBeanImpl.class);
    mxBeanCache1.enableStatistics();
    mxBeanCache2.disableStatistics();
    startGrid(2);
    assertCachesStatisticsMode(true, false);
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheMetricsMXBean(org.apache.ignite.mxbean.CacheMetricsMXBean) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 4 with GridAbsPredicate

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

the class IgfsAbstractSelfTest method testAppendDeleteParentNoClose.

/**
 * Test delete on the file parent when it was opened for write(append) and is not closed yet.
 *
 * @throws Exception If failed.
 */
public void testAppendDeleteParentNoClose() throws Exception {
    if (mode != PRIMARY)
        return;
    if (appendSupported()) {
        create(igfs, paths(DIR, SUBDIR), null);
        createFile(igfs, FILE, false);
        IgfsOutputStream os = null;
        IgniteUuid id = null;
        try {
            id = igfs.context().meta().fileId(FILE);
            os = igfs.append(FILE, false);
            // Since GG-4911 we allow deletes in this case.
            boolean del = igfs.delete(SUBDIR, true);
            assertTrue(del);
            assertFalse(igfs.exists(FILE));
            // id still exists in meta cache since
            assertTrue(igfs.context().meta().exists(id));
            // it is locked for writing and just moved to TRASH.
            // Delete worker cannot delete it for that reason.
            os.write(chunk);
            os.close();
        } finally {
            U.closeQuiet(os);
        }
        assert id != null;
        final IgniteUuid id0 = id;
        // Delete worker should delete the file once its output stream is finally closed:
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                try {
                    return !igfs.context().meta().exists(id0);
                } catch (IgniteCheckedException ice) {
                    throw new IgniteException(ice);
                }
            }
        }, 5_000L);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteException(org.apache.ignite.IgniteException) IgfsOutputStream(org.apache.ignite.igfs.IgfsOutputStream)

Example 5 with GridAbsPredicate

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

the class IgfsAbstractSelfTest method testFormat.

/**
 * Ensure that formatting is not propagated to the secondary file system.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings("ConstantConditions")
public void testFormat() throws Exception {
    if (mode == PROXY)
        return;
    final GridCacheAdapter<IgfsBlockKey, byte[]> dataCache = getDataCache(igfs);
    assert dataCache != null;
    int size0 = dataCache.size(new CachePeekMode[] { CachePeekMode.ALL });
    assert size0 == 0 : "Initial data cache size = " + size0;
    if (dual)
        create(igfsSecondary, paths(DIR, SUBDIR, DIR_NEW, SUBDIR_NEW), paths(FILE, FILE_NEW));
    create(igfs, paths(DIR, SUBDIR), paths(FILE));
    try (IgfsOutputStream os = igfs.create(FILE, true)) {
        os.write(new byte[10 * 1024 * 1024]);
    }
    awaitFileClose(igfs, FILE);
    if (dual)
        checkExist(igfsSecondary, DIR, SUBDIR, FILE, DIR_NEW, SUBDIR_NEW, FILE_NEW);
    checkExist(igfs, DIR, SUBDIR, FILE);
    assertEquals(10 * 1024 * 1024, igfs.info(FILE).length());
    assert dataCache.size(new CachePeekMode[] { CachePeekMode.ALL }) > 0;
    igfs.clear();
    // Ensure format is not propagated to the secondary file system.
    if (dual) {
        checkExist(igfsSecondary, DIR, SUBDIR, FILE, DIR_NEW, SUBDIR_NEW, FILE_NEW);
        igfsSecondary.format();
    }
    // Ensure entries deletion in the primary file system.
    checkNotExist(igfs, DIR, SUBDIR, FILE);
    if (!GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            try {
                return dataCache.size(new CachePeekMode[] { CachePeekMode.ALL }) == 0;
            } catch (IgniteCheckedException ice) {
                throw new IgniteException(ice);
            }
        }
    }, 10_000)) {
        Iterable<? extends GridCacheEntryEx> entries = dataCache.allEntries();
        for (GridCacheEntryEx e : entries) {
            X.println("deleted = " + e.deleted());
            X.println("detached = " + e.detached());
            X.println("info = " + e.info());
            X.println("k = " + e.key() + ", v = " + e.valueBytes());
        }
        assert false;
    }
}
Also used : GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CachePeekMode(org.apache.ignite.cache.CachePeekMode) IgniteException(org.apache.ignite.IgniteException) IgfsOutputStream(org.apache.ignite.igfs.IgfsOutputStream)

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