Skip to content

Commit 019bf46

Browse files
authored
feat: adapt PoolState to usage of deadpool everywhere (#2128)
just killing it for deadpool::Status since it provides a public constructor and kill the long unused syncstorage-spanner bb8 based manager Closes STOR-368
1 parent efa8c4b commit 019bf46

File tree

19 files changed

+90
-172
lines changed

19 files changed

+90
-172
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

syncserver-db-common/src/lib.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod error;
22
pub mod test;
33

4-
use std::{error::Error, fmt::Debug};
4+
use std::error::Error;
55

66
#[cfg(debug_assertions)]
77
use diesel::connection::InstrumentationEvent;
@@ -14,25 +14,9 @@ use diesel_migrations::{EmbeddedMigrations, MigrationHarness};
1414
use tokio::task::spawn_blocking;
1515

1616
/// A trait to be implemented by database pool data structures. It provides an interface to
17-
/// derive the current state of the pool, as represented by the `PoolState` struct.
18-
pub trait GetPoolState {
19-
fn state(&self) -> PoolState;
20-
}
21-
22-
#[derive(Debug, Default)]
23-
/// A mockable r2d2::State
24-
pub struct PoolState {
25-
pub connections: u32,
26-
pub idle_connections: u32,
27-
}
28-
29-
impl From<deadpool::Status> for PoolState {
30-
fn from(status: deadpool::Status) -> PoolState {
31-
PoolState {
32-
connections: status.size as u32,
33-
idle_connections: status.available.max(0) as u32,
34-
}
35-
}
17+
/// derive the current status of the pool, as represented by [deadpool::Status]
18+
pub trait GetPoolStatus {
19+
fn status(&self) -> deadpool::Status;
3620
}
3721

3822
/// Establish an [AsyncConnection] logging diesel queries to the `debug` log

syncserver/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ backtrace.workspace = true
1212
base64.workspace = true
1313
cadence.workspace = true
1414
chrono.workspace = true
15+
deadpool.workspace = true
1516
docopt.workspace = true
1617
futures.workspace = true
1718
hex.workspace = true

syncserver/src/server/mod.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syncserver_common::{
1919
BlockingThreadpool, BlockingThreadpoolMetrics, Metrics, Taggable,
2020
middleware::sentry::SentryWrapper,
2121
};
22-
use syncserver_db_common::{GetPoolState, PoolState};
22+
use syncserver_db_common::GetPoolStatus;
2323
use syncserver_settings::Settings;
2424
use syncstorage_db::{DbError, DbPool, DbPoolImpl};
2525
use syncstorage_settings::{Deadman, ServerLimits};
@@ -575,7 +575,7 @@ impl FromRequest for MetricsWrapper {
575575
}
576576

577577
/// Emit database pool and threadpool metrics periodically
578-
fn spawn_metric_periodic_reporter<T: GetPoolState + Send + 'static>(
578+
fn spawn_metric_periodic_reporter<T: GetPoolStatus + Send + 'static>(
579579
interval: Duration,
580580
metrics: Arc<StatsdClient>,
581581
pool: T,
@@ -603,22 +603,14 @@ fn spawn_metric_periodic_reporter<T: GetPoolState + Send + 'static>(
603603
};
604604

605605
loop {
606-
let PoolState {
607-
connections,
608-
idle_connections,
609-
} = pool.state();
606+
let deadpool::Status {
607+
size, available, ..
608+
} = pool.status();
610609
send_gauge_with_maybe_hostname(
611610
"storage.pool.connections.active",
612-
(connections - idle_connections) as u64,
613-
);
614-
send_gauge_with_maybe_hostname(
615-
"storage.pool.connections.active",
616-
(connections - idle_connections) as u64,
617-
);
618-
send_gauge_with_maybe_hostname(
619-
"storage.pool.connections.idle",
620-
idle_connections as u64,
611+
(size - available) as u64,
621612
);
613+
send_gauge_with_maybe_hostname("storage.pool.connections.idle", available as u64);
622614

623615
let BlockingThreadpoolMetrics {
624616
queued_tasks,

syncserver/src/web/handlers.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -846,49 +846,52 @@ pub async fn lbheartbeat(req: HttpRequest) -> Result<HttpResponse, ApiError> {
846846
let db_state = if cfg!(test) {
847847
use actix_web::http::header::HeaderValue;
848848
use std::str::FromStr;
849-
use syncstorage_db::PoolState;
850-
851-
PoolState {
852-
connections: u32::from_str(
853-
req.headers()
854-
.get("TEST_CONNECTIONS")
855-
.unwrap_or(&HeaderValue::from_static("0"))
856-
.to_str()
857-
.unwrap_or("0"),
858-
)
859-
.unwrap_or_default(),
860-
idle_connections: u32::from_str(
861-
req.headers()
862-
.get("TEST_IDLES")
863-
.unwrap_or(&HeaderValue::from_static("0"))
864-
.to_str()
865-
.unwrap_or("0"),
866-
)
867-
.unwrap_or_default(),
849+
850+
let size = usize::from_str(
851+
req.headers()
852+
.get("TEST_CONNECTIONS")
853+
.unwrap_or(&HeaderValue::from_static("0"))
854+
.to_str()
855+
.unwrap_or("0"),
856+
)
857+
.unwrap_or_default();
858+
let available = usize::from_str(
859+
req.headers()
860+
.get("TEST_IDLES")
861+
.unwrap_or(&HeaderValue::from_static("0"))
862+
.to_str()
863+
.unwrap_or("0"),
864+
)
865+
.unwrap_or_default();
866+
deadpool::Status {
867+
max_size: size,
868+
size,
869+
available,
870+
waiting: 0,
868871
}
869872
} else {
870-
state.db_pool.clone().state()
873+
state.db_pool.status()
871874
};
872875

873-
let active = db_state.connections - db_state.idle_connections;
876+
let active = db_state.size - db_state.available;
874877
let mut status_code = StatusCode::OK;
875878

876-
if active >= deadman.max_size && db_state.idle_connections == 0 {
879+
if active >= deadman.max_size as usize && db_state.available == 0 {
877880
if deadman.clock_start.is_none() {
878881
deadman.clock_start = Some(Instant::now());
879882
}
880883
status_code = StatusCode::INTERNAL_SERVER_ERROR;
881884
} else if deadman.clock_start.is_some() {
882885
deadman.clock_start = None
883886
}
884-
deadman.previous_count = db_state.idle_connections as usize;
887+
deadman.previous_count = db_state.available;
885888
{
886889
*deadarc.write().await = deadman;
887890
}
888891
resp.insert("active_connections".to_string(), Value::from(active));
889892
resp.insert(
890893
"idle_connections".to_string(),
891-
Value::from(db_state.idle_connections),
894+
Value::from(db_state.available),
892895
);
893896
if let Some(clock) = deadman.clock_start {
894897
let duration: Duration = Instant::now() - clock;

syncstorage-db-common/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::fmt::Debug;
1010
use async_trait::async_trait;
1111
use lazy_static::lazy_static;
1212
use serde::Deserialize;
13-
use syncserver_db_common::GetPoolState;
13+
use syncserver_db_common::GetPoolStatus;
1414

1515
use error::DbErrorIntrospect;
1616
use util::SyncTimestamp;
@@ -49,7 +49,7 @@ pub const DEFAULT_BSO_TTL: u32 = 2_100_000_000;
4949
pub const FIRST_CUSTOM_COLLECTION_ID: i32 = 101;
5050

5151
#[async_trait]
52-
pub trait DbPool: Sync + Send + Debug + GetPoolState {
52+
pub trait DbPool: Sync + Send + Debug + GetPoolStatus {
5353
type Error;
5454

5555
async fn init(&mut self) -> Result<(), Self::Error> {

syncstorage-db/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition.workspace = true
77

88
[dependencies]
99
async-trait.workspace = true
10+
deadpool.workspace = true
1011
env_logger.workspace = true
1112
lazy_static.workspace = true
1213
log.workspace = true

syncstorage-db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub use syncstorage_spanner::DbError;
2929
#[cfg(feature = "spanner")]
3030
pub type DbImpl = syncstorage_spanner::SpannerDb;
3131

32-
pub use syncserver_db_common::{GetPoolState, PoolState};
32+
pub use syncserver_db_common::GetPoolStatus;
3333
pub use syncstorage_db_common::error::DbErrorIntrospect;
3434

3535
pub use syncstorage_db_common::{

syncstorage-db/src/mock.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Mock db implementation with methods stubbed to return default values.
22
#![allow(clippy::new_without_default)]
33
use async_trait::async_trait;
4-
use syncserver_db_common::{GetPoolState, PoolState};
4+
use syncserver_db_common::GetPoolStatus;
55
#[cfg(debug_assertions)]
66
use syncstorage_db_common::util::SyncTimestamp;
77
use syncstorage_db_common::{BatchDb, Db, DbPool, params, results};
@@ -34,9 +34,14 @@ impl DbPool for MockDbPool {
3434
}
3535
}
3636

37-
impl GetPoolState for MockDbPool {
38-
fn state(&self) -> PoolState {
39-
PoolState::default()
37+
impl GetPoolStatus for MockDbPool {
38+
fn status(&self) -> deadpool::Status {
39+
deadpool::Status {
40+
max_size: 0,
41+
size: 0,
42+
available: 0,
43+
waiting: 0,
44+
}
4045
}
4146
}
4247

syncstorage-mysql/src/pool.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syncserver_common::{BlockingThreadpool, Metrics};
2020
#[cfg(debug_assertions)]
2121
use syncserver_db_common::test::test_transaction_hook;
2222
use syncserver_db_common::{
23-
GetPoolState, PoolState, establish_connection_with_logging, manager_config_with_logging,
23+
GetPoolStatus, establish_connection_with_logging, manager_config_with_logging,
2424
run_embedded_migrations,
2525
};
2626
use syncstorage_db_common::{Db, DbPool, STD_COLLS};
@@ -170,9 +170,9 @@ impl fmt::Debug for MysqlDbPool {
170170
}
171171
}
172172

173-
impl GetPoolState for MysqlDbPool {
174-
fn state(&self) -> PoolState {
175-
self.pool.status().into()
173+
impl GetPoolStatus for MysqlDbPool {
174+
fn status(&self) -> deadpool::Status {
175+
self.pool.status()
176176
}
177177
}
178178

0 commit comments

Comments
 (0)