Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.server;
 
 
 
 import java.util.List;
 
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_CURRENT_STATE;
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_MAX_WAIT;
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_PAGE_NEXT_TOKEN;
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_PAGE_TOKEN;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 import static java.util.concurrent.TimeUnit.SECONDS;

Manages tasks on this worker node
 
 @Path("/v1/task")
 public class TaskResource
 {
     private static final DataSize DEFAULT_MAX_SIZE = new DataSize(10, .);
     private static final Duration DEFAULT_MAX_WAIT_TIME = new Duration(1, );
 
     private final TaskManager taskManager;
 
     @Inject
     public TaskResource(TaskManager taskManager)
     {
         this. = checkNotNull(taskManager"taskManager is null");
     }
 
     @GET
     public List<TaskInfogetAllTaskInfo(@Context() UriInfo uriInfo)
     {
         return .getAllTaskInfo(isFullTaskInfoRequested(uriInfo));
     }
 
     @POST
     @Path("{taskId}")
     public Response createOrUpdateTask(@PathParam("taskId"TaskId taskIdTaskUpdateRequest taskUpdateRequest, @Context UriInfo uriInfo)
     {
         checkNotNull(taskUpdateRequest"taskUpdateRequest is null");
 
         TaskInfo taskInfo = .updateTask(taskUpdateRequest.getSession(),
                 taskId,
                 taskUpdateRequest.getFragment(),
                 taskUpdateRequest.getSources(),
                 taskUpdateRequest.getOutputIds());
 
         return Response.ok().entity(taskInfo).build();
     }
 
     @GET
     @Path("{taskId}")
    public Response getTaskInfo(@PathParam("taskId"TaskId taskId,
            @HeaderParam(TaskState currentState,
            @HeaderParam(Duration maxWait,
            @Context() UriInfo uriInfo)
            throws InterruptedException
    {
        checkNotNull(taskId"taskId is null");
        if (maxWait != null) {
            .waitForStateChange(taskIdcurrentStatemaxWait);
        }
        try {
            TaskInfo taskInfo = .getTaskInfo(taskIdisFullTaskInfoRequested(uriInfo));
            return Response.ok(taskInfo).build();
        }
        catch (NoSuchElementException e) {
            return Response.status(.).build();
        }
    }
    @DELETE
    @Path("{taskId}")
    public Response cancelTask(@PathParam("taskId"TaskId taskId)
    {
        checkNotNull(taskId"taskId is null");
        try {
            TaskInfo taskInfo = .cancelTask(taskId);
            if (taskInfo != null) {
                return Response.ok(taskInfo).build();
            }
        }
        catch (NoSuchElementException ignored) {
        }
        return Response.status(.).build();
    }
    @GET
    @Path("{taskId}/results/{outputId}/{token}")
    public Response getResults(@PathParam("taskId"TaskId taskId,
            @PathParam("outputId"String outputId,
            @PathParam("token"long token)
            throws InterruptedException
    {
        checkNotNull(taskId"taskId is null");
        checkNotNull(outputId"outputId is null");
        long remainingNanos = .roundTo();
        long start = System.nanoTime();
        long end = start + remainingNanos;
        int maxSleepMillis = 1;
        while (remainingNanos > 0) {
            // todo we need a much better way to determine if a task is unknown (e.g. not scheduled yet), done, or there is current no more data
            try {
                BufferResult result = .getTaskResults(taskIdoutputIdtokennew Duration(remainingNanos));
                List<Pagepages = result.getPages();
                if (!pages.isEmpty()) {
                    GenericEntity<?> entity = new GenericEntity<>(pagesnew TypeToken<List<Page>>() {}.getType());
                    return Response.ok(entity)
                            .header(result.getToken())
                            .header(result.getNextToken())
                            .build();
                }
                else if (result.isBufferClosed()) {
                    return Response.status(.)
                            .header(result.getToken())
                            .header(result.getNextToken())
                            .build();
                }
                else {
                    return Response.status(.)
                            .header(result.getToken())
                            .header(result.getNextToken())
                            .build();
                }
            }
            catch (NoSuchElementException | NoSuchBufferException ignored) {
            }
            // task hasn't been scheduled yet.
            // sleep for a bit, before retrying
            .sleep(Math.min(remainingNanos.toNanos(maxSleepMillis)));
            remainingNanos = end - System.nanoTime();
            maxSleepMillis *= 2;
        }
        // task doesn't exist yet and wait time has expired
        return Response.status(.)
                .header(token)
                .header(token)
                .build();
    }
    @DELETE
    @Path("{taskId}/results/{outputId}")
    public Response abortResults(@PathParam("taskId"TaskId taskId, @PathParam("outputId"String outputId)
    {
        checkNotNull(taskId"taskId is null");
        checkNotNull(outputId"outputId is null");
        try {
            TaskInfo taskInfo = .abortTaskResults(taskIdoutputId);
            return Response.ok(taskInfo).build();
        }
        catch (NoSuchElementException e) {
            return Response.status(.).build();
        }
    }
    private boolean isFullTaskInfoRequested(UriInfo uriInfo)
    {
        return uriInfo.getQueryParameters().containsKey("full");
    }
New to GrepCode? Check out our FAQ X