Search in sources :

Example 11 with CacheMode

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

the class InfinispanSessionCacheIdMapperUpdater method addTokenStoreUpdaters.

public static SessionIdMapperUpdater addTokenStoreUpdaters(Context context, SessionIdMapper mapper, SessionIdMapperUpdater previousIdMapperUpdater) {
    ServletContext servletContext = context.getServletContext();
    String containerName = servletContext == null ? null : servletContext.getInitParameter(AdapterConstants.REPLICATION_CONFIG_CONTAINER_PARAM_NAME);
    String cacheName = servletContext == null ? null : servletContext.getInitParameter(AdapterConstants.REPLICATION_CONFIG_SSO_CACHE_PARAM_NAME);
    // the following is based on https://github.com/jbossas/jboss-as/blob/7.2.0.Final/clustering/web-infinispan/src/main/java/org/jboss/as/clustering/web/infinispan/DistributedCacheManagerFactory.java#L116-L122
    String host = context.getParent() == null ? "" : context.getParent().getName();
    String contextPath = context.getPath();
    if ("/".equals(contextPath)) {
        contextPath = "/ROOT";
    }
    String deploymentSessionCacheName = host + contextPath;
    if (containerName == null || cacheName == null || deploymentSessionCacheName == null) {
        LOG.warnv("Cannot determine parameters of SSO cache for deployment {0}.", host + contextPath);
        return previousIdMapperUpdater;
    }
    String cacheContainerLookup = DEFAULT_CACHE_CONTAINER_JNDI_NAME + "/" + containerName;
    try {
        EmbeddedCacheManager cacheManager = (EmbeddedCacheManager) new InitialContext().lookup(cacheContainerLookup);
        Configuration ssoCacheConfiguration = cacheManager.getCacheConfiguration(cacheName);
        if (ssoCacheConfiguration == null) {
            Configuration cacheConfiguration = cacheManager.getCacheConfiguration(deploymentSessionCacheName);
            if (cacheConfiguration == null) {
                LOG.debugv("Using default configuration for SSO cache {0}.{1}.", containerName, cacheName);
                ssoCacheConfiguration = cacheManager.getDefaultCacheConfiguration();
            } else {
                LOG.debugv("Using distributed HTTP session cache configuration for SSO cache {0}.{1}, configuration taken from cache {2}", containerName, cacheName, deploymentSessionCacheName);
                ssoCacheConfiguration = cacheConfiguration;
                cacheManager.defineConfiguration(cacheName, ssoCacheConfiguration);
            }
        } else {
            LOG.debugv("Using custom configuration of SSO cache {0}.{1}.", containerName, cacheName);
        }
        CacheMode ssoCacheMode = ssoCacheConfiguration.clustering().cacheMode();
        if (ssoCacheMode != CacheMode.REPL_ASYNC && ssoCacheMode != CacheMode.REPL_SYNC) {
            LOG.warnv("SSO cache mode is {0}, it is recommended to use replicated mode instead.", ssoCacheConfiguration.clustering().cacheModeString());
        }
        Cache<String, String[]> ssoCache = cacheManager.getCache(cacheName, true);
        final SsoSessionCacheListener listener = new SsoSessionCacheListener(ssoCache, mapper);
        ssoCache.addListener(listener);
        // Not possible to add listener for cross-DC support because of too old Infinispan in AS 7
        warnIfRemoteStoreIsUsed(ssoCache);
        LOG.debugv("Added distributed SSO session cache, lookup={0}, cache name={1}", cacheContainerLookup, cacheName);
        SsoCacheSessionIdMapperUpdater updater = new SsoCacheSessionIdMapperUpdater(ssoCache, previousIdMapperUpdater);
        return updater;
    } catch (NamingException ex) {
        LOG.warnv("Failed to obtain distributed session cache container, lookup={0}", cacheContainerLookup);
        return previousIdMapperUpdater;
    }
}
Also used : Configuration(org.infinispan.configuration.cache.Configuration) ServletContext(javax.servlet.ServletContext) CacheMode(org.infinispan.configuration.cache.CacheMode) NamingException(javax.naming.NamingException) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) InitialContext(javax.naming.InitialContext)

