Search in sources :

Example 76 with Duration

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

the class GridDhtTxPrepareFuture method onEntriesLocked.

/**
 */
private void onEntriesLocked() {
    ret = new GridCacheReturn(null, tx.localResult(), true, null, null, true);
    for (IgniteTxEntry writeEntry : req.writes()) {
        IgniteTxEntry txEntry = tx.entry(writeEntry.txKey());
        assert txEntry != null : writeEntry;
        GridCacheContext cacheCtx = txEntry.context();
        GridCacheEntryEx cached = txEntry.cached();
        ExpiryPolicy expiry = cacheCtx.expiryForTxEntry(txEntry);
        cctx.database().checkpointReadLock();
        try {
            if ((txEntry.op() == CREATE || txEntry.op() == UPDATE) && txEntry.conflictExpireTime() == CU.EXPIRE_TIME_CALCULATE) {
                if (expiry != null) {
                    cached.unswap(true);
                    Duration duration = cached.hasValue() ? expiry.getExpiryForUpdate() : expiry.getExpiryForCreation();
                    txEntry.ttl(CU.toTtl(duration));
                }
            }
            boolean hasFilters = !F.isEmptyOrNulls(txEntry.filters()) && !F.isAlwaysTrue(txEntry.filters());
            CacheObject val;
            CacheObject oldVal = null;
            boolean readOld = hasFilters || retVal || txEntry.op() == DELETE || txEntry.op() == TRANSFORM || tx.nearOnOriginatingNode() || tx.hasInterceptor();
            if (readOld) {
                boolean readThrough = !txEntry.skipStore() && (txEntry.op() == TRANSFORM || ((retVal || hasFilters) && cacheCtx.config().isLoadPreviousValue()));
                boolean evt = retVal || txEntry.op() == TRANSFORM;
                EntryProcessor entryProc = null;
                if (evt && txEntry.op() == TRANSFORM)
                    entryProc = F.first(txEntry.entryProcessors()).get1();
                final boolean keepBinary = txEntry.keepBinary();
                val = oldVal = cached.innerGet(null, tx, readThrough, /*metrics*/
                retVal, /*event*/
                evt, entryProc, tx.resolveTaskName(), null, keepBinary);
                if (retVal || txEntry.op() == TRANSFORM) {
                    if (!F.isEmpty(txEntry.entryProcessors())) {
                        invoke = true;
                        if (txEntry.hasValue())
                            val = txEntry.value();
                        KeyCacheObject key = txEntry.key();
                        Object procRes = null;
                        Exception err = null;
                        boolean modified = false;
                        txEntry.oldValueOnPrimary(val != null);
                        for (T2<EntryProcessor<Object, Object, Object>, Object[]> t : txEntry.entryProcessors()) {
                            CacheInvokeEntry<Object, Object> invokeEntry = new CacheInvokeEntry<>(key, val, txEntry.cached().version(), keepBinary, txEntry.cached());
                            EntryProcessor<Object, Object, Object> processor = t.get1();
                            IgniteThread.onEntryProcessorEntered(false);
                            if (cctx.kernalContext().deploy().enabled() && cctx.kernalContext().deploy().isGlobalLoader(processor.getClass().getClassLoader())) {
                                U.restoreDeploymentContext(cctx.kernalContext(), cctx.kernalContext().deploy().getClassLoaderId(processor.getClass().getClassLoader()));
                            }
                            try {
                                procRes = processor.process(invokeEntry, t.get2());
                                val = cacheCtx.toCacheObject(invokeEntry.getValue(true));
                                if (// no validation for remove case
                                val != null)
                                    cacheCtx.validateKeyAndValue(key, val);
                            } catch (Exception e) {
                                err = e;
                                break;
                            } finally {
                                IgniteThread.onEntryProcessorLeft();
                            }
                            modified |= invokeEntry.modified();
                        }
                        if (modified)
                            val = cacheCtx.toCacheObject(cacheCtx.unwrapTemporary(val));
                        GridCacheOperation op = modified ? (val == null ? DELETE : UPDATE) : NOOP;
                        if (op == NOOP) {
                            GridCacheAdapter<?, ?> cache = writeEntry.context().cache();
                            if (cache.context().statisticsEnabled())
                                cache.metrics0().onReadOnlyInvoke(oldVal != null);
                            if (expiry != null) {
                                long ttl = CU.toTtl(expiry.getExpiryForAccess());
                                txEntry.ttl(ttl);
                                if (ttl == CU.TTL_ZERO)
                                    op = DELETE;
                            }
                        }
                        txEntry.entryProcessorCalculatedValue(new T2<>(op, op == NOOP ? null : val));
                        if (retVal) {
                            if (err != null || procRes != null)
                                ret.addEntryProcessResult(txEntry.context(), key, null, procRes, err, keepBinary);
                            else
                                ret.invokeResult(true);
                        }
                    } else if (retVal)
                        ret.value(cacheCtx, val, keepBinary, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId));
                }
                if (hasFilters && !cacheCtx.isAll(cached, txEntry.filters())) {
                    if (expiry != null)
                        txEntry.ttl(CU.toTtl(expiry.getExpiryForAccess()));
                    txEntry.op(GridCacheOperation.NOOP);
                    if (filterFailedKeys == null)
                        filterFailedKeys = new ArrayList<>();
                    filterFailedKeys.add(cached.txKey());
                    ret.success(false);
                } else
                    ret.success(txEntry.op() != DELETE || cached.hasValue());
            }
            // Send old value in case if rebalancing is not finished.
            final boolean sndOldVal = !cacheCtx.isLocal() && !cacheCtx.topology().rebalanceFinished(tx.topologyVersion());
            if (sndOldVal) {
                if (oldVal == null && !readOld) {
                    oldVal = cached.innerGet(null, tx, /*readThrough*/
                    false, /*metrics*/
                    false, /*event*/
                    false, /*transformClo*/
                    null, /*taskName*/
                    null, /*expiryPlc*/
                    null, /*keepBinary*/
                    true);
                }
                if (oldVal != null)
                    oldVal.prepareMarshal(cacheCtx.cacheObjectContext());
                txEntry.oldValue(oldVal);
            }
        } catch (IgniteCheckedException e) {
            U.error(log, "Failed to get result value for cache entry: " + cached, e);
            onError(e);
        } catch (GridCacheEntryRemovedException e) {
            // Entry was unlocked by concurrent rollback.
            onError(tx.rollbackException());
        } finally {
            cctx.database().checkpointReadUnlock();
        }
    }
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) ArrayList(java.util.ArrayList) Duration(javax.cache.expiry.Duration) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) EntryProcessor(javax.cache.processor.EntryProcessor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheInvokeEntry(org.apache.ignite.internal.processors.cache.CacheInvokeEntry) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 77 with Duration

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

the class ExpiryPolicies method expiryPolicyForIndividualEntry.

@Test
void expiryPolicyForIndividualEntry() {
    IgniteConfiguration igniteCfg = new IgniteConfiguration();
    try (Ignite ignite = Ignition.start(igniteCfg)) {
        // tag::expiry2[]
        CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<Integer, String>("myCache");
        ignite.createCache(cacheCfg);
        IgniteCache cache = ignite.cache("myCache").withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MINUTES, 5)));
        // if the cache does not contain key 1, the entry will expire after 5 minutes
        cache.put(1, "first value");
    // end::expiry2[]
    }
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.jupiter.api.Test)

