Commit 8f1c7a06 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Caches defined by plugins can now be loaded/unloaded/shared between cluster nodes.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@9505 b35dd754-fafc-0310-a699-88a17e54d16e
parent 2669e450
...@@ -11,15 +11,15 @@ ...@@ -11,15 +11,15 @@
package org.jivesoftware.openfire.container; package org.jivesoftware.openfire.container;
import org.dom4j.io.SAXReader;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.HashMap;
/** /**
* A helper class to read cache configuration data for a plugin and register the defined caches with * A helper class to read cache configuration data for a plugin and register the defined caches with
...@@ -82,12 +82,12 @@ public class PluginCacheConfigurator { ...@@ -82,12 +82,12 @@ public class PluginCacheConfigurator {
Map<String, String> initParams = readInitParams(configData); Map<String, String> initParams = readInitParams(configData);
CacheInfo info = new CacheInfo(cacheName, CacheInfo.Type.valueof(schemeName), initParams); CacheInfo info = new CacheInfo(cacheName, CacheInfo.Type.valueof(schemeName), initParams);
PluginCacheRegistry.registerCache(pluginName, info); PluginCacheRegistry.getInstance().registerCache(pluginName, info);
} }
private Map<String, String> readInitParams(Node configData) { private Map<String, String> readInitParams(Node configData) {
Map<String, String> paramMap = new HashMap<String, String>(); Map<String, String> paramMap = new HashMap<String, String>();
List<Node> params = configData.selectNodes("//init-param"); List<Node> params = configData.selectNodes("init-params/init-param");
for (Node param : params) { for (Node param : params) {
String paramName = param.selectSingleNode("param-name").getStringValue(); String paramName = param.selectSingleNode("param-name").getStringValue();
String paramValue = param.selectSingleNode("param-value").getStringValue(); String paramValue = param.selectSingleNode("param-value").getStringValue();
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
package org.jivesoftware.openfire.container; package org.jivesoftware.openfire.container;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.util.cache.CacheFactory; import org.jivesoftware.util.cache.CacheFactory;
...@@ -23,9 +24,17 @@ import java.util.Map; ...@@ -23,9 +24,17 @@ import java.util.Map;
* A simple registry of cache configuration data for plugins. * A simple registry of cache configuration data for plugins.
*/ */
public class PluginCacheRegistry { public class PluginCacheRegistry {
private static final PluginCacheRegistry instance = new PluginCacheRegistry();
private static Map<String, CacheInfo> extraCacheMappings = new HashMap<String, CacheInfo>(); private Map<String, CacheInfo> extraCacheMappings = new HashMap<String, CacheInfo>();
private static Map<String, List<CacheInfo>> pluginCaches = new HashMap<String, List<CacheInfo>>(); private Map<String, List<CacheInfo>> pluginCaches = new HashMap<String, List<CacheInfo>>();
public static PluginCacheRegistry getInstance() {
return instance;
}
private PluginCacheRegistry() {
}
/** /**
* Registers cache configuration data for a give cache and plugin. * Registers cache configuration data for a give cache and plugin.
...@@ -33,7 +42,7 @@ public class PluginCacheRegistry { ...@@ -33,7 +42,7 @@ public class PluginCacheRegistry {
* @param pluginName the name of the plugin which will use the cache. * @param pluginName the name of the plugin which will use the cache.
* @param info the cache configuration data. * @param info the cache configuration data.
*/ */
public static void registerCache(String pluginName, CacheInfo info) { public void registerCache(String pluginName, CacheInfo info) {
extraCacheMappings.put(info.getCacheName(), info); extraCacheMappings.put(info.getCacheName(), info);
List<CacheInfo> caches = pluginCaches.get(pluginName); List<CacheInfo> caches = pluginCaches.get(pluginName);
...@@ -43,6 +52,12 @@ public class PluginCacheRegistry { ...@@ -43,6 +52,12 @@ public class PluginCacheRegistry {
} }
caches.add(info); caches.add(info);
// Set system properties for this cache
CacheFactory.setCacheTypeProperty(info.getCacheName(), info.getType().getName());
CacheFactory.setMaxSizeProperty(info.getCacheName(), getMaxSizeFromProperty(info));
CacheFactory.setMaxLifetimeProperty(info.getCacheName(), getMaxLifetimeFromProperty(info));
CacheFactory.setMinCacheSize(info.getCacheName(), getMinSizeFromProperty(info));
} }
/** /**
...@@ -50,12 +65,13 @@ public class PluginCacheRegistry { ...@@ -50,12 +65,13 @@ public class PluginCacheRegistry {
* *
* @param pluginName the name of the plugin whose caches will be unregistered. * @param pluginName the name of the plugin whose caches will be unregistered.
*/ */
public static void unregisterCaches(String pluginName) { public void unregisterCaches(String pluginName) {
List<CacheInfo> caches = pluginCaches.remove(pluginName); List<CacheInfo> caches = pluginCaches.remove(pluginName);
if (caches != null) { if (caches != null) {
for (CacheInfo info : caches) { for (CacheInfo info : caches) {
extraCacheMappings.remove(info.getCacheName()); extraCacheMappings.remove(info.getCacheName());
try { try {
// TODO Destroy cache if we are the last node hosting this plugin
CacheFactory.destroyCache(info.getCacheName()); CacheFactory.destroyCache(info.getCacheName());
} }
catch (Exception e) { catch (Exception e) {
...@@ -65,7 +81,68 @@ public class PluginCacheRegistry { ...@@ -65,7 +81,68 @@ public class PluginCacheRegistry {
} }
} }
public static CacheInfo getCacheInfo(String name) { public CacheInfo getCacheInfo(String name) {
return extraCacheMappings.get(name); return extraCacheMappings.get(name);
} }
private long getMaxSizeFromProperty(CacheInfo cacheInfo) {
String sizeProp = cacheInfo.getParams().get("back-size-high");
if (sizeProp != null) {
if ("0".equals(sizeProp)) {
return -1l;
}
try {
return Integer.parseInt(sizeProp);
}
catch (NumberFormatException nfe) {
Log.warn("Unable to parse back-size-high for cache: " + cacheInfo.getCacheName());
}
}
// Return default
return CacheFactory.DEFAULT_MAX_CACHE_SIZE;
}
private static long getMaxLifetimeFromProperty(CacheInfo cacheInfo) {
String lifetimeProp = cacheInfo.getParams().get("back-expiry");
if (lifetimeProp != null) {
if ("0".equals(lifetimeProp)) {
return -1l;
}
long factor = 1;
if (lifetimeProp.endsWith("m")) {
factor = JiveConstants.MINUTE;
}
else if (lifetimeProp.endsWith("h")) {
factor = JiveConstants.HOUR;
}
else if (lifetimeProp.endsWith("d")) {
factor = JiveConstants.DAY;
}
try {
return Long.parseLong(lifetimeProp.substring(0, lifetimeProp.length()-1)) * factor;
}
catch (NumberFormatException nfe) {
Log.warn("Unable to parse back-expiry for cache: " + cacheInfo.getCacheName());
}
}
// Return default
return CacheFactory.DEFAULT_MAX_CACHE_LIFETIME;
}
private long getMinSizeFromProperty(CacheInfo cacheInfo) {
String sizeProp = cacheInfo.getParams().get("back-size-low");
if (sizeProp != null) {
if ("0".equals(sizeProp)) {
return -1l;
}
try {
return Integer.parseInt(sizeProp);
}
catch (NumberFormatException nfe) {
Log.warn("Unable to parse back-size-low for cache: " + cacheInfo.getCacheName());
}
}
// Return default
return CacheFactory.DEFAULT_MAX_CACHE_SIZE;
}
} }
...@@ -610,7 +610,7 @@ public class PluginManager { ...@@ -610,7 +610,7 @@ public class PluginManager {
if (plugin != null && !dir.exists()) { if (plugin != null && !dir.exists()) {
// Unregister plugin caches // Unregister plugin caches
PluginCacheRegistry.unregisterCaches(pluginName); PluginCacheRegistry.getInstance().unregisterCaches(pluginName);
plugins.remove(pluginName); plugins.remove(pluginName);
pluginDirs.remove(plugin); pluginDirs.remove(plugin);
......
...@@ -484,7 +484,7 @@ public class DefaultCache<K, V> implements Cache<K, V> { ...@@ -484,7 +484,7 @@ public class DefaultCache<K, V> implements Cache<K, V> {
*/ */
public void setMaxCacheSize(int maxCacheSize) { public void setMaxCacheSize(int maxCacheSize) {
this.maxCacheSize = maxCacheSize; this.maxCacheSize = maxCacheSize;
DefaultLocalCacheStrategy.setMaxSizeProperty(name, maxCacheSize); CacheFactory.setMaxSizeProperty(name, maxCacheSize);
// It's possible that the new max size is smaller than our current cache // It's possible that the new max size is smaller than our current cache
// size. If so, we need to delete infrequently used items. // size. If so, we need to delete infrequently used items.
cullCache(); cullCache();
...@@ -512,7 +512,7 @@ public class DefaultCache<K, V> implements Cache<K, V> { ...@@ -512,7 +512,7 @@ public class DefaultCache<K, V> implements Cache<K, V> {
*/ */
public void setMaxLifetime(long maxLifetime) { public void setMaxLifetime(long maxLifetime) {
this.maxLifetime = maxLifetime; this.maxLifetime = maxLifetime;
DefaultLocalCacheStrategy.setMaxLifetimeProperty(name, maxLifetime); CacheFactory.setMaxLifetimeProperty(name, maxLifetime);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment