Search in sources :

Example 6 with UserSessionEntity

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

the class InfinispanUserSessionProvider method removeOfflineUserSession.

@Override
public void removeOfflineUserSession(RealmModel realm, UserSessionModel userSession) {
    UserSessionEntity userSessionEntity = getUserSessionEntity(realm, userSession, true);
    if (userSessionEntity != null) {
        removeUserSession(userSessionEntity, true);
    }
    session.getProvider(UserSessionPersisterProvider.class).removeUserSession(userSession.getId(), true);
}
Also used : UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 7 with UserSessionEntity

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

the class InfinispanUserSessionProvider method importUserSession.

private UserSessionEntity importUserSession(RealmModel realm, boolean offline, UserSessionModel persistentUserSession) {
    String sessionId = persistentUserSession.getId();
    log.debugf("Attempting to import user-session for sessionId=%s offline=%s", sessionId, offline);
    session.sessions().importUserSessions(Collections.singleton(persistentUserSession), offline);
    log.debugf("user-session imported, trying another lookup for sessionId=%s offline=%s", sessionId, offline);
    UserSessionEntity ispnUserSessionEntity = getUserSessionEntity(realm, sessionId, offline);
    if (ispnUserSessionEntity != null) {
        log.debugf("user-session found after import for sessionId=%s offline=%s", sessionId, offline);
        return ispnUserSessionEntity;
    }
    log.debugf("user-session could not be found after import for sessionId=%s offline=%s", sessionId, offline);
    return null;
}
Also used : UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 8 with UserSessionEntity

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

the class InfinispanUserSessionProvider method getUserSessionWithPredicate.

@Override
public UserSessionModel getUserSessionWithPredicate(RealmModel realm, String id, boolean offline, Predicate<UserSessionModel> predicate) {
    UserSessionModel userSession = getUserSession(realm, id, offline);
    if (userSession == null) {
        return null;
    }
    // We have userSession, which passes predicate. No need for remote lookup.
    if (predicate.test(userSession)) {
        log.debugf("getUserSessionWithPredicate(%s): found in local cache", id);
        return userSession;
    }
    // Try lookup userSession from remoteCache
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline);
    RemoteCache remoteCache = InfinispanUtil.getRemoteCache(cache);
    if (remoteCache != null) {
        SessionEntityWrapper<UserSessionEntity> remoteSessionEntityWrapper = (SessionEntityWrapper<UserSessionEntity>) remoteCache.get(id);
        if (remoteSessionEntityWrapper != null) {
            UserSessionEntity remoteSessionEntity = remoteSessionEntityWrapper.getEntity();
            log.debugf("getUserSessionWithPredicate(%s): remote cache contains session entity %s", id, remoteSessionEntity);
            UserSessionModel remoteSessionAdapter = wrap(realm, remoteSessionEntity, offline);
            if (predicate.test(remoteSessionAdapter)) {
                InfinispanChangelogBasedTransaction<String, UserSessionEntity> tx = getTransaction(offline);
                // Remote entity contains our predicate. Update local cache with the remote entity
                SessionEntityWrapper<UserSessionEntity> sessionWrapper = remoteSessionEntity.mergeRemoteEntityWithLocalEntity(tx.get(id));
                // Replace entity just in ispn cache. Skip remoteStore
                cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE, Flag.SKIP_CACHE_LOAD, Flag.IGNORE_RETURN_VALUES).replace(id, sessionWrapper);
                tx.reloadEntityInCurrentTransaction(realm, id, sessionWrapper);
                // Recursion. We should have it locally now
                return getUserSessionWithPredicate(realm, id, offline, predicate);
            } else {
                log.debugf("getUserSessionWithPredicate(%s): found, but predicate doesn't pass", id);
                return null;
            }
        } else {
            log.debugf("getUserSessionWithPredicate(%s): not found", id);
            // Session not available on remoteCache. Was already removed there. So removing locally too.
            // TODO: Can be optimized to skip calling remoteCache.remove
            removeUserSession(realm, userSession);
            return null;
        }
    } else {
        log.debugf("getUserSessionWithPredicate(%s): remote cache not available", id);
        return null;
    }
}
Also used : OfflineUserSessionModel(org.keycloak.models.OfflineUserSessionModel) UserSessionModel(org.keycloak.models.UserSessionModel) RemoteCache(org.infinispan.client.hotrod.RemoteCache) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 9 with UserSessionEntity

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

the class ConcurrencyJDGOfflineBackupsTest method main.

public static void main(String[] args) throws Exception {
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache1 = createManager(1).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    try {
        // 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();
        // clientSession.setAuthMethod("saml");
        // clientSession.setAction("something");
        // clientSession.setTimestamp(1234);
        // clientSession.setProtocolMappers(new HashSet<>(Arrays.asList("mapper1", "mapper2")));
        // clientSession.setRoles(new HashSet<>(Arrays.asList("role1", "role2")));
        // session.getAuthenticatedClientSessions().put(CLIENT_1_UUID.toString(), clientSession.getId());
        SessionEntityWrapper<UserSessionEntity> wrappedSession = new SessionEntityWrapper<>(session);
        // Some dummy testing of remoteStore behaviour
        logger.info("Before put");
        AtomicInteger successCount = new AtomicInteger(0);
        AtomicInteger errorsCount = new AtomicInteger(0);
        for (int i = 0; i < 100; i++) {
            try {
                cache1.getAdvancedCache().withFlags(// will still invoke remoteStore . Just doesn't propagate to cluster
                Flag.CACHE_MODE_LOCAL).put("123", wrappedSession);
                successCount.incrementAndGet();
                Thread.sleep(1000);
                logger.infof("Success in the iteration: %d", i);
            } catch (HotRodClientException hrce) {
                logger.errorf("Failed to put the item in the iteration: %d ", i);
                errorsCount.incrementAndGet();
            }
        }
        logger.infof("SuccessCount: %d, ErrorsCount: %d", successCount.get(), errorsCount.get());
    // logger.info("After put");
    // 
    // cache1.replace("123", wrappedSession);
    // 
    // logger.info("After replace");
    // 
    // cache1.get("123");
    // 
    // logger.info("After cache1.get");
    // cache2.get("123");
    // 
    // logger.info("After cache2.get");
    } finally {
        // Finish JVM
        cache1.getCacheManager().stop();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HotRodClientException(org.infinispan.client.hotrod.exceptions.HotRodClientException) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 10 with UserSessionEntity

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

the class ConcurrencyDistributedRemoveSessionTest 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)

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