Skip to content

Commit be0ef17

Browse files
authored
Minor cleanup of TargetHealth (#1353)
Fixed visibility, removed dead code, replaced cloned value with an accessor method since we always have access to the original
1 parent e785ab1 commit be0ef17

5 files changed

Lines changed: 35 additions & 36 deletions

File tree

pgdog/src/backend/pool/lb/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub mod target_health;
2828
use ban::Ban;
2929
pub use ban::UnbanReason;
3030
use monitor::*;
31-
pub use target_health::*;
31+
pub(crate) use target_health::*;
3232

3333
#[cfg(test)]
3434
mod test;
@@ -39,7 +39,6 @@ pub struct Target {
3939
pub pool: Pool,
4040
pub ban: Ban,
4141
role: PoolRole,
42-
pub health: TargetHealth,
4342
/// Smooth weighted round-robin current weight tracker.
4443
current_weight: Arc<AtomicI64>,
4544
}
@@ -54,7 +53,6 @@ impl Target {
5453
Self {
5554
ban,
5655
role: PoolRole::new(role),
57-
health: pool.inner().health.clone(),
5856
pool,
5957
current_weight: Arc::new(AtomicI64::new(0)),
6058
}
@@ -77,6 +75,10 @@ impl Target {
7775

7876
lb && pool
7977
}
78+
79+
pub(super) fn health(&self) -> &TargetHealth {
80+
&self.pool.inner().health
81+
}
8082
}
8183

8284
/// Load balancer.

pgdog/src/backend/pool/lb/monitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Monitor {
8989
let targets = &self.replicas.targets;
9090

9191
for (i, target) in targets.iter().enumerate() {
92-
let healthy = target.health.healthy();
92+
let healthy = target.health().healthy();
9393
let replica_lag_bad = target
9494
.pool
9595
.replica_lag()

pgdog/src/backend/pool/lb/target_health.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@ use std::sync::{
66
};
77

88
#[derive(Clone, Debug)]
9-
pub struct TargetHealth {
10-
#[allow(dead_code)]
11-
pub(super) id: u64,
9+
pub(crate) struct TargetHealth {
1210
pub(super) healthy: Arc<AtomicBool>,
1311
}
1412

1513
impl TargetHealth {
16-
pub fn new(id: u64) -> Self {
14+
pub(crate) fn new() -> Self {
1715
Self {
18-
id,
1916
healthy: Arc::new(AtomicBool::new(true)),
2017
}
2118
}
2219

23-
pub fn toggle(&self, healthy: bool) {
20+
pub(crate) fn toggle(&self, healthy: bool) {
2421
self.healthy.swap(healthy, Ordering::SeqCst);
2522
}
2623

27-
pub fn healthy(&self) -> bool {
24+
pub(crate) fn healthy(&self) -> bool {
2825
self.healthy.load(Ordering::Relaxed)
2926
}
3027
}

pgdog/src/backend/pool/lb/test.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ async fn test_unban_if_expired_checks_pool_health() {
277277
ban.ban(Error::ServerError, Duration::from_millis(50));
278278
assert!(ban.banned());
279279

280-
pool.inner().health.toggle(false);
280+
pool.inner().health().toggle(false);
281281

282282
sleep(Duration::from_millis(60)).await;
283283

@@ -814,7 +814,7 @@ async fn test_monitor_shuts_down_on_notify() {
814814
async fn test_monitor_bans_unhealthy_target() {
815815
let replicas = setup_test_replicas();
816816

817-
replicas.targets[0].health.toggle(false);
817+
replicas.targets[0].health().toggle(false);
818818

819819
sleep(Duration::from_millis(400)).await;
820820

@@ -851,7 +851,7 @@ async fn test_monitor_does_not_ban_single_target() {
851851
);
852852
replicas.launch();
853853

854-
replicas.targets[0].health.toggle(false);
854+
replicas.targets[0].health().toggle(false);
855855

856856
sleep(Duration::from_millis(400)).await;
857857

@@ -864,8 +864,8 @@ async fn test_monitor_does_not_ban_single_target() {
864864
async fn test_monitor_unbans_all_when_all_unhealthy() {
865865
let replicas = setup_test_replicas();
866866

867-
replicas.targets[0].health.toggle(false);
868-
replicas.targets[1].health.toggle(false);
867+
replicas.targets[0].health().toggle(false);
868+
replicas.targets[1].health().toggle(false);
869869

870870
sleep(Duration::from_millis(400)).await;
871871

@@ -924,7 +924,7 @@ async fn test_monitor_does_not_ban_with_zero_ban_timeout() {
924924
);
925925
replicas.launch();
926926

927-
replicas.targets[0].health.toggle(false);
927+
replicas.targets[0].health().toggle(false);
928928

929929
sleep(Duration::from_millis(400)).await;
930930

@@ -942,9 +942,9 @@ async fn test_monitor_health_state_race() {
942942

943943
let toggle_task = spawn(async move {
944944
for _ in 0..50 {
945-
target.health.toggle(false);
945+
target.health().toggle(false);
946946
sleep(Duration::from_micros(100)).await;
947-
target.health.toggle(true);
947+
target.health().toggle(true);
948948
sleep(Duration::from_micros(100)).await;
949949
}
950950
});
@@ -954,7 +954,7 @@ async fn test_monitor_health_state_race() {
954954
toggle_task.await.unwrap();
955955

956956
let banned = replicas.targets[0].ban.banned();
957-
let healthy = replicas.targets[0].health.healthy();
957+
let healthy = replicas.targets[0].health().healthy();
958958

959959
assert!(
960960
!banned || !healthy,
@@ -1471,7 +1471,7 @@ async fn test_monitor_unbans_all_when_second_target_becomes_unhealthy_after_firs
14711471
let replicas = setup_test_replicas();
14721472

14731473
// First target becomes unhealthy
1474-
replicas.targets[0].health.toggle(false);
1474+
replicas.targets[0].health().toggle(false);
14751475

14761476
// Wait for monitor to ban the first target
14771477
sleep(Duration::from_millis(400)).await;
@@ -1486,7 +1486,7 @@ async fn test_monitor_unbans_all_when_second_target_becomes_unhealthy_after_firs
14861486
);
14871487

14881488
// Now second target becomes unhealthy (first is already banned)
1489-
replicas.targets[1].health.toggle(false);
1489+
replicas.targets[1].health().toggle(false);
14901490

14911491
// Wait for monitor to process - should unban all since all are unhealthy
14921492
sleep(Duration::from_millis(400)).await;
@@ -1739,7 +1739,7 @@ fn test_ban_check_does_not_clear_expired_ban_when_healthy_with_bad_lag() {
17391739
let replicas = setup_test_replicas_no_launch();
17401740

17411741
// Target is healthy (default)
1742-
assert!(replicas.targets[0].health.healthy());
1742+
assert!(replicas.targets[0].health().healthy());
17431743

17441744
// Ban with short timeout
17451745
replicas.targets[0]
@@ -1776,7 +1776,7 @@ fn test_ban_check_does_not_clear_expired_ban_when_unhealthy_with_bad_lag() {
17761776
let replicas = setup_test_replicas_no_launch();
17771777

17781778
// Set target as unhealthy
1779-
replicas.targets[0].health.toggle(false);
1779+
replicas.targets[0].health().toggle(false);
17801780

17811781
// Ban with short timeout
17821782
replicas.targets[0]
@@ -1813,7 +1813,7 @@ fn test_ban_check_bans_unhealthy_replica_with_bad_lag() {
18131813
let replicas = setup_test_replicas_no_launch();
18141814

18151815
// Set target as unhealthy
1816-
replicas.targets[0].health.toggle(false);
1816+
replicas.targets[0].health().toggle(false);
18171817

18181818
// Set replica lag on the pool
18191819
replicas.targets[0].pool.lock().replica_lag = ReplicaLag {
@@ -1842,7 +1842,7 @@ fn test_ban_check_bans_healthy_replica_with_bad_lag() {
18421842
let replicas = setup_test_replicas_no_launch();
18431843

18441844
// Target stays healthy (default)
1845-
assert!(replicas.targets[0].health.healthy());
1845+
assert!(replicas.targets[0].health().healthy());
18461846

18471847
// Set replica lag on the pool
18481848
replicas.targets[0].pool.lock().replica_lag = ReplicaLag {
@@ -1874,7 +1874,7 @@ fn test_ban_check_bans_with_pool_unhealthy_reason() {
18741874
let replicas = setup_test_replicas_no_launch();
18751875

18761876
// Set target as unhealthy
1877-
replicas.targets[0].health.toggle(false);
1877+
replicas.targets[0].health().toggle(false);
18781878

18791879
// No replica lag set (defaults to zero)
18801880

@@ -1908,7 +1908,7 @@ fn test_ban_check_does_not_ban_single_target() {
19081908
// Don't launch - we're unit testing ban_check
19091909

19101910
// Set target as unhealthy
1911-
replicas.targets[0].health.toggle(false);
1911+
replicas.targets[0].health().toggle(false);
19121912

19131913
let monitor = Monitor::new_test(&replicas);
19141914
let threshold = ReplicaLag {
@@ -1973,7 +1973,7 @@ fn test_ban_check_does_not_ban_with_zero_ban_timeout() {
19731973
);
19741974

19751975
// Set target as unhealthy
1976-
replicas.targets[0].health.toggle(false);
1976+
replicas.targets[0].health().toggle(false);
19771977

19781978
let monitor = Monitor::new_test(&replicas);
19791979
let threshold = ReplicaLag {
@@ -2002,8 +2002,8 @@ fn test_ban_check_unbans_all_when_all_unhealthy() {
20022002
.ban(Error::ServerError, Duration::from_secs(60));
20032003

20042004
// Set both as unhealthy
2005-
replicas.targets[0].health.toggle(false);
2006-
replicas.targets[1].health.toggle(false);
2005+
replicas.targets[0].health().toggle(false);
2006+
replicas.targets[1].health().toggle(false);
20072007

20082008
assert!(replicas.targets[0].ban.banned());
20092009
assert!(replicas.targets[1].ban.banned());
@@ -2043,8 +2043,8 @@ fn test_ban_check_unbans_all_when_all_healthy_but_banned() {
20432043
.ban
20442044
.ban(Error::ConnectTimeout, Duration::from_secs(60));
20452045

2046-
assert!(replicas.targets[0].health.healthy());
2047-
assert!(replicas.targets[1].health.healthy());
2046+
assert!(replicas.targets[0].health().healthy());
2047+
assert!(replicas.targets[1].health().healthy());
20482048
assert!(replicas.targets[0].ban.banned());
20492049
assert!(replicas.targets[1].ban.banned());
20502050

@@ -2126,7 +2126,7 @@ fn test_ban_check_default_threshold_does_not_ban_healthy_replica_with_high_lag()
21262126
let replicas = setup_test_replicas_no_launch();
21272127

21282128
// Target is healthy (default)
2129-
assert!(replicas.targets[0].health.healthy());
2129+
assert!(replicas.targets[0].health().healthy());
21302130

21312131
// Set very high replica lag on the pool
21322132
replicas.targets[0].pool.lock().replica_lag = ReplicaLag {
@@ -2160,7 +2160,7 @@ fn test_ban_check_default_threshold_bans_unhealthy_with_pool_unhealthy_reason()
21602160
};
21612161

21622162
// Set target as unhealthy
2163-
replicas.targets[0].health.toggle(false);
2163+
replicas.targets[0].health().toggle(false);
21642164

21652165
let monitor = Monitor::new_test(&replicas);
21662166
// Use default config thresholds (MAX values)

pgdog/src/backend/pool/pool_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Pool {
7676
inner: Mutex::new(Inner::new(config.config, id)),
7777
id,
7878
config: config.config,
79-
health: TargetHealth::new(id),
79+
health: TargetHealth::new(),
8080
params: OnceCell::new(),
8181
lsn_stats: RwLock::new(LsnStats::default()),
8282
lsn_role_change: Notify::new(),

0 commit comments

Comments
 (0)