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


##########
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:
   Done in a63fb98f08. I removed the constructor and `_onSubmit`. Nothing in 
the repo used them.



##########
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:
   Done in a63fb98f08. The Javadoc of `contextAwareExecutorService` now says 
that a registered `submit()` passes its own `FutureTask` to `execute()` of the 
wrapped executor, and that this is the same for executors that implement 
`submit()` as a `FutureTask` plus `execute()`.



-- 
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