yuqi1129 commented on code in PR #11266:
URL: https://github.com/apache/gravitino/pull/11266#discussion_r3318323138


##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/purge/IcebergPurgeJob.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.service.purge;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import lombok.Getter;
+import lombok.experimental.Accessors;
+
+/**
+ * Immutable description of a table to purge. Carries exactly the fields the 
request path supplies
+ * at enqueue and the worker reads back to delete files. Mutable progress 
(state, attempts,
+ * heartbeat) lives only in the {@code iceberg_cleanup_job} row, managed by 
{@link
+ * IcebergPurgeJobStore}; the {@link State} enum here names those row states.
+ */
+@Getter
+@Accessors(fluent = true)
+public class IcebergPurgeJob {
+
+  /** Lifecycle states of an {@code iceberg_cleanup_job} row. */
+  public enum State {
+    /** Awaiting a worker; also the state a transiently-failed job returns to. 
*/
+    PENDING,
+
+    /** Claimed by a worker that is actively deleting files. */
+    RUNNING,
+
+    /** Every reachable file was deleted or already gone. */
+    SUCCEEDED,
+
+    /** Retries exhausted or a terminal failure; some files may remain 
undeleted. */
+    FAILED;
+
+    /**
+     * Whether this state is terminal.
+     *
+     * @return {@code true} for {@link #SUCCEEDED} and {@link #FAILED}
+     */
+    public boolean isTerminal() {

Review Comment:
   is finished or completed?



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/purge/IcebergPurgeJobMapper.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.service.purge;
+
+import java.util.List;
+import org.apache.ibatis.annotations.DeleteProvider;
+import org.apache.ibatis.annotations.InsertProvider;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.SelectProvider;
+import org.apache.ibatis.annotations.UpdateProvider;
+
+/**
+ * MyBatis mapper for the {@code iceberg_cleanup_job} table. SQL is supplied 
per backend by {@link
+ * IcebergPurgeJobSQLProviderFactory} and executed through the Gravitino 
entity store's shared
+ * {@code SqlSessionFactory}, so async purge reuses the relational backend's 
connection pool and
+ * multi-backend handling instead of opening its own JDBC connections.
+ */
+public interface IcebergPurgeJobMapper {
+
+  String TABLE_NAME = "iceberg_cleanup_job";
+
+  @InsertProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"insertPurgeJob")
+  void insertPurgeJob(@Param("po") IcebergPurgeJobPO po);
+
+  @SelectProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"selectClaimableIds")
+  List<Long> selectClaimableIds(

Review Comment:
   selectPendingJobIds



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/purge/IcebergPurgeJobSQLProviderFactory.java:
##########
@@ -0,0 +1,214 @@
+/*
+ * 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.service.purge;
+
+import static 
org.apache.gravitino.iceberg.service.purge.IcebergPurgeJobMapper.TABLE_NAME;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.annotations.Param;
+
+/**
+ * Supplies the SQL for {@link IcebergPurgeJobMapper}, dispatching by the 
entity store's configured
+ * JDBC backend. The statements are written with portable, parameterized SQL 
(the row id and all
+ * timestamps are generated by the application, not the database), so the 
{@link BaseProvider}
+ * serves MySQL, H2, and PostgreSQL alike. A backend that ever needs a 
divergent statement can be
+ * registered here by mapping its {@link JDBCBackendType} to a {@code 
BaseProvider} subclass that
+ * overrides only the affected methods.
+ */
+public class IcebergPurgeJobSQLProviderFactory {
+
+  private static final BaseProvider BASE_PROVIDER = new BaseProvider();
+
+  private static final Map<JDBCBackendType, BaseProvider> PROVIDERS =
+      ImmutableMap.of(
+          JDBCBackendType.MYSQL, BASE_PROVIDER,
+          JDBCBackendType.H2, BASE_PROVIDER,
+          JDBCBackendType.POSTGRESQL, BASE_PROVIDER);
+
+  private static BaseProvider getProvider() {
+    String databaseId =
+        SqlSessionFactoryHelper.getInstance()
+            .getSqlSessionFactory()
+            .getConfiguration()
+            .getDatabaseId();
+    return PROVIDERS.get(JDBCBackendType.fromString(databaseId));
+  }
+
+  public static String insertPurgeJob(@Param("po") IcebergPurgeJobPO po) {
+    return getProvider().insertPurgeJob(po);
+  }
+
+  public static String selectClaimableIds(
+      @Param("staleBefore") long staleBefore, @Param("window") int window) {
+    return getProvider().selectClaimableIds(staleBefore, window);
+  }
+
+  public static String markRunning(
+      @Param("id") long id, @Param("now") long now, @Param("staleBefore") long 
staleBefore) {
+    return getProvider().markRunning(id, now, staleBefore);
+  }
+
+  public static String selectById(@Param("id") long id) {
+    return getProvider().selectById(id);
+  }
+
+  public static String markSucceeded(@Param("id") long id, @Param("now") long 
now) {
+    return getProvider().markSucceeded(id, now);
+  }
+
+  public static String markFailed(
+      @Param("id") long id, @Param("reason") String reason, @Param("now") long 
now) {
+    return getProvider().markFailed(id, reason, now);
+  }
+
+  public static String recordFailure(
+      @Param("id") long id,
+      @Param("reason") String reason,
+      @Param("maxAttempts") int maxAttempts,
+      @Param("now") long now) {
+    return getProvider().recordFailure(id, reason, maxAttempts, now);
+  }
+
+  public static String heartbeat(
+      @Param("id") long id, @Param("lastWritten") long lastWritten, 
@Param("now") long now) {
+    return getProvider().heartbeat(id, lastWritten, now);
+  }
+
+  public static String selectActiveJobId(
+      @Param("catalog") String catalog,
+      @Param("namespace") String namespace,
+      @Param("table") String table) {
+    return getProvider().selectActiveJobId(catalog, namespace, table);
+  }
+
+  public static String pruneFinishedBefore(@Param("updatedBefore") long 
updatedBefore) {
+    return getProvider().pruneFinishedBefore(updatedBefore);
+  }
+
+  public static String selectState(@Param("id") long id) {
+    return getProvider().selectState(id);
+  }
+
+  /** Portable SQL shared by all supported JDBC backends. */
+  static class BaseProvider {
+
+    String insertPurgeJob(@Param("po") IcebergPurgeJobPO po) {
+      return "INSERT INTO "
+          + TABLE_NAME
+          + " (id, metalake_name, catalog_name, namespace, table_name, 
metadata_location,"
+          + " file_io_impl, file_io_props, state, attempts, last_error, 
heartbeat_at, created_by,"
+          + " updated_at) VALUES (#{po.id}, #{po.metalakeName}, 
#{po.catalogName}, #{po.namespace},"
+          + " #{po.tableName}, #{po.metadataLocation}, #{po.fileIOImpl}, 
#{po.fileIOProps},"
+          + " #{po.state}, #{po.attempts}, #{po.lastError}, #{po.heartbeatAt}, 
#{po.createdBy},"
+          + " #{po.updatedAt})";
+    }
+
+    String selectClaimableIds(@Param("staleBefore") long staleBefore, 
@Param("window") int window) {
+      return "SELECT id FROM "
+          + TABLE_NAME
+          + " WHERE state = 'PENDING'"
+          + " OR (state = 'RUNNING' AND (heartbeat_at IS NULL OR heartbeat_at 
< #{staleBefore}))"
+          + " ORDER BY updated_at LIMIT #{window}";
+    }
+
+    String markRunning(
+        @Param("id") long id, @Param("now") long now, @Param("staleBefore") 
long staleBefore) {
+      return "UPDATE "
+          + TABLE_NAME
+          + " SET state = 'RUNNING', heartbeat_at = #{now}, updated_at = 
#{now}"
+          + " WHERE id = #{id} AND (state = 'PENDING'"
+          + " OR (state = 'RUNNING' AND (heartbeat_at IS NULL OR heartbeat_at 
< #{staleBefore})))";
+    }
+
+    String selectById(@Param("id") long id) {
+      return "SELECT id, metalake_name AS metalakeName, catalog_name AS 
catalogName, namespace,"
+          + " table_name AS tableName, metadata_location AS metadataLocation,"
+          + " file_io_impl AS fileIOImpl, file_io_props AS fileIOProps, state, 
attempts,"
+          + " last_error AS lastError, heartbeat_at AS heartbeatAt, created_by 
AS createdBy,"
+          + " updated_at AS updatedAt FROM "
+          + TABLE_NAME
+          + " WHERE id = #{id}";
+    }
+
+    String markSucceeded(@Param("id") long id, @Param("now") long now) {
+      return "UPDATE "
+          + TABLE_NAME
+          + " SET state = 'SUCCEEDED', heartbeat_at = NULL, updated_at = 
#{now}"
+          + " WHERE id = #{id} AND state = 'RUNNING'";
+    }
+
+    String markFailed(

Review Comment:
   Can we merge these two?



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/purge/IcebergPurgeJobStore.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.service.purge;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.json.JsonUtils;
+import org.apache.gravitino.storage.IdGenerator;
+import org.apache.gravitino.storage.relational.utils.SessionUtils;
+
+/**
+ * Persistence for {@code iceberg_cleanup_job}, layered on the Gravitino 
entity store's shared
+ * relational backend. All access goes through {@link IcebergPurgeJobMapper} 
and {@link
+ * SessionUtils}, so async purge reuses the entity store's connection pool, 
transaction management,
+ * and per-backend SQL dispatch rather than opening its own JDBC connections. 
Row ids are generated
+ * by the application {@link IdGenerator}, matching the rest of the relational 
store.
+ */
+public class IcebergPurgeJobStore {
+
+  private static final int MAX_ERROR_LENGTH = 2048;
+
+  private final IdGenerator idGenerator;
+
+  /**
+   * Creates a purge job store.
+   *
+   * @param idGenerator generator for new row ids
+   */
+  public IcebergPurgeJobStore(IdGenerator idGenerator) {
+    this.idGenerator = idGenerator;
+  }
+
+  /**
+   * Persists a new PENDING job.
+   *
+   * @param job job to persist
+   * @return generated id
+   */
+  public long addJob(IcebergPurgeJob job) {
+    long id = idGenerator.nextId();
+    long now = System.currentTimeMillis();
+    IcebergPurgeJobPO po = toPO(job, id, now);
+    SessionUtils.doWithCommit(IcebergPurgeJobMapper.class, mapper -> 
mapper.insertPurgeJob(po));
+    return id;
+  }
+
+  /**
+   * Scans a small candidate window and claims the first winnable row via 
compare-and-swap.
+   *
+   * @param now current epoch millis, written as the claim heartbeat
+   * @param heartbeatTimeoutMs age past which a RUNNING heartbeat is stale
+   * @param window max candidates to consider
+   * @return the claimed job, or {@code null} if nothing was claimable
+   */
+  public IcebergPurgeJob takePendingJob(long now, long heartbeatTimeoutMs, int 
window) {
+    long staleBefore = now - heartbeatTimeoutMs;
+    List<Long> ids =
+        SessionUtils.getWithoutCommit(
+            IcebergPurgeJobMapper.class, mapper -> 
mapper.selectClaimableIds(staleBefore, window));
+    for (long id : ids) {
+      int marked =
+          SessionUtils.doWithCommitAndFetchResult(
+              IcebergPurgeJobMapper.class, mapper -> mapper.markRunning(id, 
now, staleBefore));
+      if (marked == 1) {
+        IcebergPurgeJobPO po =
+            SessionUtils.getWithoutCommit(
+                IcebergPurgeJobMapper.class, mapper -> mapper.selectById(id));
+        return fromPO(po);
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Marks a RUNNING job SUCCEEDED.
+   *
+   * @param id job id
+   */
+  public void markSucceeded(long id) {
+    long now = System.currentTimeMillis();

Review Comment:
   Do you plan to use JVM time, not DB time, for all events?



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/purge/IcebergPurgeJobMapper.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.service.purge;
+
+import java.util.List;
+import org.apache.ibatis.annotations.DeleteProvider;
+import org.apache.ibatis.annotations.InsertProvider;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.SelectProvider;
+import org.apache.ibatis.annotations.UpdateProvider;
+
+/**
+ * MyBatis mapper for the {@code iceberg_cleanup_job} table. SQL is supplied 
per backend by {@link
+ * IcebergPurgeJobSQLProviderFactory} and executed through the Gravitino 
entity store's shared
+ * {@code SqlSessionFactory}, so async purge reuses the relational backend's 
connection pool and
+ * multi-backend handling instead of opening its own JDBC connections.
+ */
+public interface IcebergPurgeJobMapper {
+
+  String TABLE_NAME = "iceberg_cleanup_job";
+
+  @InsertProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"insertPurgeJob")
+  void insertPurgeJob(@Param("po") IcebergPurgeJobPO po);
+
+  @SelectProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"selectClaimableIds")
+  List<Long> selectClaimableIds(
+      @Param("staleBefore") long staleBefore, @Param("window") int window);
+
+  @UpdateProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"markRunning")
+  int markRunning(
+      @Param("id") long id, @Param("now") long now, @Param("staleBefore") long 
staleBefore);
+
+  @SelectProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"selectById")
+  IcebergPurgeJobPO selectById(@Param("id") long id);
+
+  @UpdateProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"markSucceeded")
+  int markSucceeded(@Param("id") long id, @Param("now") long now);
+
+  @UpdateProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"markFailed")
+  int markFailed(@Param("id") long id, @Param("reason") String reason, 
@Param("now") long now);
+
+  @UpdateProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"recordFailure")
+  int recordFailure(
+      @Param("id") long id,
+      @Param("reason") String reason,
+      @Param("maxAttempts") int maxAttempts,
+      @Param("now") long now);
+
+  @UpdateProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"heartbeat")
+  int heartbeat(
+      @Param("id") long id, @Param("lastWritten") long lastWritten, 
@Param("now") long now);
+
+  @SelectProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"selectActiveJobId")
+  Long selectActiveJobId(
+      @Param("catalog") String catalog,
+      @Param("namespace") String namespace,
+      @Param("table") String table);
+
+  @DeleteProvider(type = IcebergPurgeJobSQLProviderFactory.class, method = 
"pruneFinishedBefore")
+  int pruneFinishedBefore(@Param("updatedBefore") long updatedBefore);

Review Comment:
   cleanFinishJodByTimeline?



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/purge/IcebergPurgeJobSQLProviderFactory.java:
##########
@@ -0,0 +1,214 @@
+/*
+ * 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.service.purge;
+
+import static 
org.apache.gravitino.iceberg.service.purge.IcebergPurgeJobMapper.TABLE_NAME;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.annotations.Param;
+
+/**
+ * Supplies the SQL for {@link IcebergPurgeJobMapper}, dispatching by the 
entity store's configured
+ * JDBC backend. The statements are written with portable, parameterized SQL 
(the row id and all
+ * timestamps are generated by the application, not the database), so the 
{@link BaseProvider}
+ * serves MySQL, H2, and PostgreSQL alike. A backend that ever needs a 
divergent statement can be
+ * registered here by mapping its {@link JDBCBackendType} to a {@code 
BaseProvider} subclass that
+ * overrides only the affected methods.
+ */
+public class IcebergPurgeJobSQLProviderFactory {
+
+  private static final BaseProvider BASE_PROVIDER = new BaseProvider();
+
+  private static final Map<JDBCBackendType, BaseProvider> PROVIDERS =
+      ImmutableMap.of(
+          JDBCBackendType.MYSQL, BASE_PROVIDER,
+          JDBCBackendType.H2, BASE_PROVIDER,
+          JDBCBackendType.POSTGRESQL, BASE_PROVIDER);
+
+  private static BaseProvider getProvider() {
+    String databaseId =
+        SqlSessionFactoryHelper.getInstance()
+            .getSqlSessionFactory()
+            .getConfiguration()
+            .getDatabaseId();
+    return PROVIDERS.get(JDBCBackendType.fromString(databaseId));
+  }
+
+  public static String insertPurgeJob(@Param("po") IcebergPurgeJobPO po) {
+    return getProvider().insertPurgeJob(po);
+  }
+
+  public static String selectClaimableIds(
+      @Param("staleBefore") long staleBefore, @Param("window") int window) {
+    return getProvider().selectClaimableIds(staleBefore, window);
+  }
+
+  public static String markRunning(
+      @Param("id") long id, @Param("now") long now, @Param("staleBefore") long 
staleBefore) {
+    return getProvider().markRunning(id, now, staleBefore);
+  }
+
+  public static String selectById(@Param("id") long id) {
+    return getProvider().selectById(id);
+  }
+
+  public static String markSucceeded(@Param("id") long id, @Param("now") long 
now) {
+    return getProvider().markSucceeded(id, now);
+  }
+
+  public static String markFailed(
+      @Param("id") long id, @Param("reason") String reason, @Param("now") long 
now) {
+    return getProvider().markFailed(id, reason, now);
+  }
+
+  public static String recordFailure(
+      @Param("id") long id,
+      @Param("reason") String reason,
+      @Param("maxAttempts") int maxAttempts,
+      @Param("now") long now) {
+    return getProvider().recordFailure(id, reason, maxAttempts, now);
+  }
+
+  public static String heartbeat(
+      @Param("id") long id, @Param("lastWritten") long lastWritten, 
@Param("now") long now) {
+    return getProvider().heartbeat(id, lastWritten, now);
+  }
+
+  public static String selectActiveJobId(
+      @Param("catalog") String catalog,
+      @Param("namespace") String namespace,
+      @Param("table") String table) {
+    return getProvider().selectActiveJobId(catalog, namespace, table);
+  }
+
+  public static String pruneFinishedBefore(@Param("updatedBefore") long 
updatedBefore) {
+    return getProvider().pruneFinishedBefore(updatedBefore);
+  }
+
+  public static String selectState(@Param("id") long id) {
+    return getProvider().selectState(id);
+  }
+
+  /** Portable SQL shared by all supported JDBC backends. */
+  static class BaseProvider {
+
+    String insertPurgeJob(@Param("po") IcebergPurgeJobPO po) {
+      return "INSERT INTO "
+          + TABLE_NAME
+          + " (id, metalake_name, catalog_name, namespace, table_name, 
metadata_location,"
+          + " file_io_impl, file_io_props, state, attempts, last_error, 
heartbeat_at, created_by,"
+          + " updated_at) VALUES (#{po.id}, #{po.metalakeName}, 
#{po.catalogName}, #{po.namespace},"
+          + " #{po.tableName}, #{po.metadataLocation}, #{po.fileIOImpl}, 
#{po.fileIOProps},"
+          + " #{po.state}, #{po.attempts}, #{po.lastError}, #{po.heartbeatAt}, 
#{po.createdBy},"
+          + " #{po.updatedAt})";
+    }
+
+    String selectClaimableIds(@Param("staleBefore") long staleBefore, 
@Param("window") int window) {
+      return "SELECT id FROM "
+          + TABLE_NAME
+          + " WHERE state = 'PENDING'"
+          + " OR (state = 'RUNNING' AND (heartbeat_at IS NULL OR heartbeat_at 
< #{staleBefore}))"

Review Comment:
   When wil the status is `RUNNING` and heartbeat_at IS NULL?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to