Implemented load balance algorithm context cache for clustering

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

Branch: refs/heads/master
Commit: 5241cddc34e7c3fcc7d879de1aa44189562f490b
Parents: 0fa8561
Author: Imesh Gunaratne <[email protected]>
Authored: Wed Feb 12 20:26:20 2014 -0500
Committer: Imesh Gunaratne <[email protected]>
Committed: Wed Feb 12 20:26:20 2014 -0500

----------------------------------------------------------------------
 .../balancer/algorithm/AlgorithmContext.java    |  10 +-
 .../balancer/cache/AlgorithmContextCache.java   |  45 +++++++
 .../load/balancer/cache/LoadBalancerCache.java  | 130 +++++++++++++++++++
 .../stratos/load/balancer/util/Constants.java   |   3 +
 4 files changed, 184 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5241cddc/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/algorithm/AlgorithmContext.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/algorithm/AlgorithmContext.java
 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/algorithm/AlgorithmContext.java
index 24bc70d..3aa249f 100755
--- 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/algorithm/AlgorithmContext.java
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/algorithm/AlgorithmContext.java
@@ -19,18 +19,20 @@
 
 package org.apache.stratos.load.balancer.algorithm;
 
+import org.apache.stratos.load.balancer.cache.AlgorithmContextCache;
+
 /**
  * Algorithm context is used for identifying the cluster and its current 
member for executing load balancing algorithms.
+ * Key: service name, cluster id
  */
 public class AlgorithmContext {
     private String serviceName;
     private String clusterId;
-    private int currentMemberIndex;
 
     public AlgorithmContext(String serviceName, String clusterId) {
         this.serviceName = serviceName;
         this.clusterId = clusterId;
-        this.currentMemberIndex = 0;
+        AlgorithmContextCache.putCurrentMemberIndex(serviceName, clusterId, 0);
     }
 
     public String getServiceName() {
@@ -42,10 +44,10 @@ public class AlgorithmContext {
     }
 
     public int getCurrentMemberIndex() {
-        return currentMemberIndex;
+        return AlgorithmContextCache.getCurrentMemberIndex(getServiceName(), 
getClusterId());
     }
 
     public void setCurrentMemberIndex(int currentMemberIndex) {
-        this.currentMemberIndex = currentMemberIndex;
+        AlgorithmContextCache.putCurrentMemberIndex(getServiceName(), 
getClusterId(), currentMemberIndex);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5241cddc/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
new file mode 100644
index 0000000..ab61557
--- /dev/null
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/AlgorithmContextCache.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.stratos.load.balancer.cache;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.load.balancer.util.Constants;
+
+/**
+ * Algorithm context cache manages algorithm context values in a clustered 
environment.
+ */
+public class AlgorithmContextCache {
+    private static final Log log = 
LogFactory.getLog(AlgorithmContextCache.class);
+
+    private static String prepareKey(String serviceName, String clusterId) {
+        return String.format("%s-%s", serviceName, clusterId);
+    }
+
+    public static void putCurrentMemberIndex(String serviceName, String 
clusterId, int currentMemberIndex) {
+        String key = prepareKey(serviceName, clusterId);
+        
LoadBalancerCache.getInstance().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);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5241cddc/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
new file mode 100644
index 0000000..2d9ef74
--- /dev/null
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/cache/LoadBalancerCache.java
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.stratos.load.balancer.cache;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.base.MultitenantConstants;
+import org.wso2.carbon.context.PrivilegedCarbonContext;
+
+import javax.cache.Cache;
+import javax.cache.CacheManager;
+import javax.cache.Caching;
+
+
+/**
+ * Load balancer cache manages the load balancer contexts in a clustered 
environment.
+ */
+class LoadBalancerCache {
+    private static final Log log = LogFactory.getLog(LoadBalancerCache.class);
+    private static volatile LoadBalancerCache instance;
+
+    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();
+                }
+            }
+        }
+        return instance;
+    }
+
+    private void startSuperTenantFlow() {
+        PrivilegedCarbonContext.startTenantFlow();
+        PrivilegedCarbonContext ctx = 
PrivilegedCarbonContext.getThreadLocalCarbonContext();
+        ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
+        ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
+    }
+
+    private void endSuperTenantFlow() {
+        PrivilegedCarbonContext.endTenantFlow();
+    }
+
+    void putString(String cacheName, String propertyName, String value) {
+        try {
+            startSuperTenantFlow();
+            Cache<String, String> cache = cacheManager.getCache(cacheName);
+            cache.put(propertyName, value);
+            if (log.isDebugEnabled()) {
+                log.debug(String.format("Cached property: [cache] %s 
[property] %s [value] %s", cacheName, propertyName, value));
+            }
+        } finally {
+            endSuperTenantFlow();
+        }
+    }
+
+    String getString(String cacheName, String propertyName) {
+        try {
+            startSuperTenantFlow();
+            Cache<String, String> cache = cacheManager.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));
+            }
+            return value;
+        } finally {
+            endSuperTenantFlow();
+        }
+    }
+
+    void putInteger(String cacheName, String propertyName, int value) {
+        try {
+            startSuperTenantFlow();
+            Cache<String, Integer> cache = cacheManager.getCache(cacheName);
+            cache.put(propertyName, value);
+            if (log.isDebugEnabled()) {
+                log.debug(String.format("Cached property: [cache] %s 
[property] %s [value] %d", cacheName, propertyName, value));
+            }
+        } finally {
+            endSuperTenantFlow();
+        }
+    }
+
+    int getInteger(String cacheName, String propertyName) {
+        try {
+            startSuperTenantFlow();
+            Cache<String, Integer> cache = cacheManager.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));
+            }
+            return value;
+        } finally {
+            endSuperTenantFlow();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/5241cddc/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/util/Constants.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/util/Constants.java
 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/util/Constants.java
index 674fe24..ff64d20 100644
--- 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/util/Constants.java
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/util/Constants.java
@@ -16,6 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 package org.apache.stratos.load.balancer.util;
 
 public class Constants {
@@ -26,5 +27,7 @@ public class Constants {
     public static final String LB_HTTP_PORT = "LB_HTTP_PORT";
     public static final String LB_HTTPS_PORT = "LB_HTTPS_PORT";
 
+    public static final String ALGORITHM_CONTEXT_CACHE = 
"algorithm.context.cache";
+
     public static final String AXIS2_MSG_CTX_TRANSPORT_IN_URL = 
"TransportInURL";
 }

Reply via email to