Repository: incubator-stratos
Updated Branches:
  refs/heads/master 3f0fff7b0 -> 53f1fc8bf


Fixed algorithm context cache issue by removing the cache manager local 
reference


Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/53f1fc8b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/53f1fc8b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/53f1fc8b

Branch: refs/heads/master
Commit: 53f1fc8bfb8eb417bd6f53d1b0d9bd435a60ac44
Parents: 3f0fff7
Author: Imesh Gunaratne <[email protected]>
Authored: Wed Feb 26 21:53:27 2014 -0500
Committer: Imesh Gunaratne <[email protected]>
Committed: Wed Feb 26 21:53:27 2014 -0500

----------------------------------------------------------------------
 .../balancer/cache/AlgorithmContextCache.java   |  4 +-
 .../load/balancer/cache/LoadBalancerCache.java  | 51 ++++++--------------
 2 files changed, 18 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/53f1fc8b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/AlgorithmContextCache.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/AlgorithmContextCache.java
 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/AlgorithmContextCache.java
index ab61557..2301d37 100644
--- 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/AlgorithmContextCache.java
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/AlgorithmContextCache.java
@@ -35,11 +35,11 @@ public class AlgorithmContextCache {
 
     public static void putCurrentMemberIndex(String serviceName, String 
clusterId, int currentMemberIndex) {
         String key = prepareKey(serviceName, clusterId);
-        
LoadBalancerCache.getInstance().putInteger(Constants.ALGORITHM_CONTEXT_CACHE, 
key, currentMemberIndex);
+        LoadBalancerCache.putInteger(Constants.ALGORITHM_CONTEXT_CACHE, key, 
currentMemberIndex);
     }
 
     public static int getCurrentMemberIndex(String serviceName, String 
clusterId) {
         String key = prepareKey(serviceName, clusterId);
-        return 
LoadBalancerCache.getInstance().getInteger(Constants.ALGORITHM_CONTEXT_CACHE, 
key);
+        return LoadBalancerCache.getInteger(Constants.ALGORITHM_CONTEXT_CACHE, 
key);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/53f1fc8b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/LoadBalancerCache.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/LoadBalancerCache.java
 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/LoadBalancerCache.java
index 2d9ef74..18c0a41 100644
--- 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/LoadBalancerCache.java
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/LoadBalancerCache.java
@@ -34,50 +34,31 @@ import javax.cache.Caching;
  */
 class LoadBalancerCache {
     private static final Log log = LogFactory.getLog(LoadBalancerCache.class);
-    private static volatile LoadBalancerCache instance;
+    private static final String CACHE_MANAGER_NAME = "LoadBalancerCache";
 
-    private CacheManager cacheManager;
-
-    private LoadBalancerCache() {
-        try {
-            startSuperTenantFlow();
-            cacheManager = (CacheManager) 
Caching.getCacheManagerFactory().getCacheManager("LoadBalancerCache");
-        } catch (Exception e) {
-            if (log.isErrorEnabled()) {
-                log.error(e);
-            }
-            throw new RuntimeException(e);
-        } finally {
-            endSuperTenantFlow();
-        }
-    }
-
-    public static LoadBalancerCache getInstance() {
-        if (instance == null) {
-            synchronized (LoadBalancerCache.class) {
-                if (instance == null) {
-                    instance = new LoadBalancerCache();
-                }
-            }
+    private static CacheManager getCacheManager() {
+        CacheManager cacheManager = (CacheManager) 
Caching.getCacheManagerFactory().getCacheManager(CACHE_MANAGER_NAME);
+        if(cacheManager == null) {
+            throw new RuntimeException("Could not get cache manager");
         }
-        return instance;
+        return cacheManager;
     }
 
-    private void startSuperTenantFlow() {
+    private static void startSuperTenantFlow() {
         PrivilegedCarbonContext.startTenantFlow();
         PrivilegedCarbonContext ctx = 
PrivilegedCarbonContext.getThreadLocalCarbonContext();
         ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
         ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
     }
 
-    private void endSuperTenantFlow() {
+    private static void endSuperTenantFlow() {
         PrivilegedCarbonContext.endTenantFlow();
     }
 
-    void putString(String cacheName, String propertyName, String value) {
+    static void putString(String cacheName, String propertyName, String value) 
{
         try {
             startSuperTenantFlow();
-            Cache<String, String> cache = cacheManager.getCache(cacheName);
+            Cache<String, String> cache = 
getCacheManager().getCache(cacheName);
             cache.put(propertyName, value);
             if (log.isDebugEnabled()) {
                 log.debug(String.format("Cached property: [cache] %s 
[property] %s [value] %s", cacheName, propertyName, value));
@@ -87,10 +68,10 @@ class LoadBalancerCache {
         }
     }
 
-    String getString(String cacheName, String propertyName) {
+    static String getString(String cacheName, String propertyName) {
         try {
             startSuperTenantFlow();
-            Cache<String, String> cache = cacheManager.getCache(cacheName);
+            Cache<String, String> cache = 
getCacheManager().getCache(cacheName);
             String value = cache.get(propertyName);
             if (log.isDebugEnabled()) {
                 log.debug(String.format("Read cached property: [cache] %s 
[property] %s [value] %s", cacheName, propertyName, value));
@@ -101,10 +82,10 @@ class LoadBalancerCache {
         }
     }
 
-    void putInteger(String cacheName, String propertyName, int value) {
+    static void putInteger(String cacheName, String propertyName, int value) {
         try {
             startSuperTenantFlow();
-            Cache<String, Integer> cache = cacheManager.getCache(cacheName);
+            Cache<String, Integer> cache = 
getCacheManager().getCache(cacheName);
             cache.put(propertyName, value);
             if (log.isDebugEnabled()) {
                 log.debug(String.format("Cached property: [cache] %s 
[property] %s [value] %d", cacheName, propertyName, value));
@@ -114,10 +95,10 @@ class LoadBalancerCache {
         }
     }
 
-    int getInteger(String cacheName, String propertyName) {
+    static int getInteger(String cacheName, String propertyName) {
         try {
             startSuperTenantFlow();
-            Cache<String, Integer> cache = cacheManager.getCache(cacheName);
+            Cache<String, Integer> cache = 
getCacheManager().getCache(cacheName);
             int value = cache.get(propertyName);
             if (log.isDebugEnabled()) {
                 log.debug(String.format("Read cached property: [cache] %s 
[property] %s [value] %d", cacheName, propertyName, value));

Reply via email to