-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add retry of additional errors #2694
Merged
beckjake
merged 5 commits into
dbt-labs:dev/marian-anderson
from
kconvey:kconvey-retry-upstream
Aug 12, 2020
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from contextlib import contextmanager | ||
from dataclasses import dataclass | ||
from requests.exceptions import ConnectionError | ||
from typing import Optional, Any, Dict | ||
|
||
import google.auth | ||
|
@@ -25,6 +26,18 @@ | |
|
||
BQ_QUERY_JOB_SPLIT = '-----Query Job SQL Follows-----' | ||
|
||
REOPENABLE_ERRORS = { | ||
ConnectionResetError, | ||
ConnectionError, | ||
} | ||
|
||
RETRYABLE_ERRORS = { | ||
google.cloud.exceptions.ServerError, | ||
google.cloud.exceptions.BadRequest, | ||
ConnectionResetError, | ||
ConnectionError, | ||
} | ||
|
||
|
||
class Priority(StrEnum): | ||
Interactive = 'interactive' | ||
|
@@ -390,12 +403,21 @@ def _query_and_results(self, client, sql, conn, job_params, timeout=None): | |
|
||
def _retry_and_handle(self, msg, conn, fn): | ||
"""retry a function call within the context of exception_handler.""" | ||
def reopen_conn_on_error(error): | ||
for type in REOPENABLE_ERRORS: | ||
if isinstance(error, type): | ||
logger.warning('Reopening connection after {!r}', error) | ||
self.close(conn) | ||
self.open(conn) | ||
return | ||
|
||
with self.exception_handler(msg): | ||
return retry.retry_target( | ||
target=fn, | ||
predicate=_ErrorCounter(self.get_retries(conn)).count_error, | ||
sleep_generator=self._retry_generator(), | ||
deadline=None) | ||
deadline=None, | ||
on_error=reopen_conn_on_error) | ||
|
||
def _retry_generator(self): | ||
"""Generates retry intervals that exponentially back off.""" | ||
|
@@ -425,5 +447,8 @@ def count_error(self, error): | |
|
||
|
||
def _is_retryable(error): | ||
"""Return true for 500 level (retryable) errors.""" | ||
return isinstance(error, google.cloud.exceptions.ServerError) | ||
"""Return true for errors that are unlikely to occur again if retried.""" | ||
for error_type in RETRYABLE_ERRORS: | ||
if isinstance(error, error_type): | ||
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. See above, I think this can just be 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. Done. |
||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 you can just do
if isinstance(error, REOPENABLE_ERRORS)
here, right?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.
TIL. Done.