Repository navigation
Api retry on 502 - #1009
Api retry on 502#1009
Conversation
update_db (download) and add_file_db (upload) now retry once, after 1 s, when the API answers 502 or 503 without a JSON body. Such an answer comes from the proxy (Traefik), e.g. when the backend closed an idle connection, so the request did not reach the backend and a retry is safe. A 503 with a JSON body comes from dds_web (maintenance). It is raised as the new ServiceUnavailableError and is not retried. The delivery stops: - the failing file is marked as failed and saved in the failed delivery log as before, so retry_add_file_db can still add uploaded files - files not started yet are cancelled (same flag as Ctrl+C) - files already running finish their current step - the error is raised once from the main thread after the workers are done, and the command exits with 1 update_db now also catches ApiResponseError, so a failed update does not stop the other downloads. Changes: - utils: perform_request_with_retry(), backend 503 -> ServiceUnavailableError - constants: API_TRANSIENT_STATUS_CODES, API_RETRY_WAIT - base: stop_for_maintenance(), raise_if_service_unavailable() - custom_decorators: cancel message says "Service unavailable" on maintenance - tests/test_api_retry.py: mocked 502/503/500/400 and maintenance cases
When another thread has seen a backend 503 (maintenance): - running downloads stop at the next chunk, the partial file is removed and no retry is made - no new download attempt is started - an upload that has finished encryption is not sent to S3, so no orphan object is left in the bucket (failed_op "put") An S3 upload that has already started is finished. If add_file_db then fails, the file is in the failed delivery log as before. Ctrl+C behaviour is unchanged.
A proxy 502/503 does not always mean that the backend did not get the request. If the first POST added the file, the retry fails because the file exists (500 from dds_web) and the file was reported as failed, although S3 and the database were fine. After a failed POST, ask file/match: if the file is in the database with the name_in_bucket of this upload, it was added by our own request and is treated as added. PUT (overwrite) is not checked.
An answer without a JSON body is now raised as NonJsonResponseError (subclass of ApiResponseError, so existing callers are not affected). perform_request_with_retry retries only this error, and only for the status codes in API_NON_JSON_RETRY_STATUS_CODES (renamed from API_TRANSIENT_STATUS_CODES). Before, the rule depended on which exception type perform_request happened to raise: any ApiResponseError with status 502/503 would have been retried, while a 503 from the DDS API (maintenance) must never be.
If maintenance (backend 503) stopped the upload, put() no longer calls retry_add_file_db and touch_project_after_upload: the API is down and these calls only add more 503 errors. The path to the failed delivery log is still shown, then the maintenance error is raised.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #1009 +/- ##
==========================================
+ Coverage 56.42% 59.86% +3.43%
==========================================
Files 33 33
Lines 3105 3179 +74
==========================================
+ Hits 1752 1903 +151
+ Misses 1353 1276 -77 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
samanrassam
left a comment
There was a problem hiding this comment.
Nice! I tested it locally with maintenance turned on mid transfer. Upload works as described, but got crash on download.
dds_cli/custom_decorators.py:48
This crashes on download: the file keys there are pathlib.Path, so escape(file) raises TypeError. escape(str(file)) fixes it (tried locally). A test with a Path key would catch it.
Some questions:
dds_cli/data_getter.py: 296
Do we need to stop downloads on maintenance? They don't need the API, and file/update only sets time_latest_download.
dds_cli/data_putter.py:172
After maintenance every remaining file is still submitted and logs a cancel warning, so a big delivery gets a looooot of lines. Should we stop scheduling once stop_doing is set? Same in get_data.
super minor and non-blocking:
dds_cli/base.py: 135
This prints "ongoing.. Stopping", since the dds_web message already ends with a period.
dds_cli/utils.py:349
This is unreachable, we could just return perform_request(*args, **kwargs) after the loop.
I think HMS-2694 is missing from the description.
this is a good catch; it was in fact an already existing bug but a download probably (almost) never reached this code. |
Download file keys are pathlib.Path objects, and rich's escape() needs a str, so the cancel message in verify_proceed raised a TypeError. Also remove the double period in the maintenance log line.
technically probably a download can succeed, but there will be error messages and confusion. And one can ask the question why have maintenance mode at all.
Good point, I noticed this too and thought it can be better, but maybe better to do in a separate task/PR |
The test compared against "dir/file.bin", but str(Path) uses the OS separator, so on Windows the log shows "dir\file.bin".
strange, I get:
and
|
The raise after the loop could not be reached. Now the loop only runs the attempts that can be retried, and the last attempt runs after it, so its error goes to the caller.
samanrassam
left a comment
There was a problem hiding this comment.
I ran the same tests and did not get any crash. The remaining files are cancelled and it exits with the maintenance error, nice!
Made a ticket for the issue with remaining files once stop_doing is set for a separate PR.
Pull Request Template
Before Marking as Ready for Review
dev(or other targeted branch)SPRINTLOG.mdif neededIf the target branch is
master:Summary
First of 4 PRs for the download problems seen with large deliveries. This one is only about API calls during upload/download. Range resume, disk flush, decrypt limit and
--temp-dircome in the next PRs.1. Retry once on a 502/503 from the proxy
A 502 from Traefik usually means that the backend (gunicorn) closed an idle keep-alive connection just before the request was sent. The request did not reach the backend, so it is safe to send it again. Before this PR, one 502 in
file/updatemade the download of that file fail, and one 502 infile/newmade the upload go to the end-of-runfile/addfailedpath.utils.perform_request_with_retry(): retry once after 1 s.NonJsonResponseError, subclass ofApiResponseError) and status inAPI_NON_JSON_RETRY_STATUS_CODES(502, 503). A 500/504 is not retried: the backend may have done the work.DataGetter.update_dbandDataPutter.add_file_db.update_dbnow also catchesApiResponseError, so a failed update does not stop the other downloads.2. POST
file/newthat reached the backendA proxy 502 is not always "request not received". If the first POST added the file, the retry fails because the file exists (500 from dds_web). After a failed POST,
add_file_dbasksfile/match. If the file is in the DB with thename_in_bucketof this upload (random per upload), our own request added it, and the file counts as added. PUT (overwrite) is not checked: a repeated PUT adds one closed version row, the same as the existingretry_add_file_dbfallback.3. Stop the delivery on maintenance
A 503 with a JSON body comes from dds_web (
MaintenanceOngoingException). It is raised as the newServiceUnavailableError, is never retried, and stops the delivery:failed_op) and saved in the failed delivery log as beforestop_doingflag as Ctrl+C)retry_add_file_dbandtouch_project_after_uploadare skipped (API is down). The path to the failed delivery log is shown.Ctrl+C behaviour is not changed.
Related Issue/Ticket
HMS-2694
Testing
tests/test_api_retry.py(28 tests, mockedrequests):ApiResponseErrorthat is notNonJsonResponseErrorServiceUnavailableErrorand is not retriedupdate_db/add_file_dbon maintenance: failure returned,failed_opset, delivery stoppedname_in_bucketcounts as added; othername_in_bucketor a failedfile/matchstays failed; PUT is not checkedruff format --checkandruff checkare clean.Reviewer Notes
API_NON_JSON_RETRY_STATUS_CODESand the docstring ofperform_request_with_retry.update_dbfailure (maintenance included) is still not fatal: the file is decrypted and verified locally.file/updateonly setstime_latest_download. This is the same as before.get()and keeps the two maintenance checks.Once all boxes are checked, mark the PR as Ready for Review and tag at least one team member as the initial reviewer.