Example 12 with CacheMode

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

the class InfinispanSessionCacheIdMapperUpdater method addTokenStoreUpdaters.

public static SessionIdMapperUpdater addTokenStoreUpdaters(DeploymentInfo deploymentInfo, SessionIdMapper mapper, SessionIdMapperUpdater previousIdMapperUpdater) {
    Map<String, String> initParameters = deploymentInfo.getInitParameters();
    String containerName = initParameters == null ? null : initParameters.get(AdapterConstants.REPLICATION_CONFIG_CONTAINER_PARAM_NAME);
    String cacheName = initParameters == null ? null : initParameters.get(AdapterConstants.REPLICATION_CONFIG_SSO_CACHE_PARAM_NAME);
    if (containerName == null || cacheName == null) {
        LOG.warnv("Cannot determine parameters of SSO cache for deployment {0}.", deploymentInfo.getDeploymentName());
        return previousIdMapperUpdater;
    }
    String cacheContainerLookup = DEFAULT_CACHE_CONTAINER_JNDI_NAME + "/" + containerName;
    String deploymentSessionCacheName = deploymentInfo.getDeploymentName();
    try {
        EmbeddedCacheManager cacheManager = (EmbeddedCacheManager) new InitialContext().lookup(cacheContainerLookup);
        Configuration ssoCacheConfiguration = cacheManager.getCacheConfiguration(cacheName);
        if (ssoCacheConfiguration == null) {
            Configuration cacheConfiguration = cacheManager.getCacheConfiguration(deploymentSessionCacheName);
            if (cacheConfiguration == null) {
                LOG.debugv("Using default configuration for SSO cache {0}.{1}.", containerName, cacheName);
                ssoCacheConfiguration = cacheManager.getDefaultCacheConfiguration();
            } else {
                LOG.debugv("Using distributed HTTP session cache configuration for SSO cache {0}.{1}, configuration taken from cache {2}", containerName, cacheName, deploymentSessionCacheName);
                ssoCacheConfiguration = cacheConfiguration;
                cacheManager.defineConfiguration(cacheName, ssoCacheConfiguration);
            }
        } else {
            LOG.debugv("Using custom configuration of SSO cache {0}.{1}.", containerName, cacheName);
        }
        CacheMode ssoCacheMode = ssoCacheConfiguration.clustering().cacheMode();
        if (ssoCacheMode != CacheMode.REPL_ASYNC && ssoCacheMode != CacheMode.REPL_SYNC) {
            LOG.warnv("SSO cache mode is {0}, it is recommended to use replicated mode instead.", ssoCacheConfiguration.clustering().cacheModeString());
        }
        Cache<String, String[]> ssoCache = cacheManager.getCache(cacheName, true);
        final SsoSessionCacheListener listener = new SsoSessionCacheListener(ssoCache, mapper);
        ssoCache.addListener(listener);
        addSsoCacheCrossDcListener(ssoCache, listener);
        LOG.debugv("Added distributed SSO session cache, lookup={0}, cache name={1}", cacheContainerLookup, cacheName);
        LOG.debugv("Adding session listener for SSO session cache, lookup={0}, cache name={1}", cacheContainerLookup, cacheName);
        SsoCacheSessionIdMapperUpdater updater = new SsoCacheSessionIdMapperUpdater(ssoCache, previousIdMapperUpdater);
        deploymentInfo.addSessionListener(updater);
        return updater;
    } catch (NamingException ex) {
        LOG.warnv("Failed to obtain distributed session cache container, lookup={0}", cacheContainerLookup);
        return previousIdMapperUpdater;
    }
}
Also used : Configuration(org.infinispan.configuration.cache.Configuration) CacheMode(org.infinispan.configuration.cache.CacheMode) NamingException(javax.naming.NamingException) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) InitialContext(javax.naming.InitialContext)

Example 13 with CacheMode

