use of org.infinispan.Cache in project keycloak by keycloak.
the class CacheExpirationTest method testCacheExpiration.
@Test
public void testCacheExpiration() throws Exception {
log.debug("Put two events to the main cache");
inComittedTransaction(session -> {
InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class);
Cache<String, Object> cache = provider.getCache(InfinispanConnectionProvider.WORK_CACHE_NAME);
cache.entrySet().stream().filter(me -> me.getValue() instanceof AuthenticationSessionAuthNoteUpdateEvent).forEach((c, me) -> c.remove(me.getKey()));
cache.put("1-2", AuthenticationSessionAuthNoteUpdateEvent.create("g1", "p1", "r1", Collections.emptyMap()), 20000, TimeUnit.MILLISECONDS);
cache.put("1-2-3", AuthenticationSessionAuthNoteUpdateEvent.create("g2", "p2", "r2", Collections.emptyMap()), 20000, TimeUnit.MILLISECONDS);
});
assumeThat("jmap output format unsupported", getNumberOfInstancesOfClass(AuthenticationSessionAuthNoteUpdateEvent.class), notNullValue());
// Ensure that instance counting works as expected, there should be at least two instances in memory now.
// Infinispan server is decoding the client request before processing the request at the cache level,
// therefore there are sometimes three instances of AuthenticationSessionAuthNoteUpdateEvent class in the memory
assertThat(getNumberOfInstancesOfClass(AuthenticationSessionAuthNoteUpdateEvent.class), greaterThanOrEqualTo(2));
log.debug("Starting other nodes and see that they join, receive the data and have their data expired");
AtomicInteger completedTests = new AtomicInteger(0);
inIndependentFactories(NUM_EXTRA_FACTORIES, 5 * 60, () -> {
log.debug("Joining the cluster");
inComittedTransaction(session -> {
InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class);
Cache<String, Object> cache = provider.getCache(InfinispanConnectionProvider.WORK_CACHE_NAME);
log.debug("Waiting for caches to join the cluster");
do {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
} while (!cache.getAdvancedCache().getDistributionManager().isJoinComplete());
String site = CONFIG.scope("connectionsInfinispan", "default").get("siteName");
log.debug("Cluster joined " + site);
log.debug("Waiting for cache to receive the two elements within the cluster");
do {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
} while (cache.entrySet().stream().filter(me -> me.getValue() instanceof AuthenticationSessionAuthNoteUpdateEvent).count() != 2);
// access the items in the local cache in the different site (site-2) in order to fetch them from the remote cache
assertThat(cache.get("1-2"), notNullValue());
assertThat(cache.get("1-2-3"), notNullValue());
// this is testing for a situation where an expiration lifespan configuration was missing in a replicated cache;
// the elements were no longer seen in the cache, still they weren't garbage collected.
// we must not look into the cache as that would trigger expiration explicitly.
// original issue: https://issues.redhat.com/browse/KEYCLOAK-18518
log.debug("Waiting for garbage collection to collect the entries across all caches in JVM");
do {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
} while (getNumberOfInstancesOfClass(AuthenticationSessionAuthNoteUpdateEvent.class) != 0);
completedTests.incrementAndGet();
log.debug("Test completed");
});
});
assertThat(completedTests.get(), is(NUM_EXTRA_FACTORIES));
}
use of org.infinispan.Cache in project partyline by Commonjava.
the class InfinispanJFS method getMetadata.
FileMeta getMetadata(final File target, final LocalLockOwner owner) throws IOException {
String path = target.getAbsolutePath();
return lockManager.lockAnd(path, (key, opLock) -> {
FileMeta meta = null;
TransactionManager transactionManager = metadataCache.getAdvancedCache().getTransactionManager();
try {
transactionManager.begin();
meta = metadataCache.computeIfAbsent(path, (p) -> new FileMeta(p, target.isDirectory(), this.blockSize));
LockLevel currentLockLevel = meta.getLockLevel(this.nodeKey);
// Only update the cache if the lock level changed
if (currentLockLevel == null || currentLockLevel != owner.getLockLevel()) {
meta.setLock(this.nodeKey, owner.getLockLevel());
metadataCache.put(path, meta);
}
transactionManager.commit();
} catch (Exception e) {
logger.error("Failed to execute in transaction. Rolling back. Path: " + path, e);
try {
transactionManager.rollback();
} catch (SystemException e1) {
logger.error("SystemException during transaction rollback involving path: " + path, e1);
}
}
return meta;
});
}
Aggregations