Search in sources :

Example 1 with HttpUriBuilder

use of io.airlift.http.client.HttpUriBuilder in project presto by prestodb.

the class HttpRemoteTask method sendUpdate.

private synchronized void sendUpdate() {
    TaskStatus taskStatus = getTaskStatus();
    // don't update if the task hasn't been started yet or if it is already finished
    if (!needsUpdate.get() || taskStatus.getState().isDone()) {
        return;
    }
    // if we have an old request outstanding, cancel it
    if (currentRequest != null && Duration.nanosSince(currentRequestStartNanos).compareTo(requestTimeout) >= 0) {
        needsUpdate.set(true);
        currentRequest.cancel(true);
        currentRequest = null;
        currentRequestStartNanos = 0;
    }
    // if there is a request already running, wait for it to complete
    if (this.currentRequest != null && !this.currentRequest.isDone()) {
        return;
    }
    // if throttled due to error, asynchronously wait for timeout and try again
    ListenableFuture<?> errorRateLimit = updateErrorTracker.acquireRequestPermit();
    if (!errorRateLimit.isDone()) {
        errorRateLimit.addListener(this::sendUpdate, executor);
        return;
    }
    List<TaskSource> sources = getSources();
    Optional<PlanFragment> fragment = Optional.empty();
    if (sendPlan.get()) {
        fragment = Optional.of(planFragment);
    }
    TaskUpdateRequest updateRequest = new TaskUpdateRequest(session.toSessionRepresentation(), fragment, sources, outputBuffers.get());
    HttpUriBuilder uriBuilder = getHttpUriBuilder(taskStatus);
    Request request = preparePost().setUri(uriBuilder.build()).setHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()).setBodyGenerator(jsonBodyGenerator(taskUpdateRequestCodec, updateRequest)).build();
    updateErrorTracker.startRequest();
    ListenableFuture<JsonResponse<TaskInfo>> future = httpClient.executeAsync(request, createFullJsonResponseHandler(taskInfoCodec));
    currentRequest = future;
    currentRequestStartNanos = System.nanoTime();
    // The needsUpdate flag needs to be set to false BEFORE adding the Future callback since callback might change the flag value
    // and does so without grabbing the instance lock.
    needsUpdate.set(false);
    Futures.addCallback(future, new SimpleHttpResponseHandler<>(new UpdateResponseHandler(sources), request.getUri(), stats), executor);
}
Also used : TaskUpdateRequest(com.facebook.presto.server.TaskUpdateRequest) Request(io.airlift.http.client.Request) TaskUpdateRequest(com.facebook.presto.server.TaskUpdateRequest) TaskStatus(com.facebook.presto.execution.TaskStatus) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) JsonResponse(io.airlift.http.client.FullJsonResponseHandler.JsonResponse) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) TaskSource(com.facebook.presto.TaskSource)

Example 2 with HttpUriBuilder

use of io.airlift.http.client.HttpUriBuilder in project presto by prestodb.

the class HttpRemoteTask method abort.

private synchronized void abort(TaskStatus status) {
    checkState(status.getState().isDone(), "cannot abort task with an incomplete status");
    try (SetThreadName ignored = new SetThreadName("HttpRemoteTask-%s", taskId)) {
        taskStatusFetcher.updateTaskStatus(status);
        // send abort to task
        HttpUriBuilder uriBuilder = getHttpUriBuilder(getTaskStatus());
        Request request = prepareDelete().setUri(uriBuilder.build()).build();
        scheduleAsyncCleanupRequest(new Backoff(MAX_CLEANUP_RETRY_TIME, MAX_CLEANUP_RETRY_TIME), request, "abort");
    }
}
Also used : SetThreadName(io.airlift.concurrent.SetThreadName) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) Request(io.airlift.http.client.Request) TaskUpdateRequest(com.facebook.presto.server.TaskUpdateRequest)

Example 3 with HttpUriBuilder

use of io.airlift.http.client.HttpUriBuilder in project presto by prestodb.

the class HttpRemoteTask method cleanUpTask.

private synchronized void cleanUpTask() {
    checkState(getTaskStatus().getState().isDone(), "attempt to clean up a task that is not done yet");
    // clear pending splits to free memory
    pendingSplits.clear();
    pendingSourceSplitCount = 0;
    partitionedSplitCountTracker.setPartitionedSplitCount(getPartitionedSplitCount());
    splitQueueHasSpace = true;
    whenSplitQueueHasSpace.complete(null, executor);
    // cancel pending request
    if (currentRequest != null) {
        currentRequest.cancel(true);
        currentRequest = null;
        currentRequestStartNanos = 0;
    }
    taskStatusFetcher.stop();
    // The remote task is likely to get a delete from the PageBufferClient first.
    // We send an additional delete anyway to get the final TaskInfo
    HttpUriBuilder uriBuilder = getHttpUriBuilder(getTaskStatus());
    Request request = prepareDelete().setUri(uriBuilder.build()).build();
    scheduleAsyncCleanupRequest(new Backoff(MAX_CLEANUP_RETRY_TIME, MAX_CLEANUP_RETRY_TIME), request, "cleanup");
}
Also used : HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) Request(io.airlift.http.client.Request) TaskUpdateRequest(com.facebook.presto.server.TaskUpdateRequest)

Example 4 with HttpUriBuilder

use of io.airlift.http.client.HttpUriBuilder in project presto by prestodb.

the class HttpRemoteTask method cancel.

@Override
public synchronized void cancel() {
    try (SetThreadName ignored = new SetThreadName("HttpRemoteTask-%s", taskId)) {
        TaskStatus taskStatus = getTaskStatus();
        if (taskStatus.getState().isDone()) {
            return;
        }
        // send cancel to task and ignore response
        HttpUriBuilder uriBuilder = getHttpUriBuilder(taskStatus).addParameter("abort", "false");
        Request request = prepareDelete().setUri(uriBuilder.build()).build();
        scheduleAsyncCleanupRequest(new Backoff(MAX_CLEANUP_RETRY_TIME, MAX_CLEANUP_RETRY_TIME), request, "cancel");
    }
}
Also used : SetThreadName(io.airlift.concurrent.SetThreadName) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) Request(io.airlift.http.client.Request) TaskUpdateRequest(com.facebook.presto.server.TaskUpdateRequest) TaskStatus(com.facebook.presto.execution.TaskStatus)

Example 5 with HttpUriBuilder

use of io.airlift.http.client.HttpUriBuilder in project presto by prestodb.

the class TaskInfoFetcher method sendNextRequest.

private synchronized void sendNextRequest() {
    TaskStatus taskStatus = getTaskInfo().getTaskStatus();
    if (!running) {
        return;
    }
    // we already have the final task info
    if (isDone(getTaskInfo())) {
        stop();
        return;
    }
    // if we have an outstanding request
    if (future != null && !future.isDone()) {
        return;
    }
    // if throttled due to error, asynchronously wait for timeout and try again
    ListenableFuture<?> errorRateLimit = errorTracker.acquireRequestPermit();
    if (!errorRateLimit.isDone()) {
        errorRateLimit.addListener(this::sendNextRequest, executor);
        return;
    }
    HttpUriBuilder httpUriBuilder = uriBuilderFrom(taskStatus.getSelf());
    URI uri = summarizeTaskInfo ? httpUriBuilder.addParameter("summarize").build() : httpUriBuilder.build();
    Request request = prepareGet().setUri(uri).setHeader(CONTENT_TYPE, JSON_UTF_8.toString()).build();
    errorTracker.startRequest();
    future = httpClient.executeAsync(request, createFullJsonResponseHandler(taskInfoCodec));
    currentRequestStartNanos.set(System.nanoTime());
    Futures.addCallback(future, new SimpleHttpResponseHandler<>(this, request.getUri(), stats), executor);
}
Also used : HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) Request(io.airlift.http.client.Request) TaskStatus(com.facebook.presto.execution.TaskStatus) URI(java.net.URI)

Aggregations

HttpUriBuilder (io.airlift.http.client.HttpUriBuilder)5 Request (io.airlift.http.client.Request)5 TaskUpdateRequest (com.facebook.presto.server.TaskUpdateRequest)4 TaskStatus (com.facebook.presto.execution.TaskStatus)3 SetThreadName (io.airlift.concurrent.SetThreadName)2 TaskSource (com.facebook.presto.TaskSource)1 PlanFragment (com.facebook.presto.sql.planner.PlanFragment)1 JsonResponse (io.airlift.http.client.FullJsonResponseHandler.JsonResponse)1 URI (java.net.URI)1