Search in sources :

Example 6 with BufferResult

use of com.facebook.presto.execution.buffer.BufferResult in project presto by prestodb.

the class TestSqlTask method testBufferCloseOnFinish.

@Test
public void testBufferCloseOnFinish() throws Exception {
    SqlTask sqlTask = createInitialTask();
    OutputBuffers outputBuffers = createInitialEmptyOutputBuffers(PARTITIONED).withBuffer(OUT, 0).withNoMoreBufferIds();
    updateTask(sqlTask, EMPTY_SOURCES, outputBuffers);
    ListenableFuture<BufferResult> bufferResult = sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE));
    assertFalse(bufferResult.isDone());
    // close the sources (no splits will ever be added)
    updateTask(sqlTask, ImmutableList.of(new TaskSource(TABLE_SCAN_NODE_ID, ImmutableSet.of(), true)), outputBuffers);
    // finish the task by calling abort on it
    sqlTask.abortTaskResults(OUT);
    // buffer will be closed by cancel event (wait for event to fire)
    bufferResult.get(1, SECONDS);
    // verify the buffer is closed
    bufferResult = sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE));
    assertTrue(bufferResult.isDone());
    assertTrue(bufferResult.get().isBufferComplete());
}
Also used : SqlTask.createSqlTask(com.facebook.presto.execution.SqlTask.createSqlTask) OutputBuffers.createInitialEmptyOutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers.createInitialEmptyOutputBuffers) OutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers) BufferResult(com.facebook.presto.execution.buffer.BufferResult) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

Example 7 with BufferResult

use of com.facebook.presto.execution.buffer.BufferResult in project presto by prestodb.

the class TestSqlTask method testBufferNotCloseOnFail.

@Test
public void testBufferNotCloseOnFail() throws Exception {
    SqlTask sqlTask = createInitialTask();
    updateTask(sqlTask, EMPTY_SOURCES, createInitialEmptyOutputBuffers(PARTITIONED).withBuffer(OUT, 0).withNoMoreBufferIds());
    ListenableFuture<BufferResult> bufferResult = sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE));
    assertFalse(bufferResult.isDone());
    TaskState taskState = sqlTask.getTaskInfo().getTaskStatus().getState();
    sqlTask.failed(new Exception("test"));
    assertEquals(sqlTask.getTaskInfo(taskState).get(1, SECONDS).getTaskStatus().getState(), TaskState.FAILED);
    // buffer will not be closed by fail event.  event is async so wait a bit for event to fire
    try {
        assertTrue(bufferResult.get(1, SECONDS).isBufferComplete());
        fail("expected TimeoutException");
    } catch (TimeoutException expected) {
    // expected
    }
    assertFalse(sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE)).isDone());
}
Also used : SqlTask.createSqlTask(com.facebook.presto.execution.SqlTask.createSqlTask) BufferResult(com.facebook.presto.execution.buffer.BufferResult) DataSize(io.airlift.units.DataSize) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 8 with BufferResult

use of com.facebook.presto.execution.buffer.BufferResult in project presto by prestodb.

the class MockExchangeRequestProcessor method handle.

@Override
public Response handle(Request request) {
    if (request.getMethod().equalsIgnoreCase("DELETE")) {
        return new TestingResponse(HttpStatus.NO_CONTENT, ImmutableListMultimap.of(), new byte[0]);
    }
    // verify we got a data size and it parses correctly
    assertTrue(!request.getHeaders().get(PrestoHeaders.PRESTO_MAX_SIZE).isEmpty());
    DataSize maxSize = DataSize.valueOf(request.getHeader(PrestoHeaders.PRESTO_MAX_SIZE));
    assertTrue(maxSize.compareTo(expectedMaxSize) <= 0);
    requestMaxSizes.add(maxSize);
    RequestLocation requestLocation = new RequestLocation(request.getUri());
    URI location = requestLocation.getLocation();
    BufferResult result = buffers.getUnchecked(location).getPages(requestLocation.getSequenceId(), maxSize);
    byte[] bytes = new byte[0];
    HttpStatus status;
    if (!result.getSerializedPages().isEmpty()) {
        DynamicSliceOutput sliceOutput = new DynamicSliceOutput(64);
        PagesSerdeUtil.writeSerializedPages(sliceOutput, result.getSerializedPages());
        bytes = dataChanger.apply(sliceOutput.slice().getBytes());
        status = HttpStatus.OK;
    } else {
        status = HttpStatus.NO_CONTENT;
    }
    return new TestingResponse(status, ImmutableListMultimap.of(CONTENT_TYPE, PRESTO_PAGES, PRESTO_TASK_INSTANCE_ID, String.valueOf(result.getTaskInstanceId()), PRESTO_PAGE_TOKEN, String.valueOf(result.getToken()), PRESTO_PAGE_NEXT_TOKEN, String.valueOf(result.getNextToken()), PRESTO_BUFFER_COMPLETE, String.valueOf(result.isBufferComplete())), bytes);
}
Also used : TestingResponse(com.facebook.airlift.http.client.testing.TestingResponse) BufferResult(com.facebook.presto.execution.buffer.BufferResult) HttpStatus(com.facebook.airlift.http.client.HttpStatus) DataSize(io.airlift.units.DataSize) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) URI(java.net.URI)

Example 9 with BufferResult

use of com.facebook.presto.execution.buffer.BufferResult in project presto by prestodb.

the class AsyncPageTransportServlet method processRequest.

