Search in sources :

Example 51 with Cache

use of javax.cache.Cache in project hazelcast by hazelcast.

the class CacheExpirationManagerTest method expiration_task_starts_on_new_node_after_migration_when_there_is_expirable_entry.

@Test
public void expiration_task_starts_on_new_node_after_migration_when_there_is_expirable_entry() {
    Config config = getConfig();
    config.setProperty(taskPeriodSecondsPropName(), "1");
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    final HazelcastInstance node1 = factory.newHazelcastInstance(config);
    CacheManager cacheManager = createCacheManager(node1);
    Cache cache = cacheManager.createCache("test", new CacheConfig());
    ((ICache) cache).put(1, 1, new HazelcastExpiryPolicy(ONE_HOUR, ONE_HOUR, ONE_HOUR));
    final HazelcastInstance node2 = factory.newHazelcastInstance(config);
    node1.shutdown();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertTrue("There should be one ClearExpiredRecordsTask started", hasClearExpiredRecordsTaskStarted(node2));
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) CacheConfig(com.hazelcast.config.CacheConfig) ICache(com.hazelcast.cache.ICache) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastExpiryPolicy(com.hazelcast.cache.HazelcastExpiryPolicy) Cache(javax.cache.Cache) ICache(com.hazelcast.cache.ICache) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 52 with Cache

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