use of org.infinispan.configuration.cache.CacheMode in project infinispan by infinispan.

the class CacheParser method parseDistributedCache.

protected void parseDistributedCache(ConfigurationReader reader, ConfigurationBuilderHolder holder, String name, boolean template) {
    holder.pushScope(template ? ParserScope.CACHE_TEMPLATE : ParserScope.CACHE);
    if (!template && GlobUtils.isGlob(name))
        throw CONFIG.wildcardsNotAllowedInCacheNames(name);
    String configuration = reader.getAttributeValue(Attribute.CONFIGURATION.getLocalName());
    ConfigurationBuilder builder = getConfigurationBuilder(holder, name, template, configuration);
    CacheMode baseCacheMode = configuration == null ? CacheMode.DIST_SYNC : CacheMode.DIST_SYNC.toSync(builder.clustering().cacheMode().isSynchronous());
    builder.clustering().cacheMode(baseCacheMode);
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String value = reader.getAttributeValue(i);
        Attribute attribute = Attribute.forName(reader.getAttributeName(i));
        switch(attribute) {
            case OWNERS:
                {
                    builder.clustering().hash().numOwners(Integer.parseInt(value));
                    break;
                }
            case L1_LIFESPAN:
                {
                    long lifespan = Long.parseLong(value);
                    if (lifespan > 0)
                        builder.clustering().l1().enable().lifespan(lifespan);
                    else
                        builder.clustering().l1().disable();
                    break;
                }
            case INVALIDATION_CLEANUP_TASK_FREQUENCY:
                {
                    builder.clustering().l1().cleanupTaskFrequency(Long.parseLong(value));
                    break;
                }
            case CAPACITY:
                if (reader.getSchema().since(13, 0)) {
                    throw CONFIG.attributeRemoved(Attribute.CAPACITY.getLocalName());
                }
                CONFIG.configDeprecatedUseOther(Attribute.CAPACITY, Attribute.CAPACITY_FACTOR);
            case CAPACITY_FACTOR:
                {
                    builder.clustering().hash().capacityFactor(Float.parseFloat(value));
                    break;
                }
            default:
                {
                    this.parseSegmentedCacheAttribute(reader, i, attribute, value, builder, holder.getClassLoader(), baseCacheMode);
                }
        }
    }
    while (reader.inTag()) {
        Element element = Element.forName(reader.getLocalName());
        switch(element) {
            case GROUPS:
                {
                    parseGroups(reader, holder);
                    break;
                }
            default:
                {
                    this.parseSharedStateCacheElement(reader, element, holder);
                }
        }
    }
    holder.popScope();
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) IndexMergeConfigurationBuilder(org.infinispan.configuration.cache.IndexMergeConfigurationBuilder) SoftIndexFileStoreConfigurationBuilder(org.infinispan.persistence.sifs.configuration.SoftIndexFileStoreConfigurationBuilder) CustomStoreConfigurationBuilder(org.infinispan.configuration.cache.CustomStoreConfigurationBuilder) SecurityConfigurationBuilder(org.infinispan.configuration.cache.SecurityConfigurationBuilder) GroupsConfigurationBuilder(org.infinispan.configuration.cache.GroupsConfigurationBuilder) InterceptorConfigurationBuilder(org.infinispan.configuration.cache.InterceptorConfigurationBuilder) SingleFileStoreConfigurationBuilder(org.infinispan.configuration.cache.SingleFileStoreConfigurationBuilder) IndexWriterConfigurationBuilder(org.infinispan.configuration.cache.IndexWriterConfigurationBuilder) ClusterLoaderConfigurationBuilder(org.infinispan.configuration.cache.ClusterLoaderConfigurationBuilder) AuthorizationConfigurationBuilder(org.infinispan.configuration.cache.AuthorizationConfigurationBuilder) StoreConfigurationBuilder(org.infinispan.configuration.cache.StoreConfigurationBuilder) MemoryConfigurationBuilder(org.infinispan.configuration.cache.MemoryConfigurationBuilder) ClusteringConfigurationBuilder(org.infinispan.configuration.cache.ClusteringConfigurationBuilder) BackupConfigurationBuilder(org.infinispan.configuration.cache.BackupConfigurationBuilder) PartitionHandlingConfigurationBuilder(org.infinispan.configuration.cache.PartitionHandlingConfigurationBuilder) AbstractStoreConfigurationBuilder(org.infinispan.configuration.cache.AbstractStoreConfigurationBuilder) EncodingConfigurationBuilder(org.infinispan.configuration.cache.EncodingConfigurationBuilder) ContentTypeConfigurationBuilder(org.infinispan.configuration.cache.ContentTypeConfigurationBuilder) PersistenceConfigurationBuilder(org.infinispan.configuration.cache.PersistenceConfigurationBuilder) AsyncStoreConfigurationBuilder(org.infinispan.configuration.cache.AsyncStoreConfigurationBuilder) ParseUtils.ignoreAttribute(org.infinispan.configuration.parsing.ParseUtils.ignoreAttribute) ParseUtils.ignoreElement(org.infinispan.configuration.parsing.ParseUtils.ignoreElement) CacheMode(org.infinispan.configuration.cache.CacheMode)

