|
| 1 | +package io.trino.server.remotetask; |
| 2 | + |
| 3 | +import com.google.errorprone.annotations.concurrent.GuardedBy; |
| 4 | +import io.airlift.http.client.HttpClient; |
| 5 | +import io.airlift.http.client.Request; |
| 6 | +import io.airlift.http.client.StatusResponseHandler; |
| 7 | +import io.airlift.log.Logger; |
| 8 | +import io.opentelemetry.api.trace.SpanBuilder; |
| 9 | +import io.trino.execution.TaskId; |
| 10 | +import io.trino.execution.TaskState; |
| 11 | + |
| 12 | +import java.net.URI; |
| 13 | +import java.util.concurrent.ScheduledExecutorService; |
| 14 | +import java.util.concurrent.ThreadLocalRandom; |
| 15 | +import java.util.function.Supplier; |
| 16 | + |
| 17 | +import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom; |
| 18 | +import static io.airlift.http.client.Request.Builder.preparePost; |
| 19 | +import static java.util.Objects.requireNonNull; |
| 20 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 21 | + |
| 22 | +public class RemoteTaskCleaner |
| 23 | +{ |
| 24 | + private static final Logger log = Logger.get(RemoteTaskCleaner.class); |
| 25 | + |
| 26 | + private static final long TASK_CLEANUP_DELAY_VARIANCE_MILLIS = 10_000; |
| 27 | + |
| 28 | + private final TaskId taskId; |
| 29 | + private final URI taskUri; |
| 30 | + private final HttpClient httpClient; |
| 31 | + private final ScheduledExecutorService executor; |
| 32 | + private final Supplier<SpanBuilder> spanBuilderFactory; |
| 33 | + |
| 34 | + @GuardedBy("this") |
| 35 | + private boolean taskStatusFetcherStopped; |
| 36 | + |
| 37 | + @GuardedBy("this") |
| 38 | + private boolean taskInfoFetcherStopped; |
| 39 | + |
| 40 | + @GuardedBy("this") |
| 41 | + private boolean dynamidFilterFetcherStopped; |
| 42 | + |
| 43 | + @GuardedBy("this") |
| 44 | + private TaskState taskState; |
| 45 | + |
| 46 | + public RemoteTaskCleaner(TaskId taskId, URI taskUri, HttpClient httpClient, ScheduledExecutorService executor, Supplier<SpanBuilder> spanBuilderFactory) |
| 47 | + { |
| 48 | + this.taskId = requireNonNull(taskId, "taskId is null"); |
| 49 | + this.taskUri = requireNonNull(taskUri, "taskUri is null"); |
| 50 | + this.httpClient = requireNonNull(httpClient, "httpClient is null"); |
| 51 | + this.executor = requireNonNull(executor, "executor is null"); |
| 52 | + this.spanBuilderFactory = requireNonNull(spanBuilderFactory, "spanBuilderFactory is null"); |
| 53 | + } |
| 54 | + |
| 55 | + public synchronized void markTaskStatusFetcherStopped(TaskState taskState) |
| 56 | + { |
| 57 | + if (taskStatusFetcherStopped) { |
| 58 | + return; |
| 59 | + } |
| 60 | + taskStatusFetcherStopped = true; |
| 61 | + this.taskState = taskState; |
| 62 | + cleanupIfReady(); |
| 63 | + } |
| 64 | + |
| 65 | + public synchronized void markTaskInfoFetcherStopped() |
| 66 | + { |
| 67 | + if (taskInfoFetcherStopped) { |
| 68 | + return; |
| 69 | + } |
| 70 | + taskInfoFetcherStopped = true; |
| 71 | + cleanupIfReady(); |
| 72 | + } |
| 73 | + |
| 74 | + public synchronized void markDynamidFilterFetcherStopped() |
| 75 | + { |
| 76 | + if (dynamidFilterFetcherStopped) { |
| 77 | + return; |
| 78 | + } |
| 79 | + dynamidFilterFetcherStopped = true; |
| 80 | + cleanupIfReady(); |
| 81 | + } |
| 82 | + |
| 83 | + @GuardedBy("this") |
| 84 | + private void cleanupIfReady() |
| 85 | + { |
| 86 | + if (taskState != TaskState.FINISHED) { |
| 87 | + // we do not perform early cleanup if task did not finish successfully. |
| 88 | + // other workers may still reach out for the results; and we have no control over that. |
| 89 | + return; |
| 90 | + } |
| 91 | + if (taskStatusFetcherStopped && taskInfoFetcherStopped && dynamidFilterFetcherStopped) { |
| 92 | + scheduleCleanupRequest(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void scheduleCleanupRequest() |
| 97 | + { |
| 98 | + executor.schedule( |
| 99 | + () -> { |
| 100 | + Request request = preparePost() |
| 101 | + .setUri(uriBuilderFrom(taskUri) |
| 102 | + .appendPath("/cleanup") |
| 103 | + .build()) |
| 104 | + .setSpanBuilder(spanBuilderFactory.get()) |
| 105 | + .build(); |
| 106 | + |
| 107 | + StatusResponseHandler.StatusResponse response = httpClient.execute(request, StatusResponseHandler.createStatusResponseHandler()); |
| 108 | + if (response.getStatusCode() != 200) { |
| 109 | + log.warn("Failed to cleanup task %s: %s", taskId, response.getStatusCode()); |
| 110 | + } |
| 111 | + return null; |
| 112 | + }, |
| 113 | + ThreadLocalRandom.current().nextLong(TASK_CLEANUP_DELAY_VARIANCE_MILLIS), |
| 114 | + MILLISECONDS); |
| 115 | + } |
| 116 | +} |
0 commit comments