the class CacheDataPageScanQueryTest method testDataPageScanWithRestart.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("ConstantConditions")
@Test
@Ignore("https://issues.apache.org/jira/browse/IGNITE-11998")
public void testDataPageScanWithRestart() throws Exception {
    IgniteEx ignite = startGrid(0);
    ignite.cluster().active(true);
    IgniteInternalCache<Long, String> cache = ignite.cachex(CACHE);
    CacheGroupMetricsImpl metrics = cache.context().group().metrics();
    DataRegionMetricsImpl rmx = cache.context().dataRegion().metrics();
    long maxKey = 10_000;
    Map<Long, String> map = new ConcurrentHashMap<>();
    int threads = 16;
    AtomicInteger threadShift = new AtomicInteger();
    multithreaded((Callable<Void>) () -> {
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        int shift = threadShift.getAndIncrement();
        for (int i = shift; i < maxKey; i += threads) {
            Long k = (long) i;
            // Bigger than single page.
            String v = GridTestUtils.randomString(rnd, 6 * 1024);
            cache.put(k, v);
            map.put(k, v);
        }
        return null;
    }, threads);
    assertEquals(map.size(), cache.size());
    info("Page mem  : " + rmx.getPhysicalMemorySize());
    info("Alloc size: " + metrics.getTotalAllocatedSize());
    info("Store size: " + metrics.getStorageSize());
    HashMap<Long, String> map2 = new HashMap<>(map);
    IgniteCache<Long, String> c = ignite.cache(CACHE);
    for (Cache.Entry<Long, String> e : c.query(new ScanQuery<Long, String>()).getAll()) assertEquals(e.getValue(), map.remove(e.getKey()));
    assertTrue(map.isEmpty());
    assertTrue(CacheDataTree.isLastFindWithDataPageScan());
    stopAllGrids(true);
    ignite = startGrid(0);
    ignite.cluster().active(true);
    c = ignite.cache(CACHE);
    for (Cache.Entry<Long, String> e : c.query(new ScanQuery<Long, String>()).getAll()) assertEquals(e.getValue(), map2.remove(e.getKey()));
    assertTrue(map2.isEmpty());
    assertTrue(CacheDataTree.isLastFindWithDataPageScan());
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ScanQuery(org.apache.ignite.cache.query.ScanQuery) DataRegionMetricsImpl(org.apache.ignite.internal.processors.cache.persistence.DataRegionMetricsImpl) CacheGroupMetricsImpl(org.apache.ignite.internal.processors.cache.CacheGroupMetricsImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Cache(javax.cache.Cache) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) IgniteCache(org.apache.ignite.IgniteCache) Ignore(org.junit.Ignore) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 53 with Cache

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

the class H2IndexingAbstractGeoSelfTest method checkGeoMultithreaded.

/**
 * Check geo indexing multithreaded with dynamic index creation.
 *
 * @param dynamic Whether index should be created dynamically.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void checkGeoMultithreaded(boolean dynamic) throws Exception {
    final IgniteCache<Integer, EnemyCamp> cache1 = createCache("camp", true, Integer.class, EnemyCamp.class, dynamic);
    final IgniteCache<Integer, EnemyCamp> cache2 = grid(1).cache("camp");
    final IgniteCache<Integer, EnemyCamp> cache3 = grid(2).cache("camp");
    try {
        final String[] points = new String[CNT];
        WKTReader r = new WKTReader();
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        for (int idx = 0; idx < CNT; idx++) {
            int x = rnd.nextInt(1, 100);
            int y = rnd.nextInt(1, 100);
            cache1.getAndPut(idx, new EnemyCamp(r.read("POINT(" + x + " " + y + ")"), Integer.toString(idx)));
            points[idx] = Integer.toString(idx);
        }
        Thread.sleep(200);
        final AtomicBoolean stop = new AtomicBoolean();
        final AtomicReference<Exception> err = new AtomicReference<>();
        IgniteInternalFuture<?> putFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                WKTReader r = new WKTReader();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get()) {
                    int cacheIdx = rnd.nextInt(0, 3);
                    IgniteCache<Integer, EnemyCamp> cache = cacheIdx == 0 ? cache1 : cacheIdx == 1 ? cache2 : cache3;
                    int idx = rnd.nextInt(CNT);
                    int x = rnd.nextInt(1, 100);
                    int y = rnd.nextInt(1, 100);
                    cache.getAndPut(idx, new EnemyCamp(r.read("POINT(" + x + " " + y + ")"), Integer.toString(idx)));
                    U.sleep(50);
                }
                return null;
            }
        }, Runtime.getRuntime().availableProcessors(), "put-thread");
        IgniteInternalFuture<?> qryFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                WKTReader r = new WKTReader();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get()) {
                    try {
                        int cacheIdx = rnd.nextInt(0, 3);
                        IgniteCache<Integer, EnemyCamp> cache = cacheIdx == 0 ? cache1 : cacheIdx == 1 ? cache2 : cache3;
                        SqlQuery<Integer, EnemyCamp> qry = new SqlQuery<>(EnemyCamp.class, "coords && ?");
                        Collection<Cache.Entry<Integer, EnemyCamp>> res = cache.query(qry.setArgs(r.read("POLYGON((0 0, 0 100, 100 100, 100 0, 0 0))"))).getAll();
                        checkPoints(res, points);
                        U.sleep(5);
                    } catch (Exception e) {
                        err.set(e);
                        stop.set(true);
                        break;
                    }
                }
                return null;
            }
        }, 4, "qry-thread");
        U.sleep(6000L);
        stop.set(true);
        putFut.get();
        qryFut.get();
        Exception err0 = err.get();
        if (err0 != null)
            throw err0;
    } finally {
        destroy(cache1, grid(0), dynamic);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) WKTReader(org.locationtech.jts.io.WKTReader) ParseException(org.locationtech.jts.io.ParseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Collection(java.util.Collection) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 54 with Cache

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

the class IgniteCacheLocalQuerySelfTest method testQueryLocal.

/**
 * @throws Exception If test failed.
 */
@Test
public void testQueryLocal() throws Exception {
    // check for cached prepared statements this would fail.
    for (int i = 0; i < 2; i++) {
        IgniteCache<Integer, String> cache = jcache(Integer.class, String.class);
        cache.put(1, "value1");
        cache.put(2, "value2");
        cache.put(3, "value3");
        cache.put(4, "value4");
        cache.put(5, "value5");
        // Tests equals query.
        QueryCursor<Cache.Entry<Integer, String>> qry = cache.query(new SqlQuery<Integer, String>(String.class, "_val='value1'").setLocal(true));
        Iterator<Cache.Entry<Integer, String>> iter = qry.iterator();
        Cache.Entry<Integer, String> entry = iter.next();
        assert !iter.hasNext();
        assert entry != null;
        assert entry.getKey() == 1;
        assert "value1".equals(entry.getValue());
        // Tests like query.
        qry = cache.query(new SqlQuery<Integer, String>(String.class, "_val like 'value%'").setLocal(true));
        iter = qry.iterator();
        assert iter.next() != null;
        assert iter.next() != null;
        assert iter.next() != null;
        assert iter.next() != null;
        assert iter.next() != null;
        assert !iter.hasNext();
        // Test explain for primitive index.
        List<List<?>> res = cache.query(new SqlFieldsQuery("explain select _key from String where _val > 'value1'").setLocal(true)).getAll();
        assertTrue("__ explain: \n" + res, ((String) res.get(0).get(0)).toLowerCase().contains("_val_idx"));
        cache.destroy();
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) List(java.util.List) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache) Test(org.junit.Test) IgniteCacheAbstractQuerySelfTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractQuerySelfTest)

Example 55 with Cache

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

the class CacheMvccBasicContinuousQueryTest method checkUpdateCountersGapsClosed.

/**
 * @throws Exception If failed.
 */
