Search in sources :

Example 31 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy 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 32 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy 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 33 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy 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)

Example 34 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy 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)

Example 35 with ExpiryPolicy

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

the class GridCacheAbstractFullApiSelfTest method testPeekExpired.

/**
 * TODO GG-11133.
 *
 * @throws Exception If failed.
 */
public void testPeekExpired() throws Exception {
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(c, 1).get(0);
    info("Using key: " + key);
    c.put(key, 1);
    assertEquals(Integer.valueOf(1), peek(c, key));
    int ttl = 500;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    c.withExpiryPolicy(expiry).put(key, 1);
    Thread.sleep(ttl + 100);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return peek(c, key) == null;
        }
    }, 2000);
    assert peek(c, key) == null;
    assert c.localSize() == 0 : "Cache is not empty.";
}
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

ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)54 Duration (javax.cache.expiry.Duration)23 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)15 IgniteCacheExpiryPolicy (org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)11 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)11 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)10 CacheOperationContext (org.apache.ignite.internal.processors.cache.CacheOperationContext)9 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)9 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)9 CacheOperationProvider (com.hazelcast.cache.impl.CacheOperationProvider)7 UUID (java.util.UUID)7 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 Transaction (org.apache.ignite.transactions.Transaction)7 Map (java.util.Map)5 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)5 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)5 IgniteTxTimeoutCheckedException (org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException)5