From f3d2741c543f4ef4e135471477f29c7ac0c59f52 Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Sun, 21 Aug 2022 22:36:54 -0700 Subject: [PATCH 1/2] Fix incorrect routing for replicas --- src/config.rs | 8 +++++--- src/pool.rs | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index 9d1658ff..a6ac863f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -63,7 +63,8 @@ pub struct Address { pub shard: usize, pub database: String, pub role: Role, - pub instance_index: usize, + pub replica_number: usize, + pub address_index: usize, pub username: String, pub poolname: String, } @@ -75,7 +76,8 @@ impl Default for Address { host: String::from("127.0.0.1"), port: String::from("5432"), shard: 0, - instance_index: 0, + address_index: 0, + replica_number: 0, database: String::from("database"), role: Role::Replica, username: String::from("username"), @@ -92,7 +94,7 @@ impl Address { Role::Replica => format!( "{}_shard_{}_replica_{}", - self.poolname, self.shard, self.instance_index + self.poolname, self.shard, self.replica_number ), } } diff --git a/src/pool.rs b/src/pool.rs index cbb9b43f..2f20eecb 100644 --- a/src/pool.rs +++ b/src/pool.rs @@ -102,6 +102,7 @@ impl ConnectionPool { let shard = &pool_config.shards[&shard_idx]; let mut pools = Vec::new(); let mut servers = Vec::new(); + let mut address_index = 0; let mut replica_number = 0; for server in shard.servers.iter() { @@ -120,13 +121,15 @@ impl ConnectionPool { host: server.0.clone(), port: server.1.to_string(), role: role, - instance_index: replica_number, + address_index, + replica_number, shard: shard_idx.parse::().unwrap(), username: user_info.username.clone(), poolname: pool_name.clone(), }; address_id += 1; + address_index += 1; if role == Role::Replica { replica_number += 1; @@ -276,7 +279,7 @@ impl ConnectionPool { self.stats.client_waiting(process_id, address.id); // 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() .await { From 4b4919cfe122daaf13c636a765d226bbf9553ae1 Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Sun, 21 Aug 2022 22:40:01 -0700 Subject: [PATCH 2/2] name --- src/config.rs | 8 ++++---- src/pool.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index a6ac863f..b7516937 100644 --- a/src/config.rs +++ b/src/config.rs @@ -66,7 +66,7 @@ pub struct Address { pub replica_number: usize, pub address_index: usize, pub username: String, - pub poolname: String, + pub pool_name: String, } impl Default for Address { @@ -81,7 +81,7 @@ impl Default for Address { database: String::from("database"), role: Role::Replica, username: String::from("username"), - poolname: String::from("poolname"), + pool_name: String::from("pool_name"), } } } @@ -90,11 +90,11 @@ impl Address { /// Address name (aka database) used in `SHOW STATS`, `SHOW DATABASES`, and `SHOW POOLS`. pub fn name(&self) -> String { 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!( "{}_shard_{}_replica_{}", - self.poolname, self.shard, self.replica_number + self.pool_name, self.shard, self.replica_number ), } } diff --git a/src/pool.rs b/src/pool.rs index 2f20eecb..ac5bc911 100644 --- a/src/pool.rs +++ b/src/pool.rs @@ -125,7 +125,7 @@ impl ConnectionPool { replica_number, shard: shard_idx.parse::().unwrap(), username: user_info.username.clone(), - poolname: pool_name.clone(), + pool_name: pool_name.clone(), }; address_id += 1;