gortiz commented on code in PR #20379:
URL: https://github.com/apache/pinot/pull/20379#discussion_r4697393160


##########
pinot-spi/src/main/java/org/apache/pinot/spi/query/QueryThreadContext.java:
##########
@@ -323,10 +323,46 @@ public static ExecutorService 
contextAwareExecutorService(ExecutorService execut
   ///                                    cause corruption
   public static ExecutorService contextAwareExecutorService(ExecutorService 
executorService,
       boolean registerTaskForCancellation) {
-    Consumer<Future<?>> onSubmit = registerTaskForCancellation
-        ? future -> get().getExecutionContext().addTask(future)
-        : null;
-    return new DecoratorExecutorService(executorService, onSubmit) {
+    return new DecoratorExecutorService(executorService) {

Review Comment:
   Nit: this was the only caller of `DecoratorExecutorService(ExecutorService, 
Consumer<Future<?>>)`. Now that constructor and `_onSubmit` are dead code, so I 
think we can remove them in this PR.



##########
pinot-spi/src/main/java/org/apache/pinot/spi/query/QueryThreadContext.java:
##########
@@ -323,10 +323,46 @@ public static ExecutorService 
contextAwareExecutorService(ExecutorService execut
   ///                                    cause corruption
   public static ExecutorService contextAwareExecutorService(ExecutorService 
executorService,
       boolean registerTaskForCancellation) {
-    Consumer<Future<?>> onSubmit = registerTaskForCancellation
-        ? future -> get().getExecutionContext().addTask(future)
-        : null;
-    return new DecoratorExecutorService(executorService, onSubmit) {
+    return new DecoratorExecutorService(executorService) {
+      @Override
+      public <T> Future<T> submit(Callable<T> task) {
+        if (!registerTaskForCancellation) {
+          return super.submit(task);
+        }
+        QueryExecutionContext executionContext = get().getExecutionContext();
+        return executeRegistered(new RegisteredTask<>(decorate(task), 
executionContext), executionContext);
+      }
+
+      @Override
+      public <T> Future<T> submit(Runnable task, T result) {
+        if (!registerTaskForCancellation) {
+          return super.submit(task, result);
+        }
+        QueryExecutionContext executionContext = get().getExecutionContext();
+        return executeRegistered(new RegisteredTask<>(decorate(task), result, 
executionContext), executionContext);
+      }
+
+      @Override
+      public Future<?> submit(Runnable task) {
+        return submit(task, null);
+      }
+
+      /// Registers the task before it is handed to the executor, so that it 
cannot finish before it is registered.
+      /// If the query is already terminated, [QueryExecutionContext#addTask] 
cancels the task and it does not run.
+      private <T> Future<T> executeRegistered(RegisteredTask<T> task, 
QueryExecutionContext executionContext) {
+        executionContext.addTask(task);
+        if (task.isCancelled()) {
+          return task;
+        }
+        try {
+          _executorService.execute(task);

Review Comment:
   Small behavior change worth knowing: a registered submit now calls 
`execute()` of the wrapped executor, not `submit()`. All the executors we wrap 
in the repo (`QueryExecutorService`, `BoundedAccountingExecutor`, ...) 
implement `submit()` as a `FutureTask` plus `execute()`, so the result is the 
same. Only an executor that overrides `submit()` and not `execute()` would 
behave differently. I do not think we need to change anything, maybe just 
mention it in the javadoc of `contextAwareExecutorService`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to