Search in sources :

Example 16 with TouchedExpiryPolicy

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

the class GridCacheAbstractFullApiSelfTest method testCompactExpired.

/**
     * TODO GG-11133.
     *
     * @throws Exception If failed.
     */
public void testCompactExpired() throws Exception {
    final IgniteCache<String, Integer> cache = jcache();
    final String key = F.first(primaryKeysForCache(cache, 1));
    cache.put(key, 1);
    long ttl = 500;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
    waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return cache.localPeek(key) == null;
        }
    }, ttl + 1000);
    // Peek will actually remove entry from cache.
    assertNull(cache.localPeek(key));
    assertEquals(0, cache.localSize());
    // Clear readers, if any.
    cache.remove(key);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

Example 17 with TouchedExpiryPolicy

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

the class IgniteCacheConfigVariationsFullApiTest method _testEvictExpired.

/**
     * TODO GG-11133.
     * @throws Exception In case of error.
     */
public void _testEvictExpired() throws Exception {
    final IgniteCache<String, Integer> cache = jcache();
    final String key = primaryKeysForCache(1).get(0);
    cache.put(key, 1);
    assertEquals((Integer) 1, cache.get(key));
    long ttl = 500;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    grid(0).cache(cacheName()).withExpiryPolicy(expiry).put(key, 1);
    boolean wait = waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                if (jcache(i).localPeek(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"));
    assertNull(cache.localPeek(key, ONHEAP));
    assertTrue(cache.localSize() == 0);
    if (storeEnabled()) {
        load(cache, key, true);
        Affinity<String> aff = ignite(0).affinity(cacheName());
        for (int i = 0; i < gridCount(); i++) {
            if (aff.isPrimary(grid(i).cluster().localNode(), key))
                assertEquals(1, jcache(i).localPeek(key));
            if (aff.isBackup(grid(i).cluster().localNode(), key))
                assertEquals(1, jcache(i).localPeek(key));
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

Example 18 with TouchedExpiryPolicy

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

the class IgniteCacheConfigVariationsFullApiTest method testPeekExpiredTx.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
public void testPeekExpiredTx() throws Exception {
    if (txShouldBeUsed()) {
        final IgniteCache<String, Integer> c = jcache();
        final String key = "1";
        int ttl = 500;
        try (Transaction tx = grid(0).transactions().txStart()) {
            final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
            grid(0).cache(cacheName()).withExpiryPolicy(expiry).put(key, 1);
            tx.commit();
        }
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return c.localPeek(key) == null;
            }
        }, 2000);
        assertNull(c.localPeek(key));
        assert c.localSize() == 0;
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

Example 19 with TouchedExpiryPolicy

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

the class IgniteCacheConfigVariationsFullApiTest method checkTtl.

/**
     * @param inTx In tx flag.
     * @param oldEntry {@code True} to check TTL on old entry, {@code false} on new.
     * @throws Exception If failed.
     */
private void checkTtl(boolean inTx, boolean oldEntry) throws Exception {
    // TODO GG-11133.
    if (true)
        return;
    int ttl = 1000;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(1).get(0);
    IgnitePair<Long> entryTtl;
    if (oldEntry) {
        c.put(key, 1);
        entryTtl = entryTtl(serverNodeCache(), key);
        assertEquals((Long) 0L, entryTtl.get1());
        assertEquals((Long) 0L, entryTtl.get2());
    }
    long startTime = System.currentTimeMillis();
    if (inTx) {
        // Rollback transaction for the first time.
        Transaction tx = transactions().txStart();
        try {
            jcache().withExpiryPolicy(expiry).put(key, 1);
        } finally {
            tx.rollback();
        }
        if (oldEntry) {
            entryTtl = entryTtl(serverNodeCache(), key);
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals((Long) 0L, entryTtl.get1());
            assertEquals((Long) 0L, entryTtl.get2());
        }
    }
    // Now commit transaction and check that ttl and expire time have been saved.
    Transaction tx = inTx ? transactions().txStart() : null;
    try {
        jcache().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++) {
        if (grid(i).affinity(cacheName()).isPrimaryOrBackup(grid(i).localNode(), key)) {
            IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
            assertNotNull(curEntryTtl.get1());
            assertNotNull(curEntryTtl.get2());
            assertEquals(ttl, (long) curEntryTtl.get1());
            assertTrue(curEntryTtl.get2() > startTime);
            expireTimes[i] = curEntryTtl.get2();
        }
    }
    // One more update from the same cache entry to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? transactions().txStart() : null;
    try {
        jcache().withExpiryPolicy(expiry).put(key, 2);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(cacheName()).isPrimaryOrBackup(grid(i).localNode(), key)) {
            IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
            assertNotNull(curEntryTtl.get1());
            assertNotNull(curEntryTtl.get2());
            assertEquals(ttl, (long) curEntryTtl.get1());
            assertTrue(curEntryTtl.get2() > startTime);
            expireTimes[i] = curEntryTtl.get2();
        }
    }
    // And one more direct update to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? transactions().txStart() : null;
    try {
        jcache().withExpiryPolicy(expiry).put(key, 3);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(cacheName()).isPrimaryOrBackup(grid(i).localNode(), key)) {
            IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
            assertNotNull(curEntryTtl.get1());
            assertNotNull(curEntryTtl.get2());
            assertEquals(ttl, (long) curEntryTtl.get1());
            assertTrue(curEntryTtl.get2() > startTime);
            expireTimes[i] = curEntryTtl.get2();
        }
    }
    // 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 ? transactions().txStart() : null;
    try {
        jcache().put(key, 4);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    log.info("Put 4 done");
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(cacheName()).isPrimaryOrBackup(grid(i).localNode(), key)) {
            IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
            assertNotNull(curEntryTtl.get1());
            assertNotNull(curEntryTtl.get2());
            assertEquals(ttl, (long) curEntryTtl.get1());
            assertEquals(expireTimes[i], (long) curEntryTtl.get2());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean applyx() {
            try {
                Integer val = c.get(key);
                if (val != null) {
                    info("Value is in cache [key=" + key + ", val=" + val + ']');
                    return false;
                }
                // Get "cache" field from GridCacheProxyImpl.
                GridCacheAdapter c0 = cacheFromCtx(c);
                if (!c0.context().deferredDelete()) {
                    GridCacheEntryEx e0 = c0.peekEx(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())));
    IgniteCache srvNodeCache = serverNodeCache();
    if (!isMultiJvmObject(srvNodeCache)) {
        GridCacheAdapter internalCache = internalCache(srvNodeCache);
        if (internalCache.isLocal())
            return;
    }
    assert c.get(key) == null;
    // Ensure that old TTL and expire time are not longer "visible".
    entryTtl = entryTtl(srvNodeCache, 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 {
        jcache().put(key, 10);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    U.sleep(2000);
    entryTtl = entryTtl(srvNodeCache, key);
    assertEquals((Integer) 10, c.get(key));
    assertNotNull(entryTtl.get1());
    assertNotNull(entryTtl.get2());
    assertEquals(0, (long) entryTtl.get1());
    assertEquals(0, (long) entryTtl.get2());
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) Duration(javax.cache.expiry.Duration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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)

Example 20 with TouchedExpiryPolicy

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

the class IgniteCacheConfigVariationsFullApiTest method testCompactExpired.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
public void testCompactExpired() throws Exception {
    final IgniteCache<String, Integer> cache = jcache();
    final String key = F.first(primaryKeysForCache(1));
    cache.put(key, 1);
    long ttl = 500;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    grid(0).cache(cacheName()).withExpiryPolicy(expiry).put(key, 1);
    waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return cache.localPeek(key) == null;
        }
    }, ttl + 1000);
    // Peek will actually remove entry from cache.
    assertNull(cache.localPeek(key));
    assert cache.localSize() == 0;
    // Clear readers, if any.
    cache.remove(key);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

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