You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gravitino.apache.org by "yuqi1129 (via GitHub)" <gi...@apache.org> on 2026/06/04 09:45:48 UTC

[PR] [#11301] fix(catalog): guard ClassLoaderResourceCleanerUtils against shared-class static-field mutation [gravitino]

yuqi1129 opened a new pull request, #11431:
URL: https://github.com/apache/gravitino/pull/11431

   ### What changes were proposed in this pull request?
   
   When `IsolatedClassLoader` resolves Hadoop/AWS/Azure classes via parent
   delegation (e.g. `hadoop-common.jar` or `gravitino-aws-bundle.jar` appears
   on the AppClassLoader's classpath), `Class.forName(..., targetClassLoader)`
   returns the parent-loaded class. The static fields on that class
   (`MutableQuantiles.scheduler`, `FileSystem.CACHE`, `AwsSdkMetrics` MBean,
   `AbfsClientThrottlingIntercept` timers) are JVM-global and shared across
   every catalog. Calling `scheduler.shutdownNow()` on the global instance
   permanently terminates Hadoop metrics scheduling, causing
   `RejectedExecutionException` on any subsequent catalog that creates a
   `MutableQuantiles` instance (e.g. S3A FileSystem instrumentation).
   
   This PR adds `isOwnedByClassLoader(clazz, targetClassLoader)` guards before
   any static-state mutation in `ClassLoaderResourceCleanerUtils`. If the class
   was delegated to a parent ClassLoader, cleanup is skipped; if it belongs to the
   catalog's own ClassLoader (the normal isolated case), cleanup proceeds as before.
   
   The same guard is applied to:
   - `closeStatsDataClearerInFileSystem`: `FileSystem.closeAll()`, scheduler shutdown, `STATS_DATA_CLEANER` thread interrupt.
   - `closeResourceInAWS`: `AwsSdkMetrics` MBean unregistration.
   - `closeResourceInGCP`: shaded `LogFactory` release.
   - `closeResourceInAzure`: `AbfsClientThrottlingIntercept` timer cancellation.
   
   Additionally, `closeClassLoaderResource` calls are added to
   `HiveCatalogOperations` and `FilesetCatalogOperations`, which use Hadoop
   FileSystem extensively but had no classloader cleanup on close, risking thread
   and memory leaks.
   
   ### Why are the changes needed?
   
   Fix: #11301
   
   After an Iceberg or Paimon catalog is evicted from the cache and re-created,
   any subsequent S3A FileSystem initialization fails with:
   
   ```
   RejectedExecutionException: Task ... rejected from
   ScheduledThreadPoolExecutor@...[Terminated, pool size = 0, active threads = 0,
   queued tasks = 0, completed tasks = 23664]
   ```
   
   because `closeStatsDataClearerInFileSystem` shut down the JVM-global
   `MutableQuantiles.scheduler` when Hadoop happened to be on the AppClassLoader's
   classpath.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No functional behavior change for correctly isolated deployments. For deployments
   where Hadoop is on the server's main classpath, the broken scheduler-shutdown is
   now skipped, restoring correct behavior after catalog re-creation.
   
   ### How was this patch tested?
   
   - `./gradlew :catalogs:catalog-common:spotlessApply :catalogs:catalog-hive:spotlessApply :catalogs:catalog-fileset:spotlessApply`
   - `./gradlew :catalogs:catalog-hive:test :catalogs:catalog-fileset:test -PskipDockerTests=true`


-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11301] fix(catalog): guard ClassLoaderResourceCleanerUtils against shared-class static-field mutation [gravitino]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #11431:
URL: https://github.com/apache/gravitino/pull/11431#issuecomment-4622002254

   <!-- coverage-report -->
   ### Code Coverage Report
   |Overall Project|66.77% **`+0.1%`**|:green_circle:|
   |:-|:-|:-:|
   |Files changed|73.47%|:green_circle:|
   <br>
   
   |Module|Coverage||
   |:-|:-|:-:|
   |aliyun|1.72%|:red_circle:|
   |api|46.82%|:green_circle:|
   |authorization-common|85.96%|:green_circle:|
   |aws|3.66%|:red_circle:|
   |azure|2.47%|:red_circle:|
   |catalog-common|9.62% **`-3.99%`**|:red_circle:|
   |catalog-fileset|80.35% **`+11.6%`**|:green_circle:|
   |catalog-glue|66.67%|:green_circle:|
   |catalog-hive|79.35% **`+2.55%`**|:green_circle:|
   |catalog-jdbc-clickhouse|80.02%|:green_circle:|
   |catalog-jdbc-common|45.31%|:green_circle:|
   |catalog-jdbc-doris|80.28%|:green_circle:|
   |catalog-jdbc-hologres|54.03%|:green_circle:|
   |catalog-jdbc-mysql|79.23%|:green_circle:|
   |catalog-jdbc-oceanbase|78.38%|:green_circle:|
   |catalog-jdbc-postgresql|82.29%|:green_circle:|
   |catalog-jdbc-starrocks|78.51%|:green_circle:|
   |catalog-kafka|77.01%|:green_circle:|
   |catalog-lakehouse-generic|44.89%|:green_circle:|
   |catalog-lakehouse-hudi|79.1%|:green_circle:|
   |catalog-lakehouse-iceberg|85.66%|:green_circle:|
   |catalog-lakehouse-paimon|79.29%|:green_circle:|
   |catalog-model|77.72%|:green_circle:|
   |cli|44.51%|:green_circle:|
   |client-java|77.91%|:green_circle:|
   |common|49.99%|:green_circle:|
   |core|82.49%|:green_circle:|
   |filesystem-hadoop3|76.97%|:green_circle:|
   |flink|0.0%|:red_circle:|
   |flink-common|41.2%|:green_circle:|
   |flink-runtime|0.0%|:red_circle:|
   |gcp|14.12%|:red_circle:|
   |hadoop-common|10.39%|:red_circle:|
   |hive-metastore-common|53.26%|:green_circle:|
   |iceberg-common|56.54%|:green_circle:|
   |iceberg-rest-server|72.76%|:green_circle:|
   |idp-basic|85.99%|:green_circle:|
   |integration-test-common|0.0%|:red_circle:|
   |jobs|66.17%|:green_circle:|
   |lance-common|20.83%|:red_circle:|
   |lance-rest-server|60.27%|:green_circle:|
   |lineage|53.02%|:green_circle:|
   |optimizer|82.95%|:green_circle:|
   |optimizer-api|21.95%|:red_circle:|
   |server|85.73%|:green_circle:|
   |server-common|73.16%|:green_circle:|
   |spark|29.85%|:red_circle:|
   |spark-common|40.78%|:green_circle:|
   |trino-connector|39.44%|:red_circle:|
   
   <details>
   <summary>Files</summary>
   
   |Module|File|Coverage||
   |:-|:-|:-|:-:|
   |catalog-common|[ClassLoaderResourceCleanerUtils.java](https://github.com/yuqi1129/gravitino/blob/b5d43ea4e8988454ee0039f65d5e325d1e9db6c8/catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java)|0.0%|:red_circle:|
   |catalog-fileset|[FilesetCatalogOperations.java](https://github.com/yuqi1129/gravitino/blob/b5d43ea4e8988454ee0039f65d5e325d1e9db6c8/catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/FilesetCatalogOperations.java)|82.39%|:green_circle:|
   |catalog-hive|[HiveCatalogOperations.java](https://github.com/yuqi1129/gravitino/blob/b5d43ea4e8988454ee0039f65d5e325d1e9db6c8/catalogs/catalog-hive/src/main/java/org/apache/gravitino/catalog/hive/HiveCatalogOperations.java)|81.74%|:green_circle:|
   
   </details>


-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11301] fix(catalog): guard ClassLoaderResourceCleanerUtils against shared-class static-field mutation [gravitino]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #11431:
URL: https://github.com/apache/gravitino/pull/11431#discussion_r3360159035


##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -301,6 +345,16 @@ private static void closeResourceInAzure(ClassLoader classLoader) throws Excepti
     MethodUtils.invokeStaticMethod(relocatedLogFactory, "release", classLoader);
   }
 
+  /**
+   * Returns true if {@code clazz} was loaded directly by {@code classLoader} (not delegated to a
+   * parent). Use this before touching static fields that must belong to the catalog's own
+   * classloader to avoid accidentally mutating JVM-global shared state.
+   */
+  @VisibleForTesting
+  static boolean isOwnedByClassLoader(Class<?> clazz, ClassLoader classLoader) {
+    return clazz.getClassLoader() == classLoader;
+  }

Review Comment:
   `isOwnedByClassLoader` currently returns `true` when both `clazz.getClassLoader()` and `classLoader` are `null` (bootstrap loader). That makes the guard treat bootstrap-loaded classes as “owned”, which is risky if `closeClassLoaderResource(null)` (or any other accidental `null` loader) ever happens, because it could allow static-state mutation against JVM-global/bootstrap classes rather than skipping cleanup.



-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11301] fix(catalog): guard ClassLoaderResourceCleanerUtils against shared-class static-field mutation [gravitino]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #11431:
URL: https://github.com/apache/gravitino/pull/11431#discussion_r3355964141


##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -87,23 +87,44 @@ private static void closeStatsDataClearerInFileSystem(ClassLoader targetClassLoa
       throws Exception {
     Class<?> fileSystemClass =
         Class.forName("org.apache.hadoop.fs.FileSystem", true, targetClassLoader);
+
+    // If FileSystem was resolved from a parent/AppClassLoader rather than the catalog's own
+    // classloader, its CACHE, Statistics cleaner, and MutableQuantiles scheduler are shared
+    // across all catalogs in the JVM. Operating on shared static state here would close every
+    // catalog's FileSystems and permanently terminate the global scheduler, breaking any
+    // subsequent catalog that uses Hadoop metrics. Skip cleanup for shared classes and let
+    // the JVM manage them.
+    if (!isOwnedByClassLoader(fileSystemClass, targetClassLoader)) {
+      LOG.debug(
+          "Hadoop FileSystem is owned by {}, not the target classloader {}; skipping shared-class cleanup",
+          fileSystemClass.getClassLoader(),
+          targetClassLoader);
+      return;
+    }

Review Comment:
   The new shared-class ownership guards in the Hadoop/AWS/GCP/Azure cleanup paths are not covered by tests. In unit tests, `closeClassLoaderResource` short-circuits when `GRAVITINO_TEST` is set, so the production-only behavior here can regress without detection. Please add focused tests (e.g., using a custom parent/child ClassLoader setup and invoking the private cleanup methods via reflection) to assert that static-state mutation is skipped when the resolved class is parent-loaded, and performed when the class is target-loader-owned.



-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11301] fix(catalog): guard ClassLoaderResourceCleanerUtils against shared-class static-field mutation [gravitino]

Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao merged PR #11431:
URL: https://github.com/apache/gravitino/pull/11431


-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11301] fix(catalog): guard ClassLoaderResourceCleanerUtils against shared-class static-field mutation [gravitino]

Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #11431:
URL: https://github.com/apache/gravitino/pull/11431#discussion_r3356166441


