Search in sources :

Example 11 with CreatedExpiryPolicy

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

the class IgniteCacheExpireAndUpdateConsistencyTest method updateAndEventConsistencyTest.

/**
     * @param node Node.
     * @param cacheName Cache name.
     * @param keyVal Key counter.
     * @param nodesEvts Events map.
     * @param useTx If {@code true} executes update with explicit transaction.
     * @throws Exception If failed.
     */
private void updateAndEventConsistencyTest(final Ignite node, String cacheName, final AtomicInteger keyVal, List<ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>>> nodesEvts, final boolean useTx) throws Exception {
    final ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>> updates = new ConcurrentHashMap<>();
    final int THREADS = 5;
    final int KEYS_PER_THREAD = 100;
    final IgniteCache<TestKey, TestValue> cache = node.cache(cacheName);
    final IgniteCache<TestKey, TestValue> expPlcCache = cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(SECONDS, 2)));
    GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {

        @Override
        public void apply(Integer idx) {
            List<TestKey> keys = new ArrayList<>();
            for (int i = 0; i < KEYS_PER_THREAD; i++) keys.add(new TestKey(keyVal.incrementAndGet()));
            for (TestKey key : keys) {
                expPlcCache.put(key, new TestValue(0));
                List<T2<TestValue, TestValue>> keyUpdates = new ArrayList<>();
                keyUpdates.add(new T2<>(new TestValue(0), (TestValue) null));
                updates.put(key, keyUpdates);
            }
            long stopTime = U.currentTimeMillis() + 10_000;
            int val = 0;
            Set<TestKey> expired = new HashSet<>();
            IgniteTransactions txs = node.transactions();
            while (U.currentTimeMillis() < stopTime) {
                val++;
                TestValue newVal = new TestValue(val);
                for (TestKey key : keys) {
                    Transaction tx = useTx ? txs.txStart(PESSIMISTIC, REPEATABLE_READ) : null;
                    TestValue oldVal = cache.getAndPut(key, newVal);
                    if (tx != null)
                        tx.commit();
                    List<T2<TestValue, TestValue>> keyUpdates = updates.get(key);
                    keyUpdates.add(new T2<>(newVal, oldVal));
                    if (oldVal == null)
                        expired.add(key);
                }
                if (expired.size() == keys.size())
                    break;
            }
            assertEquals(keys.size(), expired.size());
        }
    }, THREADS, "update-thread");
    for (ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>> evts : nodesEvts) checkEvents(updates, evts);
    nodesEvts.clear();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) T2(org.apache.ignite.internal.util.typedef.T2)

Example 12 with CreatedExpiryPolicy

use of javax.cache.expiry.CreatedExpiryPolicy 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, 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, 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, null, oldVal ? UPDATES : null);
    if (create)
        checkEvent(iter, key, CREATED, 10, null);
    if (expire)
        checkEvent(iter, key, EXPIRED, 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, null, oldVal ? 2 : null);
    if (create)
        checkEvent(iter, key, CREATED, 20, null);
    if (expire)
        checkEvent(iter, key, EXPIRED, 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 13 with CreatedExpiryPolicy

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

the class IgniteCacheExpiryStoreLoadSelfTest method checkLocalLoad.

/**
     * @param async If {@code true} uses asynchronous method.
     * @throws Exception If failed.
     */
private void checkLocalLoad(boolean async) throws Exception {
    final IgniteCache<String, Integer> cache = jcache(0).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, TIME_TO_LIVE)));
    List<Integer> keys = primaryKeys(cache, 3);
    if (async)
        cache.localLoadCacheAsync(null, keys.toArray(new Integer[3])).get();
    else
        cache.localLoadCache(null, keys.toArray(new Integer[3]));
    assertEquals(3, cache.localSize());
    boolean res = GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return cache.localSize() == 0;
        }
    }, TIME_TO_LIVE + WAIT_TIME);
    assertTrue(res);
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy)

Example 14 with CreatedExpiryPolicy

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

Aggregations

CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)14 Duration (javax.cache.expiry.Duration)14 Ignite (org.apache.ignite.Ignite)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 AccessedExpiryPolicy (javax.cache.expiry.AccessedExpiryPolicy)2 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)2 CaffeineConfiguration (com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration)1 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 URI (java.net.URI)1