protected void processRequest(String requestURI, TaskId taskId, OutputBufferId bufferId, long token, HttpServletRequest request, HttpServletResponse response) throws IOException {
    DataSize maxSize = DataSize.valueOf(request.getHeader(PRESTO_MAX_SIZE));
    AsyncContext asyncContext = request.startAsync(request, response);
    // wait time to get results
    Duration waitTime = randomizeWaitTime(DEFAULT_MAX_WAIT_TIME);
    asyncContext.setTimeout(waitTime.toMillis() + pageTransportTimeout.toMillis());
    asyncContext.addListener(new AsyncListener() {

        public void onComplete(AsyncEvent event) {
        }

        public void onError(AsyncEvent event) throws IOException {
            String errorMessage = format("Server error to process task result request %s : %s", requestURI, event.getThrowable().getMessage());
            log.error(event.getThrowable(), errorMessage);
            response.sendError(SC_INTERNAL_SERVER_ERROR, errorMessage);
        }

        public void onStartAsync(AsyncEvent event) {
        }

        public void onTimeout(AsyncEvent event) throws IOException {
            String errorMessage = format("Server timeout to process task result request: %s", requestURI);
            log.error(event.getThrowable(), errorMessage);
            response.sendError(SC_INTERNAL_SERVER_ERROR, errorMessage);
        }
    });
    ListenableFuture<BufferResult> bufferResultFuture = taskManager.getTaskResults(taskId, bufferId, token, maxSize);
    bufferResultFuture = addTimeout(bufferResultFuture, () -> BufferResult.emptyResults(taskManager.getTaskInstanceId(taskId), token, false), waitTime, timeoutExecutor);
    ServletOutputStream out = response.getOutputStream();
    addCallback(bufferResultFuture, new FutureCallback<BufferResult>() {

        @Override
        public void onSuccess(BufferResult bufferResult) {
            response.setHeader(CONTENT_TYPE, PRESTO_PAGES);
            response.setHeader(PRESTO_TASK_INSTANCE_ID, bufferResult.getTaskInstanceId());
            response.setHeader(PRESTO_PAGE_TOKEN, String.valueOf(bufferResult.getToken()));
            response.setHeader(PRESTO_PAGE_NEXT_TOKEN, String.valueOf(bufferResult.getNextToken()));
            response.setHeader(PRESTO_BUFFER_COMPLETE, String.valueOf(bufferResult.isBufferComplete()));
            List<SerializedPage> serializedPages = bufferResult.getSerializedPages();
            if (serializedPages.isEmpty()) {
                response.setStatus(SC_NO_CONTENT);
                asyncContext.complete();
            } else {
                int contentLength = (serializedPages.size() * PAGE_METADATA_SIZE) + serializedPages.stream().mapToInt(SerializedPage::getSizeInBytes).sum();
                response.setHeader(CONTENT_LENGTH, String.valueOf(contentLength));
                out.setWriteListener(new SerializedPageWriteListener(serializedPages, asyncContext, out));
            }
        }

        @Override
        public void onFailure(Throwable thrown) {
            String errorMessage = format("Error getting task result from TaskManager for request %s : %s", requestURI, thrown.getMessage());
            log.error(thrown, errorMessage);
            try {
                response.sendError(SC_INTERNAL_SERVER_ERROR, errorMessage);
            } catch (IOException e) {
                log.error(e, "Failed to send response with error code: %s", e.getMessage());
            }
            asyncContext.complete();
        }
    }, responseExecutor);
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) AsyncContext(javax.servlet.AsyncContext) Duration(io.airlift.units.Duration) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent) BufferResult(com.facebook.presto.execution.buffer.BufferResult) DataSize(io.airlift.units.DataSize) AsyncListener(javax.servlet.AsyncListener) List(java.util.List)

Aggregations

BufferResult (com.facebook.presto.execution.buffer.BufferResult)9 DataSize (io.airlift.units.DataSize)9 SqlTask.createSqlTask (com.facebook.presto.execution.SqlTask.createSqlTask)4 Test (org.testng.annotations.Test)4 Duration (io.airlift.units.Duration)3 BoundedExecutor (com.facebook.airlift.concurrent.BoundedExecutor)1 MoreFutures.addTimeout (com.facebook.airlift.concurrent.MoreFutures.addTimeout)1 HttpStatus (com.facebook.airlift.http.client.HttpStatus)1 TestingResponse (com.facebook.airlift.http.client.testing.TestingResponse)1 APPLICATION_THRIFT_BINARY (com.facebook.airlift.http.client.thrift.ThriftRequestUtils.APPLICATION_THRIFT_BINARY)1 APPLICATION_THRIFT_COMPACT (com.facebook.airlift.http.client.thrift.ThriftRequestUtils.APPLICATION_THRIFT_COMPACT)1 APPLICATION_THRIFT_FB_COMPACT (com.facebook.airlift.http.client.thrift.ThriftRequestUtils.APPLICATION_THRIFT_FB_COMPACT)1 AsyncResponseHandler.bindAsyncResponse (com.facebook.airlift.http.server.AsyncResponseHandler.bindAsyncResponse)1 Codec (com.facebook.airlift.json.Codec)1 JsonCodec (com.facebook.airlift.json.JsonCodec)1 SmileCodec (com.facebook.airlift.json.smile.SmileCodec)1 TimeStat (com.facebook.airlift.stats.TimeStat)1 ThriftMethod (com.facebook.drift.annotations.ThriftMethod)1 APPLICATION_JACKSON_SMILE (com.facebook.presto.PrestoMediaTypes.APPLICATION_JACKSON_SMILE)1 PRESTO_PAGES (com.facebook.presto.PrestoMediaTypes.PRESTO_PAGES)1