Search in sources :

Example 56 with Cache

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

the class CacheMvccBasicContinuousQueryTest method testCachingMaxSize.

/**
 * @throws Exception If failed.
 */
@Test
public void testCachingMaxSize() throws Exception {
    Ignite node = startGrids(1);
    final IgniteCache cache = node.createCache(cacheConfiguration(cacheMode(), FULL_SYNC, 1, 2).setCacheMode(CacheMode.PARTITIONED).setIndexedTypes(Integer.class, Integer.class));
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
        // No-op.
        }
    });
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
                try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    for (int i = 0; i < TX_SIZE_THRESHOLD + 1; i++) cache.query(new SqlFieldsQuery("INSERT INTO Integer (_key, _val) values (" + i + ", 1)")).getAll();
                    tx.commit();
                }
            }
            return null;
        }
    }, CacheException.class, "Transaction is too large. Consider reducing transaction size");
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheException(javax.cache.CacheException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) Test(org.junit.Test)

Example 57 with Cache

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

the class ScanQueriesTopologyMappingTest method testLocalCacheQueryMapping.

/**
 */
@Test
public void testLocalCacheQueryMapping() throws Exception {
    IgniteEx ign0 = startGrid(0);
    IgniteCache<Object, Object> cache = ign0.createCache(new CacheConfiguration<>(GridAbstractTest.DEFAULT_CACHE_NAME).setCacheMode(CacheMode.LOCAL));
    cache.put(1, 2);
    startGrid(1);
    ScanQuery<Object, Object> qry = new ScanQuery<>();
    {
        List<Cache.Entry<Object, Object>> res0 = grid(0).cache(GridAbstractTest.DEFAULT_CACHE_NAME).query(qry).getAll();
        assertEquals(1, res0.size());
        assertEquals(1, res0.get(0).getKey());
        assertEquals(2, res0.get(0).getValue());
    }
    {
        List<Cache.Entry<Object, Object>> res1 = grid(1).cache(GridAbstractTest.DEFAULT_CACHE_NAME).query(qry).getAll();
        assertTrue(res1.isEmpty());
    }
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ArrayList(java.util.ArrayList) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 58 with Cache

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

the class ScanQueriesTopologyMappingTest method checkQueryWithRebalance.

/**
 */
private void checkQueryWithRebalance(CacheMode cacheMode) throws Exception {
    IgniteEx ign0 = startGrid(0);
    IgniteCache<Object, Object> cache0 = ign0.createCache(new CacheConfiguration<>(GridAbstractTest.DEFAULT_CACHE_NAME).setCacheMode(cacheMode));
    cache0.put(1, 2);
    blockRebalanceSupplyMessages(ign0, DEFAULT_CACHE_NAME, getTestIgniteInstanceName(1));
    startGrid(1);
    client = true;
    startGrid(10);
    int part = ign0.affinity(DEFAULT_CACHE_NAME).partition(1);
    for (int i = 0; i < 100; i++) {
        for (Ignite ign : G.allGrids()) {
            IgniteCache<Object, Object> cache = ign.cache(DEFAULT_CACHE_NAME);
            // check scan query
            List<Cache.Entry<Object, Object>> res = cache.query(new ScanQuery<>()).getAll();
            assertEquals(1, res.size());
            assertEquals(1, res.get(0).getKey());
            assertEquals(2, res.get(0).getValue());
            res = new ArrayList<>();
            // check iterator
            for (Cache.Entry<Object, Object> entry : cache) {
                res.add(entry);
            }
            assertEquals(1, res.size());
            assertEquals(1, res.get(0).getKey());
            assertEquals(2, res.get(0).getValue());
            // check scan query by partition
            res = cache.query(new ScanQuery<>(part)).getAll();
            assertEquals(1, res.size());
            assertEquals(1, res.get(0).getKey());
            assertEquals(2, res.get(0).getValue());
        }
        ScanQuery<Object, Object> qry = new ScanQuery<>().setLocal(true);
        {
            List<Cache.Entry<Object, Object>> res0 = grid(0).cache(DEFAULT_CACHE_NAME).query(qry).getAll();
            assertEquals(1, res0.size());
            assertEquals(1, res0.get(0).getKey());
            assertEquals(2, res0.get(0).getValue());
        }
        {
            List<Cache.Entry<Object, Object>> res1 = grid(1).cache(DEFAULT_CACHE_NAME).query(qry).getAll();
            assertTrue(res1.isEmpty());
        }
        {
            List<Cache.Entry<Object, Object>> res1 = grid(10).cache(DEFAULT_CACHE_NAME).query(qry).getAll();
            assertTrue(res1.isEmpty());
        }
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) ArrayList(java.util.ArrayList) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 59 with Cache

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

the class GridCacheContinuousQueryAbstractSelfTest method afterTest.

/**
 * {@inheritDoc}
 */
@Override
protected void afterTest() throws Exception {
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                if (grid(i).cluster().nodes().size() != gridCount())
                    return false;
            }
            return true;
        }
    }, 3000);
    for (int i = 0; i < gridCount(); i++) assertEquals(gridCount(), grid(i).cluster().nodes().size());
    for (int i = 0; i < gridCount(); i++) {
        for (int j = 0; j < 5; j++) {
            try {
                IgniteCache<Object, Object> cache = grid(i).cache(DEFAULT_CACHE_NAME);
                for (Cache.Entry<Object, Object> entry : cache.localEntries(new CachePeekMode[] { CachePeekMode.ALL })) cache.remove(entry.getKey());
                break;
            } catch (IgniteException e) {
                if (j == 4)
                    throw new Exception("Failed to clear cache for grid: " + i, e);
                U.warn(log, "Failed to clear cache for grid (will retry in 500 ms) [gridIdx=" + i + ", err=" + e.getMessage() + ']');
                U.sleep(500);
            }
        }
    }
    // Wait for all routines are unregistered
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                GridContinuousProcessor proc = grid(i).context().continuous();
                if (!((Map) U.field(proc, "rmtInfos")).isEmpty())
                    return false;
            }
            return true;
        }
    }, 3000);
    for (int i = 0; i < gridCount(); i++) {
        GridContinuousProcessor proc = grid(i).context().continuous();
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "locInfos")).size());
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "rmtInfos")).size());
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "startFuts")).size());
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "stopFuts")).size());
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "bufCheckThreads")).size());
        CacheContinuousQueryManager mgr = grid(i).context().cache().internalCache(DEFAULT_CACHE_NAME).context().continuousQueries();
        assertEquals(0, ((Map) U.field(mgr, "lsnrs")).size());
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteException(org.apache.ignite.IgniteException) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 60 with Cache

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

