stratos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject [03/32] stratos git commit: Adding logic for registering and de-registering instances to Load balanacer.
Date Fri, 09 Oct 2015 14:35:16 GMT
Adding logic for registering and de-registering instances to Load balanacer.


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

Branch: refs/heads/gsoc-projects-2015
Commit: 617e04d541e92234353b98ace949d99136172fa9
Parents: bcf2c8e
Author: swapnilpatilRajaram <swapnil.patil@students.iiit.ac.in>
Authored: Sun May 31 19:10:19 2015 +0000
Committer: swapnilpatilRajaram <swapnil.patil@students.iiit.ac.in>
Committed: Sun May 31 19:10:19 2015 +0000

----------------------------------------------------------------------
 .../apache/stratos/aws/extension/AWSHelper.java |  77 +++++++++++-
 .../stratos/aws/extension/AWSLoadBalancer.java  | 120 +++++++++++++++++--
 2 files changed, 188 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/617e04d5/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 4bca6f8..7a0d4db 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
@@ -33,13 +33,20 @@ import com.amazonaws.auth.BasicAWSCredentials;
 import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient;
 import com.amazonaws.services.elasticloadbalancing.model.CreateLoadBalancerRequest;
 import com.amazonaws.services.elasticloadbalancing.model.CreateLoadBalancerResult;
+import com.amazonaws.services.elasticloadbalancing.model.DeregisterInstancesFromLoadBalancerRequest;
+import com.amazonaws.services.elasticloadbalancing.model.DeregisterInstancesFromLoadBalancerResult;
+import com.amazonaws.services.elasticloadbalancing.model.Instance;
 import com.amazonaws.services.elasticloadbalancing.model.Listener;
+import com.amazonaws.services.elasticloadbalancing.model.RegisterInstancesWithLoadBalancerRequest;
+import com.amazonaws.services.elasticloadbalancing.model.RegisterInstancesWithLoadBalancerResult;
+import com.amazonaws.services.opsworks.model.AttachElasticLoadBalancerRequest;
 
 
 public class AWSHelper 
 {
 	private String awsAccessKey;
 	private String awsSecretKey;
+	private String httpProxy;
 	private String availabilityZone;
 
 	private BasicAWSCredentials awsCredentials;
@@ -49,7 +56,7 @@ public class AWSHelper
 	
 	public AWSHelper()
 	{
-		// Read values for awsAccessKey and awsSecretKey from config file
+		// Read values for awsAccessKey, awsSecretKey etc. from config file
 		// Throw a proper exception / log warning if cant read credentials ?
 		
 		awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
@@ -80,11 +87,77 @@ public class AWSHelper
 		}
 		catch(Exception e)
 		{
-			log.error("Could not create load balancer " + name + ".", e);
+			log.error("Could not create load balancer " + name + ".");
 			return null;
 		}
 	}
 	
