@@ -6,7 +6,7 @@ use std::time::Duration;
66#[ allow( dead_code) ]
77/// Defines the possible status of a host
88#[ derive( PartialEq , Clone , Copy ) ]
9- pub enum HostStatus {
9+ pub enum ProxyStatus {
1010 /// backend server is fully operational
1111 Online ,
1212 /// backend sever is temporarily taken out of use because of either too many connection errors in a time that was too short, or the replication lag exceeded the allowed threshold
@@ -17,25 +17,66 @@ pub enum HostStatus {
1717 OfflineHard ,
1818}
1919
20- impl fmt:: Display for HostStatus {
20+ impl fmt:: Display for ProxyStatus {
2121 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2222 match self {
23- HostStatus :: Online => write ! ( f, "ONLINE" ) ,
24- HostStatus :: Shunned => write ! ( f, "SHUNNED" ) ,
25- HostStatus :: OfflineSoft => write ! ( f, "OFFLINE_SOFT" ) ,
26- HostStatus :: OfflineHard => write ! ( f, "OFFLINE_HARD" ) ,
23+ ProxyStatus :: Online => write ! ( f, "ONLINE" ) ,
24+ ProxyStatus :: Shunned => write ! ( f, "SHUNNED" ) ,
25+ ProxyStatus :: OfflineSoft => write ! ( f, "OFFLINE_SOFT" ) ,
26+ ProxyStatus :: OfflineHard => write ! ( f, "OFFLINE_HARD" ) ,
2727 }
2828 }
2929}
3030
31- impl From < String > for HostStatus {
31+ impl From < String > for ProxyStatus {
3232 fn from ( s : String ) -> Self {
3333 match s. to_uppercase ( ) . as_str ( ) {
34- "ONLINE" => HostStatus :: Online ,
35- "SHUNNED" => HostStatus :: Shunned ,
36- "OFFLINE_SOFT" => HostStatus :: OfflineSoft ,
37- "OFFLINE_HARD" => HostStatus :: OfflineHard ,
38- _ => HostStatus :: Online ,
34+ "ONLINE" => ProxyStatus :: Online ,
35+ "SHUNNED" => ProxyStatus :: Shunned ,
36+ "OFFLINE_SOFT" => ProxyStatus :: OfflineSoft ,
37+ "OFFLINE_HARD" => ProxyStatus :: OfflineHard ,
38+ _ => ProxyStatus :: Online ,
39+ }
40+ }
41+ }
42+
43+ impl From < ReadysetStatus > for ProxyStatus {
44+ fn from ( status : ReadysetStatus ) -> Self {
45+ match status {
46+ ReadysetStatus :: Online => ProxyStatus :: Online ,
47+ ReadysetStatus :: SnapshotInProgress => ProxyStatus :: Shunned ,
48+ ReadysetStatus :: Maintenance => ProxyStatus :: OfflineSoft ,
49+ ReadysetStatus :: Unknown => ProxyStatus :: Shunned ,
50+ }
51+ }
52+ }
53+
54+ #[ derive( PartialEq , Clone , Copy ) ]
55+ pub enum ReadysetStatus {
56+ Online ,
57+ SnapshotInProgress ,
58+ Maintenance ,
59+ Unknown ,
60+ }
61+
62+ impl fmt:: Display for ReadysetStatus {
63+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
64+ match self {
65+ ReadysetStatus :: Online => write ! ( f, "Online" ) ,
66+ ReadysetStatus :: SnapshotInProgress => write ! ( f, "Snapshot in progress" ) ,
67+ ReadysetStatus :: Maintenance => write ! ( f, "Maintenance mode" ) ,
68+ ReadysetStatus :: Unknown => write ! ( f, "Unknown" ) ,
69+ }
70+ }
71+ }
72+
73+ impl From < String > for ReadysetStatus {
74+ fn from ( s : String ) -> Self {
75+ match s. to_lowercase ( ) . as_str ( ) {
76+ "online" => ReadysetStatus :: Online ,
77+ "snapshot in progress" => ReadysetStatus :: SnapshotInProgress ,
78+ "maintenance mode" => ReadysetStatus :: Maintenance ,
79+ _ => ReadysetStatus :: Unknown ,
3980 }
4081 }
4182}
@@ -44,7 +85,8 @@ impl From<String> for HostStatus {
4485pub struct Host {
4586 hostname : String ,
4687 port : u16 ,
47- status : HostStatus ,
88+ proxysql_status : ProxyStatus ,
89+ readyset_status : ReadysetStatus ,
4890 conn : Option < Conn > ,
4991}
5092
@@ -62,7 +104,7 @@ impl Host {
62104 /// # Returns
63105 ///
64106 /// A new `Host` instance.
65- pub fn new ( hostname : String , port : u16 , status : String , config : & Config ) -> Host {
107+ pub fn new ( hostname : String , port : u16 , proxysql_status : String , config : & Config ) -> Host {
66108 let conn = match Conn :: new (
67109 OptsBuilder :: new ( )
68110 . ip_or_hostname ( Some ( hostname. clone ( ) ) )
@@ -80,7 +122,8 @@ impl Host {
80122 return Host {
81123 hostname,
82124 port,
83- status : HostStatus :: from ( status) ,
125+ proxysql_status : ProxyStatus :: from ( proxysql_status) ,
126+ readyset_status : ReadysetStatus :: Unknown ,
84127 conn : None ,
85128 } ;
86129 }
@@ -89,7 +132,8 @@ impl Host {
89132 Host {
90133 hostname,
91134 port,
92- status : HostStatus :: from ( status) ,
135+ proxysql_status : ProxyStatus :: from ( proxysql_status) ,
136+ readyset_status : ReadysetStatus :: Unknown ,
93137 conn : Some ( conn) ,
94138 }
95139 }
@@ -112,31 +156,40 @@ impl Host {
112156 self . port
113157 }
114158
115- /// Gets the status of the host.
159+ /// Gets the proxysql status of the host.
116160 ///
117161 /// # Returns
118162 ///
119163 /// The status of the host.
120- pub fn get_status ( & self ) -> HostStatus {
121- self . status
164+ pub fn get_proxysql_status ( & self ) -> ProxyStatus {
165+ self . proxysql_status
122166 }
123167
124- /// Changes the status of the host.
168+ /// Changes the proxysql status of the host.
125169 ///
126170 /// # Arguments
127171 ///
128172 /// * `status` - The new status of the host.
129- pub fn change_status ( & mut self , status : HostStatus ) {
130- self . status = status;
173+ pub fn change_proxysql_status ( & mut self , status : ProxyStatus ) {
174+ self . proxysql_status = status;
131175 }
132176
133- /// Checks if the host is online.
177+ /// Checks if the host is online in proxysql .
134178 ///
135179 /// # Returns
136180 ///
137181 /// true if the host is online, false otherwise.
138- pub fn is_online ( & self ) -> bool {
139- self . status == HostStatus :: Online
182+ pub fn is_proxysql_online ( & self ) -> bool {
183+ self . proxysql_status == ProxyStatus :: Online
184+ }
185+
186+ /// Gets the readyset status of the host.
187+ ///
188+ /// # Returns
189+ ///
190+ /// The status of the host.
191+ pub fn get_readyset_status ( & self ) -> ReadysetStatus {
192+ self . readyset_status
140193 }
141194
142195 /// Checks if the Readyset host is ready to serve traffic.
@@ -145,19 +198,28 @@ impl Host {
145198 /// # Returns
146199 ///
147200 /// true if the host is ready, false otherwise.
148- pub fn check_readyset_is_ready ( & mut self ) -> Result < bool , mysql:: Error > {
201+ pub fn check_readyset_is_ready ( & mut self ) -> Result < ProxyStatus , mysql:: Error > {
149202 match & mut self . conn {
150203 Some ( conn) => {
151204 let result = conn. query ( "SHOW READYSET STATUS" ) ;
152205 match result {
153206 Ok ( rows) => {
154207 let rows: Vec < ( String , String ) > = rows;
155208 for ( field, value) in rows {
156- if field == "Snapshot Status" {
157- return Ok ( value == "Completed" ) ;
209+ if field == "Snapshot Status" && value == "Completed" {
210+ self . readyset_status = ReadysetStatus :: Online ;
211+ return Ok ( ProxyStatus :: Online ) ;
212+ } else if field == "Snapshot Status" && value == "In Progress" {
213+ self . readyset_status = ReadysetStatus :: SnapshotInProgress ;
214+ return Ok ( ProxyStatus :: Shunned ) ;
215+ } else if field == "Status" {
216+ let status = ReadysetStatus :: from ( value) ;
217+ self . readyset_status = status;
218+ return Ok ( status. into ( ) ) ;
158219 }
159220 }
160- Ok ( false )
221+ self . readyset_status = ReadysetStatus :: Unknown ;
222+ Ok ( ProxyStatus :: Shunned )
161223 }
162224 Err ( err) => Err ( mysql:: Error :: IoError ( std:: io:: Error :: new (
163225 std:: io:: ErrorKind :: Other ,
0 commit comments