| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,12 +29,12 @@ | |||
| 29 | 29 | /** Java binding of the C++ FileSystemDatasetFactory. */ | |
| 30 | 30 | public class FileSystemDatasetFactory extends NativeDatasetFactory { | |
| 31 | 31 | ||
| 32 | - private final String[] uris; | ||
| 32 | + private final Set<URI> hdfsFileSystems; | ||
| 33 | 33 | ||
| 34 | 34 | public FileSystemDatasetFactory( | |
| 35 | 35 | BufferAllocator allocator, NativeMemoryPool memoryPool, FileFormat format, String uri) { | |
| 36 | 36 | super(allocator, memoryPool, createNative(format, uri, Optional.empty())); | |
| 37 | - this.uris = uri == null ? new String[0] : new String[] {uri}; | ||
| 37 | + this.hdfsFileSystems = toHdfsFileSystems(uri); | ||
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | 40 | public FileSystemDatasetFactory( | |
@@ -44,13 +44,13 @@ public FileSystemDatasetFactory( | |||
| 44 | 44 | String uri, | |
| 45 | 45 | Optional<FragmentScanOptions> fragmentScanOptions) { | |
| 46 | 46 | super(allocator, memoryPool, createNative(format, uri, fragmentScanOptions)); | |
| 47 | - this.uris = uri == null ? new String[0] : new String[] {uri}; | ||
| 47 | + this.hdfsFileSystems = toHdfsFileSystems(uri); | ||
| 48 | 48 | } | |
| 49 | 49 | ||
| 50 | 50 | public FileSystemDatasetFactory( | |
| 51 | 51 | BufferAllocator allocator, NativeMemoryPool memoryPool, FileFormat format, String[] uris) { | |
| 52 | 52 | super(allocator, memoryPool, createNative(format, uris, Optional.empty())); | |
| 53 | - this.uris = uris == null ? new String[0] : uris.clone(); | ||
| 53 | + this.hdfsFileSystems = toHdfsFileSystems(uris); | ||
| 54 | 54 | } | |
| 55 | 55 | ||
| 56 | 56 | public FileSystemDatasetFactory( | |
@@ -60,7 +60,7 @@ public FileSystemDatasetFactory( | |||
| 60 | 60 | String[] uris, | |
| 61 | 61 | Optional<FragmentScanOptions> fragmentScanOptions) { | |
| 62 | 62 | super(allocator, memoryPool, createNative(format, uris, fragmentScanOptions)); | |
| 63 | - this.uris = uris == null ? new String[0] : uris.clone(); | ||
| 63 | + this.hdfsFileSystems = toHdfsFileSystems(uris); | ||
| 64 | 64 | } | |
| 65 | 65 | ||
| 66 | 66 | /** | |
@@ -73,7 +73,7 @@ public synchronized void close() { | |||
| 73 | 73 | try { | |
| 74 | 74 | super.close(); | |
| 75 | 75 | } finally { | |
| 76 | - closeHadoopFileSystemsIfHdfs(uris); | ||
| 76 | + hdfsFileSystems.forEach(FileSystemDatasetFactory::closeHadoopFileSystem); | ||
| 77 | 77 | } | |
| 78 | 78 | } | |
| 79 | 79 | ||
@@ -85,34 +85,26 @@ public synchronized void close() { | |||
| 85 | 85 | * dependency on hadoop-common. | |
| 86 | 86 | */ | |
| 87 | 87 | static void closeHadoopFileSystemsIfHdfs(String... uris) { | |
| 88 | - if (uris == null || uris.length == 0) { | ||
| 89 | - return; | ||
| 90 | - } | ||
| 91 | - Set<URI> hdfsFileSystems = new LinkedHashSet<>(); | ||
| 92 | - for (String uri : uris) { | ||
| 93 | - URI hdfsUri = toHdfsFileSystemUri(uri); | ||
| 94 | - if (hdfsUri != null) { | ||
| 95 | - hdfsFileSystems.add(hdfsUri); | ||
| 96 | - } | ||
| 97 | - } | ||
| 98 | - for (URI hdfsUri : hdfsFileSystems) { | ||
| 99 | - closeHadoopFileSystem(hdfsUri); | ||
| 100 | - } | ||
| 88 | + toHdfsFileSystems(uris).forEach(FileSystemDatasetFactory::closeHadoopFileSystem); | ||
| 101 | 89 | } | |
| 102 | 90 | ||
| 103 | - private static URI toHdfsFileSystemUri(String uri) { | ||
| 104 | - if (uri == null) { | ||
| 105 | - return null; | ||
| 91 | + private static Set<URI> toHdfsFileSystems(String... uris) { | ||
| 92 | + Set<URI> hdfsFileSystems = new LinkedHashSet<>(); | ||
| 93 | + if (uris == null) { | ||
| 94 | + return hdfsFileSystems; | ||
| 106 | 95 | } | |
| 107 | - try { | ||
| 108 | - URI parsedUri = new URI(uri); | ||
| 109 | - if (!"hdfs".equalsIgnoreCase(parsedUri.getScheme())) { | ||
| 110 | - return null; | ||
| 96 | + for (String uri : uris) { | ||
| 97 | + try { | ||
| 98 | + URI parsedUri = new URI(uri); | ||
| 99 | + if ("hdfs".equalsIgnoreCase(parsedUri.getScheme())) { | ||
| 100 | + hdfsFileSystems.add( | ||
| 101 | + new URI(parsedUri.getScheme(), parsedUri.getAuthority(), null, null, null)); | ||
| 102 | + } | ||
| 103 | + } catch (Exception e) { | ||
| 104 | + // Ignore here; native factory creation reports invalid user URIs. | ||
| 111 | 105 | } | |
| 112 | - return new URI(parsedUri.getScheme(), parsedUri.getAuthority(), null, null, null); | ||
| 113 | - } catch (Exception e) { | ||
| 114 | - return null; | ||
| 115 | 106 | } | |
| 107 | + return hdfsFileSystems; | ||
| 116 | 108 | } | |
| 117 | 109 | ||
| 118 | 110 | private static void closeHadoopFileSystem(URI hdfsUri) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,32 +32,18 @@ | |||
| 32 | 32 | import org.junit.jupiter.api.Test; | |
| 33 | 33 | import org.junit.jupiter.api.io.TempDir; | |
| 34 | 34 | ||
| 35 | - /** | ||
| 36 | - * Validates the fix for <a href="https://github.com/apache/arrow-java/issues/1067">#1067</a>: JVM | ||
| 37 | - * hangs after reading HDFS files via Arrow Dataset API due to non-daemon native threads. | ||
| 38 | - * | ||
| 39 | - * <p>When Arrow C++ accesses HDFS via libhdfs, the C library attaches native threads to the JVM via | ||
| 40 | - * {@code AttachCurrentThread}. These threads are non-daemon by default. They block on HDFS IPC | ||
| 41 | - * connections managed by cached {@code FileSystem} instances, preventing the JVM from exiting after | ||
| 42 | - * {@code main()} returns. | ||
| 43 | - * | ||
| 44 | - * <p>These tests fork a child JVM that simulates this behavior: a non-daemon thread holds an HDFS | ||
| 45 | - * connection. Without cleanup, the JVM hangs. With {@code FileSystem.close()} on the cached | ||
| 46 | - * instance (the same mechanism used by {@link FileSystemDatasetFactory#close()}), the connection is | ||
| 47 | - * closed and the thread exits, allowing the JVM to terminate. | ||
| 48 | - */ | ||
| 35 | + /** Regression test for <a href="https://github.com/apache/arrow-java/issues/1067">#1067</a>. */ | ||
| 49 | 36 | public class TestHdfsFileSystemCleanup { | |
| 50 | 37 | ||
| 51 | 38 | private static final int CHILD_TIMEOUT_SECONDS = 10; | |
| 52 | 39 | ||
| 53 | 40 | private static MiniDFSCluster cluster; | |
| 54 | - private static Configuration conf; | ||
| 55 | 41 | ||
| 56 | 42 | @TempDir static File clusterDir; | |
| 57 | 43 | ||
| 58 | 44 | @BeforeAll | |
| 59 | 45 | static void startCluster() throws IOException { | |
| 60 | - conf = new Configuration(); | ||
| 46 | + Configuration conf = new Configuration(); | ||
| 61 | 47 | conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, clusterDir.getAbsolutePath()); | |
| 62 | 48 | cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build(); | |
| 63 | 49 | cluster.waitActive(); | |
@@ -70,35 +56,26 @@ static void stopCluster() { | |||
| 70 | 56 | } | |
| 71 | 57 | } | |
| 72 | 58 | ||
| 73 | - /** | ||
| 74 | - * Without cleanup: a child JVM with a non-daemon thread holding an HDFS connection hangs after | ||
| 75 | - * main() returns because the non-daemon thread keeps the JVM alive. | ||
| 76 | - */ | ||
| 77 | 59 | @Test | |
| 78 | 60 | void testJvmHangsWithoutCleanup() throws Exception { | |
| 79 | 61 | Process child = forkChildProcess(false); | |
| 80 | - boolean exited = child.waitFor(CHILD_TIMEOUT_SECONDS, TimeUnit.SECONDS); | ||
| 81 | - if (!exited) { | ||
| 82 | - child.destroyForcibly(); | ||
| 83 | - } | ||
| 84 | - assertFalse(exited, "JVM should hang when non-daemon HDFS thread is not cleaned up"); | ||
| 62 | + assertFalse(waitForExit(child), "JVM should hang when HDFS is not cleaned up"); | ||
| 85 | 63 | } | |
| 86 | 64 | ||
| 87 | - /** | ||
| 88 | - * With cleanup: closing the specific cached Hadoop FileSystem (the same mechanism used by {@link | ||
| 89 | - * FileSystemDatasetFactory#close()}) closes the HDFS connections, causing the non-daemon thread | ||
| 90 | - * to exit and allowing the JVM to terminate normally. | ||
| 91 | - */ | ||
| 92 | 65 | @Test | |
| 93 | 66 | void testJvmExitsWithCleanup() throws Exception { | |
| 94 | 67 | Process child = forkChildProcess(true); | |
| 68 | + assertTrue( | ||
| 69 | + waitForExit(child), "JVM should exit when FileSystemDatasetFactory cleanup runs"); | ||
| 70 | + assertEquals(0, child.exitValue(), "Child process should exit cleanly (exit code 0)"); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + private boolean waitForExit(Process child) throws InterruptedException { | ||
| 95 | 74 | boolean exited = child.waitFor(CHILD_TIMEOUT_SECONDS, TimeUnit.SECONDS); | |
| 96 | 75 | if (!exited) { | |
| 97 | 76 | child.destroyForcibly(); | |
| 98 | 77 | } | |
| 99 | - assertTrue( | ||
| 100 | - exited, "JVM should exit when FileSystemDatasetFactory cleanup runs before return"); | ||
| 101 | - assertEquals(0, child.exitValue(), "Child process should exit cleanly (exit code 0)"); | ||
| 78 | + return exited; | ||
| 102 | 79 | } | |
| 103 | 80 | ||
| 104 | 81 | private Process forkChildProcess(boolean withCleanup) throws IOException { | |
@@ -117,15 +94,7 @@ private Process forkChildProcess(boolean withCleanup) throws IOException { | |||
| 117 | 94 | return pb.start(); | |
| 118 | 95 | } | |
| 119 | 96 | ||
| 120 | - /** | ||
| 121 | - * Simulates the libhdfs behavior in a standalone JVM. | ||
| 122 | - * | ||
| 123 | - * <p>When Arrow C++ uses libhdfs to access HDFS, native threads are attached to the JVM via | ||
| 124 | - * {@code AttachCurrentThread}. These threads are non-daemon by default and block on HDFS IPC | ||
| 125 | - * connections. This class simulates that by creating a non-daemon thread that periodically polls | ||
| 126 | - * the HDFS filesystem. Without closing the cached FileSystem, the thread keeps the JVM alive | ||
| 127 | - * indefinitely after {@code main()} returns. | ||
| 128 | - */ | ||
| 97 | + /** Simulates libhdfs leaving a non-daemon thread attached to an HDFS connection. */ | ||
| 129 | 98 | public static class HdfsClientSimulator { | |
| 130 | 99 | public static void main(String[] args) throws Exception { | |
| 131 | 100 | int port = Integer.parseInt(args[0]); | |
@@ -135,13 +104,9 @@ public static void main(String[] args) throws Exception { | |||
| 135 | 104 | String hdfsUri = "hdfs://localhost:" + port; | |
| 136 | 105 | conf.set("fs.defaultFS", hdfsUri); | |
| 137 | 106 | ||
| 138 | - // Get a cached FileSystem (same as libhdfs does internally) | ||
| 139 | 107 | FileSystem fs = FileSystem.get(conf); | |
| 140 | 108 | fs.exists(new Path("/")); | |
| 141 | 109 | ||
| 142 | - // Simulate a non-daemon thread attached via AttachCurrentThread that | ||
| 143 | - // blocks on the HDFS IPC connection. In the real libhdfs scenario, | ||
| 144 | - // these are native threads that process RPC responses. | ||
| 145 | 110 | Thread connectionThread = | |
| 146 | 111 | new Thread( | |
| 147 | 112 | () -> { | |
@@ -150,24 +115,18 @@ public static void main(String[] args) throws Exception { | |||
| 150 | 115 | fs.getFileStatus(new Path("/")); | |
| 151 | 116 | Thread.sleep(500); | |
| 152 | 117 | } catch (Exception e) { | |
| 153 | - // FileSystem was closed or thread interrupted — exit | ||
| 154 | 118 | break; | |
| 155 | 119 | } | |
| 156 | 120 | } | |
| 157 | - }); | ||
| 121 | + }, | ||
| 122 | + "simulated-libhdfs-ipc-thread"); | ||
| 158 | 123 | connectionThread.setDaemon(false); | |
| 159 | - connectionThread.setName("simulated-libhdfs-ipc-thread"); | ||
| 160 | 124 | connectionThread.start(); | |
| 161 | 125 | ||
| 162 | 126 | if (withCleanup) { | |
| 163 | - // Use the same helper invoked by FileSystemDatasetFactory.close(). | ||
| 164 | - // Closing the cached FileSystem terminates IPC connections, causing | ||
| 165 | - // the non-daemon thread to get an IOException and exit. | ||
| 166 | 127 | FileSystemDatasetFactory.closeHadoopFileSystemsIfHdfs(hdfsUri); | |
| 167 | 128 | connectionThread.join(5000); | |
| 168 | 129 | } | |
| 169 | - | ||
| 170 | - // main() returns. Without cleanup, the non-daemon thread keeps the JVM alive. | ||
| 171 | 130 | } | |
| 172 | 131 | } | |
| 173 | 132 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments