Search in sources :

Example 66 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder in project keycloak by keycloak.

the class InfinispanKeyStorageProviderTest method getKeysCache.

protected Cache<String, PublicKeysEntry> getKeysCache() {
    GlobalConfigurationBuilder gcb = new GlobalConfigurationBuilder();
    gcb.jmx().domain(InfinispanConnectionProvider.JMX_DOMAIN).enable();
    final DefaultCacheManager cacheManager = new DefaultCacheManager(gcb.build());
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.memory().evictionStrategy(EvictionStrategy.REMOVE).evictionType(EvictionType.COUNT).size(InfinispanConnectionProvider.KEYS_CACHE_DEFAULT_MAX);
    cb.jmxStatistics().enabled(true);
    Configuration cfg = cb.build();
    cacheManager.defineConfiguration(InfinispanConnectionProvider.KEYS_CACHE_NAME, cfg);
    return cacheManager.getCache(InfinispanConnectionProvider.KEYS_CACHE_NAME);
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) Configuration(org.infinispan.configuration.cache.Configuration)

Example 67 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder in project keycloak by keycloak.

the class DefaultInfinispanConnectionProviderFactory method initEmbedded.

protected void initEmbedded() {
    GlobalConfigurationBuilder gcb = new GlobalConfigurationBuilder();
    boolean clustered = config.getBoolean("clustered", false);
    boolean async = config.getBoolean("async", false);
    boolean useKeycloakTimeService = config.getBoolean("useKeycloakTimeService", false);
    this.topologyInfo = new TopologyInfo(cacheManager, config, true);
    if (clustered) {
        String jgroupsUdpMcastAddr = config.get("jgroupsUdpMcastAddr", System.getProperty(InfinispanConnectionProvider.JGROUPS_UDP_MCAST_ADDR));
        configureTransport(gcb, topologyInfo.getMyNodeName(), topologyInfo.getMySiteName(), jgroupsUdpMcastAddr, "default-configs/default-keycloak-jgroups-udp.xml");
        gcb.jmx().domain(InfinispanConnectionProvider.JMX_DOMAIN + "-" + topologyInfo.getMyNodeName()).enable();
    } else {
        gcb.jmx().domain(InfinispanConnectionProvider.JMX_DOMAIN).enable();
    }
    // For Infinispan 10, we go with the JBoss marshalling.
    // TODO: This should be replaced later with the marshalling recommended by infinispan. Probably protostream.
    // See https://infinispan.org/docs/stable/titles/developing/developing.html#marshalling for the details
    gcb.serialization().marshaller(new JBossUserMarshaller());
    cacheManager = new DefaultCacheManager(gcb.build());
    if (useKeycloakTimeService) {
        setTimeServiceToKeycloakTime(cacheManager);
    }
    containerManaged = false;
    logger.debug("Started embedded Infinispan cache container");
    ConfigurationBuilder modelCacheConfigBuilder = createCacheConfigurationBuilder();
    Configuration modelCacheConfiguration = modelCacheConfigBuilder.build();
    cacheManager.defineConfiguration(InfinispanConnectionProvider.REALM_CACHE_NAME, modelCacheConfiguration);
    cacheManager.defineConfiguration(InfinispanConnectionProvider.AUTHORIZATION_CACHE_NAME, modelCacheConfiguration);
    cacheManager.defineConfiguration(InfinispanConnectionProvider.USER_CACHE_NAME, modelCacheConfiguration);
    ConfigurationBuilder sessionConfigBuilder = createCacheConfigurationBuilder();
    if (clustered) {
        String sessionsMode = config.get("sessionsMode", "distributed");
        if (sessionsMode.equalsIgnoreCase("replicated")) {
            sessionConfigBuilder.clustering().cacheMode(async ? CacheMode.REPL_ASYNC : CacheMode.REPL_SYNC);
        } else if (sessionsMode.equalsIgnoreCase("distributed")) {
            sessionConfigBuilder.clustering().cacheMode(async ? CacheMode.DIST_ASYNC : CacheMode.DIST_SYNC);
        } else {
            throw new RuntimeException("Invalid value for sessionsMode");
        }
        int owners = config.getInt("sessionsOwners", 2);
        logger.debugf("Session owners: %d", owners);
        int l1Lifespan = config.getInt("l1Lifespan", 600000);
        boolean l1Enabled = l1Lifespan > 0;
        sessionConfigBuilder.clustering().hash().numOwners(owners).numSegments(config.getInt("sessionsSegments", 60)).l1().enabled(l1Enabled).lifespan(l1Lifespan).build();
    }
    // Base configuration doesn't contain any remote stores
    Configuration sessionCacheConfigurationBase = sessionConfigBuilder.build();
    boolean jdgEnabled = config.getBoolean("remoteStoreEnabled", false);
    if (jdgEnabled) {
        sessionConfigBuilder = createCacheConfigurationBuilder();
        sessionConfigBuilder.read(sessionCacheConfigurationBase);
        configureRemoteCacheStore(sessionConfigBuilder, async, InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    }
    Configuration sessionCacheConfiguration = sessionConfigBuilder.build();
    cacheManager.defineConfiguration(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME, sessionCacheConfiguration);
    if (jdgEnabled) {
        sessionConfigBuilder = createCacheConfigurationBuilder();
        sessionConfigBuilder.read(sessionCacheConfigurationBase);
        configureRemoteCacheStore(sessionConfigBuilder, async, InfinispanConnectionProvider.OFFLINE_USER_SESSION_CACHE_NAME);
    }
    sessionCacheConfiguration = sessionConfigBuilder.build();
    cacheManager.defineConfiguration(InfinispanConnectionProvider.OFFLINE_USER_SESSION_CACHE_NAME, sessionCacheConfiguration);
    if (jdgEnabled) {
        sessionConfigBuilder = createCacheConfigurationBuilder();
        sessionConfigBuilder.read(sessionCacheConfigurationBase);
        configureRemoteCacheStore(sessionConfigBuilder, async, InfinispanConnectionProvider.CLIENT_SESSION_CACHE_NAME);
    }
    sessionCacheConfiguration = sessionConfigBuilder.build();
    cacheManager.defineConfiguration(InfinispanConnectionProvider.CLIENT_SESSION_CACHE_NAME, sessionCacheConfiguration);
    if (jdgEnabled) {
        sessionConfigBuilder = createCacheConfigurationBuilder();
        sessionConfigBuilder.read(sessionCacheConfigurationBase);
        configureRemoteCacheStore(sessionConfigBuilder, async, InfinispanConnectionProvider.OFFLINE_CLIENT_SESSION_CACHE_NAME);
    }
    sessionCacheConfiguration = sessionConfigBuilder.build();
    cacheManager.defineConfiguration(InfinispanConnectionProvider.OFFLINE_CLIENT_SESSION_CACHE_NAME, sessionCacheConfiguration);
    if (jdgEnabled) {
        sessionConfigBuilder = createCacheConfigurationBuilder();
        sessionConfigBuilder.read(sessionCacheConfigurationBase);
        configureRemoteCacheStore(sessionConfigBuilder, async, InfinispanConnectionProvider.LOGIN_FAILURE_CACHE_NAME);
    }
    sessionCacheConfiguration = sessionConfigBuilder.build();
    cacheManager.defineConfiguration(InfinispanConnectionProvider.LOGIN_FAILURE_CACHE_NAME, sessionCacheConfiguration);
    cacheManager.defineConfiguration(InfinispanConnectionProvider.AUTHENTICATION_SESSIONS_CACHE_NAME, sessionCacheConfigurationBase);
    // Retrieve caches to enforce rebalance
    cacheManager.getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME, true);
    cacheManager.getCache(InfinispanConnectionProvider.OFFLINE_USER_SESSION_CACHE_NAME, true);
    cacheManager.getCache(InfinispanConnectionProvider.CLIENT_SESSION_CACHE_NAME, true);
    cacheManager.getCache(InfinispanConnectionProvider.OFFLINE_CLIENT_SESSION_CACHE_NAME, true);
    cacheManager.getCache(InfinispanConnectionProvider.LOGIN_FAILURE_CACHE_NAME, true);
    cacheManager.getCache(InfinispanConnectionProvider.AUTHENTICATION_SESSIONS_CACHE_NAME, true);
    ConfigurationBuilder replicationConfigBuilder = createCacheConfigurationBuilder();
    if (clustered) {
        replicationConfigBuilder.clustering().cacheMode(async ? CacheMode.REPL_ASYNC : CacheMode.REPL_SYNC);
    }
    if (jdgEnabled) {
        configureRemoteCacheStore(replicationConfigBuilder, async, InfinispanConnectionProvider.WORK_CACHE_NAME);
    }
    Configuration replicationEvictionCacheConfiguration = replicationConfigBuilder.expiration().enableReaper().wakeUpInterval(15, TimeUnit.SECONDS).build();
    cacheManager.defineConfiguration(InfinispanConnectionProvider.WORK_CACHE_NAME, replicationEvictionCacheConfiguration);
    cacheManager.getCache(InfinispanConnectionProvider.WORK_CACHE_NAME, true);
    long realmRevisionsMaxEntries = cacheManager.getCache(InfinispanConnectionProvider.REALM_CACHE_NAME).getCacheConfiguration().memory().size();
    realmRevisionsMaxEntries = realmRevisionsMaxEntries > 0 ? 2 * realmRevisionsMaxEntries : InfinispanConnectionProvider.REALM_REVISIONS_CACHE_DEFAULT_MAX;
    cacheManager.defineConfiguration(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME, getRevisionCacheConfig(realmRevisionsMaxEntries));
    cacheManager.getCache(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME, true);
    long userRevisionsMaxEntries = cacheManager.getCache(InfinispanConnectionProvider.USER_CACHE_NAME).getCacheConfiguration().memory().size();
    userRevisionsMaxEntries = userRevisionsMaxEntries > 0 ? 2 * userRevisionsMaxEntries : InfinispanConnectionProvider.USER_REVISIONS_CACHE_DEFAULT_MAX;
    cacheManager.defineConfiguration(InfinispanConnectionProvider.USER_REVISIONS_CACHE_NAME, getRevisionCacheConfig(userRevisionsMaxEntries));
    cacheManager.getCache(InfinispanConnectionProvider.USER_REVISIONS_CACHE_NAME, true);
    cacheManager.defineConfiguration(InfinispanConnectionProvider.KEYS_CACHE_NAME, getKeysCacheConfig());
    cacheManager.getCache(InfinispanConnectionProvider.KEYS_CACHE_NAME, true);
    final ConfigurationBuilder actionTokenCacheConfigBuilder = getActionTokenCacheConfig();
    if (clustered) {
        actionTokenCacheConfigBuilder.clustering().cacheMode(async ? CacheMode.REPL_ASYNC : CacheMode.REPL_SYNC);
    }
    if (jdgEnabled) {
        configureRemoteActionTokenCacheStore(actionTokenCacheConfigBuilder, async);
    }
    cacheManager.defineConfiguration(InfinispanConnectionProvider.ACTION_TOKEN_CACHE, actionTokenCacheConfigBuilder.build());
    cacheManager.getCache(InfinispanConnectionProvider.ACTION_TOKEN_CACHE, true);
    long authzRevisionsMaxEntries = cacheManager.getCache(InfinispanConnectionProvider.AUTHORIZATION_CACHE_NAME).getCacheConfiguration().memory().size();
    authzRevisionsMaxEntries = authzRevisionsMaxEntries > 0 ? 2 * authzRevisionsMaxEntries : InfinispanConnectionProvider.AUTHORIZATION_REVISIONS_CACHE_DEFAULT_MAX;
    cacheManager.defineConfiguration(InfinispanConnectionProvider.AUTHORIZATION_REVISIONS_CACHE_NAME, getRevisionCacheConfig(authzRevisionsMaxEntries));
    cacheManager.getCache(InfinispanConnectionProvider.AUTHORIZATION_REVISIONS_CACHE_NAME, true);
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) RemoteStoreConfigurationBuilder(org.infinispan.persistence.remote.configuration.RemoteStoreConfigurationBuilder) InfinispanUtil.createCacheConfigurationBuilder(org.keycloak.connections.infinispan.InfinispanUtil.createCacheConfigurationBuilder) Configuration(org.infinispan.configuration.cache.Configuration) JBossUserMarshaller(org.infinispan.jboss.marshalling.core.JBossUserMarshaller)

