LuciferYang commented on code in PR #14954:
URL: https://github.com/apache/gravitino/pull/14954#discussion_r4566443274


##########
core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java:
##########
@@ -551,7 +567,12 @@ private Pair<Schema, SchemaChange[]> 
alterExternalSchemaUnderLock(
               c -> c.doWithSchemaOps(s -> s.alterSchema(ident, 
effectiveChanges)),
               NoSuchSchemaException.class);
       alterCommitted = true;
-      return Pair.of(alteredSchema, effectiveChanges);
+      if (schemaEntityForSecrets == null) {

Review Comment:
   Fixed in ed393de6e with the change described on 
`SchemaOperationDispatcher.java:558`: without an id the alter can no longer 
write versions, so it no longer writes anything under `schema:0:…`.



##########
core/src/main/java/org/apache/gravitino/catalog/FilesetOperationDispatcher.java:
##########
@@ -335,6 +336,7 @@ private Fileset alterFilesetWithPreparedSecrets(
               NoSuchFilesetException.class,
               IllegalArgumentException.class);
       alterCommitted = true;
+      secretManager.commitSecrets(writtenSecretMaterials.get());

Review Comment:
   1. Fixed in ed393de6e, see the reply on 
`FilesetOperationDispatcher.java:326`.
   2. Fixed in ed393de6e: `dropFileset` still checks existence with 
`loadFileset`, then deletes the URNs in both the catalog's properties and the 
`FilesetEntity` properties, since either side can be missing one. 
`testDropDeletesSecretsOnEitherSide` keeps one URN only on each side and checks 
that the drop deletes both.



##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/FilesetCatalogOperations.java:
##########
@@ -758,13 +758,14 @@ public Fileset alterFileset(NameIdentifier ident, 
FilesetChange... changes)
                     existing.properties() == null
                         ? new HashMap<>()
                         : new HashMap<>(existing.properties());
-                Pair<FilesetChange[], List<SecretMaterial>> secretResult =
+                Pair<FilesetChange[], PreparedSecrets> secretResult =
                     SecretAlterChanges.prepareFilesetChanges(
                         secretManager, currentProperties, existing.id(), 
changes);
                 writtenSecretMaterials.set(secretResult.getRight());
                 return updateFilesetEntity(ident, existing, 
secretResult.getLeft());
               });
       alterCommitted = true;
+      secretManager.commitSecrets(writtenSecretMaterials.get());

Review Comment:
   Done in ed393de6e with the dispatcher-only rule, documented on 
`FilesetCatalogOperations.dropFileset`: every server drop goes through 
`FilesetOperationDispatcher.dropFileset`, which deletes the secrets after the 
catalog drop. A cascade schema drop first drops each fileset through that 
dispatcher, so `FilesetCatalogOperations.dropSchema` no longer finds them and 
nothing is deleted twice; its own cleanup is there for a catalog force drop, 
which reaches it without the dispatcher.



##########
core/src/main/java/org/apache/gravitino/secret/SecretManager.java:
##########
@@ -323,6 +324,209 @@ public void rollbackSecrets(List<SecretMaterial> 
secretMaterials) {
     deleteSecrets(urns);
   }
 
+  /**
+   * Makes the versions an alter wrote current and deletes the versions it 
replaced.
+   *
+   * <p>Call only after the entity alter committed. Failures are logged and do 
not fail the
+   * committed alter. If making a written version current fails and the 
provider then reports it as
+   * current, the previous version is deleted; otherwise the written version 
is deleted. If the
+   * current version cannot be looked up, both versions are kept. Running the 
same change again
+   * writes and promotes a new version.
+   *
+   * @param prepared secrets prepared by a {@code 
SecretAlterChanges.prepare*Changes} method
+   */
+  public void commitSecrets(PreparedSecrets prepared) {
+    Preconditions.checkArgument(prepared != null, "prepared must not be null");
+    for (PreparedSecrets.WrittenVersion written : prepared.written()) {
+      // Without a previous version the write already made the new version 
current.
+      if (written.previousId() == null) {
+        continue;
+      }
+      SecretUrn urn = written.version().urn();
+      SecretProvider provider = null;
+      try {
+        provider = registry.getProvider(urn.providerName());
+        provider.promoteSecretVersion(urn, written.version().id());
+      } catch (RuntimeException e) {
+        LOG.error(
+            "Failed to make version {} of secret {} current after the alter 
committed",
+            written.version().id(),
+            urn,
+            e);
+        if (provider != null) {
+          deleteUnusedVersionAfterFailedPromote(provider, written);
+        }
+        continue;
+      }
+      try {
+        provider.deleteSecretVersion(urn, written.previousId());
+      } catch (RuntimeException e) {
+        LOG.warn("Failed to delete replaced version {} of secret {}", 
written.previousId(), urn, e);
+      }
+    }
+    deleteSecretVersions(prepared.released());
+  }
+
+  /**
+   * Deletes the version a failed promote left unused. The promote may have 
taken effect before it
+   * failed, so the version that is current now is kept; if the current 
version cannot be looked up,
+   * both versions are kept.
+   */
+  private static void deleteUnusedVersionAfterFailedPromote(
+      SecretProvider provider, PreparedSecrets.WrittenVersion written) {
+    SecretUrn urn = written.version().urn();
+    String writtenId = written.version().id();
+    String current;
+    try {
+      current = provider.currentSecretVersion(urn);
+    } catch (RuntimeException e) {
+      LOG.warn(
+          "Could not look up the current version of secret {} after a failed 
promote; keeping"
+              + " versions {} and {}",
+          urn,
+          written.previousId(),
+          writtenId,
+          e);
+      return;
+    }
+    String unused;
+    if (writtenId.equals(current)) {
+      LOG.info("Version {} of secret {} is current although its promote 
failed", writtenId, urn);
+      unused = written.previousId();
+    } else {
+      unused = writtenId;
+    }
+    try {
+      provider.deleteSecretVersion(urn, unused);
+    } catch (RuntimeException e) {
+      LOG.warn("Failed to delete unused version {} of secret {}", unused, urn, 
e);
+    }
+  }
+
+  /**
+   * Best-effort delete of the versions an alter wrote, after the entity alter 
failed.
+   *
+   * <p>The versions that were current before the alter are kept, so the 
entity's own write-through
+   * secrets keep the values it still references. A secret that {@link 
#alterSetProperty} rewrote

Review Comment:
   Done in ed393de6e by documenting it: the `setProperty` value description in 
the catalog, schema and fileset OpenAPI docs now says that on the entity's own 
write-through URN the value becomes a new version, and on another secret URN it 
is written through that secret's provider right away and a failed alter does 
not undo it. This is what `setProperty` already does on main, so I kept the 
behavior. With the in-tree providers, another entity's write-through URN only 
gets there from stored data, for example one a dev build wrote under `schema:0` 
before the schema was imported.



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