Search in sources :

Example 41 with Duration

use of javax.cache.expiry.Duration 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, 37));
    for (int kv = 0; kv < KEY_CNT; kv++) {
        Thread.sleep(40);
        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, kv);
            assertEquals(Integer.valueOf(kv), c.get(key));
            tx.commit();
        }
    }
    boolean[] seen = { false, false, false, false };
    long started = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        long dht0Keys = 0, dht1Keys = 0;
        seen[2] |= dht0.size() == EVICT_CACHE_SIZE;
        seen[3] |= dht1.size() == EVICT_CACHE_SIZE;
        info("Printing keys in dht0...");
        for (String key : dht0.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(0).localNode(), aff.mapKeyToNode(key)) + ", " + dht0Keys++ + ']');
        info("Printing keys in dht1...");
        for (String key : dht1.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(1).localNode(), aff.mapKeyToNode(key)) + ", " + dht1Keys++ + ']');
        seen[0] |= dht0Keys == EVICT_CACHE_SIZE;
        seen[1] |= dht1Keys == EVICT_CACHE_SIZE;
        if (seen[0] && seen[1] && seen[2] && seen[3] || System.currentTimeMillis() - started > 100)
            break;
    }
    assertTrue(Arrays.toString(seen), seen[0] && seen[1] && seen[2] && seen[3]);
    assertEquals(0, near(jcache(0)).nearSize());
    assertEquals(0, near(jcache(1)).nearSize());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Duration(javax.cache.expiry.Duration) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy)

Example 42 with Duration

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

the class IgniteCacheLargeValueExpireTest method checkExpire.

/**
 * @param ignite Node.
 * @param eagerTtl Value for {@link CacheConfiguration#setEagerTtl(boolean)}.
 * @throws Exception If failed.
 */
private void checkExpire(Ignite ignite, boolean eagerTtl) throws Exception {
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setEagerTtl(eagerTtl);
    ignite.createCache(ccfg);
    try {
        IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(0, 500)));
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        for (int i = 0; i < 10; i++) {
            log.info("Iteration: " + i);
            AtomicInteger cntr = new AtomicInteger();
            List<Object> keys = new ArrayList<>();
            for (int j = 0; j < 10_000; j++) {
                Object key = null;
                Object val = null;
                switch(rnd.nextInt(3)) {
                    case 0:
                        key = rnd.nextInt(100_000);
                        val = new TestKeyValue(cntr.getAndIncrement(), new byte[rnd.nextInt(3 * PAGE_SIZE)]);
                        break;
                    case 1:
                        key = new TestKeyValue(cntr.getAndIncrement(), new byte[rnd.nextInt(3 * PAGE_SIZE)]);
                        val = rnd.nextInt();
                        break;
                    case 2:
                        key = new TestKeyValue(cntr.getAndIncrement(), new byte[rnd.nextInt(3 * PAGE_SIZE)]);
                        val = new TestKeyValue(cntr.getAndIncrement(), new byte[rnd.nextInt(3 * PAGE_SIZE)]);
                        break;
                    default:
                        fail();
                }
                cache.put(key, val);
                keys.add(key);
            }
            U.sleep(1000);
            for (Object key : keys) assertNull(cache.get(key));
        }
    } finally {
        ignite.destroyCache(ccfg.getName());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 43 with Duration

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

the class CacheTtlReadOnlyModeSelfTest method getCacheConfigurations.

/**
 */
private static CacheConfiguration[] getCacheConfigurations() {
    CacheConfiguration[] cfgs = cacheConfigurations();
    List<CacheConfiguration> newCfgs = new ArrayList<>(cfgs.length);
    for (CacheConfiguration cfg : cfgs) {
        if (cfg.getAtomicityMode() == CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT) {
            // Expiry policy cannot be used with TRANSACTIONAL_SNAPSHOT.
            continue;
        }
        cfg.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(SECONDS, EXPIRATION_TIMEOUT)));
        cfg.setEagerTtl(true);
        newCfgs.add(cfg);
    }
    return newCfgs.toArray(new CacheConfiguration[0]);
}
Also used : ArrayList(java.util.ArrayList) Duration(javax.cache.expiry.Duration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 44 with Duration

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

the class ExpiryPolicyInfoLoggingTest method checkLoggingExpiryInfoForDynamicallyCreatedCache.

/**
 * Checking logging of expiry policy info for dynamically created cache.
 */
@Test
public void checkLoggingExpiryInfoForDynamicallyCreatedCache() throws Exception {
    LogListener lsnr = LogListener.matches(s -> s.startsWith(String.format(STARTED_CACHE_MSG, CACHE_2_NAME)) && s.contains(String.format(EXPRITY_POLICY_MSG, AccessedExpiryPolicy.class.getName(), false))).times(1).build();
    log.registerListener(lsnr);
    IgniteEx srv = startGrid(0);
    srv.createCache(new CacheConfiguration<>(CACHE_2_NAME).setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 5))).setEagerTtl(false));
    assertTrue(lsnr.check());
}
Also used : LogListener(org.apache.ignite.testframework.LogListener) IgniteEx(org.apache.ignite.internal.IgniteEx) Duration(javax.cache.expiry.Duration) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 45 with Duration

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

