use of javax.cache.expiry.TouchedExpiryPolicy in project ignite by apache.
the class GridCacheBasicOpAbstractTest method testPutWithExpiration.
/**
* @throws Exception In case of error.
*/
@Test
public void testPutWithExpiration() throws Exception {
MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.EXPIRATION);
IgniteCache<String, String> cache1 = ignite1.cache(DEFAULT_CACHE_NAME);
IgniteCache<String, String> cache2 = ignite2.cache(DEFAULT_CACHE_NAME);
IgniteCache<String, String> cache3 = ignite3.cache(DEFAULT_CACHE_NAME);
cache1.put("key", "val");
Transaction tx = ignite1.transactions().txStart();
long ttl = 500;
cache1.withExpiryPolicy(new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl))).put("key", "val");
assert cache1.get("key") != null;
tx.commit();
info("Going to sleep for: " + (ttl + 1000));
// Allow for expiration.
Thread.sleep(ttl + 1000);
String v1 = cache1.get("key");
String v2 = cache2.get("key");
String v3 = cache3.get("key");
assert v1 == null : "V1 should be null: " + v1;
assert v2 == null : "V2 should be null: " + v2;
assert v3 == null : "V3 should be null: " + v3;
}
use of javax.cache.expiry.TouchedExpiryPolicy in project ignite by apache.
the class GridCacheAbstractMetricsSelfTest method checkTtl.
/**
* @param inTx {@code true} for tx.
* @throws Exception If failed.
*/
private void checkTtl(boolean inTx) throws Exception {
int ttl = 1000;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
final IgniteCache<Integer, Integer> c = grid(0).cache(DEFAULT_CACHE_NAME);
final Integer key = primaryKeys(jcache(0), 1, 0).get(0);
c.put(key, 1);
GridCacheAdapter<Object, Object> c0 = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
if (c0.isNear())
c0 = c0.context().near().dht();
GridCacheEntryEx entry = c0.entryEx(key);
assert entry != null;
assertEquals(0, entry.ttl());
assertEquals(0, entry.expireTime());
long startTime = U.currentTimeMillis();
if (inTx) {
// Rollback transaction for the first time.
Transaction tx = grid(0).transactions().txStart();
try {
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
} finally {
tx.rollback();
}
entry = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME).entryEx(key);
assertEquals(0, entry.ttl());
assertEquals(0, entry.expireTime());
}
// Now commit transaction and check that ttl and expire time have been saved.
Transaction tx = inTx ? grid(0).transactions().txStart() : null;
try {
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
} finally {
if (tx != null)
tx.commit();
}
long[] expireTimes = new long[gridCount()];
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
if (c0.isNear())
c0 = c0.context().near().dht();
GridCacheEntryEx curEntry = c0.entryEx(key);
curEntry.unswap();
assertTrue(curEntry.expireTime() >= startTime);
expireTimes[i] = curEntry.expireTime();
}
}
// One more update from the same cache entry to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? grid(0).transactions().txStart() : null;
try {
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 2);
} finally {
if (tx != null)
tx.commit();
}
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
if (c0.isNear())
c0 = c0.context().near().dht();
GridCacheEntryEx curEntry = c0.entryEx(key);
curEntry.unswap();
assertTrue(curEntry.expireTime() >= startTime);
expireTimes[i] = curEntry.expireTime();
}
}
// And one more direct update to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? grid(0).transactions().txStart() : null;
try {
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 3);
} finally {
if (tx != null)
tx.commit();
}
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
if (c0.isNear())
c0 = c0.context().near().dht();
GridCacheEntryEx curEntry = c0.entryEx(key);
curEntry.unswap();
assertTrue(curEntry.expireTime() >= startTime);
expireTimes[i] = curEntry.expireTime();
}
}
// 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 ? grid(0).transactions().txStart() : null;
try {
c.put(key, 4);
} finally {
if (tx != null)
tx.commit();
}
log.info("Put 4 done");
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
if (c0.isNear())
c0 = c0.context().near().dht();
GridCacheEntryEx curEntry = c0.entryEx(key);
curEntry.unswap();
assertEquals(expireTimes[i], curEntry.expireTime());
}
}
// Avoid reloading from store.
storeStgy.removeFromStore(key);
assertTrue(waitForCondition(new GridAbsPredicateX() {
@Override
public boolean applyx() {
try {
if (c.get(key) != null)
return false;
// Get "cache" field from GridCacheProxyImpl.
GridCacheAdapter c0 = cacheFromCtx(c);
if (!c0.context().deferredDelete()) {
GridCacheEntryEx e0 = c0.entryEx(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())));
c0 = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
if (c0.isNear())
c0 = c0.context().near().dht();
// Ensure that old TTL and expire time are not longer "visible".
entry = c0.entryEx(key);
assertEquals(0, entry.expireTime());
}
use of javax.cache.expiry.TouchedExpiryPolicy in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method testCompactExpired.
/**
* @throws Exception If failed.
*/
@Test
public void testCompactExpired() throws Exception {
final IgniteCache<String, Integer> cache = jcache();
final String key = F.first(primaryKeysForCache(cache, 1));
cache.put(key, 1);
long ttl = 500;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return cache.localPeek(key) == null;
}
}, ttl + 1000);
// Peek will actually remove entry from cache.
assertNull(cache.localPeek(key));
assertEquals(0, cache.localSize());
// Clear readers, if any.
cache.remove(key);
}
use of javax.cache.expiry.TouchedExpiryPolicy in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method testEvictExpired.
/**
* @throws Exception In case of error.
*/
@Test
public void testEvictExpired() throws Exception {
final IgniteCache<String, Integer> cache = jcache(0);
final String key = primaryKeysForCache(cache, 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(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
final Affinity<String> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
boolean wait = waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
for (int i = 0; i < gridCount(); i++) {
if (peek(jcache(i), 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(peek(cache, "key"));
assertNull(cache.localPeek(key, ONHEAP));
assertTrue(cache.localSize() == 0);
load(cache, key, true);
for (int i = 0; i < gridCount(); i++) {
if (aff.isPrimary(grid(i).cluster().localNode(), key))
assertEquals((Integer) 1, peek(jcache(i), key));
if (aff.isBackup(grid(i).cluster().localNode(), key))
assertEquals((Integer) 1, peek(jcache(i), key));
}
}
use of javax.cache.expiry.TouchedExpiryPolicy in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method testPeekExpired.
/**
* @throws Exception If failed.
*/
@Test
public void testPeekExpired() throws Exception {
final IgniteCache<String, Integer> c = jcache();
final String key = primaryKeysForCache(c, 1).get(0);
info("Using key: " + key);
c.put(key, 1);
assertEquals(Integer.valueOf(1), peek(c, key));
int ttl = 500;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
c.withExpiryPolicy(expiry).put(key, 1);
Thread.sleep(ttl + 100);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return peek(c, key) == null;
}
}, 2000);
assert peek(c, key) == null;
assert c.localSize() == 0 : "Cache is not empty.";
}
Aggregations