Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include full blob key in Azure read operations exceptions #75920

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
Expand All @@ -40,6 +41,7 @@
import java.util.regex.Pattern;

import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

@SuppressForbidden(reason = "this test uses a HttpServer to emulate an Azure endpoint")
Expand Down Expand Up @@ -243,4 +245,12 @@ public void testDeleteBlobsIgnoringIfNotExists() throws Exception {
assertThat(container.listBlobs(), is(anEmptyMap()));
}
}

public void testNotFoundErrorMessageContainsFullKey() throws Exception {
try (BlobStore store = newBlobStore()) {
BlobContainer container = store.blobContainer(BlobPath.EMPTY.add("nested").add("dir"));
NoSuchFileException exception = expectThrows(NoSuchFileException.class, () -> container.readBlob("blob"));
assertThat(exception.getMessage(), equalTo("Blob [nested/dir/blob] not found"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public boolean blobExists(String blobName) throws IOException {
}

private InputStream openInputStream(String blobName, long position, @Nullable Long length) throws IOException {
String blobKey = buildKey(blobName);
logger.trace("readBlob({}) from position [{}] with length [{}]", blobName, position, length != null ? length : "unlimited");
if (blobStore.getLocationMode() == LocationMode.SECONDARY_ONLY && blobExists(blobName) == false) {
// On Azure, if the location path is a secondary location, and the blob does not
Expand All @@ -55,18 +56,18 @@ private InputStream openInputStream(String blobName, long position, @Nullable Lo
// before throwing a storage exception. This can cause long delays in retrieving
// snapshots, so we first check if the blob exists before trying to open an input
// stream to it.
throw new NoSuchFileException("Blob [" + blobName + "] does not exist");
throw new NoSuchFileException("Blob [" + blobKey + "] not found");
}
try {
return blobStore.getInputStream(buildKey(blobName), position, length);
return blobStore.getInputStream(blobKey, position, length);
} catch (Exception e) {
Throwable rootCause = Throwables.getRootCause(e);
if (rootCause instanceof BlobStorageException) {
if (((BlobStorageException) rootCause).getStatusCode() == 404) {
throw new NoSuchFileException("Blob [" + blobName + "] not found");
throw new NoSuchFileException("Blob [" + blobKey + "] not found");
}
}
throw new IOException("Unable to get input stream for blob [" + blobName + "]", e);
throw new IOException("Unable to get input stream for blob [" + blobKey + "]", e);
}
}

Expand Down