Skip to content

Commit ddc6554

Browse files
authored
Fix test clean directory deletion (#17860)
1 parent d6d5755 commit ddc6554

7 files changed

Lines changed: 140 additions & 41 deletions

File tree

iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/persistence/pipe/PipeTaskInfoConsensusPipeTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.iotdb.commons.pipe.agent.task.meta.PipeStaticMeta;
2626
import org.apache.iotdb.commons.pipe.agent.task.meta.PipeStatus;
2727
import org.apache.iotdb.commons.pipe.agent.task.meta.PipeTaskMeta;
28+
import org.apache.iotdb.commons.utils.FileUtils;
2829
import org.apache.iotdb.confignode.consensus.request.write.pipe.task.CreatePipePlanV2;
2930
import org.apache.iotdb.confignode.consensus.request.write.pipe.task.SetPipeStatusPlanV2;
3031
import org.apache.iotdb.confignode.consensus.request.write.pipe.task.SetPipeStatusWithStoppedByRuntimeExceptionPlanV2;
@@ -203,8 +204,7 @@ public void testProcessLoadSnapshotRestartsOnlyHealthyStoppedConsensusPipes() th
203204
.getStatus()
204205
.get());
205206
} finally {
206-
new File(snapshotDir, "pipe_task_info.bin").delete();
207-
snapshotDir.delete();
207+
FileUtils.deleteFileOrDirectory(snapshotDir, true);
208208
}
209209
}
210210

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/pipe/agent/plugin/PipeDataNodePluginAgentTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.iotdb.db.pipe.sink.protocol.thrift.async.IoTDBDataRegionAsyncSink;
3232
import org.apache.iotdb.db.pipe.sink.protocol.thrift.sync.IoTDBDataRegionSyncSink;
3333
import org.apache.iotdb.db.pipe.source.dataregion.IoTDBDataRegionSource;
34+
import org.apache.iotdb.db.utils.EnvironmentUtils;
3435
import org.apache.iotdb.pipe.api.customizer.parameter.PipeParameters;
3536

3637
import org.junit.After;
@@ -74,10 +75,10 @@ public void after() {
7475
String pluginPath =
7576
PipePluginExecutableManager.getInstance()
7677
.getPluginsDirPath(PIPE_PLUGIN_META.getPluginName());
77-
Files.deleteIfExists(Paths.get(pluginPath));
78-
Files.deleteIfExists(Paths.get(PipePluginExecutableManager.getInstance().getInstallDir()));
79-
Files.deleteIfExists(Paths.get(TMP_TEMP_LIB_ROOT_DIR));
80-
Files.deleteIfExists(Paths.get(TMP_LIB_ROOT_DIR));
78+
EnvironmentUtils.cleanDir(pluginPath);
79+
EnvironmentUtils.cleanDir(PipePluginExecutableManager.getInstance().getInstallDir());
80+
EnvironmentUtils.cleanDir(TMP_TEMP_LIB_ROOT_DIR);
81+
EnvironmentUtils.cleanDir(TMP_LIB_ROOT_DIR);
8182
} catch (IOException e) {
8283
Assert.fail();
8384
}

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/compaction/repair/AbstractRepairDataTest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.iotdb.commons.exception.MetadataException;
2323
import org.apache.iotdb.db.exception.StorageEngineException;
2424
import org.apache.iotdb.db.storageengine.dataregion.compaction.AbstractCompactionTest;
25+
import org.apache.iotdb.db.utils.EnvironmentUtils;
2526
import org.apache.iotdb.db.utils.constant.TestConstant;
2627

2728
import org.apache.tsfile.exception.write.WriteProcessException;
@@ -54,14 +55,6 @@ public File getEmptyRepairDataLogDir() throws IOException {
5455
}
5556

5657
private void deleteRepairDataLogDir() throws IOException {
57-
if (repairDataLogDir.exists()) {
58-
if (repairDataLogDir.isDirectory()) {
59-
File[] files = repairDataLogDir.listFiles();
60-
for (File file : files == null ? new File[] {} : files) {
61-
Files.deleteIfExists(file.toPath());
62-
}
63-
}
64-
Files.deleteIfExists(repairDataLogDir.toPath());
65-
}
58+
EnvironmentUtils.cleanDir(repairDataLogDir.getPath());
6659
}
6760
}

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ConfigurationFileUtilsTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.nio.file.Files;
3737
import java.util.HashSet;
3838
import java.util.Map;
39-
import java.util.Objects;
4039
import java.util.Properties;
4140
import java.util.Set;
4241

@@ -47,13 +46,7 @@ public class ConfigurationFileUtilsTest {
4746

4847
@After
4948
public void tearDown() throws IOException {
50-
if (!dir.exists()) {
51-
return;
52-
}
53-
for (File file : Objects.requireNonNull(dir.listFiles())) {
54-
Files.delete(file.toPath());
55-
}
56-
Files.delete(dir.toPath());
49+
EnvironmentUtils.cleanDir(dir.getPath());
5750
}
5851

5952
@Test

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/EnvironmentUtils.java

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import org.apache.thrift.TConfiguration;
5656
import org.apache.thrift.transport.TTransport;
5757
import org.apache.thrift.transport.TTransportException;
58-
import org.apache.tsfile.external.commons.io.FileUtils;
5958
import org.apache.tsfile.fileSystem.FSFactoryProducer;
6059
import org.apache.tsfile.utils.FilePathUtils;
6160
import org.slf4j.Logger;
@@ -69,6 +68,12 @@
6968
import java.io.IOException;
7069
import java.net.InetSocketAddress;
7170
import 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;
7277
import java.util.concurrent.TimeUnit;
7378

7479
import 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
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.db.utils;
21+
22+
import org.apache.iotdb.db.utils.constant.TestConstant;
23+
24+
import org.junit.After;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.nio.file.Files;
31+
import java.util.Collections;
32+
33+
public class EnvironmentUtilsTest {
34+
35+
private final File testDir = new File(TestConstant.BASE_OUTPUT_PATH, "EnvironmentUtilsTest");
36+
37+
@After
38+
public void tearDown() throws IOException {
39+
EnvironmentUtils.cleanDir(testDir.getPath());
40+
}
41+
42+
@Test
43+
public void testCleanDirDeletesNestedDirectory() throws IOException {
44+
File nestedDir = new File(testDir, "ext" + File.separator + "udf" + File.separator + "tmp");
45+
Assert.assertTrue(nestedDir.isDirectory() || nestedDir.mkdirs());
46+
Files.write(new File(nestedDir, "plugin.txt").toPath(), Collections.singletonList("plugin"));
47+
48+
EnvironmentUtils.cleanDir(testDir.getPath());
49+
50+
Assert.assertFalse(testDir.exists());
51+
}
52+
}

iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/FileUtilsTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ public void setUp() throws Exception {
4646

4747
@After
4848
public void tearDown() throws Exception {
49-
tmpDir.delete();
50-
targetDir.delete();
49+
if (tmpDir != null) {
50+
FileUtils.deleteFileOrDirectory(tmpDir, true);
51+
}
52+
if (targetDir != null) {
53+
FileUtils.deleteFileOrDirectory(targetDir, true);
54+
}
5155
}
5256

5357
@Test

0 commit comments

Comments
 (0)