Search in sources :

Example 1 with Response

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

the class SqlCancelTests method cancelQuery.

private void cancelQuery(String sql) throws InterruptedException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (stopwatch.elapsed(SECONDS) < 30) {
        String findQuerySql = "SELECT query_id from system.runtime.queries WHERE query = '%s' and state = 'RUNNING' LIMIT 2";
        QueryResult queryResult = query(format(findQuerySql, sql));
        checkState(queryResult.getRowsCount() < 2, "Query is executed multiple times");
        if (queryResult.getRowsCount() == 1) {
            String queryId = (String) queryResult.row(0).get(0);
            Response response = queryCanceller.cancel(queryId);
            Assertions.assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT.code());
            return;
        }
        MILLISECONDS.sleep(100L);
    }
    throw new IllegalStateException("Query did not reach running state or maybe it was too quick.");
}
Also used : Response(com.facebook.airlift.http.client.Response) QueryResult(io.prestodb.tempto.query.QueryResult) Stopwatch(com.google.common.base.Stopwatch)

Example 2 with Response

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

the class TestExchangeClient method testInitialRequestLimit.

@Test
public void testInitialRequestLimit() {
    DataSize bufferCapacity = new DataSize(16, MEGABYTE);
    DataSize maxResponseSize = new DataSize(DEFAULT_MAX_PAGE_SIZE_IN_BYTES, BYTE);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    MockExchangeRequestProcessor processor = new MockExchangeRequestProcessor(maxResponseSize) {

        @Override
        public Response handle(Request request) {
            if (!awaitUninterruptibly(countDownLatch, 10, SECONDS)) {
                throw new UncheckedTimeoutException();
            }
            return super.handle(request);
        }
    };
    List<URI> locations = new ArrayList<>();
    int numLocations = 16;
    List<DataSize> expectedMaxSizes = new ArrayList<>();
    // add pages
    for (int i = 0; i < numLocations; i++) {
        URI location = URI.create("http://localhost:" + (8080 + i));
        locations.add(location);
        processor.addPage(location, createPage(DEFAULT_MAX_PAGE_SIZE_IN_BYTES));
        processor.addPage(location, createPage(DEFAULT_MAX_PAGE_SIZE_IN_BYTES));
        processor.addPage(location, createPage(DEFAULT_MAX_PAGE_SIZE_IN_BYTES));
        processor.setComplete(location);
        expectedMaxSizes.add(maxResponseSize);
    }
    try (ExchangeClient exchangeClient = createExchangeClient(processor, bufferCapacity, maxResponseSize)) {
        for (int i = 0; i < numLocations; i++) {
            exchangeClient.addLocation(locations.get(i), TaskId.valueOf("taskid.0.0." + i));
        }
        exchangeClient.noMoreLocations();
        assertFalse(exchangeClient.isClosed());
        long start = System.nanoTime();
        countDownLatch.countDown();
        // wait for a page to be fetched
        do {
            // there is no thread coordination here, so sleep is the best we can do
            assertLessThan(Duration.nanosSince(start), new Duration(5, TimeUnit.SECONDS));
            sleepUninterruptibly(100, MILLISECONDS);
        } while (exchangeClient.getStatus().getBufferedPages() < 16);
        // Client should have sent 16 requests for a single page (0) and gotten them back
        // The memory limit should be hit immediately and then it doesn't fetch the third page from each
        assertEquals(exchangeClient.getStatus().getBufferedPages(), 16);
        assertTrue(exchangeClient.getStatus().getBufferedBytes() > 0);
        List<PageBufferClientStatus> pageBufferClientStatuses = exchangeClient.getStatus().getPageBufferClientStatuses();
        assertEquals(16, pageBufferClientStatuses.stream().filter(status -> status.getPagesReceived() == 1).mapToInt(PageBufferClientStatus::getPagesReceived).sum());
        assertEquals(processor.getRequestMaxSizes(), expectedMaxSizes);
        for (int i = 0; i < numLocations * 3; i++) {
            assertNotNull(getNextPage(exchangeClient));
        }
        do {
            // there is no thread coordination here, so sleep is the best we can do
            assertLessThan(Duration.nanosSince(start), new Duration(5, TimeUnit.SECONDS));
            sleepUninterruptibly(100, MILLISECONDS);
        } while (processor.getRequestMaxSizes().size() < 64);
        for (int i = 0; i < 48; i++) {
            expectedMaxSizes.add(maxResponseSize);
        }
        assertEquals(processor.getRequestMaxSizes(), expectedMaxSizes);
    }
}
Also used : TestingPagesSerdeFactory.testingPagesSerde(com.facebook.presto.execution.buffer.TestingPagesSerdeFactory.testingPagesSerde) Page(com.facebook.presto.common.Page) SerializedPage(com.facebook.presto.spi.page.SerializedPage) Test(org.testng.annotations.Test) Maps.uniqueIndex(com.google.common.collect.Maps.uniqueIndex) BlockAssertions(com.facebook.presto.block.BlockAssertions) Duration(io.airlift.units.Duration) Assertions.assertLessThan(com.facebook.airlift.testing.Assertions.assertLessThan) Uninterruptibles.awaitUninterruptibly(com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly) Executors.newScheduledThreadPool(java.util.concurrent.Executors.newScheduledThreadPool) TestingHttpClient(com.facebook.airlift.http.client.testing.TestingHttpClient) Response(com.facebook.airlift.http.client.Response) URI(java.net.URI) Assert.assertFalse(org.testng.Assert.assertFalse) DEFAULT_MAX_PAGE_SIZE_IN_BYTES(com.facebook.presto.common.block.PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeClass(org.testng.annotations.BeforeClass) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Assert.assertNotNull(org.testng.Assert.assertNotNull) Uninterruptibles.sleepUninterruptibly(com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly) Executors(java.util.concurrent.Executors) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) Threads.daemonThreadsNamed(com.facebook.airlift.concurrent.Threads.daemonThreadsNamed) CountDownLatch(java.util.concurrent.CountDownLatch) DataSize(io.airlift.units.DataSize) List(java.util.List) MoreFutures.tryGetFutureValue(com.facebook.airlift.concurrent.MoreFutures.tryGetFutureValue) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Assert.assertNull(org.testng.Assert.assertNull) MEGABYTE(io.airlift.units.DataSize.Unit.MEGABYTE) Assert.assertEquals(org.testng.Assert.assertEquals) MINUTES(java.util.concurrent.TimeUnit.MINUTES) PrestoException(com.facebook.presto.spi.PrestoException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) SimpleLocalMemoryContext(com.facebook.presto.memory.context.SimpleLocalMemoryContext) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) AggregatedMemoryContext.newSimpleAggregatedMemoryContext(com.facebook.presto.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext) ExecutorService(java.util.concurrent.ExecutorService) AfterClass(org.testng.annotations.AfterClass) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) Request(com.facebook.airlift.http.client.Request) PagesSerde(com.facebook.presto.spi.page.PagesSerde) TaskId(com.facebook.presto.execution.TaskId) Assert.assertTrue(org.testng.Assert.assertTrue) BYTE(io.airlift.units.DataSize.Unit.BYTE) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Request(com.facebook.airlift.http.client.Request) ArrayList(java.util.ArrayList) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) Duration(io.airlift.units.Duration) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

