Resilience (core.resilience)¶
Retry¶
Retry execution with exponential backoff and jitter.
- class pyspark_pipeline_framework.core.resilience.retry.RetryExecutor(config, jitter_factor=0.25, sleep_func=None)[source]¶
Bases:
objectExecutes callables with configurable retry logic.
Uses exponential backoff with jitter based on a
RetryConfig.- Parameters:
config (RetryConfig) – Retry configuration specifying attempts, delays, and retryable exceptions.
jitter_factor (float) – Random jitter multiplier applied to each delay (0 disables jitter).
sleep_func (Callable[[float], None] | None) – Injectable sleep function for testing. Defaults to
time.sleep.
- property config: RetryConfig¶
Return the retry configuration.
- calculate_delay(attempt)[source]¶
Calculate the delay in seconds for a given attempt number.
Uses exponential backoff:
min(initial * multiplier^attempt, max) * (1 + jitter).
- is_retryable(error)[source]¶
Check whether an exception should be retried.
Matches against
retry_on_exceptionsfrom config. A name without a dot is matched against the class__name__; a name with a dot is matched against the fully-qualifiedmodule.classpath.
Circuit Breaker¶
Circuit breaker pattern for fault tolerance.
- class pyspark_pipeline_framework.core.resilience.circuit_breaker.CircuitState(*values)[source]¶
Bases:
EnumPossible states of a circuit breaker.
- CLOSED = 'closed'¶
- OPEN = 'open'¶
- HALF_OPEN = 'half_open'¶
- exception pyspark_pipeline_framework.core.resilience.circuit_breaker.CircuitBreakerOpenError(component_name, time_until_reset)[source]¶
Bases:
ExceptionRaised when a call is attempted on an open circuit breaker.
- class pyspark_pipeline_framework.core.resilience.circuit_breaker.CircuitBreaker(config, name='default', on_state_change=None, clock=None)[source]¶
Bases:
objectThread-safe circuit breaker preventing cascading failures.
- State machine:
CLOSED –[failures >= threshold]–> OPEN OPEN –[timeout elapsed]——–> HALF_OPEN HALF_OPEN –[successes >= threshold]–> CLOSED HALF_OPEN –[any failure]———–> OPEN
- Parameters:
config (CircuitBreakerConfig) – Circuit breaker configuration.
name (str) – Identifier for this breaker instance (used in errors/logs).
on_state_change (Callable[[CircuitState, CircuitState], None] | None) – Optional callback
(old_state, new_state)invoked on transitions.clock (Callable[[], float] | None) – Injectable monotonic clock for testing. Defaults to
time.monotonic.
- property config: CircuitBreakerConfig¶
Return the circuit breaker configuration.
- property state: CircuitState¶
Return the current state, checking for timeout transitions.
- property time_until_reset: float¶
Return seconds remaining before an OPEN breaker transitions to HALF_OPEN.
Returns
0.0when the breaker is not in the OPEN state.
- call(func)[source]¶
Execute func through the circuit breaker.
- Parameters:
func (Callable[[], T]) – Zero-argument callable.
- Returns:
The return value of func.
- Raises:
CircuitBreakerOpenError – If the circuit is OPEN or HALF_OPEN call limit reached.
- Return type:
T