##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -87,23 +87,44 @@ private static void closeStatsDataClearerInFileSystem(ClassLoader targetClassLoa
       throws Exception {
     Class<?> fileSystemClass =
         Class.forName("org.apache.hadoop.fs.FileSystem", true, targetClassLoader);
+
+    // If FileSystem was resolved from a parent/AppClassLoader rather than the catalog's own
+    // classloader, its CACHE, Statistics cleaner, and MutableQuantiles scheduler are shared
+    // across all catalogs in the JVM. Operating on shared static state here would close every
+    // catalog's FileSystems and permanently terminate the global scheduler, breaking any
+    // subsequent catalog that uses Hadoop metrics. Skip cleanup for shared classes and let
+    // the JVM manage them.
+    if (!isOwnedByClassLoader(fileSystemClass, targetClassLoader)) {
+      LOG.debug(
+          "Hadoop FileSystem is owned by {}, not the target classloader {}; skipping shared-class cleanup",
+          fileSystemClass.getClassLoader(),
+          targetClassLoader);
+      return;
+    }

Review Comment:
   Fixed. Made `isOwnedByClassLoader` `@VisibleForTesting` package-private and added `TestClassLoaderResourceCleanerUtils` with three focused tests:
   
   1. Returns `true` when the class is loaded by exactly the target classloader (cleanup proceeds).
   2. Returns `false` when the class was delegated to the parent (child `URLClassLoader` scenario — cleanup skipped).
   3. Returns `false` for bootstrap-loaded classes (`String.class`, whose `getClassLoader()` returns `null`).
   
   Note: per project guidelines, reflection into private members from tests is avoided — the `@VisibleForTesting` package-private accessor is the preferred pattern here.



-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11301] fix(catalog): guard ClassLoaderResourceCleanerUtils against shared-class static-field mutation [gravitino]

Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #11431:
URL: https://github.com/apache/gravitino/pull/11431#discussion_r3360269682


##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -301,6 +345,16 @@ private static void closeResourceInAzure(ClassLoader classLoader) throws Excepti
     MethodUtils.invokeStaticMethod(relocatedLogFactory, "release", classLoader);
   }
 
+  /**
+   * Returns true if {@code clazz} was loaded directly by {@code classLoader} (not delegated to a
+   * parent). Use this before touching static fields that must belong to the catalog's own
+   * classloader to avoid accidentally mutating JVM-global shared state.
+   */
+  @VisibleForTesting
+  static boolean isOwnedByClassLoader(Class<?> clazz, ClassLoader classLoader) {
+    return clazz.getClassLoader() == classLoader;
+  }

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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org