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
1 change: 1 addition & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[tools]
"rust" = { version = "1.91.0" }
"cargo:cargo-nextest" = "latest"
"cargo:cargo-watch" = "latest"
5 changes: 4 additions & 1 deletion pgdog/src/backend/pool/lb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ impl LoadBalancer {
let primary_reads = match self.rw_split {
IncludePrimary => true,
IncludePrimaryIfReplicaBanned => candidates.iter().any(|target| target.ban.banned()),
ExcludePrimary => false,
// we read from the primary if we have no replicas
ExcludePrimary => !candidates
.iter()
.any(|target| target.role() == Role::Replica),
};

if !primary_reads {
Expand Down
36 changes: 36 additions & 0 deletions pgdog/src/backend/pool/lb/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,42 @@ async fn test_read_write_split_include_primary() {
replicas.shutdown();
}

#[tokio::test]
async fn test_read_write_split_exclude_primary_no_replicas() {
let primary_config = create_test_pool_config("127.0.0.1", 5432);
let primary_pool = Pool::new(&primary_config);
primary_pool.launch();

let replica_configs = [];

let replicas = LoadBalancer::new(
&Some(primary_pool),
&replica_configs,
LoadBalancingStrategy::RoundRobin,
ReadWriteSplit::ExcludePrimary,
);
replicas.launch();

let request = Request::default();

// Try getting connections multiple times and we have primary in the set
let mut used_pool_ids = HashSet::new();
for _ in 0..2 {
let conn = replicas.get(&request).await.unwrap();
used_pool_ids.insert(conn.pool.id());
}

// Should use only primary
assert_eq!(used_pool_ids.len(), 1);

// Verify primary pool ID is in the set of used pools
let primary_id = replicas.primary().unwrap().id();
assert!(used_pool_ids.contains(&primary_id));

// Shutdown
replicas.shutdown();
}

#[tokio::test]
async fn test_read_write_split_exclude_primary_no_primary() {
// Test exclude primary setting when no primary exists
Expand Down
Loading