Skip to content

Commit

Permalink
ISPN-3926 Allow internal registry caches to be marked as persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
tristantarrant committed Sep 14, 2015
1 parent 456dd14 commit db66ab8
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
Expand Up @@ -18,7 +18,8 @@
public interface InternalCacheRegistry {
enum Flag {
EXCLUSIVE, // means that the cache must be declared only once
USER, // means that this cache is visible to users
USER, // means that this cache is visible to users
PERSISTENT, // means the cache should be made persistent across restarts if global state persistence is enabled
}

void registerInternalCache(String name, Configuration configuration);
Expand Down
Expand Up @@ -6,6 +6,7 @@

import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfiguration;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.registry.InternalCacheRegistry;
Expand Down Expand Up @@ -43,7 +44,11 @@ public void registerInternalCache(String name, Configuration configuration, Enum
}
ConfigurationBuilder builder = new ConfigurationBuilder().read(configuration);
builder.jmxStatistics().disable(); // Internal caches must not be included in stats counts
SecurityActions.defineConfiguration(cacheManager, name, configuration);
GlobalConfiguration globalConfiguration = cacheManager.getCacheManagerConfiguration();
if (flags.contains(Flag.PERSISTENT) && globalConfiguration.statePersistence().enabled()) {
builder.persistence().addSingleFileStore().location(globalConfiguration.statePersistence().location()).purgeOnStartup(false).preload(true);
}
SecurityActions.defineConfiguration(cacheManager, name, builder.build());
internalCaches.add(name);
if (!flags.contains(Flag.USER)) {
privateCaches.add(name);
Expand Down
Expand Up @@ -81,7 +81,7 @@ protected void init(EmbeddedCacheManager cacheManager, InternalCacheRegistry int
this.cacheManager = cacheManager;
internalCacheRegistry.registerInternalCache(PROTOBUF_METADATA_CACHE_NAME,
getProtobufMetadataCacheConfig().build(),
EnumSet.of(InternalCacheRegistry.Flag.USER));
EnumSet.of(InternalCacheRegistry.Flag.USER, InternalCacheRegistry.Flag.PERSISTENT));
}

/**
Expand Down
@@ -0,0 +1,46 @@
package org.infinispan.query.remote.impl;

import static org.testng.AssertJUnit.assertTrue;
import org.infinispan.Cache;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.test.AbstractInfinispanTest;
import org.infinispan.test.CacheManagerCallable;
import org.infinispan.test.TestingUtil;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.testng.annotations.Test;
import org.infinispan.query.remote.client.ProtobufMetadataManagerConstants;

@Test(groups = "functional", testName = "query.remote.impl.ProtobufMetadataCachePreserveStateAcrossRestarts")
public class ProtobufMetadataCachePreserveStateAcrossRestarts extends AbstractInfinispanTest {

protected EmbeddedCacheManager createCacheManager(String persistentStateLocation) throws Exception {
GlobalConfigurationBuilder global = new GlobalConfigurationBuilder();
global.statePersistence().enable().location(persistentStateLocation);
EmbeddedCacheManager cacheManager = TestCacheManagerFactory.createCacheManager(global, new ConfigurationBuilder());
cacheManager.getCache();
return cacheManager;
}

public void testStatePreserved() throws Exception {
String persistentStateLocation = TestingUtil.tmpDirectory(this.getClass());
TestingUtil.recursiveFileRemove(persistentStateLocation);

TestingUtil.withCacheManager(new CacheManagerCallable(createCacheManager(persistentStateLocation)) {
@Override
public void call() throws Exception {
Cache<String, String> protoBufMetadaCache = cm.getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME);
protoBufMetadaCache.put("test.proto", "package X;");
}
});

TestingUtil.withCacheManager(new CacheManagerCallable(createCacheManager(persistentStateLocation)) {
@Override
public void call() throws Exception {
Cache<String, String> protoBufMetadaCache = cm.getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME);
assertTrue(protoBufMetadaCache.containsKey("test.proto"));
}
});
}
}
Expand Up @@ -65,7 +65,7 @@ public void initialize(final EmbeddedCacheManager cacheManager, InternalCacheReg
this.cacheManager = cacheManager;
ClassLoader classLoader = cacheManager.getCacheManagerConfiguration().classLoader();
this.scriptEngineManager = new ScriptEngineManager(classLoader);
internalCacheRegistry.registerInternalCache(SCRIPT_CACHE, getScriptCacheConfiguration().build(), EnumSet.of(InternalCacheRegistry.Flag.USER));
internalCacheRegistry.registerInternalCache(SCRIPT_CACHE, getScriptCacheConfiguration().build(), EnumSet.of(InternalCacheRegistry.Flag.USER, InternalCacheRegistry.Flag.PERSISTENT));
}

Cache<String, String> getScriptCache() {
Expand Down
@@ -0,0 +1,52 @@
package org.infinispan.scripting;

import static org.testng.AssertJUnit.assertTrue;
import java.io.InputStream;

import org.infinispan.Cache;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.scripting.impl.ScriptingManagerImpl;
import org.infinispan.test.AbstractInfinispanTest;
import org.infinispan.test.CacheManagerCallable;
import org.infinispan.test.TestingUtil;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.testng.annotations.Test;


@Test(groups = "functional", testName = "scripting.ScriptCachePreserveStateAcrossRestarts")
public class ScriptCachePreserveStateAcrossRestarts extends AbstractInfinispanTest {

protected EmbeddedCacheManager createCacheManager(String persistentStateLocation) throws Exception {
GlobalConfigurationBuilder global = new GlobalConfigurationBuilder();
global.statePersistence().enable().location(persistentStateLocation);
EmbeddedCacheManager cacheManager = TestCacheManagerFactory.createCacheManager(global, new ConfigurationBuilder());
cacheManager.getCache();
return cacheManager;
}

public void testStatePreserved() throws Exception {
String persistentStateLocation = TestingUtil.tmpDirectory(this.getClass());
TestingUtil.recursiveFileRemove(persistentStateLocation);

TestingUtil.withCacheManager(new CacheManagerCallable(createCacheManager(persistentStateLocation)) {
@Override
public void call() throws Exception {
Cache<String, String> scriptCache = cm.getCache(ScriptingManagerImpl.SCRIPT_CACHE);
try (InputStream is = this.getClass().getResourceAsStream("/test.js")) {
String script = TestingUtil.loadFileAsString(is);
scriptCache.put("test.js", script);
}
}
});

TestingUtil.withCacheManager(new CacheManagerCallable(createCacheManager(persistentStateLocation)) {
@Override
public void call() throws Exception {
Cache<String, String> scriptCache = cm.getCache(ScriptingManagerImpl.SCRIPT_CACHE);
assertTrue(scriptCache.containsKey("test.js"));
}
});
}
}

0 comments on commit db66ab8

Please sign in to comment.