Skip to content

Commit 077b42f

Browse files
committed
Remove extra check for object existence in repository-gcs read object
1 parent 7a76e3a commit 077b42f

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.Map;
5555
import java.util.stream.Collectors;
5656

57+
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
5758
import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
5859

5960
class GoogleCloudStorageBlobStore extends AbstractComponent implements BlobStore {
@@ -163,16 +164,19 @@ boolean blobExists(String blobName) throws IOException {
163164
*/
164165
InputStream readBlob(String blobName) throws IOException {
165166
final BlobId blobId = BlobId.of(bucketName, blobName);
166-
final Blob blob = SocketAccess.doPrivilegedIOException(() -> client().get(blobId));
167-
if (blob == null) {
168-
throw new NoSuchFileException("Blob [" + blobName + "] does not exit");
169-
}
170-
final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(blob::reader);
167+
final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(() -> client().reader(blobId));
171168
return Channels.newInputStream(new ReadableByteChannel() {
172169
@SuppressForbidden(reason = "Channel is based of a socket not a file")
173170
@Override
174171
public int read(ByteBuffer dst) throws IOException {
175-
return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst));
172+
try {
173+
return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst));
174+
} catch (StorageException e) {
175+
if (e.getCode() == HTTP_NOT_FOUND) {
176+
throw new NoSuchFileException("Blob [" + blobName + "] does not exist");
177+
}
178+
throw e;
179+
}
176180
}
177181

178182
@Override

plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/MockStorage.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,27 @@ public Iterable<Blob> getValues() {
167167
public ReadChannel reader(BlobId blob, BlobSourceOption... options) {
168168
if (bucketName.equals(blob.getBucket())) {
169169
final byte[] bytes = blobs.get(blob.getName());
170-
final ReadableByteChannel readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bytes));
170+
171+
final ReadableByteChannel readableByteChannel;
172+
if (bytes != null) {
173+
readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bytes));
174+
} else {
175+
readableByteChannel = new ReadableByteChannel() {
176+
@Override
177+
public int read(ByteBuffer dst) throws IOException {
178+
throw new StorageException(404, "Object not found");
179+
}
180+
181+
@Override
182+
public boolean isOpen() {
183+
return false;
184+
}
185+
186+
@Override
187+
public void close() throws IOException {
188+
}
189+
};
190+
}
171191
return new ReadChannel() {
172192
@Override
173193
public void close() {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public abstract class ESBlobStoreContainerTestCase extends ESTestCase {
4949
public void testReadNonExistingPath() throws IOException {
5050
try(BlobStore store = newBlobStore()) {
5151
final BlobContainer container = store.blobContainer(new BlobPath());
52-
expectThrows(NoSuchFileException.class, () -> container.readBlob("non-existing"));
52+
expectThrows(NoSuchFileException.class, () -> {
53+
try (InputStream is = container.readBlob("non-existing")) {
54+
is.read();
55+
}
56+
});
5357
}
5458
}
5559

0 commit comments

Comments
 (0)