Search in sources :

Example 1 with BufferResult

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

the class TaskResource method getResults.

@GET
@Path("{taskId}/results/{bufferId}/{token}")
@Produces(PRESTO_PAGES)
public void getResults(@PathParam("taskId") TaskId taskId, @PathParam("bufferId") OutputBufferId bufferId, @PathParam("token") final long token, @HeaderParam(PRESTO_MAX_SIZE) DataSize maxSize, @Suspended AsyncResponse asyncResponse) {
    requireNonNull(taskId, "taskId is null");
    requireNonNull(bufferId, "bufferId is null");
    long start = System.nanoTime();
    ListenableFuture<BufferResult> bufferResultFuture = taskManager.getTaskResults(taskId, bufferId, token, maxSize);
    Duration waitTime = randomizeWaitTime(DEFAULT_MAX_WAIT_TIME);
    bufferResultFuture = addTimeout(bufferResultFuture, () -> BufferResult.emptyResults(taskManager.getTaskInstanceId(taskId), token, false), waitTime, timeoutExecutor);
    ListenableFuture<Response> responseFuture = Futures.transform(bufferResultFuture, result -> {
        List<SerializedPage> serializedPages = result.getSerializedPages();
        GenericEntity<?> entity = null;
        Status status;
        if (serializedPages.isEmpty()) {
            status = Status.NO_CONTENT;
        } else {
            entity = new GenericEntity<>(serializedPages, new TypeToken<List<Page>>() {
            }.getType());
            status = Status.OK;
        }
        return Response.status(status).entity(entity).header(PRESTO_TASK_INSTANCE_ID, result.getTaskInstanceId()).header(PRESTO_PAGE_TOKEN, result.getToken()).header(PRESTO_PAGE_NEXT_TOKEN, result.getNextToken()).header(PRESTO_BUFFER_COMPLETE, result.isBufferComplete()).build();
    }, directExecutor());
    // For hard timeout, add an additional time to max wait for thread scheduling contention and GC
    Duration timeout = new Duration(waitTime.toMillis() + ADDITIONAL_WAIT_TIME.toMillis(), MILLISECONDS);
    bindAsyncResponse(asyncResponse, responseFuture, responseExecutor).withTimeout(timeout, Response.status(Status.NO_CONTENT).header(PRESTO_TASK_INSTANCE_ID, taskManager.getTaskInstanceId(taskId)).header(PRESTO_PAGE_TOKEN, token).header(PRESTO_PAGE_NEXT_TOKEN, token).header(PRESTO_BUFFER_COMPLETE, false).build());
    responseFuture.addListener(() -> readFromOutputBufferTime.add(Duration.nanosSince(start)), directExecutor());
    asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));
}
Also used : TaskStatus(com.facebook.presto.execution.TaskStatus) Status(javax.ws.rs.core.Response.Status) JsonCodec(com.facebook.airlift.json.JsonCodec) Page(com.facebook.presto.common.Page) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) Iterables.transform(com.google.common.collect.Iterables.transform) TaskStatus(com.facebook.presto.execution.TaskStatus) SerializedPage(com.facebook.presto.spi.page.SerializedPage) Path(javax.ws.rs.Path) TaskState(com.facebook.presto.execution.TaskState) OutputBufferId(com.facebook.presto.execution.buffer.OutputBuffers.OutputBufferId) APPLICATION_THRIFT_COMPACT(com.facebook.airlift.http.client.thrift.ThriftRequestUtils.APPLICATION_THRIFT_COMPACT) Duration(io.airlift.units.Duration) DEFAULT_MAX_WAIT_TIME(com.facebook.presto.util.TaskUtils.DEFAULT_MAX_WAIT_TIME) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) BufferResult(com.facebook.presto.execution.buffer.BufferResult) DELETE(javax.ws.rs.DELETE) PRESTO_PAGE_TOKEN(com.facebook.presto.client.PrestoHeaders.PRESTO_PAGE_TOKEN) Context(javax.ws.rs.core.Context) MetadataUpdates(com.facebook.presto.metadata.MetadataUpdates) AsyncResponse(javax.ws.rs.container.AsyncResponse) APPLICATION_JACKSON_SMILE(com.facebook.presto.PrestoMediaTypes.APPLICATION_JACKSON_SMILE) GenericEntity(javax.ws.rs.core.GenericEntity) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Suspended(javax.ws.rs.container.Suspended) Codec(com.facebook.airlift.json.Codec) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) DataSize(io.airlift.units.DataSize) List(java.util.List) Response(javax.ws.rs.core.Response) CompletionCallback(javax.ws.rs.container.CompletionCallback) PRESTO_BUFFER_COMPLETE(com.facebook.presto.client.PrestoHeaders.PRESTO_BUFFER_COMPLETE) UriInfo(javax.ws.rs.core.UriInfo) APPLICATION_THRIFT_BINARY(com.facebook.airlift.http.client.thrift.ThriftRequestUtils.APPLICATION_THRIFT_BINARY) PRESTO_CURRENT_STATE(com.facebook.presto.client.PrestoHeaders.PRESTO_CURRENT_STATE) TaskUtils.randomizeWaitTime(com.facebook.presto.util.TaskUtils.randomizeWaitTime) Nested(org.weakref.jmx.Nested) PathParam(javax.ws.rs.PathParam) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) GET(javax.ws.rs.GET) TypeToken(com.google.common.reflect.TypeToken) Inject(javax.inject.Inject) PRESTO_TASK_INSTANCE_ID(com.facebook.presto.client.PrestoHeaders.PRESTO_TASK_INSTANCE_ID) TimeStat(com.facebook.airlift.stats.TimeStat) BoundedExecutor(com.facebook.airlift.concurrent.BoundedExecutor) MoreFutures.addTimeout(com.facebook.airlift.concurrent.MoreFutures.addTimeout) ImmutableList(com.google.common.collect.ImmutableList) Managed(org.weakref.jmx.Managed) PRESTO_MAX_SIZE(com.facebook.presto.client.PrestoHeaders.PRESTO_MAX_SIZE) SmileCodec(com.facebook.airlift.json.smile.SmileCodec) TaskManager(com.facebook.presto.execution.TaskManager) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) INTERNAL(com.facebook.presto.server.security.RoleType.INTERNAL) APPLICATION_THRIFT_FB_COMPACT(com.facebook.airlift.http.client.thrift.ThriftRequestUtils.APPLICATION_THRIFT_FB_COMPACT) PRESTO_PAGE_NEXT_TOKEN(com.facebook.presto.client.PrestoHeaders.PRESTO_PAGE_NEXT_TOKEN) SessionPropertyManager(com.facebook.presto.metadata.SessionPropertyManager) Status(javax.ws.rs.core.Response.Status) POST(javax.ws.rs.POST) AsyncResponseHandler.bindAsyncResponse(com.facebook.airlift.http.server.AsyncResponseHandler.bindAsyncResponse) Executor(java.util.concurrent.Executor) Session(com.facebook.presto.Session) PRESTO_MAX_WAIT(com.facebook.presto.client.PrestoHeaders.PRESTO_MAX_WAIT) Futures(com.google.common.util.concurrent.Futures) PRESTO_PAGES(com.facebook.presto.PrestoMediaTypes.PRESTO_PAGES) TaskId(com.facebook.presto.execution.TaskId) TaskInfo(com.facebook.presto.execution.TaskInfo) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Duration(io.airlift.units.Duration) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) AsyncResponseHandler.bindAsyncResponse(com.facebook.airlift.http.server.AsyncResponseHandler.bindAsyncResponse) BufferResult(com.facebook.presto.execution.buffer.BufferResult) SerializedPage(com.facebook.presto.spi.page.SerializedPage) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with BufferResult

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

