Skip to content

Commit

Permalink
Merge pull request #27341 from karesti/create-caches-infinispan-prope…
Browse files Browse the repository at this point in the history
…rties

Create caches in Infinispan using the application.properties
  • Loading branch information
geoand authored Sep 5, 2022
2 parents dc9e119 + 07d341c commit 154a4a2
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;
Expand Down Expand Up @@ -142,6 +143,16 @@ public static void handleProtoStreamRequirements(Properties properties) {
*/
private static String getContents(String fileName) {
InputStream stream = InfinispanClientProducer.class.getResourceAsStream(fileName);
return getContents(stream);
}

/**
* Reads all the contents of the input stream as a single string using default charset
*
* @param stream to read contents of
* @return string containing the contents of the file
*/
private static String getContents(InputStream stream) {
try (Scanner scanner = new Scanner(stream, "UTF-8")) {
return scanner.useDelimiter("\\A").next();
}
Expand All @@ -153,6 +164,7 @@ private static String getContents(String fileName) {
*
* @param properties the properties that was static constructed
* @return the configuration builder based on the provided properties
* @throws RuntimeException if the cache configuration file is not present in the resources folder
*/
private ConfigurationBuilder builderFromProperties(Properties properties) {
// If you are changing this method, you will most likely have to change replaceProperties as well
Expand Down Expand Up @@ -214,6 +226,32 @@ private ConfigurationBuilder builderFromProperties(Properties properties) {

builder.withProperties(properties);

for (Map.Entry<String, InfinispanClientRuntimeConfig.RemoteCacheConfig> cache : infinispanClientRuntimeConfig.cache
.entrySet()) {
String cacheName = cache.getKey();
InfinispanClientRuntimeConfig.RemoteCacheConfig remoteCacheConfig = cache.getValue();
if (remoteCacheConfig.configurationUri.isPresent()) {
URL configFile = InfinispanClientProducer.class.getClassLoader()
.getResource(remoteCacheConfig.configurationUri.get());
try {
builder.remoteCache(cacheName).configurationURI(configFile.toURI());
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (remoteCacheConfig.configuration.isPresent()) {
builder.remoteCache(cacheName).configuration(remoteCacheConfig.configuration.get());
}
if (remoteCacheConfig.nearCacheMaxEntries.isPresent()) {
builder.remoteCache(cacheName).nearCacheMaxEntries(remoteCacheConfig.nearCacheMaxEntries.get());
}
if (remoteCacheConfig.nearCacheMode.isPresent()) {
builder.remoteCache(cacheName).nearCacheMode(remoteCacheConfig.nearCacheMode.get());
}
if (remoteCacheConfig.nearCacheUseBloomFilter.isPresent()) {
builder.remoteCache(cacheName).nearCacheUseBloomFilter(remoteCacheConfig.nearCacheUseBloomFilter.get());
}
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package io.quarkus.infinispan.client.runtime;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import javax.net.ssl.SSLContext;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;

import org.infinispan.client.hotrod.configuration.NearCacheMode;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
Expand Down Expand Up @@ -128,6 +133,65 @@ public class InfinispanClientRuntimeConfig {
@ConfigItem
Optional<String> trustStoreType;

/**
* Configures caches from the client with the provided configuration.
*/
@ConfigItem
Map<String, RemoteCacheConfig> cache = new HashMap<>();

@ConfigGroup
public static class RemoteCacheConfig {

// @formatter:off
/**
* Cache configuration in inlined XML to create the cache on first access.
* Will be ignored if the configuration-uri is provided for the same cache name.
* An example of the user defined property:
* quarkus.infinispan-client.cache.bookscache.configuration=<distributed-cache><encoding media-type="application/x-protostream"/></distributed-cache>
*/
// @formatter:on
@ConfigItem
public Optional<String> configuration;

// @formatter:off
/**
* Cache configuration file in XML whose path will be converted to URI to create the cache on first access.
* An example of the user defined property. cacheConfig.xml file is located in the 'resources' folder:
* quarkus.infinispan-client.cache.bookscache.configuration-uri=cacheConfig.xml
*/
// @formatter:on
@ConfigItem
public Optional<String> configurationUri;

/**
* The maximum number of entries to keep locally for the specified cache.
*/
@ConfigItem
public Optional<Integer> nearCacheMaxEntries;

// @formatter:off
/**
* Sets near cache mode used by the Infinispan Client
* Available values:
* * `DISABLED` - Means that near caching is disabled. This is the default value.
* * `INVALIDATED` - Means is near caching is invalidated, so when entries are updated or removed server-side,
* invalidation messages will be sent to clients to remove them from the near cache.
*/
// @formatter:on
@ConfigItem
public Optional<NearCacheMode> nearCacheMode;

// @formatter:off
/**
* Enables bloom filter for near caching.
* Bloom filters optimize performance for write operations by reducing the total number of
* invalidation messages.
*/
// @formatter:on
@ConfigItem
public Optional<Boolean> nearCacheUseBloomFilter;
}

@Override
public String toString() {
return "InfinispanClientRuntimeConfig{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.infinispan.client.hotrod.event.ClientCacheEntryRemovedEvent;
import org.infinispan.client.hotrod.logging.Log;
import org.infinispan.client.hotrod.logging.LogFactory;
import org.infinispan.commons.configuration.XMLStringConfiguration;
import org.infinispan.query.api.continuous.ContinuousQuery;
import org.infinispan.query.api.continuous.ContinuousQueryListener;
import org.infinispan.query.dsl.Query;
Expand All @@ -49,16 +48,9 @@ public class CacheSetup {

private CountDownLatch waitUntilStarted = new CountDownLatch(1);

private static final String CACHE_CONFIG = "<distributed-cache name=\"%s\">"
+ " <encoding media-type=\"application/x-protostream\"/>"
+ "</distributed-cache>";

void onStart(@Observes StartupEvent ev) {
RemoteCache<String, Book> defaultCache = cacheManager.administration().getOrCreateCache(DEFAULT_CACHE,
new XMLStringConfiguration(String.format(CACHE_CONFIG, DEFAULT_CACHE)));
RemoteCache<String, Magazine> magazineCache = cacheManager.administration().getOrCreateCache(MAGAZINE_CACHE,
new XMLStringConfiguration(String.format(CACHE_CONFIG, MAGAZINE_CACHE)));

RemoteCache<String, Book> defaultCache = cacheManager.getCache(DEFAULT_CACHE);
RemoteCache<String, Magazine> magazineCache = cacheManager.getCache(MAGAZINE_CACHE);
defaultCache.addClientListener(new EventPrintListener());

ContinuousQuery<String, Book> continuousQuery = Search.getContinuousQuery(defaultCache);
Expand Down Expand Up @@ -137,4 +129,4 @@ public void handleRemovedEvent(ClientCacheEntryRemovedEvent e) {
log.warn("Someone has removed an entry: " + e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public class TestServlet {
@Remote(CacheSetup.DEFAULT_CACHE)
RemoteCache<String, Book> cache;

@Inject
@Remote("booksOld")
RemoteCache<String, Book> boolsOld;

@Inject
@Remote(CacheSetup.MAGAZINE_CACHE)
RemoteCache<String, Magazine> magazineCache;
Expand Down Expand Up @@ -230,7 +226,7 @@ public String magazineQuery(@PathParam("id") String name) {
cacheSetup.ensureStarted();
QueryFactory queryFactory = Search.getQueryFactory(magazineCache);
Query query = queryFactory.create("from magazine_sample.Magazine m where m.name like '%" + name + "%'");
List<Magazine> list = query.list();
List<Magazine> list = query.execute().list();
if (list.isEmpty()) {
return "No one found for " + name;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
quarkus.infinispan-client.cache.default.configuration-uri=cacheConfig.xml
quarkus.infinispan-client.cache.magazine.configuration=<distributed-cache><encoding media-type="application/x-protostream"/></distributed-cache>
quarkus.infinispan-client.cache.default.near-cache-mode=INVALIDATED
quarkus.infinispan-client.cache.default.near-cache-max-entries=2
quarkus.infinispan-client.cache.default.near-cache-use-bloom-filter=false
quarkus.infinispan-client.cache.magazine.near-cache-mode=INVALIDATED
quarkus.infinispan-client.cache.magazine.near-cache-max-entries=2
quarkus.infinispan-client.cache.magazine.near-cache-use-bloom-filter=false
quarkus.native.resources.includes=cacheConfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<distributed-cache>
<encoding media-type="application/x-protostream"/>
</distributed-cache>
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,42 @@ public class InfinispanClientFunctionalityTest {

@Test
public void testGetAllKeys() {
System.out.println("Running getAllKeys test");
RestAssured.when().get("/test").then().body(is("[book1, book2]"));
}

@Test
public void testQuery() {
System.out.println("Running query test");
RestAssured.when().get("/test/query/So").then().body(is("[Son Martin]"));
RestAssured.when().get("/test/query/org").then().body(is("[George Martin]"));
RestAssured.when().get("/test/query/o").then().body(is("[George Martin,Son Martin]"));
}

@Test
public void testIckleQuery() {
System.out.println("Running ickleQuery test");
RestAssured.when().get("/test/icklequery/So").then().body(is("[Son Martin]"));
RestAssured.when().get("/test/icklequery/org").then().body(is("[George Martin]"));
RestAssured.when().get("/test/icklequery/o").then().body(is("[George Martin,Son Martin]"));
}

@Test
public void testCounterIncrement() {
System.out.println("Running counterIncrement test");
String initialValue = RestAssured.when().get("test/incr/somevalue").body().print();
String nextValue = RestAssured.when().get("test/incr/somevalue").body().print();
assertEquals(Integer.parseInt(initialValue) + 1, Integer.parseInt(nextValue));
}

@Test
public void testCQ() {
System.out.println("Running CQ test");
RestAssured.when().get("/test/cq").then().body(is("2023"));
}

@Test
public void testNearCacheInvalidation() {
System.out.println("Running nearCacheInvalidation test");
RestAssured.when().get("/test/nearcache").then().body(is("worked"));
}

@Test
public void testQueryWithCustomMarshaller() {
System.out.println("Running query with custom marshaller test");
RestAssured.when().get("/test/magazinequery/IM").then().body(is("[TIME:1923-03,TIME:1997-04]"));
}
}

0 comments on commit 154a4a2

Please sign in to comment.