use of com.facebook.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 (!started.get() || !needsUpdate.get() || taskStatus.getState().isDone()) {
return;
}
// 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<byte[]> fragment = sendPlan.get() ? Optional.of(planFragment.toBytes(planFragmentCodec)) : Optional.empty();
Optional<TableWriteInfo> writeInfo = sendPlan.get() ? Optional.of(tableWriteInfo) : Optional.empty();
TaskUpdateRequest updateRequest = new TaskUpdateRequest(session.toSessionRepresentation(), session.getIdentity().getExtraCredentials(), fragment, sources, outputBuffers.get(), writeInfo);
byte[] taskUpdateRequestJson = taskUpdateRequestCodec.toBytes(updateRequest);
taskUpdateRequestSize.add(taskUpdateRequestJson.length);
if (taskUpdateRequestJson.length > maxTaskUpdateSizeInBytes) {
failTask(new PrestoException(EXCEEDED_TASK_UPDATE_SIZE_LIMIT, format("TaskUpdate size of %d Bytes has exceeded the limit of %d Bytes", taskUpdateRequestJson.length, maxTaskUpdateSizeInBytes)));
}
if (fragment.isPresent()) {
stats.updateWithPlanSize(taskUpdateRequestJson.length);
} else {
if (ThreadLocalRandom.current().nextDouble() < UPDATE_WITHOUT_PLAN_STATS_SAMPLE_RATE) {
// This is to keep track of the task update size even when the plan fragment is NOT present
stats.updateWithoutPlanSize(taskUpdateRequestJson.length);
}
}
HttpUriBuilder uriBuilder = getHttpUriBuilder(taskStatus);
Request request = setContentTypeHeaders(binaryTransportEnabled, preparePost()).setUri(uriBuilder.build()).setBodyGenerator(createStaticBodyGenerator(taskUpdateRequestJson)).build();
ResponseHandler responseHandler;
if (binaryTransportEnabled) {
responseHandler = createFullSmileResponseHandler((SmileCodec<TaskInfo>) taskInfoCodec);
} else {
responseHandler = createAdaptingJsonResponseHandler((JsonCodec<TaskInfo>) taskInfoCodec);
}
updateErrorTracker.startRequest();
ListenableFuture<BaseResponse<TaskInfo>> future = httpClient.executeAsync(request, responseHandler);
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.getHttpResponseStats(), REMOTE_TASK_ERROR), executor);
}
use of com.facebook.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 = setContentTypeHeaders(binaryTransportEnabled, prepareDelete()).setUri(uriBuilder.build()).build();
scheduleAsyncCleanupRequest(createCleanupBackoff(), request, "abort");
}
}
use of com.facebook.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;
pendingSourceSplitsWeight = 0;
updateTaskStats();
splitQueueHasSpace = true;
whenSplitQueueHasSpace.complete(null, executor);
// cancel pending request
if (currentRequest != null) {
// do not terminate if the request is already running to avoid closing pooled connections
currentRequest.cancel(false);
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 = setContentTypeHeaders(binaryTransportEnabled, prepareDelete()).setUri(uriBuilder.build()).build();
scheduleAsyncCleanupRequest(createCleanupBackoff(), request, "cleanup");
}
use of com.facebook.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 = setContentTypeHeaders(binaryTransportEnabled, prepareDelete()).setUri(uriBuilder.build()).build();
scheduleAsyncCleanupRequest(createCleanupBackoff(), request, "cancel");
}
}
use of com.facebook.airlift.http.client.HttpUriBuilder in project presto by prestodb.
the class HttpLocationFactory method createTaskLocation.
@Override
public URI createTaskLocation(InternalNode node, TaskId taskId) {
requireNonNull(node, "node is null");
requireNonNull(taskId, "taskId is null");
if (taskCommunicationProtocol.equals(CommunicationProtocol.HTTP)) {
return createLegacyTaskLocation(node, taskId);
}
OptionalInt thriftPort = node.getThriftPort();
HttpUriBuilder builder = uriBuilderFrom(node.getInternalUri());
if (taskCommunicationProtocol.equals(CommunicationProtocol.THRIFT) && thriftPort.isPresent()) {
builder.scheme("thrift");
builder.port(thriftPort.getAsInt());
} else {
// fall back to http case
}
return builder.appendPath("/v1/task").appendPath(taskId.toString()).build();
}
Aggregations