Skip to content

Commit

Permalink
Merge pull request #2205 from HubSpot/lb_impl_2
Browse files Browse the repository at this point in the history
Refactor the LoadBalancerClient
  • Loading branch information
ssalinas authored Apr 28, 2021
2 parents b0379a7 + 5117b38 commit d00a529
Show file tree
Hide file tree
Showing 24 changed files with 566 additions and 352 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.hubspot.singularity;

import com.hubspot.baragon.models.BaragonRequestState;

public enum LoadBalancerRequestState {
UNKNOWN(false),
FAILED(false),
WAITING(true),
SUCCESS(false),
CANCELING(true),
CANCELED(false),
INVALID_REQUEST_NOOP(false);

private final boolean inProgress;

LoadBalancerRequestState(boolean inProgress) {
this.inProgress = inProgress;
}

public boolean isInProgress() {
return inProgress;
}

public static LoadBalancerRequestState fromBaragonRequestState(
BaragonRequestState baragonRequestState
) {
switch (baragonRequestState) {
case WAITING:
return WAITING;
case SUCCESS:
return SUCCESS;
case UNKNOWN:
return UNKNOWN;
case FAILED:
return FAILED;
case CANCELED:
return CANCELED;
case CANCELING:
return CANCELING;
case INVALID_REQUEST_NOOP:
return INVALID_REQUEST_NOOP;
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.hubspot.singularity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import com.hubspot.baragon.models.UpstreamInfo;
import java.util.Objects;
import java.util.Optional;

public class LoadBalancerUpstream {
private final String upstream;
private final String group;
private final Optional<String> rackId;

@JsonCreator
public LoadBalancerUpstream(
@JsonProperty("upstream") String upstream,
@JsonProperty("group") String group,
@JsonProperty("rackId") Optional<String> rackId
) {
this.upstream = upstream;
this.group = group;
this.rackId = rackId;
}

public static LoadBalancerUpstream fromBaragonUpstream(UpstreamInfo upstreamInfo) {
return new LoadBalancerUpstream(
upstreamInfo.getUpstream(),
upstreamInfo.getGroup(),
upstreamInfo.getRackId().toJavaUtil()
);
}

public String getUpstream() {
return upstream;
}

public String getGroup() {
return group;
}

public Optional<String> getRackId() {
return rackId;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LoadBalancerUpstream that = (LoadBalancerUpstream) o;
return (
Objects.equals(upstream, that.upstream) &&
Objects.equals(group, that.group) &&
Objects.equals(rackId, that.rackId)
);
}

@Override
public int hashCode() {
return Objects.hash(upstream, group, rackId);
}

@Override
public String toString() {
return MoreObjects
.toStringHelper(this)
.add("upstream", upstream)
.add("group", group)
.add("rackId", rackId)
.toString();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hubspot.baragon.models.BaragonRequestState;
import com.hubspot.singularity.LoadBalancerRequestType.LoadBalancerRequestId;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.Optional;

@Schema(description = "An updated to load balancer configuration")
public class SingularityLoadBalancerUpdate {
private final BaragonRequestState loadBalancerState;
private final LoadBalancerRequestState loadBalancerState;
private final Optional<String> message;
private final long timestamp;
private final Optional<String> uri;
Expand All @@ -27,7 +26,7 @@ public enum LoadBalancerMethod {

@JsonCreator
public SingularityLoadBalancerUpdate(
@JsonProperty("state") BaragonRequestState loadBalancerState,
@JsonProperty("state") LoadBalancerRequestState loadBalancerState,
@JsonProperty("loadBalancerRequestId") LoadBalancerRequestId loadBalancerRequestId,
@JsonProperty("message") Optional<String> message,
@JsonProperty("timestamp") long timestamp,
Expand All @@ -45,7 +44,7 @@ public SingularityLoadBalancerUpdate(
@Schema(
description = "The current state of the request to update load balancer configuration"
)
public BaragonRequestState getLoadBalancerState() {
public LoadBalancerRequestState getLoadBalancerState() {
return loadBalancerState;
}

Expand Down Expand Up @@ -78,7 +77,7 @@ public static SingularityLoadBalancerUpdate preEnqueue(
LoadBalancerRequestId lbRequestId
) {
return new SingularityLoadBalancerUpdate(
BaragonRequestState.UNKNOWN,
LoadBalancerRequestState.UNKNOWN,
lbRequestId,
Optional.<String>empty(),
System.currentTimeMillis(),
Expand Down
2 changes: 1 addition & 1 deletion SingularityService/.blazar.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
env:
MAVEN_ARGS: "-T 8 -Dsingularity.jar.name.format=\\${project.artifactId} -Dgpg.skip=true -DforkCount=2C"
MAVEN_ARGS: "-Dsingularity.jar.name.format=\\${project.artifactId} -Dgpg.skip=true"
SET_VERSION_OVERRIDE: "1.5.0-$GIT_BRANCH-SNAPSHOT"

buildResources:
Expand Down
7 changes: 6 additions & 1 deletion SingularityService/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,12 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkedProcessTimeoutInSeconds>300</forkedProcessTimeoutInSeconds>
<forkCount>2</forkCount>
<reuseForks>true</reuseForks>
<forkedProcessTimeoutInSeconds>600</forkedProcessTimeoutInSeconds>
<systemPropertyVariables>
<zkNamespace>sy_${surefire.forkNumber}</zkNamespace>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.hubspot.singularity.helpers.SingularityS3Services;
import com.hubspot.singularity.hooks.AbstractWebhookChecker;
import com.hubspot.singularity.hooks.LoadBalancerClient;
import com.hubspot.singularity.hooks.LoadBalancerClientImpl;
import com.hubspot.singularity.hooks.SingularityWebhookPoller;
import com.hubspot.singularity.hooks.SingularityWebhookSender;
import com.hubspot.singularity.hooks.SnsWebhookManager;
Expand Down Expand Up @@ -113,9 +112,14 @@ public class SingularityMainModule implements Module {
"singularity.last.mesos.master.heartbeat.time";

private final SingularityConfiguration configuration;
private final Class<? extends LoadBalancerClient> lbClientClass;

public SingularityMainModule(final SingularityConfiguration configuration) {
public SingularityMainModule(
final SingularityConfiguration configuration,
Class<? extends LoadBalancerClient> lbClientClass
) {
this.configuration = configuration;
this.lbClientClass = lbClientClass;
}

@Override
Expand Down Expand Up @@ -150,10 +154,7 @@ public void configure(Binder binder) {
binder.bind(SingularitySmtpSender.class).in(Scopes.SINGLETON);
binder.bind(MailTemplateHelpers.class).in(Scopes.SINGLETON);
binder.bind(SingularityExceptionNotifier.class).in(Scopes.SINGLETON);
binder
.bind(LoadBalancerClient.class)
.to(LoadBalancerClientImpl.class)
.in(Scopes.SINGLETON);
binder.bind(LoadBalancerClient.class).to(lbClientClass).in(Scopes.SINGLETON);
binder.bind(SingularityMailRecordCleaner.class).in(Scopes.SINGLETON);
binder.bind(StatusUpdateQueue.class).in(Scopes.SINGLETON);
binder.bind(SingularityWebhookPoller.class).in(Scopes.SINGLETON);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hubspot.singularity;

import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Provides;
Expand All @@ -19,15 +18,20 @@
import com.hubspot.singularity.data.transcoders.SingularityTranscoderModule;
import com.hubspot.singularity.data.zkmigrations.SingularityZkMigrationsModule;
import com.hubspot.singularity.event.SingularityEventModule;
import com.hubspot.singularity.hooks.BaragonLoadBalancerClientImpl;
import com.hubspot.singularity.hooks.LoadBalancerClient;
import com.hubspot.singularity.hooks.NoOpLoadBalancerClient;
import com.hubspot.singularity.jersey.SingularityJerseyModule;
import com.hubspot.singularity.mesos.SingularityMesosModule;
import com.hubspot.singularity.resources.SingularityOpenApiResource;
import com.hubspot.singularity.resources.SingularityResourceModule;
import com.hubspot.singularity.scheduler.SingularitySchedulerModule;
import java.util.Optional;

public class SingularityServiceModule
extends DropwizardAwareModule<SingularityConfiguration> {
private final Function<SingularityConfiguration, Module> dbModuleProvider;
private Optional<Class<? extends LoadBalancerClient>> lbClientClass = Optional.empty();

public SingularityServiceModule() {
this.dbModuleProvider = SingularityDbModule::new;
Expand All @@ -39,9 +43,26 @@ public SingularityServiceModule(
this.dbModuleProvider = dbModuleProvider;
}

public void setLoadBalancerClientClass(
Class<? extends LoadBalancerClient> lbClientClass
) {
this.lbClientClass = Optional.of(lbClientClass);
}

@Override
public void configure(Binder binder) {
binder.install(new SingularityMainModule(getConfiguration()));
SingularityConfiguration configuration = getConfiguration();
binder.install(
new SingularityMainModule(
getConfiguration(),
lbClientClass.orElseGet(
() ->
configuration.getLoadBalancerUri() != null
? BaragonLoadBalancerClientImpl.class
: NoOpLoadBalancerClient.class
)
)
);
binder.install(new SingularityDataModule(getConfiguration()));
binder.install(new SingularitySchedulerModule());
binder.install(
Expand Down Expand Up @@ -81,7 +102,9 @@ public void configure(Binder binder) {

@Provides
@Singleton
public IndexViewConfiguration provideIndexViewConfiguration() {
public IndexViewConfiguration provideIndexViewConfiguration(
LoadBalancerClient loadBalancerClient
) {
SingularityConfiguration configuration = getConfiguration();
return new IndexViewConfiguration(
configuration.getUiConfiguration(),
Expand All @@ -95,7 +118,7 @@ public IndexViewConfiguration provideIndexViewConfiguration() {
configuration.getHealthcheckTimeoutSeconds(),
configuration.getHealthcheckMaxRetries(),
configuration.getStartupTimeoutSeconds(),
!Strings.isNullOrEmpty(configuration.getLoadBalancerUri()),
loadBalancerClient.isEnabled(),
configuration.getCommonHostnameSuffixToOmit(),
configuration.getWarnIfScheduledJobIsRunningPastNextRunPct(),
configuration.getAuthConfiguration().isEnabled() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.hubspot.singularity.data.history.DeployHistoryHelper;
import com.hubspot.singularity.expiring.SingularityExpiringMachineState;
import com.hubspot.singularity.helpers.ImageName;
import com.hubspot.singularity.hooks.LoadBalancerClient;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -116,6 +117,7 @@ public class SingularityValidator {
private final PriorityManager priorityManager;
private final DisasterManager disasterManager;
private final AgentManager agentManager;
private final LoadBalancerClient loadBalancerClient;

@Inject
public SingularityValidator(
Expand All @@ -124,7 +126,8 @@ public SingularityValidator(
PriorityManager priorityManager,
DisasterManager disasterManager,
AgentManager agentManager,
UIConfiguration uiConfiguration
UIConfiguration uiConfiguration,
LoadBalancerClient loadBalancerClient
) {
this.maxDeployIdSize = configuration.getMaxDeployIdSize();
this.maxRequestIdSize = configuration.getMaxRequestIdSize();
Expand Down Expand Up @@ -183,6 +186,7 @@ public SingularityValidator(

this.disasterManager = disasterManager;
this.agentManager = agentManager;
this.loadBalancerClient = loadBalancerClient;
}

public SingularityRequest checkSingularityRequest(
Expand Down Expand Up @@ -420,16 +424,8 @@ public SingularityDeploy checkDeploy(
"Deploy id must match request id"
);

if (request.isLoadBalanced()) {
checkBadRequest(
deploy.getServiceBasePath().isPresent(),
"Deploy for loadBalanced request must include serviceBasePath"
);
checkBadRequest(
deploy.getLoadBalancerGroups().isPresent() &&
!deploy.getLoadBalancerGroups().get().isEmpty(),
"Deploy for a loadBalanced request must include at least one load balacner group"
);
if (request.isLoadBalanced() && loadBalancerClient.isEnabled()) {
loadBalancerClient.validateDeploy(deploy);
}

if (deploy.getEnv().isPresent()) {
Expand Down
Loading

0 comments on commit d00a529

Please # to comment.