yuqi1129 commented on code in PR #11846:
URL: https://github.com/apache/gravitino/pull/11846#discussion_r4548910699


##########
core/src/main/java/org/apache/gravitino/catalog/CapabilityHelpers.java:
##########
@@ -47,17 +47,52 @@
 import org.apache.gravitino.rel.partitions.Partition;
 import org.apache.gravitino.rel.partitions.Partitions;
 import org.apache.gravitino.rel.partitions.RangePartition;
+import org.apache.gravitino.utils.ThrowableFunction;
 
 public class CapabilityHelpers {
 
-  public static Capability getCapability(NameIdentifier ident, CatalogManager 
catalogManager) {
+  /**
+   * Executes {@code fn} with the {@link Capability} of the catalog identified 
by {@code ident},
+   * inside the catalog's classloader boundary. This prevents the catalog's 
{@link
+   * org.apache.gravitino.utils.IsolatedClassLoader} from being closed while 
capability methods are
+   * executing.
+   *
+   * <p>If the first attempt encounters a stale (already-closed) {@link
+   * CatalogManager.CatalogWrapper}, the wrapper is evicted from the cache and 
the call is retried
+   * once with a freshly loaded wrapper.
+   *
+   * <p>Callers should use this method instead of calling {@code 
capabilities()} on a raw wrapper
+   * invoke {@link Capability} methods so that the classloader lifecycle is 
properly bounded.
+   *
+   * @param ident any {@link NameIdentifier} that belongs to the target catalog
+   * @param catalogManager the catalog manager used to load the catalog
+   * @param fn function to execute with the catalog's {@link Capability}
+   * @param <R> return type
+   * @return the result of {@code fn}
+   */
+  public static <R> R withCapability(
+      NameIdentifier ident, CatalogManager catalogManager, 
ThrowableFunction<Capability, R> fn) {
     NameIdentifier catalogIdent = getCatalogIdentifier(ident);
-    CatalogManager.CatalogWrapper c = 
catalogManager.loadCatalogAndWrap(catalogIdent);
-    try {
-      return c.capabilities();
-    } catch (Exception e) {
-      throw new RuntimeException("Failed to get capabilities for catalog: " + 
catalogIdent, e);
+    RuntimeException closedException = null;
+    for (int i = 0; i < 2; i++) {
+      CatalogManager.CatalogWrapper c = 
catalogManager.loadCatalogAndWrap(catalogIdent);
+      try {
+        return c.doWithCapabilityOps(fn);
+      } catch (IllegalStateException e) {
+        if (c.isClosed() && i == 0) {

Review Comment:
   Fixed in 00353330b9. The retry is gone. `withCapability` now runs under the 
catalog lease from #12404, which reloads a retired wrapper before the callback 
runs. So the callback runs at most once and an exception from it is never 
replayed. It reaches the caller unchanged 
(`TestCapabilityHelpers.testWithCapabilityKeepsCallbackException`).



##########
core/src/main/java/org/apache/gravitino/catalog/TableNormalizeDispatcher.java:
##########
@@ -74,26 +74,33 @@ public Table createTable(
       SortOrder[] sortOrders,
       Index[] indexes)
       throws NoSuchSchemaException, TableAlreadyExistsException {
-    Capability capability = getCapability(ident, catalogManager);
-    return dispatcher.createTable(
-        applyCapabilities(ident, Capability.Scope.TABLE, capability),
-        applyCapabilities(columns, capability),
-        comment,
-        properties,
-        applyCapabilities(partitions, capability),
-        applyCapabilities(distribution, capability),
-        applyCapabilities(sortOrders, capability),
-        applyCapabilities(indexes, capability));
+    return withCapability(

Review Comment:
   Fixed in 00353330b9. The callback now only computes the normalized 
identifier and changes. The dispatcher call runs after the lease is released, 
for create/alter table, alter fileset/view/model and all partition methods. 
`TestCatalogWrapperLease.testNormalizeDispatcherCallsBackendOutsideCapabilityLease`
 checks that no lease is held during the backend call.



##########
core/src/main/java/org/apache/gravitino/catalog/CapabilityHelpers.java:
##########
@@ -47,17 +47,56 @@
 import org.apache.gravitino.rel.partitions.Partition;
 import org.apache.gravitino.rel.partitions.Partitions;
 import org.apache.gravitino.rel.partitions.RangePartition;
+import org.apache.gravitino.utils.ThrowableFunction;
 
 public class CapabilityHelpers {
 
-  public static Capability getCapability(NameIdentifier ident, CatalogManager 
catalogManager) {
+  /**
+   * Executes {@code fn} with the {@link Capability} of the catalog identified 
by {@code ident},
+   * inside the catalog's classloader boundary. This prevents the catalog's 
{@link
+   * org.apache.gravitino.utils.IsolatedClassLoader} from being closed while 
capability methods are
+   * executing.
+   *
+   * <p>If the first attempt encounters a stale (already-closed) {@link
+   * CatalogManager.CatalogWrapper}, the wrapper is evicted from the cache and 
the call is retried
+   * once with a freshly loaded wrapper. Retry is safe because {@code fn} is 
restricted to pure
+   * normalization and never performs external I/O, so re-execution is 
idempotent. The retry is
+   * triggered only on {@link CatalogManager.CatalogWrapperClosedException}, 
which is thrown
+   * exclusively at wrapper-entry before {@code fn} has run, preventing 
accidental replay of
+   * partially-executed non-idempotent operations.
+   *
+   * <p>Callers should use this method instead of calling {@code 
capabilities()} on a raw wrapper
+   * invoke {@link Capability} methods so that the classloader lifecycle is 
properly bounded.
+   *
+   * @param ident any {@link NameIdentifier} that belongs to the target catalog
+   * @param catalogManager the catalog manager used to load the catalog
+   * @param fn function to execute with the catalog's {@link Capability}
+   * @param <R> return type
+   * @return the result of {@code fn}
+   */
+  public static <R> R withCapability(
+      NameIdentifier ident, CatalogManager catalogManager, 
ThrowableFunction<Capability, R> fn) {
     NameIdentifier catalogIdent = getCatalogIdentifier(ident);
-    CatalogManager.CatalogWrapper c = 
catalogManager.loadCatalogAndWrap(catalogIdent);
-    try {
-      return c.capabilities();
-    } catch (Exception e) {
-      throw new RuntimeException("Failed to get capabilities for catalog: " + 
catalogIdent, e);
+    CatalogManager.CatalogWrapperClosedException lastClosed = null;
+    for (int i = 0; i < 2; i++) {
+      CatalogManager.CatalogWrapper c = 
catalogManager.loadCatalogAndWrap(catalogIdent);
+      try {
+        return c.doWithCapabilityOps(fn);

Review Comment:
   No longer applies after 00353330b9: there is no retry loop now. The catalog 
lease reloads a retired wrapper once before the callback runs, so there is 
nothing to back off from.



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