Example 14 with CacheMode

use of org.infinispan.configuration.cache.CacheMode in project infinispan by infinispan.

the class CacheParser method parseScatteredCache.

protected void parseScatteredCache(ConfigurationReader reader, ConfigurationBuilderHolder holder, String name, boolean template) {
    if (!template && GlobUtils.isGlob(name))
        throw CONFIG.wildcardsNotAllowedInCacheNames(name);
    String configuration = reader.getAttributeValue(Attribute.CONFIGURATION.getLocalName());
    ConfigurationBuilder builder = getConfigurationBuilder(holder, name, template, configuration);
    CacheMode baseCacheMode = configuration == null ? CacheMode.SCATTERED_SYNC : CacheMode.SCATTERED_SYNC.toSync(builder.clustering().cacheMode().isSynchronous());
    ClusteringConfigurationBuilder clusteringBuilder = builder.clustering();
    clusteringBuilder.cacheMode(baseCacheMode);
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String value = reader.getAttributeValue(i);
        Attribute attribute = Attribute.forName(reader.getAttributeName(i));
        switch(attribute) {
            case INVALIDATION_BATCH_SIZE:
                {
                    clusteringBuilder.invalidationBatchSize(Integer.parseInt(value));
                    break;
                }
            case BIAS_ACQUISITION:
                {
                    clusteringBuilder.biasAcquisition(BiasAcquisition.valueOf(value));
                    break;
                }
            case BIAS_LIFESPAN:
                {
                    clusteringBuilder.biasLifespan(Long.parseLong(value), TimeUnit.MILLISECONDS);
                    break;
                }
            default:
                {
                    this.parseSegmentedCacheAttribute(reader, i, attribute, value, builder, holder.getClassLoader(), baseCacheMode);
                }
        }
    }
    while (reader.inTag()) {
        Element element = Element.forName(reader.getLocalName());
        switch(element) {
            default:
                {
                    this.parseSharedStateCacheElement(reader, element, holder);
                }
        }
    }
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) IndexMergeConfigurationBuilder(org.infinispan.configuration.cache.IndexMergeConfigurationBuilder) SoftIndexFileStoreConfigurationBuilder(org.infinispan.persistence.sifs.configuration.SoftIndexFileStoreConfigurationBuilder) CustomStoreConfigurationBuilder(org.infinispan.configuration.cache.CustomStoreConfigurationBuilder) SecurityConfigurationBuilder(org.infinispan.configuration.cache.SecurityConfigurationBuilder) GroupsConfigurationBuilder(org.infinispan.configuration.cache.GroupsConfigurationBuilder) InterceptorConfigurationBuilder(org.infinispan.configuration.cache.InterceptorConfigurationBuilder) SingleFileStoreConfigurationBuilder(org.infinispan.configuration.cache.SingleFileStoreConfigurationBuilder) IndexWriterConfigurationBuilder(org.infinispan.configuration.cache.IndexWriterConfigurationBuilder) ClusterLoaderConfigurationBuilder(org.infinispan.configuration.cache.ClusterLoaderConfigurationBuilder) AuthorizationConfigurationBuilder(org.infinispan.configuration.cache.AuthorizationConfigurationBuilder) StoreConfigurationBuilder(org.infinispan.configuration.cache.StoreConfigurationBuilder) MemoryConfigurationBuilder(org.infinispan.configuration.cache.MemoryConfigurationBuilder) ClusteringConfigurationBuilder(org.infinispan.configuration.cache.ClusteringConfigurationBuilder) BackupConfigurationBuilder(org.infinispan.configuration.cache.BackupConfigurationBuilder) PartitionHandlingConfigurationBuilder(org.infinispan.configuration.cache.PartitionHandlingConfigurationBuilder) AbstractStoreConfigurationBuilder(org.infinispan.configuration.cache.AbstractStoreConfigurationBuilder) EncodingConfigurationBuilder(org.infinispan.configuration.cache.EncodingConfigurationBuilder) ContentTypeConfigurationBuilder(org.infinispan.configuration.cache.ContentTypeConfigurationBuilder) PersistenceConfigurationBuilder(org.infinispan.configuration.cache.PersistenceConfigurationBuilder) AsyncStoreConfigurationBuilder(org.infinispan.configuration.cache.AsyncStoreConfigurationBuilder) ClusteringConfigurationBuilder(org.infinispan.configuration.cache.ClusteringConfigurationBuilder) ParseUtils.ignoreAttribute(org.infinispan.configuration.parsing.ParseUtils.ignoreAttribute) ParseUtils.ignoreElement(org.infinispan.configuration.parsing.ParseUtils.ignoreElement) CacheMode(org.infinispan.configuration.cache.CacheMode)

