-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from duckdb/jwills_add_retries_for_some_errors
Add support for retrying certain types of exceptions we see when running models with DuckDB
- Loading branch information
Showing
5 changed files
with
185 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pytest | ||
from unittest.mock import patch | ||
|
||
from duckdb.duckdb import IOException | ||
|
||
from dbt.adapters.duckdb.credentials import DuckDBCredentials | ||
from dbt.adapters.duckdb.credentials import Retries | ||
from dbt.adapters.duckdb.environments import Environment | ||
|
||
class TestConnectRetries: | ||
|
||
@pytest.fixture | ||
def creds(self): | ||
# Create a mock credentials object | ||
return DuckDBCredentials( | ||
path="foo.db", | ||
retries=Retries(connect_attempts=2, retryable_exceptions=["IOException", "ArithmeticError"]) | ||
) | ||
|
||
@pytest.mark.parametrize("exception", [None, IOException, ArithmeticError, ValueError]) | ||
def test_initialize_db(self, creds, exception): | ||
# Mocking the duckdb.connect method | ||
with patch('duckdb.connect') as mock_connect: | ||
if exception: | ||
mock_connect.side_effect = [exception, None] | ||
|
||
if exception == ValueError: | ||
with pytest.raises(ValueError) as excinfo: | ||
Environment.initialize_db(creds) | ||
else: | ||
# Call the initialize_db method | ||
Environment.initialize_db(creds) | ||
if exception in {IOException, ArithmeticError}: | ||
assert mock_connect.call_count == creds.retries.connect_attempts | ||
else: | ||
mock_connect.assert_called_once_with(creds.path, read_only=False, config={}) |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
|
||
import duckdb | ||
|
||
from dbt.adapters.duckdb.credentials import Retries | ||
from dbt.adapters.duckdb.environments import RetryableCursor | ||
|
||
class TestRetryableCursor: | ||
|
||
@pytest.fixture | ||
def mock_cursor(self): | ||
return MagicMock() | ||
|
||
@pytest.fixture | ||
def mock_retries(self): | ||
return Retries(query_attempts=3) | ||
|
||
@pytest.fixture | ||
def retry_cursor(self, mock_cursor, mock_retries): | ||
return RetryableCursor( | ||
mock_cursor, | ||
mock_retries.query_attempts, | ||
mock_retries.retryable_exceptions) | ||
|
||
def test_successful_execute(self, mock_cursor, retry_cursor): | ||
""" Test that execute successfully runs the SQL query. """ | ||
sql_query = "SELECT * FROM table" | ||
retry_cursor.execute(sql_query) | ||
mock_cursor.execute.assert_called_once_with(sql_query) | ||
|
||
def test_retry_on_failure(self, mock_cursor, retry_cursor): | ||
""" Test that execute retries the SQL query on failure. """ | ||
mock_cursor.execute.side_effect = [duckdb.duckdb.IOException, None] | ||
sql_query = "SELECT * FROM table" | ||
retry_cursor.execute(sql_query) | ||
assert mock_cursor.execute.call_count == 2 | ||
|
||
def test_no_retry_on_non_retryable_exception(self, mock_cursor, retry_cursor): | ||
""" Test that a non-retryable exception is not retried. """ | ||
mock_cursor.execute.side_effect = ValueError | ||
sql_query = "SELECT * FROM table" | ||
with pytest.raises(ValueError): | ||
retry_cursor.execute(sql_query) | ||
mock_cursor.execute.assert_called_once_with(sql_query) | ||
|
||
def test_exponential_backoff(self, mock_cursor, retry_cursor): | ||
""" Test that exponential backoff is applied between retries. """ | ||
mock_cursor.execute.side_effect = [duckdb.duckdb.IOException, duckdb.duckdb.IOException, None] | ||
sql_query = "SELECT * FROM table" | ||
|
||
with patch("time.sleep") as mock_sleep: | ||
retry_cursor.execute(sql_query) | ||
assert mock_sleep.call_count == 2 | ||
mock_sleep.assert_any_call(1) | ||
mock_sleep.assert_any_call(2) |