Search in sources :

Example 86 with Duration

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

the class GridCacheTtlManagerLoadTest method testLoad.

/**
 * @throws Exception If failed.
 */
@Test
public void testLoad() throws Exception {
    cacheMode = REPLICATED;
    final IgniteKernal g = (IgniteKernal) startGrid(0);
    try {
        final AtomicBoolean stop = new AtomicBoolean();
        IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                IgniteCache<Object, Object> cache = g.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, 1000)));
                long key = 0;
                while (!stop.get()) {
                    cache.put(key, key);
                    key++;
                }
                return null;
            }
        }, 1);
        GridCacheTtlManager ttlMgr = g.internalCache(DEFAULT_CACHE_NAME).context().ttl();
        for (int i = 0; i < 5; i++) {
            U.sleep(5000);
            ttlMgr.printMemoryStats();
        }
        stop.set(true);
        fut.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCache(org.apache.ignite.IgniteCache) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) Test(org.junit.Test)

Example 87 with Duration

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

the class GridCacheTtlManagerSelfTest method checkTtl.

/**
 * @param mode Cache mode.
 * @throws Exception If failed.
 */
private void checkTtl(CacheMode mode) throws Exception {
    cacheMode = mode;
    final IgniteKernal g = (IgniteKernal) startGrid(0);
    try {
        final String key = "key";
        g.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, 1000))).put(key, 1);
        assertEquals(1, g.cache(DEFAULT_CACHE_NAME).get(key));
        U.sleep(1100);
        GridTestUtils.retryAssert(log, 10, 100, new CAX() {

            @Override
            public void applyx() {
                // Check that no more entries left in the map.
                assertNull(g.cache(DEFAULT_CACHE_NAME).get(key));
                if (!g.internalCache(DEFAULT_CACHE_NAME).context().deferredDelete())
                    assertNull(g.internalCache(DEFAULT_CACHE_NAME).map().getEntry(g.internalCache(DEFAULT_CACHE_NAME).context(), g.internalCache(DEFAULT_CACHE_NAME).context().toCacheKeyObject(key)));
            }
        });
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 88 with Duration

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

the class CacheContinuousWithTransformerReplicatedSelfTest method testExpired.

/**
 * @throws Exception If failed.
 */
@Test
public void testExpired() throws Exception {
    Ignite ignite = gridToRunQuery();
    IgniteCache<Integer, Employee> cache = ignite.cache(DEFAULT_CACHE_NAME);
    cache = cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 100)));
    final Set<Integer> keys = new GridConcurrentHashSet<>();
    final CountDownLatch latch = new CountDownLatch(2);
    ContinuousQueryWithTransformer<Integer, Employee, Integer> qry = new ContinuousQueryWithTransformer<>();
    qry.setIncludeExpired(true);
    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, Employee>() {

        @Override
        public boolean evaluate(CacheEntryEvent event) throws CacheEntryListenerException {
            return event.getEventType() == EventType.EXPIRED;
        }
    }));
    qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Employee>, Integer>() {

        @Override
        public Integer apply(CacheEntryEvent<? extends Integer, ? extends Employee> evt) {
            assertNotNull(evt.getValue());
            assertNotNull(evt.getOldValue());
            return evt.getKey();
        }
    }));
    qry.setLocalListener(new EventListener<Integer>() {

        @Override
        public void onUpdated(Iterable<? extends Integer> evts) {
            for (Integer key : evts) {
                keys.add(key);
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Employee>> ignored = cache.query(qry)) {
        cache.put(1, new Employee(SARAH_CONNOR, 42));
        cache.put(2, new Employee(JOHN_CONNOR, 42));
        // Wait for expiration.
        latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(2, keys.size());
        assertTrue(keys.contains(1));
        assertTrue(keys.contains(2));
    }
}
Also used : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 89 with Duration

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

the class DiagnosticLogForPartitionStatesTest method testShouldNotPrintMessageIfPartitionHasOtherCounterButHasCustomExpiryPolicyCacheGroup.

/**
 * Tests that partitions validation is not triggered when a cache in the cache group (at least one of them)
 * uses custom expiry policy.
 */
@Test
public void testShouldNotPrintMessageIfPartitionHasOtherCounterButHasCustomExpiryPolicyCacheGroup() throws Exception {
    String grpName = "test-group";
    CacheConfiguration<Integer, Integer> cfg1 = new CacheConfiguration<Integer, Integer>("cache-1").setBackups(1).setGroupName(grpName);
    CacheConfiguration<Integer, Integer> cfg2 = new CacheConfiguration<Integer, Integer>("cache-2").setBackups(1).setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1))).setGroupName(grpName);
    doTest(Arrays.asList(cfg1, cfg2), false);
}
Also used : Duration(javax.cache.expiry.Duration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 90 with Duration

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

the class GridCacheBasicApiAbstractTest method testPutWithExpiration.

/**
 * @throws Exception In case of error.
 */
@Test
public void testPutWithExpiration() throws Exception {
    MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.ENTRY_LOCK);
    MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.EXPIRATION);
    MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.CACHE_EVENTS);
    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) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) 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