use of javax.cache.expiry.TouchedExpiryPolicy in project ignite by apache.
the class GridCacheTtlManagerLoadTest method testLoad.
/**
* @throws Exception If failed.
*/
public void testLoad() throws Exception {
cacheMode = REPLICATED;
final IgniteKernal g = (IgniteKernal) startGrid(0);
try {
final AtomicBoolean stop = new AtomicBoolean();
IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
IgniteCache<Object, Object> cache = g.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, 1000)));
long key = 0;
while (!stop.get()) {
cache.put(key, key);
key++;
}
return null;
}
}, 1);
GridCacheTtlManager ttlMgr = g.internalCache(DEFAULT_CACHE_NAME).context().ttl();
for (int i = 0; i < 300; i++) {
U.sleep(1000);
ttlMgr.printMemoryStats();
}
stop.set(true);
fut.get();
} finally {
stopAllGrids();
}
}
use of javax.cache.expiry.TouchedExpiryPolicy 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());
}
}
Aggregations