Search in sources :

Example 1 with RemoteCacheSessionsLoaderContext

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

the class JDGPutTest method bulkLoadSessions.

private static void bulkLoadSessions(RemoteCache remoteCache) {
    int size = remoteCache.size();
    int ispnSegmentsCount = getIspnSegmentsCount(remoteCache);
    RemoteCacheSessionsLoaderContext ctx = new RemoteCacheSessionsLoaderContext(ispnSegmentsCount, 64, 1);
    Set<Integer> myIspnSegments = getMyIspnSegments(0, ctx);
    Map<Object, Object> remoteEntries = new HashMap<>();
    CloseableIterator<Map.Entry> iterator = null;
    int countLoaded = 0;
    try {
        iterator = remoteCache.retrieveEntries(null, myIspnSegments, ctx.getSessionsPerSegment());
        while (iterator.hasNext()) {
            countLoaded++;
            Map.Entry entry = iterator.next();
            remoteEntries.put(entry.getKey(), entry.getValue());
        }
    } catch (RuntimeException e) {
        System.err.println(String.format("Error loading sessions from remote cache '%s' for segment '%d'", remoteCache.getName(), 1));
        throw e;
    } finally {
        if (iterator != null) {
            iterator.close();
        }
    }
    logger.info("Loaded " + remoteEntries);
}
Also used : HashMap(java.util.HashMap) RemoteCacheSessionsLoaderContext(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoaderContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with RemoteCacheSessionsLoaderContext

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

the class InitializerStateTest method assertSegmentsForRemoteLoader.

private void assertSegmentsForRemoteLoader(int sessionsTotal, int sessionsPerSegment, int ispnSegmentsCount, int expectedSegments) {
    RemoteCacheSessionsLoaderContext ctx = new RemoteCacheSessionsLoaderContext(ispnSegmentsCount, sessionsPerSegment, sessionsTotal);
    Assert.assertEquals(expectedSegments, ctx.getSegmentsCount());
}
Also used : RemoteCacheSessionsLoaderContext(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoaderContext)

Example 3 with RemoteCacheSessionsLoaderContext

use of org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoaderContext 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

RemoteCacheSessionsLoaderContext (org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoaderContext)3 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Cache (org.infinispan.Cache)1 RemoteCache (org.infinispan.client.hotrod.RemoteCache)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 RemoteCacheSessionsLoader (org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionsLoader)1