use of org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException in project ignite by apache.
the class IgniteCacheExpiryPolicyAbstractTest method checkTtl.
/**
* @param key Key.
* @param ttl TTL.
* @param wait If {@code true} waits for ttl update.
* @throws Exception If failed.
*/
private void checkTtl(Object key, final long ttl, boolean wait) throws Exception {
boolean found = false;
for (int i = 0; i < gridCount(); i++) {
IgniteKernal grid = (IgniteKernal) grid(i);
GridCacheAdapter<Object, Object> cache = grid.context().cache().internalCache(DEFAULT_CACHE_NAME);
if (cache.context().isNear())
cache = cache.context().near().dht();
while (true) {
try {
GridCacheEntryEx e = cache.entryEx(key);
if (e != null && e.deleted()) {
assertEquals(0, e.ttl());
assertFalse("Invalid entry [e=" + e + ", node=" + i + ']', cache.affinity().isPrimaryOrBackup(grid.localNode(), key));
continue;
}
if (e == null)
assertTrue("Not found " + key, !cache.affinity().isPrimaryOrBackup(grid.localNode(), key));
else {
e.unswap();
found = true;
if (ttl > 0)
assertTrue(e.expireTime() > 0);
else
assertEquals(0, e.expireTime());
}
break;
} catch (GridCacheEntryRemovedException ignore) {
// Retry.
} catch (GridDhtInvalidPartitionException ignore) {
// No need to check.
break;
}
}
}
assertTrue(found);
}
use of org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException 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