Example 3 with Response

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

the class TaskInfoFetcher method sendMetadataUpdates.

private synchronized void sendMetadataUpdates(MetadataUpdates results) {
    TaskStatus taskStatus = getTaskInfo().getTaskStatus();
    // we already have the final task info
    if (isDone(getTaskInfo())) {
        stop();
        return;
    }
    // outstanding request?
    if (metadataUpdateFuture != null && !metadataUpdateFuture.isDone()) {
        // this should never happen
        return;
    }
    byte[] metadataUpdatesJson = metadataUpdatesCodec.toBytes(results);
    Request request = setContentTypeHeaders(isBinaryTransportEnabled, preparePost()).setUri(uriBuilderFrom(taskStatus.getSelf()).appendPath("metadataresults").build()).setBodyGenerator(createStaticBodyGenerator(metadataUpdatesJson)).build();
    errorTracker.startRequest();
    metadataUpdateFuture = httpClient.executeAsync(request, new ResponseHandler<Response, RuntimeException>() {

        @Override
        public Response handleException(Request request, Exception exception) {
            throw propagate(request, exception);
        }

        @Override
        public Response handle(Request request, Response response) {
            return response;
        }
    });
    currentRequestStartNanos.set(System.nanoTime());
}
Also used : Response(com.facebook.airlift.http.client.Response) BaseResponse(com.facebook.presto.server.smile.BaseResponse) SimpleHttpResponseHandler(com.facebook.presto.server.SimpleHttpResponseHandler) FullSmileResponseHandler.createFullSmileResponseHandler(com.facebook.presto.server.smile.FullSmileResponseHandler.createFullSmileResponseHandler) ResponseHandler(com.facebook.airlift.http.client.ResponseHandler) AdaptingJsonResponseHandler.createAdaptingJsonResponseHandler(com.facebook.presto.server.smile.AdaptingJsonResponseHandler.createAdaptingJsonResponseHandler) Request(com.facebook.airlift.http.client.Request) TaskStatus(com.facebook.presto.execution.TaskStatus)

