Skip to content

Commit

Permalink
almost running
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalinas committed Jul 10, 2017
1 parent 6980439 commit 6b30dcd
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 67 deletions.
@@ -1,16 +1,17 @@
package com.hubspot.singularity;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Binder;
import com.hubspot.dropwizard.guicier.DropwizardAwareModule;
import com.hubspot.singularity.client.SingularityClientModule;
import com.hubspot.mesos.JavaUtils;
import com.hubspot.singularity.config.ClusterCoordinatorConfiguration;
import com.hubspot.singularity.proxy.SingularityClusterCoodinatorResourcesModule;

public class SingularityClusterCoordinatorModule extends DropwizardAwareModule<ClusterCoordinatorConfiguration> {

@Override
public void configure(Binder binder) {
binder.install(new SingularityClusterCoodinatorResourcesModule());
binder.install(new SingularityClientModule());
binder.bind(ObjectMapper.class).toInstance(JavaUtils.newObjectMapper());
binder.install(new SingularityClusterCoodinatorResourcesModule(getConfiguration()));
}
}
Expand Up @@ -26,18 +26,18 @@ public class ClusterCoordinatorConfiguration extends Configuration {
private Optional<SingularityClientCredentials> defaultClientCredentials;

// Settings to inform the ui
private Integer defaultMemory;
private Integer defaultCpus;
private Integer slaveHttpPort;
private Optional<Integer> slaveHttpsPort;
private int bounceExpirationMinutes;
private long healthcheckIntervalSeconds;
private long healthcheckTimeoutSeconds;
private Optional<Integer> healthcheckMaxRetries;
private int startupTimeoutSeconds;
private boolean loadBalancingEnabled;
private Optional<String> commonHostnameSuffixToOmit;
private Integer warnIfScheduledJobIsRunningPastNextRunPct;
private Integer defaultMemory = 64;
private Integer defaultCpus = 1;
private Integer slaveHttpPort = 5051;
private Optional<Integer> slaveHttpsPort = Optional.absent();
private int bounceExpirationMinutes = 60;
private long healthcheckIntervalSeconds = 5;
private long healthcheckTimeoutSeconds = 5;
private Optional<Integer> healthcheckMaxRetries = Optional.absent();
private int startupTimeoutSeconds = 45;
private boolean loadBalancingEnabled = false;
private Optional<String> commonHostnameSuffixToOmit = Optional.absent();
private Integer warnIfScheduledJobIsRunningPastNextRunPct = 200;



Expand Down
Expand Up @@ -19,7 +19,7 @@ public class DataCenter {
// http or https
private String scheme = "http";

private Optional<SingularityClientCredentials> clientCredentials;
private Optional<SingularityClientCredentials> clientCredentials = Optional.absent();

public String getName() {
return name;
Expand Down
Expand Up @@ -8,30 +8,31 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.hubspot.singularity.SingularityClientCredentials;
import com.hubspot.singularity.SingularityRequestGroup;
import com.hubspot.singularity.SingularityRequestParent;
import com.hubspot.singularity.SingularitySlave;
import com.hubspot.singularity.client.SingularityClient;
import com.hubspot.singularity.client.SingularityClientProvider;
import com.hubspot.singularity.config.ClusterCoordinatorConfiguration;
import com.hubspot.singularity.config.DataCenter;
import com.hubspot.singularity.exceptions.DataCenterNotFoundException;

import io.dropwizard.lifecycle.Managed;

public class DataCenterLocator implements Managed {
private static final Logger LOG = LoggerFactory.getLogger(DataCenterLocator.class);

private final ClusterCoordinatorConfiguration configuration;
private final SingularityClientProvider clientProvider;
private final Map<String, DataCenter> dataCenters;
private final Map<String, SingularityClient> clients;

private final Random random = new Random();

private final Map<String, SingularityClient> clients = new ConcurrentHashMap<>();

private final Map<String, Set<String>> requestIdsByDataCenter = new ConcurrentHashMap<>();
private final Map<String, Set<String>> requestGroupsByDataCenter = new ConcurrentHashMap<>();
private final Map<String, Set<String>> slaveIdsByDataCenter = new ConcurrentHashMap<>();
Expand All @@ -40,14 +41,12 @@ public class DataCenterLocator implements Managed {


@Inject
public DataCenterLocator(ClusterCoordinatorConfiguration configuration, SingularityClientProvider clientProvider) {
public DataCenterLocator(ClusterCoordinatorConfiguration configuration, Map<String, SingularityClient> clients) {
this.configuration = configuration;
this.clientProvider = clientProvider;
this.clients = clients;

ImmutableMap.Builder builder = ImmutableMap.builder();
configuration.getDataCenters().forEach((dc) -> {
builder.put(dc.getName(), dc);
});
ImmutableMap.Builder<String, DataCenter> builder = ImmutableMap.builder();
configuration.getDataCenters().forEach((dc) -> builder.put(dc.getName(), dc));
this.dataCenters = builder.build();
}

Expand Down Expand Up @@ -170,35 +169,27 @@ DataCenter getDataCenter(String name) {

@Override
public void start() {
createClients();
loadData();
}

private void createClients() {
configuration.getDataCenters().forEach((dc) -> {
clientProvider.setHosts(dc.getHosts());
clientProvider.setContextPath(dc.getContextPath());
clientProvider.setSsl(dc.getScheme().equals("https"));
Optional<SingularityClientCredentials> maybeCredentials = dc.getClientCredentials().or(configuration.getDefaultClientCredentials());
clients.put(dc.getName(), clientProvider.get(maybeCredentials));
});
}

private void loadData() {
configuration.getDataCenters().forEach((dc) -> {
SingularityClient singularityClient = clients.get(dc.getName());

Collection<SingularityRequestParent> requestParents = singularityClient.getSingularityRequests();
requestIdsByDataCenter.put(dc.getName(), requestParents.stream().map((r) -> r.getRequest().getId()).collect(Collectors.toSet()));
LOG.info("Loaded {} requests for data center {}", requestParents.size(), dc.getName());

Collection<SingularitySlave> slaves = singularityClient.getSlaves(Optional.absent());
Set<String> rackIds = slaves.stream().map((s) -> s.getRackId()).collect(Collectors.toSet());
Set<String> rackIds = slaves.stream().map(SingularitySlave::getRackId).collect(Collectors.toSet());
rackIdsByDataCenter.put(dc.getName(), new HashSet<>(rackIds));
slaveIdsByDataCenter.put(dc.getName(), slaves.stream().map(SingularitySlave::getId).collect(Collectors.toSet()));
hostnamesByDataCenter.put(dc.getName(), slaves.stream().map(SingularitySlave::getHost).collect(Collectors.toSet()));
LOG.info("Loaded {} slaves for data center {}", slaves.size(), dc.getName());

Collection<SingularityRequestGroup> requestGroups = singularityClient.getRequestGroups();
requestGroupsByDataCenter.put(dc.getName(), requestGroups.stream().map(SingularityRequestGroup::getId).collect(Collectors.toSet()));
LOG.info("Loaded {} request groups for data center {}", requestGroups.size(), dc.getName());
});
}

Expand Down
Expand Up @@ -216,7 +216,12 @@ private <T, Q> T proxyRequest(DataCenter dc, HttpServletRequest request, Q body,
return objectMapper.readValue(response.getResponseBodyAsStream(), clazz);
}
} catch (IOException ioe) {
LOG.error("Request succeeded with status {}, but could not interpret response", response.getStatusCode(), ioe);
try {
LOG.warn("Bad response body: {}", response.getResponseBody(Charsets.UTF_8.toString()));
} catch (IOException io) {
LOG.error("Could not print response", io);
}
LOG.error("Request succeeded with status {}, but could not interpret response ({})", response.getStatusCode(), ioe);
}

return null;
Expand Down
@@ -1,51 +1,66 @@
package com.hubspot.singularity.proxy;

import com.google.inject.Binder;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Scopes;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.hubspot.dropwizard.guicier.DropwizardAwareModule;
import com.hubspot.horizon.HttpClient;
import com.hubspot.horizon.HttpConfig;
import com.hubspot.horizon.ning.NingHttpClient;
import com.hubspot.singularity.SingularityAsyncHttpClient;
import com.hubspot.singularity.SingularityClientCredentials;
import com.hubspot.singularity.SingularityServiceBaseModule;
import com.hubspot.singularity.client.SingularityClient;
import com.hubspot.singularity.client.SingularityClientProvider;
import com.hubspot.singularity.config.ClusterCoordinatorConfiguration;
import com.hubspot.singularity.config.IndexViewConfiguration;
import com.ning.http.client.AsyncHttpClient;

import io.dropwizard.server.SimpleServerFactory;

public class SingularityClusterCoodinatorResourcesModule extends DropwizardAwareModule<ClusterCoordinatorConfiguration> {
public class SingularityClusterCoodinatorResourcesModule extends AbstractModule {
private final ClusterCoordinatorConfiguration configuration;

public SingularityClusterCoodinatorResourcesModule(ClusterCoordinatorConfiguration configuration) {
this.configuration = configuration;
}

@Override
public void configure(Binder binder) {
binder.bind(AsyncHttpClient.class).to(SingularityAsyncHttpClient.class).in(Scopes.SINGLETON);
public void configure() {
bind(AsyncHttpClient.class).to(SingularityAsyncHttpClient.class).in(Scopes.SINGLETON);
bind(DataCenterLocator.class).in(Scopes.SINGLETON);

binder.bind(DeployResource.class);
binder.bind(HistoryResource.class);
binder.bind(RackResource.class);
binder.bind(RequestResource.class);
binder.bind(S3LogResource.class);
binder.bind(SandboxResource.class);
binder.bind(SlaveResource.class);
binder.bind(StateResource.class);
binder.bind(TaskResource.class);
binder.bind(WebhookResource.class);
binder.bind(AuthResource.class);
binder.bind(UserResource.class);
binder.bind(DisastersResource.class);
binder.bind(PriorityResource.class);
binder.bind(UsageResource.class);
binder.bind(RequestGroupResource.class);
binder.bind(InactiveSlaveResource.class);
binder.bind(TaskTrackerResource.class);
bind(DeployResource.class);
bind(HistoryResource.class);
bind(RackResource.class);
bind(RequestResource.class);
bind(S3LogResource.class);
bind(SandboxResource.class);
bind(SlaveResource.class);
bind(StateResource.class);
bind(TaskResource.class);
bind(WebhookResource.class);
bind(AuthResource.class);
bind(UserResource.class);
bind(DisastersResource.class);
bind(PriorityResource.class);
bind(UsageResource.class);
bind(RequestGroupResource.class);
bind(InactiveSlaveResource.class);
bind(TaskTrackerResource.class);

binder.install(new SingularityServiceBaseModule(getConfiguration().getUiConfiguration()));
install(new SingularityServiceBaseModule(configuration.getUiConfiguration()));
}

@Provides
@Singleton
public IndexViewConfiguration provideIndexViewConfiguration() {
ClusterCoordinatorConfiguration configuration = getConfiguration();
return new IndexViewConfiguration(
configuration.getUiConfiguration(),
configuration.getDefaultMemory(),
Expand All @@ -65,8 +80,24 @@ public IndexViewConfiguration provideIndexViewConfiguration() {

@Provides
@Named(SingularityServiceBaseModule.SINGULARITY_URI_BASE)
String getSingularityUriBase() {
final String singularityUiPrefix = getConfiguration().getUiConfiguration().getBaseUrl().or(((SimpleServerFactory) getConfiguration().getServerFactory()).getApplicationContextPath());
public String getSingularityUriBase() {
final String singularityUiPrefix = configuration.getUiConfiguration().getBaseUrl().or(((SimpleServerFactory) configuration.getServerFactory()).getApplicationContextPath());
return (singularityUiPrefix.endsWith("/")) ? singularityUiPrefix.substring(0, singularityUiPrefix.length() - 1) : singularityUiPrefix;
}

@Provides
@Singleton
public Map<String, SingularityClient> provideClients(ObjectMapper objectMapper) {
HttpClient httpClient = new NingHttpClient(HttpConfig.newBuilder().setObjectMapper(objectMapper).build());
SingularityClientProvider clientProvider = new SingularityClientProvider(httpClient);
Map<String, SingularityClient> clients = new HashMap<>();
configuration.getDataCenters().forEach((dc) -> {
clientProvider.setHosts(dc.getHosts());
clientProvider.setContextPath(dc.getContextPath());
clientProvider.setSsl(dc.getScheme().equals("https"));
Optional<SingularityClientCredentials> maybeCredentials = dc.getClientCredentials().or(configuration.getDefaultClientCredentials());
clients.put(dc.getName(), clientProvider.get(maybeCredentials));
});
return clients;
}
}

0 comments on commit 6b30dcd

Please sign in to comment.