+	public void registerInstancesToLoadBalancer(String loadBalancerName, List<Instance>
instances)
+	{
+		try
+		{
+			RegisterInstancesWithLoadBalancerRequest registerInstancesWithLoadBalancerRequest = new
RegisterInstancesWithLoadBalancerRequest(loadBalancerName, instances);
+		
+			AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials,
clientConfiguration);
+			
+			RegisterInstancesWithLoadBalancerResult result = lbClient.registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest);
+		}
+		catch(Exception e)
+		{
+			log.error("Could not register instances to load balancer " + loadBalancerName);
+		}
+	}
+	
+	public void deregisterInstancesFromLoadBalancer(String loadBalancerName, List<Instance>
instances)
+	{
+		try
+		{
+			DeregisterInstancesFromLoadBalancerRequest deregisterInstancesFromLoadBalancerRequest
= new DeregisterInstancesFromLoadBalancerRequest(loadBalancerName, instances);
+		
+			AmazonElasticLoadBalancingClient lbClient = new AmazonElasticLoadBalancingClient(awsCredentials,
clientConfiguration);
+			
+			DeregisterInstancesFromLoadBalancerResult result = lbClient.deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest);
+		}
+		catch(Exception e)
+		{
+			log.error("Could not de-register instances from load balancer " + loadBalancerName);
+		}
+	}
+	
+	public List<Instance> getAttachedInstances(String loadBalancerName)
+	{
+		return new ArrayList<Instance>();
+	}
+	
+	public void addListenersToLoadBalancer(String loadBalancerName, List<Listener> listeners)
+	{
+		try
+		{
+			
+		}
+		catch(Exception e)
+		{
+			log.error("Could not add listeners to load balancer " + loadBalancerName);
+		}
+	}
+	
+	public void removeListenersFromLoadBalancer(String loadBalancerName, List<Listener>
listeners)
+	{
+		try
+		{
+			
+		}
+		catch(Exception e)
+		{
+			log.error("Could not remove listeners from load balancer " + loadBalancerName);
+		}
+	}
+	
+	public List<Listener> getAttachedListeners(String loadBalancerName)
+	{
+		return new ArrayList<Listener>();
+	}
+	
 	public List<Listener> getRequiredListeners(Service service)
 	{
 		List<Listener> listeners = new ArrayList<Listener>();

http://git-wip-us.apache.org/repos/asf/stratos/blob/617e04d5/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 5e1f7b2..2e45a09 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,8 +19,13 @@
 
 package org.apache.stratos.aws.extension;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Set;
+
+import javax.management.InstanceAlreadyExistsException;
 
 import org.apache.stratos.load.balancer.common.domain.*;
 import org.apache.stratos.load.balancer.extension.api.exception.LoadBalancerExtensionException;
@@ -29,6 +34,7 @@ import org.apache.stratos.load.balancer.extension.api.LoadBalancer;
 import com.amazonaws.auth.AWSCredentials;
 import com.amazonaws.auth.BasicAWSCredentials;
 import com.amazonaws.ClientConfiguration;
+import com.amazonaws.services.elasticloadbalancing.model.Instance;
 import com.amazonaws.services.elasticloadbalancing.model.Listener;
 
 
@@ -36,13 +42,13 @@ import com.amazonaws.services.elasticloadbalancing.model.Listener;
 public class AWSLoadBalancer implements LoadBalancer {
 	
 	// A map <clusterId, load balancer id>
-	private HashMap<String, String> clusterToLoadBalancer;
+	private HashMap<String, String> clusterIdToLoadBalancerMap;
 
 	private AWSHelper awsHelper;
 	
 	public AWSLoadBalancer()
 	{
-		clusterToLoadBalancer = new HashMap<String, String>();
+		clusterIdToLoadBalancerMap = new HashMap<String, String>();
 		awsHelper = new AWSHelper();
 	}
 	
@@ -56,19 +62,110 @@ public class AWSLoadBalancer implements LoadBalancer {
                 
             	// Check if a load balancer is created for this cluster
             	
-            	if(clusterToLoadBalancer.containsKey(cluster.getClusterId()))
+            	if(clusterIdToLoadBalancerMap.containsKey(cluster.getClusterId()))
     			{
-            		// Get the load balancer
-            		// Update it 
+            		// A load balancer is already present for this cluster
+            		
+            		// Get the load balancer and update it.
+            		
+            		String loadBalancerName = clusterIdToLoadBalancerMap.get(cluster.getClusterId());
+            		
+            		// 1. Get all the instances attached
+            		//    Add/remove instances as necessary
+            		
+            		List<Instance> attachedInstances = awsHelper.getAttachedInstances(loadBalancerName);
+            		
+            		Collection<Member> clusterInstances = cluster.getMembers();
+            		
+            		List<Instance> instancesToAddToLoadBalancer = new ArrayList<Instance>();
+            		
+            		for(Member member : clusterInstances)
+            		{
+            			// if instance id of member is not in attachedInstances
+            			// 		add this to instancesToAddToLoadBalancer
+            		}
+            		
+            		List<Instance> instancesToRemoveFromLoadBalancer = new ArrayList<Instance>();
+            		
+            		for(Instance instance : attachedInstances)
+            		{
+            			if(!clusterInstances.contains(instance.getInstanceId()))
+            			{
+            				instancesToRemoveFromLoadBalancer.add(instance);
+            			}
+            		}
+            		
+            		if(instancesToRemoveFromLoadBalancer.size() > 0)
+            			awsHelper.deregisterInstancesFromLoadBalancer(loadBalancerName, instancesToRemoveFromLoadBalancer);
+            		
+            		if(instancesToAddToLoadBalancer.size() > 0)
+            			awsHelper.registerInstancesToLoadBalancer(loadBalancerName, instancesToAddToLoadBalancer);
+
+            		
+            		// 2. Get all the listeners
+            		//    Add/Remove listeners as necessary
+            		
+            		// Is it really necessary to add/remove listeners from a lb to a cluster
+            		// Need to add only if a cluster can be used for more than one service (because
a service my get added later)
+            		//                     or service port mappings may change
+     
+            		// Need to remove only if ... same for above reason
+            		
+            		List<Listener> attachedListeners = awsHelper.getAttachedListeners(loadBalancerName);
+            		
+            		List<Listener> listenersToAddToLoadBalancer = new ArrayList<Listener>();
+            		
+            		for(Listener listener : listenersForThisService)
+            		{
+            			// Need to check if Listener class supports equals method or not
+            			
+            			// if listener required for this service is not in attachedListeners
+            			// 		add this to listenersToAddToLoadBalancer
+            		}
+            		
+            		List<Listener> listenersToRemoveFromLoadBalancer = new ArrayList<Listener>();
+            		
+            		for(Listener listener : attachedListeners)
+            		{
+            			// Need to check if Listener class supports equals method or not
+            			
+            			if(!listenersForThisService.contains(listener))
+            			{
+            				listenersToRemoveFromLoadBalancer.add(listener);
+            			}
+            		}
+            		
+            		if(listenersToRemoveFromLoadBalancer.size() > 0)
+            			awsHelper.removeListenersFromLoadBalancer(loadBalancerName, listenersToRemoveFromLoadBalancer);
+            		
+            		if(listenersToAddToLoadBalancer.size() > 0)
+            			awsHelper.addListenersToLoadBalancer(loadBalancerName, listenersToAddToLoadBalancer);
+
     			}
             	else
             	{
-            		String loadBalancerName =service.getServiceName()+"-"+cluster.getClusterId();
+            		// Create a new load balancer for this cluster
+            		
+            		String loadBalancerName = service.getServiceName() + "-" + cluster.getClusterId();
             		
             		String loadBalancerDNSName = awsHelper.createLoadBalancer(loadBalancerName,
listenersForThisService);
             		
             		// register instances to LB
             		
+            		List<Instance> instances = new ArrayList<Instance>();
+            		
+            		for(Member m : cluster.getMembers())
+            		{
+            			String instanceId = ""; // of member // after making changes suggested in
mail
+            			
+            			Instance instance = new Instance();
+            			instance.setInstanceId(instanceId);
+            			
+            			instances.add(instance);
+            		}
+            		
+            		awsHelper.registerInstancesToLoadBalancer(loadBalancerName, instances);
+            		
             		// Create domain mappings
             	}
             	
@@ -86,7 +183,16 @@ public class AWSLoadBalancer implements LoadBalancer {
 		
 	}
 	     
-	public void stop() throws LoadBalancerExtensionException {
+	public void stop() throws LoadBalancerExtensionException 
+	{
+		// Remove all load balancers
+		
+		for(String loadBalancerName : clusterIdToLoadBalancerMap.values())
+		{
+			// remove load balancer
+			// Check what all needs to be done
+		}
 		
+		// Remove domain mappings
 	}	
 }


Mime
View raw message