use of javax.cache.expiry.Duration in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method testPeekExpired.
/**
* JUnit.
*
* @throws Exception If failed.
*/
@Ignore("https://issues.apache.org/jira/browse/IGNITE-11885")
@Test
public void testPeekExpired() throws Exception {
final IgniteCache<String, Integer> c = jcache();
final String key = primaryKeysForCache(1).get(0);
info("Using key: " + key);
c.put(key, 1);
assertEquals(Integer.valueOf(1), c.localPeek(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 c.localPeek(key) == null;
}
}, 2000);
assertNull(c.localPeek(key));
assert c.localSize() == 0 : "Cache is not empty.";
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class IgniteCacheEntryListenerAbstractTest method testSynchronousEvents.
/**
* @throws Exception If failed.
*/
@Test
public void testSynchronousEvents() throws Exception {
final CacheEntryCreatedListener<Object, Object> lsnr = new CreateUpdateRemoveExpireListener() {
@Override
public void onRemoved(Iterable<CacheEntryEvent<?, ?>> evts) {
super.onRemoved(evts);
awaitLatch();
}
@Override
public void onCreated(Iterable<CacheEntryEvent<?, ?>> evts) {
super.onCreated(evts);
awaitLatch();
}
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
super.onUpdated(evts);
awaitLatch();
}
private void awaitLatch() {
try {
assertTrue(syncEvtLatch.await(5000, MILLISECONDS));
} catch (InterruptedException e) {
fail("Unexpected exception: " + e);
}
}
};
CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Object, Object>>() {
@Override
public CacheEntryListener<Object, Object> create() {
return lsnr;
}
}, null, true, true);
IgniteCache<Object, Object> cache = jcache();
cache.registerCacheEntryListener(lsnrCfg);
try {
for (Integer key : keys()) {
log.info("Check synchronous create event [key=" + key + ']');
syncEvent(key, 1, cache, 1);
checkEvent(evts.iterator(), key, CREATED, 1, null);
log.info("Check synchronous update event [key=" + key + ']');
syncEvent(key, 2, cache, 1);
checkEvent(evts.iterator(), key, UPDATED, 2, 1);
log.info("Check synchronous remove event [key=" + key + ']');
syncEvent(key, null, cache, 1);
checkEvent(evts.iterator(), key, REMOVED, 2, 2);
log.info("Check synchronous expire event [key=" + key + ']');
syncEvent(key, 3, cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 1000))), eagerTtl() ? 2 : 1);
checkEvent(evts.iterator(), key, CREATED, 3, null);
if (!eagerTtl()) {
U.sleep(1100);
assertNull(primaryCache(key(key), cache.getName()).get(key(key)));
evtsLatch.await(5000, MILLISECONDS);
assertEquals(1, evts.size());
}
checkEvent(evts.iterator(), key, EXPIRED, 3, 3);
assertEquals(0, evts.size());
}
} finally {
cache.deregisterCacheEntryListener(lsnrCfg);
}
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method testPeekExpiredTx.
/**
* @throws Exception If failed.
*/
@Test
public void testPeekExpiredTx() throws Exception {
if (txShouldBeUsed()) {
final IgniteCache<String, Integer> c = jcache();
final String key = "1";
int ttl = 500;
try (Transaction tx = grid(0).transactions().txStart()) {
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
tx.commit();
}
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return peek(c, key) == null;
}
}, 2000);
assertNull(peek(c, key));
assert c.localSize() == 0;
}
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest 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 {
int ttl = 4000;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
final IgniteCache<String, Integer> c = jcache();
final String key = primaryKeysForCache(jcache(), 1).get(0);
IgnitePair<Long> entryTtl;
if (oldEntry) {
c.put(key, 1);
entryTtl = entryTtl(fullCache(), key);
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
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(fullCache(), key);
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(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
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(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
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(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
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(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(expireTimes[i], (long) curEntryTtl.get2());
}
}
// Avoid reloading from store.
storeStgy.removeFromStore(key);
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@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 fullCache = fullCache();
if (!isMultiJvmObject(fullCache)) {
GridCacheAdapter internalCache = internalCache(fullCache);
if (internalCache.isLocal())
return;
}
assert c.get(key) == null;
// Ensure that old TTL and expire time are not longer "visible".
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 {
jcache().put(key, 10);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
U.sleep(2000);
entryTtl = entryTtl(fullCache(), 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 CacheConfig method wordCache.
/**
* @return Streaming cache configuration.
*/
public static CacheConfiguration<AffinityUuid, String> wordCache() {
CacheConfiguration<AffinityUuid, String> cfg = new CacheConfiguration<>("words");
// Index all words streamed into cache.
cfg.setIndexedTypes(AffinityUuid.class, String.class);
// Sliding window of 1 seconds.
cfg.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new CreatedExpiryPolicy(new Duration(SECONDS, 1))));
return cfg;
}
Aggregations