Skip to content

Commit c4ae23f

Browse files
author
Ali Beyad
authored
Enables implementations of the BlobContainer interface to (#19749)
conform with the requirements of the writeBlob method by throwing a FileAlreadyExistsException if attempting to write to a blob that already exists. This change means implementations of BlobContainer should never overwrite blobs - to overwrite a blob, it must first be deleted and then can be written again. Closes #15579
1 parent c0b71d2 commit c4ae23f

File tree

7 files changed

+23
-3
lines changed

7 files changed

+23
-3
lines changed

core/src/main/java/org/elasticsearch/common/blobstore/BlobContainer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.io.InputStream;
24+
import java.nio.file.FileAlreadyExistsException;
2425
import java.nio.file.NoSuchFileException;
2526
import java.util.Map;
2627

@@ -68,8 +69,8 @@ public interface BlobContainer {
6869
* @param blobSize
6970
* The size of the blob to be written, in bytes. It is implementation dependent whether
7071
* this value is used in writing the blob to the repository.
71-
* @throws IOException if the input stream could not be read, a blob by the same name already exists,
72-
* or the target blob could not be written to.
72+
* @throws FileAlreadyExistsException if a blob by the same name already exists
73+
* @throws IOException if the input stream could not be read, or the target blob could not be written to.
7374
*/
7475
void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException;
7576

core/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.InputStream;
3333
import java.io.OutputStream;
3434
import java.nio.file.DirectoryStream;
35+
import java.nio.file.FileAlreadyExistsException;
3536
import java.nio.file.Files;
3637
import java.nio.file.NoSuchFileException;
3738
import java.nio.file.Path;
@@ -108,6 +109,9 @@ public InputStream readBlob(String name) throws IOException {
108109

109110
@Override
110111
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
112+
if (blobExists(blobName)) {
113+
throw new FileAlreadyExistsException("blob [" + blobName + "] already exists, cannot overwrite");
114+
}
111115
final Path file = path.resolve(blobName);
112116
try (OutputStream outputStream = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW)) {
113117
Streams.copy(inputStream, outputStream, new byte[blobStore.bufferSizeInBytes()]);

plugins/repository-azure/src/main/java/org/elasticsearch/cloud/azure/blobstore/AzureBlobContainer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.OutputStream;
3535
import java.net.HttpURLConnection;
3636
import java.net.URISyntaxException;
37+
import java.nio.file.FileAlreadyExistsException;
3738
import java.nio.file.NoSuchFileException;
3839
import java.util.Map;
3940

@@ -84,6 +85,9 @@ public InputStream readBlob(String blobName) throws IOException {
8485

8586
@Override
8687
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
88+
if (blobExists(blobName)) {
89+
throw new FileAlreadyExistsException("blob [" + blobName + "] already exists, cannot overwrite");
90+
}
8791
logger.trace("writeBlob({}, stream, {})", blobName, blobSize);
8892
try (OutputStream stream = createOutput(blobName)) {
8993
Streams.copy(inputStream, stream);

plugins/repository-gcs/src/main/java/org/elasticsearch/common/blobstore/gcs/GoogleCloudStorageBlobContainer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.IOException;
2828
import java.io.InputStream;
29+
import java.nio.file.FileAlreadyExistsException;
2930
import java.util.Map;
3031

3132
public class GoogleCloudStorageBlobContainer extends AbstractBlobContainer {
@@ -65,6 +66,9 @@ public InputStream readBlob(String blobName) throws IOException {
6566

6667
@Override
6768
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
69+
if (blobExists(blobName)) {
70+
throw new FileAlreadyExistsException("blob [" + blobName + "] already exists, cannot overwrite");
71+
}
6872
blobStore.writeBlob(buildKey(blobName), inputStream, blobSize);
6973
}
7074

plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsBlobContainer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.io.IOException;
3636
import java.io.InputStream;
37+
import java.nio.file.FileAlreadyExistsException;
3738
import java.nio.file.NoSuchFileException;
3839
import java.util.Collections;
3940
import java.util.EnumSet;
@@ -107,6 +108,9 @@ public InputStream run(FileContext fileContext) throws IOException {
107108

108109
@Override
109110
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
111+
if (blobExists(blobName)) {
112+
throw new FileAlreadyExistsException("blob [" + blobName + "] already exists, cannot overwrite");
113+
}
110114
store.execute(new Operation<Void>() {
111115
@Override
112116
public Void run(FileContext fileContext) throws IOException {

plugins/repository-s3/src/main/java/org/elasticsearch/cloud/aws/blobstore/S3BlobContainer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.io.IOException;
4040
import java.io.InputStream;
4141
import java.io.OutputStream;
42+
import java.nio.file.FileAlreadyExistsException;
4243
import java.nio.file.NoSuchFileException;
4344
import java.security.AccessController;
4445
import java.security.PrivilegedActionException;
@@ -100,6 +101,9 @@ public InputStream readBlob(String blobName) throws IOException {
100101

101102
@Override
102103
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
104+
if (blobExists(blobName)) {
105+
throw new FileAlreadyExistsException("blob [" + blobName + "] already exists, cannot overwrite");
106+
}
103107
try (OutputStream stream = createOutput(blobName)) {
104108
Streams.copy(inputStream, stream);
105109
}

test/framework/src/main/java/org/elasticsearch/repositories/ESBlobStoreContainerTestCase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ public void testDeleteBlob() throws IOException {
128128
}
129129
}
130130

131-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/15579")
132131
public void testVerifyOverwriteFails() throws IOException {
133132
try (final BlobStore store = newBlobStore()) {
134133
final String blobName = "foobar";

0 commit comments

Comments
 (0)