Skip to content

Commit 99d65fc

Browse files
authored
Check server versions on startup & refactor (#48)
* Refactor and check server parameters * warnings * fix validator
1 parent 206fdc9 commit 99d65fc

File tree

4 files changed

+37
-50
lines changed

4 files changed

+37
-50
lines changed

src/admin.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use bytes::{Buf, BufMut, BytesMut};
22
use log::trace;
33
use tokio::net::tcp::OwnedWriteHalf;
44

5-
use std::collections::HashMap;
6-
75
use crate::constants::{OID_NUMERIC, OID_TEXT};
86
use crate::errors::Error;
97
use crate::messages::write_all_half;
@@ -50,7 +48,7 @@ pub async fn show_stats(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
5048
"avg_wait_time",
5149
];
5250

53-
let stats = get_stats().unwrap_or(HashMap::new());
51+
let stats = get_stats();
5452
let mut res = BytesMut::new();
5553
let mut row_desc = BytesMut::new();
5654
let mut data_row = BytesMut::new();

src/messages.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ pub async fn startup(stream: &mut TcpStream, user: &str, database: &str) -> Resu
9191
}
9292
}
9393

94-
/// Parse StartupMessage parameters.
95-
/// e.g. user, database, application_name, etc.
96-
pub fn parse_startup(mut bytes: BytesMut) -> Result<HashMap<String, String>, Error> {
94+
/// Parse the params the server sends as a key/value format.
95+
pub fn parse_params(mut bytes: BytesMut) -> Result<HashMap<String, String>, Error> {
9796
let mut result = HashMap::new();
9897
let mut buf = Vec::new();
9998
let mut tmp = String::new();
@@ -115,7 +114,7 @@ pub fn parse_startup(mut bytes: BytesMut) -> Result<HashMap<String, String>, Err
115114

116115
// Expect pairs of name and value
117116
// and at least one pair to be present.
118-
if buf.len() % 2 != 0 && buf.len() >= 2 {
117+
if buf.len() % 2 != 0 || buf.len() < 2 {
119118
return Err(Error::ClientBadStartup);
120119
}
121120

@@ -127,6 +126,14 @@ pub fn parse_startup(mut bytes: BytesMut) -> Result<HashMap<String, String>, Err
127126
i += 2;
128127
}
129128

129+
Ok(result)
130+
}
131+
132+
/// Parse StartupMessage parameters.
133+
/// e.g. user, database, application_name, etc.
134+
pub fn parse_startup(bytes: BytesMut) -> Result<HashMap<String, String>, Error> {
135+
let result = parse_params(bytes)?;
136+
130137
// Minimum required parameters
131138
// I want to have the user at the very minimum, according to the protocol spec.
132139
if !result.contains_key("user") {

src/pool.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,22 @@ impl ConnectionPool {
126126
};
127127

128128
let mut proxy = connection.0;
129-
let _address = connection.1;
129+
let address = connection.1;
130130
let server = &mut *proxy;
131131

132-
server_infos.push(server.server_info());
132+
let server_info = server.server_info();
133+
134+
if server_infos.len() > 0 {
135+
// Compare against the last server checked.
136+
if server_info != server_infos[server_infos.len() - 1] {
137+
warn!(
138+
"{:?} has different server configuration than the last server",
139+
address
140+
);
141+
}
142+
}
143+
144+
server_infos.push(server_info);
133145
}
134146
}
135147

src/stats.rs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use log::{debug, error, info};
2-
use once_cell::sync::OnceCell;
1+
use log::{debug, info};
2+
use once_cell::sync::Lazy;
3+
use parking_lot::Mutex;
34
use statsd::Client;
4-
/// Events collector and publisher.
55
use tokio::sync::mpsc::{Receiver, Sender};
66

77
use std::collections::HashMap;
8-
use std::sync::{Arc, Mutex};
98

109
use crate::config::get_config;
1110

12-
static LATEST_STATS: OnceCell<Arc<Mutex<HashMap<String, i64>>>> = OnceCell::new();
11+
// Stats used in SHOW STATS
12+
static LATEST_STATS: Lazy<Mutex<HashMap<String, i64>>> = Lazy::new(|| Mutex::new(HashMap::new()));
1313
static STAT_PERIOD: u64 = 15000; //15 seconds
1414

1515
#[derive(Debug, Clone, Copy)]
@@ -187,16 +187,6 @@ impl Reporter {
187187

188188
let _ = self.tx.try_send(event);
189189
}
190-
191-
// pub fn flush_to_statsd(&self) {
192-
// let event = Event {
193-
// name: EventName::FlushStatsToStatsD,
194-
// value: 0,
195-
// process_id: None,
196-
// };
197-
198-
// let _ = self.tx.try_send(event);
199-
// }
200190
}
201191

202192
pub struct Collector {
@@ -217,13 +207,6 @@ impl Collector {
217207
pub async fn collect(&mut self) {
218208
info!("Events reporter started");
219209

220-
match LATEST_STATS.set(Arc::new(Mutex::new(HashMap::new()))) {
221-
Ok(_) => (),
222-
Err(_) => {
223-
error!("Latest stats will not be available");
224-
}
225-
};
226-
227210
let mut stats = HashMap::from([
228211
("total_query_count", 0),
229212
("total_xact_count", 0),
@@ -400,16 +383,10 @@ impl Collector {
400383
debug!("{:?}", stats);
401384

402385
// Update latest stats used in SHOW STATS
403-
match LATEST_STATS.get() {
404-
Some(arc) => {
405-
let mut guard = arc.lock().unwrap();
406-
for (key, value) in &stats {
407-
guard.insert(key.to_string(), value.clone());
408-
}
409-
}
410-
411-
None => (),
412-
};
386+
let mut guard = LATEST_STATS.lock();
387+
for (key, value) in &stats {
388+
guard.insert(key.to_string(), value.clone());
389+
}
413390

414391
let mut pipeline = self.client.pipeline();
415392

@@ -440,13 +417,6 @@ impl Collector {
440417
}
441418
}
442419

443-
pub fn get_stats() -> Option<HashMap<String, i64>> {
444-
match LATEST_STATS.get() {
445-
Some(arc) => {
446-
let guard = arc.lock().unwrap();
447-
Some(guard.clone())
448-
}
449-
450-
None => None,
451-
}
420+
pub fn get_stats() -> HashMap<String, i64> {
421+
LATEST_STATS.lock().clone()
452422
}

0 commit comments

Comments
 (0)