Example 15 with CacheMode

use of org.infinispan.configuration.cache.CacheMode in project infinispan by infinispan.

the class ClusterRoleMapper method setContext.

@Override
public void setContext(PrincipalRoleMapperContext context) {
    this.cacheManager = context.getCacheManager();
    GlobalConfiguration globalConfiguration = SecurityActions.getCacheManagerConfiguration(cacheManager);
    CacheMode cacheMode = globalConfiguration.isClustered() ? CacheMode.REPL_SYNC : CacheMode.LOCAL;
    ConfigurationBuilder cfg = new ConfigurationBuilder();
    cfg.clustering().cacheMode(cacheMode).stateTransfer().fetchInMemoryState(true).awaitInitialTransfer(false).security().authorization().disable();
    GlobalComponentRegistry registry = SecurityActions.getGlobalComponentRegistry(cacheManager);
    InternalCacheRegistry internalCacheRegistry = registry.getComponent(InternalCacheRegistry.class);
    internalCacheRegistry.registerInternalCache(CLUSTER_ROLE_MAPPER_CACHE, cfg.build(), EnumSet.of(InternalCacheRegistry.Flag.PERSISTENT));
    registry.registerComponent(this, PrincipalRoleMapper.class);
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) CacheMode(org.infinispan.configuration.cache.CacheMode) InternalCacheRegistry(org.infinispan.registry.InternalCacheRegistry) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry)

Aggregations

CacheMode (org.infinispan.configuration.cache.CacheMode)43 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)22 Configuration (org.infinispan.configuration.cache.Configuration)12 List (java.util.List)7 ClusteringConfigurationBuilder (org.infinispan.configuration.cache.ClusteringConfigurationBuilder)7 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)7 Test (org.testng.annotations.Test)7 ArrayList (java.util.ArrayList)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 ExecutorService (java.util.concurrent.ExecutorService)5 GroupsConfigurationBuilder (org.infinispan.configuration.cache.GroupsConfigurationBuilder)5 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Future (java.util.concurrent.Future)4 Consumer (java.util.function.Consumer)4 TransactionManager (javax.transaction.TransactionManager)4 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)4