Search in sources :

Example 96 with Duration

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

the class IgniteCacheExpiryPolicyAbstractTest method testNearExpiresWithCacheStore.

/**
 * @throws Exception If failed.
 */
@Test
public void testNearExpiresWithCacheStore() throws Exception {
    if (cacheMode() != PARTITIONED)
        return;
    factory = CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1));
    nearCache = true;
    startGridsMultiThreaded(gridCount());
    IgniteConfiguration clientCfg = getConfiguration("client");
    ((TcpDiscoverySpi) clientCfg.getDiscoverySpi()).setForceServerMode(false);
    Ignite client = startClientGrid("client", clientCfg);
    CacheConfiguration ccfg = cacheConfiguration("testCache");
    ccfg.setCacheStoreFactory(FactoryBuilder.factoryOf(GridCacheTestStore.class));
    // ccfg.setExpiryPolicyFactory( CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)));
    IgniteCache<Object, Object> cache = client.getOrCreateCache(ccfg);
    Integer key = 1;
    cache.put(key, 1);
    // Make entry cached in client NearCache.
    assertEquals(1, cache.get(key));
    assertEquals(1, cache.localPeek(key, CachePeekMode.NEAR));
    waitExpired(key);
    for (int i = 0; i < gridCount(); i++) assertNull(jcache(i).localPeek(key, CachePeekMode.BACKUP, CachePeekMode.PRIMARY));
    assertEquals(null, cache.get(key));
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Duration(javax.cache.expiry.Duration) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) GridCacheTestStore(org.apache.ignite.internal.processors.cache.GridCacheTestStore) IgniteCacheAbstractTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest) Test(org.junit.Test)

Example 97 with Duration

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

the class IgniteCacheExpiryPolicyAbstractTest method testCreateUpdate0.

/**
 * @throws Exception if failed.
 */
@Test
public void testCreateUpdate0() throws Exception {
    startGrids(1);
    long ttl = 60L;
    final String key = "key1";
    final IgniteCache<String, String> cache = jcache();
    for (int i = 0; i < 1000; i++) {
        final IgniteCache<String, String> cache0 = cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(TimeUnit.HOURS, ttl)));
        cache0.put(key, key);
        info("PUT DONE");
    }
    long pSize = grid(0).context().cache().internalCache(DEFAULT_CACHE_NAME).context().ttl().pendingSize();
    assertTrue("Too many pending entries: " + pSize, pSize <= 1);
    cache.remove(key);
    pSize = grid(0).context().cache().internalCache(DEFAULT_CACHE_NAME).context().ttl().pendingSize();
    assertEquals(0, pSize);
}
Also used : Duration(javax.cache.expiry.Duration) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) IgniteCacheAbstractTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest) Test(org.junit.Test)

Example 98 with Duration

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

the class IgniteCacheExpiryPolicyWithStoreAbstractTest method getReadThrough.

/**
 * @throws Exception If failed.
 */
protected void getReadThrough(boolean withExcPlc, TransactionConcurrency txConcurrency, TransactionIsolation txIsolation) throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(0);
    if (withExcPlc)
        cache = cache.withExpiryPolicy(new ExpiryPolicy() {

            @Override
            public Duration getExpiryForCreation() {
                return new Duration(TimeUnit.MILLISECONDS, 501);
            }

            @Override
            public Duration getExpiryForAccess() {
                return new Duration(TimeUnit.MILLISECONDS, 601);
            }

            @Override
            public Duration getExpiryForUpdate() {
                return new Duration(TimeUnit.MILLISECONDS, 701);
            }
        });
    Integer prim = primaryKeys(cache, 1, 1000).get(0);
    Integer back = backupKeys(cache, 1, 1000).get(0);
    Integer near = nearKeys(cache, 1, 1000).get(0);
    Set<Integer> prims = new HashSet<>(primaryKeys(cache, 10, prim + 1));
    Set<Integer> backs = new HashSet<>(backupKeys(cache, 10, back + 1));
    Set<Integer> nears = new HashSet<>(nearKeys(cache, 10, near + 1));
    Set<Integer> keys = new HashSet<>();
    keys.add(prim);
    keys.add(back);
    keys.add(near);
    keys.addAll(prims);
    keys.addAll(backs);
    keys.addAll(nears);
    for (Integer key : keys) storeMap.put(key, key);
    IgniteTransactions transactions = grid(0).transactions();
    Transaction tx = txConcurrency != null ? transactions.txStart(txConcurrency, txIsolation) : null;
    try {
        Collection<Integer> singleKeys = new HashSet<>();
        singleKeys.add(prim);
        singleKeys.add(back);
        singleKeys.add(near);
        assertEquals(3, singleKeys.size());
        for (Integer key : singleKeys) assertEquals(key, cache.get(key));
        Map<Integer, Integer> res = new HashMap<>();
        res.putAll(cache.getAll(prims));
        res.putAll(cache.getAll(backs));
        res.putAll(cache.getAll(nears));
        assertEquals(30, res.size());
        for (Map.Entry<Integer, Integer> e : res.entrySet()) assertEquals(e.getKey(), e.getValue());
    } finally {
        if (tx != null)
            tx.rollback();
    }
    for (Integer key : keys) checkTtl(key, withExcPlc ? 501 : 500, true);
    U.sleep(600);
    for (Integer key : keys) checkExpired(key);
}
Also used : HashMap(java.util.HashMap) Duration(javax.cache.expiry.Duration) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 99 with Duration

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

the class IgniteCacheExpireWhileRebalanceTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(gridName);
    ((TcpDiscoverySpi) cfg.getDiscoverySpi()).setIpFinder(IP_FINDER);
    cfg.setFailureHandler(new StopNodeOrHaltFailureHandler());
    CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setAtomicityMode(ATOMIC);
    ccfg.setCacheMode(PARTITIONED);
    ccfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)));
    cfg.setCacheConfiguration(ccfg);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) StopNodeOrHaltFailureHandler(org.apache.ignite.failure.StopNodeOrHaltFailureHandler) Duration(javax.cache.expiry.Duration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)

Example 100 with Duration

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

the class IgnitePdsWithTtlTest2 method getCacheConfiguration.

/**
 */
public CacheConfiguration getCacheConfiguration(String name) {
    CacheConfiguration ccfg = new CacheConfiguration();
    ccfg.setName(name);
    ccfg.setAtomicityMode(ATOMIC);
    ccfg.setBackups(1);
    ccfg.setAffinity(new RendezvousAffinityFunction(false, 32768));
    ccfg.setEagerTtl(true);
    ccfg.setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 20)));
    return ccfg;
}
Also used : RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) Duration(javax.cache.expiry.Duration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

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