Search in sources :

Example 51 with ExpiryPolicy

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

the class GridCacheNearOnlyMultiNodeFullApiSelfTest method checkReaderTtl.

/**
 * @param inTx If {@code true} starts explicit transaction.
 * @throws Exception If failed.
 */
private void checkReaderTtl(boolean inTx) throws Exception {
    int ttl = 1000;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttl));
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(fullCache(), 1).get(0);
    c.put(key, 1);
    info("Finished first put.");
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertEquals((Integer) 1, c.get(key));
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
    long startTime = System.currentTimeMillis();
    int fullIdx = nearIdx == 0 ? 1 : 0;
    // Now commit transaction and check that ttl and expire time have been saved.
    Transaction tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).withExpiryPolicy(expiry).put(key, 1);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    long[] expireTimes = new long[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        info("Checking grid: " + grid(i).localNode().id());
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertTrue(entryTtl.get2() > startTime);
            expireTimes[i] = entryTtl.get2();
        }
    }
    // One more update from the same cache entry to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).withExpiryPolicy(expiry).put(key, 2);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertTrue(entryTtl.get2() > startTime);
            expireTimes[i] = entryTtl.get2();
        }
    }
    // And one more update to ensure that ttl is not changed and expire time is not shifted forward.
    U.sleep(100);
    tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).put(key, 4);
    } finally {
        if (tx != null)
            tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertEquals(expireTimes[i], (long) entryTtl.get2());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean applyx() throws IgniteCheckedException {
            try {
                Integer val = c.get(key);
                if (val != null) {
                    info("Value is in cache [key=" + key + ", val=" + val + ']');
                    return false;
                }
                if (!internalCache(c).context().deferredDelete()) {
                    GridCacheEntryEx e0 = internalCache(c).peekEx(key);
                    return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
                } else
                    return true;
            } catch (GridCacheEntryRemovedException ignored) {
                // If e0.valueBytes() thrown this exception then entry has been removed.
                return true;
            }
        }
    }, Math.min(ttl * 10, getTestTimeout())));
    // Ensure that old TTL and expire time are not longer "visible".
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
    // Ensure that next update will not pick old expire time.
    tx = inTx ? transactions().txStart() : null;
    try {
        c.put(key, 10);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    U.sleep(2000);
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
}
Also used : Duration(javax.cache.expiry.Duration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Transaction(org.apache.ignite.transactions.Transaction) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)

Example 52 with ExpiryPolicy

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

the class HadoopJobTracker method jobMetaCache.

/**
 * @return Job meta projection.
 */
@SuppressWarnings("NonPrivateFieldAccessedInSynchronizedContext")
private IgniteInternalCache<HadoopJobId, HadoopJobMetadata> jobMetaCache() {
    IgniteInternalCache<HadoopJobId, HadoopJobMetadata> prj = jobMetaPrj;
    if (prj == null) {
        synchronized (mux) {
            if ((prj = jobMetaPrj) == null) {
                GridCacheAdapter<HadoopJobId, HadoopJobMetadata> sysCache = ctx.kernalContext().cache().internalCache(CU.SYS_CACHE_HADOOP_MR);
                assert sysCache != null;
                mrPlanner = ctx.planner();
                try {
                    ctx.kernalContext().resource().injectGeneric(mrPlanner);
                } catch (IgniteCheckedException e) {
                    // Must not happen.
                    U.error(log, "Failed to inject resources.", e);
                    throw new IllegalStateException(e);
                }
                jobMetaPrj = prj = sysCache;
                if (ctx.configuration().getFinishedJobInfoTtl() > 0) {
                    ExpiryPolicy finishedJobPlc = new ModifiedExpiryPolicy(new Duration(MILLISECONDS, ctx.configuration().getFinishedJobInfoTtl()));
                    finishedJobMetaPrj = prj.withExpiryPolicy(finishedJobPlc);
                } else
                    finishedJobMetaPrj = jobMetaPrj;
            }
        }
    }
    return prj;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Duration(javax.cache.expiry.Duration) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) HadoopJobId(org.apache.ignite.internal.processors.hadoop.HadoopJobId)

