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


##########
core/src/main/java/org/apache/gravitino/catalog/CapabilityHelpers.java:
##########
@@ -105,10 +107,45 @@ public static NameIdentifier resolvePhysicalTableName(
     }
   }
 
-  public static Capability getCapability(NameIdentifier ident, CatalogManager 
catalogManager) {
+  /**
+   * Runs {@code fn} with the {@link Capability} of the catalog that owns 
{@code ident}, while a
+   * catalog lease is held and the catalog ClassLoader is the thread context 
ClassLoader.
+   *
+   * <p>A capability is loaded by the catalog ClassLoader, and its methods can 
load more classes
+   * from it on first use. The lease keeps that ClassLoader open until {@code 
fn} returns, even if
+   * the catalog is evicted from the cache meanwhile. A retired catalog is 
reloaded by the lease, so
+   * {@code fn} runs at most once.
+   *
+   * <p>{@code fn} must only compute values from the capability, such as 
normalized names or
+   * changes, and must not return or keep the capability. It must not call the 
backend or the entity
+   * store: those calls belong after this method returns, outside the catalog 
ClassLoader.
+   *
+   * @param ident any identifier inside the catalog
+   * @param catalogManager the catalog manager that loads the catalog
+   * @param fn the computation on the capability
+   * @param <R> the result type
+   * @return the result of {@code fn}
+   * @throws NoSuchCatalogException if the catalog does not exist
+   * @throws RuntimeException the exception thrown by {@code fn}, unchanged, 
or a wrapper of a
+   *     failure to load the catalog or its capability
+   */
+  public static <R> R withCapability(
+      NameIdentifier ident, CatalogManager catalogManager, 
Function<Capability, R> fn) {
     NameIdentifier catalogIdent = getCatalogIdentifier(ident);
     try {
-      return catalogManager.doWithCatalog(catalogIdent, catalog -> 
catalog.capability());
+      return catalogManager.doWithCatalog(
+          catalogIdent,
+          catalog -> {
+            Capability capability = catalog.capability();
+            try {
+              return fn.apply(capability);
+            } catch (RuntimeException e) {
+              // Keep the caller's exception, such as an invalid name, apart 
from load failures.
+              throw new CapabilityCallbackException(e);

Review Comment:
   Only RuntimeException is wrapped, so a callback that throws a checked-like 
failure through ThrowableFunction would be treated as a load failure. Consider 
a test that an invalid-name IllegalArgumentException still reaches the caller 
unwrapped for each dispatcher.



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