nastra commented on code in PR #15312:
URL: https://github.com/apache/iceberg/pull/15312#discussion_r3115817017
##########
core/src/main/java/org/apache/iceberg/util/ThreadPools.java:
##########
@@ -149,8 +153,89 @@ public static ExecutorService newWorkerPool(String
namePrefix, int poolSize) {
* that should be automatically cleaned up on JVM shutdown.
*/
public static ExecutorService newExitingWorkerPool(String namePrefix, int
poolSize) {
- return MoreExecutors.getExitingExecutorService(
- (ThreadPoolExecutor) newFixedThreadPool(namePrefix, poolSize));
+ ExecutorService service =
+ Executors.unconfigurableExecutorService(newFixedThreadPool(namePrefix,
poolSize));
+ THREAD_POOL_MANAGER.addThreadPool(service, DEFAULT_SHUTDOWN_TIMEOUT);
+ return service;
+ }
+
+ /**
+ * Shuts down all thread pools registered via {@link
#newExitingWorkerPool(String, int)} or {@link
+ * #newExitingScheduledPool(String, int, Duration)} and removes the JVM
shutdown hook.
+ *
+ * <p>This method is useful for:
+ *
+ * <ul>
+ * <li>Preventing thread pool leaks in hot-reload environments
+ * <li>Managing graceful application shutdown when the application needs
to handle its own
+ * shutdown hooks (e.g. to commit pending files before exiting)
+ * </ul>
+ *
+ * <p>Only call this method at the end of the intended usage of the library.
Calling it earlier
+ * will stop thread pools required for normal library workflows.
+ */
+ public static void shutdownThreadPools() {
+ THREAD_POOL_MANAGER.shutdownAll();
+ removeShutdownHook();
+ }
+
+ /**
+ * Initialize a shutdown hook to stop the thread pools created via the {@link
+ * #newExitingWorkerPool(String, int)}.
+ */
+ @SuppressWarnings("ShutdownHook")
+ @VisibleForTesting
+ static synchronized void initShutdownHook() {
+ if (shutdownHook == null) {
+ shutdownHook =
+ Executors.defaultThreadFactory()
+ .newThread(
+ () -> {
+ shutdownHook = null;
+ shutdownThreadPools();
+ });
+
+ try {
+ shutdownHook.setName("DelayedShutdownHook-iceberg");
+ } catch (SecurityException e) {
+ LOG.warn("Cannot set thread name for the shutdown hook", e);
+ }
+
+ try {
+ Runtime.getRuntime().addShutdownHook(shutdownHook);
+ } catch (SecurityException e) {
+ LOG.warn("Cannot install a shutdown hook for thread pools clean up",
e);
+ }
+ }
+ }
+
+ /**
+ * Stop the shutdown hook for the thread pools created via the {@link
Review Comment:
```suggestion
* Remove the shutdown hook for the thread pools created via the {@link
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]