Search in sources :

Example 66 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSameQuery.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testMultiThreadedSameQuery() throws Exception {
    int threadCnt = 50;
    final int keyCnt = 10;
    final int logMod = 5000;
    final Ignite g = grid(0);
    // Put test values into cache.
    final IgniteCache<Integer, Integer> c = cache(Integer.class, Integer.class);
    for (int i = 0; i < keyCnt; i++) {
        c.put(i, i);
        c.localEvict(Arrays.asList(i));
    }
    final AtomicInteger cnt = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            int iter = 0;
            while (!done.get() && !Thread.currentThread().isInterrupted()) {
                iter++;
                Collection<Cache.Entry<Integer, Integer>> entries = c.query(new SqlQuery(Integer.class, "_val >= 0")).getAll();
                assert entries != null;
                assertEquals("Query results [entries=" + entries + ", aff=" + affinityNodes(entries, g) + ", iteration=" + iter + ']', keyCnt, entries.size());
                if (cnt.incrementAndGet() % logMod == 0) {
                    GridCacheQueryManager<Object, Object> qryMgr = ((IgniteKernal) g).internalCache(c.getName()).context().queries();
                    assert qryMgr != null;
                    qryMgr.printMemoryStats();
                }
            }
        }
    }, threadCnt);
    Thread.sleep(DURATION);
    info("Finishing test...");
    done.set(true);
    fut.get();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Example 67 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IntMaxValueEntriesTest method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    final IgniteCache<Integer, Object> cache = cache();
    final IgniteDataStreamer<Integer, Object> stmr = ignite().dataStreamer(cache.getName());
    final List<Thread> threads = new ArrayList<>(THREADS);
    final LongAdder addedCnt = new LongAdder();
    int delta = (int) ((KEYS_HI + Math.abs(KEYS_LO)) / THREADS);
    System.out.println("Delta: " + delta);
    for (int i = 0; i < THREADS; i++) {
        final int lo = i == 0 ? KEYS_LO : delta * i + 1;
        final int hi = i == THREADS - 1 ? (int) KEYS_HI : (int) ((long) delta * (i + 1));
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                byte val = (byte) rnd.nextInt();
                println("Start from " + lo + " to " + hi);
                for (int j = lo, k = 0; j < hi; j++, k++) {
                    stmr.addData(j, val++);
                    addedCnt.increment();
                    if (k % REPORT_DELTA == 0)
                        println(addedCnt.sum() + " entries");
                }
                println("Thread finished. " + addedCnt.sum() + " entries.");
            }
        });
        threads.add(t);
        t.start();
    }
    for (Thread thread : threads) thread.join();
    println("All threads finished. " + addedCnt.sum() + " entries.");
    println("Streamer flush");
    stmr.flush();
    println("Streamer flushed");
    println("Calculating cache size");
    println("Cache size: " + cache.size());
    println("Calculating long cache size");
    println("Cache size long: " + cache.sizeLong());
    Thread.sleep(10000);
    println("Iterating started");
    long cnt = 0;
    for (Cache.Entry<Integer, Object> ignored : cache) {
        cnt++;
        if (cnt > 0 && cnt % REPORT_DELTA == 0)
            println("Iterated via " + cnt + " entries");
    }
    println("Iterated via " + cnt + " entries");
    cache.destroy();
    return true;
}
Also used : ArrayList(java.util.ArrayList) LongAdder(java.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 68 with Cache

use of javax.cache.Cache in project ignite by apache.

the class GridCacheContinuousQueryAbstractSelfTest method testInitialQueryAndUpdates.

/**
 * @throws Exception If failed.
 */
@Test
public void testInitialQueryAndUpdates() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setInitialQuery(new ScanQuery<>(new P2<Integer, Integer>() {

        @Override
        public boolean apply(Integer k, Integer v) {
            return k >= 5;
        }
    }));
    final Map<Integer, Integer> map = new ConcurrentHashMap<>();
    final CountDownLatch latch = new CountDownLatch(2);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                map.put(e.getKey(), e.getValue());
                latch.countDown();
            }
        }
    });
    for (int i = 0; i < 10; i++) cachePut(cache, i, i);
    try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry)) {
        List<Cache.Entry<Integer, Integer>> res = cur.getAll();
        Collections.sort(res, new Comparator<Cache.Entry<Integer, Integer>>() {

            @Override
            public int compare(Cache.Entry<Integer, Integer> e1, Cache.Entry<Integer, Integer> e2) {
                return e1.getKey().compareTo(e2.getKey());
            }
        });
        assertEquals(5, res.size());
        int exp = 5;
        for (Cache.Entry<Integer, Integer> e : res) {
            assertEquals(exp, e.getKey().intValue());
            assertEquals(exp, e.getValue().intValue());
            exp++;
        }
        cachePut(cache, 10, 10);
        cachePut(cache, 11, 11);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS) : latch.getCount();
        assertEquals(2, map.size());
        for (int i = 11; i < 12; i++) assertEquals(i, (int) map.get(i));
    }
}
Also used : P2(org.apache.ignite.internal.util.typedef.P2) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 69 with Cache

