-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[ENH]: Retry Strategy for HttpClient #2183
Open
tazarov
wants to merge
2
commits into
main
Choose a base branch
from
trayan-05-11-feat_retry_strategy_for_httpclient
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,11 +1,19 @@ | ||
from typing import Generator | ||
import json | ||
import socket | ||
import time | ||
import uuid | ||
from typing import Generator, Any, Optional | ||
from unittest.mock import patch | ||
|
||
from pytest_httpserver import HTTPServer | ||
|
||
import chromadb | ||
from chromadb.config import Settings | ||
from chromadb.api import ClientAPI | ||
import chromadb.server.fastapi | ||
import pytest | ||
import tempfile | ||
from chromadb.utils.net import RetryStrategy | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -70,3 +78,196 @@ def test_http_client_with_inconsistent_port_settings() -> None: | |
str(e) | ||
== "Chroma server http port provided in settings[8001] is different to the one provided in HttpClient: [8002]" | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def retry_session(httpserver: HTTPServer) -> chromadb.api.ClientAPI: | ||
httpserver.expect_request("/api/v1/tenants/default_tenant").respond_with_data( | ||
json.dumps({"name": "default_tenant"}) | ||
) | ||
httpserver.expect_request( | ||
"/api/v1/databases/default_database", | ||
query_string="tenant=default_tenant", | ||
).respond_with_data( | ||
json.dumps( | ||
{ | ||
"id": f"{uuid.uuid4()}", | ||
"name": "default_database", | ||
"tenant": "default_tenant", | ||
} | ||
) | ||
) | ||
return chromadb.HttpClient(host=httpserver.host, port=httpserver.port) | ||
|
||
|
||
@pytest.fixture | ||
def retry_session_with_custom_retry(httpserver: HTTPServer) -> chromadb.api.ClientAPI: | ||
httpserver.expect_request("/api/v1/tenants/default_tenant").respond_with_data( | ||
json.dumps({"name": "default_tenant"}) | ||
) | ||
httpserver.expect_request( | ||
"/api/v1/databases/default_database", | ||
query_string="tenant=default_tenant", | ||
).respond_with_data( | ||
json.dumps( | ||
{ | ||
"id": f"{uuid.uuid4()}", | ||
"name": "default_database", | ||
"tenant": "default_tenant", | ||
} | ||
) | ||
) | ||
return chromadb.HttpClient( | ||
host=httpserver.host, | ||
port=httpserver.port, | ||
retry=RetryStrategy(total=2, status_codes=(504,)), | ||
) | ||
|
||
|
||
def test_retry_on_429( | ||
httpserver: HTTPServer, retry_session: chromadb.api.ClientAPI | ||
) -> None: | ||
retry_after_header = {"Retry-After": "2"} # wait for 2 sec before trying again | ||
|
||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
"Too Many Requests", status=429, headers=retry_after_header | ||
) | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
"Too Many Requests", status=429, headers=retry_after_header | ||
) | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
json.dumps({"nanosecond heartbeat": 1715365335819568533}), status=200 | ||
) | ||
start_time = time.time() | ||
retry_session.heartbeat() | ||
assert ( | ||
time.time() - start_time > 2 * 2 | ||
) # ensure that we respect the Retry-After header | ||
|
||
|
||
def test_retry_on_504( | ||
httpserver: HTTPServer, retry_session: chromadb.api.ClientAPI | ||
) -> None: | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
"Gateway Timeout", status=504 | ||
) | ||
|
||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
"Gateway Timeout", status=504 | ||
) | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
json.dumps({"nanosecond heartbeat": 1715365335819568533}), status=200 | ||
) | ||
retry_session.heartbeat() | ||
|
||
|
||
def test_retry_on_503( | ||
httpserver: HTTPServer, retry_session: chromadb.api.ClientAPI | ||
) -> None: | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
"Service Unavailable", status=503 | ||
) | ||
|
||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
"Service Unavailable", status=503 | ||
) | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
json.dumps({"nanosecond heartbeat": 1715365335819568533}), status=200 | ||
) | ||
retry_session.heartbeat() | ||
|
||
|
||
def test_retry_on_503_exceeding_max( | ||
httpserver: HTTPServer, retry_session: chromadb.api.ClientAPI | ||
) -> None: | ||
httpserver.expect_request("/api/v1").respond_with_data( | ||
"Service Unavailable", status=503 | ||
) | ||
|
||
with pytest.raises(Exception, match="Max retries exceeded with url"): | ||
retry_session.heartbeat() | ||
|
||
|
||
def test_no_retry_on_400( | ||
httpserver: HTTPServer, retry_session: chromadb.api.ClientAPI | ||
) -> None: | ||
httpserver.expect_request("/api/v1").respond_with_data("Bad Request", status=400) | ||
|
||
with pytest.raises(Exception, match="Bad Request"): | ||
retry_session.heartbeat() | ||
|
||
|
||
def test_no_retry_on_400_with_custom_retry( | ||
httpserver: HTTPServer, retry_session_with_custom_retry: chromadb.api.ClientAPI | ||
) -> None: | ||
httpserver.expect_request("/api/v1").respond_with_data("Bad Request", status=400) | ||
|
||
with pytest.raises(Exception, match="Bad Request"): | ||
retry_session_with_custom_retry.heartbeat() | ||
|
||
|
||
def test_no_retry_on_429_with_custom_retry( | ||
httpserver: HTTPServer, retry_session_with_custom_retry: chromadb.api.ClientAPI | ||
) -> None: | ||
httpserver.expect_request("/api/v1").respond_with_data( | ||
"Too Many Requests", status=429 | ||
) | ||
|
||
with pytest.raises(Exception, match="Too Many Requests"): | ||
retry_session_with_custom_retry.heartbeat() | ||
|
||
|
||
def test_no_retry_on_504_with_custom_retry( | ||
httpserver: HTTPServer, retry_session_with_custom_retry: chromadb.api.ClientAPI | ||
) -> None: | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
"Gateway Timeout", status=504 | ||
) | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
"Gateway Timeout", status=504 | ||
) | ||
httpserver.expect_oneshot_request("/api/v1").respond_with_data( | ||
json.dumps({"nanosecond heartbeat": 1715365335819568533}), status=200 | ||
) | ||
retry_session_with_custom_retry.heartbeat() | ||
|
||
|
||
def server(max_retries: int = 1) -> None: | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
s.bind(("localhost", 9999)) | ||
s.listen(1) | ||
retries = 0 | ||
while retries < max_retries: | ||
conn, _ = s.accept() | ||
conn.close() # Close the connection immediately | ||
retries += 1 | ||
|
||
|
||
@pytest.fixture | ||
def connect_retries() -> int: | ||
return 3 | ||
|
||
|
||
@pytest.fixture | ||
def local_tcp_server(connect_retries: int) -> Generator[None, None, None]: | ||
import threading | ||
|
||
t = threading.Thread(target=server, args=(connect_retries,)) | ||
t.start() | ||
yield | ||
t.join() | ||
|
||
|
||
def test_with_connection_error( | ||
local_tcp_server: Optional[Any], connect_retries: int | ||
) -> None: | ||
start_time = time.time() | ||
with pytest.raises(Exception): | ||
chromadb.HttpClient( | ||
host="localhost", | ||
port=9999, | ||
retry=RetryStrategy(connect=connect_retries - 1, backoff_factor=1), | ||
) | ||
assert ( | ||
time.time() - start_time > 4 | ||
) # ensure that we have attempted to connect twice | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, not super clear to me, does this work because |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe