snazy commented on code in PR #1965:
URL: https://github.com/apache/polaris/pull/1965#discussion_r2212830643


##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;

Review Comment:
   Not thread safe



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;

Review Comment:
   Not thread safe



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {

Review Comment:
   This is a _specific_ implementation.
   Even if it is rather just a few classes, it deserves at least its own 
package. Putting this in a separate module is even better, because that 
prevents CI from running the expensive integration tests for unrelated changes.



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;
+
+  private Future<?> backgroundTask;
+
+  private final String logGroup;
+  private final String logStream;
+  private final Region region;
+
+  @Inject
+  public AwsCloudWatchEventListener(
+      EventListenerConfiguration config, ExecutorService executorService) {
+    this.executorService = executorService;
+
+    this.logStream = 
config.awsCloudwatchlogStream().orElse(DEFAULT_LOG_STREAM_NAME);
+    this.logGroup = 
config.awsCloudwatchlogGroup().orElse(DEFAULT_LOG_GROUP_NAME);
+    this.region = 
Region.of(config.awsCloudwatchRegion().orElse(DEFAULT_REGION));
+  }
+
+  @PostConstruct
+  void start() {
+    this.client = createCloudWatchClient();
+    ensureLogGroupAndStream();
+    backgroundTask = executorService.submit(this::processQueue);
+  }
+
+  protected CloudWatchLogsClient createCloudWatchClient() {
+    return CloudWatchLogsClient.builder().region(region).build();
+  }
+
+  private void processQueue() {
+    List<EventWithTimestamp> drainedEvents = new ArrayList<>();
+    List<InputLogEvent> transformedEvents = new ArrayList<>();
+    while (running || !queue.isEmpty()) {
+      drainQueue(drainedEvents, transformedEvents);
+    }
+  }
+
+  @VisibleForTesting
+  public void drainQueue(
+      List<EventWithTimestamp> drainedEvents, List<InputLogEvent> 
transformedEvents) {
+    try {
+      EventWithTimestamp first = queue.poll(MAX_WAIT_MS, 
TimeUnit.MILLISECONDS);
+
+      if (first != null) {
+        drainedEvents.add(first);
+        queue.drainTo(drainedEvents, MAX_BATCH_SIZE - 1);
+      } else {
+        return;
+      }
+
+      drainedEvents.forEach(event -> 
transformedEvents.add(createLogEvent(event)));
+
+      sendToCloudWatch(transformedEvents);
+    } catch (InterruptedException e) {

Review Comment:
   The purpose of an IE is to _stop_, not to continue.



##########
service/common/src/main/java/org/apache/polaris/service/events/EventListenerConfiguration.java:
##########
@@ -0,0 +1,30 @@
+/*
+ * 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 java.util.Optional;
+
+public interface EventListenerConfiguration {

Review Comment:
   How would users provide (different) credentials?



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;
+
+  private Future<?> backgroundTask;

Review Comment:
   Not thread safe



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;
+
+  private Future<?> backgroundTask;
+
+  private final String logGroup;
+  private final String logStream;
+  private final Region region;
+
+  @Inject
+  public AwsCloudWatchEventListener(
+      EventListenerConfiguration config, ExecutorService executorService) {
+    this.executorService = executorService;
+
+    this.logStream = 
config.awsCloudwatchlogStream().orElse(DEFAULT_LOG_STREAM_NAME);
+    this.logGroup = 
config.awsCloudwatchlogGroup().orElse(DEFAULT_LOG_GROUP_NAME);
+    this.region = 
Region.of(config.awsCloudwatchRegion().orElse(DEFAULT_REGION));
+  }
+
+  @PostConstruct
+  void start() {
+    this.client = createCloudWatchClient();
+    ensureLogGroupAndStream();
+    backgroundTask = executorService.submit(this::processQueue);
+  }
+
+  protected CloudWatchLogsClient createCloudWatchClient() {
+    return CloudWatchLogsClient.builder().region(region).build();
+  }
+
+  private void processQueue() {
+    List<EventWithTimestamp> drainedEvents = new ArrayList<>();
+    List<InputLogEvent> transformedEvents = new ArrayList<>();
+    while (running || !queue.isEmpty()) {
+      drainQueue(drainedEvents, transformedEvents);
+    }
+  }
+
+  @VisibleForTesting
+  public void drainQueue(
+      List<EventWithTimestamp> drainedEvents, List<InputLogEvent> 
transformedEvents) {
+    try {
+      EventWithTimestamp first = queue.poll(MAX_WAIT_MS, 
TimeUnit.MILLISECONDS);
+
+      if (first != null) {
+        drainedEvents.add(first);
+        queue.drainTo(drainedEvents, MAX_BATCH_SIZE - 1);
+      } else {
+        return;
+      }
+
+      drainedEvents.forEach(event -> 
transformedEvents.add(createLogEvent(event)));
+
+      sendToCloudWatch(transformedEvents);
+    } catch (InterruptedException e) {
+      if (!queue.isEmpty()) {
+        LOGGER.debug("Interrupted while waiting for queue to drain", e);
+        queue.addAll(drainedEvents);
+      }
+    } catch (DataAlreadyAcceptedException e) {
+      LOGGER.debug("Data already accepted: {}", e.getMessage());
+    } catch (RuntimeException e) {
+      if (e instanceof SdkClientException
+          || e instanceof InvalidParameterException
+          || e instanceof UnrecognizedClientException) {
+        LOGGER.error(
+            "Error writing logs to CloudWatch - client-side error. Please 
adjust Polaris configurations: {}",
+            e.getMessage());
+      } else {
+        LOGGER.error("Error writing logs to CloudWatch - server-side error: 
{}", e.getMessage());
+      }
+      LOGGER.error("Number of dropped events: {}", transformedEvents.size());
+      LOGGER.debug("Events not logged: {}", transformedEvents);
+      queue.addAll(drainedEvents);
+    }
+    drainedEvents.clear();
+    transformedEvents.clear();
+  }
+
+  private InputLogEvent createLogEvent(EventWithTimestamp eventWithTimestamp) {
+    return InputLogEvent.builder()
+        .message(eventWithTimestamp.event)
+        .timestamp(eventWithTimestamp.timestamp)
+        .build();
+  }
+
+  private void sendToCloudWatch(List<InputLogEvent> events) {
+    events.sort(Comparator.comparingLong(InputLogEvent::timestamp));
+
+    PutLogEventsRequest.Builder requestBuilder =
+        PutLogEventsRequest.builder()
+            .logGroupName(logGroup)
+            .logStreamName(logStream)
+            .logEvents(events);
+
+    synchronized (this) {
+      if (sequenceToken != null) {
+        requestBuilder.sequenceToken(sequenceToken);
+      }
+
+      try {
+        executePutLogEvents(requestBuilder);
+      } catch (InvalidSequenceTokenException e) {
+        sequenceToken = getSequenceToken();
+        requestBuilder.sequenceToken(sequenceToken);
+        executePutLogEvents(requestBuilder);
+      }
+    }
+  }
+
+  private void executePutLogEvents(PutLogEventsRequest.Builder requestBuilder) 
{
+    PutLogEventsResponse response = 
client.putLogEvents(requestBuilder.build());
+    sequenceToken = response.nextSequenceToken();
+  }
+
+  private void ensureLogGroupAndStream() {
+    try {
+      
client.createLogGroup(CreateLogGroupRequest.builder().logGroupName(logGroup).build());
+    } catch (ResourceAlreadyExistsException ignored) {
+      LOGGER.debug("Log group {} already exists", logGroup);
+    }
+
+    try {
+      client.createLogStream(
+          
CreateLogStreamRequest.builder().logGroupName(logGroup).logStreamName(logStream).build());
+    } catch (ResourceAlreadyExistsException ignored) {
+      LOGGER.debug("Log stream {} already exists", logStream);
+    }
+
+    sequenceToken = getSequenceToken();
+  }
+
+  private String getSequenceToken() {
+    DescribeLogStreamsResponse response =
+        client.describeLogStreams(
+            DescribeLogStreamsRequest.builder()
+                .logGroupName(logGroup)
+                .logStreamNamePrefix(logStream)
+                .build());
+
+    return response.logStreams().stream()
+        .filter(s -> logStream.equals(s.logStreamName()))
+        .map(LogStream::uploadSequenceToken)
+        .filter(Objects::nonNull)
+        .findFirst()
+        .orElse(null);
+  }
+
+  @PreDestroy
+  void shutdown() {
+    running = false;
+    if (backgroundTask != null) {
+      try {
+        backgroundTask.get(10, TimeUnit.SECONDS);
+      } catch (Exception e) {
+        LOGGER.error("Error waiting for background logging task to finish: 
{}", e.getMessage());
+      }
+    }
+    if (client != null) {
+      client.close();
+    }
+  }
+
+  record EventWithTimestamp(String event, long timestamp) {}
+
+  private long getCurrentTimestamp(CallContext callContext) {
+    return callContext.getPolarisCallContext().getClock().millis();
+  }
+
+  // Event overrides below
+  private void queueEvent(PolarisEvent event, CallContext callContext) {

Review Comment:
   This can easily serialize many (100s of) MB. Can AWS CloudWatch handle such 
big payloads?



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;
+
+  private Future<?> backgroundTask;
+
+  private final String logGroup;
+  private final String logStream;
+  private final Region region;
+
+  @Inject
+  public AwsCloudWatchEventListener(
+      EventListenerConfiguration config, ExecutorService executorService) {
+    this.executorService = executorService;
+
+    this.logStream = 
config.awsCloudwatchlogStream().orElse(DEFAULT_LOG_STREAM_NAME);
+    this.logGroup = 
config.awsCloudwatchlogGroup().orElse(DEFAULT_LOG_GROUP_NAME);
+    this.region = 
Region.of(config.awsCloudwatchRegion().orElse(DEFAULT_REGION));
+  }
+
+  @PostConstruct
+  void start() {
+    this.client = createCloudWatchClient();
+    ensureLogGroupAndStream();
+    backgroundTask = executorService.submit(this::processQueue);
+  }
+
+  protected CloudWatchLogsClient createCloudWatchClient() {
+    return CloudWatchLogsClient.builder().region(region).build();
+  }
+
+  private void processQueue() {
+    List<EventWithTimestamp> drainedEvents = new ArrayList<>();
+    List<InputLogEvent> transformedEvents = new ArrayList<>();
+    while (running || !queue.isEmpty()) {
+      drainQueue(drainedEvents, transformedEvents);
+    }
+  }
+
+  @VisibleForTesting
+  public void drainQueue(
+      List<EventWithTimestamp> drainedEvents, List<InputLogEvent> 
transformedEvents) {
+    try {
+      EventWithTimestamp first = queue.poll(MAX_WAIT_MS, 
TimeUnit.MILLISECONDS);
+
+      if (first != null) {
+        drainedEvents.add(first);
+        queue.drainTo(drainedEvents, MAX_BATCH_SIZE - 1);
+      } else {
+        return;
+      }
+
+      drainedEvents.forEach(event -> 
transformedEvents.add(createLogEvent(event)));

Review Comment:
   Why is this `createLogEvent()` done here?



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();

Review Comment:
   This queue is unbounded. It's likely that Polaris will OOM in case of 
communication issues w/ CloudWatch.



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;
+
+  private Future<?> backgroundTask;
+
+  private final String logGroup;
+  private final String logStream;
+  private final Region region;
+
+  @Inject
+  public AwsCloudWatchEventListener(
+      EventListenerConfiguration config, ExecutorService executorService) {
+    this.executorService = executorService;
+
+    this.logStream = 
config.awsCloudwatchlogStream().orElse(DEFAULT_LOG_STREAM_NAME);
+    this.logGroup = 
config.awsCloudwatchlogGroup().orElse(DEFAULT_LOG_GROUP_NAME);
+    this.region = 
Region.of(config.awsCloudwatchRegion().orElse(DEFAULT_REGION));
+  }
+
+  @PostConstruct
+  void start() {
+    this.client = createCloudWatchClient();
+    ensureLogGroupAndStream();
+    backgroundTask = executorService.submit(this::processQueue);
+  }
+
+  protected CloudWatchLogsClient createCloudWatchClient() {
+    return CloudWatchLogsClient.builder().region(region).build();
+  }
+
+  private void processQueue() {
+    List<EventWithTimestamp> drainedEvents = new ArrayList<>();
+    List<InputLogEvent> transformedEvents = new ArrayList<>();
+    while (running || !queue.isEmpty()) {
+      drainQueue(drainedEvents, transformedEvents);
+    }
+  }
+
+  @VisibleForTesting
+  public void drainQueue(
+      List<EventWithTimestamp> drainedEvents, List<InputLogEvent> 
transformedEvents) {
+    try {
+      EventWithTimestamp first = queue.poll(MAX_WAIT_MS, 
TimeUnit.MILLISECONDS);
+
+      if (first != null) {
+        drainedEvents.add(first);
+        queue.drainTo(drainedEvents, MAX_BATCH_SIZE - 1);
+      } else {
+        return;
+      }
+
+      drainedEvents.forEach(event -> 
transformedEvents.add(createLogEvent(event)));
+
+      sendToCloudWatch(transformedEvents);
+    } catch (InterruptedException e) {
+      if (!queue.isEmpty()) {
+        LOGGER.debug("Interrupted while waiting for queue to drain", e);
+        queue.addAll(drainedEvents);
+      }
+    } catch (DataAlreadyAcceptedException e) {
+      LOGGER.debug("Data already accepted: {}", e.getMessage());
+    } catch (RuntimeException e) {
+      if (e instanceof SdkClientException
+          || e instanceof InvalidParameterException
+          || e instanceof UnrecognizedClientException) {
+        LOGGER.error(
+            "Error writing logs to CloudWatch - client-side error. Please 
adjust Polaris configurations: {}",
+            e.getMessage());
+      } else {
+        LOGGER.error("Error writing logs to CloudWatch - server-side error: 
{}", e.getMessage());
+      }
+      LOGGER.error("Number of dropped events: {}", transformedEvents.size());
+      LOGGER.debug("Events not logged: {}", transformedEvents);
+      queue.addAll(drainedEvents);
+    }
+    drainedEvents.clear();
+    transformedEvents.clear();
+  }
+
+  private InputLogEvent createLogEvent(EventWithTimestamp eventWithTimestamp) {
+    return InputLogEvent.builder()
+        .message(eventWithTimestamp.event)
+        .timestamp(eventWithTimestamp.timestamp)
+        .build();
+  }
+
+  private void sendToCloudWatch(List<InputLogEvent> events) {
+    events.sort(Comparator.comparingLong(InputLogEvent::timestamp));
+
+    PutLogEventsRequest.Builder requestBuilder =
+        PutLogEventsRequest.builder()
+            .logGroupName(logGroup)
+            .logStreamName(logStream)
+            .logEvents(events);

Review Comment:
   I would bet that AWS has a limit on the number of events.



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";

Review Comment:
   Those hard coded defaults shouldn't be there. Defaulting to an AWS region is 
not great and will cause user confusion. I do not mind having defaults of the 
log stream/group name in the _Quarkus_ configuration, but region is awkward 
IMHO.



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;
+
+  private Future<?> backgroundTask;
+
+  private final String logGroup;
+  private final String logStream;
+  private final Region region;
+
+  @Inject
+  public AwsCloudWatchEventListener(
+      EventListenerConfiguration config, ExecutorService executorService) {
+    this.executorService = executorService;
+
+    this.logStream = 
config.awsCloudwatchlogStream().orElse(DEFAULT_LOG_STREAM_NAME);
+    this.logGroup = 
config.awsCloudwatchlogGroup().orElse(DEFAULT_LOG_GROUP_NAME);
+    this.region = 
Region.of(config.awsCloudwatchRegion().orElse(DEFAULT_REGION));
+  }
+
+  @PostConstruct
+  void start() {
+    this.client = createCloudWatchClient();
+    ensureLogGroupAndStream();
+    backgroundTask = executorService.submit(this::processQueue);
+  }
+
+  protected CloudWatchLogsClient createCloudWatchClient() {
+    return CloudWatchLogsClient.builder().region(region).build();
+  }
+
+  private void processQueue() {
+    List<EventWithTimestamp> drainedEvents = new ArrayList<>();
+    List<InputLogEvent> transformedEvents = new ArrayList<>();
+    while (running || !queue.isEmpty()) {
+      drainQueue(drainedEvents, transformedEvents);
+    }
+  }
+
+  @VisibleForTesting
+  public void drainQueue(
+      List<EventWithTimestamp> drainedEvents, List<InputLogEvent> 
transformedEvents) {
+    try {
+      EventWithTimestamp first = queue.poll(MAX_WAIT_MS, 
TimeUnit.MILLISECONDS);
+
+      if (first != null) {
+        drainedEvents.add(first);
+        queue.drainTo(drainedEvents, MAX_BATCH_SIZE - 1);
+      } else {
+        return;
+      }
+
+      drainedEvents.forEach(event -> 
transformedEvents.add(createLogEvent(event)));
+
+      sendToCloudWatch(transformedEvents);
+    } catch (InterruptedException e) {
+      if (!queue.isEmpty()) {
+        LOGGER.debug("Interrupted while waiting for queue to drain", e);
+        queue.addAll(drainedEvents);
+      }
+    } catch (DataAlreadyAcceptedException e) {
+      LOGGER.debug("Data already accepted: {}", e.getMessage());
+    } catch (RuntimeException e) {
+      if (e instanceof SdkClientException
+          || e instanceof InvalidParameterException
+          || e instanceof UnrecognizedClientException) {
+        LOGGER.error(
+            "Error writing logs to CloudWatch - client-side error. Please 
adjust Polaris configurations: {}",
+            e.getMessage());
+      } else {
+        LOGGER.error("Error writing logs to CloudWatch - server-side error: 
{}", e.getMessage());
+      }
+      LOGGER.error("Number of dropped events: {}", transformedEvents.size());
+      LOGGER.debug("Events not logged: {}", transformedEvents);
+      queue.addAll(drainedEvents);
+    }
+    drainedEvents.clear();
+    transformedEvents.clear();
+  }
+
+  private InputLogEvent createLogEvent(EventWithTimestamp eventWithTimestamp) {
+    return InputLogEvent.builder()
+        .message(eventWithTimestamp.event)
+        .timestamp(eventWithTimestamp.timestamp)
+        .build();
+  }
+
+  private void sendToCloudWatch(List<InputLogEvent> events) {
+    events.sort(Comparator.comparingLong(InputLogEvent::timestamp));
+
+    PutLogEventsRequest.Builder requestBuilder =
+        PutLogEventsRequest.builder()
+            .logGroupName(logGroup)
+            .logStreamName(logStream)
+            .logEvents(events);
+
+    synchronized (this) {
+      if (sequenceToken != null) {
+        requestBuilder.sequenceToken(sequenceToken);
+      }
+
+      try {
+        executePutLogEvents(requestBuilder);
+      } catch (InvalidSequenceTokenException e) {
+        sequenceToken = getSequenceToken();
+        requestBuilder.sequenceToken(sequenceToken);
+        executePutLogEvents(requestBuilder);
+      }
+    }
+  }
+
+  private void executePutLogEvents(PutLogEventsRequest.Builder requestBuilder) 
{
+    PutLogEventsResponse response = 
client.putLogEvents(requestBuilder.build());
+    sequenceToken = response.nextSequenceToken();
+  }
+
+  private void ensureLogGroupAndStream() {
+    try {
+      
client.createLogGroup(CreateLogGroupRequest.builder().logGroupName(logGroup).build());
+    } catch (ResourceAlreadyExistsException ignored) {
+      LOGGER.debug("Log group {} already exists", logGroup);
+    }
+
+    try {
+      client.createLogStream(
+          
CreateLogStreamRequest.builder().logGroupName(logGroup).logStreamName(logStream).build());
+    } catch (ResourceAlreadyExistsException ignored) {
+      LOGGER.debug("Log stream {} already exists", logStream);
+    }
+
+    sequenceToken = getSequenceToken();
+  }
+
+  private String getSequenceToken() {
+    DescribeLogStreamsResponse response =
+        client.describeLogStreams(
+            DescribeLogStreamsRequest.builder()
+                .logGroupName(logGroup)
+                .logStreamNamePrefix(logStream)
+                .build());
+
+    return response.logStreams().stream()
+        .filter(s -> logStream.equals(s.logStreamName()))
+        .map(LogStream::uploadSequenceToken)
+        .filter(Objects::nonNull)
+        .findFirst()
+        .orElse(null);
+  }
+
+  @PreDestroy
+  void shutdown() {
+    running = false;
+    if (backgroundTask != null) {
+      try {
+        backgroundTask.get(10, TimeUnit.SECONDS);
+      } catch (Exception e) {
+        LOGGER.error("Error waiting for background logging task to finish: 
{}", e.getMessage());
+      }
+    }

Review Comment:
   Relying on timing does not look not safe to me.



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;
+
+  private Future<?> backgroundTask;
+
+  private final String logGroup;
+  private final String logStream;
+  private final Region region;
+
+  @Inject
+  public AwsCloudWatchEventListener(
+      EventListenerConfiguration config, ExecutorService executorService) {
+    this.executorService = executorService;
+
+    this.logStream = 
config.awsCloudwatchlogStream().orElse(DEFAULT_LOG_STREAM_NAME);
+    this.logGroup = 
config.awsCloudwatchlogGroup().orElse(DEFAULT_LOG_GROUP_NAME);
+    this.region = 
Region.of(config.awsCloudwatchRegion().orElse(DEFAULT_REGION));
+  }
+
+  @PostConstruct
+  void start() {
+    this.client = createCloudWatchClient();
+    ensureLogGroupAndStream();
+    backgroundTask = executorService.submit(this::processQueue);
+  }
+
+  protected CloudWatchLogsClient createCloudWatchClient() {
+    return CloudWatchLogsClient.builder().region(region).build();
+  }
+
+  private void processQueue() {
+    List<EventWithTimestamp> drainedEvents = new ArrayList<>();
+    List<InputLogEvent> transformedEvents = new ArrayList<>();

Review Comment:
   The two collections look to be an unnecessary complication and should be 
simplified.



##########
service/common/src/test/java/org/apache/polaris/service/events/AwsCloudWatchEventListenerTest.java:
##########
@@ -0,0 +1,278 @@
+/*
+ * 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.mockito.Mockito.when;
+
+import java.time.Clock;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.PolarisCallContext;
+import org.apache.polaris.core.admin.model.Catalog;
+import org.apache.polaris.core.admin.model.FileStorageConfigInfo;
+import org.apache.polaris.core.admin.model.PolarisCatalog;
+import org.apache.polaris.core.admin.model.StorageConfigInfo;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.localstack.LocalStackContainer;
+import org.testcontainers.utility.DockerImageName;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogGroupsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogGroupsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import software.amazon.awssdk.services.cloudwatchlogs.model.OutputLogEvent;
+
+class AwsCloudWatchEventListenerTest {
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(AwsCloudWatchEventListenerTest.class);
+
+  private static final DockerImageName LOCALSTACK_IMAGE =
+      DockerImageName.parse("localstack/localstack:3.4");

Review Comment:
   Hard coding dependency versions in source isn't good practice. This should 
be replaced with the container-spec-helper.



##########
service/common/src/main/java/org/apache/polaris/service/events/AwsCloudWatchEventListener.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.polaris.core.context.CallContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DataAlreadyAcceptedException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.InvalidSequenceTokenException;
+import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException;
+import 
software.amazon.awssdk.services.cloudwatchlogs.model.UnrecognizedClientException;
+
+@ApplicationScoped
+@Identifier("aws-cloudwatch")
+public class AwsCloudWatchEventListener extends PolarisEventListener {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AwsCloudWatchEventListener.class);
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  private static final int MAX_BATCH_SIZE = 10_000;
+  private static final int MAX_WAIT_MS = 5000;
+  private static final String DEFAULT_LOG_STREAM_NAME = 
"polaris-cloudwatch-default-stream";
+  private static final String DEFAULT_LOG_GROUP_NAME = 
"polaris-cloudwatch-default-group";
+  private static final String DEFAULT_REGION = "us-east-1";
+
+  private final BlockingQueue<EventWithTimestamp> queue = new 
LinkedBlockingQueue<>();
+  private CloudWatchLogsClient client;
+  private volatile String sequenceToken;
+
+  private volatile boolean running = true;
+
+  ExecutorService executorService;
+
+  private Future<?> backgroundTask;
+
+  private final String logGroup;
+  private final String logStream;
+  private final Region region;
+
+  @Inject
+  public AwsCloudWatchEventListener(
+      EventListenerConfiguration config, ExecutorService executorService) {
+    this.executorService = executorService;
+
+    this.logStream = 
config.awsCloudwatchlogStream().orElse(DEFAULT_LOG_STREAM_NAME);
+    this.logGroup = 
config.awsCloudwatchlogGroup().orElse(DEFAULT_LOG_GROUP_NAME);
+    this.region = 
Region.of(config.awsCloudwatchRegion().orElse(DEFAULT_REGION));
+  }
+
+  @PostConstruct
+  void start() {
+    this.client = createCloudWatchClient();
+    ensureLogGroupAndStream();
+    backgroundTask = executorService.submit(this::processQueue);
+  }
+
+  protected CloudWatchLogsClient createCloudWatchClient() {
+    return CloudWatchLogsClient.builder().region(region).build();
+  }
+
+  private void processQueue() {
+    List<EventWithTimestamp> drainedEvents = new ArrayList<>();
+    List<InputLogEvent> transformedEvents = new ArrayList<>();
+    while (running || !queue.isEmpty()) {
+      drainQueue(drainedEvents, transformedEvents);
+    }
+  }
+
+  @VisibleForTesting
+  public void drainQueue(
+      List<EventWithTimestamp> drainedEvents, List<InputLogEvent> 
transformedEvents) {
+    try {
+      EventWithTimestamp first = queue.poll(MAX_WAIT_MS, 
TimeUnit.MILLISECONDS);
+
+      if (first != null) {
+        drainedEvents.add(first);
+        queue.drainTo(drainedEvents, MAX_BATCH_SIZE - 1);
+      } else {
+        return;
+      }
+
+      drainedEvents.forEach(event -> 
transformedEvents.add(createLogEvent(event)));
+
+      sendToCloudWatch(transformedEvents);
+    } catch (InterruptedException e) {
+      if (!queue.isEmpty()) {
+        LOGGER.debug("Interrupted while waiting for queue to drain", e);
+        queue.addAll(drainedEvents);
+      }
+    } catch (DataAlreadyAcceptedException e) {
+      LOGGER.debug("Data already accepted: {}", e.getMessage());
+    } catch (RuntimeException e) {
+      if (e instanceof SdkClientException
+          || e instanceof InvalidParameterException
+          || e instanceof UnrecognizedClientException) {
+        LOGGER.error(
+            "Error writing logs to CloudWatch - client-side error. Please 
adjust Polaris configurations: {}",
+            e.getMessage());
+      } else {
+        LOGGER.error("Error writing logs to CloudWatch - server-side error: 
{}", e.getMessage());
+      }
+      LOGGER.error("Number of dropped events: {}", transformedEvents.size());
+      LOGGER.debug("Events not logged: {}", transformedEvents);
+      queue.addAll(drainedEvents);

Review Comment:
   This (and the one above) lead to OOMs. Auth issues and availability impacts 
will lead to this.



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