use of javax.cache.Cache in project ignite by apache.

the class TcpDiscoveryMultiThreadedTest method testCustomEventNodeRestart.

/**
 * @throws Exception If failed.
 */
@Ignore("https://issues.apache.org/jira/browse/IGNITE-10249")
@Test
public void testCustomEventNodeRestart() throws Exception {
    clientFlagGlobal = false;
    Ignite ignite = startGrid(0);
    ignite.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
    final long stopTime = System.currentTimeMillis() + 60_000;
    GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {

        @Override
        public void apply(Integer idx) {
            try {
                while (System.currentTimeMillis() < stopTime) {
                    Ignite ignite = startGrid(idx + 1);
                    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
                    int qryCnt = ThreadLocalRandom.current().nextInt(10) + 1;
                    for (int i = 0; i < qryCnt; i++) {
                        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                        qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                            @Override
                            public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                            // No-op.
                            }
                        });
                        QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry);
                        cur.close();
                    }
                    GridTestUtils.invoke(ignite.configuration().getDiscoverySpi(), "simulateNodeFailure");
                    ignite.close();
                }
            } catch (Exception e) {
                log.error("Unexpected error: " + e, e);
                throw new IgniteException(e);
            }
        }
    }, 5, "node-restart");
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) Ignore(org.junit.Ignore) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 70 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteBinaryObjectFieldsQuerySelfTest method checkQuery.

/**
 * @throws Exception If failed.
 */
private void checkQuery(CacheMode cacheMode, CacheAtomicityMode atomicity) throws Exception {
    IgniteCache<Object, Object> cache = grid(GRID_CNT - 1).getOrCreateCache(cache(cacheMode, atomicity));
    try {
        populate(cache);
        QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(new SqlQuery("Person", "order " + "by id asc"));
        List<Cache.Entry<Object, Object>> all = cur.getAll();
        assertEquals(100, all.size());
        for (int i = 0; i < 100; i++) {
            Object person = all.get(i).getValue();
            assertEquals(Integer.valueOf(i), U.field(person, "id"));
            assertEquals("person-" + i, U.field(person, "name"));
            assertEquals("person-last-" + i, U.field(person, "lastName"));
            assertEquals((double) (i * 25), U.field(person, "salary"));
        }
        int max = 49;
        // Check local scan query with keepBinary flag set.
        ScanQuery<BinaryObject, BinaryObject> scanQry = new ScanQuery<>(new PersonKeyFilter(max));
        QueryCursor<Cache.Entry<BinaryObject, BinaryObject>> curs = grid(GRID_CNT - 1).cache(DEFAULT_CACHE_NAME).withKeepBinary().query(scanQry);
        List<Cache.Entry<BinaryObject, BinaryObject>> records = curs.getAll();
        assertEquals(50, records.size());
        for (Cache.Entry<BinaryObject, BinaryObject> entry : records) {
            BinaryObject key = entry.getKey();
            assertTrue(key.<Integer>field("id") <= max);
            assertEquals(PERSON_KEY_CLS_NAME, key.deserialize().getClass().getName());
        }
    } finally {
        grid(GRID_CNT - 1).cache(DEFAULT_CACHE_NAME).removeAll();
        grid(GRID_CNT - 1).destroyCache(DEFAULT_CACHE_NAME);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) ScanQuery(org.apache.ignite.cache.query.ScanQuery) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Aggregations

Cache (javax.cache.Cache)271 IgniteCache (org.apache.ignite.IgniteCache)157 Test (org.junit.Test)130 Ignite (org.apache.ignite.Ignite)101 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 List (java.util.List)62 Map (java.util.Map)56 ScanQuery (org.apache.ignite.cache.query.ScanQuery)54 ArrayList (java.util.ArrayList)51 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)45 QueryCursor (org.apache.ignite.cache.query.QueryCursor)43 HashMap (java.util.HashMap)41 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)41 Collection (java.util.Collection)38 HashSet (java.util.HashSet)38 CacheManager (javax.cache.CacheManager)38 CacheException (javax.cache.CacheException)35 IgniteEx (org.apache.ignite.internal.IgniteEx)35 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)32