Search in sources :

Example 36 with Duration

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

the class GridCacheTtlManagerEvictionSelfTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setCacheMode(cacheMode);
    ccfg.setEagerTtl(true);
    ccfg.setEvictionPolicy(new FifoEvictionPolicy(ENTRIES_LIMIT, 100));
    ccfg.setOnheapCacheEnabled(true);
    ccfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12)));
    cfg.setCacheConfiguration(ccfg);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Duration(javax.cache.expiry.Duration) FifoEvictionPolicy(org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicy) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 37 with Duration

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

the class GridCacheTtlManagerNotificationTest method testThatNotificationWorkAsExpected.

/**
 * @throws Exception If failed.
 */
@Test
public void testThatNotificationWorkAsExpected() throws Exception {
    try (final Ignite g = startGrid(0)) {
        final BlockingArrayQueue<Event> queue = new BlockingArrayQueue<>();
        g.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                queue.add(evt);
                return true;
            }
        }, EventType.EVT_CACHE_OBJECT_EXPIRED);
        final String key = "key";
        IgniteCache<Object, Object> cache = g.cache(DEFAULT_CACHE_NAME);
        ExpiryPolicy plc1 = new CreatedExpiryPolicy(new Duration(MILLISECONDS, 100_000));
        cache.withExpiryPolicy(plc1).put(key + 1, 1);
        // Cleaner should see entry.
        Thread.sleep(1_000);
        ExpiryPolicy plc2 = new CreatedExpiryPolicy(new Duration(MILLISECONDS, 1000));
        cache.withExpiryPolicy(plc2).put(key + 2, 1);
        // We should receive event about second entry expiration.
        assertNotNull(queue.poll(5, SECONDS));
    }
}
Also used : CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) BlockingArrayQueue(org.eclipse.jetty.util.BlockingArrayQueue) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 38 with Duration

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

the class PendingTreeCorruptionTest method testCorruptionWhileLoadingData.

/**
 */
@Test
public void testCorruptionWhileLoadingData() throws Exception {
    IgniteEx ig = startGrid(0);
    ig.cluster().state(ClusterState.ACTIVE);
    String expireCacheName = "cacheWithExpire";
    String regularCacheName = "cacheWithoutExpire";
    String grpName = "cacheGroup";
    IgniteCache<Object, Object> expireCache = ig.getOrCreateCache(new CacheConfiguration<>(expireCacheName).setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(MINUTES, 10))).setGroupName(grpName));
    IgniteCache<Object, Object> regularCache = ig.getOrCreateCache(new CacheConfiguration<>(regularCacheName).setGroupName(grpName));
    // This will initialize partition and cache structures.
    expireCache.put(0, 0);
    expireCache.remove(0);
    int expireCacheId = CU.cacheGroupId(expireCacheName, grpName);
    CacheGroupContext grp = ig.context().cache().cacheGroup(CU.cacheId(grpName));
    IgniteCacheOffheapManager.CacheDataStore store = grp.topology().localPartition(0).dataStore();
    assertNotNull(store);
    // Get pending tree of expire cache.
    PendingEntriesTree pendingTree = store.pendingTree();
    long year = TimeUnit.DAYS.toMillis(365);
    long expiration = System.currentTimeMillis() + year;
    ig.context().cache().context().database().checkpointReadLock();
    try {
        // Carefully calculated number. Just enough for the first split to happen, but not more.
        for (int i = 0; i < 202; i++) // link != 0
        pendingTree.putx(new PendingRow(expireCacheId, expiration, expiration + i));
        // Open cursor, it'll cache first leaf of the tree.
        GridCursor<PendingRow> cur = pendingTree.find(null, new PendingRow(expireCacheId, expiration + year, 0), PendingEntriesTree.WITHOUT_KEY);
        // Required for "do" loop to work.
        assertTrue(cur.next());
        int cnt = 0;
        // Emulate real expiry loop but with a more precise control.
        do {
            PendingRow row = cur.get();
            pendingTree.removex(row);
            // with its sibling, meaning that cached "nextPageId" points to empty page from reuse list.
            if (row.link - row.expireTime == 100) {
                // Put into another cache will take a page from reuse list first. This means that cached
                // "nextPageId" points to a data page.
                regularCache.put(0, 0);
            }
            cnt++;
        } while (cur.next());
        assertEquals(202, cnt);
    } finally {
        ig.context().cache().context().database().checkpointReadUnlock();
    }
}
Also used : Duration(javax.cache.expiry.Duration) PendingEntriesTree(org.apache.ignite.internal.processors.cache.tree.PendingEntriesTree) PendingRow(org.apache.ignite.internal.processors.cache.tree.PendingRow) IgniteCacheOffheapManager(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager) IgniteEx(org.apache.ignite.internal.IgniteEx) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 39 with Duration

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

the class GridCacheContinuousQueryAbstractSelfTest method testExpired.

/**
 * @throws Exception If failed.
 */
@Test
public void testExpired() throws Exception {
    IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 1000)));
    final Map<Object, Object> map = new ConcurrentHashMap<>();
    final CountDownLatch latch = new CountDownLatch(2);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setIncludeExpired(true);
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent<?, ?> e : evts) {
                if (e.getEventType() == EventType.EXPIRED) {
                    assertEquals(e.getOldValue(), e.getValue());
                    map.put(e.getKey(), e.getOldValue());
                    latch.countDown();
                }
            }
        }
    });
    try (QueryCursor<Cache.Entry<Object, Object>> ignored = cache.query(qry)) {
        cachePut(cache, 1, 1);
        cachePut(cache, 2, 2);
        // Wait for expiration.
        Thread.sleep(2000);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(2, map.size());
        assertEquals(1, (int) map.get(1));
        assertEquals(2, (int) map.get(2));
    }
}
Also used : Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 40 with Duration

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

the class IgniteCacheWriteBehindNoUpdateSelfTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    CacheConfiguration<String, Long> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    ccfg.setCacheMode(CacheMode.PARTITIONED);
    ccfg.setBackups(1);
    ccfg.setReadFromBackup(true);
    ccfg.setCopyOnRead(false);
    ccfg.setName(THROTTLES_CACHE_NAME);
    Duration expiryDuration = new Duration(TimeUnit.MINUTES, 1);
    ccfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(expiryDuration));
    ccfg.setReadThrough(false);
    ccfg.setWriteThrough(true);
    ccfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(new TestCacheStore()));
    cfg.setCacheConfiguration(ccfg);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Duration(javax.cache.expiry.Duration) FactoryBuilder(javax.cache.configuration.FactoryBuilder) 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