Skip to content

Commit

Permalink
[Profiling] Rename request param custom_cost_factor into aws_cost_fac…
Browse files Browse the repository at this point in the history
  • Loading branch information
rockdaboot authored and timgrein committed Nov 30, 2023
1 parent 0977883 commit 386bf84
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ final class CostCalculator {
private static final double SECONDS_PER_HOUR = 60 * 60;
private static final double SECONDS_PER_YEAR = SECONDS_PER_HOUR * 24 * 365.0d; // unit: seconds
private static final double DEFAULT_COST_USD_PER_CORE_HOUR = 0.0425d; // unit: USD / (core * hour)
private static final double DEFAULT_CUSTOM_COST_FACTOR = 1.0d;
private static final double DEFAULT_AWS_COST_FACTOR = 1.0d;
private final InstanceTypeService instanceTypeService;
private final Map<String, HostMetadata> hostMetadata;
private final double samplingDurationInSeconds;
private final double customCostFactor;
private final double awsCostFactor;

CostCalculator(
InstanceTypeService instanceTypeService,
Map<String, HostMetadata> hostMetadata,
double samplingDurationInSeconds,
Double customCostFactor
Double awsCostFactor
) {
this.instanceTypeService = instanceTypeService;
this.hostMetadata = hostMetadata;
this.samplingDurationInSeconds = samplingDurationInSeconds > 0 ? samplingDurationInSeconds : 1.0d; // avoid division by zero
this.customCostFactor = customCostFactor == null ? DEFAULT_CUSTOM_COST_FACTOR : customCostFactor;
this.awsCostFactor = awsCostFactor == null ? DEFAULT_AWS_COST_FACTOR : awsCostFactor;
}

public double annualCostsUSD(String hostID, double samples) {
Expand All @@ -40,12 +40,14 @@ public double annualCostsUSD(String hostID, double samples) {
return annualCoreHours * DEFAULT_COST_USD_PER_CORE_HOUR;
}

double providerCostFactor = host.instanceType.provider.equals("aws") ? awsCostFactor : 1.0d;

CostEntry costs = instanceTypeService.getCosts(host.instanceType);
if (costs == null) {
return annualCoreHours * DEFAULT_COST_USD_PER_CORE_HOUR;
return annualCoreHours * DEFAULT_COST_USD_PER_CORE_HOUR * providerCostFactor;
}

return annualCoreHours * costs.costFactor * customCostFactor;
return annualCoreHours * costs.costFactor * providerCostFactor;
}

public static double annualCoreHours(double duration, double samples, double samplingFrequency) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class GetStackTracesRequest extends ActionRequest implements IndicesReque
public static final ParseField INDICES_FIELD = new ParseField("indices");
public static final ParseField STACKTRACE_IDS_FIELD = new ParseField("stacktrace_ids");
public static final ParseField REQUESTED_DURATION_FIELD = new ParseField("requested_duration");
public static final ParseField CUSTOM_COST_FACTOR_FIELD = new ParseField("custom_cost_factor");
public static final ParseField AWS_COST_FACTOR_FIELD = new ParseField("aws_cost_factor");
public static final ParseField CUSTOM_CO2_PER_KWH = new ParseField("co2_per_kwh");
public static final ParseField CUSTOM_DATACENTER_PUE = new ParseField("datacenter_pue");
public static final ParseField CUSTOM_PER_CORE_WATT = new ParseField("per_core_watt");
Expand All @@ -49,7 +49,7 @@ public class GetStackTracesRequest extends ActionRequest implements IndicesReque
private String indices;
private String stackTraceIds;
private Double requestedDuration;
private Double customCostFactor;
private Double awsCostFactor;
private Double customCO2PerKWH;
private Double customDatacenterPUE;
private Double customPerCoreWatt;
Expand All @@ -66,7 +66,7 @@ public GetStackTracesRequest() {
public GetStackTracesRequest(
Integer sampleSize,
Double requestedDuration,
Double customCostFactor,
Double awsCostFactor,
QueryBuilder query,
String indices,
String stackTraceIds,
Expand All @@ -76,7 +76,7 @@ public GetStackTracesRequest(
) {
this.sampleSize = sampleSize;
this.requestedDuration = requestedDuration;
this.customCostFactor = customCostFactor;
this.awsCostFactor = awsCostFactor;
this.query = query;
this.indices = indices;
this.stackTraceIds = stackTraceIds;
Expand All @@ -89,7 +89,7 @@ public GetStackTracesRequest(StreamInput in) throws IOException {
this.query = in.readOptionalNamedWriteable(QueryBuilder.class);
this.sampleSize = in.readOptionalInt();
this.requestedDuration = in.readOptionalDouble();
this.customCostFactor = in.readOptionalDouble();
this.awsCostFactor = in.readOptionalDouble();
this.adjustSampleCount = in.readOptionalBoolean();
this.indices = in.readOptionalString();
this.stackTraceIds = in.readOptionalString();
Expand All @@ -103,7 +103,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalNamedWriteable(query);
out.writeOptionalInt(sampleSize);
out.writeOptionalDouble(requestedDuration);
out.writeOptionalDouble(customCostFactor);
out.writeOptionalDouble(awsCostFactor);
out.writeOptionalBoolean(adjustSampleCount);
out.writeOptionalString(indices);
out.writeOptionalString(stackTraceIds);
Expand All @@ -120,8 +120,8 @@ public Double getRequestedDuration() {
return requestedDuration;
}

public Double getCustomCostFactor() {
return customCostFactor;
public Double getAwsCostFactor() {
return awsCostFactor;
}

public Double getCustomCO2PerKWH() {
Expand Down Expand Up @@ -179,8 +179,8 @@ public void parseXContent(XContentParser parser) throws IOException {
this.stackTraceIds = parser.text();
} else if (REQUESTED_DURATION_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
this.requestedDuration = parser.doubleValue();
} else if (CUSTOM_COST_FACTOR_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
this.customCostFactor = parser.doubleValue();
} else if (AWS_COST_FACTOR_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
this.awsCostFactor = parser.doubleValue();
} else if (CUSTOM_CO2_PER_KWH.match(currentFieldName, parser.getDeprecationHandler())) {
this.customCO2PerKWH = parser.doubleValue();
} else if (CUSTOM_DATACENTER_PUE.match(currentFieldName, parser.getDeprecationHandler())) {
Expand Down Expand Up @@ -240,7 +240,7 @@ public ActionRequestValidationException validate() {
validationException = requirePositive(SAMPLE_SIZE_FIELD, sampleSize, validationException);
}
validationException = requirePositive(REQUESTED_DURATION_FIELD, requestedDuration, validationException);
validationException = requirePositive(CUSTOM_COST_FACTOR_FIELD, customCostFactor, validationException);
validationException = requirePositive(AWS_COST_FACTOR_FIELD, awsCostFactor, validationException);
validationException = requirePositive(CUSTOM_CO2_PER_KWH, customCO2PerKWH, validationException);
validationException = requirePositive(CUSTOM_DATACENTER_PUE, customDatacenterPUE, validationException);
validationException = requirePositive(CUSTOM_PER_CORE_WATT, customPerCoreWatt, validationException);
Expand All @@ -267,7 +267,7 @@ public String getDescription() {
appendField(sb, "stacktrace_ids", stackTraceIds);
appendField(sb, "sample_size", sampleSize);
appendField(sb, "requested_duration", requestedDuration);
appendField(sb, "custom_cost_factor", customCostFactor);
appendField(sb, "aws_cost_factor", awsCostFactor);
appendField(sb, "co2_per_kwh", customCO2PerKWH);
appendField(sb, "datacenter_pue", customDatacenterPUE);
appendField(sb, "per_core_watt", customPerCoreWatt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected void doExecute(Task submitTask, GetStackTracesRequest request, ActionL
licenseChecker.requireSupportedLicense();
GetStackTracesResponseBuilder responseBuilder = new GetStackTracesResponseBuilder();
responseBuilder.setRequestedDuration(request.getRequestedDuration());
responseBuilder.setCustomCostFactor(request.getCustomCostFactor());
responseBuilder.setAwsCostFactor(request.getAwsCostFactor());
responseBuilder.setCustomCO2PerKWH(request.getCustomCO2PerKWH());
responseBuilder.setCustomDatacenterPUE(request.getCustomDatacenterPUE());
responseBuilder.setCustomPerCoreWatt(request.getCustomPerCoreWatt());
Expand Down Expand Up @@ -531,7 +531,7 @@ public void calculateCO2AndCosts() {
instanceTypeService,
hostsTable,
responseBuilder.getRequestedDuration(),
responseBuilder.customCostFactor
responseBuilder.awsCostFactor
);
Map<String, TraceEvent> events = responseBuilder.stackTraceEvents;
List<String> missingStackTraces = new ArrayList<>();
Expand Down Expand Up @@ -738,7 +738,7 @@ private static class GetStackTracesResponseBuilder {
private double samplingRate;
private long totalSamples;
private Double requestedDuration;
private Double customCostFactor;
private Double awsCostFactor;
private Double customCO2PerKWH;
private Double customDatacenterPUE;
private Double customPerCoreWatt;
Expand Down Expand Up @@ -807,8 +807,8 @@ public double getRequestedDuration() {
return end.getEpochSecond() - start.getEpochSecond();
}

public void setCustomCostFactor(Double customCostFactor) {
this.customCostFactor = customCostFactor;
public void setAwsCostFactor(Double awsCostFactor) {
this.awsCostFactor = awsCostFactor;
}

public void setCustomCO2PerKWH(Double customCO2PerKWH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public class GetStackTracesRequestTests extends ESTestCase {
public void testSerialization() throws IOException {
Integer sampleSize = randomIntBetween(1, Integer.MAX_VALUE);
Double requestedDuration = randomBoolean() ? randomDoubleBetween(0.001d, Double.MAX_VALUE, true) : null;
Double customCostFactor = randomBoolean() ? randomDoubleBetween(0.1d, 5.0d, true) : null;
Double awsCostFactor = randomBoolean() ? randomDoubleBetween(0.1d, 5.0d, true) : null;
QueryBuilder query = randomBoolean() ? new BoolQueryBuilder() : null;

GetStackTracesRequest request = new GetStackTracesRequest(
sampleSize,
requestedDuration,
customCostFactor,
awsCostFactor,
query,
null,
null,
Expand Down

0 comments on commit 386bf84

Please sign in to comment.