Example 4 with Response

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

the class HttpRpcShuffleClient method acknowledgeResultsAsync.

@Override
public void acknowledgeResultsAsync(long nextToken) {
    URI uri = uriBuilderFrom(location).appendPath(String.valueOf(nextToken)).appendPath("acknowledge").build();
    httpClient.executeAsync(prepareGet().setUri(uri).build(), new ResponseHandler<Void, RuntimeException>() {

        @Override
        public Void handleException(Request request, Exception exception) {
            log.debug(exception, "Acknowledge request failed: %s", uri);
            return null;
        }

        @Override
        public Void handle(Request request, Response response) {
            if (familyForStatusCode(response.getStatusCode()) != HttpStatus.Family.SUCCESSFUL) {
                log.debug("Unexpected acknowledge response code: %s", response.getStatusCode());
            }
            return null;
        }
    });
}
Also used : PagesResponse.createPagesResponse(com.facebook.presto.operator.PageBufferClient.PagesResponse.createPagesResponse) Response(com.facebook.airlift.http.client.Response) PagesResponse(com.facebook.presto.operator.PageBufferClient.PagesResponse) PagesResponse.createEmptyPagesResponse(com.facebook.presto.operator.PageBufferClient.PagesResponse.createEmptyPagesResponse) Request(com.facebook.airlift.http.client.Request) URI(java.net.URI) ResponseTooLargeException(com.facebook.airlift.http.client.ResponseTooLargeException) IOException(java.io.IOException)

Aggregations

Response (com.facebook.airlift.http.client.Response)4 Request (com.facebook.airlift.http.client.Request)3 URI (java.net.URI)2 MoreFutures.tryGetFutureValue (com.facebook.airlift.concurrent.MoreFutures.tryGetFutureValue)1 Threads.daemonThreadsNamed (com.facebook.airlift.concurrent.Threads.daemonThreadsNamed)1 ResponseHandler (com.facebook.airlift.http.client.ResponseHandler)1 ResponseTooLargeException (com.facebook.airlift.http.client.ResponseTooLargeException)1 TestingHttpClient (com.facebook.airlift.http.client.testing.TestingHttpClient)1 Assertions.assertLessThan (com.facebook.airlift.testing.Assertions.assertLessThan)1 BlockAssertions (com.facebook.presto.block.BlockAssertions)1 Page (com.facebook.presto.common.Page)1 DEFAULT_MAX_PAGE_SIZE_IN_BYTES (com.facebook.presto.common.block.PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES)1 TaskId (com.facebook.presto.execution.TaskId)1 TaskStatus (com.facebook.presto.execution.TaskStatus)1 TestingPagesSerdeFactory.testingPagesSerde (com.facebook.presto.execution.buffer.TestingPagesSerdeFactory.testingPagesSerde)1 AggregatedMemoryContext.newSimpleAggregatedMemoryContext (com.facebook.presto.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext)1 SimpleLocalMemoryContext (com.facebook.presto.memory.context.SimpleLocalMemoryContext)1 PagesResponse (com.facebook.presto.operator.PageBufferClient.PagesResponse)1 PagesResponse.createEmptyPagesResponse (com.facebook.presto.operator.PageBufferClient.PagesResponse.createEmptyPagesResponse)1 PagesResponse.createPagesResponse (com.facebook.presto.operator.PageBufferClient.PagesResponse.createPagesResponse)1