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 2025/08/22 09:46:07 UTC
[PR] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
yuqi1129 opened a new pull request, #8252:
URL: https://github.com/apache/gravitino/pull/8252
### What changes were proposed in this pull request?
1. Clear thread locals and daemon thread for cusom class loader to let GC free it.
2. Fix tiemstamp can be 0 for azure file system.
### Why are the changes needed?
They are bugs.
Fix: #8251
Fix: #8249
### Does this PR introduce _any_ user-facing change?
N/A.
### How was this patch tested?
Test locally.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3227813810
> > > > > My biggest concern is the maintainability of the clear resource logic. It seems that we are doing some complex work that should belong to the JVM level's GC, and this workload will get out of control as the project develops.
> > > > > I don't know if we have other alternative solutions to solve the OOM problem, but the current solution seems not to be the optimal one in the long run. WDYT? @jerryshao @FANNG1
> > > >
> > > >
> > > > I agree. My thought is that though we may have lots of catalogs, the number of providers is limited, which means the number of classloaders are limited, we don't have to create a classloader for each catalog. We can reuse the classloader for the same type of catalogs, and with this we can reduce the class-related OOM issues.
> > > > Also, many engines like Spark/Trino that support multiple connectors will face the same problem, especially for Trino, which can also support dynamic connector register/unregister. How do they handle this problem? We'd better have a long-term solution.
> > > > For now, I think we can have a short solution, but we should clearly separate this from the main logic, so we can remove it easily. Also, we should add the comments as possible as we can.
> > >
> > >
> > > I see.
> > > I will try to introduce the class loader reuse mechanism in the next version, and possibly there will be static variable sharing problems like `UGI` exists, so conducting full tests is necessary before this changes.
> >
> >
> > In Trino, there will be only one classloader for each type of catalog.
>
> We can also have the same solution as I mentioned above. The problem we face is the same as Trino (they also have the same UGI issue). Please investigate it and have a long-term solution.
I will. Should I move this PR or focus on a new solution? I'm afraid we won't be able to finish it within the time frame of 1.0.0, as large number of tests are needed.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "FANNG1 (via GitHub)" <gi...@apache.org>.
FANNG1 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3226318204
@mchades any other comments?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "mchades (via GitHub)" <gi...@apache.org>.
mchades commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2312996442
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to clean up resources related to a specific class loader to prevent memory leaks.
+ * Gravitino will create a new class loader for each catalog and release it when there exist any
+ * changes to the catalog. So, it's important to clean up resources related to the class loader to
+ * prevent memory leaks.
+ */
+public class ClassLoaderResourceCleanerUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderResourceCleanerUtils.class);
+
+ private ClassLoaderResourceCleanerUtils() {}
+
+ /**
+ * Close all resources related to the given class loader to prevent memory leaks.
+ *
+ * @param classLoader the classloader to be closed
+ */
+ public static void closeClassLoaderResource(ClassLoader classLoader) {
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ // Clear statics threads in FileSystem and close all FileSystem instances.
+ closeStatsDataClearerInFileSystem(classLoader);
+
+ // Stop all threads with the current class loader and clear their threadLocal variables for
+ // jetty threads that are loaded by the current class loader.
+ // For example, thread local `threadData` in FileSystem#StatisticsDataCleaner is created
+ // within jetty thread with the current class loader. However, there are clear by
+ // `catalog.close` in ForkJoinPool in CaffeineCache, in this case, the thread local variable
+ // will not be cleared, so we need to clear them manually here.
+ stopThreadsAndClearThreadLocalVariables(classLoader);
+
+ // Release the LogFactory for the classloader, each classloader has its own LogFactory
+ // instance.
+ releaseLogFactoryInCommonLogging(classLoader);
+
+ closeResourceInAWS(classLoader);
+
+ closeResourceInGCP(classLoader);
+
+ closeResourceInAzure(classLoader);
+
+ clearShutdownHooks(classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(threads, thread local variables) in the class loader", e);
Review Comment:
So, why do you need to catch the exception again 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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3228101499
Polish done, please help to review it again.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3227428538
> > > My biggest concern is the maintainability of the clear resource logic. It seems that we are doing some complex work that should belong to the JVM level's GC, and this workload will get out of control as the project develops.
> > > I don't know if we have other alternative solutions to solve the OOM problem, but the current solution seems not to be the optimal one in the long run. WDYT? @jerryshao @FANNG1
> >
> >
> > I agree. My thought is that though we may have lots of catalogs, the number of providers is limited, which means the number of classloaders are limited, we don't have to create a classloader for each catalog. We can reuse the classloader for the same type of catalogs, and with this we can reduce the class-related OOM issues.
> > Also, many engines like Spark/Trino that support multiple connectors will face the same problem, especially for Trino, which can also support dynamic connector register/unregister. How do they handle this problem? We'd better have a long-term solution.
> > For now, I think we can have a short solution, but we should clearly separate this from the main logic, so we can remove it easily. Also, we should add the comments as possible as we can.
>
> I see.
>
> I will try to introduce the class loader reuse mechanism in the next version, and possibly there will be static variable sharing problems like `UGI` exists, so conducting full tests is necessary before this changes.
In Trino, there will be only one classloader for each type of catalog.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "mchades (via GitHub)" <gi...@apache.org>.
mchades commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2293600138
##########
common/src/main/java/org/apache/gravitino/dto/file/FileInfoDTO.java:
##########
@@ -162,7 +162,8 @@ public FileInfoDTO.FileInfoDTOBuilder path(String path) {
public FileInfoDTO build() {
Preconditions.checkArgument(StringUtils.isNotBlank(name), "name cannot be null or empty");
Preconditions.checkArgument(size >= 0, "size cannot be negative");
- Preconditions.checkArgument(lastModified > 0, "lastModified must be a valid timestamp");
+ // Azure Blob Storage uses a timestamp in milliseconds, it can be 0 for newly created files.
+ Preconditions.checkArgument(lastModified >= 0, "lastModified must be a valid timestamp");
Review Comment:
I think you can change it to:
``
// it can be 0 for newly created files in Azure Blob Storage
``
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3226508985
> > My biggest concern is the maintainability of the clear resource logic. It seems that we are doing some complex work that should belong to the JVM level's GC, and this workload will get out of control as the project develops.
> > I don't know if we have other alternative solutions to solve the OOM problem, but the current solution seems not to be the optimal one in the long run. WDYT? @jerryshao @FANNG1
>
> Yes, the current solution depends on the implementation logic of dependency jars. classloader reuse mentioned by @jerryshao is a more general way, but my concern is the time for 1.0 and may introduce some unexpected behavior before the release.
A cache mechanism can alleviate the problem, but it cannot fix it at its source. Since we are using Caffeine cache for the catalog manager, the class loader will be recreated if the cache expires.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2308944625
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderUtils.java:
##########
@@ -0,0 +1,314 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ClassLoaderUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderUtils.class);
+
+ private ClassLoaderUtils() {}
+
+ /**
+ * Close the stats data clearer thread in Hadoop FileSystem to prevent memory leaks when using
+ *
+ * @param targetClassLoader the classloader where Hadoop FileSystem is loaded
+ */
+ private static void closeStatsDataClearerInFileSystem(ClassLoader targetClassLoader) {
+ try {
+ Class<?> FileSystem =
+ Class.forName("org.apache.hadoop.fs.FileSystem", true, targetClassLoader);
+ MethodUtils.invokeStaticMethod(FileSystem, "closeAll");
+
+ Class<?> MutableQuantiles =
+ Class.forName("org.apache.hadoop.metrics2.lib.MutableQuantiles", true, targetClassLoader);
+ Class<?> statisticsClass =
+ Class.forName("org.apache.hadoop.fs.FileSystem$Statistics", true, targetClassLoader);
+
+ ScheduledExecutorService scheduler =
+ (ScheduledExecutorService)
+ FieldUtils.readStaticField(MutableQuantiles, "scheduler", true);
+ scheduler.shutdownNow();
+ Field statisticsCleanerField =
+ FieldUtils.getField(statisticsClass, "STATS_DATA_CLEANER", true);
+ Object statisticsCleaner = statisticsCleanerField.get(null);
+ if (statisticsCleaner != null) {
+ ((Thread) statisticsCleaner).interrupt();
+ ((Thread) statisticsCleaner).setContextClassLoader(null);
+ ((Thread) statisticsCleaner).join();
+ }
+ } catch (Exception e) {
+ LOG.warn(
+ "Failed to close FileSystem and MutableQuantiles statistics cleaner in {}",
+ targetClassLoader,
+ e);
+ }
+ }
+
+ /**
+ * Stop all threads that are using the target class loader and clear thread local variables to
+ * prevent memory leaks.
+ *
+ * <pre>
+ * This method aims to:
+ * 1. Stop all threads that are using the target class loader.
+ * 2. Clear thread local variables in all threads that are using the target class loader. some thread
+ * local variables are loaded in thread jetty-webserver-* threads, which are long-lived threads and
+ * will not be stopped when the catalog is closed.
+ * </pre>
+ */
+ private static void stopThreadsAndClearThreadLocalVariables(ClassLoader classLoader) {
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // First clear thread local variables
+ clearThreadLocalMap(thread, classLoader);
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithClassLoader(thread, classLoader)) {
+ LOG.info("Interrupting thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(500);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join thread: {}", thread.getName(), e);
+ }
+ }
+ }
+ }
+
+ private static boolean runningWithClassLoader(Thread thread, ClassLoader targetClassLoader) {
+ return thread != null && thread.getContextClassLoader() == targetClassLoader;
+ }
+
+ private static Thread[] getAllThreads() {
+ ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
+ ThreadGroup parentGroup;
+ while ((parentGroup = rootGroup.getParent()) != null) {
+ rootGroup = parentGroup;
+ }
+
+ Thread[] threads = new Thread[rootGroup.activeCount()];
+ while (rootGroup.enumerate(threads, true) == threads.length) {
+ threads = new Thread[threads.length * 2];
+ }
+ return threads;
+ }
+
+ private static void clearThreadLocalMap(Thread thread, ClassLoader targetClassLoader) {
+ if (thread == null || !thread.getName().startsWith("Gravitino-webserver-")) {
+ return;
+ }
+
+ try {
+ Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
+ threadLocalsField.setAccessible(true);
+ Object threadLocalMap = threadLocalsField.get(thread);
+
+ if (threadLocalMap != null) {
+ Class<?> tlmClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
+ Field tableField = tlmClass.getDeclaredField("table");
+ tableField.setAccessible(true);
+ Object[] table = (Object[]) tableField.get(threadLocalMap);
+
+ for (Object entry : table) {
+ if (entry != null) {
+ Object value = FieldUtils.readField(entry, "value", true);
+ if (value != null
+ && value.getClass().getClassLoader() != null
+ && value.getClass().getClassLoader() == targetClassLoader) {
+ LOG.info(
+ "Cleaning up thread local {} for thread {} with custom class loader",
+ value,
+ thread.getName());
+ FieldUtils.writeField(entry, "value", null, true);
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ LOG.warn("Failed to clean up thread locals for thread {}", thread.getName(), e);
+ }
+ }
+
+ /**
+ * Clear shutdown hooks registered by the target class loader to prevent memory leaks.
+ *
+ * <p>All shutdown hooks are run with the system class loader, so we need to manually clear the
+ * shutdown hooks registered by the target class loader.
+ *
+ * @param targetClassLoader the classloader where the shutdown hooks are registered.
+ */
+ private static void clearShutdownHooks(ClassLoader targetClassLoader) {
+ try {
+ Class<?> shutdownHooks = Class.forName("java.lang.ApplicationShutdownHooks");
+ IdentityHashMap<Thread, Thread> hooks =
+ (IdentityHashMap<Thread, Thread>)
+ FieldUtils.readStaticField(shutdownHooks, "hooks", true);
+
+ hooks
+ .entrySet()
+ .removeIf(
+ entry -> {
+ Thread thread = entry.getKey();
+ return thread.getContextClassLoader() == targetClassLoader;
+ });
+ } catch (Exception e) {
+ LOG.warn("Failed to clean shutdown hooks", e);
+ }
+ }
+
+ /**
+ * Release the LogFactory for the target class loader to prevent memory leaks.
+ *
+ * @param currentClassLoader the classloader where the commons-logging is loaded.
+ */
+ private static void releaseLogFactoryInCommonLogging(ClassLoader currentClassLoader) {
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ try {
+ Class<?> logFactoryClass =
+ Class.forName("org.apache.commons.logging.LogFactory", true, currentClassLoader);
+ MethodUtils.invokeStaticMethod(logFactoryClass, "release", currentClassLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to release LogFactory for class loader {}", currentClassLoader, e);
+ }
+ }
+
+ /**
+ * Close the AWS SDK metrics MBean to prevent memory leaks when using AWS S3.
+ *
+ * @param classLoader the classloader where AWS SDK is loaded
+ */
+ private static void closeResourceInAWS(ClassLoader classLoader) {
+ // For Aws SDK metrics, unregister the metric admin MBean
+ try {
+ Class<?> methodUtilsClass =
+ Class.forName("com.amazonaws.metrics.AwsSdkMetrics", true, classLoader);
+ MethodUtils.invokeStaticMethod(methodUtilsClass, "unregisterMetricAdminMBean");
+ } catch (Exception e) {
+ LOG.warn(
+ "Failed to unregister AWS SDK metrics admin MBean from class loader {}", classLoader, e);
+ }
+ }
+
+ private static void closeResourceInGCP(ClassLoader classLoader) {
+ // For GCS
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName(
+ "org.apache.gravitino.gcp.shaded.org.apache.commons.logging.LogFactory",
+ true,
+ classLoader);
+ MethodUtils.invokeStaticMethod(relocatedLogFactory, "release", classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory in class loader {}", classLoader, e);
+ }
+ }
+
+ /**
+ * Close the timer in AbfsClientThrottlingAnalyzer to prevent memory leaks when using Azure Blob
+ * File System.
+ *
+ * <p>Timer is a daemon thread, so it won't prevent the JVM from shutting down, but it will
+ * prevent the class loader from being garbage collected.
+ *
+ * @param classLoader the classloader where Azure Blob File System is loaded
+ */
+ private static void closeResourceInAzure(ClassLoader classLoader) {
+ try {
+ // Clear timer in AbfsClientThrottlingAnalyzer
+ Class<?> abfsClientThrottlingInterceptClass =
+ Class.forName(
+ "org.apache.hadoop.fs.azurebfs.services.AbfsClientThrottlingIntercept",
+ true,
+ classLoader);
+ Object abfsClientThrottlingIntercept =
+ FieldUtils.readStaticField(abfsClientThrottlingInterceptClass, "singleton", true);
+
+ Object readThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "readThrottler", true);
+ Object writeThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "writeThrottler", true);
+
+ Timer readTimer = (Timer) FieldUtils.readField(readThrottler, "timer", true);
+ readTimer.cancel();
+ Timer writeTimer = (Timer) FieldUtils.readField(writeThrottler, "timer", true);
+ writeTimer.cancel();
+
+ // Release the LogFactory for the Azure shaded commons logging which has been relocated
+ // by the Azure SDK
+ Class<?> relocatedLogFactory =
+ Class.forName(
+ "org.apache.gravitino.azure.shaded.org.apache.commons.logging.LogFactory",
+ true,
+ classLoader);
+ MethodUtils.invokeStaticMethod(relocatedLogFactory, "release", classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to handle Azure file system in class loader {}", classLoader, e);
+ }
+ }
+
+ /**
+ * Close all resources related to the given class loader to prevent memory leaks.
+ *
+ * @param classLoader the classloader to be closed
+ */
+ public static void closeClassLoaderResource(ClassLoader classLoader) {
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ // Clear statics threads in FileSystem and close all FileSystem instances.
+ closeStatsDataClearerInFileSystem(classLoader);
+
+ // Stop all threads with the current class loader and clear their threadLocal variables for
+ // jetty threads that are loaded by the current class loader.
+ // For example, thread local `threadData` in FileSystem#StatisticsDataCleaner is created
+ // within jetty thread with the current class loader. However, there are clear by
+ // `catalog.close` in ForkJoinPool in CaffeineCache, in this case, the thread local variable
+ // will not be cleared, so we need to clear them manually here.
+ stopThreadsAndClearThreadLocalVariables(classLoader);
+
+ // Release the LogFactory for the classloader, each classloader has its own LogFactory
+ // instance.
+ releaseLogFactoryInCommonLogging(classLoader);
+
+ closeResourceInAWS(classLoader);
+
+ closeResourceInGCP(classLoader);
+
+ closeResourceInAzure(classLoader);
+
+ clearShutdownHooks(classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(Thread, ThreadLocal variants) in the class loader", e);
Review Comment:
"variants" or ""variables"?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2310021209
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderUtils.java:
##########
@@ -0,0 +1,314 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ClassLoaderUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderUtils.class);
+
+ private ClassLoaderUtils() {}
+
+ /**
+ * Close the stats data clearer thread in Hadoop FileSystem to prevent memory leaks when using
+ *
+ * @param targetClassLoader the classloader where Hadoop FileSystem is loaded
+ */
+ private static void closeStatsDataClearerInFileSystem(ClassLoader targetClassLoader) {
+ try {
+ Class<?> FileSystem =
Review Comment:
Seems this is not changed.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "mchades (via GitHub)" <gi...@apache.org>.
mchades commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2293566121
##########
common/src/main/java/org/apache/gravitino/dto/file/FileInfoDTO.java:
##########
@@ -162,7 +162,8 @@ public FileInfoDTO.FileInfoDTOBuilder path(String path) {
public FileInfoDTO build() {
Preconditions.checkArgument(StringUtils.isNotBlank(name), "name cannot be null or empty");
Preconditions.checkArgument(size >= 0, "size cannot be negative");
- Preconditions.checkArgument(lastModified > 0, "lastModified must be a valid timestamp");
+ // Azure Blob Storage uses a timestamp in milliseconds, it can be 0 for newly created files.
+ Preconditions.checkArgument(lastModified >= 0, "lastModified must be a valid timestamp");
Review Comment:
I don't get the relationship between `a timestamp in milliseconds` and `0 for newly created files`
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2293581813
##########
common/src/main/java/org/apache/gravitino/dto/file/FileInfoDTO.java:
##########
@@ -162,7 +162,8 @@ public FileInfoDTO.FileInfoDTOBuilder path(String path) {
public FileInfoDTO build() {
Preconditions.checkArgument(StringUtils.isNotBlank(name), "name cannot be null or empty");
Preconditions.checkArgument(size >= 0, "size cannot be negative");
- Preconditions.checkArgument(lastModified > 0, "lastModified must be a valid timestamp");
+ // Azure Blob Storage uses a timestamp in milliseconds, it can be 0 for newly created files.
+ Preconditions.checkArgument(lastModified >= 0, "lastModified must be a valid timestamp");
Review Comment:
All in all, it can be 0.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2293720531
##########
bundles/azure-bundle/build.gradle.kts:
##########
@@ -25,7 +25,10 @@ plugins {
}
dependencies {
- implementation(project(":bundles:azure"))
+ implementation(project(":bundles:azure")) {
+ exclude(group = "commons-logging", module = "commons-logging")
Review Comment:
There is already `commons.logging` in `hadoop-azure` for bundle, so it is no need to add it.
##########
common/src/main/java/org/apache/gravitino/dto/file/FileInfoDTO.java:
##########
@@ -162,7 +162,8 @@ public FileInfoDTO.FileInfoDTOBuilder path(String path) {
public FileInfoDTO build() {
Preconditions.checkArgument(StringUtils.isNotBlank(name), "name cannot be null or empty");
Preconditions.checkArgument(size >= 0, "size cannot be negative");
- Preconditions.checkArgument(lastModified > 0, "lastModified must be a valid timestamp");
+ // Azure Blob Storage uses a timestamp in milliseconds, it can be 0 for newly created files.
+ Preconditions.checkArgument(lastModified >= 0, "lastModified must be a valid timestamp");
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: commits-unsubscribe@gravitino.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2313017008
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to clean up resources related to a specific class loader to prevent memory leaks.
+ * Gravitino will create a new class loader for each catalog and release it when there exist any
+ * changes to the catalog. So, it's important to clean up resources related to the class loader to
+ * prevent memory leaks.
+ */
+public class ClassLoaderResourceCleanerUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderResourceCleanerUtils.class);
+
+ private ClassLoaderResourceCleanerUtils() {}
+
+ /**
+ * Close all resources related to the given class loader to prevent memory leaks.
+ *
+ * @param classLoader the classloader to be closed
+ */
+ public static void closeClassLoaderResource(ClassLoader classLoader) {
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ // Clear statics threads in FileSystem and close all FileSystem instances.
+ closeStatsDataClearerInFileSystem(classLoader);
+
+ // Stop all threads with the current class loader and clear their threadLocal variables for
+ // jetty threads that are loaded by the current class loader.
+ // For example, thread local `threadData` in FileSystem#StatisticsDataCleaner is created
+ // within jetty thread with the current class loader. However, there are clear by
+ // `catalog.close` in ForkJoinPool in CaffeineCache, in this case, the thread local variable
+ // will not be cleared, so we need to clear them manually here.
+ stopThreadsAndClearThreadLocalVariables(classLoader);
+
+ // Release the LogFactory for the classloader, each classloader has its own LogFactory
+ // instance.
+ releaseLogFactoryInCommonLogging(classLoader);
+
+ closeResourceInAWS(classLoader);
+
+ closeResourceInGCP(classLoader);
+
+ closeResourceInAzure(classLoader);
+
+ clearShutdownHooks(classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(threads, thread local variables) in the class loader", e);
Review Comment:
It's okay to remove the `try/catch` block, and it's just the preventive measures.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "FANNG1 (via GitHub)" <gi...@apache.org>.
FANNG1 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2297253772
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,182 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
Review Comment:
How about keep the cleanup process in TEST if there are no bad effects, we could check whether there are exceptions in cleanup in test.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "FANNG1 (via GitHub)" <gi...@apache.org>.
FANNG1 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2300103904
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,182 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
Review Comment:
could you add the comment to the above code?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3226601074
> Quote reply
I have checked that the reused class loader has a problem with static values like `UserGroupInformation.loginUser`, we need to check and test, and then make corresponding changes.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2300716465
##########
core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java:
##########
Review Comment:
If the comment I posted wasn't clear enough, I can't provide any evidence that is more persuasive. it can be summarized as 'Catalog related thread locals are initialized in jetty thread pool and then closed with Caffeine thread pool'
```text
// Clear thread local map for webserver threads in the current class loader. Why do we need
// this? Because the webserver threads are long-lived threads and will holds may thread
// local references to the current class loader. However, they can't be cleared as
// `Catalog.close` is called in the thread pool located in the Caffeine cache, which is not
// in the webserver threads. So we need to manually clear the thread local map for webserver
// threads.
```
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2312980756
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to clean up resources related to a specific class loader to prevent memory leaks.
+ * Gravitino will create a new class loader for each catalog and release it when there exist any
+ * changes to the catalog. So, it's important to clean up resources related to the class loader to
+ * prevent memory leaks.
+ */
+public class ClassLoaderResourceCleanerUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderResourceCleanerUtils.class);
+
+ private ClassLoaderResourceCleanerUtils() {}
+
+ /**
+ * Close all resources related to the given class loader to prevent memory leaks.
+ *
+ * @param classLoader the classloader to be closed
+ */
+ public static void closeClassLoaderResource(ClassLoader classLoader) {
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ // Clear statics threads in FileSystem and close all FileSystem instances.
+ closeStatsDataClearerInFileSystem(classLoader);
+
+ // Stop all threads with the current class loader and clear their threadLocal variables for
+ // jetty threads that are loaded by the current class loader.
+ // For example, thread local `threadData` in FileSystem#StatisticsDataCleaner is created
+ // within jetty thread with the current class loader. However, there are clear by
+ // `catalog.close` in ForkJoinPool in CaffeineCache, in this case, the thread local variable
+ // will not be cleared, so we need to clear them manually here.
+ stopThreadsAndClearThreadLocalVariables(classLoader);
+
+ // Release the LogFactory for the classloader, each classloader has its own LogFactory
+ // instance.
+ releaseLogFactoryInCommonLogging(classLoader);
+
+ closeResourceInAWS(classLoader);
+
+ closeResourceInGCP(classLoader);
+
+ closeResourceInAzure(classLoader);
+
+ clearShutdownHooks(classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(threads, thread local variables) in the class loader", e);
Review Comment:
No, I have caught any possible exceptions in `closeResourceInAWS `.
```java
private static void closeResourceInAWS(ClassLoader classLoader) {
// For Aws SDK metrics, unregister the metric admin MBean
try {
Class<?> awsSdkMetricsClass =
Class.forName("com.amazonaws.metrics.AwsSdkMetrics", true, classLoader);
MethodUtils.invokeStaticMethod(awsSdkMetricsClass, "unregisterMetricAdminMBean");
} catch (Exception e) {
LOG.warn(
"Failed to unregister AWS SDK metrics admin MBean from class loader {}", classLoader, e);
}
}
```
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2297340786
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,182 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
Review Comment:
No, it will lead problem if the logic is absent, as there is only one class loader in the test environment.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2303081694
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,211 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ closeStatsDataClearerInFileSystem();
+
+ stopThreadsAndClearThreadLocal();
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ unregisterLogFactory();
+
+ closeResourceInAWS();
+
+ closeResourceInGCP();
+
+ closeResourceInAzure();
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(Thread, ThreadLocal variants) in the class loader", e);
+ }
+ }
+
+ private static void stopThreadsAndClearThreadLocal() {
Review Comment:
"ThreadLocalVariables"
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2308930039
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderUtils.java:
##########
@@ -0,0 +1,314 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ClassLoaderUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderUtils.class);
+
+ private ClassLoaderUtils() {}
+
+ /**
+ * Close the stats data clearer thread in Hadoop FileSystem to prevent memory leaks when using
+ *
+ * @param targetClassLoader the classloader where Hadoop FileSystem is loaded
+ */
+ private static void closeStatsDataClearerInFileSystem(ClassLoader targetClassLoader) {
+ try {
+ Class<?> FileSystem =
Review Comment:
Can you please rename all the class variables to `xxxClass`?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "FANNG1 (via GitHub)" <gi...@apache.org>.
FANNG1 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2300113628
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,182 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+
+ closeStatsDataClearerInFileSystem();
+
+ FileSystem.closeAll();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting peer cache thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join peer cache thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // For Aws SDK metrics, unregister the metric admin MBean
+ try {
Review Comment:
Could you do some abstraction like `cleanupAwsResource`?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "FANNG1 (via GitHub)" <gi...@apache.org>.
FANNG1 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2300376417
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,208 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ closeStatsDataClearerInFileSystem();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader. Why do we need
+ // this? Because the webserver threads are long-lived threads and will holds may thread
+ // local
+ // references to the current class loader. However, they can't be cleared as `Catalog.close`
+ // is called in the thread pool located in the Caffeine cache, which is not in the webserver
+ // threads. So we need to manually clear the thread local map for webserver threads.
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ unregisterLogFactory();
+
+ closeResourceInAWS();
+
+ closeResourceInGCP();
+
+ closeResourceInAzure();
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear FileSystem statistics cleaner thread", e);
Review Comment:
Is the log content correct?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "FANNG1 (via GitHub)" <gi...@apache.org>.
FANNG1 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2300364083
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,208 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ closeStatsDataClearerInFileSystem();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader. Why do we need
+ // this? Because the webserver threads are long-lived threads and will holds may thread
+ // local
+ // references to the current class loader. However, they can't be cleared as `Catalog.close`
+ // is called in the thread pool located in the Caffeine cache, which is not in the webserver
+ // threads. So we need to manually clear the thread local map for webserver threads.
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ unregisterLogFactory();
+
+ closeResourceInAWS();
+
+ closeResourceInGCP();
+
+ closeResourceInAzure();
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear FileSystem statistics cleaner thread", e);
+ }
+ }
+
+ private void unregisterLogFactory() {
+ try {
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister LogFactory", e);
+ }
+ }
+
+ private static void closeResourceInAzure() {
+ // For Azure
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName("org.apache.gravitino.azure.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // Clear timer in AbfsClientThrottlingAnalyzer
+ Class<?> abfsClientThrottlingInterceptClass =
+ Class.forName("org.apache.hadoop.fs.azurebfs.services.AbfsClientThrottlingIntercept");
+ Object abfsClientThrottlingIntercept =
+ FieldUtils.readStaticField(abfsClientThrottlingInterceptClass, "singleton", true);
+
+ Object readThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "readThrottler", true);
+ Object writeThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "writeThrottler", true);
+
+ Timer readTimer = (Timer) FieldUtils.readField(readThrottler, "timer", true);
+ readTimer.cancel();
+ Timer writeTimer = (Timer) FieldUtils.readField(writeThrottler, "timer", true);
+ writeTimer.cancel();
+ } catch (Exception e) {
+ LOG.warn("Failed to handle Azure file system...", e);
+ }
+ }
+
+ private static void closeResourceInGCP() {
+ // For GCS
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName("org.apache.gravitino.gcp.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory", e);
+ }
+ }
+
+ private static void closeResourceInAWS() {
+ // For Aws SDK metrics, unregister the metric admin MBean
+ try {
+ Class<?> methodUtilsClass = Class.forName("com.amazonaws.metrics.AwsSdkMetrics");
+ MethodUtils.invokeStaticMethod(methodUtilsClass, "unregisterMetricAdminMBean");
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister AWS SDK metrics admin MBean", e);
+ // This is not critical, so we just log the warning
+ }
+ }
+
+ private static void clearThreadLocalMap(Thread thread) {
+ if (thread != null && thread.getName().startsWith("Gravitino-webserver-")) {
+ // Try to
Review Comment:
Try to?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "FANNG1 (via GitHub)" <gi...@apache.org>.
FANNG1 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3226501330
> My biggest concern is the maintainability of the clear resource logic. It seems that we are doing some complex work that should belong to the JVM level's GC, and this workload will get out of control as the project develops.
>
> I don't know if we have other alternative solutions to solve the OOM problem, but the current solution seems not to be the optimal one in the long run. WDYT? @jerryshao @FANNG1
Yes, the current solution depends on the implementation logic of dependency jars, is more general way, maybe classloader reuse mentioned by @jerryshao , but my concern is the time for 1.0 and may introduce some unexpected behavior.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2308924520
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderUtils.java:
##########
@@ -0,0 +1,314 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ClassLoaderUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderUtils.class);
+
+ private ClassLoaderUtils() {}
+
+ /**
Review Comment:
Please move the public class to the top.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2297250228
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,182 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+
+ closeStatsDataClearerInFileSystem();
+
+ FileSystem.closeAll();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting peer cache thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join peer cache thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
Review Comment:
Yes, the Paimon and Iceberg catalog share the same problem, and the cases are more complicated, I'm workong on it.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2293585595
##########
common/src/main/java/org/apache/gravitino/dto/file/FileInfoDTO.java:
##########
@@ -162,7 +162,8 @@ public FileInfoDTO.FileInfoDTOBuilder path(String path) {
public FileInfoDTO build() {
Preconditions.checkArgument(StringUtils.isNotBlank(name), "name cannot be null or empty");
Preconditions.checkArgument(size >= 0, "size cannot be negative");
- Preconditions.checkArgument(lastModified > 0, "lastModified must be a valid timestamp");
+ // Azure Blob Storage uses a timestamp in milliseconds, it can be 0 for newly created files.
+ Preconditions.checkArgument(lastModified >= 0, "lastModified must be a valid timestamp");
Review Comment:
Is the unit here not 'timestamp'?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "mchades (via GitHub)" <gi...@apache.org>.
mchades commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2313041494
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to clean up resources related to a specific class loader to prevent memory leaks.
+ * Gravitino will create a new class loader for each catalog and release it when there exist any
+ * changes to the catalog. So, it's important to clean up resources related to the class loader to
+ * prevent memory leaks.
+ */
+public class ClassLoaderResourceCleanerUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderResourceCleanerUtils.class);
+
+ private ClassLoaderResourceCleanerUtils() {}
+
+ /**
+ * Close all resources related to the given class loader to prevent memory leaks.
+ *
+ * @param classLoader the classloader to be closed
+ */
+ public static void closeClassLoaderResource(ClassLoader classLoader) {
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ // Clear statics threads in FileSystem and close all FileSystem instances.
+ closeStatsDataClearerInFileSystem(classLoader);
+
+ // Stop all threads with the current class loader and clear their threadLocal variables for
+ // jetty threads that are loaded by the current class loader.
+ // For example, thread local `threadData` in FileSystem#StatisticsDataCleaner is created
+ // within jetty thread with the current class loader. However, there are clear by
+ // `catalog.close` in ForkJoinPool in CaffeineCache, in this case, the thread local variable
+ // will not be cleared, so we need to clear them manually here.
+ stopThreadsAndClearThreadLocalVariables(classLoader);
+
+ // Release the LogFactory for the classloader, each classloader has its own LogFactory
+ // instance.
+ releaseLogFactoryInCommonLogging(classLoader);
+
+ closeResourceInAWS(classLoader);
+
+ closeResourceInGCP(classLoader);
+
+ closeResourceInAzure(classLoader);
+
+ clearShutdownHooks(classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(threads, thread local variables) in the class loader", e);
Review Comment:
we can use a function to call the resource cleaner functions and catch exceptions
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2308940480
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderUtils.java:
##########
@@ -0,0 +1,314 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ClassLoaderUtils {
Review Comment:
This class name cannot directly reflect the purpose, can you figure out a better name? Also, add the comments to this class.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "mchades (via GitHub)" <gi...@apache.org>.
mchades commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2293557985
##########
bundles/azure-bundle/build.gradle.kts:
##########
@@ -25,7 +25,10 @@ plugins {
}
dependencies {
- implementation(project(":bundles:azure"))
+ implementation(project(":bundles:azure")) {
+ exclude(group = "commons-logging", module = "commons-logging")
Review Comment:
Why exclude this? You left a comment saying it is needed for runtime.
https://github.com/apache/gravitino/blob/728ea2e702177cd0dc456ba5e525795d14e67f5d/bundles/gcp/build.gradle.kts#L44-L45
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "mchades (via GitHub)" <gi...@apache.org>.
mchades commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2300471396
##########
core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java:
##########
Review Comment:
Can you post some evidence that proves these thread locals caused OOM? I think this will also help with troubleshooting when OOM occurs again. It also helps reviewers to check this PR locally.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "mchades (via GitHub)" <gi...@apache.org>.
mchades commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2302626226
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,211 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
Review Comment:
The comment should explain "why", and the real reason is that you mentioned in the previous comment that "there is only one class loader in the test environment."
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,211 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ closeStatsDataClearerInFileSystem();
+
+ stopThreadsAndClearThreadLocal();
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ unregisterLogFactory();
+
+ closeResourceInAWS();
+
+ closeResourceInGCP();
+
+ closeResourceInAzure();
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(Thread, ThreadLocal variants) in the class loader", e);
+ }
+ }
+
+ private static void stopThreadsAndClearThreadLocal() {
+ // Clear all thread references to the ClosableHiveCatalog class loader and clear thread local
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader. Why do we need
+ // this? Because the webserver threads are long-lived threads and will holds may thread
+ // local references to the current class loader. However, they can't be cleared as
+ // `Catalog.close` is called in the thread pool located in the Caffeine cache, which is not
+ // in the webserver threads. So we need to manually clear the thread local map for webserver
+ // threads.
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
Review Comment:
Does this conflict with the `Thread.sleep(500);` in the CatalogManager?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3226505245
> My biggest concern is the maintainability of the clear resource logic. It seems that we are doing some complex work that should belong to the JVM level's GC, and this workload will get out of control as the project develops.
>
> I don't know if we have other alternative solutions to solve the OOM problem, but the current solution seems not to be the optimal one in the long run. WDYT? @jerryshao @FANNG1
I admit that the current solution is an elegant one and hard to extend, but there is no better one available for me to fix the problem. If you have any good idea, feel free to tell me.
The biggest problem is that Gravitino provides a pluggable way to support multiple features, such as fileset, we can't guarantee that if there are similar problems in the jars provided by users, another points we can also introduce new jars as the project develops. No one can guarantee that the same problem will be triggered again.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "FANNG1 (via GitHub)" <gi...@apache.org>.
FANNG1 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2297245929
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,182 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+
+ closeStatsDataClearerInFileSystem();
+
+ FileSystem.closeAll();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting peer cache thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join peer cache thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
Review Comment:
Does other catalog has similar problems for LogFactory?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2300107062
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,182 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
Review Comment:
I see.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2303078600
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,211 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ closeStatsDataClearerInFileSystem();
+
+ stopThreadsAndClearThreadLocal();
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ unregisterLogFactory();
+
+ closeResourceInAWS();
+
+ closeResourceInGCP();
+
+ closeResourceInAzure();
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(Thread, ThreadLocal variants) in the class loader", e);
+ }
+ }
+
+ private static void stopThreadsAndClearThreadLocal() {
+ // Clear all thread references to the ClosableHiveCatalog class loader and clear thread local
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader. Why do we need
+ // this? Because the webserver threads are long-lived threads and will holds may thread
+ // local references to the current class loader. However, they can't be cleared as
+ // `Catalog.close` is called in the thread pool located in the Caffeine cache, which is not
+ // in the webserver threads. So we need to manually clear the thread local map for webserver
+ // threads.
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join thread: {}", thread.getName(), e);
+ }
+ }
+ }
+ }
+
+ private void unregisterLogFactory() {
+ try {
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister LogFactory", e);
+ }
+ }
+
+ private static void closeResourceInAzure() {
+ // For Azure
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName("org.apache.gravitino.azure.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // Clear timer in AbfsClientThrottlingAnalyzer
+ Class<?> abfsClientThrottlingInterceptClass =
+ Class.forName("org.apache.hadoop.fs.azurebfs.services.AbfsClientThrottlingIntercept");
+ Object abfsClientThrottlingIntercept =
+ FieldUtils.readStaticField(abfsClientThrottlingInterceptClass, "singleton", true);
+
+ Object readThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "readThrottler", true);
+ Object writeThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "writeThrottler", true);
+
+ Timer readTimer = (Timer) FieldUtils.readField(readThrottler, "timer", true);
+ readTimer.cancel();
+ Timer writeTimer = (Timer) FieldUtils.readField(writeThrottler, "timer", true);
+ writeTimer.cancel();
+ } catch (Exception e) {
+ LOG.warn("Failed to handle Azure file system...", e);
+ }
+ }
+
+ private static void closeResourceInGCP() {
+ // For GCS
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName("org.apache.gravitino.gcp.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory", e);
+ }
+ }
+
+ private static void closeResourceInAWS() {
+ // For Aws SDK metrics, unregister the metric admin MBean
+ try {
+ Class<?> methodUtilsClass = Class.forName("com.amazonaws.metrics.AwsSdkMetrics");
+ MethodUtils.invokeStaticMethod(methodUtilsClass, "unregisterMetricAdminMBean");
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister AWS SDK metrics admin MBean", e);
+ // This is not critical, so we just log the warning
+ }
+ }
+
+ private static void clearThreadLocalMap(Thread thread) {
+ if (thread != null && thread.getName().startsWith("Gravitino-webserver-")) {
+ try {
+ Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
+ threadLocalsField.setAccessible(true);
+ Object threadLocalMap = threadLocalsField.get(thread);
+
+ if (threadLocalMap != null) {
+ Class<?> tlmClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
+ Field tableField = tlmClass.getDeclaredField("table");
+ tableField.setAccessible(true);
+ Object[] table = (Object[]) tableField.get(threadLocalMap);
+
+ for (Object entry : table) {
+ if (entry != null) {
+ Object value = FieldUtils.readField(entry, "value", true);
+ if (value != null
+ && value.getClass().getClassLoader() != null
+ && value.getClass().getClassLoader()
+ == SecureFilesetCatalogOperations.class.getClassLoader()) {
+ LOG.info(
+ "Cleaning up thread local {} for thread {} with custom class loader",
+ value,
+ thread.getName());
+ FieldUtils.writeField(entry, "value", null, true);
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ LOG.warn("Failed to clean up thread locals for thread {}", thread.getName(), e);
+ }
+ }
+ }
+
+ private static void closeStatsDataClearerInFileSystem() {
+ try {
+ ScheduledExecutorService scheduler =
+ (ScheduledExecutorService)
+ FieldUtils.readStaticField(MutableQuantiles.class, "scheduler", true);
+ scheduler.shutdownNow();
+ Field statisticsCleanerField =
+ FieldUtils.getField(FileSystem.Statistics.class, "STATS_DATA_CLEANER", true);
+ Object statisticsCleaner = statisticsCleanerField.get(null);
+ if (statisticsCleaner != null) {
+ ((Thread) statisticsCleaner).interrupt();
+ ((Thread) statisticsCleaner).setContextClassLoader(null);
+ ((Thread) statisticsCleaner).join();
+ }
+
+ FileSystem.closeAll();
+ } catch (Exception e) {
+ LOG.warn("Failed to close stats data clearer in FileSystem", e);
+ }
+ }
+
+ private static Thread[] getAllThreads() {
+ ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
+ ThreadGroup parentGroup;
+ while ((parentGroup = rootGroup.getParent()) != null) {
+ rootGroup = parentGroup;
+ }
+
+ Thread[] threads = new Thread[rootGroup.activeCount()];
+ while (rootGroup.enumerate(threads, true) == threads.length) {
+ threads = new Thread[threads.length * 2];
+ }
+ return threads;
+ }
+
+ private static boolean runningWithCurrentClassLoader(Thread thread) {
+ return thread != null
+ && thread.getContextClassLoader() == FilesetCatalogOperations.class.getClassLoader();
+ }
+
+ public static void clearShutdownHooks() {
+ try {
+ Class<?> shutdownHooks = Class.forName("java.lang.ApplicationShutdownHooks");
+ IdentityHashMap<Thread, Thread> hooks =
+ (IdentityHashMap<Thread, Thread>)
+ FieldUtils.readStaticField(shutdownHooks, "hooks", true);
+
+ hooks
+ .entrySet()
+ .removeIf(
+ entry -> {
+ Thread thread = entry.getKey();
+ return thread.getContextClassLoader()
+ == FilesetCatalogOperations.class.getClassLoader();
+ });
+ } catch (Exception e) {
+ LOG.warn("Failed to clean shutdown hooks", e);
+ }
Review Comment:
The code above is very very brittle. You should separate this bunch of code from the main logic, and use another util class to encapsulate all the logic. That util class can be removed as a whole once we figure out a better way.
Another thing is that you should add lots of comments to describe each line of reflection, otherwise, others cannot understand why you made such a change.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2303269160
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,211 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ closeStatsDataClearerInFileSystem();
+
+ stopThreadsAndClearThreadLocal();
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ unregisterLogFactory();
+
+ closeResourceInAWS();
+
+ closeResourceInGCP();
+
+ closeResourceInAzure();
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(Thread, ThreadLocal variants) in the class loader", e);
+ }
+ }
+
+ private static void stopThreadsAndClearThreadLocal() {
+ // Clear all thread references to the ClosableHiveCatalog class loader and clear thread local
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader. Why do we need
+ // this? Because the webserver threads are long-lived threads and will holds may thread
+ // local references to the current class loader. However, they can't be cleared as
+ // `Catalog.close` is called in the thread pool located in the Caffeine cache, which is not
+ // in the webserver threads. So we need to manually clear the thread local map for webserver
+ // threads.
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join thread: {}", thread.getName(), e);
+ }
+ }
+ }
+ }
+
+ private void unregisterLogFactory() {
+ try {
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister LogFactory", e);
+ }
+ }
+
+ private static void closeResourceInAzure() {
Review Comment:
Module `Azure` already depends on `catalog-fileset`, it will lead to circular dependencies if moved there.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2303304832
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,211 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ closeStatsDataClearerInFileSystem();
+
+ stopThreadsAndClearThreadLocal();
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ unregisterLogFactory();
+
+ closeResourceInAWS();
+
+ closeResourceInGCP();
+
+ closeResourceInAzure();
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(Thread, ThreadLocal variants) in the class loader", e);
+ }
+ }
+
+ private static void stopThreadsAndClearThreadLocal() {
+ // Clear all thread references to the ClosableHiveCatalog class loader and clear thread local
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader. Why do we need
+ // this? Because the webserver threads are long-lived threads and will holds may thread
+ // local references to the current class loader. However, they can't be cleared as
+ // `Catalog.close` is called in the thread pool located in the Caffeine cache, which is not
+ // in the webserver threads. So we need to manually clear the thread local map for webserver
+ // threads.
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join thread: {}", thread.getName(), e);
+ }
+ }
+ }
+ }
+
+ private void unregisterLogFactory() {
+ try {
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister LogFactory", e);
+ }
+ }
+
+ private static void closeResourceInAzure() {
Review Comment:
I'm going to move those methods to `catalog-common`, so that other catalogs can reuse them.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3227424128
> > My biggest concern is the maintainability of the clear resource logic. It seems that we are doing some complex work that should belong to the JVM level's GC, and this workload will get out of control as the project develops.
> > I don't know if we have other alternative solutions to solve the OOM problem, but the current solution seems not to be the optimal one in the long run. WDYT? @jerryshao @FANNG1
>
> I agree. My thought is that though we may have lots of catalogs, the number of providers is limited, which means the number of classloaders are limited, we don't have to create a classloader for each catalog. We can reuse the classloader for the same type of catalogs, and with this we can reduce the class-related OOM issues.
>
> Also, many engines like Spark/Trino that support multiple connectors will face the same problem, especially for Trino, which can also support dynamic connector register/unregister. How do they handle this problem? We'd better have a long-term solution.
>
> For now, I think we can have a short solution, but we should clearly separate this from the main logic, so we can remove it easily. Also, we should add the comments as possible as we can.
I see.
I will try to introduce the class loader reuse mechanism in the next version, and possibly there will be static variable sharing problems like `UGI` exists, so conducting full tests is necessary before this changes.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3227409000
> My biggest concern is the maintainability of the clear resource logic. It seems that we are doing some complex work that should belong to the JVM level's GC, and this workload will get out of control as the project develops.
>
> I don't know if we have other alternative solutions to solve the OOM problem, but the current solution seems not to be the optimal one in the long run. WDYT? @jerryshao @FANNG1
I agree. My thought is that though we may have lots of catalogs, the number of providers is limited, which means the number of classloaders are limited, we don't have create a classloader for each catalog, we can reuse the classloader for the same type of catalogs, with this we can reduce the class-related OOM issues.
Also, many engines like Spark/Trino that support multiple connectors will face the same problem, especially for Trino, they can also support dynamic connector register/unregister, how do they handle this problem? We'd better have a long term solution.
For now, I think we can have a short solution, but we should clearly separate this from the main logic, so we can remove it easily. Also, we should add the comments as possible as we can.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2293271500
##########
core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java:
##########
@@ -244,6 +244,9 @@ public void close() {
catalog = null;
return null;
});
+ // Let hooks close first before classLoader close, so that the hook can
+ // release any resources that the catalog might be using.
+ Thread.sleep(500);
Review Comment:
Using Thread.sleep() for timing coordination is unreliable and blocks the calling thread. Consider using a CountDownLatch or CompletableFuture to wait for hook completion, or implement proper shutdown coordination through the catalog interface.
```suggestion
// Removed Thread.sleep(500); -- hooks should be closed synchronously.
```
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,176 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ try {
+
+ closeStatsDataClearerInFileSystem();
+
+ FileSystem.closeAll();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting peer cache thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join peer cache thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // For Aws SDK metrics, unregister the metric admin MBean
+ try {
+ Class<?> methodUtilsClass = Class.forName("com.amazonaws.metrics.AwsSdkMetrics");
+ MethodUtils.invokeStaticMethod(methodUtilsClass, "unregisterMetricAdminMBean");
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister AWS SDK metrics admin MBean", e);
+ // This is not critical, so we just log the warning
+ }
+
+ // For GCS
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName("org.apache.gravitino.gcp.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory", e);
+ }
+
+ // For Azure
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName(
+ "org.apache.gravitino.azure.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // Clear timer in AbfsClientThrottlingAnalyzer
+ Class<?> abfsClientThrottlingInterceptClass =
+ Class.forName("org.apache.hadoop.fs.azurebfs.services.AbfsClientThrottlingIntercept");
+ Object abfsClientThrottlingIntercept =
+ FieldUtils.readStaticField(abfsClientThrottlingInterceptClass, "singleton", true);
+
+ Object readThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "readThrottler", true);
+ Object writeThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "writeThrottler", true);
+
+ Timer readTimer = (Timer) FieldUtils.readField(readThrottler, "timer", true);
+ readTimer.cancel();
+ Timer writeTimer = (Timer) FieldUtils.readField(writeThrottler, "timer", true);
+ writeTimer.cancel();
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory", e);
+ }
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear FileSystem statistics cleaner thread", e);
+ }
+ }
+
+ private static void clearThreadLocalMap(Thread thread) {
+ if (thread != null && thread.getName().startsWith("Gravitino-webserver-")) {
+ // try to
+ try {
+ Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
+ threadLocalsField.setAccessible(true);
+ Object threadLocalMap = threadLocalsField.get(thread);
+
+ if (threadLocalMap != null) {
+ Class<?> tlmClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
+ Field tableField = tlmClass.getDeclaredField("table");
+ tableField.setAccessible(true);
+ Object[] table = (Object[]) tableField.get(threadLocalMap);
+
+ for (Object entry : table) {
+ if (entry != null) {
+ Object value = FieldUtils.readField(entry, "value", true);
+ if (value != null
+ && value.getClass().getClassLoader() != null
+ && value.getClass().getClassLoader()
+ == SecureFilesetCatalogOperations.class.getClassLoader()) {
+ LOG.info(
+ "Cleaning up thread local {} for thread {} with custom class loader",
+ value,
+ thread.getName());
+ FieldUtils.writeField(entry, "value", null, true);
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ LOG.warn("Failed to clean up thread locals for thread {}", thread.getName(), e);
+ }
+ }
+ }
+
+ private static void closeStatsDataClearerInFileSystem()
+ throws IllegalAccessException, InterruptedException {
+ ScheduledExecutorService scheduler =
+ (ScheduledExecutorService)
+ FieldUtils.readStaticField(MutableQuantiles.class, "scheduler", true);
+ scheduler.shutdownNow();
+ Field statisticsCleanerField =
+ FieldUtils.getField(FileSystem.Statistics.class, "STATS_DATA_CLEANER", true);
+ Object statisticsCleaner = statisticsCleanerField.get(null);
+ if (statisticsCleaner != null) {
+ ((Thread) statisticsCleaner).interrupt();
+ ((Thread) statisticsCleaner).setContextClassLoader(null);
+ ((Thread) statisticsCleaner).join();
+ }
+ }
+
+ private static Thread[] getAllThreads() {
+ ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
+ ThreadGroup parentGroup;
+ while ((parentGroup = rootGroup.getParent()) != null) {
+ rootGroup = parentGroup;
+ }
+
+ Thread[] threads = new Thread[rootGroup.activeCount()];
+ while (rootGroup.enumerate(threads, true) == threads.length) {
+ threads = new Thread[threads.length * 2];
+ }
+ return threads;
+ }
+
+ private static boolean runningWithCurrentClassLoader(Thread thread) {
+ return thread != null
+ && thread.getContextClassLoader() == FilesetCatalogOperations.class.getClassLoader();
+ }
+
+ public static void clearShutdownHooks() {
+ try {
+ Class<?> shutdownHooks = Class.forName("java.lang.ApplicationShutdownHooks");
+ IdentityHashMap<Thread, Thread> hooks =
+ (IdentityHashMap<Thread, Thread>)
+ FieldUtils.readStaticField(shutdownHooks, "hooks", true);
+
+ hooks
+ .entrySet()
+ .removeIf(
+ entry -> {
+ Thread thread = entry.getKey();
+ return thread.getContextClassLoader()
+ == FilesetCatalogOperations.class.getClassLoader();
+ });
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to clean shutdown hooks", e);
Review Comment:
Throwing a RuntimeException during cleanup could prevent other cleanup operations from completing. Consider logging the error instead of throwing, or ensure this is the last cleanup operation.
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,176 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ try {
+
+ closeStatsDataClearerInFileSystem();
+
+ FileSystem.closeAll();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting peer cache thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join peer cache thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // For Aws SDK metrics, unregister the metric admin MBean
+ try {
+ Class<?> methodUtilsClass = Class.forName("com.amazonaws.metrics.AwsSdkMetrics");
+ MethodUtils.invokeStaticMethod(methodUtilsClass, "unregisterMetricAdminMBean");
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister AWS SDK metrics admin MBean", e);
+ // This is not critical, so we just log the warning
+ }
+
+ // For GCS
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName("org.apache.gravitino.gcp.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory", e);
+ }
+
+ // For Azure
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName(
+ "org.apache.gravitino.azure.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // Clear timer in AbfsClientThrottlingAnalyzer
+ Class<?> abfsClientThrottlingInterceptClass =
+ Class.forName("org.apache.hadoop.fs.azurebfs.services.AbfsClientThrottlingIntercept");
+ Object abfsClientThrottlingIntercept =
+ FieldUtils.readStaticField(abfsClientThrottlingInterceptClass, "singleton", true);
+
+ Object readThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "readThrottler", true);
+ Object writeThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "writeThrottler", true);
+
+ Timer readTimer = (Timer) FieldUtils.readField(readThrottler, "timer", true);
+ readTimer.cancel();
+ Timer writeTimer = (Timer) FieldUtils.readField(writeThrottler, "timer", true);
+ writeTimer.cancel();
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory", e);
Review Comment:
The error message incorrectly refers to "GCS shaded LogFactory" when the code is actually handling Azure LogFactory cleanup. The message should be "Failed to find Azure shaded LogFactory".
```suggestion
LOG.warn("Failed to find Azure shaded LogFactory", e);
```
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,176 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ try {
+
+ closeStatsDataClearerInFileSystem();
+
+ FileSystem.closeAll();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting peer cache thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join peer cache thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // For Aws SDK metrics, unregister the metric admin MBean
+ try {
+ Class<?> methodUtilsClass = Class.forName("com.amazonaws.metrics.AwsSdkMetrics");
+ MethodUtils.invokeStaticMethod(methodUtilsClass, "unregisterMetricAdminMBean");
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister AWS SDK metrics admin MBean", e);
+ // This is not critical, so we just log the warning
+ }
+
+ // For GCS
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName("org.apache.gravitino.gcp.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory", e);
+ }
+
+ // For Azure
+ try {
+ Class<?> relocatedLogFactory =
+ Class.forName(
+ "org.apache.gravitino.azure.shaded.org.apache.commons.logging.LogFactory");
+ MethodUtils.invokeStaticMethod(
+ relocatedLogFactory, "release", SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // Clear timer in AbfsClientThrottlingAnalyzer
+ Class<?> abfsClientThrottlingInterceptClass =
+ Class.forName("org.apache.hadoop.fs.azurebfs.services.AbfsClientThrottlingIntercept");
+ Object abfsClientThrottlingIntercept =
+ FieldUtils.readStaticField(abfsClientThrottlingInterceptClass, "singleton", true);
+
+ Object readThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "readThrottler", true);
+ Object writeThrottler =
+ FieldUtils.readField(abfsClientThrottlingIntercept, "writeThrottler", true);
+
+ Timer readTimer = (Timer) FieldUtils.readField(readThrottler, "timer", true);
+ readTimer.cancel();
+ Timer writeTimer = (Timer) FieldUtils.readField(writeThrottler, "timer", true);
+ writeTimer.cancel();
+ } catch (Exception e) {
+ LOG.warn("Failed to find GCS shaded LogFactory", e);
+ }
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear FileSystem statistics cleaner thread", e);
+ }
+ }
+
+ private static void clearThreadLocalMap(Thread thread) {
+ if (thread != null && thread.getName().startsWith("Gravitino-webserver-")) {
+ // try to
Review Comment:
The comment on line 366 is incomplete ("try to"). It should be completed to explain what the code is attempting to do, such as "try to clear thread local variables for webserver threads".
```suggestion
// Try to clear thread local variables for Gravitino webserver threads using the custom class loader
```
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2303071724
##########
core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java:
##########
@@ -244,6 +244,9 @@ public void close() {
catalog = null;
return null;
});
+ // Let hooks close first before classLoader close, so that the hook can
+ // release any resources that the catalog might be using.
+ Thread.sleep(500);
Review Comment:
Using sleep is very flaky, you should use another way like the coplit suggested.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3227455738
> > > > My biggest concern is the maintainability of the clear resource logic. It seems that we are doing some complex work that should belong to the JVM level's GC, and this workload will get out of control as the project develops.
> > > > I don't know if we have other alternative solutions to solve the OOM problem, but the current solution seems not to be the optimal one in the long run. WDYT? @jerryshao @FANNG1
> > >
> > >
> > > I agree. My thought is that though we may have lots of catalogs, the number of providers is limited, which means the number of classloaders are limited, we don't have to create a classloader for each catalog. We can reuse the classloader for the same type of catalogs, and with this we can reduce the class-related OOM issues.
> > > Also, many engines like Spark/Trino that support multiple connectors will face the same problem, especially for Trino, which can also support dynamic connector register/unregister. How do they handle this problem? We'd better have a long-term solution.
> > > For now, I think we can have a short solution, but we should clearly separate this from the main logic, so we can remove it easily. Also, we should add the comments as possible as we can.
> >
> >
> > I see.
> > I will try to introduce the class loader reuse mechanism in the next version, and possibly there will be static variable sharing problems like `UGI` exists, so conducting full tests is necessary before this changes.
>
> In Trino, there will be only one classloader for each type of catalog.
We can also have the same solution as I mentioned above. The problem we face is the same as Trino (they also have the same UGI issue). Please investigate it and have a long-term solution.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#issuecomment-3228017180
No, we can have a short-term solution. Just refactor the code as what I mentioned above. For the long term solution, we can move to the next release.
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2303104467
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,211 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ closeStatsDataClearerInFileSystem();
+
+ stopThreadsAndClearThreadLocal();
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ unregisterLogFactory();
+
+ closeResourceInAWS();
+
+ closeResourceInGCP();
+
+ closeResourceInAzure();
+
+ clearShutdownHooks();
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(Thread, ThreadLocal variants) in the class loader", e);
+ }
+ }
+
+ private static void stopThreadsAndClearThreadLocal() {
+ // Clear all thread references to the ClosableHiveCatalog class loader and clear thread local
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader. Why do we need
+ // this? Because the webserver threads are long-lived threads and will holds may thread
+ // local references to the current class loader. However, they can't be cleared as
+ // `Catalog.close` is called in the thread pool located in the Caffeine cache, which is not
+ // in the webserver threads. So we need to manually clear the thread local map for webserver
+ // threads.
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join thread: {}", thread.getName(), e);
+ }
+ }
+ }
+ }
+
+ private void unregisterLogFactory() {
+ try {
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+ } catch (Exception e) {
+ LOG.warn("Failed to unregister LogFactory", e);
+ }
+ }
+
+ private static void closeResourceInAzure() {
Review Comment:
Can we move such code into the specific bundle package?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2300251359
##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/SecureFilesetCatalogOperations.java:
##########
@@ -271,6 +280,182 @@ public void close() throws IOException {
catalogUserContext.close();
UserContext.cleanAllUserContext();
+
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+
+ closeStatsDataClearerInFileSystem();
+
+ FileSystem.closeAll();
+
+ // Clear all thread references to the ClosableHiveCatalog class loader.
+ Thread[] threads = getAllThreads();
+ for (Thread thread : threads) {
+ // Clear thread local map for webserver threads in the current class loader
+ clearThreadLocalMap(thread);
+
+ // Close all threads that are using the FilesetCatalogOperations class loader
+ if (runningWithCurrentClassLoader(thread)) {
+ LOG.info("Interrupting peer cache thread: {}", thread.getName());
+ thread.setContextClassLoader(null);
+ thread.interrupt();
+ try {
+ thread.join(5000);
+ } catch (InterruptedException e) {
+ LOG.warn("Failed to join peer cache thread: {}", thread.getName(), e);
+ }
+ }
+ }
+
+ // Release the LogFactory for the FilesetCatalogOperations class loader
+ LogFactory.release(SecureFilesetCatalogOperations.class.getClassLoader());
+
+ // For Aws SDK metrics, unregister the metric admin MBean
+ try {
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
Re: [PR] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "mchades (via GitHub)" <gi...@apache.org>.
mchades commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2312778170
##########
bundles/azure-bundle/build.gradle.kts:
##########
@@ -25,7 +25,10 @@ plugins {
}
dependencies {
- implementation(project(":bundles:azure"))
+ implementation(project(":bundles:azure")) {
+ exclude(group = "commons-logging", module = "commons-logging")
Review Comment:
If so, could you plz add this to the code comment?
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to clean up resources related to a specific class loader to prevent memory leaks.
+ * Gravitino will create a new class loader for each catalog and release it when there exist any
+ * changes to the catalog. So, it's important to clean up resources related to the class loader to
+ * prevent memory leaks.
+ */
+public class ClassLoaderResourceCleanerUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderResourceCleanerUtils.class);
+
+ private ClassLoaderResourceCleanerUtils() {}
+
+ /**
+ * Close all resources related to the given class loader to prevent memory leaks.
+ *
+ * @param classLoader the classloader to be closed
+ */
+ public static void closeClassLoaderResource(ClassLoader classLoader) {
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ // Clear statics threads in FileSystem and close all FileSystem instances.
+ closeStatsDataClearerInFileSystem(classLoader);
+
+ // Stop all threads with the current class loader and clear their threadLocal variables for
+ // jetty threads that are loaded by the current class loader.
+ // For example, thread local `threadData` in FileSystem#StatisticsDataCleaner is created
+ // within jetty thread with the current class loader. However, there are clear by
+ // `catalog.close` in ForkJoinPool in CaffeineCache, in this case, the thread local variable
+ // will not be cleared, so we need to clear them manually here.
+ stopThreadsAndClearThreadLocalVariables(classLoader);
+
+ // Release the LogFactory for the classloader, each classloader has its own LogFactory
+ // instance.
+ releaseLogFactoryInCommonLogging(classLoader);
+
+ closeResourceInAWS(classLoader);
+
+ closeResourceInGCP(classLoader);
+
+ closeResourceInAzure(classLoader);
+
+ clearShutdownHooks(classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(threads, thread local variables) in the class loader", e);
Review Comment:
Looks like if an error occurs during the call to `closeResourceInAWS`, the methods `closeResourceInGCP`, `closeResourceInAzure`, and `closeStatsDataClearerInFileSystem` will never be called? Is this the expected behavior?
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #8252:
URL: https://github.com/apache/gravitino/pull/8252#discussion_r2313416352
##########
catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.gravitino.utils;
+
+import java.lang.reflect.Field;
+import java.util.IdentityHashMap;
+import java.util.Timer;
+import java.util.concurrent.ScheduledExecutorService;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to clean up resources related to a specific class loader to prevent memory leaks.
+ * Gravitino will create a new class loader for each catalog and release it when there exist any
+ * changes to the catalog. So, it's important to clean up resources related to the class loader to
+ * prevent memory leaks.
+ */
+public class ClassLoaderResourceCleanerUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderResourceCleanerUtils.class);
+
+ private ClassLoaderResourceCleanerUtils() {}
+
+ /**
+ * Close all resources related to the given class loader to prevent memory leaks.
+ *
+ * @param classLoader the classloader to be closed
+ */
+ public static void closeClassLoaderResource(ClassLoader classLoader) {
+ boolean testEnv = System.getenv("GRAVITINO_TEST") != null;
+ if (testEnv) {
+ // In test environment, we do not need to clean up class loader related stuff
+ return;
+ }
+
+ try {
+ // Clear statics threads in FileSystem and close all FileSystem instances.
+ closeStatsDataClearerInFileSystem(classLoader);
+
+ // Stop all threads with the current class loader and clear their threadLocal variables for
+ // jetty threads that are loaded by the current class loader.
+ // For example, thread local `threadData` in FileSystem#StatisticsDataCleaner is created
+ // within jetty thread with the current class loader. However, there are clear by
+ // `catalog.close` in ForkJoinPool in CaffeineCache, in this case, the thread local variable
+ // will not be cleared, so we need to clear them manually here.
+ stopThreadsAndClearThreadLocalVariables(classLoader);
+
+ // Release the LogFactory for the classloader, each classloader has its own LogFactory
+ // instance.
+ releaseLogFactoryInCommonLogging(classLoader);
+
+ closeResourceInAWS(classLoader);
+
+ closeResourceInGCP(classLoader);
+
+ closeResourceInAzure(classLoader);
+
+ clearShutdownHooks(classLoader);
+ } catch (Exception e) {
+ LOG.warn("Failed to clear resources(threads, thread local variables) in the class loader", e);
Review Comment:
added
--
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] [#8251][#8249] fix(fileset-catalog): Fix OOM problem for fileset catalog [gravitino]
Posted by "jerryshao (via GitHub)" <gi...@apache.org>.
jerryshao merged PR #8252:
URL: https://github.com/apache/gravitino/pull/8252
--
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