Copilot commented on code in PR #11364:
URL: https://github.com/apache/gravitino/pull/11364#discussion_r3345718931


##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/RESTService.java:
##########
@@ -198,11 +220,14 @@ public void serviceInit(Map<String, String> properties, 
boolean auxMode) {
   @Override
   public void serviceStart() {
     icebergMetricsManager.start();
+    cleanupManager.ifPresent(IcebergCleanupManager::start);
     if (server != null) {
       try {
         server.start();
         LOG.info("Iceberg REST service started");
       } catch (Exception e) {
+        // Stop the cleanup workers we just started so they don't outlive a 
failed startup.
+        cleanupManager.ifPresent(IcebergCleanupManager::close);
         throw new RuntimeException(e);

Review Comment:
   If Jetty `server.start()` fails, `icebergMetricsManager.start()` has already 
started the metrics writer/cleaner threads, but the catch block only closes the 
cleanup manager. This can leave background threads running even though the 
service failed to start. Close `icebergMetricsManager` (and ideally any other 
started components) in the failure path before rethrowing.



##########
iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTAsyncPurgeIT.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.gravitino.iceberg.integration.test;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+import org.apache.gravitino.Catalog;
+import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants;
+import org.apache.gravitino.client.GravitinoMetalake;
+import org.apache.gravitino.iceberg.common.IcebergConfig;
+import org.apache.gravitino.integration.test.container.ContainerSuite;
+import org.apache.gravitino.integration.test.util.BaseIT;
+import org.apache.gravitino.integration.test.util.TestDatabaseName;
+import org.apache.gravitino.listener.api.event.IcebergRequestContext;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.io.PositionOutputStream;
+import org.apache.iceberg.rest.RESTCatalog;
+import org.apache.iceberg.types.Types;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * End-to-end test for async table purge. A table dropped with {@code 
purgeRequested=true} plus the
+ * {@link IcebergRequestContext#ASYNC_PURGE_HEADER} should keep its files on 
disk while the drop
+ * returns, block recreating the same name with {@code 409} until cleanup 
finishes, and have its
+ * files deleted by the background worker.
+ *
+ * <p>It drives the server with the {@link RESTCatalog} client, not Spark: 
Spark purges files
+ * client-side and sends {@code purgeRequested=false}, which skips the 
server-side path. It uses the
+ * dynamic config provider over a PostgreSQL-backed {@code lakehouse-iceberg} 
catalog with a local
+ * {@code file://} warehouse, since cleanup jobs are keyed by catalog id and 
the files must be
+ * visible on disk.
+ */
+public class IcebergRESTAsyncPurgeIT extends BaseIT {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(IcebergRESTAsyncPurgeIT.class);
+  private static final String GRAVITINO_ICEBERG_REST_PREFIX = 
"gravitino.iceberg-rest.";
+  private static final String METALAKE_NAME = "async_cleanup_metalake";
+  private static final String CATALOG_NAME = "iceberg_cleanup";
+  private static final String USER = "test";
+  private static final String DATABASE_NAME = "purge_db";
+  private static final String TABLE_NAME = "t_async";
+  // Poll interval large enough that the background worker provably cannot 
claim the job during the
+  // synchronous in-flight assertions, yet small enough to keep the 
eventual-cleanup wait short.
+  private static final int CLEANUP_POLL_INTERVAL_SECS = 5;
+
+  private static final Schema SCHEMA =
+      new Schema(
+          Types.NestedField.required(1, "id", Types.LongType.get()),
+          Types.NestedField.optional(2, "data", Types.StringType.get()));
+
+  private static final ContainerSuite CONTAINER_SUITE = 
ContainerSuite.getInstance();
+
+  private Path warehouseDir;
+  private RESTCatalog restCatalog;
+
+  @BeforeAll
+  @Override
+  public void startIntegrationTest() throws Exception {
+    
CONTAINER_SUITE.startPostgreSQLContainer(TestDatabaseName.PG_ICEBERG_ASYNC_CLEANUP_IT);
+    warehouseDir = 
Files.createTempDirectory("gravitino-iceberg-async-cleanup");
+    ignoreIcebergAuxRestService = false;
+
+    // Simple authentication so the dynamic config provider can authenticate 
to Gravitino and the
+    // REST client can identify itself; authorization stays disabled to keep 
the test focused.
+    customConfigs.put("gravitino.authenticators", "simple");
+    customConfigs.put("SimpleAuthUserName", USER);
+    customConfigs.put(
+        GRAVITINO_ICEBERG_REST_PREFIX + 
IcebergConstants.ICEBERG_REST_CATALOG_CONFIG_PROVIDER,
+        IcebergConstants.DYNAMIC_ICEBERG_CATALOG_CONFIG_PROVIDER_NAME);
+    customConfigs.put(
+        GRAVITINO_ICEBERG_REST_PREFIX + IcebergConstants.GRAVITINO_METALAKE, 
METALAKE_NAME);
+    customConfigs.put(
+        GRAVITINO_ICEBERG_REST_PREFIX + 
IcebergConstants.ICEBERG_REST_DEFAULT_DYNAMIC_CATALOG_NAME,
+        CATALOG_NAME);
+    customConfigs.put(
+        GRAVITINO_ICEBERG_REST_PREFIX + 
IcebergConstants.GRAVITINO_SIMPLE_USERNAME, USER);
+    customConfigs.put(
+        GRAVITINO_ICEBERG_REST_PREFIX + 
IcebergConfig.ASYNC_CLEANUP_POLL_INTERVAL_SECS.getKey(),
+        String.valueOf(CLEANUP_POLL_INTERVAL_SECS));
+
+    super.startIntegrationTest();
+    initMetalakeAndCatalog();
+    initRESTCatalog();
+  }
+
+  @AfterAll
+  @Override
+  public void stopIntegrationTest() throws IOException, InterruptedException {
+    if (restCatalog != null) {
+      try {
+        restCatalog.close();
+      } catch (IOException e) {
+        LOG.warn("Failed to close Iceberg REST catalog", e);
+      }
+      restCatalog = null;
+    }
+    try {
+      client.dropMetalake(METALAKE_NAME, true);
+    } catch (Exception e) {
+      LOG.warn("Failed to drop metalake {}", METALAKE_NAME, e);
+    }
+    super.stopIntegrationTest();

Review Comment:
   This IT creates a temporary warehouse directory 
(`Files.createTempDirectory`) but never deletes it in teardown. Over repeated 
local runs this can accumulate and waste disk space. Consider deleting 
`warehouseDir` in `stopIntegrationTest()` (best-effort) after the test 
completes.



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/listener/api/event/IcebergRequestContext.java:
##########
@@ -113,6 +116,24 @@ public Map<String, String> httpHeaders() {
     return httpHeaders;
   }
 
+  /**
+   * Checks whether this request opted into asynchronous table purge.
+   *
+   * <p>Async purge is opt-in. Standard Iceberg clients send no header and 
keep synchronous purge
+   * behavior; a client opts in with {@code X-Gravitino-Async-Purge: True}.
+   *
+   * @return true only when the async purge header explicitly says {@code True}

Review Comment:
   PR metadata and the design doc use `X-Gravitino-Async-Purge: true` 
(lowercase), but this implementation (and the new tests/docs) only opt in on 
the exact value `True` (capital T). This mismatch is likely to confuse users 
and reviewers; please align the documented/advertised header value with the 
implemented parsing (either accept case-insensitive `true`, or update the PR 
description/design doc to consistently say `True`).



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