huaxingao commented on code in PR #4659:
URL: https://github.com/apache/polaris/pull/4659#discussion_r3416874575


##########
runtime/service/src/main/java/org/apache/polaris/service/idempotency/IdempotencyStoreProducer.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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 io.smallrye.common.annotation.Identifier;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.enterprise.inject.Any;
+import jakarta.enterprise.inject.Instance;
+import jakarta.enterprise.inject.Produces;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.persistence.IdempotencyStore;
+import org.apache.polaris.core.persistence.IdempotencyStoreFactory;
+
+/**
+ * Produces the request-scoped {@link IdempotencyStore} that {@link 
IdempotencyHandlerSupport}
+ * injects directly — so the handler has no factory reference and no lazy-init 
logic.
+ *
+ * <p>This producer lives in the service runtime (which has a {@link 
RealmContext}), not in the
+ * persistence backends: backends expose a realm-agnostic {@link 
IdempotencyStoreFactory} (realm as
+ * a method argument) so they stay deployable where there is no request scope 
(e.g. the Admin Tool).
+ * This producer binds the selected backend to the current request's realm.
+ *
+ * <p>When idempotency is disabled it yields {@link NoOpIdempotencyStore}; 
handlers short-circuit on
+ * {@link IdempotencyHandlerSupport#isEnabled()} and never touch it. Otherwise 
the factory whose
+ * {@link Identifier} matches {@link IdempotencyConfiguration#type()} is 
selected.
+ */
+@ApplicationScoped
+public class IdempotencyStoreProducer {
+
+  @Produces
+  @RequestScoped
+  public IdempotencyStore idempotencyStore(
+      IdempotencyConfiguration configuration,
+      RealmContext realmContext,
+      @Any Instance<IdempotencyStoreFactory> factories) {
+    if (!configuration.enabled()) {
+      return NoOpIdempotencyStore.INSTANCE;
+    }
+    Instance<IdempotencyStoreFactory> selected =
+        factories.select(Identifier.Literal.of(configuration.type()));

Review Comment:
   Removed `IdempotencyStoreFactory` entirely (other thread), so this producer 
is now just a selector over `@Any Instance<IdempotencyStore>`. Nothing left to 
cache — the store is `@RequestScoped` (binds to the request realm), so the only 
per-request cost is a cheap `Instance.select(...)`.get().



##########
runtime/service/src/main/java/org/apache/polaris/service/idempotency/DisabledIdempotencyStore.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 java.time.Instant;
+import java.util.Optional;
+import java.util.UUID;
+import org.apache.polaris.core.entity.IdempotencyRecord;
+import org.apache.polaris.core.persistence.IdempotencyStore;
+
+/**
+ * Inert {@link IdempotencyStore} produced when idempotency is disabled. 
Handlers short-circuit on
+ * {@link IdempotencyHandlerSupport#isEnabled()} and never touch the store, so 
every method here
+ * fails fast: reaching one means a code path tried to use the store while the 
feature is off.
+ */
+final class NoOpIdempotencyStore implements IdempotencyStore {
+
+  static final NoOpIdempotencyStore INSTANCE = new NoOpIdempotencyStore();
+
+  private NoOpIdempotencyStore() {}
+
+  @Override
+  public Optional<IdempotencyRecord> load(UUID idempotencyKey) {
+    throw disabled();

Review Comment:
   Renamed.



##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalogHandler.java:
##########
@@ -460,14 +485,98 @@ public void authorizeCreateTableDirect(
     }
   }
 
+  /**
+   * Create a table, optionally honoring an {@code Idempotency-Key} from the 
REST request.
+   *
+   * <p>When an idempotency key is supplied and the feature is enabled:
+   *
+   * <ol>
+   *   <li>Authorization runs first, so idempotency cannot bypass it.
+   *   <li>Pre-flight loads any prior record for {@code (realm, key)}. A match 
(same caller, same
+   *       resource binding) replays the response from authoritative catalog 
state; a binding
+   *       mismatch raises 422.
+   *   <li>On a fresh key, the table is created and the record is inserted 
afterwards. A concurrent
+   *       caller that wins the race causes our insert to return DUPLICATE — 
we then replay too, so
+   *       the response is equivalent to what the winner returned.
+   * </ol>
+   *
+   * <p>No response body is stored. Replays go through {@code loadTable +
+   * buildLoadTableResponseWithDelegationCredentials}, which re-vends fresh 
credentials for the
+   * current caller.
+   */
   public LoadTableResponse createTableDirect(
       Namespace namespace,
       CreateTableRequest request,
       EnumSet<AccessDelegationMode> delegationModes,
-      Optional<String> refreshCredentialsEndpoint) {
+      Optional<String> refreshCredentialsEndpoint,
+      Optional<UUID> idempotencyKey) {
 
     authorizeCreateTableDirect(namespace, request, !delegationModes.isEmpty());
     Optional<AccessDelegationMode> resolvedMode = 
resolveAccessDelegationModes(delegationModes);
+    TableIdentifier tableIdentifier = TableIdentifier.of(namespace, 
request.name());
+
+    // Pre-flight owns the full idempotency decision: it returns Disabled when 
the feature is off or
+    // no key was supplied (plain create path), Duplicate to replay a prior 
success, or Owned to
+    // proceed. Authorization already ran above, so a replay reloads current 
catalog state (no
+    // response body is stored) and re-vends credentials for this caller;
+    // buildLoadTableResponseForExistingTable raises 422 if the table has 
advanced beyond the
+    // metadata location captured when the key was recorded. A binding 
mismatch surfaces as
+    // IdempotencyConflictException, which PolarisExceptionMapper maps to HTTP 
422.
+    IdempotencyOutcome preflight =
+        idempotencySupport()
+            .preflight(
+                idempotencyKey,
+                polarisPrincipal(),
+                IdempotentOperation.CREATE_TABLE,
+                namespace.toString(),
+                request.name(),
+                resolvedMode.map(Enum::name).orElse("none"));
+    if (preflight instanceof IdempotencyOutcome.Duplicate dup) {

Review Comment:
   Done



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