Skip to content

Commit

Permalink
bug CS-14982: Make traffic sentinel metering zones configurable. Glob…
Browse files Browse the repository at this point in the history
…al config default will be used when no zones are listed while adding Traffic Sentinel Host

status CS-14982: resolved fixed
reviewed-by: Nitin

Conflicts:
	api/src/com/cloud/api/ApiConstants.java
  • Loading branch information
kishan committed Dec 17, 2012
1 parent 0f2a095 commit 0be6e2e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 16 deletions.
2 changes: 2 additions & 0 deletions api/src/com/cloud/api/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ public class ApiConstants {
public static final String S3_CONNECTION_TIMEOUT = "connectiontimeout";
public static final String S3_MAX_ERROR_RETRY = "maxerrorretry";
public static final String S3_SOCKET_TIMEOUT = "sockettimeout";
public static final String INCL_ZONES = "includezones";
public static final String EXCL_ZONES = "excludezones";

public static final String SOURCE = "source";
public static final String COUNTER_ID = "counterid";
Expand Down
23 changes: 22 additions & 1 deletion core/src/com/cloud/agent/api/DirectNetworkUsageCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ public class DirectNetworkUsageCommand extends Command {
private List<String> publicIps;
private Date start;
private Date end;
private String includeZones;
private String excludeZones;

public DirectNetworkUsageCommand(List<String> publicIps, Date start, Date end) {
public DirectNetworkUsageCommand(List<String> publicIps, Date start, Date end, String includeZones, String excludeZones) {
this.setPublicIps(publicIps);
this.setStart(start);
this.setEnd(end);
this.setIncludeZones(includeZones);
this.setExcludeZones(excludeZones);
}

@Override
Expand Down Expand Up @@ -59,4 +63,21 @@ public void setEnd(Date end) {
public Date getEnd() {
return end;
}

public String getIncludeZones() {
return includeZones;
}

public void setIncludeZones(String includeZones) {
this.includeZones = includeZones;
}

public String getExcludeZones() {
return excludeZones;
}

public void setExcludeZones(String excludeZones) {
this.excludeZones = excludeZones;
}

}
39 changes: 31 additions & 8 deletions core/src/com/cloud/network/resource/TrafficSentinelResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import com.cloud.agent.api.StartupTrafficMonitorCommand;
import com.cloud.host.Host;
import com.cloud.resource.ServerResource;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.exception.ExecutionException;

public class TrafficSentinelResource implements ServerResource {
Expand All @@ -59,8 +58,8 @@ public class TrafficSentinelResource implements ServerResource {
private String _ip;
private String _guid;
private String _url;
private static Integer _numRetries;
private static Integer _timeoutInSeconds;
private String _inclZones;
private String _exclZones;


private static final Logger s_logger = Logger.getLogger(TrafficSentinelResource.class);
Expand Down Expand Up @@ -91,9 +90,8 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
throw new ConfigurationException("Unable to find url");
}

_numRetries = NumbersUtil.parseInt((String) params.get("numRetries"), 1);

_timeoutInSeconds = NumbersUtil.parseInt((String) params.get("timeoutInSeconds"), 300);
_inclZones = (String)params.get("inclZones");
_exclZones = (String)params.get("exclZones");

return true;
} catch (Exception e) {
Expand Down Expand Up @@ -197,6 +195,15 @@ private DirectNetworkUsageAnswer getPublicIpBytesSentAndReceived(DirectNetworkUs
try {
//Direct Network Usage
URL trafficSentinel;
//Use Global include/exclude zones if there are no per TS zones
if(_inclZones == null){
_inclZones = cmd.getIncludeZones();
}

if(_exclZones == null){
_exclZones = cmd.getExcludeZones();
}

try {
//Query traffic Sentinel
trafficSentinel = new URL(_url+"/inmsf/Query?script="+URLEncoder.encode(getScript(cmd.getPublicIps(), cmd.getStart(), cmd.getEnd()),"UTF-8")
Expand Down Expand Up @@ -247,12 +254,28 @@ private String getScript(List<String> Ips, Date start, Date end){
IpAddresses += ",";
}
}
String destZoneCondition = "";
if(_inclZones !=null && !_inclZones.isEmpty()){
destZoneCondition = " & destinationzone = "+_inclZones;
}
if(_exclZones !=null && !_exclZones.isEmpty()){
destZoneCondition += " & destinationzone != "+_exclZones;
}

String srcZoneCondition = "";
if(_inclZones !=null && !_inclZones.isEmpty()){
srcZoneCondition = " & sourcezone = "+_inclZones;
}
if(_exclZones !=null && !_exclZones.isEmpty()){
srcZoneCondition += " & sourcezone != "+_exclZones;
}

String startDate = getDateString(start);
String endtDate = getDateString(end);
StringBuffer sb = new StringBuffer();
sb.append("var q = Query.topN(\"historytrmx\",");
sb.append(" \"ipsource,bytes\",");
sb.append(" \"ipsource = "+IpAddresses+" & destinationzone = EXTERNAL\",");
sb.append(" \"ipsource = "+IpAddresses+destZoneCondition+"\",");
sb.append(" \""+startDate+", "+endtDate+"\",");
sb.append(" \"bytes\",");
sb.append(" 100000);");
Expand All @@ -265,7 +288,7 @@ private String getScript(List<String> Ips, Date start, Date end){
sb.append(" });");
sb.append("var q = Query.topN(\"historytrmx\",");
sb.append(" \"ipdestination,bytes\",");
sb.append(" \"ipdestination = "+IpAddresses+" & sourcezone = EXTERNAL\",");
sb.append(" \"ipdestination = "+IpAddresses+srcZoneCondition+"\",");
sb.append(" \""+startDate+", "+endtDate+"\",");
sb.append(" \"bytes\",");
sb.append(" 100000);");
Expand Down
14 changes: 14 additions & 0 deletions server/src/com/cloud/api/commands/AddTrafficMonitorCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,25 @@ public class AddTrafficMonitorCmd extends BaseCmd {

@Parameter(name=ApiConstants.URL, type=CommandType.STRING, required = true, description="URL of the traffic monitor Host")
private String url;

@Parameter(name=ApiConstants.INCL_ZONES, type=CommandType.STRING, description="Traffic going into the listed zones will be metered")
private String inclZones;

@Parameter(name=ApiConstants.EXCL_ZONES, type=CommandType.STRING, description="Traffic going into the listed zones will not be metered")
private String exclZones;

///////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

public String getInclZones() {
return inclZones;
}

public String getExclZones() {
return exclZones;
}

public Long getZoneId() {
return zoneId;
}
Expand Down
2 changes: 2 additions & 0 deletions server/src/com/cloud/configuration/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ public enum Config {
DirectNetworkStatsInterval("Usage", ManagementServer.class, Integer.class, "direct.network.stats.interval", "86400", "Interval (in seconds) to collect stats from Traffic Monitor", null),
UsageSanityCheckInterval("Usage", ManagementServer.class, Integer.class, "usage.sanity.check.interval", null, "Interval (in days) to check sanity of usage data", null),
UsageAggregationTimezone("Usage", ManagementServer.class, String.class, "usage.aggregation.timezone", "GMT", "The timezone to use for usage stats aggregation", null),
TrafficSentinelIncludeZones("Usage", ManagementServer.class, Integer.class, "traffic.sentinel.include.zones", "EXTERNAL", "Traffic going into specified list of zones is metered. For metering all traffic leave this parameter empty", null),
TrafficSentinelExcludeZones("Usage", ManagementServer.class, Integer.class, "traffic.sentinel.exclude.zones", "", "Traffic going into specified list of zones is not metered.", null),

// Hidden
UseSecondaryStorageVm("Hidden", ManagementServer.class, Boolean.class, "secondary.storage.vm", "false", "Deploys a VM per zone to manage secondary storage if true, otherwise secondary storage is mounted on management server", null),
Expand Down
28 changes: 21 additions & 7 deletions server/src/com/cloud/network/NetworkUsageManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public enum NetworkUsageResourceName {
@Inject ResourceManager _resourceMgr;
ScheduledExecutorService _executor;
int _networkStatsInterval;
String _TSinclZones;
String _TSexclZones;
protected SearchBuilder<IPAddressVO> AllocatedIpSearch;

@Override
Expand Down Expand Up @@ -148,8 +150,8 @@ public Host addTrafficMonitor(AddTrafficMonitorCmd cmd) {
hostParams.put("zone", String.valueOf(zoneId));
hostParams.put("ipaddress", ipAddress);
hostParams.put("url", cmd.getUrl());
//hostParams("numRetries", numRetries);
//hostParams("timeout", timeout);
hostParams.put("inclZones", (cmd.getInclZones() != null) ? cmd.getInclZones() : _TSinclZones);
hostParams.put("exclZones", (cmd.getExclZones() != null) ? cmd.getExclZones() : _TSexclZones);
hostParams.put("guid", guid);
hostParams.put("name", guid);

Expand All @@ -162,7 +164,14 @@ public Host addTrafficMonitor(AddTrafficMonitorCmd cmd) {
Map<String, String> hostDetails = new HashMap<String, String>();
hostDetails.put("url", cmd.getUrl());
hostDetails.put("last_collection", ""+System.currentTimeMillis());

if(cmd.getInclZones() != null){
hostDetails.put("inclZones", cmd.getInclZones());
}
if(cmd.getExclZones() != null){
hostDetails.put("exclZones", cmd.getExclZones());
}


Host trafficMonitor = _resourceMgr.addHost(zoneId, resource, Host.Type.TrafficMonitor, hostDetails);
return trafficMonitor;
}
Expand Down Expand Up @@ -222,6 +231,8 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
AllocatedIpSearch.done();

_networkStatsInterval = NumbersUtil.parseInt(_configDao.getValue(Config.DirectNetworkStatsInterval.key()), 86400);
_TSinclZones = _configDao.getValue(Config.TrafficSentinelIncludeZones.key());
_TSexclZones = _configDao.getValue(Config.TrafficSentinelExcludeZones.key());
_agentMgr.registerForHostEvents(new DirectNetworkStatsListener( _networkStatsInterval), true, false, false);
_resourceMgr.registerResourceStateAdapter(this.getClass().getSimpleName(), this);
return true;
Expand Down Expand Up @@ -372,7 +383,7 @@ private boolean collectDirectNetworkUsage(HostVO host){

//Get usage for Ips which were assigned for the entire duration
if(fullDurationIpUsage.size() > 0){
DirectNetworkUsageCommand cmd = new DirectNetworkUsageCommand(IpList, lastCollection, now);
DirectNetworkUsageCommand cmd = new DirectNetworkUsageCommand(IpList, lastCollection, now, _TSinclZones, _TSexclZones);
DirectNetworkUsageAnswer answer = (DirectNetworkUsageAnswer) _agentMgr.easySend(host.getId(), cmd);
if (answer == null || !answer.getResult()) {
String details = (answer != null) ? answer.getDetails() : "details unavailable";
Expand Down Expand Up @@ -405,7 +416,7 @@ private boolean collectDirectNetworkUsage(HostVO host){
for(UsageIPAddressVO usageIp : IpPartialUsage){
IpList = new ArrayList<String>() ;
IpList.add(usageIp.getAddress());
DirectNetworkUsageCommand cmd = new DirectNetworkUsageCommand(IpList, usageIp.getAssigned(), usageIp.getReleased());
DirectNetworkUsageCommand cmd = new DirectNetworkUsageCommand(IpList, usageIp.getAssigned(), usageIp.getReleased(), _TSinclZones, _TSexclZones);
DirectNetworkUsageAnswer answer = (DirectNetworkUsageAnswer) _agentMgr.easySend(host.getId(), cmd);
if (answer == null || !answer.getResult()) {
String details = (answer != null) ? answer.getDetails() : "details unavailable";
Expand Down Expand Up @@ -532,8 +543,11 @@ public HostVO createHostVOForDirectConnectAgent(HostVO host, StartupCommand[] st

@Override
public DeleteHostAnswer deleteHost(HostVO host, boolean isForced, boolean isForceDeleteStorage) throws UnableDeleteHostException {
// TODO Auto-generated method stub
return null;
if(host.getType() != Host.Type.TrafficMonitor){
return null;
}

return new DeleteHostAnswer(true);
}

}

0 comments on commit 0be6e2e

Please sign in to comment.