Skip to content

Commit 3a729bb

Browse files
authored
Minor refactor for configs (#172)
* Changes shard struct to use vector of ServerConfig * Adds to query router * Change client disconnect with error message to warn instead of debug * Add warning logs for clean up actions
1 parent 85cc2f4 commit 3a729bb

File tree

5 files changed

+32
-39
lines changed

5 files changed

+32
-39
lines changed

src/config.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ static CONFIG: Lazy<ArcSwap<Config>> = Lazy::new(|| ArcSwap::from_pointee(Config
2323
/// Server role: primary or replica.
2424
#[derive(Clone, PartialEq, Serialize, Deserialize, Hash, std::cmp::Eq, Debug, Copy)]
2525
pub enum Role {
26+
#[serde(alias = "primary", alias = "Primary")]
2627
Primary,
28+
#[serde(alias = "replica", alias = "Replica")]
2729
Replica,
2830
}
2931

@@ -202,17 +204,28 @@ impl Default for Pool {
202204
}
203205
}
204206

207+
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, Hash, Eq)]
208+
pub struct ServerConfig {
209+
pub host: String,
210+
pub port: u16,
211+
pub role: Role,
212+
}
213+
205214
/// Shard configuration.
206215
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
207216
pub struct Shard {
208217
pub database: String,
209-
pub servers: Vec<(String, u16, String)>,
218+
pub servers: Vec<ServerConfig>,
210219
}
211220

212221
impl Default for Shard {
213222
fn default() -> Shard {
214223
Shard {
215-
servers: vec![(String::from("localhost"), 5432, String::from("primary"))],
224+
servers: vec![ServerConfig {
225+
host: String::from("localhost"),
226+
port: 5432,
227+
role: Role::Primary,
228+
}],
216229
database: String::from("postgres"),
217230
}
218231
}
@@ -538,23 +551,10 @@ pub async fn parse(path: &str) -> Result<(), Error> {
538551
dup_check.insert(server);
539552

540553
// Check that we define only zero or one primary.
541-
match server.2.as_ref() {
542-
"primary" => primary_count += 1,
554+
match server.role {
555+
Role::Primary => primary_count += 1,
543556
_ => (),
544557
};
545-
546-
// Check role spelling.
547-
match server.2.as_ref() {
548-
"primary" => (),
549-
"replica" => (),
550-
_ => {
551-
error!(
552-
"Shard {} server role must be either 'primary' or 'replica', got: '{}'",
553-
shard.0, server.2
554-
);
555-
return Err(Error::BadConfig);
556-
}
557-
};
558558
}
559559

560560
if primary_count > 1 {
@@ -617,12 +617,12 @@ mod test {
617617
assert_eq!(get_config().pools["simple_db"].users.len(), 1);
618618

619619
assert_eq!(
620-
get_config().pools["sharded_db"].shards["0"].servers[0].0,
620+
get_config().pools["sharded_db"].shards["0"].servers[0].host,
621621
"127.0.0.1"
622622
);
623623
assert_eq!(
624-
get_config().pools["sharded_db"].shards["1"].servers[0].2,
625-
"primary"
624+
get_config().pools["sharded_db"].shards["1"].servers[0].role,
625+
Role::Primary
626626
);
627627
assert_eq!(
628628
get_config().pools["sharded_db"].shards["1"].database,
@@ -640,11 +640,11 @@ mod test {
640640
assert_eq!(get_config().pools["sharded_db"].default_role, "any");
641641

642642
assert_eq!(
643-
get_config().pools["simple_db"].shards["0"].servers[0].0,
643+
get_config().pools["simple_db"].shards["0"].servers[0].host,
644644
"127.0.0.1"
645645
);
646646
assert_eq!(
647-
get_config().pools["simple_db"].shards["0"].servers[0].1,
647+
get_config().pools["simple_db"].shards["0"].servers[0].port,
648648
5432
649649
);
650650
assert_eq!(

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern crate tokio;
3737
extern crate tokio_rustls;
3838
extern crate toml;
3939

40-
use log::{debug, error, info};
40+
use log::{error, info, warn};
4141
use parking_lot::Mutex;
4242
use pgcat::format_duration;
4343
use tokio::net::TcpListener;
@@ -279,7 +279,7 @@ async fn main() {
279279
}
280280
}
281281

282-
debug!("Client disconnected with error {:?}", err);
282+
warn!("Client disconnected with error {:?}", err);
283283
}
284284
};
285285
});

src/pool.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,12 @@ impl ConnectionPool {
143143
let mut replica_number = 0;
144144

145145
for server in shard.servers.iter() {
146-
let role = match server.2.as_ref() {
147-
"primary" => Role::Primary,
148-
"replica" => Role::Replica,
149-
_ => {
150-
error!("Config error: server role can be 'primary' or 'replica', have: '{}'. Defaulting to 'replica'.", server.2);
151-
Role::Replica
152-
}
153-
};
154-
155146
let address = Address {
156147
id: address_id,
157148
database: shard.database.clone(),
158-
host: server.0.clone(),
159-
port: server.1 as u16,
160-
role: role,
149+
host: server.host.clone(),
150+
port: server.port,
151+
role: server.role,
161152
address_index,
162153
replica_number,
163154
shard: shard_idx.parse::<usize>().unwrap(),
@@ -168,7 +159,7 @@ impl ConnectionPool {
168159
address_id += 1;
169160
address_index += 1;
170161

171-
if role == Role::Replica {
162+
if server.role == Role::Replica {
172163
replica_number += 1;
173164
}
174165

src/query_router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ impl QueryRouter {
169169

170170
Command::ShowShard => self.shard().to_string(),
171171
Command::ShowServerRole => match self.active_role {
172-
Some(Role::Primary) => String::from("primary"),
173-
Some(Role::Replica) => String::from("replica"),
172+
Some(Role::Primary) => Role::Primary.to_string(),
173+
Some(Role::Replica) => Role::Replica.to_string(),
174174
None => {
175175
if self.query_parser_enabled {
176176
String::from("auto")

src/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ impl Server {
591591
// server connection thrashing if clients repeatedly do this.
592592
// Instead, we ROLLBACK that transaction before putting the connection back in the pool
593593
if self.in_transaction() {
594+
warn!("Server returned while still in transaction, rolling back transaction");
594595
self.query("ROLLBACK").await?;
595596
}
596597

@@ -600,6 +601,7 @@ impl Server {
600601
// send `DISCARD ALL` if we think the session is altered instead of just sending
601602
// it before each checkin.
602603
if self.needs_cleanup {
604+
warn!("Server returned with session state altered, discarding state");
603605
self.query("DISCARD ALL").await?;
604606
self.needs_cleanup = false;
605607
}

0 commit comments

Comments
 (0)