Files
pgcat/src/main.rs

275 lines
7.7 KiB
Rust
Raw Normal View History

// Copyright (c) 2022 Lev Kokotov <hi@levthe.dev>
2022-02-08 14:59:10 -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
// 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
extern crate arc_swap;
extern crate async_trait;
extern crate bb8;
2022-02-03 13:35:40 -08:00
extern crate bytes;
extern crate env_logger;
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;
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
use log::{debug, error, info};
use parking_lot::Mutex;
use tokio::net::TcpListener;
use tokio::{
signal,
signal::unix::{signal as unix_signal, SignalKind},
sync::mpsc,
};
2022-02-03 13:35:40 -08:00
2022-02-04 09:28:52 -08:00
use std::collections::HashMap;
use std::sync::Arc;
2022-02-04 09:28:52 -08:00
mod admin;
2022-02-03 15:17:04 -08:00
mod client;
2022-02-05 10:02:13 -08:00
mod config;
mod constants;
2022-02-03 13:35:40 -08:00
mod errors;
mod messages;
2022-02-03 16:25:05 -08:00
mod pool;
mod query_router;
mod scram;
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
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() {
env_logger::init();
info!("Welcome to PgCat! Meow.");
2022-02-03 13:35:40 -08:00
if !query_router::QueryRouter::setup() {
error!("Could not setup query router");
return;
}
2022-02-10 17:05:20 -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 {
Ok(_) => (),
2022-02-08 09:25:59 -08:00
Err(err) => {
error!("Config parse error: {:?}", err);
2022-02-08 09:25:59 -08:00
return;
}
};
let config = get_config();
2022-02-08 09:25:59 -08:00
let addr = format!("{}:{}", config.general.host, config.general.port);
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) => {
error!("Listener socket error: {:?}", err);
2022-02-03 13:35:40 -08:00
return;
}
};
info!("Running on {}", addr);
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
// Statistics reporting.
2022-02-14 10:00:55 -08:00
let (tx, rx) = mpsc::channel(100);
REPORTER.store(Arc::new(Reporter::new(tx.clone())));
// Connection pool that allows to query all shards and replicas.
match ConnectionPool::from_config(client_server_map.clone()).await {
Ok(_) => (),
Err(err) => {
error!("Pool error: {:?}", err);
return;
}
};
let pool = get_pool();
// Statistics collector task.
let collector_tx = tx.clone();
// Save these for reloading
let reload_client_server_map = client_server_map.clone();
let autoreload_client_server_map = client_server_map.clone();
let addresses = pool.databases();
2022-02-14 10:00:55 -08:00
tokio::task::spawn(async move {
let mut stats_collector = Collector::new(rx, collector_tx);
stats_collector.collect(addresses).await;
2022-02-14 10:00:55 -08:00
});
info!("Waiting for clients");
2022-02-03 16:25:05 -08:00
drop(pool);
// Client connection loop.
tokio::task::spawn(async move {
loop {
let client_server_map = client_server_map.clone();
2022-02-03 13:35:40 -08:00
let (socket, addr) = match listener.accept().await {
Ok((socket, addr)) => (socket, addr),
2022-02-03 13:35:40 -08:00
Err(err) => {
error!("{:?}", err);
continue;
2022-02-03 13:35:40 -08:00
}
};
// Handle client.
tokio::task::spawn(async move {
let start = chrono::offset::Utc::now().naive_utc();
match client::Client::startup(socket, client_server_map).await {
Ok(mut client) => {
info!("Client {:?} connected", addr);
match client.handle().await {
Ok(()) => {
let duration = chrono::offset::Utc::now().naive_utc() - start;
info!(
"Client {:?} disconnected, session duration: {}",
addr,
format_duration(&duration)
);
}
Err(err) => {
error!("Client disconnected with error: {:?}", err);
client.release();
}
}
}
Err(err) => {
debug!("Client failed to login: {:?}", err);
}
};
});
}
});
// Reload config:
// kill -SIGHUP $(pgrep pgcat)
tokio::task::spawn(async move {
let mut stream = unix_signal(SignalKind::hangup()).unwrap();
loop {
stream.recv().await;
info!("Reloading config");
match reload_config(reload_client_server_map.clone()).await {
Ok(_) => (),
Err(_) => continue,
};
get_config().show();
}
});
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(_) => (),
};
}
});
}
// Exit on Ctrl-C (SIGINT) and SIGTERM.
let mut term_signal = unix_signal(SignalKind::terminate()).unwrap();
2022-02-14 10:00:55 -08:00
tokio::select! {
_ = signal::ctrl_c() => (),
_ = term_signal.recv() => (),
};
info!("Shutting down...");
2022-02-03 13:35:40 -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)
}