Search in sources :

Example 6 with Request

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

the class TestHttpPageBufferClient method testExceptionFromResponseHandler.

@Test
public void testExceptionFromResponseHandler() throws Exception {
    TestingTicker ticker = new TestingTicker();
    AtomicReference<Duration> tickerIncrement = new AtomicReference<>(new Duration(0, TimeUnit.SECONDS));
    TestingHttpClient.Processor processor = (input) -> {
        Duration delta = tickerIncrement.get();
        ticker.increment(delta.toMillis(), TimeUnit.MILLISECONDS);
        throw new RuntimeException("Foo");
    };
    CyclicBarrier requestComplete = new CyclicBarrier(2);
    TestingClientCallback callback = new TestingClientCallback(requestComplete);
    URI location = URI.create("http://localhost:8080");
    HttpPageBufferClient client = new HttpPageBufferClient(new TestingHttpClient(processor, executor), new DataSize(10, Unit.MEGABYTE), new Duration(1, TimeUnit.MINUTES), new Duration(1, TimeUnit.MINUTES), location, callback, executor, ticker);
    assertStatus(client, location, "queued", 0, 0, 0, 0, "not scheduled");
    // request processor will throw exception, verify the request is marked a completed
    // this starts the error stopwatch
    client.scheduleRequest();
    requestComplete.await(10, TimeUnit.SECONDS);
    assertEquals(callback.getPages().size(), 0);
    assertEquals(callback.getCompletedRequests(), 1);
    assertEquals(callback.getFinishedBuffers(), 0);
    assertEquals(callback.getFailedBuffers(), 0);
    assertStatus(client, location, "queued", 0, 1, 1, 1, "not scheduled");
    // advance time forward, but not enough to fail the client
    tickerIncrement.set(new Duration(30, TimeUnit.SECONDS));
    // verify that the client has not failed
    client.scheduleRequest();
    requestComplete.await(10, TimeUnit.SECONDS);
    assertEquals(callback.getPages().size(), 0);
    assertEquals(callback.getCompletedRequests(), 2);
    assertEquals(callback.getFinishedBuffers(), 0);
    assertEquals(callback.getFailedBuffers(), 0);
    assertStatus(client, location, "queued", 0, 2, 2, 2, "not scheduled");
    // advance time forward beyond the minimum error duration
    tickerIncrement.set(new Duration(31, TimeUnit.SECONDS));
    // verify that the client has failed
    client.scheduleRequest();
    requestComplete.await(10, TimeUnit.SECONDS);
    assertEquals(callback.getPages().size(), 0);
    assertEquals(callback.getCompletedRequests(), 3);
    assertEquals(callback.getFinishedBuffers(), 0);
    assertEquals(callback.getFailedBuffers(), 1);
    assertInstanceOf(callback.getFailure(), PageTransportTimeoutException.class);
    assertContains(callback.getFailure().getMessage(), WORKER_NODE_ERROR + " (http://localhost:8080/0 - 3 failures, time since last success 61.00s)");
    assertStatus(client, location, "queued", 0, 3, 3, 3, "not scheduled");
}
Also used : Page(com.facebook.presto.spi.Page) TestingTicker(io.airlift.testing.TestingTicker) TestingPagesSerdeFactory.testingPagesSerde(com.facebook.presto.execution.buffer.TestingPagesSerdeFactory.testingPagesSerde) ClientCallback(com.facebook.presto.operator.HttpPageBufferClient.ClientCallback) Assertions.assertContains(io.airlift.testing.Assertions.assertContains) Assertions.assertInstanceOf(io.airlift.testing.Assertions.assertInstanceOf) TimeoutException(java.util.concurrent.TimeoutException) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Unit(io.airlift.units.DataSize.Unit) AtomicReference(java.util.concurrent.atomic.AtomicReference) CONTENT_TYPE(com.google.common.net.HttpHeaders.CONTENT_TYPE) Duration(io.airlift.units.Duration) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Executors.newScheduledThreadPool(java.util.concurrent.Executors.newScheduledThreadPool) PAGE_TOO_LARGE(com.facebook.presto.spi.StandardErrorCode.PAGE_TOO_LARGE) Threads.daemonThreadsNamed(io.airlift.concurrent.Threads.daemonThreadsNamed) Request(io.airlift.http.client.Request) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TestingResponse(io.airlift.http.client.testing.TestingResponse) URI(java.net.URI) PAGE_TRANSPORT_ERROR(com.facebook.presto.spi.StandardErrorCode.PAGE_TRANSPORT_ERROR) AfterClass(org.testng.annotations.AfterClass) CyclicBarrier(java.util.concurrent.CyclicBarrier) BeforeClass(org.testng.annotations.BeforeClass) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) Throwables(com.google.common.base.Throwables) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) PAGE_TRANSPORT_TIMEOUT(com.facebook.presto.spi.StandardErrorCode.PAGE_TRANSPORT_TIMEOUT) WORKER_NODE_ERROR(com.facebook.presto.util.Failures.WORKER_NODE_ERROR) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) DataSize(io.airlift.units.DataSize) List(java.util.List) PRESTO_PAGES(com.facebook.presto.PrestoMediaTypes.PRESTO_PAGES) HttpStatus(io.airlift.http.client.HttpStatus) SerializedPage(com.facebook.presto.execution.buffer.SerializedPage) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Response(io.airlift.http.client.Response) PagesSerde(com.facebook.presto.execution.buffer.PagesSerde) Collections(java.util.Collections) TestingTicker(io.airlift.testing.TestingTicker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(io.airlift.units.Duration) URI(java.net.URI) CyclicBarrier(java.util.concurrent.CyclicBarrier) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

Example 7 with Request

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

the class TestServer method testTransactionSupport.

@Test
public void testTransactionSupport() throws Exception {
    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(jsonCodec(QueryResults.class)));
    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(jsonCodec(QueryResults.class)));
    }
    assertNull(queryResults.getValue().getError());
    assertNotNull(queryResults.getHeader(PRESTO_STARTED_TRANSACTION_ID));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Request(io.airlift.http.client.Request) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 8 with Request

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

