use std::sync::Arc;
use futures_util::{SinkExt, StreamExt};
use tokio::net::TcpStream;
use tokio_util::codec::Framed;
use tracing::{Instrument, debug, field, info_span, trace, warn};
use crate::clients::{ClientGuard, ClientState};
use crate::config::cluster::pool::ListenMode;
use crate::config::otel::QueryText;
use crate::connections::server::{Reply, ServerConn};
use crate::listener::ClientNotifications;
use crate::o11y;
use crate::pool::PoolManager;
use crate::proto;
use crate::proxy::intercept::{
handle_close_intercept, handle_deallocate_intercept, handle_listen_intercept,
};
use crate::proxy::prepared::{ClientPrepared, PreparedGuard, rewrite_outbound};
use crate::sql;
fn track_set_reset(stmt: &sql::Statement, server: &mut ServerConn) {
match stmt {
sql::Statement::Set {
scope: sql::SetScope::Session,
parameter,
..
} => {
server.dirty_vars.insert(parameter.clone());
}
sql::Statement::Reset {
target: sql::ResetTarget::All,
} => {
server.dirty_vars.clear();
}
sql::Statement::Reset {
target: sql::ResetTarget::Parameter(name),
} => {
server.dirty_vars.remove(name);
}
sql::Statement::Discard {
target: sql::DiscardTarget::All,
} => {
server.statements.discard_all();
server.dirty_vars.clear();
}
_ => {}
}
}
enum Release {
Idle,
ClientDone,
}
#[allow(clippy::too_many_arguments)]
pub async fn forward(
client: &mut Framed<TcpStream, proto::codec::FrontendCodec>,
pools: &Arc<PoolManager>,
client_guard: &ClientGuard,
notifications: &mut Option<ClientNotifications>,
listen_mode: ListenMode,
database: &str,
user: &str,
read_only: bool,
query_text: QueryText,
) -> anyhow::Result<()> {
let mut client_prepared = PreparedGuard {
inner: ClientPrepared::new(),
pools: Arc::clone(pools),
};
forward_loop(
client,
pools,
client_guard,
notifications,
listen_mode,
database,
user,
read_only,
&mut client_prepared.inner,
query_text,
)
.await
}
#[allow(clippy::too_many_arguments)]
async fn forward_loop(
client: &mut Framed<TcpStream, proto::codec::FrontendCodec>,
pools: &Arc<PoolManager>,
client_guard: &ClientGuard,
notifications: &mut Option<ClientNotifications>,
listen_mode: ListenMode,
database: &str,
user: &str,
read_only: bool,
client_prepared: &mut ClientPrepared,
query_text: QueryText,
) -> anyhow::Result<()> {
loop {
let (first_msg, first_stmt) = loop {
tokio::select! {
msg = client.next() => match msg.transpose()? {
Some(proto::frontend::FrontendMessage::Terminate) => {
trace!("client sent Terminate (idle)");
return Ok(());
}
Some(msg) => {
let stmt = classify(&msg);
let simple_query =
matches!(msg, proto::frontend::FrontendMessage::Query(_));
let idle = proto::types::TransactionStatus::Idle;
if simple_query
&& listen_mode == ListenMode::Multiplex
&& handle_listen_intercept(&stmt, notifications, client, idle).await?
{
continue; }
if handle_close_intercept(&msg, client_prepared, client, pools).await? {
continue; }
if simple_query
&& handle_deallocate_intercept(&stmt, client_prepared, client, pools, idle).await?
{
continue; }
break (msg, stmt);
}
None => return Ok(()),
},
Some(notif) = recv_notification(notifications) => {
client.send(notif).await?;
}
}
};
if read_only && first_stmt.is_read_write_override() {
reject_read_write(client, proto::types::TransactionStatus::Idle).await?;
continue;
}
let txn_read_only = read_only || is_read_only_begin(&first_stmt);
let txn_span = info_span!(
"proxy.transaction",
db.namespace = database,
user,
server.address = field::Empty,
otel.status_code = field::Empty,
otel.status_description = field::Empty,
);
let release = async {
let result: anyhow::Result<Release> = async {
let mut guard = pools
.checkout(client_guard, database, user, txn_read_only)
.await?;
client_guard.set_state(ClientState::InTransaction);
tracing::Span::current().record("server.address", guard.node());
guard.conn().statements.reset_pending();
let mut pinned = false;
if matches!(first_stmt, sql::Statement::Listen { .. }) {
pinned = listen_mode == ListenMode::Pin;
}
if pinned {
warn!("LISTEN detected, pinning connection to session mode");
}
let mut tracker = o11y::spans::StatementTracker::new(query_text);
tracker.on_client_msg(&first_msg, |name| {
resolve_stmt_query(name, client_prepared, pools)
});
if matches!(first_msg, proto::frontend::FrontendMessage::Query(_)) {
track_set_reset(&first_stmt, guard.conn());
}
let mut first_ends_batch = false;
if let Some(first_msg) =
rewrite_outbound(first_msg, client_prepared, guard.conn(), pools, client)
.await?
{
first_ends_batch = matches!(first_msg, proto::frontend::FrontendMessage::Sync);
guard.conn().framed.send(first_msg).await?;
}
debug!("transaction started");
let result = forward_until_idle(
client,
guard.conn(),
notifications,
listen_mode,
&mut pinned,
client_prepared,
pools,
txn_read_only,
&mut tracker,
first_ends_batch,
)
.await;
match result {
Ok(Release::Idle) => {
debug!("transaction complete, connection returned to pool");
guard.checkin();
Ok(Release::Idle)
}
Ok(Release::ClientDone) => {
guard.checkin();
Ok(Release::ClientDone)
}
Err(e) => Err(e),
}
}
.await;
client_guard.set_state(ClientState::Idle);
let span = tracing::Span::current();
match &result {
Ok(_) => {
span.record("otel.status_code", "OK");
}
Err(e) => {
span.record("otel.status_code", "ERROR");
span.record("otel.status_description", e.to_string().as_str());
}
}
result
}
.instrument(txn_span)
.await;
match release {
Ok(Release::Idle) => {}
Ok(Release::ClientDone) => return Ok(()),
Err(e) => return Err(e),
}
}
}
#[allow(clippy::too_many_arguments)]
async fn forward_until_idle(
client: &mut Framed<TcpStream, proto::codec::FrontendCodec>,
server: &mut ServerConn,
notifications: &mut Option<ClientNotifications>,
listen_mode: ListenMode,
pinned: &mut bool,
client_prepared: &mut ClientPrepared,
pools: &Arc<PoolManager>,
txn_read_only: bool,
tracker: &mut o11y::spans::StatementTracker,
awaiting_ready: bool,
) -> anyhow::Result<Release> {
let mut awaiting_ready = awaiting_ready;
let mut last_rfq: Option<proto::types::TransactionStatus> = None;
loop {
tokio::select! {
msg = client.next(), if !awaiting_ready => match msg.transpose()? {
Some(proto::frontend::FrontendMessage::Terminate) => {
trace!("client sent Terminate (in transaction)");
return Ok(Release::ClientDone);
}
Some(msg) => {
let stmt = classify(&msg);
let simple_query = matches!(msg, proto::frontend::FrontendMessage::Query(_));
if simple_query
&& listen_mode == ListenMode::Multiplex
&& handle_listen_intercept(
&stmt,
notifications,
client,
last_rfq.unwrap_or(proto::types::TransactionStatus::InTransaction),
)
.await?
{
continue;
}
if txn_read_only && stmt.is_read_write_override() {
reject_read_write(client, proto::types::TransactionStatus::InTransaction).await?;
continue;
}
if handle_close_intercept(&msg, client_prepared, client, pools).await? {
continue;
}
if simple_query
&& handle_deallocate_intercept(
&stmt,
client_prepared,
client,
pools,
last_rfq.unwrap_or(proto::types::TransactionStatus::InTransaction),
)
.await?
{
continue;
}
if listen_mode == ListenMode::Pin {
match &stmt {
sql::Statement::Listen { .. } if !*pinned => {
warn!("LISTEN detected, pinning connection to session mode");
*pinned = true;
}
sql::Statement::Unlisten {
target: sql::UnlistenTarget::Star,
} if *pinned => {
debug!("UNLISTEN * detected, unpinning connection");
*pinned = false;
}
_ => {}
}
}
tracker.on_client_msg(&msg, |name| resolve_stmt_query(name, client_prepared, pools));
if simple_query {
track_set_reset(&stmt, server);
}
if let Some(msg) = rewrite_outbound(msg, client_prepared, server, pools, client).await? {
let in_explicit_txn = last_rfq
.is_some_and(|s| s != proto::types::TransactionStatus::Idle);
if matches!(msg, proto::frontend::FrontendMessage::Sync)
&& !in_explicit_txn
{
awaiting_ready = true;
}
trace!(?msg, "client -> server");
server.framed.send(msg).await?;
}
}
None => return Ok(Release::ClientDone),
},
msg = server.framed.next() => match msg.transpose()? {
Some(proto::backend::BackendMessage::ReadyForQuery(status)) => {
awaiting_ready = false;
last_rfq = Some(status);
server.last_tx_status = status;
tracker.drain();
client.send(proto::backend::BackendMessage::ReadyForQuery(status)).await?;
if status == proto::types::TransactionStatus::Idle && !*pinned {
return Ok(Release::Idle);
}
}
Some(proto::backend::BackendMessage::CommandComplete(ref tag)) => {
tracker.on_command_complete(tag);
client.send(proto::backend::BackendMessage::CommandComplete(tag.clone())).await?;
}
Some(proto::backend::BackendMessage::EmptyQueryResponse) => {
tracker.on_command_complete("");
client.send(proto::backend::BackendMessage::EmptyQueryResponse).await?;
}
Some(proto::backend::BackendMessage::ErrorResponse(ref err)) => {
tracker.on_error(err.message().unwrap_or("unknown"));
server.statements.roll_back_after_error();
client.send(proto::backend::BackendMessage::ErrorResponse(err.clone())).await?;
}
Some(proto::backend::BackendMessage::ParseComplete) => {
match server.statements.next_parse_reply() {
Reply::Suppress => trace!("suppressing injected ParseComplete"),
Reply::Forward => {
client.send(proto::backend::BackendMessage::ParseComplete).await?;
}
}
}
Some(proto::backend::BackendMessage::CloseComplete) => {
match server.statements.next_close_reply() {
Reply::Suppress => trace!("suppressing injected CloseComplete"),
Reply::Forward => {
client.send(proto::backend::BackendMessage::CloseComplete).await?;
}
}
}
Some(msg) => {
trace!(?msg, "server -> client");
client.send(msg).await?;
}
None => {
tracker.drain();
crate::messages::error::send_fatal(
client,
"08006",
"upstream server closed unexpectedly",
)
.await;
anyhow::bail!("upstream server closed unexpectedly");
}
},
Some(notif) = recv_notification(notifications) => {
client.send(notif).await?;
},
}
}
}
async fn recv_notification(
notifs: &mut Option<ClientNotifications>,
) -> Option<proto::backend::BackendMessage> {
match notifs {
Some(n) => n.recv().await,
None => std::future::pending().await,
}
}
fn resolve_stmt_query(
client_name: &str,
client_prepared: &ClientPrepared,
pools: &PoolManager,
) -> Option<String> {
let canon = client_prepared.resolve(client_name)?;
let store = pools.stmt_store.lock();
store.get(canon).map(|parse| parse.query.clone())
}
fn classify(msg: &proto::frontend::FrontendMessage) -> sql::Statement {
match msg {
proto::frontend::FrontendMessage::Query(q) => sql::parse(q),
proto::frontend::FrontendMessage::Parse(p) => sql::parse(&p.query),
_ => sql::Statement::Other,
}
}
fn is_read_only_begin(stmt: &sql::Statement) -> bool {
matches!(
stmt,
sql::Statement::Begin {
options: sql::TransactionOptions {
read_only: Some(true),
},
}
)
}
async fn reject_read_write(
client: &mut Framed<TcpStream, proto::codec::FrontendCodec>,
status: proto::types::TransactionStatus,
) -> anyhow::Result<()> {
crate::messages::error::send_error(
client,
"25006",
"cannot switch to read-write mode: connection is routed to a read replica",
status,
)
.await
}