@@ -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,67 @@ 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+
56+ pub enum ReadysetStatus {
57+ Online ,
58+ SnapshotInProgress ,
59+ Maintenance ,
60+ Unknown ,
61+ }
62+
63+ impl fmt:: Display for ReadysetStatus {
64+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
65+ match self {
66+ ReadysetStatus :: Online => write ! ( f, "Online" ) ,
67+ ReadysetStatus :: SnapshotInProgress => write ! ( f, "Snapshot in progress" ) ,
68+ ReadysetStatus :: Maintenance => write ! ( f, "Maintenance mode" ) ,
69+ ReadysetStatus :: Unknown => write ! ( f, "Unknown" ) ,
70+ }
71+ }
72+ }
73+
74+ impl From < String > for ReadysetStatus {
75+ fn from ( s : String ) -> Self {
76+ match s. to_lowercase ( ) . as_str ( ) {
77+ "online" => ReadysetStatus :: Online ,
78+ "snapshot in progress" => ReadysetStatus :: SnapshotInProgress ,
79+ "maintenance mode" => ReadysetStatus :: Maintenance ,
80+ _ => ReadysetStatus :: Unknown ,
3981 }
4082 }
4183}
@@ -44,7 +86,8 @@ impl From<String> for HostStatus {
4486pub struct Host {
4587 hostname : String ,
4688 port : u16 ,
47- status : HostStatus ,
89+ proxysql_status : ProxyStatus ,
90+ readyset_status : ReadysetStatus ,
4891 conn : Option < Conn > ,
4992}
5093
@@ -62,7 +105,7 @@ impl Host {
62105 /// # Returns
63106 ///
64107 /// A new `Host` instance.
65- pub fn new ( hostname : String , port : u16 , status : String , config : & Config ) -> Host {
108+ pub fn new ( hostname : String , port : u16 , proxysql_status : String , config : & Config ) -> Host {
66109 let conn = match Conn :: new (
67110 OptsBuilder :: new ( )
68111 . ip_or_hostname ( Some ( hostname. clone ( ) ) )
@@ -80,7 +123,8 @@ impl Host {
80123 return Host {
81124 hostname,
82125 port,
83- status : HostStatus :: from ( status) ,
126+ proxysql_status : ProxyStatus :: from ( proxysql_status) ,
127+ readyset_status : ReadysetStatus :: Unknown ,
84128 conn : None ,
85129 } ;
86130 }
@@ -89,7 +133,8 @@ impl Host {
89133 Host {
90134 hostname,
91135 port,
92- status : HostStatus :: from ( status) ,
136+ proxysql_status : ProxyStatus :: from ( proxysql_status) ,
137+ readyset_status : ReadysetStatus :: Unknown ,
93138 conn : Some ( conn) ,
94139 }
95140 }
@@ -112,31 +157,40 @@ impl Host {
112157 self . port
113158 }
114159
115- /// Gets the status of the host.
160+ /// Gets the proxysql status of the host.
116161 ///
117162 /// # Returns
118163 ///
119164 /// The status of the host.
120- pub fn get_status ( & self ) -> HostStatus {
121- self . status
165+ pub fn get_proxysql_status ( & self ) -> ProxyStatus {
166+ self . proxysql_status
122167 }
123168
124- /// Changes the status of the host.
169+ /// Changes the proxysql status of the host.
125170 ///
126171 /// # Arguments
127172 ///
128173 /// * `status` - The new status of the host.
129- pub fn change_status ( & mut self , status : HostStatus ) {
130- self . status = status;
174+ pub fn change_proxysqlstatus ( & mut self , status : ProxyStatus ) {
175+ self . proxysql_status = status;
131176 }
132177
133- /// Checks if the host is online.
178+ /// Checks if the host is online in proxysql .
134179 ///
135180 /// # Returns
136181 ///
137182 /// true if the host is online, false otherwise.
138- pub fn is_online ( & self ) -> bool {
139- self . status == HostStatus :: Online
183+ pub fn is_proxysql_online ( & self ) -> bool {
184+ self . proxysql_status == ProxyStatus :: Online
185+ }
186+
187+ /// Gets the readyset status of the host.
188+ ///
189+ /// # Returns
190+ ///
191+ /// The status of the host.
192+ pub fn get_readyset_status ( & self ) -> ReadysetStatus {
193+ self . readyset_status
140194 }
141195
142196 /// Checks if the Readyset host is ready to serve traffic.
@@ -145,19 +199,28 @@ impl Host {
145199 /// # Returns
146200 ///
147201 /// true if the host is ready, false otherwise.
148- pub fn check_readyset_is_ready ( & mut self ) -> Result < bool , mysql:: Error > {
202+ pub fn check_readyset_is_ready ( & mut self ) -> Result < ProxyStatus , mysql:: Error > {
149203 match & mut self . conn {
150204 Some ( conn) => {
151205 let result = conn. query ( "SHOW READYSET STATUS" ) ;
152206 match result {
153207 Ok ( rows) => {
154208 let rows: Vec < ( String , String ) > = rows;
155209 for ( field, value) in rows {
156- if field == "Snapshot Status" {
157- return Ok ( value == "Completed" ) ;
210+ if field == "Snapshot Status" && value == "Completed" {
211+ self . readyset_status = ReadysetStatus :: Online ;
212+ return Ok ( ProxyStatus :: Online ) ;
213+ } else if field == "Snapshot Status" && value == "In Progress" {
214+ self . readyset_status = ReadysetStatus :: SnapshotInProgress ;
215+ return Ok ( ProxyStatus :: Shunned ) ;
216+ } else if field == "Status" {
217+ let status = ReadysetStatus :: from ( value) ;
218+ self . readyset_status = status;
219+ return Ok ( status. into ( ) ) ;
158220 }
159221 }
160- Ok ( false )
222+ self . readyset_status = ReadysetStatus :: Unknown ;
223+ Ok ( ProxyStatus :: Shunned )
161224 }
162225 Err ( err) => Err ( mysql:: Error :: IoError ( std:: io:: Error :: new (
163226 std:: io:: ErrorKind :: Other ,
0 commit comments