public Optional<SingularityTaskReconciliationStatistics> getTaskReconciliationStatistics() { final Function<String, String> uri = (host) -> String.format(TASK_RECONCILIATION_FORMAT, getApiBase(host)); LOG.info("Fetch task reconciliation statistics from {}", uri); final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(uri, Method.GET, Optional.absent(), Collections.emptyMap()); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse("task reconciliation statistics", response); LOG.info("Got task reconciliation statistics in {}ms", System.currentTimeMillis() - start); return Optional.of(response.getAs(SingularityTaskReconciliationStatistics.class)); }
private <T> Optional<T> getSingleWithParams(Function<String, String> hostToUrl, String type, String id, Optional<Map<String, Object>> queryParams, Class<T> clazz) { final long start = System.currentTimeMillis(); HttpResponse response = executeGetSingleWithParams(hostToUrl, type, id, queryParams); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse(type, response); LOG.info("Got {} {} in {}ms", type, id, System.currentTimeMillis() - start); return Optional.fromNullable(response.getAs(clazz)); }
private <T> Collection<T> getCollectionWithParams(Function<String, String> hostToUrl, String type, Optional<Map<String, Object>> queryParams, TypeReference<Collection<T>> typeReference) { final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(hostToUrl, Method.GET, Optional.absent(), queryParams.or(Collections.emptyMap())); if (response.getStatusCode() == 404) { return ImmutableList.of(); } checkResponse(type, response); LOG.info("Got {} in {}ms", type, System.currentTimeMillis() - start); return response.getAs(typeReference); }
private <T> Optional<T> deleteWithParams(Function<String, String> hostToUrl, String type, String id, Optional<?> body, Optional<Map<String, Object>> queryParams, Optional<Class<T>> clazz) { LOG.info("Deleting {} {} from Singularity", type, id); final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(hostToUrl, Method.DELETE, body, queryParams.or(Collections.emptyMap())); if (response.getStatusCode() == 404) { LOG.info("{} ({}) was not found", type, id); return Optional.absent(); } checkResponse(type, response); LOG.info("Deleted {} ({}) from Singularity in %sms", type, id, System.currentTimeMillis() - start); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } return Optional.absent(); }
private <T> Optional<T> getSingleWithParams(Function<String, String> hostToUrl, String type, String id, Optional<Map<String, Object>> queryParams, TypeReference<T> typeReference) { final long start = System.currentTimeMillis(); HttpResponse response = executeGetSingleWithParams(hostToUrl, type, id, queryParams); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse(type, response); LOG.info("Got {} {} in {}ms", type, id, System.currentTimeMillis() - start); return Optional.fromNullable(response.getAs(typeReference)); }
private SingularityClientException fail(String type, HttpResponse response) { String body = ""; try { body = response.getAsString(); } catch (Exception e) { LOG.warn("Unable to read body", e); } String uri = ""; try { uri = response.getRequest().getUrl().toString(); } catch (Exception e) { LOG.warn("Unable to read uri", e); } throw new SingularityClientException(String.format("Failed '%s' action on Singularity (%s) - code: %s, %s", type, uri, response.getStatusCode(), body), response.getStatusCode()); }
private HttpResponse getFromMesos(String uri, boolean useShortTimeout) { HttpClient currentHttpClient = useShortTimeout ? shortTimeoutHttpClient : httpClient; HttpResponse response = null; final long start = System.currentTimeMillis(); LOG.debug("Fetching {} from mesos", uri); try { response = currentHttpClient.execute(HttpRequest.newBuilder().setUrl(uri).build(), new Options()); LOG.debug("Response {} - {} after {}", response.getStatusCode(), uri, JavaUtils.duration(start)); } catch (Exception e) { throw new MesosClientException(String.format("Exception fetching %s after %s", uri, JavaUtils.duration(start)), e); } if (!response.isSuccess()) { throw new MesosClientException(String.format("Invalid response code from %s : %s", uri, response.getStatusCode())); } return response; }
@Test public void itThrowsAnExceptionOnServerErrors() { when(httpClient.execute(any())) .thenReturn(response); when(response.getStatusCode()) .thenReturn(500); when(response.isError()) .thenReturn(true); assertThatExceptionOfType(SingularityClientException.class) .isThrownBy(() -> singularityClient.pauseSingularityRequest("requestId", Optional.absent())); }
@Test public void itRetriesRequestsThatErrorDueToDeadHost() { when(httpClient.execute(any())) .thenReturn(response); when(response.getStatusCode()) .thenReturn(503) .thenReturn(200); when(response.isServerError()) .thenReturn(true) .thenReturn(false); singularityClient.pauseSingularityRequest("requestId", Optional.absent()); verify(httpClient, times(2)) .execute(requestCaptor.capture()); HttpRequest sentRequest = requestCaptor.getValue(); assertThat(sentRequest.getUrl().toString()) .matches("http://host(1|2)/singularity/v2/api/requests/request/requestId/pause"); }
private BaragonClientException fail(String type, HttpResponse response) { String body = ""; try { body = response.getAsString(); } catch (Exception e) { LOG.warn("Unable to read body", e); } String uri = ""; try { uri = response.getRequest().getUrl().toString(); } catch (Exception e) { LOG.warn("Unable to read uri", e); } throw new BaragonClientException(String.format("Failed '%s' action on Baragon (%s) - code: %s, %s", type, uri, response.getStatusCode(), body), response.getStatusCode()); }
private <T> Optional<T> getSingle(String uri, String type, String id, Class<T> clazz, Map<String, String> queryParams) { checkNotNull(id, String.format("Provide a %s id", type)); LOG.debug("Getting {} {} from {}", type, id, uri); final long start = System.currentTimeMillis(); HttpResponse response = httpClient.execute(buildRequest(uri, queryParams).build()); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse(type, response); LOG.debug("Got {} {} in {}ms", type, id, System.currentTimeMillis() - start); return Optional.fromNullable(response.getAs(clazz)); }
private <T> Collection<T> getCollection(String uri, String type, TypeReference<Collection<T>> typeReference) { LOG.debug("Getting all {} from {}", type, uri); final long start = System.currentTimeMillis(); HttpResponse response = httpClient.execute(buildRequest(uri).build()); if (response.getStatusCode() == 404) { throw new BaragonClientException(String.format("%s not found", type), 404); } checkResponse(type, response); LOG.debug("Got {} in {}ms", type, System.currentTimeMillis() - start); return response.getAs(typeReference); }
private <T> Optional<T> delete(String uri, String type, String id, Map<String, String> queryParams, Optional<Class<T>> clazz) { LOG.debug("Deleting {} {} from {}", type, id, uri); final long start = System.currentTimeMillis(); HttpRequest.Builder request = buildRequest(uri, queryParams).setMethod(Method.DELETE); HttpResponse response = httpClient.execute(request.build()); if (response.getStatusCode() == 404) { LOG.debug("{} ({}) was not found", type, id); return Optional.absent(); } checkResponse(type, response); LOG.debug("Deleted {} ({}) from Baragon in %sms", type, id, System.currentTimeMillis() - start); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } return Optional.absent(); }
LOG.warn(String.format("Going to retry failed request to '%s' (Status: %d)", request.getUrl(), response.getStatusCode())); } else { return response;
@Test public void itThrowsAnExceptionOnServerErrors() { when(httpClient.execute(any())) .thenReturn(response); when(response.getStatusCode()) .thenReturn(500); when(response.isError()) .thenReturn(true); assertThatExceptionOfType(SingularityClientException.class) .isThrownBy(() -> singularityClient.pauseSingularityRequest("requestId", Optional.absent())); }
@Test public void itRetriesRequestsThatErrorDueToDeadHost() { when(httpClient.execute(any())) .thenReturn(response); when(response.getStatusCode()) .thenReturn(503) .thenReturn(200); when(response.isServerError()) .thenReturn(true) .thenReturn(false); singularityClient.pauseSingularityRequest("requestId", Optional.absent()); verify(httpClient, times(2)) .execute(requestCaptor.capture()); HttpRequest sentRequest = requestCaptor.getValue(); assertThat(sentRequest.getUrl().toString()) .matches("http://host(1|2)/singularity/v2/api/requests/request/requestId/pause"); }