Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mark as active/inactive endpoints to client #1533

Merged
merged 2 commits into from
May 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public class SingularityClient {
private static final String SLAVES_ACTIVATE_FORMAT = SLAVES_FORMAT + "/slave/%s/activate";
private static final String SLAVES_DELETE_FORMAT = SLAVES_FORMAT + "/slave/%s";

private static final String INACTIVE_SLAVES_FORMAT = "http://%s/%s/inactive";

private static final String TASKS_FORMAT = "http://%s/%s/tasks";
private static final String TASKS_KILL_TASK_FORMAT = TASKS_FORMAT + "/task/%s";
private static final String TASKS_GET_ACTIVE_FORMAT = TASKS_FORMAT + "/active";
Expand Down Expand Up @@ -187,6 +189,8 @@ public class SingularityClient {
private static final TypeReference<Collection<SingularityDisasterType>> DISASTERS_COLLECTION = new TypeReference<Collection<SingularityDisasterType>>() {};
private static final TypeReference<Collection<SingularityDisabledAction>> DISABLED_ACTIONS_COLLECTION = new TypeReference<Collection<SingularityDisabledAction>>() {};
private static final TypeReference<SingularityPaginatedResponse<SingularityTaskIdHistory>> PAGINATED_HISTORY = new TypeReference<SingularityPaginatedResponse<SingularityTaskIdHistory>>() {};
private static final TypeReference<Collection<String>> STRING_COLLECTION = new TypeReference<Collection<String>>() {};


private final Random random;
private final Provider<List<String>> hostsProvider;
Expand Down Expand Up @@ -425,12 +429,12 @@ private <T> Optional<T> deleteWithParams(String uri, String type, String id, Opt
}

private HttpResponse put(String uri, String type, Optional<?> body) {
return executeRequest(uri, type, body, Method.PUT);
return executeRequest(uri, type, body, Method.PUT, Optional.absent());
}

private <T> Optional<T> post(String uri, String type, Optional<?> body, Optional<Class<T>> clazz) {
try {
HttpResponse response = executeRequest(uri, type, body, Method.POST);
HttpResponse response = executeRequest(uri, type, body, Method.POST, Optional.absent());

if (clazz.isPresent()) {
return Optional.of(response.getAs(clazz.get()));
Expand All @@ -442,11 +446,15 @@ private <T> Optional<T> post(String uri, String type, Optional<?> body, Optional
return Optional.<T>absent();
}

private HttpResponse postWithParams(String uri, String type, Optional<?> body, Optional<Map<String, Object>> queryParams) {
return executeRequest(uri, type, body, Method.POST, queryParams);
}

private HttpResponse post(String uri, String type, Optional<?> body) {
return executeRequest(uri, type, body, Method.POST);
return executeRequest(uri, type, body, Method.POST, Optional.absent());
}

private HttpResponse executeRequest(String uri, String type, Optional<?> body, Method method) {
private HttpResponse executeRequest(String uri, String type, Optional<?> body, Method method, Optional<Map<String, Object>> queryParams) {

final long start = System.currentTimeMillis();

Expand All @@ -456,6 +464,10 @@ private HttpResponse executeRequest(String uri, String type, Optional<?> body, M
request.setBody(body.get());
}

if (queryParams.isPresent()) {
addQueryParams(request, queryParams.get());
}

addCredentials(request);

HttpResponse response = httpClient.execute(request.build());
Expand Down Expand Up @@ -925,6 +937,29 @@ public Collection<SingularityRequestHistory> getHistoryForRequest(String request
return getCollectionWithParams(requestUri, "request history", maybeQueryParams, REQUEST_HISTORY_COLLECTION);
}

//
// Inactive/Bad Slaves
//

public Collection<String> getInactiveSlaves() {
final String requestUri = String.format(INACTIVE_SLAVES_FORMAT, getHost(), contextPath);
return getCollection(requestUri, "inactiveSlaves", STRING_COLLECTION);
}

public void markSlaveAsInactive(String host) {
final String requestUri = String.format(INACTIVE_SLAVES_FORMAT, getHost(), contextPath);
Map<String, Object> params = new HashMap<>();
params.put("host", host);
deleteWithParams(requestUri, "activateSlave", host, Optional.absent(), Optional.of(params), Optional.of(HttpResponse.class));
}

public void markSlaveAsActive(String host) {
final String requestUri = String.format(INACTIVE_SLAVES_FORMAT, getHost(), contextPath);
Map<String, Object> params = new HashMap<>();
params.put("host", host);
postWithParams(requestUri, "activateSlave", Optional.absent(), Optional.of(params));
}

//
// TASK HISTORY
//
Expand Down