private HttpResponse executeRequest(Function<String, String> hostToUri, Method method, Optional<?> body, Map<String, ?> queryParams) { HttpRequest.Builder request = HttpRequest.newBuilder().setMethod(method); if (body.isPresent()) { request.setBody(body.get()); } addQueryParams(request, queryParams); addCredentials(request); List<String> hosts = new ArrayList<>(hostsProvider.get()); request .setRetryStrategy(RetryStrategy.NEVER_RETRY) .setMaxRetries(1); try { return httpResponseRetryer.call(() -> { if (hosts.isEmpty()) { // We've tried everything we started with. Look again. hosts.addAll(hostsProvider.get()); } int selection = random.nextInt(hosts.size()); String host = hosts.get(selection); String url = hostToUri.apply(host); hosts.remove(selection); LOG.info("Making {} request to {}", method, url); request.setUrl(url); return httpClient.execute(request.build()); }); } catch (ExecutionException | RetryException exn) { throw new SingularityClientException("Failed request to Singularity", exn); } }
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 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"); }
@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())); }
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 HttpResponse post(String uri, String type, Optional<?> body, Map<String, String> params) { LOG.debug("Posting {} to {}", type, uri); final long start = System.currentTimeMillis(); HttpRequest.Builder request = buildRequest(uri, params).setMethod(Method.POST); if (body.isPresent()) { request.setBody(body.get()); } HttpResponse response = httpClient.execute(request.build()); checkResponse(type, response); LOG.debug("Successfully posted {} in {}ms", type, System.currentTimeMillis() - start); return response; }
private HttpResponse put(String uri, String type, Map<String, String> params) { LOG.debug("Posting {} to {}", type, uri); final long start = System.currentTimeMillis(); HttpRequest.Builder request = buildRequest(uri, params).setMethod(Method.PUT); HttpResponse response = httpClient.execute(request.build()); checkResponse(type, response); LOG.debug("Successfully posted {} in {}ms", type, System.currentTimeMillis() - start); return response; }
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(); }
@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"); }
@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())); }