Skip to content

Commit

Permalink
support retrying interface exceptions during query execution (#960)
Browse files Browse the repository at this point in the history
* support retrying interface exceptions during query execution

* Fix conn acquisition failure bug and make unit tests conform to new spec.

* Add changelog.

* Add another exception to the list. Others are not called according to redshift's official docs

See more here: https://github.com/aws/amazon-redshift-python-driver/blob/master/redshift_connector/error.py

* Update branch reference.

* Fix branch ref.

* Fix typo from prior solution.

* Restore exponential backoff for retries which IS on main

* remove backoff and align with 1 second retry window described in docs.

* Update hatch.toml

* Update pyproject.toml

* Update pyproject.toml

* Update Under the Hood-20241204-185729.yaml

* remove tools.hatch.metadata ref

* update adapters lower bound

* jumpstart CI

---------

Co-authored-by: VersusFacit <[email protected]>
  • Loading branch information
colin-rogers-dbt and VersusFacit authored Dec 18, 2024
1 parent 0376909 commit b3030ff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20241204-185729.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Add retry logic for retryable exceptions
time: 2024-12-04T18:57:29.925299-08:00
custom:
Author: versusfacit colin-rogers-dbt
Issue: "960"
20 changes: 13 additions & 7 deletions dbt/adapters/redshift/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,22 +439,18 @@ def open(cls, connection):

credentials = connection.credentials

def exponential_backoff(attempt: int):
return attempt * attempt

retryable_exceptions = [
retryable_exceptions = (
redshift_connector.OperationalError,
redshift_connector.DatabaseError,
redshift_connector.DataError,
redshift_connector.InterfaceError,
]
)

open_connection = cls.retry_connection(
connection,
connect=get_connection_method(credentials),
logger=logger,
retry_limit=credentials.retries,
retry_timeout=exponential_backoff,
retryable_exceptions=retryable_exceptions,
)
open_connection.backend_pid = cls._get_backend_pid(open_connection) # type: ignore
Expand Down Expand Up @@ -496,8 +492,18 @@ def add_query(self, sql, auto_begin=True, bindings=None, abridge_sql_log=False):
if without_comments == "":
continue

retryable_exceptions = (
redshift_connector.InterfaceError,
redshift_connector.InternalError,
)

connection, cursor = super().add_query(
query, auto_begin, bindings=bindings, abridge_sql_log=abridge_sql_log
query,
auto_begin,
bindings=bindings,
abridge_sql_log=abridge_sql_log,
retryable_exceptions=retryable_exceptions,
retry_limit=self.profile.credentials.retries,
)

if cursor is None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ classifiers = [
]
dependencies = [
"dbt-common>=1.10,<2.0",
"dbt-adapters>=1.7,<2.0",
"dbt-adapters>=1.11,<2.0",
"dbt-postgres>=1.8,<1.10",
# dbt-redshift depends deeply on this package. it does not follow SemVer, therefore there have been breaking changes in previous patch releases
# Pin to the patch or minor version, and bump in each new minor version of dbt-redshift.
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/test_query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import redshift_connector

from multiprocessing import get_context
from unittest import TestCase, mock

Expand Down Expand Up @@ -103,7 +105,15 @@ def test_add_query_success(self):
mock_add_query.return_value = None, cursor
self.adapter.connections.add_query("select * from test3")
mock_add_query.assert_called_once_with(
"select * from test3", True, bindings=None, abridge_sql_log=False
"select * from test3",
True,
bindings=None,
abridge_sql_log=False,
retryable_exceptions=(
redshift_connector.InterfaceError,
redshift_connector.InternalError,
),
retry_limit=1,
)

def test_add_query_with_no_cursor(self):
Expand Down

0 comments on commit b3030ff

Please sign in to comment.