Search in sources :

Example 21 with TouchedExpiryPolicy

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

the class GridCacheBasicApiAbstractTest method testPutWithExpiration.

/**
     *
     * @throws Exception In case of error.
     */
public void testPutWithExpiration() throws Exception {
    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
    CacheEventListener lsnr = new CacheEventListener(new CountDownLatch(1));
    ignite.events().localListen(lsnr, EVTS_CACHE);
    ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, 200L));
    try {
        int key = (int) System.currentTimeMillis();
        cache.withExpiryPolicy(expiry).put(key, "val");
        assert cache.get(key) != null;
        cache.withExpiryPolicy(expiry).put(key, "val");
        Thread.sleep(500);
        assert cache.get(key) == null;
    } finally {
        ignite.events().stopLocalListen(lsnr, EVTS_CACHE);
    }
}
Also used : TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 22 with TouchedExpiryPolicy

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

the class GridCacheTtlManagerSelfTest method checkTtl.

/**
     * @param mode Cache mode.
     * @throws Exception If failed.
     */
private void checkTtl(CacheMode mode) throws Exception {
    cacheMode = mode;
    final IgniteKernal g = (IgniteKernal) startGrid(0);
    try {
        final String key = "key";
        g.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, 1000))).put(key, 1);
        assertEquals(1, g.cache(DEFAULT_CACHE_NAME).get(key));
        U.sleep(1100);
        GridTestUtils.retryAssert(log, 10, 100, new CAX() {

            @Override
            public void applyx() {
                // Check that no more entries left in the map.
                assertNull(g.cache(DEFAULT_CACHE_NAME).get(key));
                if (!g.internalCache(DEFAULT_CACHE_NAME).context().deferredDelete())
                    assertNull(g.internalCache(DEFAULT_CACHE_NAME).map().getEntry(g.internalCache(DEFAULT_CACHE_NAME).context().toCacheKeyObject(key)));
            }
        });
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 23 with TouchedExpiryPolicy

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

the class GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest method testPeekExpired.

/** {@inheritDoc} */
@Override
public void testPeekExpired() throws Exception {
    IgniteCache<String, Integer> c = jcache();
    String key = primaryKeysForCache(c, 1).get(0);
    info("Using key: " + key);
    c.put(key, 1);
    assertEquals(null, c.localPeek(key, CachePeekMode.ONHEAP));
    long ttl = 500;
    grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl))).put(key, 1);
    Thread.sleep(ttl + 100);
    assert c.localPeek(key, CachePeekMode.ONHEAP) == null;
    assert c.localSize() == 0 : "Cache is not empty.";
}
Also used : TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

Example 24 with TouchedExpiryPolicy

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

the class GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest method testEvictExpired.

/** {@inheritDoc} */
@Override
public void testEvictExpired() throws Exception {
    IgniteCache<String, Integer> cache = jcache();
    final String key = primaryKeysForCache(cache, 1).get(0);
    cache.put(key, 1);
    assertEquals((Integer) 1, cache.get(key));
    long ttl = 500;
    grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl))).put(key, 1);
    boolean wait = waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                if (peek(jcache(i), key) != null)
                    return false;
            }
            return true;
        }
    }, ttl + 1000);
    assertTrue("Failed to wait for entry expiration.", wait);
    // Expired entry should not be swapped.
    cache.localEvict(Collections.singleton(key));
    assertNull(cache.localPeek(key, CachePeekMode.ONHEAP));
    assertTrue(cache.localSize() == 0);
    // Force reload on primary node.
    for (int i = 0; i < gridCount(); i++) {
        if (ignite(i).affinity(DEFAULT_CACHE_NAME).isPrimary(ignite(i).cluster().localNode(), key))
            load(jcache(i), key, true);
    }
    // Will do near get request.
    load(cache, key, true);
    assertEquals(null, cache.localPeek(key, CachePeekMode.ONHEAP));
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

Example 25 with TouchedExpiryPolicy

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

