Search in sources :

Example 16 with RemoteCache

use of org.infinispan.client.hotrod.RemoteCache in project keycloak by keycloak.

the class RemoteCacheProvider method getRemoteCache.

public RemoteCache getRemoteCache(String cacheName) {
    if (availableCaches.get(cacheName) == null) {
        synchronized (this) {
            if (availableCaches.get(cacheName) == null) {
                RemoteCache remoteCache = loadRemoteCache(cacheName);
                availableCaches.put(cacheName, remoteCache);
            }
        }
    }
    return availableCaches.get(cacheName);
}
Also used : RemoteCache(org.infinispan.client.hotrod.RemoteCache)

Example 17 with RemoteCache

use of org.infinispan.client.hotrod.RemoteCache 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)

Example 18 with RemoteCache

use of org.infinispan.client.hotrod.RemoteCache in project keycloak by keycloak.

the class HotRodMapStorage method delete.

@Override
public long delete(QueryParameters<M> queryParameters) {
    IckleQueryMapModelCriteriaBuilder<E, M> iqmcb = queryParameters.getModelCriteriaBuilder().flashToModelCriteriaBuilder(createCriteriaBuilder());
    String queryString = "SELECT id " + iqmcb.getIckleQuery();
    if (!queryParameters.getOrderBy().isEmpty()) {
        queryString += " ORDER BY " + queryParameters.getOrderBy().stream().map(HotRodMapStorage::toOrderString).collect(Collectors.joining(", "));
    }
    LOG.tracef("Executing delete Ickle query: %s", queryString);
    QueryFactory queryFactory = Search.getQueryFactory(remoteCache);
    Query<Object[]> query = paginateQuery(queryFactory.create(queryString), queryParameters.getOffset(), queryParameters.getLimit());
    query.setParameters(iqmcb.getParameters());
    AtomicLong result = new AtomicLong();
    CloseableIterator<Object[]> iterator = query.iterator();
    StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false).peek(e -> result.incrementAndGet()).map(a -> a[0]).map(String.class::cast).forEach(this::delete);
    iterator.close();
    return result.get();
}
Also used : Arrays(java.util.Arrays) Spliterators(java.util.Spliterators) Logger(org.jboss.logging.Logger) AbstractHotRodEntity(org.keycloak.models.map.storage.hotRod.common.AbstractHotRodEntity) MapKeycloakTransaction(org.keycloak.models.map.storage.MapKeycloakTransaction) QueryFactory(org.infinispan.query.dsl.QueryFactory) RemoteCache(org.infinispan.client.hotrod.RemoteCache) SearchableModelField(org.keycloak.storage.SearchableModelField) Function(java.util.function.Function) CloseableIterator(org.infinispan.commons.util.CloseableIterator) QueryParameters(org.keycloak.models.map.storage.QueryParameters) MapModelCriteriaBuilder(org.keycloak.models.map.storage.chm.MapModelCriteriaBuilder) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) ConcurrentHashMapCrudOperations(org.keycloak.models.map.storage.chm.ConcurrentHashMapCrudOperations) HotRodUtils.paginateQuery(org.keycloak.models.map.storage.hotRod.common.HotRodUtils.paginateQuery) StreamsUtil.closing(org.keycloak.utils.StreamsUtil.closing) KeycloakSession(org.keycloak.models.KeycloakSession) MapStorage(org.keycloak.models.map.storage.MapStorage) MapFieldPredicates(org.keycloak.models.map.storage.chm.MapFieldPredicates) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractEntity(org.keycloak.models.map.common.AbstractEntity) StringKeyConvertor(org.keycloak.models.map.common.StringKeyConvertor) Stream(java.util.stream.Stream) Search(org.infinispan.client.hotrod.Search) DeepCloner(org.keycloak.models.map.common.DeepCloner) Query(org.infinispan.query.dsl.Query) HotRodEntityDelegate(org.keycloak.models.map.storage.hotRod.common.HotRodEntityDelegate) ConcurrentHashMapKeycloakTransaction(org.keycloak.models.map.storage.chm.ConcurrentHashMapKeycloakTransaction) HotRodEntityDescriptor(org.keycloak.models.map.storage.hotRod.common.HotRodEntityDescriptor) QueryFactory(org.infinispan.query.dsl.QueryFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 19 with RemoteCache

use of org.infinispan.client.hotrod.RemoteCache in project keycloak by keycloak.

the class ConcurrencyJDGRemoveSessionTest method createWorker.

private static Thread createWorker(Cache<String, SessionEntityWrapper<UserSessionEntity>> cache, int threadId) {
    System.out.println("Retrieved cache: " + threadId);
    RemoteCache remoteCache = InfinispanUtil.getRemoteCache(cache);
    if (threadId == 1) {
        remoteCache1 = remoteCache;
    } else {
        remoteCache2 = remoteCache;
    }
    AtomicInteger counter = threadId == 1 ? successfulListenerWrites : successfulListenerWrites2;
    HotRodListener listener = new HotRodListener(cache, remoteCache, counter);
    remoteCache.addClientListener(listener);
    return new RemoteCacheWorker(remoteCache, threadId);
// return new CacheWorker(cache, threadId);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RemoteCache(org.infinispan.client.hotrod.RemoteCache)

Example 20 with RemoteCache

use of org.infinispan.client.hotrod.RemoteCache 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)

Aggregations

RemoteCache (org.infinispan.client.hotrod.RemoteCache)26 SessionEntityWrapper (org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)9 Cache (org.infinispan.Cache)8 Map (java.util.Map)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 HotRodClientException (org.infinispan.client.hotrod.exceptions.HotRodClientException)5 RemoteStore (org.infinispan.persistence.remote.RemoteStore)5 UUID (java.util.UUID)4 BasicCache (org.infinispan.commons.api.BasicCache)4 Logger (org.jboss.logging.Logger)4 KeycloakSession (org.keycloak.models.KeycloakSession)4 RealmModel (org.keycloak.models.RealmModel)4 UserSessionEntity (org.keycloak.models.sessions.infinispan.entities.UserSessionEntity)4 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 TimeUnit (java.util.concurrent.TimeUnit)3 BiFunction (java.util.function.BiFunction)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3