the class TestServer method testNoTransactionSupport.

@Test
public void testNoTransactionSupport() throws Exception {
    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(jsonCodec(QueryResults.class)));
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(jsonCodec(QueryResults.class)));
    }
    assertNotNull(queryResults.getError());
    assertEquals(queryResults.getError().getErrorCode(), INCOMPATIBLE_CLIENT.toErrorCode().getCode());
}
Also used : Request(io.airlift.http.client.Request) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 9 with Request

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

the class TestServer method testQuery.

@Test
public void testQuery() throws Exception {
    // 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, DISTRIBUTED_JOIN + "=true," + HASH_PARTITION_COUNT + " = 43").addHeader(PRESTO_PREPARED_STATEMENT, "foo=select * from bar").build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(jsonCodec(QueryResults.class)));
    // get the query info
    QueryInfo queryInfo = server.getQueryManager().getQueryInfo(new QueryId(queryResults.getId()));
    // verify session properties
    assertEquals(queryInfo.getSession().getSystemProperties(), ImmutableMap.builder().put(QUERY_MAX_MEMORY, "1GB").put(DISTRIBUTED_JOIN, "true").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());
    ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
    if (queryResults.getData() != null) {
        data.addAll(queryResults.getData());
    }
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(jsonCodec(QueryResults.class)));
        if (queryResults.getData() != null) {
            data.addAll(queryResults.getData());
        }
    }
    assertNull(queryResults.getError());
    // 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(io.airlift.http.client.Request) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryInfo(com.facebook.presto.execution.QueryInfo) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 10 with Request

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

the class StatementClient method advance.

public boolean advance() {
    URI nextUri = current().getNextUri();
    if (isClosed() || (nextUri == null)) {
        valid.set(false);
        return false;
    }
    Request request = prepareRequest(prepareGet(), nextUri).build();
    Exception cause = null;
    long start = System.nanoTime();
    long attempts = 0;
    do {
        // back-off on retry
        if (attempts > 0) {
            try {
                MILLISECONDS.sleep(attempts * 100);
            } catch (InterruptedException e) {
                try {
                    close();
                } finally {
                    Thread.currentThread().interrupt();
                }
                throw new RuntimeException("StatementClient thread was interrupted");
            }
        }
        attempts++;
        JsonResponse<QueryResults> response;
        try {
            response = httpClient.execute(request, responseHandler);
        } catch (RuntimeException e) {
            cause = e;
            continue;
        }
        if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) {
            processResponse(response);
            return true;
        }
        if (response.getStatusCode() != HttpStatus.SERVICE_UNAVAILABLE.code()) {
            throw requestFailedException("fetching next", request, response);
        }
    } while (((System.nanoTime() - start) < requestTimeoutNanos) && !isClosed());
    gone.set(true);
    throw new RuntimeException("Error fetching next", cause);
}
Also used : Request(io.airlift.http.client.Request) URI(java.net.URI) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Request (io.airlift.http.client.Request)20 URI (java.net.URI)8 HttpUriBuilder (io.airlift.http.client.HttpUriBuilder)5 Test (org.testng.annotations.Test)5 QueryResults (com.facebook.presto.client.QueryResults)4 TaskStatus (com.facebook.presto.execution.TaskStatus)4 TaskUpdateRequest (com.facebook.presto.server.TaskUpdateRequest)4 ImmutableList (com.google.common.collect.ImmutableList)3 JsonResponse (io.airlift.http.client.FullJsonResponseHandler.JsonResponse)3 Duration (io.airlift.units.Duration)3 List (java.util.List)3 SetThreadName (io.airlift.concurrent.SetThreadName)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 PRESTO_PAGES (com.facebook.presto.PrestoMediaTypes.PRESTO_PAGES)1 TaskSource (com.facebook.presto.TaskSource)1 QueryError (com.facebook.presto.client.QueryError)1 QueryInfo (com.facebook.presto.execution.QueryInfo)1 PagesSerde (com.facebook.presto.execution.buffer.PagesSerde)1 SerializedPage (com.facebook.presto.execution.buffer.SerializedPage)1