dimas-b commented on code in PR #4648:
URL: https://github.com/apache/polaris/pull/4648#discussion_r3382868783
##########
runtime/service/src/main/java/org/apache/polaris/service/events/PolarisEventListeners.java:
##########
@@ -116,4 +143,43 @@ private void deliverEvent(
public boolean hasListeners(PolarisEventType polarisEventType) {
return enabledEventTypes.contains(polarisEventType);
}
+
+ 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)) {
+ delegate.execute(this::drain);
Review Comment:
This may throw (e.g. `RejectedExecutionException`) and cause the `draining`
flag to be out-of-sync with actual task execution.
##########
runtime/service/src/test/java/org/apache/polaris/service/events/PolarisEventListenersOrderedDeliveryTest.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.polaris.service.events;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Awaitility.await;
+
+import com.google.common.collect.ImmutableMap;
+import io.quarkus.runtime.BlockingOperationControl;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.IntStream;
+import org.apache.polaris.service.events.listeners.PolarisEventListener;
+import org.junit.jupiter.api.Test;
+
+@QuarkusTest
+@TestProfile(
+
PolarisEventListenersOrderedDeliveryTest.PolarisEventListenersOrderedDeliveryTestProfile.class)
+public class PolarisEventListenersOrderedDeliveryTest {
+
+ static final int EVENTS_COUNT = 500;
+
+ public static class PolarisEventListenersOrderedDeliveryTestProfile
+ implements QuarkusTestProfile {
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ return ImmutableMap.<String, String>builder()
+ .put("polaris.event-listener.executor.queue-size",
String.valueOf(EVENTS_COUNT))
+ .put(
+ "polaris.event-listener.types",
+ "ordered-listener1,ordered-listener2,ordered-listener3")
+ .put(
+ "polaris.event-listener.ordered-listener1.enabled-event-types",
+ "AFTER_SEND_NOTIFICATION")
+ .put(
+ "polaris.event-listener.ordered-listener2.enabled-event-types",
+ "AFTER_SEND_NOTIFICATION")
+ .put(
+ "polaris.event-listener.ordered-listener3.enabled-event-types",
+ "AFTER_SEND_NOTIFICATION")
+ .build();
+ }
+
+ @Singleton
+ @Identifier("ordered-listener1")
+ OrderedEventListener orderedEventListener1() {
+ return new OrderedEventListener();
+ }
+
+ @Singleton
+ @Identifier("ordered-listener2")
+ OrderedEventListener orderedEventListener2() {
+ return new OrderedEventListener();
+ }
+
+ @Singleton
+ @Identifier("ordered-listener3")
+ OrderedEventListener orderedEventListener3() {
+ return new OrderedEventListener();
+ }
+ }
+
+ static class OrderedEventListener implements PolarisEventListener {
+
+ static final AttributeKey<Integer> SEQ = new AttributeKey<>("seq",
Integer.class);
+
+ final List<Integer> receivedSequences = new CopyOnWriteArrayList<>();
+ final AtomicInteger receivedEventCount = new AtomicInteger();
+
+ final CountDownLatch pauseLatch = new CountDownLatch(1);
+
+ @Override
+ public void onEvent(PolarisEvent event) {
+ assertThat(BlockingOperationControl.isBlockingAllowed()).isTrue();
+ if (!event.attributes().contains(SEQ)) {
+ return;
+ }
+ try {
+ pauseLatch.await();
Review Comment:
nit: wait timeout
##########
runtime/service/src/main/java/org/apache/polaris/service/events/PolarisEventListeners.java:
##########
@@ -116,4 +143,43 @@ private void deliverEvent(
public boolean hasListeners(PolarisEventType polarisEventType) {
return enabledEventTypes.contains(polarisEventType);
}
+
+ 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)) {
+ delegate.execute(this::drain);
+ }
+ }
+
+ private void drain() {
+ try {
+ Runnable task;
+ while ((task = queue.poll()) != null) {
+ task.run();
Review Comment:
When we have more listeners that executor threads, this can cause starvation
for some listeners, I guess 🤔
Should we cap the iteration size?
##########
runtime/service/src/main/java/org/apache/polaris/service/events/PolarisEventListeners.java:
##########
@@ -116,4 +143,43 @@ private void deliverEvent(
public boolean hasListeners(PolarisEventType polarisEventType) {
return enabledEventTypes.contains(polarisEventType);
}
+
+ private static class ListenerExecutor implements Executor {
Review Comment:
nit: this is a purely local class, I guess it does not really have to
implement `Executor`
##########
runtime/service/src/test/java/org/apache/polaris/service/events/PolarisEventListenersOrderedDeliveryTest.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.polaris.service.events;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Awaitility.await;
+
+import com.google.common.collect.ImmutableMap;
+import io.quarkus.runtime.BlockingOperationControl;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.IntStream;
+import org.apache.polaris.service.events.listeners.PolarisEventListener;
+import org.junit.jupiter.api.Test;
+
+@QuarkusTest
+@TestProfile(
+
PolarisEventListenersOrderedDeliveryTest.PolarisEventListenersOrderedDeliveryTestProfile.class)
+public class PolarisEventListenersOrderedDeliveryTest {
+
+ static final int EVENTS_COUNT = 500;
+
+ public static class PolarisEventListenersOrderedDeliveryTestProfile
+ implements QuarkusTestProfile {
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ return ImmutableMap.<String, String>builder()
+ .put("polaris.event-listener.executor.queue-size",
String.valueOf(EVENTS_COUNT))
+ .put(
+ "polaris.event-listener.types",
+ "ordered-listener1,ordered-listener2,ordered-listener3")
+ .put(
+ "polaris.event-listener.ordered-listener1.enabled-event-types",
+ "AFTER_SEND_NOTIFICATION")
+ .put(
+ "polaris.event-listener.ordered-listener2.enabled-event-types",
+ "AFTER_SEND_NOTIFICATION")
+ .put(
+ "polaris.event-listener.ordered-listener3.enabled-event-types",
+ "AFTER_SEND_NOTIFICATION")
+ .build();
+ }
+
+ @Singleton
+ @Identifier("ordered-listener1")
+ OrderedEventListener orderedEventListener1() {
+ return new OrderedEventListener();
+ }
+
+ @Singleton
+ @Identifier("ordered-listener2")
+ OrderedEventListener orderedEventListener2() {
+ return new OrderedEventListener();
+ }
+
+ @Singleton
+ @Identifier("ordered-listener3")
+ OrderedEventListener orderedEventListener3() {
+ return new OrderedEventListener();
+ }
+ }
+
+ static class OrderedEventListener implements PolarisEventListener {
+
+ static final AttributeKey<Integer> SEQ = new AttributeKey<>("seq",
Integer.class);
+
+ final List<Integer> receivedSequences = new CopyOnWriteArrayList<>();
+ final AtomicInteger receivedEventCount = new AtomicInteger();
+
+ final CountDownLatch pauseLatch = new CountDownLatch(1);
+
+ @Override
+ public void onEvent(PolarisEvent event) {
+ assertThat(BlockingOperationControl.isBlockingAllowed()).isTrue();
+ if (!event.attributes().contains(SEQ)) {
+ return;
+ }
+ try {
+ pauseLatch.await();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ receivedSequences.add(event.attributes().getRequired(SEQ));
+ receivedEventCount.incrementAndGet();
+ }
+ }
+
+ @Inject PolarisEventDispatcher eventDispatcher;
+
+ @Inject
+ @Identifier("ordered-listener1")
+ OrderedEventListener orderedEventListener1;
+
+ @Inject
+ @Identifier("ordered-listener2")
+ OrderedEventListener orderedEventListener2;
+
+ @Inject
+ @Identifier("ordered-listener3")
+ OrderedEventListener orderedEventListener3;
+
+ @Test
+ public void testOrderedEventDelivery() {
+
+ for (int i = 0; i < EVENTS_COUNT; i++) {
+ var attrs = new EventAttributeMap().put(OrderedEventListener.SEQ, i);
+ eventDispatcher.dispatch(
+ new PolarisEvent(PolarisEventType.AFTER_SEND_NOTIFICATION, null,
attrs));
+ }
+
+ orderedEventListener1.pauseLatch.countDown();
+ orderedEventListener2.pauseLatch.countDown();
+ orderedEventListener3.pauseLatch.countDown();
Review Comment:
WDYT about unblocking one of the listeners right away, one in the middle of
the event submission process and one at the very end?
--
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]