Example 68 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder in project keycloak by keycloak.

the class InfinispanUtil method createCacheConfigurationBuilder.

public static ConfigurationBuilder createCacheConfigurationBuilder() {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    // need to force the encoding to application/x-java-object to avoid unnecessary conversion of keys/values. See WFLY-14356.
    builder.encoding().mediaType(MediaType.APPLICATION_OBJECT_TYPE);
    return builder;
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) TransportConfigurationBuilder(org.infinispan.configuration.global.TransportConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder)

Example 69 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder in project keycloak by keycloak.

the class InfinispanUtil method getActionTokenCacheConfig.

public static ConfigurationBuilder getActionTokenCacheConfig() {
    ConfigurationBuilder cb = createCacheConfigurationBuilder();
    cb.memory().evictionStrategy(EvictionStrategy.NONE).evictionType(EvictionType.COUNT).size(InfinispanConnectionProvider.ACTION_TOKEN_CACHE_DEFAULT_MAX);
    cb.expiration().maxIdle(InfinispanConnectionProvider.ACTION_TOKEN_MAX_IDLE_SECONDS, TimeUnit.SECONDS).wakeUpInterval(InfinispanConnectionProvider.ACTION_TOKEN_WAKE_UP_INTERVAL_SECONDS, TimeUnit.SECONDS);
    return cb;
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) TransportConfigurationBuilder(org.infinispan.configuration.global.TransportConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder)

