use of javax.cache.expiry.Duration in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method checkTtl.
/**
* @param inTx In tx flag.
* @param oldEntry {@code True} to check TTL on old entry, {@code false} on new.
* @throws Exception If failed.
*/
private void checkTtl(boolean inTx, boolean oldEntry) throws Exception {
// TODO GG-11133.
if (true)
return;
int ttl = 1000;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
final IgniteCache<String, Integer> c = jcache();
final String key = primaryKeysForCache(1).get(0);
IgnitePair<Long> entryTtl;
if (oldEntry) {
c.put(key, 1);
entryTtl = entryTtl(serverNodeCache(), key);
assertEquals((Long) 0L, entryTtl.get1());
assertEquals((Long) 0L, entryTtl.get2());
}
long startTime = System.currentTimeMillis();
if (inTx) {
// Rollback transaction for the first time.
Transaction tx = transactions().txStart();
try {
jcache().withExpiryPolicy(expiry).put(key, 1);
} finally {
tx.rollback();
}
if (oldEntry) {
entryTtl = entryTtl(serverNodeCache(), key);
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals((Long) 0L, entryTtl.get1());
assertEquals((Long) 0L, entryTtl.get2());
}
}
// Now commit transaction and check that ttl and expire time have been saved.
Transaction tx = inTx ? transactions().txStart() : null;
try {
jcache().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++) {
if (grid(i).affinity(cacheName()).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// One more update from the same cache entry to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? transactions().txStart() : null;
try {
jcache().withExpiryPolicy(expiry).put(key, 2);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(cacheName()).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// And one more direct update to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? transactions().txStart() : null;
try {
jcache().withExpiryPolicy(expiry).put(key, 3);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(cacheName()).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// And one more update to ensure that ttl is not changed and expire time is not shifted forward.
U.sleep(100);
log.info("Put 4");
tx = inTx ? transactions().txStart() : null;
try {
jcache().put(key, 4);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
log.info("Put 4 done");
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(cacheName()).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertEquals(expireTimes[i], (long) curEntryTtl.get2());
}
}
// Avoid reloading from store.
storeStgy.removeFromStore(key);
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@SuppressWarnings("unchecked")
@Override
public boolean applyx() {
try {
Integer val = c.get(key);
if (val != null) {
info("Value is in cache [key=" + key + ", val=" + val + ']');
return false;
}
// Get "cache" field from GridCacheProxyImpl.
GridCacheAdapter c0 = cacheFromCtx(c);
if (!c0.context().deferredDelete()) {
GridCacheEntryEx e0 = c0.peekEx(key);
return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
} else
return true;
} catch (GridCacheEntryRemovedException e) {
throw new RuntimeException(e);
}
}
}, Math.min(ttl * 10, getTestTimeout())));
IgniteCache srvNodeCache = serverNodeCache();
if (!isMultiJvmObject(srvNodeCache)) {
GridCacheAdapter internalCache = internalCache(srvNodeCache);
if (internalCache.isLocal())
return;
}
assert c.get(key) == null;
// Ensure that old TTL and expire time are not longer "visible".
entryTtl = entryTtl(srvNodeCache, 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 {
jcache().put(key, 10);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
U.sleep(2000);
entryTtl = entryTtl(srvNodeCache, key);
assertEquals((Integer) 10, c.get(key));
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(0, (long) entryTtl.get1());
assertEquals(0, (long) entryTtl.get2());
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class IgniteCacheTtlCleanupSelfTest method testDeferredDeleteTtl.
/**
* @throws Exception If failed.
*/
public void testDeferredDeleteTtl() throws Exception {
IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5)));
int cnt = GridDhtLocalPartition.MAX_DELETE_QUEUE_SIZE / PART_NUM + 100;
for (long i = 0; i < cnt; i++) grid(0).cache(DEFAULT_CACHE_NAME).put(i * PART_NUM, i);
for (int i = 0; i < cnt; i++) cache.put(i * PART_NUM, i);
// Wait 5 seconds.
Thread.sleep(6_000);
assertEquals(cnt, grid(0).cache(DEFAULT_CACHE_NAME).size());
GridCacheAdapter<Object, Object> cacheAdapter = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
IgniteCacheObjectProcessor cacheObjects = cacheAdapter.context().cacheObjects();
CacheObjectContext cacheObjCtx = cacheAdapter.context().cacheObjectContext();
for (int i = 0; i < 100; i++) assertNull(cacheAdapter.map().getEntry(cacheAdapter.context(), cacheObjects.toCacheKeyObject(cacheObjCtx, null, i, true)));
}
use of javax.cache.expiry.Duration 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;
}
use of javax.cache.expiry.Duration in project carbon-apimgt by wso2.
the class CreateCache method execute.
@Override
public BValue[] execute(Context context) {
String cacheName = getStringArgument(context, 0);
String cacheTimeoutString = getStringArgument(context, 1);
// Default cache timeout is 15 minutes
int cacheTimeout = 15;
if (cacheTimeoutString != null && cacheTimeoutString.length() > 0) {
cacheTimeout = ((cacheTimeout = Integer.parseInt(cacheTimeoutString)) > 0 ? cacheTimeout : 15);
}
CacheManager cacheManager = CacheManagerHolder.getInstance().getCacheManager();
if ((cacheManager).getCache(cacheName) == null) {
Duration cacheExpiry = new Duration(TimeUnit.MINUTES, cacheTimeout);
MutableConfiguration<String, String> config = new MutableConfiguration<>();
config.setStoreByValue(true).setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(cacheExpiry)).setStatisticsEnabled(false).setStoreByValue(false);
cacheManager.createCache(cacheName, config);
}
return getBValues(new BString(cacheName));
}
use of javax.cache.expiry.Duration in project hazelcast by hazelcast.
the class AbstractCacheRecordStore method updateRecordWithExpiry.
protected void updateRecordWithExpiry(Data key, CacheRecord record, ExpiryPolicy expiryPolicy, long now, UUID source) {
expiryPolicy = getExpiryPolicy(record, expiryPolicy);
long expiryTime = TIME_NOT_AVAILABLE;
try {
Duration expiryDuration = expiryPolicy.getExpiryForUpdate();
if (expiryDuration != null) {
expiryTime = getAdjustedExpireTime(expiryDuration, now);
}
} catch (Exception e) {
ignore(e);
}
updateRecord(key, record, expiryTime, now, source);
}
Aggregations