roryqi commented on code in PR #14540:
URL: https://github.com/apache/gravitino/pull/14540#discussion_r4535091180


##########
core/src/main/java/org/apache/gravitino/catalog/OperationDispatcher.java:
##########
@@ -83,6 +89,85 @@ protected OperationDispatcher(
     this.secretManager = secretManager;
   }
 
+  /**
+   * Fills missing creation fields when another node imported the same object 
first. The cold import
+   * already carries these fields in its initial write and needs no additional 
update. Ordinary
+   * loads pass an empty requested audit, leaving an unknown source creator 
unknown. Retry once
+   * after a concurrent update if the original registration still has no 
creator.
+   *
+   * @param entity the imported registration, or null for a managed object
+   * @param type the entity class
+   * @param reported the audit reported by the source catalog
+   * @param requested the creation audit supplied by the create operation, or 
empty for ordinary
+   *     loads
+   * @param withAudit copies the current entity with the new audit
+   * @param <E> the auditable entity type
+   * @return the current registration, with missing creation fields filled if 
it still matches, or
+   *     null if the registration disappeared during recovery; callers then 
return the source
+   *     snapshot as not imported
+   */
+  @Nullable
+  protected <E extends Entity & HasIdentifier & Auditable> E 
fillMissingCreationAudit(
+      @Nullable E entity,
+      Class<E> type,
+      Audit reported,
+      AuditInfo requested,
+      BiFunction<E, AuditInfo, E> withAudit) {
+    if (entity == null || entity.auditInfo().creator() != null || 
requested.creator() == null) {
+      return entity;
+    }
+    E latest = entity;
+    for (int attempt = 0; attempt < 2; attempt++) {
+      try {
+        return store.update(
+            entity.nameIdentifier(),
+            type,
+            entity.type(),
+            current -> {
+              // Recheck inside the store's optimistic update, including 
identity: the name may
+              // have been dropped and recreated since the source snapshot was 
read.
+              if (!current.id().equals(entity.id())) {
+                throw new OptimisticLockException("Entity identity changed 
during audit import");
+              }
+              Audit audit = current.auditInfo();
+              if (audit.creator() != null) {
+                return current;
+              }
+              AuditInfo filled =
+                  AuditInfo.builder()
+                      .withCreator(
+                          reported.creator() == null ? requested.creator() : 
reported.creator())
+                      .withCreateTime(
+                          audit.createTime() != null
+                              ? audit.createTime()
+                              : reported.createTime() != null
+                                  ? reported.createTime()
+                                  : requested.createTime())
+                      .withLastModifier(audit.lastModifier())
+                      .withLastModifiedTime(audit.lastModifiedTime())
+                      .build();
+              return withAudit.apply(current, filled);
+            });
+      } catch (NoSuchEntityException e) {
+        // A rename or drop can remove the observed registration before the 
store read or CAS.
+        // Return the source snapshot without a registration; never resurrect 
it with the old audit.
+        return null;
+      } catch (OptimisticLockException e) {
+        latest = getEntity(entity.nameIdentifier(), entity.type(), type);
+        if (latest == null
+            || !latest.id().equals(entity.id())
+            || latest.auditInfo().creator() != null) {
+          return latest;
+        }
+        // The updater rechecks the original ID before applying the audit on 
the retry.
+      } catch (IOException e) {
+        throw new RuntimeException(
+            "Failed to import creation audit for " + entity.nameIdentifier(), 
e);

Review Comment:
   Restored the previous best-effort behavior: audit-recovery failures now log 
a warning and return the latest observed entity, so an already successful 
source create/register is not failed by this follow-up audit write. Updated the 
existing unit test to cover both IOException and an unchecked store failure.
   



##########
core/src/main/java/org/apache/gravitino/catalog/OperationDispatcher.java:
##########
@@ -83,6 +89,85 @@ protected OperationDispatcher(
     this.secretManager = secretManager;
   }
 
+  /**
+   * Fills missing creation fields when another node imported the same object 
first. The cold import
+   * already carries these fields in its initial write and needs no additional 
update. Ordinary
+   * loads pass an empty requested audit, leaving an unknown source creator 
unknown. Retry once
+   * after a concurrent update if the original registration still has no 
creator.
+   *
+   * @param entity the imported registration, or null for a managed object
+   * @param type the entity class
+   * @param reported the audit reported by the source catalog
+   * @param requested the creation audit supplied by the create operation, or 
empty for ordinary
+   *     loads
+   * @param withAudit copies the current entity with the new audit
+   * @param <E> the auditable entity type
+   * @return the current registration, with missing creation fields filled if 
it still matches, or
+   *     null if the registration disappeared during recovery; callers then 
return the source
+   *     snapshot as not imported
+   */
+  @Nullable
+  protected <E extends Entity & HasIdentifier & Auditable> E 
fillMissingCreationAudit(
+      @Nullable E entity,
+      Class<E> type,
+      Audit reported,
+      AuditInfo requested,
+      BiFunction<E, AuditInfo, E> withAudit) {
+    if (entity == null || entity.auditInfo().creator() != null || 
requested.creator() == null) {
+      return entity;
+    }
+    E latest = entity;
+    for (int attempt = 0; attempt < 2; attempt++) {
+      try {
+        return store.update(
+            entity.nameIdentifier(),
+            type,
+            entity.type(),
+            current -> {
+              // Recheck inside the store's optimistic update, including 
identity: the name may
+              // have been dropped and recreated since the source snapshot was 
read.
+              if (!current.id().equals(entity.id())) {
+                throw new OptimisticLockException("Entity identity changed 
during audit import");

Review Comment:
   The production store in this repository uses RelationalEntityStore with 
JDBCBackend. The service invokes the callback directly, JDBCBackend rolls back 
without wrapping it, and lockWaitTimeoutAsConflict returns an existing 
OptimisticLockException unchanged. I strengthened the existing H2 test 
testTableImportReturnsRecreatedEntityWithoutOldAudit with an assertion that the 
returned registration ID matches the recreated row. This distinguishes the 
optimistic-conflict path from the generic failure fallback, which would return 
the old registration. No new backend handling was added.
   



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