adutra commented on code in PR #4648:
URL: https://github.com/apache/polaris/pull/4648#discussion_r3395627890


##########
runtime/service/src/main/java/org/apache/polaris/service/events/PolarisEventListeners.java:
##########
@@ -136,4 +165,55 @@ private void deliverEvent(
   public boolean hasListeners(PolarisEventType eventType) {
     return eventTypesWithListeners.contains(eventType);
   }
+
+  private static class ListenerExecutor implements Executor {
+
+    private final Executor delegate;
+    private final Queue<Runnable> queue;
+    private final AtomicBoolean draining = new AtomicBoolean(false);
+
+    ListenerExecutor(Executor delegate, int capacity) {
+      this.delegate = delegate;
+      this.queue =
+          capacity == -1 ? new ConcurrentLinkedQueue<>() : new 
ArrayBlockingQueue<>(capacity);
+    }
+
+    @Override
+    public void execute(@NonNull Runnable task) {
+      if (!queue.offer(task)) {
+        throw new RejectedExecutionException("Event listener queue is full");
+      }
+      if (draining.compareAndSet(false, true)) {
+        try {
+          delegate.execute(this::drain);
+        } catch (Throwable t) {
+          draining.set(false);
+          throw t;
+        }
+      }
+    }
+
+    private void drain() {
+      try {
+        Runnable task;
+        while ((task = queue.poll()) != null) {
+          task.run();
+        }
+      } finally {
+        draining.set(false);
+        // A producer may have enqueued a task between the last poll() and the 
set(false) above,
+        // and seen draining=true so it did not schedule a drain. Catch that 
here.
+        if (!queue.isEmpty() && draining.compareAndSet(false, true)) {
+          try {
+            // re-schedule for fairness
+            delegate.execute(this::drain);
+          } catch (Exception e) {

Review Comment:
   > This is why I was wondering about reusing Mutiny of some other framework 
here 😅
   
   Any async framework could be leveraged to coordinate failure handling in a 
more composable way, for sure. The only caveat is that `Error`s are not 
propagated, since the error handling is self-contained.
   
   Here is a version with `CompletableFuture`:
   
   ```java
   @Override
   @SuppressWarnings("FutureReturnValueIgnored")
   public void execute(@NonNull Runnable task) {
     if (!queue.offer(task)) {
       throw new RejectedExecutionException("Event listener queue is full");
     }
     if (draining.compareAndSet(false, true)) {
       try {
         scheduleDrain();
       } catch (Throwable t) {
         draining.set(false);
         throw t;
       }
     }
   }
   
   private CompletableFuture<Void> scheduleDrain() {
     return CompletableFuture.runAsync(this::drain, delegate)
         .thenCompose(this::maybeReschedule)
         .exceptionally(this::onError);
   }
   
   private void drain() {
     try {
       Runnable task;
       while ((task = queue.poll()) != null) {
         task.run();
       }
     } finally {
       draining.set(false);
     }
   }
   
   private CompletableFuture<Void> maybeReschedule(Void unused) {
     if (!queue.isEmpty() && draining.compareAndSet(false, true)) {
       // re-schedule for fairness
       return scheduleDrain(); // this may throw; it would fail the stage it's 
running in
     }
     return CompletableFuture.completedFuture(null);
   }
   
   private Void onError(Throwable error) {
     draining.set(false);
     LOGGER.warn("Failed to schedule listener executor drain; tasks may be 
delayed", error);
     return null;
   }
   ```
   
   And here is the version with Mutiny:
   
   ```java
   Override
   public void execute(@NonNull Runnable task) {
     if (!queue.offer(task)) {
       throw new RejectedExecutionException("Event listener queue is full");
     }
     if (draining.compareAndSet(false, true)) {
       scheduleDrain().subscribe().with(unused -> {}); // doesn't throw 
synchronously
     }
   }
   
   private Uni<Void> scheduleDrain() {
     return Uni.createFrom()
         .item(this::drain)
         .runSubscriptionOn(delegate)
         .call(this::maybeReschedule)
         .onFailure()
         .recoverWithItem(this::onError);
   }
   
   private Void drain() {
     try {
       Runnable task;
       while ((task = queue.poll()) != null) {
         task.run();
       }
     } finally {
       draining.set(false);
     }
     return null;
   }
   
   private Uni<Void> maybeReschedule(Object unused) {
     if (!queue.isEmpty() && draining.compareAndSet(false, true)) {
       // re-schedule for fairness
       return scheduleDrain(); // doesn't throw synchronously
     }
     return Uni.createFrom().voidItem();
   }
   
   private Void onError(Throwable error) {
     draining.set(false);
     LOGGER.error(
         "Failed to schedule listener executor drain; event delivery may be 
delayed", error);
     return null;
   }
   ```



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

Reply via email to