Skip to content

Commit f0cc476

Browse files
probakowskimartijnvg
authored andcommitted
Fix clean up of old entries in DatabaseRegistry.initialize (elastic#70135)
This change switches clean up in DatabaseRegistry.initialize from using Files.walk and stream operations to Files.walkFileTree which can be made more robust in case of errors
1 parent 4f3e144 commit f0cc476

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseRegistry.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232

3333
import java.io.Closeable;
3434
import java.io.IOException;
35-
import java.io.UncheckedIOException;
3635
import java.nio.file.FileAlreadyExistsException;
36+
import java.nio.file.FileVisitResult;
37+
import java.nio.file.FileVisitor;
3738
import java.nio.file.Files;
39+
import java.nio.file.NoSuchFileException;
3840
import java.nio.file.Path;
3941
import java.nio.file.StandardCopyOption;
4042
import java.nio.file.StandardOpenOption;
43+
import java.nio.file.attribute.BasicFileAttributes;
4144
import java.security.MessageDigest;
4245
import java.util.ArrayList;
4346
import java.util.Collection;
@@ -104,18 +107,37 @@ final class DatabaseRegistry implements Closeable {
104107

105108
public void initialize(ResourceWatcherService resourceWatcher, IngestService ingestService) throws IOException {
106109
localDatabases.initialize(resourceWatcher);
107-
if (Files.exists(geoipTmpDirectory)) {
108-
Files.walk(geoipTmpDirectory)
109-
.filter(Files::isRegularFile)
110-
.peek(path -> LOGGER.info("deleting stale file [{}]", path))
111-
.forEach(path -> {
112-
try {
113-
Files.delete(path);
114-
} catch (IOException e) {
115-
throw new UncheckedIOException(e);
116-
}
117-
});
118-
} else {
110+
Files.walkFileTree(geoipTmpDirectory, new FileVisitor<Path>() {
111+
@Override
112+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
113+
return FileVisitResult.CONTINUE;
114+
}
115+
116+
@Override
117+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
118+
try {
119+
LOGGER.info("deleting stale file [{}]", file);
120+
Files.deleteIfExists(file);
121+
} catch (IOException e) {
122+
LOGGER.warn("can't delete stale file [" + file + "]", e);
123+
}
124+
return FileVisitResult.CONTINUE;
125+
}
126+
127+
@Override
128+
public FileVisitResult visitFileFailed(Path file, IOException e) {
129+
if(e instanceof NoSuchFileException == false) {
130+
LOGGER.warn("can't delete stale file [" + file + "]", e);
131+
}
132+
return FileVisitResult.CONTINUE;
133+
}
134+
135+
@Override
136+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
137+
return FileVisitResult.CONTINUE;
138+
}
139+
});
140+
if (Files.exists(geoipTmpDirectory) == false) {
119141
Files.createDirectory(geoipTmpDirectory);
120142
}
121143
LOGGER.info("initialized database registry, using geoip-databases directory [{}]", geoipTmpDirectory);

0 commit comments

Comments
 (0)