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 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 void checkResponse(String type, HttpResponse response) { if (response.isError()) { throw fail(type, response); } }
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; }
private <T> Optional<T> post(Function<String, String> hostToUri, String type, Optional<?> body, Optional<Class<T>> clazz) { try { HttpResponse response = executeRequest(hostToUri, type, body, Method.POST, Optional.absent()); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } } catch (Exception e) { LOG.warn("Http post failed", e); } return Optional.absent(); }
@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"); }
static String formatResponse(HttpResponse response) { StringBuilder builder = new StringBuilder(); builder.append("------------------------------------------\n"); response.getHeaders().forEach(header -> { builder.append(header.getName()).append(" = ").append(header.getValue()).append("\n"); }); builder.append("------------------------------------------\n"); try { builder.append(ObjectMapperUtils.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(response.getAs(Object.class))); } catch (IOException ex) { throw new RuntimeException("Unable to process response", ex); } return builder.toString(); } }
LOG.warn(String.format("Going to retry failed request to '%s' (Status: %d)", request.getUrl(), response.getStatusCode())); } else { return response;
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); singularityClient = buildClient(); when(response.getRequest()) .thenReturn(request); when(request.getUrl()) .thenReturn(new URI("test-url")); }
public boolean checkAuthForRequestGroupsUpdate(String requestId, SingularityUpdateGroupsRequest updateGroupsRequest) { final Function<String, String> requestUri = (host) -> String.format(REQUEST_GROUPS_UPDATE_AUTH_CHECK_FORMAT, getApiBase(host), requestId); final HttpResponse response = post(requestUri, String.format("check auth for update authorized groups of request %s", requestId), Optional.of(updateGroupsRequest)); return response.isSuccess(); }
@Override public boolean shouldRetry(HttpRequest request, HttpResponse response) { return response.isServerError(); }
/** * * @param requestId * @param runNowRequest * @param minimalReturn - if `true` will return a SingularityPendingRequestParent that is _not_ hydrated with extra task + deploy information * @return */ public SingularityPendingRequestParent runSingularityRequest(String requestId, Optional<SingularityRunNowRequest> runNowRequest, boolean minimalReturn) { final Function<String, String> requestUri = (host) -> String.format(REQUEST_RUN_FORMAT, getApiBase(host), requestId); final HttpResponse response = post(requestUri, String.format("run of request %s", requestId), runNowRequest, ImmutableMap.of("minimal", String.valueOf(minimalReturn))); return response.getAs(SingularityPendingRequestParent.class); }
@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"); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); singularityClient = buildClient(); when(response.getRequest()) .thenReturn(request); when(request.getUrl()) .thenReturn(new URI("test-url")); }
/** * Check if the current client's user is authorized for the specified scope on the specified request * * @param requestId * The request to check authorization on * @param userId * The user whose authorization will be checked * @param scope * The scope to check that `user` has * * @return * true if the user is authorized for scope, false otherwise */ public boolean isUserAuthorized(String requestId, SingularityAuthorizationScope scope) { final Function<String, String> requestUri = (host) -> String.format(AUTH_CHECK_FORMAT, getApiBase(host), requestId); Map<String, Object> params = Collections.singletonMap("scope", scope.name()); HttpResponse response = executeGetSingleWithParams(requestUri, "auth check", "", Optional.of(params)); return response.isSuccess(); }
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 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()); }
public SingularityPendingRequestParent updateAuthorizedGroups(String requestId, SingularityUpdateGroupsRequest updateGroupsRequest) { final Function<String, String> requestUri = (host) -> String.format(REQUEST_GROUPS_UPDATE_FORMAT, getApiBase(host), requestId); final HttpResponse response = post(requestUri, String.format("update authorized groups of request %s", requestId), Optional.of(updateGroupsRequest)); return response.getAs(SingularityPendingRequestParent.class); }