Search in sources :

Example 11 with Request

use of com.facebook.airlift.http.client.Request in project presto by prestodb.

the class WorkerResource method proxyJsonResponse.

private Response proxyJsonResponse(String nodeId, String workerPath) {
    Set<InternalNode> nodes = nodeManager.getNodes(NodeState.ACTIVE);
    InternalNode node = nodes.stream().filter(n -> n.getNodeIdentifier().equals(nodeId)).findFirst().orElseThrow(() -> new WebApplicationException(NOT_FOUND));
    Request request = prepareGet().setUri(uriBuilderFrom(node.getInternalUri()).appendPath(workerPath).build()).build();
    InputStream responseStream = httpClient.execute(request, new StreamingJsonResponseHandler());
    return Response.ok(responseStream, APPLICATION_JSON_TYPE).build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) InputStream(java.io.InputStream) Request(com.facebook.airlift.http.client.Request) InternalNode(com.facebook.presto.metadata.InternalNode)

Example 12 with Request

use of com.facebook.airlift.http.client.Request in project presto by prestodb.

the class TestServer method testTransactionSupport.

@Test
public void testTransactionSupport() {
    Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").setHeader(PRESTO_TRANSACTION_ID, "none").build();
    JsonResponse<QueryResults> queryResults = client.execute(request, createFullJsonResponseHandler(QUERY_RESULTS_CODEC));
    ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
    while (true) {
        if (queryResults.getValue().getData() != null) {
            data.addAll(queryResults.getValue().getData());
        }
        if (queryResults.getValue().getNextUri() == null) {
            break;
        }
        queryResults = client.execute(prepareGet().setUri(queryResults.getValue().getNextUri()).build(), createFullJsonResponseHandler(QUERY_RESULTS_CODEC));
    }
    assertNull(queryResults.getValue().getError());
    assertNotNull(queryResults.getHeader(PRESTO_STARTED_TRANSACTION_ID));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Request(com.facebook.airlift.http.client.Request) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 13 with Request

use of com.facebook.airlift.http.client.Request in project presto by prestodb.

the class TestServer method testNoTransactionSupport.

@Test
public void testNoTransactionSupport() {
    Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC));
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC));
    }
    assertNotNull(queryResults.getError());
    assertEquals(queryResults.getError().getErrorCode(), INCOMPATIBLE_CLIENT.toErrorCode().getCode());
}
Also used : Request(com.facebook.airlift.http.client.Request) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 14 with Request

use of com.facebook.airlift.http.client.Request in project presto by prestodb.

the class TestServer method testQuery.

@Test
public void testQuery() {
    // start query
    Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").setHeader(PRESTO_CATALOG, "catalog").setHeader(PRESTO_SCHEMA, "schema").setHeader(PRESTO_CLIENT_INFO, "{\"clientVersion\":\"testVersion\"}").addHeader(PRESTO_SESSION, QUERY_MAX_MEMORY + "=1GB").addHeader(PRESTO_SESSION, JOIN_DISTRIBUTION_TYPE + "=partitioned," + HASH_PARTITION_COUNT + " = 43").addHeader(PRESTO_PREPARED_STATEMENT, "foo=select * from bar").addHeader(PRESTO_SESSION_FUNCTION, format("%s=%s", urlEncode(SERIALIZED_SQL_FUNCTION_ID_ADD), urlEncode(SERIALIZED_SQL_FUNCTION_ADD))).build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC));
    ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC));
        if (queryResults.getData() != null) {
            data.addAll(queryResults.getData());
        }
    }
    assertNull(queryResults.getError());
    // get the query info
    BasicQueryInfo queryInfo = server.getQueryManager().getQueryInfo(new QueryId(queryResults.getId()));
    // verify session properties
    assertEquals(queryInfo.getSession().getSystemProperties(), ImmutableMap.builder().put(QUERY_MAX_MEMORY, "1GB").put(JOIN_DISTRIBUTION_TYPE, "partitioned").put(HASH_PARTITION_COUNT, "43").build());
    // verify client info in session
    assertEquals(queryInfo.getSession().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    // verify prepared statements
    assertEquals(queryInfo.getSession().getPreparedStatements(), ImmutableMap.builder().put("foo", "select * from bar").build());
    // verify session functions
    assertEquals(queryInfo.getSession().getSessionFunctions(), ImmutableMap.of(SQL_FUNCTION_ID_ADD, SQL_FUNCTION_ADD));
    // only the system catalog exists by default
    List<List<Object>> rows = data.build();
    assertEquals(rows, ImmutableList.of(ImmutableList.of("system")));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) QueryId(com.facebook.presto.spi.QueryId) Request(com.facebook.airlift.http.client.Request) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 15 with Request

use of com.facebook.airlift.http.client.Request in project presto by prestodb.

the class ResourceManagerProxy method performRequest.

public void performRequest(HttpServletRequest servletRequest, AsyncResponse asyncResponse, URI remoteUri) {
    try {
        BodyGenerator bodyGenerator = new InputStreamBodyGenerator(servletRequest.getInputStream());
        Request request = createRequest(servletRequest, servletRequest.getMethod(), remoteUri, bodyGenerator);
        ListenableFuture<ProxyResponse> proxyResponse = httpClient.executeAsync(request, new ResponseHandler());
        ListenableFuture<Response> future = transform(proxyResponse, this::toResponse, executor);
        setupAsyncResponse(servletRequest, asyncResponse, future);
    } catch (IOException e) {
        asyncResponse.resume(e);
    }
}
Also used : AsyncResponseHandler.bindAsyncResponse(com.facebook.airlift.http.server.AsyncResponseHandler.bindAsyncResponse) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) BodyGenerator(com.facebook.airlift.http.client.BodyGenerator) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(com.facebook.airlift.http.client.Request) IOException(java.io.IOException)

Aggregations

Request (com.facebook.airlift.http.client.Request)40 URI (java.net.URI)16 Test (org.testng.annotations.Test)14 QueryResults (com.facebook.presto.client.QueryResults)7 ResponseHandler (com.facebook.airlift.http.client.ResponseHandler)5 TaskStatus (com.facebook.presto.execution.TaskStatus)5 AdaptingJsonResponseHandler.createAdaptingJsonResponseHandler (com.facebook.presto.server.smile.AdaptingJsonResponseHandler.createAdaptingJsonResponseHandler)5 FullSmileResponseHandler.createFullSmileResponseHandler (com.facebook.presto.server.smile.FullSmileResponseHandler.createFullSmileResponseHandler)5 List (java.util.List)5 HttpUriBuilder (com.facebook.airlift.http.client.HttpUriBuilder)4 Response (com.facebook.airlift.http.client.Response)4 JsonCodec (com.facebook.airlift.json.JsonCodec)4 SimpleHttpResponseHandler (com.facebook.presto.server.SimpleHttpResponseHandler)4 TaskUpdateRequest (com.facebook.presto.server.TaskUpdateRequest)4 NodeState (com.facebook.presto.spi.NodeState)4 Duration (io.airlift.units.Duration)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 SmileCodec (com.facebook.airlift.json.smile.SmileCodec)3 BaseResponse (com.facebook.presto.server.smile.BaseResponse)3 TestingPrestoServer (com.facebook.presto.server.testing.TestingPrestoServer)3