the class GridCacheAbstractMetricsSelfTest method checkTtl.

/**
     * @param inTx {@code true} for tx.
     * @throws Exception If failed.
     */
private void checkTtl(boolean inTx) throws Exception {
    int ttl = 1000;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    final IgniteCache<Integer, Integer> c = grid(0).cache(DEFAULT_CACHE_NAME);
    final Integer key = primaryKeys(jcache(0), 1, 0).get(0);
    c.put(key, 1);
    GridCacheAdapter<Object, Object> c0 = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
    if (c0.isNear())
        c0 = c0.context().near().dht();
    GridCacheEntryEx entry = c0.entryEx(key);
    assert entry != null;
    assertEquals(0, entry.ttl());
    assertEquals(0, entry.expireTime());
    long startTime = U.currentTimeMillis();
    if (inTx) {
        // Rollback transaction for the first time.
        Transaction tx = grid(0).transactions().txStart();
        try {
            grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
        } finally {
            tx.rollback();
        }
        entry = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME).entryEx(key);
        assertEquals(0, entry.ttl());
        assertEquals(0, entry.expireTime());
    }
    // Now commit transaction and check that ttl and expire time have been saved.
    Transaction tx = inTx ? grid(0).transactions().txStart() : null;
    try {
        grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
    } finally {
        if (tx != null)
            tx.commit();
    }
    long[] expireTimes = new long[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            if (c0.isNear())
                c0 = c0.context().near().dht();
            GridCacheEntryEx curEntry = c0.entryEx(key);
            curEntry.unswap();
            assertTrue(curEntry.expireTime() >= startTime);
            expireTimes[i] = curEntry.expireTime();
        }
    }
    // One more update from the same cache entry to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? grid(0).transactions().txStart() : null;
    try {
        grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 2);
    } finally {
        if (tx != null)
            tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            if (c0.isNear())
                c0 = c0.context().near().dht();
            GridCacheEntryEx curEntry = c0.entryEx(key);
            curEntry.unswap();
            assertTrue(curEntry.expireTime() >= startTime);
            expireTimes[i] = curEntry.expireTime();
        }
    }
    // And one more direct update to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? grid(0).transactions().txStart() : null;
    try {
        grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 3);
    } finally {
        if (tx != null)
            tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            if (c0.isNear())
                c0 = c0.context().near().dht();
            GridCacheEntryEx curEntry = c0.entryEx(key);
            curEntry.unswap();
            assertTrue(curEntry.expireTime() >= startTime);
            expireTimes[i] = curEntry.expireTime();
        }
    }
    // And one more update to ensure that ttl is not changed and expire time is not shifted forward.
    U.sleep(100);
    log.info("Put 4");
    tx = inTx ? grid(0).transactions().txStart() : null;
    try {
        c.put(key, 4);
    } finally {
        if (tx != null)
            tx.commit();
    }
    log.info("Put 4 done");
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            if (c0.isNear())
                c0 = c0.context().near().dht();
            GridCacheEntryEx curEntry = c0.entryEx(key);
            curEntry.unswap();
            assertEquals(expireTimes[i], curEntry.expireTime());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean applyx() {
            try {
                if (c.get(key) != null)
                    return false;
                // Get "cache" field from GridCacheProxyImpl.
                GridCacheAdapter c0 = cacheFromCtx(c);
                if (!c0.context().deferredDelete()) {
                    GridCacheEntryEx e0 = c0.entryEx(key);
                    return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
                } else
                    return true;
            } catch (GridCacheEntryRemovedException e) {
                throw new RuntimeException(e);
            }
        }
    }, Math.min(ttl * 10, getTestTimeout())));
    c0 = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
    if (c0.isNear())
        c0 = c0.context().near().dht();
    // Ensure that old TTL and expire time are not longer "visible".
    entry = c0.entryEx(key);
    assertEquals(0, entry.expireTime());
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) Duration(javax.cache.expiry.Duration) Transaction(org.apache.ignite.transactions.Transaction) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy)

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