Search in sources :

Example 16 with UserSessionEntity

use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.

the class UserSessionAdapter method setState.

@Override
public void setState(State state) {
    UserSessionUpdateTask task = new UserSessionUpdateTask() {

        @Override
        public void runUpdate(UserSessionEntity entity) {
            entity.setState(state);
        }
    };
    update(task);
}
Also used : UserSessionUpdateTask(org.keycloak.models.sessions.infinispan.changes.UserSessionUpdateTask) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 17 with UserSessionEntity

use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.

the class UserSessionAdapter method setNote.

@Override
public void setNote(String name, String value) {
    UserSessionUpdateTask task = new UserSessionUpdateTask() {

        @Override
        public void runUpdate(UserSessionEntity entity) {
            if (value == null) {
                if (entity.getNotes().containsKey(name)) {
                    removeNote(name);
                }
                return;
            }
            entity.getNotes().put(name, value);
        }
    };
    update(task);
}
Also used : UserSessionUpdateTask(org.keycloak.models.sessions.infinispan.changes.UserSessionUpdateTask) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 18 with UserSessionEntity

use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.

the class DistributedCacheWriteSkewTest method main.

public static void main(String[] args) throws Exception {
    Cache<String, UserSessionEntity> cache1 = createManager("node1").getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    Cache<String, UserSessionEntity> cache2 = createManager("node2").getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    // Create initial item
    UserSessionEntity session = new UserSessionEntity();
    session.setId("123");
    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());
    cache1.put("123", session);
    // cache1.replace("123", session);
    // Create 2 workers for concurrent write and start them
    Worker worker1 = new Worker(1, cache1);
    Worker worker2 = new Worker(2, cache2);
    long start = System.currentTimeMillis();
    System.out.println("Started clustering test");
    worker1.start();
    // worker1.join();
    worker2.start();
    worker1.join();
    worker2.join();
    long took = System.currentTimeMillis() - start;
    session = cache1.get("123");
    System.out.println("Took: " + took + " ms. Notes count: " + session.getNotes().size() + ", failedReplaceCounter: " + failedReplaceCounter.get());
    // JGroups statistics
    JChannel channel = (JChannel) ((JGroupsTransport) cache1.getAdvancedCache().getRpcManager().getTransport()).getChannel();
    System.out.println("Sent MB: " + channel.getSentBytes() / 1000000 + ", sent messages: " + channel.getSentMessages() + ", received MB: " + channel.getReceivedBytes() / 1000000 + ", received messages: " + channel.getReceivedMessages());
    // Kill JVM
    cache1.stop();
    cache2.stop();
    cache1.getCacheManager().stop();
    cache2.getCacheManager().stop();
    System.out.println("Managers killed");
}
Also used : AuthenticatedClientSessionEntity(org.keycloak.models.sessions.infinispan.entities.AuthenticatedClientSessionEntity) JChannel(org.jgroups.JChannel) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 19 with UserSessionEntity

use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.

the class DistributedCacheConcurrentWritesTest method createEntityInstance.

private static SessionEntityWrapper<UserSessionEntity> createEntityInstance(String id) {
    // Create initial item
    UserSessionEntity session = new UserSessionEntity();
    session.setId(id);
    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("foo-client", clientSession.getId());
    return new SessionEntityWrapper<>(session);
}
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 20 with UserSessionEntity

use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity 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)

Aggregations

UserSessionEntity (org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)27 SessionEntityWrapper (org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)13 AuthenticatedClientSessionEntity (org.keycloak.models.sessions.infinispan.entities.AuthenticatedClientSessionEntity)9 UserSessionUpdateTask (org.keycloak.models.sessions.infinispan.changes.UserSessionUpdateTask)6 UUID (java.util.UUID)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 OfflineUserSessionModel (org.keycloak.models.OfflineUserSessionModel)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 RemoteCache (org.infinispan.client.hotrod.RemoteCache)4 Cache (org.infinispan.Cache)3 HotRodClientException (org.infinispan.client.hotrod.exceptions.HotRodClientException)3 UserSessionModel (org.keycloak.models.UserSessionModel)3 Serializable (java.io.Serializable)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2 Objects (java.util.Objects)2 Set (java.util.Set)2