Skip to content

Commit

Permalink
Count retries per blob not per read
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveCTurner committed Sep 12, 2019
1 parent 9a6f5c6 commit efb2422
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class S3RetryingInputStream extends InputStream {
private final int maxAttempts;

private InputStream currentStream;
private int attempt = 1;
private long currentOffset;

S3RetryingInputStream(S3BlobStore blobStore, String blobKey) throws IOException {
Expand Down Expand Up @@ -75,24 +76,20 @@ private InputStream openStream() throws IOException {

@Override
public int read() throws IOException {
int attempt = 0;
while (true) {
attempt += 1;
try {
final int result = currentStream.read();
currentOffset += 1;
return result;
} catch (IOException e) {
reopenStreamOrFail(attempt, e);
reopenStreamOrFail(e);
}
}
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
int attempt = 0;
while (true) {
attempt += 1;
try {
final int bytesRead = currentStream.read(b, off, len);
if (bytesRead == -1) {
Expand All @@ -101,17 +98,18 @@ public int read(byte[] b, int off, int len) throws IOException {
currentOffset += bytesRead;
return bytesRead;
} catch (IOException e) {
reopenStreamOrFail(attempt, e);
reopenStreamOrFail(e);
}
}
}

private void reopenStreamOrFail(int attempt, IOException e) throws IOException {
private void reopenStreamOrFail(IOException e) throws IOException {
if (attempt >= maxAttempts) {
throw e;
}
logger.debug(new ParameterizedMessage("failed reading [{}/{}] at offset [{}], attempt [{}] of [{}], retrying",
blobStore.bucket(), blobKey, currentOffset, attempt, maxAttempts), e);
attempt += 1;
IOUtils.closeWhileHandlingException(currentStream);
currentStream = openStream();
}
Expand Down

0 comments on commit efb2422

Please sign in to comment.