Skip to content

Commit 8196e12

Browse files
authored
More client/server integration tests (#173)
* more client tests * Count healthchecks, fix assignment overcounting * remove diff * diff * let the os handle the port
1 parent 9b6dd98 commit 8196e12

File tree

8 files changed

+205
-25
lines changed

8 files changed

+205
-25
lines changed

pgdog/src/admin/show_servers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Command for ShowServers {
6868
.add(server.stats.total.queries)
6969
.add(server.stats.total.rollbacks)
7070
.add(server.stats.total.prepared_statements)
71-
.add(server.stats.healthchecks)
71+
.add(server.stats.total.healthchecks)
7272
.add(server.stats.total.errors)
7373
.add(server.stats.total.bytes_received)
7474
.add(server.stats.total.bytes_sent)

pgdog/src/backend/pool/inner.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl Inner {
222222
/// Place connection back into the pool
223223
/// or give it to a waiting client.
224224
#[inline]
225-
pub(super) fn put(&mut self, conn: Box<Server>) {
225+
pub(super) fn put(&mut self, conn: Box<Server>, now: Instant) {
226226
// Try to give it to a client that's been waiting, if any.
227227
let id = *conn.id();
228228
if let Some(waiter) = self.waiting.pop_front() {
@@ -233,6 +233,8 @@ impl Inner {
233233
server: id,
234234
client: waiter.request.id,
235235
});
236+
self.stats.counts.server_assignment_count += 1;
237+
self.stats.counts.wait_time += now.duration_since(waiter.request.created_at);
236238
}
237239
} else {
238240
self.conns.push(conn);
@@ -324,7 +326,7 @@ impl Inner {
324326
// Finally, if the server is ok,
325327
// place the connection back into the idle list.
326328
if server.can_check_in() {
327-
self.put(server);
329+
self.put(server, now);
328330
} else {
329331
self.out_of_sync += 1;
330332
}

pgdog/src/backend/pool/monitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl Monitor {
242242
let server = Box::new(conn);
243243

244244
let mut guard = self.pool.lock();
245-
guard.put(server);
245+
guard.put(server, Instant::now());
246246
}
247247

248248
Ok(Err(err)) => {

pgdog/src/backend/pool/pool_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ impl Pool {
273273
pub(crate) fn move_conns_to(&self, destination: &Pool) {
274274
// Ensure no deadlock.
275275
assert!(self.inner.id != destination.id());
276+
let now = Instant::now();
276277

277278
{
278279
let mut from_guard = self.lock();
@@ -281,7 +282,7 @@ impl Pool {
281282
from_guard.online = false;
282283
let (idle, taken) = from_guard.move_conns_to(destination);
283284
for server in idle {
284-
to_guard.put(server);
285+
to_guard.put(server, now);
285286
}
286287
to_guard.set_taken(taken);
287288
}

pgdog/src/backend/pool/stats.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Counts {
2121
pub parse_count: usize,
2222
pub bind_count: usize,
2323
pub rollbacks: usize,
24+
pub healthchecks: usize,
2425
}
2526

2627
impl Sub for Counts {
@@ -41,6 +42,7 @@ impl Sub for Counts {
4142
parse_count: self.parse_count.saturating_sub(rhs.parse_count),
4243
bind_count: self.parse_count.saturating_sub(rhs.bind_count),
4344
rollbacks: self.rollbacks.saturating_sub(rhs.rollbacks),
45+
healthchecks: self.healthchecks.saturating_add(rhs.healthchecks),
4446
}
4547
}
4648
}
@@ -61,6 +63,7 @@ impl Div<usize> for Counts {
6163
parse_count: self.parse_count.saturating_div(rhs),
6264
bind_count: self.parse_count.saturating_div(rhs),
6365
rollbacks: self.rollbacks.saturating_div(rhs),
66+
healthchecks: self.healthchecks.saturating_div(rhs),
6467
}
6568
}
6669
}
@@ -72,7 +75,7 @@ impl Add<BackendCounts> for Counts {
7275
Counts {
7376
xact_count: self.xact_count + rhs.transactions,
7477
query_count: self.query_count + rhs.queries,
75-
server_assignment_count: self.server_assignment_count + 1,
78+
server_assignment_count: self.server_assignment_count,
7679
received: self.received + rhs.bytes_received,
7780
sent: self.sent + rhs.bytes_sent,
7881
query_time: self.query_time + rhs.query_time,
@@ -81,6 +84,7 @@ impl Add<BackendCounts> for Counts {
8184
parse_count: self.parse_count + rhs.parse,
8285
bind_count: self.parse_count + rhs.bind,
8386
rollbacks: self.rollbacks + rhs.rollbacks,
87+
healthchecks: self.healthchecks + rhs.healthchecks,
8488
}
8589
}
8690
}
@@ -114,6 +118,7 @@ impl Add for Counts {
114118
parse_count: self.parse_count.saturating_add(rhs.parse_count),
115119
bind_count: self.parse_count.saturating_add(rhs.bind_count),
116120
rollbacks: self.rollbacks.saturating_add(rhs.rollbacks),
121+
healthchecks: self.healthchecks.saturating_add(rhs.healthchecks),
117122
}
118123
}
119124
}

pgdog/src/backend/stats.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub struct Counts {
6060
pub transaction_time: Duration,
6161
pub parse: usize,
6262
pub bind: usize,
63+
pub healthchecks: usize,
6364
}
6465

6566
impl Add for Counts {
@@ -80,6 +81,7 @@ impl Add for Counts {
8081
transaction_time: self.query_time.saturating_add(rhs.transaction_time),
8182
parse: self.parse.saturating_add(rhs.parse),
8283
bind: self.bind.saturating_add(rhs.bind),
84+
healthchecks: self.healthchecks.saturating_add(rhs.healthchecks),
8385
}
8486
}
8587
}
@@ -88,8 +90,6 @@ impl Add for Counts {
8890
#[derive(Copy, Clone, Debug)]
8991
pub struct Stats {
9092
pub id: BackendKeyData,
91-
/// Number of bytes sent.
92-
pub healthchecks: usize,
9393
pub state: State,
9494
pub last_used: Instant,
9595
pub last_healthcheck: Option<Instant>,
@@ -107,7 +107,6 @@ impl Stats {
107107
let now = Instant::now();
108108
let stats = Stats {
109109
id,
110-
healthchecks: 0,
111110
state: State::Idle,
112111
last_used: now,
113112
last_healthcheck: None,
@@ -244,7 +243,8 @@ impl Stats {
244243

245244
/// Track healtchecks.
246245
pub fn healthcheck(&mut self) {
247-
self.healthchecks += 1;
246+
self.total.healthchecks += 1;
247+
self.last_checkout.healthchecks += 1;
248248
self.last_healthcheck = Some(Instant::now());
249249
self.update();
250250
}

pgdog/src/config/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,37 @@ pub mod test {
964964
init();
965965
}
966966

967+
pub fn load_test_replicas() {
968+
let mut config = ConfigAndUsers::default();
969+
config.config.databases = vec![
970+
Database {
971+
name: "pgdog".into(),
972+
host: "127.0.0.1".into(),
973+
port: 5432,
974+
role: Role::Primary,
975+
..Default::default()
976+
},
977+
Database {
978+
name: "pgdog".into(),
979+
host: "127.0.0.1".into(),
980+
port: 5432,
981+
role: Role::Replica,
982+
read_only: Some(true),
983+
..Default::default()
984+
},
985+
];
986+
config.config.general.load_balancing_strategy = LoadBalancingStrategy::RoundRobin;
987+
config.users.users = vec![User {
988+
name: "pgdog".into(),
989+
database: "pgdog".into(),
990+
password: Some("pgdog".into()),
991+
..Default::default()
992+
}];
993+
994+
set(config).unwrap();
995+
init();
996+
}
997+
967998
#[test]
968999
fn test_basic() {
9691000
let source = r#"

0 commit comments

Comments
 (0)