-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
replace naive retry with tenacity #3026
Changes from all commits
d13f27a
3605400
b389b60
c1a2edd
ab9fd0f
a73ca33
940f427
1fd519a
94c5b88
46a3a81
d2bd83e
c315c5b
8dc9936
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 |
---|---|---|
|
@@ -23,13 +23,13 @@ | |
import json | ||
import logging | ||
import socket | ||
import time | ||
import base64 | ||
|
||
from urllib.parse import urljoin, urlencode, urlparse | ||
from urllib.request import urlopen, Request | ||
from urllib.error import URLError | ||
|
||
from tenacity import Retrying, wait_fixed, stop_after_attempt | ||
from luigi import configuration | ||
from luigi.scheduler import RPC_METHODS | ||
|
||
|
@@ -144,35 +144,29 @@ def __init__(self, url='http://localhost:8082/', connect_timeout=None): | |
else: | ||
self._fetcher = URLLibFetcher() | ||
|
||
def _wait(self): | ||
if self._rpc_log_retries: | ||
logger.info("Wait for %d seconds" % self._rpc_retry_wait) | ||
time.sleep(self._rpc_retry_wait) | ||
def _get_retryer(self): | ||
def retry_logging(retry_state): | ||
if self._rpc_log_retries: | ||
logger.warning("Failed connecting to remote scheduler %r", self._url, exc_info=True) | ||
logger.info("Retrying attempt %r of %r (max)" % (retry_state.attempt_number + 1, self._rpc_retry_attempts)) | ||
logger.info("Wait for %d seconds" % self._rpc_retry_wait) | ||
|
||
return Retrying(wait=wait_fixed(self._rpc_retry_wait), | ||
stop=stop_after_attempt(self._rpc_retry_attempts), | ||
reraise=True, | ||
after=retry_logging) | ||
|
||
def _fetch(self, url_suffix, body): | ||
full_url = _urljoin(self._url, url_suffix) | ||
last_exception = None | ||
attempt = 0 | ||
while attempt < self._rpc_retry_attempts: | ||
attempt += 1 | ||
if last_exception: | ||
if self._rpc_log_retries: | ||
logger.info("Retrying attempt %r of %r (max)" % (attempt, self._rpc_retry_attempts)) | ||
self._wait() # wait for a bit and retry | ||
try: | ||
response = self._fetcher.fetch(full_url, body, self._connect_timeout) | ||
break | ||
except self._fetcher.raises as e: | ||
last_exception = e | ||
if self._rpc_log_retries: | ||
logger.warning("Failed connecting to remote scheduler %r", self._url, | ||
exc_info=True) | ||
continue | ||
else: | ||
scheduler_retry = self._get_retryer() | ||
|
||
try: | ||
response = scheduler_retry(self._fetcher.fetch, full_url, body, self._connect_timeout) | ||
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. for some reason i'm struggling to see where it's defined that 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. Thank you for your comment! So, the code above calls 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, i see. A bit obfuscated, but it is being used as tenacity expects |
||
except self._fetcher.raises as e: | ||
raise RPCError( | ||
"Errors (%d attempts) when connecting to remote scheduler %r" % | ||
(self._rpc_retry_attempts, self._url), | ||
last_exception | ||
e | ||
) | ||
return response | ||
|
||
|
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.
I think it is better to keep this log. Never know what users would do with it.
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.
added wait log and fixed test in 8f36797