Skip to content

Commit cf3ac05

Browse files
committed
Revert "only report wait times from clients currently waiting to match behavior of pgbouncer (#655)"
This reverts commit 0e8064b.
1 parent 7d33df3 commit cf3ac05

File tree

3 files changed

+12
-33
lines changed

3 files changed

+12
-33
lines changed

src/stats/client.rs

Lines changed: 5 additions & 10 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)),
@@ -113,11 +111,6 @@ impl ClientStats {
113111

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

@@ -141,6 +134,8 @@ impl ClientStats {
141134
pub fn checkout_time(&self, microseconds: u64) {
142135
self.total_wait_time
143136
.fetch_add(microseconds, Ordering::Relaxed);
137+
self.max_wait_time
138+
.fetch_max(microseconds, Ordering::Relaxed);
144139
}
145140

146141
/// 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)