Search in sources :

Example 36 with ExpiryPolicy

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

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

the class IgniteCacheReadThroughEvictionSelfTest method testReadThroughWithExpirePolicy.

/**
 * @throws Exception if failed.
 */
public void testReadThroughWithExpirePolicy() throws Exception {
    Ignite ig = testedGrid();
    CacheConfiguration<Object, Object> cc = variationConfig("expire");
    IgniteCache<Object, Object> cache = ig.createCache(cc);
    try {
        ExpiryPolicy exp = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, TIMEOUT));
        for (int i = 0; i < KEYS; i++) cache.withExpiryPolicy(exp).put(key(i), value(i));
        U.sleep(TIMEOUT);
        waitEmpty(cc.getName());
        exp = new AccessedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, TIMEOUT));
        for (int i = 0; i < KEYS; i++) {
            assertEquals(value(i), cache.get(key(i)));
            cache.withExpiryPolicy(exp).get(key(i));
        }
        U.sleep(TIMEOUT);
        waitEmpty(cc.getName());
        for (int i = 0; i < KEYS; i++) assertEquals(value(i), cache.get(key(i)));
    } finally {
        destroyCacheSafe(ig, cc.getName());
    }
}
Also used : CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) AccessedExpiryPolicy(javax.cache.expiry.AccessedExpiryPolicy) Ignite(org.apache.ignite.Ignite) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) AccessedExpiryPolicy(javax.cache.expiry.AccessedExpiryPolicy)

Example 38 with ExpiryPolicy

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

the class GridCacheAbstractFullApiSelfTest 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(cache, 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(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
    final Affinity<String> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
    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(peek(cache, "key"));
    assertNull(cache.localPeek(key, ONHEAP));
    assertTrue(cache.localSize() == 0);
    load(cache, key, true);
    for (int i = 0; i < gridCount(); i++) {
        if (aff.isPrimary(grid(i).cluster().localNode(), key))
            assertEquals((Integer) 1, peek(jcache(i), key));
        if (aff.isBackup(grid(i).cluster().localNode(), key))
            assertEquals((Integer) 1, peek(jcache(i), 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 39 with ExpiryPolicy

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

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

the class GridCacheAdapter method localLoadCache.

/**
 * {@inheritDoc}
 */
@Override
public void localLoadCache(final IgniteBiPredicate<K, V> p, Object[] args) throws IgniteCheckedException {
    final boolean replicate = ctx.isDrEnabled();
    final AffinityTopologyVersion topVer = ctx.affinity().affinityTopologyVersion();
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    ExpiryPolicy plc0 = opCtx != null ? opCtx.expiry() : null;
    final ExpiryPolicy plc = plc0 != null ? plc0 : ctx.expiry();
    final boolean keepBinary = opCtx != null && opCtx.isKeepBinary();
    if (p != null)
        ctx.kernalContext().resource().injectGeneric(p);
    try {
        if (ctx.store().isLocal()) {
            DataStreamerImpl ldr = ctx.kernalContext().dataStream().dataStreamer(ctx.name());
            try {
                ldr.skipStore(true);
                ldr.receiver(new IgniteDrDataStreamerCacheUpdater());
                ldr.keepBinary(keepBinary);
                LocalStoreLoadClosure c = new LocalStoreLoadClosure(p, ldr, plc);
                ctx.store().loadCache(c, args);
                c.onDone();
            } finally {
                ldr.closeEx(false);
            }
        } else {
            // Version for all loaded entries.
            final GridCacheVersion ver0 = ctx.versions().nextForLoad();
            ctx.store().loadCache(new CIX3<KeyCacheObject, Object, GridCacheVersion>() {

                @Override
                public void applyx(KeyCacheObject key, Object val, @Nullable GridCacheVersion ver) throws IgniteException {
                    assert ver == null;
                    long ttl = CU.ttlForLoad(plc);
                    if (ttl == CU.TTL_ZERO)
                        return;
                    loadEntry(key, val, ver0, (IgniteBiPredicate<Object, Object>) p, topVer, replicate, ttl);
                }
            }, args);
        }
    } finally {
        if (p instanceof PlatformCacheEntryFilter)
            ((PlatformCacheEntryFilter) p).onClose();
    }
}
Also used : IgniteDrDataStreamerCacheUpdater(org.apache.ignite.internal.processors.dr.IgniteDrDataStreamerCacheUpdater) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) DataStreamerImpl(org.apache.ignite.internal.processors.datastreamer.DataStreamerImpl) PlatformCacheEntryFilter(org.apache.ignite.internal.processors.platform.cache.PlatformCacheEntryFilter) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteException(org.apache.ignite.IgniteException) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) IgniteExternalizableExpiryPolicy(org.apache.ignite.internal.processors.cache.distributed.IgniteExternalizableExpiryPolicy)

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