mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-26 18:36:28 +00:00
Fix incorrect routing for replicas (#139)
* Fix incorrect routing for replicas * name
This commit is contained in:
@@ -63,9 +63,10 @@ pub struct Address {
|
|||||||
pub shard: usize,
|
pub shard: usize,
|
||||||
pub database: String,
|
pub database: String,
|
||||||
pub role: Role,
|
pub role: Role,
|
||||||
pub instance_index: usize,
|
pub replica_number: usize,
|
||||||
|
pub address_index: usize,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub poolname: String,
|
pub pool_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Address {
|
impl Default for Address {
|
||||||
@@ -75,11 +76,12 @@ impl Default for Address {
|
|||||||
host: String::from("127.0.0.1"),
|
host: String::from("127.0.0.1"),
|
||||||
port: String::from("5432"),
|
port: String::from("5432"),
|
||||||
shard: 0,
|
shard: 0,
|
||||||
instance_index: 0,
|
address_index: 0,
|
||||||
|
replica_number: 0,
|
||||||
database: String::from("database"),
|
database: String::from("database"),
|
||||||
role: Role::Replica,
|
role: Role::Replica,
|
||||||
username: String::from("username"),
|
username: String::from("username"),
|
||||||
poolname: String::from("poolname"),
|
pool_name: String::from("pool_name"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,11 +90,11 @@ impl Address {
|
|||||||
/// Address name (aka database) used in `SHOW STATS`, `SHOW DATABASES`, and `SHOW POOLS`.
|
/// Address name (aka database) used in `SHOW STATS`, `SHOW DATABASES`, and `SHOW POOLS`.
|
||||||
pub fn name(&self) -> String {
|
pub fn name(&self) -> String {
|
||||||
match self.role {
|
match self.role {
|
||||||
Role::Primary => format!("{}_shard_{}_primary", self.poolname, self.shard),
|
Role::Primary => format!("{}_shard_{}_primary", self.pool_name, self.shard),
|
||||||
|
|
||||||
Role::Replica => format!(
|
Role::Replica => format!(
|
||||||
"{}_shard_{}_replica_{}",
|
"{}_shard_{}_replica_{}",
|
||||||
self.poolname, self.shard, self.instance_index
|
self.pool_name, self.shard, self.replica_number
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,6 +102,7 @@ impl ConnectionPool {
|
|||||||
let shard = &pool_config.shards[&shard_idx];
|
let shard = &pool_config.shards[&shard_idx];
|
||||||
let mut pools = Vec::new();
|
let mut pools = Vec::new();
|
||||||
let mut servers = Vec::new();
|
let mut servers = Vec::new();
|
||||||
|
let mut address_index = 0;
|
||||||
let mut replica_number = 0;
|
let mut replica_number = 0;
|
||||||
|
|
||||||
for server in shard.servers.iter() {
|
for server in shard.servers.iter() {
|
||||||
@@ -120,13 +121,15 @@ impl ConnectionPool {
|
|||||||
host: server.0.clone(),
|
host: server.0.clone(),
|
||||||
port: server.1.to_string(),
|
port: server.1.to_string(),
|
||||||
role: role,
|
role: role,
|
||||||
instance_index: replica_number,
|
address_index,
|
||||||
|
replica_number,
|
||||||
shard: shard_idx.parse::<usize>().unwrap(),
|
shard: shard_idx.parse::<usize>().unwrap(),
|
||||||
username: user_info.username.clone(),
|
username: user_info.username.clone(),
|
||||||
poolname: pool_name.clone(),
|
pool_name: pool_name.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
address_id += 1;
|
address_id += 1;
|
||||||
|
address_index += 1;
|
||||||
|
|
||||||
if role == Role::Replica {
|
if role == Role::Replica {
|
||||||
replica_number += 1;
|
replica_number += 1;
|
||||||
@@ -276,7 +279,7 @@ impl ConnectionPool {
|
|||||||
self.stats.client_waiting(process_id, address.id);
|
self.stats.client_waiting(process_id, address.id);
|
||||||
|
|
||||||
// Check if we can connect
|
// Check if we can connect
|
||||||
let mut conn = match self.databases[address.shard][address.instance_index]
|
let mut conn = match self.databases[address.shard][address.address_index]
|
||||||
.get()
|
.get()
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user