lasdf1234 commented on code in PR #14954:
URL: https://github.com/apache/gravitino/pull/14954#discussion_r4560839779
##########
core/src/main/java/org/apache/gravitino/secret/SecretMaterialsHolder.java:
##########
@@ -18,25 +18,23 @@
*/
package org.apache.gravitino.secret;
-import java.util.List;
-
/**
- * Mutable holder so {@code store.update} lambdas can record written secrets
for rollback.
+ * Mutable holder so {@code store.update} lambdas can record prepared secrets.
*
* <p>Used by catalog, schema, and fileset alter paths that prepare secrets
inside {@code
- * store.update} and roll back on failure.
+ * store.update}, commit them after the update and roll them back on failure.
*/
public final class SecretMaterialsHolder {
- private List<SecretMaterial> materials = List.of();
+ private PreparedSecrets prepared = PreparedSecrets.EMPTY;
- /** Returns written secret materials. */
- public List<SecretMaterial> get() {
- return materials;
+ /** Returns the prepared secrets. */
+ public PreparedSecrets get() {
+ return prepared;
}
- /** Records written secret materials. */
- public void set(List<SecretMaterial> materials) {
- this.materials = materials;
+ /** Records the prepared secrets. */
+ public void set(PreparedSecrets prepared) {
Review Comment:
**Low (project convention) — `set` should reject null.**
Elsewhere in core secrets / catalog code, mutable holders and maps use Guava
`Preconditions.checkNotNull` (or `@Nullable` + documented empty). A `null` here
makes `commitSecrets` / `rollbackSecrets` NPE in `finally` paths.
```java
public void set(PreparedSecrets prepared) {
this.prepared = Preconditions.checkNotNull(prepared, "prepared");
}
```
Also add `@param prepared` to satisfy the public Javadoc bar used elsewhere
in this package.
##########
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
+ * right away is not restored.
+ *
+ * @param prepared secrets prepared by a {@code
SecretAlterChanges.prepare*Changes} method
+ */
+ public void rollbackSecrets(PreparedSecrets prepared) {
+ Preconditions.checkArgument(prepared != null, "prepared must not be null");
+ List<PreparedSecrets.Version> versions = new
ArrayList<>(prepared.written().size());
+ for (PreparedSecrets.WrittenVersion written : prepared.written()) {
+ versions.add(written.version());
+ }
+ deleteSecretVersions(versions);
+ }
+
+ /**
+ * Writes the plaintexts of an alter batch as new versions and finds the
versions it releases.
+ *
+ * <p>Works from the properties before and after the whole batch, so a batch
that changes one key
+ * several times is treated by its net effect. A URN that has a current
version keeps it readable
+ * until {@link #commitSecrets}. If a write fails, the versions already
written are deleted.
+ *
+ * @param startProperties entity properties before the alter (may be null)
+ * @param finalProperties entity properties after the alter
+ * @param entityType {@code catalog}, {@code schema}, or {@code fileset}
+ * @param entityId stable numeric entity id
+ * @param plaintexts property key → plaintext to write; each key holds a
write-through URN of this
+ * entity in {@code finalProperties}
+ * @return the written and released versions
+ * @throws UnsupportedOperationException if a provider of a written or
released secret does not
+ * support secret versions
+ */
+ PreparedSecrets prepareSecretVersions(
Review Comment:
**Note — package-private + Javadoc is fine for Gravitino core.**
Released-before-write ordering and partial-write cleanup in the `catch`
match the design and the module’s usual “best-effort cleanup + rethrow” style.
No change needed for convention; keep this out of the public API surface (good).
--
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]