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

support retrying interface exceptions during query execution #960

Merged
merged 18 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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):
VersusFacit marked this conversation as resolved.
Show resolved Hide resolved
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,
colin-rogers-dbt marked this conversation as resolved.
Show resolved Hide resolved
retry_limit=self.profile.credentials.retries,
)

if cursor is None:
Expand Down
5 changes: 4 additions & 1 deletion hatch.toml
Copy link
Contributor

Choose a reason for hiding this comment

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

Reminder to revert all changes in this file.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ packages = ["dbt"]

[envs.default]
dependencies = [
"dbt-adapters @ git+https://github.com/dbt-labs/dbt-adapters.git",
"dbt-adapters @ git+https://github.com/dbt-labs/dbt-adapters.git@addQueryRetries",
"dbt-common @ git+https://github.com/dbt-labs/dbt-common.git",
"dbt-tests-adapter @ git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter",
"dbt-core @ git+https://github.com/dbt-labs/dbt-core.git#subdirectory=core",
Expand Down Expand Up @@ -59,3 +59,6 @@ check-sdist = [
"pip freeze | grep dbt-redshift",
]
docker-prod = "docker build -f docker/Dockerfile -t dbt-redshift ."

[tool.hatch.metadata]
Copy link
Contributor

Choose a reason for hiding this comment

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

This can't be in hatch.toml nor pyproject.toml to publish to PyPI. hatch build will fail since this is not reproducible.

allow-direct-references = true
5 changes: 4 additions & 1 deletion pyproject.toml
mikealfare marked this conversation as resolved.
Show resolved Hide resolved
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 @ git+https://github.com/dbt-labs/dbt-adapters.git@addQueryRetries",
"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 Expand Up @@ -55,3 +55,6 @@ filterwarnings = [
"ignore:.*'soft_unicode' has been renamed to 'soft_str'*:DeprecationWarning",
"ignore:unclosed file .*:ResourceWarning",
]

[tool.hatch.metadata]
allow-direct-references = true
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
Loading