Search in sources :

Example 1 with RemoteCacheSessionsLoader

use of org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader in project keycloak by keycloak.

the class InfinispanUserLoginFailureProviderFactory method loadLoginFailuresFromRemoteCaches.

private void loadLoginFailuresFromRemoteCaches(final KeycloakSessionFactory sessionFactory, String cacheName, final int sessionsPerSegment, final int maxErrors) {
    log.debugf("Check pre-loading sessions from remote cache '%s'", cacheName);
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            InfinispanConnectionProvider connections = session.getProvider(InfinispanConnectionProvider.class);
            Cache<String, Serializable> workCache = connections.getCache(InfinispanConnectionProvider.WORK_CACHE_NAME);
            InfinispanCacheInitializer initializer = new InfinispanCacheInitializer(sessionFactory, workCache, new RemoteCacheSessionsLoader(cacheName, sessionsPerSegment), "remoteCacheLoad::" + cacheName, sessionsPerSegment, maxErrors);
            initializer.initCache();
            initializer.loadSessions();
        }
    });
    log.debugf("Pre-loading login failures from remote cache '%s' finished", cacheName);
}
Also used : KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) InfinispanCacheInitializer(org.keycloak.models.sessions.infinispan.initializer.InfinispanCacheInitializer) RemoteCacheSessionsLoader(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader) Cache(org.infinispan.Cache) RemoteCache(org.infinispan.client.hotrod.RemoteCache)

Example 2 with RemoteCacheSessionsLoader

use of org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader in project keycloak by keycloak.

the class InfinispanUserSessionProviderFactory method loadSessionsFromRemoteCache.

private void loadSessionsFromRemoteCache(final KeycloakSessionFactory sessionFactory, String cacheName, final int sessionsPerSegment, final int maxErrors) {
    log.debugf("Check pre-loading sessions from remote cache '%s'", cacheName);
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            InfinispanConnectionProvider connections = session.getProvider(InfinispanConnectionProvider.class);
            Cache<String, Serializable> workCache = connections.getCache(InfinispanConnectionProvider.WORK_CACHE_NAME);
            InfinispanCacheInitializer initializer = new InfinispanCacheInitializer(sessionFactory, workCache, new RemoteCacheSessionsLoader(cacheName, sessionsPerSegment), "remoteCacheLoad::" + cacheName, sessionsPerSegment, maxErrors);
            initializer.initCache();
            initializer.loadSessions();
        }
    });
    log.debugf("Pre-loading sessions from remote cache '%s' finished", cacheName);
}
Also used : KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) InfinispanCacheInitializer(org.keycloak.models.sessions.infinispan.initializer.InfinispanCacheInitializer) RemoteCacheSessionsLoader(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader) Cache(org.infinispan.Cache) RemoteCache(org.infinispan.client.hotrod.RemoteCache)

Example 3 with RemoteCacheSessionsLoader

use of org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader in project keycloak by keycloak.

the class RemoteCacheSessionsLoaderTest method testRemoteCache.

@Test
@Ignore
public void testRemoteCache() throws Exception {
    String cacheName = InfinispanConnectionProvider.USER_SESSION_CACHE_NAME;
    Cache cache1 = createManager(1, cacheName).getCache(cacheName);
    Cache cache2 = cache1.getCacheManager().getCache("local");
    RemoteCache remoteCache = InfinispanUtil.getRemoteCache(cache1);
    cache1.clear();
    cache2.clear();
    remoteCache.clear();
    try {
        for (int i = 0; i < COUNT; i++) {
            // Create initial item
            UserSessionEntity session = new UserSessionEntity();
            session.setId("loader-key-" + i);
            session.setRealmId("master");
            session.setBrokerSessionId("!23123123");
            session.setBrokerUserId(null);
            session.setUser("admin");
            session.setLoginUsername("admin");
            session.setIpAddress("123.44.143.178");
            session.setStarted(Time.currentTime());
            session.setLastSessionRefresh(Time.currentTime());
            SessionEntityWrapper<UserSessionEntity> wrappedSession = new SessionEntityWrapper<>(session);
            // Create caches, listeners and finally worker threads
            remoteCache.put("loader-key-" + i, wrappedSession);
            Assert.assertFalse(cache2.containsKey("loader-key-" + i));
            if (i % 1000 == 0) {
                logger.infof("%d sessions added", i);
            }
        }
        // RemoteCacheSessionsLoader loader = new RemoteCacheSessionsLoader(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME, 64) {
        // 
        // @Override
        // protected Cache getCache(KeycloakSession session) {
        // return cache2;
        // }
        // 
        // @Override
        // protected RemoteCache getRemoteCache(KeycloakSession session) {
        // return remoteCache;
        // }
        // 
        // };
        // Just to be able to test serializability
        RemoteCacheSessionsLoader loader = new CustomLoader(cacheName, 64, cache2, remoteCache);
        loader.init(null);
        RemoteCacheSessionsLoaderContext ctx = loader.computeLoaderContext(null);
        Assert.assertEquals(ctx.getSessionsTotal(), COUNT);
        Assert.assertEquals(ctx.getIspnSegmentsCount(), 256);
        // Assert.assertEquals(ctx.getSegmentsCount(), 16);
        Assert.assertEquals(ctx.getSessionsPerSegment(), 64);
        int totalCount = 0;
        logger.infof("segmentsCount: %d", ctx.getSegmentsCount());
        Set<String> visitedKeys = new HashSet<>();
        for (int currentSegment = 0; currentSegment < ctx.getSegmentsCount(); currentSegment++) {
            logger.infof("Loading segment %d", currentSegment);
            loader.loadSessions(null, ctx, new SessionLoader.WorkerContext(currentSegment, currentSegment));
            logger.infof("Loaded %d keys for segment %d", cache2.keySet().size(), currentSegment);
            totalCount = totalCount + cache2.keySet().size();
            visitedKeys.addAll(cache2.keySet());
            cache2.clear();
        }
        Assert.assertEquals(totalCount, COUNT);
        Assert.assertEquals(visitedKeys.size(), COUNT);
        logger.infof("SUCCESS: Loaded %d sessions", totalCount);
    } finally {
        // Finish JVM
        cache1.getCacheManager().stop();
    }
}
Also used : RemoteCacheSessionsLoaderContext(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoaderContext) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper) SessionLoader(org.keycloak.models.sessions.infinispan.initializer.SessionLoader) RemoteCache(org.infinispan.client.hotrod.RemoteCache) RemoteCacheSessionsLoader(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader) Cache(org.infinispan.Cache) RemoteCache(org.infinispan.client.hotrod.RemoteCache) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity) HashSet(java.util.HashSet) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Cache (org.infinispan.Cache)3 RemoteCache (org.infinispan.client.hotrod.RemoteCache)3 RemoteCacheSessionsLoader (org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader)3 InfinispanConnectionProvider (org.keycloak.connections.infinispan.InfinispanConnectionProvider)2 KeycloakSession (org.keycloak.models.KeycloakSession)2 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)2 InfinispanCacheInitializer (org.keycloak.models.sessions.infinispan.initializer.InfinispanCacheInitializer)2 HashSet (java.util.HashSet)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 SessionEntityWrapper (org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)1 UserSessionEntity (org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)1 SessionLoader (org.keycloak.models.sessions.infinispan.initializer.SessionLoader)1 RemoteCacheSessionsLoaderContext (org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoaderContext)1