Skip to main content

llm_optimizer_processor/actuator/
error.rs

1//! Actuator error types
2
3use thiserror::Error;
4
5/// Result type for Actuator operations
6pub type ActuatorResult<T> = Result<T, ActuatorError>;
7
8/// Error types for the Actuator
9#[derive(Error, Debug, Clone, PartialEq)]
10pub enum ActuatorError {
11    /// Configuration error
12    #[error("Configuration error: {0}")]
13    ConfigError(String),
14
15    /// Deployment failed
16    #[error("Deployment failed: {0}")]
17    DeploymentFailed(String),
18
19    /// Rollback failed
20    #[error("Rollback failed: {0}")]
21    RollbackFailed(String),
22
23    /// Health check failed
24    #[error("Health check failed: {0}")]
25    HealthCheckFailed(String),
26
27    /// Validation failed
28    #[error("Validation failed: {0}")]
29    ValidationFailed(String),
30
31    /// Traffic splitting error
32    #[error("Traffic splitting error: {0}")]
33    TrafficSplitError(String),
34
35    /// State error
36    #[error("State error: {0}")]
37    StateError(String),
38
39    /// Not running
40    #[error("Actuator is not running")]
41    NotRunning,
42
43    /// Invalid state transition
44    #[error("Invalid state transition from {from:?} to {to:?}")]
45    InvalidStateTransition {
46        from: ActuatorState,
47        to: ActuatorState,
48    },
49
50    /// Timeout
51    #[error("Operation timeout after {0}ms")]
52    Timeout(u64),
53
54    /// Internal error
55    #[error("Internal error: {0}")]
56    Internal(String),
57}
58
59/// Actuator state
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
61pub enum ActuatorState {
62    /// Initialized but not started
63    Initialized,
64    /// Starting up
65    Starting,
66    /// Running and ready to execute
67    Running,
68    /// Executing a deployment
69    Deploying,
70    /// Executing a rollback
71    RollingBack,
72    /// Draining before shutdown
73    Draining,
74    /// Stopped
75    Stopped,
76    /// Failed state
77    Failed,
78}
79
80impl ActuatorState {
81    /// Check if the state allows executing deployments
82    pub fn can_deploy(&self) -> bool {
83        matches!(self, ActuatorState::Running)
84    }
85
86    /// Check if the state allows rollback
87    pub fn can_rollback(&self) -> bool {
88        matches!(self, ActuatorState::Running | ActuatorState::Deploying)
89    }
90
91    /// Check if transition to another state is valid
92    pub fn can_transition_to(&self, to: ActuatorState) -> bool {
93        use ActuatorState::*;
94        matches!(
95            (self, to),
96            (Initialized, Starting)
97                | (Starting, Running)
98                | (Starting, Failed)
99                | (Running, Deploying)
100                | (Running, RollingBack)
101                | (Running, Draining)
102                | (Running, Failed)
103                | (Deploying, Running)
104                | (Deploying, RollingBack)
105                | (Deploying, Failed)
106                | (RollingBack, Running)
107                | (RollingBack, Failed)
108                | (Draining, Stopped)
109                | (Draining, Failed)
110                | (Failed, Initialized)
111        )
112    }
113}
114
115/// Deployment state
116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
117pub enum DeploymentState {
118    /// Deployment is pending
119    Pending,
120    /// Validating configuration
121    Validating,
122    /// Rolling out (canary)
123    RollingOut,
124    /// Monitoring health
125    Monitoring,
126    /// Completed successfully
127    Completed,
128    /// Rolling back
129    RollingBack,
130    /// Rolled back
131    RolledBack,
132    /// Failed
133    Failed,
134}
135
136impl DeploymentState {
137    /// Check if deployment is in a terminal state
138    pub fn is_terminal(&self) -> bool {
139        matches!(
140            self,
141            DeploymentState::Completed
142                | DeploymentState::RolledBack
143                | DeploymentState::Failed
144        )
145    }
146
147    /// Check if deployment can be rolled back
148    pub fn can_rollback(&self) -> bool {
149        matches!(
150            self,
151            DeploymentState::RollingOut | DeploymentState::Monitoring
152        )
153    }
154}
155
156impl std::fmt::Display for DeploymentState {
157    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
158        match self {
159            DeploymentState::Pending => write!(f, "Pending"),
160            DeploymentState::Validating => write!(f, "Validating"),
161            DeploymentState::RollingOut => write!(f, "RollingOut"),
162            DeploymentState::Monitoring => write!(f, "Monitoring"),
163            DeploymentState::Completed => write!(f, "Completed"),
164            DeploymentState::RollingBack => write!(f, "RollingBack"),
165            DeploymentState::RolledBack => write!(f, "RolledBack"),
166            DeploymentState::Failed => write!(f, "Failed"),
167        }
168    }
169}