Skip to content
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

Added requests retry mechanism for all requests call in _pulp_client #53

Merged
merged 9 commits into from
Apr 29, 2019

Conversation

midnightercz
Copy link
Member

To make sure tool process isn't interrupted due network issues,
retry mechanism based on urllib3 Retry was implemented.
New configuration environment variables to customize this
were added:

  • UBIPOP_HTTP_TOTAL_RETRIES
  • UBIPOP_HTTP_RETRY_BACKOFF

fixes #32

To make sure tool process isn't interrupted due network issues,
retry mechanism based on urllib3 Retry was implemented.
New configuration environment variables to customize this
were added:

- UBIPOP_HTTP_TOTAL_RETRIES
- UBIPOP_HTTP_RETRY_BACKOFF
@midnightercz midnightercz requested a review from a team April 16, 2019 13:30
Copy link
Contributor

@alexandrevicenzi alexandrevicenzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this retry on Exceptions as well? Like ConnectionTimeout

ubipop/_pulp_client.py Outdated Show resolved Hide resolved
tests/test_pulp.py Show resolved Hide resolved
@@ -9,11 +13,27 @@

_LOG = logging.getLogger("ubipop")

HTTP_TOTAL_RETRIES = os.environ.get("UBIPOP_HTTP_TOTAL_RETRIES", 10)
UBIPOP_HTTP_RETRY_BACKOFF = os.environ.get("UBIPOP_HTTP_RETRY_BACKOFF", 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with backoff=1, it means that will retry after 0, 2, 4, 8 (secs) and so on, 10 times can take ages, maybe 0.1 or 0.2 would be a better number no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the value from existing project that we have for helping with requests to pulp. But for sure I'm willing to change to anything we agree on.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe 5 times' enough

def __init__(self, *args, **kwargs):
kwargs['max_retries'] = Retry(
total=kwargs.get('total_retries', HTTP_TOTAL_RETRIES),
status_forcelist=[500, 502, 503, 504],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some 4xx also make sense here, for example: 429 Too Many Requests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, these are from existing projects. But sure we can add more status codes there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe makes sense to make that configurable.

- Added test params descriptions
- Removed unneeded code
- fixed linter errors
ubipop/_pulp_client.py Show resolved Hide resolved
- fixed constant names inconsistency
- fixed typecast for env variables
@midnightercz
Copy link
Member Author

Does this retry on Exceptions as well? Like ConnectionTimeout

Sorry that I overlooked your comment, urllib3 also retries for errors like timeout, socket error, protocol error and similar.

tests/test_pulp.py Outdated Show resolved Hide resolved
rohanpm
rohanpm previously approved these changes Apr 24, 2019
@midnightercz midnightercz requested review from rohanpm and a team April 24, 2019 14:09
@midnightercz
Copy link
Member Author

I had to rebased this, so please approve this again

rbikar
rbikar previously approved these changes Apr 24, 2019
@rbikar rbikar dismissed rohanpm’s stale review April 24, 2019 14:26

New commit pushed

env_retries, retry_call, retry_args, ok_response, expected_retries):
global HTTP_TOTAL_RETRIES
if env_retries:
HTTP_TOTAL_RETRIES = env_retries
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to save a restore original value.

HTTP_TOTAL_RETRIES = env_retries

retries = [make_mock_response(err_status_code, 'Fake Http error')
for _ in range(HTTP_TOTAL_RETRIES)[:-1]] + [make_mock_response(200, ok_response)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range(HTTP_TOTAL_RETRIES - 1)

And the 'ok_response' could be added in the next line, would make the code more readable.

ubipop/_pulp_client.py Outdated Show resolved Hide resolved
Copy link
Contributor

@danrodrig danrodrig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UBIPOP_HTTP_RETRY_BACKOFF should allow floats.

rbikar
rbikar previously approved these changes Apr 26, 2019
@@ -1,8 +1,30 @@
from ubipop._pulp_client import Repo, Package, ModuleDefaults, Pulp
try:
from importlib import reload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this import make sense? not used, if fails nothing happens

Copy link
Member Author

@midnightercz midnightercz Apr 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean. Python3 doesn't have reload globally available

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But where is used?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like reload is not used.

And if we need the import for an specific version better use sys.version_info.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I removed that code and forgot to remove that import

@@ -1,4 +1,7 @@
import requests
from urllib3.util.retry import Retry
import os
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imports could be sorted better

)
def test_retries(set_backoff_to_zero_fixture, mocked_getresponse, mock_pulp, should_retry, err_status_code,
env_retries, retry_call, retry_args, ok_response, expected_retries):
global HTTP_TOTAL_RETRIES
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea, I don't like global keyword, the best way would be to use pulp_client.HTTP_TOTAL_RETRIES = value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think global is fine here, the variable is used in this scope.

HTTP_TOTAL_RETRIES = env_retries

retries = [make_mock_response(err_status_code, 'Fake Http error')
for _ in range(HTTP_TOTAL_RETRIES)[:-1]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range(HTTP_TOTAL_RETRIES - 1)

@@ -9,11 +12,25 @@

_LOG = logging.getLogger("ubipop")

HTTP_TOTAL_RETRIES = int(os.environ.get("UBIPOP_HTTP_TOTAL_RETRIES", 10))
HTTP_RETRY_BACKOFF = float(os.environ.get("UBIPOP_HTTP_RETRY_BACKOFF", 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I think that the defined empty should be handled. But If nobody else thinks it's an issue I'm not requesting the change.

danrodrig
danrodrig previously approved these changes Apr 29, 2019
Copy link
Contributor

@danrodrig danrodrig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor issues, non-blocking. LGTM

- easier iteration over HTTP_TOTAL_RETRIES
@midnightercz midnightercz merged commit 58a74b1 into release-engineering:master Apr 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add retry for Pulp requests
6 participants