Search in sources :

Example 6 with TouchedExpiryPolicy

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

the class CacheQueryFilterExpiredTest method checkFilterExpired.

/**
     * @param ignite Node.
     * @param atomicityMode Cache atomicity mode.
     * @param eagerTtl Value for {@link CacheConfiguration#setEagerTtl(boolean)}.
     * @throws Exception If failed.
     */
private void checkFilterExpired(Ignite ignite, CacheAtomicityMode atomicityMode, boolean eagerTtl) throws Exception {
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setAtomicityMode(atomicityMode);
    ccfg.setEagerTtl(eagerTtl);
    ccfg.setIndexedTypes(Integer.class, Integer.class);
    final IgniteCache<Integer, Integer> cache = ignite.createCache(ccfg);
    try {
        IgniteCache<Integer, Integer> expCache = cache.withExpiryPolicy(new TouchedExpiryPolicy(new Duration(0, 2000)));
        for (int i = 0; i < 10; i++) {
            IgniteCache<Integer, Integer> cache0 = i % 2 == 0 ? cache : expCache;
            cache0.put(i, i);
        }
        assertEquals(10, cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll().size());
        assertEquals(10, cache.query(new SqlFieldsQuery("select _key, _val from Integer")).getAll().size());
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll().size() == 5 && cache.query(new SqlFieldsQuery("select _key, _val from Integer")).getAll().size() == 5;
            }
        }, 5000);
        assertEquals(5, cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll().size());
        assertEquals(5, cache.query(new SqlFieldsQuery("select _key, _val from Integer")).getAll().size());
    } finally {
        ignite.destroyCache(ccfg.getName());
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 7 with TouchedExpiryPolicy

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

the class GridCacheAbstractFullApiSelfTest 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(jcache(), 1).get(0);
    IgnitePair<Long> entryTtl;
    if (oldEntry) {
        c.put(key, 1);
        entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        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(fullCache(), key);
            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(DEFAULT_CACHE_NAME).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(DEFAULT_CACHE_NAME).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(DEFAULT_CACHE_NAME).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(DEFAULT_CACHE_NAME).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 fullCache = fullCache();
    if (!isMultiJvmObject(fullCache)) {
        GridCacheAdapter internalCache = internalCache(fullCache);
        if (internalCache.isLocal())
            return;
    }
    assert c.get(key) == null;
    // Ensure that old TTL and expire time are not longer "visible".
    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 {
        jcache().put(key, 10);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    U.sleep(2000);
    entryTtl = entryTtl(fullCache(), 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 8 with TouchedExpiryPolicy

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

the class IgniteCacheConfigVariationsFullApiTest method testPeekExpired.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
public void testPeekExpired() throws Exception {
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(1).get(0);
    info("Using key: " + key);
    c.put(key, 1);
    assertEquals(Integer.valueOf(1), c.localPeek(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 c.localPeek(key) == null;
        }
    }, 2000);
    assertNull(c.localPeek(key));
    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)

Example 9 with TouchedExpiryPolicy

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

the class GridCachePartitionedEvictionSelfTest method doTestEviction.

/**
     * @throws Exception If failed.
     * @param concurrency Tx concurrency.
     * @param isolation Tx isolation.
     */
private void doTestEviction(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    assert concurrency != null;
    assert isolation != null;
    // This condition should be "true", otherwise the test doesn't make sense.
    assert KEY_CNT >= EVICT_CACHE_SIZE;
    GridDhtCacheAdapter<String, Integer> dht0 = dht(jcache(0));
    GridDhtCacheAdapter<String, Integer> dht1 = dht(jcache(1));
    Affinity<String> aff = dht0.affinity();
    TouchedExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, 10));
    for (int kv = 0; kv < KEY_CNT; kv++) {
        String key = String.valueOf(kv);
        ClusterNode node = aff.mapKeyToNode(key);
        IgniteCache<String, Integer> c = cache(node);
        IgniteTransactions txs = G.ignite(node.id()).transactions();
        try (Transaction tx = txs.txStart(concurrency, isolation)) {
            assert c.get(key) == null;
            c.withExpiryPolicy(plc).put(key, 1);
            assertEquals(Integer.valueOf(kv), c.get(key));
            tx.commit();
        }
    }
    if (TEST_INFO) {
        info("Printing keys in dht0...");
        for (String key : dht0.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(0).localNode(), aff.mapKeyToNode(key)) + ']');
        info("Printing keys in dht1...");
        for (String key : dht1.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(1).localNode(), aff.mapKeyToNode(key)) + ']');
    }
    assertEquals(EVICT_CACHE_SIZE, dht0.size());
    assertEquals(EVICT_CACHE_SIZE, dht1.size());
    assertEquals(0, near(jcache(0)).nearSize());
    assertEquals(0, near(jcache(1)).nearSize());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Transaction(org.apache.ignite.transactions.Transaction) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 10 with TouchedExpiryPolicy

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

the class IgniteCacheAbstractQuerySelfTest method testExpiration.

/**
     * Expired entries are not included to result.
     *
     * @throws Exception If failed.
     */
public void testExpiration() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
    cache.withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, 1000))).put(7, 1);
    List<Cache.Entry<Integer, Integer>> qry = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll();
    Cache.Entry<Integer, Integer> res = F.first(qry);
    assertEquals(1, res.getValue().intValue());
    // Less than minimal amount of time that must pass before a cache entry is considered expired.
    U.sleep(800);
    qry = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll();
    res = F.first(qry);
    assertEquals(1, res.getValue().intValue());
    // No expiry guarantee here. Test should be refactored in case of fails.
    U.sleep(1200);
    qry = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "1=1")).getAll();
    res = F.first(qry);
    assertNull(res);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

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