Search in sources :

Example 26 with TouchedExpiryPolicy

use of javax.cache.expiry.TouchedExpiryPolicy in project ignite by apache.

the class GridCacheTtlManagerLoadTest method testLoad.

/**
     * @throws Exception If failed.
     */
public void testLoad() throws Exception {
    cacheMode = REPLICATED;
    final IgniteKernal g = (IgniteKernal) startGrid(0);
    try {
        final AtomicBoolean stop = new AtomicBoolean();
        IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                IgniteCache<Object, Object> cache = g.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, 1000)));
                long key = 0;
                while (!stop.get()) {
                    cache.put(key, key);
                    key++;
                }
                return null;
            }
        }, 1);
        GridCacheTtlManager ttlMgr = g.internalCache(DEFAULT_CACHE_NAME).context().ttl();
        for (int i = 0; i < 300; i++) {
            U.sleep(1000);
            ttlMgr.printMemoryStats();
        }
        stop.set(true);
        fut.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCache(org.apache.ignite.IgniteCache) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

Example 27 with TouchedExpiryPolicy

use of javax.cache.expiry.TouchedExpiryPolicy in project ignite by apache.

the class GridCacheNearOnlyMultiNodeFullApiSelfTest method checkReaderTtl.

/**
     * @param inTx If {@code true} starts explicit transaction.
     * @throws Exception If failed.
     */
private void checkReaderTtl(boolean inTx) throws Exception {
    int ttl = 1000;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttl));
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(fullCache(), 1).get(0);
    c.put(key, 1);
    info("Finished first put.");
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertEquals((Integer) 1, c.get(key));
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
    long startTime = System.currentTimeMillis();
    int fullIdx = nearIdx == 0 ? 1 : 0;
    // Now commit transaction and check that ttl and expire time have been saved.
    Transaction tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).withExpiryPolicy(expiry).put(key, 1);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    long[] expireTimes = new long[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        info("Checking grid: " + grid(i).localNode().id());
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertTrue(entryTtl.get2() > startTime);
            expireTimes[i] = entryTtl.get2();
        }
    }
    // One more update from the same cache entry to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).withExpiryPolicy(expiry).put(key, 2);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertTrue(entryTtl.get2() > startTime);
            expireTimes[i] = entryTtl.get2();
        }
    }
    // And one more update to ensure that ttl is not changed and expire time is not shifted forward.
    U.sleep(100);
    tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).put(key, 4);
    } finally {
        if (tx != null)
            tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertEquals(expireTimes[i], (long) entryTtl.get2());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean applyx() throws IgniteCheckedException {
            try {
                Integer val = c.get(key);
                if (val != null) {
                    info("Value is in cache [key=" + key + ", val=" + val + ']');
                    return false;
                }
                if (!internalCache(c).context().deferredDelete()) {
                    GridCacheEntryEx e0 = internalCache(c).peekEx(key);
                    return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
                } else
                    return true;
            } catch (GridCacheEntryRemovedException ignored) {
                // If e0.valueBytes() thrown this exception then entry has been removed.
                return true;
            }
        }
    }, Math.min(ttl * 10, getTestTimeout())));
    // Ensure that old TTL and expire time are not longer "visible".
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
    // Ensure that next update will not pick old expire time.
    tx = inTx ? transactions().txStart() : null;
    try {
        c.put(key, 10);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    U.sleep(2000);
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
}
Also used : Duration(javax.cache.expiry.Duration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Transaction(org.apache.ignite.transactions.Transaction) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)

Aggregations

Duration (javax.cache.expiry.Duration)27 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)27 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)11 Transaction (org.apache.ignite.transactions.Transaction)8 IgniteCache (org.apache.ignite.IgniteCache)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 IgniteKernal (org.apache.ignite.internal.IgniteKernal)3 GridAbsPredicateX (org.apache.ignite.internal.util.lang.GridAbsPredicateX)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 HazelcastCachingProvider (com.hazelcast.cache.HazelcastCachingProvider)1 ICache (com.hazelcast.cache.ICache)1 ClientConfig (com.hazelcast.client.config.ClientConfig)1 XmlClientConfigBuilder (com.hazelcast.client.config.XmlClientConfigBuilder)1 CacheConfig (com.hazelcast.config.CacheConfig)1 ClasspathXmlConfig (com.hazelcast.config.ClasspathXmlConfig)1 Config (com.hazelcast.config.Config)1 EvictionConfig (com.hazelcast.config.EvictionConfig)1