Search in sources :

Example 91 with Duration

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

the class IgniteCacheConfigVariationsFullApiTest method checkTtl0.

/**
 * @param inTx In tx flag.
 * @param oldEntry {@code True} to check TTL on old entry, {@code false} on new.
 * @param ttl TTL value.
 * @throws Exception If failed.
 */
private void checkTtl0(boolean inTx, boolean oldEntry, int ttl) throws Exception {
    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());
            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());
            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());
            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(expireTimes[i], (long) curEntryTtl.get2());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @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);
    assertNull(entryTtl);
    // 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(ttl + 500);
    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 92 with Duration

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

the class IgniteCacheConfigVariationsFullApiTest method testPeekExpiredTx.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
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) IgniteCacheConfigVariationsAbstractTest(org.apache.ignite.testframework.junits.IgniteCacheConfigVariationsAbstractTest) Test(org.junit.Test)

Example 93 with Duration

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

the class IgniteCacheConfigVariationsFullApiTest method testCompactExpired.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
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) IgniteCacheConfigVariationsAbstractTest(org.apache.ignite.testframework.junits.IgniteCacheConfigVariationsAbstractTest) Test(org.junit.Test)

Example 94 with Duration

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

the class IgniteCacheEntryListenerAbstractTest method checkEvents.

/**
 * @param cache Cache.
 * @param lsnrCfg Listener configuration.
 * @param key Key.
 * @param create {@code True} if listens for create events.
 * @param update {@code True} if listens for update events.
 * @param rmv {@code True} if listens for remove events.
 * @param expire {@code True} if listens for expire events.
 * @param oldVal {@code True} if old value should be provided for event.
 * @throws Exception If failed.
 */
private void checkEvents(final IgniteCache<Object, Object> cache, final CacheEntryListenerConfiguration<Object, Object> lsnrCfg, Integer key, boolean create, boolean update, boolean rmv, boolean expire, boolean oldVal) throws Exception {
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            cache.registerCacheEntryListener(lsnrCfg);
            return null;
        }
    }, IllegalArgumentException.class, null);
    final int UPDATES = 10;
    int expEvts = 0;
    if (create)
        expEvts += 4;
    if (update)
        expEvts += (UPDATES + 1);
    if (rmv)
        expEvts += 2;
    if (expire)
        expEvts += 2;
    evts = Collections.synchronizedList(new ArrayList<CacheEntryEvent<?, ?>>());
    evtsLatch = new CountDownLatch(expEvts);
    cache.put(key(key), value(0));
    for (int i = 0; i < UPDATES; i++) {
        if (i % 2 == 0)
            cache.put(key(key), value(i + 1));
        else
            cache.invoke(key(key), new EntrySetValueProcessor(value(i + 1)));
    }
    // Invoke processor does not update value, should not trigger event.
    assertEquals(String.valueOf(UPDATES), cache.invoke(key(key), new EntryToStringProcessor()));
    assertFalse(cache.putIfAbsent(key(key), value(-1)));
    assertFalse(cache.remove(key(key), value(-1)));
    assertTrue(cache.remove(key(key)));
    IgniteCache<Object, Object> expirePlcCache = cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 100)));
    expirePlcCache.put(key(key), value(10));
    U.sleep(700);
    if (!eagerTtl())
        // Provoke expire event if eager ttl is disabled.
        assertNull(primaryCache(key(key), cache.getName()).get(key(key)));
    IgniteCache<Object, Object> cache1 = cache;
    if (gridCount() > 1)
        // Do updates from another node.
        cache1 = jcache(1);
    cache1.put(key(key), value(1));
    cache1.put(key(key), value(2));
    assertTrue(cache1.remove(key(key)));
    IgniteCache<Object, Object> expirePlcCache1 = cache1.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 100)));
    expirePlcCache1.put(key(key), value(20));
    U.sleep(200);
    if (!eagerTtl())
        // Provoke expire event if eager ttl is disabled.
        assertNull(primaryCache(key(key), cache.getName()).get(key(key)));
    evtsLatch.await(5000, MILLISECONDS);
    assertEquals(expEvts, evts.size());
    Iterator<CacheEntryEvent<?, ?>> iter = evts.iterator();
    if (create)
        checkEvent(iter, key, CREATED, 0, null);
    if (update) {
        for (int i = 0; i < UPDATES; i++) checkEvent(iter, key, UPDATED, i + 1, oldVal ? i : null);
    }
    if (rmv)
        checkEvent(iter, key, REMOVED, oldVal ? UPDATES : null, oldVal ? UPDATES : null);
    if (create)
        checkEvent(iter, key, CREATED, 10, null);
    if (expire)
        checkEvent(iter, key, EXPIRED, oldVal ? 10 : null, oldVal ? 10 : null);
    if (create)
        checkEvent(iter, key, CREATED, 1, null);
    if (update)
        checkEvent(iter, key, UPDATED, 2, oldVal ? 1 : null);
    if (rmv)
        checkEvent(iter, key, REMOVED, oldVal ? 2 : null, oldVal ? 2 : null);
    if (create)
        checkEvent(iter, key, CREATED, 20, null);
    if (expire)
        checkEvent(iter, key, EXPIRED, oldVal ? 20 : null, oldVal ? 20 : null);
    assertEquals(0, evts.size());
    log.info("Remove listener.");
    cache.deregisterCacheEntryListener(lsnrCfg);
    cache.put(key(key), value(1));
    cache.put(key(key), value(2));
    assertTrue(cache.remove(key(key)));
    // Sleep some time to ensure listener was really removed.
    U.sleep(500);
    assertEquals(0, evts.size());
    cache.registerCacheEntryListener(lsnrCfg);
    cache.deregisterCacheEntryListener(lsnrCfg);
}
Also used : ArrayList(java.util.ArrayList) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) IOException(java.io.IOException)

