Search in sources :

Example 21 with SessionEntityWrapper

use of org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper in project keycloak by keycloak.

the class ConcurrencyJDGRemoveSessionTest method main.

public static void main(String[] args) throws Exception {
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache1 = createManager(1).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache2 = createManager(2).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    // Create caches, listeners and finally worker threads
    Thread worker1 = createWorker(cache1, 1);
    Thread worker2 = createWorker(cache2, 2);
    Thread worker3 = createWorker(cache1, 1);
    Thread worker4 = createWorker(cache2, 2);
    // Create 100 initial sessions
    for (int i = 0; i < ITERATIONS; i++) {
        String sessionId = String.valueOf(i);
        SessionEntityWrapper<UserSessionEntity> wrappedSession = createSessionEntity(sessionId);
        cache1.put(sessionId, wrappedSession);
        removalCounts.put(sessionId, new AtomicInteger(0));
    }
    logger.info("SESSIONS CREATED");
    // Create 100 initial sessions
    for (int i = 0; i < ITERATIONS; i++) {
        String sessionId = String.valueOf(i);
        SessionEntityWrapper loadedWrapper = cache2.get(sessionId);
        Assert.assertNotNull("Loaded wrapper for key " + sessionId, loadedWrapper);
    }
    logger.info("SESSIONS AVAILABLE ON DC2");
    long start = System.currentTimeMillis();
    try {
        worker1.start();
        worker2.start();
        worker3.start();
        worker4.start();
        worker1.join();
        worker2.join();
        worker3.join();
        worker4.join();
        logger.info("SESSIONS REMOVED");
        Map<Integer, Integer> histogram = new HashMap<>();
        for (Map.Entry<String, AtomicInteger> entry : removalCounts.entrySet()) {
            int count = entry.getValue().get();
            int current = histogram.get(count) == null ? 0 : histogram.get(count);
            current++;
            histogram.put(count, current);
        }
        logger.infof("Histogram: %s", histogram.toString());
        logger.infof("Errors: %d", errorsCounter.get());
        // Thread.sleep(5000);
        // Doing it in opposite direction to ensure that newer are checked first.
        // This us currently FAILING (expected) as listeners are executed asynchronously.
        // for (int i=ITERATIONS-1 ; i>=0 ; i--) {
        // String sessionId = String.valueOf(i);
        // 
        // logger.infof("Before call cache2.get: %s", sessionId);
        // 
        // SessionEntityWrapper loadedWrapper = cache2.get(sessionId);
        // Assert.assertNull("Loaded wrapper not null for key " + sessionId, loadedWrapper);
        // }
        // 
        // logger.info("SESSIONS NOT AVAILABLE ON DC2");
        long took = System.currentTimeMillis() - start;
        logger.infof("took %d ms", took);
    // // Start and join workers
    // worker1.start();
    // worker2.start();
    // 
    // worker1.join();
    // worker2.join();
    } finally {
        Thread.sleep(2000);
        // Finish JVM
        cache1.getCacheManager().stop();
        cache2.getCacheManager().stop();
    }
// // Output
// for (Map.Entry<String, EntryInfo> entry : state.entrySet()) {
// System.out.println(entry.getKey() + ":::" + entry.getValue());
// worker1.cache.remove(entry.getKey());
// }
// System.out.println("Finished. Took: " + took + " ms. Notes: " + cache1.get("123").getEntity().getNotes().size() +
// ", successfulListenerWrites: " + successfulListenerWrites.get() + ", successfulListenerWrites2: " + successfulListenerWrites2.get() +
// ", failedReplaceCounter: " + failedReplaceCounter.get() + ", failedReplaceCounter2: " + failedReplaceCounter2.get() );
// 
// System.out.println("Sleeping before other report");
// 
// Thread.sleep(1000);
// 
// System.out.println("Finished. Took: " + took + " ms. Notes: " + cache1.get("123").getEntity().getNotes().size() +
// ", successfulListenerWrites: " + successfulListenerWrites.get() + ", successfulListenerWrites2: " + successfulListenerWrites2.get() +
// ", failedReplaceCounter: " + failedReplaceCounter.get() + ", failedReplaceCounter2: " + failedReplaceCounter2.get());
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 22 with SessionEntityWrapper

use of org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper in project keycloak by keycloak.

the class ConcurrencyJDGRemoveSessionTest method createSessionEntity.

private static SessionEntityWrapper<UserSessionEntity> createSessionEntity(String sessionId) {
    // Create 100 initial sessions
    UserSessionEntity session = new UserSessionEntity();
    session.setId(sessionId);
    session.setRealmId("foo");
    session.setBrokerSessionId("!23123123");
    session.setBrokerUserId(null);
    session.setUser("foo");
    session.setLoginUsername("foo");
    session.setIpAddress("123.44.143.178");
    session.setStarted(Time.currentTime());
    session.setLastSessionRefresh(Time.currentTime());
    AuthenticatedClientSessionEntity clientSession = new AuthenticatedClientSessionEntity(UUID.randomUUID());
    clientSession.setAuthMethod("saml");
    clientSession.setAction("something");
    clientSession.setTimestamp(1234);
    session.getAuthenticatedClientSessions().put(CLIENT_1_UUID.toString(), clientSession.getId());
    SessionEntityWrapper<UserSessionEntity> wrappedSession = new SessionEntityWrapper<>(session);
    return wrappedSession;
}
Also used : AuthenticatedClientSessionEntity(org.keycloak.models.sessions.infinispan.entities.AuthenticatedClientSessionEntity) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 23 with SessionEntityWrapper

use of org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper in project keycloak by keycloak.

the class JDGPutTest method main.

public static void main(String[] args) throws Exception {
    Cache<String, Object> cache1 = createManager(1).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    Cache<String, Object> cache2 = createManager(2).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    try {
        // RemoteCache remoteCache1 = InfinispanUtil.getRemoteCache(cache1);
        // RemoteCache remoteCache2 = InfinispanUtil.getRemoteCache(cache2);
        // remoteCache1.put("key1", new Book("book1", "desc", 1));
        // remoteCache2.put("key2", );
        String uuidStr = UUID.randomUUID().toString();
        System.out.println(uuidStr);
        UUID uuid = UUID.fromString(uuidStr);
        AuthenticatedClientSessionEntity ace = new AuthenticatedClientSessionEntity(uuid);
        SessionEntityWrapper wrapper = new SessionEntityWrapper(ace);
        cache1.put("key1", wrapper);
        // cache1.put("key1", "val1");
        // AuthenticatedClientSessionEntity val1 = (AuthenticatedClientSessionEntity) cache2.get("key1");
        // RemoteCache remoteCache1 = InfinispanUtil.getRemoteCache(cache1);
        // remoteCache1.put("key1", "val1");
        RemoteCache remoteCache2 = InfinispanUtil.getRemoteCache(cache2);
        Object o = remoteCache2.get("key1");
        logger.info("Before retrieve entries");
        try (CloseableIterator it = remoteCache2.retrieveEntries(null, 64)) {
            Object o2 = it.next();
            logger.info("o2: " + o2);
        }
        // Object key = remoteCache2.keySet().iterator().next();
        // Object value = remoteCache2.values().iterator().next();
        // logger.info("Key: " + key + ", val: " + value);
        bulkLoadSessions(remoteCache2);
    } finally {
        Thread.sleep(2000);
        // Finish JVM
        cache1.getCacheManager().stop();
        cache2.getCacheManager().stop();
    }
}
Also used : AuthenticatedClientSessionEntity(org.keycloak.models.sessions.infinispan.entities.AuthenticatedClientSessionEntity) CloseableIterator(org.infinispan.commons.util.CloseableIterator) RemoteCache(org.infinispan.client.hotrod.RemoteCache) UUID(java.util.UUID) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)

