5555import org .apache .thrift .TConfiguration ;
5656import org .apache .thrift .transport .TTransport ;
5757import org .apache .thrift .transport .TTransportException ;
58- import org .apache .tsfile .external .commons .io .FileUtils ;
5958import org .apache .tsfile .fileSystem .FSFactoryProducer ;
6059import org .apache .tsfile .utils .FilePathUtils ;
6160import org .slf4j .Logger ;
6968import java .io .IOException ;
7069import java .net .InetSocketAddress ;
7170import java .net .Socket ;
71+ import java .nio .file .FileVisitResult ;
72+ import java .nio .file .Files ;
73+ import java .nio .file .NoSuchFileException ;
74+ import java .nio .file .Path ;
75+ import java .nio .file .SimpleFileVisitor ;
76+ import java .nio .file .attribute .BasicFileAttributes ;
7277import java .util .concurrent .TimeUnit ;
7378
7479import static org .junit .Assert .fail ;
@@ -83,6 +88,8 @@ public class EnvironmentUtils {
8388 private static final DataNodeMemoryConfig memoryConfig =
8489 IoTDBDescriptor .getInstance ().getMemoryConfig ();
8590 private static final TierManager tierManager = TierManager .getInstance ();
91+ private static final int DELETE_RETRY_TIMES = 5 ;
92+ private static final long DELETE_RETRY_INTERVAL_MS = 100 ;
8693
8794 public static long TEST_QUERY_JOB_ID = 1 ;
8895 public static QueryContext TEST_QUERY_CONTEXT = new QueryContext (TEST_QUERY_JOB_ID , false );
@@ -277,7 +284,69 @@ public static void cleanAllDir() throws IOException {
277284 }
278285
279286 public static void cleanDir (String dir ) throws IOException {
280- FSFactoryProducer .getFSFactory ().deleteDirectory (dir );
287+ Path path = FSFactoryProducer .getFSFactory ().getFile (dir ).toPath ();
288+ if (!Files .exists (path )) {
289+ return ;
290+ }
291+
292+ IOException lastException = null ;
293+ for (int i = 0 ; i < DELETE_RETRY_TIMES ; i ++) {
294+ try {
295+ deleteRecursively (path );
296+ return ;
297+ } catch (NoSuchFileException e ) {
298+ return ;
299+ } catch (IOException e ) {
300+ lastException = e ;
301+ if (i + 1 == DELETE_RETRY_TIMES ) {
302+ break ;
303+ }
304+ try {
305+ TimeUnit .MILLISECONDS .sleep (DELETE_RETRY_INTERVAL_MS );
306+ } catch (InterruptedException interruptedException ) {
307+ Thread .currentThread ().interrupt ();
308+ IOException ioException =
309+ new IOException ("Interrupted while deleting " + dir , interruptedException );
310+ ioException .addSuppressed (e );
311+ throw ioException ;
312+ }
313+ }
314+ }
315+ throw lastException ;
316+ }
317+
318+ private static void deleteRecursively (Path path ) throws IOException {
319+ if (!Files .exists (path )) {
320+ return ;
321+ }
322+
323+ Files .walkFileTree (
324+ path ,
325+ new SimpleFileVisitor <Path >() {
326+ @ Override
327+ public FileVisitResult visitFile (Path file , BasicFileAttributes attrs )
328+ throws IOException {
329+ Files .deleteIfExists (file );
330+ return FileVisitResult .CONTINUE ;
331+ }
332+
333+ @ Override
334+ public FileVisitResult visitFileFailed (Path file , IOException exc ) throws IOException {
335+ if (exc instanceof NoSuchFileException ) {
336+ return FileVisitResult .CONTINUE ;
337+ }
338+ throw exc ;
339+ }
340+
341+ @ Override
342+ public FileVisitResult postVisitDirectory (Path dir , IOException exc ) throws IOException {
343+ if (exc != null ) {
344+ throw exc ;
345+ }
346+ Files .deleteIfExists (dir );
347+ return FileVisitResult .CONTINUE ;
348+ }
349+ });
281350 }
282351
283352 /** disable memory control</br> this function should be called before all code in the setup */
@@ -338,19 +407,6 @@ private static void createDir(String dir) {
338407 }
339408
340409 public static void recursiveDeleteFolder (String path ) throws IOException {
341- File file = new File (path );
342- if (file .isDirectory ()) {
343- File [] files = file .listFiles ();
344- if (files == null || files .length == 0 ) {
345- FileUtils .deleteDirectory (file );
346- } else {
347- for (File f : files ) {
348- recursiveDeleteFolder (f .getAbsolutePath ());
349- }
350- FileUtils .deleteDirectory (file );
351- }
352- } else {
353- FileUtils .delete (file );
354- }
410+ cleanDir (path );
355411 }
356412}
0 commit comments