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


##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/JobMetaBaseSQLProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.storage.relational.mapper.provider.base;
+
+import org.apache.gravitino.storage.relational.mapper.JobMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.JobTemplateMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper;
+import org.apache.gravitino.storage.relational.po.JobPO;
+import org.apache.ibatis.annotations.Param;
+
+public class JobMetaBaseSQLProvider {
+
+  public String insertJobMeta(@Param("jobMeta") JobPO jobPO) {
+    return "INSERT INTO "
+        + JobMetaMapper.TABLE_NAME
+        + " (job_run_id, job_template_id, metalake_id,"
+        + " job_execution_id, job_run_status, job_finished_at, audit_info, 
current_version,"
+        + " last_version, deleted_at)"
+        + " VALUES (#{jobMeta.jobRunId},"
+        + " (SELECT job_template_id FROM "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " WHERE job_template_name = #{jobMeta.jobTemplateName} AND 
deleted_at = 0),"
+        + " #{jobMeta.metalakeId}, #{jobMeta.jobExecutionId},"
+        + " #{jobMeta.jobRunStatus}, #{jobMeta.jobFinishedAt}, 
#{jobMeta.auditInfo},"
+        + " #{jobMeta.currentVersion}, #{jobMeta.lastVersion},"
+        + " #{jobMeta.deletedAt})";
+  }
+
+  public String insertJobMetaOnDuplicateKeyUpdate(@Param("jobMeta") JobPO 
jobPO) {
+    return "INSERT INTO "
+        + JobMetaMapper.TABLE_NAME
+        + " (job_run_id, job_template_id, metalake_id,"
+        + " job_execution_id, job_run_status, job_finished_at, audit_info, 
current_version,"
+        + " last_version, deleted_at)"
+        + " VALUES (#{jobMeta.jobRunId},"
+        + " (SELECT job_template_id FROM "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " WHERE job_template_name = #{jobMeta.jobTemplateName} AND 
deleted_at = 0),"
+        + " #{jobMeta.metalakeId}, #{jobMeta.jobExecutionId},"
+        + " #{jobMeta.jobRunStatus}, #{jobMeta.jobFinishedAt}, 
#{jobMeta.auditInfo},"
+        + " #{jobMeta.currentVersion}, #{jobMeta.lastVersion},"
+        + " #{jobMeta.deletedAt})"
+        + " ON DUPLICATE KEY UPDATE"
+        + " job_template_id = (SELECT job_template_id FROM "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " WHERE job_template_name = #{jobMeta.jobTemplateName} AND 
deleted_at = 0),"
+        + " metalake_id = #{jobMeta.metalakeId},"
+        + " job_execution_id = #{jobMeta.jobExecutionId},"
+        + " job_run_status = #{jobMeta.jobRunStatus},"
+        + " job_finished_at = #{jobMeta.jobFinishedAt},"
+        + " audit_info = #{jobMeta.auditInfo},"
+        + " current_version = #{jobMeta.currentVersion},"
+        + " last_version = #{jobMeta.lastVersion},"
+        + " deleted_at = #{jobMeta.deletedAt}";
+  }
+
+  public String listJobPOsByMetalake(@Param("metalakeName") String 
metalakeName) {
+    return "SELECT jrm.job_run_id AS jobRunId, jtm.job_template_name AS 
jobTemplateName,"
+        + " jrm.metalake_id AS metalakeId, jrm.job_execution_id AS 
jobExecutionId,"
+        + " jrm.job_run_status AS jobRunStatus, jrm.job_finished_at AS 
jobFinishedAt,"
+        + " jrm.audit_info AS auditInfo,"
+        + " jrm.current_version AS currentVersion, jrm.last_version AS 
lastVersion,"
+        + " jrm.deleted_at AS deletedAt"
+        + " FROM "
+        + JobMetaMapper.TABLE_NAME
+        + " jrm JOIN "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " jtm ON jrm.job_template_id = jtm.job_template_id"
+        + " JOIN "
+        + MetalakeMetaMapper.TABLE_NAME
+        + " mm ON jrm.metalake_id = mm.metalake_id"
+        + " WHERE mm.metalake_name = #{metalakeName} AND jrm.deleted_at = 0 
AND mm.deleted_at = 0"
+        + " AND jtm.deleted_at = 0";
+  }
+
+  public String listJobPOsByMetalakeAndTemplate(
+      @Param("metalakeName") String metalakeName,
+      @Param("jobTemplateName") String jobTemplateName) {
+    return "SELECT jrm.job_run_id AS jobRunId, jtm.job_template_name AS 
jobTemplateName,"
+        + " jrm.metalake_id AS metalakeId, jrm.job_execution_id AS 
jobExecutionId,"
+        + " jrm.job_run_status AS jobRunStatus, jrm.job_finished_at AS 
jobFinishedAt, "
+        + " jrm.audit_info AS auditInfo,"
+        + " jrm.current_version AS currentVersion, jrm.last_version AS 
lastVersion,"
+        + " jrm.deleted_at AS deletedAt"
+        + " FROM "
+        + JobMetaMapper.TABLE_NAME
+        + " jrm JOIN "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " jtm ON jrm.job_template_id = jtm.job_template_id"
+        + " JOIN "
+        + MetalakeMetaMapper.TABLE_NAME
+        + " mm ON jrm.metalake_id = mm.metalake_id"
+        + " WHERE mm.metalake_name = #{metalakeName} AND jtm.job_template_name 
= #{jobTemplateName}"
+        + " AND jrm.deleted_at = 0 AND mm.deleted_at = 0 AND jtm.deleted_at = 
0";
+  }
+
+  public String selectJobPOByMetalakeAndRunId(
+      @Param("metalakeName") String metalakeName, @Param("jobRunId") Long 
jobRunId) {
+    return "SELECT jrm.job_run_id AS jobRunId, jtm.job_template_name AS 
jobTemplateName,"
+        + " jrm.metalake_id AS metalakeId, jrm.job_execution_id AS 
jobExecutionId,"
+        + " jrm.job_run_status AS jobRunStatus, jrm.job_finished_at AS 
jobFinishedAt,"
+        + " jrm.audit_info AS auditInfo,"
+        + " jrm.current_version AS currentVersion, jrm.last_version AS 
lastVersion,"
+        + " jrm.deleted_at AS deletedAt"
+        + " FROM "
+        + JobMetaMapper.TABLE_NAME
+        + " jrm JOIN "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " jtm ON jrm.job_template_id = jtm.job_template_id"
+        + " JOIN "
+        + MetalakeMetaMapper.TABLE_NAME
+        + " mm ON jrm.metalake_id = mm.metalake_id"
+        + " WHERE mm.metalake_name = #{metalakeName} AND jrm.job_run_id = 
#{jobRunId}"
+        + " AND jrm.deleted_at = 0 AND mm.deleted_at = 0 AND jtm.deleted_at = 
0";
+  }
+
+  public String softDeleteJobMetaByMetalakeAndTemplate(
+      @Param("metalakeName") String metalakeName,
+      @Param("jobTemplateName") String jobTemplateName) {
+    return "UPDATE "
+        + JobMetaMapper.TABLE_NAME
+        + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+        + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000.0"
+        + " WHERE metalake_id = ("
+        + " SELECT metalake_id FROM "
+        + MetalakeMetaMapper.TABLE_NAME
+        + " WHERE metalake_name = #{metalakeName} AND deleted_at = 0)"
+        + " AND job_template_id = ("
+        + " SELECT job_template_id FROM "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " WHERE job_template_name = #{jobTemplateName} AND deleted_at = 0)"

Review Comment:
   Since the unique key is `metalake_id, job_template_name, deleted_at `, the 
result of 
   ```
   SELECT job_template_id FROM "
           + JobTemplateMetaMapper.TABLE_NAME
           + " WHERE job_template_name = #{jobTemplateName} AND deleted_at = 0
   ```
   
   can be larger than 1, I'm afraid that we should not use `job_template_id =` 
here. 



##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/JobTemplateMetaBaseSQLProvider.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * 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.storage.relational.mapper.provider.base;
+
+import org.apache.gravitino.storage.relational.mapper.JobTemplateMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper;
+import org.apache.gravitino.storage.relational.po.JobTemplatePO;
+import org.apache.ibatis.annotations.Param;
+
+public class JobTemplateMetaBaseSQLProvider {
+
+  public String insertJobTemplateMeta(@Param("jobTemplateMeta") JobTemplatePO 
jobTemplatePO) {
+    return "INSERT INTO "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " (job_template_id, job_template_name, metalake_id,"
+        + " job_template_comment, job_template_content, audit_info,"
+        + " current_version, last_version, deleted_at)"
+        + " VALUES (#{jobTemplateMeta.jobTemplateId}, 
#{jobTemplateMeta.jobTemplateName},"
+        + " #{jobTemplateMeta.metalakeId}, 
#{jobTemplateMeta.jobTemplateComment},"
+        + " #{jobTemplateMeta.jobTemplateContent}, 
#{jobTemplateMeta.auditInfo},"
+        + " #{jobTemplateMeta.currentVersion}, #{jobTemplateMeta.lastVersion},"
+        + " #{jobTemplateMeta.deletedAt})";
+  }
+
+  public String insertJobTemplateMetaOnDuplicateKeyUpdate(
+      @Param("jobTemplateMeta") JobTemplatePO jobTemplatePO) {
+    return "INSERT INTO "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " (job_template_id, job_template_name, metalake_id,"
+        + " job_template_comment, job_template_content, audit_info, 
current_version,"
+        + " last_version, deleted_at)"
+        + " VALUES (#{jobTemplateMeta.jobTemplateId}, 
#{jobTemplateMeta.jobTemplateName},"
+        + " #{jobTemplateMeta.metalakeId}, 
#{jobTemplateMeta.jobTemplateComment},"
+        + " #{jobTemplateMeta.jobTemplateContent}, 
#{jobTemplateMeta.auditInfo},"
+        + " #{jobTemplateMeta.currentVersion}, #{jobTemplateMeta.lastVersion},"
+        + " #{jobTemplateMeta.deletedAt})"
+        + " ON DUPLICATE KEY UPDATE"
+        + " job_template_name = #{jobTemplateMeta.jobTemplateName},"
+        + " metalake_id = #{jobTemplateMeta.metalakeId},"
+        + " job_template_comment = #{jobTemplateMeta.jobTemplateComment},"
+        + " job_template_content = #{jobTemplateMeta.jobTemplateContent},"
+        + " audit_info = #{jobTemplateMeta.auditInfo},"
+        + " current_version = #{jobTemplateMeta.currentVersion},"
+        + " last_version = #{jobTemplateMeta.lastVersion},"
+        + " deleted_at = #{jobTemplateMeta.deletedAt}";
+  }
+
+  public String listJobTemplatePOsByMetalake(@Param("metalakeName") String 
metalakeName) {
+    return "SELECT jtm.job_template_id AS jobTemplateId, jtm.job_template_name 
AS jobTemplateName,"
+        + " jtm.metalake_id AS metalakeId, jtm.job_template_comment AS 
jobTemplateComment,"
+        + " jtm.job_template_content AS jobTemplateContent, jtm.audit_info AS 
auditInfo,"
+        + " jtm.current_version AS currentVersion, jtm.last_version AS 
lastVersion,"
+        + " jtm.deleted_at AS deletedAt"
+        + " FROM "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " jtm JOIN "
+        + MetalakeMetaMapper.TABLE_NAME
+        + " mm ON jtm.metalake_id = mm.metalake_id"
+        + " WHERE mm.metalake_name = #{metalakeName} AND jtm.deleted_at = 0 
AND mm.deleted_at = 0";
+  }
+
+  public String selectJobTemplatePOByMetalakeAndName(
+      @Param("metalakeName") String metalakeName,
+      @Param("jobTemplateName") String jobTemplateName) {
+    return "SELECT jtm.job_template_id AS jobTemplateId, jtm.job_template_name 
AS jobTemplateName,"
+        + " jtm.metalake_id AS metalakeId, jtm.job_template_comment AS 
jobTemplateComment,"
+        + " jtm.job_template_content AS jobTemplateContent, jtm.audit_info AS 
auditInfo,"
+        + " jtm.current_version AS currentVersion, jtm.last_version AS 
lastVersion,"
+        + " jtm.deleted_at AS deletedAt"
+        + " FROM "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " jtm JOIN "
+        + MetalakeMetaMapper.TABLE_NAME
+        + " mm ON jtm.metalake_id = mm.metalake_id"
+        + " WHERE mm.metalake_name = #{metalakeName} AND jtm.job_template_name 
= #{jobTemplateName}"
+        + " AND jtm.deleted_at = 0 AND mm.deleted_at = 0";
+  }
+
+  public String softDeleteJobTemplateMetaByMetalakeAndName(
+      @Param("metalakeName") String metalakeName,
+      @Param("jobTemplateName") String jobTemplateName) {
+    return "UPDATE "
+        + JobTemplateMetaMapper.TABLE_NAME
+        + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+        + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000.0"
+        + " WHERE job_template_name = #{jobTemplateName} AND metalake_id ="
+        + " (SELECT metalake_id FROM "

Review Comment:
   ditto, should use `IN` as follows.



##########
core/src/main/java/org/apache/gravitino/job/JobManager.java:
##########
@@ -366,15 +370,20 @@ public JobEntity cancelJob(String metalake, String jobId) 
throws NoSuchJobExcept
                     .withLastModifiedTime(Instant.now())
                     .build())
             .build();
-
-    try {
-      // Update the job entity in the entity store
-      entityStore.put(newJobEntity, true /* overwrite */);
-      return newJobEntity;
-    } catch (IOException e) {
-      throw new RuntimeException(
-          String.format("Failed to update job entity %s to CANCELING status", 
newJobEntity), e);
-    }
+    return TreeLockUtils.doWithTreeLock(
+        NameIdentifierUtil.ofJob(metalake, jobId),
+        LockType.WRITE,
+        () -> {
+          try {
+            // Update the job entity in the entity store
+            entityStore.put(newJobEntity, true /* overwrite */);

Review Comment:
   I wonder when we change the status to `CANCELLED` for a `CANCELLING` job?



##########
core/src/main/java/org/apache/gravitino/storage/relational/po/JobPO.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.storage.relational.po;
+
+import static 
org.apache.gravitino.storage.relational.utils.POConverters.DEFAULT_DELETED_AT;
+import static 
org.apache.gravitino.storage.relational.utils.POConverters.INIT_VERSION;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.common.base.Preconditions;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.ToString;
+import lombok.experimental.Accessors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.job.JobHandle;
+import org.apache.gravitino.json.JsonUtils;
+import org.apache.gravitino.meta.AuditInfo;
+import org.apache.gravitino.meta.JobEntity;
+
+@Getter
+@Accessors(fluent = true)
+@EqualsAndHashCode
+@ToString
+public class JobPO {
+
+  private Long jobRunId;
+  private String jobTemplateName;
+  private Long metalakeId;
+  private String jobExecutionId;
+  private String jobRunStatus;
+  private Long jobFinishedAt;
+  private String auditInfo;
+  private Long currentVersion;
+  private Long lastVersion;
+  private Long deletedAt;
+
+  public JobPO() {
+    // Default constructor for JPA
+  }
+
+  @lombok.Builder(setterPrefix = "with")
+  private JobPO(
+      Long jobRunId,
+      String jobTemplateName,
+      Long metalakeId,
+      String jobExecutionId,
+      String jobRunStatus,
+      Long jobFinishedAt,
+      String auditInfo,
+      Long currentVersion,
+      Long lastVersion,
+      Long deletedAt) {
+    Preconditions.checkArgument(jobRunId != null, "jobRunId cannot be null");
+    Preconditions.checkArgument(
+        StringUtils.isNotBlank(jobTemplateName), "jobTemplateName cannot be 
blank");
+    Preconditions.checkArgument(metalakeId != null, "metalakeId cannot be 
null");
+    Preconditions.checkArgument(
+        StringUtils.isNotBlank(jobExecutionId), "jobExecutionId cannot be 
blank");
+    Preconditions.checkArgument(
+        StringUtils.isNotBlank(jobRunStatus), "jobRunStatus cannot be blank");
+    Preconditions.checkArgument(jobFinishedAt != null, "jobFinishedAt cannot 
be null");
+    Preconditions.checkArgument(StringUtils.isNotBlank(auditInfo), "auditInfo 
cannot be blank");
+    Preconditions.checkArgument(currentVersion != null, "currentVersion cannot 
be null");
+    Preconditions.checkArgument(lastVersion != null, "lastVersion cannot be 
null");
+    Preconditions.checkArgument(deletedAt != null, "deletedAt cannot be null");
+
+    this.jobRunId = jobRunId;
+    this.jobTemplateName = jobTemplateName;
+    this.metalakeId = metalakeId;
+    this.jobExecutionId = jobExecutionId;
+    this.jobRunStatus = jobRunStatus;
+    this.jobFinishedAt = jobFinishedAt;
+    this.auditInfo = auditInfo;
+    this.currentVersion = currentVersion;
+    this.lastVersion = lastVersion;
+    this.deletedAt = deletedAt;
+  }
+
+  public static class JobPOBuilder {
+    // Builder class for JobPO
+    // Lombok will generate the builder methods based on the fields defined in 
JobPO
+  }
+
+  public static JobPO initializeJobPO(JobEntity jobEntity, JobPOBuilder 
builder) {
+    // We should not keep the terminated job entities in the database forever, 
so we set the
+    // current time as the finished timestamp if the job is in a terminal 
state,
+    // So the entity GC cleaner will clean it up later.
+    long finished = DEFAULT_DELETED_AT;
+    if (jobEntity.status() == JobHandle.Status.CANCELLED
+        || jobEntity.status() == JobHandle.Status.FAILED
+        || jobEntity.status() == JobHandle.Status.SUCCEEDED) {
+      finished = System.currentTimeMillis();

Review Comment:
   So the finish time of a `CANCELLING` job is `System.currentTimeMillis()`?  
Why do we set it explicitly in `jobEntity `?



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