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


##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/idempotency/InMemoryIdempotencyStore.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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 com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.Expiry;
+import com.github.benmanes.caffeine.cache.RemovalCause;
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.LongSupplier;
+import javax.annotation.Nullable;
+import org.apache.gravitino.iceberg.common.IcebergConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Node-local {@link IdempotencyStore} backed by a Caffeine cache.
+ *
+ * <p>This store is best-effort and intended for single-node deployments, 
development, and tests. It
+ * is not durable, and it is not shared between replicas: a retry routed to 
another node, or to a
+ * node that restarted, finds no record and re-executes the mutation. Its size 
bound can also evict
+ * a record before its reuse window elapses. Production deployments that run 
more than one replica
+ * should use a shared, durable store instead.
+ *
+ * <p>Each record's own {@code expiresAtMs} decides whether it still counts, 
so a record past its
+ * deadline reads as absent whether or not the cache has reclaimed it yet. 
Caffeine's expiry only
+ * reclaims memory, which keeps behavior independent of when maintenance runs 
and matches how a
+ * database-backed store compares {@code expires_at} in its queries.
+ */
+public class InMemoryIdempotencyStore implements IdempotencyStore {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(InMemoryIdempotencyStore.class);
+
+  /** Evictions between two warnings, so an undersized cache is visible 
without flooding the log. */
+  private static final long EVICTION_LOG_INTERVAL = 1000;
+
+  private final AtomicLong sizeEvictions = new AtomicLong();
+  private final LongSupplier clock;
+
+  private Cache<String, IdempotencyRecord> cache;
+
+  /** Creates a store reading wall-clock time. */
+  public InMemoryIdempotencyStore() {
+    this(System::currentTimeMillis);
+  }
+
+  @VisibleForTesting
+  InMemoryIdempotencyStore(LongSupplier clock) {
+    this.clock = clock;
+  }
+
+  @Override
+  public void initialize(Map<String, String> properties) {
+    IcebergConfig icebergConfig = new IcebergConfig(properties);
+    int maxEntries = 
icebergConfig.get(IcebergConfig.ICEBERG_IDEMPOTENCY_MAX_ENTRIES);
+    this.cache =
+        Caffeine.newBuilder()
+            // Expiry off each record's own deadline rather than 
expireAfterWrite, which restarts on
+            // every write: finalizing a record is a write, and would 
otherwise push a record
+            // finalized late in its window well past the lifetime advertised 
to clients.
+            .expireAfter(
+                new Expiry<String, IdempotencyRecord>() {
+                  @Override
+                  public long expireAfterCreate(
+                      String key, IdempotencyRecord record, long 
currentTimeNanos) {
+                    return remainingNanos(record);
+                  }
+
+                  @Override
+                  public long expireAfterUpdate(
+                      String key,
+                      IdempotencyRecord record,
+                      long currentTimeNanos,
+                      long currentDurationNanos) {
+                    return remainingNanos(record);
+                  }
+
+                  @Override
+                  public long expireAfterRead(
+                      String key,
+                      IdempotencyRecord record,
+                      long currentTimeNanos,
+                      long currentDurationNanos) {
+                    return remainingNanos(record);
+                  }
+                })
+            .maximumSize(maxEntries)

Review Comment:
   Addressed in b6a092c63. The store Javadoc and idempotency-max-entries 
configuration documentation now explicitly warn that capacity eviction can 
remove in-flight reservations, allowing a retry to execute concurrently. They 
also clarify that claim fencing protects replacement records but cannot prevent 
duplicate mutations after eviction. Kept the default unchanged, since raising 
it would only postpone the same limit.



##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/idempotency/IdempotencyStoreFactory.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * 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 com.google.common.collect.ImmutableMap;
+import org.apache.gravitino.iceberg.common.IcebergConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Loads the {@link IdempotencyStore} named by {@code 
idempotency-store-type}. */
+public class IdempotencyStoreFactory {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(IdempotencyStoreFactory.class);
+
+  /**
+   * Short names for the built-in stores, so that a deployment does not have 
to spell out a
+   * fully-qualified class name for them.
+   */
+  private static final ImmutableMap<String, String> STORE_CLASS_NAMES =
+      ImmutableMap.of("in-memory", 
InMemoryIdempotencyStore.class.getCanonicalName());
+
+  private IdempotencyStoreFactory() {}
+
+  /**
+   * Creates and initializes the configured store.
+   *
+   * @param icebergConfig the Iceberg REST server configuration
+   * @return an initialized store
+   * @throws RuntimeException if the store cannot be instantiated or 
initialized
+   */
+  public static IdempotencyStore create(IcebergConfig icebergConfig) {
+    String storeType = 
icebergConfig.get(IcebergConfig.ICEBERG_IDEMPOTENCY_STORE_TYPE);
+    String storeClassName = STORE_CLASS_NAMES.getOrDefault(storeType, 
storeType);
+    LOG.info("Load Iceberg idempotency store: {}.", storeClassName);
+    try {
+      IdempotencyStore store =
+          (IdempotencyStore) 
Class.forName(storeClassName).getDeclaredConstructor().newInstance();
+      store.initialize(icebergConfig.getAllConfig());

Review Comment:
   Fixed in b6a092c63. The factory now closes a constructed store when 
initialization fails. If close also fails, that exception is suppressed on the 
original initialization exception. Added regression tests for both successful 
and failing cleanup; both failed before the fix and pass now.



##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/idempotency/InMemoryIdempotencyStore.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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 com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.Expiry;
+import com.github.benmanes.caffeine.cache.RemovalCause;
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.LongSupplier;
+import javax.annotation.Nullable;
+import org.apache.gravitino.iceberg.common.IcebergConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Node-local {@link IdempotencyStore} backed by a Caffeine cache.
+ *
+ * <p>This store is best-effort and intended for single-node deployments, 
development, and tests. It
+ * is not durable, and it is not shared between replicas: a retry routed to 
another node, or to a
+ * node that restarted, finds no record and re-executes the mutation. Its size 
bound can also evict
+ * a record before its reuse window elapses. Production deployments that run 
more than one replica
+ * should use a shared, durable store instead.
+ *
+ * <p>Each record's own {@code expiresAtMs} decides whether it still counts, 
so a record past its
+ * deadline reads as absent whether or not the cache has reclaimed it yet. 
Caffeine's expiry only
+ * reclaims memory, which keeps behavior independent of when maintenance runs 
and matches how a
+ * database-backed store compares {@code expires_at} in its queries.
+ */
+public class InMemoryIdempotencyStore implements IdempotencyStore {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(InMemoryIdempotencyStore.class);
+
+  /** Evictions between two warnings, so an undersized cache is visible 
without flooding the log. */
+  private static final long EVICTION_LOG_INTERVAL = 1000;
+
+  private final AtomicLong sizeEvictions = new AtomicLong();
+  private final LongSupplier clock;
+
+  private Cache<String, IdempotencyRecord> cache;
+
+  /** Creates a store reading wall-clock time. */
+  public InMemoryIdempotencyStore() {
+    this(System::currentTimeMillis);
+  }
+
+  @VisibleForTesting
+  InMemoryIdempotencyStore(LongSupplier clock) {
+    this.clock = clock;
+  }
+
+  @Override
+  public void initialize(Map<String, String> properties) {
+    IcebergConfig icebergConfig = new IcebergConfig(properties);
+    int maxEntries = 
icebergConfig.get(IcebergConfig.ICEBERG_IDEMPOTENCY_MAX_ENTRIES);
+    this.cache =
+        Caffeine.newBuilder()
+            // Expiry off each record's own deadline rather than 
expireAfterWrite, which restarts on
+            // every write: finalizing a record is a write, and would 
otherwise push a record
+            // finalized late in its window well past the lifetime advertised 
to clients.
+            .expireAfter(
+                new Expiry<String, IdempotencyRecord>() {
+                  @Override
+                  public long expireAfterCreate(
+                      String key, IdempotencyRecord record, long 
currentTimeNanos) {
+                    return remainingNanos(record);
+                  }
+
+                  @Override
+                  public long expireAfterUpdate(
+                      String key,
+                      IdempotencyRecord record,
+                      long currentTimeNanos,
+                      long currentDurationNanos) {
+                    return remainingNanos(record);
+                  }
+
+                  @Override
+                  public long expireAfterRead(
+                      String key,
+                      IdempotencyRecord record,
+                      long currentTimeNanos,
+                      long currentDurationNanos) {
+                    return remainingNanos(record);
+                  }
+                })
+            .maximumSize(maxEntries)
+            .removalListener(
+                (String key, IdempotencyRecord record, RemovalCause cause) -> {
+                  if (cause == RemovalCause.SIZE) {
+                    onSizeEviction(maxEntries);
+                  }
+                })
+            .build();
+    LOG.info("Initialized in-memory Iceberg idempotency store, max entries: 
{}.", maxEntries);
+  }
+
+  @Override
+  public ReserveResult reserve(
+      String idempotencyKey, String operationBinding, long claim, long 
expiresAtMs) {
+    String key = IdempotencyKeys.canonicalize(idempotencyKey);
+    IdempotencyRecord reserved =
+        IdempotencyRecord.reserved(key, operationBinding, claim, 
clock.getAsLong(), expiresAtMs);
+    // A record past its deadline is treated as absent rather than waiting for 
the cache to reclaim
+    // it, so behavior turns on the record's own expiry instead of when 
maintenance happens to run.
+    AtomicBoolean claimed = new AtomicBoolean();
+    cache
+        .asMap()
+        .compute(
+            key,
+            (cacheKey, existing) -> {
+              if (existing == null || isExpired(existing)) {
+                claimed.set(true);
+                return reserved;
+              }
+              return existing;
+            });
+    return claimed.get() ? ReserveResult.RESERVED : ReserveResult.DUPLICATE;
+  }
+
+  @Override
+  public Optional<IdempotencyRecord> load(String idempotencyKey) {
+    return 
Optional.ofNullable(cache.getIfPresent(IdempotencyKeys.canonicalize(idempotencyKey)))
+        .filter(record -> !isExpired(record))
+        .filter(IdempotencyRecord::isFinalized);
+  }
+
+  @Override
+  public void finalizeRecord(
+      String idempotencyKey, long claim, int httpStatus, @Nullable String 
responseSummary) {
+    // computeIfPresent, so a record purged while the mutation was running is 
not resurrected. The
+    // claim check leaves a record reserved by someone else untouched.
+    cache
+        .asMap()
+        .computeIfPresent(
+            IdempotencyKeys.canonicalize(idempotencyKey),
+            (key, record) ->
+                record.claim() == claim && !isExpired(record)
+                    ? record.withResponse(httpStatus, responseSummary)
+                    : record);
+  }
+
+  @Override
+  public void release(String idempotencyKey, long claim) {
+    // Returning null removes the entry. A finalized record is left alone so 
that a late release
+    // cannot drop a response another request may still replay, and so is a 
record whose claim has
+    // moved on, so a caller that lost its reservation cannot free a key 
someone else is executing.
+    cache
+        .asMap()
+        .computeIfPresent(
+            IdempotencyKeys.canonicalize(idempotencyKey),
+            (key, record) -> record.isFinalized() || record.claim() != claim ? 
record : null);
+  }
+
+  @Override
+  public int purgeExpired(long beforeMs) {
+    int purged = 0;
+    Iterator<Map.Entry<String, IdempotencyRecord>> entries = 
cache.asMap().entrySet().iterator();
+    while (entries.hasNext()) {
+      Map.Entry<String, IdempotencyRecord> entry = entries.next();
+      if (entry.getValue().expiresAtMs() < beforeMs

Review Comment:
   Fixed in b6a092c63. purgeExpired now uses <=, and the SPI documentation 
specifies an inclusive cutoff. Updated the controllable-clock test to purge 
exactly at the deadline while retaining a later-expiring record. It failed 
before the fix and passes now. All 174 iceberg-common tests pass.



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