Example 78 with Duration

use of javax.cache.expiry.Duration 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 79 with Duration

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

the class IgniteCacheEntryListenerExpiredEventsTest method checkExpiredEvents.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void checkExpiredEvents(CacheConfiguration<Object, Object> ccfg) throws Exception {
    IgniteCache<Object, Object> cache = ignite(0).createCache(ccfg);
    try {
        evtCntr = new AtomicInteger();
        CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new ExpiredListenerFactory(), null, true, false);
        cache.registerCacheEntryListener(lsnrCfg);
        IgniteCache<Object, Object> expiryCache = cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 500)));
        expiryCache.put(1, 1);
        for (int i = 0; i < 10; i++) cache.get(i);
        boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return evtCntr.get() > 0;
            }
        }, 5000);
        assertTrue(wait);
        U.sleep(100);
        assertEquals(1, evtCntr.get());
    } finally {
        ignite(0).destroyCache(cache.getName());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Duration(javax.cache.expiry.Duration) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy)

Example 80 with Duration

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

the class CacheMvccTransactionsTest method testExpiration.

/**
 * @throws Exception If failed.
 */
@Ignore("https://issues.apache.org/jira/browse/IGNITE-7311")
@Test
public void testExpiration() throws Exception {
    final IgniteEx node = startGrid(0);
    IgniteCache cache = node.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, 64));
    final IgniteCache expiryCache = cache.withExpiryPolicy(new TouchedExpiryPolicy(new Duration(TimeUnit.SECONDS, 1)));
    for (int i = 0; i < 10; i++) expiryCache.put(1, i);
    assertTrue("Failed to wait for expiration", GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return expiryCache.localPeek(1) == null;
        }
    }, 5000));
    for (int i = 0; i < 11; i++) {
        if (i % 2 == 0)
            expiryCache.put(1, i);
        else
            expiryCache.remove(1);
    }
    assertTrue("Failed to wait for expiration", GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return expiryCache.localPeek(1) == null;
        }
    }, 5000));
    expiryCache.put(1, 1);
    assertTrue("Failed to wait for expiration", GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            try {
                GridCacheContext cctx = node.context().cache().context().cacheContext(CU.cacheId(DEFAULT_CACHE_NAME));
                KeyCacheObject key = cctx.toCacheKeyObject(1);
                return cctx.offheap().read(cctx, key) == null;
            } catch (Exception e) {
                fail();
                return false;
            }
        }
    }, 5000));
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteCache(org.apache.ignite.IgniteCache) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Ignore(org.junit.Ignore) 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