|
27 | 27 |
|
28 | 28 | import java.io.File;
|
29 | 29 | import java.io.IOException;
|
| 30 | +import java.net.ConnectException; |
30 | 31 | import java.security.SecureRandom;
|
31 | 32 | import java.util.AbstractMap;
|
32 | 33 | import java.util.ArrayList;
|
|
67 | 68 | import org.apache.hadoop.conf.Configuration;
|
68 | 69 | import org.apache.hadoop.fs.FileSystem;
|
69 | 70 | import org.apache.hadoop.fs.LocalFileSystem;
|
| 71 | +import org.apache.hadoop.fs.Path; |
| 72 | +import org.apache.hadoop.hdfs.DistributedFileSystem; |
70 | 73 | import org.apache.hadoop.io.Text;
|
71 | 74 | import org.junit.jupiter.api.Test;
|
72 | 75 |
|
@@ -840,4 +843,42 @@ public void testMultipleFilesAndCache() throws Exception {
|
840 | 843 | assertEquals(testData, toMap(scanner));
|
841 | 844 | scanner.close();
|
842 | 845 | }
|
| 846 | + |
| 847 | + @Test |
| 848 | + public void testFileSystemFromUri() throws Exception { |
| 849 | + String localFsClass = "LocalFileSystem"; |
| 850 | + |
| 851 | + String remoteFsHost = "127.0.0.5:8080"; |
| 852 | + String fileUri = "hdfs://" + remoteFsHost + "/bulk-xyx/file1.rf"; |
| 853 | + // There was a bug in the code where the default hadoop file system was always used. This test |
| 854 | + // checks that the hadoop filesystem used it based on the URI and not the default filesystem. In |
| 855 | + // this env the default file system is the local hadoop file system. |
| 856 | + var exception = |
| 857 | + assertThrows(ConnectException.class, () -> RFile.newWriter().to(fileUri).build()); |
| 858 | + assertTrue(exception.getMessage().contains("to " + remoteFsHost |
| 859 | + + " failed on connection exception: java.net.ConnectException: Connection refused")); |
| 860 | + // Ensure the DistributedFileSystem was used. |
| 861 | + assertTrue(Arrays.stream(exception.getStackTrace()) |
| 862 | + .anyMatch(ste -> ste.getClassName().contains(DistributedFileSystem.class.getName()))); |
| 863 | + assertTrue(Arrays.stream(exception.getStackTrace()) |
| 864 | + .noneMatch(ste -> ste.getClassName().contains(localFsClass))); |
| 865 | + |
| 866 | + var exception2 = assertThrows(RuntimeException.class, () -> { |
| 867 | + var scanner = RFile.newScanner().from(fileUri).build(); |
| 868 | + scanner.iterator(); |
| 869 | + }); |
| 870 | + assertTrue(exception2.getMessage().contains("to " + remoteFsHost |
| 871 | + + " failed on connection exception: java.net.ConnectException: Connection refused")); |
| 872 | + assertTrue(Arrays.stream(exception2.getCause().getStackTrace()) |
| 873 | + .anyMatch(ste -> ste.getClassName().contains(DistributedFileSystem.class.getName()))); |
| 874 | + assertTrue(Arrays.stream(exception2.getCause().getStackTrace()) |
| 875 | + .noneMatch(ste -> ste.getClassName().contains(localFsClass))); |
| 876 | + |
| 877 | + // verify the assumptions this test is making about the local filesystem being the default. |
| 878 | + var exception3 = assertThrows(IllegalArgumentException.class, |
| 879 | + () -> FileSystem.get(new Configuration()).open(new Path(fileUri))); |
| 880 | + assertTrue(exception3.getMessage().contains("Wrong FS: " + fileUri + ", expected: file:///")); |
| 881 | + assertTrue(Arrays.stream(exception3.getStackTrace()) |
| 882 | + .anyMatch(ste -> ste.getClassName().contains(localFsClass))); |
| 883 | + } |
843 | 884 | }
|
0 commit comments