You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by is...@apache.org on 2015/11/10 10:06:56 UTC
[05/12] stratos git commit: formatting code
http://git-wip-us.apache.org/repos/asf/stratos/blob/8ccdeb6d/extensions/load-balancer/modules/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
----------------------------------------------------------------------
diff --git a/extensions/load-balancer/modules/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java b/extensions/load-balancer/modules/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
index ca374c9..32ec919 100644
--- a/extensions/load-balancer/modules/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
+++ b/extensions/load-balancer/modules/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
@@ -41,356 +41,357 @@ import java.util.concurrent.ConcurrentHashMap;
public class AWSLoadBalancer implements LoadBalancer {
- private static final Log log = LogFactory.getLog(AWSLoadBalancer.class);
+ private static final Log log = LogFactory.getLog(AWSLoadBalancer.class);
- // 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>();
+ // 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;
+ // Object used to invoke methods related to AWS API
+ private AWSHelper awsHelper;
- // PersistenceManager: used to persist LB Information by tuples of <lb name, cluster id, region>
- PersistenceManager persistenceManager;
+ // PersistenceManager: used to persist LB Information by tuples of <lb name, cluster id, region>
+ PersistenceManager persistenceManager;
- public AWSLoadBalancer() throws LoadBalancerExtensionException {
- awsHelper = new AWSHelper();
- persistenceManager = new FileBasedPersistenceManager();
- initialize();
- }
+ public AWSLoadBalancer() throws LoadBalancerExtensionException {
+ awsHelper = new AWSHelper();
+ persistenceManager = new FileBasedPersistenceManager();
+ initialize();
+ }
- /*
- * configure method iterates over topology and configures the AWS load
- * balancers needed. Configuration may involve creating a new load balancer
- * for a cluster, updating existing load balancers or deleting unwanted load
- * balancers.
- */
- public boolean configure(Topology topology)
- throws LoadBalancerExtensionException {
+ /*
+ * configure method iterates over topology and configures the AWS load
+ * balancers needed. Configuration may involve creating a new load balancer
+ * for a cluster, updating existing load balancers or deleting unwanted load
+ * balancers.
+ */
+ public boolean configure(Topology topology)
+ throws LoadBalancerExtensionException {
- log.info("AWS load balancer extension is being reconfigured.");
+ log.info("AWS load balancer extension is being reconfigured.");
- HashSet<String> activeClusters = new HashSet<String>();
+ HashSet<String> activeClusters = new HashSet<String>();
- for (Service service : topology.getServices()) {
- for (Cluster cluster : service.getClusters()) {
- // Check if a load balancer is created for this cluster
- if (clusterIdToLoadBalancerMap.containsKey(cluster.getClusterId())) {
- // A load balancer is already present for this cluster
- // Get the load balancer and update it.
+ for (Service service : topology.getServices()) {
+ for (Cluster cluster : service.getClusters()) {
+ // Check if a load balancer is created for this cluster
+ if (clusterIdToLoadBalancerMap.containsKey(cluster.getClusterId())) {
+ // A load balancer is already present for this cluster
+ // Get the load balancer and update it.
- if (log.isDebugEnabled()) {
- log.debug("Load balancer for cluster " + cluster.getClusterId() + " is already present.");
- }
+ if (log.isDebugEnabled()) {
+ log.debug("Load balancer for cluster " + cluster.getClusterId() + " is already present.");
+ }
- LoadBalancerInfo loadBalancerInfo = clusterIdToLoadBalancerMap.get(cluster.getClusterId());
+ LoadBalancerInfo loadBalancerInfo = clusterIdToLoadBalancerMap.get(cluster.getClusterId());
- String loadBalancerName = loadBalancerInfo.getName();
- String region = loadBalancerInfo.getRegion();
+ String loadBalancerName = loadBalancerInfo.getName();
+ String region = loadBalancerInfo.getRegion();
- // Get all the instances attached - Attach newly added instances to load balancer
+ // Get all the instances attached - Attach newly added instances to load balancer
- // attachedInstances list is useful in finding out what are the new instances which
- // should be attached to this load balancer.
- List<Instance> attachedInstances = awsHelper.getAttachedInstances(loadBalancerName, region);
+ // attachedInstances list is useful in finding out what are the new instances which
+ // should be attached to this load balancer.
+ List<Instance> attachedInstances = awsHelper.getAttachedInstances(loadBalancerName, region);
- // clusterMembers stores all the members of a cluster.
- Collection<Member> clusterMembers = cluster.getMembers();
+ // clusterMembers stores all the members of a cluster.
+ Collection<Member> clusterMembers = cluster.getMembers();
- if (clusterMembers.size() > 0) {
- activeClusters.add(cluster.getClusterId());
+ if (clusterMembers.size() > 0) {
+ activeClusters.add(cluster.getClusterId());
- List<Instance> instancesToAddToLoadBalancer = new ArrayList<Instance>();
- List<String> availabilityZones = new ArrayList<String>();
+ List<Instance> instancesToAddToLoadBalancer = new ArrayList<Instance>();
+ List<String> availabilityZones = new ArrayList<String>();
- for (Member member : clusterMembers) {
- // if instance id of member is not in
- // attachedInstances
- // add this to instancesToAddToLoadBalancer
- Instance instance = new Instance(awsHelper.getAWSInstanceName(member.getInstanceId()));
+ for (Member member : clusterMembers) {
+ // if instance id of member is not in
+ // attachedInstances
+ // add this to instancesToAddToLoadBalancer
+ Instance instance = new Instance(awsHelper.getAWSInstanceName(member.getInstanceId()));
- if (attachedInstances == null || !attachedInstances.contains(instance)) {
- instancesToAddToLoadBalancer.add(instance);
+ if (attachedInstances == null || !attachedInstances.contains(instance)) {
+ instancesToAddToLoadBalancer.add(instance);
- if (log.isDebugEnabled()) {
- log.debug("Instance " + awsHelper.getAWSInstanceName(member.getInstanceId()) +
+ if (log.isDebugEnabled()) {
+ log.debug("Instance " + awsHelper.getAWSInstanceName(member.getInstanceId()) +
" needs to be registered to load balancer " + loadBalancerName);
- }
-
- // LB Common Member has a property 'EC2_AVAILABILITY_ZONE' points to the ec2 availability zone
- // for this member. Use the property value to update the LB about the relevant zone
- String availabilityZone = getEC2AvaialbilityZoneOfMember(member);
- if (availabilityZone != null) {
- availabilityZones.add(availabilityZone);
- }
- }
- }
-
- if (instancesToAddToLoadBalancer.size() > 0) {
- awsHelper.registerInstancesToLoadBalancer(
- loadBalancerName,
- instancesToAddToLoadBalancer, region);
- }
-
- // update LB with the zones
- if (!availabilityZones.isEmpty()) {
- awsHelper.addAvailabilityZonesForLoadBalancer(loadBalancerName, availabilityZones, region);
- }
- }
-
- } else {
- // Create a new load balancer for this cluster
- Collection<Member> clusterMembers = cluster.getMembers();
-
- if (clusterMembers.size() > 0) {
- Member aMember = clusterMembers.iterator().next();
-
- // a unique load balancer name with user-defined prefix and a sequence number.
- String loadBalancerName = awsHelper.generateLoadBalancerName(cluster.getServiceName());
-
- String region = awsHelper.getAWSRegion(aMember.getInstanceId());
-
- // list of AWS listeners obtained using port mappings of one of the members of the cluster.
- List<Listener> listenersForThisCluster = awsHelper.getRequiredListeners(aMember);
-
- // Get the zone from the first member and use in LB creation. Zone will be updated for each member below
- String initialAvailabilityZone = getEC2AvaialbilityZoneOfMember(aMember);
- if (initialAvailabilityZone == null) {
- // could not get the availability zone from the Member property 'EC2_AVAILABILITY_ZONE'
- // use the default (<region>a)
- initialAvailabilityZone = awsHelper.getAvailabilityZoneFromRegion(region);
- }
-
- // Returns DNS name of load balancer which was created.
- // This is used in the domain mapping of this cluster.
- String loadBalancerDNSName = awsHelper.createLoadBalancer(loadBalancerName, listenersForThisCluster,
+ }
+
+ // LB Common Member has a property 'EC2_AVAILABILITY_ZONE' points to the ec2 availability zone
+ // for this member. Use the property value to update the LB about the relevant zone
+ String availabilityZone = getEC2AvaialbilityZoneOfMember(member);
+ if (availabilityZone != null) {
+ availabilityZones.add(availabilityZone);
+ }
+ }
+ }
+
+ if (instancesToAddToLoadBalancer.size() > 0) {
+ awsHelper.registerInstancesToLoadBalancer(
+ loadBalancerName,
+ instancesToAddToLoadBalancer, region);
+ }
+
+ // update LB with the zones
+ if (!availabilityZones.isEmpty()) {
+ awsHelper.addAvailabilityZonesForLoadBalancer(loadBalancerName, availabilityZones, region);
+ }
+ }
+
+ } else {
+ // Create a new load balancer for this cluster
+ Collection<Member> clusterMembers = cluster.getMembers();
+
+ if (clusterMembers.size() > 0) {
+ Member aMember = clusterMembers.iterator().next();
+
+ // a unique load balancer name with user-defined prefix and a sequence number.
+ String loadBalancerName = awsHelper.generateLoadBalancerName(cluster.getServiceName());
+
+ String region = awsHelper.getAWSRegion(aMember.getInstanceId());
+
+ // list of AWS listeners obtained using port mappings of one of the members of the cluster.
+ List<Listener> listenersForThisCluster = awsHelper.getRequiredListeners(aMember);
+
+ // Get the zone from the first member and use in LB creation. Zone will be updated for each member below
+ String initialAvailabilityZone = getEC2AvaialbilityZoneOfMember(aMember);
+ if (initialAvailabilityZone == null) {
+ // could not get the availability zone from the Member property 'EC2_AVAILABILITY_ZONE'
+ // use the default (<region>a)
+ initialAvailabilityZone = awsHelper.getAvailabilityZoneFromRegion(region);
+ }
+
+ // Returns DNS name of load balancer which was created.
+ // This is used in the domain mapping of this cluster.
+ String loadBalancerDNSName = awsHelper.createLoadBalancer(loadBalancerName, listenersForThisCluster,
region, initialAvailabilityZone, AWSExtensionContext.getInstance().isOperatingInVPC());
- // enable connection draining (default) and cross zone load balancing (if specified in aws-extension.sh)
- awsHelper.modifyLBAttributes(loadBalancerName, region, AWSExtensionContext.getInstance().
+ // enable connection draining (default) and cross zone load balancing (if specified in aws-extension.sh)
+ awsHelper.modifyLBAttributes(loadBalancerName, region, AWSExtensionContext.getInstance().
isCrossZoneLoadBalancingEnabled(), true);
- // 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 in the security group.
- for (Listener listener : listenersForThisCluster) {
- int port = listener.getLoadBalancerPort();
+ // 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 in the security group.
+ for (Listener listener : listenersForThisCluster) {
+ int port = listener.getLoadBalancerPort();
- for (String protocol : awsHelper.getAllowedProtocolsForLBSecurityGroup()) {
- awsHelper.addInboundRuleToSecurityGroup(awsHelper.getSecurityGroupId(awsHelper
- .getLbSecurityGroupName(), region), region, protocol, port);
- }
- }
+ for (String protocol : awsHelper.getAllowedProtocolsForLBSecurityGroup()) {
+ awsHelper.addInboundRuleToSecurityGroup(awsHelper.getSecurityGroupId(awsHelper
+ .getLbSecurityGroupName(), region), region, protocol, port);
+ }
+ }
- log.info("Load balancer '" + loadBalancerDNSName + "' created for cluster '" + cluster.getClusterId());
+ log.info("Load balancer '" + loadBalancerDNSName + "' created for cluster '" + cluster.getClusterId());
- // Register instances in the cluster to load balancer
- List<Instance> instances = new ArrayList<Instance>();
- List<String> availabilityZones = new ArrayList<String>();
+ // Register instances in the cluster to load balancer
+ List<Instance> instances = new ArrayList<Instance>();
+ List<String> availabilityZones = new ArrayList<String>();
- for (Member member : clusterMembers) {
- String instanceId = member.getInstanceId();
+ for (Member member : clusterMembers) {
+ String instanceId = member.getInstanceId();
- if (log.isDebugEnabled()) {
- log.debug("Instance " + awsHelper.getAWSInstanceName(instanceId) + " needs to be registered to load balancer "
+ if (log.isDebugEnabled()) {
+ log.debug("Instance " + awsHelper.getAWSInstanceName(instanceId) + " needs to be registered to load balancer "
+ loadBalancerName);
- }
-
- Instance instance = new Instance();
- instance.setInstanceId(awsHelper.getAWSInstanceName(instanceId));
-
- instances.add(instance);
- // LB Common Member has a property 'EC2_AVAILABILITY_ZONE' which points to the ec2 availability
- // zone for this member. Use the property value to update the LB about the relevant zone
- String availabilityZone = getEC2AvaialbilityZoneOfMember(member);
- if (availabilityZone != null) {
- availabilityZones.add(availabilityZone);
- }
- }
-
- awsHelper.registerInstancesToLoadBalancer(loadBalancerName, instances, region);
-
- // update LB with the zones
- if (!availabilityZones.isEmpty()) {
- awsHelper.addAvailabilityZonesForLoadBalancer(loadBalancerName, availabilityZones, region);
- }
-
- // add stickiness policy
- if (awsHelper.getAppStickySessionCookie() != null && !awsHelper.getAppStickySessionCookie().isEmpty()) {
- CreateAppCookieStickinessPolicyResult result = awsHelper.createStickySessionPolicy(loadBalancerName,
+ }
+
+ Instance instance = new Instance();
+ instance.setInstanceId(awsHelper.getAWSInstanceName(instanceId));
+
+ instances.add(instance);
+ // LB Common Member has a property 'EC2_AVAILABILITY_ZONE' which points to the ec2 availability
+ // zone for this member. Use the property value to update the LB about the relevant zone
+ String availabilityZone = getEC2AvaialbilityZoneOfMember(member);
+ if (availabilityZone != null) {
+ availabilityZones.add(availabilityZone);
+ }
+ }
+
+ awsHelper.registerInstancesToLoadBalancer(loadBalancerName, instances, region);
+
+ // update LB with the zones
+ if (!availabilityZones.isEmpty()) {
+ awsHelper.addAvailabilityZonesForLoadBalancer(loadBalancerName, availabilityZones, region);
+ }
+
+ // add stickiness policy
+ if (awsHelper.getAppStickySessionCookie() != null && !awsHelper.getAppStickySessionCookie().isEmpty()) {
+ CreateAppCookieStickinessPolicyResult result = awsHelper.createStickySessionPolicy(loadBalancerName,
awsHelper.getAppStickySessionCookie(), Constants.STICKINESS_POLICY, region);
- if (result != null) {
- // Take a single port mapping from a member, and apply the policy for
- // the LB Listener port (Proxy port of the port mapping)
- awsHelper.applyPolicyToLBListenerPorts(aMember.getPorts(), loadBalancerName,
+ if (result != null) {
+ // Take a single port mapping from a member, and apply the policy for
+ // the LB Listener port (Proxy port of the port mapping)
+ awsHelper.applyPolicyToLBListenerPorts(aMember.getPorts(), loadBalancerName,
Constants.STICKINESS_POLICY, region);
- }
- }
-
- // persist LB info
- try {
- persistenceManager.persist(new LBInfoDTO(loadBalancerName, cluster.getClusterId(), region));
-
- } catch (PersistenceException e) {
- log.error("Unable to persist LB Information for " + loadBalancerName + ", cluster id " +
- cluster.getClusterId());
- }
-
- LoadBalancerInfo loadBalancerInfo = new LoadBalancerInfo(
- loadBalancerName, region);
-
- clusterIdToLoadBalancerMap.put(cluster.getClusterId(),
- loadBalancerInfo);
- activeClusters.add(cluster.getClusterId());
- }
-
- pause(3000);
- }
- }
- }
-
- // if 'terminate.lb.on.cluster.removal' = true in aws-extension.sh
- if (AWSExtensionContext.getInstance().terminateLBOnClusterRemoval()) {
-
- // Find out clusters which were present earlier but are not now.
- List<String> clustersToRemoveFromMap = new ArrayList<String>();
- // TODO: improve using an iterator and removing the unwanted cluster id in this loop
- for (String clusterId : clusterIdToLoadBalancerMap.keySet()) {
- if (!activeClusters.contains(clusterId)) {
- clustersToRemoveFromMap.add(clusterId);
-
- if (log.isDebugEnabled()) {
- log.debug("Load balancer for cluster " + clusterId
- + " needs to be removed.");
- }
-
- }
- }
-
-
- // Delete load balancers associated with these clusters.
- for (String clusterId : clustersToRemoveFromMap) {
- // Remove load balancer for this cluster.
- final String loadBalancerName = clusterIdToLoadBalancerMap.get(clusterId).getName();
- final String region = clusterIdToLoadBalancerMap.get(clusterId).getRegion();
- awsHelper.deleteLoadBalancer(loadBalancerName, region);
- //remove and persist
- try {
- persistenceManager.remove(new LBInfoDTO(loadBalancerName, clusterId, region));
-
- } catch (PersistenceException e) {
- log.error("Unable to persist LB Information for " + loadBalancerName + ", cluster id " +
- clusterId);
- }
- clusterIdToLoadBalancerMap.remove(clusterId);
- }
- }
-
- activeClusters.clear();
- log.info("AWS load balancer extension was reconfigured as per the topology.");
- return true;
- }
-
- private String getEC2AvaialbilityZoneOfMember(Member member) {
- if (member.getProperties() != null) {
+ }
+ }
+
+ // persist LB info
+ try {
+ persistenceManager.persist(new LBInfoDTO(loadBalancerName, cluster.getClusterId(), region));
+
+ } catch (PersistenceException e) {
+ log.error("Unable to persist LB Information for " + loadBalancerName + ", cluster id " +
+ cluster.getClusterId());
+ }
+
+ LoadBalancerInfo loadBalancerInfo = new LoadBalancerInfo(
+ loadBalancerName, region);
+
+ clusterIdToLoadBalancerMap.put(cluster.getClusterId(),
+ loadBalancerInfo);
+ activeClusters.add(cluster.getClusterId());
+ }
+
+ pause(3000);
+ }
+ }
+ }
+
+ // if 'terminate.lb.on.cluster.removal' = true in aws-extension.sh
+ if (AWSExtensionContext.getInstance().terminateLBOnClusterRemoval()) {
+
+ // Find out clusters which were present earlier but are not now.
+ List<String> clustersToRemoveFromMap = new ArrayList<String>();
+ // TODO: improve using an iterator and removing the unwanted cluster id in this loop
+ for (String clusterId : clusterIdToLoadBalancerMap.keySet()) {
+ if (!activeClusters.contains(clusterId)) {
+ clustersToRemoveFromMap.add(clusterId);
+
+ if (log.isDebugEnabled()) {
+ log.debug("Load balancer for cluster " + clusterId
+ + " needs to be removed.");
+ }
+
+ }
+ }
+
+
+ // Delete load balancers associated with these clusters.
+ for (String clusterId : clustersToRemoveFromMap) {
+ // Remove load balancer for this cluster.
+ final String loadBalancerName = clusterIdToLoadBalancerMap.get(clusterId).getName();
+ final String region = clusterIdToLoadBalancerMap.get(clusterId).getRegion();
+ awsHelper.deleteLoadBalancer(loadBalancerName, region);
+ //remove and persist
+ try {
+ persistenceManager.remove(new LBInfoDTO(loadBalancerName, clusterId, region));
+
+ } catch (PersistenceException e) {
+ log.error("Unable to persist LB Information for " + loadBalancerName + ", cluster id " +
+ clusterId);
+ }
+ clusterIdToLoadBalancerMap.remove(clusterId);
+ }
+ }
+
+ activeClusters.clear();
+ log.info("AWS load balancer extension was reconfigured as per the topology.");
+ return true;
+ }
+
+ private String getEC2AvaialbilityZoneOfMember(Member member) {
+ if (member.getProperties() != null) {
return member.getProperties().getProperty(Constants.EC2_AVAILABILITY_ZONE_PROPERTY);
}
- return null;
- }
-
- /*
- * start method is called after extension if configured first time. Does
- * nothing but logs the message.
- */
- public void start() throws LoadBalancerExtensionException {
-
- log.info("AWS load balancer extension started.");
- }
-
- private void initialize() {
- // load persisted LB information
- Set<LBInfoDTO> lbInfo = null;
- try {
- lbInfo = persistenceManager.retrieve();
-
- } catch (PersistenceException e) {
- log.error("Unable to retrieve persisted LB Information", e);
- }
-
- if (lbInfo != null) {
- for (LBInfoDTO lbInfoDTO : lbInfo) {
- LoadBalancerDescription lbDesc = awsHelper.getLoadBalancerDescription(lbInfoDTO.getName(),
- lbInfoDTO.getRegion());
- if (lbDesc != null) {
- clusterIdToLoadBalancerMap.put(lbInfoDTO.getClusterId(), new LoadBalancerInfo(lbInfoDTO.getName(),
- lbInfoDTO.getRegion()));
- } else {
- // make debug
- if (log.isInfoEnabled()) {
- log.info("Unable to locate LB " + lbInfoDTO.getName());
- }
- // remove the persisted entry
- try {
- persistenceManager.remove(new LBInfoDTO(lbInfoDTO.getName(), lbInfoDTO.getClusterId(), lbInfoDTO.getRegion()));
-
- } catch (PersistenceException e) {
- log.error("Unable to remove persisted LB Information", e);
- }
- }
-
- }
- }
- }
-
- /*
- * reload method is called every time after extension if configured. Does
- * nothing but logs the message.
- */
- public void reload() throws LoadBalancerExtensionException {
- // Check what is appropriate to do here.
- log.info("AWS load balancer extension reloaded.");
- }
-
- /*
- * stop method deletes load balancers for all clusters in the topology.
- */
- public void stop() throws LoadBalancerExtensionException {
- // Remove all load balancers if 'terminate.lbs.on.extension.stop' = true in aws-extension.sh
- if (AWSExtensionContext.getInstance().terminateLBsOnExtensionStop()) {
- for (Map.Entry<String, LoadBalancerInfo> lbInfoEntry : clusterIdToLoadBalancerMap
- .entrySet()) {
- // Remove load balancer
- awsHelper.deleteLoadBalancer(lbInfoEntry.getValue().getName(),
- lbInfoEntry.getValue().getRegion());
-
- // remove the persisted entry
- try {
- persistenceManager.remove(new LBInfoDTO(lbInfoEntry.getValue().getName(), lbInfoEntry.getKey(),
- lbInfoEntry.getValue().getRegion()));
-
- } catch (PersistenceException e) {
- log.error("Unable to remove persisted LB Information", e);
- }
- }
- } else {
- if (log.isInfoEnabled()) {
- log.info("Not terminating LBs since terminate.lbs.on.extension.stop=false");
- }
- }
- }
-
- private static void pause (long duration) {
- // sleep to stop AWS Rate Exceeding: Caused by: com.amazonaws.AmazonServiceException: Rate exceeded
- // (Service: AmazonElasticLoadBalancing; Status Code: 400; Error Code: Throttling; Request ID: xxx-xxx)
- try {
- Thread.sleep(duration);
- } catch (InterruptedException ignored) {}
- }
-
- public static ConcurrentHashMap<String, LoadBalancerInfo> getClusterIdToLoadBalancerMap() {
- return clusterIdToLoadBalancerMap;
- }
+ return null;
+ }
+
+ /*
+ * start method is called after extension if configured first time. Does
+ * nothing but logs the message.
+ */
+ public void start() throws LoadBalancerExtensionException {
+
+ log.info("AWS load balancer extension started.");
+ }
+
+ private void initialize() {
+ // load persisted LB information
+ Set<LBInfoDTO> lbInfo = null;
+ try {
+ lbInfo = persistenceManager.retrieve();
+
+ } catch (PersistenceException e) {
+ log.error("Unable to retrieve persisted LB Information", e);
+ }
+
+ if (lbInfo != null) {
+ for (LBInfoDTO lbInfoDTO : lbInfo) {
+ LoadBalancerDescription lbDesc = awsHelper.getLoadBalancerDescription(lbInfoDTO.getName(),
+ lbInfoDTO.getRegion());
+ if (lbDesc != null) {
+ clusterIdToLoadBalancerMap.put(lbInfoDTO.getClusterId(), new LoadBalancerInfo(lbInfoDTO.getName(),
+ lbInfoDTO.getRegion()));
+ } else {
+ // make debug
+ if (log.isInfoEnabled()) {
+ log.info("Unable to locate LB " + lbInfoDTO.getName());
+ }
+ // remove the persisted entry
+ try {
+ persistenceManager.remove(new LBInfoDTO(lbInfoDTO.getName(), lbInfoDTO.getClusterId(), lbInfoDTO.getRegion()));
+
+ } catch (PersistenceException e) {
+ log.error("Unable to remove persisted LB Information", e);
+ }
+ }
+
+ }
+ }
+ }
+
+ /*
+ * reload method is called every time after extension if configured. Does
+ * nothing but logs the message.
+ */
+ public void reload() throws LoadBalancerExtensionException {
+ // Check what is appropriate to do here.
+ log.info("AWS load balancer extension reloaded.");
+ }
+
+ /*
+ * stop method deletes load balancers for all clusters in the topology.
+ */
+ public void stop() throws LoadBalancerExtensionException {
+ // Remove all load balancers if 'terminate.lbs.on.extension.stop' = true in aws-extension.sh
+ if (AWSExtensionContext.getInstance().terminateLBsOnExtensionStop()) {
+ for (Map.Entry<String, LoadBalancerInfo> lbInfoEntry : clusterIdToLoadBalancerMap
+ .entrySet()) {
+ // Remove load balancer
+ awsHelper.deleteLoadBalancer(lbInfoEntry.getValue().getName(),
+ lbInfoEntry.getValue().getRegion());
+
+ // remove the persisted entry
+ try {
+ persistenceManager.remove(new LBInfoDTO(lbInfoEntry.getValue().getName(), lbInfoEntry.getKey(),
+ lbInfoEntry.getValue().getRegion()));
+
+ } catch (PersistenceException e) {
+ log.error("Unable to remove persisted LB Information", e);
+ }
+ }
+ } else {
+ if (log.isInfoEnabled()) {
+ log.info("Not terminating LBs since terminate.lbs.on.extension.stop=false");
+ }
+ }
+ }
+
+ private static void pause(long duration) {
+ // sleep to stop AWS Rate Exceeding: Caused by: com.amazonaws.AmazonServiceException: Rate exceeded
+ // (Service: AmazonElasticLoadBalancing; Status Code: 400; Error Code: Throttling; Request ID: xxx-xxx)
+ try {
+ Thread.sleep(duration);
+ } catch (InterruptedException ignored) {
+ }
+ }
+
+ public static ConcurrentHashMap<String, LoadBalancerInfo> getClusterIdToLoadBalancerMap() {
+ return clusterIdToLoadBalancerMap;
+ }
}
/**
@@ -399,19 +400,19 @@ public class AWSLoadBalancer implements LoadBalancer {
* balancer.
*/
class LoadBalancerInfo {
- private String name;
- private String region;
+ private String name;
+ private String region;
- public LoadBalancerInfo(String name, String region) {
- this.name = name;
- this.region = region;
- }
+ public LoadBalancerInfo(String name, String region) {
+ this.name = name;
+ this.region = region;
+ }
- public String getName() {
- return name;
- }
+ public String getName() {
+ return name;
+ }
- public String getRegion() {
- return region;
- }
+ public String getRegion() {
+ return region;
+ }
}