Skip to content

Commit 2301d8e

Browse files
lozbrownwendigo
authored andcommitted
Scope empty response retries to statement requests
1 parent c4b2c57 commit 2301d8e

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎tests/unit/test_client.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,27 @@ def test_empty_200_response_retry(monkeypatch):
11421142
assert get_retry.retry_count == attempts
11431143

11441144

1145+
@pytest.mark.parametrize("method_name", ["_get", "_post", "_delete", "_head"])
1146+
def test_empty_200_transport_response_does_not_retry(method_name, monkeypatch):
1147+
http_resp = TrinoRequest.http.Response()
1148+
http_resp.status_code = 200
1149+
http_resp._content = b""
1150+
1151+
retry = RetryRecorder(result=http_resp)
1152+
session_method = method_name.removeprefix("_")
1153+
monkeypatch.setattr(TrinoRequest.http.Session, session_method, retry)
1154+
1155+
req = TrinoRequest(
1156+
host="coordinator",
1157+
port=8080,
1158+
client_session=ClientSession(user="test"),
1159+
max_attempts=3,
1160+
)
1161+
1162+
getattr(req, method_name)("URL")
1163+
assert retry.retry_count == 1
1164+
1165+
11451166
@pytest.mark.parametrize("status_code", [
11461167
501
11471168
])

‎trino/client.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,11 +638,23 @@ def max_attempts(self, value: int) -> None:
638638
if value == 1: # No retry
639639
self._get = self._http_session.get
640640
self._post = self._http_session.post
641+
self._get_statement = self._http_session.get
642+
self._post_statement = self._http_session.post
641643
self._delete = self._http_session.delete
642644
self._head = self._http_session.head
643645
return
644646

645647
with_retry = _retry_with(
648+
self._handle_retry,
649+
handled_exceptions=self._exceptions,
650+
conditions=(
651+
# need retry when there is no exception but the status code is 429, 502, 503, or 504
652+
lambda response: getattr(response, "status_code", None)
653+
in (429, 502, 503, 504),
654+
),
655+
max_attempts=self._max_attempts,
656+
)
657+
with_statement_retry = _retry_with(
646658
self._handle_retry,
647659
handled_exceptions=self._exceptions,
648660
conditions=(
@@ -651,12 +663,14 @@ def max_attempts(self, value: int) -> None:
651663
in (429, 502, 503, 504),
652664
# need retry when the server returns 200 with an empty body (transient under load)
653665
lambda response: getattr(response, "status_code", None) == 200
654-
and not getattr(response, "text", "").strip(),
666+
and not getattr(response, "content", b""),
655667
),
656668
max_attempts=self._max_attempts,
657669
)
658670
self._get = with_retry(self._http_session.get)
659671
self._post = with_retry(self._http_session.post)
672+
self._get_statement = with_statement_retry(self._http_session.get)
673+
self._post_statement = with_statement_retry(self._http_session.post)
660674
self._delete = with_retry(self._http_session.delete)
661675
self._head = with_retry(self._http_session.head)
662676

@@ -686,7 +700,7 @@ def post(self, sql: str, additional_http_headers: Optional[Dict[str, Any]] = Non
686700
# explicitly to match the Trino JDBC client. Users may still override it.
687701
http_headers.setdefault(constants.HEADER_CONTENT_TYPE, constants.CONTENT_TYPE_TEXT_UTF8)
688702

689-
http_response = self._post(
703+
http_response = self._post_statement(
690704
self.statement_url,
691705
data=data,
692706
headers=http_headers,
@@ -696,7 +710,7 @@ def post(self, sql: str, additional_http_headers: Optional[Dict[str, Any]] = Non
696710
return http_response
697711

698712
def get(self, url: str) -> Response:
699-
return self._get(
713+
return self._get_statement(
700714
url,
701715
headers=self.http_headers,
702716
timeout=self._request_timeout,

0 commit comments

Comments
 (0)