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


##########
runtime/service/src/test/java/org/apache/polaris/service/events/listeners/aws/cloudwatch/AwsCloudWatchEventListenerTest.java:
##########
@@ -86,17 +92,22 @@ class AwsCloudWatchEventListenerTest {
 
   private ExecutorService executorService;
   private AutoCloseable mockitoContext;
+  private ObjectMapper objectMapper;
 
   @BeforeEach
   void setUp() {
     mockitoContext = MockitoAnnotations.openMocks(this);
     executorService = Executors.newSingleThreadExecutor();
 
+    // Build the test mapper and apply the same customizations Quarkus would
+    objectMapper = new ObjectMapper();

Review Comment:
   nit: could be a final field.



##########
runtime/service/src/main/java/org/apache/polaris/service/events/listeners/aws/cloudwatch/AwsCloudWatchEventListener.java:
##########
@@ -71,13 +78,38 @@ public class AwsCloudWatchEventListener extends 
PropertyMapEventListener {
   public AwsCloudWatchEventListener(
       AwsCloudWatchConfiguration config,
       Clock clock,
-      PolarisIcebergObjectMapperCustomizer customizer) {
+      PolarisIcebergObjectMapperCustomizer customizer,

Review Comment:
   You don't need to pass the customizer, the customizer will be invoked for 
you by Quarkus. Just inject the object mapper.



##########
runtime/service/src/main/java/org/apache/polaris/service/events/listeners/AllEventsForwardingListener.java:
##########
@@ -0,0 +1,854 @@
+/*
+ * 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.listeners;
+
+import org.apache.polaris.service.events.AfterAttemptTaskEvent;
+import org.apache.polaris.service.events.BeforeAttemptTaskEvent;
+import org.apache.polaris.service.events.BeforeLimitRequestRateEvent;
+import org.apache.polaris.service.events.CatalogGenericTableServiceEvents;
+import org.apache.polaris.service.events.CatalogPolicyServiceEvents;
+import org.apache.polaris.service.events.CatalogsServiceEvents;
+import org.apache.polaris.service.events.IcebergRestCatalogEvents;
+import org.apache.polaris.service.events.PolarisEvent;
+import org.apache.polaris.service.events.PrincipalRolesServiceEvents;
+import org.apache.polaris.service.events.PrincipalsServiceEvents;
+
+/**
+ * Base class for event listeners that with to generically forward all {@link 
PolarisEvent
+ * PolarisEvents} to an external sink.
+ *
+ * <p>This design follows the Template Method pattern, centralizing shared 
control flow in the base
+ * class while allowing subclasses to supply the event-specific behavior.
+ */
+public abstract class AllEventsForwardingListener implements 
PolarisEventListener {
+
+  /** Subclasses implement the actual logic once, generically. */
+  protected abstract void handle(PolarisEvent event);
+
+  /** Optional filter (config-based). Default: handle all. */
+  protected boolean shouldHandle(Object event) {

Review Comment:
   ```suggestion
     protected boolean shouldHandle(PolarisEvent event) {
   ```



##########
runtime/service/src/main/java/org/apache/polaris/service/events/listeners/aws/cloudwatch/AwsCloudWatchEventListener.java:
##########
@@ -71,13 +78,38 @@ public class AwsCloudWatchEventListener extends 
PropertyMapEventListener {
   public AwsCloudWatchEventListener(
       AwsCloudWatchConfiguration config,
       Clock clock,
-      PolarisIcebergObjectMapperCustomizer customizer) {
+      PolarisIcebergObjectMapperCustomizer customizer,
+      ObjectMapper mapper) {
     this.logStream = config.awsCloudWatchLogStream();
     this.logGroup = config.awsCloudWatchLogGroup();
     this.region = Region.of(config.awsCloudWatchRegion());
     this.synchronousMode = config.synchronousMode();
     this.clock = clock;
+    this.objectMapper = mapper;
     customizer.customize(this.objectMapper);
+    this.listenToAllEvents =
+        config.eventTypes().isEmpty()
+            || config.eventTypes().map(Set::isEmpty).orElse(true)
+            || config.eventTypes().get().stream().anyMatch(e -> e == 
PolarisEvent.class);
+    this.allowedEventTypes = listenToAllEvents ? Set.of() : 
Set.copyOf(config.eventTypes().get());
+  }
+
+  @Override
+  protected boolean shouldHandle(Object event) {
+    if (!(event instanceof PolarisEvent polarisEvent)) {
+      return false;
+    }
+
+    if (this.listenToAllEvents) {
+      return true;
+    }
+    Class<? extends PolarisEvent> actualType = polarisEvent.getClass();

Review Comment:
   Note: this is probably a bit inefficient under a high load. Switching to 
event type IDs should help because the lookup would be faster when using an 
EnumSet.



##########
runtime/service/src/main/java/org/apache/polaris/service/events/listeners/aws/cloudwatch/AwsCloudWatchEventListener.java:
##########
@@ -86,6 +118,14 @@ void start() {
     ensureLogGroupAndStream();
   }
 
+  @PostConstruct
+  void verifyMapper() {

Review Comment:
   Unnecessary.



##########
runtime/service/src/main/java/org/apache/polaris/service/events/listeners/AllEventsForwardingListener.java:
##########
@@ -0,0 +1,854 @@
+/*
+ * 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.listeners;
+
+import org.apache.polaris.service.events.AfterAttemptTaskEvent;
+import org.apache.polaris.service.events.BeforeAttemptTaskEvent;
+import org.apache.polaris.service.events.BeforeLimitRequestRateEvent;
+import org.apache.polaris.service.events.CatalogGenericTableServiceEvents;
+import org.apache.polaris.service.events.CatalogPolicyServiceEvents;
+import org.apache.polaris.service.events.CatalogsServiceEvents;
+import org.apache.polaris.service.events.IcebergRestCatalogEvents;
+import org.apache.polaris.service.events.PolarisEvent;
+import org.apache.polaris.service.events.PrincipalRolesServiceEvents;
+import org.apache.polaris.service.events.PrincipalsServiceEvents;
+
+/**
+ * Base class for event listeners that with to generically forward all {@link 
PolarisEvent

Review Comment:
   ```suggestion
    * Base class for event listeners that wish to generically forward all 
{@link PolarisEvent
   ```



##########
runtime/defaults/src/main/resources/application.properties:
##########
@@ -145,6 +145,7 @@ polaris.event-listener.type=no-op
 # 
polaris.event-listener.aws-cloudwatch.log-stream=polaris-cloudwatch-default-stream
 # polaris.event-listener.aws-cloudwatch.region=us-east-1
 # polaris.event-listener.aws-cloudwatch.synchronous-mode=false
+# polaris.event-listener.aws-cloudwatch.event-types= // the absence of this 
property would result if processing all Polaris event types.

Review Comment:
   ```suggestion
   # polaris.event-listener.aws-cloudwatch.event-types= // the absence of this 
property would result in processing all Polaris event types.
   ```



##########
runtime/service/src/main/java/org/apache/polaris/service/events/listeners/aws/cloudwatch/AwsCloudWatchEventListener.java:
##########
@@ -71,13 +78,38 @@ public class AwsCloudWatchEventListener extends 
PropertyMapEventListener {
   public AwsCloudWatchEventListener(
       AwsCloudWatchConfiguration config,
       Clock clock,
-      PolarisIcebergObjectMapperCustomizer customizer) {
+      PolarisIcebergObjectMapperCustomizer customizer,
+      ObjectMapper mapper) {
     this.logStream = config.awsCloudWatchLogStream();
     this.logGroup = config.awsCloudWatchLogGroup();
     this.region = Region.of(config.awsCloudWatchRegion());
     this.synchronousMode = config.synchronousMode();
     this.clock = clock;
+    this.objectMapper = mapper;
     customizer.customize(this.objectMapper);
+    this.listenToAllEvents =
+        config.eventTypes().isEmpty()
+            || config.eventTypes().map(Set::isEmpty).orElse(true)
+            || config.eventTypes().get().stream().anyMatch(e -> e == 
PolarisEvent.class);
+    this.allowedEventTypes = listenToAllEvents ? Set.of() : 
Set.copyOf(config.eventTypes().get());

Review Comment:
   nit: I think we can simplify a bit:
   
   ```java
       this.allowedEventTypes = config.eventTypes().orElse(Set.of());
       this.listenToAllEvents =
           allowedEventTypes.isEmpty()
               || allowedEventTypes.stream().anyMatch(c -> c == 
PolarisEvent.class);
   ```



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