Example 95 with Duration

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

the class IgniteCacheEntryListenerAbstractTest method checkFilter.

/**
 * @param cache Cache.
 * @param vals Values in cache.
 * @throws Exception If failed.
 */
private void checkFilter(final IgniteCache<Object, Object> cache, Map<Object, Object> vals) throws Exception {
    evts = Collections.synchronizedList(new ArrayList<CacheEntryEvent<?, ?>>());
    // Remove, create, update and expire for half of modified entries.
    final int expEvts = (vals.size() / 2) * 4;
    evtsLatch = new CountDownLatch(expEvts);
    cache.removeAll(vals.keySet());
    cache.putAll(vals);
    final Map<Object, Object> newVals = new HashMap<>();
    for (Object key : vals.keySet()) newVals.put(key, value(-1));
    cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 500))).putAll(newVals);
    U.sleep(1000);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (Object key : newVals.keySet()) {
                if (primaryCache(key, cache.getName()).get(key) != null)
                    return false;
            }
            return true;
        }
    }, 5000);
    evtsLatch.await(5000, MILLISECONDS);
    assertEquals(expEvts, evts.size());
    Set<Object> rmvd = new HashSet<>();
    Set<Object> created = new HashSet<>();
    Set<Object> updated = new HashSet<>();
    Set<Object> expired = new HashSet<>();
    for (CacheEntryEvent<?, ?> evt : evts) {
        Integer key;
        if (useObjects)
            key = ((ListenerTestKey) evt.getKey()).key;
        else
            key = (Integer) evt.getKey();
        assertTrue(key % 2 == 0);
        assertTrue(vals.keySet().contains(evt.getKey()));
        switch(evt.getEventType()) {
            case REMOVED:
                assertEquals(evt.getOldValue(), evt.getValue());
                assertEquals(vals.get(evt.getKey()), evt.getOldValue());
                assertTrue(rmvd.add(evt.getKey()));
                break;
            case CREATED:
                assertEquals(vals.get(evt.getKey()), evt.getValue());
                assertNull(evt.getOldValue());
                assertTrue(rmvd.contains(evt.getKey()));
                assertTrue(created.add(evt.getKey()));
                break;
            case UPDATED:
                assertEquals(value(-1), evt.getValue());
                assertEquals(vals.get(evt.getKey()), evt.getOldValue());
                assertTrue(rmvd.contains(evt.getKey()));
                assertTrue(created.contains(evt.getKey()));
                assertTrue(updated.add(evt.getKey()));
                break;
            case EXPIRED:
                assertEquals(evt.getOldValue(), evt.getValue());
                assertEquals(value(-1), evt.getOldValue());
                assertTrue(rmvd.contains(evt.getKey()));
                assertTrue(created.contains(evt.getKey()));
                assertTrue(updated.contains(evt.getKey()));
                assertTrue(expired.add(evt.getKey()));
                break;
            default:
                fail("Unexpected type: " + evt.getEventType());
        }
    }
    assertEquals(vals.size() / 2, rmvd.size());
    assertEquals(vals.size() / 2, created.size());
    assertEquals(vals.size() / 2, updated.size());
    assertEquals(vals.size() / 2, expired.size());
}
Also used : HashMap(java.util.HashMap) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ArrayList(java.util.ArrayList) Duration(javax.cache.expiry.Duration) CountDownLatch(java.util.concurrent.CountDownLatch) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) HashSet(java.util.HashSet)

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