-
Notifications
You must be signed in to change notification settings - Fork 113
Add retry mechanism to telemetry requests #617
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
Open
saishreeeee
wants to merge
9
commits into
telemetry
Choose a base branch
from
telemetry-retry
base: telemetry
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
9 commits
Select commit
Hold shift + click to select a range
4b6e331
retry
saishreeeee 1a9c253
Merge branch 'telemetry' into telemetry-retry
saishreeeee 41bdfe7
Merge branch 'telemetry' into telemetry-retry
saishreeeee a92821b
changed error to debug log
saishreeeee e684cc3
added e2e tests to check retry logic in telemetry
saishreeeee d135406
removed caplog
saishreeeee 48345ec
retry policy default values
saishreeeee e7d2779
formatting
saishreeeee 7c64c7b
compact tests
saishreeeee 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 hidden or 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 hidden or 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 hidden or 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,107 @@ | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
import io | ||
import time | ||
|
||
from databricks.sql.telemetry.telemetry_client import TelemetryClientFactory | ||
from databricks.sql.auth.retry import DatabricksRetryPolicy | ||
|
||
PATCH_TARGET = 'urllib3.connectionpool.HTTPSConnectionPool._get_conn' | ||
|
||
def create_mock_conn(responses): | ||
"""Creates a mock connection object whose getresponse() method yields a series of responses.""" | ||
mock_conn = MagicMock() | ||
mock_http_responses = [] | ||
for resp in responses: | ||
mock_http_response = MagicMock() | ||
mock_http_response.status = resp.get("status") | ||
mock_http_response.headers = resp.get("headers", {}) | ||
body = resp.get("body", b'{}') | ||
mock_http_response.fp = io.BytesIO(body) | ||
def release(): | ||
mock_http_response.fp.close() | ||
mock_http_response.release_conn = release | ||
mock_http_responses.append(mock_http_response) | ||
mock_conn.getresponse.side_effect = mock_http_responses | ||
return mock_conn | ||
|
||
class TestTelemetryClientRetries: | ||
@pytest.fixture(autouse=True) | ||
def setup_and_teardown(self): | ||
TelemetryClientFactory._initialized = False | ||
TelemetryClientFactory._clients = {} | ||
TelemetryClientFactory._executor = None | ||
yield | ||
if TelemetryClientFactory._executor: | ||
TelemetryClientFactory._executor.shutdown(wait=True) | ||
TelemetryClientFactory._initialized = False | ||
TelemetryClientFactory._clients = {} | ||
TelemetryClientFactory._executor = None | ||
|
||
def get_client(self, session_id, num_retries=3): | ||
""" | ||
Configures a client with a specific number of retries. | ||
""" | ||
TelemetryClientFactory.initialize_telemetry_client( | ||
telemetry_enabled=True, | ||
session_id_hex=session_id, | ||
auth_provider=None, | ||
host_url="test.databricks.com", | ||
) | ||
client = TelemetryClientFactory.get_telemetry_client(session_id) | ||
|
||
retry_policy = DatabricksRetryPolicy( | ||
delay_min=0.01, | ||
delay_max=0.02, | ||
stop_after_attempts_duration=2.0, | ||
stop_after_attempts_count=num_retries, | ||
delay_default=0.1, | ||
force_dangerous_codes=[], | ||
urllib3_kwargs={'total': num_retries} | ||
) | ||
adapter = client._session.adapters.get("https://") | ||
adapter.max_retries = retry_policy | ||
return client, adapter | ||
|
||
@pytest.mark.parametrize( | ||
"status_code, description", | ||
[ | ||
(401, "Unauthorized"), | ||
(403, "Forbidden"), | ||
(501, "Not Implemented"), | ||
(200, "Success"), | ||
], | ||
) | ||
def test_non_retryable_status_codes_are_not_retried(self, status_code, description): | ||
""" | ||
Verifies that terminal error codes (401, 403, 501) and success codes (200) are not retried. | ||
""" | ||
# Use the status code in the session ID for easier debugging if it fails | ||
client, _ = self.get_client(f"session-{status_code}") | ||
mock_responses = [{"status": status_code}] | ||
|
||
with patch(PATCH_TARGET, return_value=create_mock_conn(mock_responses)) as mock_get_conn: | ||
client.export_failure_log("TestError", "Test message") | ||
TelemetryClientFactory.close(client._session_id_hex) | ||
|
||
mock_get_conn.return_value.getresponse.assert_called_once() | ||
|
||
def test_exceeds_retry_count_limit(self): | ||
""" | ||
Verifies that the client retries up to the specified number of times before giving up. | ||
Verifies that the client respects the Retry-After header and retries on 429, 502, 503. | ||
""" | ||
num_retries = 3 | ||
expected_total_calls = num_retries + 1 | ||
retry_after = 1 | ||
client, _ = self.get_client("session-exceed-limit", num_retries=num_retries) | ||
mock_responses = [{"status": 503, "headers": {"Retry-After": str(retry_after)}}, {"status": 429}, {"status": 502}, {"status": 503}] | ||
|
||
with patch(PATCH_TARGET, return_value=create_mock_conn(mock_responses)) as mock_get_conn: | ||
start_time = time.time() | ||
client.export_failure_log("TestError", "Test message") | ||
TelemetryClientFactory.close(client._session_id_hex) | ||
end_time = time.time() | ||
|
||
assert mock_get_conn.return_value.getresponse.call_count == expected_total_calls | ||
assert end_time - start_time > retry_after |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.