the class CacheKeepBinaryIterationTest method doTestLocalEntries.

/**
 * @param ccfg Cache configuration.
 */
private void doTestLocalEntries(final CacheConfiguration<Object, Object> ccfg, boolean keepBinary, boolean primitives) throws Exception {
    IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
    assertEquals(0, cache.size());
    try {
        for (int i = 0; i < KEYS; i++) if (primitives)
            cache.put(i, i);
        else
            cache.put(new QueryTestKey(i), new QueryTestValue(i));
        for (int i = 0; i < getServerNodeCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(ccfg.getName());
            if (keepBinary)
                cache0 = cache0.withKeepBinary();
            for (CachePeekMode mode : CachePeekMode.values()) {
                int size = 0;
                for (Cache.Entry<Object, Object> e : cache0.localEntries(mode)) {
                    Object key = e.getKey();
                    Object val = e.getValue();
                    if (!primitives) {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == key instanceof BinaryObject);
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == val instanceof BinaryObject);
                    } else {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, key instanceof Integer);
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, val instanceof Integer);
                    }
                    ++size;
                }
                if (mode == CachePeekMode.ALL || mode == CachePeekMode.PRIMARY || mode == CachePeekMode.BACKUP || (mode == CachePeekMode.NEAR && i == 0 && ccfg.getNearConfiguration() != null))
                    assertTrue("Zero result at mode: " + mode, size > 0);
            }
        }
    } finally {
        if (ccfg.getEvictionPolicy() != null) {
            // TODO: IGNITE-3462. Fixes evictionPolicy issues at cache destroy.
            stopAllGrids();
            startGridsMultiThreaded(getServerNodeCount());
        } else
            grid(0).destroyCache(ccfg.getName());
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) CachePeekMode(org.apache.ignite.cache.CachePeekMode) 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