the class TestSqlTaskManager method testSimpleQuery.

@Test
public void testSimpleQuery() throws Exception {
    try (SqlTaskManager sqlTaskManager = createSqlTaskManager(new TaskManagerConfig())) {
        TaskId taskId = TASK_ID;
        TaskInfo taskInfo = createTask(sqlTaskManager, taskId, ImmutableSet.of(SPLIT), createInitialEmptyOutputBuffers(PARTITIONED).withBuffer(OUT, 0).withNoMoreBufferIds());
        assertEquals(taskInfo.getTaskStatus().getState(), TaskState.RUNNING);
        taskInfo = sqlTaskManager.getTaskInfo(taskId);
        assertEquals(taskInfo.getTaskStatus().getState(), TaskState.RUNNING);
        BufferResult results = sqlTaskManager.getTaskResults(taskId, OUT, 0, new DataSize(1, Unit.MEGABYTE)).get();
        assertEquals(results.isBufferComplete(), false);
        assertEquals(results.getSerializedPages().size(), 1);
        assertEquals(results.getSerializedPages().get(0).getPositionCount(), 1);
        for (boolean moreResults = true; moreResults; moreResults = !results.isBufferComplete()) {
            results = sqlTaskManager.getTaskResults(taskId, OUT, results.getToken() + results.getSerializedPages().size(), new DataSize(1, Unit.MEGABYTE)).get();
        }
        assertEquals(results.isBufferComplete(), true);
        assertEquals(results.getSerializedPages().size(), 0);
        // complete the task by calling abort on it
        TaskInfo info = sqlTaskManager.abortTaskResults(taskId, OUT);
        assertEquals(info.getOutputBuffers().getState(), BufferState.FINISHED);
        taskInfo = sqlTaskManager.getTaskInfo(taskId, taskInfo.getTaskStatus().getState()).get(1, TimeUnit.SECONDS);
        assertEquals(taskInfo.getTaskStatus().getState(), TaskState.FINISHED);
        taskInfo = sqlTaskManager.getTaskInfo(taskId);
        assertEquals(taskInfo.getTaskStatus().getState(), TaskState.FINISHED);
    }
}
Also used : BufferResult(com.facebook.presto.execution.buffer.BufferResult) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

