Search in sources :

Example 26 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy in project hazelcast by hazelcast.

the class CachePutAllMessageTask method prepareOperation.

@Override
protected Operation prepareOperation() {
    CacheOperationProvider operationProvider = getOperationProvider(parameters.name);
    ExpiryPolicy expiryPolicy = (ExpiryPolicy) nodeEngine.toObject(parameters.expiryPolicy);
    return operationProvider.createPutAllOperation(parameters.entries, expiryPolicy, parameters.completionId);
}
Also used : CacheOperationProvider(com.hazelcast.cache.impl.CacheOperationProvider) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy)

Example 27 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy in project hazelcast by hazelcast.

the class CachePutMessageTask method prepareOperation.

@Override
protected Operation prepareOperation() {
    CacheOperationProvider operationProvider = getOperationProvider(parameters.name);
    ExpiryPolicy expiryPolicy = (ExpiryPolicy) nodeEngine.toObject(parameters.expiryPolicy);
    return operationProvider.createPutOperation(parameters.key, parameters.value, expiryPolicy, parameters.get, parameters.completionId);
}
Also used : CacheOperationProvider(com.hazelcast.cache.impl.CacheOperationProvider) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy)

Example 28 with ExpiryPolicy

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

the class IgniteTxAdapter method conflictResolve.

/**
 * Resolve DR conflict.
 *
 * @param op Initially proposed operation.
 * @param txEntry TX entry being updated.
 * @param newVal New value.
 * @param newVer New version.
 * @param old Old entry.
 * @return Tuple with adjusted operation type and conflict context.
 * @throws IgniteCheckedException In case of eny exception.
 * @throws GridCacheEntryRemovedException If entry got removed.
 */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
protected IgniteBiTuple<GridCacheOperation, GridCacheVersionConflictContext> conflictResolve(GridCacheOperation op, IgniteTxEntry txEntry, CacheObject newVal, GridCacheVersion newVer, GridCacheEntryEx old) throws IgniteCheckedException, GridCacheEntryRemovedException {
    assert newVer != null;
    // 1. Calculate TTL and expire time.
    long newTtl = txEntry.ttl();
    long newExpireTime = txEntry.conflictExpireTime();
    // 1.1. If TTL is not changed, then calculate it based on expiry.
    if (newTtl == CU.TTL_NOT_CHANGED) {
        ExpiryPolicy expiry = txEntry.context().expiryForTxEntry(txEntry);
        if (expiry != null) {
            if (op == CREATE)
                newTtl = CU.toTtl(expiry.getExpiryForCreation());
            else if (op == UPDATE)
                newTtl = CU.toTtl(expiry.getExpiryForUpdate());
        }
    }
    // 1.2. If TTL is set to zero, then mark operation as "DELETE".
    if (newTtl == CU.TTL_ZERO) {
        op = DELETE;
        newTtl = CU.TTL_ETERNAL;
    }
    // 1.3. If TTL is still not changed, then either use old entry TTL or set it to "ETERNAL".
    if (newTtl == CU.TTL_NOT_CHANGED) {
        if (old.isNewLocked())
            newTtl = CU.TTL_ETERNAL;
        else {
            newTtl = old.rawTtl();
            newExpireTime = old.rawExpireTime();
        }
    }
    // TTL must be resolved at this point.
    assert newTtl != CU.TTL_ZERO && newTtl != CU.TTL_NOT_CHANGED;
    // 1.4 If expire time was not set explicitly, then calculate it.
    if (newExpireTime == CU.EXPIRE_TIME_CALCULATE)
        newExpireTime = CU.toExpireTime(newTtl);
    // Expire time must be resolved at this point.
    assert newExpireTime != CU.EXPIRE_TIME_CALCULATE;
    // Construct old entry info.
    GridCacheVersionedEntryEx oldEntry = old.versionedEntry(txEntry.keepBinary());
    // Construct new entry info.
    GridCacheContext entryCtx = txEntry.context();
    GridCacheVersionedEntryEx newEntry = new GridCacheLazyPlainVersionedEntry(entryCtx, txEntry.key(), newVal, newTtl, newExpireTime, newVer, false, txEntry.keepBinary());
    GridCacheVersionConflictContext ctx = old.context().conflictResolve(oldEntry, newEntry, false);
    if (ctx.isMerge()) {
        Object resVal = ctx.mergeValue();
        if ((op == CREATE || op == UPDATE) && resVal == null)
            op = DELETE;
        else if (op == DELETE && resVal != null)
            op = old.isNewLocked() ? CREATE : UPDATE;
    }
    return F.t(op, ctx);
}
Also used : GridCacheVersionConflictContext(org.apache.ignite.internal.processors.cache.version.GridCacheVersionConflictContext) GridCacheLazyPlainVersionedEntry(org.apache.ignite.internal.processors.cache.version.GridCacheLazyPlainVersionedEntry) GridCacheVersionedEntryEx(org.apache.ignite.internal.processors.cache.version.GridCacheVersionedEntryEx) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 29 with ExpiryPolicy

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

the class IgniteTxAdapter method applyTransformClosures.

/**
 * @param txEntry Entry to process.
 * @param metrics {@code True} if metrics should be updated.
 * @param ret Optional return value to initialize.
 * @return Tuple containing transformation results.
 * @throws IgniteCheckedException If failed to get previous value for transform.
 * @throws GridCacheEntryRemovedException If entry was concurrently deleted.
 */
