lasdf1234 commented on code in PR #12470:
URL: https://github.com/apache/gravitino/pull/12470#discussion_r4537751883


##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/idempotency/IdempotencyStore.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.iceberg.common.idempotency;
+
+import java.io.Closeable;
+import java.util.Map;
+import java.util.Optional;
+import javax.annotation.Nullable;
+
+/**
+ * Storage for {@code Idempotency-Key} records, pluggable so that a deployment 
can trade durability
+ * for latency.
+ *
+ * <p>The server drives a store through a reserve-execute-finalize cycle: 
exactly one caller wins
+ * {@link #reserve}, executes the mutation, and then either {@link 
#finalizeRecord finalizes} the
+ * response for later replay or {@link #release releases} the reservation so 
the client can retry
+ * with the same key. Implementations must make {@link #reserve} atomic across 
every caller that can
+ * see the same storage, since that is the only thing standing between a 
retried request and a
+ * duplicate mutation.
+ *
+ * <p>A reservation does not always survive until its owner finishes: a 
bounded store can drop it
+ * under memory pressure, and a purge can remove it once it expires. The key 
is then free, and
+ * another caller can reserve it while the first is still running. Every call 
that mutates an
+ * existing record therefore carries the {@code claim} its caller reserved 
with, and implementations
+ * must ignore the call when the stored claim differs, so a caller that has 
lost its reservation
+ * cannot finalize or release a record that now belongs to someone else.
+ *
+ * <p>Keys are compared in the folded form returned by {@link 
IdempotencyKeys#canonicalize}, so
+ * implementations must canonicalize before reading or writing storage rather 
than trusting the
+ * caller to have done it.
+ *
+ * <p>Implementations must be thread-safe and are expected to have a public 
no-argument constructor
+ * so they can be loaded by class name.
+ */
+public interface IdempotencyStore extends Closeable {
+
+  /** Outcome of an attempt to claim a key. */
+  enum ReserveResult {
+    /** The key was newly claimed by this caller, which now owns the mutation. 
*/
+    RESERVED,
+    /** The key is already reserved or finalized, so the request is a retry. */
+    DUPLICATE
+  }
+
+  /**
+   * Initializes the store from the Iceberg REST server configuration.
+   *
+   * @param properties the full Iceberg REST server configuration
+   */
+  void initialize(Map<String, String> properties);
+
+  /**
+   * Atomically attempts to claim a key.
+   *
+   * @param idempotencyKey the client-provided UUIDv7 key
+   * @param operationBinding request identity, for example {@code POST
+   *     /v1/cat1/namespaces/ns1/tables}
+   * @param claim a fencing token identifying this attempt, which the caller 
must generate afresh
+   *     for every reservation and pass back to {@link #finalizeRecord} and 
{@link #release}
+   * @param expiresAtMs time after which the record may be purged, in unix 
epoch millis
+   * @return {@link ReserveResult#RESERVED} if newly claimed, {@link 
ReserveResult#DUPLICATE} if a
+   *     record for this key already exists

Review Comment:
   **Medium — `DUPLICATE` javadoc does not match the in-memory behavior**
   
   The `@return` says `DUPLICATE` when "a record for this key already exists", 
but `InMemoryIdempotencyStore` returns `RESERVED` when the existing entry is 
**expired** (it replaces the row).
   
   Please clarify that `DUPLICATE` applies only to a **non-expired** 
reserved/finalized record. Future JDBC implementations need the same rule so 
expired keys can be reused after the advertised window.



##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/idempotency/IdempotencyStore.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.iceberg.common.idempotency;
+
+import java.io.Closeable;
+import java.util.Map;
+import java.util.Optional;
+import javax.annotation.Nullable;
+
+/**
+ * Storage for {@code Idempotency-Key} records, pluggable so that a deployment 
can trade durability
+ * for latency.
+ *
+ * <p>The server drives a store through a reserve-execute-finalize cycle: 
exactly one caller wins
+ * {@link #reserve}, executes the mutation, and then either {@link 
#finalizeRecord finalizes} the
+ * response for later replay or {@link #release releases} the reservation so 
the client can retry
+ * with the same key. Implementations must make {@link #reserve} atomic across 
every caller that can
+ * see the same storage, since that is the only thing standing between a 
retried request and a
+ * duplicate mutation.
+ *
+ * <p>A reservation does not always survive until its owner finishes: a 
bounded store can drop it
+ * under memory pressure, and a purge can remove it once it expires. The key 
is then free, and
+ * another caller can reserve it while the first is still running. Every call 
that mutates an
+ * existing record therefore carries the {@code claim} its caller reserved 
with, and implementations
+ * must ignore the call when the stored claim differs, so a caller that has 
lost its reservation
+ * cannot finalize or release a record that now belongs to someone else.
+ *
+ * <p>Keys are compared in the folded form returned by {@link 
IdempotencyKeys#canonicalize}, so
+ * implementations must canonicalize before reading or writing storage rather 
than trusting the
+ * caller to have done it.
+ *
+ * <p>Implementations must be thread-safe and are expected to have a public 
no-argument constructor
+ * so they can be loaded by class name.
+ */
+public interface IdempotencyStore extends Closeable {
+
+  /** Outcome of an attempt to claim a key. */
+  enum ReserveResult {
+    /** The key was newly claimed by this caller, which now owns the mutation. 
*/
+    RESERVED,
+    /** The key is already reserved or finalized, so the request is a retry. */
+    DUPLICATE
+  }
+
+  /**
+   * Initializes the store from the Iceberg REST server configuration.
+   *
+   * @param properties the full Iceberg REST server configuration
+   */
+  void initialize(Map<String, String> properties);
+
+  /**
+   * Atomically attempts to claim a key.
+   *
+   * @param idempotencyKey the client-provided UUIDv7 key
+   * @param operationBinding request identity, for example {@code POST
+   *     /v1/cat1/namespaces/ns1/tables}
+   * @param claim a fencing token identifying this attempt, which the caller 
must generate afresh
+   *     for every reservation and pass back to {@link #finalizeRecord} and 
{@link #release}
+   * @param expiresAtMs time after which the record may be purged, in unix 
epoch millis
+   * @return {@link ReserveResult#RESERVED} if newly claimed, {@link 
ReserveResult#DUPLICATE} if a
+   *     record for this key already exists
+   */
+  ReserveResult reserve(
+      String idempotencyKey, String operationBinding, long claim, long 
expiresAtMs);

Review Comment:
   **Medium — `operationBinding` is not enforced at the store layer**
   
   `reserve` stores `operationBinding` but does not compare it when returning 
`DUPLICATE`. Binding mismatch detection is left to the REST handler via 
`load().operationBinding()`.
   
   That works for **finalized** replays (`load` returns the record). For 
**in-flight** duplicates with a wrong binding, `DUPLICATE` + empty `load()` 
likely surfaces as a generic in-flight `409`, not a binding-mismatch error.
   
   Please document that binding checks are handler-owned (and what in-flight + 
mismatch should return), or extend the SPI (e.g. expose in-progress binding / 
distinct conflict outcomes) so Phase 2 JDBC and #12474 stay aligned.



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