예제 #1
0
 @Override
 public void onSuccess(JsonResponse<T> response) {
   try {
     if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) {
       callback.success(response.getValue());
     } else if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.code()) {
       callback.failed(new ServiceUnavailableException(uri));
     } else {
       // Something is broken in the server or the client, so fail the task immediately (includes
       // 500 errors)
       Exception cause = response.getException();
       if (cause == null) {
         if (response.getStatusCode() == HttpStatus.OK.code()) {
           cause =
               new PrestoException(
                   REMOTE_TASK_ERROR, format("Expected response from %s is empty", uri));
         } else {
           cause =
               new PrestoException(
                   REMOTE_TASK_ERROR,
                   format(
                       "Expected response code from %s to be %s, but was %s: %s%n%s",
                       uri,
                       HttpStatus.OK.code(),
                       response.getStatusCode(),
                       response.getStatusMessage(),
                       response.getResponseBody()));
         }
       }
       callback.fatal(cause);
     }
   } catch (Throwable t) {
     // this should never happen
     callback.fatal(t);
   }
 }
예제 #2
0
  @Test
  public void testExecute() throws Exception {
    String expected =
        "{\"columns\":["
            + "{\"name\":\"foo\",\"type\":\"integer\",\"typeSignature\":{\"rawType\":\"integer\",\"arguments\":[],\"typeArguments\":[],\"literalArguments\":[]}},"
            + "{\"name\":\"phoo\",\"type\":\"bigint\",\"typeSignature\":{\"rawType\":\"bigint\",\"arguments\":[],\"typeArguments\":[],\"literalArguments\":[]}},"
            + "{\"name\":\"bar\",\"type\":\"varchar(3)\",\"typeSignature\":"
            + "{\"rawType\":\"varchar\",\"arguments\":[{\"kind\":\"LONG_LITERAL\",\"value\":3}],\"typeArguments\":[],\"literalArguments\":[]}},"
            + "{\"name\":\"baz\",\"type\":\"array(bigint)\",\"typeSignature\":"
            + "{\"rawType\":\"array\",\"arguments\":[{\"kind\":\"TYPE_SIGNATURE\",\"value\":{\"rawType\":\"bigint\",\"arguments\":[],\"typeArguments\":[],\"literalArguments\":[]}}],\"typeArguments\":[{\"rawType\":\"bigint\",\"arguments\":[],\"typeArguments\":[],\"literalArguments\":[]}],\"literalArguments\":[]}}],"
            + "\"data\":[[123, 12300000000, \"abc\",[42,44]]]}\n";
    StringResponse response =
        executeQuery(
            "SELECT 123 foo, 12300000000 phoo, 'abc' bar, CAST(JSON_PARSE('[42,44]') AS ARRAY<BIGINT>) baz");
    assertEquals(response.getStatusCode(), HttpStatus.OK.code());
    assertEquals(response.getHeader(HttpHeaders.CONTENT_TYPE), "application/json");

    ObjectMapper mapper = new ObjectMapper();
    assertEquals(mapper.readTree(response.getBody()), mapper.readTree(expected));
  }
예제 #3
0
    @Override
    public PagesResponse handle(Request request, Response response) {
      // no content means no content was created within the wait period, but query is still ok
      // if job is finished, complete is set in the response
      if (response.getStatusCode() == HttpStatus.NO_CONTENT.code()) {
        return createEmptyPagesResponse(
            getTaskInstanceId(response),
            getToken(response),
            getNextToken(response),
            getComplete(response));
      }

      // otherwise we must have gotten an OK response, everything else is considered fatal
      if (response.getStatusCode() != HttpStatus.OK.code()) {
        StringBuilder body = new StringBuilder();
        try (BufferedReader reader =
            new BufferedReader(new InputStreamReader(response.getInputStream()))) {
          // Get up to 1000 lines for debugging
          for (int i = 0; i < 1000; i++) {
            String line = reader.readLine();
            // Don't output more than 100KB
            if (line == null || body.length() + line.length() > 100 * 1024) {
              break;
            }
            body.append(line + "\n");
          }
        } catch (RuntimeException | IOException e) {
          // Ignored. Just return whatever message we were able to decode
        }
        throw new PageTransportErrorException(
            format(
                "Expected response code to be 200, but was %s %s: %s%n%s",
                response.getStatusCode(),
                response.getStatusMessage(),
                request.getUri(),
                body.toString()));
      }

      // invalid content type can happen when an error page is returned, but is unlikely given the
      // above 200
      String contentType = response.getHeader(CONTENT_TYPE);
      if (contentType == null) {
        throw new PageTransportErrorException(
            format("%s header is not set: %s %s", CONTENT_TYPE, request.getUri(), response));
      }
      if (!mediaTypeMatches(contentType, PRESTO_PAGES_TYPE)) {
        throw new PageTransportErrorException(
            format(
                "Expected %s response from server but got %s: %s",
                PRESTO_PAGES_TYPE, contentType, request.getUri()));
      }

      String taskInstanceId = getTaskInstanceId(response);
      long token = getToken(response);
      long nextToken = getNextToken(response);
      boolean complete = getComplete(response);

      try (SliceInput input = new InputStreamSliceInput(response.getInputStream())) {
        List<Page> pages = ImmutableList.copyOf(readPages(blockEncodingSerde, input));
        return createPagesResponse(taskInstanceId, token, nextToken, pages, complete);
      } catch (IOException e) {
        throw Throwables.propagate(e);
      }
    }