LuciferYang commented on code in PR #14954:
URL: https://github.com/apache/gravitino/pull/14954#discussion_r4566429150
##########
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:
Done in ed393de6e: `set` now rejects null with `Preconditions.checkNotNull`,
and both methods have `@param` / `@return` Javadoc.
##########
core/src/main/java/org/apache/gravitino/secret/SecretVersionChanges.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.secret;
+
+import java.util.List;
+import javax.annotation.Nullable;
+
+/**
+ * Write-through secret versions that a prepared entity alter wrote or stopped
referencing.
+ *
+ * <p>Created by the {@code SecretAlterChanges.prepare*Changes} methods. After
the entity alter
+ * commits, {@link SecretManager#commitSecrets} applies them; if the alter
fails, {@link
+ * SecretManager#rollbackSecrets(PreparedSecrets)} deletes the written
versions.
+ */
+public final class PreparedSecrets {
+
Review Comment:
Renamed to `SecretVersionChanges` in ed393de6e.
##########
common/src/main/java/org/apache/gravitino/secret/SecretProvider.java:
##########
@@ -68,12 +70,88 @@ public interface SecretProvider {
String readSecret(SecretUrn urn);
/**
- * Deletes a secret by URN.
+ * Deletes a secret by URN, including every version of it.
*
* @param urn the secret URN
*/
void deleteSecret(SecretUrn urn);
+ /**
+ * Writes {@code plaintext} as a new version of the write-through secret
{@code urn}.
+ *
+ * <p>The new version becomes the value {@link #readSecret} returns only
when {@code urn} has no
+ * current version. Otherwise the current version stays readable until {@link
+ * #promoteSecretVersion} is called, so a failed entity alter can discard
the new version and keep
+ * the value the entity still references.
+ *
+ * <p>Providers that support write-through must implement this method and
the other version
+ * methods; altering a write-through secret uses them. Providers that only
build external
+ * references can keep the defaults, which throw.
+ *
+ * <p>Version ids are non-blank and are not reused for the same URN until
{@link #deleteSecret}
+ * removes it, because Gravitino deletes replaced versions by id. Gravitino
passes to the other
+ * version methods only ids this provider returned.
+ *
+ * @param urn the write-through secret URN
+ * @param plaintext the secret plaintext
+ * @return the id of the new version
+ * @throws UnsupportedOperationException if this provider does not support
versions
+ */
+ default String writeSecretVersion(SecretUrn urn, String plaintext) {
+ throw new UnsupportedOperationException(type() + " does not support secret
versions");
+ }
+
+ /**
+ * Makes {@code version} the value {@link #readSecret} returns for {@code
urn}.
+ *
+ * <p>Write-through providers must implement this method; see {@link
#writeSecretVersion}.
+ *
+ * <p>If this method throws, the promote must not take effect afterwards,
and {@link
+ * #currentSecretVersion} must already report whether it did: after a failed
promote Gravitino
+ * deletes the previous version if {@code version} is current and {@code
version} otherwise, and
+ * deletes neither if {@code currentSecretVersion} throws. Settle an unknown
outcome before
+ * throwing, for example with a conditional update, and read the current
version with
+ * read-after-write consistency.
+ *
+ * @param urn the write-through secret URN
+ * @param version a version id returned by {@link #writeSecretVersion}
+ * @throws IllegalArgumentException if {@code urn} has no such version
+ * @throws UnsupportedOperationException if this provider does not support
versions
+ */
+ default void promoteSecretVersion(SecretUrn urn, String version) {
+ throw new UnsupportedOperationException(type() + " does not support secret
versions");
Review Comment:
Renamed to `setCurrentSecretVersion` in ed393de6e. The Javadoc, log
messages, tests and design doc no longer call it promote.
##########
core/src/main/java/org/apache/gravitino/catalog/FilesetOperationDispatcher.java:
##########
@@ -322,7 +323,7 @@ private Fileset alterFilesetWithPreparedSecrets(
SecretMaterialsHolder writtenSecretMaterials = new SecretMaterialsHolder();
boolean alterCommitted = false;
try {
- Pair<FilesetChange[], List<SecretMaterial>> secretResult =
+ Pair<FilesetChange[], PreparedSecrets> secretResult =
Review Comment:
Done in ed393de6e, following the rule you described. The three
`SecretAlterChanges.prepare*Changes` methods and `alterSetProperty` take a
`@Nullable Long entityId`, and both dispatchers pass null instead of `0L` when
there is no `StringIdentifier` and no entity. With a null id,
`setSecretBinding` fails with `IllegalArgumentException`, and so do
`setSecretReference`, `setProperty` and `removeProperty` on a key holding a
write-through URN of that entity type and key; for a schema the message says to
load it first, which imports it. References and plain properties still go
through, and no secret version is written or released. `alterSetSecretBinding`
and `prepareSecretVersions` keep a `long`. `TestSecretManagerAlter` and the two
dispatcher tests cover the no-id cases.
##########
core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java:
##########
@@ -539,7 +555,7 @@ private Pair<Schema, SchemaChange[]>
alterExternalSchemaUnderLock(
SecretMaterialsHolder writtenSecretMaterials = new SecretMaterialsHolder();
boolean alterCommitted = false;
try {
- Pair<SchemaChange[], List<SecretMaterial>> secretResult =
+ Pair<SchemaChange[], PreparedSecrets> secretResult =
Review Comment:
Same change as in the fileset thread (ed393de6e):
`alterExternalSchemaUnderLock` passes null instead of `0L`, so a schema that
was never imported rejects write-through changes and still accepts references
and plain properties. A binding on a schema that does not exist still fails
with `NoSuchSchemaException`.
--
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]