Skip to content

Commit c9544bd

Browse files
authored
Fix default_role being ignored when query_parser_enabled was false (#847)
Fix default_role being ignored when query_parser_enabled was false
1 parent cdcfa99 commit c9544bd

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/client.rs

+1
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ where
881881
};
882882

883883
query_router.update_pool_settings(&pool.settings);
884+
query_router.set_default_role();
884885

885886
// Our custom protocol loop.
886887
// We expect the client to either start a transaction with regular queries

src/query_router.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,11 @@ impl QueryRouter {
10611061
self.active_shard
10621062
}
10631063

1064+
/// Set active_role as the default_role specified in the pool.
1065+
pub fn set_default_role(&mut self) {
1066+
self.active_role = self.pool_settings.default_role;
1067+
}
1068+
10641069
/// Get the current desired server role we should be talking to.
10651070
pub fn role(&self) -> Option<Role> {
10661071
self.active_role

tests/ruby/load_balancing_spec.rb

+35-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,41 @@
5656
end
5757
end
5858
end
59+
60+
context "when all replicas are down " do
61+
let(:processes) { Helpers::Pgcat.single_shard_setup("sharded_db", 5, "transaction", "random", "debug", {"default_role" => "replica"}) }
62+
63+
it "unbans them automatically to prevent false positives in health checks that could make all replicas unavailable" do
64+
conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
65+
failed_count = 0
66+
number_of_replicas = processes[:replicas].length
67+
68+
# Take down all replicas
69+
processes[:replicas].each(&:take_down)
70+
71+
(number_of_replicas + 1).times do |n|
72+
conn.async_exec("SELECT 1 + 2")
73+
rescue
74+
conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
75+
failed_count += 1
76+
end
77+
78+
expect(failed_count).to eq(number_of_replicas + 1)
79+
failed_count = 0
80+
81+
# Ban_time is configured to 60 so this reset will only work
82+
# if the replicas are unbanned automatically
83+
processes[:replicas].each(&:reset)
84+
85+
number_of_replicas.times do
86+
conn.async_exec("SELECT 1 + 2")
87+
rescue
88+
conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
89+
failed_count += 1
90+
end
91+
expect(failed_count).to eq(0)
92+
end
93+
end
5994
end
6095

6196
describe "Least Outstanding Queries Load Balancing" do
@@ -161,4 +196,3 @@
161196
end
162197
end
163198
end
164-

0 commit comments

Comments
 (0)