Skip to content

Commit dc4d6ed

Browse files
authored
Revert max_wait changes (#658)
* Revert "Reset wait times when checked out successfully (#656)" This reverts commit ec3920d. * Revert "Not sure how this sneaked past CI" This reverts commit 4c5498b. * Revert "only report wait times from clients currently waiting to match behavior of pgbouncer (#655)" This reverts commit 0e8064b.
1 parent ec3920d commit dc4d6ed

File tree

4 files changed

+13
-36
lines changed

4 files changed

+13
-36
lines changed

src/admin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ where
699699
res.put(row_description(&columns));
700700

701701
for (_, client) in new_map {
702-
let max_wait = client.wait_start.load(Ordering::Relaxed);
702+
let max_wait = client.max_wait_time.load(Ordering::Relaxed);
703703
let row = vec![
704704
format!("{:#010X}", client.client_id()),
705705
client.pool_name(),

src/stats/client.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ pub struct ClientStats {
3838
/// Total time spent waiting for a connection from pool, measures in microseconds
3939
pub total_wait_time: Arc<AtomicU64>,
4040

41-
/// When this client started waiting.
42-
/// Stored as microseconds since connect_time so it can fit in an AtomicU64 instead
43-
/// of us using an "AtomicInstant"
44-
pub wait_start: Arc<AtomicU64>,
41+
/// Maximum time spent waiting for a connection from pool, measures in microseconds
42+
pub max_wait_time: Arc<AtomicU64>,
4543

4644
/// Current state of the client
4745
pub state: Arc<AtomicClientState>,
@@ -65,7 +63,7 @@ impl Default for ClientStats {
6563
username: String::new(),
6664
pool_name: String::new(),
6765
total_wait_time: Arc::new(AtomicU64::new(0)),
68-
wait_start: Arc::new(AtomicU64::new(0)),
66+
max_wait_time: Arc::new(AtomicU64::new(0)),
6967
state: Arc::new(AtomicClientState::new(ClientState::Idle)),
7068
transaction_count: Arc::new(AtomicU64::new(0)),
7169
query_count: Arc::new(AtomicU64::new(0)),
@@ -109,23 +107,16 @@ impl ClientStats {
109107
/// Reports a client is done querying the server and is no longer assigned a server connection
110108
pub fn idle(&self) {
111109
self.state.store(ClientState::Idle, Ordering::Relaxed);
112-
self.wait_start.store(0, Ordering::Relaxed);
113110
}
114111

115112
/// Reports a client is waiting for a connection
116113
pub fn waiting(&self) {
117-
// safe to truncate, we only lose info if duration is greater than ~585,000 years
118-
self.wait_start.store(
119-
Instant::now().duration_since(self.connect_time).as_micros() as u64,
120-
Ordering::Relaxed,
121-
);
122114
self.state.store(ClientState::Waiting, Ordering::Relaxed);
123115
}
124116

125117
/// Reports a client is done waiting for a connection and is about to query the server.
126118
pub fn active(&self) {
127119
self.state.store(ClientState::Active, Ordering::Relaxed);
128-
self.wait_start.store(0, Ordering::Relaxed);
129120
}
130121

131122
/// Reports a client has failed to obtain a connection from a connection pool
@@ -143,6 +134,8 @@ impl ClientStats {
143134
pub fn checkout_time(&self, microseconds: u64) {
144135
self.total_wait_time
145136
.fetch_add(microseconds, Ordering::Relaxed);
137+
self.max_wait_time
138+
.fetch_max(microseconds, Ordering::Relaxed);
146139
}
147140

148141
/// Report a query executed by a client against a server

src/stats/pool.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use super::{ClientState, ServerState};
44
use crate::{config::PoolMode, messages::DataType, pool::PoolIdentifier};
55
use std::collections::HashMap;
66
use std::sync::atomic::*;
7-
use tokio::time::Instant;
87

98
use crate::pool::get_all_pools;
109

@@ -54,7 +53,6 @@ impl PoolStats {
5453
);
5554
}
5655

57-
let now = Instant::now();
5856
for client in client_map.values() {
5957
match map.get_mut(&PoolIdentifier {
6058
db: client.pool_name(),
@@ -64,16 +62,10 @@ impl PoolStats {
6462
match client.state.load(Ordering::Relaxed) {
6563
ClientState::Active => pool_stats.cl_active += 1,
6664
ClientState::Idle => pool_stats.cl_idle += 1,
67-
ClientState::Waiting => {
68-
pool_stats.cl_waiting += 1;
69-
// wait_start is measured as microseconds since connect_time
70-
// so compute wait_time as (now() - connect_time) - (wait_start - connect_time)
71-
let duration_since_connect = now.duration_since(client.connect_time());
72-
let wait_time = (duration_since_connect.as_micros() as u64)
73-
- client.wait_start.load(Ordering::Relaxed);
74-
pool_stats.maxwait = std::cmp::max(pool_stats.maxwait, wait_time);
75-
}
65+
ClientState::Waiting => pool_stats.cl_waiting += 1,
7666
}
67+
let max_wait = client.max_wait_time.load(Ordering::Relaxed);
68+
pool_stats.maxwait = std::cmp::max(pool_stats.maxwait, max_wait);
7769
}
7870
None => debug!("Client from an obselete pool"),
7971
}

tests/ruby/stats_spec.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
sleep(1.1) # Allow time for stats to update
234234
admin_conn = PG::connect(processes.pgcat.admin_connection_string)
235235
results = admin_conn.async_exec("SHOW POOLS")[0]
236-
%w[cl_idle cl_cancel_req sv_idle sv_used sv_tested sv_login].each do |s|
236+
%w[cl_idle cl_cancel_req sv_idle sv_used sv_tested sv_login maxwait].each do |s|
237237
raise StandardError, "Field #{s} was expected to be 0 but found to be #{results[s]}" if results[s] != "0"
238238
end
239239

@@ -260,20 +260,12 @@
260260
threads << Thread.new { c.async_exec("SELECT pg_sleep(1.5)") rescue nil }
261261
end
262262

263-
admin_conn = PG::connect(processes.pgcat.admin_connection_string)
264-
265-
# two connections waiting => they report wait time
266-
sleep(1.1) # Allow time for stats to update
267-
results = admin_conn.async_exec("SHOW POOLS")[0]
268-
expect(results["maxwait"]).to eq("1")
269-
expect(results["maxwait_us"].to_i).to be_within(200_000).of(100_000)
270-
271263
sleep(2.5) # Allow time for stats to update
264+
admin_conn = PG::connect(processes.pgcat.admin_connection_string)
272265
results = admin_conn.async_exec("SHOW POOLS")[0]
273266

274-
# no connections waiting => no reported wait time
275-
expect(results["maxwait"]).to eq("0")
276-
expect(results["maxwait_us"]).to eq("0")
267+
expect(results["maxwait"]).to eq("1")
268+
expect(results["maxwait_us"].to_i).to be_within(200_000).of(500_000)
277269
connections.map(&:close)
278270

279271
sleep(4.5) # Allow time for stats to update

0 commit comments

Comments
 (0)