This is an automated email from the ASF dual-hosted git repository.
huaxingao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git
The following commit(s) were added to refs/heads/main by this push:
new 6cf1f6e4e6 Core: exit idle REST metrics reporter threads (#19100)
6cf1f6e4e6 is described below
commit 6cf1f6e4e6cfa6210f0017466283be6d01acab68
Author: Yong Zheng <[email protected]>
AuthorDate: Wed Nov 25 11:41:30 2026 -0600
Core: exit idle REST metrics reporter threads (#19100)
* Fix metrics executor idle thread
* Move executor craation into RESTSessionCatalog
* Add test runTaskAfterIdleThreadExits
* Added test threadUsesIcebergClassLoader
---
.../apache/iceberg/rest/RESTSessionCatalog.java | 38 +++++++-
.../iceberg/rest/TestRESTMetricsExecutor.java | 107 +++++++++++++++++++++
2 files changed, 143 insertions(+), 2 deletions(-)
diff --git a/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java
b/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java
index 56bed57e32..4a089da7f6 100644
--- a/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java
+++ b/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java
@@ -21,11 +21,15 @@ package org.apache.iceberg.rest;
import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
+import java.time.Duration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -75,6 +79,7 @@ import
org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import
org.apache.iceberg.relocated.com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.iceberg.rest.RESTCatalogProperties.ScanPlanningMode;
import org.apache.iceberg.rest.RESTCatalogProperties.SnapshotMode;
import org.apache.iceberg.rest.RESTTableCache.TableWithETag;
@@ -105,7 +110,6 @@ import
org.apache.iceberg.rest.responses.UnregisterTableResponse;
import org.apache.iceberg.rest.responses.UpdateNamespacePropertiesResponse;
import org.apache.iceberg.util.EnvironmentUtil;
import org.apache.iceberg.util.PropertyUtil;
-import org.apache.iceberg.util.ThreadPools;
import org.apache.iceberg.view.BaseView;
import org.apache.iceberg.view.ImmutableSQLViewRepresentation;
import org.apache.iceberg.view.ImmutableViewVersion;
@@ -286,7 +290,7 @@ public class RESTSessionCatalog extends
BaseViewSessionCatalog
RESTCatalogProperties.METRICS_REPORTING_ENABLED_DEFAULT);
if (reportingViaRestEnabled) {
- this.metricsExecutor =
ThreadPools.newFixedThreadPool("rest-metrics-reporter", 1);
+ this.metricsExecutor = newMetricsExecutor(Duration.ofSeconds(60));
this.closeables.addCloseable(metricsExecutor::shutdown);
}
@@ -1979,4 +1983,34 @@ public class RESTSessionCatalog extends
BaseViewSessionCatalog
return new BaseView(ops, ViewUtil.fullViewName(name(), identifier));
}
}
+
+ /**
+ * Creates the single-threaded executor used to send metrics reports. Its
thread exits after being
+ * idle for the given duration and does not inherit thread-locals or the
context class loader of
+ * the thread that creates it, so a catalog that is never closed does not
keep its users
+ * reachable.
+ */
+ @VisibleForTesting
+ static ExecutorService newMetricsExecutor(Duration idleTimeout) {
+ ThreadPoolExecutor executor =
+ new ThreadPoolExecutor(
+ 1,
+ 1,
+ idleTimeout.toNanos(),
+ TimeUnit.NANOSECONDS,
+ new LinkedBlockingQueue<>(),
+ new ThreadFactoryBuilder()
+ .setThreadFactory(RESTSessionCatalog::newNonInheritingThread)
+ .setDaemon(true)
+ .setNameFormat("rest-metrics-reporter-%d")
+ .build());
+ executor.allowCoreThreadTimeOut(true);
+ return executor;
+ }
+
+ private static Thread newNonInheritingThread(Runnable runnable) {
+ Thread thread = new Thread(null, runnable, "", 0, false /*
inheritThreadLocals */);
+ thread.setContextClassLoader(RESTSessionCatalog.class.getClassLoader());
+ return thread;
+ }
}
diff --git
a/core/src/test/java/org/apache/iceberg/rest/TestRESTMetricsExecutor.java
b/core/src/test/java/org/apache/iceberg/rest/TestRESTMetricsExecutor.java
new file mode 100644
index 0000000000..5b6242f900
--- /dev/null
+++ b/core/src/test/java/org/apache/iceberg/rest/TestRESTMetricsExecutor.java
@@ -0,0 +1,107 @@
+/*
+ * 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.iceberg.rest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.time.Duration;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import org.apache.iceberg.relocated.com.google.common.base.Splitter;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Test;
+
+class TestRESTMetricsExecutor {
+
+ @Test
+ void idleThreadExits() {
+ ThreadPoolExecutor executor =
+ (ThreadPoolExecutor)
RESTSessionCatalog.newMetricsExecutor(Duration.ofMillis(10));
+ try {
+ executor.execute(() -> {});
+ Awaitility.await()
+ .atMost(Duration.ofSeconds(10))
+ .untilAsserted(() -> assertThat(executor.getPoolSize()).isZero());
+ } finally {
+ executor.shutdownNow();
+ }
+ }
+
+ @Test
+ void runTaskAfterIdleThreadExits() throws Exception {
+ ThreadPoolExecutor executor =
+ (ThreadPoolExecutor)
RESTSessionCatalog.newMetricsExecutor(Duration.ofMillis(10));
+ try {
+ executor.submit(() -> {}).get();
+ Awaitility.await()
+ .atMost(Duration.ofSeconds(10))
+ .untilAsserted(() -> assertThat(executor.getPoolSize()).isZero());
+ assertThat(executor.submit(() -> "sent").get(10,
TimeUnit.SECONDS)).isEqualTo("sent");
+ } finally {
+ executor.shutdownNow();
+ }
+ }
+
+ @Test
+ void threadDoesNotInheritThreadLocals() throws Exception {
+ InheritableThreadLocal<String> threadLocal = new
InheritableThreadLocal<>();
+ threadLocal.set("value");
+ ExecutorService executor =
RESTSessionCatalog.newMetricsExecutor(Duration.ofMinutes(1));
+ try {
+ assertThat(executor.submit(threadLocal::get).get()).isNull();
+ } finally {
+ executor.shutdownNow();
+ threadLocal.remove();
+ }
+ }
+
+ @Test
+ void threadUsesIcebergClassLoader() throws Exception {
+ // Load the catalog through a non-system class loader, like how Apache
Spark does for
+ // '--jars' and '--packages'. Otherwise, the thread falls back to the
system class loader.
+ List<URL> classpath = Lists.newArrayList();
+ for (String entry :
+
Splitter.on(File.pathSeparator).split(System.getProperty("java.class.path"))) {
+ classpath.add(new File(entry).toURI().toURL());
+ }
+
+ try (URLClassLoader icebergLoader =
+ new URLClassLoader(classpath.toArray(new URL[0]),
ClassLoader.getPlatformClassLoader())) {
+ Method newMetricsExecutor =
+ Class.forName(RESTSessionCatalog.class.getName(), true,
icebergLoader)
+ .getDeclaredMethod("newMetricsExecutor", Duration.class);
+ newMetricsExecutor.setAccessible(true);
+ ExecutorService executor =
+ (ExecutorService) newMetricsExecutor.invoke(null,
Duration.ofMinutes(1));
+ try {
+ assertThat(executor.submit(() ->
Thread.currentThread().getContextClassLoader()).get())
+ .isSameAs(icebergLoader);
+ } finally {
+ executor.shutdownNow();
+ }
+ }
+ }
+}