use of javax.cache.expiry.ModifiedExpiryPolicy in project ignite by apache.
the class HadoopJobTracker method jobMetaCache.
/**
* @return Job meta projection.
*/
@SuppressWarnings("NonPrivateFieldAccessedInSynchronizedContext")
private IgniteInternalCache<HadoopJobId, HadoopJobMetadata> jobMetaCache() {
IgniteInternalCache<HadoopJobId, HadoopJobMetadata> prj = jobMetaPrj;
if (prj == null) {
synchronized (mux) {
if ((prj = jobMetaPrj) == null) {
GridCacheAdapter<HadoopJobId, HadoopJobMetadata> sysCache = ctx.kernalContext().cache().internalCache(CU.SYS_CACHE_HADOOP_MR);
assert sysCache != null;
mrPlanner = ctx.planner();
try {
ctx.kernalContext().resource().injectGeneric(mrPlanner);
} catch (IgniteCheckedException e) {
// Must not happen.
U.error(log, "Failed to inject resources.", e);
throw new IllegalStateException(e);
}
jobMetaPrj = prj = sysCache;
if (ctx.configuration().getFinishedJobInfoTtl() > 0) {
ExpiryPolicy finishedJobPlc = new ModifiedExpiryPolicy(new Duration(MILLISECONDS, ctx.configuration().getFinishedJobInfoTtl()));
finishedJobMetaPrj = prj.withExpiryPolicy(finishedJobPlc);
} else
finishedJobMetaPrj = jobMetaPrj;
}
}
}
return prj;
}
use of javax.cache.expiry.ModifiedExpiryPolicy in project ignite by apache.
the class IgniteCacheEntryListenerExpiredEventsTest method checkExpiredEvents.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void checkExpiredEvents(CacheConfiguration<Object, Object> ccfg) throws Exception {
IgniteCache<Object, Object> cache = ignite(0).createCache(ccfg);
try {
evtCntr = new AtomicInteger();
CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new ExpiredListenerFactory(), null, true, false);
cache.registerCacheEntryListener(lsnrCfg);
IgniteCache<Object, Object> expiryCache = cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 500)));
expiryCache.put(1, 1);
for (int i = 0; i < 10; i++) cache.get(i);
boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return evtCntr.get() > 0;
}
}, 5000);
assertTrue(wait);
U.sleep(100);
assertEquals(1, evtCntr.get());
} finally {
ignite(0).destroyCache(cache.getName());
}
}
use of javax.cache.expiry.ModifiedExpiryPolicy in project ignite by apache.
the class IgniteCacheEntryListenerAbstractTest method checkFilter.
/**
* @param cache Cache.
* @param vals Values in cache.
* @throws Exception If failed.
*/
private void checkFilter(final IgniteCache<Object, Object> cache, Map<Object, Object> vals) throws Exception {
evts = Collections.synchronizedList(new ArrayList<CacheEntryEvent<?, ?>>());
// Remove, create, update and expire for half of modified entries.
final int expEvts = (vals.size() / 2) * 4;
evtsLatch = new CountDownLatch(expEvts);
cache.removeAll(vals.keySet());
cache.putAll(vals);
final Map<Object, Object> newVals = new HashMap<>();
for (Object key : vals.keySet()) newVals.put(key, value(-1));
cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 500))).putAll(newVals);
U.sleep(1000);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
for (Object key : newVals.keySet()) {
if (primaryCache(key, cache.getName()).get(key) != null)
return false;
}
return true;
}
}, 5000);
evtsLatch.await(5000, MILLISECONDS);
assertEquals(expEvts, evts.size());
Set<Object> rmvd = new HashSet<>();
Set<Object> created = new HashSet<>();
Set<Object> updated = new HashSet<>();
Set<Object> expired = new HashSet<>();
for (CacheEntryEvent<?, ?> evt : evts) {
Integer key;
if (useObjects)
key = ((ListenerTestKey) evt.getKey()).key;
else
key = (Integer) evt.getKey();
assertTrue(key % 2 == 0);
assertTrue(vals.keySet().contains(evt.getKey()));
switch(evt.getEventType()) {
case REMOVED:
assertEquals(evt.getOldValue(), evt.getValue());
assertEquals(vals.get(evt.getKey()), evt.getOldValue());
assertTrue(rmvd.add(evt.getKey()));
break;
case CREATED:
assertEquals(vals.get(evt.getKey()), evt.getValue());
assertNull(evt.getOldValue());
assertTrue(rmvd.contains(evt.getKey()));
assertTrue(created.add(evt.getKey()));
break;
case UPDATED:
assertEquals(value(-1), evt.getValue());
assertEquals(vals.get(evt.getKey()), evt.getOldValue());
assertTrue(rmvd.contains(evt.getKey()));
assertTrue(created.contains(evt.getKey()));
assertTrue(updated.add(evt.getKey()));
break;
case EXPIRED:
assertEquals(evt.getOldValue(), evt.getValue());
assertEquals(value(-1), evt.getOldValue());
assertTrue(rmvd.contains(evt.getKey()));
assertTrue(created.contains(evt.getKey()));
assertTrue(updated.contains(evt.getKey()));
assertTrue(expired.add(evt.getKey()));
break;
default:
fail("Unexpected type: " + evt.getEventType());
}
}
assertEquals(vals.size() / 2, rmvd.size());
assertEquals(vals.size() / 2, created.size());
assertEquals(vals.size() / 2, updated.size());
assertEquals(vals.size() / 2, expired.size());
}
use of javax.cache.expiry.ModifiedExpiryPolicy in project ignite by apache.
the class IgniteCacheExpiryPolicyAbstractTest method testCreateUpdate0.
/**
* @throws Exception if failed.
*/
@Test
public void testCreateUpdate0() throws Exception {
startGrids(1);
long ttl = 60L;
final String key = "key1";
final IgniteCache<String, String> cache = jcache();
for (int i = 0; i < 1000; i++) {
final IgniteCache<String, String> cache0 = cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(TimeUnit.HOURS, ttl)));
cache0.put(key, key);
info("PUT DONE");
}
long pSize = grid(0).context().cache().internalCache(DEFAULT_CACHE_NAME).context().ttl().pendingSize();
assertTrue("Too many pending entries: " + pSize, pSize <= 1);
cache.remove(key);
pSize = grid(0).context().cache().internalCache(DEFAULT_CACHE_NAME).context().ttl().pendingSize();
assertEquals(0, pSize);
}
use of javax.cache.expiry.ModifiedExpiryPolicy in project ignite by apache.
the class FunctionalTest method testExpirePolicy.
/**
* Test cache with expire policy.
*/
@Test
public void testExpirePolicy() throws Exception {
long ttl = 600L;
int MAX_RETRIES = 5;
try (Ignite ignite = Ignition.start(Config.getServerConfiguration());
IgniteClient client = Ignition.startClient(getClientConfiguration())) {
ClientCache<Integer, Object> cache = client.createCache(new ClientCacheConfiguration().setName("cache").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
Duration dur = new Duration(TimeUnit.MILLISECONDS, ttl);
ClientCache<Integer, Object> cachePlcCreated = cache.withExpirePolicy(new CreatedExpiryPolicy(dur));
ClientCache<Integer, Object> cachePlcUpdated = cache.withExpirePolicy(new ModifiedExpiryPolicy(dur));
ClientCache<Integer, Object> cachePlcAccessed = cache.withExpirePolicy(new AccessedExpiryPolicy(dur));
for (int i = 0; i < MAX_RETRIES; i++) {
cache.clear();
long ts = U.currentTimeMillis();
cache.put(0, 0);
cachePlcCreated.put(1, 1);
cachePlcUpdated.put(2, 2);
cachePlcAccessed.put(3, 3);
U.sleep(ttl / 3 * 2);
boolean containsKey0 = cache.containsKey(0);
boolean containsKey1 = cache.containsKey(1);
boolean containsKey2 = cache.containsKey(2);
boolean containsKey3 = cache.containsKey(3);
if (// Retry if this block execution takes too long.
U.currentTimeMillis() - ts >= ttl)
continue;
assertTrue(containsKey0);
assertTrue(containsKey1);
assertTrue(containsKey2);
assertTrue(containsKey3);
ts = U.currentTimeMillis();
cachePlcCreated.put(1, 2);
// Update and access key with created expire policy.
cachePlcCreated.get(1);
// Update key with modified expire policy.
cachePlcUpdated.put(2, 3);
// Access key with accessed expire policy.
cachePlcAccessed.get(3);
U.sleep(ttl / 3 * 2);
containsKey0 = cache.containsKey(0);
containsKey1 = cache.containsKey(1);
containsKey2 = cache.containsKey(2);
containsKey3 = cache.containsKey(3);
if (// Retry if this block execution takes too long.
U.currentTimeMillis() - ts >= ttl)
continue;
assertTrue(containsKey0);
assertFalse(containsKey1);
assertTrue(containsKey2);
assertTrue(containsKey3);
U.sleep(ttl / 3 * 2);
// Access key with updated expire policy.
cachePlcUpdated.get(2);
U.sleep(ttl / 3 * 2);
assertTrue(cache.containsKey(0));
assertFalse(cache.containsKey(1));
assertFalse(cache.containsKey(2));
assertFalse(cache.containsKey(3));
// Expire policy, keep binary and transactional flags together.
ClientCache<Integer, Object> binCache = cachePlcCreated.withKeepBinary();
try (ClientTransaction tx = client.transactions().txStart()) {
binCache.put(4, new T2<>("test", "test"));
tx.commit();
}
assertTrue(binCache.get(4) instanceof BinaryObject);
assertFalse(cache.get(4) instanceof BinaryObject);
U.sleep(ttl / 3 * 4);
assertFalse(cache.containsKey(4));
return;
}
fail("Failed to check expire policy within " + MAX_RETRIES + " retries (block execution takes too long)");
}
}
Aggregations