Example 70 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder in project keycloak by keycloak.

the class HotRodServerRule method createKeycloakCaches.

private void createKeycloakCaches(boolean async, String... cache) {
    ConfigurationBuilder sessionConfigBuilder1 = createCacheConfigurationBuilder();
    ConfigurationBuilder sessionConfigBuilder2 = createCacheConfigurationBuilder();
    sessionConfigBuilder1.clustering().cacheMode(async ? CacheMode.REPL_ASYNC : CacheMode.REPL_SYNC);
    sessionConfigBuilder2.clustering().cacheMode(async ? CacheMode.REPL_ASYNC : CacheMode.REPL_SYNC);
    sessionConfigBuilder1.sites().addBackup().site("site-2").backupFailurePolicy(BackupFailurePolicy.IGNORE).strategy(BackupConfiguration.BackupStrategy.SYNC).replicationTimeout(15000).enabled(true);
    sessionConfigBuilder2.sites().addBackup().site("site-1").backupFailurePolicy(BackupFailurePolicy.IGNORE).strategy(BackupConfiguration.BackupStrategy.SYNC).replicationTimeout(15000).enabled(true);
    Configuration sessionCacheConfiguration1 = sessionConfigBuilder1.build();
    Configuration sessionCacheConfiguration2 = sessionConfigBuilder2.build();
    for (String c : cache) {
        hotRodCacheManager.defineConfiguration(c, sessionCacheConfiguration1);
        hotRodCacheManager2.defineConfiguration(c, sessionCacheConfiguration2);
    }
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) HotRodServerConfigurationBuilder(org.infinispan.server.hotrod.configuration.HotRodServerConfigurationBuilder) BackupConfiguration(org.infinispan.configuration.cache.BackupConfiguration) HotRodServerConfiguration(org.infinispan.server.hotrod.configuration.HotRodServerConfiguration) Configuration(org.infinispan.configuration.cache.Configuration)

Aggregations

ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)71 Configuration (org.infinispan.configuration.cache.Configuration)36 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)29 DefaultCacheManager (org.infinispan.manager.DefaultCacheManager)23 Test (org.junit.Test)19 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)9 ClusteringConfigurationBuilder (org.infinispan.configuration.cache.ClusteringConfigurationBuilder)8 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)7 Properties (java.util.Properties)6 Map (java.util.Map)5 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)5 TimestampsRegionImpl (org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl)5 BeforeClass (org.junit.BeforeClass)5 AdvancedCache (org.infinispan.AdvancedCache)4 BackupConfiguration (org.infinispan.configuration.cache.BackupConfiguration)3 BackupConfigurationBuilder (org.infinispan.configuration.cache.BackupConfigurationBuilder)3 CacheMode (org.infinispan.configuration.cache.CacheMode)3 SitesConfigurationBuilder (org.infinispan.configuration.cache.SitesConfigurationBuilder)3 TransportConfigurationBuilder (org.infinispan.configuration.global.TransportConfigurationBuilder)3 EmbeddedTransactionManagerLookup (org.infinispan.transaction.lookup.EmbeddedTransactionManagerLookup)3