Skip to content

Commit f90e252

Browse files
committed
GH-1067: Address HDFS cleanup review feedback
1 parent a5f572e commit f90e252

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

dataset/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ under the License.
5353
<artifactId>arrow-c-data</artifactId>
5454
<scope>compile</scope>
5555
</dependency>
56+
<dependency>
57+
<groupId>org.slf4j</groupId>
58+
<artifactId>slf4j-api</artifactId>
59+
</dependency>
5660
<dependency>
5761
<groupId>org.immutables</groupId>
5862
<artifactId>value-annotations</artifactId>

dataset/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
requires org.apache.arrow.c;
2727
requires org.apache.arrow.memory.core;
2828
requires org.apache.arrow.vector;
29+
requires org.slf4j;
2930
}

dataset/src/main/java/org/apache/arrow/dataset/file/FileSystemDatasetFactory.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.net.URI;
21+
import java.net.URISyntaxException;
2122
import java.util.LinkedHashSet;
2223
import java.util.Optional;
2324
import java.util.Set;
2425
import org.apache.arrow.dataset.jni.NativeDatasetFactory;
2526
import org.apache.arrow.dataset.jni.NativeMemoryPool;
2627
import org.apache.arrow.dataset.scanner.FragmentScanOptions;
2728
import org.apache.arrow.memory.BufferAllocator;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
2831

2932
/** Java binding of the C++ FileSystemDatasetFactory. */
3033
public class FileSystemDatasetFactory extends NativeDatasetFactory {
3134

35+
private static final Logger LOGGER = LoggerFactory.getLogger(FileSystemDatasetFactory.class);
36+
3237
private final Set<URI> hdfsFileSystems;
3338

3439
public FileSystemDatasetFactory(
@@ -78,11 +83,10 @@ public synchronized void close() {
7883
}
7984

8085
/**
81-
* For each {@code hdfs://} URI, close the cached Hadoop FileSystem.
82-
* When Arrow C++ accesses HDFS via libhdfs, the Hadoop Java client creates cached FileSystem
83-
* instances with non-daemon threads (IPC connections, lease renewers) that prevent JVM exit.
84-
* Closing the FileSystem terminates these connections. Uses reflection to avoid a compile-time
85-
* dependency on hadoop-common.
86+
* For each {@code hdfs://} URI, close the cached Hadoop FileSystem. When Arrow C++ accesses HDFS
87+
* via libhdfs, the Hadoop Java client creates cached FileSystem instances with non-daemon threads
88+
* (IPC connections, lease renewers) that prevent JVM exit. Closing the FileSystem terminates
89+
* these connections. Uses reflection to avoid a compile-time dependency on hadoop-common.
8690
*/
8791
static void closeHadoopFileSystemsIfHdfs(String... uris) {
8892
toHdfsFileSystems(uris).forEach(FileSystemDatasetFactory::closeHadoopFileSystem);
@@ -94,13 +98,16 @@ private static Set<URI> toHdfsFileSystems(String... uris) {
9498
return hdfsFileSystems;
9599
}
96100
for (String uri : uris) {
101+
if (uri == null) {
102+
continue;
103+
}
97104
try {
98105
URI parsedUri = new URI(uri);
99106
if ("hdfs".equalsIgnoreCase(parsedUri.getScheme())) {
100107
hdfsFileSystems.add(
101108
new URI(parsedUri.getScheme(), parsedUri.getAuthority(), null, null, null));
102109
}
103-
} catch (Exception e) {
110+
} catch (URISyntaxException e) {
104111
// Ignore here; native factory creation reports invalid user URIs.
105112
}
106113
}
@@ -117,7 +124,8 @@ private static void closeHadoopFileSystem(URI hdfsUri) {
117124
Method closeMethod = fsClass.getMethod("close");
118125
closeMethod.invoke(fs);
119126
} catch (Exception e) {
120-
// Best-effort cleanup; Hadoop may not be on classpath or FileSystem already closed
127+
// Best-effort cleanup; Hadoop may not be on classpath or FileSystem already closed.
128+
LOGGER.debug("Failed to close Hadoop FileSystem for {}", hdfsUri, e);
121129
}
122130
}
123131

dataset/src/test/java/org/apache/arrow/dataset/file/TestHdfsFileSystemCleanup.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ void testJvmHangsWithoutCleanup() throws Exception {
6565
@Test
6666
void testJvmExitsWithCleanup() throws Exception {
6767
Process child = forkChildProcess(true);
68-
assertTrue(
69-
waitForExit(child), "JVM should exit when FileSystemDatasetFactory cleanup runs");
68+
assertTrue(waitForExit(child), "JVM should exit when FileSystemDatasetFactory cleanup runs");
7069
assertEquals(0, child.exitValue(), "Child process should exit cleanly (exit code 0)");
7170
}
7271

0 commit comments

Comments
 (0)