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);
}
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);
}
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);
}
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);
}
}
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));
}
}
}
Aggregations