-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Retry S3BlobContainer#getRegister
on all exceptions
#114813
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
Changes from 2 commits
33195c9
0158c9b
6b5e4f0
b4cc1c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 114813 | ||
summary: Retry `S3BlobContainer#getRegister` on all exceptions | ||
area: Snapshot/Restore | ||
type: enhancement | ||
issues: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
import org.elasticsearch.action.support.SubscribableListener; | ||
import org.elasticsearch.action.support.ThreadedActionListener; | ||
import org.elasticsearch.cluster.service.MasterService; | ||
import org.elasticsearch.common.BackoffPolicy; | ||
import org.elasticsearch.common.Randomness; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.blobstore.BlobContainer; | ||
|
@@ -910,21 +911,43 @@ public void compareAndExchangeRegister( | |
@Override | ||
public void getRegister(OperationPurpose purpose, String key, ActionListener<OptionalBytesReference> listener) { | ||
ActionListener.completeWith(listener, () -> { | ||
final var getObjectRequest = new GetObjectRequest(blobStore.bucket(), buildKey(key)); | ||
S3BlobStore.configureRequestForMetrics(getObjectRequest, blobStore, Operation.GET_OBJECT, purpose); | ||
try ( | ||
var clientReference = blobStore.clientReference(); | ||
var s3Object = SocketAccess.doPrivileged(() -> clientReference.client().getObject(getObjectRequest)); | ||
var stream = s3Object.getObjectContent() | ||
) { | ||
return OptionalBytesReference.of(getRegisterUsingConsistentRead(stream, keyPath, key)); | ||
} catch (AmazonS3Exception e) { | ||
logger.trace(() -> Strings.format("[%s]: getRegister failed", key), e); | ||
if (e.getStatusCode() == 404) { | ||
return OptionalBytesReference.EMPTY; | ||
} else { | ||
throw e; | ||
final var backoffPolicy = purpose == OperationPurpose.REPOSITORY_ANALYSIS | ||
? BackoffPolicy.noBackoff() | ||
: BackoffPolicy.constantBackoff(blobStore.getGetRegisterRetryDelay(), blobStore.getMaxRetries()); | ||
final var retryDelayIterator = backoffPolicy.iterator(); | ||
|
||
Exception finalException = null; | ||
while (true) { | ||
final var getObjectRequest = new GetObjectRequest(blobStore.bucket(), buildKey(key)); | ||
S3BlobStore.configureRequestForMetrics(getObjectRequest, blobStore, Operation.GET_OBJECT, purpose); | ||
try ( | ||
var clientReference = blobStore.clientReference(); | ||
var s3Object = SocketAccess.doPrivileged(() -> clientReference.client().getObject(getObjectRequest)); | ||
var stream = s3Object.getObjectContent() | ||
) { | ||
return OptionalBytesReference.of(getRegisterUsingConsistentRead(stream, keyPath, key)); | ||
} catch (Exception attemptException) { | ||
logger.trace(() -> Strings.format("[%s]: getRegister failed", key), attemptException); | ||
if (attemptException instanceof AmazonS3Exception amazonS3Exception && amazonS3Exception.getStatusCode() == 404) { | ||
return OptionalBytesReference.EMPTY; | ||
} else if (finalException == null) { | ||
finalException = attemptException; | ||
} else if (finalException != attemptException) { | ||
finalException.addSuppressed(attemptException); | ||
} | ||
} | ||
if (retryDelayIterator.hasNext()) { | ||
try { | ||
// noinspection BusyWait | ||
Thread.sleep(retryDelayIterator.next().millis()); | ||
continue; | ||
} catch (InterruptedException interruptedException) { | ||
Thread.currentThread().interrupt(); | ||
finalException.addSuppressed(interruptedException); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we break out of the loop if we're interrupted? I'm guessing something else in the loop will throw an InterruptedException if we continue, but would it be more polite to bail early? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we do break out here, we fall through to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes |
||
} | ||
} | ||
|
||
throw finalException; | ||
} | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it possible that
finalException == attemptException
? does something we call throw a singleton exception? I'm not sure I follow this condition.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who knows what the SDK does or might do in future, but calling
addSuppressed
with the same exception throws anIllegalArgumentException
and we don't want that here.