Skip to content

Commit

Permalink
use MesosClient instead
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetr committed Jan 9, 2015
1 parent 46dbe67 commit c96e624
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
5 changes: 5 additions & 0 deletions SingularityExecutorCleanup/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<artifactId>SingularityClient</artifactId>
</dependency>

<dependency>
<groupId>com.hubspot</groupId>
<artifactId>SingularityMesosClient</artifactId>
</dependency>

<dependency>
<groupId>com.hubspot</groupId>
<artifactId>HorizonCore</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.hubspot.horizon.HttpClient;
import com.hubspot.horizon.HttpRequest;
import com.hubspot.horizon.HttpResponse;
import com.hubspot.mesos.JavaUtils;
import com.hubspot.mesos.client.MesosClient;
import com.hubspot.mesos.json.MesosSlaveStateObject;
import com.hubspot.singularity.SingularityTask;
import com.hubspot.singularity.SingularityTaskHistory;
Expand All @@ -40,23 +42,21 @@ public class SingularityExecutorCleanup {

private static final Logger LOG = LoggerFactory.getLogger(SingularityExecutorCleanup.class);

public static final String LOCAL_SLAVE_STATE_URL_FORMAT = "http://%s:5051/slave(1)/state.json";

private final JsonObjectFileHelper jsonObjectFileHelper;
private final SingularityExecutorConfiguration executorConfiguration;
private final SingularityClient singularityClient;
private final TemplateManager templateManager;
private final SingularityExecutorCleanupConfiguration cleanupConfiguration;
private final HttpClient httpClient;
private final MesosClient mesosClient;

@Inject
public SingularityExecutorCleanup(SingularityClient singularityClient, JsonObjectFileHelper jsonObjectFileHelper, SingularityExecutorConfiguration executorConfiguration, SingularityExecutorCleanupConfiguration cleanupConfiguration, TemplateManager templateManager, @Named(SingularityClientModule.HTTP_CLIENT_NAME) HttpClient httpClient) {
public SingularityExecutorCleanup(SingularityClient singularityClient, JsonObjectFileHelper jsonObjectFileHelper, SingularityExecutorConfiguration executorConfiguration, SingularityExecutorCleanupConfiguration cleanupConfiguration, TemplateManager templateManager, MesosClient mesosClient) {
this.jsonObjectFileHelper = jsonObjectFileHelper;
this.executorConfiguration = executorConfiguration;
this.cleanupConfiguration = cleanupConfiguration;
this.singularityClient = singularityClient;
this.templateManager = templateManager;
this.httpClient = httpClient;
this.mesosClient = mesosClient;
}

public SingularityExecutorCleanupStatistics clean() {
Expand Down Expand Up @@ -138,31 +138,22 @@ public SingularityExecutorCleanupStatistics clean() {
return statisticsBldr.build();
}

private String getLocalSlaveID() {
private Set<String> getRunningTaskIds() {
try {
final HttpRequest request = HttpRequest.newBuilder().setUrl(String.format(LOCAL_SLAVE_STATE_URL_FORMAT, JavaUtils.getHostAddress())).build();
final HttpResponse response = httpClient.execute(request);
final String slaveId = mesosClient.getSlaveState(mesosClient.getSlaveUri(JavaUtils.getHostAddress())).getId();

if (response.isSuccess()) {
return response.getAs(MesosSlaveStateObject.class).getId();
} else {
throw new RuntimeException(String.format("Failed to get local Slave ID -- HTTP %d: %s", response.getStatusCode(), response.getAsString()));
}
} catch (SocketException se) {
throw new RuntimeException("Failed to get host address", se);
}
}
final Collection<SingularityTask> activeTasks = singularityClient.getActiveTasksOnSlave(slaveId);

private Set<String> getRunningTaskIds() {
final Collection<SingularityTask> activeTasks = singularityClient.getActiveTasksOnSlave(getLocalSlaveID());
final Set<String> runningTaskIds = Sets.newHashSet();

final Set<String> runningTaskIds = Sets.newHashSet();
for (SingularityTask task : activeTasks) {
runningTaskIds.add(task.getTaskId().getId());
}

for (SingularityTask task : activeTasks) {
runningTaskIds.add(task.getTaskId().getId());
return runningTaskIds;
} catch (SocketException se) {
throw Throwables.propagate(se);
}

return runningTaskIds;
}

private boolean cleanTask(SingularityExecutorTaskDefinition taskDefinition, Optional<SingularityTaskHistory> taskHistory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.inject.Injector;
import com.google.inject.Stage;
import com.hubspot.mesos.JavaUtils;
import com.hubspot.mesos.client.SingularityMesosClientModule;
import com.hubspot.singularity.client.SingularityClientModule;
import com.hubspot.singularity.executor.SingularityExecutorCleanupStatistics;
import com.hubspot.singularity.executor.cleanup.config.SingularityExecutorCleanupConfiguration;
Expand All @@ -27,7 +28,7 @@ public static void main(String... args) {
final long start = System.currentTimeMillis();

try {
final Injector injector = Guice.createInjector(Stage.PRODUCTION, new SingularityRunnerBaseModule(new SingularityS3ConfigurationLoader(), new SingularityExecutorConfigurationLoader(), new SingularityExecutorCleanupConfigurationLoader()), new SingularityExecutorModule(), new SingularityClientModule());
final Injector injector = Guice.createInjector(Stage.PRODUCTION, new SingularityRunnerBaseModule(new SingularityS3ConfigurationLoader(), new SingularityExecutorConfigurationLoader(), new SingularityExecutorCleanupConfigurationLoader()), new SingularityExecutorModule(), new SingularityClientModule(), new SingularityMesosClientModule());
final SingularityExecutorCleanupRunner runner = injector.getInstance(SingularityExecutorCleanupRunner.class);

LOG.info("Starting cleanup");
Expand Down

0 comments on commit c96e624

Please sign in to comment.