Skip to content

Commit 196fb16

Browse files
committed
Fix a flaky test with Files.walk()
Fixed formatting issue with extra empty line Signed-off-by: Joe Liu <[email protected]>
1 parent 0c89456 commit 196fb16

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

server/src/internalClusterTest/java/org/opensearch/index/shard/IndexShardIT.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@
102102
import java.io.IOException;
103103
import java.io.UncheckedIOException;
104104
import java.nio.file.DirectoryStream;
105+
import java.nio.file.FileVisitResult;
105106
import java.nio.file.Files;
107+
import java.nio.file.NoSuchFileException;
106108
import java.nio.file.Path;
109+
import java.nio.file.SimpleFileVisitor;
110+
import java.nio.file.attribute.BasicFileAttributes;
107111
import java.util.ArrayList;
108112
import java.util.Arrays;
109113
import java.util.Collection;
@@ -334,18 +338,36 @@ public void testIndexCanChangeCustomDataPath() throws Exception {
334338

335339
logger.info("--> copying data on disk from [{}] to [{}]", indexDataPath, newIndexDataPath);
336340
assert Files.exists(newIndexDataPath) == false : "new index data path directory should not exist!";
337-
try (Stream<Path> stream = Files.walk(indexDataPath)) {
338-
stream.forEach(path -> {
339-
try {
340-
if (path.endsWith(".lock") == false) {
341-
Files.copy(path, newIndexDataPath.resolve(indexDataPath.relativize(path)));
341+
Files.walkFileTree(indexDataPath, new SimpleFileVisitor<Path>() {
342+
@Override
343+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
344+
Path targetDir = newIndexDataPath.resolve(indexDataPath.relativize(dir));
345+
Files.createDirectories(targetDir);
346+
return FileVisitResult.CONTINUE;
347+
}
348+
349+
@Override
350+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
351+
if (file.getFileName().toString().endsWith(".lock") == false) {
352+
Path target = newIndexDataPath.resolve(indexDataPath.relativize(file));
353+
try {
354+
Files.copy(file, target);
355+
} catch (NoSuchFileException e) {
356+
logger.error("Failed to copy data path directory", e);
357+
fail();
342358
}
343-
} catch (final Exception e) {
344-
logger.error("Failed to copy data path directory", e);
345-
fail();
346359
}
347-
});
348-
}
360+
return FileVisitResult.CONTINUE;
361+
}
362+
363+
@Override
364+
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
365+
if (exc instanceof NoSuchFileException) {
366+
return FileVisitResult.CONTINUE;
367+
}
368+
return FileVisitResult.TERMINATE;
369+
}
370+
});
349371

350372
logger.info("--> updating data_path to [{}] for index [{}]", newIndexDataPath, index);
351373
// update data path and re-enable refresh

0 commit comments

Comments
 (0)