Example 24 with SessionEntityWrapper

use of org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper in project keycloak by keycloak.

the class DistributedCacheConcurrentWritesTest method createCache.

// Cache creation utils
public static BasicCache<String, SessionEntityWrapper<UserSessionEntity>> createCache(String nodeName) {
    EmbeddedCacheManager mgr = createManager(nodeName);
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = mgr.getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    return cache;
}
Also used : EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)

Example 25 with SessionEntityWrapper

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

SessionEntityWrapper (org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)30 UserSessionEntity (org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)13 RemoteCache (org.infinispan.client.hotrod.RemoteCache)9 AuthenticatedClientSessionEntity (org.keycloak.models.sessions.infinispan.entities.AuthenticatedClientSessionEntity)8 HashMap (java.util.HashMap)5 UUID (java.util.UUID)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 InfinispanConnectionProvider (org.keycloak.connections.infinispan.InfinispanConnectionProvider)5 UserSessionPersisterProvider (org.keycloak.models.session.UserSessionPersisterProvider)5 Map (java.util.Map)4 Cache (org.infinispan.Cache)4 RealmModel (org.keycloak.models.RealmModel)4 Set (java.util.Set)3 Future (java.util.concurrent.Future)3 TimeUnit (java.util.concurrent.TimeUnit)3 BiFunction (java.util.function.BiFunction)3 Stream (java.util.stream.Stream)3 HotRodClientException (org.infinispan.client.hotrod.exceptions.HotRodClientException)3 Flag (org.infinispan.context.Flag)3