roryqi commented on code in PR #11266: URL: https://github.com/apache/gravitino/pull/11266#discussion_r3322912431
########## iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/cleanup/IcebergCleanupJobStore.java: ########## @@ -0,0 +1,250 @@ +/* + * 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.cleanup; + +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. Async cleanup reuses the entity store's connection pool, transaction + * management, and per-backend SQL dispatch instead of opening its own JDBC connections. Row ids and + * timestamps are supplied by the application, keeping the SQL portable across H2, MySQL, and + * PostgreSQL. + */ +public class IcebergCleanupJobStore { + + private static final int MAX_ERROR_LENGTH = 2048; + + private final IdGenerator idGenerator; + + /** + * Creates a cleanup job store. + * + * @param idGenerator generator for new row ids + */ + public IcebergCleanupJobStore(IdGenerator idGenerator) { + this.idGenerator = idGenerator; + } + + /** + * Persists a new PENDING job. + * + * @param job job to persist + * @return generated id + */ + public long addJob(IcebergCleanupJob job) { + long id = idGenerator.nextId(); + long now = System.currentTimeMillis(); + IcebergCleanupJobPO po = toPO(job, id, now); + SessionUtils.doWithCommit(IcebergCleanupJobMapper.class, mapper -> mapper.insertCleanupJob(po)); + return id; + } + + /** + * Scans a small candidate window and takes the first available row via compare-and-swap. + * + * @param now current epoch millis, written as the initial heartbeat + * @param heartbeatTimeoutMs age past which a RUNNING heartbeat is stale + * @param window max candidates to consider + * @return the taken job, or {@code null} if nothing was available + */ + public IcebergCleanupJob takePendingJob(long now, long heartbeatTimeoutMs, int window) { + long heartbeatExpiry = now - heartbeatTimeoutMs; + List<Long> ids = + SessionUtils.getWithoutCommit( + IcebergCleanupJobMapper.class, + mapper -> mapper.selectCandidateJobIds(heartbeatExpiry, window)); + for (long id : ids) { + int marked = + SessionUtils.doWithCommitAndFetchResult( + IcebergCleanupJobMapper.class, + mapper -> mapper.markRunning(id, now, heartbeatExpiry)); + if (marked == 1) { + IcebergCleanupJobPO po = + SessionUtils.getWithoutCommit( + IcebergCleanupJobMapper.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(); + SessionUtils.doWithCommit( + IcebergCleanupJobMapper.class, + mapper -> mapper.markFinished(id, IcebergCleanupJob.State.SUCCEEDED.name(), null, now)); + } + + /** + * Marks a RUNNING job FAILED immediately. + * + * @param id job id + * @param reason failure text + */ + public void markFailed(long id, String reason) { + long now = System.currentTimeMillis(); + String err = truncate(reason); + SessionUtils.doWithCommit( + IcebergCleanupJobMapper.class, + mapper -> mapper.markFinished(id, IcebergCleanupJob.State.FAILED.name(), err, now)); + } + + /** + * Records a transient failure: {@code attempts++}, then FAILED at the ceiling else PENDING. + * + * @param id job id + * @param reason failure text + * @param maxAttempts ceiling from config + */ + public void recordFailure(long id, String reason, int maxAttempts) { + long now = System.currentTimeMillis(); + String err = truncate(reason); + SessionUtils.doWithCommit( + IcebergCleanupJobMapper.class, mapper -> mapper.recordFailure(id, err, maxAttempts, now)); + } Review Comment: Added check. ########## iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/cleanup/IcebergCleanupJobStore.java: ########## @@ -0,0 +1,250 @@ +/* + * 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.cleanup; + +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. Async cleanup reuses the entity store's connection pool, transaction + * management, and per-backend SQL dispatch instead of opening its own JDBC connections. Row ids and + * timestamps are supplied by the application, keeping the SQL portable across H2, MySQL, and + * PostgreSQL. + */ +public class IcebergCleanupJobStore { + + private static final int MAX_ERROR_LENGTH = 2048; + + private final IdGenerator idGenerator; + + /** + * Creates a cleanup job store. + * + * @param idGenerator generator for new row ids + */ + public IcebergCleanupJobStore(IdGenerator idGenerator) { + this.idGenerator = idGenerator; + } + + /** + * Persists a new PENDING job. + * + * @param job job to persist + * @return generated id + */ + public long addJob(IcebergCleanupJob job) { + long id = idGenerator.nextId(); + long now = System.currentTimeMillis(); + IcebergCleanupJobPO po = toPO(job, id, now); + SessionUtils.doWithCommit(IcebergCleanupJobMapper.class, mapper -> mapper.insertCleanupJob(po)); + return id; + } + + /** + * Scans a small candidate window and takes the first available row via compare-and-swap. + * + * @param now current epoch millis, written as the initial heartbeat + * @param heartbeatTimeoutMs age past which a RUNNING heartbeat is stale + * @param window max candidates to consider + * @return the taken job, or {@code null} if nothing was available + */ + public IcebergCleanupJob takePendingJob(long now, long heartbeatTimeoutMs, int window) { + long heartbeatExpiry = now - heartbeatTimeoutMs; + List<Long> ids = + SessionUtils.getWithoutCommit( + IcebergCleanupJobMapper.class, + mapper -> mapper.selectCandidateJobIds(heartbeatExpiry, window)); Review Comment: Removed. -- 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]
