Added/Improved comments.

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

Branch: refs/heads/gsoc-projects-2015
Commit: df8684b7ecdddcd5e02f8d0d8bbb255fdafb55d8
Parents: 8414eaa
Author: swapnilpatilRajaram <[email protected]>
Authored: Sun Aug 16 06:24:57 2015 +0000
Committer: swapnilpatilRajaram <[email protected]>
Committed: Sun Aug 16 06:24:57 2015 +0000

----------------------------------------------------------------------
 .../apache/stratos/aws/extension/AWSHelper.java | 120 ++++++++++++++++---
 .../stratos/aws/extension/AWSLoadBalancer.java  |  35 ++----
 .../aws/extension/AWSStatisticsReader.java      |  10 +-
 3 files changed, 118 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/df8684b7/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
----------------------------------------------------------------------
diff --git 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
index 5a3393f..768a850 100644
--- 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
+++ 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
@@ -40,7 +40,6 @@ import org.joda.time.DateTime;
 import org.joda.time.DateTimeZone;
 
 import com.amazonaws.AmazonClientException;
-import com.amazonaws.AmazonServiceException;
 import com.amazonaws.ClientConfiguration;
 import com.amazonaws.auth.BasicAWSCredentials;
 import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
@@ -219,8 +218,11 @@ public class AWSHelper {
         * cluster is added.
         * 
         * @param name
+        *            of the load balancer to be created
         * @param listeners
+        *            to be attached to the load balancer
         * @param region
+        *            in which the load balancer needs to be created
         * @return DNS name of newly created load balancer
         * @throws LoadBalancerExtensionException
         */
@@ -267,7 +269,9 @@ public class AWSHelper {
         * with which this load balancer was associated, is removed.
         * 
         * @param loadBalancerName
+        *            to be deleted
         * @param region
+        *            of the laod balancer
         */
        public void deleteLoadBalancer(String loadBalancerName, String region) {
 
@@ -293,13 +297,19 @@ public class AWSHelper {
         * 
         * @param loadBalancerName
         * @param instances
+        *            to attached to the load balancer
         * @param region
+        *            of the load balancer
         */
        public void registerInstancesToLoadBalancer(String loadBalancerName,
                        List<Instance> instances, String region) {
 
-               log.info("Attaching instance " + instances.get(0)
-                               + " to load balancer + " + loadBalancerName);
+               log.info("Attaching following instance(s) to load balancer + "
+                               + loadBalancerName);
+
+               for (Instance instance : instances) {
+                       log.info(instance.getInstanceId());
+               }
 
                RegisterInstancesWithLoadBalancerRequest 
registerInstancesWithLoadBalancerRequest = new 
RegisterInstancesWithLoadBalancerRequest(
                                loadBalancerName, instances);
@@ -308,7 +318,8 @@ public class AWSHelper {
                        elbClient.setEndpoint(String.format(
                                        Constants.ELB_ENDPOINT_URL_FORMAT, 
region));
 
-                       
elbClient.registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest);
+                       elbClient
+                                       
.registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest);
 
                } catch (AmazonClientException e) {
                        log.error("Could not register instances to load 
balancer "
@@ -323,13 +334,19 @@ public class AWSHelper {
         * 
         * @param loadBalancerName
         * @param instances
+        *            to be de-registered from load balancer
         * @param region
+        *            of the load balancer
         */
        public void deregisterInstancesFromLoadBalancer(String loadBalancerName,
                        List<Instance> instances, String region) {
 
-               log.info("Detaching instance " + instances.get(0)
-                               + " from load balancer + " + loadBalancerName);
+               log.info("De-registering following instance(s) from load 
balancer + "
+                               + loadBalancerName);
+
+               for (Instance instance : instances) {
+                       log.info(instance.getInstanceId());
+               }
 
                DeregisterInstancesFromLoadBalancerRequest 
deregisterInstancesFromLoadBalancerRequest = new 
DeregisterInstancesFromLoadBalancerRequest(
                                loadBalancerName, instances);
@@ -338,7 +355,8 @@ public class AWSHelper {
                        elbClient.setEndpoint(String.format(
                                        Constants.ELB_ENDPOINT_URL_FORMAT, 
region));
 
-                       
elbClient.deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest);
+                       elbClient
+                                       
.deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest);
 
                } catch (AmazonClientException e) {
                        log.error("Could not de-register instances from load 
balancer "
@@ -352,13 +370,13 @@ public class AWSHelper {
         * 
         * @param loadBalancerName
         * @param region
+        *            of the load balancer
         * @return description of the load balancer
         */
        private LoadBalancerDescription getLoadBalancerDescription(
                        String loadBalancerName, String region) {
 
                List<String> loadBalancers = new ArrayList<String>();
-
                loadBalancers.add(loadBalancerName);
 
                DescribeLoadBalancersRequest describeLoadBalancersRequest = new 
DescribeLoadBalancersRequest(
@@ -450,6 +468,15 @@ public class AWSHelper {
 
        }
 
+       /**
+        * Checks if the security group is already present in the given region. 
If
+        * yes, then returns its group id. If not, present the returns null.
+        * 
+        * @param groupName
+        *            to be checked for presence.
+        * @param region
+        * @return id of the security group
+        */
        public String getSecurityGroupId(String groupName, String region) {
                if (groupName == null || groupName.isEmpty()) {
                        return null;
@@ -482,6 +509,17 @@ public class AWSHelper {
                return null;
        }
 
+       /**
+        * Creates security group with the given name in the given region
+        * 
+        * @param groupName
+        *            to be created
+        * @param description
+        * @param region
+        *            in which the security group to be created
+        * @return Id of the security group created
+        * @throws LoadBalancerExtensionException
+        */
        public String createSecurityGroup(String groupName, String description,
                        String region) throws LoadBalancerExtensionException {
                if (groupName == null || groupName.isEmpty()) {
@@ -510,6 +548,22 @@ public class AWSHelper {
 
        }
 
+       /**
+        * Adds inbound rule to the security group which allows users to access 
load
+        * balancer at specified port and using the specified protocol. Port
+        * specified should be a proxy port mentioned in the port mappings of 
the
+        * cartridge.
+        * 
+        * @param groupId
+        *            to which this rule to be added
+        * @param region
+        *            of the security group
+        * @param protocol
+        *            with which load balancer can be accessed
+        * @param port
+        *            at which load balancer can be accessed
+        * @throws LoadBalancerExtensionException
+        */
        public void addInboundRuleToSecurityGroup(String groupId, String region,
                        String protocol, int port) throws 
LoadBalancerExtensionException {
                if (groupId == null || groupId.isEmpty()) {
@@ -581,17 +635,22 @@ public class AWSHelper {
                                                
.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
 
                        } catch (AmazonClientException e) {
-
-                               // if(!e.getMessage().contains("already exist"))
-                               // {
                                throw new LoadBalancerExtensionException(
                                                "Could not add inbound rule to 
security group "
-                                                               + groupId + 
".");
-                               // }
+                                                               + groupId + 
".", e);
                        }
                }
        }
 
+       /**
+        * Returns the security group id for the given region if it is already
+        * present. If it is not already present then creates a new security 
group
+        * in that region.
+        * 
+        * @param region
+        * @return Id of the security group
+        * @throws LoadBalancerExtensionException
+        */
        public String getSecurityGroupIdForRegion(String region)
                        throws LoadBalancerExtensionException {
                if (region == null)
@@ -616,11 +675,14 @@ public class AWSHelper {
        }
 
        /**
+        * Retrieves the total number of requests that were made to the load
+        * balancer during the given time interval in the past
+        * 
         * @param loadBalancerName
         * @param region
         * @param timeInterval
-        *            in seconds
-        * @return
+        *            in seconds which must be multiple of 60
+        * @return number of requests made
         */
        public int getRequestCount(String loadBalancerName, String region,
                        int timeInterval) {
@@ -673,6 +735,16 @@ public class AWSHelper {
                return count;
        }
 
+       /**
+        * Retrieves total number of responses generated by all instances 
attached
+        * to the load balancer during the time interval in the past.
+        * 
+        * @param loadBalancerName
+        * @param region
+        * @param timeInterval
+        *            in seconds which must be multiple of 60
+        * @return number of responses generated
+        */
        public int getAllResponsesCount(String loadBalancerName, String region,
                        int timeInterval) {
                int total = 0;
@@ -697,6 +769,24 @@ public class AWSHelper {
                return total;
        }
 
+       /**
+        * Retrieves the number of responses generated for a particular response
+        * code like 2XX, 3XX, 4XX, 5XX
+        * 
+        * @param loadBalancerName
+        * @param region
+        * @param metricName
+        *            which is one among HTTPCode_Backend_2XX or
+        *            HTTPCode_Backend_3XX or HTTPCode_Backend_4XX or
+        *            HTTPCode_Backend_5XX
+        * @param startTime
+        *            of the window to be scanned
+        * @param endTime
+        *            of the window to be scanned
+        * @param timeInterval
+        *            in seconds
+        * @return number for response for this metric
+        */
        public int getResponseCountForMetric(String loadBalancerName,
                        String region, String metricName, Date startTime, Date 
endTime,
                        int timeInterval) {

http://git-wip-us.apache.org/repos/asf/stratos/blob/df8684b7/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
----------------------------------------------------------------------
diff --git 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
index 6b5c3fc..59b313e 100644
--- 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
+++ 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
@@ -19,10 +19,8 @@
 
 package org.apache.stratos.aws.extension;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
@@ -33,7 +31,6 @@ import org.apache.stratos.load.balancer.common.domain.*;
 import 
org.apache.stratos.load.balancer.extension.api.exception.LoadBalancerExtensionException;
 import org.apache.stratos.load.balancer.extension.api.LoadBalancer;
 
-import com.amazonaws.AmazonClientException;
 import com.amazonaws.services.elasticloadbalancing.model.Instance;
 import com.amazonaws.services.elasticloadbalancing.model.Listener;
 
@@ -41,9 +38,10 @@ public class AWSLoadBalancer implements LoadBalancer {
 
        private static final Log log = LogFactory.getLog(AWSLoadBalancer.class);
 
-       // A map <clusterId, load balancer id>
+       // A map <clusterId, load balancer info> to store load balancer 
information against the cluster id
        private static ConcurrentHashMap<String, LoadBalancerInfo> 
clusterIdToLoadBalancerMap = new ConcurrentHashMap<String, LoadBalancerInfo>();
 
+       // Object used to invoke methods related to AWS API
        private AWSHelper awsHelper;
 
        public AWSLoadBalancer() throws LoadBalancerExtensionException {
@@ -78,11 +76,11 @@ public class AWSLoadBalancer implements LoadBalancer {
                                                String loadBalancerName = 
loadBalancerInfo.getName();
                                                String region = 
loadBalancerInfo.getRegion();
 
-                                               // 1. Get all the instances 
attached
-                                               // Add/remove instances as 
necessary
+                                               // Get all the instances 
attached
+                                               // Attach newly added instances 
to load balancer
 
                                                // attachedInstances list is 
useful in finding out what
-                                               // all new instances
+                                               // all new instances which
                                                // should be attached to this 
load balancer.
                                                List<Instance> 
attachedInstances = awsHelper
                                                                
.getAttachedInstances(loadBalancerName, region);
@@ -120,8 +118,6 @@ public class AWSLoadBalancer implements LoadBalancer {
                                                                
awsHelper.registerInstancesToLoadBalancer(
                                                                                
loadBalancerName,
                                                                                
instancesToAddToLoadBalancer, region);
-
-                                                       // Update domain 
mappings
                                                }
 
                                        } else {
@@ -130,9 +126,6 @@ public class AWSLoadBalancer implements LoadBalancer {
                                                                .getMembers();
 
                                                if (clusterMembers.size() > 0) {
-
-                                                       // try
-                                                       // {
                                                        // a unique load 
balancer name with user-defined
                                                        // prefix and a 
sequence number.
                                                        String loadBalancerName 
= awsHelper
@@ -155,10 +148,9 @@ public class AWSLoadBalancer implements LoadBalancer {
                                                                        
.createLoadBalancer(loadBalancerName,
                                                                                
        listenersForThisCluster, region);
 
-                                                       // Also add the inbound 
rule
+                                                       // Add the inbound rule 
the security group of the load balancer
                                                        // For each listener, 
add a new rule with load
-                                                       // balancer port as 
allowed protocol.
-
+                                                       // balancer port as 
allowed protocol in the security group.
                                                        for (Listener listener 
: listenersForThisCluster) {
                                                                int port = 
listener.getLoadBalancerPort();
 
@@ -179,7 +171,7 @@ public class AWSLoadBalancer implements LoadBalancer {
                                                                        + "' 
created for cluster '"
                                                                        + 
cluster.getClusterId());
 
-                                                       // register instances 
to LB
+                                                       // Register instances 
in the cluster to load balancer
                                                        List<Instance> 
instances = new ArrayList<Instance>();
 
                                                        for (Member member : 
clusterMembers) {
@@ -199,19 +191,12 @@ public class AWSLoadBalancer implements LoadBalancer {
                                                        
awsHelper.registerInstancesToLoadBalancer(
                                                                        
loadBalancerName, instances, region);
 
-                                                       // Create domain 
mappings
-
                                                        LoadBalancerInfo 
loadBalancerInfo = new LoadBalancerInfo(
                                                                        
loadBalancerName, region);
 
                                                        
clusterIdToLoadBalancerMap.put(
                                                                        
cluster.getClusterId(), loadBalancerInfo);
                                                        
activeClusters.add(cluster.getClusterId());
-                                                       // }
-                                                       // 
catch(LoadBalancerExtensionException e)
-                                                       // {
-                                                       // log.debug(e);
-                                                       // }
                                                }
                                        }
                                }
@@ -270,12 +255,10 @@ public class AWSLoadBalancer implements LoadBalancer {
                // Remove all load balancers
                for (LoadBalancerInfo loadBalancerInfo : 
clusterIdToLoadBalancerMap
                                .values()) {
-                       // remove load balancer
+                       // Remove load balancer
                        awsHelper.deleteLoadBalancer(loadBalancerInfo.getName(),
                                        loadBalancerInfo.getRegion());
                }
-
-               // Remove domain mappings
        }
 
        public static ConcurrentHashMap<String, LoadBalancerInfo> 
getClusterIdToLoadBalancerMap() {

http://git-wip-us.apache.org/repos/asf/stratos/blob/df8684b7/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java
----------------------------------------------------------------------
diff --git 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java
 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java
index 40d51e9..55aca3d 100644
--- 
a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java
+++ 
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java
@@ -22,16 +22,10 @@ package org.apache.stratos.aws.extension;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.common.constants.StratosConstants;
-import org.apache.stratos.common.util.CommandUtils;
-import org.apache.stratos.load.balancer.common.domain.Cluster;
-import org.apache.stratos.load.balancer.common.domain.Member;
-import org.apache.stratos.load.balancer.common.domain.Port;
-import org.apache.stratos.load.balancer.common.domain.Service;
 import 
org.apache.stratos.load.balancer.common.statistics.LoadBalancerStatisticsReader;
 import org.apache.stratos.load.balancer.common.topology.TopologyProvider;
 import 
org.apache.stratos.load.balancer.extension.api.exception.LoadBalancerExtensionException;
 
-import java.io.IOException;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -69,6 +63,9 @@ public class AWSStatisticsReader implements 
LoadBalancerStatisticsReader {
                ConcurrentHashMap<String, LoadBalancerInfo> 
clusterIdToLoadBalancerMap = AWSLoadBalancer
                                .getClusterIdToLoadBalancerMap();
 
+               // Check if load balancer info is available for this cluster.
+               // If yes, then find difference between total requests made to 
the load balancer and 
+               // total responses generated by instances attached to it.
                if (clusterIdToLoadBalancerMap.containsKey(clusterId)) {
                        LoadBalancerInfo loadBalancerInfo = 
clusterIdToLoadBalancerMap
                                        .get(clusterId);
@@ -76,6 +73,7 @@ public class AWSStatisticsReader implements 
LoadBalancerStatisticsReader {
                        String loadBalancerName = loadBalancerInfo.getName();
                        String region = loadBalancerInfo.getRegion();
 
+                       // In flight request count = total requests - total 
responses
                        inFlightRequestCount = 
awsHelper.getRequestCount(loadBalancerName,
                                        region, 
awsHelper.getStatisticsInterval())
                                        - 
awsHelper.getAllResponsesCount(loadBalancerName, region,

Reply via email to