amoghrajesh opened a new pull request, #56762:
URL: https://github.com/apache/airflow/pull/56762
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
## Motivation
The task SDK uses httpx for HTTP operations but depends on `retryhttp` for
retry logic. This brings in the entire `requests` library as a transitive
dependency, even though it's never actually used.
Memray reports also show some stats which can be improved.
For `import retryhttp`:
<img width="2557" height="883" alt="image"
src="https://github.com/user-attachments/assets/e7f7f1d3-c9a8-46db-9052-6deb626cab32"
/>
<img width="1141" height="624" alt="image"
src="https://github.com/user-attachments/assets/1ddad4b9-790c-4020-a41c-425b966ab3b1"
/>
<img width="1141" height="624" alt="image"
src="https://github.com/user-attachments/assets/df6d5f18-6871-4314-ab92-ca9c5a7eb483"
/>
This is because retryhttp unconditionally imports both httpx and requests,
even though we only use httpx.
The retry handler works fine, but it's unnecessary bloat. We're already
using tenacity in sdk client, and we only use httpx, not requests, so might as
well use tenacity for better memory results and reduced footprint.
## Alternatives Considered
### 1. Switch to stamina
- Modern, opinionated wrapper around tenacity
- Better ergonomics and async support
### 2. Use pure tenacity
- Zero new dependencies (tenacity already used)
- Maximum memory and size savings
- Full control over retry behavior
Went ahead and selected tenacity because it is a battle tested library
already present in task sdk and is used often and there is no need for a new
library when the benefits of memory footprint (Net savings: ~444KB vs ~544KB
with pure tenacity) aren't significantly high.
## Changes of note
Migrated task SDK to use tenacity instead of retryhttp while maintaining
total parity with what retryhttp offered. A lot of code thats written is
inspired by code of retryhttp!
Tenacity is a generic retry library - it doesn't know anything about HTTP.
It just retries when you tell it to, so to maintain parity, some wrappers and
helpers had to be written.
**`_should_retry_api_request(exception)`**
This function determines which errors should trigger a retry. It replicates
retryhttp's behavior of retrying on:
- Server errors (5xx status codes)
- Rate limiting (429 status code)
- Network failures (httpx.NetworkError)
- Timeouts (httpx.TimeoutException)
But NOT retrying on client errors like 404 or 401, which would be pointless.
**`_get_retry_wait_time(retry_state)`**
This function handles the wait time between retries. It replicates
retryhttp's `wait_retry_after` behavior:
- For 429 responses with a `Retry-After` header, it uses that exact value
- Otherwise, it falls back to exponential backoff with random jitters
### How parity was maintained
The original retryhttp decorator looked like this:
```python
@retry(
max_attempt_number=API_RETRIES,
wait_server_errors=_default_wait,
wait_network_errors=_default_wait,
wait_timeouts=_default_wait,
wait_rate_limited=wait_retry_after(fallback=_default_wait),
before_sleep=before_log(log, logging.WARNING),
)
```
Each of those parameters mapped to specific HTTP behaviors. Our new
decorator with tenacity:
```python
@retry(
retry=retry_if_exception(_should_retry_api_request),
stop=stop_after_attempt(API_RETRIES),
wait=_get_retry_wait_time,
before_sleep=before_log(log, logging.WARNING),
reraise=True,
)
```
Removed from dependencies:
- `retryhttp`
- `requests`
- `types-requests`
## How this was tested
### Unit tests
All existing retry tests pass without modification:
- Server error recovery (500 errors with retry)
- Rate limiting with Retry-After header (429 responses)
- Non-retryable errors (422 validation errors)
- Max retry attempts exhaustion
### Integration testing
Ran the task SDK against a live API server and killed the server mid
request. The client correctly retried with exponential backoff and recovered
when the server came back up, confirming network error handling works as
expected.
<img width="1665" height="152" alt="image"
src="https://github.com/user-attachments/assets/8a78fd22-ecfc-4c39-829a-eba0c62dfe65"
/>
## Benefits
### Memray results
Importing `import tenacity` and showing difference:
Flamegraph (now):
<img width="2559" height="833" alt="image"
src="https://github.com/user-attachments/assets/351e231b-53e2-4817-95f4-5092b800e3e6"
/>
Flamegraph (earlier):
<img width="2557" height="883" alt="image"
src="https://github.com/user-attachments/assets/e7f7f1d3-c9a8-46db-9052-6deb626cab32"
/>
Memory Graph (now):
<img width="1142" height="631" alt="image"
src="https://github.com/user-attachments/assets/b5c0d18d-f5d4-4337-ac22-2d3d881db1bc"
/>
Memory Graph (before):
<img width="1141" height="624" alt="image"
src="https://github.com/user-attachments/assets/1ddad4b9-790c-4020-a41c-425b966ab3b1"
/>
Stats (now):
<img width="1142" height="631" alt="image"
src="https://github.com/user-attachments/assets/9bf0afc1-d17f-4557-839c-97bd0295dc3b"
/>
Stats (earlier):
<img width="1141" height="624" alt="image"
src="https://github.com/user-attachments/assets/df6d5f18-6871-4314-ab92-ca9c5a7eb483"
/>
### Package size
INSTALLED DEPENDENCIES SIZE:
Removed packages:
• retryhttp ..................... 40K
• requests ..................... 228K
• urllib3 (dep of requests) .... 488K
• charset_normalizer (dep) ..... 808K
• idna (dep) ................... 352K
• certifi (dep) ................ 296K
─────────────────────────────────────
TOTAL DISK SAVINGS: ........... ~2.2 MB
Workers do not need to have these dependencies anymore.
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information.
In case of fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party
License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a
newsfragment file, named `{pr_number}.significant.rst` or
`{issue_number}.significant.rst`, in
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]