Example 3 with BufferResult

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

the class ThriftTaskService method getResults.

@ThriftMethod
public ListenableFuture<ThriftBufferResult> getResults(TaskId taskId, OutputBufferId bufferId, long token, long maxSizeInBytes) {
    requireNonNull(taskId, "taskId is null");
    requireNonNull(bufferId, "bufferId is null");
    ListenableFuture<BufferResult> bufferResultFuture = taskManager.getTaskResults(taskId, bufferId, token, new DataSize(maxSizeInBytes, BYTE));
    Duration waitTime = randomizeWaitTime(DEFAULT_MAX_WAIT_TIME);
    bufferResultFuture = addTimeout(bufferResultFuture, () -> BufferResult.emptyResults(taskManager.getTaskInstanceId(taskId), token, false), waitTime, timeoutExecutor);
    return Futures.transform(bufferResultFuture, ThriftBufferResult::fromBufferResult, directExecutor());
}
Also used : ThriftBufferResult(com.facebook.presto.execution.buffer.ThriftBufferResult) BufferResult(com.facebook.presto.execution.buffer.BufferResult) DataSize(io.airlift.units.DataSize) Duration(io.airlift.units.Duration) ThriftBufferResult(com.facebook.presto.execution.buffer.ThriftBufferResult) ThriftMethod(com.facebook.drift.annotations.ThriftMethod)

Example 4 with BufferResult

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

the class TestSqlTask method testBufferCloseOnCancel.

@Test
public void testBufferCloseOnCancel() 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());
    sqlTask.cancel();
    assertEquals(sqlTask.getTaskInfo().getTaskStatus().getState(), TaskState.CANCELED);
    // buffer future will complete.. the event is async so wait a bit for event to propagate
    bufferResult.get(1, SECONDS);
    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) BufferResult(com.facebook.presto.execution.buffer.BufferResult) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

Example 5 with BufferResult

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

the class TestSqlTask method testSimpleQuery.

@Test
public void testSimpleQuery() throws Exception {
    SqlTask sqlTask = createInitialTask();
    TaskInfo taskInfo = sqlTask.updateTask(TEST_SESSION, Optional.of(PLAN_FRAGMENT), ImmutableList.of(new TaskSource(TABLE_SCAN_NODE_ID, ImmutableSet.of(SPLIT), true)), createInitialEmptyOutputBuffers(PARTITIONED).withBuffer(OUT, 0).withNoMoreBufferIds(), Optional.of(new TableWriteInfo(Optional.empty(), Optional.empty(), Optional.empty())));
    assertEquals(taskInfo.getTaskStatus().getState(), TaskState.RUNNING);
    taskInfo = sqlTask.getTaskInfo();
    assertEquals(taskInfo.getTaskStatus().getState(), TaskState.RUNNING);
    BufferResult results = sqlTask.getTaskResults(OUT, 0, new DataSize(1, MEGABYTE)).get();
    assertEquals(results.isBufferComplete(), false);
    assertEquals(results.getSerializedPages().size(), 1);
    assertEquals(results.getSerializedPages().get(0).getPositionCount(), 1);
    for (boolean moreResults = true; moreResults; moreResults = !results.isBufferComplete()) {
        results = sqlTask.getTaskResults(OUT, results.getToken() + results.getSerializedPages().size(), new DataSize(1, MEGABYTE)).get();
    }
    assertEquals(results.getSerializedPages().size(), 0);
    // complete the task by calling abort on it
    TaskInfo info = sqlTask.abortTaskResults(OUT);
    assertEquals(info.getOutputBuffers().getState(), BufferState.FINISHED);
    taskInfo = sqlTask.getTaskInfo(taskInfo.getTaskStatus().getState()).get(1, SECONDS);
    assertEquals(taskInfo.getTaskStatus().getState(), TaskState.FINISHED);
    taskInfo = sqlTask.getTaskInfo();
    assertEquals(taskInfo.getTaskStatus().getState(), TaskState.FINISHED);
}
Also used : SqlTask.createSqlTask(com.facebook.presto.execution.SqlTask.createSqlTask) TableWriteInfo(com.facebook.presto.execution.scheduler.TableWriteInfo) BufferResult(com.facebook.presto.execution.buffer.BufferResult) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

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