Browse Source

Remove Mostly Redundant Deleting in FsBlobContainer (#60117)

In almost all cases we write uuid named files via this method.
Preemptively deleting just wastes IO ops, we can delete after a write failed
and retry the write to cover the few cases where we actually do an overwrite.
Armin Braun 5 years ago
parent
commit
94fdece88a

+ 8 - 3
server/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java

@@ -176,11 +176,16 @@ public class FsBlobContainer extends AbstractBlobContainer {
 
     @Override
     public void writeBlob(String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) throws IOException {
-        if (failIfAlreadyExists == false) {
+        final Path file = path.resolve(blobName);
+        try {
+            writeToPath(inputStream, file, blobSize);
+        } catch (FileAlreadyExistsException faee) {
+            if (failIfAlreadyExists) {
+                throw faee;
+            }
             deleteBlobsIgnoringIfNotExists(Collections.singletonList(blobName));
+            writeToPath(inputStream, file, blobSize);
         }
-        final Path file = path.resolve(blobName);
-        writeToPath(inputStream, file, blobSize);
         IOUtils.fsync(path, true);
     }