Search in sources :

Example 1 with AuthenticationSessionAuthNoteUpdateEvent

use of org.keycloak.models.cache.infinispan.events.AuthenticationSessionAuthNoteUpdateEvent in project keycloak by keycloak.

the class InfinispanAuthenticationSessionProviderFactory method updateAuthNotes.

private void updateAuthNotes(ClusterEvent clEvent) {
    if (!(clEvent instanceof AuthenticationSessionAuthNoteUpdateEvent)) {
        return;
    }
    AuthenticationSessionAuthNoteUpdateEvent event = (AuthenticationSessionAuthNoteUpdateEvent) clEvent;
    RootAuthenticationSessionEntity authSession = this.authSessionsCache.get(event.getAuthSessionId());
    updateAuthSession(authSession, event.getTabId(), event.getAuthNotesFragment());
}
Also used : AuthenticationSessionAuthNoteUpdateEvent(org.keycloak.models.cache.infinispan.events.AuthenticationSessionAuthNoteUpdateEvent) RootAuthenticationSessionEntity(org.keycloak.models.sessions.infinispan.entities.RootAuthenticationSessionEntity)

Example 2 with AuthenticationSessionAuthNoteUpdateEvent

use of org.keycloak.models.cache.infinispan.events.AuthenticationSessionAuthNoteUpdateEvent 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));
}
Also used : InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Assume.assumeThat(org.junit.Assume.assumeThat) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Test(org.junit.Test) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) IOException(java.io.IOException) Cache(org.infinispan.Cache) InputStreamReader(java.io.InputStreamReader) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assume(org.junit.Assume) AuthenticationSessionAuthNoteUpdateEvent(org.keycloak.models.cache.infinispan.events.AuthenticationSessionAuthNoteUpdateEvent) RequireProvider(org.keycloak.testsuite.model.RequireProvider) Matchers.is(org.hamcrest.Matchers.is) BufferedReader(java.io.BufferedReader) ManagementFactory(java.lang.management.ManagementFactory) Pattern(java.util.regex.Pattern) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) AuthenticationSessionAuthNoteUpdateEvent(org.keycloak.models.cache.infinispan.events.AuthenticationSessionAuthNoteUpdateEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) Test(org.junit.Test) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest)

Aggregations

AuthenticationSessionAuthNoteUpdateEvent (org.keycloak.models.cache.infinispan.events.AuthenticationSessionAuthNoteUpdateEvent)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ManagementFactory (java.lang.management.ManagementFactory)1 Collections (java.util.Collections)1 Objects (java.util.Objects)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.greaterThanOrEqualTo (org.hamcrest.Matchers.greaterThanOrEqualTo)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)1 Cache (org.infinispan.Cache)1 Assume (org.junit.Assume)1 Assume.assumeThat (org.junit.Assume.assumeThat)1 Test (org.junit.Test)1 InfinispanConnectionProvider (org.keycloak.connections.infinispan.InfinispanConnectionProvider)1