Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Unreleased
- Honor `_port` and `_connection_uri` on kernel connections (`use_kernel=True`), as Thrift does. Requires a databricks-sql-kernel release that includes databricks/databricks-sql-kernel#505.
- Speed up `fetchone()`, `fetchmany()` and `fetchall()` by building each result `Row` with fewer Python calls. The rows are unchanged. Large results and many concurrent cursors in one process benefit most.
- Honor `_retry_stop_after_attempts_count` as the total number of attempts (first request plus retries) instead of allowing one extra wire attempt: with a count of 1, a failed request is no longer retried.

# 4.10.0 (2026-11-24)
- Upgrade Databricks SQL Kernel to 1.5.0, with lower overhead for buffered fetches and closing drained cursors, and Arrow and Quinn security updates.
Expand Down
28 changes: 19 additions & 9 deletions src/databricks/sql/auth/retry.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class DatabricksRetryPolicy(Retry):
Float of seconds for the maximum delay between retries.

:param stop_after_attempts_count:
Integer maximum number of attempts that will be retried. This is an alias for urllib3's
`total`.
Integer maximum number of attempts in total (the initial request plus
retries). urllib3's `total` counts only retries after the first
request, so it is set to one less than this value.

:param stop_after_attempts_duration:
Float of maximum number of seconds within which a request may be retried starting from
Expand Down Expand Up @@ -114,14 +115,18 @@ def __init__(
_history: Optional[Tuple[RequestHistory, ...]] = urllib3_kwargs.get("history")

if not _history:
# no attempts were made so we can retry the current command as many times as specified
# by the user
_attempts_remaining = self.stop_after_attempts_count
# No attempts have been made yet. urllib3's `total` counts retries
# *after* the first request, while `stop_after_attempts_count` is
# the total number of attempts (first request + retries), so the
# initial `total` is one less than the configured count. With a
# count of 1, `total` is 0 and urllib3 raises MaxRetryError after
# the single attempt instead of retrying once more.
_attempts_remaining = max(0, self.stop_after_attempts_count - 1)
else:
# at least one of our attempts has been consumed, and urllib3 will have set a total
# `total` is a counter that begins equal to self.stop_after_attempts_count and is
# decremented after each unsuccessful request. When `total` is zero, urllib3 raises a
# MaxRetryError
# `total` is a counter that begins equal to self.stop_after_attempts_count - 1 and is
# decremented after each unsuccessful request. When `total` goes below zero, urllib3
# raises a MaxRetryError
_total: int = urllib3_kwargs.pop("total")
_attempts_remaining = _total

Expand Down Expand Up @@ -313,7 +318,12 @@ def get_backoff_time(self) -> float:
:return:
"""

current_attempt = self.stop_after_attempts_count - int(self.total or 0)
# Index the backoff by the retries already performed (the length of
# the retry history), not by the remaining `total`: `total` counts
# retries after the first request, so deriving the attempt from it
# would shift the backoff curve by one step now that the initial
# `total` is one less than the configured attempt count.
current_attempt = len(self.history or ())
proposed_backoff = (2**current_attempt) * self.delay_min

library_version = version("urllib3")
Expand Down
40 changes: 37 additions & 3 deletions tests/unit/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def test_sleep__no_retry_after(self, t_mock, retry_policy, error_history):

@patch("time.sleep")
def test_sleep__no_retry_after_header__multiple_retries(self, t_mock, retry_policy):
num_attempts = retry_policy.stop_after_attempts_count
# With stop_after_attempts_count=3 the policy allows 3 attempts in
# total, i.e. 2 (sleep, increment) pairs complete before the 3rd
# increment exhausts it — see test_stop_after_attempts_count below.
num_attempts = retry_policy.stop_after_attempts_count - 1

retry_policy._retry_start_time = time.time()
retry_policy.command_type = CommandType.OTHER
Expand Down Expand Up @@ -77,8 +80,10 @@ def test_sleep__no_retry_after_header__multiple_retries(self, t_mock, retry_poli

@patch("time.sleep")
def test_excessive_retry_attempts_error(self, t_mock, retry_policy):
# Attempting more than stop_after_attempt_count
num_attempts = retry_policy.stop_after_attempts_count + 1
# Attempting stop_after_attempts_count times exhausts the policy:
# that many attempts in total are allowed, and the next increment
# raises MaxRetryError.
num_attempts = retry_policy.stop_after_attempts_count

retry_policy._retry_start_time = time.time()
retry_policy.command_type = CommandType.OTHER
Expand All @@ -89,6 +94,35 @@ def test_excessive_retry_attempts_error(self, t_mock, retry_policy):
# Internally urllib3 calls the increment function generating a new instance for every retry
retry_policy = retry_policy.increment()

@pytest.mark.parametrize("count", [1, 2, 3, 5, 30])
def test_stop_after_attempts_count_is_total_attempts(self, count):
# Regression test for #890 (ERRORRECOV-013): the knob counts total
# attempts (first request + retries), not retries after the first
# request. urllib3's `total` is one less than the configured count.
policy = self._make_retry_policy(stop_after_attempts_count=count)
policy._command_type = None
policy._retry_start_time = None
assert policy.total == count - 1

attempts = 1 # the initial request
while True:
try:
policy = policy.increment()
except MaxRetryError:
break
attempts += 1
assert attempts == count

def test_stop_after_attempts_count_one_disables_retries(self):
# With a count of 1, the first failure must raise MaxRetryError
# immediately — no second wire attempt is made.
policy = self._make_retry_policy(stop_after_attempts_count=1)
policy._command_type = None
policy._retry_start_time = None
assert policy.total == 0
with pytest.raises(MaxRetryError):
policy.increment()

def test_respect_server_retry_after__retries_with_retry_after(self):
"""429 + Retry-After header → should retry"""
policy = self._make_retry_policy(respect_server_retry_after_header=True)
Expand Down