Skip to content

Commit

Permalink
allow deploy to specify port index for healthchecks and LB api
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalinas committed Feb 9, 2016
1 parent a6dab03 commit 0ace1e3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class SingularityDeploy {
private final Optional<String> healthcheckUri;
private final Optional<Long> healthcheckIntervalSeconds;
private final Optional<Long> healthcheckTimeoutSeconds;
private final Optional<Integer> healthcheckPortIndex;
private final Optional<Boolean> skipHealthchecksOnDeploy;
private final Optional<HealthcheckProtocol> healthcheckProtocol;

Expand All @@ -58,6 +59,7 @@ public class SingularityDeploy {

private final Optional<String> serviceBasePath;
private final Optional<Set<String>> loadBalancerGroups;
private final Optional<Integer> loadBalancerPortIndex;
private final Optional<Map<String, Object>> loadBalancerOptions;

public static SingularityDeployBuilder newBuilder(String requestId, String id) {
Expand Down Expand Up @@ -87,10 +89,12 @@ public SingularityDeploy(@JsonProperty("requestId") String requestId,
@JsonProperty("healthcheckUri") Optional<String> healthcheckUri,
@JsonProperty("healthcheckIntervalSeconds") Optional<Long> healthcheckIntervalSeconds,
@JsonProperty("healthcheckTimeoutSeconds") Optional<Long> healthcheckTimeoutSeconds,
@JsonProperty("healthcheckPortIndex") Optional<Integer> healthcheckPortIndex,
@JsonProperty("healthcheckMaxRetries") Optional<Integer> healthcheckMaxRetries,
@JsonProperty("healthcheckMaxTotalTimeoutSeconds") Optional<Long> healthcheckMaxTotalTimeoutSeconds,
@JsonProperty("serviceBasePath") Optional<String> serviceBasePath,
@JsonProperty("loadBalancerGroups") Optional<Set<String>> loadBalancerGroups,
@JsonProperty("loadBalancerPortIndex") Optional<Integer> loadBalancerPortIndex,
@JsonProperty("considerHealthyAfterRunningForSeconds") Optional<Long> considerHealthyAfterRunningForSeconds,
@JsonProperty("loadBalancerOptions") Optional<Map<String, Object>> loadBalancerOptions,
@JsonProperty("skipHealthchecksOnDeploy") Optional<Boolean> skipHealthchecksOnDeploy,
Expand Down Expand Up @@ -121,6 +125,7 @@ public SingularityDeploy(@JsonProperty("requestId") String requestId,
this.healthcheckUri = healthcheckUri;
this.healthcheckIntervalSeconds = healthcheckIntervalSeconds;
this.healthcheckTimeoutSeconds = healthcheckTimeoutSeconds;
this.healthcheckPortIndex = healthcheckPortIndex;
this.skipHealthchecksOnDeploy = skipHealthchecksOnDeploy;
this.healthcheckProtocol = healthcheckProtocol;

Expand All @@ -133,6 +138,7 @@ public SingularityDeploy(@JsonProperty("requestId") String requestId,

this.serviceBasePath = serviceBasePath;
this.loadBalancerGroups = loadBalancerGroups;
this.loadBalancerPortIndex = loadBalancerPortIndex;
this.loadBalancerOptions = loadBalancerOptions;
}

Expand All @@ -151,6 +157,7 @@ public SingularityDeployBuilder toBuilder() {
.setHealthcheckUri(healthcheckUri)
.setHealthcheckIntervalSeconds(healthcheckIntervalSeconds)
.setHealthcheckTimeoutSeconds(healthcheckTimeoutSeconds)
.setHealthcheckPortIndex(healthcheckPortIndex)
.setSkipHealthchecksOnDeploy(skipHealthchecksOnDeploy)
.setHealthcheckProtocol(healthcheckProtocol)

Expand All @@ -161,6 +168,7 @@ public SingularityDeployBuilder toBuilder() {
.setDeployHealthTimeoutSeconds(deployHealthTimeoutSeconds)
.setServiceBasePath(serviceBasePath)
.setLoadBalancerGroups(copyOfSet(loadBalancerGroups))
.setLoadBalancerPortIndex(loadBalancerPortIndex)
.setLoadBalancerOptions(copyOfMap(loadBalancerOptions))

.setMetadata(copyOfMap(metadata))
Expand Down Expand Up @@ -281,6 +289,11 @@ public Optional<Long> getHealthcheckTimeoutSeconds() {
return healthcheckTimeoutSeconds;
}

@ApiModelProperty(required=false, value="Perform healthcheck on this dynamically allocated port (e.g. 0 for first port)")
public Optional<Integer> getHealthcheckPortIndex() {
return healthcheckPortIndex;
}

@ApiModelProperty(required=false, value="The base path for the API exposed by the deploy. Used in conjunction with the Load balancer API.")
public Optional<String> getServiceBasePath() {
return serviceBasePath;
Expand All @@ -296,6 +309,11 @@ public Optional<Set<String>> getLoadBalancerGroups() {
return loadBalancerGroups;
}

@ApiModelProperty(required=false, value="Send this port to the load balancer api (e.g. 0 for first port)")
public Optional<Integer> getLoadBalancerPortIndex() {
return loadBalancerPortIndex;
}

@ApiModelProperty(required=false, value="Map (Key/Value) of options for the load balancer.")
public Optional<Map<String, Object>> getLoadBalancerOptions() {
return loadBalancerOptions;
Expand Down Expand Up @@ -344,6 +362,7 @@ public String toString() {
", healthcheckUri=" + healthcheckUri +
", healthcheckIntervalSeconds=" + healthcheckIntervalSeconds +
", healthcheckTimeoutSeconds=" + healthcheckTimeoutSeconds +
", healthcheckPortIndex=" + healthcheckPortIndex +
", skipHealthchecksOnDeploy=" + skipHealthchecksOnDeploy +
", healthcheckProtocol=" + healthcheckProtocol +
", healthcheckMaxRetries=" + healthcheckMaxRetries +
Expand All @@ -352,6 +371,7 @@ public String toString() {
", considerHealthyAfterRunningForSeconds=" + considerHealthyAfterRunningForSeconds +
", serviceBasePath=" + serviceBasePath +
", loadBalancerGroups=" + loadBalancerGroups +
", loadBalancerPortIndex=" + loadBalancerPortIndex +
", loadBalancerOptions=" + loadBalancerOptions +
", labels=" + labels +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SingularityDeployBuilder {
private Optional<String> healthcheckUri;
private Optional<Long> healthcheckIntervalSeconds;
private Optional<Long> healthcheckTimeoutSeconds;
private Optional<Integer> healthcheckPortIndex;
private Optional<Boolean> skipHealthchecksOnDeploy;
private Optional<HealthcheckProtocol> healthcheckProtocol;

Expand All @@ -51,6 +52,7 @@ public class SingularityDeployBuilder {

private Optional<String> serviceBasePath;
private Optional<Set<String>> loadBalancerGroups;
private Optional<Integer> loadBalancerPortIndex;
private Optional<Map<String, Object>> loadBalancerOptions;

public SingularityDeployBuilder(String requestId, String id) {
Expand All @@ -75,6 +77,7 @@ public SingularityDeployBuilder(String requestId, String id) {
this.healthcheckUri = Optional.absent();
this.healthcheckIntervalSeconds = Optional.absent();
this.healthcheckTimeoutSeconds = Optional.absent();
this.healthcheckPortIndex = Optional.absent();
this.skipHealthchecksOnDeploy = Optional.absent();
this.deployHealthTimeoutSeconds = Optional.absent();
this.healthcheckProtocol = Optional.absent();
Expand All @@ -83,13 +86,14 @@ public SingularityDeployBuilder(String requestId, String id) {
this.considerHealthyAfterRunningForSeconds = Optional.absent();
this.serviceBasePath = Optional.absent();
this.loadBalancerGroups = Optional.absent();
this.loadBalancerPortIndex = Optional.absent();
this.loadBalancerOptions = Optional.absent();
}

public SingularityDeploy build() {
return new SingularityDeploy(requestId, id, command, arguments, containerInfo, customExecutorCmd, customExecutorId, customExecutorSource, customExecutorResources, customExecutorUser, resources, env,
uris, metadata, executorData, version, timestamp, labels, deployHealthTimeoutSeconds, healthcheckUri, healthcheckIntervalSeconds, healthcheckTimeoutSeconds, healthcheckMaxRetries,
healthcheckMaxTotalTimeoutSeconds, serviceBasePath, loadBalancerGroups, considerHealthyAfterRunningForSeconds, loadBalancerOptions, skipHealthchecksOnDeploy, healthcheckProtocol);
uris, metadata, executorData, version, timestamp, labels, deployHealthTimeoutSeconds, healthcheckUri, healthcheckIntervalSeconds, healthcheckTimeoutSeconds, healthcheckPortIndex, healthcheckMaxRetries,
healthcheckMaxTotalTimeoutSeconds, serviceBasePath, loadBalancerGroups, loadBalancerPortIndex, considerHealthyAfterRunningForSeconds, loadBalancerOptions, skipHealthchecksOnDeploy, healthcheckProtocol);
}

public String getRequestId() {
Expand Down Expand Up @@ -285,6 +289,15 @@ public SingularityDeployBuilder setHealthcheckTimeoutSeconds(Optional<Long> heal
return this;
}

public Optional<Integer> getHealthcheckPortIndex() {
return healthcheckPortIndex;
}

public SingularityDeployBuilder setHealthcheckPortIndex(Optional<Integer> healthcheckPortIndex) {
this.healthcheckPortIndex = healthcheckPortIndex;
return this;
}

public Optional<String> getServiceBasePath() {
return serviceBasePath;
}
Expand All @@ -303,6 +316,15 @@ public SingularityDeployBuilder setLoadBalancerGroups(Optional<Set<String>> load
return this;
}

public Optional<Integer> getLoadBalancerPortIndex() {
return loadBalancerPortIndex;
}

public SingularityDeployBuilder setLoadBalancerPortIndex(Optional<Integer> loadBalancerPortIndex) {
this.loadBalancerPortIndex = loadBalancerPortIndex;
return this;
}

public Optional<Map<String, Object>> getLoadBalancerOptions() {
return loadBalancerOptions;
}
Expand Down Expand Up @@ -381,6 +403,7 @@ public String toString() {
", healthcheckUri=" + healthcheckUri +
", healthcheckIntervalSeconds=" + healthcheckIntervalSeconds +
", healthcheckTimeoutSeconds=" + healthcheckTimeoutSeconds +
", healthcheckPortIndex=" + healthcheckPortIndex +
", skipHealthchecksOnDeploy=" + skipHealthchecksOnDeploy +
", healthcheckProtocol=" + healthcheckProtocol +
", healthcheckMaxRetries=" + healthcheckMaxRetries +
Expand All @@ -389,6 +412,7 @@ public String toString() {
", considerHealthyAfterRunningForSeconds=" + considerHealthyAfterRunningForSeconds +
", serviceBasePath=" + serviceBasePath +
", loadBalancerGroups=" + loadBalancerGroups +
", loadBalancerPortIndex=" + loadBalancerPortIndex +
", loadBalancerOptions=" + loadBalancerOptions +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hubspot.singularity;

import java.util.List;

import org.apache.mesos.Protos.Offer;
import org.apache.mesos.Protos.Resource;
import org.apache.mesos.Protos.TaskInfo;
Expand Down Expand Up @@ -45,16 +47,13 @@ public Optional<String> getRackId() {
}

@JsonIgnore
public Optional<Long> getFirstPort() {
for (Resource resource : mesosTask.getResourcesList()) {
if (resource.getName().equals(MesosUtils.PORTS)) {
for (Range range : resource.getRanges().getRangeList()) {
return Optional.of(range.getBegin());
}
}
public Optional<Long> getPortByIndex(int index) {
List<Long> ports = MesosUtils.getAllPorts(mesosTask.getResourcesList());
if (index >= ports.size()) {
return Optional.absent();
} else {
return Optional.of(ports.get(index));
}

return Optional.absent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ private List<UpstreamInfo> tasksToUpstreams(List<SingularityTask> tasks, String
final List<UpstreamInfo> upstreams = Lists.newArrayListWithCapacity(tasks.size());

for (SingularityTask task : tasks) {
final Optional<Long> maybeFirstPort = task.getFirstPort();
final Optional<Long> maybeLoadBalancerPort = task.getPortByIndex(task.getTaskRequest().getDeploy().getLoadBalancerPortIndex().or(0));

if (maybeFirstPort.isPresent()) {
String upstream = String.format("%s:%d", task.getOffer().getHostname(), maybeFirstPort.get());
if (maybeLoadBalancerPort.isPresent()) {
String upstream = String.format("%s:%d", task.getOffer().getHostname(), maybeLoadBalancerPort.get());
upstreams.add(new UpstreamInfo(upstream, Optional.of(requestId), task.getRackId()));
} else {
LOG.warn("Task {} is missing port but is being passed to LB ({})", task.getTaskId(), task);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hubspot.singularity.scheduler;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand All @@ -19,6 +20,7 @@
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.hubspot.mesos.MesosUtils;
import com.hubspot.singularity.HealthcheckProtocol;
import com.hubspot.singularity.SingularityAbort;
import com.hubspot.singularity.SingularityMainModule;
Expand Down Expand Up @@ -167,9 +169,9 @@ private Optional<String> getHealthcheckUri(SingularityTask task) {

final String hostname = task.getOffer().getHostname();

Optional<Long> firstPort = task.getFirstPort();
Optional<Long> healthecheckPort = task.getPortByIndex(task.getTaskRequest().getDeploy().getHealthcheckPortIndex().or(0));

if (!firstPort.isPresent() || firstPort.get() < 1L) {
if (!healthecheckPort.isPresent() || healthecheckPort.get() < 1L) {
LOG.warn("Couldn't find a port for health check for task {}", task);
return Optional.absent();
}
Expand All @@ -182,7 +184,7 @@ private Optional<String> getHealthcheckUri(SingularityTask task) {

HealthcheckProtocol protocol = task.getTaskRequest().getDeploy().getHealthcheckProtocol().or(DEFAULT_HEALTH_CHECK_SCHEME);

return Optional.of(String.format("%s://%s:%d/%s", protocol.getProtocol(), hostname, firstPort.get(), uri));
return Optional.of(String.format("%s://%s:%d/%s", protocol.getProtocol(), hostname, healthecheckPort.get(), uri));
}

private void saveFailure(SingularityHealthcheckAsyncHandler handler, String message) {
Expand Down

0 comments on commit 0ace1e3

Please sign in to comment.