dimas-b commented on code in PR #4659:
URL: https://github.com/apache/polaris/pull/4659#discussion_r3406660890


##########
runtime/service/src/main/java/org/apache/polaris/service/idempotency/IdempotencyHandlerSupport.java:
##########
@@ -0,0 +1,309 @@
+/*
+ * 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.polaris.service.idempotency;
+
+import jakarta.annotation.Nullable;
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.inject.Inject;
+import java.time.Clock;
+import java.time.Instant;
+import java.util.Optional;
+import java.util.TreeSet;
+import java.util.UUID;
+import org.apache.polaris.core.DigestUtils;
+import org.apache.polaris.core.auth.PolarisPrincipal;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.entity.IdempotencyRecord;
+import org.apache.polaris.core.persistence.IdempotencyStore;
+import org.apache.polaris.core.persistence.IdempotencyStoreFactory;
+
+/**
+ * Handler-side helper for the single-transaction ("optimistic commit") 
idempotency model.
+ *
+ * <p>Responsibilities:
+ *
+ * <ul>
+ *   <li>Read and validate the {@code Idempotency-Key} request header (UUIDv7 
only).
+ *   <li>Compute the principal/resource hashes that form the binding stored 
alongside each record.
+ *   <li>Pre-flight: from the raw request inputs, decide whether idempotency 
applies and, if so,
+ *       look up an existing record for the same {@code (realm, key)}, 
returning an {@link
+ *       IdempotencyOutcome} for the handler to branch on.
+ *   <li>Record the terminal outcome after a successful operation, returning 
{@link
+ *       IdempotencyOutcome.Owned} on win and {@link 
IdempotencyOutcome.Duplicate} on a race-driven
+ *       duplicate.
+ *   <li>Resolve a concurrent create-table race by polling for the winner's 
record.
+ * </ul>
+ *
+ * <p>This bean is {@link RequestScoped}: a single request operates within one 
realm, so the
+ * realm-scoped {@link IdempotencyStore} is resolved once (lazily) from {@link
+ * IdempotencyStoreFactory} for the request's {@link RealmContext}. When 
idempotency is disabled the
+ * store is never resolved — the bean is an inert shell. No response body is 
stored; duplicate
+ * responses are rebuilt from authoritative catalog state by the handler 
itself.
+ */
+@RequestScoped
+public class IdempotencyHandlerSupport {
+
+  // Bounded lookup for the idempotency record written by a concurrent 
create-table race winner,
+  // which records only after committing the table (so a loser may briefly not 
see it yet). Backoff
+  // is exponential: 5+10+20+40+80 = 155ms total budget across 5 attempts. If 
the winner's record is
+  // still not visible after that (e.g. a long GC pause or slow store write), 
the original 409
+  // surfaces instead of a replay — correct but not ideal; widen the budget if 
this proves too
+  // tight.
+  private static final int CONCURRENT_REPLAY_MAX_ATTEMPTS = 5;
+  private static final long CONCURRENT_REPLAY_INITIAL_BACKOFF_MILLIS = 5;
+
+  @Inject IdempotencyConfiguration configuration;
+  @Inject IdempotencyStoreFactory storeFactory;
+  @Inject RealmContext realmContext;
+  @Inject Clock clock;
+
+  // Resolved lazily on first use within the request; never resolved when 
idempotency is disabled.
+  private IdempotencyStore store;
+
+  /**
+   * Returns an instance with idempotency permanently disabled. Useful for 
test fixtures that need a
+   * non-null {@code IdempotencyHandlerSupport} but exercise non-idempotent 
code paths.
+   */
+  public static IdempotencyHandlerSupport disabled() {
+    IdempotencyHandlerSupport instance = new IdempotencyHandlerSupport();
+    instance.configuration = DisabledConfiguration.INSTANCE;

Review Comment:
   ok



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