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


##########
core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java:
##########
@@ -171,14 +172,17 @@ public void insertSchema(SchemaEntity schemaEntity, 
boolean overwrite) throws IO
         String[] parts = schemaName.split(Pattern.quote(logicalSep), -1);
         for (int nSeg = 1; nSeg < parts.length; nSeg++) {
           String ancestorLogical = String.join(logicalSep, 
Arrays.copyOf(parts, nSeg));
+          // Creating a registration does not imply that this request created 
the external
+          // ancestor. Keep its audit unknown rather than attributing the 
leaf's audit to it;
+          // callers that know the ancestor's audit import that namespace 
explicitly.
           SchemaEntity ancestor =
               SchemaEntity.builder()
                   .withId(nextIdForNestedAncestor())
                   .withName(ancestorLogical)
                   .withNamespace(schemaEntity.namespace())
                   .withComment(null)
                   .withProperties(Collections.emptyMap())
-                  .withAuditInfo(schemaEntity.auditInfo())
+                  .withAuditInfo(AuditInfo.EMPTY)

Review Comment:
   Yes, this is intentional for all catalogs. A synthetic ancestor registration 
does not establish who created the external namespace, so copying the leaf's 
creator/time would make an unsupported attribution in non-Iceberg flows too. 
Only those generated ancestor rows receive an unknown audit; the requested leaf 
keeps its supplied audit. A caller that knows an ancestor's source or creation 
audit should explicitly import that namespace with it.
   
   Clarified the catalog-independent scope in 3f94660eb2. The core storage test 
`testInsertHierarchicalSchemaCreatesAncestorsAndLeaf` directly exercises this 
behavior without an IRC hook, checking ancestor/leaf audit JSON, list/get and 
DTO conversion. Both selected H2 hierarchy tests passed.
   



##########
core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java:
##########
@@ -676,10 +657,61 @@ private static Map<String, String> 
propertiesForSchemaEntityAlter(
     return newProps;
   }
 
-  private void importSchema(NameIdentifier identifier) {
+  private Schema loadAndImportSchema(NameIdentifier ident, AuditInfo 
importAudit)
+      throws NoSuchSchemaException {
+    // Load the schema and check if this schema is already imported.
+    EntityCombinedSchema schema =
+        TreeLockUtils.doWithTreeLock(ident, LockType.READ, () -> 
internalLoadSchema(ident));
+
+    if (!schema.imported()) {
+      try {
+        schema =
+            TreeLockUtils.doWithTreeLock(
+                NameIdentifier.of(ident.namespace().levels()),
+                LockType.WRITE,
+                () -> importSchemaFromCatalog(ident, importAudit));
+      } catch (EntityAlreadyExistsException e) {
+        // HA race: another Gravitino node concurrently imported this schema. 
Reload from the
+        // entity store to verify the entity stored by the winning node is 
consistent.
+        LOG.info(
+            "Schema {} was concurrently imported by another node; reloading 
from store.", ident);
+        EntityCombinedSchema reloaded =
+            TreeLockUtils.doWithTreeLock(ident, LockType.READ, () -> 
internalLoadSchema(ident));
+        if (!reloaded.imported()) {
+          throw new UnsupportedOperationException(
+              "Schema managed by multiple catalogs. This may cause unexpected 
issues such as "
+                  + "privilege conflicts. To resolve: Remove all catalogs 
managing this schema, "
+                  + "then recreate one catalog to ensure single-catalog 
management.");
+        }
+        schema = reloaded;
+      }
+    }
+
+    SchemaEntity entity =
+        fillMissingCreationAudit(

Review Comment:
   Added the explanation in 3f94660eb2: the response uses the source snapshot 
already read, so a concurrent rename/drop can happen after that read. Audit 
recovery protects the stored registration with an identity check and the 
store's version check; it does not promise a fresh source snapshot at response 
time or retry the old audit against a replacement.
   
   Checking this also exposed a small gap: a rename/drop can produce 
`NoSuchEntityException` before the update reads the row or when its CAS fails. 
The helper now handles that by returning no registration, so the caller returns 
its source snapshot marked not imported. Added a helper test that verifies no 
retry, plus `testSchemaAuditRecoveryToleratesConcurrentRenameAndDrop`, which 
changes the real source and H2 registration before recovery and verifies the 
old registration stays absent and the renamed row's identity/audit remain 
intact. All 150 selected core unit tests, 2 core H2 tests and 7 hook/storage 
tests passed; Spotless passed.
   



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