Skip to content

Commit 5af58ed

Browse files
authored
Improve logging for S3RetryingInputStream (#104892)
* Improve logging for S3RetryingInputStream This PR adds a logging message when opening or read succeeds after retries. See also #103300 (comment) It also makes the logging messages for retries exponentially less so that the log size is more scalable when there are many threads retrying. See also #103300 (comment) Relates: #103300
1 parent 5138cfc commit 5af58ed

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class S3RetryingInputStream extends InputStream {
7777
this.failures = new ArrayList<>(MAX_SUPPRESSED_EXCEPTIONS);
7878
this.start = start;
7979
this.end = end;
80+
final int initialAttempt = attempt;
8081
openStreamWithRetry();
82+
maybeLogForSuccessAfterRetries(initialAttempt, "opened");
8183
}
8284

8385
private void openStreamWithRetry() throws IOException {
@@ -130,14 +132,16 @@ private long getStreamLength(final S3Object object) {
130132
@Override
131133
public int read() throws IOException {
132134
ensureOpen();
135+
final int initialAttempt = attempt;
133136
while (true) {
134137
try {
135138
final int result = currentStream.read();
136139
if (result == -1) {
137140
eof = true;
138-
return -1;
141+
} else {
142+
currentOffset += 1;
139143
}
140-
currentOffset += 1;
144+
maybeLogForSuccessAfterRetries(initialAttempt, "read");
141145
return result;
142146
} catch (IOException e) {
143147
reopenStreamOrFail(e);
@@ -148,14 +152,16 @@ public int read() throws IOException {
148152
@Override
149153
public int read(byte[] b, int off, int len) throws IOException {
150154
ensureOpen();
155+
final int initialAttempt = attempt;
151156
while (true) {
152157
try {
153158
final int bytesRead = currentStream.read(b, off, len);
154159
if (bytesRead == -1) {
155160
eof = true;
156-
return -1;
161+
} else {
162+
currentOffset += bytesRead;
157163
}
158-
currentOffset += bytesRead;
164+
maybeLogForSuccessAfterRetries(initialAttempt, "read");
159165
return bytesRead;
160166
} catch (IOException e) {
161167
reopenStreamOrFail(e);
@@ -192,8 +198,8 @@ private <T extends Exception> long maybeLogAndComputeRetryDelay(String action, T
192198
throw finalException;
193199
}
194200

195-
// Log at info level for the 1st retry and every ~5 minutes afterward
196-
logForRetry((attempt == 1 || attempt % 30 == 0) ? Level.INFO : Level.DEBUG, action, e);
201+
// Log at info level for the 1st retry and then exponentially less
202+
logForRetry(Integer.bitCount(attempt) == 1 ? Level.INFO : Level.DEBUG, action, e);
197203
if (failures.size() < MAX_SUPPRESSED_EXCEPTIONS) {
198204
failures.add(e);
199205
}
@@ -239,6 +245,19 @@ private void logForRetry(Level level, String action, Exception e) {
239245
);
240246
}
241247

248+
private void maybeLogForSuccessAfterRetries(int initialAttempt, String action) {
249+
if (attempt > initialAttempt) {
250+
logger.info(
251+
"successfully {} input stream for [{}/{}] with purpose [{}] after [{}] retries",
252+
action,
253+
blobStore.bucket(),
254+
blobKey,
255+
purpose,
256+
attempt - initialAttempt
257+
);
258+
}
259+
}
260+
242261
private long currentStreamProgress() {
243262
return Math.subtractExact(Math.addExact(start, currentOffset), currentStreamFirstOffset);
244263
}

0 commit comments

Comments
 (0)