-
Notifications
You must be signed in to change notification settings - Fork 128
Circuit breaker changes using pybreaker #705
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
37ec282
Added driver connection params
nikhilsuri-db 2504053
Added model fields for chunk/result latency
nikhilsuri-db ef41f4c
fixed linting issues
nikhilsuri-db 2f54be8
lint issue fixing
nikhilsuri-db db93974
circuit breaker changes using pybreaker
nikhilsuri-db 1f9c4d3
Added interface layer top of http client to use circuit rbeaker
nikhilsuri-db 939b548
Added test cases to validate ciruit breaker
nikhilsuri-db 6c72f86
fixing broken tests
nikhilsuri-db ac845a5
fixed linting issues
nikhilsuri-db a602c39
fixed failing test cases
nikhilsuri-db c1b6e25
fixed urllib3 issue
nikhilsuri-db e3d85f4
added more test cases for telemetry
nikhilsuri-db 9dfb623
simplified CB config
nikhilsuri-db e7e8b4b
poetry lock
nikhilsuri-db dab4b38
fix minor issues & improvement
nikhilsuri-db e1e08b0
improved circuit breaker for handling only 429/503
nikhilsuri-db b527e7c
linting issue fixed
nikhilsuri-db 2b45814
raise CB only for 429/503
nikhilsuri-db 1193af7
fix broken test cases
nikhilsuri-db aa459e9
fixed untyped references
nikhilsuri-db 4cb87b1
Merge remote-tracking branch 'origin/main' into PECOBLR-993
nikhilsuri-db 7cbc4c8
added more test to verify the changes
nikhilsuri-db c646335
description changed
nikhilsuri-db bcd6760
remove cb congig class to constants
nikhilsuri-db 4376b6d
removed mocked reponse and use a new exlucded exception in CB
nikhilsuri-db d9e7c89
fixed broken test
nikhilsuri-db 1b8e47c
added e2e test to verify circuit breaker
nikhilsuri-db 4c75963
Merge remote-tracking branch 'origin/main' into PECOBLR-993
nikhilsuri-db 172e03f
lower log level for telemetry
nikhilsuri-db dbd915f
Merge remote-tracking branch 'origin/main' into PECOBLR-993
nikhilsuri-db 35b7459
fixed broken test, removed tests on log assertions
nikhilsuri-db 5cfde8c
modified unit to reduce the noise and follow dry principle
nikhilsuri-db 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
112 changes: 112 additions & 0 deletions
112
src/databricks/sql/telemetry/circuit_breaker_manager.py
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,112 @@ | ||
| """ | ||
| Circuit breaker implementation for telemetry requests. | ||
| This module provides circuit breaker functionality to prevent telemetry failures | ||
| from impacting the main SQL operations. It uses pybreaker library to implement | ||
| the circuit breaker pattern. | ||
| """ | ||
|
|
||
| import logging | ||
| import threading | ||
| from typing import Dict | ||
|
|
||
| import pybreaker | ||
| from pybreaker import CircuitBreaker, CircuitBreakerError, CircuitBreakerListener | ||
|
|
||
| from databricks.sql.exc import TelemetryNonRateLimitError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Circuit Breaker Constants | ||
| MINIMUM_CALLS = 20 # Number of failures before circuit opens | ||
nikhilsuri-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| RESET_TIMEOUT = 30 # Seconds to wait before trying to close circuit | ||
| NAME_PREFIX = "telemetry-circuit-breaker" | ||
|
|
||
| # Circuit Breaker State Constants (used in logging) | ||
| CIRCUIT_BREAKER_STATE_OPEN = "open" | ||
| CIRCUIT_BREAKER_STATE_CLOSED = "closed" | ||
| CIRCUIT_BREAKER_STATE_HALF_OPEN = "half-open" | ||
|
|
||
| # Logging Message Constants | ||
| LOG_CIRCUIT_BREAKER_STATE_CHANGED = "Circuit breaker state changed from %s to %s for %s" | ||
| LOG_CIRCUIT_BREAKER_OPENED = ( | ||
| "Circuit breaker opened for %s - telemetry requests will be blocked" | ||
| ) | ||
| LOG_CIRCUIT_BREAKER_CLOSED = ( | ||
| "Circuit breaker closed for %s - telemetry requests will be allowed" | ||
| ) | ||
| LOG_CIRCUIT_BREAKER_HALF_OPEN = ( | ||
| "Circuit breaker half-open for %s - testing telemetry requests" | ||
| ) | ||
|
|
||
|
|
||
| class CircuitBreakerStateListener(CircuitBreakerListener): | ||
nikhilsuri-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Listener for circuit breaker state changes.""" | ||
|
|
||
| def before_call(self, cb: CircuitBreaker, func, *args, **kwargs) -> None: | ||
| """Called before the circuit breaker calls a function.""" | ||
| pass | ||
|
|
||
| def failure(self, cb: CircuitBreaker, exc: BaseException) -> None: | ||
| """Called when a function called by the circuit breaker fails.""" | ||
| pass | ||
|
|
||
| def success(self, cb: CircuitBreaker) -> None: | ||
| """Called when a function called by the circuit breaker succeeds.""" | ||
| pass | ||
|
|
||
| def state_change(self, cb: CircuitBreaker, old_state, new_state) -> None: | ||
| """Called when the circuit breaker state changes.""" | ||
| old_state_name = old_state.name if old_state else "None" | ||
| new_state_name = new_state.name if new_state else "None" | ||
|
|
||
| logger.info( | ||
| LOG_CIRCUIT_BREAKER_STATE_CHANGED, old_state_name, new_state_name, cb.name | ||
| ) | ||
|
|
||
| if new_state_name == CIRCUIT_BREAKER_STATE_OPEN: | ||
| logger.warning(LOG_CIRCUIT_BREAKER_OPENED, cb.name) | ||
| elif new_state_name == CIRCUIT_BREAKER_STATE_CLOSED: | ||
| logger.info(LOG_CIRCUIT_BREAKER_CLOSED, cb.name) | ||
| elif new_state_name == CIRCUIT_BREAKER_STATE_HALF_OPEN: | ||
| logger.info(LOG_CIRCUIT_BREAKER_HALF_OPEN, cb.name) | ||
|
|
||
|
|
||
| class CircuitBreakerManager: | ||
| """ | ||
| Manages circuit breaker instances for telemetry requests. | ||
| Creates and caches circuit breaker instances per host to ensure telemetry | ||
| failures don't impact main SQL operations. | ||
| """ | ||
|
|
||
| _instances: Dict[str, CircuitBreaker] = {} | ||
| _lock = threading.RLock() | ||
|
|
||
| @classmethod | ||
| def get_circuit_breaker(cls, host: str) -> CircuitBreaker: | ||
| """ | ||
| Get or create a circuit breaker instance for the specified host. | ||
| Args: | ||
| host: The hostname for which to get the circuit breaker | ||
| Returns: | ||
| CircuitBreaker instance for the host | ||
| """ | ||
| with cls._lock: | ||
| if host not in cls._instances: | ||
| breaker = CircuitBreaker( | ||
| fail_max=MINIMUM_CALLS, | ||
| reset_timeout=RESET_TIMEOUT, | ||
| name=f"{NAME_PREFIX}-{host}", | ||
| exclude=[ | ||
| TelemetryNonRateLimitError | ||
| ], # Don't count these as failures | ||
| ) | ||
| # Add state change listener for logging | ||
| breaker.add_listener(CircuitBreakerStateListener()) | ||
| cls._instances[host] = breaker | ||
| logger.debug("Created circuit breaker for host: %s", host) | ||
|
|
||
| return cls._instances[host] | ||
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.
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.