Commit 05814256 authored by Robin Collier's avatar Robin Collier Committed by rcollier

OF-588 Added ability to create local only cache from CacheFactory.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@13356 b35dd754-fafc-0310-a699-88a17e54d16e
parent 5bc80bc3
...@@ -52,7 +52,7 @@ public class PEPServiceManager { ...@@ -52,7 +52,7 @@ public class PEPServiceManager {
* Cache of PEP services. Table, Key: bare JID (String); Value: PEPService * Cache of PEP services. Table, Key: bare JID (String); Value: PEPService
*/ */
private final Cache<String, PEPService> pepServices = CacheFactory private final Cache<String, PEPService> pepServices = CacheFactory
.createCache("PEPServiceManager"); .createLocalCache("PEPServiceManager");
private PubSubEngine pubSubEngine = null; private PubSubEngine pubSubEngine = null;
......
...@@ -50,6 +50,7 @@ import org.slf4j.LoggerFactory; ...@@ -50,6 +50,7 @@ import org.slf4j.LoggerFactory;
* named "opt-$cacheStats". * named "opt-$cacheStats".
* *
*/ */
@SuppressWarnings("rawtypes")
public class CacheFactory { public class CacheFactory {
private static final Logger Log = LoggerFactory.getLogger(CacheFactory.class); private static final Logger Log = LoggerFactory.getLogger(CacheFactory.class);
...@@ -65,9 +66,15 @@ public class CacheFactory { ...@@ -65,9 +66,15 @@ public class CacheFactory {
*/ */
private static Map<String, Cache> caches = new ConcurrentHashMap<String, Cache>(); private static Map<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
/**
* Maps the cache to the strategy that created it.
*/
private static Map<String, CacheFactoryStrategy> strategyLookup = new ConcurrentHashMap<String, CacheFactoryStrategy>();
private static String localCacheFactoryClass; private static String localCacheFactoryClass;
private static String clusteredCacheFactoryClass; private static String clusteredCacheFactoryClass;
private static CacheFactoryStrategy cacheFactoryStrategy; private static CacheFactoryStrategy cacheFactoryStrategy;
private static CacheFactoryStrategy localCacheFactoryStrategy;
private static Thread statsThread; private static Thread statsThread;
public static final int DEFAULT_MAX_CACHE_SIZE = 1024 * 256; public static final int DEFAULT_MAX_CACHE_SIZE = 1024 * 256;
...@@ -345,12 +352,30 @@ public class CacheFactory { ...@@ -345,12 +352,30 @@ public class CacheFactory {
if (cache != null) { if (cache != null) {
return cache; return cache;
} }
strategyLookup.put(name, cacheFactoryStrategy);
cache = (T) cacheFactoryStrategy.createCache(name); cache = (T) cacheFactoryStrategy.createCache(name);
return wrapCache(cache, name); return wrapCache(cache, name);
} }
/**
* Returns the named local cache, creating it as necessary.
*
* @param name the name of the cache to create.
* @return the named cache, creating it as necessary.
*/
@SuppressWarnings("unchecked")
public static synchronized <T extends Cache> T createLocalCache(String name) {
T cache = (T) caches.get(name);
if (cache != null) {
return cache;
}
strategyLookup.put(name, localCacheFactoryStrategy);
cache = (T) localCacheFactoryStrategy.createCache(name);
return wrapCache(cache, name);
}
/** /**
* Destroys the cache for the cache name specified. * Destroys the cache for the cache name specified.
* *
...@@ -359,7 +384,7 @@ public class CacheFactory { ...@@ -359,7 +384,7 @@ public class CacheFactory {
public static void destroyCache(String name) { public static void destroyCache(String name) {
Cache cache = caches.remove(name); Cache cache = caches.remove(name);
if (cache != null) { if (cache != null) {
cacheFactoryStrategy.destroyCache(cache); strategyLookup.get(name).destroyCache(cache);
} }
} }
...@@ -378,7 +403,7 @@ public class CacheFactory { ...@@ -378,7 +403,7 @@ public class CacheFactory {
* @return an existing lock on the specified key or creates a new one if none was found. * @return an existing lock on the specified key or creates a new one if none was found.
*/ */
public static Lock getLock(Object key, Cache cache) { public static Lock getLock(Object key, Cache cache) {
return cacheFactoryStrategy.getLock(key, cache); return strategyLookup.get(cache.getName()).getLock(key, cache);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
...@@ -549,6 +574,7 @@ public class CacheFactory { ...@@ -549,6 +574,7 @@ public class CacheFactory {
try { try {
cacheFactoryStrategy = (CacheFactoryStrategy) Class cacheFactoryStrategy = (CacheFactoryStrategy) Class
.forName(localCacheFactoryClass).newInstance(); .forName(localCacheFactoryClass).newInstance();
localCacheFactoryStrategy = cacheFactoryStrategy;
} }
catch (InstantiationException e) { catch (InstantiationException e) {
throw new InitializationException(e); throw new InitializationException(e);
...@@ -592,11 +618,7 @@ public class CacheFactory { ...@@ -592,11 +618,7 @@ public class CacheFactory {
} }
if (!clusteringStarting) { if (!clusteringStarting) {
// Revert to local cache factory if cluster fails to start // Revert to local cache factory if cluster fails to start
try { cacheFactoryStrategy = localCacheFactoryStrategy;
cacheFactoryStrategy = (CacheFactoryStrategy) Class.forName(localCacheFactoryClass).newInstance();
} catch (Exception e) {
Log.error("Fatal error - Failed to join the cluster and failed to use local cache", e);
}
} }
else { else {
if (statsThread == null) { if (statsThread == null) {
...@@ -661,9 +683,7 @@ public class CacheFactory { ...@@ -661,9 +683,7 @@ public class CacheFactory {
// Stop the cluster // Stop the cluster
cacheFactoryStrategy.stopCluster(); cacheFactoryStrategy.stopCluster();
// Set the strategy to local // Set the strategy to local
cacheFactoryStrategy = (CacheFactoryStrategy) Class.forName(localCacheFactoryClass) cacheFactoryStrategy = localCacheFactoryStrategy;
.newInstance();
} }
catch (Exception e) { catch (Exception e) {
Log.error("Unable to stop clustering - continuing in clustered mode", e); Log.error("Unable to stop clustering - continuing in clustered mode", e);
...@@ -673,6 +693,7 @@ public class CacheFactory { ...@@ -673,6 +693,7 @@ public class CacheFactory {
/** /**
* Notification message indicating that this JVM has joined a cluster. * Notification message indicating that this JVM has joined a cluster.
*/ */
@SuppressWarnings("unchecked")
public static void joinedCluster() { public static void joinedCluster() {
// Loop through local caches and switch them to clustered cache (migrate content) // Loop through local caches and switch them to clustered cache (migrate content)
for (Cache cache : getAllCaches()) { for (Cache cache : getAllCaches()) {
...@@ -687,17 +708,21 @@ public class CacheFactory { ...@@ -687,17 +708,21 @@ public class CacheFactory {
/** /**
* Notification message indicating that this JVM has left the cluster. * Notification message indicating that this JVM has left the cluster.
*/ */
@SuppressWarnings("unchecked")
public static void leftCluster() { public static void leftCluster() {
clusteringStarted = false; clusteringStarted = false;
// Loop through clustered caches and change them to local caches (migrate content) // Loop through clustered caches and change them to local caches (migrate content)
try { try {
cacheFactoryStrategy = (CacheFactoryStrategy) Class.forName(localCacheFactoryClass).newInstance(); cacheFactoryStrategy = localCacheFactoryStrategy;
for (Cache cache : getAllCaches()) { for (Cache cache : getAllCaches()) {
if (strategyLookup.get(cache.getName()) != localCacheFactoryStrategy) {
CacheWrapper cacheWrapper = ((CacheWrapper) cache); CacheWrapper cacheWrapper = ((CacheWrapper) cache);
Cache standaloneCache = cacheFactoryStrategy.createCache(cacheWrapper.getName()); Cache standaloneCache = cacheFactoryStrategy.createCache(cacheWrapper.getName());
cacheWrapper.setWrappedCache(standaloneCache); cacheWrapper.setWrappedCache(standaloneCache);
} }
}
} catch (Exception e) { } catch (Exception e) {
Log.error("Error reverting caches to local caches", e); Log.error("Error reverting caches to local caches", e);
} }
......
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