From 6f7e71cc76e9a83b3a80f3694fe4e1315eaa691a Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Fri, 15 Nov 2019 16:26:58 +0100 Subject: [PATCH] Make FsBlobContainer Listing Resilient to Concurrent Modifications (#49142) * Make FsBlobContainer Listing Resilient to Concurrent Modifications If we list out files in a folder via the lazily computed directory stream, we have to deal with concurrent deletes when reading the file attributes since we don't have a lock on the directory in any way. Closes #37581 --- .../common/blobstore/fs/FsBlobContainer.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java b/server/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java index 9dbb46913c202..74d8a167de3aa 100644 --- a/server/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java +++ b/server/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java @@ -97,7 +97,13 @@ public Map listBlobsByPrefix(String blobNamePrefix) throws blobNamePrefix = blobNamePrefix == null ? "" : blobNamePrefix; try (DirectoryStream stream = Files.newDirectoryStream(path, blobNamePrefix + "*")) { for (Path file : stream) { - final BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class); + final BasicFileAttributes attrs; + try { + attrs = Files.readAttributes(file, BasicFileAttributes.class); + } catch (FileNotFoundException | NoSuchFileException e) { + // The file was concurrently deleted between listing files and trying to get its attributes so we skip it here + continue; + } if (attrs.isRegularFile()) { builder.put(file.getFileName().toString(), new PlainBlobMetaData(file.getFileName().toString(), attrs.size())); }