CLOUDSTACK-1959: GSLB: add capability to provide weights to the sites

involved in the GSLB

add weights to each site participating in the GSLB. Traffic will be load
balanced across the sites based on the weigths associated with each
site. If not specified weight of site is defaulted to 1.
This commit is contained in:
Murali Reddy 2013-07-02 17:53:42 +05:30
parent 7e8234f7fe
commit 93090aa7c5
8 changed files with 87 additions and 6 deletions

View File

@ -94,6 +94,7 @@ public class ApiConstants {
public static final String GSLB_SERVICE_DOMAIN_NAME = "gslbdomainname";
public static final String GSLB_SERVICE_TYPE = "gslbservicetype";
public static final String GSLB_STICKY_SESSION_METHOD = "gslbstickysessionmethodname";
public static final String GSLB_LBRULE_WEIGHT_MAP = "gslblbruleweightsmap";
public static final String GUEST_CIDR_ADDRESS = "guestcidraddress";
public static final String GUEST_VLAN_RANGE = "guestvlanrange";
public static final String HA_ENABLE = "haenable";

View File

@ -17,8 +17,10 @@
package org.apache.cloudstack.api.command.user.region.ha.gslb;
import com.cloud.dao.EntityManager;
import com.cloud.event.EventTypes;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.network.rules.LoadBalancer;
import com.cloud.region.ha.GlobalLoadBalancerRule;
import com.cloud.region.ha.GlobalLoadBalancingRulesService;
import com.cloud.user.Account;
@ -31,7 +33,12 @@ import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.log4j.Logger;
import javax.inject.Inject;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@APICommand(name = "assignToGlobalLoadBalancerRule", description="Assign load balancer rule or list of load " +
"balancer rules to a global load balancer rules.", responseObject=SuccessResponse.class)
@ -41,6 +48,8 @@ public class AssignToGlobalLoadBalancerRuleCmd extends BaseAsyncCmd {
private static final String s_name = "assigntogloballoadbalancerruleresponse";
@Inject public EntityManager _entityMgr;
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@ -54,6 +63,12 @@ public class AssignToGlobalLoadBalancerRuleCmd extends BaseAsyncCmd {
"will be assigned to gloabal load balacner rule")
private List<Long> loadBalancerRulesIds;
@Parameter(name=ApiConstants.GSLB_LBRULE_WEIGHT_MAP, type= CommandType.MAP,
description = "Map of LB rule id's and corresponding weights (between 1-100) in the GSLB rule, if not specified weight of " +
"a LB rule is defaulted to 1. Specified as 'gslblbruleweightsmap[0].loadbalancerid=UUID" +
"&gslblbruleweightsmap[0].weight=10'", required=false)
private Map gslbLbRuleWieghtMap;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -66,6 +81,36 @@ public class AssignToGlobalLoadBalancerRuleCmd extends BaseAsyncCmd {
return loadBalancerRulesIds;
}
public Map<Long, Long> getLoadBalancerRuleWeightMap() {
Map <Long, Long> lbRuleWeightMap = new HashMap<Long, Long>();
if (gslbLbRuleWieghtMap == null || gslbLbRuleWieghtMap.isEmpty()) {
return null;
}
Collection lbruleWeightsCollection = gslbLbRuleWieghtMap.values();
Iterator iter = lbruleWeightsCollection.iterator();
while (iter.hasNext()) {
HashMap<String, String> map = (HashMap<String, String>) iter.next();
Long weight;
LoadBalancer lbrule = _entityMgr.findByUuid(LoadBalancer.class, map.get("loadbalancerid"));
if (lbrule == null) {
throw new InvalidParameterValueException("Unable to find load balancer rule with ID: " + map.get("loadbalancerid"));
}
try {
weight = Long.parseLong(map.get("weight"));
if (weight < 1 || weight > 100) {
throw new InvalidParameterValueException("Invalid weight " + weight + " given for the LB rule id: " + map.get("loadbalancerid"));
}
} catch (NumberFormatException nfe) {
throw new InvalidParameterValueException("Unable to translate weight given for the LB rule id: " + map.get("loadbalancerid"));
}
lbRuleWeightMap.put(lbrule.getId(), weight);
}
return lbRuleWeightMap;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

View File

@ -45,6 +45,9 @@ public class SiteLoadBalancerConfig {
// zone id in which site is located
long dataCenterId;
// wight corresponding to this site
long weight = 1;
public SiteLoadBalancerConfig(boolean revoked, String serviceType, String servicePublicIp, String servicePort,
long dataCenterId) {
this.revoked = revoked;
@ -118,4 +121,14 @@ public class SiteLoadBalancerConfig {
public boolean forRevoke() {
return revoked;
}
public void setWeight(long weight) {
assert(weight >= 1 && weight <= 100);
this.weight = weight;
}
public long getWeight() {
return weight;
}
}

View File

@ -40,17 +40,21 @@ public class GlobalLoadBalancerLbRuleMapVO implements InternalIdentity {
@Column(name="gslb_rule_id")
private long gslbLoadBalancerId;
@Column(name="weight")
private long weight;
@Column(name="revoke")
private boolean revoke = false;
public GlobalLoadBalancerLbRuleMapVO() {
this.weight = 1;
}
public GlobalLoadBalancerLbRuleMapVO(long loadBalancerId, long gslbLoadBalancerId) {
public GlobalLoadBalancerLbRuleMapVO(long loadBalancerId, long gslbLoadBalancerId, long weight) {
this.loadBalancerId = loadBalancerId;
this.gslbLoadBalancerId = gslbLoadBalancerId;
this.revoke = false;
this.weight = weight;
}
public long getId() {
@ -80,4 +84,12 @@ public class GlobalLoadBalancerLbRuleMapVO implements InternalIdentity {
public void setRevoke(boolean revoke) {
this.revoke = revoke;
}
public void setWeight(long weight) {
this.weight = weight;
}
public long getWeight() {
return weight;
}
}

View File

@ -902,7 +902,7 @@ public class NetscalerResource implements ServerResource {
servicePublicIp, servicePublicPort, siteName);
// Bind 'gslbservice' service object to GSLB virtual server
GSLB.createVserverServiceBinding(_netscalerService, serviceName, vserverName);
GSLB.createVserverServiceBinding(_netscalerService, serviceName, vserverName, site.getWeight());
// create a monitor for the service running on the site
GSLB.createGslbServiceMonitor(_netscalerService, servicePublicIp, serviceName);
@ -1334,13 +1334,16 @@ public class NetscalerResource implements ServerResource {
}
}
private static void createVserverServiceBinding(nitro_service client, String serviceName, String vserverName)
private static void createVserverServiceBinding(nitro_service client, String serviceName, String vserverName,
long weight)
throws ExecutionException {
String errMsg;
try {
assert(weight >= 1 && weight <= 100);
gslbvserver_gslbservice_binding binding = new gslbvserver_gslbservice_binding();
binding.set_name(vserverName);
binding.set_servicename(serviceName);
binding.set_weight(weight);
gslbvserver_gslbservice_binding.add(client, binding);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully created service: " + serviceName + " and virtual server: "

View File

@ -235,6 +235,8 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR
}
}
Map<Long, Long> lbRuleWeightMap = assignToGslbCmd.getLoadBalancerRuleWeightMap();
Transaction txn = Transaction.currentTxn();
txn.start();
@ -243,6 +245,9 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR
GlobalLoadBalancerLbRuleMapVO newGslbLbMap = new GlobalLoadBalancerLbRuleMapVO();
newGslbLbMap.setGslbLoadBalancerId(gslbRuleId);
newGslbLbMap.setLoadBalancerId(lbRuleId);
if (lbRuleWeightMap != null && lbRuleWeightMap.get(lbRuleId) != null) {
newGslbLbMap.setWeight(lbRuleWeightMap.get(lbRuleId));
}
_gslbLbMapDao.persist(newGslbLbMap);
}
@ -633,6 +638,7 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR
siteLb.setGslbProviderPublicIp(_gslbProvider.getZoneGslbProviderPublicIp(dataCenterId, physicalNetworkId));
siteLb.setGslbProviderPrivateIp(_gslbProvider.getZoneGslbProviderPrivateIp(dataCenterId, physicalNetworkId));
siteLb.setWeight(gslbLbMapVo.getWeight());
zoneSiteLoadbalancerMap.put(network.getDataCenterId(), siteLb);
}

View File

@ -748,7 +748,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase {
phyNetworkId.set(networkVo, new Long(200));
when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo);
GlobalLoadBalancerLbRuleMapVO gslbLbMap = new GlobalLoadBalancerLbRuleMapVO(1, 1);
GlobalLoadBalancerLbRuleMapVO gslbLbMap = new GlobalLoadBalancerLbRuleMapVO(1, 1, 1);
List<GlobalLoadBalancerLbRuleMapVO> listSslbLbMap = new ArrayList<GlobalLoadBalancerLbRuleMapVO>();
listSslbLbMap.add(gslbLbMap);
when(gslbServiceImpl._gslbLbMapDao.listByGslbRuleId(new Long(1))).thenReturn(listSslbLbMap);
@ -950,7 +950,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase {
when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
GlobalLoadBalancerLbRuleMapVO gslbLmMap = new GlobalLoadBalancerLbRuleMapVO(1,1);
GlobalLoadBalancerLbRuleMapVO gslbLmMap = new GlobalLoadBalancerLbRuleMapVO(1,1,1);
List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapVos = new ArrayList<GlobalLoadBalancerLbRuleMapVO>();
gslbLbMapVos.add(gslbLmMap);
when(gslbServiceImpl._gslbLbMapDao.listByGslbRuleId(new Long(1))).thenReturn(gslbLbMapVos);

View File

@ -513,6 +513,7 @@ CREATE TABLE `cloud`.`global_load_balancer_lb_rule_map` (
`id` bigint unsigned NOT NULL auto_increment,
`gslb_rule_id` bigint unsigned NOT NULL,
`lb_rule_id` bigint unsigned NOT NULL,
`weight` bigint unsigned NOT NULL DEFAULT 1 COMMENT 'weight of the site in gslb',
`revoke` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '1 is when rule is set for Revoke',
PRIMARY KEY (`id`),
UNIQUE KEY (`gslb_rule_id`, `lb_rule_id`),