Search in sources :

Example 16 with SessionEntityWrapper

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

the class UserSessionAdapter method setLastSessionRefresh.

public void setLastSessionRefresh(int lastSessionRefresh) {
    if (offline) {
        // Received the message from the other DC that we should update the lastSessionRefresh in local cluster. Don't update DB in that case.
        // The other DC already did.
        Boolean ignoreRemoteCacheUpdate = (Boolean) session.getAttribute(CrossDCLastSessionRefreshListener.IGNORE_REMOTE_CACHE_UPDATE);
        if (ignoreRemoteCacheUpdate == null || !ignoreRemoteCacheUpdate) {
            provider.getPersisterLastSessionRefreshStore().putLastSessionRefresh(session, entity.getId(), realm.getId(), lastSessionRefresh);
        }
    }
    UserSessionUpdateTask task = new UserSessionUpdateTask() {

        @Override
        public void runUpdate(UserSessionEntity entity) {
            entity.setLastSessionRefresh(lastSessionRefresh);
        }

        @Override
        public CrossDCMessageStatus getCrossDCMessageStatus(SessionEntityWrapper<UserSessionEntity> sessionWrapper) {
            return new CrossDCLastSessionRefreshChecker(provider.getLastSessionRefreshStore(), provider.getOfflineLastSessionRefreshStore()).shouldSaveUserSessionToRemoteCache(UserSessionAdapter.this.session, UserSessionAdapter.this.realm, sessionWrapper, offline, lastSessionRefresh);
        }

        @Override
        public String toString() {
            return "setLastSessionRefresh(" + lastSessionRefresh + ')';
        }
    };
    update(task);
}
Also used : UserSessionUpdateTask(org.keycloak.models.sessions.infinispan.changes.UserSessionUpdateTask) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper) CrossDCLastSessionRefreshChecker(org.keycloak.models.sessions.infinispan.changes.sessions.CrossDCLastSessionRefreshChecker) UserSessionEntity(org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)

Example 17 with SessionEntityWrapper

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

the class UserSessionEntity method mergeRemoteEntityWithLocalEntity.

@Override
public SessionEntityWrapper mergeRemoteEntityWithLocalEntity(SessionEntityWrapper localEntityWrapper) {
    int lsrRemote = getLastSessionRefresh();
    SessionEntityWrapper entityWrapper;
    if (localEntityWrapper == null) {
        entityWrapper = new SessionEntityWrapper<>(this);
    } else {
        UserSessionEntity localUserSession = (UserSessionEntity) localEntityWrapper.getEntity();
        // local lastSessionRefresh should always contain the bigger
        if (lsrRemote < localUserSession.getLastSessionRefresh()) {
            setLastSessionRefresh(localUserSession.getLastSessionRefresh());
        }
        entityWrapper = new SessionEntityWrapper<>(localEntityWrapper.getLocalMetadata(), this);
    }
    entityWrapper.putLocalMetadataNoteInt(LAST_SESSION_REFRESH_REMOTE, lsrRemote);
    logger.debugf("Updating session entity '%s'. lastSessionRefresh=%d, lastSessionRefreshRemote=%d", getId(), getLastSessionRefresh(), lsrRemote);
    return entityWrapper;
}
Also used : SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)

Example 18 with SessionEntityWrapper

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

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

the class AbstractSessionCacheCommand method doRunCommand.

@Override
protected void doRunCommand(KeycloakSession session) {
    InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class);
    String cacheName = getArg(0);
    if (!SUPPORTED_CACHE_NAMES.contains(cacheName)) {
        log.errorf("Invalid cache name: '%s', Only cache names '%s' are supported", cacheName, SUPPORTED_CACHE_NAMES);
        throw new HandledException();
    }
    Cache<String, SessionEntityWrapper> ispnCache = provider.getCache(cacheName);
    doRunCacheCommand(session, ispnCache);
    ispnCache.entrySet().stream().skip(0).limit(10).collect(java.util.stream.Collectors.toMap(new java.util.function.Function() {

        public Object apply(Object entry) {
            return ((java.util.Map.Entry) entry).getKey();
        }
    }, new java.util.function.Function() {

        public Object apply(Object entry) {
            return ((java.util.Map.Entry) entry).getValue();
        }
    }));
}
Also used : InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)

Example 20 with SessionEntityWrapper

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

the class InfinispanUserSessionProviderFactory method checkRemoteCache.

private <K, V extends SessionEntity> RemoteCache checkRemoteCache(KeycloakSession session, Cache<K, SessionEntityWrapper<V>> ispnCache, RemoteCacheInvoker.MaxIdleTimeLoader maxIdleLoader, BiFunction<RealmModel, V, Long> lifespanMsLoader, BiFunction<RealmModel, V, Long> maxIdleTimeMsLoader) {
    Set<RemoteStore> remoteStores = InfinispanUtil.getRemoteStores(ispnCache);
    if (remoteStores.isEmpty()) {
        log.debugf("No remote store configured for cache '%s'", ispnCache.getName());
        return null;
    } else {
        log.infof("Remote store configured for cache '%s'", ispnCache.getName());
        RemoteCache<K, SessionEntityWrapper<V>> remoteCache = (RemoteCache) remoteStores.iterator().next().getRemoteCache();
        if (remoteCache == null) {
            throw new IllegalStateException("No remote cache available for the infinispan cache: " + ispnCache.getName());
        }
        remoteCacheInvoker.addRemoteCache(ispnCache.getName(), remoteCache, maxIdleLoader);
        RemoteCacheSessionListener hotrodListener = RemoteCacheSessionListener.createListener(session, ispnCache, remoteCache, lifespanMsLoader, maxIdleTimeMsLoader);
        remoteCache.addClientListener(hotrodListener);
        return remoteCache;
    }
}
Also used : RemoteCacheSessionListener(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheSessionListener) RemoteCache(org.infinispan.client.hotrod.RemoteCache) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper) RemoteStore(org.infinispan.persistence.remote.RemoteStore)

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