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