2022-06-24 14:52:38 -07:00
|
|
|
// Copyright (c) 2022 Lev Kokotov <hi@levthe.dev>
|
2022-02-08 14:59:10 -08:00
|
|
|
|
2022-02-20 13:49:30 -08:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
// the following conditions:
|
2022-02-08 14:59:10 -08:00
|
|
|
|
2022-02-20 13:49:30 -08:00
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2022-02-08 14:59:10 -08:00
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
extern crate arc_swap;
|
2022-02-03 17:06:19 -08:00
|
|
|
extern crate async_trait;
|
|
|
|
|
extern crate bb8;
|
2022-02-03 13:35:40 -08:00
|
|
|
extern crate bytes;
|
2022-02-20 22:47:08 -08:00
|
|
|
extern crate env_logger;
|
2022-02-18 07:10:18 -08:00
|
|
|
extern crate log;
|
2022-02-03 15:17:04 -08:00
|
|
|
extern crate md5;
|
2022-02-10 17:05:20 -08:00
|
|
|
extern crate num_cpus;
|
|
|
|
|
extern crate once_cell;
|
2022-02-08 09:25:59 -08:00
|
|
|
extern crate serde;
|
|
|
|
|
extern crate serde_derive;
|
2022-02-18 07:10:18 -08:00
|
|
|
extern crate sqlparser;
|
2022-02-03 13:35:40 -08:00
|
|
|
extern crate tokio;
|
2022-02-08 09:25:59 -08:00
|
|
|
extern crate toml;
|
2022-02-03 13:35:40 -08:00
|
|
|
|
2022-06-24 14:52:38 -07:00
|
|
|
use log::{debug, error, info};
|
2022-02-24 08:44:41 -08:00
|
|
|
use parking_lot::Mutex;
|
2022-02-03 17:06:19 -08:00
|
|
|
use tokio::net::TcpListener;
|
2022-02-19 13:57:35 -08:00
|
|
|
use tokio::{
|
|
|
|
|
signal,
|
|
|
|
|
signal::unix::{signal as unix_signal, SignalKind},
|
2022-02-20 22:47:08 -08:00
|
|
|
sync::mpsc,
|
2022-02-19 13:57:35 -08:00
|
|
|
};
|
2022-02-03 13:35:40 -08:00
|
|
|
|
2022-02-04 09:28:52 -08:00
|
|
|
use std::collections::HashMap;
|
2022-02-24 08:44:41 -08:00
|
|
|
use std::sync::Arc;
|
2022-02-04 09:28:52 -08:00
|
|
|
|
2022-02-25 18:20:15 -08:00
|
|
|
mod admin;
|
2022-02-03 15:17:04 -08:00
|
|
|
mod client;
|
2022-02-05 10:02:13 -08:00
|
|
|
mod config;
|
2022-02-15 22:45:45 -08:00
|
|
|
mod constants;
|
2022-02-03 13:35:40 -08:00
|
|
|
mod errors;
|
|
|
|
|
mod messages;
|
2022-02-03 16:25:05 -08:00
|
|
|
mod pool;
|
2022-02-16 22:52:11 -08:00
|
|
|
mod query_router;
|
2022-06-18 18:36:00 -07:00
|
|
|
mod scram;
|
2022-02-03 17:06:19 -08:00
|
|
|
mod server;
|
2022-02-05 19:43:48 -08:00
|
|
|
mod sharding;
|
2022-02-14 10:00:55 -08:00
|
|
|
mod stats;
|
2022-02-03 13:35:40 -08:00
|
|
|
|
2022-06-24 14:52:38 -07:00
|
|
|
use config::{get_config, reload_config};
|
|
|
|
|
use pool::{get_pool, ClientServerMap, ConnectionPool};
|
|
|
|
|
use stats::{Collector, Reporter, REPORTER};
|
2022-02-05 13:15:53 -08:00
|
|
|
|
2022-02-10 13:48:56 -08:00
|
|
|
#[tokio::main(worker_threads = 4)]
|
2022-02-03 13:35:40 -08:00
|
|
|
async fn main() {
|
2022-02-20 22:47:08 -08:00
|
|
|
env_logger::init();
|
|
|
|
|
info!("Welcome to PgCat! Meow.");
|
2022-02-03 13:35:40 -08:00
|
|
|
|
2022-02-16 22:52:11 -08:00
|
|
|
if !query_router::QueryRouter::setup() {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!("Could not setup query router");
|
2022-02-16 22:52:11 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2022-02-10 17:05:20 -08:00
|
|
|
|
2022-02-21 20:41:32 -08:00
|
|
|
let args = std::env::args().collect::<Vec<String>>();
|
|
|
|
|
|
|
|
|
|
let config_file = if args.len() == 2 {
|
|
|
|
|
args[1].to_string()
|
|
|
|
|
} else {
|
|
|
|
|
String::from("pgcat.toml")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match config::parse(&config_file).await {
|
2022-02-19 13:57:35 -08:00
|
|
|
Ok(_) => (),
|
2022-02-08 09:25:59 -08:00
|
|
|
Err(err) => {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!("Config parse error: {:?}", err);
|
2022-02-08 09:25:59 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
let config = get_config();
|
2022-02-08 09:25:59 -08:00
|
|
|
let addr = format!("{}:{}", config.general.host, config.general.port);
|
2022-03-10 01:33:29 -08:00
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
let listener = match TcpListener::bind(&addr).await {
|
2022-02-03 13:35:40 -08:00
|
|
|
Ok(sock) => sock,
|
|
|
|
|
Err(err) => {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!("Listener socket error: {:?}", err);
|
2022-02-03 13:35:40 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-20 22:47:08 -08:00
|
|
|
info!("Running on {}", addr);
|
2022-03-10 01:33:29 -08:00
|
|
|
|
2022-02-19 13:57:35 -08:00
|
|
|
config.show();
|
2022-02-05 10:02:13 -08:00
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
// Tracks which client is connected to which server for query cancellation.
|
2022-02-04 16:01:35 -08:00
|
|
|
let client_server_map: ClientServerMap = Arc::new(Mutex::new(HashMap::new()));
|
2022-02-05 10:02:13 -08:00
|
|
|
|
2022-03-10 01:33:29 -08:00
|
|
|
// Statistics reporting.
|
2022-02-14 10:00:55 -08:00
|
|
|
let (tx, rx) = mpsc::channel(100);
|
2022-06-24 14:52:38 -07:00
|
|
|
REPORTER.store(Arc::new(Reporter::new(tx.clone())));
|
2022-03-04 17:04:27 -08:00
|
|
|
|
2022-03-10 01:33:29 -08:00
|
|
|
// Connection pool that allows to query all shards and replicas.
|
2022-06-24 14:52:38 -07:00
|
|
|
match ConnectionPool::from_config(client_server_map.clone()).await {
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
error!("Pool error: {:?}", err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let pool = get_pool();
|
2022-03-04 17:04:27 -08:00
|
|
|
|
2022-03-10 01:33:29 -08:00
|
|
|
// Statistics collector task.
|
2022-02-22 18:10:30 -08:00
|
|
|
let collector_tx = tx.clone();
|
2022-06-24 14:52:38 -07:00
|
|
|
|
|
|
|
|
// Save these for reloading
|
|
|
|
|
let reload_client_server_map = client_server_map.clone();
|
2022-06-25 11:46:20 -07:00
|
|
|
let autoreload_client_server_map = client_server_map.clone();
|
2022-06-24 14:52:38 -07:00
|
|
|
|
2022-03-04 17:04:27 -08:00
|
|
|
let addresses = pool.databases();
|
2022-02-14 10:00:55 -08:00
|
|
|
tokio::task::spawn(async move {
|
2022-02-22 18:10:30 -08:00
|
|
|
let mut stats_collector = Collector::new(rx, collector_tx);
|
2022-03-04 17:04:27 -08:00
|
|
|
stats_collector.collect(addresses).await;
|
2022-02-14 10:00:55 -08:00
|
|
|
});
|
|
|
|
|
|
2022-02-20 22:47:08 -08:00
|
|
|
info!("Waiting for clients");
|
2022-02-03 16:25:05 -08:00
|
|
|
|
2022-06-24 14:52:38 -07:00
|
|
|
drop(pool);
|
|
|
|
|
|
2022-03-10 01:33:29 -08:00
|
|
|
// Client connection loop.
|
2022-02-12 10:16:05 -08:00
|
|
|
tokio::task::spawn(async move {
|
|
|
|
|
loop {
|
|
|
|
|
let client_server_map = client_server_map.clone();
|
2022-02-03 13:35:40 -08:00
|
|
|
|
2022-02-12 10:16:05 -08:00
|
|
|
let (socket, addr) = match listener.accept().await {
|
|
|
|
|
Ok((socket, addr)) => (socket, addr),
|
2022-02-03 13:35:40 -08:00
|
|
|
Err(err) => {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!("{:?}", err);
|
2022-02-12 10:16:05 -08:00
|
|
|
continue;
|
2022-02-03 13:35:40 -08:00
|
|
|
}
|
|
|
|
|
};
|
2022-02-12 10:16:05 -08:00
|
|
|
|
2022-03-10 01:33:29 -08:00
|
|
|
// Handle client.
|
2022-02-12 10:16:05 -08:00
|
|
|
tokio::task::spawn(async move {
|
|
|
|
|
let start = chrono::offset::Utc::now().naive_utc();
|
2022-06-24 14:52:38 -07:00
|
|
|
match client::Client::startup(socket, client_server_map).await {
|
2022-02-12 10:16:05 -08:00
|
|
|
Ok(mut client) => {
|
2022-02-20 22:47:08 -08:00
|
|
|
info!("Client {:?} connected", addr);
|
2022-06-24 14:52:38 -07:00
|
|
|
|
|
|
|
|
match client.handle().await {
|
2022-02-12 10:16:05 -08:00
|
|
|
Ok(()) => {
|
|
|
|
|
let duration = chrono::offset::Utc::now().naive_utc() - start;
|
|
|
|
|
|
2022-02-20 22:47:08 -08:00
|
|
|
info!(
|
|
|
|
|
"Client {:?} disconnected, session duration: {}",
|
2022-02-12 10:16:05 -08:00
|
|
|
addr,
|
|
|
|
|
format_duration(&duration)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Err(err) => {
|
2022-02-20 22:47:08 -08:00
|
|
|
error!("Client disconnected with error: {:?}", err);
|
2022-02-12 10:16:05 -08:00
|
|
|
client.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Err(err) => {
|
2022-06-24 14:52:38 -07:00
|
|
|
debug!("Client failed to login: {:?}", err);
|
2022-02-12 10:16:05 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-10 01:33:29 -08:00
|
|
|
// Reload config:
|
2022-02-19 13:57:35 -08:00
|
|
|
// kill -SIGHUP $(pgrep pgcat)
|
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
|
let mut stream = unix_signal(SignalKind::hangup()).unwrap();
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
stream.recv().await;
|
2022-06-24 14:52:38 -07:00
|
|
|
|
2022-02-20 22:47:08 -08:00
|
|
|
info!("Reloading config");
|
2022-06-24 14:52:38 -07:00
|
|
|
|
|
|
|
|
match reload_config(reload_client_server_map.clone()).await {
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
Err(_) => continue,
|
2022-02-19 13:57:35 -08:00
|
|
|
};
|
2022-06-24 14:52:38 -07:00
|
|
|
|
|
|
|
|
get_config().show();
|
2022-02-19 13:57:35 -08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-25 11:46:20 -07:00
|
|
|
if config.general.autoreload {
|
|
|
|
|
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(15_000));
|
|
|
|
|
|
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
|
info!("Config autoreloader started");
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
interval.tick().await;
|
|
|
|
|
match reload_config(autoreload_client_server_map.clone()).await {
|
|
|
|
|
Ok(changed) => {
|
|
|
|
|
if changed {
|
|
|
|
|
get_config().show()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(_) => (),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 01:33:29 -08:00
|
|
|
// Exit on Ctrl-C (SIGINT) and SIGTERM.
|
2022-03-08 17:18:48 -08:00
|
|
|
let mut term_signal = unix_signal(SignalKind::terminate()).unwrap();
|
2022-02-14 10:00:55 -08:00
|
|
|
|
2022-03-08 17:18:48 -08:00
|
|
|
tokio::select! {
|
|
|
|
|
_ = signal::ctrl_c() => (),
|
|
|
|
|
_ = term_signal.recv() => (),
|
2022-02-12 10:16:05 -08:00
|
|
|
};
|
2022-03-08 17:18:48 -08:00
|
|
|
|
|
|
|
|
info!("Shutting down...");
|
2022-02-03 13:35:40 -08:00
|
|
|
}
|
2022-02-12 09:24:24 -08:00
|
|
|
|
|
|
|
|
/// Format chrono::Duration to be more human-friendly.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `duration` - A duration of time
|
|
|
|
|
fn format_duration(duration: &chrono::Duration) -> String {
|
|
|
|
|
let seconds = {
|
|
|
|
|
let seconds = duration.num_seconds() % 60;
|
|
|
|
|
if seconds < 10 {
|
|
|
|
|
format!("0{}", seconds)
|
|
|
|
|
} else {
|
|
|
|
|
format!("{}", seconds)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let minutes = {
|
|
|
|
|
let minutes = duration.num_minutes() % 60;
|
|
|
|
|
if minutes < 10 {
|
|
|
|
|
format!("0{}", minutes)
|
|
|
|
|
} else {
|
|
|
|
|
format!("{}", minutes)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let hours = {
|
|
|
|
|
let hours = duration.num_hours() % 24;
|
|
|
|
|
if hours < 10 {
|
|
|
|
|
format!("0{}", hours)
|
|
|
|
|
} else {
|
|
|
|
|
format!("{}", hours)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let days = duration.num_days().to_string();
|
|
|
|
|
|
|
|
|
|
format!("{}d {}:{}:{}", days, hours, minutes, seconds)
|
|
|
|
|
}
|