use of javax.cache.expiry.Duration in project ignite by apache.
the class GridCachePartitionedEvictionSelfTest method doTestEviction.
/**
* @throws Exception If failed.
* @param concurrency Tx concurrency.
* @param isolation Tx isolation.
*/
private void doTestEviction(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
assert concurrency != null;
assert isolation != null;
// This condition should be "true", otherwise the test doesn't make sense.
assert KEY_CNT >= EVICT_CACHE_SIZE;
GridDhtCacheAdapter<String, Integer> dht0 = dht(jcache(0));
GridDhtCacheAdapter<String, Integer> dht1 = dht(jcache(1));
Affinity<String> aff = dht0.affinity();
TouchedExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, 37));
for (int kv = 0; kv < KEY_CNT; kv++) {
Thread.sleep(40);
String key = String.valueOf(kv);
ClusterNode node = aff.mapKeyToNode(key);
IgniteCache<String, Integer> c = cache(node);
IgniteTransactions txs = G.ignite(node.id()).transactions();
try (Transaction tx = txs.txStart(concurrency, isolation)) {
assert c.get(key) == null;
c.withExpiryPolicy(plc).put(key, kv);
assertEquals(Integer.valueOf(kv), c.get(key));
tx.commit();
}
}
boolean[] seen = { false, false, false, false };
long started = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
long dht0Keys = 0, dht1Keys = 0;
seen[2] |= dht0.size() == EVICT_CACHE_SIZE;
seen[3] |= dht1.size() == EVICT_CACHE_SIZE;
info("Printing keys in dht0...");
for (String key : dht0.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(0).localNode(), aff.mapKeyToNode(key)) + ", " + dht0Keys++ + ']');
info("Printing keys in dht1...");
for (String key : dht1.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(1).localNode(), aff.mapKeyToNode(key)) + ", " + dht1Keys++ + ']');
seen[0] |= dht0Keys == EVICT_CACHE_SIZE;
seen[1] |= dht1Keys == EVICT_CACHE_SIZE;
if (seen[0] && seen[1] && seen[2] && seen[3] || System.currentTimeMillis() - started > 100)
break;
}
assertTrue(Arrays.toString(seen), seen[0] && seen[1] && seen[2] && seen[3]);
assertEquals(0, near(jcache(0)).nearSize());
assertEquals(0, near(jcache(1)).nearSize());
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class IgniteCacheLargeValueExpireTest method checkExpire.
/**
* @param ignite Node.
* @param eagerTtl Value for {@link CacheConfiguration#setEagerTtl(boolean)}.
* @throws Exception If failed.
*/
private void checkExpire(Ignite ignite, boolean eagerTtl) throws Exception {
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setEagerTtl(eagerTtl);
ignite.createCache(ccfg);
try {
IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new TouchedExpiryPolicy(new Duration(0, 500)));
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < 10; i++) {
log.info("Iteration: " + i);
AtomicInteger cntr = new AtomicInteger();
List<Object> keys = new ArrayList<>();
for (int j = 0; j < 10_000; j++) {
Object key = null;
Object val = null;
switch(rnd.nextInt(3)) {
case 0:
key = rnd.nextInt(100_000);
val = new TestKeyValue(cntr.getAndIncrement(), new byte[rnd.nextInt(3 * PAGE_SIZE)]);
break;
case 1:
key = new TestKeyValue(cntr.getAndIncrement(), new byte[rnd.nextInt(3 * PAGE_SIZE)]);
val = rnd.nextInt();
break;
case 2:
key = new TestKeyValue(cntr.getAndIncrement(), new byte[rnd.nextInt(3 * PAGE_SIZE)]);
val = new TestKeyValue(cntr.getAndIncrement(), new byte[rnd.nextInt(3 * PAGE_SIZE)]);
break;
default:
fail();
}
cache.put(key, val);
keys.add(key);
}
U.sleep(1000);
for (Object key : keys) assertNull(cache.get(key));
}
} finally {
ignite.destroyCache(ccfg.getName());
}
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class CacheTtlReadOnlyModeSelfTest method getCacheConfigurations.
/**
*/
private static CacheConfiguration[] getCacheConfigurations() {
CacheConfiguration[] cfgs = cacheConfigurations();
List<CacheConfiguration> newCfgs = new ArrayList<>(cfgs.length);
for (CacheConfiguration cfg : cfgs) {
if (cfg.getAtomicityMode() == CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT) {
// Expiry policy cannot be used with TRANSACTIONAL_SNAPSHOT.
continue;
}
cfg.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(SECONDS, EXPIRATION_TIMEOUT)));
cfg.setEagerTtl(true);
newCfgs.add(cfg);
}
return newCfgs.toArray(new CacheConfiguration[0]);
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class ExpiryPolicyInfoLoggingTest method checkLoggingExpiryInfoForDynamicallyCreatedCache.
/**
* Checking logging of expiry policy info for dynamically created cache.
*/
@Test
public void checkLoggingExpiryInfoForDynamicallyCreatedCache() throws Exception {
LogListener lsnr = LogListener.matches(s -> s.startsWith(String.format(STARTED_CACHE_MSG, CACHE_2_NAME)) && s.contains(String.format(EXPRITY_POLICY_MSG, AccessedExpiryPolicy.class.getName(), false))).times(1).build();
log.registerListener(lsnr);
IgniteEx srv = startGrid(0);
srv.createCache(new CacheConfiguration<>(CACHE_2_NAME).setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 5))).setEagerTtl(false));
assertTrue(lsnr.check());
}
use of javax.cache.expiry.Duration in project ignite by apache.
the class IgniteCacheClientNearCacheExpiryTest method testExpirationOnClient.
/**
* @throws Exception If failed.
*/
@Test
public void testExpirationOnClient() throws Exception {
Ignite ignite = grid(NODES - 1);
// Check size of near entries via reflection because entries is filtered for size() API call.
IgniteEx igniteEx = (IgniteEx) ignite;
GridCacheAdapter internalCache = igniteEx.context().cache().internalCache(DEFAULT_CACHE_NAME);
GridCacheLocalConcurrentMap map = GridTestUtils.getFieldValue(internalCache, GridCacheAdapter.class, "map");
assertTrue(ignite.configuration().isClientMode());
IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
assertTrue(((IgniteCacheProxy) cache).context().isNear());
for (int i = 0; i < KEYS_COUNT; i++) cache.put(i, i);
CreatedExpiryPolicy plc = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 500));
IgniteCache<Object, Object> cacheWithExpiry = cache.withExpiryPolicy(plc);
for (int i = KEYS_COUNT; i < KEYS_COUNT * 2; i++) {
cacheWithExpiry.put(i, i);
assertEquals(i, cacheWithExpiry.localPeek(i));
}
assertEquals(KEYS_COUNT * 2, map.publicSize(internalCache.context().cacheId()));
U.sleep(1000);
assertEquals(KEYS_COUNT, map.publicSize(internalCache.context().cacheId()));
assertEquals(KEYS_COUNT, cache.size());
for (int i = 0; i < KEYS_COUNT; i++) assertEquals(i, cacheWithExpiry.localPeek(i));
for (int i = KEYS_COUNT; i < KEYS_COUNT * 2; i++) assertNull(cache.localPeek(i));
}
Aggregations