-
Notifications
You must be signed in to change notification settings - Fork 68
New Downloader for transient retries #553
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 all commits
bc2df9e
d76619a
062791b
ad02cba
a0d7e44
f931f27
9569e69
f633e83
973c1a8
efc02a7
d475d4d
2c9f853
19344a7
ae4f536
ba58069
169a9af
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,22 +6,40 @@ using HTTP.MessageRequest: body_was_streamed | |||||
| This backend uses the Downloads.jl stdlib to use libcurl | ||||||
| as an HTTP client to connect to the AWS REST API. | ||||||
|
|
||||||
| It has one field, | ||||||
|
|
||||||
| - `downloader::Union{Nothing,Downloads.Downloader}` | ||||||
|
|
||||||
| which is the `Downloads.Downloader` to use. If set to `nothing`, the default, | ||||||
| then a global downloader object will be used. | ||||||
|
|
||||||
| Downloads.jl tends to perform better under concurrent operation than HTTP.jl, | ||||||
| particularly with `@async` / `asyncmap`. As of March 2022, threading (e.g. `@spawn` or `@threads`) with Downloads.jl is broken on all releases of Julia ([Downloads.jl#110](https://github.com/JuliaLang/Downloads.jl/issues/110)), and there are still reported issues on the upcoming | ||||||
| 1.7.3 and 1.8 releases ([Downloads.jl#182](https://github.com/JuliaLang/Downloads.jl/issues/182])). | ||||||
|
|
||||||
| There are three constructors: | ||||||
|
|
||||||
| DownloadsBackend() | ||||||
|
|
||||||
| This constructor uses AWS.jl's global downloader object. If a transient error occurs, the global downloader object is replaced with a fresh one. | ||||||
|
|
||||||
| DownloadsBackend(create_new_downloader) | ||||||
|
|
||||||
| This constructor calls `create_new_downloader()` to create a new downloader which it uses. Upon encountering a transient error, another new downloader is created and used from then on. | ||||||
|
|
||||||
| DownloadsBackend(D::Downloader) | ||||||
|
|
||||||
| This constructor uses the provided downloader `D`, and uses it always. | ||||||
| """ | ||||||
| struct DownloadsBackend <: AWS.AbstractBackend | ||||||
| mutable struct DownloadsBackend <: AWS.AbstractBackend | ||||||
| downloader::Union{Nothing,Downloads.Downloader} | ||||||
| # `downloader_lock===nothing` signals that we don't want to replace | ||||||
| # the existing `downloader` field. | ||||||
| downloader_lock::Union{Nothing,ReentrantLock} | ||||||
| create_new_downloader::Any | ||||||
| end | ||||||
|
|
||||||
| DownloadsBackend() = DownloadsBackend(nothing) | ||||||
| # Use global downloader; don't replace `downloader` field on transient errors | ||||||
| DownloadsBackend() = DownloadsBackend(nothing, nothing, () -> get_downloader(; fresh=true)) | ||||||
| # Use `create_new_downloader` to create a new `downloader`; DO replace the `downloader` field on transient errors | ||||||
| function DownloadsBackend(create_new_downloader) | ||||||
| return DownloadsBackend(create_new_downloader(), ReentrantLock(), create_new_downloader) | ||||||
| end | ||||||
| # Use provided downloader `D`; don't replace `downloader` on transient errors | ||||||
| DownloadsBackend(D::Downloader) = DownloadsBackend(D, nothing, () -> D) | ||||||
|
|
||||||
| const AWS_DOWNLOADER = Ref{Union{Nothing,Downloader}}(nothing) | ||||||
|
Member
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. Not to derail this PR but using undef when the downloader is not yet defined seems preferable
Suggested change
You'd just need to change some code in
Member
Author
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. Let's do that separately if we are going to change it. I vaguely remember some problems with that approach when trying it in the original implementation. Maybe it was useful to be able to reset back to nothing, or something like that... |
||||||
| const AWS_DOWNLOAD_LOCK = ReentrantLock() | ||||||
|
|
@@ -31,13 +49,13 @@ const AWS_DOWNLOAD_LOCK = ReentrantLock() | |||||
| # because we add a hook to avoid redirects in order to try to match the HTTPBackend's | ||||||
| # implementation, and we don't want to mutate the global downloader from Downloads.jl. | ||||||
| # https://github.com/JuliaLang/Downloads.jl/blob/84e948c02b8a0625552a764bf90f7d2ee97c949c/src/Downloads.jl#L293-L301 | ||||||
| function get_downloader(downloader=nothing) | ||||||
| function get_downloader(; fresh=false) | ||||||
| downloader = nothing | ||||||
| lock(AWS_DOWNLOAD_LOCK) do | ||||||
| yield() # let other downloads finish | ||||||
| downloader isa Downloader && return nothing | ||||||
| while true | ||||||
|
omus marked this conversation as resolved.
|
||||||
| downloader = AWS_DOWNLOADER[] | ||||||
| downloader isa Downloader && return nothing | ||||||
| !fresh && downloader isa Downloader && return nothing | ||||||
| D = Downloader() | ||||||
| D.easy_hook = | ||||||
| (easy, info) -> Curl.setopt(easy, Curl.CURLOPT_FOLLOWLOCATION, false) | ||||||
|
|
@@ -57,14 +75,27 @@ function read_body(x::IO) | |||||
| return read(x) | ||||||
| end | ||||||
|
|
||||||
| function _http_request(backend::DownloadsBackend, request::Request, response_stream::IO) | ||||||
| function _http_request( | ||||||
| backend::DownloadsBackend, request::Request, response_stream::IO; transient_retry=false | ||||||
| ) | ||||||
| # HTTP.jl sets this header automatically. | ||||||
| request.headers["Content-Length"] = string(body_length(request.content)) | ||||||
|
|
||||||
| # We pass an `input` only when we have content we wish to send. | ||||||
| input = !isempty(request.content) ? IOBuffer(request.content) : nothing | ||||||
|
|
||||||
| downloader = @something(backend.downloader, get_downloader()) | ||||||
| if transient_retry | ||||||
| downloader = backend.create_new_downloader() | ||||||
|
|
||||||
| # If we have a lock, use it to replace our existing downloader. | ||||||
| if backend.downloader_lock !== nothing | ||||||
| Base.@lock backend.downloader_lock begin | ||||||
| backend.downloader = downloader | ||||||
| end | ||||||
| end | ||||||
| else | ||||||
| downloader = @something(backend.downloader, get_downloader()) | ||||||
| end | ||||||
|
|
||||||
| # set the hook so that we don't follow redirects. Only | ||||||
| # need to do this on per-request downloaders, because we | ||||||
|
|
@@ -78,6 +109,18 @@ function _http_request(backend::DownloadsBackend, request::Request, response_str | |||||
| local response | ||||||
|
|
||||||
| check = function (s, e) | ||||||
| if is_transient_error(e) | ||||||
|
|
||||||
| # TODO: remove this restriction. These valid transient errors | ||||||
| # are currently not supported by our tests. | ||||||
| if (isa(e, HTTP.StatusError) && AWS._http_status(e) < 500) | ||||||
| return false | ||||||
| end | ||||||
|
|
||||||
| # We want a new one, ref https://github.com/JuliaCloud/AWS.jl/issues/552 | ||||||
| downloader = backend.create_new_downloader() | ||||||
| return true | ||||||
| end | ||||||
| return (isa(e, HTTP.StatusError) && AWS._http_status(e) >= 500) || | ||||||
| isa(e, Downloads.RequestError) | ||||||
| end | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,16 +100,23 @@ function submit_request(aws::AbstractAWSConfig, request::Request; return_headers | |
| TOO_MANY_REQUESTS = 429 | ||
| EXPIRED_ERROR_CODES = ["ExpiredToken", "ExpiredTokenException", "RequestExpired"] | ||
| REDIRECT_ERROR_CODES = [301, 302, 303, 304, 305, 307, 308] | ||
|
|
||
| # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html?highlight=retry | ||
| # https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-retries.html#cli-usage-retries-modes-standard.title | ||
| THROTTLING_ERROR_CODES = [ | ||
| "Throttling", | ||
| "ThrottlingException", | ||
| "ThrottledException", | ||
| "RequestThrottledException", | ||
| "TooManyRequestsException", | ||
| "ProvisionedThroughputExceededException", | ||
| "TransactionInProgressException", | ||
| "RequestLimitExceeded", | ||
| "BandwidthLimitExceeded", | ||
| "LimitExceededException", | ||
| "RequestThrottled", | ||
| "PriorRequestNotComplete", | ||
|
Member
Author
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. This is a transient error, not a throttling one, according to https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-retries.html#cli-usage-retries-modes-standard.title However, in this PR only Downloads backend is getting special transient error handling, so maybe I should add this back with a comment, so we don't mess up the HTTP behavior.
Member
Author
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. I added it later on as a separate check |
||
| "SlowDown", | ||
| "EC2ThrottledException", | ||
| ] | ||
|
|
||
| request.headers["User-Agent"] = user_agent[] | ||
|
|
@@ -119,11 +126,15 @@ function submit_request(aws::AbstractAWSConfig, request::Request; return_headers | |
| local aws_response | ||
| local response | ||
|
|
||
| transient_retry = false | ||
|
|
||
| get_response = | ||
| () -> begin | ||
| credentials(aws) === nothing || sign!(aws, request) | ||
|
|
||
| aws_response = @mock _http_request(request.backend, request, stream) | ||
| aws_response = @mock _http_request( | ||
| request.backend, request, stream; transient_retry=transient_retry | ||
| ) | ||
| response = aws_response.response | ||
|
|
||
| if response.status in REDIRECT_ERROR_CODES | ||
|
|
@@ -153,6 +164,10 @@ function submit_request(aws::AbstractAWSConfig, request::Request; return_headers | |
| if !(e isa AWSException) | ||
| return false | ||
| end | ||
| if is_transient_error(e) | ||
| transient_retry = true | ||
| return true | ||
| end | ||
|
|
||
| occursin("Signature expired", e.message) && return true | ||
|
|
||
|
|
@@ -170,6 +185,12 @@ function submit_request(aws::AbstractAWSConfig, request::Request; return_headers | |
| return true | ||
| end | ||
|
|
||
| if e.code == "PriorRequestNotComplete" | ||
| # Retry this transient error, because the | ||
| # HTTP backend currently doesn't have a check for it. | ||
| return true | ||
| end | ||
|
|
||
| # Handle BadDigest error and CRC32 check sum failure | ||
| if _header(e.cause, "crc32body") == "x-amz-crc32" || | ||
| e.code in ("BadDigest", "RequestTimeout", "RequestTimeoutException") | ||
|
|
@@ -198,7 +219,9 @@ function submit_request(aws::AbstractAWSConfig, request::Request; return_headers | |
| end | ||
| end | ||
|
|
||
| function _http_request(http_backend::HTTPBackend, request::Request, response_stream::IO) | ||
| function _http_request( | ||
| http_backend::HTTPBackend, request::Request, response_stream::IO; transient_retry=false | ||
| ) | ||
| http_options = merge(http_backend.http_options, request.http_options) | ||
|
|
||
| # HTTP options such as `status_exception` need to be used when creating the stack | ||
|
|
||
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.
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.
That isn't documented; is it really better than
Any?