Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Dev CLI.
#

set -ex

# Connect to the admin database.
function admin() {
PGPASSWORD=pgdog psql -h 127.0.0.1 -p 6432 -U admin admin
Expand All @@ -15,7 +17,7 @@ function admin() {
# - protocol: simple|extended|prepared
#
function bench() {
PGPASSWORD=pgdog pgbench -h 127.0.0.1 -p 6432 -U pgdog pgdog --protocol ${2:-simple} -t 100000 -c 10 -P 1 -S
PGPASSWORD=pgdog pgbench -h 127.0.0.1 -p 6432 -U pgdog pgdog --protocol ${1:-simple} -t 100000000 -c 10 -P 1 -S
}

function bench_init() {
Expand Down
4 changes: 4 additions & 0 deletions pgdog-stats/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub struct Stats {
pub last_checkout: Counts,
pub pool_id: u64,
pub memory: MemoryStats,
pub last_sent: u8,
pub last_received: u8,
}

impl Default for Stats {
Expand All @@ -106,6 +108,8 @@ impl Default for Stats {
last_checkout: Counts::default(),
pool_id: 0,
memory: MemoryStats::default(),
last_sent: 0,
last_received: 0,
}
}
}
7 changes: 3 additions & 4 deletions pgdog/src/admin/show_server_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ impl Command for ShowServerMemory {
let stats = stats();
for (_, server) in stats {
let mut row = DataRow::new();
let stats = server.stats;
let memory = &stats.memory;
let memory = &server.stats.memory;

row.add(stats.pool_id as i64)
row.add(server.stats.pool_id as i64)
.add(server.addr.database_name.as_str())
.add(server.addr.user.as_str())
.add(server.addr.host.as_str())
.add(server.addr.port as i64)
.add(stats.id.pid as i64)
.add(server.stats.id.pid as i64)
.add(memory.buffer.reallocs as i64)
.add(memory.buffer.reclaims as i64)
.add(memory.buffer.bytes_used as i64)
Expand Down
49 changes: 32 additions & 17 deletions pgdog/src/admin/show_servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Command for ShowServers {
Field::text("connect_time"),
Field::text("request_time"),
Field::numeric("remote_pid"),
// Field::bigint("client_id"),
Field::bigint("client_id"),
Field::numeric("transactions"),
Field::numeric("queries"),
Field::numeric("rollbacks"),
Expand All @@ -62,6 +62,8 @@ impl Command for ShowServers {
Field::numeric("errors"),
Field::numeric("bytes_received"),
Field::numeric("bytes_sent"),
Field::text("last_sent"),
Field::text("last_received"),
Field::numeric("age"),
Field::text("application_name"),
],
Expand All @@ -78,32 +80,45 @@ impl Command for ShowServers {
let now_time = SystemTime::now();

for (_, server) in stats {
let stats = server.stats;
let age = now.duration_since(stats.created_at);
let request_age = now.duration_since(stats.last_used);
let age = now.duration_since(server.stats.created_at);
let request_age = now.duration_since(server.stats.last_used);
let request_time = now_time - request_age;

let dr = self
.row
.clone()
.add("pool_id", stats.pool_id)
.add("pool_id", server.stats.pool_id)
.add("database", server.addr.database_name)
.add("user", server.addr.user)
.add("addr", server.addr.host.as_str())
.add("port", server.addr.port.to_string())
.add("state", stats.state.to_string())
.add("connect_time", format_time(stats.created_at_time.into()))
.add("state", server.stats.state.to_string())
.add(
"connect_time",
format_time(server.stats.created_at_time.into()),
)
.add("request_time", format_time(request_time.into()))
.add("remote_pid", stats.id.pid as i64)
// .add("client_id", stats.client_id.map(|client| client.pid as i64))
.add("transactions", stats.total.transactions)
.add("queries", stats.total.queries)
.add("rollbacks", stats.total.rollbacks)
.add("prepared_statements", stats.total.prepared_statements)
.add("healthchecks", stats.total.healthchecks)
.add("errors", stats.total.errors)
.add("bytes_received", stats.total.bytes_received)
.add("bytes_sent", stats.total.bytes_sent)
.add("remote_pid", server.stats.id.pid as i64)
.add(
"client_id",
server.stats.client_id.map(|client| client.pid as i64),
)
.add("transactions", server.stats.total.transactions)
.add("queries", server.stats.total.queries)
.add("rollbacks", server.stats.total.rollbacks)
.add(
"prepared_statements",
server.stats.total.prepared_statements,
)
.add("healthchecks", server.stats.total.healthchecks)
.add("errors", server.stats.total.errors)
.add("bytes_received", server.stats.total.bytes_received)
.add("bytes_sent", server.stats.total.bytes_sent)
.add("last_sent", (server.stats.last_sent as char).to_string())
.add(
"last_received",
(server.stats.last_received as char).to_string(),
)
.add("age", age.as_secs() as i64)
.add("application_name", server.application_name.as_str())
.data_row();
Expand Down
11 changes: 6 additions & 5 deletions pgdog/src/admin/show_stats.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! SHOW STATS.
use crate::backend::databases::databases;
use crate::util::millis;

use super::prelude::*;

Expand Down Expand Up @@ -83,17 +84,17 @@ impl Command for ShowStats {
.add(stat.server_assignment_count)
.add(stat.received)
.add(stat.sent)
.add(stat.xact_time.as_millis() as u64)
.add(stat.idle_xact_time.as_millis() as u64)
.add(stat.query_time.as_millis() as u64)
.add(stat.wait_time.as_millis() as u64)
.add(millis(stat.xact_time))
.add(millis(stat.idle_xact_time))
.add(millis(stat.query_time))
.add(millis(stat.wait_time))
.add(stat.parse_count)
.add(stat.bind_count)
.add(stat.close)
.add(stat.errors)
.add(stat.cleaned)
.add(stat.rollbacks)
.add(stat.connect_time.as_millis() as u64)
.add(millis(stat.connect_time))
.add(stat.connect_count);
}

Expand Down
12 changes: 8 additions & 4 deletions pgdog/src/backend/pool/connection/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,18 @@ impl Binding {
Binding::Direct(Some(server)) => {
debug!(
"server is in \"{}\" state [{}]",
server.stats().state,
server.stats().get_state(),
server.addr()
);
server.stats().state == state
server.stats().get_state() == state
}
Binding::MultiShard(servers, _) => servers.iter().all(|s| {
debug!("server is in \"{}\" state [{}]", s.stats().state, s.addr());
s.stats().state == state
debug!(
"server is in \"{}\" state [{}]",
s.stats().get_state(),
s.addr()
);
s.stats().get_state() == state
}),
_ => true,
}
Expand Down
18 changes: 9 additions & 9 deletions pgdog/src/backend/pool/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Guard {
} else {
debug!(
"[cleanup] no cleanup needed, server in \"{}\" state [{}]",
server.stats().state,
server.stats().get_state(),
server.addr(),
);
if let Err(err) = pool.checkin(server) {
Expand All @@ -120,7 +120,7 @@ impl Guard {
// Receive whatever data the client left before disconnecting.
debug!(
"[cleanup] draining data from \"{}\" server [{}]",
server.stats().state,
server.stats().get_state(),
server.addr()
);
server.drain().await?;
Expand All @@ -138,7 +138,7 @@ impl Guard {
if conn_recovery.can_rollback() {
debug!(
"[cleanup] rolling back server transaction, in \"{}\" state [{}]",
server.stats().state,
server.stats().get_state(),
server.addr(),
);
server.rollback().await?;
Expand All @@ -152,7 +152,7 @@ impl Guard {
debug!(
"[cleanup] running {} cleanup queries, server in \"{}\" state [{}]",
cleanup.len(),
server.stats().state,
server.stats().get_state(),
server.addr()
);
server.execute_batch(cleanup.queries()).await?;
Expand Down Expand Up @@ -180,7 +180,7 @@ impl Guard {
if sync_prepared {
debug!(
"[cleanup] syncing prepared statements, server in \"{}\" state [{}]",
server.stats().state,
server.stats().get_state(),
server.addr()
);
server.sync_prepared_statements().await?;
Expand Down Expand Up @@ -510,7 +510,7 @@ mod test {
.unwrap();

use crate::state::State;
assert_eq!(server.stats().state, State::ForceClose);
assert_eq!(server.stats().get_state(), State::ForceClose);
assert!(server.needs_drain());
}

Expand Down Expand Up @@ -556,7 +556,7 @@ mod test {
.unwrap();

use crate::state::State;
assert_eq!(server.stats().state, State::ForceClose);
assert_eq!(server.stats().get_state(), State::ForceClose);
assert!(server.needs_drain());
}

Expand Down Expand Up @@ -669,7 +669,7 @@ mod test {
.unwrap();

use crate::state::State;
assert_eq!(server.stats().state, State::ForceClose);
assert_eq!(server.stats().get_state(), State::ForceClose);
assert!(server.in_transaction());
}

Expand Down Expand Up @@ -730,7 +730,7 @@ mod test {
.unwrap();

use crate::state::State;
assert_eq!(server.stats().state, State::ForceClose);
assert_eq!(server.stats().get_state(), State::ForceClose);
assert!(server.needs_drain());
assert!(server.in_transaction());
}
Expand Down
6 changes: 3 additions & 3 deletions pgdog/src/backend/pool/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl Inner {
let taken = std::mem::take(&mut self.taken);

for conn in idle.iter_mut() {
conn.stats_mut().pool_id = destination.id();
conn.stats_mut().set_pool_id(destination.id());
}

(idle, taken)
Expand All @@ -319,8 +319,8 @@ impl Inner {
result.replenish = false;
// Prevents deadlocks.
if moved.id() != self.id {
server.stats_mut().pool_id = moved.id();
server.stats_mut().update();
server.stats_mut().set_pool_id(moved.id());
server.stats().update();
moved.lock().maybe_check_in(server, now, stats)?;
return Ok(result);
}
Expand Down
8 changes: 5 additions & 3 deletions pgdog/src/backend/pool/pool_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,15 @@ impl Pool {
let now = if server.pooler_mode() == &PoolerMode::Session {
Instant::now()
} else {
server.stats().last_used
server.stats().last_used()
};

let counts = {
let stats = server.stats_mut();
stats.client_id = None;
stats.reset_last_checkout()
stats.clear_client_id();
let counts = stats.reset_last_checkout();
stats.update();
counts
};

// Check everything and maybe check the connection
Expand Down
8 changes: 4 additions & 4 deletions pgdog/src/backend/pool/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ async fn test_prepared_statements_limit() {
|| guard.prepared_statements_mut().contains("__pgdog_98")
);
assert_eq!(guard.prepared_statements_mut().len(), 2);
assert_eq!(guard.stats().total.prepared_statements, 2); // stats are accurate.
assert_eq!(guard.stats().total().prepared_statements, 2); // stats are accurate.

let pool = pool_with_prepared_capacity(100);

Expand Down Expand Up @@ -468,14 +468,14 @@ async fn test_prepared_statements_limit() {
let mut guard = pool.get(&Request::default()).await.unwrap();
assert!(guard.prepared_statements_mut().contains("__pgdog_99"));
assert_eq!(guard.prepared_statements_mut().len(), 100);
assert_eq!(guard.stats().total.prepared_statements, 100); // stats are accurate.
assert_eq!(guard.stats().total().prepared_statements, 100); // stats are accurate.

// Let's make sure Postgres agreees.
guard.sync_prepared_statements().await.unwrap();

assert!(guard.prepared_statements_mut().contains("__pgdog_99"));
assert_eq!(guard.prepared_statements_mut().len(), 100);
assert_eq!(guard.stats().total.prepared_statements, 100); // stats are accurate.
assert_eq!(guard.stats().total().prepared_statements, 100); // stats are accurate.
}

#[tokio::test]
Expand Down Expand Up @@ -612,7 +612,7 @@ async fn test_move_conns_to() {
assert_eq!(source.lock().total(), 0);
let new_pool_id = destination.id();
for conn in destination.lock().idle_conns() {
assert_eq!(conn.stats().pool_id, new_pool_id);
assert_eq!(conn.stats().pool_id(), new_pool_id);
}

drop(conn2);
Expand Down
Loading
Loading