Search in sources :

Example 1 with INTERNAL_ONLY

use of io.trino.server.security.ResourceSecurity.AccessType.INTERNAL_ONLY in project trino by trinodb.

the class TaskResource method getResults.

@ResourceSecurity(INTERNAL_ONLY)
@GET
@Path("{taskId}/results/{bufferId}/{token}")
@Produces(TRINO_PAGES)
public void getResults(@PathParam("taskId") TaskId taskId, @PathParam("bufferId") OutputBufferId bufferId, @PathParam("token") long token, @HeaderParam(TRINO_MAX_SIZE) DataSize maxSize, @Suspended AsyncResponse asyncResponse) {
    requireNonNull(taskId, "taskId is null");
    requireNonNull(bufferId, "bufferId is null");
    if (injectFailure(taskManager.getTraceToken(taskId), taskId, RequestType.GET_RESULTS, asyncResponse)) {
        return;
    }
    TaskState state = taskManager.getTaskStatus(taskId).getState();
    boolean taskFailed = state == TaskState.ABORTED || state == TaskState.FAILED;
    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<Slice> serializedPages = result.getSerializedPages();
        GenericEntity<?> entity = null;
        Status status;
        if (serializedPages.isEmpty()) {
            status = Status.NO_CONTENT;
        } else {
            entity = new GenericEntity<>(serializedPages, new TypeToken<List<Slice>>() {
            }.getType());
            status = Status.OK;
        }
        return Response.status(status).entity(entity).header(TRINO_TASK_INSTANCE_ID, result.getTaskInstanceId()).header(TRINO_PAGE_TOKEN, result.getToken()).header(TRINO_PAGE_NEXT_TOKEN, result.getNextToken()).header(TRINO_BUFFER_COMPLETE, result.isBufferComplete()).header(TRINO_TASK_FAILED, taskFailed).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(TRINO_TASK_INSTANCE_ID, taskManager.getTaskInstanceId(taskId)).header(TRINO_PAGE_TOKEN, token).header(TRINO_PAGE_NEXT_TOKEN, token).header(TRINO_BUFFER_COMPLETE, false).header(TRINO_TASK_FAILED, taskFailed).build());
    responseFuture.addListener(() -> readFromOutputBufferTime.add(Duration.nanosSince(start)), directExecutor());
    asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));
}
Also used : Status(javax.ws.rs.core.Response.Status) TaskStatus(io.trino.execution.TaskStatus) ResourceSecurity(io.trino.server.security.ResourceSecurity) Produces(javax.ws.rs.Produces) Iterables.transform(com.google.common.collect.Iterables.transform) TRINO_PAGES(io.trino.TrinoMediaTypes.TRINO_PAGES) Path(javax.ws.rs.Path) OutputBufferId(io.trino.execution.buffer.OutputBuffers.OutputBufferId) Duration(io.airlift.units.Duration) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) BoundedExecutor(io.airlift.concurrent.BoundedExecutor) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) INTERNAL_ONLY(io.trino.server.security.ResourceSecurity.AccessType.INTERNAL_ONLY) FailureInjector(io.trino.execution.FailureInjector) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) TRINO_PAGE_TOKEN(io.trino.server.InternalHeaders.TRINO_PAGE_TOKEN) AsyncResponse(javax.ws.rs.container.AsyncResponse) GenericEntity(javax.ws.rs.core.GenericEntity) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) TaskId(io.trino.execution.TaskId) Suspended(javax.ws.rs.container.Suspended) InjectedFailure(io.trino.execution.FailureInjector.InjectedFailure) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) TRINO_TASK_INSTANCE_ID(io.trino.server.InternalHeaders.TRINO_TASK_INSTANCE_ID) SessionPropertyManager(io.trino.metadata.SessionPropertyManager) DataSize(io.airlift.units.DataSize) List(java.util.List) Response(javax.ws.rs.core.Response) CompletionCallback(javax.ws.rs.container.CompletionCallback) TaskInfo(io.trino.execution.TaskInfo) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) TRINO_CURRENT_VERSION(io.trino.server.InternalHeaders.TRINO_CURRENT_VERSION) AsyncResponseHandler.bindAsyncResponse(io.airlift.jaxrs.AsyncResponseHandler.bindAsyncResponse) Session(io.trino.Session) Nested(org.weakref.jmx.Nested) PathParam(javax.ws.rs.PathParam) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Slice(io.airlift.slice.Slice) TRINO_TASK_FAILED(io.trino.server.InternalHeaders.TRINO_TASK_FAILED) Logger(io.airlift.log.Logger) GET(javax.ws.rs.GET) SqlTaskManager(io.trino.execution.SqlTaskManager) TypeToken(com.google.common.reflect.TypeToken) TRINO_PAGE_NEXT_TOKEN(io.trino.server.InternalHeaders.TRINO_PAGE_NEXT_TOKEN) TRINO_MAX_SIZE(io.trino.server.InternalHeaders.TRINO_MAX_SIZE) Inject(javax.inject.Inject) BufferResult(io.trino.execution.buffer.BufferResult) ImmutableList(com.google.common.collect.ImmutableList) Managed(org.weakref.jmx.Managed) TRINO_MAX_WAIT(io.trino.server.InternalHeaders.TRINO_MAX_WAIT) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Objects.requireNonNull(java.util.Objects.requireNonNull) TaskState(io.trino.execution.TaskState) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MoreFutures.addTimeout(io.airlift.concurrent.MoreFutures.addTimeout) TimeStat(io.airlift.stats.TimeStat) Status(javax.ws.rs.core.Response.Status) TRINO_BUFFER_COMPLETE(io.trino.server.InternalHeaders.TRINO_BUFFER_COMPLETE) POST(javax.ws.rs.POST) Executor(java.util.concurrent.Executor) TaskStatus(io.trino.execution.TaskStatus) Futures(com.google.common.util.concurrent.Futures) 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(io.airlift.jaxrs.AsyncResponseHandler.bindAsyncResponse) BufferResult(io.trino.execution.buffer.BufferResult) Slice(io.airlift.slice.Slice) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) TaskState(io.trino.execution.TaskState) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ResourceSecurity(io.trino.server.security.ResourceSecurity)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Iterables.transform (com.google.common.collect.Iterables.transform)1 TypeToken (com.google.common.reflect.TypeToken)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors.directExecutor (com.google.common.util.concurrent.MoreExecutors.directExecutor)1 BoundedExecutor (io.airlift.concurrent.BoundedExecutor)1 MoreFutures.addTimeout (io.airlift.concurrent.MoreFutures.addTimeout)1 AsyncResponseHandler.bindAsyncResponse (io.airlift.jaxrs.AsyncResponseHandler.bindAsyncResponse)1 Logger (io.airlift.log.Logger)1 Slice (io.airlift.slice.Slice)1 TimeStat (io.airlift.stats.TimeStat)1 DataSize (io.airlift.units.DataSize)1 Duration (io.airlift.units.Duration)1 Session (io.trino.Session)1 TRINO_PAGES (io.trino.TrinoMediaTypes.TRINO_PAGES)1 FailureInjector (io.trino.execution.FailureInjector)1 InjectedFailure (io.trino.execution.FailureInjector.InjectedFailure)1 SqlTaskManager (io.trino.execution.SqlTaskManager)1 TaskId (io.trino.execution.TaskId)1