private void checkUpdateCountersGapsClosed(CacheMode cacheMode) throws Exception {
    testSpi = true;
    int srvCnt = 4;
    startGridsMultiThreaded(srvCnt);
    IgniteEx nearNode = grid(srvCnt - 1);
    IgniteCache<Object, Object> cache = nearNode.createCache(cacheConfiguration(cacheMode, FULL_SYNC, srvCnt - 1, srvCnt).setIndexedTypes(Integer.class, Integer.class));
    IgniteEx primary = grid(0);
    Affinity<Object> aff = nearNode.affinity(cache.getName());
    int[] nearBackupParts = aff.backupPartitions(nearNode.localNode());
    int[] primaryParts = aff.primaryPartitions(primary.localNode());
    Collection<Integer> nearSet = new HashSet<>();
    for (int part : nearBackupParts) nearSet.add(part);
    Collection<Integer> primarySet = new HashSet<>();
    for (int part : primaryParts) primarySet.add(part);
    // We need backup partitions on the near node.
    nearSet.retainAll(primarySet);
    List<Integer> keys = singlePartKeys(primary.cache(DEFAULT_CACHE_NAME), 20, nearSet.iterator().next());
    int range = 3;
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    List<CacheEntryEvent> arrivedEvts = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(range * 2);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent e : evts) {
                arrivedEvts.add(e);
                latch.countDown();
            }
        }
    });
    QueryCursor<Cache.Entry<Integer, Integer>> cur = nearNode.cache(DEFAULT_CACHE_NAME).query(qry);
    // prevent first transaction prepare on backups
    TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(primary);
    spi.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

        private final AtomicInteger limiter = new AtomicInteger();

        @Override
        public boolean apply(ClusterNode node, Message msg) {
            if (msg instanceof GridDhtTxPrepareRequest)
                return limiter.getAndIncrement() < srvCnt - 1;
            return false;
        }
    });
    Transaction txA = primary.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
    for (int i = 0; i < range; i++) primary.cache(DEFAULT_CACHE_NAME).put(keys.get(i), 2);
    txA.commitAsync();
    GridTestUtils.runAsync(() -> {
        try (Transaction tx = primary.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            for (int i = range; i < range * 2; i++) primary.cache(DEFAULT_CACHE_NAME).put(keys.get(i), 1);
            tx.commit();
        }
    }).get();
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return primary.context().cache().context().tm().activeTransactions().stream().allMatch(tx -> tx.state() == PREPARING);
        }
    }, 3_000);
    GridTestUtils.runAsync(() -> {
        try (Transaction txB = primary.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            for (int i = range * 2; i < range * 3; i++) primary.cache(DEFAULT_CACHE_NAME).put(keys.get(i), 3);
            txB.commit();
        }
    }).get();
    long primaryUpdCntr = getUpdateCounter(primary, keys.get(0));
    assertEquals(range * 3, primaryUpdCntr);
    // drop primary
    stopGrid(primary.name());
    // Wait all txs are rolled back.
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            boolean allRolledBack = true;
            for (int i = 1; i < srvCnt; i++) {
                boolean rolledBack = grid(i).context().cache().context().tm().activeTransactions().stream().allMatch(tx -> tx.state() == ROLLED_BACK);
                allRolledBack &= rolledBack;
            }
            return allRolledBack;
        }
    }, 3_000);
    for (int i = 1; i < srvCnt; i++) {
        IgniteCache backupCache = grid(i).cache(DEFAULT_CACHE_NAME);
        int size = backupCache.query(new SqlFieldsQuery("select * from Integer")).getAll().size();
        long backupCntr = getUpdateCounter(grid(i), keys.get(0));
        assertEquals(range * 2, size);
        assertEquals(primaryUpdCntr, backupCntr);
    }
    assertTrue(latch.await(5, SECONDS));
    assertEquals(range * 2, arrivedEvts.size());
    cur.close();
}
Also used : GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Affinity(org.apache.ignite.cache.affinity.Affinity) CacheContinuousQueryManager(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager) Transaction(org.apache.ignite.transactions.Transaction) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Cache(javax.cache.Cache) CacheException(javax.cache.CacheException) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) G(org.apache.ignite.internal.util.typedef.G) Collection(java.util.Collection) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) PREPARING(org.apache.ignite.transactions.TransactionState.PREPARING) GridContinuousMessage(org.apache.ignite.internal.processors.continuous.GridContinuousMessage) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) CountDownLatch(java.util.concurrent.CountDownLatch) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) List(java.util.List) CacheEntryEvent(javax.cache.event.CacheEntryEvent) QueryCursor(org.apache.ignite.cache.query.QueryCursor) TX_SIZE_THRESHOLD(org.apache.ignite.internal.processors.cache.mvcc.MvccCachingManager.TX_SIZE_THRESHOLD) PA(org.apache.ignite.internal.util.typedef.PA) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) Message(org.apache.ignite.plugin.extensions.communication.Message) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) SECONDS(java.util.concurrent.TimeUnit.SECONDS) CacheMode(org.apache.ignite.cache.CacheMode) GridContinuousMessage(org.apache.ignite.internal.processors.continuous.GridContinuousMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) HashSet(java.util.HashSet) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx)

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