protected IgniteBiTuple<GridCacheOperation, CacheObject> applyTransformClosures(IgniteTxEntry txEntry, boolean metrics, @Nullable GridCacheReturn ret) throws GridCacheEntryRemovedException, IgniteCheckedException {
    assert txEntry.op() != TRANSFORM || !F.isEmpty(txEntry.entryProcessors()) : txEntry;
    GridCacheContext cacheCtx = txEntry.context();
    assert cacheCtx != null;
    if (isSystemInvalidate())
        return F.t(cacheCtx.writeThrough() ? RELOAD : DELETE, null);
    if (F.isEmpty(txEntry.entryProcessors())) {
        if (ret != null)
            ret.value(cacheCtx, txEntry.value(), txEntry.keepBinary());
        return F.t(txEntry.op(), txEntry.value());
    } else {
        T2<GridCacheOperation, CacheObject> calcVal = txEntry.entryProcessorCalculatedValue();
        if (calcVal != null)
            return calcVal;
        boolean recordEvt = cctx.gridEvents().isRecordable(EVT_CACHE_OBJECT_READ);
        final boolean keepBinary = txEntry.keepBinary();
        CacheObject cacheVal;
        if (txEntry.hasValue())
            cacheVal = txEntry.value();
        else if (txEntry.hasOldValue())
            cacheVal = txEntry.oldValue();
        else {
            cacheVal = txEntry.cached().innerGet(null, this, /*read through*/
            false, /*metrics*/
            metrics, /*event*/
            recordEvt, /*subjId*/
            subjId, /*closure name */
            recordEvt ? F.first(txEntry.entryProcessors()).get1() : null, resolveTaskName(), null, keepBinary);
        }
        boolean modified = false;
        Object val = null;
        Object key = null;
        GridCacheVersion ver;
        try {
            ver = txEntry.cached().version();
        } catch (GridCacheEntryRemovedException e) {
            assert optimistic() : txEntry;
            if (log.isDebugEnabled())
                log.debug("Failed to get entry version: [msg=" + e.getMessage() + ']');
            ver = null;
        }
        for (T2<EntryProcessor<Object, Object, Object>, Object[]> t : txEntry.entryProcessors()) {
            CacheInvokeEntry<Object, Object> invokeEntry = new CacheInvokeEntry<>(txEntry.key(), key, cacheVal, val, ver, keepBinary, txEntry.cached());
            Object procRes = null;
            Exception err = null;
            try {
                EntryProcessor<Object, Object, Object> processor = t.get1();
                procRes = processor.process(invokeEntry, t.get2());
                val = invokeEntry.getValue();
                key = invokeEntry.key();
            } catch (Exception e) {
                err = e;
            }
            if (ret != null) {
                if (err != null || procRes != null)
                    ret.addEntryProcessResult(txEntry.context(), txEntry.key(), null, procRes, err, keepBinary);
                else
                    ret.invokeResult(true);
            }
            modified |= invokeEntry.modified();
        }
        if (modified)
            cacheVal = cacheCtx.toCacheObject(cacheCtx.unwrapTemporary(val));
        GridCacheOperation op = modified ? (cacheVal == null ? DELETE : UPDATE) : NOOP;
        if (op == NOOP) {
            ExpiryPolicy expiry = cacheCtx.expiryForTxEntry(txEntry);
            if (expiry != null) {
                long ttl = CU.toTtl(expiry.getExpiryForAccess());
                txEntry.ttl(ttl);
                if (ttl == CU.TTL_ZERO)
                    op = DELETE;
            }
        }
        return F.t(op, cacheVal);
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IOException(java.io.IOException) ObjectStreamException(java.io.ObjectStreamException) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) EntryProcessor(javax.cache.processor.EntryProcessor) 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)

Example 30 with ExpiryPolicy

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

the class IgniteCacheConfigVariationsFullApiTest method _testEvictExpired.

/**
 * TODO GG-11133.
 * @throws Exception In case of error.
 */
public void _testEvictExpired() throws Exception {
    final IgniteCache<String, Integer> cache = jcache();
    final String key = primaryKeysForCache(1).get(0);
    cache.put(key, 1);
    assertEquals((Integer) 1, cache.get(key));
    long ttl = 500;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    grid(0).cache(cacheName()).withExpiryPolicy(expiry).put(key, 1);
    boolean wait = waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                if (jcache(i).localPeek(key) != null)
                    return false;
            }
            return true;
        }
    }, ttl + 1000);
    assertTrue("Failed to wait for entry expiration.", wait);
    // Expired entry should not be swapped.
    cache.localEvict(Collections.singleton(key));
    assertNull(cache.localPeek("key"));
    assertNull(cache.localPeek(key, ONHEAP));
    assertTrue(cache.localSize() == 0);
    if (storeEnabled()) {
        load(cache, key, true);
        Affinity<String> aff = ignite(0).affinity(cacheName());
        for (int i = 0; i < gridCount(); i++) {
            if (aff.isPrimary(grid(i).cluster().localNode(), key))
                assertEquals(1, jcache(i).localPeek(key));
            if (aff.isBackup(grid(i).cluster().localNode(), key))
                assertEquals(1, jcache(i).localPeek(key));
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

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