2022-02-19 13:57:35 -08:00
|
|
|
use arc_swap::{ArcSwap, Guard};
|
2022-02-20 22:47:08 -08:00
|
|
|
use log::{error, info};
|
2022-02-19 13:57:35 -08:00
|
|
|
use once_cell::sync::Lazy;
|
2022-02-08 09:25:59 -08:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
|
use tokio::fs::File;
|
|
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
|
use toml;
|
|
|
|
|
|
2022-02-09 21:19:14 -08:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2022-02-19 13:57:35 -08:00
|
|
|
use std::sync::Arc;
|
2022-02-08 17:08:17 -08:00
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
use crate::errors::Error;
|
|
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
static CONFIG: Lazy<ArcSwap<Config>> = Lazy::new(|| ArcSwap::from_pointee(Config::default()));
|
|
|
|
|
|
2022-02-09 20:02:20 -08:00
|
|
|
#[derive(Clone, PartialEq, Deserialize, Hash, std::cmp::Eq, Debug, Copy)]
|
|
|
|
|
pub enum Role {
|
|
|
|
|
Primary,
|
|
|
|
|
Replica,
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 07:10:18 -08:00
|
|
|
impl PartialEq<Option<Role>> for Role {
|
|
|
|
|
fn eq(&self, other: &Option<Role>) -> bool {
|
|
|
|
|
match other {
|
|
|
|
|
None => true,
|
|
|
|
|
Some(role) => *self == *role,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PartialEq<Role> for Option<Role> {
|
|
|
|
|
fn eq(&self, other: &Role) -> bool {
|
|
|
|
|
match *self {
|
|
|
|
|
None => true,
|
|
|
|
|
Some(role) => role == *other,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 13:15:53 -08:00
|
|
|
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Debug)]
|
2022-02-05 10:02:13 -08:00
|
|
|
pub struct Address {
|
|
|
|
|
pub host: String,
|
|
|
|
|
pub port: String,
|
2022-02-15 08:18:01 -08:00
|
|
|
pub shard: usize,
|
2022-02-09 20:02:20 -08:00
|
|
|
pub role: Role,
|
2022-02-05 10:02:13 -08:00
|
|
|
}
|
|
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
impl Default for Address {
|
|
|
|
|
fn default() -> Address {
|
|
|
|
|
Address {
|
|
|
|
|
host: String::from("127.0.0.1"),
|
|
|
|
|
port: String::from("5432"),
|
|
|
|
|
shard: 0,
|
|
|
|
|
role: Role::Replica,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Deserialize, Debug)]
|
2022-02-05 10:02:13 -08:00
|
|
|
pub struct User {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub password: String,
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
impl Default for User {
|
|
|
|
|
fn default() -> User {
|
|
|
|
|
User {
|
|
|
|
|
name: String::from("postgres"),
|
|
|
|
|
password: String::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
|
pub struct General {
|
|
|
|
|
pub host: String,
|
|
|
|
|
pub port: i16,
|
|
|
|
|
pub pool_size: u32,
|
|
|
|
|
pub pool_mode: String,
|
|
|
|
|
pub connect_timeout: u64,
|
|
|
|
|
pub healthcheck_timeout: u64,
|
|
|
|
|
pub ban_time: i64,
|
2022-02-19 13:57:35 -08:00
|
|
|
pub statsd_address: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for General {
|
|
|
|
|
fn default() -> General {
|
|
|
|
|
General {
|
|
|
|
|
host: String::from("localhost"),
|
|
|
|
|
port: 5432,
|
|
|
|
|
pool_size: 15,
|
|
|
|
|
pool_mode: String::from("transaction"),
|
|
|
|
|
connect_timeout: 5000,
|
|
|
|
|
healthcheck_timeout: 1000,
|
|
|
|
|
ban_time: 60,
|
|
|
|
|
statsd_address: String::from("127.0.0.1:8125"),
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-08 09:25:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
|
pub struct Shard {
|
2022-02-09 20:02:20 -08:00
|
|
|
pub servers: Vec<(String, u16, String)>,
|
2022-02-08 09:25:59 -08:00
|
|
|
pub database: String,
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
impl Default for Shard {
|
|
|
|
|
fn default() -> Shard {
|
|
|
|
|
Shard {
|
|
|
|
|
servers: vec![(String::from("localhost"), 5432, String::from("primary"))],
|
|
|
|
|
database: String::from("postgres"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 11:19:40 -08:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
|
pub struct QueryRouter {
|
|
|
|
|
pub default_role: String,
|
2022-02-18 07:10:18 -08:00
|
|
|
pub query_parser_enabled: bool,
|
|
|
|
|
pub primary_reads_enabled: bool,
|
2022-02-23 11:47:24 -08:00
|
|
|
pub sharding_function: String,
|
2022-02-11 11:19:40 -08:00
|
|
|
}
|
|
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
impl Default for QueryRouter {
|
|
|
|
|
fn default() -> QueryRouter {
|
|
|
|
|
QueryRouter {
|
|
|
|
|
default_role: String::from("any"),
|
|
|
|
|
query_parser_enabled: false,
|
|
|
|
|
primary_reads_enabled: true,
|
2022-02-23 11:47:24 -08:00
|
|
|
sharding_function: "pg_bigint_hash".to_string(),
|
2022-02-19 13:57:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
|
pub struct Config {
|
|
|
|
|
pub general: General,
|
|
|
|
|
pub user: User,
|
|
|
|
|
pub shards: HashMap<String, Shard>,
|
2022-02-11 11:19:40 -08:00
|
|
|
pub query_router: QueryRouter,
|
2022-02-08 09:25:59 -08:00
|
|
|
}
|
|
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
impl Default for Config {
|
|
|
|
|
fn default() -> Config {
|
|
|
|
|
Config {
|
|
|
|
|
general: General::default(),
|
|
|
|
|
user: User::default(),
|
|
|
|
|
shards: HashMap::from([(String::from("1"), Shard::default())]),
|
|
|
|
|
query_router: QueryRouter::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
|
pub fn show(&self) {
|
2022-02-20 22:47:08 -08:00
|
|
|
info!("Pool size: {}", self.general.pool_size);
|
|
|
|
|
info!("Pool mode: {}", self.general.pool_mode);
|
|
|
|
|
info!("Ban time: {}s", self.general.ban_time);
|
|
|
|
|
info!(
|
|
|
|
|
"Healthcheck timeout: {}ms",
|
2022-02-19 13:57:35 -08:00
|
|
|
self.general.healthcheck_timeout
|
|
|
|
|
);
|
2022-02-20 22:47:08 -08:00
|
|
|
info!("Connection timeout: {}ms", self.general.connect_timeout);
|
2022-02-23 11:47:24 -08:00
|
|
|
info!("Sharding function: {}", self.query_router.sharding_function);
|
|
|
|
|
info!("Number of shards: {}", self.shards.len());
|
2022-02-19 13:57:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_config() -> Guard<Arc<Config>> {
|
|
|
|
|
CONFIG.load()
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 06:51:31 -08:00
|
|
|
/// Parse the config.
|
2022-02-19 13:57:35 -08:00
|
|
|
pub async fn parse(path: &str) -> Result<(), Error> {
|
2022-02-08 09:25:59 -08:00
|
|
|
let mut contents = String::new();
|
|
|
|
|
let mut file = match File::open(path).await {
|
|
|
|
|
Ok(file) => file,
|
|
|
|
|
Err(err) => {
|
2022-02-21 20:41:32 -08:00
|
|
|
error!("Could not open '{}': {}", path, err.to_string());
|
2022-02-08 09:25:59 -08:00
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match file.read_to_string(&mut contents).await {
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
Err(err) => {
|
2022-02-21 20:41:32 -08:00
|
|
|
error!("Could not read config file: {}", err.to_string());
|
2022-02-08 09:25:59 -08:00
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let config: Config = match toml::from_str(&contents) {
|
|
|
|
|
Ok(config) => config,
|
|
|
|
|
Err(err) => {
|
2022-02-21 20:41:32 -08:00
|
|
|
error!("Could not parse config file: {}", err.to_string());
|
2022-02-08 09:25:59 -08:00
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-23 11:47:24 -08:00
|
|
|
match config.query_router.sharding_function.as_ref() {
|
|
|
|
|
"pg_bigint_hash" => (),
|
|
|
|
|
"sha1" => (),
|
|
|
|
|
_ => {
|
|
|
|
|
error!(
|
|
|
|
|
"Supported sharding functions are: 'pg_bigint_hash', 'sha1', got: '{}'",
|
|
|
|
|
config.query_router.sharding_function
|
|
|
|
|
);
|
|
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-10 09:07:10 -08:00
|
|
|
// Quick config sanity check.
|
2022-02-09 21:19:14 -08:00
|
|
|
for shard in &config.shards {
|
2022-02-10 09:07:10 -08:00
|
|
|
// We use addresses as unique identifiers,
|
|
|
|
|
// let's make sure they are unique in the config as well.
|
2022-02-09 21:19:14 -08:00
|
|
|
let mut dup_check = HashSet::new();
|
2022-02-10 09:07:10 -08:00
|
|
|
let mut primary_count = 0;
|
2022-02-09 21:19:14 -08:00
|
|
|
|
2022-02-21 20:41:32 -08:00
|
|
|
match shard.0.parse::<usize>() {
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
Err(_) => {
|
|
|
|
|
error!(
|
|
|
|
|
"Shard '{}' is not a valid number, shards must be numbered starting at 0",
|
|
|
|
|
shard.0
|
|
|
|
|
);
|
|
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-11 22:19:49 -08:00
|
|
|
if shard.1.servers.len() == 0 {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!("Shard {} has no servers configured", shard.0);
|
2022-02-11 22:19:49 -08:00
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 21:19:14 -08:00
|
|
|
for server in &shard.1.servers {
|
|
|
|
|
dup_check.insert(server);
|
2022-02-10 09:07:10 -08:00
|
|
|
|
|
|
|
|
// Check that we define only zero or one primary.
|
|
|
|
|
match server.2.as_ref() {
|
|
|
|
|
"primary" => primary_count += 1,
|
|
|
|
|
_ => (),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Check role spelling.
|
|
|
|
|
match server.2.as_ref() {
|
|
|
|
|
"primary" => (),
|
|
|
|
|
"replica" => (),
|
|
|
|
|
_ => {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!(
|
|
|
|
|
"Shard {} server role must be either 'primary' or 'replica', got: '{}'",
|
2022-02-10 09:07:10 -08:00
|
|
|
shard.0, server.2
|
|
|
|
|
);
|
|
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if primary_count > 1 {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!("Shard {} has more than on primary configured", &shard.0);
|
2022-02-10 09:07:10 -08:00
|
|
|
return Err(Error::BadConfig);
|
2022-02-09 21:19:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if dup_check.len() != shard.1.servers.len() {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!("Shard {} contains duplicate server configs", &shard.0);
|
2022-02-09 21:19:14 -08:00
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 11:19:40 -08:00
|
|
|
match config.query_router.default_role.as_ref() {
|
|
|
|
|
"any" => (),
|
|
|
|
|
"primary" => (),
|
|
|
|
|
"replica" => (),
|
|
|
|
|
other => {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!(
|
|
|
|
|
"Query router default_role must be 'primary', 'replica', or 'any', got: '{}'",
|
2022-02-11 11:19:40 -08:00
|
|
|
other
|
|
|
|
|
);
|
|
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
CONFIG.store(Arc::new(config.clone()));
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2022-02-08 09:25:59 -08:00
|
|
|
}
|
2022-02-08 17:08:17 -08:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_config() {
|
2022-02-19 13:57:35 -08:00
|
|
|
parse("pgcat.toml").await.unwrap();
|
|
|
|
|
assert_eq!(get_config().general.pool_size, 15);
|
|
|
|
|
assert_eq!(get_config().shards.len(), 3);
|
|
|
|
|
assert_eq!(get_config().shards["1"].servers[0].0, "127.0.0.1");
|
|
|
|
|
assert_eq!(get_config().shards["0"].servers[0].2, "primary");
|
|
|
|
|
assert_eq!(get_config().query_router.default_role, "any");
|
2022-02-08 17:08:17 -08:00
|
|
|
}
|
|
|
|
|
}
|