Example 53 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy 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 54 with ExpiryPolicy

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

the class GridNearTxPrepareFutureAdapter method onPrepareResponse.

/**
 * @param m Mapping.
 * @param res Response.
 * @param updateMapping Update mapping flag.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
final void onPrepareResponse(GridDistributedTxMapping m, GridNearTxPrepareResponse res, boolean updateMapping) {
    if (res == null)
        return;
    assert res.error() == null : res;
    if (tx.onePhaseCommit() && !res.onePhaseCommit())
        tx.onePhaseCommit(false);
    UUID nodeId = m.primary().id();
    for (Map.Entry<IgniteTxKey, CacheVersionedValue> entry : res.ownedValues().entrySet()) {
        IgniteTxEntry txEntry = tx.entry(entry.getKey());
        assert txEntry != null;
        GridCacheContext cacheCtx = txEntry.context();
        while (true) {
            try {
                if (cacheCtx.isNear()) {
                    GridNearCacheEntry nearEntry = (GridNearCacheEntry) txEntry.cached();
                    CacheVersionedValue tup = entry.getValue();
                    nearEntry.resetFromPrimary(tup.value(), tx.xidVersion(), tup.version(), nodeId, tx.topologyVersion());
                } else if (txEntry.cached().detached()) {
                    GridDhtDetachedCacheEntry detachedEntry = (GridDhtDetachedCacheEntry) txEntry.cached();
                    CacheVersionedValue tup = entry.getValue();
                    detachedEntry.resetFromPrimary(tup.value(), tx.xidVersion());
                }
                break;
            } catch (GridCacheEntryRemovedException ignored) {
                // Retry.
                txEntry.cached(cacheCtx.cache().entryEx(txEntry.key(), tx.topologyVersion()));
            }
        }
    }
    tx.implicitSingleResult(res.returnValue());
    for (IgniteTxKey key : res.filterFailedKeys()) {
        IgniteTxEntry txEntry = tx.entry(key);
        assert txEntry != null : "Missing tx entry for write key: " + key;
        txEntry.op(NOOP);
        assert txEntry.context() != null;
        ExpiryPolicy expiry = txEntry.context().expiryForTxEntry(txEntry);
        if (expiry != null)
            txEntry.ttl(CU.toTtl(expiry.getExpiryForAccess()));
    }
    if (!m.empty()) {
        // This step is very important as near and DHT versions grow separately.
        cctx.versions().onReceived(nodeId, res.dhtVersion());
        if (updateMapping && m.hasNearCacheEntries()) {
            GridCacheVersion writeVer = res.writeVersion();
            if (writeVer == null)
                writeVer = res.dhtVersion();
            // Register DHT version.
            m.dhtVersion(res.dhtVersion(), writeVer);
            GridDistributedTxMapping map = tx.mappings().get(nodeId);
            if (map != null)
                map.dhtVersion(res.dhtVersion(), writeVer);
            tx.readyNearLocks(m, res.pending(), res.committedVersions(), res.rolledbackVersions());
        }
    }
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridDhtDetachedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.colocated.GridDhtDetachedCacheEntry) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) UUID(java.util.UUID) Map(java.util.Map)

Aggregations

ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)54 Duration (javax.cache.expiry.Duration)23 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)15 IgniteCacheExpiryPolicy (org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)11 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)11 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)10 CacheOperationContext (org.apache.ignite.internal.processors.cache.CacheOperationContext)9 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)9 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)9 CacheOperationProvider (com.hazelcast.cache.impl.CacheOperationProvider)7 UUID (java.util.UUID)7 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 Transaction (org.apache.ignite.transactions.Transaction)7 Map (java.util.Map)5 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)5 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)5 IgniteTxTimeoutCheckedException (org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException)5