org.infinispan.manager.DefaultCacheManager Maven / Gradle / Ivy
package org.infinispan.manager;
import static org.infinispan.factories.KnownComponentNames.CACHE_DEPENDENCY_GRAPH;
import static org.infinispan.util.logging.Log.CONFIG;
import static org.infinispan.util.logging.Log.CONTAINER;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.security.auth.Subject;
import org.infinispan.Cache;
import org.infinispan.commons.CacheConfigurationException;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.IllegalLifecycleStateException;
import org.infinispan.commons.api.CacheContainerAdmin;
import org.infinispan.commons.api.Lifecycle;
import org.infinispan.commons.configuration.ClassAllowList;
import org.infinispan.commons.dataconversion.MediaType;
import org.infinispan.commons.util.FileLookupFactory;
import org.infinispan.commons.util.Immutables;
import org.infinispan.configuration.ConfigurationManager;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.format.PropertyFormatter;
import org.infinispan.configuration.global.GlobalAuthorizationConfiguration;
import org.infinispan.configuration.global.GlobalConfiguration;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.configuration.global.TransportConfiguration;
import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
import org.infinispan.configuration.parsing.ParserRegistry;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.factories.GlobalComponentRegistry;
import org.infinispan.factories.InternalCacheFactory;
import org.infinispan.factories.KnownComponentNames;
import org.infinispan.factories.annotations.SurvivesRestarts;
import org.infinispan.factories.impl.BasicComponentRegistry;
import org.infinispan.factories.scopes.Scope;
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.globalstate.GlobalConfigurationManager;
import org.infinispan.health.Health;
import org.infinispan.health.impl.HealthImpl;
import org.infinispan.health.impl.jmx.HealthJMXExposerImpl;
import org.infinispan.health.jmx.HealthJMXExposer;
import org.infinispan.jmx.CacheManagerJmxRegistration;
import org.infinispan.jmx.annotations.DataType;
import org.infinispan.jmx.annotations.MBean;
import org.infinispan.jmx.annotations.ManagedAttribute;
import org.infinispan.jmx.annotations.ManagedOperation;
import org.infinispan.jmx.annotations.Parameter;
import org.infinispan.lifecycle.ComponentStatus;
import org.infinispan.manager.impl.ClusterExecutors;
import org.infinispan.notifications.cachemanagerlistener.CacheManagerNotifier;
import org.infinispan.registry.InternalCacheRegistry;
import org.infinispan.remoting.transport.Address;
import org.infinispan.remoting.transport.Transport;
import org.infinispan.security.AuditContext;
import org.infinispan.security.AuthorizationPermission;
import org.infinispan.security.impl.Authorizer;
import org.infinispan.security.impl.PrincipalRoleMapperContextImpl;
import org.infinispan.security.impl.SecureCacheImpl;
import org.infinispan.stats.CacheContainerStats;
import org.infinispan.stats.impl.CacheContainerStatsImpl;
import org.infinispan.util.CyclicDependencyException;
import org.infinispan.util.DependencyGraph;
import org.infinispan.util.concurrent.CompletableFutures;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
/**
* A CacheManager is the primary mechanism for retrieving a {@link Cache} instance, and is often used as a
* starting point to using the {@link Cache}.
*
* CacheManagers are heavyweight objects, and we foresee no more than one CacheManager being used per
* JVM (unless specific configuration requirements require more than one; but either way, this would be a minimal and
* finite number of instances).
*
* Constructing a CacheManager is done via one of its constructors, which optionally take in a {@link
* org.infinispan.configuration.cache.Configuration} or a path or URL to a configuration XML file.
*
* Lifecycle - CacheManagers have a lifecycle (it implements {@link Lifecycle}) and the default constructors
* also call {@link #start()}. Overloaded versions of the constructors are available, that do not start the
* CacheManager, although it must be kept in mind that CacheManagers need to be started before they
* can be used to create Cache instances.
*
* Once constructed, CacheManagers should be made available to any component that requires a Cache,
* via JNDI or via some other mechanism such as an IoC container.
*
* You obtain Cache instances from the CacheManager by using one of the overloaded
* getCache(), methods. Note that with getCache(), there is no guarantee that the instance you get is
* brand-new and empty, since caches are named and shared. Because of this, the CacheManager also acts as a
* repository of Caches, and is an effective mechanism of looking up or creating Caches on demand.
*
* When the system shuts down, it should call {@link #stop()} on the CacheManager. This will ensure all caches
* within its scope are properly stopped as well.
*
* Sample usage:
*
* CacheManager manager = CacheManager.getInstance("my-config-file.xml");
* Cache<String, Person> entityCache = manager.getCache("myEntityCache");
* entityCache.put("aPerson", new Person());
*
* ConfigurationBuilder confBuilder = new ConfigurationBuilder();
* confBuilder.clustering().cacheMode(CacheMode.REPL_SYNC);
* manager.createCache("myReplicatedCache", confBuilder.build());
* Cache<String, String> replicatedCache = manager.getCache("myReplicatedCache");
*
*
* @author Manik Surtani
* @author Galder Zamarreño
* @since 4.0
*/
@Scope(Scopes.GLOBAL)
@SurvivesRestarts
@MBean(objectName = DefaultCacheManager.OBJECT_NAME, description = "Component that acts as a manager, factory and container for caches in the system.")
public class DefaultCacheManager implements EmbeddedCacheManager {
public static final String OBJECT_NAME = "CacheManager";
private static final Log log = LogFactory.getLog(DefaultCacheManager.class);
private final ConcurrentMap>> caches = new ConcurrentHashMap<>();
private final GlobalComponentRegistry globalComponentRegistry;
private final Authorizer authorizer;
private final DependencyGraph cacheDependencyGraph = new DependencyGraph<>();
private final CacheContainerStats stats;
private final Health health;
private final ConfigurationManager configurationManager;
private final String defaultCacheName;
private final Lock lifecycleLock = new ReentrantLock();
private final Condition lifecycleCondition = lifecycleLock.newCondition();
private volatile ComponentStatus status = ComponentStatus.INSTANTIATED;
private final DefaultCacheManagerAdmin cacheManagerAdmin;
private final ClassAllowList classAllowList;
private final CacheManagerInfo cacheManagerInfo;
// Keep the transport around so async view listeners can still see the address after stop
private volatile Transport transport;
/**
* Constructs and starts a default instance of the CacheManager, using configuration defaults. See
* {@link org.infinispan.configuration.cache.Configuration} and {@link org.infinispan.configuration.global.GlobalConfiguration}
* for details of these defaults.
*/
public DefaultCacheManager() {
this(null, null, true);
}
/**
* Constructs a default instance of the CacheManager, using configuration defaults. See
* {@link org.infinispan.configuration.cache.Configuration} and {@link org.infinispan.configuration.global.GlobalConfiguration}
* for details of these defaults.
*
* @param start if true, the cache manager is started
*/
public DefaultCacheManager(boolean start) {
this(null, null, start);
}
/**
* Constructs and starts a new instance of the CacheManager, using the default configuration passed in. See
* {@link org.infinispan.configuration.cache.Configuration} and {@link org.infinispan.configuration.global.GlobalConfiguration}
* for details of these defaults.
*
* @param defaultConfiguration configuration to use as a template for all caches created
* @deprecated Since 11.0, please use {@link #DefaultCacheManager(ConfigurationBuilderHolder, boolean)} instead.
*/
@Deprecated
public DefaultCacheManager(Configuration defaultConfiguration) {
this(null, defaultConfiguration, true);
}
/**
* Constructs a new instance of the CacheManager, using the default configuration passed in. See
* {@link org.infinispan.configuration.global.GlobalConfiguration} for details of these defaults.
*
* @param defaultConfiguration configuration file to use as a template for all caches created
* @param start if true, the cache manager is started
* @deprecated Since 11.0, please use {@link #DefaultCacheManager(ConfigurationBuilderHolder, boolean)} instead.
*/
@Deprecated
public DefaultCacheManager(Configuration defaultConfiguration, boolean start) {
this(null, defaultConfiguration, start);
}
/**
* Constructs and starts a new instance of the CacheManager, using the global configuration passed in, and system
* defaults for the default named cache configuration. See {@link org.infinispan.configuration.cache.Configuration}
* for details of these defaults.
*
* @param globalConfiguration GlobalConfiguration to use for all caches created
*/
public DefaultCacheManager(GlobalConfiguration globalConfiguration) {
this(globalConfiguration, null, true);
}
/**
* Constructs a new instance of the CacheManager, using the global configuration passed in, and system defaults for
* the default named cache configuration. See {@link org.infinispan.configuration.cache.Configuration}
* for details of these defaults.
*
* @param globalConfiguration GlobalConfiguration to use for all caches created
* @param start if true, the cache manager is started.
*/
public DefaultCacheManager(GlobalConfiguration globalConfiguration, boolean start) {
this(globalConfiguration, null, start);
}
/**
* Constructs and starts a new instance of the CacheManager, using the global and default configurations passed in.
* If either of these are null, system defaults are used.
*
* @param globalConfiguration global configuration to use. If null, a default instance is created.
* @param defaultConfiguration default configuration to use. If null, a default instance is created.
* @deprecated Since 11.0, please use {@link #DefaultCacheManager(ConfigurationBuilderHolder, boolean)} instead.
*/
@Deprecated
public DefaultCacheManager(GlobalConfiguration globalConfiguration, Configuration defaultConfiguration) {
this(globalConfiguration, defaultConfiguration, true);
}
/**
* Constructs a new instance of the CacheManager, using the global and default configurations passed in. If either of
* these are null, system defaults are used.
*
* @param globalConfiguration global configuration to use. If null, a default instance is created.
* @param defaultConfiguration default configuration to use. If null, a default instance is created.
* @param start if true, the cache manager is started
* @deprecated Since 11.0, please use {@link #DefaultCacheManager(ConfigurationBuilderHolder, boolean)} instead.
*/
@Deprecated
public DefaultCacheManager(GlobalConfiguration globalConfiguration, Configuration defaultConfiguration,
boolean start) {
globalConfiguration = globalConfiguration == null ? new GlobalConfigurationBuilder().build() : globalConfiguration;
this.configurationManager = new ConfigurationManager(globalConfiguration);
if (defaultConfiguration != null) {
if (globalConfiguration.defaultCacheName().isPresent()) {
defaultCacheName = globalConfiguration.defaultCacheName().get();
} else {
throw CONFIG.defaultCacheConfigurationWithoutName();
}
configurationManager.putConfiguration(defaultCacheName, defaultConfiguration);
} else {
if (globalConfiguration.defaultCacheName().isPresent()) {
throw CONFIG.missingDefaultCacheDeclaration(globalConfiguration.defaultCacheName().get());
} else {
defaultCacheName = null;
}
}
ModuleRepository moduleRepository = ModuleRepository.newModuleRepository(globalConfiguration.classLoader(), globalConfiguration);
this.classAllowList = globalConfiguration.serialization().allowList().create();
this.globalComponentRegistry = new GlobalComponentRegistry(globalConfiguration, this, caches.keySet(),
moduleRepository, configurationManager);
InternalCacheRegistry internalCacheRegistry = globalComponentRegistry.getComponent(InternalCacheRegistry.class);
this.globalComponentRegistry.registerComponent(cacheDependencyGraph, CACHE_DEPENDENCY_GRAPH, false);
this.authorizer = new Authorizer(globalConfiguration.security(), AuditContext.CACHEMANAGER, globalConfiguration.cacheManagerName(), null);
this.globalComponentRegistry.registerComponent(authorizer, Authorizer.class);
this.stats = new CacheContainerStatsImpl(this);
globalComponentRegistry.registerComponent(stats, CacheContainerStats.class);
health = new HealthImpl(this, globalComponentRegistry.getComponent(InternalCacheRegistry.class));
cacheManagerInfo = new CacheManagerInfo(this, configurationManager, internalCacheRegistry);
globalComponentRegistry.registerComponent(new HealthJMXExposerImpl(health), HealthJMXExposer.class);
this.cacheManagerAdmin = new DefaultCacheManagerAdmin(this, authorizer, EnumSet.noneOf(CacheContainerAdmin.AdminFlag.class), null,
globalComponentRegistry.getComponent(GlobalConfigurationManager.class));
if (start)
start();
}
/**
* Constructs and starts a new instance of the CacheManager, using the configuration file name passed in. This
* constructor first searches for the named file on the classpath, and failing that, treats the file name as an
* absolute path.
*
* @param configurationFile name of configuration file to use as a template for all caches created
* @throws java.io.IOException if there is a problem with the configuration file.
*/
public DefaultCacheManager(String configurationFile) throws IOException {
this(configurationFile, true);
}
/**
* Constructs a new instance of the CacheManager, using the configuration file name passed in. This constructor first
* searches for the named file on the classpath, and failing that, treats the file name as an absolute path.
*
* @param configurationFile name of configuration file to use as a template for all caches created
* @param start if true, the cache manager is started
* @throws java.io.IOException if there is a problem with the configuration file.
*/
public DefaultCacheManager(String configurationFile, boolean start) throws IOException {
this(FileLookupFactory.newInstance().lookupFileStrict(configurationFile, Thread.currentThread().getContextClassLoader()), start);
}
/**
* Constructs and starts a new instance of the CacheManager, using the input stream passed in to read configuration
* file contents.
*
* @param configurationStream stream containing configuration file contents, to use as a template for all caches
* created
* @throws java.io.IOException if there is a problem with the configuration stream.
*/
public DefaultCacheManager(InputStream configurationStream) throws IOException {
this(configurationStream, true);
}
/**
* Constructs a new instance of the CacheManager, using the input stream passed in to read configuration file
* contents.
*
* @param configurationStream stream containing configuration file contents, to use as a template for all caches
* created
* @param start if true, the cache manager is started
* @throws java.io.IOException if there is a problem reading the configuration stream
*/
public DefaultCacheManager(InputStream configurationStream, boolean start) throws IOException {
this(new ParserRegistry().parse(configurationStream, null, MediaType.APPLICATION_XML), start);
}
/**
* Constructs a new instance of the CacheManager, using the input stream passed in to read configuration file
* contents.
*
* @param configurationURL stream containing configuration file contents, to use as a template for all caches
* created
* @param start if true, the cache manager is started
* @throws java.io.IOException if there is a problem reading the configuration stream
*/
public DefaultCacheManager(URL configurationURL, boolean start) throws IOException {
this(new ParserRegistry().parse(configurationURL), start);
}
/**
* Constructs a new instance of the CacheManager, using the holder passed in to read configuration settings.
*
* @param holder holder containing configuration settings, to use as a template for all caches created
* @param start if true, the cache manager is started
*/
public DefaultCacheManager(ConfigurationBuilderHolder holder, boolean start) {
try {
configurationManager = new ConfigurationManager(holder);
GlobalConfiguration globalConfiguration = configurationManager.getGlobalConfiguration();
classAllowList = globalConfiguration.serialization().allowList().create();
defaultCacheName = globalConfiguration.defaultCacheName().orElse(null);
ModuleRepository moduleRepository = ModuleRepository.newModuleRepository(globalConfiguration.classLoader(), globalConfiguration);
globalComponentRegistry = new GlobalComponentRegistry(globalConfiguration, this, caches.keySet(),
moduleRepository, configurationManager);
InternalCacheRegistry internalCacheRegistry = globalComponentRegistry.getComponent(InternalCacheRegistry.class);
globalComponentRegistry.registerComponent(cacheDependencyGraph, CACHE_DEPENDENCY_GRAPH, false);
stats = new CacheContainerStatsImpl(this);
globalComponentRegistry.registerComponent(stats, CacheContainerStats.class);
health = new HealthImpl(this, internalCacheRegistry);
cacheManagerInfo = new CacheManagerInfo(this, getConfigurationManager(), internalCacheRegistry);
globalComponentRegistry.registerComponent(new HealthJMXExposerImpl(health), HealthJMXExposer.class);
authorizer = new Authorizer(globalConfiguration.security(), AuditContext.CACHEMANAGER, globalConfiguration.cacheManagerName(), null);
globalComponentRegistry.registerComponent(authorizer, Authorizer.class);
cacheManagerAdmin = new DefaultCacheManagerAdmin(this, authorizer, EnumSet.noneOf(CacheContainerAdmin.AdminFlag.class),
null, globalComponentRegistry.getComponent(GlobalConfigurationManager.class));
} catch (CacheConfigurationException ce) {
throw ce;
} catch (RuntimeException re) {
throw new CacheConfigurationException(re);
}
if (start)
start();
}
private DefaultCacheManager(DefaultCacheManager original) {
this.authorizer = original.authorizer;
this.configurationManager = original.configurationManager;
this.health = original.health;
this.classAllowList = original.classAllowList;
this.cacheManagerInfo = original.cacheManagerInfo;
this.cacheManagerAdmin = original.cacheManagerAdmin;
this.defaultCacheName = original.defaultCacheName;
this.stats = original.stats;
this.globalComponentRegistry = original.globalComponentRegistry;
}
@Override
public Configuration defineConfiguration(String name, Configuration configuration) {
return doDefineConfiguration(name, configuration);
}
@Override
public Configuration defineConfiguration(String name, String template, Configuration configurationOverride) {
if (template != null) {
Configuration c = configurationManager.getConfiguration(template, true);
if (c == null) {
throw CONFIG.undeclaredConfiguration(template, name);
} else if (configurationOverride == null) {
return doDefineConfiguration(name, c);
} else {
return doDefineConfiguration(name, c, configurationOverride);
}
}
return doDefineConfiguration(name, configurationOverride);
}
private Configuration doDefineConfiguration(String name, Configuration... configurations) {
authorizer.checkPermission(getSubject(), AuthorizationPermission.ADMIN);
assertIsNotTerminated();
if (name == null || configurations == null)
throw new NullPointerException("Null arguments not allowed");
Configuration existing = configurationManager.getConfiguration(name, false);
if (existing != null) {
throw CONFIG.configAlreadyDefined(name);
}
ConfigurationBuilder builder = new ConfigurationBuilder();
boolean template = true;
for (Configuration configuration : configurations) {
if (configuration != null) {
builder.read(configuration);
template = template && configuration.isTemplate();
} else {
throw new NullPointerException("Null arguments not allowed");
}
}
builder.template(template);
return configurationManager.putConfiguration(name, builder);
}
@Override
public void undefineConfiguration(String configurationName) {
authorizer.checkPermission(getSubject(), AuthorizationPermission.ADMIN);
Configuration existing = configurationManager.getConfiguration(configurationName, false);
if (existing != null) {
for (CompletableFuture> cacheFuture : caches.values()) {
Cache, ?> cache = cacheFuture.exceptionally(t -> null).join();
if (cache != null && cache.getCacheConfiguration() == existing && cache.getStatus() != ComponentStatus.TERMINATED) {
throw CONFIG.configurationInUse(configurationName);
}
}
configurationManager.removeConfiguration(configurationName);
globalComponentRegistry.removeCache(configurationName);
}
}
@Override
public Cache createCache(String name, Configuration configuration) {
defineConfiguration(name, configuration);
return getCache(name);
}
/**
* Retrieves the default cache associated with this cache manager. Note that the default cache does not need to be
* explicitly created with {@link #createCache(String)} (String)} since it is automatically created lazily
* when first used.
*
* As such, this method is always guaranteed to return the default cache.
*
* @return the default cache.
*/
@Override
public Cache getCache() {
if (defaultCacheName == null) {
throw CONFIG.noDefaultCache();
}
return internalGetCache(defaultCacheName);
}
/**
* Retrieves a named cache from the system. If the cache has been previously created with the same name, the running
* cache instance is returned. Otherwise, this method attempts to create the cache first.
*
* When creating a new cache, this method will use the configuration passed in to the CacheManager on construction,
* as a template, and then optionally apply any overrides previously defined for the named cache using the {@link
* #defineConfiguration(String, Configuration)} or {@link #defineConfiguration(String, String, Configuration)}
* methods, or declared in the configuration file.
*
* @param cacheName name of cache to retrieve
* @return a cache instance identified by cacheName
*/
@Override
public Cache getCache(String cacheName) {
return internalGetCache(cacheName);
}
private Cache internalGetCache(String cacheName) {
if (cacheName == null)
throw new NullPointerException("Null arguments not allowed");
assertIsNotTerminated();
// No need to block if another thread (or even the current thread) is starting the global components
// Because each cache component will wait for the global components it depends on
// And and ComponentRegistry depends on GlobalComponentRegistry.ModuleInitializer
internalStart(false);
CompletableFuture> cacheFuture = caches.get(cacheName);
if (cacheFuture != null) {
try {
return (Cache) cacheFuture.join();
} catch (CompletionException e) {
throw ((CacheException) e.getCause());
}
}
return createCache(cacheName);
}
@Override
public boolean cacheExists(String cacheName) {
return caches.containsKey(cacheName);
}
@Override
public Cache getCache(String cacheName, boolean createIfAbsent) {
boolean cacheExists = cacheExists(cacheName);
if (!cacheExists && !createIfAbsent)
return null;
else {
return internalGetCache(cacheName);
}
}
@Override
public EmbeddedCacheManager startCaches(final String... cacheNames) {
authorizer.checkPermission(getSubject(), AuthorizationPermission.LIFECYCLE);
internalStart(false);
Map threads = new HashMap<>(cacheNames.length);
final AtomicReference exception = new AtomicReference<>(null);
for (final String cacheName : cacheNames) {
if (!threads.containsKey(cacheName)) {
String threadName = "CacheStartThread," + configurationManager.getGlobalConfiguration().transport().nodeName() + "," + cacheName;
Thread thread = new Thread(threadName) {
@Override
public void run() {
try {
createCache(cacheName);
} catch (RuntimeException e) {
exception.set(e);
} catch (Throwable t) {
exception.set(new RuntimeException(t));
}
}
};
thread.start();
threads.put(cacheName, thread);
}
}
try {
for (Thread thread : threads.values()) {
thread.join();
}
} catch (InterruptedException e) {
throw new CacheException("Interrupted while waiting for the caches to start");
}
RuntimeException runtimeException = exception.get();
if (runtimeException != null) {
throw runtimeException;
}
return this;
}
@Override
public void removeCache(String cacheName) {
cacheManagerAdmin.removeCache(cacheName);
}
/**
* {@inheritDoc}
*/
@Override
public List getMembers() {
Transport t = getTransport();
return t == null ? null : t.getMembers();
}
/**
* {@inheritDoc}
*/
@Override
public Address getAddress() {
Transport t = getTransport();
return t == null ? null : t.getAddress();
}
/**
* {@inheritDoc}
*/
@Override
public Address getCoordinator() {
Transport t = getTransport();
return t == null ? null : t.getCoordinator();
}
@ManagedAttribute(description = "The logical address of the cluster's coordinator", displayName = "Coordinator address")
public String getCoordinatorAddress() {
return cacheManagerInfo.getCoordinatorAddress();
}
/**
* {@inheritDoc}
*/
@Override
@ManagedAttribute(description = "Indicates whether this node is coordinator", displayName = "Is coordinator?")
public boolean isCoordinator() {
return cacheManagerInfo.isCoordinator();
}
private Cache createCache(String cacheName) {
final boolean trace = log.isTraceEnabled();
LogFactory.pushNDC(cacheName, trace);
try {
return wireAndStartCache(cacheName);
} finally {
LogFactory.popNDC(trace);
}
}
/**
* @return a null return value means the cache was created by someone else before we got the lock
*/
private Cache wireAndStartCache(String cacheName) {
Configuration c = configurationManager.getConfiguration(cacheName);
if (c == null) {
throw CONFIG.noSuchCacheConfiguration(cacheName);
}
if (c.security().authorization().enabled()) {
// Don't even attempt to wire anything if we don't have LIFECYCLE privileges
authorizer.checkPermission(c.security().authorization(), getSubject(), AuthorizationPermission.LIFECYCLE, null);
}
if (c.isTemplate()) {
throw CONFIG.templateConfigurationStartAttempt(cacheName);
}
CompletableFuture> cacheFuture = new CompletableFuture<>();
CompletableFuture> oldFuture = caches.computeIfAbsent(cacheName, name -> {
assertIsNotTerminated();
return cacheFuture;
});
Cache cache = null;
try {
if (oldFuture != cacheFuture) {
cache = (Cache) oldFuture.join();
if (!cache.getStatus().isTerminated()) {
return cache;
}
}
} catch (CompletionException ce) {
throw ((CacheException) ce.getCause());
}
try {
log.tracef("About to wire and start cache %s", cacheName);
if (cache == null) {
cache = new InternalCacheFactory().createCache(c, globalComponentRegistry, cacheName);
if (cache.getAdvancedCache().getAuthorizationManager() != null) {
cache = new SecureCacheImpl<>(cache.getAdvancedCache());
}
}
ComponentRegistry cr = SecurityActions.getUnwrappedCache(cache).getAdvancedCache().getComponentRegistry();
boolean notStartedYet =
cr.getStatus() != ComponentStatus.RUNNING && cr.getStatus() != ComponentStatus.INITIALIZING;
// start the cache-level components
cache.start();
cacheFuture.complete(cache);
boolean needToNotifyCacheStarted = notStartedYet && cr.getStatus() == ComponentStatus.RUNNING;
if (needToNotifyCacheStarted) {
globalComponentRegistry.notifyCacheStarted(cacheName);
}
log.tracef("Cache %s started", cacheName);
return cache;
} catch (CacheException e) {
cacheFuture.completeExceptionally(e);
throw e;
} catch (Throwable t) {
cacheFuture.completeExceptionally(new CacheException(t));
throw t;
}
}
@Override
public void start() {
authorizer.checkPermission(getSubject(), AuthorizationPermission.LIFECYCLE);
internalStart(true);
}
/**
* @param block {@code true} when we need all the global components to be running.
*/
private void internalStart(boolean block) {
if (status == ComponentStatus.RUNNING)
return;
final GlobalConfiguration globalConfiguration = configurationManager.getGlobalConfiguration();
lifecycleLock.lock();
try {
while (block && status == ComponentStatus.INITIALIZING) {
lifecycleCondition.await();
}
if (status != ComponentStatus.INSTANTIATED) {
return;
}
log.debugf("Starting cache manager %s", configurationManager.getGlobalConfiguration().transport().nodeName());
initializeSecurity(globalConfiguration);
updateStatus(ComponentStatus.INITIALIZING);
} catch (InterruptedException e) {
throw new CacheException("Interrupted waiting for the cache manager to start");
} finally {
lifecycleLock.unlock();
}
try {
globalComponentRegistry.getComponent(CacheManagerJmxRegistration.class).start();
globalComponentRegistry.start();
String nodeName = globalConfiguration.transport().nodeName();
log.debugf("Started cache manager %s on %s", nodeName, getAddress());
} catch (Exception e) {
throw new EmbeddedCacheManagerStartupException(e);
} finally {
updateStatus(globalComponentRegistry.getStatus());
}
}
private void initializeSecurity(GlobalConfiguration globalConfiguration) {
GlobalAuthorizationConfiguration authorizationConfig = globalConfiguration.security().authorization();
if (authorizationConfig.enabled() && System.getSecurityManager() == null) {
CONFIG.authorizationEnabledWithoutSecurityManager();
}
if (authorizationConfig.enabled()) {
authorizationConfig.principalRoleMapper().setContext(new PrincipalRoleMapperContextImpl(this));
}
}
private void updateStatus(ComponentStatus status) {
lifecycleLock.lock();
try {
this.status = status;
lifecycleCondition.signalAll();
} finally {
lifecycleLock.unlock();
}
}
private void terminate(String cacheName) {
CompletableFuture> cacheFuture = this.caches.get(cacheName);
if (cacheFuture != null) {
Cache, ?> cache = cacheFuture.join();
if (cache.getStatus().isTerminated()) {
log.tracef("Ignoring cache %s, it is already terminated.", cacheName);
return;
}
cache.stop();
}
}
@Override
public void stop() {
authorizer.checkPermission(getSubject(), AuthorizationPermission.LIFECYCLE);
internalStop();
}
private void internalStop() {
lifecycleLock.lock();
try {
while (status == ComponentStatus.STOPPING) {
lifecycleCondition.await();
}
if (status != ComponentStatus.RUNNING && status != ComponentStatus.FAILED) {
log.trace("Ignore call to stop as the cache manager is not running");
return;
}
// We can stop the manager
log.infof("Stopping cache manager %s on %s", configurationManager.getGlobalConfiguration().transport().nodeName(), getAddress());
updateStatus(ComponentStatus.STOPPING);
} catch (InterruptedException e) {
throw new CacheException("Interrupted waiting for the cache manager to stop");
} finally {
lifecycleLock.unlock();
}
try {
stopCaches();
globalComponentRegistry.getComponent(CacheManagerJmxRegistration.class).stop();
globalComponentRegistry.stop();
log.debugf("Stopped cache manager %s", configurationManager.getGlobalConfiguration().transport().nodeName());
} finally {
updateStatus(ComponentStatus.TERMINATED);
}
}
private void stopCaches() {
Set cachesToStop = new LinkedHashSet<>(this.caches.size());
// stop ordered caches first
try {
List ordered = cacheDependencyGraph.topologicalSort();
cachesToStop.addAll(ordered);
} catch (CyclicDependencyException e) {
CONTAINER.stopOrderIgnored();
}
// The caches map includes the default cache
cachesToStop.addAll(caches.keySet());
log.tracef("Cache stop order: %s", cachesToStop);
for (String cacheName : cachesToStop) {
try {
terminate(cacheName);
} catch (Throwable t) {
CONTAINER.componentFailedToStop(t);
}
}
}
@Override
public CompletionStage addListenerAsync(Object listener) {
authorizer.checkPermission(getSubject(), AuthorizationPermission.LISTEN);
CacheManagerNotifier notifier = globalComponentRegistry.getComponent(CacheManagerNotifier.class);
return notifier.addListenerAsync(listener);
}
@Override
public CompletionStage removeListenerAsync(Object listener) {
authorizer.checkPermission(getSubject(), AuthorizationPermission.LISTEN);
try {
CacheManagerNotifier notifier = globalComponentRegistry.getComponent(CacheManagerNotifier.class);
return notifier.removeListenerAsync(listener);
} catch (IllegalLifecycleStateException e) {
// Ignore the exception for backwards compatibility
return CompletableFutures.completedNull();
}
}
@Deprecated
@Override
public Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy