laserninja commented on code in PR #14972:
URL: https://github.com/apache/gravitino/pull/14972#discussion_r4594426313


##########
docs/iceberg-rest-service.md:
##########
@@ -178,6 +178,41 @@ The settings below tune the background workers and are 
optional.
 | `gravitino.iceberg-rest.async-cleanup.max-attempts`           | Number of 
failed attempts before a cleanup job is marked `FAILED`.                        
           | `5`           | No       |
 | `gravitino.iceberg-rest.async-cleanup.retention-hours`        | Retention 
time for terminal `SUCCEEDED` or `FAILED` cleanup rows before pruning.          
           | `720`         | No       |
 
+### JDBC Idempotency Storage
+
+The `jdbc` idempotency store persists request reservations and response bodies 
in Gravitino's
+relational entity store. Replicas connected to the same database share 
records, and finalized
+responses survive Iceberg REST service restarts. This supplies the storage 
backend for
+[Idempotency-Key endpoint 
integration](https://github.com/apache/gravitino/pull/12474); the storage
+backend alone does not enable HTTP request replay.
+
+Use this backend in auxiliary mode, with an initialized relational entity 
store. Standalone Iceberg
+REST servers cannot use it. It reuses Gravitino's connection pool and supports 
H2, MySQL, and
+PostgreSQL. For multiple replicas, use the same shared MySQL or PostgreSQL 
database; separate local
+H2 databases do not provide cross-replica deduplication.
+
+| Configuration item | Description | Default value | Required |
+|--------------------|-------------|---------------|----------|
+| `gravitino.iceberg-rest.idempotency-store-type` | Select `jdbc` for durable 
storage, `in-memory` for node-local storage, or a custom implementation class. 
| `in-memory` | No |
+| `gravitino.iceberg-rest.idempotency-cleanup-interval` | Time between bounded 
JDBC cleanup passes, as an ISO-8601 duration of at least one millisecond. | 
`PT5M` | No |
+
+Initialize a new database with the 2.0.0 schema or apply the 1.3.0-to-2.0.0 
upgrade before selecting
+`jdbc`. An existing development 2.0.0 database created before this table was 
added must also apply
+the `iceberg_idempotency_key_meta` table and its expiry index from the 
matching database's schema
+script. Store initialization fails if the table is missing. The new table is 
additive and does not
+modify catalog metadata.
+
+Reservations are atomic across replicas. A claim token prevents an old request 
from finalizing or
+releasing a newer reservation. Expiry is enforced on reads, finalization, and 
reservation attempts;
+reusing an expired key does not depend on the cleanup schedule. Each cleanup 
pass deletes at most
+100 batches of 1,000 expired rows, and transient failures are retried on the 
next pass. Synchronize
+replica clocks because reuse-window deadlines are evaluated using each 
server's UTC clock.
+
+Durable storage does not make catalog mutations and response persistence one 
transaction. A crash
+or response-persistence failure after the catalog mutation can still allow 
re-execution after the
+reservation is released or expires. Claim fencing protects idempotency 
records, not external catalog
+side effects. A request running beyond its reuse window can overlap a retry.

Review Comment:
   Promoted clock synchronization to a dedicated warning in 8aa3d9080, 
including the replay/re-execution consequence, offset monitoring, and stopping 
an unsynchronized service instance so its cleanup task cannot expire records 
early. The existing non-atomic catalog/response and TTL-overlap limitations 
remain explicit.



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/idempotency/mapper/provider/base/IcebergIdempotencyKeyBaseSQLProvider.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.service.idempotency.mapper.provider.base;
+
+import static 
org.apache.gravitino.iceberg.service.idempotency.mapper.IcebergIdempotencyKeyMapper.TABLE_NAME;
+
+import 
org.apache.gravitino.iceberg.service.idempotency.po.IcebergIdempotencyKeyPO;
+import org.apache.ibatis.annotations.Param;
+
+/** SQL statements shared by the MySQL and H2 entity stores. */
+public class IcebergIdempotencyKeyBaseSQLProvider {
+  /**
+   * Returns the reservation INSERT.
+   *
+   * @param po reservation to persist
+   * @return SQL statement
+   */
+  public String insert(@Param("po") IcebergIdempotencyKeyPO po) {
+    return "INSERT INTO "
+        + TABLE_NAME
+        + " (idempotency_key, operation_binding, claim, audit_info, 
created_at, expires_at)"
+        + " VALUES (#{po.idempotencyKey}, #{po.operationBinding}, #{po.claim}, 
#{po.auditInfo},"
+        + " #{po.createdAt}, #{po.expiresAt})";
+  }
+
+  /**
+   * Returns an atomic replacement of an expired reservation or response.
+   *
+   * @param po reservation to persist
+   * @return SQL statement
+   */
+  public String replaceExpired(@Param("po") IcebergIdempotencyKeyPO po) {
+    return "UPDATE "
+        + TABLE_NAME
+        + " SET operation_binding = #{po.operationBinding}, claim = 
#{po.claim},"
+        + " http_status = NULL, response_summary = NULL, audit_info = 
#{po.auditInfo},"
+        + " created_at = #{po.createdAt}, expires_at = #{po.expiresAt}"
+        + " WHERE idempotency_key = #{po.idempotencyKey} AND expires_at <= 
#{po.createdAt}";
+  }
+
+  /**
+   * Returns only a finalized, unexpired response.
+   *
+   * @param idempotencyKey canonical UUIDv7 key
+   * @param nowMs current time in unix epoch milliseconds
+   * @return SQL statement
+   */
+  public String selectRecord(
+      @Param("idempotencyKey") String idempotencyKey, @Param("nowMs") long 
nowMs) {
+    return "SELECT idempotency_key AS idempotencyKey, operation_binding AS 
operationBinding,"
+        + " claim, http_status AS httpStatus, response_summary AS 
responseSummary,"
+        + " audit_info AS auditInfo, created_at AS createdAt, expires_at AS 
expiresAt FROM "
+        + TABLE_NAME
+        + " WHERE idempotency_key = #{idempotencyKey}"
+        + " AND http_status IS NOT NULL AND expires_at > #{nowMs}";
+  }
+
+  /**
+   * Returns a finalization guarded by the reservation's claim and expiry.
+   *
+   * @param idempotencyKey canonical UUIDv7 key
+   * @param claim reservation fencing token
+   * @param httpStatus response status
+   * @param responseSummary serialized response body, or null for an empty body
+   * @param nowMs current time in unix epoch milliseconds
+   * @return SQL statement
+   */
+  public String finalizeRecord(
+      @Param("idempotencyKey") String idempotencyKey,
+      @Param("claim") long claim,
+      @Param("httpStatus") int httpStatus,
+      @Param("responseSummary") String responseSummary,
+      @Param("nowMs") long nowMs) {
+    return "UPDATE "
+        + TABLE_NAME
+        + " SET http_status = #{httpStatus}, response_summary = 
#{responseSummary}"
+        + " WHERE idempotency_key = #{idempotencyKey} AND claim = #{claim}"
+        + " AND http_status IS NULL AND expires_at > #{nowMs}";
+  }

Review Comment:
   Agreed. Kept these fencing predicates and the void/no-op SPI contract 
unchanged. In 8aa3d9080, zero-row finalize/release paths now emit debug 
diagnostics, and the added release/re-reserve tests verify stale callers cannot 
affect the new owner. Completion of either method alone is not confirmation 
that a row was mutated.



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