Skip to content

Commit efb2422

Browse files
committed
Count retries per blob not per read
1 parent 9a6f5c6 commit efb2422

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RetryingInputStream.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class S3RetryingInputStream extends InputStream {
4646
private final int maxAttempts;
4747

4848
private InputStream currentStream;
49+
private int attempt = 1;
4950
private long currentOffset;
5051

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

7677
@Override
7778
public int read() throws IOException {
78-
int attempt = 0;
7979
while (true) {
80-
attempt += 1;
8180
try {
8281
final int result = currentStream.read();
8382
currentOffset += 1;
8483
return result;
8584
} catch (IOException e) {
86-
reopenStreamOrFail(attempt, e);
85+
reopenStreamOrFail(e);
8786
}
8887
}
8988
}
9089

9190
@Override
9291
public int read(byte[] b, int off, int len) throws IOException {
93-
int attempt = 0;
9492
while (true) {
95-
attempt += 1;
9693
try {
9794
final int bytesRead = currentStream.read(b, off, len);
9895
if (bytesRead == -1) {
@@ -101,17 +98,18 @@ public int read(byte[] b, int off, int len) throws IOException {
10198
currentOffset += bytesRead;
10299
return bytesRead;
103100
} catch (IOException e) {
104-
reopenStreamOrFail(attempt, e);
101+
reopenStreamOrFail(e);
105102
}
106103
}
107104
}
108105

109-
private void reopenStreamOrFail(int attempt, IOException e) throws IOException {
106+
private void reopenStreamOrFail(IOException e) throws IOException {
110107
if (attempt >= maxAttempts) {
111108
throw e;
112109
}
113110
logger.debug(new ParameterizedMessage("failed reading [{}/{}] at offset [{}], attempt [{}] of [{}], retrying",
114111
blobStore.bucket(), blobKey, currentOffset, attempt, maxAttempts), e);
112+
attempt += 1;
115113
IOUtils.closeWhileHandlingException(currentStream);
116114
currentStream = openStream();
117115
}

0 commit comments

Comments
 (0)