the class IgniteCacheClientNearCacheExpiryTest method testExpirationOnClient.

/**
 * @throws Exception If failed.
 */
@Test
public void testExpirationOnClient() throws Exception {
    Ignite ignite = grid(NODES - 1);
    // Check size of near entries via reflection because entries is filtered for size() API call.
    IgniteEx igniteEx = (IgniteEx) ignite;
    GridCacheAdapter internalCache = igniteEx.context().cache().internalCache(DEFAULT_CACHE_NAME);
    GridCacheLocalConcurrentMap map = GridTestUtils.getFieldValue(internalCache, GridCacheAdapter.class, "map");
    assertTrue(ignite.configuration().isClientMode());
    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
    assertTrue(((IgniteCacheProxy) cache).context().isNear());
    for (int i = 0; i < KEYS_COUNT; i++) cache.put(i, i);
    CreatedExpiryPolicy plc = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 500));
    IgniteCache<Object, Object> cacheWithExpiry = cache.withExpiryPolicy(plc);
    for (int i = KEYS_COUNT; i < KEYS_COUNT * 2; i++) {
        cacheWithExpiry.put(i, i);
        assertEquals(i, cacheWithExpiry.localPeek(i));
    }
    assertEquals(KEYS_COUNT * 2, map.publicSize(internalCache.context().cacheId()));
    U.sleep(1000);
    assertEquals(KEYS_COUNT, map.publicSize(internalCache.context().cacheId()));
    assertEquals(KEYS_COUNT, cache.size());
    for (int i = 0; i < KEYS_COUNT; i++) assertEquals(i, cacheWithExpiry.localPeek(i));
    for (int i = KEYS_COUNT; i < KEYS_COUNT * 2; i++) assertNull(cache.localPeek(i));
}
Also used : GridCacheLocalConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheLocalConcurrentMap) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) Duration(javax.cache.expiry.Duration) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) IgniteCacheAbstractTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest) Test(org.junit.Test)

Aggregations

Duration (javax.cache.expiry.Duration)119 Test (org.junit.Test)49 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)36 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)31 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)31 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)29 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)19 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)15 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)13 ModifiedExpiryPolicy (javax.cache.expiry.ModifiedExpiryPolicy)12 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)12 Ignite (org.apache.ignite.Ignite)11 Transaction (org.apache.ignite.transactions.Transaction)11 CacheLoaderException (javax.cache.integration.CacheLoaderException)10 ArrayList (java.util.ArrayList)9 IgniteCache (org.apache.ignite.IgniteCache)9 MutableConfiguration (javax.cache.configuration.MutableConfiguration)8 CacheWriterException (javax.cache.integration.CacheWriterException)8 CacheNotExistsException (com.hazelcast.cache.CacheNotExistsException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7