77import java .io .BufferedReader ;
88import java .io .InputStreamReader ;
99import java .util .Map ;
10+ import java .util .concurrent .TimeUnit ;
1011
1112public class HadoopUtils {
1213 private static final Logger log = LoggerFactory .getLogger (HadoopUtils .class );
14+ private static final long DEFAULT_TIMEOUT_SECONDS = 300 ;
1315
1416 public static boolean pathExists (String hdfsPath ) {
15- if (hdfsPath == null || StringUtils .containsWhitespace (hdfsPath )) {
16- throw new IllegalArgumentException ("hdfsPath cannot be blank" );
17- }
17+ validatePath (hdfsPath );
1818
1919 try {
2020 ProcessBuilder pb = new ProcessBuilder ("hadoop" , "fs" , "-test" , "-e" , hdfsPath );
2121 pb .redirectErrorStream (true );
2222 clearJavaToolOptions (pb .environment ());
2323 Process process = pb .start ();
24- int exitCode = process .waitFor ();
24+ boolean finished = process .waitFor (DEFAULT_TIMEOUT_SECONDS , TimeUnit .SECONDS );
25+ if (!finished ) {
26+ process .destroyForcibly ();
27+ throw new RuntimeException ("Check hdfs path exists timeout: path=" + hdfsPath );
28+ }
29+ int exitCode = process .exitValue ();
2530 return exitCode == 0 ;
2631 } catch (Exception e ) {
2732 throw new RuntimeException ("Check hdfs path exists failed: path=" + hdfsPath , e );
2833 }
2934 }
3035
3136 public static void deletePath (String hdfsPath ) {
32- if (StringUtils .isEmpty (hdfsPath ) || StringUtils .containsWhitespace (hdfsPath )) {
33- throw new IllegalArgumentException ("hdfsPath cannot be blank" );
34- }
37+ validatePath (hdfsPath );
3538
3639 try {
3740 ProcessBuilder pb = new ProcessBuilder ("hadoop" , "fs" , "-rm" , "-r" , "-f" , hdfsPath );
@@ -47,7 +50,12 @@ public static void deletePath(String hdfsPath) {
4750 }
4851 }
4952
50- int exitCode = process .waitFor ();
53+ boolean finished = process .waitFor (DEFAULT_TIMEOUT_SECONDS , TimeUnit .SECONDS );
54+ if (!finished ) {
55+ process .destroyForcibly ();
56+ throw new RuntimeException ("Delete hdfs path timeout: path=" + hdfsPath );
57+ }
58+ int exitCode = process .exitValue ();
5159 if (exitCode != 0 ) {
5260 log .warn ("Delete hdfs path failed: path={}, exitCode={}, output={}" , hdfsPath , exitCode , output );
5361 throw new RuntimeException ("Delete hdfs path failed: path=" + hdfsPath + " output=" + output );
@@ -58,6 +66,18 @@ public static void deletePath(String hdfsPath) {
5866 }
5967 }
6068
69+ private static void validatePath (String hdfsPath ) {
70+ if (hdfsPath == null || StringUtils .containsWhitespace (hdfsPath )) {
71+ throw new IllegalArgumentException ("hdfsPath cannot be blank" );
72+ }
73+
74+ if (hdfsPath .contains (";" ) || hdfsPath .contains ("|" ) || hdfsPath .contains ("&" ) ||
75+ hdfsPath .contains ("`" ) || hdfsPath .contains ("$" ) || hdfsPath .contains ("(" ) ||
76+ hdfsPath .contains (")" ) || hdfsPath .contains ("<" ) || hdfsPath .contains (">" )) {
77+ throw new IllegalArgumentException ("hdfsPath contains invalid characters: " + hdfsPath );
78+ }
79+ }
80+
6181 private static void clearJavaToolOptions (Map <String , String > environment ) {
6282 environment .remove ("JAVA_TOOL_OPTIONS" );
6383 }
0 commit comments