Files
pgcat/src/pool.rs

306 lines
9.4 KiB
Rust
Raw Normal View History

2022-02-05 13:25:03 -08:00
/// Pooling and failover and banlist.
2022-02-03 16:25:05 -08:00
use async_trait::async_trait;
2022-02-05 18:20:53 -08:00
use bb8::{ManageConnection, Pool, PooledConnection};
2022-02-05 10:02:13 -08:00
use chrono::naive::NaiveDateTime;
2022-02-03 16:25:05 -08:00
2022-02-08 09:25:59 -08:00
use crate::config::{Address, Config, User};
2022-02-03 16:25:05 -08:00
use crate::errors::Error;
use crate::server::Server;
2022-02-05 10:02:13 -08:00
use std::collections::HashMap;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex,
};
// Banlist: bad servers go in here.
2022-02-06 11:13:12 -08:00
pub type BanList = Arc<Mutex<Vec<HashMap<Address, NaiveDateTime>>>>;
2022-02-05 10:02:13 -08:00
pub type Counter = Arc<AtomicUsize>;
pub type ClientServerMap = Arc<Mutex<HashMap<(i32, i32), (i32, i32, String, String)>>>;
2022-02-03 16:25:05 -08:00
2022-02-05 13:25:03 -08:00
// 60 seconds of ban time.
// After that, the replica will be allowed to serve traffic again.
const BAN_TIME: i64 = 60;
2022-02-06 11:13:12 -08:00
// DB pool size (per actual database server)
2022-02-05 18:20:53 -08:00
const POOL_SIZE: u32 = 15;
2022-02-05 13:25:03 -08:00
2022-02-06 11:13:12 -08:00
// 5 seconds to connect before we give up
const CONNECT_TIMEOUT: u64 = 5000;
// How much time to give the server to answer a SELECT 1 query.
const HEALTHCHECK_TIMEOUT: u64 = 1000;
2022-02-08 09:25:59 -08:00
#[derive(Clone, Debug)]
2022-02-05 18:20:53 -08:00
pub struct ConnectionPool {
2022-02-05 19:43:48 -08:00
databases: Vec<Vec<Pool<ServerPool>>>,
addresses: Vec<Vec<Address>>,
2022-02-05 18:20:53 -08:00
round_robin: Counter,
banlist: BanList,
2022-02-08 09:25:59 -08:00
healthcheck_timeout: u64,
2022-02-08 09:28:53 -08:00
ban_time: i64,
2022-02-03 16:25:05 -08:00
}
2022-02-05 18:20:53 -08:00
impl ConnectionPool {
2022-02-08 09:25:59 -08:00
// Construct the connection pool for a single-shard cluster.
2022-02-05 18:20:53 -08:00
pub async fn new(
addresses: Vec<Address>,
2022-02-05 13:15:53 -08:00
user: User,
2022-02-04 16:01:35 -08:00
database: &str,
client_server_map: ClientServerMap,
2022-02-05 18:20:53 -08:00
) -> ConnectionPool {
let mut databases = Vec::new();
for address in &addresses {
let manager = ServerPool::new(
address.clone(),
user.clone(),
database,
client_server_map.clone(),
);
let pool = Pool::builder()
.max_size(POOL_SIZE)
2022-02-06 11:13:12 -08:00
.connection_timeout(std::time::Duration::from_millis(CONNECT_TIMEOUT))
2022-02-05 18:20:53 -08:00
.test_on_check_out(false)
.build(manager)
.await
.unwrap();
databases.push(pool);
2022-02-03 16:25:05 -08:00
}
2022-02-05 18:20:53 -08:00
ConnectionPool {
2022-02-05 19:43:48 -08:00
databases: vec![databases],
addresses: vec![addresses],
2022-02-05 18:20:53 -08:00
round_robin: Arc::new(AtomicUsize::new(0)),
2022-02-06 11:13:12 -08:00
banlist: Arc::new(Mutex::new(vec![HashMap::new()])),
2022-02-08 09:25:59 -08:00
healthcheck_timeout: HEALTHCHECK_TIMEOUT,
2022-02-08 09:28:53 -08:00
ban_time: BAN_TIME,
2022-02-05 13:15:53 -08:00
}
2022-02-03 16:25:05 -08:00
}
2022-02-08 09:25:59 -08:00
/// Construct the connection pool from a config file.
pub async fn from_config(config: Config, client_server_map: ClientServerMap) -> ConnectionPool {
let mut shards = Vec::new();
let mut addresses = Vec::new();
let mut banlist = Vec::new();
let mut shard_ids = config
.shards
.clone()
.into_keys()
.map(|x| x.to_string())
.collect::<Vec<String>>();
shard_ids.sort_by_key(|k| k.parse::<i64>().unwrap());
for shard in shard_ids {
let shard = &config.shards[&shard];
let mut pools = Vec::new();
let mut replica_addresses = Vec::new();
for server in &shard.servers {
let address = Address {
host: server.0.clone(),
port: server.1.to_string(),
};
let manager = ServerPool::new(
address.clone(),
config.user.clone(),
&shard.database,
client_server_map.clone(),
);
let pool = Pool::builder()
.max_size(config.general.pool_size)
.connection_timeout(std::time::Duration::from_millis(
config.general.connect_timeout,
))
.test_on_check_out(false)
.build(manager)
.await
.unwrap();
pools.push(pool);
replica_addresses.push(address);
}
shards.push(pools);
addresses.push(replica_addresses);
banlist.push(HashMap::new());
}
ConnectionPool {
databases: shards,
addresses: addresses,
round_robin: Arc::new(AtomicUsize::new(0)),
banlist: Arc::new(Mutex::new(banlist)),
healthcheck_timeout: config.general.healthcheck_timeout,
2022-02-08 09:28:53 -08:00
ban_time: config.general.ban_time,
2022-02-08 09:25:59 -08:00
}
}
/// Get a connection from the pool.
2022-02-05 18:20:53 -08:00
pub async fn get(
&self,
2022-02-05 19:43:48 -08:00
shard: Option<usize>,
2022-02-05 18:20:53 -08:00
) -> Result<(PooledConnection<'_, ServerPool>, Address), Error> {
2022-02-05 19:43:48 -08:00
// Set this to false to gain ~3-4% speed.
let with_health_check = true;
let shard = match shard {
Some(shard) => shard,
None => 0, // TODO: pick a shard at random
};
loop {
let index =
self.round_robin.fetch_add(1, Ordering::SeqCst) % self.databases[shard].len();
let address = self.addresses[shard][index].clone();
2022-02-06 11:13:12 -08:00
if self.is_banned(&address, shard) {
2022-02-05 19:43:48 -08:00
continue;
}
// Check if we can connect
2022-02-06 11:13:12 -08:00
// TODO: implement query wait timeout, i.e. time to get a conn from the pool
2022-02-05 19:43:48 -08:00
let mut conn = match self.databases[shard][index].get().await {
Ok(conn) => conn,
Err(err) => {
println!(">> Banning replica {}, error: {:?}", index, err);
2022-02-06 11:13:12 -08:00
self.ban(&address, shard);
2022-02-05 19:43:48 -08:00
continue;
2022-02-05 18:20:53 -08:00
}
2022-02-05 19:43:48 -08:00
};
if !with_health_check {
return Ok((conn, address));
2022-02-05 13:15:53 -08:00
}
2022-02-03 16:25:05 -08:00
2022-02-05 19:43:48 -08:00
// // Check if this server is alive with a health check
let server = &mut *conn;
match tokio::time::timeout(
2022-02-06 11:13:12 -08:00
tokio::time::Duration::from_millis(HEALTHCHECK_TIMEOUT),
2022-02-05 19:43:48 -08:00
server.query("SELECT 1"),
)
.await
{
Ok(_) => return Ok((conn, address)),
Err(_) => {
println!(
">> Banning replica {} because of failed health check",
index
);
2022-02-06 11:13:12 -08:00
self.ban(&address, shard);
2022-02-05 19:43:48 -08:00
continue;
2022-02-05 18:20:53 -08:00
}
}
2022-02-05 10:02:13 -08:00
}
}
2022-02-05 13:25:03 -08:00
/// Ban an address (i.e. replica). It no longer will serve
/// traffic for any new transactions. Existing transactions on that replica
/// will finish successfully or error out to the clients.
2022-02-06 11:13:12 -08:00
pub fn ban(&self, address: &Address, shard: usize) {
2022-02-05 13:15:53 -08:00
println!(">> Banning {:?}", address);
2022-02-05 10:02:13 -08:00
let now = chrono::offset::Utc::now().naive_utc();
let mut guard = self.banlist.lock().unwrap();
2022-02-06 11:13:12 -08:00
guard[shard].insert(address.clone(), now);
2022-02-05 10:02:13 -08:00
}
2022-02-05 13:25:03 -08:00
/// Clear the replica to receive traffic again. Takes effect immediately
/// for all new transactions.
2022-02-06 11:13:12 -08:00
pub fn unban(&self, address: &Address, shard: usize) {
2022-02-05 10:02:13 -08:00
let mut guard = self.banlist.lock().unwrap();
2022-02-06 11:13:12 -08:00
guard[shard].remove(address);
2022-02-05 10:02:13 -08:00
}
2022-02-05 13:25:03 -08:00
/// Check if a replica can serve traffic. If all replicas are banned,
/// we unban all of them. Better to try then not to.
2022-02-06 11:13:12 -08:00
pub fn is_banned(&self, address: &Address, shard: usize) -> bool {
2022-02-05 10:02:13 -08:00
let mut guard = self.banlist.lock().unwrap();
// Everything is banned, nothig is banned
2022-02-06 11:13:12 -08:00
if guard[shard].len() == self.databases[shard].len() {
2022-02-06 12:52:59 -08:00
guard[shard].clear();
2022-02-05 13:15:53 -08:00
drop(guard);
2022-02-05 13:25:03 -08:00
println!(">> Unbanning all replicas.");
2022-02-05 10:02:13 -08:00
return false;
}
// I expect this to miss 99.9999% of the time.
2022-02-06 11:13:12 -08:00
match guard[shard].get(address) {
2022-02-05 10:02:13 -08:00
Some(timestamp) => {
let now = chrono::offset::Utc::now().naive_utc();
2022-02-08 09:28:53 -08:00
if now.timestamp() - timestamp.timestamp() > self.ban_time {
2022-02-05 10:02:13 -08:00
// 1 minute
2022-02-06 11:13:12 -08:00
guard[shard].remove(address);
2022-02-05 10:02:13 -08:00
false
} else {
true
}
}
None => false,
}
}
2022-02-05 19:43:48 -08:00
pub fn shards(&self) -> usize {
self.databases.len()
}
2022-02-05 18:20:53 -08:00
}
2022-02-05 10:02:13 -08:00
2022-02-05 18:20:53 -08:00
pub struct ServerPool {
address: Address,
user: User,
database: String,
client_server_map: ClientServerMap,
}
impl ServerPool {
pub fn new(
address: Address,
user: User,
database: &str,
client_server_map: ClientServerMap,
) -> ServerPool {
ServerPool {
address: address,
user: user,
database: database.to_string(),
client_server_map: client_server_map,
2022-02-05 10:02:13 -08:00
}
}
}
2022-02-05 18:20:53 -08:00
#[async_trait]
impl ManageConnection for ServerPool {
type Connection = Server;
type Error = Error;
/// Attempts to create a new connection.
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
println!(">> Getting new connection from the pool");
Server::startup(
&self.address.host,
&self.address.port,
&self.user.name,
&self.user.password,
&self.database,
self.client_server_map.clone(),
)
.await
}
/// Determines if the connection is still connected to the database.
async fn is_valid(&self, _conn: &mut PooledConnection<'_, Self>) -> Result<(), Self::Error> {
Ok(())
}
/// Synchronously determine if the connection is no longer usable, if possible.
fn has_broken(&self, conn: &mut Self::Connection) -> bool {
conn.is_bad()
}
}