Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Bumps Infinispan to 8.2.3 (iss #12) #13

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</distributionManagement>

<properties>
<infinispan.core.schema.version>8.0</infinispan.core.schema.version>
<infinispan.core.schema.version>8.2</infinispan.core.schema.version>
<version.testng>6.8.8</version.testng>
<jboss.releases.repo.url>https://repository.jboss.org/nexus/service/local/staging/deploy/maven2/</jboss.releases.repo.url>
<jboss.snapshots.repo.url>https://repository.jboss.org/nexus/content/repositories/snapshots/</jboss.snapshots.repo.url>
Expand Down Expand Up @@ -209,7 +209,7 @@
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>8.0.0.Final</version>
<version>8.2.3.Final</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -256,7 +256,7 @@
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>8.0.0.Final</version>
<version>8.2.3.Final</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Expand Down
76 changes: 49 additions & 27 deletions src/main/java/org/infinispan/persistence/redis/RedisStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

Expand All @@ -23,6 +21,22 @@
@ConfiguredBy(RedisStoreConfiguration.class)
final public class RedisStore implements AdvancedLoadWriteStore
{
/**
* The instances of this class, keyed by Infinispan cache name.
*/
private static Map<String, RedisStore> instances = new Hashtable<>();


/**
* Returns the {@link #init initialised} instances of this class.
*
* @return The instances of this class as an unmodifiable map, keyed by
* Infinispan cache name.
*/
public static Map<String, RedisStore> getInstances() {
return Collections.unmodifiableMap(instances);
}

private static final Log log = LogFactory.getLog(RedisStore.class, Log.class);

private InitializationContext ctx = null;
Expand All @@ -37,8 +51,13 @@ final public class RedisStore implements AdvancedLoadWriteStore
@Override
public void init(InitializationContext ctx)
{
RedisStore.log.info("Redis cache store initialising");
RedisStore.log.infof("Initialising Redis store for cache '%s'", ctx.getCache().getName());
this.ctx = ctx;

// Register store
if (ctx.getCache().getName() != null) {
RedisStore.instances.put(ctx.getCache().getName(), this);
}
}

/**
Expand All @@ -47,13 +66,13 @@ public void init(InitializationContext ctx)
@Override
public void start()
{
RedisStore.log.info("Redis cache store starting");
RedisStore.log.infof("Starting Redis store for cache '%s'", ctx.getCache().getName());

try {
this.connectionPool = RedisConnectionPoolFactory.factory(this.ctx.getConfiguration(), this.ctx.getMarshaller());
}
catch(Exception ex) {
RedisStore.log.error("Failed to initialise the redis store", ex);
RedisStore.log.errorf(ex, "Failed to initialise the Redis store for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
}
Expand All @@ -64,11 +83,16 @@ public void start()
@Override
public void stop()
{
RedisStore.log.info("Redis cache store stopping");
RedisStore.log.infof("Stopping Redis store for cache '%s'", ctx.getCache().getName());

if (null != this.connectionPool) {
this.connectionPool.shutdown();
}

// Unregister store
if (ctx.getCache().getName() != null) {
RedisStore.instances.remove(ctx.getCache().getName());
}
}

/**
Expand Down Expand Up @@ -101,7 +125,7 @@ public void process(
boolean fetchMetadata
)
{
RedisStore.log.debug("Iterating Redis store entries");
RedisStore.log.debugf("Iterating Redis store entries for cache '%s'", ctx.getCache().getName());

final InitializationContext ctx = this.ctx;
final TaskContext taskContext = new TaskContextImpl();
Expand Down Expand Up @@ -137,15 +161,15 @@ public void run() {
}
}
catch (Exception ex) {
RedisStore.log.error("Failed to process the redis store key", ex);
RedisStore.log.errorf(ex, "Failed to process a Redis store key for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
}
});
}
}
catch(Exception ex) {
RedisStore.log.error("Failed to process the redis store keys", ex);
RedisStore.log.errorf(ex, "Failed to process the Redis store keys for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
finally {
Expand Down Expand Up @@ -175,7 +199,7 @@ public void purge(Executor executor, final PurgeListener purgeListener)
@Override
public int size()
{
RedisStore.log.debug("Calculating Redis store size");
RedisStore.log.debugf("Fetching the Redis store size for cache '%s'", ctx.getCache().getName());
RedisConnection connection = null;

try {
Expand All @@ -186,11 +210,9 @@ public int size()
// If the number of elements in redis is more than the int max size,
// log the anomaly and return the int max size
if (dbSize > Integer.MAX_VALUE) {
RedisStore.log.info(
String.format("Redis store is holding more elements than we can count! " +
"Total number of elements found %d. Limited to returning count as %d",
dbSize, Integer.MAX_VALUE
)
RedisStore.log.warnf("The Redis store for cache '%s' is holding more entries than we can count! " +
"Total number of entries found %d. Limited to returning count as %d",
ctx.getCache().getName(), dbSize, Integer.MAX_VALUE
);

return Integer.MAX_VALUE;
Expand All @@ -200,7 +222,7 @@ public int size()
}
}
catch(Exception ex) {
RedisStore.log.error("Failed to fetch element count from the redis store", ex);
RedisStore.log.errorf(ex, "Failed to fetch the entry count for the Redis store for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
finally {
Expand All @@ -218,15 +240,15 @@ public int size()
@Override
public void clear()
{
RedisStore.log.debug("Clearing Redis store");
RedisStore.log.debugf("Clearing the Redis store for cache '%s'", ctx.getCache().getName());
RedisConnection connection = null;

try {
connection = this.connectionPool.getConnection();
connection.flushDb();
}
catch(Exception ex) {
RedisStore.log.error("Failed to clear all elements in the redis store", ex);
RedisStore.log.errorf(ex, "Failed to clear the Redis store for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
finally {
Expand All @@ -247,7 +269,7 @@ public void clear()
@Override
public MarshalledEntry load(Object key)
{
RedisStore.log.debug("Loading entry from Redis store");
RedisStore.log.debugf("Loading entry from the Redis store for cache '%s'", ctx.getCache().getName());
RedisConnection connection = null;

try {
Expand All @@ -270,7 +292,7 @@ public MarshalledEntry load(Object key)
return this.ctx.getMarshalledEntryFactory().newMarshalledEntry(key, valueBuf, metadataBuf);
}
catch(Exception ex) {
RedisStore.log.error("Failed to load element from the redis store", ex);
RedisStore.log.errorf(ex, "Failed to load entry from the Redis store for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
finally {
Expand All @@ -289,7 +311,7 @@ public MarshalledEntry load(Object key)
@Override
public void write(MarshalledEntry marshalledEntry)
{
RedisStore.log.debug("Writing entry to Redis store");
RedisStore.log.debugf("Writing entry to the Redis store for cache '%s'", ctx.getCache().getName());
RedisConnection connection = null;

try {
Expand Down Expand Up @@ -317,7 +339,7 @@ public void write(MarshalledEntry marshalledEntry)
}
}
catch(Exception ex) {
RedisStore.log.error("Failed to write element to the redis store", ex);
RedisStore.log.errorf(ex,"Failed to write entry to the Redis store for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
finally {
Expand All @@ -336,15 +358,15 @@ public void write(MarshalledEntry marshalledEntry)
@Override
public boolean delete(Object key)
{
RedisStore.log.debug("Deleting entry from Redis store");
RedisStore.log.debugf("Deleting entry from Redis store for cache '%s'", ctx.getCache().getName());
RedisConnection connection = null;

try {
connection = this.connectionPool.getConnection();
return connection.delete(key);
}
catch(Exception ex) {
RedisStore.log.error("Failed to delete element from the redis store", ex);
RedisStore.log.errorf(ex,"Failed to delete entry from the Redis store for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
finally {
Expand All @@ -363,15 +385,15 @@ public boolean delete(Object key)
@Override
public boolean contains(Object key)
{
RedisStore.log.debug("Checking store for Redis entry");
RedisStore.log.debugf("Checking key in Redis store for cache '%s'", ctx.getCache().getName());
RedisConnection connection = null;

try {
connection = this.connectionPool.getConnection();
return connection.exists(key);
}
catch(Exception ex) {
RedisStore.log.error("Failed to discover if element is in the redis store", ex);
RedisStore.log.errorf(ex,"Failed to check key in Redis store for cache '%s'", ctx.getCache().getName());
throw new PersistenceException(ex);
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public RedisServerConnectionPool(RedisStoreConfiguration configuration, RedisMar
server = servers.get(0);

if (servers.size() > 1) {
RedisServerConnectionPool.log.warn(String.format("Multiple redis servers defined. Using the first only (%s:%d)",
server.host(), server.port()));
RedisServerConnectionPool.log.warnf("Multiple redis servers defined. Using the first only (%s:%d)",
server.host(), server.port());
}

ConnectionPoolConfiguration connectionPoolConfiguration = configuration.connectionPool();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ private void parseRedisStoreAttributes(XMLExtendedStreamReader reader, RedisStor
builder.maxRedirections(Integer.parseInt(value));
break;
}

default: {
Parser.parseStoreAttribute(reader, i, builder);
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,44 @@ public void testTwoCachesSameCacheStore()
Assert.assertEquals("val2", this.unwrap(second.get("key2")));
Assert.assertNull(first.get("key2"));
}

public void testGetRedisStoreInstance()
{
ConfigurationBuilder cb1 = new ConfigurationBuilder();
cb1.read(this.cacheManager.getDefaultCacheConfiguration());
this.createCacheStoreConfig(cb1.persistence(), false, 0);
Configuration c1 = cb1.build();

ConfigurationBuilder cb2 = new ConfigurationBuilder();
cb2.read(this.cacheManager.getDefaultCacheConfiguration());
this.createCacheStoreConfig(cb2.persistence(), false, 1);
Configuration c2 = cb2.build();

int initialNumRedisStoreInstances = RedisStore.getInstances().size();

this.cacheManager.defineConfiguration("testTwoCachesSameCacheStore-1", c1);
this.cacheManager.defineConfiguration("testTwoCachesSameCacheStore-2", c2);
Cache first = this.cacheManager.getCache("testTwoCachesSameCacheStore-1");
Cache second = this.cacheManager.getCache("testTwoCachesSameCacheStore-2");

first.start();
second.start();

RedisStore firstRedisStore = RedisStore.getInstances().get("testTwoCachesSameCacheStore-1");
RedisStore secondRedisStore = RedisStore.getInstances().get("testTwoCachesSameCacheStore-2");
Assert.assertEquals(initialNumRedisStoreInstances + 2, RedisStore.getInstances().size());

first.put("key1", "val2");
second.put("key2", "val2");

Assert.assertTrue(firstRedisStore.contains("key1"));
Assert.assertTrue(secondRedisStore.contains("key2"));

first.stop();
second.stop();

Assert.assertNull(RedisStore.getInstances().get("testTwoCachesSameCacheStore-1"));
Assert.assertNull(RedisStore.getInstances().get("testTwoCachesSameCacheStore-2"));
Assert.assertEquals(initialNumRedisStoreInstances, RedisStore.getInstances().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ public void testRedisCacheStore() throws Exception
assert store.servers().size() == 2;
}

public void testInheritedCacheStoreAttributes() throws Exception
{
String config = InfinispanStartTag.LATEST +
"<cache-container default-cache=\"default\">" +
" <local-cache name=\"default\">\n" +
" <persistence>\n" +
" <redis-store xmlns=\"urn:infinispan:config:store:redis:"+ InfinispanStartTag.LATEST.majorMinor()+ "\"" +
" shared=\"true\" preload=\"true\" >\n" +
" <redis-server host=\"one\" />\n" +
" </redis-store>\n" +
" </persistence>\n" +
" </local-cache>\n" +
"</cache-container>" +
TestingUtil.INFINISPAN_END_TAG;

RedisStoreConfiguration store = (RedisStoreConfiguration) buildCacheManagerWithCacheStore(config);
assert store.shared();
assert store.preload();
assert store.servers().size() == 1;
}

private StoreConfiguration buildCacheManagerWithCacheStore(final String config)
throws IOException
{
Expand Down