I have this infinispan.xml configuration:
<infinispan>
<cache-container default-cache="dist-sync">
<transport/>
<local-cache name="local">
<expiration lifespan="-1" max-idle="5000" />
</local-cache>
<invalidation-cache name="invalidation" mode="SYNC"/>
<replicated-cache name="repl-sync" mode="SYNC"/>
<distributed-cache name="dist-sync" mode="SYNC"/>
</cache-container>
</infinispan>
How can I instantiate DefaultCacheManager with the cache name configuration local instead of the default cache (dist-sync)
Something like this:
DefaultCacheManager cacheManager = new DefaultCacheManager("infinispan.xml");
Cache<K, V> cache = cacheManager.getCache("local");
More info in the documentation: https://infinispan.org/docs/stable/titles/configuring/configuring.html#cache_modes
Or javadoc: https://docs.jboss.org/infinispan/11.0/apidocs/org/infinispan/manager/DefaultCacheManager.html#getCache(java.lang.String)
As a side note, DefaultCacheManager.getCache() returns the cache with name defined in the default-cache attribute (<cache-container default-cache="dist-sync">)
Related
I have upgraded a Spring boot service to Infinispan 9.4.16.Final from 5.2.20.Final. The service has two XML files. I used the conversion script to convert them. Both have local-cache entries and no other types of caches. One was left with empty transport element by the conversion tool.
When we deploy and run these services, we often see this warning at startup:
org.infinispan.manager.EmbeddedCacheManagerStartupException: org.infinispan.commons.CacheException: Unable to invoke method public void org.infinispan.globalstate.impl.GlobalConfigurationManagerImpl.start() on object of type GlobalConfigurationManagerImpl
The above is the first warning/error we see. There is no stack trace. Why would it be calling GlobalConfigurationManagerImpl when we're only using local cache?
A few lines later in the log, then I see many The cache has been stopped and invocations are not allowed! errors. The last error we see is as follows. The service fails to start up successfully.
Caused by: org.infinispan.commons.CacheException: Initial state transfer timed out for cache org.infinispan.CONFIG on <server_name>
Why are these errors/warnings happening on startup? Is there a problem in the config files? I've searched online and have not found a solution.
~~More Info~~~
Here is one of the two XML config files:
<infinispan
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "urn:infinispan:config:9.4 http://www.infinispan.org/schemas/infinispan-config-9.4.xsd"
xmlns = "urn:infinispan:config:9.4">
<threads/>
<cache-container name = "TestCenterServiceCache">
<!-- The conversion tool added this empty "transport" element. It was not present in our old config file -->
<transport/>
<jmx domain = "org.infinispan.TestCenterServiceCache"/>
<local-cache name = "authorizedLocations">
<expiration lifespan = "3600000"/>
</local-cache>
<!--caching for 24 hours: 3,600,000 milliseconds/hr x 24 hours -->
<local-cache name = "proximitySearchConfiguration">
<expiration lifespan = "86400000"/>
</local-cache>
</cache-container>
</infinispan>
The above is instantiated via applicationContext.xml. The first warning (GlobalConfigurationManagerImpl.start()) is referencing these beans.
<bean id="infinispanCacheManager"
class="org.infinispan.spring.embedded.support.InfinispanEmbeddedCacheManagerFactoryBean"
p:configurationFileLocation="classpath:testCenterServices-cache-config.xml" />
<bean id="cacheManager"
class="org.infinispan.spring.embedded.provider.SpringEmbeddedCacheManager">
<constructor-arg ref="infinispanCacheManager" />
</bean>
Here is the second XML config file:
<?xml version="1.0" ?>
<infinispan
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "urn:infinispan:config:9.4 http://www.infinispan.org/schemas/infinispan-config-9.4.xsd"
xmlns = "urn:infinispan:config:9.4">
<threads />
<cache-container name="AtlasServicesCacheManager">
<local-cache name="allLocaleCache" />
<local-cache name="localeCacheByID" />
<local-cache name="countryByCode" />
<local-cache name="allActiveCountries" />
<local-cache name="allCountries" />
<local-cache name="allStatesForCountryCode" />
<local-cache name="allActiveStatesForCountryCode" />
<local-cache name="stateForCountryCodeStateCode" />
</cache-container>
</infinispan>
The above is instantiated via java code.
#Bean(name="atlasServicesCacheManager")
public CacheManager makeCacheManager() throws IOException {
return new SpringEmbeddedCacheManager(new DefaultCacheManager("atlas-cache-config.xml"));
}
I don't know if it's meaningful, but only after the upgrade, we log messages that include "JGroups", such as Unable to use any JGroups configuration mechanisms provided in properties {}. Using default JGroups configuration!.
The service instances are running on Windows Server 2012 R2 Standard (Windows 8).
To fix this, remove the empty <transport /> element for local caches.
Adding that empty element seems to be a defect in the config-converter. With the empty transport element in place, it seems that Infinispan is partially configured for cluster synchronization. For details on the underlying issue, see bug report: https://issues.redhat.com/browse/ISPN-11854.
I have a infinispan cache defined in wildfly stanadlone.xml as shown below.
<cache-container name="dummycache" default-cache="stringCache">
<local-cache name="stringCache">
<expiration lifespan="1800000"/>
</local-cache>
</cache-container>
This cache is initialized by the infinispan running embedded in wildfly itself. I could lookup and use the cache like below.
#Resource(lookup = "java:comp/env/infinispan/stringCache")
private Cache<String, String> stringCache;
Now I would like to add listeners to get notified about the cache expired events. But the question is how do I register this listener to the infinispan cache which is completely managed by widlfly container?
I find that we can addListeners() to cache object but at what moment ? Ideally I want it be initialized when the wildfly creates the cache itself. It looks the infinspan-standalone schema does not support listener element to set it in the cache configuration. So the only other way is to inject the cache in filter or similar and call addListener() then?
Is there any neat way to do this ?
To set the scene - I am running a web app on wildfly 10, which is run in standalone mode. The cache-container config in standalone.xml is:
<cache-container name="web" default-cache="passivation" module="org.wildfly.clustering.web.infinispan">
<local-cache name="passivation">
<locking isolation="REPEATABLE_READ"/>
<transaction locking="OPTIMISTIC" mode="BATCH"/>
<file-store passivation="true" purge="false"/>
</local-cache>
<local-cache name="persistent">
<locking isolation="REPEATABLE_READ"/>
<transaction locking="OPTIMISTIC" mode="BATCH"/>
<file-store passivation="false" purge="false"/>
</local-cache>
</cache-container>
Here are my changes step by step:
Dependency to infinispan
I have added
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>9.2.4.Final</version>
<scope>provided</scope>
</dependency>
web.xml
Add a resource (in my case I am trying to get the resource for the cache container web)
<resource-ref>
<res-ref-name>infinispan/cacheContainer</res-ref-name>
<lookup-name>java:jboss/infinispan/container/web</lookup-name>
</resource-ref>
I also added a servlet context listener because I want to add the cache listener on contextInitialized
<listener>
<listener-class>some.package.ContextListenerCacheAddListener</listener-class>
</listener>
jboss-deployment-structure.xml
I have added this to my WEB-INF directory of the war
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="org.infinispan" />
</dependencies>
</deployment>
</jboss-deployment-structure>
Turns out that if you skip this - infinispan classes will be loaded by different class loaders (as far as I can understand)
Java code
Injecting the resource and using it to add listeners to caches
public class ContextListenerCacheAddListener implements ServletContextListener {
#Resource(name = "infinispan/cacheContainer")
CacheContainer cacheContainer;
static Logger logger = Logger.getLogger(ContextListenerCacheAddListener.class);
#Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
cacheContainer.getCache("myapp.war").addListener(new MyListener("myapp.war"));
cacheContainer.getCache("passivation").addListener(new MyListener("passivation"));
cacheContainer.getCache("persistent").addListener(new MyListener("persistent"));
}
#Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
#Listener
public static class MyListener{
private String name;
public MyListener(String name) {
this.name = name;
}
#CacheEntryCreated
public void onCreate(CacheEntryEvent event){
logger.warn(name +" created entry "+event.getKey() );
}
#CacheEntryModified
public void onModify(CacheEntryEvent event){
logger.warn(name +" modified entry "+event.getKey());
}
#CacheEntryVisited
public void onVisited(CacheEntryEvent event){
logger.warn(name +" visited entry "+event.getKey());
}
#TransactionRegistered
public void onTransactionRegistered(TransactionRegisteredEvent event){
logger.warn(name +" registered transaction entry ");
}
#TransactionCompleted
public void onTransactionCompleted(TransactionCompletedEvent event){
logger.warn(name +" completed transaction entry ");
}
}
}
My changes are just for testing and debugging purposes to solve a different problem than yours.
in my application im using infinispan to manage my data, and now i want to save my data in a flat file and it doesn't work.
here is my infinispan conf file :
http://infinispan.org/schemas/infinispan-config-9.3.xsd"
xmlns="urn:infinispan:config:9.3">
<loaders passivation="false" shared="false" preload="true">
<loader class="org.infinispan.loaders.file.FileCacheStore" fetchPersistentState="true"
ignoreModifications="false" purgeOnStartup="false">
<properties>
<property name="location" value="C:\infinispan-4.0.0.FINAL\store"/>
</properties>
</loader>
</loaders>
</namedCache>
when i run my class , i encounter this error :
org.infinispan.commons.CacheConfigurationException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[4,35]
Message: Unexpected element '{urn:infinispan:config:9.3}namedCache' encountered
Have you any idea please ?
You are using an old configuration format which we have abandoned since Infinispan 7.0. For 9.3 you should use something like the following:
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:9.3 http://www.infinispan.org/schemas/infinispan-config-9.3.xsd"
xmlns="urn:infinispan:config:9.3">
<cache-container default-cache="local">
<local-cache name="local">
<persistence>
<file-store path="path" shared="false" purge="false"/>
</persistence>
</local-cache>
</cache-container>
</infinispan>
I have below configuration in standalone.xml. The custom cache named data-local-query defines the max-entries="-1" which is valid as per documentation to have unlimited entries.
But it seems there is a conflict with default local cache and hence WildFly has startup error.
Why cannot I define max-entries="-1" to override default one ?
error
Eviction maxEntries value cannot be less than or equal to zero if eviction is enabled
standalone.xml
<cache-container name="hibernate" default-cache="local-query" module="org.hibernate.infinispan">
<local-cache name="entity">
<transaction mode="NON_XA"/>
<eviction strategy="LRU" max-entries="10000"/>
<expiration max-idle="100000"/>
</local-cache>
<local-cache name="local-query">
<eviction strategy="LRU" max-entries="10000"/>
<expiration max-idle="100000"/>
</local-cache>
<local-cache name="data-local-query">
<eviction strategy="LRU" max-entries="-1"/>
<expiration max-idle="14400" interval="20000" lifespan="86400000"/>
</local-cache>
<local-cache name="timestamps"/>
</cache-container>
log error
2017-04-19 18:00:27,215 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 62) MSC000001: Failed to start service jboss.infinispan.hibernate.data-local-query.config: org.jboss.msc.service.StartException in service jboss.infinispan.hibernate.data-local-query.config: org.infinispan.commons.CacheConfigurationException: Eviction maxEntries value cannot be less than or equal to zero if eviction is enabled
Caused by: org.infinispan.commons.CacheConfigurationException: Eviction maxEntries value cannot be less than or equal to zero if eviction is enabled
at org.infinispan.configuration.cache.EvictionConfigurationBuilder.validate(EvictionConfigurationBuilder.java:108)
at org.infinispan.configuration.cache.ConfigurationBuilder.validate(ConfigurationBuilder.java:203)
at org.infinispan.configuration.cache.ConfigurationBuilder.build(ConfigurationBuilder.java:246)
at org.infinispan.configuration.cache.ConfigurationBuilder.build(ConfigurationBuilder.java:236)
at org.wildfly.clustering.infinispan.spi.service.ConfigurationBuilder.start(ConfigurationBuilder.java:79)
at org.wildfly.clustering.service.AsynchronousServiceBuilder$1.run(AsynchronousServiceBuilder.java:102)
I have created a two node cluster running Infinispan cache in replication mode. I see the cache comes up fine. and both the nodes connect to each other. I am here using TreeCache Api to convert the Map based structure to tree based as below:
private static Cache<Object, Object> Cache1;
private static TreeCache<Object, Object> Cache;
Cache1 = new DefaultCacheManager("infinispan.xml").getCache();
Cache = new TreeCacheFactory().createTreeCache(Cache1);
If i call Cache.put , I dont get any error but the entry does not get saved to the cache. I confirmed it by getting the data again which returns NULL.
Cache.put(fqn,key, value);
if(Cache.get(fqn, key) == null)
{
System.out.println("Entry is not saved");
}
Below is the config file,
<?xml version="1.0" encoding="UTF-8"?>
<infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:5.3 http://www.infinispan.org/schemas/infinispan-config-5.3.xsd"
xmlns="urn:infinispan:config:5.3">
<global>
<globalJmxStatistics enabled = "true" allowDuplicateDomains = "true"/>
<transport clusterName = "test_cluster">
<properties>
<property name="configurationFile" value="tcp.xml" />
</properties>
</transport>
</global>
<default>
<clustering mode="replication">
<async asyncMarshalling="true" useReplQueue="true" replQueueInterval="10" replQueueMaxElements="100" />
<stateTransfer timeout="2000000"/>
</clustering>
<invocationBatching enabled = "true"/>
<locking isolationLevel = "REPEATABLE_READ"
writeSkewCheck = "false"
concurrencyLevel = "1000"/>
<jmxStatistics enabled="true"/>
</default>
</infinispan>
Is it that I am missing to initialize any thing?