repo
stringlengths
6
65
file_url
stringlengths
81
311
file_path
stringlengths
6
227
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:31:58
2026-01-04 20:25:31
truncated
bool
2 classes
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/tls/mod.rs
proxy/src/tls/mod.rs
pub mod client_config; pub mod postgres_rustls; pub mod server_config; use anyhow::Context; use base64::Engine as _; use base64::prelude::BASE64_STANDARD; use rustls::pki_types::CertificateDer; use sha2::{Digest, Sha256}; use tracing::{error, info}; use x509_cert::der::{Reader, SliceReader, oid}; /// <https://github.com/postgres/postgres/blob/ca481d3c9ab7bf69ff0c8d71ad3951d407f6a33c/src/include/libpq/pqcomm.h#L159> pub const PG_ALPN_PROTOCOL: &[u8] = b"postgresql"; /// Channel binding parameter /// /// <https://www.rfc-editor.org/rfc/rfc5929#section-4> /// Description: The hash of the TLS server's certificate as it /// appears, octet for octet, in the server's Certificate message. Note /// that the Certificate message contains a certificate_list, in which /// the first element is the server's certificate. /// /// The hash function is to be selected as follows: /// /// * if the certificate's signatureAlgorithm uses a single hash /// function, and that hash function is either MD5 or SHA-1, then use SHA-256; /// /// * if the certificate's signatureAlgorithm uses a single hash /// function and that hash function neither MD5 nor SHA-1, then use /// the hash function associated with the certificate's /// signatureAlgorithm; /// /// * if the certificate's signatureAlgorithm uses no hash functions or /// uses multiple hash functions, then this channel binding type's /// channel bindings are undefined at this time (updates to is channel /// binding type may occur to address this issue if it ever arises). #[derive(Debug, Clone, Copy)] pub enum TlsServerEndPoint { Sha256([u8; 32]), Undefined, } impl TlsServerEndPoint { pub fn new(cert: &CertificateDer<'_>) -> anyhow::Result<Self> { const SHA256_OIDS: &[oid::ObjectIdentifier] = &[ // I'm explicitly not adding MD5 or SHA1 here... They're bad. oid::db::rfc5912::ECDSA_WITH_SHA_256, oid::db::rfc5912::SHA_256_WITH_RSA_ENCRYPTION, ]; let certificate = SliceReader::new(cert) .context("Failed to parse cerficiate")? .decode::<x509_cert::Certificate>() .context("Failed to parse cerficiate")?; let subject = certificate.tbs_certificate.subject; info!(%subject, "parsing TLS certificate"); let oid = certificate.signature_algorithm.oid; if SHA256_OIDS.contains(&oid) { let tls_server_end_point: [u8; 32] = Sha256::new().chain_update(cert).finalize().into(); info!(%subject, tls_server_end_point = %BASE64_STANDARD.encode(tls_server_end_point), "determined channel binding"); Ok(Self::Sha256(tls_server_end_point)) } else { error!(%subject, "unknown channel binding"); Ok(Self::Undefined) } } pub fn supported(&self) -> bool { !matches!(self, TlsServerEndPoint::Undefined) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/tls/postgres_rustls.rs
proxy/src/tls/postgres_rustls.rs
use std::convert::TryFrom; use std::sync::Arc; use postgres_client::tls::MakeTlsConnect; use rustls::pki_types::{InvalidDnsNameError, ServerName}; use tokio::io::{AsyncRead, AsyncWrite}; use crate::config::ComputeConfig; mod private { use std::future::Future; use std::io; use std::pin::Pin; use std::task::{Context, Poll}; use postgres_client::tls::{ChannelBinding, TlsConnect}; use rustls::pki_types::ServerName; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tokio_rustls::TlsConnector; use tokio_rustls::client::TlsStream; use crate::tls::TlsServerEndPoint; pub struct TlsConnectFuture<S> { inner: tokio_rustls::Connect<S>, } impl<S> Future for TlsConnectFuture<S> where S: AsyncRead + AsyncWrite + Unpin, { type Output = io::Result<RustlsStream<S>>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { Pin::new(&mut self.inner) .poll(cx) .map_ok(|s| RustlsStream(Box::new(s))) } } pub struct RustlsConnect(pub RustlsConnectData); pub struct RustlsConnectData { pub hostname: ServerName<'static>, pub connector: TlsConnector, } impl<S> TlsConnect<S> for RustlsConnect where S: AsyncRead + AsyncWrite + Unpin + Send + 'static, { type Stream = RustlsStream<S>; type Error = io::Error; type Future = TlsConnectFuture<S>; fn connect(self, stream: S) -> Self::Future { TlsConnectFuture { inner: self.0.connector.connect(self.0.hostname, stream), } } } pub struct RustlsStream<S>(Box<TlsStream<S>>); impl<S> postgres_client::tls::TlsStream for RustlsStream<S> where S: AsyncRead + AsyncWrite + Unpin, { fn channel_binding(&self) -> ChannelBinding { let (_, session) = self.0.get_ref(); match session.peer_certificates() { Some([cert, ..]) => TlsServerEndPoint::new(cert) .ok() .and_then(|cb| match cb { TlsServerEndPoint::Sha256(hash) => Some(hash), TlsServerEndPoint::Undefined => None, }) .map_or_else(ChannelBinding::none, |hash| { ChannelBinding::tls_server_end_point(hash.to_vec()) }), _ => ChannelBinding::none(), } } } impl<S> AsyncRead for RustlsStream<S> where S: AsyncRead + AsyncWrite + Unpin, { fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<tokio::io::Result<()>> { Pin::new(&mut self.0).poll_read(cx, buf) } } impl<S> AsyncWrite for RustlsStream<S> where S: AsyncRead + AsyncWrite + Unpin, { fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<tokio::io::Result<usize>> { Pin::new(&mut self.0).poll_write(cx, buf) } fn poll_flush( mut self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<tokio::io::Result<()>> { Pin::new(&mut self.0).poll_flush(cx) } fn poll_shutdown( mut self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<tokio::io::Result<()>> { Pin::new(&mut self.0).poll_shutdown(cx) } } } impl<S> MakeTlsConnect<S> for ComputeConfig where S: AsyncRead + AsyncWrite + Unpin + Send + 'static, { type Stream = private::RustlsStream<S>; type TlsConnect = private::RustlsConnect; type Error = InvalidDnsNameError; fn make_tls_connect(&self, hostname: &str) -> Result<Self::TlsConnect, Self::Error> { make_tls_connect(&self.tls, hostname) } } pub fn make_tls_connect( tls: &Arc<rustls::ClientConfig>, hostname: &str, ) -> Result<private::RustlsConnect, InvalidDnsNameError> { ServerName::try_from(hostname).map(|dns_name| { private::RustlsConnect(private::RustlsConnectData { hostname: dns_name.to_owned(), connector: tls.clone().into(), }) }) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/tls/server_config.rs
proxy/src/tls/server_config.rs
use std::collections::{HashMap, HashSet}; use std::path::Path; use std::sync::Arc; use anyhow::{Context, bail}; use itertools::Itertools; use rustls::crypto::ring::{self, sign}; use rustls::pki_types::{CertificateDer, PrivateKeyDer}; use rustls::sign::CertifiedKey; use x509_cert::der::{Reader, SliceReader}; use super::{PG_ALPN_PROTOCOL, TlsServerEndPoint}; pub struct TlsConfig { // unfortunate split since we cannot change the ALPN on demand. // <https://github.com/rustls/rustls/issues/2260> pub http_config: Arc<rustls::ServerConfig>, pub pg_config: Arc<rustls::ServerConfig>, pub common_names: HashSet<String>, pub cert_resolver: Arc<CertResolver>, } /// Configure TLS for the main endpoint. pub fn configure_tls( key_path: &Path, cert_path: &Path, certs_dir: Option<&Path>, allow_tls_keylogfile: bool, ) -> anyhow::Result<TlsConfig> { // add default certificate let mut cert_resolver = CertResolver::parse_new(key_path, cert_path)?; // add extra certificates if let Some(certs_dir) = certs_dir { for entry in std::fs::read_dir(certs_dir)? { let entry = entry?; let path = entry.path(); if path.is_dir() { // file names aligned with default cert-manager names let key_path = path.join("tls.key"); let cert_path = path.join("tls.crt"); if key_path.exists() && cert_path.exists() { cert_resolver.add_cert_path(&key_path, &cert_path)?; } } } } let common_names = cert_resolver.get_common_names(); let cert_resolver = Arc::new(cert_resolver); // allow TLS 1.2 to be compatible with older client libraries let mut config = rustls::ServerConfig::builder_with_provider(Arc::new(ring::default_provider())) .with_protocol_versions(&[&rustls::version::TLS13, &rustls::version::TLS12]) .context("ring should support TLS1.2 and TLS1.3")? .with_no_client_auth() .with_cert_resolver(cert_resolver.clone()); config.alpn_protocols = vec![PG_ALPN_PROTOCOL.to_vec()]; if allow_tls_keylogfile { // KeyLogFile will check for the SSLKEYLOGFILE environment variable. config.key_log = Arc::new(rustls::KeyLogFile::new()); } let mut http_config = config.clone(); let mut pg_config = config; http_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()]; pg_config.alpn_protocols = vec![b"postgresql".to_vec()]; Ok(TlsConfig { http_config: Arc::new(http_config), pg_config: Arc::new(pg_config), common_names, cert_resolver, }) } #[derive(Debug)] pub struct CertResolver { certs: HashMap<String, (Arc<rustls::sign::CertifiedKey>, TlsServerEndPoint)>, default: (Arc<rustls::sign::CertifiedKey>, TlsServerEndPoint), } impl CertResolver { fn parse_new(key_path: &Path, cert_path: &Path) -> anyhow::Result<Self> { let (priv_key, cert_chain) = parse_key_cert(key_path, cert_path)?; Self::new(priv_key, cert_chain) } pub fn new( priv_key: PrivateKeyDer<'static>, cert_chain: Vec<CertificateDer<'static>>, ) -> anyhow::Result<Self> { let (common_name, cert, tls_server_end_point) = process_key_cert(priv_key, cert_chain)?; let mut certs = HashMap::new(); let default = (cert.clone(), tls_server_end_point); certs.insert(common_name, (cert, tls_server_end_point)); Ok(Self { certs, default }) } fn add_cert_path(&mut self, key_path: &Path, cert_path: &Path) -> anyhow::Result<()> { let (priv_key, cert_chain) = parse_key_cert(key_path, cert_path)?; self.add_cert(priv_key, cert_chain) } fn add_cert( &mut self, priv_key: PrivateKeyDer<'static>, cert_chain: Vec<CertificateDer<'static>>, ) -> anyhow::Result<()> { let (common_name, cert, tls_server_end_point) = process_key_cert(priv_key, cert_chain)?; self.certs.insert(common_name, (cert, tls_server_end_point)); Ok(()) } pub fn get_common_names(&self) -> HashSet<String> { self.certs.keys().cloned().collect() } } fn parse_key_cert( key_path: &Path, cert_path: &Path, ) -> anyhow::Result<(PrivateKeyDer<'static>, Vec<CertificateDer<'static>>)> { let priv_key = { let key_bytes = std::fs::read(key_path) .with_context(|| format!("Failed to read TLS keys at '{}'", key_path.display()))?; rustls_pemfile::private_key(&mut &key_bytes[..]) .with_context(|| format!("Failed to parse TLS keys at '{}'", key_path.display()))? .with_context(|| format!("Failed to parse TLS keys at '{}'", key_path.display()))? }; let cert_chain_bytes = std::fs::read(cert_path).context(format!( "Failed to read TLS cert file at '{}.'", cert_path.display() ))?; let cert_chain = { rustls_pemfile::certs(&mut &cert_chain_bytes[..]) .try_collect() .with_context(|| { format!( "Failed to read TLS certificate chain from bytes from file at '{}'.", cert_path.display() ) })? }; Ok((priv_key, cert_chain)) } fn process_key_cert( priv_key: PrivateKeyDer<'static>, cert_chain: Vec<CertificateDer<'static>>, ) -> anyhow::Result<(String, Arc<CertifiedKey>, TlsServerEndPoint)> { let key = sign::any_supported_type(&priv_key).context("invalid private key")?; let first_cert = &cert_chain[0]; let tls_server_end_point = TlsServerEndPoint::new(first_cert)?; let certificate = SliceReader::new(first_cert) .context("Failed to parse cerficiate")? .decode::<x509_cert::Certificate>() .context("Failed to parse cerficiate")?; let common_name = certificate.tbs_certificate.subject.to_string(); // We need to get the canonical name for this certificate so we can match them against any domain names // seen within the proxy codebase. // // In scram-proxy we use wildcard certificates only, with the database endpoint as the wildcard subdomain, taken from SNI. // We need to remove the wildcard prefix for the purposes of certificate selection. // // auth-broker does not use SNI and instead uses the Neon-Connection-String header. // Auth broker has the subdomain `apiauth` we need to remove for the purposes of validating the Neon-Connection-String. // // Console Redirect proxy does not use any wildcard domains and does not need any certificate selection or conn string // validation, so let's we can continue with any common-name let common_name = if let Some(s) = common_name.strip_prefix("CN=*.") { s.to_string() } else if let Some(s) = common_name.strip_prefix("CN=apiauth.") { s.to_string() } else if let Some(s) = common_name.strip_prefix("CN=") { s.to_string() } else { bail!("Failed to parse common name from certificate") }; let cert = Arc::new(rustls::sign::CertifiedKey::new(cert_chain, key)); Ok((common_name, cert, tls_server_end_point)) } impl rustls::server::ResolvesServerCert for CertResolver { fn resolve( &self, client_hello: rustls::server::ClientHello<'_>, ) -> Option<Arc<rustls::sign::CertifiedKey>> { Some(self.resolve(client_hello.server_name()).0) } } impl CertResolver { pub fn resolve( &self, server_name: Option<&str>, ) -> (Arc<rustls::sign::CertifiedKey>, TlsServerEndPoint) { // loop here and cut off more and more subdomains until we find // a match to get a proper wildcard support. OTOH, we now do not // use nested domains, so keep this simple for now. // // With the current coding foo.com will match *.foo.com and that // repeats behavior of the old code. if let Some(mut sni_name) = server_name { loop { if let Some(cert) = self.certs.get(sni_name) { return cert.clone(); } if let Some((_, rest)) = sni_name.split_once('.') { sni_name = rest; } else { // The customer has some custom DNS mapping - just return // a default certificate. // // This will error if the customer uses anything stronger // than sslmode=require. That's a choice they can make. return self.default.clone(); } } } else { // No SNI, use the default certificate, otherwise we can't get to // options parameter which can be used to set endpoint name too. // That means that non-SNI flow will not work for CNAME domains in // verify-full mode. // // If that will be a problem we can: // // a) Instead of multi-cert approach use single cert with extra // domains listed in Subject Alternative Name (SAN). // b) Deploy separate proxy instances for extra domains. self.default.clone() } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/proxy/connect_compute.rs
proxy/src/proxy/connect_compute.rs
use tokio::time; use tracing::{debug, info, warn}; use crate::cache::node_info::CachedNodeInfo; use crate::compute::{self, COULD_NOT_CONNECT, ComputeConnection}; use crate::config::{ComputeConfig, ProxyConfig, RetryConfig}; use crate::context::RequestContext; use crate::control_plane::NodeInfo; use crate::control_plane::locks::ApiLocks; use crate::metrics::{ ConnectOutcome, ConnectionFailureKind, Metrics, RetriesMetricGroup, RetryType, }; use crate::proxy::retry::{ShouldRetryWakeCompute, retry_after, should_retry}; use crate::proxy::wake_compute::{WakeComputeBackend, wake_compute}; use crate::types::Host; /// If we couldn't connect, a cached connection info might be to blame /// (e.g. the compute node's address might've changed at the wrong time). /// Invalidate the cache entry (if any) to prevent subsequent errors. #[tracing::instrument(skip_all)] pub(crate) fn invalidate_cache(node_info: CachedNodeInfo) -> NodeInfo { let is_cached = node_info.cached(); if is_cached { warn!("invalidating stalled compute node info cache entry"); } let label = if is_cached { ConnectionFailureKind::ComputeCached } else { ConnectionFailureKind::ComputeUncached }; Metrics::get().proxy.connection_failures_total.inc(label); node_info.invalidate() } pub(crate) trait ConnectMechanism { type Connection; async fn connect_once( &self, ctx: &RequestContext, node_info: &CachedNodeInfo, config: &ComputeConfig, ) -> Result<Self::Connection, compute::ConnectionError>; } struct TcpMechanism<'a> { /// connect_to_compute concurrency lock locks: &'a ApiLocks<Host>, tls: TlsNegotiation, } #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum TlsNegotiation { /// TLS is assumed Direct, /// We must ask for TLS using the postgres SSLRequest message Postgres, } impl ConnectMechanism for TcpMechanism<'_> { type Connection = ComputeConnection; #[tracing::instrument(skip_all, fields( pid = tracing::field::Empty, compute_id = tracing::field::Empty ))] async fn connect_once( &self, ctx: &RequestContext, node_info: &CachedNodeInfo, config: &ComputeConfig, ) -> Result<ComputeConnection, compute::ConnectionError> { let permit = self.locks.get_permit(&node_info.conn_info.host).await?; permit.release_result( node_info .conn_info .connect(ctx, &node_info.aux, config, self.tls) .await, ) } } /// Try to connect to the compute node, retrying if necessary. #[tracing::instrument(skip_all)] pub(crate) async fn connect_to_compute<B: WakeComputeBackend>( ctx: &RequestContext, config: &ProxyConfig, user_info: &B, tls: TlsNegotiation, ) -> Result<ComputeConnection, compute::ConnectionError> { connect_to_compute_inner( ctx, &TcpMechanism { locks: &config.connect_compute_locks, tls, }, user_info, config.wake_compute_retry_config, &config.connect_to_compute, ) .await } /// Try to connect to the compute node, retrying if necessary. pub(crate) async fn connect_to_compute_inner<M: ConnectMechanism, B: WakeComputeBackend>( ctx: &RequestContext, mechanism: &M, user_info: &B, wake_compute_retry_config: RetryConfig, compute: &ComputeConfig, ) -> Result<M::Connection, compute::ConnectionError> { let mut num_retries = 0; let node_info = wake_compute(&mut num_retries, ctx, user_info, wake_compute_retry_config).await?; // try once let err = match mechanism.connect_once(ctx, &node_info, compute).await { Ok(res) => { ctx.success(); Metrics::get().proxy.retries_metric.observe( RetriesMetricGroup { outcome: ConnectOutcome::Success, retry_type: RetryType::ConnectToCompute, }, num_retries.into(), ); return Ok(res); } Err(e) => e, }; debug!(error = ?err, COULD_NOT_CONNECT); let node_info = if !node_info.cached() || !err.should_retry_wake_compute() { // If we just received this from cplane and not from the cache, we shouldn't retry. // Do not need to retrieve a new node_info, just return the old one. if !should_retry(&err, num_retries, compute.retry) { Metrics::get().proxy.retries_metric.observe( RetriesMetricGroup { outcome: ConnectOutcome::Failed, retry_type: RetryType::ConnectToCompute, }, num_retries.into(), ); return Err(err); } node_info } else { // if we failed to connect, it's likely that the compute node was suspended, wake a new compute node debug!("compute node's state has likely changed; requesting a wake-up"); invalidate_cache(node_info); // TODO: increment num_retries? wake_compute(&mut num_retries, ctx, user_info, wake_compute_retry_config).await? }; // now that we have a new node, try connect to it repeatedly. // this can error for a few reasons, for instance: // * DNS connection settings haven't quite propagated yet debug!("wake_compute success. attempting to connect"); num_retries = 1; loop { match mechanism.connect_once(ctx, &node_info, compute).await { Ok(res) => { ctx.success(); Metrics::get().proxy.retries_metric.observe( RetriesMetricGroup { outcome: ConnectOutcome::Success, retry_type: RetryType::ConnectToCompute, }, num_retries.into(), ); // TODO: is this necessary? We have a metric. info!(?num_retries, "connected to compute node after"); return Ok(res); } Err(e) => { if !should_retry(&e, num_retries, compute.retry) { // Don't log an error here, caller will print the error Metrics::get().proxy.retries_metric.observe( RetriesMetricGroup { outcome: ConnectOutcome::Failed, retry_type: RetryType::ConnectToCompute, }, num_retries.into(), ); return Err(e); } warn!(error = ?e, num_retries, retriable = true, COULD_NOT_CONNECT); } } let wait_duration = retry_after(num_retries, compute.retry); num_retries += 1; let pause = ctx.latency_timer_pause(crate::metrics::Waiting::RetryTimeout); time::sleep(wait_duration).await; drop(pause); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/proxy/connect_auth.rs
proxy/src/proxy/connect_auth.rs
use thiserror::Error; use crate::auth::Backend; use crate::auth::backend::ComputeUserInfo; use crate::cache::common::Cache; use crate::compute::{AuthInfo, ComputeConnection, ConnectionError, PostgresError}; use crate::config::ProxyConfig; use crate::context::RequestContext; use crate::control_plane::client::ControlPlaneClient; use crate::error::{ReportableError, UserFacingError}; use crate::proxy::connect_compute::{TlsNegotiation, connect_to_compute}; use crate::proxy::retry::ShouldRetryWakeCompute; #[derive(Debug, Error)] pub enum AuthError { #[error(transparent)] Auth(#[from] PostgresError), #[error(transparent)] Connect(#[from] ConnectionError), } impl UserFacingError for AuthError { fn to_string_client(&self) -> String { match self { AuthError::Auth(postgres_error) => postgres_error.to_string_client(), AuthError::Connect(connection_error) => connection_error.to_string_client(), } } } impl ReportableError for AuthError { fn get_error_kind(&self) -> crate::error::ErrorKind { match self { AuthError::Auth(postgres_error) => postgres_error.get_error_kind(), AuthError::Connect(connection_error) => connection_error.get_error_kind(), } } } /// Try to connect to the compute node, retrying if necessary. #[tracing::instrument(skip_all)] pub(crate) async fn connect_to_compute_and_auth( ctx: &RequestContext, config: &ProxyConfig, user_info: &Backend<'_, ComputeUserInfo>, auth_info: AuthInfo, tls: TlsNegotiation, ) -> Result<ComputeConnection, AuthError> { let mut attempt = 0; // NOTE: This is messy, but should hopefully be detangled with PGLB. // We wanted to separate the concerns of **connect** to compute (a PGLB operation), // from **authenticate** to compute (a NeonKeeper operation). // // This unfortunately removed retry handling for one error case where // the compute was cached, and we connected, but the compute cache was actually stale // and is associated with the wrong endpoint. We detect this when the **authentication** fails. // As such, we retry once here if the `authenticate` function fails and the error is valid to retry. loop { attempt += 1; let mut node = connect_to_compute(ctx, config, user_info, tls).await?; let res = auth_info.authenticate(ctx, &mut node).await; match res { Ok(()) => return Ok(node), Err(e) => { if attempt < 2 && let Backend::ControlPlane(cplane, user_info) = user_info && let ControlPlaneClient::ProxyV1(cplane_proxy_v1) = &**cplane && e.should_retry_wake_compute() { tracing::warn!(error = ?e, "retrying wake compute"); let key = user_info.endpoint_cache_key(); cplane_proxy_v1.caches.node_info.invalidate(&key); continue; } return Err(e)?; } } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/proxy/mod.rs
proxy/src/proxy/mod.rs
#[cfg(test)] mod tests; pub(crate) mod connect_auth; pub(crate) mod connect_compute; pub(crate) mod retry; pub(crate) mod wake_compute; use std::collections::HashSet; use std::convert::Infallible; use std::sync::Arc; use futures::TryStreamExt; use itertools::Itertools; use once_cell::sync::OnceCell; use postgres_client::RawCancelToken; use postgres_client::connect_raw::StartupStream; use postgres_protocol::message::backend::Message; use regex::Regex; use serde::{Deserialize, Serialize}; use smol_str::{SmolStr, format_smolstr}; use tokio::io::{AsyncRead, AsyncWrite}; use tokio::net::TcpStream; use tokio::sync::oneshot; use tracing::Instrument; use crate::cancellation::{CancelClosure, CancellationHandler}; use crate::compute::{ComputeConnection, PostgresError, RustlsStream}; use crate::config::ProxyConfig; use crate::context::RequestContext; pub use crate::pglb::copy_bidirectional::{ErrorSource, copy_bidirectional_client_compute}; use crate::pglb::{ClientMode, ClientRequestError}; use crate::pqproto::{BeMessage, CancelKeyData, StartupMessageParams}; use crate::rate_limiter::EndpointRateLimiter; use crate::stream::{PqStream, Stream}; use crate::types::EndpointCacheKey; use crate::{auth, compute}; #[allow(clippy::too_many_arguments)] pub(crate) async fn handle_client<S: AsyncRead + AsyncWrite + Unpin + Send>( config: &'static ProxyConfig, auth_backend: &'static auth::Backend<'static, ()>, ctx: &RequestContext, cancellation_handler: Arc<CancellationHandler>, client: &mut PqStream<Stream<S>>, mode: &ClientMode, endpoint_rate_limiter: Arc<EndpointRateLimiter>, common_names: Option<&HashSet<String>>, params: &StartupMessageParams, ) -> Result<(ComputeConnection, oneshot::Sender<Infallible>), ClientRequestError> { let hostname = mode.hostname(client.get_ref()); // Extract credentials which we're going to use for auth. let result = auth_backend .as_ref() .map(|()| auth::ComputeUserInfoMaybeEndpoint::parse(ctx, params, hostname, common_names)) .transpose(); let user_info = match result { Ok(user_info) => user_info, Err(e) => Err(client.throw_error(e, Some(ctx)).await)?, }; let user = user_info.get_user().to_owned(); let user_info = match user_info .authenticate( ctx, client, mode.allow_cleartext(), &config.authentication_config, endpoint_rate_limiter, ) .await { Ok(auth_result) => auth_result, Err(e) => { let db = params.get("database"); let app = params.get("application_name"); let params_span = tracing::info_span!("", ?user, ?db, ?app); return Err(client .throw_error(e, Some(ctx)) .instrument(params_span) .await)?; } }; let (cplane, creds) = match user_info { auth::Backend::ControlPlane(cplane, creds) => (cplane, creds), auth::Backend::Local(_) => unreachable!("local proxy does not run tcp proxy service"), }; let params_compat = creds.info.options.get(NeonOptions::PARAMS_COMPAT).is_some(); let mut auth_info = compute::AuthInfo::with_auth_keys(creds.keys); auth_info.set_startup_params(params, params_compat); let backend = auth::Backend::ControlPlane(cplane, creds.info); // TODO: callback to pglb let res = connect_auth::connect_to_compute_and_auth( ctx, config, &backend, auth_info, connect_compute::TlsNegotiation::Postgres, ) .await; let mut node = match res { Ok(node) => node, Err(e) => Err(client.throw_error(e, Some(ctx)).await)?, }; send_client_greeting(ctx, &config.greetings, client); let auth::Backend::ControlPlane(_, user_info) = backend else { unreachable!("ensured above"); }; let session = cancellation_handler.get_key(); let (process_id, secret_key) = forward_compute_params_to_client(ctx, *session.key(), client, &mut node.stream).await?; let hostname = node.hostname.to_string(); let session_id = ctx.session_id(); let (cancel_on_shutdown, cancel) = oneshot::channel(); tokio::spawn(async move { session .maintain_cancel_key( session_id, cancel, &CancelClosure { socket_addr: node.socket_addr, cancel_token: RawCancelToken { ssl_mode: node.ssl_mode, process_id, secret_key, }, hostname, user_info, }, &config.connect_to_compute, ) .await; }); Ok((node, cancel_on_shutdown)) } /// Greet the client with any useful information. pub(crate) fn send_client_greeting( ctx: &RequestContext, greetings: &String, client: &mut PqStream<impl AsyncRead + AsyncWrite + Unpin>, ) { // Expose session_id to clients if we have a greeting message. if !greetings.is_empty() { let session_msg = format!("{}, session_id: {}", greetings, ctx.session_id()); client.write_message(BeMessage::NoticeResponse(session_msg.as_str())); } // Forward recorded latencies for probing requests if let Some(testodrome_id) = ctx.get_testodrome_id() { client.write_message(BeMessage::ParameterStatus { name: "neon.testodrome_id".as_bytes(), value: testodrome_id.as_bytes(), }); let latency_measured = ctx.get_proxy_latency(); client.write_message(BeMessage::ParameterStatus { name: "neon.cplane_latency".as_bytes(), value: latency_measured.cplane.as_micros().to_string().as_bytes(), }); client.write_message(BeMessage::ParameterStatus { name: "neon.client_latency".as_bytes(), value: latency_measured.client.as_micros().to_string().as_bytes(), }); client.write_message(BeMessage::ParameterStatus { name: "neon.compute_latency".as_bytes(), value: latency_measured.compute.as_micros().to_string().as_bytes(), }); client.write_message(BeMessage::ParameterStatus { name: "neon.retry_latency".as_bytes(), value: latency_measured.retry.as_micros().to_string().as_bytes(), }); } } pub(crate) async fn forward_compute_params_to_client( ctx: &RequestContext, cancel_key_data: CancelKeyData, client: &mut PqStream<impl AsyncRead + AsyncWrite + Unpin>, compute: &mut StartupStream<TcpStream, RustlsStream>, ) -> Result<(i32, i32), ClientRequestError> { let mut process_id = 0; let mut secret_key = 0; let err = loop { // if the client buffer is too large, let's write out some bytes now to save some space client.write_if_full().await?; let msg = match compute.try_next().await { Ok(msg) => msg, Err(e) => break postgres_client::Error::io(e), }; match msg { // Send our cancellation key data instead. Some(Message::BackendKeyData(body)) => { client.write_message(BeMessage::BackendKeyData(cancel_key_data)); process_id = body.process_id(); secret_key = body.secret_key(); } // Forward all postgres connection params to the client. Some(Message::ParameterStatus(body)) => { if let Ok(name) = body.name() && let Ok(value) = body.value() { client.write_message(BeMessage::ParameterStatus { name: name.as_bytes(), value: value.as_bytes(), }); } } // Forward all notices to the client. Some(Message::NoticeResponse(notice)) => { client.write_raw(notice.as_bytes().len(), b'N', |buf| { buf.extend_from_slice(notice.as_bytes()); }); } Some(Message::ReadyForQuery(_)) => { client.write_message(BeMessage::ReadyForQuery); return Ok((process_id, secret_key)); } Some(Message::ErrorResponse(body)) => break postgres_client::Error::db(body), Some(_) => break postgres_client::Error::unexpected_message(), None => break postgres_client::Error::closed(), } }; Err(client .throw_error(PostgresError::Postgres(err), Some(ctx)) .await)? } #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] pub(crate) struct NeonOptions(Vec<(SmolStr, SmolStr)>); impl NeonOptions { // proxy options: /// `PARAMS_COMPAT` allows opting in to forwarding all startup parameters from client to compute. pub const PARAMS_COMPAT: &'static str = "proxy_params_compat"; // cplane options: /// `LSN` allows provisioning an ephemeral compute with time-travel to the provided LSN. const LSN: &'static str = "lsn"; /// `TIMESTAMP` allows provisioning an ephemeral compute with time-travel to the provided timestamp. const TIMESTAMP: &'static str = "timestamp"; /// `ENDPOINT_TYPE` allows configuring an ephemeral compute to be read_only or read_write. const ENDPOINT_TYPE: &'static str = "endpoint_type"; pub(crate) fn parse_params(params: &StartupMessageParams) -> Self { params .options_raw() .map(Self::parse_from_iter) .unwrap_or_default() } pub(crate) fn parse_options_raw(options: &str) -> Self { Self::parse_from_iter(StartupMessageParams::parse_options_raw(options)) } pub(crate) fn get(&self, key: &str) -> Option<SmolStr> { self.0 .iter() .find_map(|(k, v)| (k == key).then_some(v)) .cloned() } pub(crate) fn is_ephemeral(&self) -> bool { self.0.iter().any(|(k, _)| match &**k { // This is not a cplane option, we know it does not create ephemeral computes. Self::PARAMS_COMPAT => false, Self::LSN => true, Self::TIMESTAMP => true, Self::ENDPOINT_TYPE => true, // err on the side of caution. any cplane options we don't know about // might lead to ephemeral computes. _ => true, }) } fn parse_from_iter<'a>(options: impl Iterator<Item = &'a str>) -> Self { let mut options = options .filter_map(neon_option) .map(|(k, v)| (k.into(), v.into())) .collect_vec(); options.sort(); Self(options) } pub(crate) fn get_cache_key(&self, prefix: &str) -> EndpointCacheKey { // prefix + format!(" {k}:{v}") // kinda jank because SmolStr is immutable std::iter::once(prefix) .chain(self.0.iter().flat_map(|(k, v)| [" ", &**k, ":", &**v])) .collect::<SmolStr>() .into() } /// <https://swagger.io/docs/specification/serialization/> DeepObject format /// `paramName[prop1]=value1&paramName[prop2]=value2&...` pub(crate) fn to_deep_object(&self) -> Vec<(SmolStr, SmolStr)> { self.0 .iter() .map(|(k, v)| (format_smolstr!("options[{}]", k), v.clone())) .collect() } } pub(crate) fn neon_option(bytes: &str) -> Option<(&str, &str)> { static RE: OnceCell<Regex> = OnceCell::new(); let re = RE.get_or_init(|| Regex::new(r"^neon_(\w+):(.+)").expect("regex should be correct")); let cap = re.captures(bytes)?; let (_, [k, v]) = cap.extract(); Some((k, v)) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/proxy/retry.rs
proxy/src/proxy/retry.rs
use std::error::Error; use std::io; use tokio::time; use crate::compute::{self, PostgresError}; use crate::config::RetryConfig; pub(crate) trait CouldRetry { /// Returns true if the error could be retried fn could_retry(&self) -> bool; } pub(crate) trait ShouldRetryWakeCompute { /// Returns true if we need to invalidate the cache for this node. /// If false, we can continue retrying with the current node cache. fn should_retry_wake_compute(&self) -> bool; } pub(crate) fn should_retry(err: &impl CouldRetry, num_retries: u32, config: RetryConfig) -> bool { num_retries < config.max_retries && err.could_retry() } impl CouldRetry for io::Error { fn could_retry(&self) -> bool { use std::io::ErrorKind; matches!( self.kind(), ErrorKind::ConnectionRefused | ErrorKind::AddrNotAvailable | ErrorKind::TimedOut ) } } impl ShouldRetryWakeCompute for postgres_client::error::DbError { fn should_retry_wake_compute(&self) -> bool { use postgres_client::error::SqlState; // Here are errors that happens after the user successfully authenticated to the database. // TODO: there are pgbouncer errors that should be retried, but they are not listed here. let non_retriable_pg_errors = matches!( self.code(), &SqlState::TOO_MANY_CONNECTIONS | &SqlState::OUT_OF_MEMORY | &SqlState::SYNTAX_ERROR | &SqlState::T_R_SERIALIZATION_FAILURE | &SqlState::INVALID_CATALOG_NAME | &SqlState::INVALID_SCHEMA_NAME | &SqlState::INVALID_PARAMETER_VALUE, ); if non_retriable_pg_errors { return false; } // PGBouncer errors that should not trigger a wake_compute retry. if self.code() == &SqlState::PROTOCOL_VIOLATION { // Source for the error message: // https://github.com/pgbouncer/pgbouncer/blob/f15997fe3effe3a94ba8bcc1ea562e6117d1a131/src/client.c#L1070 return !self .message() .contains("no more connections allowed (max_client_conn)"); } true } } impl ShouldRetryWakeCompute for postgres_client::Error { fn should_retry_wake_compute(&self) -> bool { if let Some(db_err) = self.source().and_then(|x| x.downcast_ref()) { postgres_client::error::DbError::should_retry_wake_compute(db_err) } else { // likely an IO error. Possible the compute has shutdown and the // cache is stale. true } } } impl CouldRetry for compute::ConnectionError { fn could_retry(&self) -> bool { match self { compute::ConnectionError::TlsError(err) => err.could_retry(), compute::ConnectionError::WakeComputeError(err) => err.could_retry(), compute::ConnectionError::TooManyConnectionAttempts(_) => false, #[cfg(test)] compute::ConnectionError::TestError { retryable, .. } => *retryable, } } } impl ShouldRetryWakeCompute for compute::ConnectionError { fn should_retry_wake_compute(&self) -> bool { match self { // the cache entry was not checked for validity compute::ConnectionError::TooManyConnectionAttempts(_) => false, #[cfg(test)] compute::ConnectionError::TestError { wakeable, .. } => *wakeable, _ => true, } } } impl ShouldRetryWakeCompute for PostgresError { fn should_retry_wake_compute(&self) -> bool { match self { PostgresError::Postgres(error) => error.should_retry_wake_compute(), } } } pub(crate) fn retry_after(num_retries: u32, config: RetryConfig) -> time::Duration { config .base_delay .mul_f64(config.backoff_factor.powi((num_retries as i32) - 1)) } #[cfg(test)] mod tests { use postgres_client::error::{DbError, SqlState}; use super::ShouldRetryWakeCompute; #[test] fn should_retry_wake_compute_for_db_error() { // These SQLStates should NOT trigger a wake_compute retry. let non_retry_states = [ SqlState::TOO_MANY_CONNECTIONS, SqlState::OUT_OF_MEMORY, SqlState::SYNTAX_ERROR, SqlState::T_R_SERIALIZATION_FAILURE, SqlState::INVALID_CATALOG_NAME, SqlState::INVALID_SCHEMA_NAME, SqlState::INVALID_PARAMETER_VALUE, ]; for state in non_retry_states { let err = DbError::new_test_error(state.clone(), "oops".to_string()); assert!( !err.should_retry_wake_compute(), "State {state:?} unexpectedly retried" ); } // Errors coming from pgbouncer should not trigger a wake_compute retry let non_retry_pgbouncer_errors = ["no more connections allowed (max_client_conn)"]; for error in non_retry_pgbouncer_errors { let err = DbError::new_test_error(SqlState::PROTOCOL_VIOLATION, error.to_string()); assert!( !err.should_retry_wake_compute(), "PGBouncer error {error:?} unexpectedly retried" ); } // These SQLStates should trigger a wake_compute retry. let retry_states = [ SqlState::CONNECTION_FAILURE, SqlState::CONNECTION_EXCEPTION, SqlState::CONNECTION_DOES_NOT_EXIST, SqlState::SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, ]; for state in retry_states { let err = DbError::new_test_error(state.clone(), "oops".to_string()); assert!( err.should_retry_wake_compute(), "State {state:?} unexpectedly skipped retry" ); } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/proxy/wake_compute.rs
proxy/src/proxy/wake_compute.rs
use async_trait::async_trait; use tracing::{error, info}; use crate::cache::node_info::CachedNodeInfo; use crate::config::RetryConfig; use crate::context::RequestContext; use crate::control_plane::errors::{ControlPlaneError, WakeComputeError}; use crate::error::ReportableError; use crate::metrics::{ ConnectOutcome, ConnectionFailuresBreakdownGroup, Metrics, RetriesMetricGroup, RetryType, }; use crate::proxy::retry::{retry_after, should_retry}; // Use macro to retain original callsite. macro_rules! log_wake_compute_error { (error = ?$error:expr, $num_retries:expr, retriable = $retriable:literal) => { match $error { WakeComputeError::ControlPlane(ControlPlaneError::Message(_)) => { info!(error = ?$error, num_retries = $num_retries, retriable = $retriable, "couldn't wake compute node") } _ => error!(error = ?$error, num_retries = $num_retries, retriable = $retriable, "couldn't wake compute node"), } }; } #[async_trait] pub(crate) trait WakeComputeBackend { async fn wake_compute(&self, ctx: &RequestContext) -> Result<CachedNodeInfo, WakeComputeError>; } pub(crate) async fn wake_compute<B: WakeComputeBackend>( num_retries: &mut u32, ctx: &RequestContext, api: &B, config: RetryConfig, ) -> Result<CachedNodeInfo, WakeComputeError> { loop { match api.wake_compute(ctx).await { Err(e) if !should_retry(&e, *num_retries, config) => { log_wake_compute_error!(error = ?e, num_retries, retriable = false); report_error(&e, false); Metrics::get().proxy.retries_metric.observe( RetriesMetricGroup { outcome: ConnectOutcome::Failed, retry_type: RetryType::WakeCompute, }, (*num_retries).into(), ); return Err(e); } Err(e) => { log_wake_compute_error!(error = ?e, num_retries, retriable = true); report_error(&e, true); } Ok(n) => { Metrics::get().proxy.retries_metric.observe( RetriesMetricGroup { outcome: ConnectOutcome::Success, retry_type: RetryType::WakeCompute, }, (*num_retries).into(), ); // TODO: is this necessary? We have a metric. // TODO: this log line is misleading as "wake_compute" might return cached (and stale) info. info!(?num_retries, "compute node woken up after"); return Ok(n); } } let wait_duration = retry_after(*num_retries, config); *num_retries += 1; let pause = ctx.latency_timer_pause(crate::metrics::Waiting::RetryTimeout); tokio::time::sleep(wait_duration).await; drop(pause); } } fn report_error(e: &WakeComputeError, retry: bool) { let kind = e.get_error_kind(); Metrics::get() .proxy .connection_failures_breakdown .inc(ConnectionFailuresBreakdownGroup { kind, retry: retry.into(), }); }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/proxy/tests/mitm.rs
proxy/src/proxy/tests/mitm.rs
//! Man-in-the-middle tests //! //! Channel binding should prevent a proxy server //! *that has access to create valid certificates* //! from controlling the TLS connection. use std::fmt::Debug; use bytes::{Bytes, BytesMut}; use futures::{SinkExt, StreamExt}; use postgres_client::tls::TlsConnect; use postgres_protocol::message::frontend; use tokio::io::{AsyncReadExt, AsyncWriteExt, DuplexStream}; use tokio_util::codec::{Decoder, Encoder}; use super::*; use crate::config::TlsConfig; use crate::context::RequestContext; use crate::pglb::handshake::{HandshakeData, handshake}; enum Intercept { None, Methods, SASLResponse, } async fn proxy_mitm( intercept: Intercept, ) -> (DuplexStream, DuplexStream, ClientConfig<'static>, TlsConfig) { let (end_server1, client1) = tokio::io::duplex(1024); let (server2, end_client2) = tokio::io::duplex(1024); let (client_config1, server_config1) = generate_tls_config("generic-project-name.localhost", "localhost").unwrap(); let (client_config2, server_config2) = generate_tls_config("generic-project-name.localhost", "localhost").unwrap(); tokio::spawn(async move { // begin handshake with end_server let end_server = connect_tls(server2, client_config2.make_tls_connect().unwrap()).await; let (end_client, startup) = match handshake( &RequestContext::test(), client1, Some(&server_config1), false, ) .await .unwrap() { HandshakeData::Startup(stream, params) => (stream, params), HandshakeData::Cancel(_) => panic!("cancellation not supported"), }; let mut end_server = tokio_util::codec::Framed::new(end_server, PgFrame); let end_client = end_client.flush_and_into_inner().await.unwrap(); let mut end_client = tokio_util::codec::Framed::new(end_client, PgFrame); // give the end_server the startup parameters let mut buf = BytesMut::new(); frontend::startup_message( &postgres_protocol::message::frontend::StartupMessageParams { params: startup.params.as_bytes().into(), }, &mut buf, ) .unwrap(); end_server.send(buf.freeze()).await.unwrap(); // proxy messages between end_client and end_server loop { tokio::select! { message = end_server.next() => { match message { Some(Ok(message)) => { // intercept SASL and return only SCRAM-SHA-256 ;) if matches!(intercept, Intercept::Methods) && message.starts_with(b"R") && message[5..].starts_with(&[0,0,0,10]) { end_client.send(Bytes::from_static(b"R\0\0\0\x17\0\0\0\x0aSCRAM-SHA-256\0\0")).await.unwrap(); continue; } end_client.send(message).await.unwrap(); } _ => break, } } message = end_client.next() => { match message { Some(Ok(message)) => { // intercept SASL response and return SCRAM-SHA-256 with no channel binding ;) if matches!(intercept, Intercept::SASLResponse) && message.starts_with(b"p") && message[5..].starts_with(b"SCRAM-SHA-256-PLUS\0") { let sasl_message = &message[1+4+19+4..]; let mut new_message = b"n,,".to_vec(); new_message.extend_from_slice(sasl_message.strip_prefix(b"p=tls-server-end-point,,").unwrap()); let mut buf = BytesMut::new(); frontend::sasl_initial_response("SCRAM-SHA-256", &new_message, &mut buf).unwrap(); end_server.send(buf.freeze()).await.unwrap(); continue; } end_server.send(message).await.unwrap(); } _ => break, } } else => { break } } } }); (end_server1, end_client2, client_config1, server_config2) } /// taken from tokio-postgres pub(crate) async fn connect_tls<S, T>(mut stream: S, tls: T) -> T::Stream where S: AsyncRead + AsyncWrite + Unpin, T: TlsConnect<S>, T::Error: Debug, { let mut buf = BytesMut::new(); frontend::ssl_request(&mut buf); stream.write_all(&buf).await.unwrap(); let mut buf = [0]; stream.read_exact(&mut buf).await.unwrap(); assert!(buf[0] == b'S', "ssl not supported by server"); tls.connect(stream).await.unwrap() } struct PgFrame; impl Decoder for PgFrame { type Item = Bytes; type Error = std::io::Error; fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { if src.len() < 5 { src.reserve(5 - src.len()); return Ok(None); } let len = u32::from_be_bytes(src[1..5].try_into().unwrap()) as usize + 1; if src.len() < len { src.reserve(len - src.len()); return Ok(None); } Ok(Some(src.split_to(len).freeze())) } } impl Encoder<Bytes> for PgFrame { type Error = std::io::Error; fn encode(&mut self, item: Bytes, dst: &mut BytesMut) -> Result<(), Self::Error> { dst.extend_from_slice(&item); Ok(()) } } /// If the client doesn't support channel bindings, it can be exploited. #[tokio::test] async fn scram_auth_disable_channel_binding() -> anyhow::Result<()> { let (server, client, client_config, server_config) = proxy_mitm(Intercept::None).await; let proxy = tokio::spawn(dummy_proxy( client, Some(server_config), Scram::new("password").await?, )); let _client_err = postgres_client::Config::new("test".to_owned(), 5432) .channel_binding(postgres_client::config::ChannelBinding::Disable) .user("user") .dbname("db") .password("password") .ssl_mode(SslMode::Require) .tls_and_authenticate(server, client_config.make_tls_connect()?) .await?; proxy.await? } /// If the client chooses SCRAM-PLUS, it will fail #[tokio::test] async fn scram_auth_prefer_channel_binding() -> anyhow::Result<()> { connect_failure( Intercept::None, postgres_client::config::ChannelBinding::Prefer, ) .await } /// If the MITM pretends like SCRAM-PLUS isn't available, but the client supports it, it will fail #[tokio::test] async fn scram_auth_prefer_channel_binding_intercept() -> anyhow::Result<()> { connect_failure( Intercept::Methods, postgres_client::config::ChannelBinding::Prefer, ) .await } /// If the MITM pretends like the client doesn't support channel bindings, it will fail #[tokio::test] async fn scram_auth_prefer_channel_binding_intercept_response() -> anyhow::Result<()> { connect_failure( Intercept::SASLResponse, postgres_client::config::ChannelBinding::Prefer, ) .await } /// If the client chooses SCRAM-PLUS, it will fail #[tokio::test] async fn scram_auth_require_channel_binding() -> anyhow::Result<()> { connect_failure( Intercept::None, postgres_client::config::ChannelBinding::Require, ) .await } /// If the client requires SCRAM-PLUS, and it is spoofed to remove SCRAM-PLUS, it will fail #[tokio::test] async fn scram_auth_require_channel_binding_intercept() -> anyhow::Result<()> { connect_failure( Intercept::Methods, postgres_client::config::ChannelBinding::Require, ) .await } /// If the client requires SCRAM-PLUS, and it is spoofed to remove SCRAM-PLUS, it will fail #[tokio::test] async fn scram_auth_require_channel_binding_intercept_response() -> anyhow::Result<()> { connect_failure( Intercept::SASLResponse, postgres_client::config::ChannelBinding::Require, ) .await } async fn connect_failure( intercept: Intercept, channel_binding: postgres_client::config::ChannelBinding, ) -> anyhow::Result<()> { let (server, client, client_config, server_config) = proxy_mitm(intercept).await; let proxy = tokio::spawn(dummy_proxy( client, Some(server_config), Scram::new("password").await?, )); let _client_err = postgres_client::Config::new("test".to_owned(), 5432) .channel_binding(channel_binding) .user("user") .dbname("db") .password("password") .ssl_mode(SslMode::Require) .tls_and_authenticate(server, client_config.make_tls_connect()?) .await .err() .context("client shouldn't be able to connect")?; let _server_err = proxy .await? .err() .context("server shouldn't accept client")?; Ok(()) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/proxy/tests/mod.rs
proxy/src/proxy/tests/mod.rs
//! A group of high-level tests for connection establishing logic and auth. #![allow(clippy::unimplemented)] mod mitm; use std::sync::Arc; use std::time::Duration; use anyhow::{Context, bail}; use async_trait::async_trait; use http::StatusCode; use postgres_client::config::SslMode; use postgres_client::tls::{MakeTlsConnect, NoTls}; use rstest::rstest; use rustls::crypto::ring; use rustls::pki_types; use tokio::io::{AsyncRead, AsyncWrite, DuplexStream}; use tokio::time::Instant; use tracing_test::traced_test; use super::retry::CouldRetry; use crate::auth::backend::{ComputeUserInfo, MaybeOwned}; use crate::cache::node_info::{CachedNodeInfo, NodeInfoCache}; use crate::config::{CacheOptions, ComputeConfig, RetryConfig, TlsConfig}; use crate::context::RequestContext; use crate::control_plane::client::{ControlPlaneClient, TestControlPlaneClient}; use crate::control_plane::messages::{ControlPlaneErrorMessage, Details, MetricsAuxInfo, Status}; use crate::control_plane::{self, NodeInfo}; use crate::error::ErrorKind; use crate::pglb::ERR_INSECURE_CONNECTION; use crate::pglb::handshake::{HandshakeData, handshake}; use crate::pqproto::BeMessage; use crate::proxy::NeonOptions; use crate::proxy::connect_compute::{ConnectMechanism, connect_to_compute_inner}; use crate::proxy::retry::retry_after; use crate::stream::{PqStream, Stream}; use crate::tls::client_config::compute_client_config_with_certs; use crate::tls::server_config::CertResolver; use crate::types::{BranchId, EndpointId, ProjectId}; use crate::{auth, compute, sasl, scram}; /// Generate a set of TLS certificates: CA + server. fn generate_certs( hostname: &str, common_name: &str, ) -> anyhow::Result<( pki_types::CertificateDer<'static>, pki_types::CertificateDer<'static>, pki_types::PrivateKeyDer<'static>, )> { let ca_key = rcgen::KeyPair::generate()?; let ca = { let mut params = rcgen::CertificateParams::default(); params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained); params.self_signed(&ca_key)? }; let cert_key = rcgen::KeyPair::generate()?; let cert = { let mut params = rcgen::CertificateParams::new(vec![hostname.into()])?; params.distinguished_name = rcgen::DistinguishedName::new(); params .distinguished_name .push(rcgen::DnType::CommonName, common_name); params.signed_by(&cert_key, &ca, &ca_key)? }; Ok(( ca.der().clone(), cert.der().clone(), pki_types::PrivateKeyDer::Pkcs8(cert_key.serialize_der().into()), )) } struct ClientConfig<'a> { config: Arc<rustls::ClientConfig>, hostname: &'a str, } type TlsConnect<S> = <ComputeConfig as MakeTlsConnect<S>>::TlsConnect; impl ClientConfig<'_> { fn make_tls_connect(self) -> anyhow::Result<TlsConnect<DuplexStream>> { Ok(crate::tls::postgres_rustls::make_tls_connect( &self.config, self.hostname, )?) } } /// Generate TLS certificates and build rustls configs for client and server. fn generate_tls_config<'a>( hostname: &'a str, common_name: &'a str, ) -> anyhow::Result<(ClientConfig<'a>, TlsConfig)> { let (ca, cert, key) = generate_certs(hostname, common_name)?; let tls_config = { let config = rustls::ServerConfig::builder_with_provider(Arc::new(ring::default_provider())) .with_safe_default_protocol_versions() .context("ring should support the default protocol versions")? .with_no_client_auth() .with_single_cert(vec![cert.clone()], key.clone_key())?; let cert_resolver = CertResolver::new(key, vec![cert])?; let common_names = cert_resolver.get_common_names(); let config = Arc::new(config); TlsConfig { http_config: config.clone(), pg_config: config, common_names, cert_resolver: Arc::new(cert_resolver), } }; let client_config = { let config = Arc::new(compute_client_config_with_certs([ca])); ClientConfig { config, hostname } }; Ok((client_config, tls_config)) } #[async_trait] trait TestAuth: Sized { async fn authenticate<S: AsyncRead + AsyncWrite + Unpin + Send>( self, stream: &mut PqStream<Stream<S>>, ) -> anyhow::Result<()> { stream.write_message(BeMessage::AuthenticationOk); Ok(()) } } struct NoAuth; impl TestAuth for NoAuth {} struct Scram(scram::ServerSecret); impl Scram { async fn new(password: &str) -> anyhow::Result<Self> { let secret = scram::ServerSecret::build(password) .await .context("failed to generate scram secret")?; Ok(Scram(secret)) } fn mock() -> Self { Scram(scram::ServerSecret::mock(rand::random())) } } #[async_trait] impl TestAuth for Scram { async fn authenticate<S: AsyncRead + AsyncWrite + Unpin + Send>( self, stream: &mut PqStream<Stream<S>>, ) -> anyhow::Result<()> { let outcome = auth::AuthFlow::new(stream, auth::Scram(&self.0, &RequestContext::test())) .authenticate() .await?; use sasl::Outcome::*; match outcome { Success(_) => Ok(()), Failure(reason) => bail!("autentication failed with an error: {reason}"), } } } /// A dummy proxy impl which performs a handshake and reports auth success. async fn dummy_proxy( client: impl AsyncRead + AsyncWrite + Unpin + Send, tls: Option<TlsConfig>, auth: impl TestAuth + Send, ) -> anyhow::Result<()> { let mut stream = match handshake(&RequestContext::test(), client, tls.as_ref(), false).await? { HandshakeData::Startup(stream, _) => stream, HandshakeData::Cancel(_) => bail!("cancellation not supported"), }; auth.authenticate(&mut stream).await?; stream.write_message(BeMessage::ParameterStatus { name: b"client_encoding", value: b"UTF8", }); stream.write_message(BeMessage::ReadyForQuery); stream.flush().await?; Ok(()) } #[tokio::test] async fn handshake_tls_is_enforced_by_proxy() -> anyhow::Result<()> { let (client, server) = tokio::io::duplex(1024); let (_, server_config) = generate_tls_config("generic-project-name.localhost", "localhost")?; let proxy = tokio::spawn(dummy_proxy(client, Some(server_config), NoAuth)); let client_err = postgres_client::Config::new("test".to_owned(), 5432) .user("john_doe") .dbname("earth") .ssl_mode(SslMode::Disable) .tls_and_authenticate(server, NoTls) .await .err() // -> Option<E> .context("client shouldn't be able to connect")?; assert!(client_err.to_string().contains(ERR_INSECURE_CONNECTION)); let server_err = proxy .await? .err() // -> Option<E> .context("server shouldn't accept client")?; assert!(client_err.to_string().contains(&server_err.to_string())); Ok(()) } #[tokio::test] async fn handshake_tls() -> anyhow::Result<()> { let (client, server) = tokio::io::duplex(1024); let (client_config, server_config) = generate_tls_config("generic-project-name.localhost", "localhost")?; let proxy = tokio::spawn(dummy_proxy(client, Some(server_config), NoAuth)); let _conn = postgres_client::Config::new("test".to_owned(), 5432) .user("john_doe") .dbname("earth") .ssl_mode(SslMode::Require) .tls_and_authenticate(server, client_config.make_tls_connect()?) .await?; proxy.await? } #[tokio::test] async fn handshake_raw() -> anyhow::Result<()> { let (client, server) = tokio::io::duplex(1024); let proxy = tokio::spawn(dummy_proxy(client, None, NoAuth)); let _conn = postgres_client::Config::new("test".to_owned(), 5432) .user("john_doe") .dbname("earth") .set_param("options", "project=generic-project-name") .ssl_mode(SslMode::Prefer) .tls_and_authenticate(server, NoTls) .await?; proxy.await? } #[tokio::test] async fn keepalive_is_inherited() -> anyhow::Result<()> { use tokio::net::{TcpListener, TcpStream}; let listener = TcpListener::bind("127.0.0.1:0").await?; let port = listener.local_addr()?.port(); socket2::SockRef::from(&listener).set_keepalive(true)?; let t = tokio::spawn(async move { let (client, _) = listener.accept().await?; let keepalive = socket2::SockRef::from(&client).keepalive()?; anyhow::Ok(keepalive) }); TcpStream::connect(("127.0.0.1", port)).await?; assert!(t.await??, "keepalive should be inherited"); Ok(()) } #[rstest] #[case("password_foo")] #[case("pwd-bar")] #[case("")] #[tokio::test] async fn scram_auth_good(#[case] password: &str) -> anyhow::Result<()> { let (client, server) = tokio::io::duplex(1024); let (client_config, server_config) = generate_tls_config("generic-project-name.localhost", "localhost")?; let proxy = tokio::spawn(dummy_proxy( client, Some(server_config), Scram::new(password).await?, )); let _conn = postgres_client::Config::new("test".to_owned(), 5432) .channel_binding(postgres_client::config::ChannelBinding::Require) .user("user") .dbname("db") .password(password) .ssl_mode(SslMode::Require) .tls_and_authenticate(server, client_config.make_tls_connect()?) .await?; proxy.await? } #[tokio::test] async fn scram_auth_disable_channel_binding() -> anyhow::Result<()> { let (client, server) = tokio::io::duplex(1024); let (client_config, server_config) = generate_tls_config("generic-project-name.localhost", "localhost")?; let proxy = tokio::spawn(dummy_proxy( client, Some(server_config), Scram::new("password").await?, )); let _conn = postgres_client::Config::new("test".to_owned(), 5432) .channel_binding(postgres_client::config::ChannelBinding::Disable) .user("user") .dbname("db") .password("password") .ssl_mode(SslMode::Require) .tls_and_authenticate(server, client_config.make_tls_connect()?) .await?; proxy.await? } #[tokio::test] async fn scram_auth_mock() -> anyhow::Result<()> { let (client, server) = tokio::io::duplex(1024); let (client_config, server_config) = generate_tls_config("generic-project-name.localhost", "localhost")?; let proxy = tokio::spawn(dummy_proxy(client, Some(server_config), Scram::mock())); use rand::Rng; use rand::distr::Alphanumeric; let password: String = rand::rng() .sample_iter(&Alphanumeric) .take(rand::random::<u8>() as usize) .map(char::from) .collect(); let _client_err = postgres_client::Config::new("test".to_owned(), 5432) .user("user") .dbname("db") .password(&password) // no password will match the mocked secret .ssl_mode(SslMode::Require) .tls_and_authenticate(server, client_config.make_tls_connect()?) .await .err() // -> Option<E> .context("client shouldn't be able to connect")?; let _server_err = proxy .await? .err() // -> Option<E> .context("server shouldn't accept client")?; Ok(()) } #[test] fn connect_compute_total_wait() { let mut total_wait = tokio::time::Duration::ZERO; let config = RetryConfig { base_delay: Duration::from_secs(1), max_retries: 5, backoff_factor: 2.0, }; for num_retries in 1..config.max_retries { total_wait += retry_after(num_retries, config); } assert!(f64::abs(total_wait.as_secs_f64() - 15.0) < 0.1); } #[derive(Clone, Copy, Debug)] enum ConnectAction { Wake, WakeCold, WakeFail, WakeRetry, Connect, // connect_once -> Err, could_retry = true, should_retry_wake_compute = true Retry, // connect_once -> Err, could_retry = true, should_retry_wake_compute = false RetryNoWake, // connect_once -> Err, could_retry = false, should_retry_wake_compute = true Fail, // connect_once -> Err, could_retry = false, should_retry_wake_compute = false FailNoWake, } #[derive(Clone)] struct TestConnectMechanism { counter: Arc<std::sync::Mutex<usize>>, sequence: Vec<ConnectAction>, cache: &'static NodeInfoCache, } impl TestConnectMechanism { fn verify(&self) { let counter = self.counter.lock().unwrap(); assert_eq!( *counter, self.sequence.len(), "sequence does not proceed to the end" ); } } impl TestConnectMechanism { fn new(sequence: Vec<ConnectAction>) -> Self { Self { counter: Arc::new(std::sync::Mutex::new(0)), sequence, cache: Box::leak(Box::new(NodeInfoCache::new(CacheOptions { size: Some(1), absolute_ttl: Some(Duration::from_secs(100)), idle_ttl: None, }))), } } } #[derive(Debug)] struct TestConnection; impl ConnectMechanism for TestConnectMechanism { type Connection = TestConnection; async fn connect_once( &self, _ctx: &RequestContext, _node_info: &CachedNodeInfo, _config: &ComputeConfig, ) -> Result<Self::Connection, compute::ConnectionError> { let mut counter = self.counter.lock().unwrap(); let action = self.sequence[*counter]; *counter += 1; match action { ConnectAction::Connect => Ok(TestConnection), ConnectAction::Retry => Err(compute::ConnectionError::TestError { retryable: true, wakeable: true, kind: ErrorKind::Compute, }), ConnectAction::RetryNoWake => Err(compute::ConnectionError::TestError { retryable: true, wakeable: false, kind: ErrorKind::Compute, }), ConnectAction::Fail => Err(compute::ConnectionError::TestError { retryable: false, wakeable: true, kind: ErrorKind::Compute, }), ConnectAction::FailNoWake => Err(compute::ConnectionError::TestError { retryable: false, wakeable: false, kind: ErrorKind::Compute, }), x => panic!("expecting action {x:?}, connect is called instead"), } } } impl TestControlPlaneClient for TestConnectMechanism { fn wake_compute(&self) -> Result<CachedNodeInfo, control_plane::errors::WakeComputeError> { let mut counter = self.counter.lock().unwrap(); let action = self.sequence[*counter]; *counter += 1; match action { ConnectAction::Wake => Ok(helper_create_cached_node_info(self.cache)), ConnectAction::WakeCold => Ok(CachedNodeInfo::new_uncached( helper_create_uncached_node_info(), )), ConnectAction::WakeFail => { let err = control_plane::errors::ControlPlaneError::Message(Box::new( ControlPlaneErrorMessage { http_status_code: StatusCode::BAD_REQUEST, error: "TEST".into(), status: None, }, )); assert!(!err.could_retry()); Err(control_plane::errors::WakeComputeError::ControlPlane(err)) } ConnectAction::WakeRetry => { let err = control_plane::errors::ControlPlaneError::Message(Box::new( ControlPlaneErrorMessage { http_status_code: StatusCode::BAD_REQUEST, error: "TEST".into(), status: Some(Status { code: "error".into(), message: "error".into(), details: Details { error_info: None, retry_info: Some(control_plane::messages::RetryInfo { retry_at: Instant::now() + Duration::from_millis(1), }), user_facing_message: None, }, }), }, )); assert!(err.could_retry()); Err(control_plane::errors::WakeComputeError::ControlPlane(err)) } x => panic!("expecting action {x:?}, wake_compute is called instead"), } } fn get_access_control( &self, ) -> Result<control_plane::EndpointAccessControl, control_plane::errors::GetAuthInfoError> { unimplemented!("not used in tests") } fn dyn_clone(&self) -> Box<dyn TestControlPlaneClient> { Box::new(self.clone()) } } fn helper_create_uncached_node_info() -> NodeInfo { NodeInfo { conn_info: compute::ConnectInfo { host: "test".into(), port: 5432, ssl_mode: SslMode::Disable, host_addr: None, }, aux: MetricsAuxInfo { endpoint_id: (&EndpointId::from("endpoint")).into(), project_id: (&ProjectId::from("project")).into(), branch_id: (&BranchId::from("branch")).into(), compute_id: "compute".into(), cold_start_info: crate::control_plane::messages::ColdStartInfo::Warm, }, } } fn helper_create_cached_node_info(cache: &'static NodeInfoCache) -> CachedNodeInfo { let node = helper_create_uncached_node_info(); cache.insert("key".into(), Ok(node.clone())); CachedNodeInfo { token: Some((cache, "key".into())), value: node, } } fn helper_create_connect_info( mechanism: &TestConnectMechanism, ) -> auth::Backend<'static, ComputeUserInfo> { auth::Backend::ControlPlane( MaybeOwned::Owned(ControlPlaneClient::Test(Box::new(mechanism.clone()))), ComputeUserInfo { endpoint: "endpoint".into(), user: "user".into(), options: NeonOptions::parse_options_raw(""), }, ) } fn config() -> ComputeConfig { let retry = RetryConfig { base_delay: Duration::from_secs(1), max_retries: 5, backoff_factor: 2.0, }; ComputeConfig { retry, tls: Arc::new(compute_client_config_with_certs(std::iter::empty())), timeout: Duration::from_secs(2), } } #[tokio::test] async fn connect_to_compute_success() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); let mechanism = TestConnectMechanism::new(vec![Wake, Connect]); let user_info = helper_create_connect_info(&mechanism); let config = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, config.retry, &config) .await .unwrap(); mechanism.verify(); } #[tokio::test] async fn connect_to_compute_retry() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); let mechanism = TestConnectMechanism::new(vec![Wake, Retry, Wake, Connect]); let user_info = helper_create_connect_info(&mechanism); let config = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, config.retry, &config) .await .unwrap(); mechanism.verify(); } /// Test that we don't retry if the error is not retryable. #[tokio::test] async fn connect_to_compute_non_retry_1() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); let mechanism = TestConnectMechanism::new(vec![Wake, Retry, Wake, Fail]); let user_info = helper_create_connect_info(&mechanism); let config = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, config.retry, &config) .await .unwrap_err(); mechanism.verify(); } /// Even for non-retryable errors, we should retry at least once. #[tokio::test] async fn connect_to_compute_non_retry_2() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); let mechanism = TestConnectMechanism::new(vec![Wake, Fail, Wake, Connect]); let user_info = helper_create_connect_info(&mechanism); let config = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, config.retry, &config) .await .unwrap(); mechanism.verify(); } /// Retry for at most `NUM_RETRIES_CONNECT` times. #[tokio::test] async fn connect_to_compute_non_retry_3() { let _ = env_logger::try_init(); tokio::time::pause(); use ConnectAction::*; let ctx = RequestContext::test(); let mechanism = TestConnectMechanism::new(vec![Wake, Retry, Wake, Retry, Retry, Retry, Retry, Retry]); let user_info = helper_create_connect_info(&mechanism); let wake_compute_retry_config = RetryConfig { base_delay: Duration::from_secs(1), max_retries: 1, backoff_factor: 2.0, }; let config = config(); connect_to_compute_inner( &ctx, &mechanism, &user_info, wake_compute_retry_config, &config, ) .await .unwrap_err(); mechanism.verify(); } /// Should retry wake compute. #[tokio::test] async fn wake_retry() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); let mechanism = TestConnectMechanism::new(vec![WakeRetry, Wake, Connect]); let user_info = helper_create_connect_info(&mechanism); let config = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, config.retry, &config) .await .unwrap(); mechanism.verify(); } /// Wake failed with a non-retryable error. #[tokio::test] async fn wake_non_retry() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); let mechanism = TestConnectMechanism::new(vec![WakeRetry, WakeFail]); let user_info = helper_create_connect_info(&mechanism); let config = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, config.retry, &config) .await .unwrap_err(); mechanism.verify(); } #[tokio::test] #[traced_test] async fn fail_but_wake_invalidates_cache() { let ctx = RequestContext::test(); let mech = TestConnectMechanism::new(vec![ ConnectAction::Wake, ConnectAction::Fail, ConnectAction::Wake, ConnectAction::Connect, ]); let user = helper_create_connect_info(&mech); let cfg = config(); connect_to_compute_inner(&ctx, &mech, &user, cfg.retry, &cfg) .await .unwrap(); assert!(logs_contain( "invalidating stalled compute node info cache entry" )); } #[tokio::test] #[traced_test] async fn fail_no_wake_skips_cache_invalidation() { let ctx = RequestContext::test(); let mech = TestConnectMechanism::new(vec![ ConnectAction::Wake, ConnectAction::RetryNoWake, ConnectAction::Connect, ]); let user = helper_create_connect_info(&mech); let cfg = config(); connect_to_compute_inner(&ctx, &mech, &user, cfg.retry, &cfg) .await .unwrap(); assert!(!logs_contain( "invalidating stalled compute node info cache entry" )); } #[tokio::test] #[traced_test] async fn retry_but_wake_invalidates_cache() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); // Wake → Retry (retryable + wakeable) → Wake → Connect let mechanism = TestConnectMechanism::new(vec![Wake, Retry, Wake, Connect]); let user_info = helper_create_connect_info(&mechanism); let cfg = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, cfg.retry, &cfg) .await .unwrap(); mechanism.verify(); // Because Retry has wakeable=true, we should see invalidate_cache assert!(logs_contain( "invalidating stalled compute node info cache entry" )); } #[tokio::test] #[traced_test] async fn retry_no_wake_skips_invalidation() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); // Wake → RetryNoWake (retryable + NOT wakeable) let mechanism = TestConnectMechanism::new(vec![Wake, RetryNoWake, Fail]); let user_info = helper_create_connect_info(&mechanism); let cfg = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, cfg.retry, &cfg) .await .unwrap_err(); mechanism.verify(); // Because RetryNoWake has wakeable=false, we must NOT see invalidate_cache assert!(!logs_contain( "invalidating stalled compute node info cache entry" )); } #[tokio::test] #[traced_test] async fn retry_no_wake_error_fast() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); // Wake → FailNoWake (not retryable + NOT wakeable) let mechanism = TestConnectMechanism::new(vec![Wake, FailNoWake]); let user_info = helper_create_connect_info(&mechanism); let cfg = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, cfg.retry, &cfg) .await .unwrap_err(); mechanism.verify(); // Because FailNoWake has wakeable=false, we must NOT see invalidate_cache assert!(!logs_contain( "invalidating stalled compute node info cache entry" )); } #[tokio::test] #[traced_test] async fn retry_cold_wake_skips_invalidation() { let _ = env_logger::try_init(); use ConnectAction::*; let ctx = RequestContext::test(); // WakeCold → FailNoWake (not retryable + NOT wakeable) let mechanism = TestConnectMechanism::new(vec![WakeCold, Retry, Connect]); let user_info = helper_create_connect_info(&mechanism); let cfg = config(); connect_to_compute_inner(&ctx, &mechanism, &user_info, cfg.retry, &cfg) .await .unwrap(); mechanism.verify(); }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/cache/node_info.rs
proxy/src/cache/node_info.rs
use crate::cache::common::{Cache, count_cache_insert, count_cache_outcome, eviction_listener}; use crate::cache::{Cached, ControlPlaneResult, CplaneExpiry}; use crate::config::CacheOptions; use crate::control_plane::NodeInfo; use crate::metrics::{CacheKind, Metrics}; use crate::types::EndpointCacheKey; pub(crate) struct NodeInfoCache(moka::sync::Cache<EndpointCacheKey, ControlPlaneResult<NodeInfo>>); pub(crate) type CachedNodeInfo = Cached<&'static NodeInfoCache, NodeInfo>; impl Cache for NodeInfoCache { type Key = EndpointCacheKey; type Value = ControlPlaneResult<NodeInfo>; fn invalidate(&self, info: &EndpointCacheKey) { self.0.invalidate(info); } } impl NodeInfoCache { pub fn new(config: CacheOptions) -> Self { let builder = moka::sync::Cache::builder() .name("node_info") .expire_after(CplaneExpiry::default()); let builder = config.moka(builder); if let Some(size) = config.size { Metrics::get() .cache .capacity .set(CacheKind::NodeInfo, size as i64); } let builder = builder .eviction_listener(|_k, _v, cause| eviction_listener(CacheKind::NodeInfo, cause)); Self(builder.build()) } pub fn insert(&self, key: EndpointCacheKey, value: ControlPlaneResult<NodeInfo>) { count_cache_insert(CacheKind::NodeInfo); self.0.insert(key, value); } pub fn get(&self, key: &EndpointCacheKey) -> Option<ControlPlaneResult<NodeInfo>> { count_cache_outcome(CacheKind::NodeInfo, self.0.get(key)) } pub fn get_entry( &'static self, key: &EndpointCacheKey, ) -> Option<ControlPlaneResult<CachedNodeInfo>> { self.get(key).map(|res| { res.map(|value| Cached { token: Some((self, key.clone())), value, }) }) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/cache/project_info.rs
proxy/src/cache/project_info.rs
use std::collections::HashSet; use std::convert::Infallible; use clashmap::ClashMap; use moka::sync::Cache; use tracing::{debug, info}; use crate::cache::common::{ ControlPlaneResult, CplaneExpiry, count_cache_insert, count_cache_outcome, eviction_listener, }; use crate::config::ProjectInfoCacheOptions; use crate::control_plane::messages::{ControlPlaneErrorMessage, Reason}; use crate::control_plane::{EndpointAccessControl, RoleAccessControl}; use crate::intern::{AccountIdInt, EndpointIdInt, ProjectIdInt, RoleNameInt}; use crate::metrics::{CacheKind, Metrics}; use crate::types::{EndpointId, RoleName}; /// Cache for project info. /// This is used to cache auth data for endpoints. /// Invalidation is done by console notifications or by TTL (if console notifications are disabled). /// /// We also store endpoint-to-project mapping in the cache, to be able to access per-endpoint data. /// One may ask, why the data is stored per project, when on the user request there is only data about the endpoint available? /// On the cplane side updates are done per project (or per branch), so it's easier to invalidate the whole project cache. pub struct ProjectInfoCache { role_controls: Cache<(EndpointIdInt, RoleNameInt), ControlPlaneResult<RoleAccessControl>>, ep_controls: Cache<EndpointIdInt, ControlPlaneResult<EndpointAccessControl>>, project2ep: ClashMap<ProjectIdInt, HashSet<EndpointIdInt>>, // FIXME(stefan): we need a way to GC the account2ep map. account2ep: ClashMap<AccountIdInt, HashSet<EndpointIdInt>>, config: ProjectInfoCacheOptions, } impl ProjectInfoCache { pub fn invalidate_endpoint_access(&self, endpoint_id: EndpointIdInt) { info!("invalidating endpoint access for `{endpoint_id}`"); self.ep_controls.invalidate(&endpoint_id); } pub fn invalidate_endpoint_access_for_project(&self, project_id: ProjectIdInt) { info!("invalidating endpoint access for project `{project_id}`"); let endpoints = self .project2ep .get(&project_id) .map(|kv| kv.value().clone()) .unwrap_or_default(); for endpoint_id in endpoints { self.ep_controls.invalidate(&endpoint_id); } } pub fn invalidate_endpoint_access_for_org(&self, account_id: AccountIdInt) { info!("invalidating endpoint access for org `{account_id}`"); let endpoints = self .account2ep .get(&account_id) .map(|kv| kv.value().clone()) .unwrap_or_default(); for endpoint_id in endpoints { self.ep_controls.invalidate(&endpoint_id); } } pub fn invalidate_role_secret_for_project( &self, project_id: ProjectIdInt, role_name: RoleNameInt, ) { info!( "invalidating role secret for project_id `{}` and role_name `{}`", project_id, role_name, ); let endpoints = self .project2ep .get(&project_id) .map(|kv| kv.value().clone()) .unwrap_or_default(); for endpoint_id in endpoints { self.role_controls.invalidate(&(endpoint_id, role_name)); } } } impl ProjectInfoCache { pub(crate) fn new(config: ProjectInfoCacheOptions) -> Self { Metrics::get().cache.capacity.set( CacheKind::ProjectInfoRoles, (config.size * config.max_roles) as i64, ); Metrics::get() .cache .capacity .set(CacheKind::ProjectInfoEndpoints, config.size as i64); // we cache errors for 30 seconds, unless retry_at is set. let expiry = CplaneExpiry::default(); Self { role_controls: Cache::builder() .name("project_info_roles") .eviction_listener(|_k, _v, cause| { eviction_listener(CacheKind::ProjectInfoRoles, cause); }) .max_capacity(config.size * config.max_roles) .time_to_live(config.ttl) .expire_after(expiry) .build(), ep_controls: Cache::builder() .name("project_info_endpoints") .eviction_listener(|_k, _v, cause| { eviction_listener(CacheKind::ProjectInfoEndpoints, cause); }) .max_capacity(config.size) .time_to_live(config.ttl) .expire_after(expiry) .build(), project2ep: ClashMap::new(), account2ep: ClashMap::new(), config, } } pub(crate) fn get_role_secret( &self, endpoint_id: &EndpointId, role_name: &RoleName, ) -> Option<ControlPlaneResult<RoleAccessControl>> { let endpoint_id = EndpointIdInt::get(endpoint_id)?; let role_name = RoleNameInt::get(role_name)?; count_cache_outcome( CacheKind::ProjectInfoRoles, self.role_controls.get(&(endpoint_id, role_name)), ) } pub(crate) fn get_endpoint_access( &self, endpoint_id: &EndpointId, ) -> Option<ControlPlaneResult<EndpointAccessControl>> { let endpoint_id = EndpointIdInt::get(endpoint_id)?; count_cache_outcome( CacheKind::ProjectInfoEndpoints, self.ep_controls.get(&endpoint_id), ) } pub(crate) fn insert_endpoint_access( &self, account_id: Option<AccountIdInt>, project_id: Option<ProjectIdInt>, endpoint_id: EndpointIdInt, role_name: RoleNameInt, controls: EndpointAccessControl, role_controls: RoleAccessControl, ) { if let Some(account_id) = account_id { self.insert_account2endpoint(account_id, endpoint_id); } if let Some(project_id) = project_id { self.insert_project2endpoint(project_id, endpoint_id); } debug!( key = &*endpoint_id, "created a cache entry for endpoint access" ); count_cache_insert(CacheKind::ProjectInfoEndpoints); count_cache_insert(CacheKind::ProjectInfoRoles); self.ep_controls.insert(endpoint_id, Ok(controls)); self.role_controls .insert((endpoint_id, role_name), Ok(role_controls)); } pub(crate) fn insert_endpoint_access_err( &self, endpoint_id: EndpointIdInt, role_name: RoleNameInt, msg: Box<ControlPlaneErrorMessage>, ) { debug!( key = &*endpoint_id, "created a cache entry for an endpoint access error" ); // RoleProtected is the only role-specific error that control plane can give us. // If a given role name does not exist, it still returns a successful response, // just with an empty secret. if msg.get_reason() != Reason::RoleProtected { // We can cache all the other errors in ep_controls because they don't // depend on what role name we pass to control plane. self.ep_controls .entry(endpoint_id) .and_compute_with(|entry| match entry { // leave the entry alone if it's already Ok Some(entry) if entry.value().is_ok() => moka::ops::compute::Op::Nop, // replace the entry _ => { count_cache_insert(CacheKind::ProjectInfoEndpoints); moka::ops::compute::Op::Put(Err(msg.clone())) } }); } count_cache_insert(CacheKind::ProjectInfoRoles); self.role_controls .insert((endpoint_id, role_name), Err(msg)); } fn insert_project2endpoint(&self, project_id: ProjectIdInt, endpoint_id: EndpointIdInt) { if let Some(mut endpoints) = self.project2ep.get_mut(&project_id) { endpoints.insert(endpoint_id); } else { self.project2ep .insert(project_id, HashSet::from([endpoint_id])); } } fn insert_account2endpoint(&self, account_id: AccountIdInt, endpoint_id: EndpointIdInt) { if let Some(mut endpoints) = self.account2ep.get_mut(&account_id) { endpoints.insert(endpoint_id); } else { self.account2ep .insert(account_id, HashSet::from([endpoint_id])); } } pub fn maybe_invalidate_role_secret(&self, _endpoint_id: &EndpointId, _role_name: &RoleName) { // TODO: Expire the value early if the key is idle. // Currently not an issue as we would just use the TTL to decide, which is what already happens. } pub async fn gc_worker(&self) -> anyhow::Result<Infallible> { let mut interval = tokio::time::interval(self.config.gc_interval); loop { interval.tick().await; self.ep_controls.run_pending_tasks(); self.role_controls.run_pending_tasks(); } } } #[cfg(test)] mod tests { use std::sync::Arc; use std::time::Duration; use super::*; use crate::control_plane::messages::{Details, EndpointRateLimitConfig, ErrorInfo, Status}; use crate::control_plane::{AccessBlockerFlags, AuthSecret}; use crate::scram::ServerSecret; #[tokio::test] async fn test_project_info_cache_settings() { let cache = ProjectInfoCache::new(ProjectInfoCacheOptions { size: 1, max_roles: 2, ttl: Duration::from_secs(1), gc_interval: Duration::from_secs(600), }); let project_id: Option<ProjectIdInt> = Some(ProjectIdInt::from(&"project".into())); let endpoint_id: EndpointId = "endpoint".into(); let account_id = None; let user1: RoleName = "user1".into(); let user2: RoleName = "user2".into(); let secret1 = Some(AuthSecret::Scram(ServerSecret::mock([1; 32]))); let secret2 = None; let allowed_ips = Arc::new(vec![ "127.0.0.1".parse().unwrap(), "127.0.0.2".parse().unwrap(), ]); cache.insert_endpoint_access( account_id, project_id, (&endpoint_id).into(), (&user1).into(), EndpointAccessControl { allowed_ips: allowed_ips.clone(), allowed_vpce: Arc::new(vec![]), flags: AccessBlockerFlags::default(), rate_limits: EndpointRateLimitConfig::default(), }, RoleAccessControl { secret: secret1.clone(), }, ); cache.insert_endpoint_access( account_id, project_id, (&endpoint_id).into(), (&user2).into(), EndpointAccessControl { allowed_ips: allowed_ips.clone(), allowed_vpce: Arc::new(vec![]), flags: AccessBlockerFlags::default(), rate_limits: EndpointRateLimitConfig::default(), }, RoleAccessControl { secret: secret2.clone(), }, ); let cached = cache.get_role_secret(&endpoint_id, &user1).unwrap(); assert_eq!(cached.unwrap().secret, secret1); let cached = cache.get_role_secret(&endpoint_id, &user2).unwrap(); assert_eq!(cached.unwrap().secret, secret2); // Shouldn't add more than 2 roles. let user3: RoleName = "user3".into(); let secret3 = Some(AuthSecret::Scram(ServerSecret::mock([3; 32]))); cache.role_controls.run_pending_tasks(); cache.insert_endpoint_access( account_id, project_id, (&endpoint_id).into(), (&user3).into(), EndpointAccessControl { allowed_ips: allowed_ips.clone(), allowed_vpce: Arc::new(vec![]), flags: AccessBlockerFlags::default(), rate_limits: EndpointRateLimitConfig::default(), }, RoleAccessControl { secret: secret3.clone(), }, ); cache.role_controls.run_pending_tasks(); assert_eq!(cache.role_controls.entry_count(), 2); tokio::time::sleep(Duration::from_secs(2)).await; cache.role_controls.run_pending_tasks(); assert_eq!(cache.role_controls.entry_count(), 0); } #[tokio::test] async fn test_caching_project_info_errors() { let cache = ProjectInfoCache::new(ProjectInfoCacheOptions { size: 10, max_roles: 10, ttl: Duration::from_secs(1), gc_interval: Duration::from_secs(600), }); let project_id = Some(ProjectIdInt::from(&"project".into())); let endpoint_id: EndpointId = "endpoint".into(); let account_id = None; let user1: RoleName = "user1".into(); let user2: RoleName = "user2".into(); let secret = Some(AuthSecret::Scram(ServerSecret::mock([1; 32]))); let role_msg = Box::new(ControlPlaneErrorMessage { error: "role is protected and cannot be used for password-based authentication" .to_owned() .into_boxed_str(), http_status_code: http::StatusCode::NOT_FOUND, status: Some(Status { code: "PERMISSION_DENIED".to_owned().into_boxed_str(), message: "role is protected and cannot be used for password-based authentication" .to_owned() .into_boxed_str(), details: Details { error_info: Some(ErrorInfo { reason: Reason::RoleProtected, }), retry_info: None, user_facing_message: None, }, }), }); let generic_msg = Box::new(ControlPlaneErrorMessage { error: "oh noes".to_owned().into_boxed_str(), http_status_code: http::StatusCode::NOT_FOUND, status: None, }); let get_role_secret = |endpoint_id, role_name| cache.get_role_secret(endpoint_id, role_name).unwrap(); let get_endpoint_access = |endpoint_id| cache.get_endpoint_access(endpoint_id).unwrap(); // stores role-specific errors only for get_role_secret cache.insert_endpoint_access_err((&endpoint_id).into(), (&user1).into(), role_msg.clone()); assert_eq!( get_role_secret(&endpoint_id, &user1).unwrap_err().error, role_msg.error ); assert!(cache.get_endpoint_access(&endpoint_id).is_none()); // stores non-role specific errors for both get_role_secret and get_endpoint_access cache.insert_endpoint_access_err( (&endpoint_id).into(), (&user1).into(), generic_msg.clone(), ); assert_eq!( get_role_secret(&endpoint_id, &user1).unwrap_err().error, generic_msg.error ); assert_eq!( get_endpoint_access(&endpoint_id).unwrap_err().error, generic_msg.error ); // error isn't returned for other roles in the same endpoint assert!(cache.get_role_secret(&endpoint_id, &user2).is_none()); // success for a role does not overwrite errors for other roles cache.insert_endpoint_access( account_id, project_id, (&endpoint_id).into(), (&user2).into(), EndpointAccessControl { allowed_ips: Arc::new(vec![]), allowed_vpce: Arc::new(vec![]), flags: AccessBlockerFlags::default(), rate_limits: EndpointRateLimitConfig::default(), }, RoleAccessControl { secret: secret.clone(), }, ); assert!(get_role_secret(&endpoint_id, &user1).is_err()); assert!(get_role_secret(&endpoint_id, &user2).is_ok()); // ...but does clear the access control error assert!(get_endpoint_access(&endpoint_id).is_ok()); // storing an error does not overwrite successful access control response cache.insert_endpoint_access_err( (&endpoint_id).into(), (&user2).into(), generic_msg.clone(), ); assert!(get_role_secret(&endpoint_id, &user2).is_err()); assert!(get_endpoint_access(&endpoint_id).is_ok()); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/cache/mod.rs
proxy/src/cache/mod.rs
pub(crate) mod common; pub(crate) mod node_info; pub(crate) mod project_info; pub(crate) use common::{Cached, ControlPlaneResult, CplaneExpiry};
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/cache/common.rs
proxy/src/cache/common.rs
use std::ops::{Deref, DerefMut}; use std::time::{Duration, Instant}; use moka::Expiry; use moka::notification::RemovalCause; use crate::control_plane::messages::ControlPlaneErrorMessage; use crate::metrics::{ CacheEviction, CacheKind, CacheOutcome, CacheOutcomeGroup, CacheRemovalCause, Metrics, }; /// Default TTL used when caching errors from control plane. pub const DEFAULT_ERROR_TTL: Duration = Duration::from_secs(30); /// A generic trait which exposes types of cache's key and value, /// as well as the notion of cache entry invalidation. /// This is useful for [`Cached`]. pub(crate) trait Cache { /// Entry's key. type Key; /// Entry's value. type Value; /// Invalidate an entry using a lookup info. /// We don't have an empty default impl because it's error-prone. fn invalidate(&self, _: &Self::Key); } impl<C: Cache> Cache for &C { type Key = C::Key; type Value = C::Value; fn invalidate(&self, info: &Self::Key) { C::invalidate(self, info); } } /// Wrapper for convenient entry invalidation. pub(crate) struct Cached<C: Cache, V = <C as Cache>::Value> { /// Cache + lookup info. pub(crate) token: Option<(C, C::Key)>, /// The value itself. pub(crate) value: V, } impl<C: Cache, V> Cached<C, V> { /// Place any entry into this wrapper; invalidation will be a no-op. pub(crate) fn new_uncached(value: V) -> Self { Self { token: None, value } } /// Drop this entry from a cache if it's still there. pub(crate) fn invalidate(self) -> V { if let Some((cache, info)) = &self.token { cache.invalidate(info); } self.value } /// Tell if this entry is actually cached. pub(crate) fn cached(&self) -> bool { self.token.is_some() } } impl<C: Cache, V> Deref for Cached<C, V> { type Target = V; fn deref(&self) -> &Self::Target { &self.value } } impl<C: Cache, V> DerefMut for Cached<C, V> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.value } } pub type ControlPlaneResult<T> = Result<T, Box<ControlPlaneErrorMessage>>; #[derive(Clone, Copy)] pub struct CplaneExpiry { pub error: Duration, } impl Default for CplaneExpiry { fn default() -> Self { Self { error: DEFAULT_ERROR_TTL, } } } impl CplaneExpiry { pub fn expire_early<V>( &self, value: &ControlPlaneResult<V>, updated: Instant, ) -> Option<Duration> { match value { Ok(_) => None, Err(err) => Some(self.expire_err_early(err, updated)), } } pub fn expire_err_early(&self, err: &ControlPlaneErrorMessage, updated: Instant) -> Duration { err.status .as_ref() .and_then(|s| s.details.retry_info.as_ref()) .map_or(self.error, |r| r.retry_at.into_std() - updated) } } impl<K, V> Expiry<K, ControlPlaneResult<V>> for CplaneExpiry { fn expire_after_create( &self, _key: &K, value: &ControlPlaneResult<V>, created_at: Instant, ) -> Option<Duration> { self.expire_early(value, created_at) } fn expire_after_update( &self, _key: &K, value: &ControlPlaneResult<V>, updated_at: Instant, _duration_until_expiry: Option<Duration>, ) -> Option<Duration> { self.expire_early(value, updated_at) } } pub fn eviction_listener(kind: CacheKind, cause: RemovalCause) { let cause = match cause { RemovalCause::Expired => CacheRemovalCause::Expired, RemovalCause::Explicit => CacheRemovalCause::Explicit, RemovalCause::Replaced => CacheRemovalCause::Replaced, RemovalCause::Size => CacheRemovalCause::Size, }; Metrics::get() .cache .evicted_total .inc(CacheEviction { cache: kind, cause }); } #[inline] pub fn count_cache_outcome<T>(kind: CacheKind, cache_result: Option<T>) -> Option<T> { let outcome = if cache_result.is_some() { CacheOutcome::Hit } else { CacheOutcome::Miss }; Metrics::get().cache.request_total.inc(CacheOutcomeGroup { cache: kind, outcome, }); cache_result } #[inline] pub fn count_cache_insert(kind: CacheKind) { Metrics::get().cache.inserted_total.inc(kind); }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/control_plane/errors.rs
proxy/src/control_plane/errors.rs
use std::io; use thiserror::Error; use crate::control_plane::client::ApiLockError; use crate::control_plane::messages::{self, ControlPlaneErrorMessage, Reason}; use crate::error::{ErrorKind, ReportableError, UserFacingError}; use crate::proxy::retry::CouldRetry; /// A go-to error message which doesn't leak any detail. pub(crate) const REQUEST_FAILED: &str = "Control plane request failed"; /// Common console API error. #[derive(Debug, Error)] pub(crate) enum ControlPlaneError { /// Error returned by the console itself. #[error("{REQUEST_FAILED} with {0}")] Message(Box<ControlPlaneErrorMessage>), /// Various IO errors like broken pipe or malformed payload. #[error("{REQUEST_FAILED}: {0}")] Transport(#[from] std::io::Error), } impl ControlPlaneError { /// Returns HTTP status code if it's the reason for failure. pub(crate) fn get_reason(&self) -> messages::Reason { match self { ControlPlaneError::Message(e) => e.get_reason(), ControlPlaneError::Transport(_) => messages::Reason::Unknown, } } } impl UserFacingError for ControlPlaneError { fn to_string_client(&self) -> String { match self { // To minimize risks, only select errors are forwarded to users. ControlPlaneError::Message(c) => c.get_user_facing_message(), ControlPlaneError::Transport(_) => REQUEST_FAILED.to_owned(), } } } impl ReportableError for ControlPlaneError { fn get_error_kind(&self) -> ErrorKind { match self { ControlPlaneError::Message(e) => match e.get_reason() { Reason::RoleProtected | Reason::ResourceNotFound | Reason::ProjectNotFound | Reason::EndpointNotFound | Reason::EndpointDisabled | Reason::BranchNotFound | Reason::WrongLsnOrTimestamp => ErrorKind::User, Reason::RateLimitExceeded => ErrorKind::ServiceRateLimit, Reason::NonDefaultBranchComputeTimeExceeded | Reason::ActiveTimeQuotaExceeded | Reason::ComputeTimeQuotaExceeded | Reason::WrittenDataQuotaExceeded | Reason::DataTransferQuotaExceeded | Reason::LogicalSizeQuotaExceeded | Reason::ActiveEndpointsLimitExceeded => ErrorKind::Quota, Reason::ConcurrencyLimitReached | Reason::LockAlreadyTaken | Reason::RunningOperations | Reason::EndpointIdle | Reason::ProjectUnderMaintenance | Reason::Unknown => ErrorKind::ControlPlane, }, ControlPlaneError::Transport(_) => ErrorKind::ControlPlane, } } } impl CouldRetry for ControlPlaneError { fn could_retry(&self) -> bool { match self { // retry some transport errors Self::Transport(io) => io.could_retry(), Self::Message(e) => e.could_retry(), } } } impl From<reqwest::Error> for ControlPlaneError { fn from(e: reqwest::Error) -> Self { io::Error::other(e).into() } } impl From<reqwest_middleware::Error> for ControlPlaneError { fn from(e: reqwest_middleware::Error) -> Self { io::Error::other(e).into() } } #[derive(Debug, Error)] pub(crate) enum GetAuthInfoError { // We shouldn't include the actual secret here. #[error("Console responded with a malformed auth secret")] BadSecret, #[error(transparent)] ApiError(ControlPlaneError), } // This allows more useful interactions than `#[from]`. impl<E: Into<ControlPlaneError>> From<E> for GetAuthInfoError { fn from(e: E) -> Self { Self::ApiError(e.into()) } } impl UserFacingError for GetAuthInfoError { fn to_string_client(&self) -> String { match self { // We absolutely should not leak any secrets! Self::BadSecret => REQUEST_FAILED.to_owned(), // However, API might return a meaningful error. Self::ApiError(e) => e.to_string_client(), } } } impl ReportableError for GetAuthInfoError { fn get_error_kind(&self) -> ErrorKind { match self { Self::BadSecret => ErrorKind::ControlPlane, Self::ApiError(_) => ErrorKind::ControlPlane, } } } #[derive(Debug, Error)] pub(crate) enum WakeComputeError { #[error("Console responded with a malformed compute address: {0}")] BadComputeAddress(Box<str>), #[error(transparent)] ControlPlane(ControlPlaneError), #[error("Too many connections attempts")] TooManyConnections, #[error("error acquiring resource permit: {0}")] TooManyConnectionAttempts(#[from] ApiLockError), } // This allows more useful interactions than `#[from]`. impl<E: Into<ControlPlaneError>> From<E> for WakeComputeError { fn from(e: E) -> Self { Self::ControlPlane(e.into()) } } impl UserFacingError for WakeComputeError { fn to_string_client(&self) -> String { match self { // We shouldn't show user the address even if it's broken. // Besides, user is unlikely to care about this detail. Self::BadComputeAddress(_) => REQUEST_FAILED.to_owned(), // However, control plane might return a meaningful error. Self::ControlPlane(e) => e.to_string_client(), Self::TooManyConnections => self.to_string(), Self::TooManyConnectionAttempts(_) => { "Failed to acquire permit to connect to the database. Too many database connection attempts are currently ongoing.".to_owned() } } } } impl ReportableError for WakeComputeError { fn get_error_kind(&self) -> crate::error::ErrorKind { match self { Self::BadComputeAddress(_) => crate::error::ErrorKind::ControlPlane, Self::ControlPlane(e) => e.get_error_kind(), Self::TooManyConnections => crate::error::ErrorKind::RateLimit, Self::TooManyConnectionAttempts(e) => e.get_error_kind(), } } } impl CouldRetry for WakeComputeError { fn could_retry(&self) -> bool { match self { Self::BadComputeAddress(_) => false, Self::ControlPlane(e) => e.could_retry(), Self::TooManyConnections => false, Self::TooManyConnectionAttempts(_) => false, } } } #[derive(Debug, Error)] pub enum GetEndpointJwksError { #[error("failed to build control plane request: {0}")] RequestBuild(#[source] reqwest::Error), #[error("failed to send control plane request: {0}")] RequestExecute(#[source] reqwest_middleware::Error), #[error(transparent)] ControlPlane(#[from] ControlPlaneError), #[cfg(any(test, feature = "testing"))] #[error(transparent)] TokioPostgres(#[from] tokio_postgres::Error), #[cfg(any(test, feature = "testing"))] #[error(transparent)] ParseUrl(#[from] url::ParseError), #[cfg(any(test, feature = "testing"))] #[error(transparent)] TaskJoin(#[from] tokio::task::JoinError), }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/control_plane/messages.rs
proxy/src/control_plane/messages.rs
use std::fmt::{self, Display}; use std::time::Duration; use measured::FixedCardinalityLabel; use serde::{Deserialize, Serialize}; use smol_str::SmolStr; use tokio::time::Instant; use crate::auth::IpPattern; use crate::intern::{AccountIdInt, BranchIdInt, EndpointIdInt, ProjectIdInt, RoleNameInt}; use crate::proxy::retry::CouldRetry; /// Generic error response with human-readable description. /// Note that we can't always present it to user as is. #[derive(Debug, Deserialize, Clone)] pub(crate) struct ControlPlaneErrorMessage { pub(crate) error: Box<str>, #[serde(skip)] pub(crate) http_status_code: http::StatusCode, pub(crate) status: Option<Status>, } impl ControlPlaneErrorMessage { pub(crate) fn get_reason(&self) -> Reason { self.status .as_ref() .and_then(|s| s.details.error_info.as_ref()) .map_or(Reason::Unknown, |e| e.reason) } pub(crate) fn get_user_facing_message(&self) -> String { use super::errors::REQUEST_FAILED; self.status .as_ref() .and_then(|s| s.details.user_facing_message.as_ref()) .map_or_else(|| { // Ask @neondatabase/control-plane for review before adding more. match self.http_status_code { http::StatusCode::NOT_FOUND => { // Status 404: failed to get a project-related resource. format!("{REQUEST_FAILED}: endpoint cannot be found") } http::StatusCode::NOT_ACCEPTABLE => { // Status 406: endpoint is disabled (we don't allow connections). format!("{REQUEST_FAILED}: endpoint is disabled") } http::StatusCode::LOCKED | http::StatusCode::UNPROCESSABLE_ENTITY => { // Status 423: project might be in maintenance mode (or bad state), or quotas exceeded. format!("{REQUEST_FAILED}: endpoint is temporarily unavailable. Check your quotas and/or contact our support.") } _ => REQUEST_FAILED.to_owned(), } }, |m| m.message.clone().into()) } } impl Display for ControlPlaneErrorMessage { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let msg: &str = self .status .as_ref() .and_then(|s| s.details.user_facing_message.as_ref()) .map_or_else(|| self.error.as_ref(), |m| m.message.as_ref()); write!(f, "{msg}") } } impl CouldRetry for ControlPlaneErrorMessage { fn could_retry(&self) -> bool { // If the error message does not have a status, // the error is unknown and probably should not retry automatically let Some(status) = &self.status else { return false; }; // retry if the retry info is set. if status.details.retry_info.is_some() { return true; } // if no retry info set, attempt to use the error code to guess the retry state. let reason = status .details .error_info .map_or(Reason::Unknown, |e| e.reason); reason.can_retry() } } #[derive(Debug, Deserialize, Clone)] #[allow(dead_code)] pub(crate) struct Status { pub(crate) code: Box<str>, pub(crate) message: Box<str>, pub(crate) details: Details, } #[derive(Debug, Deserialize, Clone)] pub(crate) struct Details { pub(crate) error_info: Option<ErrorInfo>, pub(crate) retry_info: Option<RetryInfo>, pub(crate) user_facing_message: Option<UserFacingMessage>, } #[derive(Copy, Clone, Debug, Deserialize)] pub(crate) struct ErrorInfo { pub(crate) reason: Reason, // Schema could also have `metadata` field, but it's not structured. Skip it for now. } #[derive(Clone, Copy, Debug, Deserialize, Default, PartialEq, Eq)] pub(crate) enum Reason { /// RoleProtected indicates that the role is protected and the attempted operation is not permitted on protected roles. #[serde(rename = "ROLE_PROTECTED")] RoleProtected, /// ResourceNotFound indicates that a resource (project, endpoint, branch, etc.) wasn't found, /// usually due to the provided ID not being correct or because the subject doesn't have enough permissions to /// access the requested resource. /// Prefer a more specific reason if possible, e.g., ProjectNotFound, EndpointNotFound, etc. #[serde(rename = "RESOURCE_NOT_FOUND")] ResourceNotFound, /// ProjectNotFound indicates that the project wasn't found, usually due to the provided ID not being correct, /// or that the subject doesn't have enough permissions to access the requested project. #[serde(rename = "PROJECT_NOT_FOUND")] ProjectNotFound, /// EndpointNotFound indicates that the endpoint wasn't found, usually due to the provided ID not being correct, /// or that the subject doesn't have enough permissions to access the requested endpoint. #[serde(rename = "ENDPOINT_NOT_FOUND")] EndpointNotFound, /// EndpointDisabled indicates that the endpoint has been disabled and does not accept connections. #[serde(rename = "ENDPOINT_DISABLED")] EndpointDisabled, /// BranchNotFound indicates that the branch wasn't found, usually due to the provided ID not being correct, /// or that the subject doesn't have enough permissions to access the requested branch. #[serde(rename = "BRANCH_NOT_FOUND")] BranchNotFound, /// WrongLsnOrTimestamp indicates that the specified LSN or timestamp are wrong. #[serde(rename = "WRONG_LSN_OR_TIMESTAMP")] WrongLsnOrTimestamp, /// RateLimitExceeded indicates that the rate limit for the operation has been exceeded. #[serde(rename = "RATE_LIMIT_EXCEEDED")] RateLimitExceeded, /// NonDefaultBranchComputeTimeExceeded indicates that the compute time quota of non-default branches has been /// exceeded. #[serde(rename = "NON_PRIMARY_BRANCH_COMPUTE_TIME_EXCEEDED")] NonDefaultBranchComputeTimeExceeded, /// ActiveTimeQuotaExceeded indicates that the active time quota was exceeded. #[serde(rename = "ACTIVE_TIME_QUOTA_EXCEEDED")] ActiveTimeQuotaExceeded, /// ComputeTimeQuotaExceeded indicates that the compute time quota was exceeded. #[serde(rename = "COMPUTE_TIME_QUOTA_EXCEEDED")] ComputeTimeQuotaExceeded, /// WrittenDataQuotaExceeded indicates that the written data quota was exceeded. #[serde(rename = "WRITTEN_DATA_QUOTA_EXCEEDED")] WrittenDataQuotaExceeded, /// DataTransferQuotaExceeded indicates that the data transfer quota was exceeded. #[serde(rename = "DATA_TRANSFER_QUOTA_EXCEEDED")] DataTransferQuotaExceeded, /// LogicalSizeQuotaExceeded indicates that the logical size quota was exceeded. #[serde(rename = "LOGICAL_SIZE_QUOTA_EXCEEDED")] LogicalSizeQuotaExceeded, /// ActiveEndpointsLimitExceeded indicates that the limit of concurrently active endpoints was exceeded. #[serde(rename = "ACTIVE_ENDPOINTS_LIMIT_EXCEEDED")] ActiveEndpointsLimitExceeded, /// RunningOperations indicates that the project already has some running operations /// and scheduling of new ones is prohibited. #[serde(rename = "RUNNING_OPERATIONS")] RunningOperations, /// ConcurrencyLimitReached indicates that the concurrency limit for an action was reached. #[serde(rename = "CONCURRENCY_LIMIT_REACHED")] ConcurrencyLimitReached, /// LockAlreadyTaken indicates that the we attempted to take a lock that was already taken. #[serde(rename = "LOCK_ALREADY_TAKEN")] LockAlreadyTaken, /// EndpointIdle indicates that the endpoint cannot become active, because it's idle. #[serde(rename = "ENDPOINT_IDLE")] EndpointIdle, /// ProjectUnderMaintenance indicates that the project is currently ongoing maintenance, /// and thus cannot accept connections. #[serde(rename = "PROJECT_UNDER_MAINTENANCE")] ProjectUnderMaintenance, #[default] #[serde(other)] Unknown, } impl Reason { pub(crate) fn is_not_found(self) -> bool { matches!( self, Reason::ResourceNotFound | Reason::ProjectNotFound | Reason::EndpointNotFound | Reason::BranchNotFound ) } pub(crate) fn can_retry(self) -> bool { match self { // do not retry role protected errors // not a transient error Reason::RoleProtected => false, // on retry, it will still not be found or valid Reason::ResourceNotFound | Reason::ProjectNotFound | Reason::EndpointNotFound | Reason::EndpointDisabled | Reason::BranchNotFound | Reason::WrongLsnOrTimestamp => false, // we were asked to go away Reason::RateLimitExceeded | Reason::NonDefaultBranchComputeTimeExceeded | Reason::ActiveTimeQuotaExceeded | Reason::ComputeTimeQuotaExceeded | Reason::WrittenDataQuotaExceeded | Reason::DataTransferQuotaExceeded | Reason::LogicalSizeQuotaExceeded | Reason::ActiveEndpointsLimitExceeded => false, // transient error. control plane is currently busy // but might be ready soon Reason::RunningOperations | Reason::ConcurrencyLimitReached | Reason::LockAlreadyTaken | Reason::EndpointIdle | Reason::ProjectUnderMaintenance => true, // unknown error. better not retry it. Reason::Unknown => false, } } } #[derive(Copy, Clone, Debug, Deserialize)] #[allow(dead_code)] pub(crate) struct RetryInfo { #[serde(rename = "retry_delay_ms", deserialize_with = "milliseconds_from_now")] pub(crate) retry_at: Instant, } fn milliseconds_from_now<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Instant, D::Error> { let millis = u64::deserialize(d)?; Ok(Instant::now() + Duration::from_millis(millis)) } #[derive(Debug, Deserialize, Clone)] pub(crate) struct UserFacingMessage { pub(crate) message: Box<str>, } /// Response which holds client's auth secret, e.g. [`crate::scram::ServerSecret`]. /// Returned by the `/get_endpoint_access_control` API method. #[derive(Deserialize)] pub(crate) struct GetEndpointAccessControl { pub(crate) role_secret: Box<str>, pub(crate) project_id: Option<ProjectIdInt>, pub(crate) account_id: Option<AccountIdInt>, pub(crate) allowed_ips: Option<Vec<IpPattern>>, pub(crate) allowed_vpc_endpoint_ids: Option<Vec<String>>, pub(crate) block_public_connections: Option<bool>, pub(crate) block_vpc_connections: Option<bool>, #[serde(default)] pub(crate) rate_limits: EndpointRateLimitConfig, } #[derive(Copy, Clone, Deserialize, Default, Debug)] pub struct EndpointRateLimitConfig { pub connection_attempts: ConnectionAttemptsLimit, } #[derive(Copy, Clone, Deserialize, Default, Debug)] pub struct ConnectionAttemptsLimit { pub tcp: Option<LeakyBucketSetting>, pub ws: Option<LeakyBucketSetting>, pub http: Option<LeakyBucketSetting>, } #[derive(Copy, Clone, Deserialize, Debug)] pub struct LeakyBucketSetting { pub rps: f64, pub burst: f64, } /// Response which holds compute node's `host:port` pair. /// Returned by the `/proxy_wake_compute` API method. #[derive(Debug, Deserialize)] pub(crate) struct WakeCompute { pub(crate) address: Box<str>, pub(crate) server_name: Option<String>, pub(crate) aux: MetricsAuxInfo, } /// Async response which concludes the console redirect auth flow. /// Also known as `kickResponse` in the console. #[derive(Debug, Deserialize)] pub(crate) struct KickSession<'a> { /// Session ID is assigned by the proxy. pub(crate) session_id: &'a str, /// Compute node connection params. #[serde(deserialize_with = "KickSession::parse_db_info")] pub(crate) result: DatabaseInfo, } impl KickSession<'_> { fn parse_db_info<'de, D>(des: D) -> Result<DatabaseInfo, D::Error> where D: serde::Deserializer<'de>, { #[derive(Deserialize)] enum Wrapper { // Currently, console only reports `Success`. // `Failure(String)` used to be here... RIP. Success(DatabaseInfo), } Wrapper::deserialize(des).map(|x| match x { Wrapper::Success(info) => info, }) } } /// Compute node connection params. #[derive(Deserialize)] pub(crate) struct DatabaseInfo { pub(crate) host: Box<str>, pub(crate) port: u16, pub(crate) dbname: Box<str>, pub(crate) user: Box<str>, /// Console always provides a password, but it might /// be inconvenient for debug with local PG instance. pub(crate) password: Option<Box<str>>, pub(crate) aux: MetricsAuxInfo, #[serde(default)] pub(crate) allowed_ips: Option<Vec<IpPattern>>, #[serde(default)] pub(crate) allowed_vpc_endpoint_ids: Option<Vec<String>>, #[serde(default)] pub(crate) public_access_allowed: Option<bool>, } // Manually implement debug to omit sensitive info. impl fmt::Debug for DatabaseInfo { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("DatabaseInfo") .field("host", &self.host) .field("port", &self.port) .field("dbname", &self.dbname) .field("user", &self.user) .field("allowed_ips", &self.allowed_ips) .field("allowed_vpc_endpoint_ids", &self.allowed_vpc_endpoint_ids) .finish_non_exhaustive() } } /// Various labels for prometheus metrics. /// Also known as `ProxyMetricsAuxInfo` in the console. #[derive(Debug, Deserialize, Clone)] pub(crate) struct MetricsAuxInfo { pub(crate) endpoint_id: EndpointIdInt, pub(crate) project_id: ProjectIdInt, pub(crate) branch_id: BranchIdInt, // note: we don't use interned strings for compute IDs. // they churn too quickly and we have no way to clean up interned strings. pub(crate) compute_id: SmolStr, #[serde(default)] pub(crate) cold_start_info: ColdStartInfo, } #[derive(Debug, Default, Serialize, Deserialize, Clone, Copy, FixedCardinalityLabel)] #[serde(rename_all = "snake_case")] pub enum ColdStartInfo { #[default] Unknown, /// Compute was already running Warm, #[serde(rename = "pool_hit")] #[label(rename = "pool_hit")] /// Compute was not running but there was an available VM VmPoolHit, #[serde(rename = "pool_miss")] #[label(rename = "pool_miss")] /// Compute was not running and there were no VMs available VmPoolMiss, // not provided by control plane /// Connection available from HTTP pool HttpPoolHit, /// Cached connection info WarmCached, } impl ColdStartInfo { pub(crate) fn as_str(self) -> &'static str { match self { ColdStartInfo::Unknown => "unknown", ColdStartInfo::Warm => "warm", ColdStartInfo::VmPoolHit => "pool_hit", ColdStartInfo::VmPoolMiss => "pool_miss", ColdStartInfo::HttpPoolHit => "http_pool_hit", ColdStartInfo::WarmCached => "warm_cached", } } } #[derive(Debug, Deserialize, Clone)] pub struct EndpointJwksResponse { pub jwks: Vec<JwksSettings>, } #[derive(Debug, Deserialize, Clone)] pub struct JwksSettings { pub id: String, pub jwks_url: url::Url, #[serde(rename = "provider_name")] pub _provider_name: String, pub jwt_audience: Option<String>, pub role_names: Vec<RoleNameInt>, } #[cfg(test)] mod tests { use serde_json::json; use super::*; fn dummy_aux() -> serde_json::Value { json!({ "endpoint_id": "endpoint", "project_id": "project", "branch_id": "branch", "compute_id": "compute", "cold_start_info": "unknown", }) } #[test] fn parse_kick_session() -> anyhow::Result<()> { // This is what the console's kickResponse looks like. let json = json!({ "session_id": "deadbeef", "result": { "Success": { "host": "localhost", "port": 5432, "dbname": "postgres", "user": "john_doe", "password": "password", "aux": dummy_aux(), } } }); serde_json::from_str::<KickSession<'_>>(&json.to_string())?; Ok(()) } #[test] fn parse_db_info() -> anyhow::Result<()> { // with password serde_json::from_value::<DatabaseInfo>(json!({ "host": "localhost", "port": 5432, "dbname": "postgres", "user": "john_doe", "password": "password", "aux": dummy_aux(), }))?; // without password serde_json::from_value::<DatabaseInfo>(json!({ "host": "localhost", "port": 5432, "dbname": "postgres", "user": "john_doe", "aux": dummy_aux(), }))?; // new field (forward compatibility) serde_json::from_value::<DatabaseInfo>(json!({ "host": "localhost", "port": 5432, "dbname": "postgres", "user": "john_doe", "project": "hello_world", "N.E.W": "forward compatibility check", "aux": dummy_aux(), }))?; // with allowed_ips let dbinfo = serde_json::from_value::<DatabaseInfo>(json!({ "host": "localhost", "port": 5432, "dbname": "postgres", "user": "john_doe", "password": "password", "aux": dummy_aux(), "allowed_ips": ["127.0.0.1"], }))?; assert_eq!( dbinfo.allowed_ips, Some(vec![IpPattern::Single("127.0.0.1".parse()?)]) ); Ok(()) } #[test] fn parse_wake_compute() -> anyhow::Result<()> { let json = json!({ "address": "0.0.0.0", "aux": dummy_aux(), }); serde_json::from_str::<WakeCompute>(&json.to_string())?; Ok(()) } #[test] fn parse_get_role_secret() -> anyhow::Result<()> { // Empty `allowed_ips` and `allowed_vpc_endpoint_ids` field. let json = json!({ "role_secret": "secret", }); serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?; let json = json!({ "role_secret": "secret", "allowed_ips": ["8.8.8.8"], }); serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?; let json = json!({ "role_secret": "secret", "allowed_vpc_endpoint_ids": ["vpce-0abcd1234567890ef"], }); serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?; let json = json!({ "role_secret": "secret", "allowed_ips": ["8.8.8.8"], "allowed_vpc_endpoint_ids": ["vpce-0abcd1234567890ef"], }); serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?; let json = json!({ "role_secret": "secret", "allowed_ips": ["8.8.8.8"], "allowed_vpc_endpoint_ids": ["vpce-0abcd1234567890ef"], "project_id": "project", }); serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?; Ok(()) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/control_plane/mgmt.rs
proxy/src/control_plane/mgmt.rs
use std::convert::Infallible; use anyhow::Context; use once_cell::sync::Lazy; use postgres_backend::{AuthType, PostgresBackend, PostgresBackendTCP, QueryError}; use pq_proto::{BeMessage, SINGLE_COL_ROWDESC}; use tokio::net::{TcpListener, TcpStream}; use tokio_util::sync::CancellationToken; use tracing::{Instrument, error, info, info_span}; use crate::control_plane::messages::{DatabaseInfo, KickSession}; use crate::waiters::{self, Waiter, Waiters}; static CPLANE_WAITERS: Lazy<Waiters<ComputeReady>> = Lazy::new(Default::default); /// Give caller an opportunity to wait for the cloud's reply. pub(crate) fn get_waiter( psql_session_id: impl Into<String>, ) -> Result<Waiter<'static, ComputeReady>, waiters::RegisterError> { CPLANE_WAITERS.register(psql_session_id.into()) } pub(crate) fn notify(psql_session_id: &str, msg: ComputeReady) -> Result<(), waiters::NotifyError> { CPLANE_WAITERS.notify(psql_session_id, msg) } /// Management API listener task. /// It spawns management response handlers needed for the console redirect auth flow. pub async fn task_main(listener: TcpListener) -> anyhow::Result<Infallible> { scopeguard::defer! { info!("mgmt has shut down"); } loop { let (socket, peer_addr) = listener.accept().await?; info!("accepted connection from {peer_addr}"); socket .set_nodelay(true) .context("failed to set client socket option")?; let span = info_span!("mgmt", peer = %peer_addr); tokio::task::spawn( async move { info!("serving a new management API connection"); // these might be long running connections, have a separate logging for cancelling // on shutdown and other ways of stopping. let cancelled = scopeguard::guard(tracing::Span::current(), |span| { let _e = span.entered(); info!("management API task cancelled"); }); if let Err(e) = handle_connection(socket).await { error!("serving failed with an error: {e}"); } else { info!("serving completed"); } // we can no longer get dropped scopeguard::ScopeGuard::into_inner(cancelled); } .instrument(span), ); } } async fn handle_connection(socket: TcpStream) -> Result<(), QueryError> { let pgbackend = PostgresBackend::new(socket, AuthType::Trust, None)?; pgbackend .run(&mut MgmtHandler, &CancellationToken::new()) .await } /// A message received by `mgmt` when a compute node is ready. pub(crate) type ComputeReady = DatabaseInfo; // TODO: replace with an http-based protocol. struct MgmtHandler; impl postgres_backend::Handler<tokio::net::TcpStream> for MgmtHandler { async fn process_query( &mut self, pgb: &mut PostgresBackendTCP, query: &str, ) -> Result<(), QueryError> { try_process_query(pgb, query).map_err(|e| { error!("failed to process response: {e:?}"); e }) } } fn try_process_query(pgb: &mut PostgresBackendTCP, query: &str) -> Result<(), QueryError> { let resp: KickSession<'_> = serde_json::from_str(query).context("Failed to parse query as json")?; let span = info_span!("event", session_id = resp.session_id); let _enter = span.enter(); info!("got response: {:?}", resp.result); match notify(resp.session_id, resp.result) { Ok(()) => { pgb.write_message_noflush(&SINGLE_COL_ROWDESC)? .write_message_noflush(&BeMessage::DataRow(&[Some(b"ok")]))? .write_message_noflush(&BeMessage::CommandComplete(b"SELECT 1"))?; } Err(e) => { error!("failed to deliver response to per-client task"); pgb.write_message_noflush(&BeMessage::ErrorResponse(&e.to_string(), None))?; } } Ok(()) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/control_plane/mod.rs
proxy/src/control_plane/mod.rs
//! Various stuff for dealing with the Neon Console. //! Later we might move some API wrappers here. /// Payloads used in the console's APIs. pub mod messages; /// Wrappers for console APIs and their mocks. pub mod client; pub(crate) mod errors; use std::sync::Arc; use messages::EndpointRateLimitConfig; use crate::auth::backend::ComputeUserInfo; use crate::auth::backend::jwt::AuthRule; use crate::auth::{AuthError, IpPattern, check_peer_addr_is_in_list}; use crate::cache::node_info::CachedNodeInfo; use crate::context::RequestContext; use crate::control_plane::messages::MetricsAuxInfo; use crate::intern::{AccountIdInt, EndpointIdInt, ProjectIdInt}; use crate::protocol2::ConnectionInfoExtra; use crate::rate_limiter::{EndpointRateLimiter, LeakyBucketConfig}; use crate::types::{EndpointId, RoleName}; use crate::{compute, scram}; /// Various cache-related types. pub mod caches { pub use super::client::ApiCaches; } /// Various cache-related types. pub mod locks { pub use super::client::ApiLocks; } /// Console's management API. pub mod mgmt; /// Auth secret which is managed by the cloud. #[derive(Clone, Eq, PartialEq, Debug)] pub(crate) enum AuthSecret { /// [SCRAM](crate::scram) authentication info. Scram(scram::ServerSecret), } #[derive(Default)] pub(crate) struct AuthInfo { pub(crate) secret: Option<AuthSecret>, /// List of IP addresses allowed for the autorization. pub(crate) allowed_ips: Vec<IpPattern>, /// List of VPC endpoints allowed for the autorization. pub(crate) allowed_vpc_endpoint_ids: Vec<String>, /// Project ID. This is used for cache invalidation. pub(crate) project_id: Option<ProjectIdInt>, /// Account ID. This is used for cache invalidation. pub(crate) account_id: Option<AccountIdInt>, /// Are public connections or VPC connections blocked? pub(crate) access_blocker_flags: AccessBlockerFlags, /// The rate limits for this endpoint. pub(crate) rate_limits: EndpointRateLimitConfig, } /// Info for establishing a connection to a compute node. #[derive(Clone)] pub(crate) struct NodeInfo { pub(crate) conn_info: compute::ConnectInfo, /// Labels for proxy's metrics. pub(crate) aux: MetricsAuxInfo, } #[derive(Copy, Clone, Default, Debug)] pub(crate) struct AccessBlockerFlags { pub public_access_blocked: bool, pub vpc_access_blocked: bool, } #[derive(Clone, Debug)] pub struct RoleAccessControl { pub secret: Option<AuthSecret>, } #[derive(Clone, Debug)] pub struct EndpointAccessControl { pub allowed_ips: Arc<Vec<IpPattern>>, pub allowed_vpce: Arc<Vec<String>>, pub flags: AccessBlockerFlags, pub rate_limits: EndpointRateLimitConfig, } impl EndpointAccessControl { pub fn check( &self, ctx: &RequestContext, check_ip_allowed: bool, check_vpc_allowed: bool, ) -> Result<(), AuthError> { if check_ip_allowed && !check_peer_addr_is_in_list(&ctx.peer_addr(), &self.allowed_ips) { return Err(AuthError::IpAddressNotAllowed(ctx.peer_addr())); } // check if a VPC endpoint ID is coming in and if yes, if it's allowed if check_vpc_allowed { if self.flags.vpc_access_blocked { return Err(AuthError::NetworkNotAllowed); } let incoming_vpc_endpoint_id = match ctx.extra() { None => return Err(AuthError::MissingVPCEndpointId), Some(ConnectionInfoExtra::Aws { vpce_id }) => vpce_id.to_string(), Some(ConnectionInfoExtra::Azure { link_id }) => link_id.to_string(), }; let vpce = &self.allowed_vpce; // TODO: For now an empty VPC endpoint ID list means all are allowed. We should replace that. if !vpce.is_empty() && !vpce.contains(&incoming_vpc_endpoint_id) { return Err(AuthError::vpc_endpoint_id_not_allowed( incoming_vpc_endpoint_id, )); } } else if self.flags.public_access_blocked { return Err(AuthError::NetworkNotAllowed); } Ok(()) } pub fn connection_attempt_rate_limit( &self, ctx: &RequestContext, endpoint: &EndpointId, rate_limiter: &EndpointRateLimiter, ) -> Result<(), AuthError> { let endpoint = EndpointIdInt::from(endpoint); let limits = &self.rate_limits.connection_attempts; let config = match ctx.protocol() { crate::metrics::Protocol::Http => limits.http, crate::metrics::Protocol::Ws => limits.ws, crate::metrics::Protocol::Tcp => limits.tcp, crate::metrics::Protocol::SniRouter => return Ok(()), }; let config = config.and_then(|config| { if config.rps <= 0.0 || config.burst <= 0.0 { return None; } Some(LeakyBucketConfig::new(config.rps, config.burst)) }); if !rate_limiter.check(endpoint, config, 1) { return Err(AuthError::too_many_connections()); } Ok(()) } } /// This will allocate per each call, but the http requests alone /// already require a few allocations, so it should be fine. pub(crate) trait ControlPlaneApi { async fn get_role_access_control( &self, ctx: &RequestContext, endpoint: &EndpointId, role: &RoleName, ) -> Result<RoleAccessControl, errors::GetAuthInfoError>; async fn get_endpoint_access_control( &self, ctx: &RequestContext, endpoint: &EndpointId, role: &RoleName, ) -> Result<EndpointAccessControl, errors::GetAuthInfoError>; async fn get_endpoint_jwks( &self, ctx: &RequestContext, endpoint: &EndpointId, ) -> Result<Vec<AuthRule>, errors::GetEndpointJwksError>; /// Wake up the compute node and return the corresponding connection info. async fn wake_compute( &self, ctx: &RequestContext, user_info: &ComputeUserInfo, ) -> Result<CachedNodeInfo, errors::WakeComputeError>; }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/control_plane/client/cplane_proxy_v1.rs
proxy/src/control_plane/client/cplane_proxy_v1.rs
//! Production console backend. use std::net::IpAddr; use std::str::FromStr; use std::sync::Arc; use ::http::HeaderName; use ::http::header::AUTHORIZATION; use bytes::Bytes; use futures::TryFutureExt; use hyper::StatusCode; use postgres_client::config::SslMode; use tokio::time::Instant; use tracing::{Instrument, debug, info, info_span, warn}; use super::super::messages::{ControlPlaneErrorMessage, GetEndpointAccessControl, WakeCompute}; use crate::auth::backend::ComputeUserInfo; use crate::auth::backend::jwt::AuthRule; use crate::cache::Cached; use crate::cache::node_info::CachedNodeInfo; use crate::context::RequestContext; use crate::control_plane::caches::ApiCaches; use crate::control_plane::errors::{ ControlPlaneError, GetAuthInfoError, GetEndpointJwksError, WakeComputeError, }; use crate::control_plane::locks::ApiLocks; use crate::control_plane::messages::{ColdStartInfo, EndpointJwksResponse}; use crate::control_plane::{ AccessBlockerFlags, AuthInfo, AuthSecret, EndpointAccessControl, NodeInfo, RoleAccessControl, }; use crate::metrics::Metrics; use crate::proxy::retry::CouldRetry; use crate::rate_limiter::WakeComputeRateLimiter; use crate::types::{EndpointCacheKey, EndpointId, RoleName}; use crate::{compute, http, scram}; pub(crate) const X_REQUEST_ID: HeaderName = HeaderName::from_static("x-request-id"); #[derive(Clone)] pub struct NeonControlPlaneClient { endpoint: http::Endpoint, pub caches: &'static ApiCaches, pub(crate) locks: &'static ApiLocks<EndpointCacheKey>, pub(crate) wake_compute_endpoint_rate_limiter: Arc<WakeComputeRateLimiter>, // put in a shared ref so we don't copy secrets all over in memory jwt: Arc<str>, } impl NeonControlPlaneClient { /// Construct an API object containing the auth parameters. pub fn new( endpoint: http::Endpoint, jwt: Arc<str>, caches: &'static ApiCaches, locks: &'static ApiLocks<EndpointCacheKey>, wake_compute_endpoint_rate_limiter: Arc<WakeComputeRateLimiter>, ) -> Self { Self { endpoint, caches, locks, wake_compute_endpoint_rate_limiter, jwt, } } pub(crate) fn url(&self) -> &str { self.endpoint.url().as_str() } async fn get_and_cache_auth_info<T>( &self, ctx: &RequestContext, endpoint: &EndpointId, role: &RoleName, cache_key: &EndpointId, extract: impl FnOnce(&EndpointAccessControl, &RoleAccessControl) -> T, ) -> Result<T, GetAuthInfoError> { match self.do_get_auth_req(ctx, endpoint, role).await { Ok(auth_info) => { let control = EndpointAccessControl { allowed_ips: Arc::new(auth_info.allowed_ips), allowed_vpce: Arc::new(auth_info.allowed_vpc_endpoint_ids), flags: auth_info.access_blocker_flags, rate_limits: auth_info.rate_limits, }; let role_control = RoleAccessControl { secret: auth_info.secret, }; let res = extract(&control, &role_control); self.caches.project_info.insert_endpoint_access( auth_info.account_id, auth_info.project_id, cache_key.into(), role.into(), control, role_control, ); if let Some(project_id) = auth_info.project_id { ctx.set_project_id(project_id); } Ok(res) } Err(err) => match err { GetAuthInfoError::ApiError(ControlPlaneError::Message(ref msg)) => { let retry_info = msg.status.as_ref().and_then(|s| s.details.retry_info); // If we can retry this error, do not cache it, // unless we were given a retry delay. if msg.could_retry() && retry_info.is_none() { return Err(err); } self.caches.project_info.insert_endpoint_access_err( cache_key.into(), role.into(), msg.clone(), ); Err(err) } err => Err(err), }, } } async fn do_get_auth_req( &self, ctx: &RequestContext, endpoint: &EndpointId, role: &RoleName, ) -> Result<AuthInfo, GetAuthInfoError> { async { let response = { let request = self .endpoint .get_path("get_endpoint_access_control") .header(X_REQUEST_ID, ctx.session_id().to_string()) .header(AUTHORIZATION, format!("Bearer {}", &self.jwt)) .query(&[("session_id", ctx.session_id())]) .query(&[ ("application_name", ctx.console_application_name().as_str()), ("endpointish", endpoint.as_str()), ("role", role.as_str()), ]) .build()?; debug!(url = request.url().as_str(), "sending http request"); let start = Instant::now(); let _pause = ctx.latency_timer_pause_at(start, crate::metrics::Waiting::Cplane); let response = self.endpoint.execute(request).await?; info!(duration = ?start.elapsed(), "received http response"); response }; let body = match parse_body::<GetEndpointAccessControl>( response.status(), response.bytes().await?, ) { Ok(body) => body, // Error 404 is special: it's ok not to have a secret. // TODO(anna): retry Err(e) => { return if e.get_reason().is_not_found() { // TODO: refactor this because it's weird // this is a failure to authenticate but we return Ok. Ok(AuthInfo::default()) } else { Err(e.into()) }; } }; let secret = if body.role_secret.is_empty() { None } else { let secret = scram::ServerSecret::parse(&body.role_secret) .map(AuthSecret::Scram) .ok_or(GetAuthInfoError::BadSecret)?; Some(secret) }; let allowed_ips = body.allowed_ips.unwrap_or_default(); Metrics::get() .proxy .allowed_ips_number .observe(allowed_ips.len() as f64); let allowed_vpc_endpoint_ids = body.allowed_vpc_endpoint_ids.unwrap_or_default(); Metrics::get() .proxy .allowed_vpc_endpoint_ids .observe(allowed_vpc_endpoint_ids.len() as f64); let block_public_connections = body.block_public_connections.unwrap_or_default(); let block_vpc_connections = body.block_vpc_connections.unwrap_or_default(); Ok(AuthInfo { secret, allowed_ips, allowed_vpc_endpoint_ids, project_id: body.project_id, account_id: body.account_id, access_blocker_flags: AccessBlockerFlags { public_access_blocked: block_public_connections, vpc_access_blocked: block_vpc_connections, }, rate_limits: body.rate_limits, }) } .inspect_err(|e| tracing::debug!(error = ?e)) .instrument(info_span!("do_get_auth_info")) .await } async fn do_get_endpoint_jwks( &self, ctx: &RequestContext, endpoint: &EndpointId, ) -> Result<Vec<AuthRule>, GetEndpointJwksError> { let request_id = ctx.session_id().to_string(); async { let request = self .endpoint .get_with_url(|url| { url.path_segments_mut() .push("endpoints") .push(endpoint.as_str()) .push("jwks"); }) .header(X_REQUEST_ID, &request_id) .header(AUTHORIZATION, format!("Bearer {}", &self.jwt)) .query(&[("session_id", ctx.session_id())]) .build() .map_err(GetEndpointJwksError::RequestBuild)?; debug!(url = request.url().as_str(), "sending http request"); let start = Instant::now(); let pause = ctx.latency_timer_pause(crate::metrics::Waiting::Cplane); let response = self .endpoint .execute(request) .await .map_err(GetEndpointJwksError::RequestExecute)?; drop(pause); info!(duration = ?start.elapsed(), "received http response"); let body = parse_body::<EndpointJwksResponse>( response.status(), response.bytes().await.map_err(ControlPlaneError::from)?, )?; let rules = body .jwks .into_iter() .map(|jwks| AuthRule { id: jwks.id, jwks_url: jwks.jwks_url, audience: jwks.jwt_audience, role_names: jwks.role_names, }) .collect(); Ok(rules) } .inspect_err(|e| tracing::debug!(error = ?e)) .instrument(info_span!("do_get_endpoint_jwks")) .await } async fn do_wake_compute( &self, ctx: &RequestContext, user_info: &ComputeUserInfo, ) -> Result<NodeInfo, WakeComputeError> { let request_id = ctx.session_id().to_string(); let application_name = ctx.console_application_name(); async { let mut request_builder = self .endpoint .get_path("wake_compute") .header("X-Request-ID", &request_id) .header("Authorization", format!("Bearer {}", &self.jwt)) .query(&[("session_id", ctx.session_id())]) .query(&[ ("application_name", application_name.as_str()), ("endpointish", user_info.endpoint.as_str()), ]); let options = user_info.options.to_deep_object(); if !options.is_empty() { request_builder = request_builder.query(&options); } let request = request_builder.build()?; debug!(url = request.url().as_str(), "sending http request"); let start = Instant::now(); let pause = ctx.latency_timer_pause(crate::metrics::Waiting::Cplane); let response = self.endpoint.execute(request).await?; drop(pause); info!(duration = ?start.elapsed(), "received http response"); let body = parse_body::<WakeCompute>(response.status(), response.bytes().await?)?; let Some((host, port)) = parse_host_port(&body.address) else { return Err(WakeComputeError::BadComputeAddress(body.address)); }; let host_addr = IpAddr::from_str(host).ok(); let ssl_mode = match &body.server_name { Some(_) => SslMode::Require, None => SslMode::Disable, }; let host = match body.server_name { Some(host) => host.into(), None => host.into(), }; let node = NodeInfo { conn_info: compute::ConnectInfo { host_addr, host, port, ssl_mode, }, aux: body.aux, }; Ok(node) } .inspect_err(|e| tracing::debug!(error = ?e)) .instrument(info_span!("do_wake_compute")) .await } } impl super::ControlPlaneApi for NeonControlPlaneClient { #[tracing::instrument(skip_all)] async fn get_role_access_control( &self, ctx: &RequestContext, endpoint: &EndpointId, role: &RoleName, ) -> Result<RoleAccessControl, GetAuthInfoError> { let key = endpoint.normalize(); if let Some(role_control) = self.caches.project_info.get_role_secret(&key, role) { return match role_control { Err(msg) => { info!(key = &*key, "found cached get_role_access_control error"); Err(GetAuthInfoError::ApiError(ControlPlaneError::Message(msg))) } Ok(role_control) => { debug!(key = &*key, "found cached role access control"); Ok(role_control) } }; } self.get_and_cache_auth_info(ctx, endpoint, role, &key, |_, role_control| { role_control.clone() }) .await } #[tracing::instrument(skip_all)] async fn get_endpoint_access_control( &self, ctx: &RequestContext, endpoint: &EndpointId, role: &RoleName, ) -> Result<EndpointAccessControl, GetAuthInfoError> { let key = endpoint.normalize(); if let Some(control) = self.caches.project_info.get_endpoint_access(&key) { return match control { Err(msg) => { info!( key = &*key, "found cached get_endpoint_access_control error" ); Err(GetAuthInfoError::ApiError(ControlPlaneError::Message(msg))) } Ok(control) => { debug!(key = &*key, "found cached endpoint access control"); Ok(control) } }; } self.get_and_cache_auth_info(ctx, endpoint, role, &key, |control, _| control.clone()) .await } #[tracing::instrument(skip_all)] async fn get_endpoint_jwks( &self, ctx: &RequestContext, endpoint: &EndpointId, ) -> Result<Vec<AuthRule>, GetEndpointJwksError> { self.do_get_endpoint_jwks(ctx, endpoint).await } #[tracing::instrument(skip_all)] async fn wake_compute( &self, ctx: &RequestContext, user_info: &ComputeUserInfo, ) -> Result<CachedNodeInfo, WakeComputeError> { let key = user_info.endpoint_cache_key(); macro_rules! check_cache { () => { if let Some(info) = self.caches.node_info.get_entry(&key) { return match info { Err(msg) => { info!(key = &*key, "found cached wake_compute error"); Err(WakeComputeError::ControlPlane(ControlPlaneError::Message( msg, ))) } Ok(info) => { debug!(key = &*key, "found cached compute node info"); ctx.set_project(info.aux.clone()); Ok(info) } }; } }; } // Every time we do a wakeup http request, the compute node will stay up // for some time (highly depends on the console's scale-to-zero policy); // The connection info remains the same during that period of time, // which means that we might cache it to reduce the load and latency. check_cache!(); let permit = self.locks.get_permit(&key).await?; // after getting back a permit - it's possible the cache was filled // double check if permit.should_check_cache() { // TODO: if there is something in the cache, mark the permit as success. check_cache!(); } // check rate limit if !self .wake_compute_endpoint_rate_limiter .check(user_info.endpoint.normalize_intern(), 1) { return Err(WakeComputeError::TooManyConnections); } let node = permit.release_result(self.do_wake_compute(ctx, user_info).await); match node { Ok(node) => { ctx.set_project(node.aux.clone()); debug!(key = &*key, "created a cache entry for woken compute node"); let mut stored_node = node.clone(); // store the cached node as 'warm_cached' stored_node.aux.cold_start_info = ColdStartInfo::WarmCached; self.caches.node_info.insert(key.clone(), Ok(stored_node)); Ok(Cached { token: Some((&self.caches.node_info, key)), value: node, }) } Err(err) => match err { WakeComputeError::ControlPlane(ControlPlaneError::Message(ref msg)) => { let retry_info = msg.status.as_ref().and_then(|s| s.details.retry_info); // If we can retry this error, do not cache it, // unless we were given a retry delay. if msg.could_retry() && retry_info.is_none() { return Err(err); } debug!( key = &*key, "created a cache entry for the wake compute error" ); self.caches.node_info.insert(key, Err(msg.clone())); Err(err) } err => Err(err), }, } } } /// Parse http response body, taking status code into account. fn parse_body<T: for<'a> serde::Deserialize<'a>>( status: StatusCode, body: Bytes, ) -> Result<T, ControlPlaneError> { if status.is_success() { // We shouldn't log raw body because it may contain secrets. info!("request succeeded, processing the body"); return Ok(serde_json::from_slice(&body).map_err(std::io::Error::other)?); } // Log plaintext to be able to detect, whether there are some cases not covered by the error struct. info!("response_error plaintext: {:?}", body); // Don't throw an error here because it's not as important // as the fact that the request itself has failed. let mut body = serde_json::from_slice(&body).unwrap_or_else(|e| { warn!("failed to parse error body: {e}"); Box::new(ControlPlaneErrorMessage { error: "reason unclear (malformed error message)".into(), http_status_code: status, status: None, }) }); body.http_status_code = status; warn!("console responded with an error ({status}): {body:?}"); Err(ControlPlaneError::Message(body)) } fn parse_host_port(input: &str) -> Option<(&str, u16)> { let (host, port) = input.rsplit_once(':')?; let ipv6_brackets: &[_] = &['[', ']']; Some((host.trim_matches(ipv6_brackets), port.parse().ok()?)) } #[cfg(test)] mod tests { use super::*; #[test] fn test_parse_host_port_v4() { let (host, port) = parse_host_port("127.0.0.1:5432").expect("failed to parse"); assert_eq!(host, "127.0.0.1"); assert_eq!(port, 5432); } #[test] fn test_parse_host_port_v6() { let (host, port) = parse_host_port("[2001:db8::1]:5432").expect("failed to parse"); assert_eq!(host, "2001:db8::1"); assert_eq!(port, 5432); } #[test] fn test_parse_host_port_url() { let (host, port) = parse_host_port("compute-foo-bar-1234.default.svc.cluster.local:5432") .expect("failed to parse"); assert_eq!(host, "compute-foo-bar-1234.default.svc.cluster.local"); assert_eq!(port, 5432); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/control_plane/client/mod.rs
proxy/src/control_plane/client/mod.rs
pub mod cplane_proxy_v1; #[cfg(any(test, feature = "testing"))] pub mod mock; use std::hash::Hash; use std::sync::Arc; use std::time::Duration; use clashmap::ClashMap; use tokio::time::Instant; use tracing::{debug, info}; use super::{EndpointAccessControl, RoleAccessControl}; use crate::auth::backend::ComputeUserInfo; use crate::auth::backend::jwt::{AuthRule, FetchAuthRules, FetchAuthRulesError}; use crate::cache::node_info::{CachedNodeInfo, NodeInfoCache}; use crate::cache::project_info::ProjectInfoCache; use crate::config::{CacheOptions, ProjectInfoCacheOptions}; use crate::context::RequestContext; use crate::control_plane::{ControlPlaneApi, errors}; use crate::error::ReportableError; use crate::metrics::ApiLockMetrics; use crate::rate_limiter::{DynamicLimiter, Outcome, RateLimiterConfig, Token}; use crate::types::EndpointId; #[non_exhaustive] #[derive(Clone)] pub enum ControlPlaneClient { /// Proxy V1 control plane API ProxyV1(cplane_proxy_v1::NeonControlPlaneClient), /// Local mock control plane. #[cfg(any(test, feature = "testing"))] PostgresMock(mock::MockControlPlane), /// Internal testing #[cfg(test)] #[allow(private_interfaces)] Test(Box<dyn TestControlPlaneClient>), } impl ControlPlaneApi for ControlPlaneClient { async fn get_role_access_control( &self, ctx: &RequestContext, endpoint: &EndpointId, role: &crate::types::RoleName, ) -> Result<RoleAccessControl, errors::GetAuthInfoError> { match self { Self::ProxyV1(api) => api.get_role_access_control(ctx, endpoint, role).await, #[cfg(any(test, feature = "testing"))] Self::PostgresMock(api) => api.get_role_access_control(ctx, endpoint, role).await, #[cfg(test)] Self::Test(_api) => { unreachable!("this function should never be called in the test backend") } } } async fn get_endpoint_access_control( &self, ctx: &RequestContext, endpoint: &EndpointId, role: &crate::types::RoleName, ) -> Result<EndpointAccessControl, errors::GetAuthInfoError> { match self { Self::ProxyV1(api) => api.get_endpoint_access_control(ctx, endpoint, role).await, #[cfg(any(test, feature = "testing"))] Self::PostgresMock(api) => api.get_endpoint_access_control(ctx, endpoint, role).await, #[cfg(test)] Self::Test(api) => api.get_access_control(), } } async fn get_endpoint_jwks( &self, ctx: &RequestContext, endpoint: &EndpointId, ) -> Result<Vec<AuthRule>, errors::GetEndpointJwksError> { match self { Self::ProxyV1(api) => api.get_endpoint_jwks(ctx, endpoint).await, #[cfg(any(test, feature = "testing"))] Self::PostgresMock(api) => api.get_endpoint_jwks(ctx, endpoint).await, #[cfg(test)] Self::Test(_api) => Ok(vec![]), } } async fn wake_compute( &self, ctx: &RequestContext, user_info: &ComputeUserInfo, ) -> Result<CachedNodeInfo, errors::WakeComputeError> { match self { Self::ProxyV1(api) => api.wake_compute(ctx, user_info).await, #[cfg(any(test, feature = "testing"))] Self::PostgresMock(api) => api.wake_compute(ctx, user_info).await, #[cfg(test)] Self::Test(api) => api.wake_compute(), } } } #[cfg(test)] pub(crate) trait TestControlPlaneClient: Send + Sync + 'static { fn wake_compute(&self) -> Result<CachedNodeInfo, errors::WakeComputeError>; fn get_access_control(&self) -> Result<EndpointAccessControl, errors::GetAuthInfoError>; fn dyn_clone(&self) -> Box<dyn TestControlPlaneClient>; } #[cfg(test)] impl Clone for Box<dyn TestControlPlaneClient> { fn clone(&self) -> Self { TestControlPlaneClient::dyn_clone(&**self) } } /// Various caches for [`control_plane`](super). pub struct ApiCaches { /// Cache for the `wake_compute` API method. pub(crate) node_info: NodeInfoCache, /// Cache which stores project_id -> endpoint_ids mapping. pub project_info: Arc<ProjectInfoCache>, } impl ApiCaches { pub fn new( wake_compute_cache_config: CacheOptions, project_info_cache_config: ProjectInfoCacheOptions, ) -> Self { Self { node_info: NodeInfoCache::new(wake_compute_cache_config), project_info: Arc::new(ProjectInfoCache::new(project_info_cache_config)), } } } /// Various caches for [`control_plane`](super). pub struct ApiLocks<K> { name: &'static str, node_locks: ClashMap<K, Arc<DynamicLimiter>>, config: RateLimiterConfig, timeout: Duration, epoch: std::time::Duration, metrics: &'static ApiLockMetrics, } #[derive(Debug, thiserror::Error)] pub(crate) enum ApiLockError { #[error("timeout acquiring resource permit")] TimeoutError(#[from] tokio::time::error::Elapsed), } impl ReportableError for ApiLockError { fn get_error_kind(&self) -> crate::error::ErrorKind { match self { ApiLockError::TimeoutError(_) => crate::error::ErrorKind::RateLimit, } } } impl<K: Hash + Eq + Clone> ApiLocks<K> { pub fn new( name: &'static str, config: RateLimiterConfig, shards: usize, timeout: Duration, epoch: std::time::Duration, metrics: &'static ApiLockMetrics, ) -> Self { Self { name, node_locks: ClashMap::with_shard_amount(shards), config, timeout, epoch, metrics, } } pub(crate) async fn get_permit(&self, key: &K) -> Result<WakeComputePermit, ApiLockError> { if self.config.initial_limit == 0 { return Ok(WakeComputePermit { permit: Token::disabled(), }); } let now = Instant::now(); let semaphore = { // get fast path if let Some(semaphore) = self.node_locks.get(key) { semaphore.clone() } else { self.node_locks .entry(key.clone()) .or_insert_with(|| { self.metrics.semaphores_registered.inc(); DynamicLimiter::new(self.config) }) .clone() } }; let permit = semaphore.acquire_timeout(self.timeout).await; self.metrics .semaphore_acquire_seconds .observe(now.elapsed().as_secs_f64()); if permit.is_ok() { debug!(elapsed = ?now.elapsed(), "acquired permit"); } else { debug!(elapsed = ?now.elapsed(), "timed out acquiring permit"); } Ok(WakeComputePermit { permit: permit? }) } pub async fn garbage_collect_worker(&self) { if self.config.initial_limit == 0 { return; } let mut interval = tokio::time::interval(self.epoch / (self.node_locks.shards().len()) as u32); loop { for (i, shard) in self.node_locks.shards().iter().enumerate() { interval.tick().await; // temporary lock a single shard and then clear any semaphores that aren't currently checked out // race conditions: if strong_count == 1, there's no way that it can increase while the shard is locked // therefore releasing it is safe from race conditions info!( name = self.name, shard = i, "performing epoch reclamation on api lock" ); let mut lock = shard.write(); let timer = self.metrics.reclamation_lag_seconds.start_timer(); let count = lock .extract_if(|(_, semaphore)| Arc::strong_count(semaphore) == 1) .count(); drop(lock); self.metrics.semaphores_unregistered.inc_by(count as u64); timer.observe(); } } } } pub(crate) struct WakeComputePermit { permit: Token, } impl WakeComputePermit { pub(crate) fn should_check_cache(&self) -> bool { !self.permit.is_disabled() } pub(crate) fn release(self, outcome: Outcome) { self.permit.release(outcome); } pub(crate) fn release_result<T, E>(self, res: Result<T, E>) -> Result<T, E> { match res { Ok(_) => self.release(Outcome::Success), Err(_) => self.release(Outcome::Overload), } res } } impl FetchAuthRules for ControlPlaneClient { async fn fetch_auth_rules( &self, ctx: &RequestContext, endpoint: EndpointId, ) -> Result<Vec<AuthRule>, FetchAuthRulesError> { self.get_endpoint_jwks(ctx, &endpoint) .await .map_err(FetchAuthRulesError::GetEndpointJwks) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/control_plane/client/mock.rs
proxy/src/control_plane/client/mock.rs
//! Mock console backend which relies on a user-provided postgres instance. use std::io; use std::net::{IpAddr, Ipv4Addr}; use std::str::FromStr; use std::sync::Arc; use futures::TryFutureExt; use postgres_client::config::SslMode; use thiserror::Error; use tokio_postgres::Client; use tracing::{Instrument, error, info, info_span, warn}; use crate::auth::IpPattern; use crate::auth::backend::ComputeUserInfo; use crate::auth::backend::jwt::AuthRule; use crate::cache::Cached; use crate::cache::node_info::CachedNodeInfo; use crate::compute::ConnectInfo; use crate::context::RequestContext; use crate::control_plane::errors::{ ControlPlaneError, GetAuthInfoError, GetEndpointJwksError, WakeComputeError, }; use crate::control_plane::messages::{EndpointRateLimitConfig, MetricsAuxInfo}; use crate::control_plane::{ AccessBlockerFlags, AuthInfo, AuthSecret, EndpointAccessControl, NodeInfo, RoleAccessControl, }; use crate::intern::RoleNameInt; use crate::scram; use crate::types::{BranchId, EndpointId, ProjectId, RoleName}; use crate::url::ApiUrl; #[derive(Debug, Error)] enum MockApiError { #[error("Failed to read password: {0}")] PasswordNotSet(tokio_postgres::Error), } impl From<MockApiError> for ControlPlaneError { fn from(e: MockApiError) -> Self { io::Error::other(e).into() } } impl From<tokio_postgres::Error> for ControlPlaneError { fn from(e: tokio_postgres::Error) -> Self { io::Error::other(e).into() } } #[derive(Clone)] pub struct MockControlPlane { endpoint: ApiUrl, ip_allowlist_check_enabled: bool, } impl MockControlPlane { pub fn new(endpoint: ApiUrl, ip_allowlist_check_enabled: bool) -> Self { Self { endpoint, ip_allowlist_check_enabled, } } pub(crate) fn url(&self) -> &str { self.endpoint.as_str() } async fn do_get_auth_info( &self, endpoint: &EndpointId, role: &RoleName, ) -> Result<AuthInfo, GetAuthInfoError> { let (secret, allowed_ips) = async { // Perhaps we could persist this connection, but then we'd have to // write more code for reopening it if it got closed, which doesn't // seem worth it. let (client, connection) = tokio_postgres::connect(self.endpoint.as_str(), tokio_postgres::NoTls).await?; tokio::spawn(connection); let secret = if let Some(entry) = get_execute_postgres_query( &client, "select rolpassword from pg_catalog.pg_authid where rolname = $1", &[&role.as_str()], "rolpassword", ) .await? { info!("got a secret: {entry}"); // safe since it's not a prod scenario scram::ServerSecret::parse(&entry).map(AuthSecret::Scram) } else { warn!("user '{role}' does not exist"); None }; let allowed_ips = if self.ip_allowlist_check_enabled { match get_execute_postgres_query( &client, "select allowed_ips from neon_control_plane.endpoints where endpoint_id = $1", &[&endpoint.as_str()], "allowed_ips", ) .await? { Some(s) => { info!("got allowed_ips: {s}"); s.split(',') .map(|s| { IpPattern::from_str(s).expect("mocked ip pattern should be correct") }) .collect() } None => vec![], } } else { vec![] }; Ok((secret, allowed_ips)) } .inspect_err(|e: &GetAuthInfoError| tracing::error!("{e}")) .instrument(info_span!("postgres", url = self.endpoint.as_str())) .await?; Ok(AuthInfo { secret, allowed_ips, allowed_vpc_endpoint_ids: vec![], project_id: None, account_id: None, access_blocker_flags: AccessBlockerFlags::default(), rate_limits: EndpointRateLimitConfig::default(), }) } async fn do_get_endpoint_jwks( &self, endpoint: &EndpointId, ) -> Result<Vec<AuthRule>, GetEndpointJwksError> { let (client, connection) = tokio_postgres::connect(self.endpoint.as_str(), tokio_postgres::NoTls).await?; let connection = tokio::spawn(connection); let res = client.query( "select id, jwks_url, audience, role_names from neon_control_plane.endpoint_jwks where endpoint_id = $1", &[&endpoint.as_str()], ) .await?; let mut rows = vec![]; for row in res { rows.push(AuthRule { id: row.get("id"), jwks_url: url::Url::parse(row.get("jwks_url"))?, audience: row.get("audience"), role_names: row .get::<_, Vec<String>>("role_names") .into_iter() .map(RoleName::from) .map(|s| RoleNameInt::from(&s)) .collect(), }); } drop(client); connection.await??; Ok(rows) } async fn do_wake_compute(&self) -> Result<NodeInfo, WakeComputeError> { let port = self.endpoint.port().unwrap_or(5432); let conn_info = match self.endpoint.host_str() { None => ConnectInfo { host_addr: Some(IpAddr::V4(Ipv4Addr::LOCALHOST)), host: "localhost".into(), port, ssl_mode: SslMode::Disable, }, Some(host) => ConnectInfo { host_addr: IpAddr::from_str(host).ok(), host: host.into(), port, ssl_mode: SslMode::Disable, }, }; let node = NodeInfo { conn_info, aux: MetricsAuxInfo { endpoint_id: (&EndpointId::from("endpoint")).into(), project_id: (&ProjectId::from("project")).into(), branch_id: (&BranchId::from("branch")).into(), compute_id: "compute".into(), cold_start_info: crate::control_plane::messages::ColdStartInfo::Warm, }, }; Ok(node) } } async fn get_execute_postgres_query( client: &Client, query: &str, params: &[&(dyn tokio_postgres::types::ToSql + Sync)], idx: &str, ) -> Result<Option<String>, GetAuthInfoError> { let rows = client.query(query, params).await?; // We can get at most one row, because `rolname` is unique. let Some(row) = rows.first() else { // This means that the user doesn't exist, so there can be no secret. // However, this is still a *valid* outcome which is very similar // to getting `404 Not found` from the Neon console. return Ok(None); }; let entry = row.try_get(idx).map_err(MockApiError::PasswordNotSet)?; Ok(Some(entry)) } impl super::ControlPlaneApi for MockControlPlane { async fn get_endpoint_access_control( &self, _ctx: &RequestContext, endpoint: &EndpointId, role: &RoleName, ) -> Result<EndpointAccessControl, GetAuthInfoError> { let info = self.do_get_auth_info(endpoint, role).await?; Ok(EndpointAccessControl { allowed_ips: Arc::new(info.allowed_ips), allowed_vpce: Arc::new(info.allowed_vpc_endpoint_ids), flags: info.access_blocker_flags, rate_limits: info.rate_limits, }) } async fn get_role_access_control( &self, _ctx: &RequestContext, endpoint: &EndpointId, role: &RoleName, ) -> Result<RoleAccessControl, GetAuthInfoError> { let info = self.do_get_auth_info(endpoint, role).await?; Ok(RoleAccessControl { secret: info.secret, }) } async fn get_endpoint_jwks( &self, _ctx: &RequestContext, endpoint: &EndpointId, ) -> Result<Vec<AuthRule>, GetEndpointJwksError> { self.do_get_endpoint_jwks(endpoint).await } #[tracing::instrument(skip_all)] async fn wake_compute( &self, _ctx: &RequestContext, _user_info: &ComputeUserInfo, ) -> Result<CachedNodeInfo, WakeComputeError> { self.do_wake_compute().map_ok(Cached::new_uncached).await } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/compute/tls.rs
proxy/src/compute/tls.rs
use futures::FutureExt; use postgres_client::config::SslMode; use postgres_client::maybe_tls_stream::MaybeTlsStream; use postgres_client::tls::{MakeTlsConnect, TlsConnect}; use rustls::pki_types::InvalidDnsNameError; use thiserror::Error; use tokio::io::{AsyncRead, AsyncWrite}; use crate::pqproto::request_tls; use crate::proxy::connect_compute::TlsNegotiation; use crate::proxy::retry::CouldRetry; #[derive(Debug, Error)] pub enum TlsError { #[error(transparent)] Dns(#[from] InvalidDnsNameError), #[error(transparent)] Connection(#[from] std::io::Error), #[error("TLS required but not provided")] Required, } impl CouldRetry for TlsError { fn could_retry(&self) -> bool { match self { TlsError::Dns(_) => false, TlsError::Connection(err) => err.could_retry(), // perhaps compute didn't realise it supports TLS? TlsError::Required => true, } } } pub async fn connect_tls<S, T>( mut stream: S, mode: SslMode, tls: &T, host: &str, negotiation: TlsNegotiation, ) -> Result<MaybeTlsStream<S, T::Stream>, TlsError> where S: AsyncRead + AsyncWrite + Unpin + Send, T: MakeTlsConnect< S, Error = InvalidDnsNameError, TlsConnect: TlsConnect<S, Error = std::io::Error, Future: Send>, >, { match mode { SslMode::Disable => return Ok(MaybeTlsStream::Raw(stream)), SslMode::Prefer | SslMode::Require => {} } match negotiation { // No TLS request needed TlsNegotiation::Direct => {} // TLS request successful TlsNegotiation::Postgres if request_tls(&mut stream).await? => {} // TLS request failed but is required TlsNegotiation::Postgres if SslMode::Require == mode => return Err(TlsError::Required), // TLS request failed but is not required TlsNegotiation::Postgres => return Ok(MaybeTlsStream::Raw(stream)), } Ok(MaybeTlsStream::Tls( tls.make_tls_connect(host)?.connect(stream).boxed().await?, )) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/compute/mod.rs
proxy/src/compute/mod.rs
mod tls; use std::fmt::Debug; use std::io; use std::net::{IpAddr, SocketAddr}; use futures::{FutureExt, TryFutureExt}; use itertools::Itertools; use postgres_client::config::{AuthKeys, ChannelBinding, SslMode}; use postgres_client::connect_raw::StartupStream; use postgres_client::error::SqlState; use postgres_client::maybe_tls_stream::MaybeTlsStream; use postgres_client::tls::MakeTlsConnect; use thiserror::Error; use tokio::net::{TcpStream, lookup_host}; use tracing::{debug, error, info, warn}; use crate::auth::backend::ComputeCredentialKeys; use crate::auth::parse_endpoint_param; use crate::compute::tls::TlsError; use crate::config::ComputeConfig; use crate::context::RequestContext; use crate::control_plane::client::ApiLockError; use crate::control_plane::errors::WakeComputeError; use crate::control_plane::messages::MetricsAuxInfo; use crate::error::{ErrorKind, ReportableError, UserFacingError}; use crate::metrics::{Metrics, NumDbConnectionsGuard}; use crate::pqproto::StartupMessageParams; use crate::proxy::connect_compute::TlsNegotiation; use crate::proxy::neon_option; use crate::types::Host; pub const COULD_NOT_CONNECT: &str = "Couldn't connect to compute node"; #[derive(Debug, Error)] pub(crate) enum PostgresError { /// This error doesn't seem to reveal any secrets; for instance, /// `postgres_client::error::Kind` doesn't contain ip addresses and such. #[error("{COULD_NOT_CONNECT}: {0}")] Postgres(#[from] postgres_client::Error), } impl UserFacingError for PostgresError { fn to_string_client(&self) -> String { match self { // This helps us drop irrelevant library-specific prefixes. // TODO: propagate severity level and other parameters. PostgresError::Postgres(err) => match err.as_db_error() { Some(err) => { let msg = err.message(); if msg.starts_with("unsupported startup parameter: ") || msg.starts_with("unsupported startup parameter in options: ") { format!( "{msg}. Please use unpooled connection or remove this parameter from the startup package. More details: https://neon.tech/docs/connect/connection-errors#unsupported-startup-parameter" ) } else { msg.to_owned() } } None => err.to_string(), }, } } } impl ReportableError for PostgresError { fn get_error_kind(&self) -> ErrorKind { match self { PostgresError::Postgres(err) => match err.as_db_error() { Some(err) if err.code() == &SqlState::INVALID_CATALOG_NAME => ErrorKind::User, Some(_) => ErrorKind::Postgres, None => ErrorKind::Compute, }, } } } #[derive(Debug, Error)] pub(crate) enum ConnectionError { #[error("{COULD_NOT_CONNECT}: {0}")] TlsError(#[from] TlsError), #[error("{COULD_NOT_CONNECT}: {0}")] WakeComputeError(#[from] WakeComputeError), #[error("error acquiring resource permit: {0}")] TooManyConnectionAttempts(#[from] ApiLockError), #[cfg(test)] #[error("retryable: {retryable}, wakeable: {wakeable}, kind: {kind:?}")] TestError { retryable: bool, wakeable: bool, kind: crate::error::ErrorKind, }, } impl UserFacingError for ConnectionError { fn to_string_client(&self) -> String { match self { ConnectionError::WakeComputeError(err) => err.to_string_client(), ConnectionError::TooManyConnectionAttempts(_) => { "Failed to acquire permit to connect to the database. Too many database connection attempts are currently ongoing.".to_owned() } ConnectionError::TlsError(_) => COULD_NOT_CONNECT.to_owned(), #[cfg(test)] ConnectionError::TestError { .. } => self.to_string(), } } } impl ReportableError for ConnectionError { fn get_error_kind(&self) -> ErrorKind { match self { ConnectionError::TlsError(_) => ErrorKind::Compute, ConnectionError::WakeComputeError(e) => e.get_error_kind(), ConnectionError::TooManyConnectionAttempts(e) => e.get_error_kind(), #[cfg(test)] ConnectionError::TestError { kind, .. } => *kind, } } } /// A pair of `ClientKey` & `ServerKey` for `SCRAM-SHA-256`. pub(crate) type ScramKeys = postgres_client::config::ScramKeys<32>; #[derive(Clone)] pub enum Auth { /// Only used during console-redirect. Password(Vec<u8>), /// Used by sql-over-http, ws, tcp. Scram(Box<ScramKeys>), } /// A config for authenticating to the compute node. pub(crate) struct AuthInfo { /// None for local-proxy, as we use trust-based localhost auth. /// Some for sql-over-http, ws, tcp, and in most cases for console-redirect. /// Might be None for console-redirect, but that's only a consequence of testing environments ATM. auth: Option<Auth>, server_params: StartupMessageParams, channel_binding: ChannelBinding, /// Console redirect sets user and database, we shouldn't re-use those from the params. skip_db_user: bool, } /// Contains only the data needed to establish a secure connection to compute. #[derive(Clone)] pub struct ConnectInfo { pub host_addr: Option<IpAddr>, pub host: Host, pub port: u16, pub ssl_mode: SslMode, } /// Creation and initialization routines. impl AuthInfo { pub(crate) fn for_console_redirect(db: &str, user: &str, pw: Option<&str>) -> Self { let mut server_params = StartupMessageParams::default(); server_params.insert("database", db); server_params.insert("user", user); Self { auth: pw.map(|pw| Auth::Password(pw.as_bytes().to_owned())), server_params, skip_db_user: true, // pg-sni-router is a mitm so this would fail. channel_binding: ChannelBinding::Disable, } } pub(crate) fn with_auth_keys(keys: ComputeCredentialKeys) -> Self { Self { auth: match keys { ComputeCredentialKeys::AuthKeys(AuthKeys::ScramSha256(auth_keys)) => { Some(Auth::Scram(Box::new(auth_keys))) } ComputeCredentialKeys::JwtPayload(_) => None, }, server_params: StartupMessageParams::default(), skip_db_user: false, channel_binding: ChannelBinding::Prefer, } } } impl ConnectInfo { pub fn to_postgres_client_config(&self) -> postgres_client::Config { let mut config = postgres_client::Config::new(self.host.to_string(), self.port); config.ssl_mode(self.ssl_mode); if let Some(host_addr) = self.host_addr { config.set_host_addr(host_addr); } config } } impl AuthInfo { fn enrich(&self, mut config: postgres_client::Config) -> postgres_client::Config { match &self.auth { Some(Auth::Scram(keys)) => config.auth_keys(AuthKeys::ScramSha256(**keys)), Some(Auth::Password(pw)) => config.password(pw), None => &mut config, }; config.channel_binding(self.channel_binding); for (k, v) in self.server_params.iter() { config.set_param(k, v); } config } /// Apply startup message params to the connection config. pub(crate) fn set_startup_params( &mut self, params: &StartupMessageParams, arbitrary_params: bool, ) { if !arbitrary_params { self.server_params.insert("client_encoding", "UTF8"); } for (k, v) in params.iter() { match k { // Only set `user` if it's not present in the config. // Console redirect auth flow takes username from the console's response. "user" | "database" if self.skip_db_user => {} "options" => { if let Some(options) = filtered_options(v) { self.server_params.insert(k, &options); } } "user" | "database" | "application_name" | "replication" => { self.server_params.insert(k, v); } // if we allow arbitrary params, then we forward them through. // this is a flag for a period of backwards compatibility k if arbitrary_params => { self.server_params.insert(k, v); } _ => {} } } } pub async fn authenticate( &self, ctx: &RequestContext, compute: &mut ComputeConnection, ) -> Result<(), PostgresError> { // client config with stubbed connect info. // TODO(conrad): should we rewrite this to bypass tokio-postgres2 entirely, // utilising pqproto.rs. let mut tmp_config = postgres_client::Config::new(String::new(), 0); // We have already established SSL if necessary. tmp_config.ssl_mode(SslMode::Disable); let tmp_config = self.enrich(tmp_config); let pause = ctx.latency_timer_pause(crate::metrics::Waiting::Compute); tmp_config.authenticate(&mut compute.stream).await?; drop(pause); Ok(()) } } impl ConnectInfo { /// Establish a raw TCP+TLS connection to the compute node. async fn connect_raw( &self, config: &ComputeConfig, tls: TlsNegotiation, ) -> Result<(SocketAddr, MaybeTlsStream<TcpStream, RustlsStream>), TlsError> { let timeout = config.timeout; // wrap TcpStream::connect with timeout let connect_with_timeout = |addrs| { tokio::time::timeout(timeout, TcpStream::connect(addrs)).map(move |res| match res { Ok(tcpstream_connect_res) => tcpstream_connect_res, Err(_) => Err(io::Error::new( io::ErrorKind::TimedOut, format!("exceeded connection timeout {timeout:?}"), )), }) }; let connect_once = |addrs| { debug!("trying to connect to compute node at {addrs:?}"); connect_with_timeout(addrs).and_then(|stream| async { let socket_addr = stream.peer_addr()?; let socket = socket2::SockRef::from(&stream); // Disable Nagle's algorithm to not introduce latency between // client and compute. socket.set_nodelay(true)?; // This prevents load balancer from severing the connection. socket.set_keepalive(true)?; Ok((socket_addr, stream)) }) }; // We can't reuse connection establishing logic from `postgres_client` here, // because it has no means for extracting the underlying socket which we // require for our business. let port = self.port; let host = &*self.host; let addrs = match self.host_addr { Some(addr) => vec![SocketAddr::new(addr, port)], None => lookup_host((host, port)).await?.collect(), }; match connect_once(&*addrs).await { Ok((sockaddr, stream)) => Ok(( sockaddr, tls::connect_tls(stream, self.ssl_mode, config, host, tls).await?, )), Err(err) => { warn!("couldn't connect to compute node at {host}:{port}: {err}"); Err(TlsError::Connection(err)) } } } } pub type RustlsStream = <ComputeConfig as MakeTlsConnect<tokio::net::TcpStream>>::Stream; pub type MaybeRustlsStream = MaybeTlsStream<tokio::net::TcpStream, RustlsStream>; pub struct ComputeConnection { /// Socket connected to a compute node. pub stream: StartupStream<tokio::net::TcpStream, RustlsStream>, /// Labels for proxy's metrics. pub aux: MetricsAuxInfo, pub hostname: Host, pub ssl_mode: SslMode, pub socket_addr: SocketAddr, pub guage: NumDbConnectionsGuard<'static>, } impl ConnectInfo { /// Connect to a corresponding compute node. pub async fn connect( &self, ctx: &RequestContext, aux: &MetricsAuxInfo, config: &ComputeConfig, tls: TlsNegotiation, ) -> Result<ComputeConnection, ConnectionError> { let pause = ctx.latency_timer_pause(crate::metrics::Waiting::Compute); let (socket_addr, stream) = self.connect_raw(config, tls).await?; drop(pause); tracing::Span::current().record("compute_id", tracing::field::display(&aux.compute_id)); // TODO: lots of useful info but maybe we can move it elsewhere (eg traces?) info!( cold_start_info = ctx.cold_start_info().as_str(), "connected to compute node at {} ({socket_addr}) sslmode={:?}, latency={}, query_id={}", self.host, self.ssl_mode, ctx.get_proxy_latency(), ctx.get_testodrome_id().unwrap_or_default(), ); let stream = StartupStream::new(stream); let connection = ComputeConnection { stream, socket_addr, hostname: self.host.clone(), ssl_mode: self.ssl_mode, aux: aux.clone(), guage: Metrics::get().proxy.db_connections.guard(ctx.protocol()), }; Ok(connection) } } /// Retrieve `options` from a startup message, dropping all proxy-secific flags. fn filtered_options(options: &str) -> Option<String> { #[allow(unstable_name_collisions)] let options: String = StartupMessageParams::parse_options_raw(options) .filter(|opt| parse_endpoint_param(opt).is_none() && neon_option(opt).is_none()) .intersperse(" ") // TODO: use impl from std once it's stabilized .collect(); // Don't even bother with empty options. if options.is_empty() { return None; } Some(options) } #[cfg(test)] mod tests { use super::*; #[test] fn test_filtered_options() { // Empty options is unlikely to be useful anyway. let params = ""; assert_eq!(filtered_options(params), None); // It's likely that clients will only use options to specify endpoint/project. let params = "project=foo"; assert_eq!(filtered_options(params), None); // Same, because unescaped whitespaces are no-op. let params = " project=foo "; assert_eq!(filtered_options(params).as_deref(), None); let params = r"\ project=foo \ "; assert_eq!(filtered_options(params).as_deref(), Some(r"\ \ ")); let params = "project = foo"; assert_eq!(filtered_options(params).as_deref(), Some("project = foo")); let params = "project = foo neon_endpoint_type:read_write neon_lsn:0/2 neon_proxy_params_compat:true"; assert_eq!(filtered_options(params).as_deref(), Some("project = foo")); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/redis/kv_ops.rs
proxy/src/redis/kv_ops.rs
use std::time::Duration; use futures::FutureExt; use redis::aio::ConnectionLike; use redis::{Cmd, FromRedisValue, Pipeline, RedisError, RedisResult}; use super::connection_with_credentials_provider::ConnectionWithCredentialsProvider; use crate::redis::connection_with_credentials_provider::ConnectionProviderError; #[derive(thiserror::Error, Debug)] pub enum RedisKVClientError { #[error(transparent)] Redis(#[from] RedisError), #[error(transparent)] ConnectionProvider(#[from] ConnectionProviderError), } pub struct RedisKVClient { client: ConnectionWithCredentialsProvider, } #[allow(async_fn_in_trait)] pub trait Queryable { async fn query<T: FromRedisValue>(&self, conn: &mut impl ConnectionLike) -> RedisResult<T>; } impl Queryable for Pipeline { async fn query<T: FromRedisValue>(&self, conn: &mut impl ConnectionLike) -> RedisResult<T> { self.query_async(conn).await } } impl Queryable for Cmd { async fn query<T: FromRedisValue>(&self, conn: &mut impl ConnectionLike) -> RedisResult<T> { self.query_async(conn).await } } impl RedisKVClient { pub fn new(client: ConnectionWithCredentialsProvider) -> Self { Self { client } } pub async fn try_connect(&mut self) -> Result<(), RedisKVClientError> { self.client .connect() .boxed() .await .inspect_err(|e| tracing::error!("failed to connect to redis: {e}")) .map_err(Into::into) } pub(crate) fn credentials_refreshed(&self) -> bool { self.client.credentials_refreshed() } pub(crate) async fn query<T: FromRedisValue>( &mut self, q: &impl Queryable, ) -> Result<T, RedisKVClientError> { let e = match q.query(&mut self.client).await { Ok(t) => return Ok(t), Err(e) => e, }; tracing::debug!("failed to run query: {e}"); match e.retry_method() { redis::RetryMethod::Reconnect => { tracing::info!("Redis client is disconnected. Reconnecting..."); self.try_connect().await?; } redis::RetryMethod::RetryImmediately => {} redis::RetryMethod::WaitAndRetry => { // somewhat arbitrary. tokio::time::sleep(Duration::from_millis(100)).await; } _ => Err(e)?, } Ok(q.query(&mut self.client).await?) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/redis/elasticache.rs
proxy/src/redis/elasticache.rs
use std::sync::Arc; use std::time::{Duration, SystemTime}; use aws_config::Region; use aws_config::environment::EnvironmentVariableCredentialsProvider; use aws_config::imds::credentials::ImdsCredentialsProvider; use aws_config::meta::credentials::CredentialsProviderChain; use aws_config::meta::region::RegionProviderChain; use aws_config::profile::ProfileFileCredentialsProvider; use aws_config::provider_config::ProviderConfig; use aws_config::web_identity_token::WebIdentityTokenCredentialsProvider; use aws_credential_types::provider::error::CredentialsError; use aws_sdk_iam::config::ProvideCredentials; use aws_sigv4::http_request::{ self, SignableBody, SignableRequest, SignatureLocation, SigningError, SigningSettings, }; use aws_sigv4::sign::v4::signing_params::BuildError; use tracing::info; #[derive(Debug)] pub struct AWSIRSAConfig { region: String, service_name: String, cluster_name: String, user_id: String, token_ttl: Duration, action: String, } impl AWSIRSAConfig { pub fn new(region: String, cluster_name: Option<String>, user_id: Option<String>) -> Self { AWSIRSAConfig { region, service_name: "elasticache".to_string(), cluster_name: cluster_name.unwrap_or_default(), user_id: user_id.unwrap_or_default(), // "The IAM authentication token is valid for 15 minutes" // https://docs.aws.amazon.com/memorydb/latest/devguide/auth-iam.html#auth-iam-limits token_ttl: Duration::from_secs(15 * 60), action: "connect".to_string(), } } } #[derive(thiserror::Error, Debug)] pub enum CredentialsProviderError { #[error(transparent)] AwsCredentials(#[from] CredentialsError), #[error(transparent)] AwsSigv4Build(#[from] BuildError), #[error(transparent)] AwsSigv4Singing(#[from] SigningError), #[error(transparent)] Http(#[from] http::Error), } /// Credentials provider for AWS elasticache authentication. /// /// Official documentation: /// <https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/auth-iam.html> /// /// Useful resources: /// <https://aws.amazon.com/blogs/database/simplify-managing-access-to-amazon-elasticache-for-redis-clusters-with-iam/> pub struct CredentialsProvider { config: AWSIRSAConfig, credentials_provider: CredentialsProviderChain, } impl CredentialsProvider { pub async fn new( aws_region: String, redis_cluster_name: Option<String>, redis_user_id: Option<String>, ) -> Arc<CredentialsProvider> { let region_provider = RegionProviderChain::default_provider().or_else(Region::new(aws_region.clone())); let provider_conf = ProviderConfig::without_region().with_region(region_provider.region().await); let aws_credentials_provider = { // uses "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY" CredentialsProviderChain::first_try( "env", EnvironmentVariableCredentialsProvider::new(), ) // uses "AWS_PROFILE" / `aws sso login --profile <profile>` .or_else( "profile-sso", ProfileFileCredentialsProvider::builder() .configure(&provider_conf) .build(), ) // uses "AWS_WEB_IDENTITY_TOKEN_FILE", "AWS_ROLE_ARN", "AWS_ROLE_SESSION_NAME" // needed to access remote extensions bucket .or_else( "token", WebIdentityTokenCredentialsProvider::builder() .configure(&provider_conf) .build(), ) // uses imds v2 .or_else("imds", ImdsCredentialsProvider::builder().build()) }; Arc::new(CredentialsProvider { config: AWSIRSAConfig::new(aws_region, redis_cluster_name, redis_user_id), credentials_provider: aws_credentials_provider, }) } pub(crate) async fn provide_credentials( &self, ) -> Result<(String, String), CredentialsProviderError> { let aws_credentials = self .credentials_provider .provide_credentials() .await? .into(); info!("AWS credentials successfully obtained"); info!("Connecting to Redis with configuration: {:?}", self.config); let mut settings = SigningSettings::default(); settings.signature_location = SignatureLocation::QueryParams; settings.expires_in = Some(self.config.token_ttl); let signing_params = aws_sigv4::sign::v4::SigningParams::builder() .identity(&aws_credentials) .region(&self.config.region) .name(&self.config.service_name) .time(SystemTime::now()) .settings(settings) .build()? .into(); let auth_params = [ ("Action", &self.config.action), ("User", &self.config.user_id), ]; let auth_params = url::form_urlencoded::Serializer::new(String::new()) .extend_pairs(auth_params) .finish(); let auth_uri = http::Uri::builder() .scheme("http") .authority(self.config.cluster_name.as_bytes()) .path_and_query(format!("/?{auth_params}")) .build()?; info!("{}", auth_uri); // Convert the HTTP request into a signable request let signable_request = SignableRequest::new( "GET", auth_uri.to_string(), std::iter::empty(), SignableBody::Bytes(&[]), )?; // Sign and then apply the signature to the request let (si, _) = http_request::sign(signable_request, &signing_params)?.into_parts(); let mut signable_request = http::Request::builder() .method("GET") .uri(auth_uri) .body(())?; si.apply_to_request_http1x(&mut signable_request); Ok(( self.config.user_id.clone(), signable_request .uri() .to_string() .replacen("http://", "", 1), )) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/redis/connection_with_credentials_provider.rs
proxy/src/redis/connection_with_credentials_provider.rs
use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use futures::FutureExt; use redis::aio::{ConnectionLike, MultiplexedConnection}; use redis::{ConnectionInfo, IntoConnectionInfo, RedisConnectionInfo, RedisError, RedisResult}; use tokio::task::AbortHandle; use tracing::{error, info, warn}; use super::elasticache::CredentialsProvider; use crate::redis::elasticache::CredentialsProviderError; enum Credentials { Static(ConnectionInfo), Dynamic(Arc<CredentialsProvider>, redis::ConnectionAddr), } impl Clone for Credentials { fn clone(&self) -> Self { match self { Credentials::Static(info) => Credentials::Static(info.clone()), Credentials::Dynamic(provider, addr) => { Credentials::Dynamic(Arc::clone(provider), addr.clone()) } } } } #[derive(thiserror::Error, Debug)] pub enum ConnectionProviderError { #[error(transparent)] Redis(#[from] RedisError), #[error(transparent)] CredentialsProvider(#[from] CredentialsProviderError), } /// A wrapper around `redis::MultiplexedConnection` that automatically refreshes the token. /// Provides PubSub connection without credentials refresh. pub struct ConnectionWithCredentialsProvider { credentials: Credentials, // TODO: with more load on the connection, we should consider using a connection pool con: Option<MultiplexedConnection>, refresh_token_task: Option<AbortHandle>, mutex: tokio::sync::Mutex<()>, credentials_refreshed: Arc<AtomicBool>, } impl Clone for ConnectionWithCredentialsProvider { fn clone(&self) -> Self { Self { credentials: self.credentials.clone(), con: None, refresh_token_task: None, mutex: tokio::sync::Mutex::new(()), credentials_refreshed: Arc::new(AtomicBool::new(false)), } } } impl ConnectionWithCredentialsProvider { pub fn new_with_credentials_provider( host: String, port: u16, credentials_provider: Arc<CredentialsProvider>, ) -> Self { Self { credentials: Credentials::Dynamic( credentials_provider, redis::ConnectionAddr::TcpTls { host, port, insecure: false, tls_params: None, }, ), con: None, refresh_token_task: None, mutex: tokio::sync::Mutex::new(()), credentials_refreshed: Arc::new(AtomicBool::new(false)), } } pub fn new_with_static_credentials<T: IntoConnectionInfo>(params: T) -> Self { Self { credentials: Credentials::Static( params .into_connection_info() .expect("static configured redis credentials should be a valid format"), ), con: None, refresh_token_task: None, mutex: tokio::sync::Mutex::new(()), credentials_refreshed: Arc::new(AtomicBool::new(true)), } } async fn ping(con: &mut MultiplexedConnection) -> Result<(), ConnectionProviderError> { redis::cmd("PING") .query_async(con) .await .map_err(Into::into) } pub(crate) fn credentials_refreshed(&self) -> bool { self.credentials_refreshed.load(Ordering::Relaxed) } pub(crate) async fn connect(&mut self) -> Result<(), ConnectionProviderError> { let _guard = self.mutex.lock().await; if let Some(con) = self.con.as_mut() { match Self::ping(con).await { Ok(()) => { return Ok(()); } Err(e) => { warn!("Error during PING: {e:?}"); } } } else { info!("Connection is not established"); } info!("Establishing a new connection..."); self.con = None; if let Some(f) = self.refresh_token_task.take() { f.abort(); } let mut con = self .get_client() .await? .get_multiplexed_tokio_connection() .await?; if let Credentials::Dynamic(credentials_provider, _) = &self.credentials { let credentials_provider = credentials_provider.clone(); let con2 = con.clone(); let credentials_refreshed = self.credentials_refreshed.clone(); let f = tokio::spawn(Self::keep_connection( con2, credentials_provider, credentials_refreshed, )); self.refresh_token_task = Some(f.abort_handle()); } match Self::ping(&mut con).await { Ok(()) => { info!("Connection succesfully established"); } Err(e) => { warn!("Connection is broken. Error during PING: {e:?}"); } } self.con = Some(con); Ok(()) } async fn get_connection_info(&self) -> Result<ConnectionInfo, ConnectionProviderError> { match &self.credentials { Credentials::Static(info) => Ok(info.clone()), Credentials::Dynamic(provider, addr) => { let (username, password) = provider.provide_credentials().await?; Ok(ConnectionInfo { addr: addr.clone(), redis: RedisConnectionInfo { db: 0, username: Some(username), password: Some(password.clone()), // TODO: switch to RESP3 after testing new client version. protocol: redis::ProtocolVersion::RESP2, }, }) } } } async fn get_client(&self) -> Result<redis::Client, ConnectionProviderError> { let client = redis::Client::open(self.get_connection_info().await?)?; self.credentials_refreshed.store(true, Ordering::Relaxed); Ok(client) } // PubSub does not support credentials refresh. // Requires manual reconnection every 12h. pub(crate) async fn get_async_pubsub(&self) -> anyhow::Result<redis::aio::PubSub> { Ok(self.get_client().await?.get_async_pubsub().await?) } // The connection lives for 12h. // It can be prolonged with sending `AUTH` commands with the refreshed token. // https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/auth-iam.html#auth-iam-limits async fn keep_connection( mut con: MultiplexedConnection, credentials_provider: Arc<CredentialsProvider>, credentials_refreshed: Arc<AtomicBool>, ) -> ! { loop { // The connection lives for 12h, for the sanity check we refresh it every hour. tokio::time::sleep(Duration::from_secs(60 * 60)).await; match Self::refresh_token(&mut con, credentials_provider.clone()).await { Ok(()) => { info!("Token refreshed"); credentials_refreshed.store(true, Ordering::Relaxed); } Err(e) => { error!("Error during token refresh: {e:?}"); credentials_refreshed.store(false, Ordering::Relaxed); } } } } async fn refresh_token( con: &mut MultiplexedConnection, credentials_provider: Arc<CredentialsProvider>, ) -> anyhow::Result<()> { let (user, password) = credentials_provider.provide_credentials().await?; let _: () = redis::cmd("AUTH") .arg(user) .arg(password) .query_async(con) .await?; Ok(()) } /// Sends an already encoded (packed) command into the TCP socket and /// reads the single response from it. pub(crate) async fn send_packed_command( &mut self, cmd: &redis::Cmd, ) -> RedisResult<redis::Value> { // Clone connection to avoid having to lock the ArcSwap in write mode let con = self.con.as_mut().ok_or(redis::RedisError::from(( redis::ErrorKind::IoError, "Connection not established", )))?; con.send_packed_command(cmd).await } /// Sends multiple already encoded (packed) command into the TCP socket /// and reads `count` responses from it. This is used to implement /// pipelining. pub(crate) async fn send_packed_commands( &mut self, cmd: &redis::Pipeline, offset: usize, count: usize, ) -> RedisResult<Vec<redis::Value>> { // Clone shared connection future to avoid having to lock the ArcSwap in write mode let con = self.con.as_mut().ok_or(redis::RedisError::from(( redis::ErrorKind::IoError, "Connection not established", )))?; con.send_packed_commands(cmd, offset, count).await } } impl ConnectionLike for ConnectionWithCredentialsProvider { fn req_packed_command<'a>( &'a mut self, cmd: &'a redis::Cmd, ) -> redis::RedisFuture<'a, redis::Value> { self.send_packed_command(cmd).boxed() } fn req_packed_commands<'a>( &'a mut self, cmd: &'a redis::Pipeline, offset: usize, count: usize, ) -> redis::RedisFuture<'a, Vec<redis::Value>> { self.send_packed_commands(cmd, offset, count).boxed() } fn get_db(&self) -> i64 { self.con.as_ref().map_or(0, |c| c.get_db()) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/redis/mod.rs
proxy/src/redis/mod.rs
pub mod connection_with_credentials_provider; pub mod elasticache; pub mod keys; pub mod kv_ops; pub mod notifications;
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/redis/notifications.rs
proxy/src/redis/notifications.rs
use std::convert::Infallible; use std::sync::Arc; use futures::StreamExt; use redis::aio::PubSub; use serde::Deserialize; use tokio_util::sync::CancellationToken; use super::connection_with_credentials_provider::ConnectionWithCredentialsProvider; use crate::cache::project_info::ProjectInfoCache; use crate::intern::{AccountIdInt, EndpointIdInt, ProjectIdInt, RoleNameInt}; use crate::metrics::{Metrics, RedisErrors, RedisEventsCount}; use crate::util::deserialize_json_string; const CPLANE_CHANNEL_NAME: &str = "neondb-proxy-ws-updates"; const RECONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(20); const INVALIDATION_LAG: std::time::Duration = std::time::Duration::from_secs(20); async fn try_connect(client: &ConnectionWithCredentialsProvider) -> anyhow::Result<PubSub> { let mut conn = client.get_async_pubsub().await?; tracing::info!("subscribing to a channel `{CPLANE_CHANNEL_NAME}`"); conn.subscribe(CPLANE_CHANNEL_NAME).await?; Ok(conn) } #[derive(Debug, Deserialize)] struct NotificationHeader<'a> { topic: &'a str, } #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] #[serde(tag = "topic", content = "data")] enum Notification { #[serde( rename = "/account_settings_update", alias = "/allowed_vpc_endpoints_updated_for_org", deserialize_with = "deserialize_json_string" )] AccountSettingsUpdate(InvalidateAccount), #[serde( rename = "/endpoint_settings_update", deserialize_with = "deserialize_json_string" )] EndpointSettingsUpdate(InvalidateEndpoint), #[serde( rename = "/project_settings_update", alias = "/allowed_ips_updated", alias = "/block_public_or_vpc_access_updated", alias = "/allowed_vpc_endpoints_updated_for_projects", deserialize_with = "deserialize_json_string" )] ProjectSettingsUpdate(InvalidateProject), #[serde( rename = "/role_setting_update", alias = "/password_updated", deserialize_with = "deserialize_json_string" )] RoleSettingUpdate(InvalidateRole), #[serde( other, deserialize_with = "deserialize_unknown_topic", skip_serializing )] UnknownTopic, } #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] #[serde(rename_all = "snake_case")] enum InvalidateEndpoint { EndpointId(EndpointIdInt), EndpointIds(Vec<EndpointIdInt>), } impl std::ops::Deref for InvalidateEndpoint { type Target = [EndpointIdInt]; fn deref(&self) -> &Self::Target { match self { Self::EndpointId(id) => std::slice::from_ref(id), Self::EndpointIds(ids) => ids, } } } #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] #[serde(rename_all = "snake_case")] enum InvalidateProject { ProjectId(ProjectIdInt), ProjectIds(Vec<ProjectIdInt>), } impl std::ops::Deref for InvalidateProject { type Target = [ProjectIdInt]; fn deref(&self) -> &Self::Target { match self { Self::ProjectId(id) => std::slice::from_ref(id), Self::ProjectIds(ids) => ids, } } } #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] #[serde(rename_all = "snake_case")] enum InvalidateAccount { AccountId(AccountIdInt), AccountIds(Vec<AccountIdInt>), } impl std::ops::Deref for InvalidateAccount { type Target = [AccountIdInt]; fn deref(&self) -> &Self::Target { match self { Self::AccountId(id) => std::slice::from_ref(id), Self::AccountIds(ids) => ids, } } } #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] struct InvalidateRole { project_id: ProjectIdInt, role_name: RoleNameInt, } // https://github.com/serde-rs/serde/issues/1714 fn deserialize_unknown_topic<'de, D>(deserializer: D) -> Result<(), D::Error> where D: serde::Deserializer<'de>, { deserializer.deserialize_any(serde::de::IgnoredAny)?; Ok(()) } struct MessageHandler<C: Send + Sync + 'static> { cache: Arc<C>, } impl<C: Send + Sync + 'static> Clone for MessageHandler<C> { fn clone(&self) -> Self { Self { cache: self.cache.clone(), } } } impl MessageHandler<ProjectInfoCache> { pub(crate) fn new(cache: Arc<ProjectInfoCache>) -> Self { Self { cache } } #[tracing::instrument(skip(self, msg), fields(session_id = tracing::field::Empty))] async fn handle_message(&self, msg: redis::Msg) -> anyhow::Result<()> { let payload: String = msg.get_payload()?; tracing::debug!(?payload, "received a message payload"); let msg: Notification = match serde_json::from_str(&payload) { Ok(Notification::UnknownTopic) => { match serde_json::from_str::<NotificationHeader>(&payload) { // don't update the metric for redis errors if it's just a topic we don't know about. Ok(header) => tracing::warn!(topic = header.topic, "unknown topic"), Err(e) => { Metrics::get().proxy.redis_errors_total.inc(RedisErrors { channel: msg.get_channel_name(), }); tracing::error!("broken message: {e}"); } } return Ok(()); } Ok(msg) => msg, Err(e) => { Metrics::get().proxy.redis_errors_total.inc(RedisErrors { channel: msg.get_channel_name(), }); match serde_json::from_str::<NotificationHeader>(&payload) { Ok(header) => tracing::error!(topic = header.topic, "broken message: {e}"), Err(_) => tracing::error!("broken message: {e}"), } return Ok(()); } }; tracing::debug!(?msg, "received a message"); match msg { Notification::RoleSettingUpdate { .. } | Notification::EndpointSettingsUpdate { .. } | Notification::ProjectSettingsUpdate { .. } | Notification::AccountSettingsUpdate { .. } => { invalidate_cache(self.cache.clone(), msg.clone()); let m = &Metrics::get().proxy.redis_events_count; match msg { Notification::RoleSettingUpdate { .. } => { m.inc(RedisEventsCount::InvalidateRole); } Notification::EndpointSettingsUpdate { .. } => { m.inc(RedisEventsCount::InvalidateEndpoint); } Notification::ProjectSettingsUpdate { .. } => { m.inc(RedisEventsCount::InvalidateProject); } Notification::AccountSettingsUpdate { .. } => { m.inc(RedisEventsCount::InvalidateOrg); } Notification::UnknownTopic => {} } // TODO: add additional metrics for the other event types. // It might happen that the invalid entry is on the way to be cached. // To make sure that the entry is invalidated, let's repeat the invalidation in INVALIDATION_LAG seconds. // TODO: include the version (or the timestamp) in the message and invalidate only if the entry is cached before the message. let cache = self.cache.clone(); tokio::spawn(async move { tokio::time::sleep(INVALIDATION_LAG).await; invalidate_cache(cache, msg); }); } Notification::UnknownTopic => unreachable!(), } Ok(()) } } fn invalidate_cache(cache: Arc<ProjectInfoCache>, msg: Notification) { match msg { Notification::EndpointSettingsUpdate(ids) => ids .iter() .for_each(|&id| cache.invalidate_endpoint_access(id)), Notification::AccountSettingsUpdate(ids) => ids .iter() .for_each(|&id| cache.invalidate_endpoint_access_for_org(id)), Notification::ProjectSettingsUpdate(ids) => ids .iter() .for_each(|&id| cache.invalidate_endpoint_access_for_project(id)), Notification::RoleSettingUpdate(InvalidateRole { project_id, role_name, }) => cache.invalidate_role_secret_for_project(project_id, role_name), Notification::UnknownTopic => unreachable!(), } } async fn handle_messages( handler: MessageHandler<ProjectInfoCache>, redis: ConnectionWithCredentialsProvider, cancellation_token: CancellationToken, ) -> anyhow::Result<()> { loop { if cancellation_token.is_cancelled() { return Ok(()); } let mut conn = match try_connect(&redis).await { Ok(conn) => conn, Err(e) => { tracing::error!( "failed to connect to redis: {e}, will try to reconnect in {RECONNECT_TIMEOUT:#?}" ); tokio::time::sleep(RECONNECT_TIMEOUT).await; continue; } }; let mut stream = conn.on_message(); while let Some(msg) = stream.next().await { match handler.handle_message(msg).await { Ok(()) => {} Err(e) => { tracing::error!("failed to handle message: {e}, will try to reconnect"); break; } } if cancellation_token.is_cancelled() { return Ok(()); } } } } /// Handle console's invalidation messages. #[tracing::instrument(name = "redis_notifications", skip_all)] pub async fn task_main( redis: ConnectionWithCredentialsProvider, cache: Arc<ProjectInfoCache>, ) -> anyhow::Result<Infallible> { let handler = MessageHandler::new(cache); // 6h - 1m. // There will be 1 minute overlap between two tasks. But at least we can be sure that no message is lost. let mut interval = tokio::time::interval(std::time::Duration::from_secs(6 * 60 * 60 - 60)); loop { let cancellation_token = CancellationToken::new(); interval.tick().await; tokio::spawn(handle_messages( handler.clone(), redis.clone(), cancellation_token.clone(), )); tokio::spawn(async move { tokio::time::sleep(std::time::Duration::from_secs(6 * 60 * 60)).await; // 6h. cancellation_token.cancel(); }); } } #[cfg(test)] mod tests { use serde_json::json; use super::*; use crate::types::{ProjectId, RoleName}; #[test] fn parse_allowed_ips() -> anyhow::Result<()> { let project_id: ProjectId = "new_project".into(); let data = format!("{{\"project_id\": \"{project_id}\"}}"); let text = json!({ "type": "message", "topic": "/allowed_ips_updated", "data": data, "extre_fields": "something" }) .to_string(); let result: Notification = serde_json::from_str(&text)?; assert_eq!( result, Notification::ProjectSettingsUpdate(InvalidateProject::ProjectId((&project_id).into())) ); Ok(()) } #[test] fn parse_multiple_projects() -> anyhow::Result<()> { let project_id1: ProjectId = "new_project1".into(); let project_id2: ProjectId = "new_project2".into(); let data = format!("{{\"project_ids\": [\"{project_id1}\",\"{project_id2}\"]}}"); let text = json!({ "type": "message", "topic": "/allowed_vpc_endpoints_updated_for_projects", "data": data, "extre_fields": "something" }) .to_string(); let result: Notification = serde_json::from_str(&text)?; assert_eq!( result, Notification::ProjectSettingsUpdate(InvalidateProject::ProjectIds(vec![ (&project_id1).into(), (&project_id2).into() ])) ); Ok(()) } #[test] fn parse_password_updated() -> anyhow::Result<()> { let project_id: ProjectId = "new_project".into(); let role_name: RoleName = "new_role".into(); let data = format!("{{\"project_id\": \"{project_id}\", \"role_name\": \"{role_name}\"}}"); let text = json!({ "type": "message", "topic": "/password_updated", "data": data, "extre_fields": "something" }) .to_string(); let result: Notification = serde_json::from_str(&text)?; assert_eq!( result, Notification::RoleSettingUpdate(InvalidateRole { project_id: (&project_id).into(), role_name: (&role_name).into(), }) ); Ok(()) } #[test] fn parse_unknown_topic() -> anyhow::Result<()> { let with_data = json!({ "type": "message", "topic": "/doesnotexist", "data": { "payload": "ignored" }, "extra_fields": "something" }) .to_string(); let result: Notification = serde_json::from_str(&with_data)?; assert_eq!(result, Notification::UnknownTopic); let without_data = json!({ "type": "message", "topic": "/doesnotexist", "extra_fields": "something" }) .to_string(); let result: Notification = serde_json::from_str(&without_data)?; assert_eq!(result, Notification::UnknownTopic); Ok(()) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/redis/keys.rs
proxy/src/redis/keys.rs
use crate::pqproto::CancelKeyData; pub mod keyspace { pub const CANCEL_PREFIX: &str = "cancel"; } #[derive(Clone, Debug, Eq, PartialEq)] pub(crate) enum KeyPrefix { Cancel(CancelKeyData), } impl KeyPrefix { pub(crate) fn build_redis_key(&self) -> String { match self { KeyPrefix::Cancel(key) => { let id = key.0.get(); let keyspace = keyspace::CANCEL_PREFIX; format!("{keyspace}:{id:x}") } } } } #[cfg(test)] mod tests { use super::*; use crate::pqproto::id_to_cancel_key; #[test] fn test_build_redis_key() { let cancel_key: KeyPrefix = KeyPrefix::Cancel(id_to_cancel_key(12345 << 32 | 54321)); let redis_key = cancel_key.build_redis_key(); assert_eq!(redis_key, "cancel:30390000d431"); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/sasl/stream.rs
proxy/src/sasl/stream.rs
//! Abstraction for the string-oriented SASL protocols. use std::io; use tokio::io::{AsyncRead, AsyncWrite}; use super::{Mechanism, Step}; use crate::context::RequestContext; use crate::pqproto::{BeAuthenticationSaslMessage, BeMessage}; use crate::stream::PqStream; /// SASL authentication outcome. /// It's much easier to match on those two variants /// than to peek into a noisy protocol error type. #[must_use = "caller must explicitly check for success"] pub(crate) enum Outcome<R> { /// Authentication succeeded and produced some value. Success(R), /// Authentication failed (reason attached). Failure(&'static str), } pub async fn authenticate<S, F, M>( ctx: &RequestContext, stream: &mut PqStream<S>, mechanism: F, ) -> super::Result<Outcome<M::Output>> where S: AsyncRead + AsyncWrite + Unpin, F: FnOnce(&str) -> super::Result<M>, M: Mechanism, { let (mut mechanism, mut input) = { // pause the timer while we communicate with the client let _paused = ctx.latency_timer_pause(crate::metrics::Waiting::Client); // Initial client message contains the chosen auth method's name. let msg = stream.read_password_message().await?; let sasl = super::FirstMessage::parse(msg) .ok_or(super::Error::BadClientMessage("bad sasl message"))?; (mechanism(sasl.method)?, sasl.message) }; loop { match mechanism.exchange(input) { Ok(Step::Continue(moved_mechanism, reply)) => { mechanism = moved_mechanism; // write reply let sasl_msg = BeAuthenticationSaslMessage::Continue(reply.as_bytes()); stream.write_message(BeMessage::AuthenticationSasl(sasl_msg)); drop(reply); } Ok(Step::Success(result, reply)) => { // write reply let sasl_msg = BeAuthenticationSaslMessage::Final(reply.as_bytes()); stream.write_message(BeMessage::AuthenticationSasl(sasl_msg)); stream.write_message(BeMessage::AuthenticationOk); // exit with success break Ok(Outcome::Success(result)); } // exit with failure Ok(Step::Failure(reason)) => break Ok(Outcome::Failure(reason)), Err(error) => { tracing::info!(?error, "error during SASL exchange"); return Err(error); } } // pause the timer while we communicate with the client let _paused = ctx.latency_timer_pause(crate::metrics::Waiting::Client); // get next input stream.flush().await?; let msg = stream.read_password_message().await?; input = std::str::from_utf8(msg) .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "bad encoding"))?; } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/sasl/messages.rs
proxy/src/sasl/messages.rs
//! Definitions for SASL messages. use crate::parse::split_cstr; /// SASL-specific payload of [`PasswordMessage`](pq_proto::FeMessage::PasswordMessage). #[derive(Debug)] pub(crate) struct FirstMessage<'a> { /// Authentication method, e.g. `"SCRAM-SHA-256"`. pub(crate) method: &'a str, /// Initial client message. pub(crate) message: &'a str, } impl<'a> FirstMessage<'a> { // NB: FromStr doesn't work with lifetimes pub(crate) fn parse(bytes: &'a [u8]) -> Option<Self> { let (method_cstr, tail) = split_cstr(bytes)?; let method = method_cstr.to_str().ok()?; let (len_bytes, bytes) = tail.split_first_chunk()?; let len = u32::from_be_bytes(*len_bytes) as usize; if len != bytes.len() { return None; } let message = std::str::from_utf8(bytes).ok()?; Some(Self { method, message }) } } #[cfg(test)] mod tests { use super::*; #[test] fn parse_sasl_first_message() { let proto = "SCRAM-SHA-256"; let sasl = "n,,n=,r=KHQ2Gjc7NptyB8aov5/TnUy4"; let sasl_len = (sasl.len() as u32).to_be_bytes(); let bytes = [proto.as_bytes(), &[0], sasl_len.as_ref(), sasl.as_bytes()].concat(); let password = FirstMessage::parse(&bytes).unwrap(); assert_eq!(password.method, proto); assert_eq!(password.message, sasl); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/sasl/channel_binding.rs
proxy/src/sasl/channel_binding.rs
//! Definition and parser for channel binding flag (a part of the `GS2` header). use base64::Engine as _; use base64::prelude::BASE64_STANDARD; /// Channel binding flag (possibly with params). #[derive(Debug, PartialEq, Eq)] pub(crate) enum ChannelBinding<T> { /// Client doesn't support channel binding. NotSupportedClient, /// Client thinks server doesn't support channel binding. NotSupportedServer, /// Client wants to use this type of channel binding. Required(T), } impl<T> ChannelBinding<T> { pub(crate) fn and_then<R, E>( self, f: impl FnOnce(T) -> Result<R, E>, ) -> Result<ChannelBinding<R>, E> { Ok(match self { Self::NotSupportedClient => ChannelBinding::NotSupportedClient, Self::NotSupportedServer => ChannelBinding::NotSupportedServer, Self::Required(x) => ChannelBinding::Required(f(x)?), }) } } impl<'a> ChannelBinding<&'a str> { // NB: FromStr doesn't work with lifetimes pub(crate) fn parse(input: &'a str) -> Option<Self> { Some(match input { "n" => Self::NotSupportedClient, "y" => Self::NotSupportedServer, other => Self::Required(other.strip_prefix("p=")?), }) } } impl<T: std::fmt::Display> ChannelBinding<T> { /// Encode channel binding data as base64 for subsequent checks. pub(crate) fn encode<'a, E>( &self, get_cbind_data: impl FnOnce(&T) -> Result<&'a [u8], E>, ) -> Result<std::borrow::Cow<'static, str>, E> { Ok(match self { Self::NotSupportedClient => { // base64::encode("n,,") "biws".into() } Self::NotSupportedServer => { // base64::encode("y,,") "eSws".into() } Self::Required(mode) => { let mut cbind_input = format!("p={mode},,",).into_bytes(); cbind_input.extend_from_slice(get_cbind_data(mode)?); BASE64_STANDARD.encode(&cbind_input).into() } }) } } #[cfg(test)] mod tests { use super::*; #[test] fn channel_binding_encode() -> anyhow::Result<()> { use ChannelBinding::*; let cases = [ (NotSupportedClient, BASE64_STANDARD.encode("n,,")), (NotSupportedServer, BASE64_STANDARD.encode("y,,")), (Required("foo"), BASE64_STANDARD.encode("p=foo,,bar")), ]; for (cb, input) in cases { assert_eq!(cb.encode(|_| anyhow::Ok(b"bar"))?, input); } Ok(()) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/sasl/mod.rs
proxy/src/sasl/mod.rs
//! Simple Authentication and Security Layer. //! //! RFC: <https://datatracker.ietf.org/doc/html/rfc4422>. //! //! Reference implementation: //! * <https://github.com/postgres/postgres/blob/94226d4506e66d6e7cbf4b391f1e7393c1962841/src/backend/libpq/auth-sasl.c> //! * <https://github.com/postgres/postgres/blob/94226d4506e66d6e7cbf4b391f1e7393c1962841/src/interfaces/libpq/fe-auth.c> mod channel_binding; mod messages; mod stream; use std::io; pub(crate) use channel_binding::ChannelBinding; pub(crate) use messages::FirstMessage; pub(crate) use stream::{Outcome, authenticate}; use thiserror::Error; use crate::error::{ReportableError, UserFacingError}; /// Fine-grained auth errors help in writing tests. #[derive(Error, Debug)] pub(crate) enum Error { #[error("Unsupported authentication method: {0}")] BadAuthMethod(Box<str>), #[error("Channel binding failed: {0}")] ChannelBindingFailed(&'static str), #[error("Unsupported channel binding method: {0}")] ChannelBindingBadMethod(Box<str>), #[error("Bad client message: {0}")] BadClientMessage(&'static str), #[error("Internal error: missing digest")] MissingBinding, #[error("could not decode salt: {0}")] Base64(#[from] base64::DecodeError), #[error(transparent)] Io(#[from] io::Error), } impl UserFacingError for Error { fn to_string_client(&self) -> String { match self { Self::ChannelBindingFailed(m) => (*m).to_string(), Self::ChannelBindingBadMethod(m) => format!("unsupported channel binding method {m}"), _ => "authentication protocol violation".to_string(), } } } impl ReportableError for Error { fn get_error_kind(&self) -> crate::error::ErrorKind { match self { Error::BadAuthMethod(_) => crate::error::ErrorKind::User, Error::ChannelBindingFailed(_) => crate::error::ErrorKind::User, Error::ChannelBindingBadMethod(_) => crate::error::ErrorKind::User, Error::BadClientMessage(_) => crate::error::ErrorKind::User, Error::MissingBinding => crate::error::ErrorKind::Service, Error::Base64(_) => crate::error::ErrorKind::ControlPlane, Error::Io(_) => crate::error::ErrorKind::ClientDisconnect, } } } /// A convenient result type for SASL exchange. pub(crate) type Result<T> = std::result::Result<T, Error>; /// A result of one SASL exchange. #[must_use] pub(crate) enum Step<T, R> { /// We should continue exchanging messages. Continue(T, String), /// The client has been authenticated successfully. Success(R, String), /// Authentication failed (reason attached). Failure(&'static str), } /// Every SASL mechanism (e.g. [SCRAM](crate::scram)) is expected to implement this trait. pub(crate) trait Mechanism: Sized { /// What's produced as a result of successful authentication. type Output; /// Produce a server challenge to be sent to the client. /// This is how this method is called in PostgreSQL (`libpq/sasl.h`). fn exchange(self, input: &str) -> Result<Step<Self, Self::Output>>; }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/bin/pg_sni_router.rs
proxy/src/bin/pg_sni_router.rs
//! A stand-alone program that routes connections, e.g. from //! `aaa--bbb--1234.external.domain` to `aaa.bbb.internal.domain:1234`. //! //! This allows connecting to pods/services running in the same Kubernetes cluster from //! the outside. Similar to an ingress controller for HTTPS. #[tokio::main] async fn main() -> anyhow::Result<()> { proxy::binary::pg_sni_router::run().await }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/bin/local_proxy.rs
proxy/src/bin/local_proxy.rs
#[global_allocator] static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; #[tokio::main] async fn main() -> anyhow::Result<()> { proxy::binary::local_proxy::run().await }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/bin/proxy.rs
proxy/src/bin/proxy.rs
#[global_allocator] static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; #[allow(non_upper_case_globals)] #[unsafe(export_name = "malloc_conf")] pub static malloc_conf: &[u8] = b"prof:true,prof_active:true,lg_prof_sample:21\0"; #[tokio::main] async fn main() -> anyhow::Result<()> { proxy::binary::proxy::run().await }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/binary/pg_sni_router.rs
proxy/src/binary/pg_sni_router.rs
//! A stand-alone program that routes connections, e.g. from //! `aaa--bbb--1234.external.domain` to `aaa.bbb.internal.domain:1234`. //! //! This allows connecting to pods/services running in the same Kubernetes cluster from //! the outside. Similar to an ingress controller for HTTPS. use std::io; use std::net::SocketAddr; use std::path::Path; use std::sync::Arc; use anyhow::{Context, anyhow, bail, ensure}; use clap::Arg; use futures::future::Either; use futures::{FutureExt, TryFutureExt}; use itertools::Itertools; use rustls::crypto::ring; use rustls::pki_types::{DnsName, PrivateKeyDer}; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use tokio::net::TcpListener; use tokio_rustls::TlsConnector; use tokio_rustls::server::TlsStream; use tokio_util::sync::CancellationToken; use tracing::{Instrument, error, info}; use utils::project_git_version; use utils::sentry_init::init_sentry; use crate::context::RequestContext; use crate::metrics::{Metrics, ServiceInfo}; use crate::pglb::TlsRequired; use crate::pqproto::FeStartupPacket; use crate::protocol2::ConnectionInfo; use crate::proxy::{ErrorSource, copy_bidirectional_client_compute}; use crate::stream::{PqStream, Stream}; use crate::util::run_until_cancelled; project_git_version!(GIT_VERSION); fn cli() -> clap::Command { clap::Command::new("Neon proxy/router") .version(GIT_VERSION) .arg( Arg::new("listen") .short('l') .long("listen") .help("listen for incoming client connections on ip:port") .default_value("127.0.0.1:4432"), ) .arg( Arg::new("listen-tls") .long("listen-tls") .help("listen for incoming client connections on ip:port, requiring TLS to compute") .default_value("127.0.0.1:4433"), ) .arg( Arg::new("tls-key") .short('k') .long("tls-key") .help("path to TLS key for client postgres connections") .required(true), ) .arg( Arg::new("tls-cert") .short('c') .long("tls-cert") .help("path to TLS cert for client postgres connections") .required(true), ) .arg( Arg::new("dest") .short('d') .long("destination") .help("append this domain zone to the SNI hostname to get the destination address") .required(true), ) } pub async fn run() -> anyhow::Result<()> { let _logging_guard = crate::logging::init()?; let _panic_hook_guard = utils::logging::replace_panic_hook_with_tracing_panic_hook(); let _sentry_guard = init_sentry(Some(GIT_VERSION.into()), &[]); let args = cli().get_matches(); let destination: String = args .get_one::<String>("dest") .expect("string argument defined") .parse()?; // Configure TLS let tls_config = match ( args.get_one::<String>("tls-key"), args.get_one::<String>("tls-cert"), ) { (Some(key_path), Some(cert_path)) => parse_tls(key_path.as_ref(), cert_path.as_ref())?, _ => bail!("tls-key and tls-cert must be specified"), }; let compute_tls_config = Arc::new(crate::tls::client_config::compute_client_config_with_root_certs()?); // Start listening for incoming client connections let proxy_address: SocketAddr = args .get_one::<String>("listen") .expect("listen argument defined") .parse()?; let proxy_address_compute_tls: SocketAddr = args .get_one::<String>("listen-tls") .expect("listen-tls argument defined") .parse()?; info!("Starting sni router on {proxy_address}"); info!("Starting sni router on {proxy_address_compute_tls}"); let proxy_listener = TcpListener::bind(proxy_address).await?; let proxy_listener_compute_tls = TcpListener::bind(proxy_address_compute_tls).await?; let cancellation_token = CancellationToken::new(); let dest = Arc::new(destination); let main = tokio::spawn(task_main( dest.clone(), tls_config.clone(), None, proxy_listener, cancellation_token.clone(), )) .map(crate::error::flatten_err); let main_tls = tokio::spawn(task_main( dest, tls_config, Some(compute_tls_config), proxy_listener_compute_tls, cancellation_token.clone(), )) .map(crate::error::flatten_err); Metrics::get() .service .info .set_label(ServiceInfo::running()); let signals_task = tokio::spawn(crate::signals::handle(cancellation_token, || {})); // the signal task cant ever succeed. // the main task can error, or can succeed on cancellation. // we want to immediately exit on either of these cases let main = futures::future::try_join(main, main_tls); let signal = match futures::future::select(signals_task, main).await { Either::Left((res, _)) => crate::error::flatten_err(res)?, Either::Right((res, _)) => { res?; return Ok(()); } }; // maintenance tasks return `Infallible` success values, this is an impossible value // so this match statically ensures that there are no possibilities for that value match signal {} } pub(super) fn parse_tls( key_path: &Path, cert_path: &Path, ) -> anyhow::Result<Arc<rustls::ServerConfig>> { let key = { let key_bytes = std::fs::read(key_path).context("TLS key file")?; let mut keys = rustls_pemfile::pkcs8_private_keys(&mut &key_bytes[..]).collect_vec(); ensure!(keys.len() == 1, "keys.len() = {} (should be 1)", keys.len()); PrivateKeyDer::Pkcs8( keys.pop() .expect("keys should not be empty") .context(format!( "Failed to read TLS keys at '{}'", key_path.display() ))?, ) }; let cert_chain_bytes = std::fs::read(cert_path).context(format!( "Failed to read TLS cert file at '{}.'", cert_path.display() ))?; let cert_chain: Vec<_> = { rustls_pemfile::certs(&mut &cert_chain_bytes[..]) .try_collect() .with_context(|| { format!( "Failed to read TLS certificate chain from bytes from file at '{}'.", cert_path.display() ) })? }; let tls_config = rustls::ServerConfig::builder_with_provider(Arc::new(ring::default_provider())) .with_protocol_versions(&[&rustls::version::TLS13, &rustls::version::TLS12]) .context("ring should support TLS1.2 and TLS1.3")? .with_no_client_auth() .with_single_cert(cert_chain, key)? .into(); Ok(tls_config) } pub(super) async fn task_main( dest_suffix: Arc<String>, tls_config: Arc<rustls::ServerConfig>, compute_tls_config: Option<Arc<rustls::ClientConfig>>, listener: tokio::net::TcpListener, cancellation_token: CancellationToken, ) -> anyhow::Result<()> { // When set for the server socket, the keepalive setting // will be inherited by all accepted client sockets. socket2::SockRef::from(&listener).set_keepalive(true)?; let connections = tokio_util::task::task_tracker::TaskTracker::new(); while let Some(accept_result) = run_until_cancelled(listener.accept(), &cancellation_token).await { let (socket, peer_addr) = accept_result?; let session_id = uuid::Uuid::new_v4(); let tls_config = Arc::clone(&tls_config); let dest_suffix = Arc::clone(&dest_suffix); let compute_tls_config = compute_tls_config.clone(); connections.spawn( async move { socket .set_nodelay(true) .context("failed to set socket option")?; let ctx = RequestContext::new( session_id, ConnectionInfo { addr: peer_addr, extra: None, }, crate::metrics::Protocol::SniRouter, ); handle_client(ctx, dest_suffix, tls_config, compute_tls_config, socket).await } .unwrap_or_else(|e| { if let Some(FirstMessage(io_error)) = e.downcast_ref() { // this is noisy. if we get EOF on the very first message that's likely // just NLB doing a healthcheck. if io_error.kind() == io::ErrorKind::UnexpectedEof { return; } } // Acknowledge that the task has finished with an error. error!("per-client task finished with an error: {e:#}"); }) .instrument(tracing::info_span!("handle_client", ?session_id)), ); } connections.close(); drop(listener); connections.wait().await; info!("all client connections have finished"); Ok(()) } #[derive(Debug, thiserror::Error)] #[error(transparent)] struct FirstMessage(io::Error); async fn ssl_handshake<S: AsyncRead + AsyncWrite + Unpin>( ctx: &RequestContext, raw_stream: S, tls_config: Arc<rustls::ServerConfig>, ) -> anyhow::Result<TlsStream<S>> { let (mut stream, msg) = PqStream::parse_startup(Stream::from_raw(raw_stream)) .await .map_err(FirstMessage)?; match msg { FeStartupPacket::SslRequest { direct: None } => { let raw = stream.accept_tls().await?; Ok(raw .upgrade(tls_config, !ctx.has_private_peer_addr()) .await?) } unexpected => { info!( ?unexpected, "unexpected startup packet, rejecting connection" ); Err(stream.throw_error(TlsRequired, None).await)? } } } async fn handle_client( ctx: RequestContext, dest_suffix: Arc<String>, tls_config: Arc<rustls::ServerConfig>, compute_tls_config: Option<Arc<rustls::ClientConfig>>, stream: impl AsyncRead + AsyncWrite + Unpin, ) -> anyhow::Result<()> { let mut tls_stream = ssl_handshake(&ctx, stream, tls_config).await?; // Cut off first part of the SNI domain // We receive required destination details in the format of // `{k8s_service_name}--{k8s_namespace}--{port}.non-sni-domain` let sni = tls_stream .get_ref() .1 .server_name() .ok_or(anyhow!("SNI missing"))?; let dest: Vec<&str> = sni .split_once('.') .context("invalid SNI")? .0 .splitn(3, "--") .collect(); let port = dest[2].parse::<u16>().context("invalid port")?; let destination = format!("{}.{}.{}:{}", dest[0], dest[1], dest_suffix, port); info!("destination: {}", destination); let mut client = tokio::net::TcpStream::connect(&destination).await?; let client = if let Some(compute_tls_config) = compute_tls_config { info!("upgrading TLS"); // send SslRequest client .write_all(b"\x00\x00\x00\x08\x04\xd2\x16\x2f") .await?; // wait for S/N respons let mut resp = b'N'; client.read_exact(std::slice::from_mut(&mut resp)).await?; // error if not S ensure!(resp == b'S', "compute refused TLS"); // upgrade to TLS. let domain = DnsName::try_from(destination)?; let domain = rustls::pki_types::ServerName::DnsName(domain); let client = TlsConnector::from(compute_tls_config) .connect(domain, client) .await?; Connection::Tls(client) } else { Connection::Raw(client) }; // doesn't yet matter as pg-sni-router doesn't report analytics logs ctx.set_success(); ctx.log_connect(); // Starting from here we only proxy the client's traffic. info!("performing the proxy pass..."); let res = match client { Connection::Raw(mut c) => copy_bidirectional_client_compute(&mut tls_stream, &mut c).await, Connection::Tls(mut c) => copy_bidirectional_client_compute(&mut tls_stream, &mut c).await, }; match res { Ok(_) => Ok(()), Err(ErrorSource::Client(err)) => Err(err).context("client"), Err(ErrorSource::Compute(err)) => Err(err).context("compute"), } } #[allow(clippy::large_enum_variant)] enum Connection { Raw(tokio::net::TcpStream), Tls(tokio_rustls::client::TlsStream<tokio::net::TcpStream>), }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/binary/local_proxy.rs
proxy/src/binary/local_proxy.rs
use std::env; use std::net::SocketAddr; use std::pin::pin; use std::sync::Arc; use std::time::Duration; use anyhow::bail; use arc_swap::ArcSwapOption; use camino::Utf8PathBuf; use clap::Parser; use futures::future::Either; use tokio::net::TcpListener; use tokio::sync::Notify; use tokio::task::JoinSet; use tokio_util::sync::CancellationToken; use tracing::{debug, error, info}; use utils::sentry_init::init_sentry; use utils::{pid_file, project_build_tag, project_git_version}; use crate::auth::backend::jwt::JwkCache; use crate::auth::backend::local::LocalBackend; use crate::auth::{self}; use crate::cancellation::CancellationHandler; #[cfg(feature = "rest_broker")] use crate::config::RestConfig; use crate::config::{ self, AuthenticationConfig, ComputeConfig, HttpConfig, ProxyConfig, RetryConfig, refresh_config_loop, }; use crate::control_plane::locks::ApiLocks; use crate::http::health_server::AppMetrics; use crate::metrics::{Metrics, ServiceInfo}; use crate::rate_limiter::{EndpointRateLimiter, LeakyBucketConfig, RateBucketInfo}; use crate::scram::threadpool::ThreadPool; use crate::serverless::cancel_set::CancelSet; use crate::serverless::{self, GlobalConnPoolOptions}; use crate::tls::client_config::compute_client_config_with_root_certs; use crate::url::ApiUrl; project_git_version!(GIT_VERSION); project_build_tag!(BUILD_TAG); /// Neon proxy/router #[derive(Parser)] #[command(version = GIT_VERSION, about)] struct LocalProxyCliArgs { /// listen for incoming metrics connections on ip:port #[clap(long, default_value = "127.0.0.1:7001")] metrics: String, /// listen for incoming http connections on ip:port #[clap(long)] http: String, /// timeout for the TLS handshake #[clap(long, default_value = "15s", value_parser = humantime::parse_duration)] handshake_timeout: tokio::time::Duration, /// lock for `connect_compute` api method. example: "shards=32,permits=4,epoch=10m,timeout=1s". (use `permits=0` to disable). #[clap(long, default_value = config::ConcurrencyLockOptions::DEFAULT_OPTIONS_CONNECT_COMPUTE_LOCK)] connect_compute_lock: String, #[clap(flatten)] sql_over_http: SqlOverHttpArgs, /// User rate limiter max number of requests per second. /// /// Provided in the form `<Requests Per Second>@<Bucket Duration Size>`. /// Can be given multiple times for different bucket sizes. #[clap(long, default_values_t = RateBucketInfo::DEFAULT_ENDPOINT_SET)] user_rps_limit: Vec<RateBucketInfo>, /// Whether to retry the connection to the compute node #[clap(long, default_value = config::RetryConfig::CONNECT_TO_COMPUTE_DEFAULT_VALUES)] connect_to_compute_retry: String, /// Address of the postgres server #[clap(long, default_value = "127.0.0.1:5432")] postgres: SocketAddr, /// Address of the internal compute-ctl api service #[clap(long, default_value = "http://127.0.0.1:3081/")] compute_ctl: ApiUrl, /// Path of the local proxy config file #[clap(long, default_value = "./local_proxy.json")] config_path: Utf8PathBuf, /// Path of the local proxy PID file #[clap(long, default_value = "./local_proxy.pid")] pid_path: Utf8PathBuf, /// Disable pg_session_jwt extension installation /// This is useful for testing the local proxy with vanilla postgres. #[clap(long, default_value = "false")] #[cfg(feature = "testing")] disable_pg_session_jwt: bool, } #[derive(clap::Args, Clone, Copy, Debug)] struct SqlOverHttpArgs { /// How many connections to pool for each endpoint. Excess connections are discarded #[clap(long, default_value_t = 200)] sql_over_http_pool_max_total_conns: usize, /// How long pooled connections should remain idle for before closing #[clap(long, default_value = "5m", value_parser = humantime::parse_duration)] sql_over_http_idle_timeout: tokio::time::Duration, #[clap(long, default_value_t = 100)] sql_over_http_client_conn_threshold: u64, #[clap(long, default_value_t = 16)] sql_over_http_cancel_set_shards: usize, #[clap(long, default_value_t = 10 * 1024 * 1024)] // 10 MiB sql_over_http_max_request_size_bytes: usize, #[clap(long, default_value_t = 10 * 1024 * 1024)] // 10 MiB sql_over_http_max_response_size_bytes: usize, } pub async fn run() -> anyhow::Result<()> { let _logging_guard = crate::logging::init_local_proxy()?; let _panic_hook_guard = utils::logging::replace_panic_hook_with_tracing_panic_hook(); let _sentry_guard = init_sentry(Some(GIT_VERSION.into()), &[]); // TODO: refactor these to use labels debug!("Version: {GIT_VERSION}"); debug!("Build_tag: {BUILD_TAG}"); let neon_metrics = ::metrics::NeonMetrics::new(::metrics::BuildInfo { revision: GIT_VERSION, build_tag: BUILD_TAG, }); let jemalloc = match crate::jemalloc::MetricRecorder::new() { Ok(t) => Some(t), Err(e) => { tracing::error!(error = ?e, "could not start jemalloc metrics loop"); None } }; let args = LocalProxyCliArgs::parse(); let config = build_config(&args)?; let auth_backend = build_auth_backend(&args); // before we bind to any ports, write the process ID to a file // so that compute-ctl can find our process later // in order to trigger the appropriate SIGHUP on config change. // // This also claims a "lock" that makes sure only one instance // of local_proxy runs at a time. let _process_guard = loop { match pid_file::claim_for_current_process(&args.pid_path) { Ok(guard) => break guard, Err(e) => { // compute-ctl might have tried to read the pid-file to let us // know about some config change. We should try again. error!(path=?args.pid_path, "could not claim PID file guard: {e:?}"); tokio::time::sleep(Duration::from_secs(1)).await; } } }; let metrics_listener = TcpListener::bind(args.metrics).await?.into_std()?; let http_listener = TcpListener::bind(args.http).await?; let shutdown = CancellationToken::new(); // todo: should scale with CU let endpoint_rate_limiter = Arc::new(EndpointRateLimiter::new_with_shards( LeakyBucketConfig { rps: 10.0, max: 100.0, }, 16, )); let mut maintenance_tasks = JoinSet::new(); let refresh_config_notify = Arc::new(Notify::new()); maintenance_tasks.spawn(crate::signals::handle(shutdown.clone(), { let refresh_config_notify = Arc::clone(&refresh_config_notify); move || { refresh_config_notify.notify_one(); } })); // trigger the first config load **after** setting up the signal hook // to avoid the race condition where: // 1. No config file registered when local_proxy starts up // 2. The config file is written but the signal hook is not yet received // 3. local_proxy completes startup but has no config loaded, despite there being a registerd config. refresh_config_notify.notify_one(); tokio::spawn(refresh_config_loop( config, args.config_path, refresh_config_notify, )); maintenance_tasks.spawn(crate::http::health_server::task_main( metrics_listener, AppMetrics { jemalloc, neon_metrics, proxy: crate::metrics::Metrics::get(), }, )); let task = serverless::task_main( config, auth_backend, http_listener, shutdown.clone(), Arc::new(CancellationHandler::new(&config.connect_to_compute)), endpoint_rate_limiter, ); Metrics::get() .service .info .set_label(ServiceInfo::running()); match futures::future::select(pin!(maintenance_tasks.join_next()), pin!(task)).await { // exit immediately on maintenance task completion Either::Left((Some(res), _)) => match crate::error::flatten_err(res)? {}, // exit with error immediately if all maintenance tasks have ceased (should be caught by branch above) Either::Left((None, _)) => bail!("no maintenance tasks running. invalid state"), // exit immediately on client task error Either::Right((res, _)) => res?, } Ok(()) } /// ProxyConfig is created at proxy startup, and lives forever. fn build_config(args: &LocalProxyCliArgs) -> anyhow::Result<&'static ProxyConfig> { let config::ConcurrencyLockOptions { shards, limiter, epoch, timeout, } = args.connect_compute_lock.parse()?; info!( ?limiter, shards, ?epoch, "Using NodeLocks (connect_compute)" ); let connect_compute_locks = ApiLocks::new( "connect_compute_lock", limiter, shards, timeout, epoch, &Metrics::get().proxy.connect_compute_lock, ); let http_config = HttpConfig { accept_websockets: false, pool_options: GlobalConnPoolOptions { gc_epoch: Duration::from_secs(60), pool_shards: 2, idle_timeout: args.sql_over_http.sql_over_http_idle_timeout, opt_in: false, max_conns_per_endpoint: args.sql_over_http.sql_over_http_pool_max_total_conns, max_total_conns: args.sql_over_http.sql_over_http_pool_max_total_conns, }, cancel_set: CancelSet::new(args.sql_over_http.sql_over_http_cancel_set_shards), client_conn_threshold: args.sql_over_http.sql_over_http_client_conn_threshold, max_request_size_bytes: args.sql_over_http.sql_over_http_max_request_size_bytes, max_response_size_bytes: args.sql_over_http.sql_over_http_max_response_size_bytes, }; let compute_config = ComputeConfig { retry: RetryConfig::parse(RetryConfig::CONNECT_TO_COMPUTE_DEFAULT_VALUES)?, tls: Arc::new(compute_client_config_with_root_certs()?), timeout: Duration::from_secs(2), }; let greetings = env::var_os("NEON_MOTD").map_or(String::new(), |s| match s.into_string() { Ok(s) => s, Err(_) => { debug!("NEON_MOTD environment variable is not valid UTF-8"); String::new() } }); Ok(Box::leak(Box::new(ProxyConfig { tls_config: ArcSwapOption::from(None), metric_collection: None, http_config, authentication_config: AuthenticationConfig { jwks_cache: JwkCache::default(), scram_thread_pool: ThreadPool::new(0), scram_protocol_timeout: Duration::from_secs(10), ip_allowlist_check_enabled: true, is_vpc_acccess_proxy: false, is_auth_broker: false, accept_jwts: true, console_redirect_confirmation_timeout: Duration::ZERO, }, #[cfg(feature = "rest_broker")] rest_config: RestConfig { is_rest_broker: false, db_schema_cache: None, max_schema_size: 0, hostname_prefix: String::new(), }, proxy_protocol_v2: config::ProxyProtocolV2::Rejected, handshake_timeout: Duration::from_secs(10), wake_compute_retry_config: RetryConfig::parse(RetryConfig::WAKE_COMPUTE_DEFAULT_VALUES)?, connect_compute_locks, connect_to_compute: compute_config, greetings, #[cfg(feature = "testing")] disable_pg_session_jwt: args.disable_pg_session_jwt, }))) } /// auth::Backend is created at proxy startup, and lives forever. fn build_auth_backend(args: &LocalProxyCliArgs) -> &'static auth::Backend<'static, ()> { let auth_backend = crate::auth::Backend::Local(crate::auth::backend::MaybeOwned::Owned( LocalBackend::new(args.postgres, args.compute_ctl.clone()), )); Box::leak(Box::new(auth_backend)) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/binary/mod.rs
proxy/src/binary/mod.rs
//! All binaries have the body of their main() defined here, so that the code //! is also covered by code style configs in lib.rs and the unused-code check is //! more effective when practically all modules are private to the lib. pub mod local_proxy; pub mod pg_sni_router; pub mod proxy;
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/binary/proxy.rs
proxy/src/binary/proxy.rs
use std::env; use std::net::SocketAddr; use std::path::PathBuf; use std::pin::pin; use std::sync::Arc; use std::time::Duration; #[cfg(any(test, feature = "testing"))] use anyhow::Context; use anyhow::{bail, ensure}; use arc_swap::ArcSwapOption; #[cfg(any(test, feature = "testing"))] use camino::Utf8PathBuf; use futures::future::Either; use itertools::{Itertools, Position}; use rand::Rng; use remote_storage::RemoteStorageConfig; use tokio::net::TcpListener; #[cfg(any(test, feature = "testing"))] use tokio::sync::Notify; use tokio::task::JoinSet; use tokio_util::sync::CancellationToken; use tracing::{debug, error, info, warn}; use utils::sentry_init::init_sentry; use utils::{project_build_tag, project_git_version}; use crate::auth::backend::jwt::JwkCache; #[cfg(any(test, feature = "testing"))] use crate::auth::backend::local::LocalBackend; use crate::auth::backend::{ConsoleRedirectBackend, MaybeOwned}; use crate::batch::BatchQueue; use crate::cancellation::{CancellationHandler, CancellationProcessor}; #[cfg(feature = "rest_broker")] use crate::config::RestConfig; #[cfg(any(test, feature = "testing"))] use crate::config::refresh_config_loop; use crate::config::{ self, AuthenticationConfig, CacheOptions, ComputeConfig, HttpConfig, ProjectInfoCacheOptions, ProxyConfig, ProxyProtocolV2, remote_storage_from_toml, }; use crate::context::parquet::ParquetUploadArgs; use crate::http::health_server::AppMetrics; use crate::metrics::{Metrics, ServiceInfo}; use crate::rate_limiter::{EndpointRateLimiter, RateBucketInfo, WakeComputeRateLimiter}; use crate::redis::connection_with_credentials_provider::ConnectionWithCredentialsProvider; use crate::redis::kv_ops::RedisKVClient; use crate::redis::{elasticache, notifications}; use crate::scram::threadpool::ThreadPool; use crate::serverless::GlobalConnPoolOptions; use crate::serverless::cancel_set::CancelSet; #[cfg(feature = "rest_broker")] use crate::serverless::rest::DbSchemaCache; use crate::tls::client_config::compute_client_config_with_root_certs; #[cfg(any(test, feature = "testing"))] use crate::url::ApiUrl; use crate::{auth, control_plane, http, serverless, usage_metrics}; project_git_version!(GIT_VERSION); project_build_tag!(BUILD_TAG); use clap::{Parser, ValueEnum}; #[derive(Clone, Debug, ValueEnum)] #[clap(rename_all = "kebab-case")] enum AuthBackendType { #[clap(alias("cplane-v1"))] ControlPlane, #[clap(alias("link"))] ConsoleRedirect, #[cfg(any(test, feature = "testing"))] Postgres, #[cfg(any(test, feature = "testing"))] Local, } /// Neon proxy/router #[derive(Parser)] #[command(version = GIT_VERSION, about)] struct ProxyCliArgs { /// Name of the region this proxy is deployed in #[clap(long, default_value_t = String::new())] region: String, /// listen for incoming client connections on ip:port #[clap(short, long, default_value = "127.0.0.1:4432")] proxy: SocketAddr, #[clap(value_enum, long, default_value_t = AuthBackendType::ConsoleRedirect)] auth_backend: AuthBackendType, /// Path of the local proxy config file (used for local-file auth backend) #[clap(long, default_value = "./local_proxy.json")] #[cfg(any(test, feature = "testing"))] config_path: Utf8PathBuf, /// listen for management callback connection on ip:port #[clap(short, long, default_value = "127.0.0.1:7000")] mgmt: SocketAddr, /// listen for incoming http connections (metrics, etc) on ip:port #[clap(long, default_value = "127.0.0.1:7001")] http: SocketAddr, /// listen for incoming wss connections on ip:port #[clap(long)] wss: Option<SocketAddr>, /// redirect unauthenticated users to the given uri in case of console redirect auth #[clap(short, long, default_value = "http://localhost:3000/psql_session/")] uri: String, /// cloud API endpoint for authenticating users #[clap( short, long, default_value = "http://localhost:3000/authenticate_proxy_request/" )] auth_endpoint: String, /// JWT used to connect to control plane. #[clap( long, value_name = "JWT", default_value = "", env = "NEON_PROXY_TO_CONTROLPLANE_TOKEN" )] control_plane_token: Arc<str>, /// if this is not local proxy, this toggles whether we accept jwt or passwords for http #[clap(long, default_value_t = false, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)] is_auth_broker: bool, /// path to TLS key for client postgres connections /// /// tls-key and tls-cert are for backwards compatibility, we can put all certs in one dir #[clap(short = 'k', long, alias = "ssl-key")] tls_key: Option<PathBuf>, /// path to TLS cert for client postgres connections /// /// tls-key and tls-cert are for backwards compatibility, we can put all certs in one dir #[clap(short = 'c', long, alias = "ssl-cert")] tls_cert: Option<PathBuf>, /// Allow writing TLS session keys to the given file pointed to by the environment variable `SSLKEYLOGFILE`. #[clap(long, alias = "allow-ssl-keylogfile")] allow_tls_keylogfile: bool, /// path to directory with TLS certificates for client postgres connections #[clap(long)] certs_dir: Option<PathBuf>, /// timeout for the TLS handshake #[clap(long, default_value = "15s", value_parser = humantime::parse_duration)] handshake_timeout: tokio::time::Duration, /// cache for `wake_compute` api method (use `size=0` to disable) #[clap(long, default_value = config::CacheOptions::CACHE_DEFAULT_OPTIONS)] wake_compute_cache: String, /// lock for `wake_compute` api method. example: "shards=32,permits=4,epoch=10m,timeout=1s". (use `permits=0` to disable). #[clap(long, default_value = config::ConcurrencyLockOptions::DEFAULT_OPTIONS_WAKE_COMPUTE_LOCK)] wake_compute_lock: String, /// lock for `connect_compute` api method. example: "shards=32,permits=4,epoch=10m,timeout=1s". (use `permits=0` to disable). #[clap(long, default_value = config::ConcurrencyLockOptions::DEFAULT_OPTIONS_CONNECT_COMPUTE_LOCK)] connect_compute_lock: String, #[clap(flatten)] sql_over_http: SqlOverHttpArgs, /// timeout for scram authentication protocol #[clap(long, default_value = "15s", value_parser = humantime::parse_duration)] scram_protocol_timeout: tokio::time::Duration, /// size of the threadpool for password hashing #[clap(long, default_value_t = 4)] scram_thread_pool_size: u8, /// Endpoint rate limiter max number of requests per second. /// /// Provided in the form `<Requests Per Second>@<Bucket Duration Size>`. /// Can be given multiple times for different bucket sizes. #[clap(long, default_values_t = RateBucketInfo::DEFAULT_ENDPOINT_SET)] endpoint_rps_limit: Vec<RateBucketInfo>, /// Wake compute rate limiter max number of requests per second. #[clap(long, default_values_t = RateBucketInfo::DEFAULT_SET)] wake_compute_limit: Vec<RateBucketInfo>, /// Cancellation channel size (max queue size for redis kv client) #[clap(long, default_value_t = 1024)] cancellation_ch_size: usize, /// Cancellation ops batch size for redis #[clap(long, default_value_t = 8)] cancellation_batch_size: usize, /// redis url for plain authentication #[clap(long, alias("redis-notifications"))] redis_plain: Option<String>, /// what from the available authentications type to use for redis. Supported are "irsa" and "plain". #[clap(long, default_value = "irsa")] redis_auth_type: String, /// redis host for irsa authentication #[clap(long)] redis_host: Option<String>, /// redis port for irsa authentication #[clap(long)] redis_port: Option<u16>, /// redis cluster name for irsa authentication #[clap(long)] redis_cluster_name: Option<String>, /// redis user_id for irsa authentication #[clap(long)] redis_user_id: Option<String>, /// aws region for irsa authentication #[clap(long, default_value_t = String::new())] aws_region: String, /// cache for `project_info` (use `size=0` to disable) #[clap(long, default_value = config::ProjectInfoCacheOptions::CACHE_DEFAULT_OPTIONS)] project_info_cache: String, /// cache for all valid endpoints // TODO: remove after a couple of releases. #[clap(long, default_value_t = String::new())] #[deprecated] endpoint_cache_config: String, #[clap(flatten)] parquet_upload: ParquetUploadArgs, /// http endpoint to receive periodic metric updates #[clap(long)] metric_collection_endpoint: Option<String>, /// how often metrics should be sent to a collection endpoint #[clap(long)] metric_collection_interval: Option<String>, /// interval for backup metric collection #[clap(long, default_value = "10m", value_parser = humantime::parse_duration)] metric_backup_collection_interval: std::time::Duration, /// remote storage configuration for backup metric collection /// Encoded as toml (same format as pageservers), eg /// `{bucket_name='the-bucket',bucket_region='us-east-1',prefix_in_bucket='proxy',endpoint='http://minio:9000'}` #[clap(long, value_parser = remote_storage_from_toml)] metric_backup_collection_remote_storage: Option<RemoteStorageConfig>, /// chunk size for backup metric collection /// Size of each event is no more than 400 bytes, so 2**22 is about 200MB before the compression. #[clap(long, default_value = "4194304")] metric_backup_collection_chunk_size: usize, /// Whether to retry the connection to the compute node #[clap(long, default_value = config::RetryConfig::CONNECT_TO_COMPUTE_DEFAULT_VALUES)] connect_to_compute_retry: String, /// Whether to retry the wake_compute request #[clap(long, default_value = config::RetryConfig::WAKE_COMPUTE_DEFAULT_VALUES)] wake_compute_retry: String, /// Configure if this is a private access proxy for the POC: In that case the proxy will ignore the IP allowlist #[clap(long, default_value_t = false, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)] is_private_access_proxy: bool, /// Configure whether all incoming requests have a Proxy Protocol V2 packet. #[clap(value_enum, long, default_value_t = ProxyProtocolV2::Rejected)] proxy_protocol_v2: ProxyProtocolV2, /// Time the proxy waits for the webauth session to be confirmed by the control plane. // TODO: rename to `console_redirect_confirmation_timeout`. #[clap(long, default_value = "2m", value_parser = humantime::parse_duration)] webauth_confirmation_timeout: std::time::Duration, #[clap(flatten)] pg_sni_router: PgSniRouterArgs, /// if this is not local proxy, this toggles whether we accept Postgres REST requests #[clap(long, default_value_t = false, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)] #[cfg(feature = "rest_broker")] is_rest_broker: bool, /// cache for `db_schema_cache` introspection (use `size=0` to disable) #[clap(long, default_value = "size=1000,ttl=1h")] #[cfg(feature = "rest_broker")] db_schema_cache: String, /// Maximum size allowed for schema in bytes #[clap(long, default_value_t = 5 * 1024 * 1024)] // 5MB #[cfg(feature = "rest_broker")] max_schema_size: usize, /// Hostname prefix to strip from request hostname to get database hostname #[clap(long, default_value = "apirest.")] #[cfg(feature = "rest_broker")] hostname_prefix: String, } #[derive(clap::Args, Clone, Copy, Debug)] struct SqlOverHttpArgs { /// timeout for http connection requests #[clap(long, default_value = "15s", value_parser = humantime::parse_duration)] sql_over_http_timeout: tokio::time::Duration, /// Whether the SQL over http pool is opt-in #[clap(long, default_value_t = true, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)] sql_over_http_pool_opt_in: bool, /// How many connections to pool for each endpoint. Excess connections are discarded #[clap(long, default_value_t = 20)] sql_over_http_pool_max_conns_per_endpoint: usize, /// How many connections to pool for each endpoint. Excess connections are discarded #[clap(long, default_value_t = 20000)] sql_over_http_pool_max_total_conns: usize, /// How long pooled connections should remain idle for before closing #[clap(long, default_value = "5m", value_parser = humantime::parse_duration)] sql_over_http_idle_timeout: tokio::time::Duration, /// Duration each shard will wait on average before a GC sweep. /// A longer time will causes sweeps to take longer but will interfere less frequently. #[clap(long, default_value = "10m", value_parser = humantime::parse_duration)] sql_over_http_pool_gc_epoch: tokio::time::Duration, /// How many shards should the global pool have. Must be a power of two. /// More shards will introduce less contention for pool operations, but can /// increase memory used by the pool #[clap(long, default_value_t = 128)] sql_over_http_pool_shards: usize, #[clap(long, default_value_t = 10000)] sql_over_http_client_conn_threshold: u64, #[clap(long, default_value_t = 64)] sql_over_http_cancel_set_shards: usize, #[clap(long, default_value_t = 10 * 1024 * 1024)] // 10 MiB sql_over_http_max_request_size_bytes: usize, #[clap(long, default_value_t = 10 * 1024 * 1024)] // 10 MiB sql_over_http_max_response_size_bytes: usize, } #[derive(clap::Args, Clone, Debug)] struct PgSniRouterArgs { /// listen for incoming client connections on ip:port #[clap(id = "sni-router-listen", long, default_value = "127.0.0.1:4432")] listen: SocketAddr, /// listen for incoming client connections on ip:port, requiring TLS to compute #[clap(id = "sni-router-listen-tls", long, default_value = "127.0.0.1:4433")] listen_tls: SocketAddr, /// path to TLS key for client postgres connections #[clap(id = "sni-router-tls-key", long)] tls_key: Option<PathBuf>, /// path to TLS cert for client postgres connections #[clap(id = "sni-router-tls-cert", long)] tls_cert: Option<PathBuf>, /// append this domain zone to the SNI hostname to get the destination address #[clap(id = "sni-router-destination", long)] dest: Option<String>, } pub async fn run() -> anyhow::Result<()> { let _logging_guard = crate::logging::init()?; let _panic_hook_guard = utils::logging::replace_panic_hook_with_tracing_panic_hook(); let _sentry_guard = init_sentry(Some(GIT_VERSION.into()), &[]); // TODO: refactor these to use labels info!("Version: {GIT_VERSION}"); info!("Build_tag: {BUILD_TAG}"); let neon_metrics = ::metrics::NeonMetrics::new(::metrics::BuildInfo { revision: GIT_VERSION, build_tag: BUILD_TAG, }); let jemalloc = match crate::jemalloc::MetricRecorder::new() { Ok(t) => Some(t), Err(e) => { error!(error = ?e, "could not start jemalloc metrics loop"); None } }; let args = ProxyCliArgs::parse(); let config = build_config(&args)?; let auth_backend = build_auth_backend(&args)?; match auth_backend { Either::Left(auth_backend) => info!("Authentication backend: {auth_backend}"), Either::Right(auth_backend) => info!("Authentication backend: {auth_backend:?}"), } info!("Using region: {}", args.aws_region); let redis_client = configure_redis(&args).await?; // Check that we can bind to address before further initialization info!("Starting http on {}", args.http); let http_listener = TcpListener::bind(args.http).await?.into_std()?; info!("Starting mgmt on {}", args.mgmt); let mgmt_listener = TcpListener::bind(args.mgmt).await?; let proxy_listener = if args.is_auth_broker { None } else { info!("Starting proxy on {}", args.proxy); Some(TcpListener::bind(args.proxy).await?) }; let sni_router_listeners = { let args = &args.pg_sni_router; if args.dest.is_some() { ensure!( args.tls_key.is_some(), "sni-router-tls-key must be provided" ); ensure!( args.tls_cert.is_some(), "sni-router-tls-cert must be provided" ); info!( "Starting pg-sni-router on {} and {}", args.listen, args.listen_tls ); Some(( TcpListener::bind(args.listen).await?, TcpListener::bind(args.listen_tls).await?, )) } else { None } }; // TODO: rename the argument to something like serverless. // It now covers more than just websockets, it also covers SQL over HTTP. let serverless_listener = if let Some(serverless_address) = args.wss { info!("Starting wss on {serverless_address}"); Some(TcpListener::bind(serverless_address).await?) } else if args.is_auth_broker { bail!("wss arg must be present for auth-broker") } else { None }; let cancellation_token = CancellationToken::new(); let cancellation_handler = Arc::new(CancellationHandler::new(&config.connect_to_compute)); let endpoint_rate_limiter = Arc::new(EndpointRateLimiter::new_with_shards( RateBucketInfo::to_leaky_bucket(&args.endpoint_rps_limit) .unwrap_or(EndpointRateLimiter::DEFAULT), 64, )); #[cfg(any(test, feature = "testing"))] let refresh_config_notify = Arc::new(Notify::new()); // client facing tasks. these will exit on error or on cancellation // cancellation returns Ok(()) let mut client_tasks = JoinSet::new(); match auth_backend { Either::Left(auth_backend) => { if let Some(proxy_listener) = proxy_listener { client_tasks.spawn(crate::pglb::task_main( config, auth_backend, proxy_listener, cancellation_token.clone(), cancellation_handler.clone(), endpoint_rate_limiter.clone(), )); } if let Some(serverless_listener) = serverless_listener { client_tasks.spawn(serverless::task_main( config, auth_backend, serverless_listener, cancellation_token.clone(), cancellation_handler.clone(), endpoint_rate_limiter.clone(), )); } // if auth backend is local, we need to load the config file #[cfg(any(test, feature = "testing"))] if let auth::Backend::Local(_) = &auth_backend { refresh_config_notify.notify_one(); tokio::spawn(refresh_config_loop( config, args.config_path, refresh_config_notify.clone(), )); } } Either::Right(auth_backend) => { if let Some(proxy_listener) = proxy_listener { client_tasks.spawn(crate::console_redirect_proxy::task_main( config, auth_backend, proxy_listener, cancellation_token.clone(), cancellation_handler.clone(), )); } } } // spawn pg-sni-router mode. if let Some((listen, listen_tls)) = sni_router_listeners { let args = args.pg_sni_router; let dest = args.dest.expect("already asserted it is set"); let key_path = args.tls_key.expect("already asserted it is set"); let cert_path = args.tls_cert.expect("already asserted it is set"); let tls_config = super::pg_sni_router::parse_tls(&key_path, &cert_path)?; let dest = Arc::new(dest); client_tasks.spawn(super::pg_sni_router::task_main( dest.clone(), tls_config.clone(), None, listen, cancellation_token.clone(), )); client_tasks.spawn(super::pg_sni_router::task_main( dest, tls_config, Some(config.connect_to_compute.tls.clone()), listen_tls, cancellation_token.clone(), )); } client_tasks.spawn(crate::context::parquet::worker( cancellation_token.clone(), args.parquet_upload, args.region, )); // maintenance tasks. these never return unless there's an error let mut maintenance_tasks = JoinSet::new(); maintenance_tasks.spawn(crate::signals::handle(cancellation_token.clone(), { move || { #[cfg(any(test, feature = "testing"))] refresh_config_notify.notify_one(); } })); maintenance_tasks.spawn(http::health_server::task_main( http_listener, AppMetrics { jemalloc, neon_metrics, proxy: crate::metrics::Metrics::get(), }, )); maintenance_tasks.spawn(control_plane::mgmt::task_main(mgmt_listener)); // add a task to flush the db_schema cache every 10 minutes #[cfg(feature = "rest_broker")] if let Some(db_schema_cache) = &config.rest_config.db_schema_cache { maintenance_tasks.spawn(db_schema_cache.maintain()); } if let Some(metrics_config) = &config.metric_collection { // TODO: Add gc regardles of the metric collection being enabled. maintenance_tasks.spawn(usage_metrics::task_main(metrics_config)); } if let Some(client) = redis_client { // Try to connect to Redis 3 times with 1 + (0..0.1) second interval. // This prevents immediate exit and pod restart, // which can cause hammering of the redis in case of connection issues. // cancellation key management let mut redis_kv_client = RedisKVClient::new(client.clone()); for attempt in (0..3).with_position() { match redis_kv_client.try_connect().await { Ok(()) => { info!("Connected to Redis KV client"); cancellation_handler.init_tx(BatchQueue::new(CancellationProcessor { client: redis_kv_client, batch_size: args.cancellation_batch_size, })); break; } Err(e) => { error!("Failed to connect to Redis KV client: {e}"); if matches!(attempt, Position::Last(_)) { bail!( "Failed to connect to Redis KV client after {} attempts", attempt.into_inner() ); } let jitter = rand::rng().random_range(0..100); tokio::time::sleep(Duration::from_millis(1000 + jitter)).await; } } } #[allow(irrefutable_let_patterns)] if let Either::Left(auth::Backend::ControlPlane(api, ())) = &auth_backend && let crate::control_plane::client::ControlPlaneClient::ProxyV1(api) = &**api { // project info cache and invalidation of that cache. let cache = api.caches.project_info.clone(); maintenance_tasks.spawn(notifications::task_main(client, cache.clone())); maintenance_tasks.spawn(async move { cache.gc_worker().await }); } } Metrics::get() .service .info .set_label(ServiceInfo::running()); let maintenance = loop { // get one complete task match futures::future::select( pin!(maintenance_tasks.join_next()), pin!(client_tasks.join_next()), ) .await { // exit immediately on maintenance task completion Either::Left((Some(res), _)) => break crate::error::flatten_err(res)?, // exit with error immediately if all maintenance tasks have ceased (should be caught by branch above) Either::Left((None, _)) => bail!("no maintenance tasks running. invalid state"), // exit immediately on client task error Either::Right((Some(res), _)) => crate::error::flatten_err(res)?, // exit if all our client tasks have shutdown gracefully Either::Right((None, _)) => return Ok(()), } }; // maintenance tasks return Infallible success values, this is an impossible value // so this match statically ensures that there are no possibilities for that value match maintenance {} } /// ProxyConfig is created at proxy startup, and lives forever. fn build_config(args: &ProxyCliArgs) -> anyhow::Result<&'static ProxyConfig> { let thread_pool = ThreadPool::new(args.scram_thread_pool_size); Metrics::get() .proxy .scram_pool .0 .set(thread_pool.metrics.clone()) .ok(); let tls_config = match (&args.tls_key, &args.tls_cert) { (Some(key_path), Some(cert_path)) => Some(config::configure_tls( key_path, cert_path, args.certs_dir.as_deref(), args.allow_tls_keylogfile, )?), (None, None) => None, _ => bail!("either both or neither tls-key and tls-cert must be specified"), }; let tls_config = ArcSwapOption::from(tls_config.map(Arc::new)); let backup_metric_collection_config = config::MetricBackupCollectionConfig { remote_storage_config: args.metric_backup_collection_remote_storage.clone(), chunk_size: args.metric_backup_collection_chunk_size, }; let metric_collection = match ( &args.metric_collection_endpoint, &args.metric_collection_interval, ) { (Some(endpoint), Some(interval)) => Some(config::MetricCollectionConfig { endpoint: endpoint.parse()?, interval: humantime::parse_duration(interval)?, backup_metric_collection_config, }), (None, None) => None, _ => bail!( "either both or neither metric-collection-endpoint \ and metric-collection-interval must be specified" ), }; let config::ConcurrencyLockOptions { shards, limiter, epoch, timeout, } = args.connect_compute_lock.parse()?; info!( ?limiter, shards, ?epoch, "Using NodeLocks (connect_compute)" ); let connect_compute_locks = control_plane::locks::ApiLocks::new( "connect_compute_lock", limiter, shards, timeout, epoch, &Metrics::get().proxy.connect_compute_lock, ); let http_config = HttpConfig { accept_websockets: !args.is_auth_broker, pool_options: GlobalConnPoolOptions { max_conns_per_endpoint: args.sql_over_http.sql_over_http_pool_max_conns_per_endpoint, gc_epoch: args.sql_over_http.sql_over_http_pool_gc_epoch, pool_shards: args.sql_over_http.sql_over_http_pool_shards, idle_timeout: args.sql_over_http.sql_over_http_idle_timeout, opt_in: args.sql_over_http.sql_over_http_pool_opt_in, max_total_conns: args.sql_over_http.sql_over_http_pool_max_total_conns, }, cancel_set: CancelSet::new(args.sql_over_http.sql_over_http_cancel_set_shards), client_conn_threshold: args.sql_over_http.sql_over_http_client_conn_threshold, max_request_size_bytes: args.sql_over_http.sql_over_http_max_request_size_bytes, max_response_size_bytes: args.sql_over_http.sql_over_http_max_response_size_bytes, }; let authentication_config = AuthenticationConfig { jwks_cache: JwkCache::default(), scram_thread_pool: thread_pool, scram_protocol_timeout: args.scram_protocol_timeout, ip_allowlist_check_enabled: !args.is_private_access_proxy, is_vpc_acccess_proxy: args.is_private_access_proxy, is_auth_broker: args.is_auth_broker, #[cfg(not(feature = "rest_broker"))] accept_jwts: args.is_auth_broker, #[cfg(feature = "rest_broker")] accept_jwts: args.is_auth_broker || args.is_rest_broker, console_redirect_confirmation_timeout: args.webauth_confirmation_timeout, }; let compute_config = ComputeConfig { retry: config::RetryConfig::parse(&args.connect_to_compute_retry)?, tls: Arc::new(compute_client_config_with_root_certs()?), timeout: Duration::from_secs(2), }; #[cfg(feature = "rest_broker")] let rest_config = { let db_schema_cache_config: CacheOptions = args.db_schema_cache.parse()?; info!("Using DbSchemaCache with options={db_schema_cache_config:?}"); let db_schema_cache = if args.is_rest_broker { Some(DbSchemaCache::new(db_schema_cache_config)) } else { None }; RestConfig { is_rest_broker: args.is_rest_broker, db_schema_cache, max_schema_size: args.max_schema_size, hostname_prefix: args.hostname_prefix.clone(), } }; let mut greetings = env::var_os("NEON_MOTD").map_or(String::new(), |s| match s.into_string() { Ok(s) => s, Err(_) => { debug!("NEON_MOTD environment variable is not valid UTF-8"); String::new() } }); match &args.auth_backend { AuthBackendType::ControlPlane => {} #[cfg(any(test, feature = "testing"))] AuthBackendType::Postgres => {} #[cfg(any(test, feature = "testing"))] AuthBackendType::Local => {} AuthBackendType::ConsoleRedirect => { greetings = "Connected to database".to_string(); } } let config = ProxyConfig { tls_config, metric_collection, http_config, authentication_config, proxy_protocol_v2: args.proxy_protocol_v2, handshake_timeout: args.handshake_timeout, wake_compute_retry_config: config::RetryConfig::parse(&args.wake_compute_retry)?, connect_compute_locks, connect_to_compute: compute_config, greetings, #[cfg(feature = "testing")] disable_pg_session_jwt: false, #[cfg(feature = "rest_broker")] rest_config, }; let config = Box::leak(Box::new(config)); tokio::spawn(config.connect_compute_locks.garbage_collect_worker()); Ok(config) } /// auth::Backend is created at proxy startup, and lives forever. fn build_auth_backend( args: &ProxyCliArgs, ) -> anyhow::Result<Either<&'static auth::Backend<'static, ()>, &'static ConsoleRedirectBackend>> { match &args.auth_backend { AuthBackendType::ControlPlane => { let wake_compute_cache_config: CacheOptions = args.wake_compute_cache.parse()?; let project_info_cache_config: ProjectInfoCacheOptions = args.project_info_cache.parse()?; info!("Using NodeInfoCache (wake_compute) with options={wake_compute_cache_config:?}"); info!( "Using AllowedIpsCache (wake_compute) with options={project_info_cache_config:?}" ); let caches = Box::leak(Box::new(control_plane::caches::ApiCaches::new( wake_compute_cache_config, project_info_cache_config, ))); let config::ConcurrencyLockOptions { shards, limiter, epoch, timeout, } = args.wake_compute_lock.parse()?; info!(?limiter, shards, ?epoch, "Using NodeLocks (wake_compute)"); let locks = Box::leak(Box::new(control_plane::locks::ApiLocks::new( "wake_compute_lock", limiter, shards, timeout, epoch, &Metrics::get().wake_compute_lock, ))); tokio::spawn(locks.garbage_collect_worker()); let url: crate::url::ApiUrl = args.auth_endpoint.parse()?; let endpoint = http::Endpoint::new(url, http::new_client()); let mut wake_compute_rps_limit = args.wake_compute_limit.clone(); RateBucketInfo::validate(&mut wake_compute_rps_limit)?; let wake_compute_endpoint_rate_limiter =
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/pglb/copy_bidirectional.rs
proxy/src/pglb/copy_bidirectional.rs
use std::future::poll_fn; use std::io; use std::pin::Pin; use std::task::{Context, Poll, ready}; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tracing::info; #[derive(Debug)] enum TransferState { Running(CopyBuffer), ShuttingDown(u64), Done(u64), } #[derive(Debug)] pub(crate) enum ErrorDirection { Read(io::Error), Write(io::Error), } impl ErrorSource { fn from_client(err: ErrorDirection) -> ErrorSource { match err { ErrorDirection::Read(client) => Self::Client(client), ErrorDirection::Write(compute) => Self::Compute(compute), } } fn from_compute(err: ErrorDirection) -> ErrorSource { match err { ErrorDirection::Write(client) => Self::Client(client), ErrorDirection::Read(compute) => Self::Compute(compute), } } } #[derive(Debug)] pub enum ErrorSource { Client(io::Error), Compute(io::Error), } fn transfer_one_direction<A, B>( cx: &mut Context<'_>, state: &mut TransferState, r: &mut A, w: &mut B, ) -> Poll<Result<u64, ErrorDirection>> where A: AsyncRead + AsyncWrite + Unpin + ?Sized, B: AsyncRead + AsyncWrite + Unpin + ?Sized, { let mut r = Pin::new(r); let mut w = Pin::new(w); loop { match state { TransferState::Running(buf) => { let count = ready!(buf.poll_copy(cx, r.as_mut(), w.as_mut()))?; *state = TransferState::ShuttingDown(count); } TransferState::ShuttingDown(count) => { ready!(w.as_mut().poll_shutdown(cx)).map_err(ErrorDirection::Write)?; *state = TransferState::Done(*count); } TransferState::Done(count) => return Poll::Ready(Ok(*count)), } } } #[tracing::instrument(skip_all)] pub async fn copy_bidirectional_client_compute<Client, Compute>( client: &mut Client, compute: &mut Compute, ) -> Result<(u64, u64), ErrorSource> where Client: AsyncRead + AsyncWrite + Unpin + ?Sized, Compute: AsyncRead + AsyncWrite + Unpin + ?Sized, { let mut client_to_compute = TransferState::Running(CopyBuffer::new()); let mut compute_to_client = TransferState::Running(CopyBuffer::new()); poll_fn(|cx| { let mut client_to_compute_result = transfer_one_direction(cx, &mut client_to_compute, client, compute) .map_err(ErrorSource::from_client)?; let mut compute_to_client_result = transfer_one_direction(cx, &mut compute_to_client, compute, client) .map_err(ErrorSource::from_compute)?; // TODO: 1 info log, with a enum label for close direction. // Early termination checks from compute to client. if let TransferState::Done(_) = compute_to_client && let TransferState::Running(buf) = &client_to_compute { info!("Compute is done, terminate client"); // Initiate shutdown client_to_compute = TransferState::ShuttingDown(buf.amt); client_to_compute_result = transfer_one_direction(cx, &mut client_to_compute, client, compute) .map_err(ErrorSource::from_client)?; } // Early termination checks from client to compute. if let TransferState::Done(_) = client_to_compute && let TransferState::Running(buf) = &compute_to_client { info!("Client is done, terminate compute"); // Initiate shutdown compute_to_client = TransferState::ShuttingDown(buf.amt); compute_to_client_result = transfer_one_direction(cx, &mut compute_to_client, compute, client) .map_err(ErrorSource::from_compute)?; } // It is not a problem if ready! returns early ... (comment remains the same) let client_to_compute = ready!(client_to_compute_result); let compute_to_client = ready!(compute_to_client_result); Poll::Ready(Ok((client_to_compute, compute_to_client))) }) .await } #[derive(Debug)] pub(super) struct CopyBuffer { read_done: bool, need_flush: bool, pos: usize, cap: usize, amt: u64, buf: Box<[u8]>, } const DEFAULT_BUF_SIZE: usize = 1024; impl CopyBuffer { pub(super) fn new() -> Self { Self { read_done: false, need_flush: false, pos: 0, cap: 0, amt: 0, buf: vec![0; DEFAULT_BUF_SIZE].into_boxed_slice(), } } fn poll_fill_buf<R>( &mut self, cx: &mut Context<'_>, reader: Pin<&mut R>, ) -> Poll<io::Result<()>> where R: AsyncRead + ?Sized, { let me = &mut *self; let mut buf = ReadBuf::new(&mut me.buf); buf.set_filled(me.cap); let res = reader.poll_read(cx, &mut buf); if let Poll::Ready(Ok(())) = res { let filled_len = buf.filled().len(); me.read_done = me.cap == filled_len; me.cap = filled_len; } res } fn poll_write_buf<R, W>( &mut self, cx: &mut Context<'_>, mut reader: Pin<&mut R>, mut writer: Pin<&mut W>, ) -> Poll<Result<usize, ErrorDirection>> where R: AsyncRead + ?Sized, W: AsyncWrite + ?Sized, { let me = &mut *self; match writer.as_mut().poll_write(cx, &me.buf[me.pos..me.cap]) { Poll::Pending => { // Top up the buffer towards full if we can read a bit more // data - this should improve the chances of a large write if !me.read_done && me.cap < me.buf.len() { ready!(me.poll_fill_buf(cx, reader.as_mut())).map_err(ErrorDirection::Read)?; } Poll::Pending } res @ Poll::Ready(_) => res.map_err(ErrorDirection::Write), } } pub(super) fn poll_copy<R, W>( &mut self, cx: &mut Context<'_>, mut reader: Pin<&mut R>, mut writer: Pin<&mut W>, ) -> Poll<Result<u64, ErrorDirection>> where R: AsyncRead + ?Sized, W: AsyncWrite + ?Sized, { loop { // If there is some space left in our buffer, then we try to read some // data to continue, thus maximizing the chances of a large write. if self.cap < self.buf.len() && !self.read_done { match self.poll_fill_buf(cx, reader.as_mut()) { Poll::Ready(Ok(())) => (), Poll::Ready(Err(err)) => return Poll::Ready(Err(ErrorDirection::Read(err))), Poll::Pending => { // Ignore pending reads when our buffer is not empty, because // we can try to write data immediately. if self.pos == self.cap { // Try flushing when the reader has no progress to avoid deadlock // when the reader depends on buffered writer. if self.need_flush { ready!(writer.as_mut().poll_flush(cx)) .map_err(ErrorDirection::Write)?; self.need_flush = false; } return Poll::Pending; } } } } // If our buffer has some data, let's write it out! while self.pos < self.cap { let i = ready!(self.poll_write_buf(cx, reader.as_mut(), writer.as_mut()))?; if i == 0 { return Poll::Ready(Err(ErrorDirection::Write(io::Error::new( io::ErrorKind::WriteZero, "write zero byte into writer", )))); } self.pos += i; self.amt += i as u64; self.need_flush = true; } // If pos larger than cap, this loop will never stop. // In particular, user's wrong poll_write implementation returning // incorrect written length may lead to thread blocking. debug_assert!( self.pos <= self.cap, "writer returned length larger than input slice" ); // All data has been written, the buffer can be considered empty again self.pos = 0; self.cap = 0; // If we've written all the data and we've seen EOF, flush out the // data and finish the transfer. if self.read_done { ready!(writer.as_mut().poll_flush(cx)).map_err(ErrorDirection::Write)?; return Poll::Ready(Ok(self.amt)); } } } } #[cfg(test)] mod tests { use tokio::io::AsyncWriteExt; use super::*; #[tokio::test] async fn test_client_to_compute() { let (mut client_client, mut client_proxy) = tokio::io::duplex(8); // Create a mock duplex stream let (mut compute_proxy, mut compute_client) = tokio::io::duplex(32); // Create a mock duplex stream // Simulate 'a' finishing while there's still data for 'b' client_client.write_all(b"hello").await.unwrap(); client_client.shutdown().await.unwrap(); compute_client.write_all(b"Neon").await.unwrap(); compute_client.shutdown().await.unwrap(); let result = copy_bidirectional_client_compute(&mut client_proxy, &mut compute_proxy) .await .unwrap(); // Assert correct transferred amounts let (client_to_compute_count, compute_to_client_count) = result; assert_eq!(client_to_compute_count, 5); // 'hello' was transferred assert_eq!(compute_to_client_count, 4); // response only partially transferred or not at all } #[tokio::test] async fn test_compute_to_client() { let (mut client_client, mut client_proxy) = tokio::io::duplex(32); // Create a mock duplex stream let (mut compute_proxy, mut compute_client) = tokio::io::duplex(8); // Create a mock duplex stream // Simulate 'a' finishing while there's still data for 'b' compute_client.write_all(b"hello").await.unwrap(); compute_client.shutdown().await.unwrap(); client_client .write_all(b"Neon Serverless Postgres") .await .unwrap(); let result = copy_bidirectional_client_compute(&mut client_proxy, &mut compute_proxy) .await .unwrap(); // Assert correct transferred amounts let (client_to_compute_count, compute_to_client_count) = result; assert_eq!(compute_to_client_count, 5); // 'hello' was transferred assert!(client_to_compute_count <= 8); // response only partially transferred or not at all } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/pglb/passthrough.rs
proxy/src/pglb/passthrough.rs
use std::convert::Infallible; use smol_str::SmolStr; use tokio::io::{AsyncRead, AsyncWrite}; use tracing::debug; use utils::measured_stream::MeasuredStream; use super::copy_bidirectional::ErrorSource; use crate::compute::MaybeRustlsStream; use crate::control_plane::messages::MetricsAuxInfo; use crate::metrics::{ Direction, Metrics, NumClientConnectionsGuard, NumConnectionRequestsGuard, NumDbConnectionsGuard, }; use crate::stream::Stream; use crate::usage_metrics::{Ids, MetricCounterRecorder, USAGE_METRICS}; /// Forward bytes in both directions (client <-> compute). #[tracing::instrument(skip_all)] pub(crate) async fn proxy_pass( client: impl AsyncRead + AsyncWrite + Unpin, compute: impl AsyncRead + AsyncWrite + Unpin, aux: MetricsAuxInfo, private_link_id: Option<SmolStr>, ) -> Result<(), ErrorSource> { // we will report ingress at a later date let usage_tx = USAGE_METRICS.register(Ids { endpoint_id: aux.endpoint_id, branch_id: aux.branch_id, private_link_id, }); let metrics = &Metrics::get().proxy.io_bytes; let m_sent = metrics.with_labels(Direction::Tx); let mut client = MeasuredStream::new( client, |_| {}, |cnt| { // Number of bytes we sent to the client (outbound). metrics.get_metric(m_sent).inc_by(cnt as u64); usage_tx.record_egress(cnt as u64); }, ); let m_recv = metrics.with_labels(Direction::Rx); let mut compute = MeasuredStream::new( compute, |_| {}, |cnt| { // Number of bytes the client sent to the compute node (inbound). metrics.get_metric(m_recv).inc_by(cnt as u64); usage_tx.record_ingress(cnt as u64); }, ); // Starting from here we only proxy the client's traffic. debug!("performing the proxy pass..."); let _ = crate::pglb::copy_bidirectional::copy_bidirectional_client_compute( &mut client, &mut compute, ) .await?; Ok(()) } pub(crate) struct ProxyPassthrough<S> { pub(crate) client: Stream<S>, pub(crate) compute: MaybeRustlsStream, pub(crate) aux: MetricsAuxInfo, pub(crate) private_link_id: Option<SmolStr>, pub(crate) _cancel_on_shutdown: tokio::sync::oneshot::Sender<Infallible>, pub(crate) _req: NumConnectionRequestsGuard<'static>, pub(crate) _conn: NumClientConnectionsGuard<'static>, pub(crate) _db_conn: NumDbConnectionsGuard<'static>, } impl<S: AsyncRead + AsyncWrite + Unpin> ProxyPassthrough<S> { pub(crate) async fn proxy_pass(self) -> Result<(), ErrorSource> { proxy_pass(self.client, self.compute, self.aux, self.private_link_id).await } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/pglb/inprocess.rs
proxy/src/pglb/inprocess.rs
#![allow(dead_code, reason = "TODO: work in progress")] use std::pin::{Pin, pin}; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use std::task::{Context, Poll}; use std::{fmt, io}; use tokio::io::{AsyncRead, AsyncWrite, DuplexStream, ReadBuf}; use tokio::sync::mpsc; const STREAM_CHANNEL_SIZE: usize = 16; const MAX_STREAM_BUFFER_SIZE: usize = 4096; #[derive(Debug)] pub struct Connection { stream_sender: mpsc::Sender<Stream>, stream_receiver: mpsc::Receiver<Stream>, stream_id_counter: Arc<AtomicUsize>, } impl Connection { pub fn new() -> (Connection, Connection) { let (sender_a, receiver_a) = mpsc::channel(STREAM_CHANNEL_SIZE); let (sender_b, receiver_b) = mpsc::channel(STREAM_CHANNEL_SIZE); let stream_id_counter = Arc::new(AtomicUsize::new(1)); let conn_a = Connection { stream_sender: sender_a, stream_receiver: receiver_b, stream_id_counter: Arc::clone(&stream_id_counter), }; let conn_b = Connection { stream_sender: sender_b, stream_receiver: receiver_a, stream_id_counter, }; (conn_a, conn_b) } #[inline] fn next_stream_id(&self) -> StreamId { StreamId(self.stream_id_counter.fetch_add(1, Ordering::Relaxed)) } #[tracing::instrument(skip_all, fields(stream_id = tracing::field::Empty, err))] pub async fn open_stream(&self) -> io::Result<Stream> { let (local, remote) = tokio::io::duplex(MAX_STREAM_BUFFER_SIZE); let stream_id = self.next_stream_id(); tracing::Span::current().record("stream_id", stream_id.0); let local = Stream { inner: local, id: stream_id, }; let remote = Stream { inner: remote, id: stream_id, }; self.stream_sender .send(remote) .await .map_err(io::Error::other)?; Ok(local) } #[tracing::instrument(skip_all, fields(stream_id = tracing::field::Empty, err))] pub async fn accept_stream(&mut self) -> io::Result<Option<Stream>> { Ok(self.stream_receiver.recv().await.inspect(|stream| { tracing::Span::current().record("stream_id", stream.id.0); })) } } #[derive(Copy, Clone, Debug)] pub struct StreamId(usize); impl fmt::Display for StreamId { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } // TODO: Proper closing. Currently Streams can outlive their Connections. // Carry WeakSender and check strong_count? #[derive(Debug)] pub struct Stream { inner: DuplexStream, id: StreamId, } impl Stream { #[inline] pub fn id(&self) -> StreamId { self.id } } impl AsyncRead for Stream { #[tracing::instrument(level = "debug", skip_all, fields(stream_id = %self.id))] #[inline] fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<io::Result<()>> { pin!(&mut self.inner).poll_read(cx, buf) } } impl AsyncWrite for Stream { #[tracing::instrument(level = "debug", skip_all, fields(stream_id = %self.id))] #[inline] fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize, io::Error>> { pin!(&mut self.inner).poll_write(cx, buf) } #[tracing::instrument(level = "debug", skip_all, fields(stream_id = %self.id))] #[inline] fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { pin!(&mut self.inner).poll_flush(cx) } #[tracing::instrument(level = "debug", skip_all, fields(stream_id = %self.id))] #[inline] fn poll_shutdown( mut self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), io::Error>> { pin!(&mut self.inner).poll_shutdown(cx) } #[tracing::instrument(level = "debug", skip_all, fields(stream_id = %self.id))] #[inline] fn poll_write_vectored( mut self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[io::IoSlice<'_>], ) -> Poll<Result<usize, io::Error>> { pin!(&mut self.inner).poll_write_vectored(cx, bufs) } #[inline] fn is_write_vectored(&self) -> bool { self.inner.is_write_vectored() } } #[cfg(test)] mod tests { use tokio::io::{AsyncReadExt, AsyncWriteExt}; use super::*; #[tokio::test] async fn test_simple_roundtrip() { let (client, mut server) = Connection::new(); let server_task = tokio::spawn(async move { while let Some(mut stream) = server.accept_stream().await.unwrap() { tokio::spawn(async move { let mut buf = [0; 64]; loop { match stream.read(&mut buf).await.unwrap() { 0 => break, n => stream.write(&buf[..n]).await.unwrap(), }; } }); } }); let mut stream = client.open_stream().await.unwrap(); stream.write_all(b"hello!").await.unwrap(); let mut buf = [0; 64]; let n = stream.read(&mut buf).await.unwrap(); assert_eq!(n, 6); assert_eq!(&buf[..n], b"hello!"); drop(stream); drop(client); server_task.await.unwrap(); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/pglb/mod.rs
proxy/src/pglb/mod.rs
pub mod copy_bidirectional; pub mod handshake; pub mod inprocess; pub mod passthrough; use std::sync::Arc; use futures::FutureExt; use smol_str::ToSmolStr; use thiserror::Error; use tokio::io::{AsyncRead, AsyncWrite}; use tokio_util::sync::CancellationToken; use tracing::{Instrument, debug, error, info, warn}; use crate::auth; use crate::cancellation::{self, CancellationHandler}; use crate::config::{ProxyConfig, ProxyProtocolV2, TlsConfig}; use crate::context::RequestContext; use crate::error::{ReportableError, UserFacingError}; use crate::metrics::{Metrics, NumClientConnectionsGuard}; pub use crate::pglb::copy_bidirectional::ErrorSource; use crate::pglb::handshake::{HandshakeData, HandshakeError, handshake}; use crate::pglb::passthrough::ProxyPassthrough; use crate::protocol2::{ConnectHeader, ConnectionInfo, ConnectionInfoExtra, read_proxy_protocol}; use crate::proxy::handle_client; use crate::rate_limiter::EndpointRateLimiter; use crate::stream::Stream; use crate::util::run_until_cancelled; pub const ERR_INSECURE_CONNECTION: &str = "connection is insecure (try using `sslmode=require`)"; #[derive(Error, Debug)] #[error("{ERR_INSECURE_CONNECTION}")] pub struct TlsRequired; impl ReportableError for TlsRequired { fn get_error_kind(&self) -> crate::error::ErrorKind { crate::error::ErrorKind::User } } impl UserFacingError for TlsRequired {} pub async fn task_main( config: &'static ProxyConfig, auth_backend: &'static auth::Backend<'static, ()>, listener: tokio::net::TcpListener, cancellation_token: CancellationToken, cancellation_handler: Arc<CancellationHandler>, endpoint_rate_limiter: Arc<EndpointRateLimiter>, ) -> anyhow::Result<()> { scopeguard::defer! { info!("proxy has shut down"); } // When set for the server socket, the keepalive setting // will be inherited by all accepted client sockets. socket2::SockRef::from(&listener).set_keepalive(true)?; let connections = tokio_util::task::task_tracker::TaskTracker::new(); let cancellations = tokio_util::task::task_tracker::TaskTracker::new(); while let Some(accept_result) = run_until_cancelled(listener.accept(), &cancellation_token).await { let (socket, peer_addr) = accept_result?; let conn_gauge = Metrics::get() .proxy .client_connections .guard(crate::metrics::Protocol::Tcp); let session_id = uuid::Uuid::new_v4(); let cancellation_handler = Arc::clone(&cancellation_handler); let cancellations = cancellations.clone(); debug!(protocol = "tcp", %session_id, "accepted new TCP connection"); let endpoint_rate_limiter2 = endpoint_rate_limiter.clone(); connections.spawn(async move { let (socket, conn_info) = match config.proxy_protocol_v2 { ProxyProtocolV2::Required => { match read_proxy_protocol(socket).await { Err(e) => { warn!("per-client task finished with an error: {e:#}"); return; } // our load balancers will not send any more data. let's just exit immediately Ok((_socket, ConnectHeader::Local)) => { debug!("healthcheck received"); return; } Ok((socket, ConnectHeader::Proxy(info))) => (socket, info), } } // ignore the header - it cannot be confused for a postgres or http connection so will // error later. ProxyProtocolV2::Rejected => ( socket, ConnectionInfo { addr: peer_addr, extra: None, }, ), }; match socket.set_nodelay(true) { Ok(()) => {} Err(e) => { error!( "per-client task finished with an error: failed to set socket option: {e:#}" ); return; } } let ctx = RequestContext::new(session_id, conn_info, crate::metrics::Protocol::Tcp); let res = handle_connection( config, auth_backend, &ctx, cancellation_handler, socket, ClientMode::Tcp, endpoint_rate_limiter2, conn_gauge, cancellations, ) .instrument(ctx.span()) .boxed() .await; match res { Err(e) => { ctx.set_error_kind(e.get_error_kind()); warn!(parent: &ctx.span(), "per-client task finished with an error: {e:#}"); } Ok(None) => { ctx.set_success(); } Ok(Some(p)) => { ctx.set_success(); let _disconnect = ctx.log_connect(); match p.proxy_pass().await { Ok(()) => {} Err(ErrorSource::Client(e)) => { warn!( ?session_id, "per-client task finished with an IO error from the client: {e:#}" ); } Err(ErrorSource::Compute(e)) => { error!( ?session_id, "per-client task finished with an IO error from the compute: {e:#}" ); } } } } }); } connections.close(); cancellations.close(); drop(listener); // Drain connections connections.wait().await; cancellations.wait().await; Ok(()) } pub(crate) enum ClientMode { Tcp, Websockets { hostname: Option<String> }, } /// Abstracts the logic of handling TCP vs WS clients impl ClientMode { pub fn allow_cleartext(&self) -> bool { match self { ClientMode::Tcp => false, ClientMode::Websockets { .. } => true, } } pub fn hostname<'a, S>(&'a self, s: &'a Stream<S>) -> Option<&'a str> { match self { ClientMode::Tcp => s.sni_hostname(), ClientMode::Websockets { hostname } => hostname.as_deref(), } } pub fn handshake_tls<'a>(&self, tls: Option<&'a TlsConfig>) -> Option<&'a TlsConfig> { match self { ClientMode::Tcp => tls, // TLS is None here if using websockets, because the connection is already encrypted. ClientMode::Websockets { .. } => None, } } } #[derive(Debug, Error)] // almost all errors should be reported to the user, but there's a few cases where we cannot // 1. Cancellation: we are not allowed to tell the client any cancellation statuses for security reasons // 2. Handshake: handshake reports errors if it can, otherwise if the handshake fails due to protocol violation, // we cannot be sure the client even understands our error message // 3. PrepareClient: The client disconnected, so we can't tell them anyway... pub(crate) enum ClientRequestError { #[error("{0}")] Cancellation(#[from] cancellation::CancelError), #[error("{0}")] Handshake(#[from] HandshakeError), #[error("{0}")] HandshakeTimeout(#[from] tokio::time::error::Elapsed), #[error("{0}")] PrepareClient(#[from] std::io::Error), #[error("{0}")] ReportedError(#[from] crate::stream::ReportedError), } impl ReportableError for ClientRequestError { fn get_error_kind(&self) -> crate::error::ErrorKind { match self { ClientRequestError::Cancellation(e) => e.get_error_kind(), ClientRequestError::Handshake(e) => e.get_error_kind(), ClientRequestError::HandshakeTimeout(_) => crate::error::ErrorKind::RateLimit, ClientRequestError::ReportedError(e) => e.get_error_kind(), ClientRequestError::PrepareClient(_) => crate::error::ErrorKind::ClientDisconnect, } } } #[allow(clippy::too_many_arguments)] pub(crate) async fn handle_connection<S: AsyncRead + AsyncWrite + Unpin + Send>( config: &'static ProxyConfig, auth_backend: &'static auth::Backend<'static, ()>, ctx: &RequestContext, cancellation_handler: Arc<CancellationHandler>, client: S, mode: ClientMode, endpoint_rate_limiter: Arc<EndpointRateLimiter>, conn_gauge: NumClientConnectionsGuard<'static>, cancellations: tokio_util::task::task_tracker::TaskTracker, ) -> Result<Option<ProxyPassthrough<S>>, ClientRequestError> { debug!( protocol = %ctx.protocol(), "handling interactive connection from client" ); let metrics = &Metrics::get().proxy; let proto = ctx.protocol(); let request_gauge = metrics.connection_requests.guard(proto); let tls = config.tls_config.load(); let tls = tls.as_deref(); let record_handshake_error = !ctx.has_private_peer_addr(); let pause = ctx.latency_timer_pause(crate::metrics::Waiting::Client); let do_handshake = handshake(ctx, client, mode.handshake_tls(tls), record_handshake_error); let (mut client, params) = match tokio::time::timeout(config.handshake_timeout, do_handshake) .await?? { HandshakeData::Startup(client, params) => (client, params), HandshakeData::Cancel(cancel_key_data) => { // spawn a task to cancel the session, but don't wait for it cancellations.spawn({ let cancellation_handler_clone = Arc::clone(&cancellation_handler); let ctx = ctx.clone(); let cancel_span = tracing::span!(parent: None, tracing::Level::INFO, "cancel_session", session_id = ?ctx.session_id()); cancel_span.follows_from(tracing::Span::current()); async move { cancellation_handler_clone .cancel_session( cancel_key_data, ctx, config.authentication_config.ip_allowlist_check_enabled, config.authentication_config.is_vpc_acccess_proxy, auth_backend.get_api(), ) .await .inspect_err(|e | debug!(error = ?e, "cancel_session failed")).ok(); }.instrument(cancel_span) }); return Ok(None); } }; drop(pause); ctx.set_db_options(params.clone()); let common_names = tls.map(|tls| &tls.common_names); let (node, cancel_on_shutdown) = handle_client( config, auth_backend, ctx, cancellation_handler, &mut client, &mode, endpoint_rate_limiter, common_names, &params, ) .await?; let client = client.flush_and_into_inner().await?; let private_link_id = match ctx.extra() { Some(ConnectionInfoExtra::Aws { vpce_id }) => Some(vpce_id.clone()), Some(ConnectionInfoExtra::Azure { link_id }) => Some(link_id.to_smolstr()), None => None, }; Ok(Some(ProxyPassthrough { client, compute: node.stream.into_framed().into_inner(), aux: node.aux, private_link_id, _cancel_on_shutdown: cancel_on_shutdown, _req: request_gauge, _conn: conn_gauge, _db_conn: node.guage, })) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/pglb/handshake.rs
proxy/src/pglb/handshake.rs
use futures::{FutureExt, TryFutureExt}; use thiserror::Error; use tokio::io::{AsyncRead, AsyncWrite}; use tracing::{debug, info, warn}; use crate::auth::endpoint_sni; use crate::config::TlsConfig; use crate::context::RequestContext; use crate::error::ReportableError; use crate::metrics::Metrics; use crate::pglb::TlsRequired; use crate::pqproto::{ BeMessage, CancelKeyData, FeStartupPacket, ProtocolVersion, StartupMessageParams, }; use crate::stream::{PqStream, Stream, StreamUpgradeError}; use crate::tls::PG_ALPN_PROTOCOL; #[derive(Error, Debug)] pub(crate) enum HandshakeError { #[error("data is sent before server replied with EncryptionResponse")] EarlyData, #[error("protocol violation")] ProtocolViolation, #[error("{0}")] StreamUpgradeError(#[from] StreamUpgradeError), #[error("{0}")] Io(#[from] std::io::Error), #[error("{0}")] ReportedError(#[from] crate::stream::ReportedError), } impl ReportableError for HandshakeError { fn get_error_kind(&self) -> crate::error::ErrorKind { match self { HandshakeError::EarlyData => crate::error::ErrorKind::User, HandshakeError::ProtocolViolation => crate::error::ErrorKind::User, HandshakeError::StreamUpgradeError(upgrade) => match upgrade { StreamUpgradeError::AlreadyTls => crate::error::ErrorKind::Service, StreamUpgradeError::Io(_) => crate::error::ErrorKind::ClientDisconnect, }, HandshakeError::Io(_) => crate::error::ErrorKind::ClientDisconnect, HandshakeError::ReportedError(e) => e.get_error_kind(), } } } pub(crate) enum HandshakeData<S> { Startup(PqStream<Stream<S>>, StartupMessageParams), Cancel(CancelKeyData), } /// Establish a (most probably, secure) connection with the client. /// For better testing experience, `stream` can be any object satisfying the traits. /// It's easier to work with owned `stream` here as we need to upgrade it to TLS; /// we also take an extra care of propagating only the select handshake errors to client. #[tracing::instrument(skip_all)] pub(crate) async fn handshake<S: AsyncRead + AsyncWrite + Unpin + Send>( ctx: &RequestContext, stream: S, mut tls: Option<&TlsConfig>, record_handshake_error: bool, ) -> Result<HandshakeData<S>, HandshakeError> { // Client may try upgrading to each protocol only once let (mut tried_ssl, mut tried_gss) = (false, false); const PG_PROTOCOL_EARLIEST: ProtocolVersion = ProtocolVersion::new(3, 0); const PG_PROTOCOL_LATEST: ProtocolVersion = ProtocolVersion::new(3, 0); let (mut stream, mut msg) = PqStream::parse_startup(Stream::from_raw(stream)).await?; loop { match msg { FeStartupPacket::SslRequest { direct } => match stream.get_ref() { Stream::Raw { .. } if !tried_ssl => { tried_ssl = true; if let Some(tls) = tls.take() { // Upgrade raw stream into a secure TLS-backed stream. // NOTE: We've consumed `tls`; this fact will be used later. let mut read_buf; let raw = if let Some(direct) = &direct { read_buf = &direct[..]; stream.accept_direct_tls() } else { read_buf = &[]; stream.accept_tls().await? }; let Stream::Raw { raw } = raw else { return Err(HandshakeError::StreamUpgradeError( StreamUpgradeError::AlreadyTls, )); }; let mut res = Ok(()); let accept = tokio_rustls::TlsAcceptor::from(tls.pg_config.clone()) .accept_with(raw, |session| { // push the early data to the tls session while !read_buf.is_empty() { match session.read_tls(&mut read_buf) { Ok(_) => {} Err(e) => { res = Err(e); break; } } } }) .map_ok(Box::new) .boxed(); res?; if !read_buf.is_empty() { return Err(HandshakeError::EarlyData); } let tls_stream = accept.await.inspect_err(|_| { if record_handshake_error { Metrics::get().proxy.tls_handshake_failures.inc(); } })?; let conn_info = tls_stream.get_ref().1; // try parse endpoint let ep = conn_info .server_name() .and_then(|sni| endpoint_sni(sni, &tls.common_names)); if let Some(ep) = ep { ctx.set_endpoint_id(ep); } // check the ALPN, if exists, as required. match conn_info.alpn_protocol() { None | Some(PG_ALPN_PROTOCOL) => {} Some(other) => { let alpn = String::from_utf8_lossy(other); warn!(%alpn, "unexpected ALPN"); return Err(HandshakeError::ProtocolViolation); } } let (_, tls_server_end_point) = tls.cert_resolver.resolve(conn_info.server_name()); let tls = Stream::Tls { tls: tls_stream, tls_server_end_point, }; (stream, msg) = PqStream::parse_startup(tls).await?; } else { if direct.is_some() { // client sent us a ClientHello already, we can't do anything with it. return Err(HandshakeError::ProtocolViolation); } msg = stream.reject_encryption().await?; } } _ => return Err(HandshakeError::ProtocolViolation), }, FeStartupPacket::GssEncRequest => match stream.get_ref() { Stream::Raw { .. } if !tried_gss => { tried_gss = true; // Currently, we don't support GSSAPI msg = stream.reject_encryption().await?; } _ => return Err(HandshakeError::ProtocolViolation), }, FeStartupPacket::StartupMessage { params, version } if PG_PROTOCOL_EARLIEST <= version && version <= PG_PROTOCOL_LATEST => { // Check that the config has been consumed during upgrade // OR we didn't provide it at all (for dev purposes). if tls.is_some() { Err(stream.throw_error(TlsRequired, None).await)?; } // This log highlights the start of the connection. // This contains useful information for debugging, not logged elsewhere, like role name and endpoint id. info!( ?version, ?params, session_type = "normal", "successful handshake" ); break Ok(HandshakeData::Startup(stream, params)); } // downgrade protocol version FeStartupPacket::StartupMessage { params, version } if version.major() == 3 && version > PG_PROTOCOL_LATEST => { debug!(?version, "unsupported minor version"); // no protocol extensions are supported. // <https://github.com/postgres/postgres/blob/ca481d3c9ab7bf69ff0c8d71ad3951d407f6a33c/src/backend/tcop/backend_startup.c#L744-L753> let mut unsupported = vec![]; let mut supported = StartupMessageParams::default(); for (k, v) in params.iter() { if k.starts_with("_pq_.") { unsupported.push(k); } else { supported.insert(k, v); } } stream.write_message(BeMessage::NegotiateProtocolVersion { version: PG_PROTOCOL_LATEST, options: &unsupported, }); stream.flush().await?; info!( ?version, ?params, session_type = "normal", "successful handshake; unsupported minor version requested" ); break Ok(HandshakeData::Startup(stream, supported)); } FeStartupPacket::StartupMessage { version, params } => { warn!( ?version, ?params, session_type = "normal", "unsuccessful handshake; unsupported version" ); return Err(HandshakeError::ProtocolViolation); } FeStartupPacket::CancelRequest(cancel_key_data) => { info!(session_type = "cancellation", "successful handshake"); break Ok(HandshakeData::Cancel(cancel_key_data)); } } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/conn_pool_lib.rs
proxy/src/serverless/conn_pool_lib.rs
use std::collections::HashMap; use std::marker::PhantomData; use std::ops::Deref; use std::sync::atomic::{self, AtomicUsize}; use std::sync::{Arc, Weak}; use std::time::Duration; use clashmap::ClashMap; use parking_lot::RwLock; use rand::Rng; use smol_str::ToSmolStr; use tracing::{Span, debug, info, warn}; use super::backend::HttpConnError; use super::conn_pool::ClientDataRemote; use super::http_conn_pool::ClientDataHttp; use super::local_conn_pool::ClientDataLocal; use crate::auth::backend::ComputeUserInfo; use crate::context::RequestContext; use crate::control_plane::messages::{ColdStartInfo, MetricsAuxInfo}; use crate::metrics::{HttpEndpointPoolsGuard, Metrics}; use crate::protocol2::ConnectionInfoExtra; use crate::types::{DbName, EndpointCacheKey, RoleName}; use crate::usage_metrics::{Ids, MetricCounter, USAGE_METRICS}; #[derive(Debug, Clone)] pub(crate) struct ConnInfo { pub(crate) user_info: ComputeUserInfo, pub(crate) dbname: DbName, } impl ConnInfo { // hm, change to hasher to avoid cloning? pub(crate) fn db_and_user(&self) -> (DbName, RoleName) { (self.dbname.clone(), self.user_info.user.clone()) } pub(crate) fn endpoint_cache_key(&self) -> Option<EndpointCacheKey> { // We don't want to cache http connections for ephemeral endpoints. if self.user_info.options.is_ephemeral() { None } else { Some(self.user_info.endpoint_cache_key()) } } } #[derive(Clone)] #[allow(clippy::large_enum_variant, reason = "TODO")] pub(crate) enum ClientDataEnum { Remote(ClientDataRemote), Local(ClientDataLocal), Http(ClientDataHttp), } #[derive(Clone)] pub(crate) struct ClientInnerCommon<C: ClientInnerExt> { pub(crate) inner: C, pub(crate) aux: MetricsAuxInfo, pub(crate) conn_id: uuid::Uuid, pub(crate) data: ClientDataEnum, // custom client data like session, key, jti } impl<C: ClientInnerExt> Drop for ClientInnerCommon<C> { fn drop(&mut self) { match &mut self.data { ClientDataEnum::Remote(remote_data) => { remote_data.cancel(); } ClientDataEnum::Local(local_data) => { local_data.cancel(); } ClientDataEnum::Http(_http_data) => (), } } } impl<C: ClientInnerExt> ClientInnerCommon<C> { pub(crate) fn get_conn_id(&self) -> uuid::Uuid { self.conn_id } pub(crate) fn get_data(&mut self) -> &mut ClientDataEnum { &mut self.data } } pub(crate) struct ConnPoolEntry<C: ClientInnerExt> { pub(crate) conn: ClientInnerCommon<C>, pub(crate) _last_access: std::time::Instant, } // Per-endpoint connection pool, (dbname, username) -> DbUserConnPool // Number of open connections is limited by the `max_conns_per_endpoint`. pub(crate) struct EndpointConnPool<C: ClientInnerExt> { pools: HashMap<(DbName, RoleName), DbUserConnPool<C>>, total_conns: usize, /// max # connections per endpoint max_conns: usize, _guard: HttpEndpointPoolsGuard<'static>, global_connections_count: Arc<AtomicUsize>, global_pool_size_max_conns: usize, pool_name: String, } impl<C: ClientInnerExt> EndpointConnPool<C> { pub(crate) fn new( hmap: HashMap<(DbName, RoleName), DbUserConnPool<C>>, tconns: usize, max_conns_per_endpoint: usize, global_connections_count: Arc<AtomicUsize>, max_total_conns: usize, pname: String, ) -> Self { Self { pools: hmap, total_conns: tconns, max_conns: max_conns_per_endpoint, _guard: Metrics::get().proxy.http_endpoint_pools.guard(), global_connections_count, global_pool_size_max_conns: max_total_conns, pool_name: pname, } } pub(crate) fn get_conn_entry( &mut self, db_user: (DbName, RoleName), ) -> Option<ConnPoolEntry<C>> { let Self { pools, total_conns, global_connections_count, .. } = self; pools.get_mut(&db_user).and_then(|pool_entries| { let (entry, removed) = pool_entries.get_conn_entry(total_conns); global_connections_count.fetch_sub(removed, atomic::Ordering::Relaxed); entry }) } pub(crate) fn remove_client( &mut self, db_user: (DbName, RoleName), conn_id: uuid::Uuid, ) -> bool { let Self { pools, total_conns, global_connections_count, .. } = self; if let Some(pool) = pools.get_mut(&db_user) { let old_len = pool.get_conns().len(); pool.get_conns() .retain(|conn| conn.conn.get_conn_id() != conn_id); let new_len = pool.get_conns().len(); let removed = old_len - new_len; if removed > 0 { global_connections_count.fetch_sub(removed, atomic::Ordering::Relaxed); Metrics::get() .proxy .http_pool_opened_connections .get_metric() .dec_by(removed as i64); } *total_conns -= removed; removed > 0 } else { false } } pub(crate) fn get_name(&self) -> &str { &self.pool_name } pub(crate) fn get_pool(&self, db_user: (DbName, RoleName)) -> Option<&DbUserConnPool<C>> { self.pools.get(&db_user) } pub(crate) fn get_pool_mut( &mut self, db_user: (DbName, RoleName), ) -> Option<&mut DbUserConnPool<C>> { self.pools.get_mut(&db_user) } pub(crate) fn put(pool: &RwLock<Self>, conn_info: &ConnInfo, mut client: ClientInnerCommon<C>) { let conn_id = client.get_conn_id(); let (max_conn, conn_count, pool_name) = { let pool = pool.read(); ( pool.global_pool_size_max_conns, pool.global_connections_count .load(atomic::Ordering::Relaxed), pool.get_name().to_string(), ) }; if client.inner.is_closed() { info!(%conn_id, "{pool_name}: throwing away connection '{conn_info}' because connection is closed"); return; } if let Err(error) = client.inner.reset() { warn!(?error, %conn_id, "{pool_name}: throwing away connection '{conn_info}' because connection could not be reset"); return; } if conn_count >= max_conn { info!(%conn_id, "{pool_name}: throwing away connection '{conn_info}' because pool is full"); return; } // return connection to the pool let mut returned = false; let mut per_db_size = 0; let total_conns = { let mut pool = pool.write(); if pool.total_conns < pool.max_conns { let pool_entries = pool.pools.entry(conn_info.db_and_user()).or_default(); pool_entries.get_conns().push(ConnPoolEntry { conn: client, _last_access: std::time::Instant::now(), }); returned = true; per_db_size = pool_entries.get_conns().len(); pool.total_conns += 1; pool.global_connections_count .fetch_add(1, atomic::Ordering::Relaxed); Metrics::get() .proxy .http_pool_opened_connections .get_metric() .inc(); } pool.total_conns }; // do logging outside of the mutex if returned { debug!(%conn_id, "{pool_name}: returning connection '{conn_info}' back to the pool, total_conns={total_conns}, for this (db, user)={per_db_size}"); } else { info!(%conn_id, "{pool_name}: throwing away connection '{conn_info}' because pool is full, total_conns={total_conns}"); } } } impl<C: ClientInnerExt> Drop for EndpointConnPool<C> { fn drop(&mut self) { if self.total_conns > 0 { self.global_connections_count .fetch_sub(self.total_conns, atomic::Ordering::Relaxed); Metrics::get() .proxy .http_pool_opened_connections .get_metric() .dec_by(self.total_conns as i64); } } } pub(crate) struct DbUserConnPool<C: ClientInnerExt> { pub(crate) conns: Vec<ConnPoolEntry<C>>, pub(crate) initialized: Option<bool>, // a bit ugly, exists only for local pools } impl<C: ClientInnerExt> Default for DbUserConnPool<C> { fn default() -> Self { Self { conns: Vec::new(), initialized: None, } } } pub(crate) trait DbUserConn<C: ClientInnerExt>: Default { fn set_initialized(&mut self); fn is_initialized(&self) -> bool; fn clear_closed_clients(&mut self, conns: &mut usize) -> usize; fn get_conn_entry(&mut self, conns: &mut usize) -> (Option<ConnPoolEntry<C>>, usize); fn get_conns(&mut self) -> &mut Vec<ConnPoolEntry<C>>; } impl<C: ClientInnerExt> DbUserConn<C> for DbUserConnPool<C> { fn set_initialized(&mut self) { self.initialized = Some(true); } fn is_initialized(&self) -> bool { self.initialized.unwrap_or(false) } fn clear_closed_clients(&mut self, conns: &mut usize) -> usize { let old_len = self.conns.len(); self.conns.retain(|conn| !conn.conn.inner.is_closed()); let new_len = self.conns.len(); let removed = old_len - new_len; *conns -= removed; removed } fn get_conn_entry(&mut self, conns: &mut usize) -> (Option<ConnPoolEntry<C>>, usize) { let mut removed = self.clear_closed_clients(conns); let conn = self.conns.pop(); if conn.is_some() { *conns -= 1; removed += 1; } Metrics::get() .proxy .http_pool_opened_connections .get_metric() .dec_by(removed as i64); (conn, removed) } fn get_conns(&mut self) -> &mut Vec<ConnPoolEntry<C>> { &mut self.conns } } pub(crate) trait EndpointConnPoolExt<C: ClientInnerExt> { fn clear_closed(&mut self) -> usize; fn total_conns(&self) -> usize; } impl<C: ClientInnerExt> EndpointConnPoolExt<C> for EndpointConnPool<C> { fn clear_closed(&mut self) -> usize { let mut clients_removed: usize = 0; for db_pool in self.pools.values_mut() { clients_removed += db_pool.clear_closed_clients(&mut self.total_conns); } clients_removed } fn total_conns(&self) -> usize { self.total_conns } } pub(crate) struct GlobalConnPool<C, P> where C: ClientInnerExt, P: EndpointConnPoolExt<C>, { // endpoint -> per-endpoint connection pool // // That should be a fairly conteded map, so return reference to the per-endpoint // pool as early as possible and release the lock. pub(crate) global_pool: ClashMap<EndpointCacheKey, Arc<RwLock<P>>>, /// Number of endpoint-connection pools /// /// [`ClashMap::len`] iterates over all inner pools and acquires a read lock on each. /// That seems like far too much effort, so we're using a relaxed increment counter instead. /// It's only used for diagnostics. pub(crate) global_pool_size: AtomicUsize, /// Total number of connections in the pool pub(crate) global_connections_count: Arc<AtomicUsize>, pub(crate) config: &'static crate::config::HttpConfig, _marker: PhantomData<C>, } #[derive(Debug, Clone, Copy)] pub struct GlobalConnPoolOptions { // Maximum number of connections per one endpoint. // Can mix different (dbname, username) connections. // When running out of free slots for a particular endpoint, // falls back to opening a new connection for each request. pub max_conns_per_endpoint: usize, pub gc_epoch: Duration, pub pool_shards: usize, pub idle_timeout: Duration, pub opt_in: bool, // Total number of connections in the pool. pub max_total_conns: usize, } impl<C, P> GlobalConnPool<C, P> where C: ClientInnerExt, P: EndpointConnPoolExt<C>, { pub(crate) fn new(config: &'static crate::config::HttpConfig) -> Arc<Self> { let shards = config.pool_options.pool_shards; Arc::new(Self { global_pool: ClashMap::with_shard_amount(shards), global_pool_size: AtomicUsize::new(0), config, global_connections_count: Arc::new(AtomicUsize::new(0)), _marker: PhantomData, }) } #[cfg(test)] pub(crate) fn get_global_connections_count(&self) -> usize { self.global_connections_count .load(atomic::Ordering::Relaxed) } pub(crate) fn get_idle_timeout(&self) -> Duration { self.config.pool_options.idle_timeout } pub(crate) fn shutdown(&self) { // drops all strong references to endpoint-pools self.global_pool.clear(); } pub(crate) async fn gc_worker(&self, mut rng: impl Rng) { let epoch = self.config.pool_options.gc_epoch; let mut interval = tokio::time::interval(epoch / (self.global_pool.shards().len()) as u32); loop { interval.tick().await; let shard = rng.random_range(0..self.global_pool.shards().len()); self.gc(shard); } } pub(crate) fn gc(&self, shard: usize) { debug!(shard, "pool: performing epoch reclamation"); // acquire a random shard lock let mut shard = self.global_pool.shards()[shard].write(); let timer = Metrics::get() .proxy .http_pool_reclaimation_lag_seconds .start_timer(); let current_len = shard.len(); let mut clients_removed = 0; shard.retain(|(endpoint, x)| { // if the current endpoint pool is unique (no other strong or weak references) // then it is currently not in use by any connections. if let Some(pool) = Arc::get_mut(x) { let endpoints = pool.get_mut(); clients_removed = endpoints.clear_closed(); if endpoints.total_conns() == 0 { info!("pool: discarding pool for endpoint {endpoint}"); return false; } } true }); let new_len = shard.len(); drop(shard); timer.observe(); // Do logging outside of the lock. if clients_removed > 0 { let size = self .global_connections_count .fetch_sub(clients_removed, atomic::Ordering::Relaxed) - clients_removed; Metrics::get() .proxy .http_pool_opened_connections .get_metric() .dec_by(clients_removed as i64); info!( "pool: performed global pool gc. removed {clients_removed} clients, total number of clients in pool is {size}" ); } let removed = current_len - new_len; if removed > 0 { let global_pool_size = self .global_pool_size .fetch_sub(removed, atomic::Ordering::Relaxed) - removed; info!("pool: performed global pool gc. size now {global_pool_size}"); } } } impl<C: ClientInnerExt> GlobalConnPool<C, EndpointConnPool<C>> { pub(crate) fn get( self: &Arc<Self>, ctx: &RequestContext, conn_info: &ConnInfo, ) -> Result<Option<Client<C>>, HttpConnError> { let mut client: Option<ClientInnerCommon<C>> = None; let Some(endpoint) = conn_info.endpoint_cache_key() else { return Ok(None); }; let endpoint_pool = self.get_or_create_endpoint_pool(&endpoint); if let Some(entry) = endpoint_pool .write() .get_conn_entry(conn_info.db_and_user()) { client = Some(entry.conn); } let endpoint_pool = Arc::downgrade(&endpoint_pool); // ok return cached connection if found and establish a new one otherwise if let Some(mut client) = client { if client.inner.is_closed() { info!("pool: cached connection '{conn_info}' is closed, opening a new one"); return Ok(None); } tracing::Span::current() .record("conn_id", tracing::field::display(client.get_conn_id())); tracing::Span::current().record( "pid", tracing::field::display(client.inner.get_process_id()), ); debug!( cold_start_info = ColdStartInfo::HttpPoolHit.as_str(), "pool: reusing connection '{conn_info}'" ); match client.get_data() { ClientDataEnum::Local(data) => { data.session().send(ctx.session_id())?; } ClientDataEnum::Remote(data) => { data.session().send(ctx.session_id())?; } ClientDataEnum::Http(_) => (), } ctx.set_cold_start_info(ColdStartInfo::HttpPoolHit); ctx.success(); return Ok(Some(Client::new(client, conn_info.clone(), endpoint_pool))); } Ok(None) } pub(crate) fn get_or_create_endpoint_pool( self: &Arc<Self>, endpoint: &EndpointCacheKey, ) -> Arc<RwLock<EndpointConnPool<C>>> { // fast path if let Some(pool) = self.global_pool.get(endpoint) { return pool.clone(); } // slow path let new_pool = Arc::new(RwLock::new(EndpointConnPool { pools: HashMap::new(), total_conns: 0, max_conns: self.config.pool_options.max_conns_per_endpoint, _guard: Metrics::get().proxy.http_endpoint_pools.guard(), global_connections_count: self.global_connections_count.clone(), global_pool_size_max_conns: self.config.pool_options.max_total_conns, pool_name: String::from("remote"), })); // find or create a pool for this endpoint let mut created = false; let pool = self .global_pool .entry(endpoint.clone()) .or_insert_with(|| { created = true; new_pool }) .clone(); // log new global pool size if created { let global_pool_size = self .global_pool_size .fetch_add(1, atomic::Ordering::Relaxed) + 1; info!( "pool: created new pool for '{endpoint}', global pool size now {global_pool_size}" ); } pool } } pub(crate) struct Client<C: ClientInnerExt> { span: Span, inner: Option<ClientInnerCommon<C>>, conn_info: ConnInfo, pool: Weak<RwLock<EndpointConnPool<C>>>, } pub(crate) struct Discard<'a, C: ClientInnerExt> { conn_info: &'a ConnInfo, pool: &'a mut Weak<RwLock<EndpointConnPool<C>>>, } impl<C: ClientInnerExt> Client<C> { pub(crate) fn new( inner: ClientInnerCommon<C>, conn_info: ConnInfo, pool: Weak<RwLock<EndpointConnPool<C>>>, ) -> Self { Self { inner: Some(inner), span: Span::current(), conn_info, pool, } } pub(crate) fn client_inner(&mut self) -> (&mut ClientInnerCommon<C>, Discard<'_, C>) { let Self { inner, pool, conn_info, span: _, } = self; let inner_m = inner.as_mut().expect("client inner should not be removed"); (inner_m, Discard { conn_info, pool }) } pub(crate) fn inner(&mut self) -> (&mut C, Discard<'_, C>) { let Self { inner, pool, conn_info, span: _, } = self; let inner = inner.as_mut().expect("client inner should not be removed"); (&mut inner.inner, Discard { conn_info, pool }) } pub(crate) fn metrics(&self, ctx: &RequestContext) -> Arc<MetricCounter> { let aux = &self .inner .as_ref() .expect("client inner should not be removed") .aux; let private_link_id = match ctx.extra() { None => None, Some(ConnectionInfoExtra::Aws { vpce_id }) => Some(vpce_id.clone()), Some(ConnectionInfoExtra::Azure { link_id }) => Some(link_id.to_smolstr()), }; USAGE_METRICS.register(Ids { endpoint_id: aux.endpoint_id, branch_id: aux.branch_id, private_link_id, }) } } impl<C: ClientInnerExt> Drop for Client<C> { fn drop(&mut self) { let conn_info = self.conn_info.clone(); let client = self .inner .take() .expect("client inner should not be removed"); if let Some(conn_pool) = std::mem::take(&mut self.pool).upgrade() { let _current_span = self.span.enter(); // return connection to the pool EndpointConnPool::put(&conn_pool, &conn_info, client); } } } impl<C: ClientInnerExt> Deref for Client<C> { type Target = C; fn deref(&self) -> &Self::Target { &self .inner .as_ref() .expect("client inner should not be removed") .inner } } pub(crate) trait ClientInnerExt: Sync + Send + 'static { fn is_closed(&self) -> bool; fn get_process_id(&self) -> i32; fn reset(&mut self) -> Result<(), postgres_client::Error>; } impl ClientInnerExt for postgres_client::Client { fn is_closed(&self) -> bool { self.is_closed() } fn get_process_id(&self) -> i32 { self.get_process_id() } fn reset(&mut self) -> Result<(), postgres_client::Error> { self.reset_session_background() } } impl<C: ClientInnerExt> Discard<'_, C> { pub(crate) fn discard(&mut self) { let conn_info = &self.conn_info; if std::mem::take(self.pool).strong_count() > 0 { info!( "pool: throwing away connection '{conn_info}' because connection is potentially in a broken state" ); } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/cancel_set.rs
proxy/src/serverless/cancel_set.rs
//! A set for cancelling random http connections use std::hash::{BuildHasher, BuildHasherDefault}; use std::num::NonZeroUsize; use std::time::Duration; use indexmap::IndexMap; use parking_lot::Mutex; use rand::distr::uniform::{UniformSampler, UniformUsize}; use rustc_hash::FxHasher; use tokio::time::Instant; use tokio_util::sync::CancellationToken; use uuid::Uuid; type Hasher = BuildHasherDefault<FxHasher>; pub struct CancelSet { shards: Box<[Mutex<CancelShard>]>, // keyed by random uuid, fxhasher is fine hasher: Hasher, } pub(crate) struct CancelShard { tokens: IndexMap<uuid::Uuid, (Instant, CancellationToken), Hasher>, } impl CancelSet { pub fn new(shards: usize) -> Self { CancelSet { shards: (0..shards) .map(|_| { Mutex::new(CancelShard { tokens: IndexMap::with_hasher(Hasher::default()), }) }) .collect(), hasher: Hasher::default(), } } pub(crate) fn take(&self) -> Option<CancellationToken> { let dist = UniformUsize::new_inclusive(0, usize::MAX).expect("valid bounds"); for _ in 0..4 { if let Some(token) = self.take_raw(dist.sample(&mut rand::rng())) { return Some(token); } tracing::trace!("failed to get cancel token"); } None } fn take_raw(&self, rng: usize) -> Option<CancellationToken> { NonZeroUsize::new(self.shards.len()) .and_then(|len| self.shards[rng % len].lock().take(rng / len)) } pub(crate) fn insert(&self, id: uuid::Uuid, token: CancellationToken) -> CancelGuard<'_> { let shard = NonZeroUsize::new(self.shards.len()).map(|len| { let hash = self.hasher.hash_one(id) as usize; let shard = &self.shards[hash % len]; shard.lock().insert(id, token); shard }); CancelGuard { shard, id } } } impl CancelShard { fn take(&mut self, rng: usize) -> Option<CancellationToken> { NonZeroUsize::new(self.tokens.len()).and_then(|len| { // 10 second grace period so we don't cancel new connections if self.tokens.get_index(rng % len)?.1.0.elapsed() < Duration::from_secs(10) { return None; } let (_key, (_insert, token)) = self.tokens.swap_remove_index(rng % len)?; Some(token) }) } fn remove(&mut self, id: uuid::Uuid) { self.tokens.swap_remove(&id); } fn insert(&mut self, id: uuid::Uuid, token: CancellationToken) { self.tokens.insert(id, (Instant::now(), token)); } } pub(crate) struct CancelGuard<'a> { shard: Option<&'a Mutex<CancelShard>>, id: Uuid, } impl Drop for CancelGuard<'_> { fn drop(&mut self) { if let Some(shard) = self.shard { shard.lock().remove(self.id); } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/rest.rs
proxy/src/serverless/rest.rs
use std::borrow::Cow; use std::collections::HashMap; use std::convert::Infallible; use std::sync::Arc; use bytes::Bytes; use http::Method; use http::header::{ ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_REQUEST_HEADERS, ALLOW, AUTHORIZATION, CONTENT_TYPE, HOST, ORIGIN, }; use http_body_util::combinators::BoxBody; use http_body_util::{BodyExt, Empty, Full}; use http_utils::error::ApiError; use hyper::body::Incoming; use hyper::http::response::Builder; use hyper::http::{HeaderMap, HeaderName, HeaderValue}; use hyper::{Request, Response, StatusCode}; use indexmap::IndexMap; use moka::sync::Cache; use ouroboros::self_referencing; use serde::de::DeserializeOwned; use serde::{Deserialize, Deserializer}; use serde_json::Value as JsonValue; use serde_json::value::RawValue; use subzero_core::api::ContentType::{ApplicationJSON, Other, SingularJSON, TextCSV}; use subzero_core::api::QueryNode::{Delete, FunctionCall, Insert, Update}; use subzero_core::api::Resolution::{IgnoreDuplicates, MergeDuplicates}; use subzero_core::api::{ApiResponse, ListVal, Payload, Preferences, Representation, SingleVal}; use subzero_core::config::{db_allowed_select_functions, db_schemas, role_claim_key}; use subzero_core::dynamic_statement::{JoinIterator, param, sql}; use subzero_core::error::Error::{ self as SubzeroCoreError, ContentTypeError, GucHeadersError, GucStatusError, InternalError, JsonDeserialize, JwtTokenInvalid, NotFound, }; use subzero_core::error::pg_error_to_status_code; use subzero_core::formatter::Param::{LV, PL, SV, Str, StrOwned}; use subzero_core::formatter::postgresql::{fmt_main_query, generate}; use subzero_core::formatter::{Param, Snippet, SqlParam}; use subzero_core::parser::postgrest::parse; use subzero_core::permissions::{check_safe_functions, replace_select_star}; use subzero_core::schema::{ DbSchema, POSTGRESQL_INTROSPECTION_SQL, get_postgresql_configuration_query, }; use subzero_core::{content_range_header, content_range_status}; use tokio_util::sync::CancellationToken; use tracing::{error, info}; use typed_json::json; use url::form_urlencoded; use super::backend::{HttpConnError, LocalProxyConnError, PoolingBackend}; use super::conn_pool::AuthData; use super::conn_pool_lib::ConnInfo; use super::error::{ConnInfoError, Credentials, HttpCodeError, ReadPayloadError}; use super::http_conn_pool::{self, LocalProxyClient}; use super::http_util::{ ALLOW_POOL, CONN_STRING, NEON_REQUEST_ID, RAW_TEXT_OUTPUT, TXN_ISOLATION_LEVEL, TXN_READ_ONLY, get_conn_info, json_response, uuid_to_header_value, }; use super::json::JsonConversionError; use crate::auth::backend::ComputeCredentialKeys; use crate::cache::common::{count_cache_insert, count_cache_outcome, eviction_listener}; use crate::config::ProxyConfig; use crate::context::RequestContext; use crate::error::{ErrorKind, ReportableError, UserFacingError}; use crate::http::read_body_with_limit; use crate::metrics::{CacheKind, Metrics}; use crate::serverless::sql_over_http::HEADER_VALUE_TRUE; use crate::types::EndpointCacheKey; use crate::util::deserialize_json_string; static EMPTY_JSON_SCHEMA: &str = r#"{"schemas":[]}"#; const INTROSPECTION_SQL: &str = POSTGRESQL_INTROSPECTION_SQL; const HEADER_VALUE_ALLOW_ALL_ORIGINS: HeaderValue = HeaderValue::from_static("*"); // CORS headers values const ACCESS_CONTROL_ALLOW_METHODS_VALUE: HeaderValue = HeaderValue::from_static("GET, POST, PATCH, PUT, DELETE, OPTIONS"); const ACCESS_CONTROL_MAX_AGE_VALUE: HeaderValue = HeaderValue::from_static("86400"); const ACCESS_CONTROL_EXPOSE_HEADERS_VALUE: HeaderValue = HeaderValue::from_static( "Content-Encoding, Content-Location, Content-Range, Content-Type, Date, Location, Server, Transfer-Encoding, Range-Unit", ); const ACCESS_CONTROL_ALLOW_HEADERS_VALUE: HeaderValue = HeaderValue::from_static("Authorization"); // A wrapper around the DbSchema that allows for self-referencing #[self_referencing] pub struct DbSchemaOwned { schema_string: String, #[covariant] #[borrows(schema_string)] schema: DbSchema<'this>, } impl<'de> Deserialize<'de> for DbSchemaOwned { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; DbSchemaOwned::try_new(s, |s| serde_json::from_str(s)) .map_err(<D::Error as serde::de::Error>::custom) } } fn split_comma_separated(s: &str) -> Vec<String> { s.split(',').map(|s| s.trim().to_string()).collect() } fn deserialize_comma_separated<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error> where D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; Ok(split_comma_separated(&s)) } fn deserialize_comma_separated_option<'de, D>( deserializer: D, ) -> Result<Option<Vec<String>>, D::Error> where D: Deserializer<'de>, { let opt = Option::<String>::deserialize(deserializer)?; if let Some(s) = &opt { let trimmed = s.trim(); if trimmed.is_empty() { return Ok(None); } return Ok(Some(split_comma_separated(trimmed))); } Ok(None) } // The ApiConfig is the configuration for the API per endpoint // The configuration is read from the database and cached in the DbSchemaCache #[derive(Deserialize, Debug)] pub struct ApiConfig { #[serde( default = "db_schemas", deserialize_with = "deserialize_comma_separated" )] pub db_schemas: Vec<String>, pub db_anon_role: Option<String>, pub db_max_rows: Option<String>, #[serde(default = "db_allowed_select_functions")] pub db_allowed_select_functions: Vec<String>, // #[serde(deserialize_with = "to_tuple", default)] // pub db_pre_request: Option<(String, String)>, #[allow(dead_code)] #[serde(default = "role_claim_key")] pub role_claim_key: String, #[serde(default, deserialize_with = "deserialize_comma_separated_option")] pub db_extra_search_path: Option<Vec<String>>, #[serde(default, deserialize_with = "deserialize_comma_separated_option")] pub server_cors_allowed_origins: Option<Vec<String>>, } // The DbSchemaCache is a cache of the ApiConfig and DbSchemaOwned for each endpoint pub(crate) struct DbSchemaCache(Cache<EndpointCacheKey, Arc<(ApiConfig, DbSchemaOwned)>>); impl DbSchemaCache { pub fn new(config: crate::config::CacheOptions) -> Self { let builder = Cache::builder().name("schema"); let builder = config.moka(builder); let metrics = &Metrics::get().cache; if let Some(size) = config.size { metrics.capacity.set(CacheKind::Schema, size as i64); } let builder = builder.eviction_listener(|_k, _v, cause| eviction_listener(CacheKind::Schema, cause)); Self(builder.build()) } pub async fn maintain(&self) -> Result<Infallible, anyhow::Error> { let mut ticker = tokio::time::interval(std::time::Duration::from_secs(60)); loop { ticker.tick().await; self.0.run_pending_tasks(); } } pub fn get_cached( &self, endpoint_id: &EndpointCacheKey, ) -> Option<Arc<(ApiConfig, DbSchemaOwned)>> { count_cache_outcome(CacheKind::Schema, self.0.get(endpoint_id)) } pub async fn get_remote( &self, endpoint_id: &EndpointCacheKey, auth_header: &HeaderValue, connection_string: &str, client: &mut http_conn_pool::Client<LocalProxyClient>, ctx: &RequestContext, config: &'static ProxyConfig, ) -> Result<Arc<(ApiConfig, DbSchemaOwned)>, RestError> { info!("db_schema cache miss for endpoint: {:?}", endpoint_id); let remote_value = self .internal_get_remote(auth_header, connection_string, client, ctx, config) .await; let (api_config, schema_owned) = match remote_value { Ok((api_config, schema_owned)) => (api_config, schema_owned), Err(e @ RestError::SchemaTooLarge) => { // for the case where the schema is too large, we cache an empty dummy value // all the other requests will fail without triggering the introspection query let schema_owned = serde_json::from_str::<DbSchemaOwned>(EMPTY_JSON_SCHEMA) .map_err(|e| JsonDeserialize { source: e })?; let api_config = ApiConfig { db_schemas: vec![], db_anon_role: None, db_max_rows: None, db_allowed_select_functions: vec![], role_claim_key: String::new(), db_extra_search_path: None, server_cors_allowed_origins: None, }; let value = Arc::new((api_config, schema_owned)); count_cache_insert(CacheKind::Schema); self.0.insert(endpoint_id.clone(), value); return Err(e); } Err(e) => { return Err(e); } }; let value = Arc::new((api_config, schema_owned)); count_cache_insert(CacheKind::Schema); self.0.insert(endpoint_id.clone(), value.clone()); Ok(value) } async fn internal_get_remote( &self, auth_header: &HeaderValue, connection_string: &str, client: &mut http_conn_pool::Client<LocalProxyClient>, ctx: &RequestContext, config: &'static ProxyConfig, ) -> Result<(ApiConfig, DbSchemaOwned), RestError> { #[derive(Deserialize)] struct SingleRow<Row> { rows: [Row; 1], } #[derive(Deserialize)] struct ConfigRow { #[serde(deserialize_with = "deserialize_json_string")] config: ApiConfig, } #[derive(Deserialize)] struct SchemaRow { json_schema: DbSchemaOwned, } let headers = vec![ (&NEON_REQUEST_ID, uuid_to_header_value(ctx.session_id())), ( &CONN_STRING, HeaderValue::from_str(connection_string).expect( "connection string came from a header, so it must be a valid headervalue", ), ), (&AUTHORIZATION, auth_header.clone()), (&RAW_TEXT_OUTPUT, HEADER_VALUE_TRUE), ]; let query = get_postgresql_configuration_query(Some("pgrst.pre_config")); let SingleRow { rows: [ConfigRow { config: api_config }], } = make_local_proxy_request( client, headers.iter().cloned(), QueryData { query: Cow::Owned(query), params: vec![], }, config.rest_config.max_schema_size, ) .await .map_err(|e| match e { RestError::ReadPayload(ReadPayloadError::BodyTooLarge { .. }) => { RestError::SchemaTooLarge } e => e, })?; // now that we have the api_config let's run the second INTROSPECTION_SQL query let SingleRow { rows: [SchemaRow { json_schema }], } = make_local_proxy_request( client, headers, QueryData { query: INTROSPECTION_SQL.into(), params: vec![ serde_json::to_value(&api_config.db_schemas) .expect("Vec<String> is always valid to encode as JSON"), JsonValue::Bool(false), // include_roles_with_login JsonValue::Bool(false), // use_internal_permissions ], }, config.rest_config.max_schema_size, ) .await .map_err(|e| match e { RestError::ReadPayload(ReadPayloadError::BodyTooLarge { .. }) => { RestError::SchemaTooLarge } e => e, })?; Ok((api_config, json_schema)) } } // A type to represent a postgresql errors // we use our own type (instead of postgres_client::Error) because we get the error from the json response #[derive(Debug, thiserror::Error, Deserialize)] pub(crate) struct PostgresError { pub code: String, pub message: String, pub detail: Option<String>, pub hint: Option<String>, } impl HttpCodeError for PostgresError { fn get_http_status_code(&self) -> StatusCode { let status = pg_error_to_status_code(&self.code, true); StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR) } } impl ReportableError for PostgresError { fn get_error_kind(&self) -> ErrorKind { ErrorKind::User } } impl UserFacingError for PostgresError { fn to_string_client(&self) -> String { if self.code.starts_with("PT") { "Postgres error".to_string() } else { self.message.clone() } } } impl std::fmt::Display for PostgresError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.message) } } // A type to represent errors that can occur in the rest broker #[derive(Debug, thiserror::Error)] pub(crate) enum RestError { #[error(transparent)] ReadPayload(#[from] ReadPayloadError), #[error(transparent)] ConnectCompute(#[from] HttpConnError), #[error(transparent)] ConnInfo(#[from] ConnInfoError), #[error(transparent)] Postgres(#[from] PostgresError), #[error(transparent)] JsonConversion(#[from] JsonConversionError), #[error(transparent)] SubzeroCore(#[from] SubzeroCoreError), #[error("schema is too large")] SchemaTooLarge, } impl ReportableError for RestError { fn get_error_kind(&self) -> ErrorKind { match self { RestError::ReadPayload(e) => e.get_error_kind(), RestError::ConnectCompute(e) => e.get_error_kind(), RestError::ConnInfo(e) => e.get_error_kind(), RestError::Postgres(_) => ErrorKind::Postgres, RestError::JsonConversion(_) => ErrorKind::Postgres, RestError::SubzeroCore(_) => ErrorKind::User, RestError::SchemaTooLarge => ErrorKind::User, } } } impl UserFacingError for RestError { fn to_string_client(&self) -> String { match self { RestError::ReadPayload(p) => p.to_string(), RestError::ConnectCompute(c) => c.to_string_client(), RestError::ConnInfo(c) => c.to_string_client(), RestError::SchemaTooLarge => self.to_string(), RestError::Postgres(p) => p.to_string_client(), RestError::JsonConversion(_) => "could not parse postgres response".to_string(), RestError::SubzeroCore(s) => { // TODO: this is a hack to get the message from the json body let json = s.json_body(); let default_message = "Unknown error".to_string(); json.get("message") .map_or(default_message.clone(), |m| match m { JsonValue::String(s) => s.clone(), _ => default_message, }) } } } } impl HttpCodeError for RestError { fn get_http_status_code(&self) -> StatusCode { match self { RestError::ReadPayload(e) => e.get_http_status_code(), RestError::ConnectCompute(h) => match h.get_error_kind() { ErrorKind::User => StatusCode::BAD_REQUEST, _ => StatusCode::INTERNAL_SERVER_ERROR, }, RestError::ConnInfo(_) => StatusCode::BAD_REQUEST, RestError::Postgres(e) => e.get_http_status_code(), RestError::JsonConversion(_) => StatusCode::INTERNAL_SERVER_ERROR, RestError::SchemaTooLarge => StatusCode::INTERNAL_SERVER_ERROR, RestError::SubzeroCore(e) => { let status = e.status_code(); StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR) } } } } // Helper functions for the rest broker fn fmt_env_query<'a>(env: &'a HashMap<&'a str, &'a str>) -> Snippet<'a> { "select " + if env.is_empty() { sql("null") } else { env.iter() .map(|(k, v)| { "set_config(" + param(k as &SqlParam) + ", " + param(v as &SqlParam) + ", true)" }) .join(",") } } // TODO: see about removing the need for cloning the values (inner things are &Cow<str> already) fn to_sql_param(p: &Param) -> JsonValue { match p { SV(SingleVal(v, ..)) => JsonValue::String(v.to_string()), Str(v) => JsonValue::String((*v).to_string()), StrOwned(v) => JsonValue::String((*v).clone()), PL(Payload(v, ..)) => JsonValue::String(v.clone().into_owned()), LV(ListVal(v, ..)) => { if v.is_empty() { JsonValue::String(r"{}".to_string()) } else { JsonValue::String(format!( "{{\"{}\"}}", v.iter() .map(|e| e.replace('\\', "\\\\").replace('\"', "\\\"")) .collect::<Vec<_>>() .join("\",\"") )) } } } } #[derive(serde::Serialize)] struct QueryData<'a> { query: Cow<'a, str>, params: Vec<JsonValue>, } #[derive(serde::Serialize)] struct BatchQueryData<'a> { queries: Vec<QueryData<'a>>, } async fn make_local_proxy_request<S: DeserializeOwned>( client: &mut http_conn_pool::Client<LocalProxyClient>, headers: impl IntoIterator<Item = (&HeaderName, HeaderValue)>, body: QueryData<'_>, max_len: usize, ) -> Result<S, RestError> { let body_string = serde_json::to_string(&body) .map_err(|e| RestError::JsonConversion(JsonConversionError::ParseJsonError(e)))?; let response = make_raw_local_proxy_request(client, headers, body_string).await?; let response_status = response.status(); if response_status != StatusCode::OK { return Err(RestError::SubzeroCore(InternalError { message: "Failed to get endpoint schema".to_string(), })); } // Capture the response body let response_body = crate::http::read_body_with_limit(response.into_body(), max_len) .await .map_err(ReadPayloadError::from)?; // Parse the JSON response let response_json: S = serde_json::from_slice(&response_body) .map_err(|e| RestError::SubzeroCore(JsonDeserialize { source: e }))?; Ok(response_json) } async fn make_raw_local_proxy_request( client: &mut http_conn_pool::Client<LocalProxyClient>, headers: impl IntoIterator<Item = (&HeaderName, HeaderValue)>, body: String, ) -> Result<Response<Incoming>, RestError> { let local_proxy_uri = ::http::Uri::from_static("http://proxy.local/sql"); let mut req = Request::builder().method(Method::POST).uri(local_proxy_uri); let req_headers = req.headers_mut().expect("failed to get headers"); // Add all provided headers to the request for (header_name, header_value) in headers { req_headers.insert(header_name, header_value.clone()); } let body_boxed = Full::new(Bytes::from(body)) .map_err(|never| match never {}) // Convert Infallible to hyper::Error .boxed(); let req = req.body(body_boxed).map_err(|_| { RestError::SubzeroCore(InternalError { message: "Failed to build request".to_string(), }) })?; // Send the request to the local proxy client .inner .inner .send_request(req) .await .map_err(LocalProxyConnError::from) .map_err(HttpConnError::from) .map_err(RestError::from) } pub(crate) async fn handle( config: &'static ProxyConfig, ctx: RequestContext, request: Request<Incoming>, backend: Arc<PoolingBackend>, cancel: CancellationToken, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, ApiError> { let result = handle_inner(cancel, config, &ctx, request, backend).await; let response = match result { Ok(r) => { ctx.set_success(); // Handling the error response from local proxy here if r.status().is_server_error() { let status = r.status(); let body_bytes = r .collect() .await .map_err(|e| { ApiError::InternalServerError(anyhow::Error::msg(format!( "could not collect http body: {e}" ))) })? .to_bytes(); if let Ok(mut json_map) = serde_json::from_slice::<IndexMap<&str, &RawValue>>(&body_bytes) { let message = json_map.get("message"); if let Some(message) = message { let msg: String = match serde_json::from_str(message.get()) { Ok(msg) => msg, Err(_) => { "Unable to parse the response message from server".to_string() } }; error!("Error response from local_proxy: {status} {msg}"); json_map.retain(|key, _| !key.starts_with("neon:")); // remove all the neon-related keys let resp_json = serde_json::to_string(&json_map) .unwrap_or("failed to serialize the response message".to_string()); return json_response(status, resp_json); } } error!("Unable to parse the response message from local_proxy"); return json_response( status, json!({ "message": "Unable to parse the response message from server".to_string() }), ); } r } Err(e @ RestError::SubzeroCore(_)) => { let error_kind = e.get_error_kind(); ctx.set_error_kind(error_kind); tracing::info!( kind=error_kind.to_metric_label(), error=%e, msg="subzero core error", "forwarding error to user" ); let RestError::SubzeroCore(subzero_err) = e else { panic!("expected subzero core error") }; let json_body = subzero_err.json_body(); let status_code = StatusCode::from_u16(subzero_err.status_code()) .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR); json_response(status_code, json_body)? } Err(e) => { let error_kind = e.get_error_kind(); ctx.set_error_kind(error_kind); let message = e.to_string_client(); let status_code = e.get_http_status_code(); tracing::info!( kind=error_kind.to_metric_label(), error=%e, msg=message, "forwarding error to user" ); let (code, detail, hint) = match e { RestError::Postgres(e) => ( if e.code.starts_with("PT") { None } else { Some(e.code) }, e.detail, e.hint, ), _ => (None, None, None), }; json_response( status_code, json!({ "message": message, "code": code, "detail": detail, "hint": hint, }), )? } }; Ok(response) } async fn handle_inner( _cancel: CancellationToken, config: &'static ProxyConfig, ctx: &RequestContext, request: Request<Incoming>, backend: Arc<PoolingBackend>, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, RestError> { let _requeset_gauge = Metrics::get() .proxy .connection_requests .guard(ctx.protocol()); info!( protocol = %ctx.protocol(), "handling interactive connection from client" ); // Read host from Host, then URI host as fallback // TODO: will this be a problem if behind a load balancer? // TODO: can we use the x-forwarded-host header? let host = request .headers() .get(HOST) .and_then(|v| v.to_str().ok()) .unwrap_or_else(|| request.uri().host().unwrap_or("")); // a valid path is /database/rest/v1/... so splitting should be ["", "database", "rest", "v1", ...] let database_name = request .uri() .path() .split('/') .nth(1) .ok_or(RestError::SubzeroCore(NotFound { target: request.uri().path().to_string(), }))?; // we always use the authenticator role to connect to the database let authenticator_role = "authenticator"; // Strip the hostname prefix from the host to get the database hostname let database_host = host.replace(&config.rest_config.hostname_prefix, ""); let connection_string = format!("postgresql://{authenticator_role}@{database_host}/{database_name}"); let conn_info = get_conn_info( &config.authentication_config, ctx, Some(&connection_string), request.headers(), )?; info!( user = conn_info.conn_info.user_info.user.as_str(), "credentials" ); match conn_info.auth { AuthData::Jwt(jwt) => { let api_prefix = format!("/{database_name}/rest/v1/"); handle_rest_inner( config, ctx, &api_prefix, request, &connection_string, conn_info.conn_info, jwt, backend, ) .await } AuthData::Password(_) => Err(RestError::ConnInfo(ConnInfoError::MissingCredentials( Credentials::BearerJwt, ))), } } fn apply_common_cors_headers( response: &mut Builder, request_headers: &HeaderMap, allowed_origins: Option<&Vec<String>>, ) { let request_origin = request_headers .get(ORIGIN) .map(|v| v.to_str().unwrap_or("")); let response_allow_origin = match (request_origin, allowed_origins) { (Some(or), Some(allowed_origins)) => { if allowed_origins.iter().any(|o| o == or) { Some(HeaderValue::from_str(or).unwrap_or(HEADER_VALUE_ALLOW_ALL_ORIGINS)) } else { None } } (Some(_), None) => Some(HEADER_VALUE_ALLOW_ALL_ORIGINS), _ => None, }; if let Some(h) = response.headers_mut() { h.insert( ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_EXPOSE_HEADERS_VALUE, ); if let Some(origin) = response_allow_origin { h.insert(ACCESS_CONTROL_ALLOW_ORIGIN, origin); } } } #[allow(clippy::too_many_arguments)] async fn handle_rest_inner( config: &'static ProxyConfig, ctx: &RequestContext, api_prefix: &str, request: Request<Incoming>, connection_string: &str, conn_info: ConnInfo, jwt: String, backend: Arc<PoolingBackend>, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, RestError> { let db_schema_cache = config .rest_config .db_schema_cache .as_ref() .ok_or(RestError::SubzeroCore(InternalError { message: "DB schema cache is not configured".to_string(), }))?; let endpoint_cache_key = conn_info .endpoint_cache_key() .ok_or(RestError::SubzeroCore(InternalError { message: "Failed to get endpoint cache key".to_string(), }))?; let (parts, originial_body) = request.into_parts(); // try and get the cached entry for this endpoint // it contains the api config and the introspected db schema let cached_entry = db_schema_cache.get_cached(&endpoint_cache_key); let allowed_origins = cached_entry .as_ref() .and_then(|arc| arc.0.server_cors_allowed_origins.as_ref()); let mut response = Response::builder(); apply_common_cors_headers(&mut response, &parts.headers, allowed_origins); // handle the OPTIONS request if parts.method == Method::OPTIONS { let allowed_headers = parts .headers .get(ACCESS_CONTROL_REQUEST_HEADERS) .and_then(|a| a.to_str().ok()) .filter(|v| !v.is_empty()) .map_or_else( || "Authorization".to_string(), |v| format!("{v}, Authorization"), ); return response .status(StatusCode::OK) .header( ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_METHODS_VALUE, ) .header(ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_MAX_AGE_VALUE) .header( ACCESS_CONTROL_ALLOW_HEADERS, HeaderValue::from_str(&allowed_headers) .unwrap_or(ACCESS_CONTROL_ALLOW_HEADERS_VALUE), ) .header(ALLOW, ACCESS_CONTROL_ALLOW_METHODS_VALUE) .body(Empty::new().map_err(|x| match x {}).boxed()) .map_err(|e| { RestError::SubzeroCore(InternalError { message: e.to_string(), }) }); } // validate the jwt token let jwt_parsed = backend .authenticate_with_jwt(ctx, &conn_info.user_info, jwt) .await .map_err(HttpConnError::from)?; let auth_header = parts .headers .get(AUTHORIZATION) .ok_or(RestError::SubzeroCore(InternalError { message: "Authorization header is required".to_string(), }))?; let mut client = backend.connect_to_local_proxy(ctx, conn_info).await?; let entry = match cached_entry { Some(e) => e, None => { // if not cached, get the remote entry (will run the introspection query) db_schema_cache .get_remote( &endpoint_cache_key, auth_header, connection_string, &mut client, ctx, config, ) .await? } }; let (api_config, db_schema_owned) = entry.as_ref(); let db_schema = db_schema_owned.borrow_schema(); let db_schemas = &api_config.db_schemas; // list of schemas available for the api let db_extra_search_path = &api_config.db_extra_search_path; // TODO: use this when we get a replacement for jsonpath_lib // let role_claim_key = &api_config.role_claim_key; // let role_claim_path = format!("${role_claim_key}"); let db_anon_role = &api_config.db_anon_role; let max_rows = api_config.db_max_rows.as_deref(); let db_allowed_select_functions = api_config .db_allowed_select_functions .iter() .map(|s| s.as_str()) .collect::<Vec<_>>(); // extract the jwt claims (we'll need them later to set the role and env) let jwt_claims = match jwt_parsed.keys { ComputeCredentialKeys::JwtPayload(payload_bytes) => { // `payload_bytes` contains the raw JWT payload as Vec<u8> // You can deserialize it back to JSON or parse specific claims let payload: serde_json::Value = serde_json::from_slice(&payload_bytes) .map_err(|e| RestError::SubzeroCore(JsonDeserialize { source: e }))?; Some(payload) } ComputeCredentialKeys::AuthKeys(_) => None, }; // read the role from the jwt claims (and set it to the "anon" role if not present) let (role, authenticated) = match &jwt_claims { Some(claims) => match claims.get("role") { Some(JsonValue::String(r)) => (Some(r), true), _ => (db_anon_role.as_ref(), true), }, None => (db_anon_role.as_ref(), false), }; // do not allow unauthenticated requests when there is no anonymous role setup if let (None, false) = (role, authenticated) { return Err(RestError::SubzeroCore(JwtTokenInvalid {
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/local_conn_pool.rs
proxy/src/serverless/local_conn_pool.rs
//! Manages the pool of connections between local_proxy and postgres. //! //! The pool is keyed by database and role_name, and can contain multiple connections //! shared between users. //! //! The pool manages the pg_session_jwt extension used for authorizing //! requests in the db. //! //! The first time a db/role pair is seen, local_proxy attempts to install the extension //! and grant usage to the role on the given schema. use std::collections::HashMap; use std::pin::pin; use std::sync::Arc; use std::sync::atomic::AtomicUsize; use std::task::{Poll, ready}; use std::time::Duration; use base64::Engine as _; use base64::prelude::BASE64_URL_SAFE_NO_PAD; use ed25519_dalek::{Signature, Signer, SigningKey}; use futures::future::poll_fn; use futures::{Future, FutureExt}; use indexmap::IndexMap; use jose_jwk::jose_b64::base64ct::{Base64UrlUnpadded, Encoding}; use parking_lot::RwLock; use postgres_client::tls::NoTlsStream; use serde_json::value::RawValue; use tokio::net::TcpStream; use tokio::time::Instant; use tokio_util::sync::CancellationToken; use tracing::{debug, error, info, info_span}; use super::backend::HttpConnError; use super::conn_pool_lib::{ Client, ClientDataEnum, ClientInnerCommon, ClientInnerExt, ConnInfo, DbUserConn, EndpointConnPool, }; use super::sql_over_http::SqlOverHttpError; use crate::context::RequestContext; use crate::control_plane::messages::{ColdStartInfo, MetricsAuxInfo}; use crate::metrics::Metrics; pub(crate) const EXT_NAME: &str = "pg_session_jwt"; pub(crate) const EXT_VERSION: &str = "0.3.1"; pub(crate) const EXT_SCHEMA: &str = "auth"; #[derive(Clone)] pub(crate) struct ClientDataLocal { session: tokio::sync::watch::Sender<uuid::Uuid>, cancel: CancellationToken, key: SigningKey, jti: u64, } impl ClientDataLocal { pub fn session(&mut self) -> &mut tokio::sync::watch::Sender<uuid::Uuid> { &mut self.session } pub fn cancel(&mut self) { self.cancel.cancel(); } } pub(crate) struct LocalConnPool<C: ClientInnerExt> { global_pool: Arc<RwLock<EndpointConnPool<C>>>, config: &'static crate::config::HttpConfig, } impl<C: ClientInnerExt> LocalConnPool<C> { pub(crate) fn new(config: &'static crate::config::HttpConfig) -> Arc<Self> { Arc::new(Self { global_pool: Arc::new(RwLock::new(EndpointConnPool::new( HashMap::new(), 0, config.pool_options.max_conns_per_endpoint, Arc::new(AtomicUsize::new(0)), config.pool_options.max_total_conns, String::from("local_pool"), ))), config, }) } pub(crate) fn get_idle_timeout(&self) -> Duration { self.config.pool_options.idle_timeout } pub(crate) fn get( self: &Arc<Self>, ctx: &RequestContext, conn_info: &ConnInfo, ) -> Result<Option<Client<C>>, HttpConnError> { let client = self .global_pool .write() .get_conn_entry(conn_info.db_and_user()) .map(|entry| entry.conn); // ok return cached connection if found and establish a new one otherwise if let Some(mut client) = client { if client.inner.is_closed() { info!("local_pool: cached connection '{conn_info}' is closed, opening a new one"); return Ok(None); } tracing::Span::current() .record("conn_id", tracing::field::display(client.get_conn_id())); tracing::Span::current().record( "pid", tracing::field::display(client.inner.get_process_id()), ); debug!( cold_start_info = ColdStartInfo::HttpPoolHit.as_str(), "local_pool: reusing connection '{conn_info}'" ); match client.get_data() { ClientDataEnum::Local(data) => { data.session().send(ctx.session_id())?; } ClientDataEnum::Remote(data) => { data.session().send(ctx.session_id())?; } ClientDataEnum::Http(_) => (), } ctx.set_cold_start_info(ColdStartInfo::HttpPoolHit); ctx.success(); return Ok(Some(Client::new( client, conn_info.clone(), Arc::downgrade(&self.global_pool), ))); } Ok(None) } pub(crate) fn initialized(self: &Arc<Self>, conn_info: &ConnInfo) -> bool { if let Some(pool) = self.global_pool.read().get_pool(conn_info.db_and_user()) { return pool.is_initialized(); } false } pub(crate) fn set_initialized(self: &Arc<Self>, conn_info: &ConnInfo) { if let Some(pool) = self .global_pool .write() .get_pool_mut(conn_info.db_and_user()) { pool.set_initialized(); } } } #[allow(clippy::too_many_arguments)] pub(crate) fn poll_client<C: ClientInnerExt>( global_pool: Arc<LocalConnPool<C>>, ctx: &RequestContext, conn_info: ConnInfo, client: C, mut connection: postgres_client::Connection<TcpStream, NoTlsStream>, key: SigningKey, conn_id: uuid::Uuid, aux: MetricsAuxInfo, ) -> Client<C> { let conn_gauge = Metrics::get().proxy.db_connections.guard(ctx.protocol()); let mut session_id = ctx.session_id(); let (tx, mut rx) = tokio::sync::watch::channel(session_id); let span = info_span!(parent: None, "connection", %conn_id); let cold_start_info = ctx.cold_start_info(); span.in_scope(|| { info!(cold_start_info = cold_start_info.as_str(), %conn_info, %session_id, "new connection"); }); let pool = Arc::downgrade(&global_pool); let db_user = conn_info.db_and_user(); let idle = global_pool.get_idle_timeout(); let cancel = CancellationToken::new(); let cancelled = cancel.clone().cancelled_owned(); tokio::spawn(async move { let _conn_gauge = conn_gauge; let mut idle_timeout = pin!(tokio::time::sleep(idle)); let mut cancelled = pin!(cancelled); poll_fn(move |cx| { let _instrument = span.enter(); if cancelled.as_mut().poll(cx).is_ready() { info!("connection dropped"); return Poll::Ready(()); } match rx.has_changed() { Ok(true) => { session_id = *rx.borrow_and_update(); info!(%session_id, "changed session"); idle_timeout.as_mut().reset(Instant::now() + idle); } Err(_) => { info!("connection dropped"); return Poll::Ready(()); } _ => {} } // 5 minute idle connection timeout if idle_timeout.as_mut().poll(cx).is_ready() { idle_timeout.as_mut().reset(Instant::now() + idle); info!("connection idle"); if let Some(pool) = pool.clone().upgrade() { // remove client from pool - should close the connection if it's idle. // does nothing if the client is currently checked-out and in-use if pool .global_pool .write() .remove_client(db_user.clone(), conn_id) { info!("idle connection removed"); } } } match ready!(connection.poll_unpin(cx)) { Err(e) => error!(%session_id, "connection error: {}", e), Ok(()) => info!("connection closed"), } // remove from connection pool if let Some(pool) = pool.clone().upgrade() && pool .global_pool .write() .remove_client(db_user.clone(), conn_id) { info!("closed connection removed"); } Poll::Ready(()) }) .await; }); let inner = ClientInnerCommon { inner: client, aux, conn_id, data: ClientDataEnum::Local(ClientDataLocal { session: tx, cancel, key, jti: 0, }), }; Client::new(inner, conn_info, Arc::downgrade(&global_pool.global_pool)) } impl ClientInnerCommon<postgres_client::Client> { pub(crate) async fn set_jwt_session(&mut self, payload: &[u8]) -> Result<(), SqlOverHttpError> { if let ClientDataEnum::Local(local_data) = &mut self.data { local_data.jti += 1; let token = resign_jwt(&local_data.key, payload, local_data.jti)?; // initiates the auth session // this is safe from query injections as the jwt format free of any escape characters. let query = format!("select auth.jwt_session_init('{token}')"); self.inner .batch_execute(&query) .await .map_err(SqlOverHttpError::InternalPostgres)?; let pid = self.inner.get_process_id(); info!(pid, jti = local_data.jti, "user session state init"); Ok(()) } else { panic!("unexpected client data type"); } } } /// implements relatively efficient in-place json object key upserting /// /// only supports top-level keys fn upsert_json_object( payload: &[u8], key: &str, value: &RawValue, ) -> Result<String, serde_json::Error> { let mut payload = serde_json::from_slice::<IndexMap<&str, &RawValue>>(payload)?; payload.insert(key, value); serde_json::to_string(&payload) } fn resign_jwt(sk: &SigningKey, payload: &[u8], jti: u64) -> Result<String, HttpConnError> { let mut buffer = itoa::Buffer::new(); // encode the jti integer to a json rawvalue let jti = serde_json::from_str::<&RawValue>(buffer.format(jti)) .expect("itoa formatted integer should be guaranteed valid json"); // update the jti in-place let payload = upsert_json_object(payload, "jti", jti).map_err(HttpConnError::JwtPayloadError)?; // sign the jwt let token = sign_jwt(sk, payload.as_bytes()); Ok(token) } fn sign_jwt(sk: &SigningKey, payload: &[u8]) -> String { let header_len = 20; let payload_len = Base64UrlUnpadded::encoded_len(payload); let signature_len = Base64UrlUnpadded::encoded_len(&[0; 64]); let total_len = header_len + payload_len + signature_len + 2; let mut jwt = String::with_capacity(total_len); let cap = jwt.capacity(); // we only need an empty header with the alg specified. // base64url(r#"{"alg":"EdDSA"}"#) == "eyJhbGciOiJFZERTQSJ9" jwt.push_str("eyJhbGciOiJFZERTQSJ9."); // encode the jwt payload in-place BASE64_URL_SAFE_NO_PAD.encode_string(payload, &mut jwt); // create the signature from the encoded header || payload let sig: Signature = sk.sign(jwt.as_bytes()); jwt.push('.'); // encode the jwt signature in-place BASE64_URL_SAFE_NO_PAD.encode_string(sig.to_bytes(), &mut jwt); debug_assert_eq!( jwt.len(), total_len, "the jwt len should match our expected len" ); debug_assert_eq!(jwt.capacity(), cap, "the jwt capacity should not change"); jwt } #[cfg(test)] mod tests { use ed25519_dalek::SigningKey; use typed_json::json; use super::resign_jwt; #[test] fn jwt_token_snapshot() { let key = SigningKey::from_bytes(&[1; 32]); let data = json!({"foo":"bar","jti":"foo\nbar","nested":{"jti":"tricky nesting"}}).to_string(); let jwt = resign_jwt(&key, data.as_bytes(), 2).unwrap(); // To validate the JWT, copy the JWT string and paste it into https://jwt.io/. // In the public-key box, paste the following jwk public key // `{"kty":"OKP","crv":"Ed25519","x":"iojj3XQJ8ZX9UtstPLpdcspnCb8dlBIb83SIAbQPb1w"}` // Note - jwt.io doesn't support EdDSA :( // https://github.com/jsonwebtoken/jsonwebtoken.github.io/issues/509 // let jwk = jose_jwk::Key::Okp(jose_jwk::Okp { // crv: jose_jwk::OkpCurves::Ed25519, // x: jose_jwk::jose_b64::serde::Bytes::from(key.verifying_key().to_bytes().to_vec()), // d: None, // }); // println!("{}", serde_json::to_string(&jwk).unwrap()); assert_eq!( jwt, "eyJhbGciOiJFZERTQSJ9.eyJmb28iOiJiYXIiLCJqdGkiOjIsIm5lc3RlZCI6eyJqdGkiOiJ0cmlja3kgbmVzdGluZyJ9fQ.Cvyc2By33KI0f0obystwdy8PN111L3Sc9_Mr2CU3XshtSqSdxuRxNEZGbb_RvyJf2IzheC_s7aBZ-jLeQ9N0Bg" ); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/websocket.rs
proxy/src/serverless/websocket.rs
use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll, ready}; use anyhow::Context as _; use bytes::{Buf, BufMut, Bytes, BytesMut}; use framed_websockets::{Frame, OpCode, WebSocketServer}; use futures::{Sink, Stream}; use hyper::upgrade::OnUpgrade; use hyper_util::rt::TokioIo; use pin_project_lite::pin_project; use tokio::io::{self, AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf}; use tracing::warn; use crate::cancellation::CancellationHandler; use crate::config::ProxyConfig; use crate::context::RequestContext; use crate::error::ReportableError; use crate::metrics::Metrics; use crate::pglb::{ClientMode, handle_connection}; use crate::proxy::ErrorSource; use crate::rate_limiter::EndpointRateLimiter; pin_project! { /// This is a wrapper around a [`WebSocketStream`] that /// implements [`AsyncRead`] and [`AsyncWrite`]. pub(crate) struct WebSocketRw<S> { #[pin] stream: WebSocketServer<S>, recv: Bytes, send: BytesMut, } } impl<S> WebSocketRw<S> { pub(crate) fn new(stream: WebSocketServer<S>) -> Self { Self { stream, recv: Bytes::new(), send: BytesMut::new(), } } } impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for WebSocketRw<S> { fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<io::Result<usize>> { let this = self.project(); let mut stream = this.stream; ready!(stream.as_mut().poll_ready(cx).map_err(io::Error::other))?; this.send.put(buf); match stream.as_mut().start_send(Frame::binary(this.send.split())) { Ok(()) => Poll::Ready(Ok(buf.len())), Err(e) => Poll::Ready(Err(io::Error::other(e))), } } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { let stream = self.project().stream; stream.poll_flush(cx).map_err(io::Error::other) } fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { let stream = self.project().stream; stream.poll_close(cx).map_err(io::Error::other) } } impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for WebSocketRw<S> { fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<io::Result<()>> { let bytes = ready!(self.as_mut().poll_fill_buf(cx))?; let len = std::cmp::min(bytes.len(), buf.remaining()); buf.put_slice(&bytes[..len]); self.consume(len); Poll::Ready(Ok(())) } } impl<S: AsyncRead + AsyncWrite + Unpin> AsyncBufRead for WebSocketRw<S> { fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> { // Please refer to poll_fill_buf's documentation. const EOF: Poll<io::Result<&[u8]>> = Poll::Ready(Ok(&[])); let mut this = self.project(); loop { if !this.recv.chunk().is_empty() { let chunk = (*this.recv).chunk(); return Poll::Ready(Ok(chunk)); } let res = ready!(this.stream.as_mut().poll_next(cx)); match res.transpose().map_err(io::Error::other)? { Some(message) => match message.opcode { OpCode::Ping => {} OpCode::Pong => {} OpCode::Text => { // We expect to see only binary messages. let error = "unexpected text message in the websocket"; warn!(length = message.payload.len(), error); return Poll::Ready(Err(io::Error::other(error))); } OpCode::Binary | OpCode::Continuation => { debug_assert!(this.recv.is_empty()); *this.recv = message.payload.freeze(); } OpCode::Close => return EOF, }, None => return EOF, } } } fn consume(self: Pin<&mut Self>, amount: usize) { self.project().recv.advance(amount); } } #[allow(clippy::too_many_arguments)] pub(crate) async fn serve_websocket( config: &'static ProxyConfig, auth_backend: &'static crate::auth::Backend<'static, ()>, ctx: RequestContext, websocket: OnUpgrade, cancellation_handler: Arc<CancellationHandler>, endpoint_rate_limiter: Arc<EndpointRateLimiter>, hostname: Option<String>, cancellations: tokio_util::task::task_tracker::TaskTracker, ) -> anyhow::Result<()> { let websocket = websocket.await?; let websocket = WebSocketServer::after_handshake(TokioIo::new(websocket)); let conn_gauge = Metrics::get() .proxy .client_connections .guard(crate::metrics::Protocol::Ws); let res = Box::pin(handle_connection( config, auth_backend, &ctx, cancellation_handler, WebSocketRw::new(websocket), ClientMode::Websockets { hostname }, endpoint_rate_limiter, conn_gauge, cancellations, )) .await; match res { Err(e) => { ctx.set_error_kind(e.get_error_kind()); Err(e.into()) } Ok(None) => { ctx.set_success(); Ok(()) } Ok(Some(p)) => { ctx.set_success(); ctx.log_connect(); match p.proxy_pass().await { Ok(()) => Ok(()), Err(ErrorSource::Client(err)) => Err(err).context("client"), Err(ErrorSource::Compute(err)) => Err(err).context("compute"), } } } } #[cfg(test)] mod tests { use std::pin::pin; use framed_websockets::WebSocketServer; use futures::{SinkExt, StreamExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt, duplex}; use tokio::task::JoinSet; use tokio_tungstenite::WebSocketStream; use tokio_tungstenite::tungstenite::Message; use tokio_tungstenite::tungstenite::protocol::Role; use super::WebSocketRw; #[tokio::test] async fn websocket_stream_wrapper_happy_path() { let (stream1, stream2) = duplex(1024); let mut js = JoinSet::new(); js.spawn(async move { let mut client = WebSocketStream::from_raw_socket(stream1, Role::Client, None).await; client .send(Message::Binary(b"hello world".to_vec())) .await .unwrap(); let message = client.next().await.unwrap().unwrap(); assert_eq!(message, Message::Binary(b"websockets are cool".to_vec())); client.close(None).await.unwrap(); }); js.spawn(async move { let mut rw = pin!(WebSocketRw::new(WebSocketServer::after_handshake(stream2))); let mut buf = vec![0; 1024]; let n = rw.read(&mut buf).await.unwrap(); assert_eq!(&buf[..n], b"hello world"); rw.write_all(b"websockets are cool").await.unwrap(); rw.flush().await.unwrap(); let n = rw.read_to_end(&mut buf).await.unwrap(); assert_eq!(n, 0); }); js.join_next().await.unwrap().unwrap(); js.join_next().await.unwrap().unwrap(); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/backend.rs
proxy/src/serverless/backend.rs
use std::sync::Arc; use std::time::Duration; use ed25519_dalek::SigningKey; use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer}; use jose_jwk::jose_b64; use postgres_client::error::SqlState; use postgres_client::maybe_tls_stream::MaybeTlsStream; use rand_core::OsRng; use tracing::field::display; use tracing::{debug, info}; use super::AsyncRW; use super::conn_pool::poll_client; use super::conn_pool_lib::{Client, ConnInfo, EndpointConnPool, GlobalConnPool}; use super::http_conn_pool::{self, HttpConnPool, LocalProxyClient, poll_http2_client}; use super::local_conn_pool::{self, EXT_NAME, EXT_SCHEMA, EXT_VERSION, LocalConnPool}; use crate::auth::backend::local::StaticAuthRules; use crate::auth::backend::{ComputeCredentials, ComputeUserInfo}; use crate::auth::{self, AuthError}; use crate::compute; use crate::compute_ctl::{ ComputeCtlError, ExtensionInstallRequest, Privilege, SetRoleGrantsRequest, }; use crate::config::ProxyConfig; use crate::context::RequestContext; use crate::control_plane::client::ApiLockError; use crate::control_plane::errors::{GetAuthInfoError, WakeComputeError}; use crate::error::{ErrorKind, ReportableError, UserFacingError}; use crate::intern::{EndpointIdInt, RoleNameInt}; use crate::pqproto::StartupMessageParams; use crate::proxy::{connect_auth, connect_compute}; use crate::rate_limiter::EndpointRateLimiter; use crate::types::{EndpointId, LOCAL_PROXY_SUFFIX}; pub(crate) struct PoolingBackend { pub(crate) http_conn_pool: Arc<GlobalConnPool<LocalProxyClient, HttpConnPool<LocalProxyClient>>>, pub(crate) local_pool: Arc<LocalConnPool<postgres_client::Client>>, pub(crate) pool: Arc<GlobalConnPool<postgres_client::Client, EndpointConnPool<postgres_client::Client>>>, pub(crate) config: &'static ProxyConfig, pub(crate) auth_backend: &'static crate::auth::Backend<'static, ()>, pub(crate) endpoint_rate_limiter: Arc<EndpointRateLimiter>, } impl PoolingBackend { pub(crate) async fn authenticate_with_password( &self, ctx: &RequestContext, user_info: &ComputeUserInfo, password: &[u8], ) -> Result<ComputeCredentials, AuthError> { ctx.set_auth_method(crate::context::AuthMethod::Cleartext); let user_info = user_info.clone(); let backend = self.auth_backend.as_ref().map(|()| user_info.clone()); let access_control = backend.get_endpoint_access_control(ctx).await?; access_control.check( ctx, self.config.authentication_config.ip_allowlist_check_enabled, self.config.authentication_config.is_vpc_acccess_proxy, )?; access_control.connection_attempt_rate_limit( ctx, &user_info.endpoint, &self.endpoint_rate_limiter, )?; let role_access = backend.get_role_secret(ctx).await?; let Some(secret) = role_access.secret else { // If we don't have an authentication secret, for the http flow we can just return an error. info!("authentication info not found"); return Err(AuthError::password_failed(&*user_info.user)); }; let ep = EndpointIdInt::from(&user_info.endpoint); let role = RoleNameInt::from(&user_info.user); let auth_outcome = crate::auth::validate_password_and_exchange( &self.config.authentication_config.scram_thread_pool, ep, role, password, secret, ) .await?; let res = match auth_outcome { crate::sasl::Outcome::Success(key) => { info!("user successfully authenticated"); Ok(key) } crate::sasl::Outcome::Failure(reason) => { info!("auth backend failed with an error: {reason}"); Err(AuthError::password_failed(&*user_info.user)) } }; res.map(|key| ComputeCredentials { info: user_info, keys: key, }) } pub(crate) async fn authenticate_with_jwt( &self, ctx: &RequestContext, user_info: &ComputeUserInfo, jwt: String, ) -> Result<ComputeCredentials, AuthError> { ctx.set_auth_method(crate::context::AuthMethod::Jwt); match &self.auth_backend { crate::auth::Backend::ControlPlane(console, ()) => { let keys = self .config .authentication_config .jwks_cache .check_jwt( ctx, user_info.endpoint.clone(), &user_info.user, &**console, &jwt, ) .await?; Ok(ComputeCredentials { info: user_info.clone(), keys, }) } crate::auth::Backend::Local(_) => { let keys = self .config .authentication_config .jwks_cache .check_jwt( ctx, user_info.endpoint.clone(), &user_info.user, &StaticAuthRules, &jwt, ) .await?; Ok(ComputeCredentials { info: user_info.clone(), keys, }) } } } // Wake up the destination if needed. Code here is a bit involved because // we reuse the code from the usual proxy and we need to prepare few structures // that this code expects. #[tracing::instrument(skip_all, fields( pid = tracing::field::Empty, compute_id = tracing::field::Empty, conn_id = tracing::field::Empty, ))] pub(crate) async fn connect_to_compute( &self, ctx: &RequestContext, conn_info: ConnInfo, keys: ComputeCredentials, force_new: bool, ) -> Result<Client<postgres_client::Client>, HttpConnError> { let maybe_client = if force_new { debug!("pool: pool is disabled"); None } else { debug!("pool: looking for an existing connection"); self.pool.get(ctx, &conn_info)? }; if let Some(client) = maybe_client { return Ok(client); } let conn_id = uuid::Uuid::new_v4(); tracing::Span::current().record("conn_id", display(conn_id)); info!(%conn_id, "pool: opening a new connection '{conn_info}'"); let backend = self.auth_backend.as_ref().map(|()| keys.info); let mut params = StartupMessageParams::default(); params.insert("database", &conn_info.dbname); params.insert("user", &conn_info.user_info.user); let mut auth_info = compute::AuthInfo::with_auth_keys(keys.keys); auth_info.set_startup_params(&params, true); let node = connect_auth::connect_to_compute_and_auth( ctx, self.config, &backend, auth_info, connect_compute::TlsNegotiation::Postgres, ) .await?; let (client, connection) = postgres_client::connect::managed( node.stream, Some(node.socket_addr.ip()), postgres_client::config::Host::Tcp(node.hostname.to_string()), node.socket_addr.port(), node.ssl_mode, Some(self.config.connect_to_compute.timeout), ) .await?; Ok(poll_client( self.pool.clone(), ctx, conn_info, client, connection, conn_id, node.aux, )) } // Wake up the destination if needed #[tracing::instrument(skip_all, fields( compute_id = tracing::field::Empty, conn_id = tracing::field::Empty, ))] pub(crate) async fn connect_to_local_proxy( &self, ctx: &RequestContext, conn_info: ConnInfo, ) -> Result<http_conn_pool::Client<LocalProxyClient>, HttpConnError> { debug!("pool: looking for an existing connection"); if let Ok(Some(client)) = self.http_conn_pool.get(ctx, &conn_info) { return Ok(client); } let conn_id = uuid::Uuid::new_v4(); tracing::Span::current().record("conn_id", display(conn_id)); debug!(%conn_id, "pool: opening a new connection '{conn_info}'"); let backend = self.auth_backend.as_ref().map(|()| ComputeUserInfo { user: conn_info.user_info.user.clone(), endpoint: EndpointId::from(format!( "{}{LOCAL_PROXY_SUFFIX}", conn_info.user_info.endpoint.normalize() )), options: conn_info.user_info.options.clone(), }); let node = connect_compute::connect_to_compute( ctx, self.config, &backend, connect_compute::TlsNegotiation::Direct, ) .await?; let stream = match node.stream.into_framed().into_inner() { MaybeTlsStream::Raw(s) => Box::pin(s) as AsyncRW, MaybeTlsStream::Tls(s) => Box::pin(s) as AsyncRW, }; let (client, connection) = hyper::client::conn::http2::Builder::new(TokioExecutor::new()) .timer(TokioTimer::new()) .keep_alive_interval(Duration::from_secs(20)) .keep_alive_while_idle(true) .keep_alive_timeout(Duration::from_secs(5)) .handshake(TokioIo::new(stream)) .await .map_err(LocalProxyConnError::H2)?; Ok(poll_http2_client( self.http_conn_pool.clone(), ctx, &conn_info, client, connection, conn_id, node.aux.clone(), )) } /// Connect to postgres over localhost. /// /// We expect postgres to be started here, so we won't do any retries. /// /// # Panics /// /// Panics if called with a non-local_proxy backend. #[tracing::instrument(skip_all, fields( pid = tracing::field::Empty, conn_id = tracing::field::Empty, ))] pub(crate) async fn connect_to_local_postgres( &self, ctx: &RequestContext, conn_info: ConnInfo, disable_pg_session_jwt: bool, ) -> Result<Client<postgres_client::Client>, HttpConnError> { if let Some(client) = self.local_pool.get(ctx, &conn_info)? { return Ok(client); } let local_backend = match &self.auth_backend { auth::Backend::ControlPlane(_, ()) => { unreachable!("only local_proxy can connect to local postgres") } auth::Backend::Local(local) => local, }; if !self.local_pool.initialized(&conn_info) { // only install and grant usage one at a time. let _permit = local_backend .initialize .acquire() .await .expect("semaphore should never be closed"); // check again for race if !self.local_pool.initialized(&conn_info) && !disable_pg_session_jwt { local_backend .compute_ctl .install_extension(&ExtensionInstallRequest { extension: EXT_NAME, database: conn_info.dbname.clone(), version: EXT_VERSION, }) .await?; local_backend .compute_ctl .grant_role(&SetRoleGrantsRequest { schema: EXT_SCHEMA, privileges: vec![Privilege::Usage], database: conn_info.dbname.clone(), role: conn_info.user_info.user.clone(), }) .await?; self.local_pool.set_initialized(&conn_info); } } let conn_id = uuid::Uuid::new_v4(); tracing::Span::current().record("conn_id", display(conn_id)); info!(%conn_id, "local_pool: opening a new connection '{conn_info}'"); let (key, jwk) = create_random_jwk(); let mut config = local_backend .node_info .conn_info .to_postgres_client_config(); config .user(&conn_info.user_info.user) .dbname(&conn_info.dbname); if !disable_pg_session_jwt { config.set_param( "options", &format!( "-c pg_session_jwt.jwk={}", serde_json::to_string(&jwk).expect("serializing jwk to json should not fail") ), ); } let pause = ctx.latency_timer_pause(crate::metrics::Waiting::Compute); let (client, connection) = config.connect(&postgres_client::NoTls).await?; drop(pause); let pid = client.get_process_id(); tracing::Span::current().record("pid", pid); let mut handle = local_conn_pool::poll_client( self.local_pool.clone(), ctx, conn_info, client, connection, key, conn_id, local_backend.node_info.aux.clone(), ); { let (client, mut discard) = handle.inner(); debug!("setting up backend session state"); // initiates the auth session if !disable_pg_session_jwt && let Err(e) = client.batch_execute("select auth.init();").await { discard.discard(); return Err(e.into()); } info!("backend session state initialized"); } Ok(handle) } } fn create_random_jwk() -> (SigningKey, jose_jwk::Key) { let key = SigningKey::generate(&mut OsRng); let jwk = jose_jwk::Key::Okp(jose_jwk::Okp { crv: jose_jwk::OkpCurves::Ed25519, x: jose_b64::serde::Bytes::from(key.verifying_key().to_bytes().to_vec()), d: None, }); (key, jwk) } #[derive(Debug, thiserror::Error)] pub(crate) enum HttpConnError { #[error("pooled connection closed at inconsistent state")] ConnectionClosedAbruptly(#[from] tokio::sync::watch::error::SendError<uuid::Uuid>), #[error("could not connect to compute")] ConnectError(#[from] compute::ConnectionError), #[error("could not connect to postgres in compute")] PostgresConnectionError(#[from] postgres_client::Error), #[error("could not connect to local-proxy in compute")] LocalProxyConnectionError(#[from] LocalProxyConnError), #[error("could not parse JWT payload")] JwtPayloadError(serde_json::Error), #[error("could not install extension: {0}")] ComputeCtl(#[from] ComputeCtlError), #[error("could not get auth info")] GetAuthInfo(#[from] GetAuthInfoError), #[error("user not authenticated")] AuthError(#[from] AuthError), #[error("wake_compute returned error")] WakeCompute(#[from] WakeComputeError), #[error("error acquiring resource permit: {0}")] TooManyConnectionAttempts(#[from] ApiLockError), } impl From<connect_auth::AuthError> for HttpConnError { fn from(value: connect_auth::AuthError) -> Self { match value { connect_auth::AuthError::Auth(compute::PostgresError::Postgres(error)) => { Self::PostgresConnectionError(error) } connect_auth::AuthError::Connect(error) => Self::ConnectError(error), } } } #[derive(Debug, thiserror::Error)] pub(crate) enum LocalProxyConnError { #[error("could not establish h2 connection")] H2(#[from] hyper::Error), } impl ReportableError for HttpConnError { fn get_error_kind(&self) -> ErrorKind { match self { HttpConnError::ConnectError(e) => e.get_error_kind(), HttpConnError::ConnectionClosedAbruptly(_) => ErrorKind::Compute, HttpConnError::PostgresConnectionError(p) => match p.as_db_error() { // user provided a wrong database name Some(err) if err.code() == &SqlState::INVALID_CATALOG_NAME => ErrorKind::User, // postgres rejected the connection Some(_) => ErrorKind::Postgres, // couldn't even reach postgres None => ErrorKind::Compute, }, HttpConnError::LocalProxyConnectionError(_) => ErrorKind::Compute, HttpConnError::ComputeCtl(_) => ErrorKind::Service, HttpConnError::JwtPayloadError(_) => ErrorKind::User, HttpConnError::GetAuthInfo(a) => a.get_error_kind(), HttpConnError::AuthError(a) => a.get_error_kind(), HttpConnError::WakeCompute(w) => w.get_error_kind(), HttpConnError::TooManyConnectionAttempts(w) => w.get_error_kind(), } } } impl UserFacingError for HttpConnError { fn to_string_client(&self) -> String { match self { HttpConnError::ConnectError(p) => p.to_string_client(), HttpConnError::ConnectionClosedAbruptly(_) => self.to_string(), HttpConnError::PostgresConnectionError(p) => p.to_string(), HttpConnError::LocalProxyConnectionError(p) => p.to_string(), HttpConnError::ComputeCtl(_) => "could not set up the JWT authorization database extension".to_string(), HttpConnError::JwtPayloadError(p) => p.to_string(), HttpConnError::GetAuthInfo(c) => c.to_string_client(), HttpConnError::AuthError(c) => c.to_string_client(), HttpConnError::WakeCompute(c) => c.to_string_client(), HttpConnError::TooManyConnectionAttempts(_) => { "Failed to acquire permit to connect to the database. Too many database connection attempts are currently ongoing.".to_owned() } } } } impl ReportableError for LocalProxyConnError { fn get_error_kind(&self) -> ErrorKind { match self { LocalProxyConnError::H2(_) => ErrorKind::Compute, } } } impl UserFacingError for LocalProxyConnError { fn to_string_client(&self) -> String { "Could not establish HTTP connection to the database".to_string() } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/json.rs
proxy/src/serverless/json.rs
use json::{ListSer, ObjectSer, ValueSer}; use postgres_client::Row; use postgres_client::types::{Kind, Type}; use serde_json::Value; // // Convert json non-string types to strings, so that they can be passed to Postgres // as parameters. // pub(crate) fn json_to_pg_text(json: Vec<Value>) -> Vec<Option<String>> { json.iter().map(json_value_to_pg_text).collect() } fn json_value_to_pg_text(value: &Value) -> Option<String> { match value { // special care for nulls Value::Null => None, // convert to text with escaping v @ (Value::Bool(_) | Value::Number(_) | Value::Object(_)) => Some(v.to_string()), // avoid escaping here, as we pass this as a parameter Value::String(s) => Some(s.clone()), // special care for arrays Value::Array(_) => json_array_to_pg_array(value), } } // // Serialize a JSON array to a Postgres array. Contrary to the strings in the params // in the array we need to escape the strings. Postgres is okay with arrays of form // '{1,"2",3}'::int[], so we don't check that array holds values of the same type, leaving // it for Postgres to check. // // Example of the same escaping in node-postgres: packages/pg/lib/utils.js // fn json_array_to_pg_array(value: &Value) -> Option<String> { match value { // special care for nulls Value::Null => None, // convert to text with escaping // here string needs to be escaped, as it is part of the array v @ (Value::Bool(_) | Value::Number(_) | Value::String(_)) => Some(v.to_string()), v @ Value::Object(_) => json_array_to_pg_array(&Value::String(v.to_string())), // recurse into array Value::Array(arr) => { let vals = arr .iter() .map(json_array_to_pg_array) .map(|v| v.unwrap_or_else(|| "NULL".to_string())) .collect::<Vec<_>>() .join(","); Some(format!("{{{vals}}}")) } } } #[derive(Debug, thiserror::Error)] pub(crate) enum JsonConversionError { #[error("internal error compute returned invalid data: {0}")] AsTextError(postgres_client::Error), #[error("parse int error: {0}")] ParseIntError(#[from] std::num::ParseIntError), #[error("parse float error: {0}")] ParseFloatError(#[from] std::num::ParseFloatError), #[error("parse json error: {0}")] ParseJsonError(#[from] serde_json::Error), #[error("unbalanced array")] UnbalancedArray, #[error("unbalanced quoted string")] UnbalancedString, } enum OutputMode<'a> { Array(ListSer<'a>), Object(ObjectSer<'a>), } impl OutputMode<'_> { fn key(&mut self, key: &str) -> ValueSer<'_> { match self { OutputMode::Array(values) => values.entry(), OutputMode::Object(map) => map.key(key), } } fn finish(self) { match self { OutputMode::Array(values) => values.finish(), OutputMode::Object(map) => map.finish(), } } } // // Convert postgres row with text-encoded values to JSON object // pub(crate) fn pg_text_row_to_json( output: ValueSer, row: &Row, raw_output: bool, array_mode: bool, ) -> Result<(), JsonConversionError> { let mut entries = if array_mode { OutputMode::Array(output.list()) } else { OutputMode::Object(output.object()) }; for (i, column) in row.columns().iter().enumerate() { let pg_value = row.as_text(i).map_err(JsonConversionError::AsTextError)?; let value = entries.key(column.name()); match pg_value { Some(v) if raw_output => value.value(v), Some(v) => pg_text_to_json(value, v, column.type_())?, None => value.value(json::Null), } } entries.finish(); Ok(()) } // // Convert postgres text-encoded value to JSON value // fn pg_text_to_json(output: ValueSer, val: &str, pg_type: &Type) -> Result<(), JsonConversionError> { if let Kind::Array(elem_type) = pg_type.kind() { // todo: we should fetch this from postgres. let delimiter = ','; json::value_as_list!(|output| pg_array_parse(output, val, elem_type, delimiter)?); return Ok(()); } match *pg_type { Type::BOOL => output.value(val == "t"), Type::INT2 | Type::INT4 => { let val = val.parse::<i32>()?; output.value(val); } Type::FLOAT4 | Type::FLOAT8 => { let fval = val.parse::<f64>()?; if fval.is_finite() { output.value(fval); } else { // Pass Nan, Inf, -Inf as strings // JS JSON.stringify() does converts them to null, but we // want to preserve them, so we pass them as strings output.value(val); } } // we assume that the string value is valid json. Type::JSON | Type::JSONB => output.write_raw_json(val.as_bytes()), _ => output.value(val), } Ok(()) } /// Parse postgres array into JSON array. /// /// This is a bit involved because we need to handle nested arrays and quoted /// values. Unlike postgres we don't check that all nested arrays have the same /// dimensions, we just return them as is. /// /// <https://www.postgresql.org/docs/current/arrays.html#ARRAYS-IO> /// /// The external text representation of an array value consists of items that are interpreted /// according to the I/O conversion rules for the array's element type, plus decoration that /// indicates the array structure. The decoration consists of curly braces (`{` and `}`) around /// the array value plus delimiter characters between adjacent items. The delimiter character /// is usually a comma (,) but can be something else: it is determined by the typdelim setting /// for the array's element type. Among the standard data types provided in the PostgreSQL /// distribution, all use a comma, except for type box, which uses a semicolon (;). /// /// In a multidimensional array, each dimension (row, plane, cube, etc.) /// gets its own level of curly braces, and delimiters must be written between adjacent /// curly-braced entities of the same level. fn pg_array_parse( elements: &mut ListSer, mut pg_array: &str, elem: &Type, delim: char, ) -> Result<(), JsonConversionError> { // skip bounds decoration, eg: // `[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}` // technically these are significant, but we have no way to represent them in json. if let Some('[') = pg_array.chars().next() { let Some((_bounds, array)) = pg_array.split_once('=') else { return Err(JsonConversionError::UnbalancedArray); }; pg_array = array; } // whitespace might preceed a `{`. let pg_array = pg_array.trim_start(); let rest = pg_array_parse_inner(elements, pg_array, elem, delim)?; if !rest.is_empty() { return Err(JsonConversionError::UnbalancedArray); } Ok(()) } /// reads a single array from the `pg_array` string and pushes each values to `elements`. /// returns the rest of the `pg_array` string that was not read. fn pg_array_parse_inner<'a>( elements: &mut ListSer, mut pg_array: &'a str, elem: &Type, delim: char, ) -> Result<&'a str, JsonConversionError> { // array should have a `{` prefix. pg_array = pg_array .strip_prefix('{') .ok_or(JsonConversionError::UnbalancedArray)?; let mut q = String::new(); loop { let value = elements.entry(); pg_array = pg_array_parse_item(value, &mut q, pg_array, elem, delim)?; // check for separator. if let Some(next) = pg_array.strip_prefix(delim) { // next item. pg_array = next; } else { break; } } let Some(next) = pg_array.strip_prefix('}') else { // missing `}` terminator. return Err(JsonConversionError::UnbalancedArray); }; // whitespace might follow a `}`. Ok(next.trim_start()) } /// reads a single item from the `pg_array` string. /// returns the rest of the `pg_array` string that was not read. /// /// `quoted` is a scratch allocation that has no defined output. fn pg_array_parse_item<'a>( output: ValueSer, quoted: &mut String, mut pg_array: &'a str, elem: &Type, delim: char, ) -> Result<&'a str, JsonConversionError> { // We are trying to parse an array item. // This could be a new array, if this is a multi-dimentional array. // This could be a quoted string representing `elem`. // This could be an unquoted string representing `elem`. // whitespace might preceed an item. pg_array = pg_array.trim_start(); if pg_array.starts_with('{') { // nested array. pg_array = json::value_as_list!(|output| pg_array_parse_inner(output, pg_array, elem, delim))?; return Ok(pg_array); } if let Some(mut pg_array) = pg_array.strip_prefix('"') { // the parsed string is un-escaped and written into quoted. pg_array = pg_array_parse_quoted(quoted, pg_array)?; // we have un-escaped the string, parse it as pgtext. pg_text_to_json(output, quoted, elem)?; return Ok(pg_array); } // we need to parse an item. read until we find a delimiter or `}`. let index = pg_array .find([delim, '}']) .ok_or(JsonConversionError::UnbalancedArray)?; let item; (item, pg_array) = pg_array.split_at(index); // item might have trailing whitespace that we need to ignore. let item = item.trim_end(); // we might have an item string: // check for null if item == "NULL" { output.value(json::Null); } else { pg_text_to_json(output, item, elem)?; } Ok(pg_array) } /// reads a single quoted item from the `pg_array` string. /// /// Returns the rest of the `pg_array` string that was not read. /// The output is written into `quoted`. /// /// The pg_array string must have a `"` terminator, but the `"` initial value /// must have already been removed from the input. The terminator is removed. fn pg_array_parse_quoted<'a>( quoted: &mut String, mut pg_array: &'a str, ) -> Result<&'a str, JsonConversionError> { // The array output routine will put double quotes around element values if they are empty strings, // contain curly braces, delimiter characters, double quotes, backslashes, or white space, // or match the word `NULL`. Double quotes and backslashes embedded in element values will be backslash-escaped. // For numeric data types it is safe to assume that double quotes will never appear, // but for textual data types one should be prepared to cope with either the presence or absence of quotes. quoted.clear(); // We write to quoted in chunks terminated by an escape character. // Eg if we have the input `foo\"bar"`, then we write `foo`, then `"`, then finally `bar`. loop { // we need to parse an chunk. read until we find a '\\' or `"`. let i = pg_array .find(['\\', '"']) .ok_or(JsonConversionError::UnbalancedString)?; let chunk: &str; (chunk, pg_array) = pg_array .split_at_checked(i) .expect("i is guaranteed to be in-bounds of pg_array"); // push the chunk. quoted.push_str(chunk); // consume the chunk_end character. let chunk_end: char; (chunk_end, pg_array) = split_first_char(pg_array).expect("pg_array should start with either '\\\\' or '\"'"); // finished. if chunk_end == '"' { // whitespace might follow the '"'. pg_array = pg_array.trim_start(); break Ok(pg_array); } // consume the escaped character. let escaped: char; (escaped, pg_array) = split_first_char(pg_array).ok_or(JsonConversionError::UnbalancedString)?; quoted.push(escaped); } } fn split_first_char(s: &str) -> Option<(char, &str)> { let mut chars = s.chars(); let c = chars.next()?; Some((c, chars.as_str())) } #[cfg(test)] mod tests { use serde_json::json; use super::*; #[test] fn test_atomic_types_to_pg_params() { let json = vec![Value::Bool(true), Value::Bool(false)]; let pg_params = json_to_pg_text(json); assert_eq!( pg_params, vec![Some("true".to_owned()), Some("false".to_owned())] ); let json = vec![Value::Number(serde_json::Number::from(42))]; let pg_params = json_to_pg_text(json); assert_eq!(pg_params, vec![Some("42".to_owned())]); let json = vec![Value::String("foo\"".to_string())]; let pg_params = json_to_pg_text(json); assert_eq!(pg_params, vec![Some("foo\"".to_owned())]); let json = vec![Value::Null]; let pg_params = json_to_pg_text(json); assert_eq!(pg_params, vec![None]); } #[test] fn test_json_array_to_pg_array() { // atoms and escaping let json = "[true, false, null, \"NULL\", 42, \"foo\", \"bar\\\"-\\\\\"]"; let json: Value = serde_json::from_str(json).unwrap(); let pg_params = json_to_pg_text(vec![json]); assert_eq!( pg_params, vec![Some( "{true,false,NULL,\"NULL\",42,\"foo\",\"bar\\\"-\\\\\"}".to_owned() )] ); // nested arrays let json = "[[true, false], [null, 42], [\"foo\", \"bar\\\"-\\\\\"]]"; let json: Value = serde_json::from_str(json).unwrap(); let pg_params = json_to_pg_text(vec![json]); assert_eq!( pg_params, vec![Some( "{{true,false},{NULL,42},{\"foo\",\"bar\\\"-\\\\\"}}".to_owned() )] ); // array of objects let json = r#"[{"foo": 1},{"bar": 2}]"#; let json: Value = serde_json::from_str(json).unwrap(); let pg_params = json_to_pg_text(vec![json]); assert_eq!( pg_params, vec![Some(r#"{"{\"foo\":1}","{\"bar\":2}"}"#.to_owned())] ); } fn pg_text_to_json(val: &str, pg_type: &Type) -> Value { let output = json::value_to_string!(|v| super::pg_text_to_json(v, val, pg_type).unwrap()); serde_json::from_str(&output).unwrap() } fn pg_array_parse(pg_array: &str, pg_type: &Type) -> Value { let output = json::value_to_string!(|v| json::value_as_list!(|v| { super::pg_array_parse(v, pg_array, pg_type, ',').unwrap(); })); serde_json::from_str(&output).unwrap() } #[test] fn test_atomic_types_parse() { assert_eq!(pg_text_to_json("foo", &Type::TEXT), json!("foo")); assert_eq!(pg_text_to_json("42", &Type::INT4), json!(42)); assert_eq!(pg_text_to_json("42", &Type::INT2), json!(42)); assert_eq!(pg_text_to_json("42", &Type::INT8), json!("42")); assert_eq!(pg_text_to_json("42.42", &Type::FLOAT8), json!(42.42)); assert_eq!(pg_text_to_json("42.42", &Type::FLOAT4), json!(42.42)); assert_eq!(pg_text_to_json("NaN", &Type::FLOAT4), json!("NaN")); assert_eq!( pg_text_to_json("Infinity", &Type::FLOAT4), json!("Infinity") ); assert_eq!( pg_text_to_json("-Infinity", &Type::FLOAT4), json!("-Infinity") ); let json: Value = serde_json::from_str("{\"s\":\"str\",\"n\":42,\"f\":4.2,\"a\":[null,3,\"a\"]}") .unwrap(); assert_eq!( pg_text_to_json( r#"{"s":"str","n":42,"f":4.2,"a":[null,3,"a"]}"#, &Type::JSONB ), json ); } #[test] fn test_pg_array_parse_text() { fn pt(pg_arr: &str) -> Value { pg_array_parse(pg_arr, &Type::TEXT) } assert_eq!( pt(r#"{"aa\"\\\,a",cha,"bbbb"}"#), json!(["aa\"\\,a", "cha", "bbbb"]) ); assert_eq!( pt(r#"{{"foo","bar"},{"bee","bop"}}"#), json!([["foo", "bar"], ["bee", "bop"]]) ); assert_eq!( pt(r#"{{{{"foo",NULL,"bop",bup}}}}"#), json!([[[["foo", null, "bop", "bup"]]]]) ); assert_eq!( pt(r#"{{"1",2,3},{4,NULL,6},{NULL,NULL,NULL}}"#), json!([["1", "2", "3"], ["4", null, "6"], [null, null, null]]) ); } #[test] fn test_pg_array_parse_bool() { fn pb(pg_arr: &str) -> Value { pg_array_parse(pg_arr, &Type::BOOL) } assert_eq!(pb(r#"{t,f,t}"#), json!([true, false, true])); assert_eq!(pb(r#"{{t,f,t}}"#), json!([[true, false, true]])); assert_eq!( pb(r#"{{t,f},{f,t}}"#), json!([[true, false], [false, true]]) ); assert_eq!( pb(r#"{{t,NULL},{NULL,f}}"#), json!([[true, null], [null, false]]) ); } #[test] fn test_pg_array_parse_numbers() { fn pn(pg_arr: &str, ty: &Type) -> Value { pg_array_parse(pg_arr, ty) } assert_eq!(pn(r#"{1,2,3}"#, &Type::INT4), json!([1, 2, 3])); assert_eq!(pn(r#"{1,2,3}"#, &Type::INT2), json!([1, 2, 3])); assert_eq!(pn(r#"{1,2,3}"#, &Type::INT8), json!(["1", "2", "3"])); assert_eq!(pn(r#"{1,2,3}"#, &Type::FLOAT4), json!([1.0, 2.0, 3.0])); assert_eq!(pn(r#"{1,2,3}"#, &Type::FLOAT8), json!([1.0, 2.0, 3.0])); assert_eq!( pn(r#"{1.1,2.2,3.3}"#, &Type::FLOAT4), json!([1.1, 2.2, 3.3]) ); assert_eq!( pn(r#"{1.1,2.2,3.3}"#, &Type::FLOAT8), json!([1.1, 2.2, 3.3]) ); assert_eq!( pn(r#"{NaN,Infinity,-Infinity}"#, &Type::FLOAT4), json!(["NaN", "Infinity", "-Infinity"]) ); assert_eq!( pn(r#"{NaN,Infinity,-Infinity}"#, &Type::FLOAT8), json!(["NaN", "Infinity", "-Infinity"]) ); } #[test] fn test_pg_array_with_decoration() { fn p(pg_arr: &str) -> Value { pg_array_parse(pg_arr, &Type::INT2) } assert_eq!( p(r#"[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}"#), json!([[[1, 2, 3], [4, 5, 6]]]) ); } #[test] fn test_pg_array_parse_json() { fn pt(pg_arr: &str) -> Value { pg_array_parse(pg_arr, &Type::JSONB) } assert_eq!(pt(r#"{"{}"}"#), json!([{}])); assert_eq!( pt(r#"{"{\"foo\": 1, \"bar\": 2}"}"#), json!([{"foo": 1, "bar": 2}]) ); assert_eq!( pt(r#"{"{\"foo\": 1}", "{\"bar\": 2}"}"#), json!([{"foo": 1}, {"bar": 2}]) ); assert_eq!( pt(r#"{{"{\"foo\": 1}", "{\"bar\": 2}"}}"#), json!([[{"foo": 1}, {"bar": 2}]]) ); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/error.rs
proxy/src/serverless/error.rs
use http::StatusCode; use http::header::HeaderName; use crate::auth::ComputeUserInfoParseError; use crate::error::{ErrorKind, ReportableError, UserFacingError}; use crate::http::ReadBodyError; pub trait HttpCodeError { fn get_http_status_code(&self) -> StatusCode; } #[derive(Debug, thiserror::Error)] pub(crate) enum ConnInfoError { #[error("invalid header: {0}")] InvalidHeader(&'static HeaderName), #[error("invalid connection string: {0}")] UrlParseError(#[from] url::ParseError), #[error("incorrect scheme")] IncorrectScheme, #[error("missing database name")] MissingDbName, #[error("invalid database name")] InvalidDbName, #[error("missing username")] MissingUsername, #[error("invalid username: {0}")] InvalidUsername(#[from] std::string::FromUtf8Error), #[error("missing authentication credentials: {0}")] MissingCredentials(Credentials), #[error("missing hostname")] MissingHostname, #[error("invalid hostname: {0}")] InvalidEndpoint(#[from] ComputeUserInfoParseError), } #[derive(Debug, thiserror::Error)] pub(crate) enum Credentials { #[error("required password")] Password, #[error("required authorization bearer token in JWT format")] BearerJwt, } impl ReportableError for ConnInfoError { fn get_error_kind(&self) -> ErrorKind { ErrorKind::User } } impl UserFacingError for ConnInfoError { fn to_string_client(&self) -> String { self.to_string() } } #[derive(Debug, thiserror::Error)] pub(crate) enum ReadPayloadError { #[error("could not read the HTTP request body: {0}")] Read(#[from] hyper::Error), #[error("request is too large (max is {limit} bytes)")] BodyTooLarge { limit: usize }, #[error("could not parse the HTTP request body: {0}")] Parse(#[from] serde_json::Error), } impl From<ReadBodyError<hyper::Error>> for ReadPayloadError { fn from(value: ReadBodyError<hyper::Error>) -> Self { match value { ReadBodyError::BodyTooLarge { limit } => Self::BodyTooLarge { limit }, ReadBodyError::Read(e) => Self::Read(e), } } } impl ReportableError for ReadPayloadError { fn get_error_kind(&self) -> ErrorKind { match self { ReadPayloadError::Read(_) => ErrorKind::ClientDisconnect, ReadPayloadError::BodyTooLarge { .. } => ErrorKind::User, ReadPayloadError::Parse(_) => ErrorKind::User, } } } impl HttpCodeError for ReadPayloadError { fn get_http_status_code(&self) -> StatusCode { match self { ReadPayloadError::Read(_) => StatusCode::BAD_REQUEST, ReadPayloadError::BodyTooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE, ReadPayloadError::Parse(_) => StatusCode::BAD_REQUEST, } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/mod.rs
proxy/src/serverless/mod.rs
//! Routers for our serverless APIs //! //! Handles both SQL over HTTP and SQL over Websockets. mod backend; pub mod cancel_set; mod conn_pool; mod conn_pool_lib; mod error; mod http_conn_pool; mod http_util; mod json; mod local_conn_pool; #[cfg(feature = "rest_broker")] pub mod rest; mod sql_over_http; mod websocket; use std::net::{IpAddr, SocketAddr}; use std::pin::{Pin, pin}; use std::sync::Arc; use anyhow::Context; use arc_swap::ArcSwapOption; use async_trait::async_trait; use atomic_take::AtomicTake; use bytes::Bytes; pub use conn_pool_lib::GlobalConnPoolOptions; use futures::TryFutureExt; use futures::future::{Either, select}; use http::{Method, Response, StatusCode}; use http_body_util::combinators::BoxBody; use http_body_util::{BodyExt, Empty}; use http_util::{NEON_REQUEST_ID, uuid_to_header_value}; use http_utils::error::ApiError; use hyper::body::Incoming; use hyper_util::rt::TokioExecutor; use hyper_util::server::conn::auto::Builder; use rand::SeedableRng; use rand::rngs::StdRng; use tokio::io::{AsyncRead, AsyncWrite}; use tokio::net::{TcpListener, TcpStream}; use tokio::time::timeout; use tokio_rustls::TlsAcceptor; use tokio_util::sync::CancellationToken; use tokio_util::task::TaskTracker; use tracing::{Instrument, info, warn}; use crate::cancellation::CancellationHandler; use crate::config::{ProxyConfig, ProxyProtocolV2}; use crate::context::RequestContext; use crate::ext::TaskExt; use crate::metrics::Metrics; use crate::protocol2::{ConnectHeader, ConnectionInfo, read_proxy_protocol}; use crate::rate_limiter::EndpointRateLimiter; use crate::serverless::backend::PoolingBackend; use crate::serverless::http_util::{api_error_into_response, json_response}; use crate::util::run_until_cancelled; pub(crate) const SERVERLESS_DRIVER_SNI: &str = "api"; pub(crate) const AUTH_BROKER_SNI: &str = "apiauth"; pub async fn task_main( config: &'static ProxyConfig, auth_backend: &'static crate::auth::Backend<'static, ()>, ws_listener: TcpListener, cancellation_token: CancellationToken, cancellation_handler: Arc<CancellationHandler>, endpoint_rate_limiter: Arc<EndpointRateLimiter>, ) -> anyhow::Result<()> { scopeguard::defer! { info!("websocket server has shut down"); } let local_pool = local_conn_pool::LocalConnPool::new(&config.http_config); let conn_pool = conn_pool_lib::GlobalConnPool::new(&config.http_config); { let conn_pool = Arc::clone(&conn_pool); tokio::spawn(async move { conn_pool.gc_worker(StdRng::from_os_rng()).await; }); } // shutdown the connection pool tokio::spawn({ let cancellation_token = cancellation_token.clone(); let conn_pool = conn_pool.clone(); async move { cancellation_token.cancelled().await; tokio::task::spawn_blocking(move || conn_pool.shutdown()) .await .propagate_task_panic(); } }); let http_conn_pool = conn_pool_lib::GlobalConnPool::new(&config.http_config); { let http_conn_pool = Arc::clone(&http_conn_pool); tokio::spawn(async move { http_conn_pool.gc_worker(StdRng::from_os_rng()).await; }); } // shutdown the connection pool tokio::spawn({ let cancellation_token = cancellation_token.clone(); let http_conn_pool = http_conn_pool.clone(); async move { cancellation_token.cancelled().await; tokio::task::spawn_blocking(move || http_conn_pool.shutdown()) .await .propagate_task_panic(); } }); let backend = Arc::new(PoolingBackend { http_conn_pool: Arc::clone(&http_conn_pool), local_pool, pool: Arc::clone(&conn_pool), config, auth_backend, endpoint_rate_limiter: Arc::clone(&endpoint_rate_limiter), }); let tls_acceptor: Arc<dyn MaybeTlsAcceptor> = Arc::new(&config.tls_config); let connections = tokio_util::task::task_tracker::TaskTracker::new(); connections.close(); // allows `connections.wait to complete` let cancellations = tokio_util::task::task_tracker::TaskTracker::new(); while let Some(res) = run_until_cancelled(ws_listener.accept(), &cancellation_token).await { let (conn, peer_addr) = res.context("could not accept TCP stream")?; if let Err(e) = conn.set_nodelay(true) { tracing::error!("could not set nodelay: {e}"); continue; } let conn_id = uuid::Uuid::new_v4(); let http_conn_span = tracing::info_span!("http_conn", ?conn_id); let n_connections = Metrics::get() .proxy .client_connections .sample(crate::metrics::Protocol::Http); tracing::trace!(?n_connections, threshold = ?config.http_config.client_conn_threshold, "check"); if n_connections > config.http_config.client_conn_threshold { tracing::trace!("attempting to cancel a random connection"); if let Some(token) = config.http_config.cancel_set.take() { tracing::debug!("cancelling a random connection"); token.cancel(); } } let conn_token = cancellation_token.child_token(); let tls_acceptor = tls_acceptor.clone(); let backend = backend.clone(); let connections2 = connections.clone(); let cancellation_handler = cancellation_handler.clone(); let endpoint_rate_limiter = endpoint_rate_limiter.clone(); let cancellations = cancellations.clone(); connections.spawn( async move { let conn_token2 = conn_token.clone(); let _cancel_guard = config.http_config.cancel_set.insert(conn_id, conn_token2); let session_id = uuid::Uuid::new_v4(); let _gauge = Metrics::get() .proxy .client_connections .guard(crate::metrics::Protocol::Http); let startup_result = Box::pin(connection_startup( config, tls_acceptor, session_id, conn, peer_addr, )) .await; let Some((conn, conn_info)) = startup_result else { return; }; Box::pin(connection_handler( config, backend, connections2, cancellations, cancellation_handler, endpoint_rate_limiter, conn_token, conn, conn_info, session_id, )) .await; } .instrument(http_conn_span), ); } connections.wait().await; Ok(()) } pub(crate) trait AsyncReadWrite: AsyncRead + AsyncWrite + Send + 'static {} impl<T: AsyncRead + AsyncWrite + Send + 'static> AsyncReadWrite for T {} pub(crate) type AsyncRW = Pin<Box<dyn AsyncReadWrite>>; #[async_trait] trait MaybeTlsAcceptor: Send + Sync + 'static { async fn accept(&self, conn: TcpStream) -> std::io::Result<AsyncRW>; } #[async_trait] impl MaybeTlsAcceptor for &'static ArcSwapOption<crate::config::TlsConfig> { async fn accept(&self, conn: TcpStream) -> std::io::Result<AsyncRW> { match &*self.load() { Some(config) => Ok(Box::pin( TlsAcceptor::from(config.http_config.clone()) .accept(conn) .await?, )), None => Ok(Box::pin(conn)), } } } /// Handles the TCP startup lifecycle. /// 1. Parses PROXY protocol V2 /// 2. Handles TLS handshake async fn connection_startup( config: &ProxyConfig, tls_acceptor: Arc<dyn MaybeTlsAcceptor>, session_id: uuid::Uuid, conn: TcpStream, peer_addr: SocketAddr, ) -> Option<(AsyncRW, ConnectionInfo)> { // handle PROXY protocol let (conn, conn_info) = match config.proxy_protocol_v2 { ProxyProtocolV2::Required => { match read_proxy_protocol(conn).await { Err(e) => { warn!("per-client task finished with an error: {e:#}"); return None; } // our load balancers will not send any more data. let's just exit immediately Ok((_conn, ConnectHeader::Local)) => { tracing::debug!("healthcheck received"); return None; } Ok((conn, ConnectHeader::Proxy(info))) => (conn, info), } } // ignore the header - it cannot be confused for a postgres or http connection so will // error later. ProxyProtocolV2::Rejected => ( conn, ConnectionInfo { addr: peer_addr, extra: None, }, ), }; let has_private_peer_addr = match conn_info.addr.ip() { IpAddr::V4(ip) => ip.is_private(), IpAddr::V6(_) => false, }; info!(?session_id, %conn_info, "accepted new TCP connection"); // try upgrade to TLS, but with a timeout. let conn = match timeout(config.handshake_timeout, tls_acceptor.accept(conn)).await { Ok(Ok(conn)) => { info!(?session_id, %conn_info, "accepted new TLS connection"); conn } // The handshake failed Ok(Err(e)) => { if !has_private_peer_addr { Metrics::get().proxy.tls_handshake_failures.inc(); } warn!(?session_id, %conn_info, "failed to accept TLS connection: {e:?}"); return None; } // The handshake timed out Err(e) => { if !has_private_peer_addr { Metrics::get().proxy.tls_handshake_failures.inc(); } warn!(?session_id, %conn_info, "failed to accept TLS connection: {e:?}"); return None; } }; Some((conn, conn_info)) } /// Handles HTTP connection /// 1. With graceful shutdowns /// 2. With graceful request cancellation with connection failure /// 3. With websocket upgrade support. #[allow(clippy::too_many_arguments)] async fn connection_handler( config: &'static ProxyConfig, backend: Arc<PoolingBackend>, connections: TaskTracker, cancellations: TaskTracker, cancellation_handler: Arc<CancellationHandler>, endpoint_rate_limiter: Arc<EndpointRateLimiter>, cancellation_token: CancellationToken, conn: AsyncRW, conn_info: ConnectionInfo, session_id: uuid::Uuid, ) { let session_id = AtomicTake::new(session_id); // Cancel all current inflight HTTP requests if the HTTP connection is closed. let http_cancellation_token = CancellationToken::new(); let _cancel_connection = http_cancellation_token.clone().drop_guard(); let conn_info2 = conn_info.clone(); let server = Builder::new(TokioExecutor::new()); let conn = server.serve_connection_with_upgrades( hyper_util::rt::TokioIo::new(conn), hyper::service::service_fn(move |req: hyper::Request<Incoming>| { // First HTTP request shares the same session ID let mut session_id = session_id.take().unwrap_or_else(uuid::Uuid::new_v4); if matches!(backend.auth_backend, crate::auth::Backend::Local(_)) { // take session_id from request, if given. if let Some(id) = req .headers() .get(&NEON_REQUEST_ID) .and_then(|id| uuid::Uuid::try_parse_ascii(id.as_bytes()).ok()) { session_id = id; } } // Cancel the current inflight HTTP request if the requets stream is closed. // This is slightly different to `_cancel_connection` in that // h2 can cancel individual requests with a `RST_STREAM`. let http_request_token = http_cancellation_token.child_token(); let cancel_request = http_request_token.clone().drop_guard(); // `request_handler` is not cancel safe. It expects to be cancelled only at specific times. // By spawning the future, we ensure it never gets cancelled until it decides to. let cancellations = cancellations.clone(); let handler = connections.spawn( request_handler( req, config, backend.clone(), connections.clone(), cancellation_handler.clone(), session_id, conn_info2.clone(), http_request_token, endpoint_rate_limiter.clone(), cancellations, ) .in_current_span() .map_ok_or_else(api_error_into_response, |r| r), ); async move { let mut res = handler.await; cancel_request.disarm(); // add the session ID to the response if let Ok(resp) = &mut res { resp.headers_mut() .append(&NEON_REQUEST_ID, uuid_to_header_value(session_id)); } res } }), ); // On cancellation, trigger the HTTP connection handler to shut down. let res = match select(pin!(cancellation_token.cancelled()), pin!(conn)).await { Either::Left((_cancelled, mut conn)) => { tracing::debug!(%conn_info, "cancelling connection"); conn.as_mut().graceful_shutdown(); conn.await } Either::Right((res, _)) => res, }; match res { Ok(()) => tracing::info!(%conn_info, "HTTP connection closed"), Err(e) => tracing::warn!(%conn_info, "HTTP connection error {e}"), } } #[allow(clippy::too_many_arguments)] async fn request_handler( mut request: hyper::Request<Incoming>, config: &'static ProxyConfig, backend: Arc<PoolingBackend>, ws_connections: TaskTracker, cancellation_handler: Arc<CancellationHandler>, session_id: uuid::Uuid, conn_info: ConnectionInfo, // used to cancel in-flight HTTP requests. not used to cancel websockets http_cancellation_token: CancellationToken, endpoint_rate_limiter: Arc<EndpointRateLimiter>, cancellations: TaskTracker, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, ApiError> { let host = request .headers() .get("host") .and_then(|h| h.to_str().ok()) .and_then(|h| h.split(':').next()) .map(|s| s.to_string()); // Check if the request is a websocket upgrade request. if config.http_config.accept_websockets && framed_websockets::upgrade::is_upgrade_request(&request) { let ctx = RequestContext::new(session_id, conn_info, crate::metrics::Protocol::Ws); ctx.set_user_agent( request .headers() .get(hyper::header::USER_AGENT) .and_then(|h| h.to_str().ok()) .map(Into::into), ); let span = ctx.span(); info!(parent: &span, "performing websocket upgrade"); let (response, websocket) = framed_websockets::upgrade::upgrade(&mut request) .map_err(|e| ApiError::BadRequest(e.into()))?; let cancellations = cancellations.clone(); ws_connections.spawn( async move { if let Err(e) = websocket::serve_websocket( config, backend.auth_backend, ctx, websocket, cancellation_handler, endpoint_rate_limiter, host, cancellations, ) .await { warn!("error in websocket connection: {e:#}"); } } .instrument(span), ); // Return the response so the spawned future can continue. Ok(response.map(|b| b.map_err(|x| match x {}).boxed())) } else if request.uri().path() == "/sql" && *request.method() == Method::POST { let ctx = RequestContext::new(session_id, conn_info, crate::metrics::Protocol::Http); let span = ctx.span(); let testodrome_id = request .headers() .get("X-Neon-Query-ID") .and_then(|value| value.to_str().ok()) .map(|s| s.to_string()); if let Some(query_id) = testodrome_id { info!(parent: &ctx.span(), "testodrome query ID: {query_id}"); ctx.set_testodrome_id(query_id.into()); } sql_over_http::handle(config, ctx, request, backend, http_cancellation_token) .instrument(span) .await } else if request.uri().path() == "/sql" && *request.method() == Method::OPTIONS { Response::builder() .header("Allow", "OPTIONS, POST") .header("Access-Control-Allow-Origin", "*") .header( "Access-Control-Allow-Headers", "Authorization, Neon-Connection-String, Neon-Raw-Text-Output, Neon-Array-Mode, Neon-Pool-Opt-In, Neon-Batch-Read-Only, Neon-Batch-Isolation-Level", ) .header("Access-Control-Max-Age", "86400" /* 24 hours */) .status(StatusCode::OK) // 204 is also valid, but see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS#status_code .body(Empty::new().map_err(|x| match x {}).boxed()) .map_err(|e| ApiError::InternalServerError(e.into())) } else { #[cfg(feature = "rest_broker")] { if config.rest_config.is_rest_broker // we are testing for the path to be /database_name/rest/... && request .uri() .path() .split('/') .nth(2) .is_some_and(|part| part.starts_with("rest")) { let ctx = RequestContext::new(session_id, conn_info, crate::metrics::Protocol::Http); let span = ctx.span(); let testodrome_id = request .headers() .get("X-Neon-Query-ID") .and_then(|value| value.to_str().ok()) .map(|s| s.to_string()); if let Some(query_id) = testodrome_id { info!(parent: &span, "testodrome query ID: {query_id}"); ctx.set_testodrome_id(query_id.into()); } rest::handle(config, ctx, request, backend, http_cancellation_token) .instrument(span) .await } else { json_response(StatusCode::BAD_REQUEST, "query is not supported") } } #[cfg(not(feature = "rest_broker"))] { json_response(StatusCode::BAD_REQUEST, "query is not supported") } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/sql_over_http.rs
proxy/src/serverless/sql_over_http.rs
use std::pin::pin; use std::sync::Arc; use bytes::Bytes; use futures::future::{Either, select, try_join}; use futures::{StreamExt, TryFutureExt}; use http::Method; use http::header::AUTHORIZATION; use http_body_util::combinators::BoxBody; use http_body_util::{BodyExt, Full}; use http_utils::error::ApiError; use hyper::body::Incoming; use hyper::http::{HeaderName, HeaderValue}; use hyper::{Request, Response, StatusCode, header}; use indexmap::IndexMap; use postgres_client::error::{DbError, ErrorPosition, SqlState}; use postgres_client::{GenericClient, IsolationLevel, NoTls, ReadyForQueryStatus, Transaction}; use serde_json::Value; use serde_json::value::RawValue; use tokio::time::{self, Instant}; use tokio_util::sync::CancellationToken; use tracing::{Level, debug, error, info}; use typed_json::json; use super::backend::{LocalProxyConnError, PoolingBackend}; use super::conn_pool::AuthData; use super::conn_pool_lib::{self, ConnInfo}; use super::error::{ConnInfoError, HttpCodeError, ReadPayloadError}; use super::http_util::{ ALLOW_POOL, ARRAY_MODE, CONN_STRING, NEON_REQUEST_ID, RAW_TEXT_OUTPUT, TXN_DEFERRABLE, TXN_ISOLATION_LEVEL, TXN_READ_ONLY, get_conn_info, json_response, uuid_to_header_value, }; use super::json::{JsonConversionError, json_to_pg_text, pg_text_row_to_json}; use crate::auth::backend::ComputeCredentialKeys; use crate::config::{HttpConfig, ProxyConfig}; use crate::context::RequestContext; use crate::error::{ErrorKind, ReportableError, UserFacingError}; use crate::http::read_body_with_limit; use crate::metrics::{HttpDirection, Metrics}; use crate::serverless::backend::HttpConnError; use crate::usage_metrics::{MetricCounter, MetricCounterRecorder}; use crate::util::run_until_cancelled; #[derive(serde::Deserialize)] #[serde(rename_all = "camelCase")] struct QueryData { query: String, #[serde(deserialize_with = "bytes_to_pg_text")] #[serde(default)] params: Vec<Option<String>>, #[serde(default)] array_mode: Option<bool>, } #[derive(serde::Deserialize)] struct BatchQueryData { queries: Vec<QueryData>, } #[derive(serde::Deserialize)] #[serde(untagged)] enum Payload { Single(QueryData), Batch(BatchQueryData), } pub(super) const HEADER_VALUE_TRUE: HeaderValue = HeaderValue::from_static("true"); fn bytes_to_pg_text<'de, D>(deserializer: D) -> Result<Vec<Option<String>>, D::Error> where D: serde::de::Deserializer<'de>, { // TODO: consider avoiding the allocation here. let json: Vec<Value> = serde::de::Deserialize::deserialize(deserializer)?; Ok(json_to_pg_text(json)) } pub(crate) async fn handle( config: &'static ProxyConfig, ctx: RequestContext, request: Request<Incoming>, backend: Arc<PoolingBackend>, cancel: CancellationToken, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, ApiError> { let result = handle_inner(cancel, config, &ctx, request, backend).await; let mut response = match result { Ok(r) => { ctx.set_success(); // Handling the error response from local proxy here if config.authentication_config.is_auth_broker && r.status().is_server_error() { let status = r.status(); let body_bytes = r .collect() .await .map_err(|e| { ApiError::InternalServerError(anyhow::Error::msg(format!( "could not collect http body: {e}" ))) })? .to_bytes(); if let Ok(mut json_map) = serde_json::from_slice::<IndexMap<&str, &RawValue>>(&body_bytes) { let message = json_map.get("message"); if let Some(message) = message { let msg: String = match serde_json::from_str(message.get()) { Ok(msg) => msg, Err(_) => { "Unable to parse the response message from server".to_string() } }; error!("Error response from local_proxy: {status} {msg}"); json_map.retain(|key, _| !key.starts_with("neon:")); // remove all the neon-related keys let resp_json = serde_json::to_string(&json_map) .unwrap_or("failed to serialize the response message".to_string()); return json_response(status, resp_json); } } error!("Unable to parse the response message from local_proxy"); return json_response( status, json!({ "message": "Unable to parse the response message from server".to_string() }), ); } r } Err(e @ SqlOverHttpError::Cancelled(_)) => { let error_kind = e.get_error_kind(); ctx.set_error_kind(error_kind); let message = "Query cancelled, connection was terminated"; tracing::info!( kind=error_kind.to_metric_label(), error=%e, msg=message, "forwarding error to user" ); json_response( StatusCode::BAD_REQUEST, json!({ "message": message, "code": SqlState::PROTOCOL_VIOLATION.code() }), )? } Err(e) => { let error_kind = e.get_error_kind(); ctx.set_error_kind(error_kind); let mut message = e.to_string_client(); let db_error = match &e { SqlOverHttpError::ConnectCompute(HttpConnError::PostgresConnectionError(e)) | SqlOverHttpError::Postgres(e) => e.as_db_error(), _ => None, }; fn get<'a, T: Default>(db: Option<&'a DbError>, x: impl FnOnce(&'a DbError) -> T) -> T { db.map(x).unwrap_or_default() } if let Some(db_error) = db_error { db_error.message().clone_into(&mut message); } let position = db_error.and_then(|db| db.position()); let (position, internal_position, internal_query) = match position { Some(ErrorPosition::Original(position)) => (Some(position.to_string()), None, None), Some(ErrorPosition::Internal { position, query }) => { (None, Some(position.to_string()), Some(query.clone())) } None => (None, None, None), }; let code = get(db_error, |db| db.code().code()); let severity = get(db_error, |db| db.severity()); let detail = get(db_error, |db| db.detail()); let hint = get(db_error, |db| db.hint()); let where_ = get(db_error, |db| db.where_()); let table = get(db_error, |db| db.table()); let column = get(db_error, |db| db.column()); let schema = get(db_error, |db| db.schema()); let datatype = get(db_error, |db| db.datatype()); let constraint = get(db_error, |db| db.constraint()); let file = get(db_error, |db| db.file()); let line = get(db_error, |db| db.line().map(|l| l.to_string())); let routine = get(db_error, |db| db.routine()); if db_error.is_some() && error_kind == ErrorKind::User { // this error contains too much info, and it's not an error we care about. if tracing::enabled!(Level::DEBUG) { debug!( kind=error_kind.to_metric_label(), error=%e, msg=message, "forwarding error to user" ); } else { info!( kind = error_kind.to_metric_label(), error = "bad query", "forwarding error to user" ); } } else { info!( kind=error_kind.to_metric_label(), error=%e, msg=message, "forwarding error to user" ); } json_response( e.get_http_status_code(), json!({ "message": message, "code": code, "detail": detail, "hint": hint, "position": position, "internalPosition": internal_position, "internalQuery": internal_query, "severity": severity, "where": where_, "table": table, "column": column, "schema": schema, "dataType": datatype, "constraint": constraint, "file": file, "line": line, "routine": routine, }), )? } }; response .headers_mut() .insert("Access-Control-Allow-Origin", HeaderValue::from_static("*")); Ok(response) } #[derive(Debug, thiserror::Error)] pub(crate) enum SqlOverHttpError { #[error("{0}")] ReadPayload(#[from] ReadPayloadError), #[error("{0}")] ConnectCompute(#[from] HttpConnError), #[error("{0}")] ConnInfo(#[from] ConnInfoError), #[error("response is too large (max is {0} bytes)")] ResponseTooLarge(usize), #[error("invalid isolation level")] InvalidIsolationLevel, /// for queries our customers choose to run #[error("{0}")] Postgres(#[source] postgres_client::Error), /// for queries we choose to run #[error("{0}")] InternalPostgres(#[source] postgres_client::Error), #[error("{0}")] JsonConversion(#[from] JsonConversionError), #[error("{0}")] Cancelled(SqlOverHttpCancel), } impl ReportableError for SqlOverHttpError { fn get_error_kind(&self) -> ErrorKind { match self { SqlOverHttpError::ReadPayload(e) => e.get_error_kind(), SqlOverHttpError::ConnectCompute(e) => e.get_error_kind(), SqlOverHttpError::ConnInfo(e) => e.get_error_kind(), SqlOverHttpError::ResponseTooLarge(_) => ErrorKind::User, SqlOverHttpError::InvalidIsolationLevel => ErrorKind::User, // customer initiated SQL errors. SqlOverHttpError::Postgres(p) => { if p.as_db_error().is_some() { ErrorKind::User } else { ErrorKind::Compute } } // proxy initiated SQL errors. SqlOverHttpError::InternalPostgres(p) => { if p.as_db_error().is_some() { ErrorKind::Service } else { ErrorKind::Compute } } // postgres returned a bad row format that we couldn't parse. SqlOverHttpError::JsonConversion(_) => ErrorKind::Postgres, SqlOverHttpError::Cancelled(c) => c.get_error_kind(), } } } impl UserFacingError for SqlOverHttpError { fn to_string_client(&self) -> String { match self { SqlOverHttpError::ReadPayload(p) => p.to_string(), SqlOverHttpError::ConnectCompute(c) => c.to_string_client(), SqlOverHttpError::ConnInfo(c) => c.to_string_client(), SqlOverHttpError::ResponseTooLarge(_) => self.to_string(), SqlOverHttpError::InvalidIsolationLevel => self.to_string(), SqlOverHttpError::Postgres(p) => p.to_string(), SqlOverHttpError::InternalPostgres(p) => p.to_string(), SqlOverHttpError::JsonConversion(_) => "could not parse postgres response".to_string(), SqlOverHttpError::Cancelled(_) => self.to_string(), } } } impl HttpCodeError for SqlOverHttpError { fn get_http_status_code(&self) -> StatusCode { match self { SqlOverHttpError::ReadPayload(e) => e.get_http_status_code(), SqlOverHttpError::ConnectCompute(h) => match h.get_error_kind() { ErrorKind::User => StatusCode::BAD_REQUEST, _ => StatusCode::INTERNAL_SERVER_ERROR, }, SqlOverHttpError::ConnInfo(_) => StatusCode::BAD_REQUEST, SqlOverHttpError::ResponseTooLarge(_) => StatusCode::INSUFFICIENT_STORAGE, SqlOverHttpError::InvalidIsolationLevel => StatusCode::BAD_REQUEST, SqlOverHttpError::Postgres(_) => StatusCode::BAD_REQUEST, SqlOverHttpError::InternalPostgres(_) => StatusCode::INTERNAL_SERVER_ERROR, SqlOverHttpError::JsonConversion(_) => StatusCode::INTERNAL_SERVER_ERROR, SqlOverHttpError::Cancelled(_) => StatusCode::INTERNAL_SERVER_ERROR, } } } #[derive(Debug, thiserror::Error)] pub(crate) enum SqlOverHttpCancel { #[error("query was cancelled")] Postgres, #[error("query was cancelled while stuck trying to connect to the database")] Connect, } impl ReportableError for SqlOverHttpCancel { fn get_error_kind(&self) -> ErrorKind { match self { SqlOverHttpCancel::Postgres => ErrorKind::ClientDisconnect, SqlOverHttpCancel::Connect => ErrorKind::ClientDisconnect, } } } #[derive(Clone, Copy, Debug)] struct HttpHeaders { raw_output: bool, default_array_mode: bool, txn_isolation_level: Option<IsolationLevel>, txn_read_only: bool, txn_deferrable: bool, } impl HttpHeaders { fn try_parse(headers: &hyper::http::HeaderMap) -> Result<Self, SqlOverHttpError> { // Determine the output options. Default behaviour is 'false'. Anything that is not // strictly 'true' assumed to be false. let raw_output = headers.get(&RAW_TEXT_OUTPUT) == Some(&HEADER_VALUE_TRUE); let default_array_mode = headers.get(&ARRAY_MODE) == Some(&HEADER_VALUE_TRUE); // isolation level, read only and deferrable let txn_isolation_level = match headers.get(&TXN_ISOLATION_LEVEL) { Some(x) => Some( map_header_to_isolation_level(x).ok_or(SqlOverHttpError::InvalidIsolationLevel)?, ), None => None, }; let txn_read_only = headers.get(&TXN_READ_ONLY) == Some(&HEADER_VALUE_TRUE); let txn_deferrable = headers.get(&TXN_DEFERRABLE) == Some(&HEADER_VALUE_TRUE); Ok(Self { raw_output, default_array_mode, txn_isolation_level, txn_read_only, txn_deferrable, }) } } fn map_header_to_isolation_level(level: &HeaderValue) -> Option<IsolationLevel> { match level.as_bytes() { b"Serializable" => Some(IsolationLevel::Serializable), b"ReadUncommitted" => Some(IsolationLevel::ReadUncommitted), b"ReadCommitted" => Some(IsolationLevel::ReadCommitted), b"RepeatableRead" => Some(IsolationLevel::RepeatableRead), _ => None, } } fn map_isolation_level_to_headers(level: IsolationLevel) -> Option<HeaderValue> { match level { IsolationLevel::ReadUncommitted => Some(HeaderValue::from_static("ReadUncommitted")), IsolationLevel::ReadCommitted => Some(HeaderValue::from_static("ReadCommitted")), IsolationLevel::RepeatableRead => Some(HeaderValue::from_static("RepeatableRead")), IsolationLevel::Serializable => Some(HeaderValue::from_static("Serializable")), _ => None, } } async fn handle_inner( cancel: CancellationToken, config: &'static ProxyConfig, ctx: &RequestContext, request: Request<Incoming>, backend: Arc<PoolingBackend>, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> { let _requeset_gauge = Metrics::get() .proxy .connection_requests .guard(ctx.protocol()); info!( protocol = %ctx.protocol(), "handling interactive connection from client" ); let conn_info = get_conn_info(&config.authentication_config, ctx, None, request.headers())?; info!( user = conn_info.conn_info.user_info.user.as_str(), "credentials" ); match conn_info.auth { AuthData::Jwt(jwt) if config.authentication_config.is_auth_broker => { handle_auth_broker_inner(ctx, request, conn_info.conn_info, jwt, backend).await } auth => { handle_db_inner( cancel, config, ctx, request, conn_info.conn_info, auth, backend, ) .await } } } async fn handle_db_inner( cancel: CancellationToken, config: &'static ProxyConfig, ctx: &RequestContext, request: Request<Incoming>, conn_info: ConnInfo, auth: AuthData, backend: Arc<PoolingBackend>, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> { // // Determine the destination and connection params // let headers = request.headers(); // Allow connection pooling only if explicitly requested // or if we have decided that http pool is no longer opt-in let allow_pool = !config.http_config.pool_options.opt_in || headers.get(&ALLOW_POOL) == Some(&HEADER_VALUE_TRUE); let parsed_headers = HttpHeaders::try_parse(headers)?; let mut request_len = 0; let fetch_and_process_request = Box::pin( async { let body = read_body_with_limit( request.into_body(), config.http_config.max_request_size_bytes, ) .await?; request_len = body.len(); Metrics::get() .proxy .http_conn_content_length_bytes .observe(HttpDirection::Request, body.len() as f64); debug!(length = body.len(), "request payload read"); let payload: Payload = serde_json::from_slice(&body)?; Ok::<Payload, ReadPayloadError>(payload) // Adjust error type accordingly } .map_err(SqlOverHttpError::from), ); let authenticate_and_connect = Box::pin( async { let keys = match auth { AuthData::Password(pw) => backend .authenticate_with_password(ctx, &conn_info.user_info, &pw) .await .map_err(HttpConnError::AuthError)?, AuthData::Jwt(jwt) => backend .authenticate_with_jwt(ctx, &conn_info.user_info, jwt) .await .map_err(HttpConnError::AuthError)?, }; let client = match keys.keys { ComputeCredentialKeys::JwtPayload(payload) if backend.auth_backend.is_local_proxy() => { #[cfg(feature = "testing")] let disable_pg_session_jwt = config.disable_pg_session_jwt; #[cfg(not(feature = "testing"))] let disable_pg_session_jwt = false; let mut client = backend .connect_to_local_postgres(ctx, conn_info, disable_pg_session_jwt) .await?; if !disable_pg_session_jwt { let (cli_inner, _dsc) = client.client_inner(); cli_inner.set_jwt_session(&payload).await?; } Client::Local(client) } _ => { let client = backend .connect_to_compute(ctx, conn_info, keys, !allow_pool) .await?; Client::Remote(client) } }; // not strictly necessary to mark success here, // but it's just insurance for if we forget it somewhere else ctx.success(); Ok::<_, SqlOverHttpError>(client) } .map_err(SqlOverHttpError::from), ); let (payload, mut client) = match run_until_cancelled( // Run both operations in parallel try_join( pin!(fetch_and_process_request), pin!(authenticate_and_connect), ), &cancel, ) .await { Some(result) => result?, None => return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Connect)), }; let mut response = Response::builder() .status(StatusCode::OK) .header(header::CONTENT_TYPE, "application/json"); // Now execute the query and return the result. let json_output = match payload { Payload::Single(stmt) => { stmt.process(&config.http_config, cancel, &mut client, parsed_headers) .await? } Payload::Batch(statements) => { if parsed_headers.txn_read_only { response = response.header(TXN_READ_ONLY.clone(), &HEADER_VALUE_TRUE); } if parsed_headers.txn_deferrable { response = response.header(TXN_DEFERRABLE.clone(), &HEADER_VALUE_TRUE); } if let Some(txn_isolation_level) = parsed_headers .txn_isolation_level .and_then(map_isolation_level_to_headers) { response = response.header(TXN_ISOLATION_LEVEL.clone(), txn_isolation_level); } statements .process(&config.http_config, cancel, &mut client, parsed_headers) .await? } }; let metrics = client.metrics(ctx); let len = json_output.len(); let response = response .body( Full::new(Bytes::from(json_output)) .map_err(|x| match x {}) .boxed(), ) // only fails if invalid status code or invalid header/values are given. // these are not user configurable so it cannot fail dynamically .expect("building response payload should not fail"); // count the egress bytes - we miss the TLS and header overhead but oh well... // moving this later in the stack is going to be a lot of effort and ehhhh metrics.record_egress(len as u64); metrics.record_ingress(request_len as u64); Metrics::get() .proxy .http_conn_content_length_bytes .observe(HttpDirection::Response, len as f64); Ok(response) } static HEADERS_TO_FORWARD: &[&HeaderName] = &[ &AUTHORIZATION, &CONN_STRING, &RAW_TEXT_OUTPUT, &ARRAY_MODE, &TXN_ISOLATION_LEVEL, &TXN_READ_ONLY, &TXN_DEFERRABLE, ]; async fn handle_auth_broker_inner( ctx: &RequestContext, request: Request<Incoming>, conn_info: ConnInfo, jwt: String, backend: Arc<PoolingBackend>, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> { backend .authenticate_with_jwt(ctx, &conn_info.user_info, jwt) .await .map_err(HttpConnError::from)?; let mut client = backend.connect_to_local_proxy(ctx, conn_info).await?; let local_proxy_uri = ::http::Uri::from_static("http://proxy.local/sql"); let (mut parts, body) = request.into_parts(); let mut req = Request::builder().method(Method::POST).uri(local_proxy_uri); // todo(conradludgate): maybe auth-broker should parse these and re-serialize // these instead just to ensure they remain normalised. for &h in HEADERS_TO_FORWARD { if let Some(hv) = parts.headers.remove(h) { req = req.header(h, hv); } } req = req.header(&NEON_REQUEST_ID, uuid_to_header_value(ctx.session_id())); let req = req .body(body.map_err(|e| e).boxed()) //TODO: is there a potential for a regression here? .expect("all headers and params received via hyper should be valid for request"); // todo: map body to count egress let _metrics = client.metrics(ctx); Ok(client .inner .inner .send_request(req) .await .map_err(LocalProxyConnError::from) .map_err(HttpConnError::from)? .map(|b| b.boxed())) } impl QueryData { async fn process( self, config: &'static HttpConfig, cancel: CancellationToken, client: &mut Client, parsed_headers: HttpHeaders, ) -> Result<String, SqlOverHttpError> { let (inner, mut discard) = client.inner(); let cancel_token = inner.cancel_token(); let mut json_buf = vec![]; let batch_result = match select( pin!(query_to_json( config, &mut *inner, self, json::ValueSer::new(&mut json_buf), parsed_headers )), pin!(cancel.cancelled()), ) .await { Either::Left((res, __not_yet_cancelled)) => res, Either::Right((_cancelled, query)) => { tracing::info!("cancelling query"); if let Err(err) = cancel_token.cancel_query(NoTls).await { tracing::warn!(?err, "could not cancel query"); } // wait for the query cancellation match time::timeout(time::Duration::from_millis(100), query).await { // query successed before it was cancelled. Ok(Ok(status)) => Ok(status), // query failed or was cancelled. Ok(Err(error)) => { let db_error = match &error { SqlOverHttpError::ConnectCompute( HttpConnError::PostgresConnectionError(e), ) | SqlOverHttpError::Postgres(e) => e.as_db_error(), _ => None, }; // if errored for some other reason, it might not be safe to return if !db_error.is_some_and(|e| *e.code() == SqlState::QUERY_CANCELED) { discard.discard(); } return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres)); } Err(_timeout) => { discard.discard(); return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres)); } } } }; match batch_result { // The query successfully completed. Ok(_) => { let json_output = String::from_utf8(json_buf).expect("json should be valid utf8"); Ok(json_output) } // The query failed with an error Err(e) => { discard.discard(); Err(e) } } } } impl BatchQueryData { async fn process( self, config: &'static HttpConfig, cancel: CancellationToken, client: &mut Client, parsed_headers: HttpHeaders, ) -> Result<String, SqlOverHttpError> { info!("starting transaction"); let (inner, mut discard) = client.inner(); let cancel_token = inner.cancel_token(); let mut builder = inner.build_transaction(); if let Some(isolation_level) = parsed_headers.txn_isolation_level { builder = builder.isolation_level(isolation_level); } if parsed_headers.txn_read_only { builder = builder.read_only(true); } if parsed_headers.txn_deferrable { builder = builder.deferrable(true); } let mut transaction = builder .start() .await .inspect_err(|_| { // if we cannot start a transaction, we should return immediately // and not return to the pool. connection is clearly broken discard.discard(); }) .map_err(SqlOverHttpError::Postgres)?; let json_output = match query_batch_to_json( config, cancel.child_token(), &mut transaction, self, parsed_headers, ) .await { Ok(json_output) => { info!("commit"); transaction .commit() .await .inspect_err(|_| { // if we cannot commit - for now don't return connection to pool // TODO: get a query status from the error discard.discard(); }) .map_err(SqlOverHttpError::Postgres)?; json_output } Err(SqlOverHttpError::Cancelled(_)) => { if let Err(err) = cancel_token.cancel_query(NoTls).await { tracing::warn!(?err, "could not cancel query"); } // TODO: after cancelling, wait to see if we can get a status. maybe the connection is still safe. discard.discard(); return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres)); } Err(err) => { return Err(err); } }; Ok(json_output) } } async fn query_batch( config: &'static HttpConfig, cancel: CancellationToken, transaction: &mut Transaction<'_>, queries: BatchQueryData, parsed_headers: HttpHeaders, results: &mut json::ListSer<'_>, ) -> Result<(), SqlOverHttpError> { for stmt in queries.queries { let query = pin!(query_to_json( config, transaction, stmt, results.entry(), parsed_headers, )); let cancelled = pin!(cancel.cancelled()); let res = select(query, cancelled).await; match res { // TODO: maybe we should check that the transaction bit is set here Either::Left((Ok(_), _cancelled)) => {} Either::Left((Err(e), _cancelled)) => { return Err(e); } Either::Right((_cancelled, _)) => { return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres)); } } } Ok(()) } async fn query_batch_to_json( config: &'static HttpConfig, cancel: CancellationToken, tx: &mut Transaction<'_>, queries: BatchQueryData, headers: HttpHeaders, ) -> Result<String, SqlOverHttpError> { let json_output = json::value_to_string!(|obj| json::value_as_object!(|obj| { let results = obj.key("results"); json::value_as_list!(|results| { query_batch(config, cancel, tx, queries, headers, results).await?; }); })); Ok(json_output) } async fn query_to_json<T: GenericClient>( config: &'static HttpConfig, client: &mut T, data: QueryData, output: json::ValueSer<'_>, parsed_headers: HttpHeaders, ) -> Result<ReadyForQueryStatus, SqlOverHttpError> { let query_start = Instant::now(); let mut output = json::ObjectSer::new(output); let mut row_stream = client .query_raw_txt(&data.query, data.params) .await .map_err(SqlOverHttpError::Postgres)?; let query_acknowledged = Instant::now(); let mut json_fields = output.key("fields").list(); for c in row_stream.statement.columns() { let json_field = json_fields.entry(); json::value_as_object!(|json_field| { json_field.entry("name", c.name()); json_field.entry("dataTypeID", c.type_().oid()); json_field.entry("tableID", c.table_oid()); json_field.entry("columnID", c.column_id()); json_field.entry("dataTypeSize", c.type_size()); json_field.entry("dataTypeModifier", c.type_modifier()); json_field.entry("format", "text"); }); } json_fields.finish(); let array_mode = data.array_mode.unwrap_or(parsed_headers.default_array_mode); let raw_output = parsed_headers.raw_output; // Manually drain the stream into a vector to leave row_stream hanging // around to get a command tag. Also check that the response is not too // big. let mut rows = 0; let mut json_rows = output.key("rows").list(); while let Some(row) = row_stream.next().await {
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/conn_pool.rs
proxy/src/serverless/conn_pool.rs
use std::fmt; use std::pin::pin; use std::sync::{Arc, Weak}; use std::task::{Poll, ready}; use futures::future::poll_fn; use futures::{Future, FutureExt}; use postgres_client::tls::MakeTlsConnect; use smallvec::SmallVec; use tokio::net::TcpStream; use tokio::time::Instant; use tokio_util::sync::CancellationToken; use tracing::{error, info, info_span}; #[cfg(test)] use { super::conn_pool_lib::GlobalConnPoolOptions, crate::auth::backend::ComputeUserInfo, std::{sync::atomic, time::Duration}, }; use super::conn_pool_lib::{ Client, ClientDataEnum, ClientInnerCommon, ClientInnerExt, ConnInfo, EndpointConnPool, GlobalConnPool, }; use crate::config::ComputeConfig; use crate::context::RequestContext; use crate::control_plane::messages::MetricsAuxInfo; use crate::metrics::Metrics; type TlsStream = <ComputeConfig as MakeTlsConnect<TcpStream>>::Stream; #[derive(Debug, Clone)] pub(crate) struct ConnInfoWithAuth { pub(crate) conn_info: ConnInfo, pub(crate) auth: AuthData, } #[derive(Debug, Clone)] pub(crate) enum AuthData { Password(SmallVec<[u8; 16]>), Jwt(String), } impl fmt::Display for ConnInfo { // use custom display to avoid logging password fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "{}@{}/{}?{}", self.user_info.user, self.user_info.endpoint, self.dbname, self.user_info.options.get_cache_key("") ) } } pub(crate) fn poll_client<C: ClientInnerExt>( global_pool: Arc<GlobalConnPool<C, EndpointConnPool<C>>>, ctx: &RequestContext, conn_info: ConnInfo, client: C, mut connection: postgres_client::Connection<TcpStream, TlsStream>, conn_id: uuid::Uuid, aux: MetricsAuxInfo, ) -> Client<C> { let conn_gauge = Metrics::get().proxy.db_connections.guard(ctx.protocol()); let mut session_id = ctx.session_id(); let (tx, mut rx) = tokio::sync::watch::channel(session_id); let span = info_span!(parent: None, "connection", %conn_id); let cold_start_info = ctx.cold_start_info(); span.in_scope(|| { info!(cold_start_info = cold_start_info.as_str(), %conn_info, %session_id, "new connection"); }); let pool = match conn_info.endpoint_cache_key() { Some(endpoint) => Arc::downgrade(&global_pool.get_or_create_endpoint_pool(&endpoint)), None => Weak::new(), }; let pool_clone = pool.clone(); let db_user = conn_info.db_and_user(); let idle = global_pool.get_idle_timeout(); let cancel = CancellationToken::new(); let cancelled = cancel.clone().cancelled_owned(); tokio::spawn(async move { let _conn_gauge = conn_gauge; let mut idle_timeout = pin!(tokio::time::sleep(idle)); let mut cancelled = pin!(cancelled); poll_fn(move |cx| { let _instrument = span.enter(); if cancelled.as_mut().poll(cx).is_ready() { info!("connection dropped"); return Poll::Ready(()); } match rx.has_changed() { Ok(true) => { session_id = *rx.borrow_and_update(); info!(%session_id, "changed session"); idle_timeout.as_mut().reset(Instant::now() + idle); } Err(_) => { info!("connection dropped"); return Poll::Ready(()); } _ => {} } // 5 minute idle connection timeout if idle_timeout.as_mut().poll(cx).is_ready() { idle_timeout.as_mut().reset(Instant::now() + idle); info!("connection idle"); if let Some(pool) = pool.clone().upgrade() { // remove client from pool - should close the connection if it's idle. // does nothing if the client is currently checked-out and in-use if pool.write().remove_client(db_user.clone(), conn_id) { info!("idle connection removed"); } } } match ready!(connection.poll_unpin(cx)) { Err(e) => error!(%session_id, "connection error: {}", e), Ok(()) => info!("connection closed"), } // remove from connection pool if let Some(pool) = pool.clone().upgrade() && pool.write().remove_client(db_user.clone(), conn_id) { info!("closed connection removed"); } Poll::Ready(()) }) .await; }); let inner = ClientInnerCommon { inner: client, aux, conn_id, data: ClientDataEnum::Remote(ClientDataRemote { session: tx, cancel, }), }; Client::new(inner, conn_info, pool_clone) } #[derive(Clone)] pub(crate) struct ClientDataRemote { session: tokio::sync::watch::Sender<uuid::Uuid>, cancel: CancellationToken, } impl ClientDataRemote { pub fn session(&mut self) -> &mut tokio::sync::watch::Sender<uuid::Uuid> { &mut self.session } pub fn cancel(&mut self) { self.cancel.cancel(); } } #[cfg(test)] mod tests { use std::sync::atomic::AtomicBool; use super::*; use crate::proxy::NeonOptions; use crate::serverless::cancel_set::CancelSet; use crate::types::{BranchId, EndpointId, ProjectId}; struct MockClient(Arc<AtomicBool>); impl MockClient { fn new(is_closed: bool) -> Self { MockClient(Arc::new(is_closed.into())) } } impl ClientInnerExt for MockClient { fn is_closed(&self) -> bool { self.0.load(atomic::Ordering::Relaxed) } fn get_process_id(&self) -> i32 { 0 } fn reset(&mut self) -> Result<(), postgres_client::Error> { Ok(()) } } fn create_inner() -> ClientInnerCommon<MockClient> { create_inner_with(MockClient::new(false)) } fn create_inner_with(client: MockClient) -> ClientInnerCommon<MockClient> { ClientInnerCommon { inner: client, aux: MetricsAuxInfo { endpoint_id: (&EndpointId::from("endpoint")).into(), project_id: (&ProjectId::from("project")).into(), branch_id: (&BranchId::from("branch")).into(), compute_id: "compute".into(), cold_start_info: crate::control_plane::messages::ColdStartInfo::Warm, }, conn_id: uuid::Uuid::new_v4(), data: ClientDataEnum::Remote(ClientDataRemote { session: tokio::sync::watch::Sender::new(uuid::Uuid::new_v4()), cancel: CancellationToken::new(), }), } } #[tokio::test] async fn test_pool() { let _ = env_logger::try_init(); let config = Box::leak(Box::new(crate::config::HttpConfig { accept_websockets: false, pool_options: GlobalConnPoolOptions { max_conns_per_endpoint: 2, gc_epoch: Duration::from_secs(1), pool_shards: 2, idle_timeout: Duration::from_secs(1), opt_in: false, max_total_conns: 3, }, cancel_set: CancelSet::new(0), client_conn_threshold: u64::MAX, max_request_size_bytes: usize::MAX, max_response_size_bytes: usize::MAX, })); let pool = GlobalConnPool::new(config); let conn_info = ConnInfo { user_info: ComputeUserInfo { user: "user".into(), endpoint: "endpoint".into(), options: NeonOptions::default(), }, dbname: "dbname".into(), }; let ep_pool = Arc::downgrade( &pool.get_or_create_endpoint_pool(&conn_info.endpoint_cache_key().unwrap()), ); { let mut client = Client::new(create_inner(), conn_info.clone(), ep_pool.clone()); assert_eq!(0, pool.get_global_connections_count()); client.inner().1.discard(); // Discard should not add the connection from the pool. assert_eq!(0, pool.get_global_connections_count()); } { let client = Client::new(create_inner(), conn_info.clone(), ep_pool.clone()); drop(client); assert_eq!(1, pool.get_global_connections_count()); } { let closed_client = Client::new( create_inner_with(MockClient::new(true)), conn_info.clone(), ep_pool.clone(), ); drop(closed_client); assert_eq!(1, pool.get_global_connections_count()); } let is_closed: Arc<AtomicBool> = Arc::new(false.into()); { let client = Client::new( create_inner_with(MockClient(is_closed.clone())), conn_info.clone(), ep_pool.clone(), ); drop(client); // The client should be added to the pool. assert_eq!(2, pool.get_global_connections_count()); } { let client = Client::new(create_inner(), conn_info, ep_pool); drop(client); // The client shouldn't be added to the pool. Because the ep-pool is full. assert_eq!(2, pool.get_global_connections_count()); } let conn_info = ConnInfo { user_info: ComputeUserInfo { user: "user".into(), endpoint: "endpoint-2".into(), options: NeonOptions::default(), }, dbname: "dbname".into(), }; let ep_pool = Arc::downgrade( &pool.get_or_create_endpoint_pool(&conn_info.endpoint_cache_key().unwrap()), ); { let client = Client::new(create_inner(), conn_info.clone(), ep_pool.clone()); drop(client); assert_eq!(3, pool.get_global_connections_count()); } { let client = Client::new(create_inner(), conn_info.clone(), ep_pool.clone()); drop(client); // The client shouldn't be added to the pool. Because the global pool is full. assert_eq!(3, pool.get_global_connections_count()); } is_closed.store(true, atomic::Ordering::Relaxed); // Do gc for all shards. pool.gc(0); pool.gc(1); // Closed client should be removed from the pool. assert_eq!(2, pool.get_global_connections_count()); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/http_conn_pool.rs
proxy/src/serverless/http_conn_pool.rs
use std::collections::VecDeque; use std::sync::atomic::{self, AtomicUsize}; use std::sync::{Arc, Weak}; use bytes::Bytes; use http_body_util::combinators::BoxBody; use hyper::client::conn::http2; use hyper_util::rt::{TokioExecutor, TokioIo}; use parking_lot::RwLock; use smol_str::ToSmolStr; use tracing::{Instrument, debug, error, info, info_span}; use super::AsyncRW; use super::backend::HttpConnError; use super::conn_pool_lib::{ ClientDataEnum, ClientInnerCommon, ClientInnerExt, ConnInfo, ConnPoolEntry, EndpointConnPoolExt, GlobalConnPool, }; use crate::context::RequestContext; use crate::control_plane::messages::{ColdStartInfo, MetricsAuxInfo}; use crate::metrics::{HttpEndpointPoolsGuard, Metrics}; use crate::protocol2::ConnectionInfoExtra; use crate::types::EndpointCacheKey; use crate::usage_metrics::{Ids, MetricCounter, USAGE_METRICS}; pub(crate) type LocalProxyClient = http2::SendRequest<BoxBody<Bytes, hyper::Error>>; pub(crate) type LocalProxyConnection = http2::Connection<TokioIo<AsyncRW>, BoxBody<Bytes, hyper::Error>, TokioExecutor>; #[derive(Clone)] pub(crate) struct ClientDataHttp(); // Per-endpoint connection pool // Number of open connections is limited by the `max_conns_per_endpoint`. pub(crate) struct HttpConnPool<C: ClientInnerExt + Clone> { // TODO(conrad): // either we should open more connections depending on stream count // (not exposed by hyper, need our own counter) // or we can change this to an Option rather than a VecDeque. // // Opening more connections to the same db because we run out of streams // seems somewhat redundant though. // // Probably we should run a semaphore and just the single conn. TBD. conns: VecDeque<ConnPoolEntry<C>>, _guard: HttpEndpointPoolsGuard<'static>, global_connections_count: Arc<AtomicUsize>, } impl<C: ClientInnerExt + Clone> HttpConnPool<C> { fn get_conn_entry(&mut self) -> Option<ConnPoolEntry<C>> { let Self { conns, .. } = self; loop { let conn = conns.pop_front()?; if !conn.conn.inner.is_closed() { let new_conn = ConnPoolEntry { conn: conn.conn.clone(), _last_access: std::time::Instant::now(), }; conns.push_back(new_conn); return Some(conn); } } } fn remove_conn(&mut self, conn_id: uuid::Uuid) -> bool { let Self { conns, global_connections_count, .. } = self; let old_len = conns.len(); conns.retain(|entry| entry.conn.conn_id != conn_id); let new_len = conns.len(); let removed = old_len - new_len; if removed > 0 { global_connections_count.fetch_sub(removed, atomic::Ordering::Relaxed); Metrics::get() .proxy .http_pool_opened_connections .get_metric() .dec_by(removed as i64); } removed > 0 } } impl<C: ClientInnerExt + Clone> EndpointConnPoolExt<C> for HttpConnPool<C> { fn clear_closed(&mut self) -> usize { let Self { conns, .. } = self; let old_len = conns.len(); conns.retain(|entry| !entry.conn.inner.is_closed()); let new_len = conns.len(); old_len - new_len } fn total_conns(&self) -> usize { self.conns.len() } } impl<C: ClientInnerExt + Clone> Drop for HttpConnPool<C> { fn drop(&mut self) { if !self.conns.is_empty() { self.global_connections_count .fetch_sub(self.conns.len(), atomic::Ordering::Relaxed); Metrics::get() .proxy .http_pool_opened_connections .get_metric() .dec_by(self.conns.len() as i64); } } } impl<C: ClientInnerExt + Clone> GlobalConnPool<C, HttpConnPool<C>> { #[expect(unused_results)] pub(crate) fn get( self: &Arc<Self>, ctx: &RequestContext, conn_info: &ConnInfo, ) -> Result<Option<Client<C>>, HttpConnError> { let result: Result<Option<Client<C>>, HttpConnError>; let Some(endpoint) = conn_info.endpoint_cache_key() else { result = Ok(None); return result; }; let endpoint_pool = self.get_or_create_endpoint_pool(&endpoint); let Some(client) = endpoint_pool.write().get_conn_entry() else { result = Ok(None); return result; }; tracing::Span::current().record("conn_id", tracing::field::display(client.conn.conn_id)); debug!( cold_start_info = ColdStartInfo::HttpPoolHit.as_str(), "pool: reusing connection '{conn_info}'" ); ctx.set_cold_start_info(ColdStartInfo::HttpPoolHit); ctx.success(); Ok(Some(Client::new(client.conn.clone()))) } fn get_or_create_endpoint_pool( self: &Arc<Self>, endpoint: &EndpointCacheKey, ) -> Arc<RwLock<HttpConnPool<C>>> { // fast path if let Some(pool) = self.global_pool.get(endpoint) { return pool.clone(); } // slow path let new_pool = Arc::new(RwLock::new(HttpConnPool { conns: VecDeque::new(), _guard: Metrics::get().proxy.http_endpoint_pools.guard(), global_connections_count: self.global_connections_count.clone(), })); // find or create a pool for this endpoint let mut created = false; let pool = self .global_pool .entry(endpoint.clone()) .or_insert_with(|| { created = true; new_pool }) .clone(); // log new global pool size if created { let global_pool_size = self .global_pool_size .fetch_add(1, atomic::Ordering::Relaxed) + 1; info!( "pool: created new pool for '{endpoint}', global pool size now {global_pool_size}" ); } pool } } pub(crate) fn poll_http2_client( global_pool: Arc<GlobalConnPool<LocalProxyClient, HttpConnPool<LocalProxyClient>>>, ctx: &RequestContext, conn_info: &ConnInfo, client: LocalProxyClient, connection: LocalProxyConnection, conn_id: uuid::Uuid, aux: MetricsAuxInfo, ) -> Client<LocalProxyClient> { let conn_gauge = Metrics::get().proxy.db_connections.guard(ctx.protocol()); let session_id = ctx.session_id(); let span = info_span!(parent: None, "connection", %conn_id); let cold_start_info = ctx.cold_start_info(); span.in_scope(|| { info!(cold_start_info = cold_start_info.as_str(), %conn_info, %session_id, "new connection"); }); let pool = match conn_info.endpoint_cache_key() { Some(endpoint) => { let pool = global_pool.get_or_create_endpoint_pool(&endpoint); let client = ClientInnerCommon { inner: client.clone(), aux: aux.clone(), conn_id, data: ClientDataEnum::Http(ClientDataHttp()), }; pool.write().conns.push_back(ConnPoolEntry { conn: client, _last_access: std::time::Instant::now(), }); Metrics::get() .proxy .http_pool_opened_connections .get_metric() .inc(); Arc::downgrade(&pool) } None => Weak::new(), }; tokio::spawn( async move { let _conn_gauge = conn_gauge; let res = connection.await; match res { Ok(()) => info!("connection closed"), Err(e) => error!(%session_id, "connection error: {e:?}"), } // remove from connection pool if let Some(pool) = pool.clone().upgrade() && pool.write().remove_conn(conn_id) { info!("closed connection removed"); } } .instrument(span), ); let client = ClientInnerCommon { inner: client, aux, conn_id, data: ClientDataEnum::Http(ClientDataHttp()), }; Client::new(client) } pub(crate) struct Client<C: ClientInnerExt + Clone> { pub(crate) inner: ClientInnerCommon<C>, } impl<C: ClientInnerExt + Clone> Client<C> { pub(self) fn new(inner: ClientInnerCommon<C>) -> Self { Self { inner } } pub(crate) fn metrics(&self, ctx: &RequestContext) -> Arc<MetricCounter> { let aux = &self.inner.aux; let private_link_id = match ctx.extra() { None => None, Some(ConnectionInfoExtra::Aws { vpce_id }) => Some(vpce_id.clone()), Some(ConnectionInfoExtra::Azure { link_id }) => Some(link_id.to_smolstr()), }; USAGE_METRICS.register(Ids { endpoint_id: aux.endpoint_id, branch_id: aux.branch_id, private_link_id, }) } } impl ClientInnerExt for LocalProxyClient { fn is_closed(&self) -> bool { self.is_closed() } fn get_process_id(&self) -> i32 { // ideally throw something meaningful -1 } fn reset(&mut self) -> Result<(), postgres_client::Error> { // We use HTTP/2.0 to talk to local proxy. HTTP is stateless, // so there's nothing to reset. Ok(()) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/serverless/http_util.rs
proxy/src/serverless/http_util.rs
//! Things stolen from `libs/utils/src/http` to add hyper 1.0 compatibility //! Will merge back in at some point in the future. use anyhow::Context; use bytes::Bytes; use http::header::AUTHORIZATION; use http::{HeaderMap, HeaderName, HeaderValue, Response, StatusCode}; use http_body_util::combinators::BoxBody; use http_body_util::{BodyExt, Full}; use http_utils::error::ApiError; use serde::Serialize; use url::Url; use uuid::Uuid; use super::conn_pool::{AuthData, ConnInfoWithAuth}; use super::conn_pool_lib::ConnInfo; use super::error::{ConnInfoError, Credentials}; use crate::auth::backend::ComputeUserInfo; use crate::config::AuthenticationConfig; use crate::context::RequestContext; use crate::metrics::{Metrics, SniGroup, SniKind}; use crate::pqproto::StartupMessageParams; use crate::proxy::NeonOptions; use crate::types::{DbName, EndpointId, RoleName}; // Common header names used across serverless modules pub(super) static NEON_REQUEST_ID: HeaderName = HeaderName::from_static("neon-request-id"); pub(super) static CONN_STRING: HeaderName = HeaderName::from_static("neon-connection-string"); pub(super) static RAW_TEXT_OUTPUT: HeaderName = HeaderName::from_static("neon-raw-text-output"); pub(super) static ARRAY_MODE: HeaderName = HeaderName::from_static("neon-array-mode"); pub(super) static ALLOW_POOL: HeaderName = HeaderName::from_static("neon-pool-opt-in"); pub(super) static TXN_ISOLATION_LEVEL: HeaderName = HeaderName::from_static("neon-batch-isolation-level"); pub(super) static TXN_READ_ONLY: HeaderName = HeaderName::from_static("neon-batch-read-only"); pub(super) static TXN_DEFERRABLE: HeaderName = HeaderName::from_static("neon-batch-deferrable"); pub(crate) fn uuid_to_header_value(id: Uuid) -> HeaderValue { let mut uuid = [0; uuid::fmt::Hyphenated::LENGTH]; HeaderValue::from_str(id.as_hyphenated().encode_lower(&mut uuid[..])) .expect("uuid hyphenated format should be all valid header characters") } /// Like [`ApiError::into_response`] pub(crate) fn api_error_into_response(this: ApiError) -> Response<BoxBody<Bytes, hyper::Error>> { match this { ApiError::BadRequest(err) => HttpErrorBody::response_from_msg_and_status( format!("{err:#?}"), // use debug printing so that we give the cause StatusCode::BAD_REQUEST, ), ApiError::Forbidden(_) => { HttpErrorBody::response_from_msg_and_status(this.to_string(), StatusCode::FORBIDDEN) } ApiError::Unauthorized(_) => { HttpErrorBody::response_from_msg_and_status(this.to_string(), StatusCode::UNAUTHORIZED) } ApiError::NotFound(_) => { HttpErrorBody::response_from_msg_and_status(this.to_string(), StatusCode::NOT_FOUND) } ApiError::Conflict(_) => { HttpErrorBody::response_from_msg_and_status(this.to_string(), StatusCode::CONFLICT) } ApiError::PreconditionFailed(_) => HttpErrorBody::response_from_msg_and_status( this.to_string(), StatusCode::PRECONDITION_FAILED, ), ApiError::ShuttingDown => HttpErrorBody::response_from_msg_and_status( "Shutting down".to_string(), StatusCode::SERVICE_UNAVAILABLE, ), ApiError::ResourceUnavailable(err) => HttpErrorBody::response_from_msg_and_status( err.to_string(), StatusCode::SERVICE_UNAVAILABLE, ), ApiError::TooManyRequests(err) => HttpErrorBody::response_from_msg_and_status( err.to_string(), StatusCode::TOO_MANY_REQUESTS, ), ApiError::Timeout(err) => HttpErrorBody::response_from_msg_and_status( err.to_string(), StatusCode::REQUEST_TIMEOUT, ), ApiError::Cancelled => HttpErrorBody::response_from_msg_and_status( this.to_string(), StatusCode::INTERNAL_SERVER_ERROR, ), ApiError::InternalServerError(err) => HttpErrorBody::response_from_msg_and_status( err.to_string(), StatusCode::INTERNAL_SERVER_ERROR, ), } } /// Same as [`http_utils::error::HttpErrorBody`] #[derive(Serialize)] struct HttpErrorBody { pub(crate) msg: String, } impl HttpErrorBody { /// Same as [`http_utils::error::HttpErrorBody::response_from_msg_and_status`] fn response_from_msg_and_status( msg: String, status: StatusCode, ) -> Response<BoxBody<Bytes, hyper::Error>> { HttpErrorBody { msg }.to_response(status) } /// Same as [`http_utils::error::HttpErrorBody::to_response`] fn to_response(&self, status: StatusCode) -> Response<BoxBody<Bytes, hyper::Error>> { Response::builder() .status(status) .header(http::header::CONTENT_TYPE, "application/json") // we do not have nested maps with non string keys so serialization shouldn't fail .body( Full::new(Bytes::from( serde_json::to_string(self) .expect("serialising HttpErrorBody should never fail"), )) .map_err(|x| match x {}) .boxed(), ) .expect("content-type header should be valid") } } /// Same as [`http_utils::json::json_response`] pub(crate) fn json_response<T: Serialize>( status: StatusCode, data: T, ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, ApiError> { let json = serde_json::to_string(&data) .context("Failed to serialize JSON response") .map_err(ApiError::InternalServerError)?; let response = Response::builder() .status(status) .header(http::header::CONTENT_TYPE, "application/json") .body(Full::new(Bytes::from(json)).map_err(|x| match x {}).boxed()) .map_err(|e| ApiError::InternalServerError(e.into()))?; Ok(response) } pub(crate) fn get_conn_info( config: &'static AuthenticationConfig, ctx: &RequestContext, connection_string: Option<&str>, headers: &HeaderMap, ) -> Result<ConnInfoWithAuth, ConnInfoError> { let connection_url = match connection_string { Some(connection_string) => Url::parse(connection_string)?, None => { let connection_string = headers .get(&CONN_STRING) .ok_or(ConnInfoError::InvalidHeader(&CONN_STRING))? .to_str() .map_err(|_| ConnInfoError::InvalidHeader(&CONN_STRING))?; Url::parse(connection_string)? } }; let protocol = connection_url.scheme(); if protocol != "postgres" && protocol != "postgresql" { return Err(ConnInfoError::IncorrectScheme); } let mut url_path = connection_url .path_segments() .ok_or(ConnInfoError::MissingDbName)?; let dbname: DbName = urlencoding::decode(url_path.next().ok_or(ConnInfoError::InvalidDbName)?)?.into(); ctx.set_dbname(dbname.clone()); let username = RoleName::from(urlencoding::decode(connection_url.username())?); if username.is_empty() { return Err(ConnInfoError::MissingUsername); } ctx.set_user(username.clone()); // TODO: make sure this is right in the context of rest broker let auth = if let Some(auth) = headers.get(&AUTHORIZATION) { if !config.accept_jwts { return Err(ConnInfoError::MissingCredentials(Credentials::Password)); } let auth = auth .to_str() .map_err(|_| ConnInfoError::InvalidHeader(&AUTHORIZATION))?; AuthData::Jwt( auth.strip_prefix("Bearer ") .ok_or(ConnInfoError::MissingCredentials(Credentials::BearerJwt))? .into(), ) } else if let Some(pass) = connection_url.password() { // wrong credentials provided if config.accept_jwts { return Err(ConnInfoError::MissingCredentials(Credentials::BearerJwt)); } AuthData::Password(match urlencoding::decode_binary(pass.as_bytes()) { std::borrow::Cow::Borrowed(b) => b.into(), std::borrow::Cow::Owned(b) => b.into(), }) } else if config.accept_jwts { return Err(ConnInfoError::MissingCredentials(Credentials::BearerJwt)); } else { return Err(ConnInfoError::MissingCredentials(Credentials::Password)); }; let endpoint: EndpointId = match connection_url.host() { Some(url::Host::Domain(hostname)) => hostname .split_once('.') .map_or(hostname, |(prefix, _)| prefix) .into(), Some(url::Host::Ipv4(_) | url::Host::Ipv6(_)) | None => { return Err(ConnInfoError::MissingHostname); } }; ctx.set_endpoint_id(endpoint.clone()); let pairs = connection_url.query_pairs(); let mut options = Option::None; let mut params = StartupMessageParams::default(); params.insert("user", &username); params.insert("database", &dbname); for (key, value) in pairs { params.insert(&key, &value); if key == "options" { options = Some(NeonOptions::parse_options_raw(&value)); } } // check the URL that was used, for metrics { let host_endpoint = headers // get the host header .get("host") // extract the domain .and_then(|h| { let (host, _port) = h.to_str().ok()?.split_once(':')?; Some(host) }) // get the endpoint prefix .map(|h| h.split_once('.').map_or(h, |(prefix, _)| prefix)); let kind = if host_endpoint == Some(&*endpoint) { SniKind::Sni } else { SniKind::NoSni }; let protocol = ctx.protocol(); Metrics::get() .proxy .accepted_connections_by_sni .inc(SniGroup { protocol, kind }); } ctx.set_user_agent( headers .get(hyper::header::USER_AGENT) .and_then(|h| h.to_str().ok()) .map(Into::into), ); let user_info = ComputeUserInfo { endpoint, user: username, options: options.unwrap_or_default(), }; let conn_info = ConnInfo { user_info, dbname }; Ok(ConnInfoWithAuth { conn_info, auth }) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/messages.rs
proxy/src/scram/messages.rs
//! Definitions for SCRAM messages. use std::fmt; use std::ops::Range; use base64::Engine as _; use base64::prelude::BASE64_STANDARD; use super::base64_decode_array; use super::key::{SCRAM_KEY_LEN, ScramKey}; use super::signature::SignatureBuilder; use crate::sasl::ChannelBinding; /// Faithfully taken from PostgreSQL. pub(crate) const SCRAM_RAW_NONCE_LEN: usize = 18; /// Although we ignore all extensions, we still have to validate the message. fn validate_sasl_extensions<'a>(parts: impl Iterator<Item = &'a str>) -> Option<()> { for mut chars in parts.map(|s| s.chars()) { let attr = chars.next()?; if !attr.is_ascii_alphabetic() { return None; } let eq = chars.next()?; if eq != '=' { return None; } } Some(()) } #[derive(Debug)] pub(crate) struct ClientFirstMessage<'a> { /// `client-first-message-bare`. pub(crate) bare: &'a str, /// Channel binding mode. pub(crate) cbind_flag: ChannelBinding<&'a str>, /// Client nonce. pub(crate) nonce: &'a str, } impl<'a> ClientFirstMessage<'a> { // NB: FromStr doesn't work with lifetimes pub(crate) fn parse(input: &'a str) -> Option<Self> { let mut parts = input.split(','); let cbind_flag = ChannelBinding::parse(parts.next()?)?; // PG doesn't support authorization identity, // so we don't bother defining GS2 header type let authzid = parts.next()?; if !authzid.is_empty() { return None; } // Unfortunately, `parts.as_str()` is unstable let pos = authzid.as_ptr() as usize - input.as_ptr() as usize + 1; let (_, bare) = input.split_at(pos); // In theory, these might be preceded by "reserved-mext" (i.e. "m=") let username = parts.next()?.strip_prefix("n=")?; // https://github.com/postgres/postgres/blob/f83908798f78c4cafda217ca875602c88ea2ae28/src/backend/libpq/auth-scram.c#L13-L14 if !username.is_empty() { tracing::warn!(username, "scram username provided, but is not expected"); // TODO(conrad): // return None; } let nonce = parts.next()?.strip_prefix("r=")?; // Validate but ignore auth extensions validate_sasl_extensions(parts)?; Some(Self { bare, cbind_flag, nonce, }) } /// Build a response to [`ClientFirstMessage`]. pub(crate) fn build_server_first_message( &self, nonce: &[u8; SCRAM_RAW_NONCE_LEN], salt_base64: &str, iterations: u32, ) -> OwnedServerFirstMessage { let mut message = String::with_capacity(128); message.push_str("r="); // write combined nonce let combined_nonce_start = message.len(); message.push_str(self.nonce); BASE64_STANDARD.encode_string(nonce, &mut message); let combined_nonce = combined_nonce_start..message.len(); // write salt and iterations message.push_str(",s="); message.push_str(salt_base64); message.push_str(",i="); message.push_str(itoa::Buffer::new().format(iterations)); // This design guarantees that it's impossible to create a // server-first-message without receiving a client-first-message OwnedServerFirstMessage { message, nonce: combined_nonce, } } } #[derive(Debug)] pub(crate) struct ClientFinalMessage<'a> { /// `client-final-message-without-proof`. pub(crate) without_proof: &'a str, /// Channel binding data (base64). pub(crate) channel_binding: &'a str, /// Combined client & server nonce. pub(crate) nonce: &'a str, /// Client auth proof. pub(crate) proof: [u8; SCRAM_KEY_LEN], } impl<'a> ClientFinalMessage<'a> { // NB: FromStr doesn't work with lifetimes pub(crate) fn parse(input: &'a str) -> Option<Self> { let (without_proof, proof) = input.rsplit_once(',')?; let mut parts = without_proof.split(','); let channel_binding = parts.next()?.strip_prefix("c=")?; let nonce = parts.next()?.strip_prefix("r=")?; // Validate but ignore auth extensions validate_sasl_extensions(parts)?; let proof = base64_decode_array(proof.strip_prefix("p=")?)?; Some(Self { without_proof, channel_binding, nonce, proof, }) } /// Build a response to [`ClientFinalMessage`]. pub(crate) fn build_server_final_message( &self, signature_builder: SignatureBuilder<'_>, server_key: &ScramKey, ) -> String { let mut buf = String::from("v="); BASE64_STANDARD.encode_string(signature_builder.build(server_key), &mut buf); buf } } /// We need to keep a convenient representation of this /// message for the next authentication step. pub(crate) struct OwnedServerFirstMessage { /// Owned `server-first-message`. message: String, /// Slice into `message`. nonce: Range<usize>, } impl OwnedServerFirstMessage { /// Extract combined nonce from the message. #[inline(always)] pub(crate) fn nonce(&self) -> &str { &self.message[self.nonce.clone()] } /// Get reference to a text representation of the message. #[inline(always)] pub(crate) fn as_str(&self) -> &str { &self.message } } impl fmt::Debug for OwnedServerFirstMessage { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("ServerFirstMessage") .field("message", &self.as_str()) .field("nonce", &self.nonce()) .finish() } } #[cfg(test)] mod tests { use super::*; #[test] fn parse_client_first_message() { use ChannelBinding::*; // (Almost) real strings captured during debug sessions let cases = [ (NotSupportedClient, "n,,n=,r=t8JwklwKecDLwSsA72rHmVju"), (NotSupportedServer, "y,,n=,r=t8JwklwKecDLwSsA72rHmVju"), ( Required("tls-server-end-point"), "p=tls-server-end-point,,n=,r=t8JwklwKecDLwSsA72rHmVju", ), ]; for (cb, input) in cases { let msg = ClientFirstMessage::parse(input).unwrap(); assert_eq!(msg.bare, "n=,r=t8JwklwKecDLwSsA72rHmVju"); assert_eq!(msg.nonce, "t8JwklwKecDLwSsA72rHmVju"); assert_eq!(msg.cbind_flag, cb); } } #[test] fn parse_client_first_message_with_invalid_gs2_authz() { assert!(ClientFirstMessage::parse("n,authzid,n=,r=nonce").is_none()); } #[test] fn parse_client_first_message_with_extra_params() { let msg = ClientFirstMessage::parse("n,,n=,r=nonce,a=foo,b=bar,c=baz").unwrap(); assert_eq!(msg.bare, "n=,r=nonce,a=foo,b=bar,c=baz"); assert_eq!(msg.nonce, "nonce"); assert_eq!(msg.cbind_flag, ChannelBinding::NotSupportedClient); } #[test] fn parse_client_first_message_with_extra_params_invalid() { // must be of the form `<ascii letter>=<...>` assert!(ClientFirstMessage::parse("n,,n=,r=nonce,abc=foo").is_none()); assert!(ClientFirstMessage::parse("n,,n=,r=nonce,1=foo").is_none()); assert!(ClientFirstMessage::parse("n,,n=,r=nonce,a").is_none()); } #[test] fn parse_client_final_message() { let input = [ "c=eSws", "r=iiYEfS3rOgn8S3rtpSdrOsHtPLWvIkdgmHxA0hf3JNOAG4dU", "p=SRpfsIVS4Gk11w1LqQ4QvCUBZYQmqXNSDEcHqbQ3CHI=", ] .join(","); let msg = ClientFinalMessage::parse(&input).unwrap(); assert_eq!( msg.without_proof, "c=eSws,r=iiYEfS3rOgn8S3rtpSdrOsHtPLWvIkdgmHxA0hf3JNOAG4dU" ); assert_eq!( msg.nonce, "iiYEfS3rOgn8S3rtpSdrOsHtPLWvIkdgmHxA0hf3JNOAG4dU" ); assert_eq!( BASE64_STANDARD.encode(msg.proof), "SRpfsIVS4Gk11w1LqQ4QvCUBZYQmqXNSDEcHqbQ3CHI=" ); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/key.rs
proxy/src/scram/key.rs
//! Tools for client/server/stored key management. use hmac::Mac as _; use sha2::Digest as _; use subtle::ConstantTimeEq; use zeroize::Zeroize as _; use crate::metrics::Metrics; use crate::scram::pbkdf2::Prf; /// Faithfully taken from PostgreSQL. pub(crate) const SCRAM_KEY_LEN: usize = 32; /// One of the keys derived from the user's password. /// We use the same structure for all keys, i.e. /// `ClientKey`, `StoredKey`, and `ServerKey`. #[derive(Clone, Default, Eq, Debug)] #[repr(transparent)] pub(crate) struct ScramKey { bytes: [u8; SCRAM_KEY_LEN], } impl Drop for ScramKey { fn drop(&mut self) { self.bytes.zeroize(); } } impl PartialEq for ScramKey { fn eq(&self, other: &Self) -> bool { self.ct_eq(other).into() } } impl ConstantTimeEq for ScramKey { fn ct_eq(&self, other: &Self) -> subtle::Choice { self.bytes.ct_eq(&other.bytes) } } impl ScramKey { pub(crate) fn sha256(&self) -> Self { Metrics::get().proxy.sha_rounds.inc_by(1); Self { bytes: sha2::Sha256::digest(self.as_bytes()).into(), } } pub(crate) fn as_bytes(&self) -> [u8; SCRAM_KEY_LEN] { self.bytes } pub(crate) fn client_key(b: &[u8; 32]) -> Self { // Prf::new_from_slice will run 2 sha256 rounds. // Update + Finalize run 2 sha256 rounds. Metrics::get().proxy.sha_rounds.inc_by(4); let mut prf = Prf::new_from_slice(b).expect("HMAC is able to accept all key sizes"); prf.update(b"Client Key"); let client_key: [u8; 32] = prf.finalize().into_bytes().into(); client_key.into() } } impl From<[u8; SCRAM_KEY_LEN]> for ScramKey { #[inline(always)] fn from(bytes: [u8; SCRAM_KEY_LEN]) -> Self { Self { bytes } } } impl AsRef<[u8]> for ScramKey { #[inline(always)] fn as_ref(&self) -> &[u8] { &self.bytes } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/exchange.rs
proxy/src/scram/exchange.rs
//! Implementation of the SCRAM authentication algorithm. use std::convert::Infallible; use base64::Engine as _; use base64::prelude::BASE64_STANDARD; use tracing::{debug, trace}; use super::messages::{ ClientFinalMessage, ClientFirstMessage, OwnedServerFirstMessage, SCRAM_RAW_NONCE_LEN, }; use super::pbkdf2::Pbkdf2; use super::secret::ServerSecret; use super::signature::SignatureBuilder; use super::threadpool::ThreadPool; use super::{ScramKey, pbkdf2}; use crate::intern::{EndpointIdInt, RoleNameInt}; use crate::sasl::{self, ChannelBinding, Error as SaslError}; use crate::scram::cache::Pbkdf2CacheEntry; /// The only channel binding mode we currently support. #[derive(Debug)] struct TlsServerEndPoint; impl std::fmt::Display for TlsServerEndPoint { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "tls-server-end-point") } } impl std::str::FromStr for TlsServerEndPoint { type Err = sasl::Error; fn from_str(s: &str) -> Result<Self, Self::Err> { match s { "tls-server-end-point" => Ok(TlsServerEndPoint), _ => Err(sasl::Error::ChannelBindingBadMethod(s.into())), } } } struct SaslSentInner { cbind_flag: ChannelBinding<TlsServerEndPoint>, client_first_message_bare: String, server_first_message: OwnedServerFirstMessage, } struct SaslInitial { nonce: fn() -> [u8; SCRAM_RAW_NONCE_LEN], } enum ExchangeState { /// Waiting for [`ClientFirstMessage`]. Initial(SaslInitial), /// Waiting for [`ClientFinalMessage`]. SaltSent(SaslSentInner), } /// Server's side of SCRAM auth algorithm. pub(crate) struct Exchange<'a> { state: ExchangeState, secret: &'a ServerSecret, tls_server_end_point: crate::tls::TlsServerEndPoint, } impl<'a> Exchange<'a> { pub(crate) fn new( secret: &'a ServerSecret, nonce: fn() -> [u8; SCRAM_RAW_NONCE_LEN], tls_server_end_point: crate::tls::TlsServerEndPoint, ) -> Self { Self { state: ExchangeState::Initial(SaslInitial { nonce }), secret, tls_server_end_point, } } } async fn derive_client_key( pool: &ThreadPool, endpoint: EndpointIdInt, password: &[u8], salt: &[u8], iterations: u32, ) -> pbkdf2::Block { pool.spawn_job(endpoint, Pbkdf2::start(password, salt, iterations)) .await } /// For cleartext flow, we need to derive the client key to /// 1. authenticate the client. /// 2. authenticate with compute. pub(crate) async fn exchange( pool: &ThreadPool, endpoint: EndpointIdInt, role: RoleNameInt, secret: &ServerSecret, password: &[u8], ) -> sasl::Result<sasl::Outcome<super::ScramKey>> { if secret.iterations > CACHED_ROUNDS { exchange_with_cache(pool, endpoint, role, secret, password).await } else { let salt = BASE64_STANDARD.decode(&*secret.salt_base64)?; let hash = derive_client_key(pool, endpoint, password, &salt, secret.iterations).await; Ok(validate_pbkdf2(secret, &hash)) } } /// Compute the client key using a cache. We cache the suffix of the pbkdf2 result only, /// which is not enough by itself to perform an offline brute force. async fn exchange_with_cache( pool: &ThreadPool, endpoint: EndpointIdInt, role: RoleNameInt, secret: &ServerSecret, password: &[u8], ) -> sasl::Result<sasl::Outcome<super::ScramKey>> { let salt = BASE64_STANDARD.decode(&*secret.salt_base64)?; debug_assert!( secret.iterations > CACHED_ROUNDS, "we should not cache password data if there isn't enough rounds needed" ); // compute the prefix of the pbkdf2 output. let prefix = derive_client_key(pool, endpoint, password, &salt, CACHED_ROUNDS).await; if let Some(entry) = pool.cache.get_entry(endpoint, role) { // hot path: let's check the threadpool cache if secret.cached_at == entry.cached_from { // cache is valid. compute the full hash by adding the prefix to the suffix. let mut hash = prefix; pbkdf2::xor_assign(&mut hash, &entry.suffix); let outcome = validate_pbkdf2(secret, &hash); if matches!(outcome, sasl::Outcome::Success(_)) { trace!("password validated from cache"); } return Ok(outcome); } // cached key is no longer valid. debug!("invalidating cached password"); entry.invalidate(); } // slow path: full password hash. let hash = derive_client_key(pool, endpoint, password, &salt, secret.iterations).await; let outcome = validate_pbkdf2(secret, &hash); let client_key = match outcome { sasl::Outcome::Success(client_key) => client_key, sasl::Outcome::Failure(_) => return Ok(outcome), }; trace!("storing cached password"); // time to cache, compute the suffix by subtracting the prefix from the hash. let mut suffix = hash; pbkdf2::xor_assign(&mut suffix, &prefix); pool.cache.insert( endpoint, role, Pbkdf2CacheEntry { cached_from: secret.cached_at, suffix, }, ); Ok(sasl::Outcome::Success(client_key)) } fn validate_pbkdf2(secret: &ServerSecret, hash: &pbkdf2::Block) -> sasl::Outcome<ScramKey> { let client_key = super::ScramKey::client_key(&(*hash).into()); if secret.is_password_invalid(&client_key).into() { sasl::Outcome::Failure("password doesn't match") } else { sasl::Outcome::Success(client_key) } } const CACHED_ROUNDS: u32 = 16; impl SaslInitial { fn transition( &self, secret: &ServerSecret, tls_server_end_point: &crate::tls::TlsServerEndPoint, input: &str, ) -> sasl::Result<sasl::Step<SaslSentInner, Infallible>> { let client_first_message = ClientFirstMessage::parse(input) .ok_or(SaslError::BadClientMessage("invalid client-first-message"))?; // If the flag is set to "y" and the server supports channel // binding, the server MUST fail authentication if client_first_message.cbind_flag == ChannelBinding::NotSupportedServer && tls_server_end_point.supported() { return Err(SaslError::ChannelBindingFailed("SCRAM-PLUS not used")); } let server_first_message = client_first_message.build_server_first_message( &(self.nonce)(), &secret.salt_base64, secret.iterations, ); let msg = server_first_message.as_str().to_owned(); let next = SaslSentInner { cbind_flag: client_first_message.cbind_flag.and_then(str::parse)?, client_first_message_bare: client_first_message.bare.to_owned(), server_first_message, }; Ok(sasl::Step::Continue(next, msg)) } } impl SaslSentInner { fn transition( &self, secret: &ServerSecret, tls_server_end_point: &crate::tls::TlsServerEndPoint, input: &str, ) -> sasl::Result<sasl::Step<Infallible, super::ScramKey>> { let Self { cbind_flag, client_first_message_bare, server_first_message, } = self; let client_final_message = ClientFinalMessage::parse(input) .ok_or(SaslError::BadClientMessage("invalid client-final-message"))?; let channel_binding = cbind_flag.encode(|_| match tls_server_end_point { crate::tls::TlsServerEndPoint::Sha256(x) => Ok(x), crate::tls::TlsServerEndPoint::Undefined => Err(SaslError::MissingBinding), })?; // This might've been caused by a MITM attack if client_final_message.channel_binding != channel_binding { return Err(SaslError::ChannelBindingFailed( "insecure connection: secure channel data mismatch", )); } if client_final_message.nonce != server_first_message.nonce() { return Err(SaslError::BadClientMessage("combined nonce doesn't match")); } let signature_builder = SignatureBuilder { client_first_message_bare, server_first_message: server_first_message.as_str(), client_final_message_without_proof: client_final_message.without_proof, }; let client_key = signature_builder .build(&secret.stored_key) .derive_client_key(&client_final_message.proof); // Auth fails either if keys don't match or it's pre-determined to fail. if secret.is_password_invalid(&client_key).into() { return Ok(sasl::Step::Failure("password doesn't match")); } let msg = client_final_message.build_server_final_message(signature_builder, &secret.server_key); Ok(sasl::Step::Success(client_key, msg)) } } impl sasl::Mechanism for Exchange<'_> { type Output = super::ScramKey; fn exchange(mut self, input: &str) -> sasl::Result<sasl::Step<Self, Self::Output>> { use ExchangeState; use sasl::Step; match &self.state { ExchangeState::Initial(init) => { match init.transition(self.secret, &self.tls_server_end_point, input)? { Step::Continue(sent, msg) => { self.state = ExchangeState::SaltSent(sent); Ok(Step::Continue(self, msg)) } Step::Failure(msg) => Ok(Step::Failure(msg)), } } ExchangeState::SaltSent(sent) => { match sent.transition(self.secret, &self.tls_server_end_point, input)? { Step::Success(keys, msg) => Ok(Step::Success(keys, msg)), Step::Failure(msg) => Ok(Step::Failure(msg)), } } } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/signature.rs
proxy/src/scram/signature.rs
//! Tools for client/server signature management. use hmac::Mac as _; use super::key::{SCRAM_KEY_LEN, ScramKey}; use crate::metrics::Metrics; use crate::scram::pbkdf2::Prf; /// A collection of message parts needed to derive the client's signature. #[derive(Debug)] pub(crate) struct SignatureBuilder<'a> { pub(crate) client_first_message_bare: &'a str, pub(crate) server_first_message: &'a str, pub(crate) client_final_message_without_proof: &'a str, } impl SignatureBuilder<'_> { pub(crate) fn build(&self, key: &ScramKey) -> Signature { // don't know exactly. this is a rough approx Metrics::get().proxy.sha_rounds.inc_by(8); let mut mac = Prf::new_from_slice(key.as_ref()).expect("HMAC accepts all key sizes"); mac.update(self.client_first_message_bare.as_bytes()); mac.update(b","); mac.update(self.server_first_message.as_bytes()); mac.update(b","); mac.update(self.client_final_message_without_proof.as_bytes()); Signature { bytes: mac.finalize().into_bytes().into(), } } } /// A computed value which, when xored with `ClientProof`, /// produces `ClientKey` that we need for authentication. #[derive(Debug)] #[repr(transparent)] pub(crate) struct Signature { bytes: [u8; SCRAM_KEY_LEN], } impl Signature { /// Derive `ClientKey` from client's signature and proof. pub(crate) fn derive_client_key(&self, proof: &[u8; SCRAM_KEY_LEN]) -> ScramKey { // This is how the proof is calculated: // // 1. sha256(ClientKey) -> StoredKey // 2. hmac_sha256(StoredKey, [messages...]) -> ClientSignature // 3. ClientKey ^ ClientSignature -> ClientProof // // Step 3 implies that we can restore ClientKey from the proof // by xoring the latter with the ClientSignature. Afterwards we // can check that the presumed ClientKey meets our expectations. let mut signature = self.bytes; for (i, x) in proof.iter().enumerate() { signature[i] ^= x; } signature.into() } } impl From<[u8; SCRAM_KEY_LEN]> for Signature { fn from(bytes: [u8; SCRAM_KEY_LEN]) -> Self { Self { bytes } } } impl AsRef<[u8]> for Signature { fn as_ref(&self) -> &[u8] { &self.bytes } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/threadpool.rs
proxy/src/scram/threadpool.rs
//! Custom threadpool implementation for password hashing. //! //! Requirements: //! 1. Fairness per endpoint. //! 2. Yield support for high iteration counts. use std::cell::RefCell; use std::future::Future; use std::pin::Pin; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Weak}; use std::task::{Context, Poll}; use futures::FutureExt; use rand::rngs::SmallRng; use rand::{Rng, SeedableRng}; use super::cache::Pbkdf2Cache; use super::pbkdf2; use super::pbkdf2::Pbkdf2; use crate::intern::EndpointIdInt; use crate::metrics::{ThreadPoolMetrics, ThreadPoolWorkerId}; use crate::scram::countmin::CountMinSketch; pub struct ThreadPool { runtime: Option<tokio::runtime::Runtime>, pub metrics: Arc<ThreadPoolMetrics>, // we hash a lot of passwords. // we keep a cache of partial hashes for faster validation. pub(super) cache: Pbkdf2Cache, } /// How often to reset the sketch values const SKETCH_RESET_INTERVAL: u64 = 1021; thread_local! { static STATE: RefCell<Option<ThreadRt>> = const { RefCell::new(None) }; } impl ThreadPool { pub fn new(mut n_workers: u8) -> Arc<Self> { // rayon would be nice here, but yielding in rayon does not work well afaict. if n_workers == 0 { n_workers = 1; } Arc::new_cyclic(|pool| { let pool = pool.clone(); let worker_id = AtomicUsize::new(0); let runtime = tokio::runtime::Builder::new_multi_thread() .worker_threads(n_workers as usize) .on_thread_start(move || { STATE.with_borrow_mut(|state| { *state = Some(ThreadRt { pool: pool.clone(), id: ThreadPoolWorkerId(worker_id.fetch_add(1, Ordering::Relaxed)), rng: SmallRng::from_os_rng(), // used to determine whether we should temporarily skip tasks for fairness. // 99% of estimates will overcount by no more than 4096 samples countmin: CountMinSketch::with_params( 1.0 / (SKETCH_RESET_INTERVAL as f64), 0.01, ), tick: 0, }); }); }) .build() .expect("password threadpool runtime should be configured correctly"); Self { runtime: Some(runtime), metrics: Arc::new(ThreadPoolMetrics::new(n_workers as usize)), cache: Pbkdf2Cache::new(), } }) } pub(crate) fn spawn_job(&self, endpoint: EndpointIdInt, pbkdf2: Pbkdf2) -> JobHandle { JobHandle( self.runtime .as_ref() .expect("runtime is always set") .spawn(JobSpec { pbkdf2, endpoint }), ) } } impl Drop for ThreadPool { fn drop(&mut self) { self.runtime .take() .expect("runtime is always set") .shutdown_background(); } } struct ThreadRt { pool: Weak<ThreadPool>, id: ThreadPoolWorkerId, rng: SmallRng, countmin: CountMinSketch, tick: u64, } impl ThreadRt { fn should_run(&mut self, job: &JobSpec) -> bool { let rate = self .countmin .inc_and_return(&job.endpoint, job.pbkdf2.cost()); const P: f64 = 2000.0; // probability decreases as rate increases. // lower probability, higher chance of being skipped // // estimates (rate in terms of 4096 rounds): // rate = 0 => probability = 100% // rate = 10 => probability = 71.3% // rate = 50 => probability = 62.1% // rate = 500 => probability = 52.3% // rate = 1021 => probability = 49.8% // // My expectation is that the pool queue will only begin backing up at ~1000rps // in which case the SKETCH_RESET_INTERVAL represents 1 second. Thus, the rates above // are in requests per second. let probability = P.ln() / (P + rate as f64).ln(); self.rng.random_bool(probability) } } struct JobSpec { pbkdf2: Pbkdf2, endpoint: EndpointIdInt, } impl Future for JobSpec { type Output = pbkdf2::Block; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { STATE.with_borrow_mut(|state| { let state = state.as_mut().expect("should be set on thread startup"); state.tick = state.tick.wrapping_add(1); if state.tick.is_multiple_of(SKETCH_RESET_INTERVAL) { state.countmin.reset(); } if state.should_run(&self) { if let Some(pool) = state.pool.upgrade() { pool.metrics.worker_task_turns_total.inc(state.id); } match self.pbkdf2.turn() { Poll::Ready(result) => Poll::Ready(result), // more to do, we shall requeue Poll::Pending => { cx.waker().wake_by_ref(); Poll::Pending } } } else { if let Some(pool) = state.pool.upgrade() { pool.metrics.worker_task_skips_total.inc(state.id); } cx.waker().wake_by_ref(); Poll::Pending } }) } } pub(crate) struct JobHandle(tokio::task::JoinHandle<pbkdf2::Block>); impl Future for JobHandle { type Output = pbkdf2::Block; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match self.0.poll_unpin(cx) { Poll::Ready(Ok(ok)) => Poll::Ready(ok), Poll::Ready(Err(err)) => std::panic::resume_unwind(err.into_panic()), Poll::Pending => Poll::Pending, } } } impl Drop for JobHandle { fn drop(&mut self) { self.0.abort(); } } #[cfg(test)] mod tests { use super::*; use crate::types::EndpointId; #[tokio::test] async fn hash_is_correct() { let pool = ThreadPool::new(1); let ep = EndpointId::from("foo"); let ep = EndpointIdInt::from(ep); let salt = [0x55; 32]; let actual = pool .spawn_job(ep, Pbkdf2::start(b"password", &salt, 4096)) .await; let expected = &[ 10, 114, 73, 188, 140, 222, 196, 156, 214, 184, 79, 157, 119, 242, 16, 31, 53, 242, 178, 43, 95, 8, 225, 182, 122, 40, 219, 21, 89, 147, 64, 140, ]; assert_eq!(actual.as_slice(), expected); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/countmin.rs
proxy/src/scram/countmin.rs
use std::hash::Hash; /// estimator of hash jobs per second. /// <https://en.wikipedia.org/wiki/Count%E2%80%93min_sketch> pub(crate) struct CountMinSketch { // one for each depth hashers: Vec<ahash::RandomState>, width: usize, depth: usize, // buckets, width*depth buckets: Vec<u32>, } impl CountMinSketch { /// Given parameters (ε, δ), /// set width = ceil(e/ε) /// set depth = ceil(ln(1/δ)) /// /// guarantees: /// actual <= estimate /// estimate <= actual + ε * N with probability 1 - δ /// where N is the cardinality of the stream pub(crate) fn with_params(epsilon: f64, delta: f64) -> Self { CountMinSketch::new( (std::f64::consts::E / epsilon).ceil() as usize, (1.0_f64 / delta).ln().ceil() as usize, ) } fn new(width: usize, depth: usize) -> Self { Self { #[cfg(test)] hashers: (0..depth) .map(|i| { // digits of pi for good randomness ahash::RandomState::with_seeds( 314159265358979323, 84626433832795028, 84197169399375105, 82097494459230781 + i as u64, ) }) .collect(), #[cfg(not(test))] hashers: (0..depth).map(|_| ahash::RandomState::new()).collect(), width, depth, buckets: vec![0; width * depth], } } pub(crate) fn inc_and_return<T: Hash>(&mut self, t: &T, x: u32) -> u32 { let mut min = u32::MAX; for row in 0..self.depth { let col = (self.hashers[row].hash_one(t) as usize) % self.width; let row = &mut self.buckets[row * self.width..][..self.width]; row[col] = row[col].saturating_add(x); min = std::cmp::min(min, row[col]); } min } pub(crate) fn reset(&mut self) { self.buckets.clear(); self.buckets.resize(self.width * self.depth, 0); } } #[cfg(test)] mod tests { use rand::rngs::StdRng; use rand::seq::SliceRandom; use rand::{Rng, SeedableRng}; use super::CountMinSketch; fn eval_precision(n: usize, p: f64, q: f64) -> usize { // fixed value of phi for consistent test let mut rng = StdRng::seed_from_u64(16180339887498948482); #[allow(non_snake_case)] let mut N = 0; let mut ids = vec![]; for _ in 0..n { // number to insert at once let n = rng.random_range(1..4096); // number of insert operations let m = rng.random_range(1..100); let id = uuid::Builder::from_random_bytes(rng.random()).into_uuid(); ids.push((id, n, m)); // N = sum(actual) N += n * m; } // q% of counts will be within p of the actual value let mut sketch = CountMinSketch::with_params(p / N as f64, 1.0 - q); // insert a bunch of entries in a random order let mut ids2 = ids.clone(); while !ids2.is_empty() { ids2.shuffle(&mut rng); ids2.retain_mut(|id| { sketch.inc_and_return(&id.0, id.1); id.2 -= 1; id.2 > 0 }); } let mut within_p = 0; for (id, n, m) in ids { let actual = n * m; let estimate = sketch.inc_and_return(&id, 0); // This estimate has the guarantee that actual <= estimate assert!(actual <= estimate); // This estimate has the guarantee that estimate <= actual + εN with probability 1 - δ. // ε = p / N, δ = 1 - q; // therefore, estimate <= actual + p with probability q. if estimate as f64 <= actual as f64 + p { within_p += 1; } } within_p } #[test] fn precision() { assert_eq!(eval_precision(100, 100.0, 0.99), 100); assert_eq!(eval_precision(1000, 100.0, 0.99), 1000); assert_eq!(eval_precision(100, 4096.0, 0.99), 100); assert_eq!(eval_precision(1000, 4096.0, 0.99), 1000); // seems to be more precise than the literature indicates? // probably numbers are too small to truly represent the probabilities. assert_eq!(eval_precision(100, 4096.0, 0.90), 100); assert_eq!(eval_precision(1000, 4096.0, 0.90), 1000); assert_eq!(eval_precision(100, 4096.0, 0.1), 100); assert_eq!(eval_precision(1000, 4096.0, 0.1), 978); } // returns memory usage in bytes, and the time complexity per insert. fn eval_cost(p: f64, q: f64) -> (usize, usize) { #[allow(non_snake_case)] // N = sum(actual) // Let's assume 1021 samples, all of 4096 let N = 1021 * 4096; let sketch = CountMinSketch::with_params(p / N as f64, 1.0 - q); let memory = size_of::<u32>() * sketch.buckets.len(); let time = sketch.depth; (memory, time) } #[test] fn memory_usage() { assert_eq!(eval_cost(100.0, 0.99), (2273580, 5)); assert_eq!(eval_cost(4096.0, 0.99), (55520, 5)); assert_eq!(eval_cost(4096.0, 0.90), (33312, 3)); assert_eq!(eval_cost(4096.0, 0.1), (11104, 1)); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/secret.rs
proxy/src/scram/secret.rs
//! Tools for SCRAM server secret management. use base64::Engine as _; use base64::prelude::BASE64_STANDARD; use subtle::{Choice, ConstantTimeEq}; use tokio::time::Instant; use super::base64_decode_array; use super::key::ScramKey; /// Server secret is produced from user's password, /// and is used throughout the authentication process. #[derive(Clone, Eq, PartialEq, Debug)] pub(crate) struct ServerSecret { /// When this secret was cached. pub(crate) cached_at: Instant, /// Number of iterations for `PBKDF2` function. pub(crate) iterations: u32, /// Salt used to hash user's password. pub(crate) salt_base64: Box<str>, /// Hashed `ClientKey`. pub(crate) stored_key: ScramKey, /// Used by client to verify server's signature. pub(crate) server_key: ScramKey, /// Should auth fail no matter what? /// This is exactly the case for mocked secrets. pub(crate) doomed: bool, } impl ServerSecret { pub(crate) fn parse(input: &str) -> Option<Self> { // SCRAM-SHA-256$<iterations>:<salt>$<storedkey>:<serverkey> let s = input.strip_prefix("SCRAM-SHA-256$")?; let (params, keys) = s.split_once('$')?; let ((iterations, salt), (stored_key, server_key)) = params.split_once(':').zip(keys.split_once(':'))?; let secret = ServerSecret { cached_at: Instant::now(), iterations: iterations.parse().ok()?, salt_base64: salt.into(), stored_key: base64_decode_array(stored_key)?.into(), server_key: base64_decode_array(server_key)?.into(), doomed: false, }; Some(secret) } pub(crate) fn is_password_invalid(&self, client_key: &ScramKey) -> Choice { // constant time to not leak partial key match client_key.sha256().ct_ne(&self.stored_key) | Choice::from(self.doomed as u8) } /// To avoid revealing information to an attacker, we use a /// mocked server secret even if the user doesn't exist. /// See `auth-scram.c : mock_scram_secret` for details. pub(crate) fn mock(nonce: [u8; 32]) -> Self { Self { cached_at: Instant::now(), // this doesn't reveal much information as we're going to use // iteration count 1 for our generated passwords going forward. // PG16 users can set iteration count=1 already today. iterations: 1, salt_base64: BASE64_STANDARD.encode(nonce).into_boxed_str(), stored_key: ScramKey::default(), server_key: ScramKey::default(), doomed: true, } } /// Build a new server secret from the prerequisites. /// XXX: We only use this function in tests. #[cfg(test)] pub(crate) async fn build(password: &str) -> Option<Self> { Self::parse(&postgres_protocol::password::scram_sha_256(password.as_bytes()).await) } } #[cfg(test)] mod tests { use super::*; #[test] fn parse_scram_secret() { let iterations = 4096; let salt = "+/tQQax7twvwTj64mjBsxQ=="; let stored_key = "D5h6KTMBlUvDJk2Y8ELfC1Sjtc6k9YHjRyuRZyBNJns="; let server_key = "Pi3QHbcluX//NDfVkKlFl88GGzlJ5LkyPwcdlN/QBvI="; let secret = format!("SCRAM-SHA-256${iterations}:{salt}${stored_key}:{server_key}"); let parsed = ServerSecret::parse(&secret).unwrap(); assert_eq!(parsed.iterations, iterations); assert_eq!(&*parsed.salt_base64, salt); assert_eq!(BASE64_STANDARD.encode(parsed.stored_key), stored_key); assert_eq!(BASE64_STANDARD.encode(parsed.server_key), server_key); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/mod.rs
proxy/src/scram/mod.rs
//! Salted Challenge Response Authentication Mechanism. //! //! RFC: <https://datatracker.ietf.org/doc/html/rfc5802>. //! //! Reference implementation: //! * <https://github.com/postgres/postgres/blob/94226d4506e66d6e7cbf4b391f1e7393c1962841/src/backend/libpq/auth-scram.c> //! * <https://github.com/postgres/postgres/blob/94226d4506e66d6e7cbf4b391f1e7393c1962841/src/interfaces/libpq/fe-auth-scram.c> mod cache; mod countmin; mod exchange; mod key; mod messages; mod pbkdf2; mod secret; mod signature; pub mod threadpool; use base64::Engine as _; use base64::prelude::BASE64_STANDARD; pub(crate) use exchange::{Exchange, exchange}; pub(crate) use key::ScramKey; pub(crate) use secret::ServerSecret; const SCRAM_SHA_256: &str = "SCRAM-SHA-256"; const SCRAM_SHA_256_PLUS: &str = "SCRAM-SHA-256-PLUS"; /// A list of supported SCRAM methods. pub(crate) const METHODS: &[&str] = &[SCRAM_SHA_256_PLUS, SCRAM_SHA_256]; pub(crate) const METHODS_WITHOUT_PLUS: &[&str] = &[SCRAM_SHA_256]; /// Decode base64 into array without any heap allocations fn base64_decode_array<const N: usize>(input: impl AsRef<[u8]>) -> Option<[u8; N]> { let mut bytes = [0u8; N]; let size = BASE64_STANDARD.decode_slice(input, &mut bytes).ok()?; if size != N { return None; } Some(bytes) } #[cfg(test)] mod tests { use super::threadpool::ThreadPool; use super::{Exchange, ServerSecret}; use crate::intern::{EndpointIdInt, RoleNameInt}; use crate::sasl::{Mechanism, Step}; use crate::types::{EndpointId, RoleName}; #[test] fn snapshot() { let iterations = 4096; let salt = "QSXCR+Q6sek8bf92"; let stored_key = "FO+9jBb3MUukt6jJnzjPZOWc5ow/Pu6JtPyju0aqaE8="; let server_key = "qxJ1SbmSAi5EcS0J5Ck/cKAm/+Ixa+Kwp63f4OHDgzo="; let secret = format!("SCRAM-SHA-256${iterations}:{salt}${stored_key}:{server_key}",); let secret = ServerSecret::parse(&secret).unwrap(); const NONCE: [u8; 18] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, ]; let mut exchange = Exchange::new(&secret, || NONCE, crate::tls::TlsServerEndPoint::Undefined); let client_first = "n,,n=user,r=rOprNGfwEbeRWgbNEkqO"; let client_final = "c=biws,r=rOprNGfwEbeRWgbNEkqOAQIDBAUGBwgJCgsMDQ4PEBES,p=rw1r5Kph5ThxmaUBC2GAQ6MfXbPnNkFiTIvdb/Rear0="; let server_first = "r=rOprNGfwEbeRWgbNEkqOAQIDBAUGBwgJCgsMDQ4PEBES,s=QSXCR+Q6sek8bf92,i=4096"; let server_final = "v=qtUDIofVnIhM7tKn93EQUUt5vgMOldcDVu1HC+OH0o0="; exchange = match exchange.exchange(client_first).unwrap() { Step::Continue(exchange, message) => { assert_eq!(message, server_first); exchange } Step::Success(_, _) => panic!("expected continue, got success"), Step::Failure(f) => panic!("{f}"), }; let key = match exchange.exchange(client_final).unwrap() { Step::Success(key, message) => { assert_eq!(message, server_final); key } Step::Continue(_, _) => panic!("expected success, got continue"), Step::Failure(f) => panic!("{f}"), }; assert_eq!( key.as_bytes(), [ 74, 103, 1, 132, 12, 31, 200, 48, 28, 54, 82, 232, 207, 12, 138, 189, 40, 32, 134, 27, 125, 170, 232, 35, 171, 167, 166, 41, 70, 228, 182, 112, ] ); } async fn check( pool: &ThreadPool, scram_secret: &ServerSecret, password: &[u8], ) -> Result<(), &'static str> { let ep = EndpointId::from("foo"); let ep = EndpointIdInt::from(ep); let role = RoleName::from("user"); let role = RoleNameInt::from(&role); let outcome = super::exchange(pool, ep, role, scram_secret, password) .await .unwrap(); match outcome { crate::sasl::Outcome::Success(_) => Ok(()), crate::sasl::Outcome::Failure(r) => Err(r), } } async fn run_round_trip_test(server_password: &str, client_password: &str) { let pool = ThreadPool::new(1); let scram_secret = ServerSecret::build(server_password).await.unwrap(); check(&pool, &scram_secret, client_password.as_bytes()) .await .unwrap(); } #[tokio::test] async fn round_trip() { run_round_trip_test("pencil", "pencil").await; } #[tokio::test] #[should_panic(expected = "password doesn't match")] async fn failure() { run_round_trip_test("pencil", "eraser").await; } #[tokio::test] #[tracing_test::traced_test] async fn password_cache() { let pool = ThreadPool::new(1); let scram_secret = ServerSecret::build("password").await.unwrap(); // wrong passwords are not added to cache check(&pool, &scram_secret, b"wrong").await.unwrap_err(); assert!(!logs_contain("storing cached password")); // correct passwords get cached check(&pool, &scram_secret, b"password").await.unwrap(); assert!(logs_contain("storing cached password")); // wrong passwords do not match the cache check(&pool, &scram_secret, b"wrong").await.unwrap_err(); assert!(!logs_contain("password validated from cache")); // correct passwords match the cache check(&pool, &scram_secret, b"password").await.unwrap(); assert!(logs_contain("password validated from cache")); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/pbkdf2.rs
proxy/src/scram/pbkdf2.rs
//! For postgres password authentication, we need to perform a PBKDF2 using //! PRF=HMAC-SHA2-256, producing only 1 block (32 bytes) of output key. use hmac::Mac as _; use hmac::digest::consts::U32; use hmac::digest::generic_array::GenericArray; use zeroize::Zeroize as _; use crate::metrics::Metrics; /// The Psuedo-random function used during PBKDF2 and the SCRAM-SHA-256 handshake. pub type Prf = hmac::Hmac<sha2::Sha256>; pub(crate) type Block = GenericArray<u8, U32>; pub(crate) struct Pbkdf2 { hmac: Prf, /// U{r-1} for whatever iteration r we are currently on. prev: Block, /// the output of `fold(xor, U{1}..U{r})` for whatever iteration r we are currently on. hi: Block, /// number of iterations left iterations: u32, } impl Drop for Pbkdf2 { fn drop(&mut self) { self.prev.zeroize(); self.hi.zeroize(); } } // inspired from <https://github.com/neondatabase/rust-postgres/blob/20031d7a9ee1addeae6e0968e3899ae6bf01cee2/postgres-protocol/src/authentication/sasl.rs#L36-L61> impl Pbkdf2 { pub(crate) fn start(pw: &[u8], salt: &[u8], iterations: u32) -> Self { // key the HMAC and derive the first block in-place let mut hmac = Prf::new_from_slice(pw).expect("HMAC is able to accept all key sizes"); // U1 = PRF(Password, Salt + INT_32_BE(i)) // i = 1 since we only need 1 block of output. hmac.update(salt); hmac.update(&1u32.to_be_bytes()); let init_block = hmac.finalize_reset().into_bytes(); // Prf::new_from_slice will run 2 sha256 rounds. // Our update + finalize run 2 sha256 rounds for each pbkdf2 round. Metrics::get().proxy.sha_rounds.inc_by(4); Self { hmac, // one iteration spent above iterations: iterations - 1, hi: init_block, prev: init_block, } } pub(crate) fn cost(&self) -> u32 { (self.iterations).clamp(0, 4096) } /// For "fairness", we implement PBKDF2 with cooperative yielding, which is why we use this `turn` /// function that only executes a fixed number of iterations before continuing. /// /// Task must be rescheuled if this returns [`std::task::Poll::Pending`]. pub(crate) fn turn(&mut self) -> std::task::Poll<Block> { let Self { hmac, prev, hi, iterations, } = self; // only do up to 4096 iterations per turn for fairness let n = (*iterations).clamp(0, 4096); for _ in 0..n { let next = single_round(hmac, prev); xor_assign(hi, &next); *prev = next; } // Our update + finalize run 2 sha256 rounds for each pbkdf2 round. Metrics::get().proxy.sha_rounds.inc_by(2 * n as u64); *iterations -= n; if *iterations == 0 { std::task::Poll::Ready(*hi) } else { std::task::Poll::Pending } } } #[inline(always)] pub fn xor_assign(x: &mut Block, y: &Block) { for (x, &y) in std::iter::zip(x, y) { *x ^= y; } } #[inline(always)] fn single_round(prf: &mut Prf, ui: &Block) -> Block { // Ui = PRF(Password, Ui-1) prf.update(ui); prf.finalize_reset().into_bytes() } #[cfg(test)] mod tests { use pbkdf2::pbkdf2_hmac_array; use sha2::Sha256; use super::Pbkdf2; #[test] fn works() { let salt = b"sodium chloride"; let pass = b"Ne0n_!5_50_C007"; let mut job = Pbkdf2::start(pass, salt, 60000); let hash: [u8; 32] = loop { let std::task::Poll::Ready(hash) = job.turn() else { continue; }; break hash.into(); }; let expected = pbkdf2_hmac_array::<Sha256, 32>(pass, salt, 60000); assert_eq!(hash, expected); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/scram/cache.rs
proxy/src/scram/cache.rs
use tokio::time::Instant; use zeroize::Zeroize as _; use super::pbkdf2; use crate::cache::Cached; use crate::cache::common::{Cache, count_cache_insert, count_cache_outcome, eviction_listener}; use crate::intern::{EndpointIdInt, RoleNameInt}; use crate::metrics::{CacheKind, Metrics}; pub(crate) struct Pbkdf2Cache(moka::sync::Cache<(EndpointIdInt, RoleNameInt), Pbkdf2CacheEntry>); pub(crate) type CachedPbkdf2<'a> = Cached<&'a Pbkdf2Cache>; impl Cache for Pbkdf2Cache { type Key = (EndpointIdInt, RoleNameInt); type Value = Pbkdf2CacheEntry; fn invalidate(&self, info: &(EndpointIdInt, RoleNameInt)) { self.0.invalidate(info); } } /// To speed up password hashing for more active customers, we store the tail results of the /// PBKDF2 algorithm. If the output of PBKDF2 is U1 ^ U2 ^ ⋯ ^ Uc, then we store /// suffix = U17 ^ U18 ^ ⋯ ^ Uc. We only need to calculate U1 ^ U2 ^ ⋯ ^ U15 ^ U16 /// to determine the final result. /// /// The suffix alone isn't enough to crack the password. The stored_key is still required. /// While both are cached in memory, given they're in different locations is makes it much /// harder to exploit, even if any such memory exploit exists in proxy. #[derive(Clone)] pub struct Pbkdf2CacheEntry { /// corresponds to [`super::ServerSecret::cached_at`] pub(super) cached_from: Instant, pub(super) suffix: pbkdf2::Block, } impl Drop for Pbkdf2CacheEntry { fn drop(&mut self) { self.suffix.zeroize(); } } impl Pbkdf2Cache { pub fn new() -> Self { const SIZE: u64 = 100; const TTL: std::time::Duration = std::time::Duration::from_secs(60); let builder = moka::sync::Cache::builder() .name("pbkdf2") .max_capacity(SIZE) // We use time_to_live so we don't refresh the lifetime for an invalid password attempt. .time_to_live(TTL); Metrics::get() .cache .capacity .set(CacheKind::Pbkdf2, SIZE as i64); let builder = builder.eviction_listener(|_k, _v, cause| eviction_listener(CacheKind::Pbkdf2, cause)); Self(builder.build()) } pub fn insert(&self, endpoint: EndpointIdInt, role: RoleNameInt, value: Pbkdf2CacheEntry) { count_cache_insert(CacheKind::Pbkdf2); self.0.insert((endpoint, role), value); } fn get(&self, endpoint: EndpointIdInt, role: RoleNameInt) -> Option<Pbkdf2CacheEntry> { count_cache_outcome(CacheKind::Pbkdf2, self.0.get(&(endpoint, role))) } pub fn get_entry( &self, endpoint: EndpointIdInt, role: RoleNameInt, ) -> Option<CachedPbkdf2<'_>> { self.get(endpoint, role).map(|value| Cached { token: Some((self, (endpoint, role))), value, }) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/context/mod.rs
proxy/src/context/mod.rs
//! Connection request monitoring contexts use std::net::IpAddr; use chrono::Utc; use once_cell::sync::OnceCell; use smol_str::SmolStr; use tokio::sync::mpsc; use tracing::field::display; use tracing::{Span, error, info_span}; use try_lock::TryLock; use uuid::Uuid; use self::parquet::RequestData; use crate::control_plane::messages::{ColdStartInfo, MetricsAuxInfo}; use crate::error::ErrorKind; use crate::intern::{BranchIdInt, ProjectIdInt}; use crate::metrics::{LatencyAccumulated, LatencyTimer, Metrics, Protocol, Waiting}; use crate::pqproto::StartupMessageParams; use crate::protocol2::{ConnectionInfo, ConnectionInfoExtra}; use crate::types::{DbName, EndpointId, RoleName}; pub mod parquet; pub(crate) static LOG_CHAN: OnceCell<mpsc::WeakUnboundedSender<RequestData>> = OnceCell::new(); pub(crate) static LOG_CHAN_DISCONNECT: OnceCell<mpsc::WeakUnboundedSender<RequestData>> = OnceCell::new(); /// Context data for a single request to connect to a database. /// /// This data should **not** be used for connection logic, only for observability and limiting purposes. /// All connection logic should instead use strongly typed state machines, not a bunch of Options. pub struct RequestContext( /// To allow easier use of the ctx object, we have interior mutability. /// I would typically use a RefCell but that would break the `Send` requirements /// so we need something with thread-safety. `TryLock` is a cheap alternative /// that offers similar semantics to a `RefCell` but with synchronisation. TryLock<RequestContextInner>, ); struct RequestContextInner { pub(crate) conn_info: ConnectionInfo, pub(crate) session_id: Uuid, pub(crate) protocol: Protocol, first_packet: chrono::DateTime<Utc>, pub(crate) span: Span, // filled in as they are discovered project: Option<ProjectIdInt>, branch: Option<BranchIdInt>, endpoint_id: Option<EndpointId>, dbname: Option<DbName>, user: Option<RoleName>, application: Option<SmolStr>, user_agent: Option<SmolStr>, error_kind: Option<ErrorKind>, pub(crate) auth_method: Option<AuthMethod>, jwt_issuer: Option<String>, success: bool, pub(crate) cold_start_info: ColdStartInfo, pg_options: Option<StartupMessageParams>, testodrome_query_id: Option<SmolStr>, // extra // This sender is here to keep the request monitoring channel open while requests are taking place. sender: Option<mpsc::UnboundedSender<RequestData>>, // This sender is only used to log the length of session in case of success. disconnect_sender: Option<mpsc::UnboundedSender<RequestData>>, pub(crate) latency_timer: LatencyTimer, disconnect_timestamp: Option<chrono::DateTime<Utc>>, } #[derive(Clone, Debug)] pub(crate) enum AuthMethod { // aka link ConsoleRedirect, ScramSha256, ScramSha256Plus, Cleartext, Jwt, } impl Clone for RequestContext { fn clone(&self) -> Self { let inner = self.0.try_lock().expect("should not deadlock"); let new = RequestContextInner { conn_info: inner.conn_info.clone(), session_id: inner.session_id, protocol: inner.protocol, first_packet: inner.first_packet, span: info_span!("background_task"), project: inner.project, branch: inner.branch, endpoint_id: inner.endpoint_id.clone(), dbname: inner.dbname.clone(), user: inner.user.clone(), application: inner.application.clone(), user_agent: inner.user_agent.clone(), error_kind: inner.error_kind, auth_method: inner.auth_method.clone(), jwt_issuer: inner.jwt_issuer.clone(), success: inner.success, cold_start_info: inner.cold_start_info, pg_options: inner.pg_options.clone(), testodrome_query_id: inner.testodrome_query_id.clone(), sender: None, disconnect_sender: None, latency_timer: LatencyTimer::noop(inner.protocol), disconnect_timestamp: inner.disconnect_timestamp, }; Self(TryLock::new(new)) } } impl RequestContext { pub fn new(session_id: Uuid, conn_info: ConnectionInfo, protocol: Protocol) -> Self { // TODO: be careful with long lived spans let span = info_span!( "connect_request", %protocol, ?session_id, %conn_info, ep = tracing::field::Empty, role = tracing::field::Empty, ); let inner = RequestContextInner { conn_info, session_id, protocol, first_packet: Utc::now(), span, project: None, branch: None, endpoint_id: None, dbname: None, user: None, application: None, user_agent: None, error_kind: None, auth_method: None, jwt_issuer: None, success: false, cold_start_info: ColdStartInfo::Unknown, pg_options: None, testodrome_query_id: None, sender: LOG_CHAN.get().and_then(|tx| tx.upgrade()), disconnect_sender: LOG_CHAN_DISCONNECT.get().and_then(|tx| tx.upgrade()), latency_timer: LatencyTimer::new(protocol), disconnect_timestamp: None, }; Self(TryLock::new(inner)) } #[cfg(test)] pub(crate) fn test() -> Self { use std::net::SocketAddr; let ip = IpAddr::from([127, 0, 0, 1]); let addr = SocketAddr::new(ip, 5432); let conn_info = ConnectionInfo { addr, extra: None }; RequestContext::new(Uuid::now_v7(), conn_info, Protocol::Tcp) } pub(crate) fn console_application_name(&self) -> String { let this = self.0.try_lock().expect("should not deadlock"); format!( "{}/{}", this.application.as_deref().unwrap_or_default(), this.protocol ) } pub(crate) fn set_cold_start_info(&self, info: ColdStartInfo) { self.0 .try_lock() .expect("should not deadlock") .set_cold_start_info(info); } pub(crate) fn set_db_options(&self, options: StartupMessageParams) { let mut this = self.0.try_lock().expect("should not deadlock"); this.set_application(options.get("application_name").map(SmolStr::from)); if let Some(user) = options.get("user") { this.set_user(user.into()); } if let Some(dbname) = options.get("database") { this.set_dbname(dbname.into()); } // Try to get testodrome_query_id directly from parameters if let Some(options_str) = options.get("options") { // If not found directly, try to extract it from the options string for option in options_str.split_whitespace() { if let Some(value) = option.strip_prefix("neon_query_id:") { this.set_testodrome_id(value.into()); break; } } } this.pg_options = Some(options); } pub(crate) fn set_project(&self, x: MetricsAuxInfo) { let mut this = self.0.try_lock().expect("should not deadlock"); if this.endpoint_id.is_none() { this.set_endpoint_id(x.endpoint_id.as_str().into()); } this.branch = Some(x.branch_id); this.project = Some(x.project_id); this.set_cold_start_info(x.cold_start_info); } pub(crate) fn set_project_id(&self, project_id: ProjectIdInt) { let mut this = self.0.try_lock().expect("should not deadlock"); this.project = Some(project_id); } pub(crate) fn set_endpoint_id(&self, endpoint_id: EndpointId) { self.0 .try_lock() .expect("should not deadlock") .set_endpoint_id(endpoint_id); } pub(crate) fn set_dbname(&self, dbname: DbName) { self.0 .try_lock() .expect("should not deadlock") .set_dbname(dbname); } pub(crate) fn set_user(&self, user: RoleName) { self.0 .try_lock() .expect("should not deadlock") .set_user(user); } pub(crate) fn set_user_agent(&self, user_agent: Option<SmolStr>) { self.0 .try_lock() .expect("should not deadlock") .set_user_agent(user_agent); } pub(crate) fn set_testodrome_id(&self, query_id: SmolStr) { self.0 .try_lock() .expect("should not deadlock") .set_testodrome_id(query_id); } pub(crate) fn set_auth_method(&self, auth_method: AuthMethod) { let mut this = self.0.try_lock().expect("should not deadlock"); this.auth_method = Some(auth_method); } pub(crate) fn set_jwt_issuer(&self, jwt_issuer: String) { let mut this = self.0.try_lock().expect("should not deadlock"); this.jwt_issuer = Some(jwt_issuer); } pub fn has_private_peer_addr(&self) -> bool { self.0 .try_lock() .expect("should not deadlock") .has_private_peer_addr() } pub(crate) fn set_error_kind(&self, kind: ErrorKind) { let mut this = self.0.try_lock().expect("should not deadlock"); // Do not record errors from the private address to metrics. if !this.has_private_peer_addr() { Metrics::get().proxy.errors_total.inc(kind); } if let Some(ep) = &this.endpoint_id { let metric = &Metrics::get().proxy.endpoints_affected_by_errors; let label = metric.with_labels(kind); metric.get_metric(label).measure(ep); } this.error_kind = Some(kind); } pub fn set_success(&self) { let mut this = self.0.try_lock().expect("should not deadlock"); this.success = true; } pub fn log_connect(self) -> DisconnectLogger { let mut this = self.0.into_inner(); this.log_connect(); // close current span. this.span = Span::none(); DisconnectLogger(this) } pub(crate) fn protocol(&self) -> Protocol { self.0.try_lock().expect("should not deadlock").protocol } pub(crate) fn span(&self) -> Span { self.0.try_lock().expect("should not deadlock").span.clone() } pub(crate) fn session_id(&self) -> Uuid { self.0.try_lock().expect("should not deadlock").session_id } pub(crate) fn peer_addr(&self) -> IpAddr { self.0 .try_lock() .expect("should not deadlock") .conn_info .addr .ip() } pub(crate) fn extra(&self) -> Option<ConnectionInfoExtra> { self.0 .try_lock() .expect("should not deadlock") .conn_info .extra .clone() } pub(crate) fn cold_start_info(&self) -> ColdStartInfo { self.0 .try_lock() .expect("should not deadlock") .cold_start_info } pub(crate) fn latency_timer_pause(&self, waiting_for: Waiting) -> LatencyTimerPause<'_> { LatencyTimerPause { ctx: self, start: tokio::time::Instant::now(), waiting_for, } } pub(crate) fn latency_timer_pause_at( &self, at: tokio::time::Instant, waiting_for: Waiting, ) -> LatencyTimerPause<'_> { LatencyTimerPause { ctx: self, start: at, waiting_for, } } pub(crate) fn get_proxy_latency(&self) -> LatencyAccumulated { self.0 .try_lock() .expect("should not deadlock") .latency_timer .accumulated() } pub(crate) fn get_testodrome_id(&self) -> Option<SmolStr> { self.0 .try_lock() .expect("should not deadlock") .testodrome_query_id .clone() } pub(crate) fn success(&self) { self.0 .try_lock() .expect("should not deadlock") .latency_timer .success(); } } pub(crate) struct LatencyTimerPause<'a> { ctx: &'a RequestContext, start: tokio::time::Instant, waiting_for: Waiting, } impl Drop for LatencyTimerPause<'_> { fn drop(&mut self) { self.ctx .0 .try_lock() .expect("should not deadlock") .latency_timer .unpause(self.start, self.waiting_for); } } impl RequestContextInner { fn set_cold_start_info(&mut self, info: ColdStartInfo) { self.cold_start_info = info; self.latency_timer.cold_start_info(info); } fn set_endpoint_id(&mut self, endpoint_id: EndpointId) { if self.endpoint_id.is_none() { self.span.record("ep", display(&endpoint_id)); let metric = &Metrics::get().proxy.connecting_endpoints; let label = metric.with_labels(self.protocol); metric.get_metric(label).measure(&endpoint_id); self.endpoint_id = Some(endpoint_id); } } fn set_application(&mut self, app: Option<SmolStr>) { if let Some(app) = app { self.application = Some(app); } } fn set_user_agent(&mut self, user_agent: Option<SmolStr>) { self.user_agent = user_agent; } fn set_dbname(&mut self, dbname: DbName) { self.dbname = Some(dbname); } fn set_user(&mut self, user: RoleName) { self.span.record("role", display(&user)); self.user = Some(user); } fn set_testodrome_id(&mut self, query_id: SmolStr) { self.testodrome_query_id = Some(query_id); } fn has_private_peer_addr(&self) -> bool { match self.conn_info.addr.ip() { IpAddr::V4(ip) => ip.is_private(), IpAddr::V6(_) => false, } } fn log_connect(&mut self) { if let Some(tx) = self.sender.take() { // If type changes, this error handling needs to be updated. let tx: mpsc::UnboundedSender<RequestData> = tx; if let Err(e) = tx.send(RequestData::from(&*self)) { error!("log_connect channel send failed: {e}"); } } } fn log_disconnect(&mut self) { // If we are here, it's guaranteed that the user successfully connected to the endpoint. // Here we log the length of the session. self.disconnect_timestamp = Some(Utc::now()); if let Some(tx) = self.disconnect_sender.take() { // If type changes, this error handling needs to be updated. let tx: mpsc::UnboundedSender<RequestData> = tx; if let Err(e) = tx.send(RequestData::from(&*self)) { error!("log_disconnect channel send failed: {e}"); } } } } impl Drop for RequestContextInner { fn drop(&mut self) { if self.sender.is_some() { self.log_connect(); } } } pub struct DisconnectLogger(RequestContextInner); impl Drop for DisconnectLogger { fn drop(&mut self) { self.0.log_disconnect(); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/context/parquet.rs
proxy/src/context/parquet.rs
use std::sync::Arc; use std::time::SystemTime; use anyhow::Context; use bytes::buf::Writer; use bytes::{BufMut, BytesMut}; use chrono::{Datelike, Timelike}; use futures::{Stream, StreamExt}; use parquet::basic::Compression; use parquet::file::metadata::RowGroupMetaDataPtr; use parquet::file::properties::{DEFAULT_PAGE_SIZE, WriterProperties, WriterPropertiesPtr}; use parquet::file::writer::SerializedFileWriter; use parquet::record::RecordWriter; use remote_storage::{GenericRemoteStorage, RemotePath, RemoteStorageConfig, TimeoutOrCancel}; use serde::ser::SerializeMap; use tokio::sync::mpsc; use tokio::time; use tokio_util::sync::CancellationToken; use tracing::{Span, debug, info}; use utils::backoff; use super::{LOG_CHAN, RequestContextInner}; use crate::config::remote_storage_from_toml; use crate::context::LOG_CHAN_DISCONNECT; use crate::ext::TaskExt; use crate::pqproto::StartupMessageParams; #[derive(clap::Args, Clone, Debug)] pub struct ParquetUploadArgs { /// Storage location to upload the parquet files to. /// Encoded as toml (same format as pageservers), eg /// `{bucket_name='the-bucket',bucket_region='us-east-1',prefix_in_bucket='proxy',endpoint='http://minio:9000'}` #[clap(long, value_parser = remote_storage_from_toml)] parquet_upload_remote_storage: Option<RemoteStorageConfig>, #[clap(long, value_parser = remote_storage_from_toml)] parquet_upload_disconnect_events_remote_storage: Option<RemoteStorageConfig>, /// How many rows to include in a row group #[clap(long, default_value_t = 8192)] parquet_upload_row_group_size: usize, /// How large each column page should be in bytes #[clap(long, default_value_t = DEFAULT_PAGE_SIZE)] parquet_upload_page_size: usize, /// How large the total parquet file should be in bytes #[clap(long, default_value_t = 100_000_000)] parquet_upload_size: i64, /// How long to wait before forcing a file upload #[clap(long, default_value = "20m", value_parser = humantime::parse_duration)] parquet_upload_maximum_duration: tokio::time::Duration, /// What level of compression to use #[clap(long, default_value_t = Compression::UNCOMPRESSED)] parquet_upload_compression: Compression, } // Occasional network issues and such can cause remote operations to fail, and // that's expected. If a upload fails, we log it at info-level, and retry. // But after FAILED_UPLOAD_WARN_THRESHOLD retries, we start to log it at WARN // level instead, as repeated failures can mean a more serious problem. If it // fails more than FAILED_UPLOAD_RETRIES times, we give up pub(crate) const FAILED_UPLOAD_WARN_THRESHOLD: u32 = 3; pub(crate) const FAILED_UPLOAD_MAX_RETRIES: u32 = 10; // the parquet crate leaves a lot to be desired... // what follows is an attempt to write parquet files with minimal allocs. // complication: parquet is a columnar format, while we want to write in as rows. // design: // * we batch up to 1024 rows, then flush them into a 'row group' // * after each rowgroup write, we check the length of the file and upload to s3 if large enough #[derive(parquet_derive::ParquetRecordWriter)] pub(crate) struct RequestData { region: String, protocol: &'static str, /// Must be UTC. The derive macro doesn't like the timezones timestamp: chrono::NaiveDateTime, session_id: uuid::Uuid, peer_addr: String, username: Option<String>, application_name: Option<String>, user_agent: Option<String>, endpoint_id: Option<String>, database: Option<String>, project: Option<String>, branch: Option<String>, pg_options: Option<String>, auth_method: Option<&'static str>, jwt_issuer: Option<String>, error: Option<&'static str>, /// Success is counted if we form a HTTP response with sql rows inside /// Or if we make it to proxy_pass success: bool, /// Indicates if the cplane started the new compute node for this request. cold_start_info: &'static str, /// Tracks time from session start (HTTP request/libpq TCP handshake) /// Through to success/failure duration_us: u64, /// If the session was successful after the disconnect, will be created one more event with filled `disconnect_timestamp`. disconnect_timestamp: Option<chrono::NaiveDateTime>, } struct Options<'a> { options: &'a StartupMessageParams, } impl serde::Serialize for Options<'_> { fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> where S: serde::Serializer, { let mut state = s.serialize_map(None)?; for (k, v) in self.options.iter() { state.serialize_entry(k, v)?; } state.end() } } impl From<&RequestContextInner> for RequestData { fn from(value: &RequestContextInner) -> Self { Self { session_id: value.session_id, peer_addr: value.conn_info.addr.ip().to_string(), timestamp: value.first_packet.naive_utc(), username: value.user.as_deref().map(String::from), application_name: value.application.as_deref().map(String::from), user_agent: value.user_agent.as_deref().map(String::from), endpoint_id: value.endpoint_id.as_deref().map(String::from), database: value.dbname.as_deref().map(String::from), project: value.project.as_deref().map(String::from), branch: value.branch.as_deref().map(String::from), pg_options: value .pg_options .as_ref() .and_then(|options| serde_json::to_string(&Options { options }).ok()), auth_method: value.auth_method.as_ref().map(|x| match x { super::AuthMethod::ConsoleRedirect => "console_redirect", super::AuthMethod::ScramSha256 => "scram_sha_256", super::AuthMethod::ScramSha256Plus => "scram_sha_256_plus", super::AuthMethod::Cleartext => "cleartext", super::AuthMethod::Jwt => "jwt", }), jwt_issuer: value.jwt_issuer.clone(), protocol: value.protocol.as_str(), region: String::new(), error: value.error_kind.as_ref().map(|e| e.to_metric_label()), success: value.success, cold_start_info: value.cold_start_info.as_str(), duration_us: SystemTime::from(value.first_packet) .elapsed() .unwrap_or_default() .as_micros() as u64, // 584 millenia... good enough disconnect_timestamp: value.disconnect_timestamp.map(|x| x.naive_utc()), } } } /// Parquet request context worker /// /// It listened on a channel for all completed requests, extracts the data and writes it into a parquet file, /// then uploads a completed batch to S3 pub async fn worker( cancellation_token: CancellationToken, config: ParquetUploadArgs, region: String, ) -> anyhow::Result<()> { let Some(remote_storage_config) = config.parquet_upload_remote_storage else { tracing::warn!("parquet request upload: no s3 bucket configured"); return Ok(()); }; let (tx, mut rx) = mpsc::unbounded_channel(); LOG_CHAN .set(tx.downgrade()) .expect("only one worker should set the channel"); // setup row stream that will close on cancellation let cancellation_token2 = cancellation_token.clone(); tokio::spawn(async move { cancellation_token2.cancelled().await; // dropping this sender will cause the channel to close only once // all the remaining inflight requests have been completed. drop(tx); }); let rx = futures::stream::poll_fn(move |cx| rx.poll_recv(cx)); let rx = rx.map(RequestData::from); let storage = GenericRemoteStorage::from_config(&remote_storage_config) .await .context("remote storage init")?; let properties = WriterProperties::builder() .set_data_page_size_limit(config.parquet_upload_page_size) .set_compression(config.parquet_upload_compression); let parquet_config = ParquetConfig { propeties: Arc::new(properties.build()), rows_per_group: config.parquet_upload_row_group_size, file_size: config.parquet_upload_size, max_duration: config.parquet_upload_maximum_duration, #[cfg(any(test, feature = "testing"))] test_remote_failures: 0, }; // TODO(anna): consider moving this to a separate function. if let Some(disconnect_events_storage_config) = config.parquet_upload_disconnect_events_remote_storage { let (tx_disconnect, mut rx_disconnect) = mpsc::unbounded_channel(); LOG_CHAN_DISCONNECT .set(tx_disconnect.downgrade()) .expect("only one worker should set the channel"); // setup row stream that will close on cancellation tokio::spawn(async move { cancellation_token.cancelled().await; // dropping this sender will cause the channel to close only once // all the remaining inflight requests have been completed. drop(tx_disconnect); }); let rx_disconnect = futures::stream::poll_fn(move |cx| rx_disconnect.poll_recv(cx)); let rx_disconnect = rx_disconnect.map(RequestData::from); let storage_disconnect = GenericRemoteStorage::from_config(&disconnect_events_storage_config) .await .context("remote storage for disconnect events init")?; let parquet_config_disconnect = parquet_config.clone(); tokio::try_join!( worker_inner(storage, rx, parquet_config, &region), worker_inner( storage_disconnect, rx_disconnect, parquet_config_disconnect, &region ) ) .map(|_| ()) } else { worker_inner(storage, rx, parquet_config, &region).await } } #[derive(Clone, Debug)] struct ParquetConfig { propeties: WriterPropertiesPtr, rows_per_group: usize, file_size: i64, max_duration: tokio::time::Duration, #[cfg(any(test, feature = "testing"))] test_remote_failures: u64, } async fn worker_inner( storage: GenericRemoteStorage, rx: impl Stream<Item = RequestData>, config: ParquetConfig, region: &str, ) -> anyhow::Result<()> { #[cfg(any(test, feature = "testing"))] let storage = if config.test_remote_failures > 0 { GenericRemoteStorage::unreliable_wrapper(storage, config.test_remote_failures, 100) } else { storage }; let mut rx = std::pin::pin!(rx); let mut rows = Vec::with_capacity(config.rows_per_group); let schema = rows.as_slice().schema()?; let buffer = BytesMut::new(); let w = buffer.writer(); let mut w = SerializedFileWriter::new(w, schema.clone(), config.propeties.clone())?; let mut last_upload = time::Instant::now(); let mut len = 0; while let Some(mut row) = rx.next().await { region.clone_into(&mut row.region); rows.push(row); let force = last_upload.elapsed() > config.max_duration; if rows.len() == config.rows_per_group || force { let rg_meta; (rows, w, rg_meta) = flush_rows(rows, w).await?; len += rg_meta.compressed_size(); } if len > config.file_size || force { last_upload = time::Instant::now(); let file = upload_parquet(w, len, &storage).await?; w = SerializedFileWriter::new(file, schema.clone(), config.propeties.clone())?; len = 0; } } if !rows.is_empty() { let rg_meta; (_, w, rg_meta) = flush_rows(rows, w).await?; len += rg_meta.compressed_size(); } if !w.flushed_row_groups().is_empty() { let _rtchk: Writer<BytesMut> = upload_parquet(w, len, &storage).await?; } Ok(()) } async fn flush_rows<W>( rows: Vec<RequestData>, mut w: SerializedFileWriter<W>, ) -> anyhow::Result<( Vec<RequestData>, SerializedFileWriter<W>, RowGroupMetaDataPtr, )> where W: std::io::Write + Send + 'static, { let span = Span::current(); let (mut rows, w, rg_meta) = tokio::task::spawn_blocking(move || { let _enter = span.enter(); let mut rg = w.next_row_group()?; rows.as_slice().write_to_row_group(&mut rg)?; let rg_meta = rg.close()?; let size = rg_meta.compressed_size(); let compression = rg_meta.compressed_size() as f64 / rg_meta.total_byte_size() as f64; debug!(size, compression, "flushed row group to parquet file"); Ok::<_, parquet::errors::ParquetError>((rows, w, rg_meta)) }) .await .propagate_task_panic()?; rows.clear(); Ok((rows, w, rg_meta)) } async fn upload_parquet( mut w: SerializedFileWriter<Writer<BytesMut>>, len: i64, storage: &GenericRemoteStorage, ) -> anyhow::Result<Writer<BytesMut>> { let len_uncompressed = w .flushed_row_groups() .iter() .map(|rg| rg.total_byte_size()) .sum::<i64>(); // I don't know how compute intensive this is, although it probably isn't much... better be safe than sorry. // finish method only available on the fork: https://github.com/apache/arrow-rs/issues/5253 let (mut buffer, metadata) = tokio::task::spawn_blocking(move || -> parquet::errors::Result<_> { let metadata = w.finish()?; let buffer = std::mem::take(w.inner_mut().get_mut()); Ok((buffer, metadata)) }) .await .propagate_task_panic()?; let data = buffer.split().freeze(); let compression = len as f64 / len_uncompressed as f64; let size = data.len(); let now = chrono::Utc::now(); let id = uuid::Uuid::new_v7(uuid::Timestamp::from_unix( uuid::NoContext, // we won't be running this in 1970. this cast is ok now.timestamp() as u64, now.timestamp_subsec_nanos(), )); info!( %id, rows = metadata.num_rows, size, compression, "uploading request parquet file" ); let year = now.year(); let month = now.month(); let day = now.day(); let hour = now.hour(); // segment files by time for S3 performance let path = RemotePath::from_string(&format!( "{year:04}/{month:02}/{day:02}/{hour:02}/requests_{id}.parquet" ))?; let cancel = CancellationToken::new(); let maybe_err = backoff::retry( || async { let stream = futures::stream::once(futures::future::ready(Ok(data.clone()))); storage .upload(stream, data.len(), &path, None, &cancel) .await }, TimeoutOrCancel::caused_by_cancel, FAILED_UPLOAD_WARN_THRESHOLD, FAILED_UPLOAD_MAX_RETRIES, "request_data_upload", // we don't want cancellation to interrupt here, so we make a dummy cancel token &cancel, ) .await .ok_or_else(|| anyhow::Error::new(TimeoutOrCancel::Cancel)) .and_then(|x| x) .with_context(|| format!("request_data_upload: path={path}")) .err(); if let Some(err) = maybe_err { tracing::error!(%id, %path, error = ?err, "failed to upload request data"); } Ok(buffer.writer()) } #[cfg(test)] mod tests { use std::net::Ipv4Addr; use std::num::NonZeroUsize; use std::sync::Arc; use camino::Utf8Path; use clap::Parser; use futures::{Stream, StreamExt}; use itertools::Itertools; use parquet::basic::{Compression, ZstdLevel}; use parquet::file::properties::{DEFAULT_PAGE_SIZE, WriterProperties}; use parquet::file::reader::FileReader; use parquet::file::serialized_reader::SerializedFileReader; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use remote_storage::{ DEFAULT_MAX_KEYS_PER_LIST_RESPONSE, DEFAULT_REMOTE_STORAGE_S3_CONCURRENCY_LIMIT, GenericRemoteStorage, RemoteStorageConfig, RemoteStorageKind, S3Config, }; use tokio::sync::mpsc; use tokio::time; use walkdir::WalkDir; use super::{ParquetConfig, ParquetUploadArgs, RequestData, worker_inner}; #[derive(Parser)] struct ProxyCliArgs { #[clap(flatten)] parquet_upload: ParquetUploadArgs, } #[test] fn default_parser() { let ProxyCliArgs { parquet_upload } = ProxyCliArgs::parse_from(["proxy"]); assert_eq!(parquet_upload.parquet_upload_remote_storage, None); assert_eq!(parquet_upload.parquet_upload_row_group_size, 8192); assert_eq!(parquet_upload.parquet_upload_page_size, DEFAULT_PAGE_SIZE); assert_eq!(parquet_upload.parquet_upload_size, 100_000_000); assert_eq!( parquet_upload.parquet_upload_maximum_duration, time::Duration::from_secs(20 * 60) ); assert_eq!( parquet_upload.parquet_upload_compression, Compression::UNCOMPRESSED ); } #[test] fn full_parser() { let ProxyCliArgs { parquet_upload } = ProxyCliArgs::parse_from([ "proxy", "--parquet-upload-remote-storage", "{bucket_name='default',prefix_in_bucket='proxy/',bucket_region='us-east-1',endpoint='http://minio:9000'}", "--parquet-upload-row-group-size", "100", "--parquet-upload-page-size", "10000", "--parquet-upload-size", "10000000", "--parquet-upload-maximum-duration", "10m", "--parquet-upload-compression", "zstd(5)", ]); assert_eq!( parquet_upload.parquet_upload_remote_storage, Some(RemoteStorageConfig { storage: RemoteStorageKind::AwsS3(S3Config { bucket_name: "default".into(), bucket_region: "us-east-1".into(), prefix_in_bucket: Some("proxy/".into()), endpoint: Some("http://minio:9000".into()), concurrency_limit: NonZeroUsize::new( DEFAULT_REMOTE_STORAGE_S3_CONCURRENCY_LIMIT ) .unwrap(), max_keys_per_list_response: DEFAULT_MAX_KEYS_PER_LIST_RESPONSE, upload_storage_class: None, }), timeout: RemoteStorageConfig::DEFAULT_TIMEOUT, small_timeout: RemoteStorageConfig::DEFAULT_SMALL_TIMEOUT, }) ); assert_eq!(parquet_upload.parquet_upload_row_group_size, 100); assert_eq!(parquet_upload.parquet_upload_page_size, 10000); assert_eq!(parquet_upload.parquet_upload_size, 10_000_000); assert_eq!( parquet_upload.parquet_upload_maximum_duration, time::Duration::from_secs(10 * 60) ); assert_eq!( parquet_upload.parquet_upload_compression, Compression::ZSTD(ZstdLevel::try_new(5).unwrap()) ); } fn generate_request_data(rng: &mut impl Rng) -> RequestData { RequestData { session_id: uuid::Builder::from_random_bytes(rng.random()).into_uuid(), peer_addr: Ipv4Addr::from(rng.random::<[u8; 4]>()).to_string(), timestamp: chrono::DateTime::from_timestamp_millis( rng.random_range(1703862754..1803862754), ) .unwrap() .naive_utc(), application_name: Some("test".to_owned()), user_agent: Some("test-user-agent".to_owned()), username: Some(hex::encode(rng.random::<[u8; 4]>())), endpoint_id: Some(hex::encode(rng.random::<[u8; 16]>())), database: Some(hex::encode(rng.random::<[u8; 16]>())), project: Some(hex::encode(rng.random::<[u8; 16]>())), branch: Some(hex::encode(rng.random::<[u8; 16]>())), pg_options: None, auth_method: None, jwt_issuer: None, protocol: ["tcp", "ws", "http"][rng.random_range(0..3)], region: String::new(), error: None, success: rng.random(), cold_start_info: "no", duration_us: rng.random_range(0..30_000_000), disconnect_timestamp: None, } } fn random_stream(len: usize) -> impl Stream<Item = RequestData> + Unpin { let mut rng = StdRng::from_seed([0x39; 32]); futures::stream::iter( std::iter::repeat_with(move || generate_request_data(&mut rng)).take(len), ) } async fn run_test( tmpdir: &Utf8Path, config: ParquetConfig, rx: impl Stream<Item = RequestData>, ) -> Vec<(u64, usize, i64)> { let remote_storage_config = RemoteStorageConfig { storage: RemoteStorageKind::LocalFs { local_path: tmpdir.to_path_buf(), }, timeout: std::time::Duration::from_secs(120), small_timeout: std::time::Duration::from_secs(30), }; let storage = GenericRemoteStorage::from_config(&remote_storage_config) .await .unwrap(); worker_inner(storage, rx, config, "us-east-1") .await .unwrap(); let mut files = WalkDir::new(tmpdir.as_std_path()) .into_iter() .filter_map(|entry| entry.ok()) .filter(|entry| entry.file_type().is_file()) .map(|entry| entry.path().to_path_buf()) .collect_vec(); files.sort(); files .into_iter() .map(|path| std::fs::File::open(tmpdir.as_std_path().join(path)).unwrap()) .map(|file| { ( file.metadata().unwrap(), SerializedFileReader::new(file).unwrap().metadata().clone(), ) }) .map(|(file_meta, parquet_meta)| { ( file_meta.len(), parquet_meta.num_row_groups(), parquet_meta.file_metadata().num_rows(), ) }) .collect() } #[tokio::test] async fn verify_parquet_no_compression() { let tmpdir = camino_tempfile::tempdir().unwrap(); let config = ParquetConfig { propeties: Arc::new(WriterProperties::new()), rows_per_group: 2_000, file_size: 1_000_000, max_duration: time::Duration::from_secs(20 * 60), test_remote_failures: 0, }; let rx = random_stream(50_000); let file_stats = run_test(tmpdir.path(), config, rx).await; assert_eq!( file_stats, [ (1313878, 3, 6000), (1313891, 3, 6000), (1314058, 3, 6000), (1313914, 3, 6000), (1313760, 3, 6000), (1314084, 3, 6000), (1313965, 3, 6000), (1313911, 3, 6000), (438290, 1, 2000) ] ); tmpdir.close().unwrap(); } #[tokio::test] async fn verify_parquet_strong_compression() { let tmpdir = camino_tempfile::tempdir().unwrap(); let config = ParquetConfig { propeties: Arc::new( WriterProperties::builder() .set_compression(parquet::basic::Compression::ZSTD( ZstdLevel::try_new(10).unwrap(), )) .build(), ), rows_per_group: 2_000, file_size: 1_000_000, max_duration: time::Duration::from_secs(20 * 60), test_remote_failures: 0, }; let rx = random_stream(50_000); let file_stats = run_test(tmpdir.path(), config, rx).await; // with strong compression, the files are smaller assert_eq!( file_stats, [ (1206039, 5, 10000), (1205798, 5, 10000), (1205776, 5, 10000), (1206051, 5, 10000), (1205746, 5, 10000) ] ); tmpdir.close().unwrap(); } #[tokio::test] async fn verify_parquet_unreliable_upload() { let tmpdir = camino_tempfile::tempdir().unwrap(); let config = ParquetConfig { propeties: Arc::new(WriterProperties::new()), rows_per_group: 2_000, file_size: 1_000_000, max_duration: time::Duration::from_secs(20 * 60), test_remote_failures: 2, }; let rx = random_stream(50_000); let file_stats = run_test(tmpdir.path(), config, rx).await; assert_eq!( file_stats, [ (1313878, 3, 6000), (1313891, 3, 6000), (1314058, 3, 6000), (1313914, 3, 6000), (1313760, 3, 6000), (1314084, 3, 6000), (1313965, 3, 6000), (1313911, 3, 6000), (438290, 1, 2000) ] ); tmpdir.close().unwrap(); } #[tokio::test(start_paused = true)] async fn verify_parquet_regular_upload() { let tmpdir = camino_tempfile::tempdir().unwrap(); let config = ParquetConfig { propeties: Arc::new(WriterProperties::new()), rows_per_group: 2_000, file_size: 1_000_000, max_duration: time::Duration::from_secs(60), test_remote_failures: 2, }; let (tx, mut rx) = mpsc::unbounded_channel(); tokio::spawn(async move { for _ in 0..3 { let mut s = random_stream(3000); while let Some(r) = s.next().await { tx.send(r).unwrap(); } time::sleep(time::Duration::from_secs(70)).await; } }); let rx = futures::stream::poll_fn(move |cx| rx.poll_recv(cx)); let file_stats = run_test(tmpdir.path(), config, rx).await; // files are smaller than the size threshold, but they took too long to fill so were flushed early assert_eq!( file_stats, [(658552, 2, 3001), (658265, 2, 3000), (658061, 2, 2999)] ); tmpdir.close().unwrap(); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/rate_limiter/limit_algorithm.rs
proxy/src/rate_limiter/limit_algorithm.rs
//! Algorithms for controlling concurrency limits. use std::pin::pin; use std::sync::Arc; use std::time::Duration; use parking_lot::Mutex; use tokio::sync::Notify; use tokio::time::Instant; use tokio::time::error::Elapsed; use self::aimd::Aimd; pub(crate) mod aimd; /// Whether a job succeeded or failed as a result of congestion/overload. /// /// Errors not considered to be caused by overload should be ignored. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum Outcome { /// The job succeeded, or failed in a way unrelated to overload. Success, /// The job failed because of overload, e.g. it timed out or an explicit backpressure signal /// was observed. Overload, } /// An algorithm for controlling a concurrency limit. pub(crate) trait LimitAlgorithm: Send + Sync + 'static { /// Update the concurrency limit in response to a new job completion. fn update(&self, old_limit: usize, sample: Sample) -> usize; } /// The result of a job (or jobs), including the [`Outcome`] (loss) and latency (delay). #[derive(Debug, Clone, PartialEq, Eq, Copy)] pub(crate) struct Sample { pub(crate) latency: Duration, /// Jobs in flight when the sample was taken. pub(crate) in_flight: usize, pub(crate) outcome: Outcome, } #[derive(Clone, Copy, Debug, Default, serde::Deserialize, PartialEq)] #[serde(rename_all = "snake_case")] pub(crate) enum RateLimitAlgorithm { #[default] Fixed, Aimd { #[serde(flatten)] conf: Aimd, }, } pub(crate) struct Fixed; impl LimitAlgorithm for Fixed { fn update(&self, old_limit: usize, _sample: Sample) -> usize { old_limit } } #[derive(Clone, Copy, Debug, serde::Deserialize, PartialEq)] pub struct RateLimiterConfig { #[serde(flatten)] pub(crate) algorithm: RateLimitAlgorithm, pub(crate) initial_limit: usize, } impl RateLimiterConfig { pub(crate) fn create_rate_limit_algorithm(self) -> Box<dyn LimitAlgorithm> { match self.algorithm { RateLimitAlgorithm::Fixed => Box::new(Fixed), RateLimitAlgorithm::Aimd { conf } => Box::new(conf), } } } pub(crate) struct LimiterInner { alg: Box<dyn LimitAlgorithm>, available: usize, limit: usize, in_flight: usize, } impl LimiterInner { fn update_limit(&mut self, latency: Duration, outcome: Option<Outcome>) { if let Some(outcome) = outcome { let sample = Sample { latency, in_flight: self.in_flight, outcome, }; self.limit = self.alg.update(self.limit, sample); } } fn take(&mut self, ready: &Notify) -> Option<()> { if self.available >= 1 { self.available -= 1; self.in_flight += 1; // tell the next in the queue that there is a permit ready if self.available >= 1 { ready.notify_one(); } Some(()) } else { None } } } /// Limits the number of concurrent jobs. /// /// Concurrency is limited through the use of [`Token`]s. Acquire a token to run a job, and release the /// token once the job is finished. /// /// The limit will be automatically adjusted based on observed latency (delay) and/or failures /// caused by overload (loss). pub(crate) struct DynamicLimiter { config: RateLimiterConfig, inner: Mutex<LimiterInner>, // to notify when a token is available ready: Notify, } /// A concurrency token, required to run a job. /// /// Release the token back to the [`DynamicLimiter`] after the job is complete. pub(crate) struct Token { start: Instant, limiter: Option<Arc<DynamicLimiter>>, } /// A snapshot of the state of the [`DynamicLimiter`]. /// /// Not guaranteed to be consistent under high concurrency. #[derive(Debug, Clone, Copy)] #[cfg(test)] struct LimiterState { limit: usize, } impl DynamicLimiter { /// Create a limiter with a given limit control algorithm. pub(crate) fn new(config: RateLimiterConfig) -> Arc<Self> { let ready = Notify::new(); ready.notify_one(); Arc::new(Self { inner: Mutex::new(LimiterInner { alg: config.create_rate_limit_algorithm(), available: config.initial_limit, limit: config.initial_limit, in_flight: 0, }), ready, config, }) } /// Try to acquire a concurrency [Token], waiting for `duration` if there are none available. pub(crate) async fn acquire_timeout( self: &Arc<Self>, duration: Duration, ) -> Result<Token, Elapsed> { tokio::time::timeout(duration, self.acquire()).await? } /// Try to acquire a concurrency [Token]. async fn acquire(self: &Arc<Self>) -> Result<Token, Elapsed> { if self.config.initial_limit == 0 { // If the rate limiter is disabled, we can always acquire a token. Ok(Token::disabled()) } else { let mut notified = pin!(self.ready.notified()); let mut ready = notified.as_mut().enable(); loop { if ready { let mut inner = self.inner.lock(); if inner.take(&self.ready).is_some() { break Ok(Token::new(self.clone())); } notified.set(self.ready.notified()); } notified.as_mut().await; ready = true; } } } /// Return the concurrency [Token], along with the outcome of the job. /// /// The [Outcome] of the job, and the time taken to perform it, may be used /// to update the concurrency limit. /// /// Set the outcome to `None` to ignore the job. fn release_inner(&self, start: Instant, outcome: Option<Outcome>) { if outcome.is_none() { tracing::warn!("outcome is {:?}", outcome); } else { tracing::debug!("outcome is {:?}", outcome); } if self.config.initial_limit == 0 { return; } let mut inner = self.inner.lock(); inner.update_limit(start.elapsed(), outcome); inner.in_flight -= 1; if inner.in_flight < inner.limit { inner.available = inner.limit - inner.in_flight; // At least 1 permit is now available self.ready.notify_one(); } } /// The current state of the limiter. #[cfg(test)] fn state(&self) -> LimiterState { let inner = self.inner.lock(); LimiterState { limit: inner.limit } } } impl Token { fn new(limiter: Arc<DynamicLimiter>) -> Self { Self { start: Instant::now(), limiter: Some(limiter), } } pub(crate) fn disabled() -> Self { Self { start: Instant::now(), limiter: None, } } pub(crate) fn is_disabled(&self) -> bool { self.limiter.is_none() } pub(crate) fn release(mut self, outcome: Outcome) { self.release_mut(Some(outcome)); } pub(crate) fn release_mut(&mut self, outcome: Option<Outcome>) { if let Some(limiter) = self.limiter.take() { limiter.release_inner(self.start, outcome); } } } impl Drop for Token { fn drop(&mut self) { self.release_mut(None); } } #[cfg(test)] impl LimiterState { /// The current concurrency limit. fn limit(self) -> usize { self.limit } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/rate_limiter/limiter.rs
proxy/src/rate_limiter/limiter.rs
use std::borrow::Cow; use std::collections::hash_map::RandomState; use std::hash::{BuildHasher, Hash}; use std::sync::Mutex; use std::sync::atomic::{AtomicUsize, Ordering}; use anyhow::bail; use clashmap::ClashMap; use itertools::Itertools; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use tokio::time::{Duration, Instant}; use tracing::info; use super::LeakyBucketConfig; use crate::ext::LockExt; use crate::intern::EndpointIdInt; // Simple per-endpoint rate limiter. // // Check that number of connections to the endpoint is below `max_rps` rps. // Purposefully ignore user name and database name as clients can reconnect // with different names, so we'll end up sending some http requests to // the control plane. pub type WakeComputeRateLimiter = BucketRateLimiter<EndpointIdInt, StdRng, RandomState>; pub struct BucketRateLimiter<Key, Rand = StdRng, Hasher = RandomState> { map: ClashMap<Key, Vec<RateBucket>, Hasher>, info: Cow<'static, [RateBucketInfo]>, access_count: AtomicUsize, rand: Mutex<Rand>, } #[derive(Clone, Copy)] struct RateBucket { start: Instant, count: u32, } impl RateBucket { fn should_allow_request(&mut self, info: &RateBucketInfo, now: Instant, n: u32) -> bool { if now - self.start < info.interval { self.count + n <= info.max_rpi } else { // bucket expired, reset self.count = 0; self.start = now; true } } fn inc(&mut self, n: u32) { self.count += n; } } #[derive(Clone, Copy, PartialEq)] pub struct RateBucketInfo { pub(crate) interval: Duration, // requests per interval pub(crate) max_rpi: u32, } impl std::fmt::Display for RateBucketInfo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let rps = self.rps().floor() as u64; write!(f, "{rps}@{}", humantime::format_duration(self.interval)) } } impl std::fmt::Debug for RateBucketInfo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{self}") } } impl std::str::FromStr for RateBucketInfo { type Err = anyhow::Error; fn from_str(s: &str) -> Result<Self, Self::Err> { let Some((max_rps, interval)) = s.split_once('@') else { bail!("invalid rate info") }; let max_rps = max_rps.parse()?; let interval = humantime::parse_duration(interval)?; Ok(Self::new(max_rps, interval)) } } impl RateBucketInfo { pub const DEFAULT_SET: [Self; 3] = [ Self::new(300, Duration::from_secs(1)), Self::new(200, Duration::from_secs(60)), Self::new(100, Duration::from_secs(600)), ]; pub const DEFAULT_ENDPOINT_SET: [Self; 3] = [ Self::new(500, Duration::from_secs(1)), Self::new(300, Duration::from_secs(60)), Self::new(200, Duration::from_secs(600)), ]; pub fn rps(&self) -> f64 { (self.max_rpi as f64) / self.interval.as_secs_f64() } pub fn validate(info: &mut [Self]) -> anyhow::Result<()> { info.sort_unstable_by_key(|info| info.interval); let invalid = info .iter() .tuple_windows() .find(|(a, b)| a.max_rpi > b.max_rpi); if let Some((a, b)) = invalid { bail!( "invalid bucket RPS limits. {b} allows fewer requests per bucket than {a} ({} vs {})", b.max_rpi, a.max_rpi, ); } Ok(()) } pub const fn new(max_rps: u32, interval: Duration) -> Self { Self { interval, max_rpi: ((max_rps as u64) * (interval.as_millis() as u64) / 1000) as u32, } } pub fn to_leaky_bucket(this: &[Self]) -> Option<LeakyBucketConfig> { // bit of a hack - find the min rps and max rps supported and turn it into // leaky bucket config instead let mut iter = this.iter().map(|info| info.rps()); let first = iter.next()?; let (min, max) = (first, first); let (min, max) = iter.fold((min, max), |(min, max), rps| { (f64::min(min, rps), f64::max(max, rps)) }); Some(LeakyBucketConfig { rps: min, max }) } } impl<K: Hash + Eq> BucketRateLimiter<K> { pub fn new(info: impl Into<Cow<'static, [RateBucketInfo]>>) -> Self { Self::new_with_rand_and_hasher(info, StdRng::from_os_rng(), RandomState::new()) } } impl<K: Hash + Eq, R: Rng, S: BuildHasher + Clone> BucketRateLimiter<K, R, S> { fn new_with_rand_and_hasher( info: impl Into<Cow<'static, [RateBucketInfo]>>, rand: R, hasher: S, ) -> Self { let info = info.into(); info!(buckets = ?info, "endpoint rate limiter"); Self { info, map: ClashMap::with_hasher_and_shard_amount(hasher, 64), access_count: AtomicUsize::new(1), // start from 1 to avoid GC on the first request rand: Mutex::new(rand), } } /// Check that number of connections to the endpoint is below `max_rps` rps. pub(crate) fn check(&self, key: K, n: u32) -> bool { // do a partial GC every 2k requests. This cleans up ~ 1/64th of the map. // worst case memory usage is about: // = 2 * 2048 * 64 * (48B + 72B) // = 30MB if self .access_count .fetch_add(1, Ordering::AcqRel) .is_multiple_of(2048) { self.do_gc(); } let now = Instant::now(); let mut entry = self.map.entry(key).or_insert_with(|| { vec![ RateBucket { start: now, count: 0, }; self.info.len() ] }); let should_allow_request = entry .iter_mut() .zip(&*self.info) .all(|(bucket, info)| bucket.should_allow_request(info, now, n)); if should_allow_request { // only increment the bucket counts if the request will actually be accepted entry.iter_mut().for_each(|b| b.inc(n)); } should_allow_request } /// Clean the map. Simple strategy: remove all entries in a random shard. /// At worst, we'll double the effective max_rps during the cleanup. /// But that way deletion does not aquire mutex on each entry access. pub(crate) fn do_gc(&self) { info!( "cleaning up bucket rate limiter, current size = {}", self.map.len() ); let n = self.map.shards().len(); // this lock is ok as the periodic cycle of do_gc makes this very unlikely to collide // (impossible, infact, unless we have 2048 threads) let shard = self.rand.lock_propagate_poison().random_range(0..n); self.map.shards()[shard].write().clear(); } } #[cfg(test)] mod tests { use std::hash::BuildHasherDefault; use std::time::Duration; use rand::SeedableRng; use rustc_hash::FxHasher; use tokio::time; use super::{BucketRateLimiter, WakeComputeRateLimiter}; use crate::intern::EndpointIdInt; use crate::rate_limiter::RateBucketInfo; use crate::types::EndpointId; #[test] fn rate_bucket_rpi() { let rate_bucket = RateBucketInfo::new(50, Duration::from_secs(5)); assert_eq!(rate_bucket.max_rpi, 50 * 5); let rate_bucket = RateBucketInfo::new(50, Duration::from_millis(500)); assert_eq!(rate_bucket.max_rpi, 50 / 2); } #[test] fn rate_bucket_parse() { let rate_bucket: RateBucketInfo = "100@10s".parse().unwrap(); assert_eq!(rate_bucket.interval, Duration::from_secs(10)); assert_eq!(rate_bucket.max_rpi, 100 * 10); assert_eq!(rate_bucket.to_string(), "100@10s"); let rate_bucket: RateBucketInfo = "100@1m".parse().unwrap(); assert_eq!(rate_bucket.interval, Duration::from_secs(60)); assert_eq!(rate_bucket.max_rpi, 100 * 60); assert_eq!(rate_bucket.to_string(), "100@1m"); } #[test] fn default_rate_buckets() { let mut defaults = RateBucketInfo::DEFAULT_SET; RateBucketInfo::validate(&mut defaults[..]).unwrap(); } #[test] #[should_panic = "invalid bucket RPS limits. 10@10s allows fewer requests per bucket than 300@1s (100 vs 300)"] fn rate_buckets_validate() { let mut rates: Vec<RateBucketInfo> = ["300@1s", "10@10s"] .into_iter() .map(|s| s.parse().unwrap()) .collect(); RateBucketInfo::validate(&mut rates).unwrap(); } #[tokio::test] async fn test_rate_limits() { let mut rates: Vec<RateBucketInfo> = ["100@1s", "20@30s"] .into_iter() .map(|s| s.parse().unwrap()) .collect(); RateBucketInfo::validate(&mut rates).unwrap(); let limiter = WakeComputeRateLimiter::new(rates); let endpoint = EndpointId::from("ep-my-endpoint-1234"); let endpoint = EndpointIdInt::from(endpoint); time::pause(); for _ in 0..100 { assert!(limiter.check(endpoint, 1)); } // more connections fail assert!(!limiter.check(endpoint, 1)); // fail even after 500ms as it's in the same bucket time::advance(time::Duration::from_millis(500)).await; assert!(!limiter.check(endpoint, 1)); // after a full 1s, 100 requests are allowed again time::advance(time::Duration::from_millis(500)).await; for _ in 1..6 { for _ in 0..50 { assert!(limiter.check(endpoint, 2)); } time::advance(time::Duration::from_millis(1000)).await; } // more connections after 600 will exceed the 20rps@30s limit assert!(!limiter.check(endpoint, 1)); // will still fail before the 30 second limit time::advance(time::Duration::from_millis(30_000 - 6_000 - 1)).await; assert!(!limiter.check(endpoint, 1)); // after the full 30 seconds, 100 requests are allowed again time::advance(time::Duration::from_millis(1)).await; for _ in 0..100 { assert!(limiter.check(endpoint, 1)); } } #[tokio::test] async fn test_rate_limits_gc() { // fixed seeded random/hasher to ensure that the test is not flaky let rand = rand::rngs::StdRng::from_seed([1; 32]); let hasher = BuildHasherDefault::<FxHasher>::default(); let limiter = BucketRateLimiter::new_with_rand_and_hasher(&RateBucketInfo::DEFAULT_SET, rand, hasher); for i in 0..1_000_000 { limiter.check(i, 1); } assert!(limiter.map.len() < 150_000); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/rate_limiter/mod.rs
proxy/src/rate_limiter/mod.rs
mod leaky_bucket; mod limit_algorithm; mod limiter; pub use leaky_bucket::{EndpointRateLimiter, LeakyBucketConfig, LeakyBucketRateLimiter}; #[cfg(test)] pub(crate) use limit_algorithm::aimd::Aimd; pub(crate) use limit_algorithm::{ DynamicLimiter, Outcome, RateLimitAlgorithm, RateLimiterConfig, Token, }; pub use limiter::{RateBucketInfo, WakeComputeRateLimiter};
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/rate_limiter/leaky_bucket.rs
proxy/src/rate_limiter/leaky_bucket.rs
use std::hash::Hash; use std::sync::atomic::{AtomicUsize, Ordering}; use ahash::RandomState; use clashmap::ClashMap; use rand::Rng; use tokio::time::Instant; use tracing::info; use utils::leaky_bucket::LeakyBucketState; use crate::intern::EndpointIdInt; // Simple per-endpoint rate limiter. pub type EndpointRateLimiter = LeakyBucketRateLimiter<EndpointIdInt>; pub struct LeakyBucketRateLimiter<Key> { map: ClashMap<Key, LeakyBucketState, RandomState>, default_config: utils::leaky_bucket::LeakyBucketConfig, access_count: AtomicUsize, } impl<K: Hash + Eq> LeakyBucketRateLimiter<K> { pub const DEFAULT: LeakyBucketConfig = LeakyBucketConfig { rps: 600.0, max: 1500.0, }; pub fn new_with_shards(config: LeakyBucketConfig, shards: usize) -> Self { Self { map: ClashMap::with_hasher_and_shard_amount(RandomState::new(), shards), default_config: config.into(), access_count: AtomicUsize::new(0), } } /// Check that number of connections to the endpoint is below `max_rps` rps. pub(crate) fn check(&self, key: K, config: Option<LeakyBucketConfig>, n: u32) -> bool { let now = Instant::now(); let config = config.map_or(self.default_config, Into::into); if self .access_count .fetch_add(1, Ordering::AcqRel) .is_multiple_of(2048) { self.do_gc(now); } let mut entry = self .map .entry(key) .or_insert_with(|| LeakyBucketState { empty_at: now }); entry.add_tokens(&config, now, n as f64).is_ok() } fn do_gc(&self, now: Instant) { info!( "cleaning up bucket rate limiter, current size = {}", self.map.len() ); let n = self.map.shards().len(); let shard = rand::rng().random_range(0..n); self.map.shards()[shard] .write() .retain(|(_, value)| !value.bucket_is_empty(now)); } } pub struct LeakyBucketConfig { pub rps: f64, pub max: f64, } impl LeakyBucketConfig { pub fn new(rps: f64, max: f64) -> Self { assert!(rps > 0.0, "rps must be positive"); assert!(max > 0.0, "max must be positive"); Self { rps, max } } } impl From<LeakyBucketConfig> for utils::leaky_bucket::LeakyBucketConfig { fn from(config: LeakyBucketConfig) -> Self { utils::leaky_bucket::LeakyBucketConfig::new(config.rps, config.max) } } #[cfg(test)] #[allow(clippy::float_cmp)] mod tests { use std::time::Duration; use tokio::time::Instant; use utils::leaky_bucket::LeakyBucketState; use super::LeakyBucketConfig; #[tokio::test(start_paused = true)] async fn check() { let config: utils::leaky_bucket::LeakyBucketConfig = LeakyBucketConfig::new(500.0, 2000.0).into(); assert_eq!(config.cost, Duration::from_millis(2)); assert_eq!(config.bucket_width, Duration::from_secs(4)); let mut bucket = LeakyBucketState { empty_at: Instant::now(), }; // should work for 2000 requests this second for _ in 0..2000 { bucket.add_tokens(&config, Instant::now(), 1.0).unwrap(); } bucket.add_tokens(&config, Instant::now(), 1.0).unwrap_err(); assert_eq!(bucket.empty_at - Instant::now(), config.bucket_width); // in 1ms we should drain 0.5 tokens. // make sure we don't lose any tokens tokio::time::advance(Duration::from_millis(1)).await; bucket.add_tokens(&config, Instant::now(), 1.0).unwrap_err(); tokio::time::advance(Duration::from_millis(1)).await; bucket.add_tokens(&config, Instant::now(), 1.0).unwrap(); // in 10ms we should drain 5 tokens tokio::time::advance(Duration::from_millis(10)).await; for _ in 0..5 { bucket.add_tokens(&config, Instant::now(), 1.0).unwrap(); } bucket.add_tokens(&config, Instant::now(), 1.0).unwrap_err(); // in 10s we should drain 5000 tokens // but cap is only 2000 tokio::time::advance(Duration::from_secs(10)).await; for _ in 0..2000 { bucket.add_tokens(&config, Instant::now(), 1.0).unwrap(); } bucket.add_tokens(&config, Instant::now(), 1.0).unwrap_err(); // should sustain 500rps for _ in 0..2000 { tokio::time::advance(Duration::from_millis(10)).await; for _ in 0..5 { bucket.add_tokens(&config, Instant::now(), 1.0).unwrap(); } } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/rate_limiter/limit_algorithm/aimd.rs
proxy/src/rate_limiter/limit_algorithm/aimd.rs
use super::{LimitAlgorithm, Outcome, Sample}; /// Loss-based congestion avoidance. /// /// Additive-increase, multiplicative decrease. /// /// Adds available currency when: /// 1. no load-based errors are observed, and /// 2. the utilisation of the current limit is high. /// /// Reduces available concurrency by a factor when load-based errors are detected. #[derive(Clone, Copy, Debug, serde::Deserialize, PartialEq)] pub(crate) struct Aimd { /// Minimum limit for AIMD algorithm. pub(crate) min: usize, /// Maximum limit for AIMD algorithm. pub(crate) max: usize, /// Decrease AIMD decrease by value in case of error. pub(crate) dec: f32, /// Increase AIMD increase by value in case of success. pub(crate) inc: usize, /// A threshold below which the limit won't be increased. pub(crate) utilisation: f32, } impl LimitAlgorithm for Aimd { fn update(&self, old_limit: usize, sample: Sample) -> usize { match sample.outcome { Outcome::Success => { let utilisation = sample.in_flight as f32 / old_limit as f32; if utilisation > self.utilisation { let limit = old_limit + self.inc; let new_limit = limit.clamp(self.min, self.max); if new_limit > old_limit { tracing::info!(old_limit, new_limit, "limit increased"); } else { tracing::debug!(old_limit, new_limit, "limit clamped at max"); } new_limit } else { old_limit } } Outcome::Overload => { let new_limit = old_limit as f32 * self.dec; // Floor instead of round, so the limit reduces even with small numbers. // E.g. round(2 * 0.9) = 2, but floor(2 * 0.9) = 1 let new_limit = new_limit.floor() as usize; let new_limit = new_limit.clamp(self.min, self.max); if new_limit < old_limit { tracing::info!(old_limit, new_limit, "limit decreased"); } else { tracing::debug!(old_limit, new_limit, "limit clamped at min"); } new_limit } } } } #[cfg(test)] mod tests { use std::time::Duration; use super::*; use crate::rate_limiter::limit_algorithm::{ DynamicLimiter, RateLimitAlgorithm, RateLimiterConfig, }; #[tokio::test(start_paused = true)] async fn increase_decrease() { let config = RateLimiterConfig { initial_limit: 1, algorithm: RateLimitAlgorithm::Aimd { conf: Aimd { min: 1, max: 2, inc: 10, dec: 0.5, utilisation: 0.8, }, }, }; let limiter = DynamicLimiter::new(config); let token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); token.release(Outcome::Success); assert_eq!(limiter.state().limit(), 2); let token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); token.release(Outcome::Success); assert_eq!(limiter.state().limit(), 2); let token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); token.release(Outcome::Overload); assert_eq!(limiter.state().limit(), 1); let token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); token.release(Outcome::Overload); assert_eq!(limiter.state().limit(), 1); } #[tokio::test(start_paused = true)] async fn should_decrease_limit_on_overload() { let config = RateLimiterConfig { initial_limit: 10, algorithm: RateLimitAlgorithm::Aimd { conf: Aimd { min: 1, max: 1500, inc: 10, dec: 0.5, utilisation: 0.8, }, }, }; let limiter = DynamicLimiter::new(config); let token = limiter .acquire_timeout(Duration::from_millis(100)) .await .unwrap(); token.release(Outcome::Overload); assert_eq!(limiter.state().limit(), 5, "overload: decrease"); } #[tokio::test(start_paused = true)] async fn acquire_timeout_times_out() { let config = RateLimiterConfig { initial_limit: 1, algorithm: RateLimitAlgorithm::Aimd { conf: Aimd { min: 1, max: 2, inc: 10, dec: 0.5, utilisation: 0.8, }, }, }; let limiter = DynamicLimiter::new(config); let token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); let now = tokio::time::Instant::now(); limiter .acquire_timeout(Duration::from_secs(1)) .await .err() .unwrap(); assert!(now.elapsed() >= Duration::from_secs(1)); token.release(Outcome::Success); assert_eq!(limiter.state().limit(), 2); } #[tokio::test(start_paused = true)] async fn should_increase_limit_on_success_when_using_gt_util_threshold() { let config = RateLimiterConfig { initial_limit: 4, algorithm: RateLimitAlgorithm::Aimd { conf: Aimd { min: 1, max: 1500, inc: 1, dec: 0.5, utilisation: 0.5, }, }, }; let limiter = DynamicLimiter::new(config); let token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); let _token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); let _token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); token.release(Outcome::Success); assert_eq!(limiter.state().limit(), 5, "success: increase"); } #[tokio::test(start_paused = true)] async fn should_not_change_limit_on_success_when_using_lt_util_threshold() { let config = RateLimiterConfig { initial_limit: 4, algorithm: RateLimitAlgorithm::Aimd { conf: Aimd { min: 1, max: 1500, inc: 10, dec: 0.5, utilisation: 0.5, }, }, }; let limiter = DynamicLimiter::new(config); let token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); token.release(Outcome::Success); assert_eq!( limiter.state().limit(), 4, "success: ignore when < half limit" ); } #[tokio::test(start_paused = true)] async fn should_not_change_limit_when_no_outcome() { let config = RateLimiterConfig { initial_limit: 10, algorithm: RateLimitAlgorithm::Aimd { conf: Aimd { min: 1, max: 1500, inc: 10, dec: 0.5, utilisation: 0.5, }, }, }; let limiter = DynamicLimiter::new(config); let token = limiter .acquire_timeout(Duration::from_millis(1)) .await .unwrap(); drop(token); assert_eq!(limiter.state().limit(), 10, "ignore"); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/http/mod.rs
proxy/src/http/mod.rs
//! HTTP client and server impls. //! Other modules should use stuff from this module instead of //! directly relying on deps like `reqwest` (think loose coupling). pub mod health_server; use std::time::{Duration, Instant}; use bytes::Bytes; use futures::FutureExt; use http::Method; use http_body_util::BodyExt; use hyper::body::Body; pub(crate) use reqwest::{Request, Response}; use reqwest_middleware::RequestBuilder; pub(crate) use reqwest_middleware::{ClientWithMiddleware, Error}; pub(crate) use reqwest_retry::RetryTransientMiddleware; pub(crate) use reqwest_retry::policies::ExponentialBackoff; use thiserror::Error; use crate::metrics::{ConsoleRequest, Metrics}; use crate::url::ApiUrl; /// This is the preferred way to create new http clients, /// because it takes care of observability (OpenTelemetry). /// We deliberately don't want to replace this with a public static. pub fn new_client() -> ClientWithMiddleware { let client = reqwest::ClientBuilder::new() .build() .expect("Failed to create http client"); reqwest_middleware::ClientBuilder::new(client) .with(reqwest_tracing::TracingMiddleware::default()) .build() } pub(crate) fn new_client_with_timeout( request_timeout: Duration, total_retry_duration: Duration, ) -> ClientWithMiddleware { let timeout_client = reqwest::ClientBuilder::new() .timeout(request_timeout) .build() .expect("Failed to create http client with timeout"); let retry_policy = ExponentialBackoff::builder().build_with_total_retry_duration(total_retry_duration); reqwest_middleware::ClientBuilder::new(timeout_client) .with(reqwest_tracing::TracingMiddleware::default()) // As per docs, "This middleware always errors when given requests with streaming bodies". // That's all right because we only use this client to send `serde_json::RawValue`, which // is not a stream. // // ex-maintainer note: // this limitation can be fixed if streaming is necessary. // retries will still not be performed, but it wont error immediately .with(RetryTransientMiddleware::new_with_policy(retry_policy)) .build() } /// Thin convenience wrapper for an API provided by an http endpoint. #[derive(Debug, Clone)] pub struct Endpoint { /// API's base URL. endpoint: ApiUrl, /// Connection manager with built-in pooling. client: ClientWithMiddleware, } impl Endpoint { /// Construct a new HTTP endpoint wrapper. /// Http client is not constructed under the hood so that it can be shared. pub fn new(endpoint: ApiUrl, client: impl Into<ClientWithMiddleware>) -> Self { Self { endpoint, client: client.into(), } } #[inline(always)] pub(crate) fn url(&self) -> &ApiUrl { &self.endpoint } /// Return a [builder](RequestBuilder) for a `GET` request, /// appending a single `path` segment to the base endpoint URL. pub(crate) fn get_path(&self, path: &str) -> RequestBuilder { self.get_with_url(|u| { u.path_segments_mut().push(path); }) } /// Return a [builder](RequestBuilder) for a `GET` request, /// accepting a closure to modify the url path segments for more complex paths queries. pub(crate) fn get_with_url(&self, f: impl for<'a> FnOnce(&'a mut ApiUrl)) -> RequestBuilder { self.request_with_url(Method::GET, f) } /// Return a [builder](RequestBuilder) for a request, /// accepting a closure to modify the url path segments for more complex paths queries. pub(crate) fn request_with_url( &self, method: Method, f: impl for<'a> FnOnce(&'a mut ApiUrl), ) -> RequestBuilder { let mut url = self.endpoint.clone(); f(&mut url); self.client.request(method, url.into_inner()) } /// Execute a [request](reqwest::Request). pub(crate) fn execute( &self, request: Request, ) -> impl Future<Output = Result<Response, Error>> { let metric = Metrics::get() .proxy .console_request_latency .with_labels(ConsoleRequest { request: request.url().path(), }); let req = self.client.execute(request).boxed(); async move { let start = Instant::now(); scopeguard::defer!({ Metrics::get() .proxy .console_request_latency .get_metric(metric) .observe_duration_since(start); }); req.await } } } #[derive(Error, Debug)] pub(crate) enum ReadBodyError<E> { #[error("Content length exceeds limit of {limit} bytes")] BodyTooLarge { limit: usize }, #[error(transparent)] Read(#[from] E), } pub(crate) async fn read_body_with_limit<E>( mut b: impl Body<Data = Bytes, Error = E> + Unpin, limit: usize, ) -> Result<Vec<u8>, ReadBodyError<E>> { // We could use `b.limited().collect().await.to_bytes()` here // but this ends up being slightly more efficient as far as I can tell. // check the lower bound of the size hint. // in reqwest, this value is influenced by the Content-Length header. let lower_bound = match usize::try_from(b.size_hint().lower()) { Ok(bound) if bound <= limit => bound, _ => return Err(ReadBodyError::BodyTooLarge { limit }), }; let mut bytes = Vec::with_capacity(lower_bound); while let Some(frame) = b.frame().await.transpose()? { if let Ok(data) = frame.into_data() { if bytes.len() + data.len() > limit { return Err(ReadBodyError::BodyTooLarge { limit }); } bytes.extend_from_slice(&data); } } Ok(bytes) } #[cfg(test)] mod tests { use reqwest::Client; use super::*; #[test] fn optional_query_params() -> anyhow::Result<()> { let url = "http://example.com".parse()?; let endpoint = Endpoint::new(url, Client::new()); // Validate that this pattern makes sense. let req = endpoint .get_path("frobnicate") .query(&[ ("foo", Some("10")), // should be just `foo=10` ("bar", None), // shouldn't be passed at all ]) .build()?; assert_eq!(req.url().as_str(), "http://example.com/frobnicate?foo=10"); Ok(()) } #[test] fn uuid_params() -> anyhow::Result<()> { let url = "http://example.com".parse()?; let endpoint = Endpoint::new(url, Client::new()); let req = endpoint .get_path("frobnicate") .query(&[("session_id", uuid::Uuid::nil())]) .build()?; assert_eq!( req.url().as_str(), "http://example.com/frobnicate?session_id=00000000-0000-0000-0000-000000000000" ); Ok(()) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/proxy/src/http/health_server.rs
proxy/src/http/health_server.rs
use std::convert::Infallible; use std::net::TcpListener; use std::sync::{Arc, Mutex}; use anyhow::{anyhow, bail}; use http_utils::endpoint::{self, profile_cpu_handler, profile_heap_handler, request_span}; use http_utils::error::ApiError; use http_utils::json::json_response; use http_utils::{RouterBuilder, RouterService}; use hyper0::header::CONTENT_TYPE; use hyper0::{Body, Request, Response, StatusCode}; use measured::MetricGroup; use measured::text::BufferedTextEncoder; use metrics::NeonMetrics; use tracing::{info, info_span}; use crate::ext::{LockExt, TaskExt}; use crate::jemalloc; async fn status_handler(_: Request<Body>) -> Result<Response<Body>, ApiError> { json_response(StatusCode::OK, "") } fn make_router(metrics: AppMetrics) -> RouterBuilder<hyper0::Body, ApiError> { let state = Arc::new(Mutex::new(PrometheusHandler { encoder: BufferedTextEncoder::new(), metrics, })); endpoint::make_router() .get("/metrics", move |r| { let state = state.clone(); request_span(r, move |b| prometheus_metrics_handler(b, state)) }) .get("/v1/status", status_handler) .get("/profile/cpu", move |r| { request_span(r, profile_cpu_handler) }) .get("/profile/heap", move |r| { request_span(r, profile_heap_handler) }) } pub async fn task_main( http_listener: TcpListener, metrics: AppMetrics, ) -> anyhow::Result<Infallible> { scopeguard::defer! { info!("http has shut down"); } let service = || RouterService::new(make_router(metrics).build()?); hyper0::Server::from_tcp(http_listener)? .serve(service().map_err(|e| anyhow!(e))?) .await?; bail!("hyper server without shutdown handling cannot shutdown successfully"); } struct PrometheusHandler { encoder: BufferedTextEncoder, metrics: AppMetrics, } #[derive(MetricGroup)] pub struct AppMetrics { #[metric(namespace = "jemalloc")] pub jemalloc: Option<jemalloc::MetricRecorder>, #[metric(flatten)] pub neon_metrics: NeonMetrics, #[metric(flatten)] pub proxy: &'static crate::metrics::Metrics, } async fn prometheus_metrics_handler( _req: Request<Body>, state: Arc<Mutex<PrometheusHandler>>, ) -> Result<Response<Body>, ApiError> { let started_at = std::time::Instant::now(); let span = info_span!("blocking"); let body = tokio::task::spawn_blocking(move || { let _span = span.entered(); let mut state = state.lock_propagate_poison(); let PrometheusHandler { encoder, metrics } = &mut *state; metrics .collect_group_into(&mut *encoder) .unwrap_or_else(|infallible| match infallible {}); let body = encoder.finish(); tracing::info!( bytes = body.len(), elapsed_ms = started_at.elapsed().as_millis(), "responded /metrics" ); body }) .await .propagate_task_panic(); let response = Response::builder() .status(200) .header(CONTENT_TYPE, "text/plain; version=0.0.4") .body(Body::from(body)) .expect("response headers should be valid"); Ok(response) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/page_api/build.rs
pageserver/page_api/build.rs
use std::env; use std::path::PathBuf; /// Generates Rust code from .proto Protobuf schemas, along with a binary file /// descriptor set for Protobuf schema reflection. fn main() -> Result<(), Box<dyn std::error::Error>> { let out_dir = PathBuf::from(env::var("OUT_DIR")?); tonic_build::configure() .bytes(["."]) .file_descriptor_set_path(out_dir.join("page_api_descriptor.bin")) .compile_protos(&["proto/page_service.proto"], &["proto"]) .map_err(|err| err.into()) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/page_api/src/lib.rs
pageserver/page_api/src/lib.rs
//! This crate provides the Pageserver's page API. It contains: //! //! * proto/page_service.proto: the Protobuf schema for the page API. //! * proto: auto-generated Protobuf types for gRPC. //! //! This crate is used by both the client and the server. Try to keep it slim. // Code generated by protobuf. pub mod proto { tonic::include_proto!("page_api"); /// File descriptor set for Protobuf schema reflection. This allows using /// e.g. grpcurl with the API. pub const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("page_api_descriptor"); pub use page_service_client::PageServiceClient; pub use page_service_server::{PageService, PageServiceServer}; } mod client; mod model; mod split; pub use client::Client; pub use model::*; pub use split::{GetPageSplitter, SplitError};
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/page_api/src/client.rs
pageserver/page_api/src/client.rs
use anyhow::Context as _; use futures::future::ready; use futures::{Stream, StreamExt as _, TryStreamExt as _}; use tokio::io::AsyncRead; use tokio_util::io::StreamReader; use tonic::codec::CompressionEncoding; use tonic::metadata::AsciiMetadataValue; use tonic::service::Interceptor; use tonic::service::interceptor::InterceptedService; use tonic::transport::{Channel, Endpoint}; use utils::id::{TenantId, TimelineId}; use utils::shard::ShardIndex; use crate::model::*; use crate::proto; /// A basic Pageserver gRPC client, for a single tenant shard. This API uses native Rust domain /// types from `model` rather than generated Protobuf types. pub struct Client { inner: proto::PageServiceClient<InterceptedService<Channel, AuthInterceptor>>, } impl Client { /// Connects to the given gRPC endpoint. pub async fn connect<E>( endpoint: E, tenant_id: TenantId, timeline_id: TimelineId, shard_id: ShardIndex, auth_token: Option<String>, compression: Option<CompressionEncoding>, ) -> anyhow::Result<Self> where E: TryInto<Endpoint> + Send + Sync + 'static, <E as TryInto<Endpoint>>::Error: std::error::Error + Send + Sync, { let endpoint: Endpoint = endpoint.try_into().context("invalid endpoint")?; let channel = endpoint.connect().await?; Self::new( channel, tenant_id, timeline_id, shard_id, auth_token, compression, ) } /// Creates a new client using the given gRPC channel. pub fn new( channel: Channel, tenant_id: TenantId, timeline_id: TimelineId, shard_id: ShardIndex, auth_token: Option<String>, compression: Option<CompressionEncoding>, ) -> anyhow::Result<Self> { let auth = AuthInterceptor::new(tenant_id, timeline_id, shard_id, auth_token)?; let mut inner = proto::PageServiceClient::with_interceptor(channel, auth); if let Some(compression) = compression { // TODO: benchmark this (including network latency). inner = inner .accept_compressed(compression) .send_compressed(compression); } Ok(Self { inner }) } /// Fetches a base backup. pub async fn get_base_backup( &mut self, req: GetBaseBackupRequest, ) -> tonic::Result<impl AsyncRead + use<>> { let req = proto::GetBaseBackupRequest::from(req); let chunks = self.inner.get_base_backup(req).await?.into_inner(); Ok(StreamReader::new( chunks .map_ok(|resp| resp.chunk) .map_err(std::io::Error::other), )) } /// Returns the total size of a database, as # of bytes. pub async fn get_db_size(&mut self, req: GetDbSizeRequest) -> tonic::Result<GetDbSizeResponse> { let req = proto::GetDbSizeRequest::from(req); let resp = self.inner.get_db_size(req).await?.into_inner(); Ok(resp.into()) } /// Fetches pages. /// /// This is implemented as a bidirectional streaming RPC for performance. Per-request errors are /// typically returned as status_code instead of errors, to avoid tearing down the entire stream /// via a tonic::Status error. pub async fn get_pages( &mut self, reqs: impl Stream<Item = GetPageRequest> + Send + 'static, ) -> tonic::Result<impl Stream<Item = tonic::Result<GetPageResponse>> + Send + 'static> { let reqs = reqs.map(proto::GetPageRequest::from); let resps = self.inner.get_pages(reqs).await?.into_inner(); Ok(resps.and_then(|resp| ready(GetPageResponse::try_from(resp).map_err(|err| err.into())))) } /// Returns the size of a relation as # of blocks, or None if allow_missing=true and the /// relation does not exist. pub async fn get_rel_size( &mut self, req: GetRelSizeRequest, ) -> tonic::Result<GetRelSizeResponse> { let req = proto::GetRelSizeRequest::from(req); let resp = self.inner.get_rel_size(req).await?.into_inner(); Ok(resp.into()) } /// Fetches an SLRU segment. pub async fn get_slru_segment( &mut self, req: GetSlruSegmentRequest, ) -> tonic::Result<GetSlruSegmentResponse> { let req = proto::GetSlruSegmentRequest::from(req); let resp = self.inner.get_slru_segment(req).await?.into_inner(); Ok(resp.try_into()?) } /// Acquires or extends a lease on the given LSN. This guarantees that the Pageserver won't /// garbage collect the LSN until the lease expires. Must be acquired on all relevant shards. /// /// Returns the lease expiration time, or a FailedPrecondition status if the lease could not be /// acquired because the LSN has already been garbage collected. pub async fn lease_lsn(&mut self, req: LeaseLsnRequest) -> tonic::Result<LeaseLsnResponse> { let req = proto::LeaseLsnRequest::from(req); let resp = self.inner.lease_lsn(req).await?.into_inner(); Ok(resp.try_into()?) } } /// Adds authentication metadata to gRPC requests. #[derive(Clone)] struct AuthInterceptor { tenant_id: AsciiMetadataValue, timeline_id: AsciiMetadataValue, shard_id: AsciiMetadataValue, auth_header: Option<AsciiMetadataValue>, // including "Bearer " prefix } impl AuthInterceptor { fn new( tenant_id: TenantId, timeline_id: TimelineId, shard_id: ShardIndex, auth_token: Option<String>, ) -> anyhow::Result<Self> { Ok(Self { tenant_id: tenant_id.to_string().try_into()?, timeline_id: timeline_id.to_string().try_into()?, shard_id: shard_id.to_string().try_into()?, auth_header: auth_token .map(|token| format!("Bearer {token}").try_into()) .transpose()?, }) } } impl Interceptor for AuthInterceptor { fn call(&mut self, mut req: tonic::Request<()>) -> tonic::Result<tonic::Request<()>> { let metadata = req.metadata_mut(); metadata.insert("neon-tenant-id", self.tenant_id.clone()); metadata.insert("neon-timeline-id", self.timeline_id.clone()); metadata.insert("neon-shard-id", self.shard_id.clone()); if let Some(ref auth_header) = self.auth_header { metadata.insert("authorization", auth_header.clone()); } Ok(req) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/page_api/src/model.rs
pageserver/page_api/src/model.rs
//! Structs representing the canonical page service API. //! //! These mirror the autogenerated Protobuf types. The differences are: //! //! - Types that are in fact required by the API are not Options. The protobuf "required" //! attribute is deprecated and 'prost' marks a lot of members as optional because of that. //! (See <https://github.com/tokio-rs/prost/issues/800> for a gripe on this) //! //! - Use more precise datatypes, e.g. Lsn and uints shorter than 32 bits. //! //! - Validate protocol invariants, via try_from() and try_into(). //! //! Validation only happens on the receiver side, i.e. when converting from Protobuf to domain //! types. This is where it matters -- the Protobuf types are less strict than the domain types, and //! receivers should expect all sorts of junk from senders. This also allows the sender to use e.g. //! stream combinators without dealing with errors, and avoids validating the same message twice. use std::fmt::Display; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use bytes::Bytes; use postgres_ffi_types::Oid; // TODO: split out Lsn, RelTag, SlruKind and other basic types to a separate crate, to avoid // pulling in all of their other crate dependencies when building the client. use utils::lsn::Lsn; use crate::proto; /// A protocol error. Typically returned via try_from() or try_into(). #[derive(thiserror::Error, Clone, Debug)] pub enum ProtocolError { #[error("field '{0}' has invalid value '{1}'")] Invalid(&'static str, String), #[error("required field '{0}' is missing")] Missing(&'static str), } impl ProtocolError { /// Helper to generate a new ProtocolError::Invalid for the given field and value. pub fn invalid(field: &'static str, value: impl std::fmt::Debug) -> Self { Self::Invalid(field, format!("{value:?}")) } } impl From<ProtocolError> for tonic::Status { fn from(err: ProtocolError) -> Self { tonic::Status::invalid_argument(format!("{err}")) } } /// The LSN a request should read at. #[derive(Clone, Copy, Debug, Default)] pub struct ReadLsn { /// The request's read LSN. pub request_lsn: Lsn, /// If given, the caller guarantees that the page has not been modified since this LSN. Must be /// smaller than or equal to request_lsn. This allows the Pageserver to serve an old page /// without waiting for the request LSN to arrive. If not given, the request will read at the /// request_lsn and wait for it to arrive if necessary. Valid for all request types. /// /// It is undefined behaviour to make a request such that the page was, in fact, modified /// between request_lsn and not_modified_since_lsn. The Pageserver might detect it and return an /// error, or it might return the old page version or the new page version. Setting /// not_modified_since_lsn equal to request_lsn is always safe, but can lead to unnecessary /// waiting. pub not_modified_since_lsn: Option<Lsn>, } impl Display for ReadLsn { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let req_lsn = self.request_lsn; if let Some(mod_lsn) = self.not_modified_since_lsn { write!(f, "{req_lsn}>={mod_lsn}") } else { req_lsn.fmt(f) } } } impl TryFrom<proto::ReadLsn> for ReadLsn { type Error = ProtocolError; fn try_from(pb: proto::ReadLsn) -> Result<Self, Self::Error> { if pb.request_lsn == 0 { return Err(ProtocolError::invalid("request_lsn", pb.request_lsn)); } if pb.not_modified_since_lsn > pb.request_lsn { return Err(ProtocolError::invalid( "not_modified_since_lsn", pb.not_modified_since_lsn, )); } Ok(Self { request_lsn: Lsn(pb.request_lsn), not_modified_since_lsn: match pb.not_modified_since_lsn { 0 => None, lsn => Some(Lsn(lsn)), }, }) } } impl From<ReadLsn> for proto::ReadLsn { fn from(read_lsn: ReadLsn) -> Self { Self { request_lsn: read_lsn.request_lsn.0, not_modified_since_lsn: read_lsn.not_modified_since_lsn.unwrap_or_default().0, } } } // RelTag is defined in pageserver_api::reltag. pub type RelTag = pageserver_api::reltag::RelTag; impl TryFrom<proto::RelTag> for RelTag { type Error = ProtocolError; fn try_from(pb: proto::RelTag) -> Result<Self, Self::Error> { Ok(Self { spcnode: pb.spc_oid, dbnode: pb.db_oid, relnode: pb.rel_number, forknum: pb .fork_number .try_into() .map_err(|_| ProtocolError::invalid("fork_number", pb.fork_number))?, }) } } impl From<RelTag> for proto::RelTag { fn from(rel_tag: RelTag) -> Self { Self { spc_oid: rel_tag.spcnode, db_oid: rel_tag.dbnode, rel_number: rel_tag.relnode, fork_number: rel_tag.forknum as u32, } } } /// Requests a base backup. #[derive(Clone, Copy, Debug)] pub struct GetBaseBackupRequest { /// The LSN to fetch a base backup at. If None, uses the latest LSN known to the Pageserver. pub lsn: Option<Lsn>, /// If true, logical replication slots will not be created. pub replica: bool, /// If true, include relation files in the base backup. Mainly for debugging and tests. pub full: bool, /// Compression algorithm to use. Base backups send a compressed payload instead of using gRPC /// compression, so that we can cache compressed backups on the server. pub compression: BaseBackupCompression, } impl TryFrom<proto::GetBaseBackupRequest> for GetBaseBackupRequest { type Error = ProtocolError; fn try_from(pb: proto::GetBaseBackupRequest) -> Result<Self, Self::Error> { Ok(Self { lsn: (pb.lsn != 0).then_some(Lsn(pb.lsn)), replica: pb.replica, full: pb.full, compression: pb.compression.try_into()?, }) } } impl From<GetBaseBackupRequest> for proto::GetBaseBackupRequest { fn from(request: GetBaseBackupRequest) -> Self { Self { lsn: request.lsn.unwrap_or_default().0, replica: request.replica, full: request.full, compression: request.compression.into(), } } } /// Base backup compression algorithm. #[derive(Clone, Copy, Debug)] pub enum BaseBackupCompression { None, Gzip, } impl TryFrom<proto::BaseBackupCompression> for BaseBackupCompression { type Error = ProtocolError; fn try_from(pb: proto::BaseBackupCompression) -> Result<Self, Self::Error> { match pb { proto::BaseBackupCompression::Unknown => Err(ProtocolError::invalid("compression", pb)), proto::BaseBackupCompression::None => Ok(Self::None), proto::BaseBackupCompression::Gzip => Ok(Self::Gzip), } } } impl TryFrom<i32> for BaseBackupCompression { type Error = ProtocolError; fn try_from(compression: i32) -> Result<Self, Self::Error> { proto::BaseBackupCompression::try_from(compression) .map_err(|_| ProtocolError::invalid("compression", compression)) .and_then(Self::try_from) } } impl From<BaseBackupCompression> for proto::BaseBackupCompression { fn from(compression: BaseBackupCompression) -> Self { match compression { BaseBackupCompression::None => Self::None, BaseBackupCompression::Gzip => Self::Gzip, } } } impl From<BaseBackupCompression> for i32 { fn from(compression: BaseBackupCompression) -> Self { proto::BaseBackupCompression::from(compression).into() } } pub type GetBaseBackupResponseChunk = Bytes; impl TryFrom<proto::GetBaseBackupResponseChunk> for GetBaseBackupResponseChunk { type Error = ProtocolError; fn try_from(pb: proto::GetBaseBackupResponseChunk) -> Result<Self, Self::Error> { if pb.chunk.is_empty() { return Err(ProtocolError::Missing("chunk")); } Ok(pb.chunk) } } impl From<GetBaseBackupResponseChunk> for proto::GetBaseBackupResponseChunk { fn from(chunk: GetBaseBackupResponseChunk) -> Self { Self { chunk } } } /// Requests the size of a database, as # of bytes. Only valid on shard 0, other shards will error. #[derive(Clone, Copy, Debug)] pub struct GetDbSizeRequest { pub read_lsn: ReadLsn, pub db_oid: Oid, } impl TryFrom<proto::GetDbSizeRequest> for GetDbSizeRequest { type Error = ProtocolError; fn try_from(pb: proto::GetDbSizeRequest) -> Result<Self, Self::Error> { Ok(Self { read_lsn: pb .read_lsn .ok_or(ProtocolError::Missing("read_lsn"))? .try_into()?, db_oid: pb.db_oid, }) } } impl From<GetDbSizeRequest> for proto::GetDbSizeRequest { fn from(request: GetDbSizeRequest) -> Self { Self { read_lsn: Some(request.read_lsn.into()), db_oid: request.db_oid, } } } pub type GetDbSizeResponse = u64; impl From<proto::GetDbSizeResponse> for GetDbSizeResponse { fn from(pb: proto::GetDbSizeResponse) -> Self { pb.num_bytes } } impl From<GetDbSizeResponse> for proto::GetDbSizeResponse { fn from(num_bytes: GetDbSizeResponse) -> Self { Self { num_bytes } } } /// Requests one or more pages. #[derive(Clone, Debug, Default)] pub struct GetPageRequest { /// A request ID. Will be included in the response. Should be unique for in-flight requests on /// the stream. pub request_id: RequestID, /// The request class. pub request_class: GetPageClass, /// The LSN to read at. pub read_lsn: ReadLsn, /// The relation to read from. pub rel: RelTag, /// Page numbers to read. Must belong to the remote shard. /// /// Multiple pages will be executed as a single batch by the Pageserver, amortizing layer access /// costs and parallelizing them. This may increase the latency of any individual request, but /// improves the overall latency and throughput of the batch as a whole. pub block_numbers: Vec<u32>, } impl TryFrom<proto::GetPageRequest> for GetPageRequest { type Error = ProtocolError; fn try_from(pb: proto::GetPageRequest) -> Result<Self, Self::Error> { if pb.block_number.is_empty() { return Err(ProtocolError::Missing("block_number")); } Ok(Self { request_id: pb .request_id .ok_or(ProtocolError::Missing("request_id"))? .into(), request_class: pb.request_class.into(), read_lsn: pb .read_lsn .ok_or(ProtocolError::Missing("read_lsn"))? .try_into()?, rel: pb.rel.ok_or(ProtocolError::Missing("rel"))?.try_into()?, block_numbers: pb.block_number, }) } } impl From<GetPageRequest> for proto::GetPageRequest { fn from(request: GetPageRequest) -> Self { Self { request_id: Some(request.request_id.into()), request_class: request.request_class.into(), read_lsn: Some(request.read_lsn.into()), rel: Some(request.rel.into()), block_number: request.block_numbers, } } } /// A GetPage request ID and retry attempt. Should be unique for in-flight requests on a stream. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct RequestID { /// The base request ID. pub id: u64, // The request attempt. Starts at 0, incremented on each retry. pub attempt: u32, } impl RequestID { /// Creates a new RequestID with the given ID and an initial attempt of 0. pub fn new(id: u64) -> Self { Self { id, attempt: 0 } } } impl Display for RequestID { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}.{}", self.id, self.attempt) } } impl From<proto::RequestId> for RequestID { fn from(pb: proto::RequestId) -> Self { Self { id: pb.id, attempt: pb.attempt, } } } impl From<u64> for RequestID { fn from(id: u64) -> Self { Self::new(id) } } impl From<RequestID> for proto::RequestId { fn from(request_id: RequestID) -> Self { Self { id: request_id.id, attempt: request_id.attempt, } } } /// A GetPage request class. #[derive(Clone, Copy, Debug, Default, strum_macros::Display)] pub enum GetPageClass { /// Unknown class. For backwards compatibility: used when an older client version sends a class /// that a newer server version has removed. Unknown, /// A normal request. This is the default. #[default] Normal, /// A prefetch request. NB: can only be classified on pg < 18. Prefetch, /// A background request (e.g. vacuum). Background, } impl From<proto::GetPageClass> for GetPageClass { fn from(pb: proto::GetPageClass) -> Self { match pb { proto::GetPageClass::Unknown => Self::Unknown, proto::GetPageClass::Normal => Self::Normal, proto::GetPageClass::Prefetch => Self::Prefetch, proto::GetPageClass::Background => Self::Background, } } } impl From<i32> for GetPageClass { fn from(class: i32) -> Self { proto::GetPageClass::try_from(class) .unwrap_or(proto::GetPageClass::Unknown) .into() } } impl From<GetPageClass> for proto::GetPageClass { fn from(class: GetPageClass) -> Self { match class { GetPageClass::Unknown => Self::Unknown, GetPageClass::Normal => Self::Normal, GetPageClass::Prefetch => Self::Prefetch, GetPageClass::Background => Self::Background, } } } impl From<GetPageClass> for i32 { fn from(class: GetPageClass) -> Self { proto::GetPageClass::from(class).into() } } /// A GetPage response. /// /// A batch response will contain all of the requested pages. We could eagerly emit individual pages /// as soon as they are ready, but on a readv() Postgres holds buffer pool locks on all pages in the /// batch and we'll only return once the entire batch is ready, so no one can make use of the /// individual pages. #[derive(Clone, Debug)] pub struct GetPageResponse { /// The original request's ID. pub request_id: RequestID, /// The response status code. If not OK, the `rel` and `pages` fields will be empty. pub status_code: GetPageStatusCode, /// A string describing the status, if any. pub reason: Option<String>, /// The relation that the pages belong to. pub rel: RelTag, // The page(s), in the same order as the request. pub pages: Vec<Page>, } impl TryFrom<proto::GetPageResponse> for GetPageResponse { type Error = ProtocolError; fn try_from(pb: proto::GetPageResponse) -> Result<Self, ProtocolError> { Ok(Self { request_id: pb .request_id .ok_or(ProtocolError::Missing("request_id"))? .into(), status_code: pb.status_code.into(), reason: Some(pb.reason).filter(|r| !r.is_empty()), rel: pb.rel.ok_or(ProtocolError::Missing("rel"))?.try_into()?, pages: pb.page.into_iter().map(Page::from).collect(), }) } } impl From<GetPageResponse> for proto::GetPageResponse { fn from(response: GetPageResponse) -> Self { Self { request_id: Some(response.request_id.into()), status_code: response.status_code.into(), reason: response.reason.unwrap_or_default(), rel: Some(response.rel.into()), page: response.pages.into_iter().map(proto::Page::from).collect(), } } } impl GetPageResponse { /// Attempts to represent a tonic::Status as a GetPageResponse if appropriate. Returning a /// tonic::Status will terminate the GetPage stream, so per-request errors are emitted as a /// GetPageResponse with a non-OK status code instead. #[allow(clippy::result_large_err)] pub fn try_from_status( status: tonic::Status, request_id: RequestID, ) -> Result<Self, tonic::Status> { // We shouldn't see an OK status here, because we're emitting an error. debug_assert_ne!(status.code(), tonic::Code::Ok); if status.code() == tonic::Code::Ok { return Err(tonic::Status::internal(format!( "unexpected OK status: {status:?}", ))); } // If we can't convert the tonic::Code to a GetPageStatusCode, this is not a per-request // error and we should return a tonic::Status to terminate the stream. let Ok(status_code) = status.code().try_into() else { return Err(status); }; // Return a GetPageResponse for the status. Ok(Self { request_id, status_code, reason: Some(status.message().to_string()), rel: RelTag::default(), pages: Vec::new(), }) } } // A page. #[derive(Clone, Debug)] pub struct Page { /// The page number. pub block_number: u32, /// The materialized page image, as an 8KB byte vector. pub image: Bytes, } impl From<proto::Page> for Page { fn from(pb: proto::Page) -> Self { Self { block_number: pb.block_number, image: pb.image, } } } impl From<Page> for proto::Page { fn from(page: Page) -> Self { Self { block_number: page.block_number, image: page.image, } } } /// A GetPage response status code. /// /// These are effectively equivalent to gRPC statuses. However, we use a bidirectional stream /// (potentially shared by many backends), and a gRPC status response would terminate the stream so /// we send GetPageResponse messages with these codes instead. #[derive(Clone, Copy, Debug, PartialEq, strum_macros::Display)] pub enum GetPageStatusCode { /// Unknown status. For forwards compatibility: used when an older client version receives a new /// status code from a newer server version. Unknown, /// The request was successful. Ok, /// The page did not exist. The tenant/timeline/shard has already been validated during stream /// setup. NotFound, /// The request was invalid. InvalidRequest, /// The request failed due to an internal server error. InternalError, /// The tenant is rate limited. Slow down and retry later. SlowDown, } impl From<proto::GetPageStatusCode> for GetPageStatusCode { fn from(pb: proto::GetPageStatusCode) -> Self { match pb { proto::GetPageStatusCode::Unknown => Self::Unknown, proto::GetPageStatusCode::Ok => Self::Ok, proto::GetPageStatusCode::NotFound => Self::NotFound, proto::GetPageStatusCode::InvalidRequest => Self::InvalidRequest, proto::GetPageStatusCode::InternalError => Self::InternalError, proto::GetPageStatusCode::SlowDown => Self::SlowDown, } } } impl From<i32> for GetPageStatusCode { fn from(status_code: i32) -> Self { proto::GetPageStatusCode::try_from(status_code) .unwrap_or(proto::GetPageStatusCode::Unknown) .into() } } impl From<GetPageStatusCode> for proto::GetPageStatusCode { fn from(status_code: GetPageStatusCode) -> Self { match status_code { GetPageStatusCode::Unknown => Self::Unknown, GetPageStatusCode::Ok => Self::Ok, GetPageStatusCode::NotFound => Self::NotFound, GetPageStatusCode::InvalidRequest => Self::InvalidRequest, GetPageStatusCode::InternalError => Self::InternalError, GetPageStatusCode::SlowDown => Self::SlowDown, } } } impl From<GetPageStatusCode> for i32 { fn from(status_code: GetPageStatusCode) -> Self { proto::GetPageStatusCode::from(status_code).into() } } impl TryFrom<tonic::Code> for GetPageStatusCode { type Error = tonic::Code; fn try_from(code: tonic::Code) -> Result<Self, Self::Error> { use tonic::Code; let status_code = match code { Code::Ok => Self::Ok, // These are per-request errors, which should be returned as GetPageResponses. Code::AlreadyExists => Self::InvalidRequest, Code::DataLoss => Self::InternalError, Code::FailedPrecondition => Self::InvalidRequest, Code::InvalidArgument => Self::InvalidRequest, Code::Internal => Self::InternalError, Code::NotFound => Self::NotFound, Code::OutOfRange => Self::InvalidRequest, Code::ResourceExhausted => Self::SlowDown, // These should terminate the stream by returning a tonic::Status. Code::Aborted | Code::Cancelled | Code::DeadlineExceeded | Code::PermissionDenied | Code::Unauthenticated | Code::Unavailable | Code::Unimplemented | Code::Unknown => return Err(code), }; Ok(status_code) } } impl From<GetPageStatusCode> for tonic::Code { fn from(status_code: GetPageStatusCode) -> Self { use tonic::Code; match status_code { GetPageStatusCode::Unknown => Code::Unknown, GetPageStatusCode::Ok => Code::Ok, GetPageStatusCode::NotFound => Code::NotFound, GetPageStatusCode::InvalidRequest => Code::InvalidArgument, GetPageStatusCode::InternalError => Code::Internal, GetPageStatusCode::SlowDown => Code::ResourceExhausted, } } } // Fetches the size of a relation at a given LSN, as # of blocks. Only valid on shard 0, other // shards will error. #[derive(Clone, Copy, Debug)] pub struct GetRelSizeRequest { pub read_lsn: ReadLsn, pub rel: RelTag, /// If true, return missing=true for missing relations instead of a NotFound error. pub allow_missing: bool, } impl TryFrom<proto::GetRelSizeRequest> for GetRelSizeRequest { type Error = ProtocolError; fn try_from(proto: proto::GetRelSizeRequest) -> Result<Self, Self::Error> { Ok(Self { read_lsn: proto .read_lsn .ok_or(ProtocolError::Missing("read_lsn"))? .try_into()?, rel: proto.rel.ok_or(ProtocolError::Missing("rel"))?.try_into()?, allow_missing: proto.allow_missing, }) } } impl From<GetRelSizeRequest> for proto::GetRelSizeRequest { fn from(request: GetRelSizeRequest) -> Self { Self { read_lsn: Some(request.read_lsn.into()), rel: Some(request.rel.into()), allow_missing: request.allow_missing, } } } /// The size of a relation as number of blocks, or None if `allow_missing=true` and the relation /// does not exist. /// /// INVARIANT: never None if `allow_missing=false` (returns `NotFound` error instead). pub type GetRelSizeResponse = Option<u32>; impl From<proto::GetRelSizeResponse> for GetRelSizeResponse { fn from(pb: proto::GetRelSizeResponse) -> Self { (!pb.missing).then_some(pb.num_blocks) } } impl From<GetRelSizeResponse> for proto::GetRelSizeResponse { fn from(resp: GetRelSizeResponse) -> Self { Self { num_blocks: resp.unwrap_or_default(), missing: resp.is_none(), } } } /// Requests an SLRU segment. Only valid on shard 0, other shards will error. #[derive(Clone, Copy, Debug)] pub struct GetSlruSegmentRequest { pub read_lsn: ReadLsn, pub kind: SlruKind, pub segno: u32, } impl TryFrom<proto::GetSlruSegmentRequest> for GetSlruSegmentRequest { type Error = ProtocolError; fn try_from(pb: proto::GetSlruSegmentRequest) -> Result<Self, Self::Error> { Ok(Self { read_lsn: pb .read_lsn .ok_or(ProtocolError::Missing("read_lsn"))? .try_into()?, kind: u8::try_from(pb.kind) .ok() .and_then(SlruKind::from_repr) .ok_or_else(|| ProtocolError::invalid("slru_kind", pb.kind))?, segno: pb.segno, }) } } impl From<GetSlruSegmentRequest> for proto::GetSlruSegmentRequest { fn from(request: GetSlruSegmentRequest) -> Self { Self { read_lsn: Some(request.read_lsn.into()), kind: request.kind as u32, segno: request.segno, } } } pub type GetSlruSegmentResponse = Bytes; impl TryFrom<proto::GetSlruSegmentResponse> for GetSlruSegmentResponse { type Error = ProtocolError; fn try_from(pb: proto::GetSlruSegmentResponse) -> Result<Self, Self::Error> { if pb.segment.is_empty() { return Err(ProtocolError::Missing("segment")); } Ok(pb.segment) } } impl From<GetSlruSegmentResponse> for proto::GetSlruSegmentResponse { fn from(segment: GetSlruSegmentResponse) -> Self { Self { segment } } } // SlruKind is defined in pageserver_api::reltag. pub type SlruKind = pageserver_api::reltag::SlruKind; /// Acquires or extends a lease on the given LSN. This guarantees that the Pageserver won't garbage /// collect the LSN until the lease expires. pub struct LeaseLsnRequest { /// The LSN to lease. pub lsn: Lsn, } impl TryFrom<proto::LeaseLsnRequest> for LeaseLsnRequest { type Error = ProtocolError; fn try_from(pb: proto::LeaseLsnRequest) -> Result<Self, Self::Error> { if pb.lsn == 0 { return Err(ProtocolError::Missing("lsn")); } Ok(Self { lsn: Lsn(pb.lsn) }) } } impl From<LeaseLsnRequest> for proto::LeaseLsnRequest { fn from(request: LeaseLsnRequest) -> Self { Self { lsn: request.lsn.0 } } } /// Lease expiration time. If the lease could not be granted because the LSN has already been /// garbage collected, a FailedPrecondition status will be returned instead. pub type LeaseLsnResponse = SystemTime; impl TryFrom<proto::LeaseLsnResponse> for LeaseLsnResponse { type Error = ProtocolError; fn try_from(pb: proto::LeaseLsnResponse) -> Result<Self, Self::Error> { let expires = pb.expires.ok_or(ProtocolError::Missing("expires"))?; UNIX_EPOCH .checked_add(Duration::new(expires.seconds as u64, expires.nanos as u32)) .ok_or_else(|| ProtocolError::invalid("expires", expires)) } } impl From<LeaseLsnResponse> for proto::LeaseLsnResponse { fn from(response: LeaseLsnResponse) -> Self { let expires = response.duration_since(UNIX_EPOCH).unwrap_or_default(); Self { expires: Some(prost_types::Timestamp { seconds: expires.as_secs() as i64, nanos: expires.subsec_nanos() as i32, }), } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/page_api/src/split.rs
pageserver/page_api/src/split.rs
use std::collections::HashMap; use bytes::Bytes; use crate::model::*; use pageserver_api::key::rel_block_to_key; use pageserver_api::shard::key_to_shard_number; use utils::shard::{ShardCount, ShardIndex, ShardStripeSize}; /// Splits GetPageRequests that straddle shard boundaries and assembles the responses. /// TODO: add tests for this. pub struct GetPageSplitter { /// Split requests by shard index. requests: HashMap<ShardIndex, GetPageRequest>, /// The response being assembled. Preallocated with empty pages, to be filled in. response: GetPageResponse, /// Maps the offset in `request.block_numbers` and `response.pages` to the owning shard. Used /// to assemble the response pages in the same order as the original request. block_shards: Vec<ShardIndex>, } impl GetPageSplitter { /// Checks if the given request only touches a single shard, and returns the shard ID. This is /// the common case, so we check first in order to avoid unnecessary allocations and overhead. pub fn for_single_shard( req: &GetPageRequest, count: ShardCount, stripe_size: Option<ShardStripeSize>, ) -> Result<Option<ShardIndex>, SplitError> { // Fast path: unsharded tenant. if count.is_unsharded() { return Ok(Some(ShardIndex::unsharded())); } let Some(stripe_size) = stripe_size else { return Err("stripe size must be given for sharded tenants".into()); }; // Find the first page's shard, for comparison. let Some(&first_page) = req.block_numbers.first() else { return Err("no block numbers in request".into()); }; let key = rel_block_to_key(req.rel, first_page); let shard_number = key_to_shard_number(count, stripe_size, &key); Ok(req .block_numbers .iter() .skip(1) // computed above .all(|&blkno| { let key = rel_block_to_key(req.rel, blkno); key_to_shard_number(count, stripe_size, &key) == shard_number }) .then_some(ShardIndex::new(shard_number, count))) } /// Splits the given request. pub fn split( req: GetPageRequest, count: ShardCount, stripe_size: Option<ShardStripeSize>, ) -> Result<Self, SplitError> { // The caller should make sure we don't split requests unnecessarily. debug_assert!( Self::for_single_shard(&req, count, stripe_size)?.is_none(), "unnecessary request split" ); if count.is_unsharded() { return Err("unsharded tenant, no point in splitting request".into()); } let Some(stripe_size) = stripe_size else { return Err("stripe size must be given for sharded tenants".into()); }; // Split the requests by shard index. let mut requests = HashMap::with_capacity(2); // common case let mut block_shards = Vec::with_capacity(req.block_numbers.len()); for &blkno in &req.block_numbers { let key = rel_block_to_key(req.rel, blkno); let shard_number = key_to_shard_number(count, stripe_size, &key); let shard_id = ShardIndex::new(shard_number, count); requests .entry(shard_id) .or_insert_with(|| GetPageRequest { request_id: req.request_id, request_class: req.request_class, rel: req.rel, read_lsn: req.read_lsn, block_numbers: Vec::new(), }) .block_numbers .push(blkno); block_shards.push(shard_id); } // Construct a response to be populated by shard responses. Preallocate empty page slots // with the expected block numbers. let response = GetPageResponse { request_id: req.request_id, status_code: GetPageStatusCode::Ok, reason: None, rel: req.rel, pages: req .block_numbers .into_iter() .map(|block_number| { Page { block_number, image: Bytes::new(), // empty page slot to be filled in } }) .collect(), }; Ok(Self { requests, response, block_shards, }) } /// Drains the per-shard requests, moving them out of the splitter to avoid extra allocations. pub fn drain_requests(&mut self) -> impl Iterator<Item = (ShardIndex, GetPageRequest)> { self.requests.drain() } /// Adds a response from the given shard. The response must match the request ID and have an OK /// status code. A response must not already exist for the given shard ID. pub fn add_response( &mut self, shard_id: ShardIndex, response: GetPageResponse, ) -> Result<(), SplitError> { // The caller should already have converted status codes into tonic::Status. if response.status_code != GetPageStatusCode::Ok { return Err(SplitError(format!( "unexpected non-OK response for shard {shard_id}: {} {}", response.status_code, response.reason.unwrap_or_default() ))); } if response.request_id != self.response.request_id { return Err(SplitError(format!( "response ID mismatch for shard {shard_id}: expected {}, got {}", self.response.request_id, response.request_id ))); } if response.request_id != self.response.request_id { return Err(SplitError(format!( "response ID mismatch for shard {shard_id}: expected {}, got {}", self.response.request_id, response.request_id ))); } // Place the shard response pages into the assembled response, in request order. let mut pages = response.pages.into_iter(); for (i, &s) in self.block_shards.iter().enumerate() { if shard_id != s { continue; } let Some(slot) = self.response.pages.get_mut(i) else { return Err(SplitError(format!( "no block_shards slot {i} for shard {shard_id}" ))); }; let Some(page) = pages.next() else { return Err(SplitError(format!( "missing page {} in shard {shard_id} response", slot.block_number ))); }; if page.block_number != slot.block_number { return Err(SplitError(format!( "shard {shard_id} returned wrong page at index {i}, expected {} got {}", slot.block_number, page.block_number ))); } if !slot.image.is_empty() { return Err(SplitError(format!( "shard {shard_id} returned duplicate page {} at index {i}", slot.block_number ))); } *slot = page; } // Make sure we've consumed all pages from the shard response. if let Some(extra_page) = pages.next() { return Err(SplitError(format!( "shard {shard_id} returned extra page: {}", extra_page.block_number ))); } Ok(()) } /// Collects the final, assembled response. pub fn collect_response(self) -> Result<GetPageResponse, SplitError> { // Check that the response is complete. for (i, page) in self.response.pages.iter().enumerate() { if page.image.is_empty() { return Err(SplitError(format!( "missing page {} for shard {}", page.block_number, self.block_shards .get(i) .map(|s| s.to_string()) .unwrap_or_else(|| "?".to_string()) ))); } } Ok(self.response) } } /// A GetPageSplitter error. #[derive(Debug, thiserror::Error)] #[error("{0}")] pub struct SplitError(String); impl From<&str> for SplitError { fn from(err: &str) -> Self { SplitError(err.to_string()) } } impl From<String> for SplitError { fn from(err: String) -> Self { SplitError(err) } } impl From<SplitError> for tonic::Status { fn from(err: SplitError) -> Self { tonic::Status::internal(err.0) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/config.rs
pageserver/src/config.rs
//! Functions for handling page server configuration options //! //! Configuration options can be set in the pageserver.toml configuration //! file, or on the command line. //! See also `settings.md` for better description on every parameter. pub mod ignored_fields; use std::env; use std::num::NonZeroUsize; use std::sync::Arc; use std::time::Duration; use anyhow::{Context, ensure}; use camino::{Utf8Path, Utf8PathBuf}; use once_cell::sync::OnceCell; use pageserver_api::config::{ DiskUsageEvictionTaskConfig, MaxGetVectoredKeys, MaxVectoredReadBytes, PageServicePipeliningConfig, PageServicePipeliningConfigPipelined, PostHogConfig, }; use pageserver_api::models::ImageCompressionAlgorithm; use pageserver_api::shard::TenantShardId; use pem::Pem; use postgres_backend::AuthType; use postgres_ffi::PgMajorVersion; use remote_storage::{RemotePath, RemoteStorageConfig}; use reqwest::Url; use storage_broker::Uri; use utils::id::{NodeId, TimelineId}; use utils::logging::{LogFormat, SecretString}; use crate::tenant::storage_layer::inmemory_layer::IndexEntry; use crate::tenant::{TENANTS_SEGMENT_NAME, TIMELINES_SEGMENT_NAME}; use crate::virtual_file::io_engine; use crate::{TENANT_HEATMAP_BASENAME, TENANT_LOCATION_CONFIG_NAME, virtual_file}; /// Global state of pageserver. /// /// It's mostly immutable configuration, but some semaphores and the /// like crept in over time and the name stuck. /// /// Instantiated by deserializing `pageserver.toml` into [`pageserver_api::config::ConfigToml`] /// and passing that to [`PageServerConf::parse_and_validate`]. /// /// # Adding a New Field /// /// 1. Add the field to `pageserver_api::config::ConfigToml`. /// 2. Fix compiler errors (exhaustive destructuring will guide you). /// /// For fields that require additional validation or filling in of defaults at runtime, /// check for examples in the [`PageServerConf::parse_and_validate`] method. #[derive(Debug, Clone)] pub struct PageServerConf { // Identifier of that particular pageserver so e g safekeepers // can safely distinguish different pageservers pub id: NodeId, /// Example (default): 127.0.0.1:64000 pub listen_pg_addr: String, /// Example (default): 127.0.0.1:9898 pub listen_http_addr: String, /// Example: 127.0.0.1:9899 pub listen_https_addr: Option<String>, /// If set, expose a gRPC API on this address. /// Example: 127.0.0.1:51051 /// /// EXPERIMENTAL: this protocol is unstable and under active development. pub listen_grpc_addr: Option<String>, /// Path to a file with certificate's private key for https and gRPC API. /// Default: server.key pub ssl_key_file: Utf8PathBuf, /// Path to a file with a X509 certificate for https and gRPC API. /// Default: server.crt pub ssl_cert_file: Utf8PathBuf, /// Period to reload certificate and private key from files. /// Default: 60s. pub ssl_cert_reload_period: Duration, /// Trusted root CA certificates to use in https APIs in PEM format. pub ssl_ca_certs: Vec<Pem>, /// Current availability zone. Used for traffic metrics. pub availability_zone: Option<String>, // Timeout when waiting for WAL receiver to catch up to an LSN given in a GetPage@LSN call. pub wait_lsn_timeout: Duration, // How long to wait for WAL redo to complete. pub wal_redo_timeout: Duration, pub superuser: String, pub locale: String, pub page_cache_size: usize, pub max_file_descriptors: usize, // Repository directory, relative to current working directory. // Normally, the page server changes the current working directory // to the repository, and 'workdir' is always '.'. But we don't do // that during unit testing, because the current directory is global // to the process but different unit tests work on different // repositories. pub workdir: Utf8PathBuf, pub pg_distrib_dir: Utf8PathBuf, // Authentication /// authentication method for the HTTP mgmt API pub http_auth_type: AuthType, /// authentication method for libpq connections from compute pub pg_auth_type: AuthType, /// authentication method for gRPC connections from compute pub grpc_auth_type: AuthType, /// Path to a file or directory containing public key(s) for verifying JWT tokens. /// Used for both mgmt and compute auth, if enabled. pub auth_validation_public_key_path: Option<Utf8PathBuf>, pub remote_storage_config: Option<RemoteStorageConfig>, pub default_tenant_conf: pageserver_api::config::TenantConfigToml, /// Storage broker endpoints to connect to. pub broker_endpoint: Uri, pub broker_keepalive_interval: Duration, pub log_format: LogFormat, /// Number of tenants which will be concurrently loaded from remote storage proactively on startup or attach. /// /// A lower value implicitly deprioritizes loading such tenants, vs. other work in the system. pub concurrent_tenant_warmup: ConfigurableSemaphore, /// Number of concurrent [`TenantShard::gather_size_inputs`](crate::tenant::TenantShard::gather_size_inputs) allowed. pub concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore, /// Limit of concurrent [`TenantShard::gather_size_inputs`] issued by module `eviction_task`. /// The number of permits is the same as `concurrent_tenant_size_logical_size_queries`. /// See the comment in `eviction_task` for details. /// /// [`TenantShard::gather_size_inputs`]: crate::tenant::TenantShard::gather_size_inputs pub eviction_task_immitated_concurrent_logical_size_queries: ConfigurableSemaphore, // How often to collect metrics and send them to the metrics endpoint. pub metric_collection_interval: Duration, // How often to send unchanged cached metrics to the metrics endpoint. pub metric_collection_endpoint: Option<Url>, pub metric_collection_bucket: Option<RemoteStorageConfig>, pub synthetic_size_calculation_interval: Duration, pub disk_usage_based_eviction: DiskUsageEvictionTaskConfig, // The number of allowed failures in remote storage operations. pub test_remote_failures: u64, // The probability of failure in remote storage operations. Only works when test_remote_failures > 1. // Use 100 for 100% failure, 0 for no failure. pub test_remote_failures_probability: u64, pub ondemand_download_behavior_treat_error_as_warn: bool, /// How long will background tasks be delayed at most after initial load of tenants. /// /// Our largest initialization completions are in the range of 100-200s, so perhaps 10s works /// as we now isolate initial loading, initial logical size calculation and background tasks. /// Smaller nodes will have background tasks "not running" for this long unless every timeline /// has it's initial logical size calculated. Not running background tasks for some seconds is /// not terrible. pub background_task_maximum_delay: Duration, pub control_plane_api: Url, /// JWT token for use with the control plane API. pub control_plane_api_token: Option<SecretString>, pub import_pgdata_upcall_api: Option<Url>, pub import_pgdata_upcall_api_token: Option<SecretString>, pub import_pgdata_aws_endpoint_url: Option<Url>, /// If true, pageserver will make best-effort to operate without a control plane: only /// for use in major incidents. pub control_plane_emergency_mode: bool, /// How many heatmap uploads may be done concurrency: lower values implicitly deprioritize /// heatmap uploads vs. other remote storage operations. pub heatmap_upload_concurrency: usize, /// How many remote storage downloads may be done for secondary tenants concurrently. Implicitly /// deprioritises secondary downloads vs. remote storage operations for attached tenants. pub secondary_download_concurrency: usize, /// Maximum number of WAL records to be ingested and committed at the same time pub ingest_batch_size: u64, pub virtual_file_io_engine: virtual_file::IoEngineKind, pub max_vectored_read_bytes: MaxVectoredReadBytes, /// Maximum number of keys to be read in a single get_vectored call. pub max_get_vectored_keys: MaxGetVectoredKeys, pub image_compression: ImageCompressionAlgorithm, /// Whether to offload archived timelines automatically pub timeline_offloading: bool, /// How many bytes of ephemeral layer content will we allow per kilobyte of RAM. When this /// is exceeded, we start proactively closing ephemeral layers to limit the total amount /// of ephemeral data. /// /// Setting this to zero disables limits on total ephemeral layer size. pub ephemeral_bytes_per_memory_kb: usize, pub l0_flush: crate::l0_flush::L0FlushConfig, /// Direct IO settings pub virtual_file_io_mode: virtual_file::IoMode, /// Optionally disable disk syncs (unsafe!) pub no_sync: bool, pub page_service_pipelining: pageserver_api::config::PageServicePipeliningConfig, pub get_vectored_concurrent_io: pageserver_api::config::GetVectoredConcurrentIo, /// Enable read path debugging. If enabled, read key errors will print a backtrace of the layer /// files read. pub enable_read_path_debugging: bool, /// Interpreted protocol feature: if enabled, validate that the logical WAL received from /// safekeepers does not have gaps. pub validate_wal_contiguity: bool, /// When set, the previously written to disk heatmap is loaded on tenant attach and used /// to avoid clobbering the heatmap from new, cold, attached locations. pub load_previous_heatmap: bool, /// When set, include visible layers in the next uploaded heatmaps of an unarchived timeline. pub generate_unarchival_heatmap: bool, pub tracing: Option<pageserver_api::config::Tracing>, /// Enable TLS in page service API. /// Does not force TLS: the client negotiates TLS usage during the handshake. /// Uses key and certificate from ssl_key_file/ssl_cert_file. pub enable_tls_page_service_api: bool, /// Run in development mode, which disables certain safety checks /// such as authentication requirements for HTTP and PostgreSQL APIs. /// This is insecure and should only be used in development environments. pub dev_mode: bool, /// PostHog integration config. pub posthog_config: Option<PostHogConfig>, pub timeline_import_config: pageserver_api::config::TimelineImportConfig, pub basebackup_cache_config: Option<pageserver_api::config::BasebackupCacheConfig>, /// Defines what is a big tenant for the purpose of image layer generation. /// See Timeline::should_check_if_image_layers_required pub image_layer_generation_large_timeline_threshold: Option<u64>, /// Controls whether to collect all metrics on each scrape or to return potentially stale /// results. pub force_metric_collection_on_scrape: bool, } /// Token for authentication to safekeepers /// /// We do not want to store this in a PageServerConf because the latter may be logged /// and/or serialized at a whim, while the token is secret. Currently this token is the /// same for accessing all tenants/timelines, but may become per-tenant/per-timeline in /// the future, more tokens and auth may arrive for storage broker, completely changing the logic. /// Hence, we resort to a global variable for now instead of passing the token from the /// startup code to the connection code through a dozen layers. pub static SAFEKEEPER_AUTH_TOKEN: OnceCell<Arc<String>> = OnceCell::new(); impl PageServerConf { // // Repository paths, relative to workdir. // pub fn tenants_path(&self) -> Utf8PathBuf { self.workdir.join(TENANTS_SEGMENT_NAME) } pub fn deletion_prefix(&self) -> Utf8PathBuf { self.workdir.join("deletion") } pub fn metadata_path(&self) -> Utf8PathBuf { self.workdir.join("metadata.json") } pub fn basebackup_cache_dir(&self) -> Utf8PathBuf { self.workdir.join("basebackup_cache") } pub fn deletion_list_path(&self, sequence: u64) -> Utf8PathBuf { // Encode a version in the filename, so that if we ever switch away from JSON we can // increment this. const VERSION: u8 = 1; self.deletion_prefix() .join(format!("{sequence:016x}-{VERSION:02x}.list")) } pub fn deletion_header_path(&self) -> Utf8PathBuf { // Encode a version in the filename, so that if we ever switch away from JSON we can // increment this. const VERSION: u8 = 1; self.deletion_prefix().join(format!("header-{VERSION:02x}")) } pub fn tenant_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf { self.tenants_path().join(tenant_shard_id.to_string()) } /// Points to a place in pageserver's local directory, /// where certain tenant's LocationConf be stored. pub(crate) fn tenant_location_config_path( &self, tenant_shard_id: &TenantShardId, ) -> Utf8PathBuf { self.tenant_path(tenant_shard_id) .join(TENANT_LOCATION_CONFIG_NAME) } pub(crate) fn tenant_heatmap_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf { self.tenant_path(tenant_shard_id) .join(TENANT_HEATMAP_BASENAME) } pub fn timelines_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf { self.tenant_path(tenant_shard_id) .join(TIMELINES_SEGMENT_NAME) } pub fn timeline_path( &self, tenant_shard_id: &TenantShardId, timeline_id: &TimelineId, ) -> Utf8PathBuf { self.timelines_path(tenant_shard_id) .join(timeline_id.to_string()) } /// Turns storage remote path of a file into its local path. pub fn local_path(&self, remote_path: &RemotePath) -> Utf8PathBuf { remote_path.with_base(&self.workdir) } // // Postgres distribution paths // pub fn pg_distrib_dir(&self, pg_version: PgMajorVersion) -> anyhow::Result<Utf8PathBuf> { let path = self.pg_distrib_dir.clone(); Ok(path.join(pg_version.v_str())) } pub fn pg_bin_dir(&self, pg_version: PgMajorVersion) -> anyhow::Result<Utf8PathBuf> { Ok(self.pg_distrib_dir(pg_version)?.join("bin")) } pub fn pg_lib_dir(&self, pg_version: PgMajorVersion) -> anyhow::Result<Utf8PathBuf> { Ok(self.pg_distrib_dir(pg_version)?.join("lib")) } /// Parse a configuration file (pageserver.toml) into a PageServerConf struct, /// validating the input and failing on errors. /// /// This leaves any options not present in the file in the built-in defaults. pub fn parse_and_validate( id: NodeId, config_toml: pageserver_api::config::ConfigToml, workdir: &Utf8Path, ) -> anyhow::Result<Self> { let pageserver_api::config::ConfigToml { listen_pg_addr, listen_http_addr, listen_https_addr, listen_grpc_addr, ssl_key_file, ssl_cert_file, ssl_cert_reload_period, ssl_ca_file, availability_zone, wait_lsn_timeout, wal_redo_timeout, superuser, locale, page_cache_size, max_file_descriptors, pg_distrib_dir, http_auth_type, pg_auth_type, grpc_auth_type, auth_validation_public_key_path, remote_storage, broker_endpoint, broker_keepalive_interval, log_format, metric_collection_interval, metric_collection_endpoint, metric_collection_bucket, synthetic_size_calculation_interval, disk_usage_based_eviction, test_remote_failures, test_remote_failures_probability, ondemand_download_behavior_treat_error_as_warn, background_task_maximum_delay, control_plane_api, control_plane_api_token, control_plane_emergency_mode, import_pgdata_upcall_api, import_pgdata_upcall_api_token, import_pgdata_aws_endpoint_url, heatmap_upload_concurrency, secondary_download_concurrency, ingest_batch_size, max_vectored_read_bytes, max_get_vectored_keys, image_compression, timeline_offloading, ephemeral_bytes_per_memory_kb, l0_flush, virtual_file_io_mode, concurrent_tenant_warmup, concurrent_tenant_size_logical_size_queries, virtual_file_io_engine, tenant_config, no_sync, page_service_pipelining, get_vectored_concurrent_io, enable_read_path_debugging, validate_wal_contiguity, load_previous_heatmap, generate_unarchival_heatmap, tracing, enable_tls_page_service_api, dev_mode, posthog_config, timeline_import_config, basebackup_cache_config, image_layer_generation_large_timeline_threshold, force_metric_collection_on_scrape, } = config_toml; let mut conf = PageServerConf { // ------------------------------------------------------------ // fields that are already fully validated by the ConfigToml Deserialize impl // ------------------------------------------------------------ listen_pg_addr, listen_http_addr, listen_https_addr, listen_grpc_addr, ssl_key_file, ssl_cert_file, ssl_cert_reload_period, availability_zone, wait_lsn_timeout, wal_redo_timeout, superuser, locale, page_cache_size, max_file_descriptors, http_auth_type, pg_auth_type, grpc_auth_type, auth_validation_public_key_path, remote_storage_config: remote_storage, broker_endpoint, broker_keepalive_interval, log_format, metric_collection_interval, metric_collection_endpoint, metric_collection_bucket, synthetic_size_calculation_interval, disk_usage_based_eviction, test_remote_failures, test_remote_failures_probability, ondemand_download_behavior_treat_error_as_warn, background_task_maximum_delay, control_plane_api: control_plane_api .ok_or_else(|| anyhow::anyhow!("`control_plane_api` must be set"))?, control_plane_emergency_mode, heatmap_upload_concurrency, secondary_download_concurrency, ingest_batch_size, max_vectored_read_bytes, max_get_vectored_keys, image_compression, timeline_offloading, ephemeral_bytes_per_memory_kb, import_pgdata_upcall_api, import_pgdata_upcall_api_token: import_pgdata_upcall_api_token.map(SecretString::from), import_pgdata_aws_endpoint_url, page_service_pipelining, get_vectored_concurrent_io, tracing, enable_tls_page_service_api, dev_mode, timeline_import_config, basebackup_cache_config, image_layer_generation_large_timeline_threshold, force_metric_collection_on_scrape, // ------------------------------------------------------------ // fields that require additional validation or custom handling // ------------------------------------------------------------ workdir: workdir.to_owned(), pg_distrib_dir: pg_distrib_dir.unwrap_or_else(|| { std::env::current_dir() .expect("current_dir() failed") .try_into() .expect("current_dir() is not a valid Utf8Path") }), control_plane_api_token: control_plane_api_token.map(SecretString::from), id, default_tenant_conf: tenant_config, concurrent_tenant_warmup: ConfigurableSemaphore::new(concurrent_tenant_warmup), concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore::new( concurrent_tenant_size_logical_size_queries, ), eviction_task_immitated_concurrent_logical_size_queries: ConfigurableSemaphore::new( // re-use `concurrent_tenant_size_logical_size_queries` concurrent_tenant_size_logical_size_queries, ), virtual_file_io_engine: match virtual_file_io_engine { Some(v) => v, None => match crate::virtual_file::io_engine_feature_test() .context("auto-detect virtual_file_io_engine")? { io_engine::FeatureTestResult::PlatformPreferred(v) => v, // make no noise io_engine::FeatureTestResult::Worse { engine, remark } => { // TODO: bubble this up to the caller so we can tracing::warn! it. eprintln!( "auto-detected IO engine is not platform-preferred: engine={engine:?} remark={remark:?}" ); engine } }, }, l0_flush: l0_flush .map(crate::l0_flush::L0FlushConfig::from) .unwrap_or_default(), virtual_file_io_mode: virtual_file_io_mode.unwrap_or(virtual_file::IoMode::preferred()), no_sync: no_sync.unwrap_or(false), enable_read_path_debugging: enable_read_path_debugging.unwrap_or(false), validate_wal_contiguity: validate_wal_contiguity.unwrap_or(false), load_previous_heatmap: load_previous_heatmap.unwrap_or(true), generate_unarchival_heatmap: generate_unarchival_heatmap.unwrap_or(true), ssl_ca_certs: match ssl_ca_file { Some(ssl_ca_file) => { let buf = std::fs::read(ssl_ca_file)?; pem::parse_many(&buf)? .into_iter() .filter(|pem| pem.tag() == "CERTIFICATE") .collect() } None => Vec::new(), }, posthog_config, }; // ------------------------------------------------------------ // custom validation code that covers more than one field in isolation // ------------------------------------------------------------ if [conf.http_auth_type, conf.pg_auth_type, conf.grpc_auth_type] .contains(&AuthType::NeonJWT) { let auth_validation_public_key_path = conf .auth_validation_public_key_path .get_or_insert_with(|| workdir.join("auth_public_key.pem")); ensure!( auth_validation_public_key_path.exists(), format!( "Can't find auth_validation_public_key at '{auth_validation_public_key_path}'", ) ); } if let Some(tracing_config) = conf.tracing.as_ref() { let ratio = &tracing_config.sampling_ratio; ensure!( ratio.denominator != 0 && ratio.denominator >= ratio.numerator, format!( "Invalid sampling ratio: {}/{}", ratio.numerator, ratio.denominator ) ); let url = Url::parse(&tracing_config.export_config.endpoint) .map_err(anyhow::Error::msg) .with_context(|| { format!( "tracing endpoint URL is invalid : {}", tracing_config.export_config.endpoint ) })?; ensure!( url.scheme() == "http" || url.scheme() == "https", format!( "tracing endpoint URL must start with http:// or https://: {}", tracing_config.export_config.endpoint ) ); } IndexEntry::validate_checkpoint_distance(conf.default_tenant_conf.checkpoint_distance) .map_err(anyhow::Error::msg) .with_context(|| { format!( "effective checkpoint distance is unsupported: {}", conf.default_tenant_conf.checkpoint_distance ) })?; if let PageServicePipeliningConfig::Pipelined(PageServicePipeliningConfigPipelined { max_batch_size, .. }) = conf.page_service_pipelining { if max_batch_size.get() > conf.max_get_vectored_keys.get() { return Err(anyhow::anyhow!( "`max_batch_size` ({max_batch_size}) must be less than or equal to `max_get_vectored_keys` ({})", conf.max_get_vectored_keys.get() )); } }; Ok(conf) } #[cfg(test)] pub fn test_repo_dir(test_name: &str) -> Utf8PathBuf { let test_output_dir = std::env::var("TEST_OUTPUT").unwrap_or("../tmp_check".into()); let test_id = uuid::Uuid::new_v4(); Utf8PathBuf::from(format!("{test_output_dir}/test_{test_name}_{test_id}")) } pub fn dummy_conf(repo_dir: Utf8PathBuf) -> Self { let pg_distrib_dir = Utf8PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../pg_install"); let mut config_toml = pageserver_api::config::ConfigToml { wait_lsn_timeout: Duration::from_secs(60), wal_redo_timeout: Duration::from_secs(60), pg_distrib_dir: Some(pg_distrib_dir), metric_collection_interval: Duration::from_secs(60), synthetic_size_calculation_interval: Duration::from_secs(60), background_task_maximum_delay: Duration::ZERO, load_previous_heatmap: Some(true), generate_unarchival_heatmap: Some(true), control_plane_api: Some(Url::parse("http://localhost:6666").unwrap()), ..Default::default() }; // Test authors tend to forget about the default 10min initial lease deadline // when writing tests, which turns their immediate gc requests via mgmt API // into no-ops. Override the binary default here, such that there is no initial // lease deadline by default in tests. Tests that care can always override it // themselves. // Cf https://databricks.atlassian.net/browse/LKB-92?focusedCommentId=6722329 config_toml.tenant_config.lsn_lease_length = Duration::from_secs(0); PageServerConf::parse_and_validate(NodeId(0), config_toml, &repo_dir).unwrap() } } #[derive(serde::Deserialize, serde::Serialize)] pub struct PageserverIdentity { pub id: NodeId, } /// Configurable semaphore permits setting. /// /// Does not allow semaphore permits to be zero, because at runtime initially zero permits and empty /// semaphore cannot be distinguished, leading any feature using these to await forever (or until /// new permits are added). #[derive(Debug, Clone)] pub struct ConfigurableSemaphore { initial_permits: NonZeroUsize, inner: std::sync::Arc<tokio::sync::Semaphore>, } impl ConfigurableSemaphore { /// Initializse using a non-zero amount of permits. /// /// Require a non-zero initial permits, because using permits == 0 is a crude way to disable a /// feature such as [`TenantShard::gather_size_inputs`]. Otherwise any semaphore using future will /// behave like [`futures::future::pending`], just waiting until new permits are added. /// /// [`TenantShard::gather_size_inputs`]: crate::tenant::TenantShard::gather_size_inputs pub fn new(initial_permits: NonZeroUsize) -> Self { ConfigurableSemaphore { initial_permits, inner: std::sync::Arc::new(tokio::sync::Semaphore::new(initial_permits.get())), } } /// Returns the configured amount of permits. pub fn initial_permits(&self) -> NonZeroUsize { self.initial_permits } } impl PartialEq for ConfigurableSemaphore { fn eq(&self, other: &Self) -> bool { // the number of permits can be increased at runtime, so we cannot really fulfill the // PartialEq value equality otherwise self.initial_permits == other.initial_permits } } impl Eq for ConfigurableSemaphore {} impl ConfigurableSemaphore { pub fn inner(&self) -> &std::sync::Arc<tokio::sync::Semaphore> { &self.inner } } #[cfg(test)] mod tests { use std::time::Duration; use camino::Utf8PathBuf; use pageserver_api::config::{DiskUsageEvictionTaskConfig, EvictionOrder}; use rstest::rstest; use utils::{id::NodeId, serde_percent::Percent}; use super::PageServerConf; #[test] fn test_minimal_config_toml_is_valid() { // The minimal valid config for running a pageserver: // - control_plane_api is mandatory, as pageservers cannot run in isolation // - we use Default impl of everything else in this situation let input = r#" control_plane_api = "http://localhost:6666" "#; let config_toml = toml_edit::de::from_str::<pageserver_api::config::ConfigToml>(input) .expect("empty config is valid"); let workdir = Utf8PathBuf::from("/nonexistent"); PageServerConf::parse_and_validate(NodeId(0), config_toml, &workdir) .expect("parse_and_validate"); } #[test] fn test_config_tracing_endpoint_is_invalid() { let input = r#" control_plane_api = "http://localhost:6666" [tracing] sampling_ratio = { numerator = 1, denominator = 0 } [tracing.export_config] endpoint = "localhost:4317" protocol = "http-binary" timeout = "1ms" "#; let config_toml = toml_edit::de::from_str::<pageserver_api::config::ConfigToml>(input) .expect("config has valid fields"); let workdir = Utf8PathBuf::from("/nonexistent"); PageServerConf::parse_and_validate(NodeId(0), config_toml, &workdir) .expect_err("parse_and_validate should fail for endpoint without scheme"); } #[rstest] #[case(32, 32, true)] #[case(64, 32, false)] #[case(64, 64, true)] #[case(128, 128, true)] fn test_config_max_batch_size_is_valid( #[case] max_batch_size: usize, #[case] max_get_vectored_keys: usize, #[case] is_valid: bool, ) { let input = format!( r#" control_plane_api = "http://localhost:6666" max_get_vectored_keys = {max_get_vectored_keys} page_service_pipelining = {{ mode="pipelined", execution="concurrent-futures", max_batch_size={max_batch_size}, batching="uniform-lsn" }} "#, ); let config_toml = toml_edit::de::from_str::<pageserver_api::config::ConfigToml>(&input) .expect("config has valid fields"); let workdir = Utf8PathBuf::from("/nonexistent"); let result = PageServerConf::parse_and_validate(NodeId(0), config_toml, &workdir); assert_eq!(result.is_ok(), is_valid); } #[test] fn test_config_posthog_config_is_valid() { let input = r#" control_plane_api = "http://localhost:6666" [posthog_config] server_api_key = "phs_AAA" client_api_key = "phc_BBB" project_id = "000" private_api_url = "https://us.posthog.com" public_api_url = "https://us.i.posthog.com" "#; let config_toml = toml_edit::de::from_str::<pageserver_api::config::ConfigToml>(input) .expect("posthogconfig is valid"); let workdir = Utf8PathBuf::from("/nonexistent"); PageServerConf::parse_and_validate(NodeId(0), config_toml, &workdir) .expect("parse_and_validate"); } #[test] fn test_config_posthog_incomplete_config_is_valid() { let input = r#" control_plane_api = "http://localhost:6666" [posthog_config] server_api_key = "phs_AAA" private_api_url = "https://us.posthog.com"
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/tenant.rs
pageserver/src/tenant.rs
//! Timeline repository implementation that keeps old data in layer files, and //! the recent changes in ephemeral files. //! //! See tenant/*_layer.rs files. The functions here are responsible for locating //! the correct layer for the get/put call, walking back the timeline branching //! history as needed. //! //! The files are stored in the .neon/tenants/<tenant_id>/timelines/<timeline_id> //! directory. See docs/pageserver-storage.md for how the files are managed. //! In addition to the layer files, there is a metadata file in the same //! directory that contains information about the timeline, in particular its //! parent timeline, and the last LSN that has been written to disk. //! use std::collections::hash_map::Entry; use std::collections::{BTreeMap, HashMap, HashSet}; use std::fmt::{Debug, Display}; use std::fs::File; use std::future::Future; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex, Weak}; use std::time::{Duration, Instant, SystemTime}; use std::{fmt, fs}; use anyhow::{Context, bail}; use arc_swap::ArcSwap; use camino::{Utf8Path, Utf8PathBuf}; use chrono::NaiveDateTime; use enumset::EnumSet; use futures::StreamExt; use futures::stream::FuturesUnordered; use itertools::Itertools as _; use once_cell::sync::Lazy; pub use pageserver_api::models::TenantState; use pageserver_api::models::{self, RelSizeMigration}; use pageserver_api::models::{ CompactInfoResponse, TimelineArchivalState, TimelineState, TopTenantShardItem, WalRedoManagerStatus, }; use pageserver_api::shard::{ShardIdentity, ShardStripeSize, TenantShardId}; use postgres_ffi::PgMajorVersion; use remote_storage::{DownloadError, GenericRemoteStorage, TimeoutOrCancel}; use remote_timeline_client::index::GcCompactionState; use remote_timeline_client::manifest::{ LATEST_TENANT_MANIFEST_VERSION, OffloadedTimelineManifest, TenantManifest, }; use remote_timeline_client::{ FAILED_REMOTE_OP_RETRIES, FAILED_UPLOAD_WARN_THRESHOLD, UploadQueueNotReadyError, download_tenant_manifest, }; use secondary::heatmap::{HeatMapTenant, HeatMapTimeline}; use storage_broker::BrokerClientChannel; use timeline::compaction::{CompactionOutcome, GcCompactionQueue}; use timeline::import_pgdata::ImportingTimeline; use timeline::layer_manager::LayerManagerLockHolder; use timeline::offload::{OffloadError, offload_timeline}; use timeline::{ CompactFlags, CompactOptions, CompactionError, PreviousHeatmap, ShutdownMode, import_pgdata, }; use tokio::io::BufReader; use tokio::sync::{Notify, Semaphore, watch}; use tokio::task::JoinSet; use tokio_util::sync::CancellationToken; use tracing::*; use upload_queue::NotInitialized; use utils::circuit_breaker::CircuitBreaker; use utils::crashsafe::path_with_suffix_extension; use utils::sync::gate::{Gate, GateGuard}; use utils::timeout::{TimeoutCancellableError, timeout_cancellable}; use utils::try_rcu::ArcSwapExt; use utils::zstd::{create_zst_tarball, extract_zst_tarball}; use utils::{backoff, completion, failpoint_support, fs_ext, pausable_failpoint}; use self::config::{AttachedLocationConfig, AttachmentMode, LocationConf}; use self::metadata::TimelineMetadata; use self::mgr::{GetActiveTenantError, GetTenantError}; use self::remote_timeline_client::upload::{upload_index_part, upload_tenant_manifest}; use self::remote_timeline_client::{RemoteTimelineClient, WaitCompletionError}; use self::timeline::uninit::{TimelineCreateGuard, TimelineExclusionError, UninitializedTimeline}; use self::timeline::{ EvictionTaskTenantState, GcCutoffs, TimelineDeleteProgress, TimelineResources, WaitLsnError, }; use crate::basebackup_cache::BasebackupCache; use crate::config::PageServerConf; use crate::context; use crate::context::RequestContextBuilder; use crate::context::{DownloadBehavior, RequestContext}; use crate::deletion_queue::{DeletionQueueClient, DeletionQueueError}; use crate::feature_resolver::{FeatureResolver, TenantFeatureResolver}; use crate::l0_flush::L0FlushGlobalState; use crate::metrics::{ BROKEN_TENANTS_SET, CIRCUIT_BREAKERS_BROKEN, CIRCUIT_BREAKERS_UNBROKEN, CONCURRENT_INITDBS, INITDB_RUN_TIME, INITDB_SEMAPHORE_ACQUISITION_TIME, TENANT, TENANT_OFFLOADED_TIMELINES, TENANT_STATE_METRIC, TENANT_SYNTHETIC_SIZE_METRIC, TIMELINE_STATE_METRIC, remove_tenant_metrics, }; use crate::task_mgr::TaskKind; use crate::tenant::config::LocationMode; use crate::tenant::gc_result::GcResult; pub use crate::tenant::remote_timeline_client::index::IndexPart; use crate::tenant::remote_timeline_client::{ INITDB_PATH, MaybeDeletedIndexPart, remote_initdb_archive_path, }; use crate::tenant::storage_layer::{DeltaLayer, ImageLayer}; use crate::tenant::timeline::delete::DeleteTimelineFlow; use crate::tenant::timeline::uninit::cleanup_timeline_directory; use crate::virtual_file::VirtualFile; use crate::walingest::WalLagCooldown; use crate::walredo::{PostgresRedoManager, RedoAttemptType}; use crate::{InitializationOrder, TEMP_FILE_SUFFIX, import_datadir, span, task_mgr, walredo}; static INIT_DB_SEMAPHORE: Lazy<Semaphore> = Lazy::new(|| Semaphore::new(8)); use utils::crashsafe; use utils::generation::Generation; use utils::id::TimelineId; use utils::lsn::{Lsn, RecordLsn}; pub mod blob_io; pub mod block_io; pub mod vectored_blob_io; pub mod disk_btree; pub(crate) mod ephemeral_file; pub mod layer_map; pub mod metadata; pub mod remote_timeline_client; pub mod storage_layer; pub mod checks; pub mod config; pub mod mgr; pub mod secondary; pub mod tasks; pub mod upload_queue; pub(crate) mod timeline; pub mod size; mod gc_block; mod gc_result; pub(crate) mod throttle; #[cfg(test)] pub mod debug; pub(crate) use timeline::{LogicalSizeCalculationCause, PageReconstructError, Timeline}; pub(crate) use crate::span::debug_assert_current_span_has_tenant_and_timeline_id; // re-export for use in walreceiver pub use crate::tenant::timeline::WalReceiverInfo; /// The "tenants" part of `tenants/<tenant>/timelines...` pub const TENANTS_SEGMENT_NAME: &str = "tenants"; /// Parts of the `.neon/tenants/<tenant_id>/timelines/<timeline_id>` directory prefix. pub const TIMELINES_SEGMENT_NAME: &str = "timelines"; /// References to shared objects that are passed into each tenant, such /// as the shared remote storage client and process initialization state. #[derive(Clone)] pub struct TenantSharedResources { pub broker_client: storage_broker::BrokerClientChannel, pub remote_storage: GenericRemoteStorage, pub deletion_queue_client: DeletionQueueClient, pub l0_flush_global_state: L0FlushGlobalState, pub basebackup_cache: Arc<BasebackupCache>, pub feature_resolver: FeatureResolver, } /// A [`TenantShard`] is really an _attached_ tenant. The configuration /// for an attached tenant is a subset of the [`LocationConf`], represented /// in this struct. #[derive(Clone)] pub(super) struct AttachedTenantConf { tenant_conf: pageserver_api::models::TenantConfig, location: AttachedLocationConfig, /// The deadline before which we are blocked from GC so that /// leases have a chance to be renewed. lsn_lease_deadline: Option<tokio::time::Instant>, } impl AttachedTenantConf { fn new( conf: &'static PageServerConf, tenant_conf: pageserver_api::models::TenantConfig, location: AttachedLocationConfig, ) -> Self { // Sets a deadline before which we cannot proceed to GC due to lsn lease. // // We do this as the leases mapping are not persisted to disk. By delaying GC by lease // length, we guarantee that all the leases we granted before will have a chance to renew // when we run GC for the first time after restart / transition from AttachedMulti to AttachedSingle. let lsn_lease_deadline = if location.attach_mode == AttachmentMode::Single { Some( tokio::time::Instant::now() + TenantShard::get_lsn_lease_length_impl(conf, &tenant_conf), ) } else { // We don't use `lsn_lease_deadline` to delay GC in AttachedMulti and AttachedStale // because we don't do GC in these modes. None }; Self { tenant_conf, location, lsn_lease_deadline, } } fn try_from( conf: &'static PageServerConf, location_conf: LocationConf, ) -> anyhow::Result<Self> { match &location_conf.mode { LocationMode::Attached(attach_conf) => { Ok(Self::new(conf, location_conf.tenant_conf, *attach_conf)) } LocationMode::Secondary(_) => { anyhow::bail!( "Attempted to construct AttachedTenantConf from a LocationConf in secondary mode" ) } } } fn is_gc_blocked_by_lsn_lease_deadline(&self) -> bool { self.lsn_lease_deadline .map(|d| tokio::time::Instant::now() < d) .unwrap_or(false) } } struct TimelinePreload { timeline_id: TimelineId, client: RemoteTimelineClient, index_part: Result<MaybeDeletedIndexPart, DownloadError>, previous_heatmap: Option<PreviousHeatmap>, } pub(crate) struct TenantPreload { /// The tenant manifest from remote storage, or None if no manifest was found. tenant_manifest: Option<TenantManifest>, /// Map from timeline ID to a possible timeline preload. It is None iff the timeline is offloaded according to the manifest. timelines: HashMap<TimelineId, Option<TimelinePreload>>, } /// When we spawn a tenant, there is a special mode for tenant creation that /// avoids trying to read anything from remote storage. pub(crate) enum SpawnMode { /// Activate as soon as possible Eager, /// Lazy activation in the background, with the option to skip the queue if the need comes up Lazy, } /// /// Tenant consists of multiple timelines. Keep them in a hash table. /// pub struct TenantShard { // Global pageserver config parameters pub conf: &'static PageServerConf, /// The value creation timestamp, used to measure activation delay, see: /// <https://github.com/neondatabase/neon/issues/4025> constructed_at: Instant, state: watch::Sender<TenantState>, // Overridden tenant-specific config parameters. // We keep pageserver_api::models::TenantConfig sturct here to preserve the information // about parameters that are not set. // This is necessary to allow global config updates. tenant_conf: Arc<ArcSwap<AttachedTenantConf>>, tenant_shard_id: TenantShardId, // The detailed sharding information, beyond the number/count in tenant_shard_id shard_identity: ShardIdentity, /// The remote storage generation, used to protect S3 objects from split-brain. /// Does not change over the lifetime of the [`TenantShard`] object. /// /// This duplicates the generation stored in LocationConf, but that structure is mutable: /// this copy enforces the invariant that generatio doesn't change during a Tenant's lifetime. generation: Generation, timelines: Mutex<HashMap<TimelineId, Arc<Timeline>>>, /// During timeline creation, we first insert the TimelineId to the /// creating map, then `timelines`, then remove it from the creating map. /// **Lock order**: if acquiring all (or a subset), acquire them in order `timelines`, `timelines_offloaded`, `timelines_creating` timelines_creating: std::sync::Mutex<HashSet<TimelineId>>, /// Possibly offloaded and archived timelines /// **Lock order**: if acquiring all (or a subset), acquire them in order `timelines`, `timelines_offloaded`, `timelines_creating` timelines_offloaded: Mutex<HashMap<TimelineId, Arc<OffloadedTimeline>>>, /// Tracks the timelines that are currently importing into this tenant shard. /// /// Note that importing timelines are also present in [`Self::timelines_creating`]. /// Keep this in mind when ordering lock acquisition. /// /// Lifetime: /// * An imported timeline is created while scanning the bucket on tenant attach /// if the index part contains an `import_pgdata` entry and said field marks the import /// as in progress. /// * Imported timelines are removed when the storage controller calls the post timeline /// import activation endpoint. timelines_importing: std::sync::Mutex<HashMap<TimelineId, Arc<ImportingTimeline>>>, /// The last tenant manifest known to be in remote storage. None if the manifest has not yet /// been either downloaded or uploaded. Always Some after tenant attach. /// /// Initially populated during tenant attach, updated via `maybe_upload_tenant_manifest`. /// /// Do not modify this directly. It is used to check whether a new manifest needs to be /// uploaded. The manifest is constructed in `build_tenant_manifest`, and uploaded via /// `maybe_upload_tenant_manifest`. remote_tenant_manifest: tokio::sync::Mutex<Option<TenantManifest>>, // This mutex prevents creation of new timelines during GC. // Adding yet another mutex (in addition to `timelines`) is needed because holding // `timelines` mutex during all GC iteration // may block for a long time `get_timeline`, `get_timelines_state`,... and other operations // with timelines, which in turn may cause dropping replication connection, expiration of wait_for_lsn // timeout... gc_cs: tokio::sync::Mutex<()>, walredo_mgr: Option<Arc<WalRedoManager>>, /// Provides access to timeline data sitting in the remote storage. pub(crate) remote_storage: GenericRemoteStorage, /// Access to global deletion queue for when this tenant wants to schedule a deletion. deletion_queue_client: DeletionQueueClient, /// A channel to send async requests to prepare a basebackup for the basebackup cache. basebackup_cache: Arc<BasebackupCache>, /// Cached logical sizes updated updated on each [`TenantShard::gather_size_inputs`]. cached_logical_sizes: tokio::sync::Mutex<HashMap<(TimelineId, Lsn), u64>>, cached_synthetic_tenant_size: Arc<AtomicU64>, eviction_task_tenant_state: tokio::sync::Mutex<EvictionTaskTenantState>, /// Track repeated failures to compact, so that we can back off. /// Overhead of mutex is acceptable because compaction is done with a multi-second period. compaction_circuit_breaker: std::sync::Mutex<CircuitBreaker>, /// Signals the tenant compaction loop that there is L0 compaction work to be done. pub(crate) l0_compaction_trigger: Arc<Notify>, /// Scheduled gc-compaction tasks. scheduled_compaction_tasks: std::sync::Mutex<HashMap<TimelineId, Arc<GcCompactionQueue>>>, /// If the tenant is in Activating state, notify this to encourage it /// to proceed to Active as soon as possible, rather than waiting for lazy /// background warmup. pub(crate) activate_now_sem: tokio::sync::Semaphore, /// Time it took for the tenant to activate. Zero if not active yet. attach_wal_lag_cooldown: Arc<std::sync::OnceLock<WalLagCooldown>>, // Cancellation token fires when we have entered shutdown(). This is a parent of // Timelines' cancellation token. pub(crate) cancel: CancellationToken, // Users of the TenantShard such as the page service must take this Gate to avoid // trying to use a TenantShard which is shutting down. pub(crate) gate: Gate, /// Throttle applied at the top of [`Timeline::get`]. /// All [`TenantShard::timelines`] of a given [`TenantShard`] instance share the same [`throttle::Throttle`] instance. pub(crate) pagestream_throttle: Arc<throttle::Throttle>, pub(crate) pagestream_throttle_metrics: Arc<crate::metrics::tenant_throttling::Pagestream>, /// An ongoing timeline detach concurrency limiter. /// /// As a tenant will likely be restarted as part of timeline detach ancestor it makes no sense /// to have two running at the same time. A different one can be started if an earlier one /// has failed for whatever reason. ongoing_timeline_detach: std::sync::Mutex<Option<(TimelineId, utils::completion::Barrier)>>, /// `index_part.json` based gc blocking reason tracking. /// /// New gc iterations must start a new iteration by acquiring `GcBlock::start` before /// proceeding. pub(crate) gc_block: gc_block::GcBlock, l0_flush_global_state: L0FlushGlobalState, pub(crate) feature_resolver: Arc<TenantFeatureResolver>, } impl std::fmt::Debug for TenantShard { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{} ({})", self.tenant_shard_id, self.current_state()) } } pub(crate) enum WalRedoManager { Prod(WalredoManagerId, PostgresRedoManager), #[cfg(test)] Test(harness::TestRedoManager), } #[derive(thiserror::Error, Debug)] #[error("pageserver is shutting down")] pub(crate) struct GlobalShutDown; impl WalRedoManager { pub(crate) fn new(mgr: PostgresRedoManager) -> Result<Arc<Self>, GlobalShutDown> { let id = WalredoManagerId::next(); let arc = Arc::new(Self::Prod(id, mgr)); let mut guard = WALREDO_MANAGERS.lock().unwrap(); match &mut *guard { Some(map) => { map.insert(id, Arc::downgrade(&arc)); Ok(arc) } None => Err(GlobalShutDown), } } } impl Drop for WalRedoManager { fn drop(&mut self) { match self { Self::Prod(id, _) => { let mut guard = WALREDO_MANAGERS.lock().unwrap(); if let Some(map) = &mut *guard { map.remove(id).expect("new() registers, drop() unregisters"); } } #[cfg(test)] Self::Test(_) => { // Not applicable to test redo manager } } } } /// Global registry of all walredo managers so that [`crate::shutdown_pageserver`] can shut down /// the walredo processes outside of the regular order. /// /// This is necessary to work around a systemd bug where it freezes if there are /// walredo processes left => <https://github.com/neondatabase/cloud/issues/11387> #[allow(clippy::type_complexity)] pub(crate) static WALREDO_MANAGERS: once_cell::sync::Lazy< Mutex<Option<HashMap<WalredoManagerId, Weak<WalRedoManager>>>>, > = once_cell::sync::Lazy::new(|| Mutex::new(Some(HashMap::new()))); #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] pub(crate) struct WalredoManagerId(u64); impl WalredoManagerId { pub fn next() -> Self { static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); let id = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); if id == 0 { panic!( "WalredoManagerId::new() returned 0, indicating wraparound, risking it's no longer unique" ); } Self(id) } } #[cfg(test)] impl From<harness::TestRedoManager> for WalRedoManager { fn from(mgr: harness::TestRedoManager) -> Self { Self::Test(mgr) } } impl WalRedoManager { pub(crate) async fn shutdown(&self) -> bool { match self { Self::Prod(_, mgr) => mgr.shutdown().await, #[cfg(test)] Self::Test(_) => { // Not applicable to test redo manager true } } } pub(crate) fn maybe_quiesce(&self, idle_timeout: Duration) { match self { Self::Prod(_, mgr) => mgr.maybe_quiesce(idle_timeout), #[cfg(test)] Self::Test(_) => { // Not applicable to test redo manager } } } /// # Cancel-Safety /// /// This method is cancellation-safe. pub async fn request_redo( &self, key: pageserver_api::key::Key, lsn: Lsn, base_img: Option<(Lsn, bytes::Bytes)>, records: Vec<(Lsn, wal_decoder::models::record::NeonWalRecord)>, pg_version: PgMajorVersion, redo_attempt_type: RedoAttemptType, ) -> Result<bytes::Bytes, walredo::Error> { match self { Self::Prod(_, mgr) => { mgr.request_redo(key, lsn, base_img, records, pg_version, redo_attempt_type) .await } #[cfg(test)] Self::Test(mgr) => { mgr.request_redo(key, lsn, base_img, records, pg_version, redo_attempt_type) .await } } } pub(crate) fn status(&self) -> Option<WalRedoManagerStatus> { match self { WalRedoManager::Prod(_, m) => Some(m.status()), #[cfg(test)] WalRedoManager::Test(_) => None, } } } /// A very lightweight memory representation of an offloaded timeline. /// /// We need to store the list of offloaded timelines so that we can perform operations on them, /// like unoffloading them, or (at a later date), decide to perform flattening. /// This type has a much smaller memory impact than [`Timeline`], and thus we can store many /// more offloaded timelines than we can manage ones that aren't. pub struct OffloadedTimeline { pub tenant_shard_id: TenantShardId, pub timeline_id: TimelineId, pub ancestor_timeline_id: Option<TimelineId>, /// Whether to retain the branch lsn at the ancestor or not pub ancestor_retain_lsn: Option<Lsn>, /// When the timeline was archived. /// /// Present for future flattening deliberations. pub archived_at: NaiveDateTime, /// Prevent two tasks from deleting the timeline at the same time. If held, the /// timeline is being deleted. If 'true', the timeline has already been deleted. pub delete_progress: TimelineDeleteProgress, /// Part of the `OffloadedTimeline` object's lifecycle: this needs to be set before we drop it pub deleted_from_ancestor: AtomicBool, _metrics_guard: OffloadedTimelineMetricsGuard, } /// Increases the offloaded timeline count metric when created, and decreases when dropped. struct OffloadedTimelineMetricsGuard; impl OffloadedTimelineMetricsGuard { fn new() -> Self { TIMELINE_STATE_METRIC .with_label_values(&["offloaded"]) .inc(); Self } } impl Drop for OffloadedTimelineMetricsGuard { fn drop(&mut self) { TIMELINE_STATE_METRIC .with_label_values(&["offloaded"]) .dec(); } } impl OffloadedTimeline { /// Obtains an offloaded timeline from a given timeline object. /// /// Returns `None` if the `archived_at` flag couldn't be obtained, i.e. /// the timeline is not in a stopped state. /// Panics if the timeline is not archived. fn from_timeline(timeline: &Timeline) -> Result<Self, UploadQueueNotReadyError> { let (ancestor_retain_lsn, ancestor_timeline_id) = if let Some(ancestor_timeline) = timeline.ancestor_timeline() { let ancestor_lsn = timeline.get_ancestor_lsn(); let ancestor_timeline_id = ancestor_timeline.timeline_id; let mut gc_info = ancestor_timeline.gc_info.write().unwrap(); gc_info.insert_child(timeline.timeline_id, ancestor_lsn, MaybeOffloaded::Yes); (Some(ancestor_lsn), Some(ancestor_timeline_id)) } else { (None, None) }; let archived_at = timeline .remote_client .archived_at_stopped_queue()? .expect("must be called on an archived timeline"); Ok(Self { tenant_shard_id: timeline.tenant_shard_id, timeline_id: timeline.timeline_id, ancestor_timeline_id, ancestor_retain_lsn, archived_at, delete_progress: timeline.delete_progress.clone(), deleted_from_ancestor: AtomicBool::new(false), _metrics_guard: OffloadedTimelineMetricsGuard::new(), }) } fn from_manifest(tenant_shard_id: TenantShardId, manifest: &OffloadedTimelineManifest) -> Self { // We expect to reach this case in tenant loading, where the `retain_lsn` is populated in the parent's `gc_info` // by the `initialize_gc_info` function. let OffloadedTimelineManifest { timeline_id, ancestor_timeline_id, ancestor_retain_lsn, archived_at, } = *manifest; Self { tenant_shard_id, timeline_id, ancestor_timeline_id, ancestor_retain_lsn, archived_at, delete_progress: TimelineDeleteProgress::default(), deleted_from_ancestor: AtomicBool::new(false), _metrics_guard: OffloadedTimelineMetricsGuard::new(), } } fn manifest(&self) -> OffloadedTimelineManifest { let Self { timeline_id, ancestor_timeline_id, ancestor_retain_lsn, archived_at, .. } = self; OffloadedTimelineManifest { timeline_id: *timeline_id, ancestor_timeline_id: *ancestor_timeline_id, ancestor_retain_lsn: *ancestor_retain_lsn, archived_at: *archived_at, } } /// Delete this timeline's retain_lsn from its ancestor, if present in the given tenant fn delete_from_ancestor_with_timelines( &self, timelines: &std::sync::MutexGuard<'_, HashMap<TimelineId, Arc<Timeline>>>, ) { if let (Some(_retain_lsn), Some(ancestor_timeline_id)) = (self.ancestor_retain_lsn, self.ancestor_timeline_id) { if let Some((_, ancestor_timeline)) = timelines .iter() .find(|(tid, _tl)| **tid == ancestor_timeline_id) { let removal_happened = ancestor_timeline .gc_info .write() .unwrap() .remove_child_offloaded(self.timeline_id); if !removal_happened { tracing::error!(tenant_id = %self.tenant_shard_id.tenant_id, shard_id = %self.tenant_shard_id.shard_slug(), timeline_id = %self.timeline_id, "Couldn't remove retain_lsn entry from offloaded timeline's parent: already removed"); } } } self.deleted_from_ancestor.store(true, Ordering::Release); } /// Call [`Self::delete_from_ancestor_with_timelines`] instead if possible. /// /// As the entire tenant is being dropped, don't bother deregistering the `retain_lsn` from the ancestor. fn defuse_for_tenant_drop(&self) { self.deleted_from_ancestor.store(true, Ordering::Release); } } impl fmt::Debug for OffloadedTimeline { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "OffloadedTimeline<{}>", self.timeline_id) } } impl Drop for OffloadedTimeline { fn drop(&mut self) { if !self.deleted_from_ancestor.load(Ordering::Acquire) { tracing::warn!( "offloaded timeline {} was dropped without having cleaned it up at the ancestor", self.timeline_id ); } } } #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub enum MaybeOffloaded { Yes, No, } #[derive(Clone, Debug)] pub enum TimelineOrOffloaded { Timeline(Arc<Timeline>), Offloaded(Arc<OffloadedTimeline>), Importing(Arc<ImportingTimeline>), } impl TimelineOrOffloaded { pub fn arc_ref(&self) -> TimelineOrOffloadedArcRef<'_> { match self { TimelineOrOffloaded::Timeline(timeline) => { TimelineOrOffloadedArcRef::Timeline(timeline) } TimelineOrOffloaded::Offloaded(offloaded) => { TimelineOrOffloadedArcRef::Offloaded(offloaded) } TimelineOrOffloaded::Importing(importing) => { TimelineOrOffloadedArcRef::Importing(importing) } } } pub fn tenant_shard_id(&self) -> TenantShardId { self.arc_ref().tenant_shard_id() } pub fn timeline_id(&self) -> TimelineId { self.arc_ref().timeline_id() } pub fn delete_progress(&self) -> &Arc<tokio::sync::Mutex<DeleteTimelineFlow>> { match self { TimelineOrOffloaded::Timeline(timeline) => &timeline.delete_progress, TimelineOrOffloaded::Offloaded(offloaded) => &offloaded.delete_progress, TimelineOrOffloaded::Importing(importing) => &importing.delete_progress, } } fn maybe_remote_client(&self) -> Option<Arc<RemoteTimelineClient>> { match self { TimelineOrOffloaded::Timeline(timeline) => Some(timeline.remote_client.clone()), TimelineOrOffloaded::Offloaded(_offloaded) => None, TimelineOrOffloaded::Importing(importing) => { Some(importing.timeline.remote_client.clone()) } } } } pub enum TimelineOrOffloadedArcRef<'a> { Timeline(&'a Arc<Timeline>), Offloaded(&'a Arc<OffloadedTimeline>), Importing(&'a Arc<ImportingTimeline>), } impl TimelineOrOffloadedArcRef<'_> { pub fn tenant_shard_id(&self) -> TenantShardId { match self { TimelineOrOffloadedArcRef::Timeline(timeline) => timeline.tenant_shard_id, TimelineOrOffloadedArcRef::Offloaded(offloaded) => offloaded.tenant_shard_id, TimelineOrOffloadedArcRef::Importing(importing) => importing.timeline.tenant_shard_id, } } pub fn timeline_id(&self) -> TimelineId { match self { TimelineOrOffloadedArcRef::Timeline(timeline) => timeline.timeline_id, TimelineOrOffloadedArcRef::Offloaded(offloaded) => offloaded.timeline_id, TimelineOrOffloadedArcRef::Importing(importing) => importing.timeline.timeline_id, } } } impl<'a> From<&'a Arc<Timeline>> for TimelineOrOffloadedArcRef<'a> { fn from(timeline: &'a Arc<Timeline>) -> Self { Self::Timeline(timeline) } } impl<'a> From<&'a Arc<OffloadedTimeline>> for TimelineOrOffloadedArcRef<'a> { fn from(timeline: &'a Arc<OffloadedTimeline>) -> Self { Self::Offloaded(timeline) } } impl<'a> From<&'a Arc<ImportingTimeline>> for TimelineOrOffloadedArcRef<'a> { fn from(timeline: &'a Arc<ImportingTimeline>) -> Self { Self::Importing(timeline) } } #[derive(Debug, thiserror::Error, PartialEq, Eq)] pub enum GetTimelineError { #[error("Timeline is shutting down")] ShuttingDown, #[error("Timeline {tenant_id}/{timeline_id} is not active, state: {state:?}")] NotActive { tenant_id: TenantShardId, timeline_id: TimelineId, state: TimelineState, }, #[error("Timeline {tenant_id}/{timeline_id} was not found")] NotFound { tenant_id: TenantShardId, timeline_id: TimelineId, }, } #[derive(Debug, thiserror::Error)] pub enum LoadLocalTimelineError { #[error("FailedToLoad")] Load(#[source] anyhow::Error), #[error("FailedToResumeDeletion")] ResumeDeletion(#[source] anyhow::Error), } #[derive(thiserror::Error)] pub enum DeleteTimelineError { #[error("NotFound")] NotFound, #[error("HasChildren")] HasChildren(Vec<TimelineId>), #[error("Timeline deletion is already in progress")] AlreadyInProgress(Arc<tokio::sync::Mutex<DeleteTimelineFlow>>), #[error("Cancelled")] Cancelled, #[error(transparent)] Other(#[from] anyhow::Error), } impl Debug for DeleteTimelineError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::NotFound => write!(f, "NotFound"), Self::HasChildren(c) => f.debug_tuple("HasChildren").field(c).finish(), Self::AlreadyInProgress(_) => f.debug_tuple("AlreadyInProgress").finish(), Self::Cancelled => f.debug_tuple("Cancelled").finish(), Self::Other(e) => f.debug_tuple("Other").field(e).finish(), } } } #[derive(thiserror::Error)] pub enum TimelineArchivalError { #[error("NotFound")] NotFound, #[error("Timeout")] Timeout, #[error("Cancelled")] Cancelled, #[error("ancestor is archived: {}", .0)] HasArchivedParent(TimelineId), #[error("HasUnarchivedChildren")] HasUnarchivedChildren(Vec<TimelineId>), #[error("Timeline archival is already in progress")] AlreadyInProgress, #[error(transparent)] Other(anyhow::Error), } #[derive(thiserror::Error, Debug)] pub(crate) enum TenantManifestError { #[error("Remote storage error: {0}")] RemoteStorage(anyhow::Error),
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/basebackup_cache.rs
pageserver/src/basebackup_cache.rs
use std::{collections::HashMap, sync::Arc}; use anyhow::Context; use camino::{Utf8Path, Utf8PathBuf}; use metrics::core::{AtomicU64, GenericCounter}; use pageserver_api::{config::BasebackupCacheConfig, models::TenantState}; use tokio::{ io::{AsyncWriteExt, BufWriter}, sync::mpsc::{Receiver, Sender, error::TrySendError}, }; use tokio_util::sync::CancellationToken; use utils::{ id::{TenantId, TenantTimelineId, TimelineId}, lsn::Lsn, shard::TenantShardId, }; use crate::{ basebackup::send_basebackup_tarball, context::{DownloadBehavior, RequestContext}, metrics::{ BASEBACKUP_CACHE_ENTRIES, BASEBACKUP_CACHE_PREPARE, BASEBACKUP_CACHE_PREPARE_QUEUE_SIZE, BASEBACKUP_CACHE_READ, BASEBACKUP_CACHE_SIZE, }, task_mgr::TaskKind, tenant::{ Timeline, mgr::{TenantManager, TenantSlot}, }, }; pub struct BasebackupPrepareRequest { pub tenant_shard_id: TenantShardId, pub timeline_id: TimelineId, pub lsn: Lsn, } pub type BasebackupPrepareSender = Sender<BasebackupPrepareRequest>; pub type BasebackupPrepareReceiver = Receiver<BasebackupPrepareRequest>; #[derive(Clone)] struct CacheEntry { /// LSN at which the basebackup was taken. lsn: Lsn, /// Size of the basebackup archive in bytes. size_bytes: u64, } /// BasebackupCache stores cached basebackup archives for timelines on local disk. /// /// The main purpose of this cache is to speed up the startup process of compute nodes /// after scaling to zero. /// Thus, the basebackup is stored only for the latest LSN of the timeline and with /// fixed set of parameters (gzip=true, full_backup=false, replica=false, prev_lsn=none). /// /// The cache receives prepare requests through the `BasebackupPrepareSender` channel, /// generates a basebackup from the timeline in the background, and stores it on disk. /// /// Basebackup requests are pretty rare. We expect ~thousands of entries in the cache /// and ~1 RPS for get requests. pub struct BasebackupCache { data_dir: Utf8PathBuf, config: Option<BasebackupCacheConfig>, entries: std::sync::Mutex<HashMap<TenantTimelineId, CacheEntry>>, prepare_sender: BasebackupPrepareSender, read_hit_count: GenericCounter<AtomicU64>, read_miss_count: GenericCounter<AtomicU64>, read_err_count: GenericCounter<AtomicU64>, prepare_skip_count: GenericCounter<AtomicU64>, } impl BasebackupCache { /// Create a new BasebackupCache instance. /// Also returns a BasebackupPrepareReceiver which is needed to start /// the background task. /// The cache is initialized from the data_dir in the background task. /// The cache will return `None` for any get requests until the initialization is complete. /// The background task is spawned separately using [`Self::spawn_background_task`] /// to avoid a circular dependency between the cache and the tenant manager. pub fn new( data_dir: Utf8PathBuf, config: Option<BasebackupCacheConfig>, ) -> (Arc<Self>, BasebackupPrepareReceiver) { let chan_size = config.as_ref().map(|c| c.max_size_entries).unwrap_or(1); let (prepare_sender, prepare_receiver) = tokio::sync::mpsc::channel(chan_size); let cache = Arc::new(BasebackupCache { data_dir, config, entries: std::sync::Mutex::new(HashMap::new()), prepare_sender, read_hit_count: BASEBACKUP_CACHE_READ.with_label_values(&["hit"]), read_miss_count: BASEBACKUP_CACHE_READ.with_label_values(&["miss"]), read_err_count: BASEBACKUP_CACHE_READ.with_label_values(&["error"]), prepare_skip_count: BASEBACKUP_CACHE_PREPARE.with_label_values(&["skip"]), }); (cache, prepare_receiver) } /// Spawns the background task. /// The background task initializes the cache from the disk, /// processes prepare requests, and cleans up outdated cache entries. /// Noop if the cache is disabled (config is None). pub fn spawn_background_task( self: Arc<Self>, runtime_handle: &tokio::runtime::Handle, prepare_receiver: BasebackupPrepareReceiver, tenant_manager: Arc<TenantManager>, cancel: CancellationToken, ) { if let Some(config) = self.config.clone() { let background = BackgroundTask { c: self, config, tenant_manager, cancel, entry_count: 0, total_size_bytes: 0, prepare_ok_count: BASEBACKUP_CACHE_PREPARE.with_label_values(&["ok"]), prepare_skip_count: BASEBACKUP_CACHE_PREPARE.with_label_values(&["skip"]), prepare_err_count: BASEBACKUP_CACHE_PREPARE.with_label_values(&["error"]), }; runtime_handle.spawn(background.run(prepare_receiver)); } } /// Send a basebackup prepare request to the background task. /// The basebackup will be prepared asynchronously, it does not block the caller. /// The request will be skipped if any cache limits are exceeded. pub fn send_prepare(&self, tenant_shard_id: TenantShardId, timeline_id: TimelineId, lsn: Lsn) { let req = BasebackupPrepareRequest { tenant_shard_id, timeline_id, lsn, }; BASEBACKUP_CACHE_PREPARE_QUEUE_SIZE.inc(); let res = self.prepare_sender.try_send(req); if let Err(e) = res { BASEBACKUP_CACHE_PREPARE_QUEUE_SIZE.dec(); self.prepare_skip_count.inc(); match e { TrySendError::Full(_) => { // Basebackup prepares are pretty rare, normally we should not hit this. tracing::info!( tenant_id = %tenant_shard_id.tenant_id, %timeline_id, %lsn, "Basebackup prepare channel is full, skipping the request" ); } TrySendError::Closed(_) => { // Normal during shutdown, not critical. tracing::info!( tenant_id = %tenant_shard_id.tenant_id, %timeline_id, %lsn, "Basebackup prepare channel is closed, skipping the request" ); } } } } /// Gets a basebackup entry from the cache. /// If the entry is found, opens a file with the basebackup archive and returns it. /// The open file descriptor will prevent the file system from deleting the file /// even if the entry is removed from the cache in the background. pub async fn get( &self, tenant_id: TenantId, timeline_id: TimelineId, lsn: Lsn, ) -> Option<tokio::fs::File> { if !self.is_enabled() { return None; } // Fast path. Check if the entry exists using the in-memory state. let tti = TenantTimelineId::new(tenant_id, timeline_id); if self.entries.lock().unwrap().get(&tti).map(|e| e.lsn) != Some(lsn) { self.read_miss_count.inc(); return None; } let path = self.entry_path(tenant_id, timeline_id, lsn); match tokio::fs::File::open(path).await { Ok(file) => { self.read_hit_count.inc(); Some(file) } Err(e) => { if e.kind() == std::io::ErrorKind::NotFound { // We may end up here if the basebackup was concurrently removed by the cleanup task. self.read_miss_count.inc(); } else { self.read_err_count.inc(); tracing::warn!("Unexpected error opening basebackup cache file: {:?}", e); } None } } } pub fn is_enabled(&self) -> bool { self.config.is_some() } // Private methods. fn entry_filename(tenant_id: TenantId, timeline_id: TimelineId, lsn: Lsn) -> String { // The default format for LSN is 0/ABCDEF. // The backslash is not filename friendly, so serialize it as plain hex. let lsn = lsn.0; format!("basebackup_{tenant_id}_{timeline_id}_{lsn:016X}.tar.gz") } fn entry_path(&self, tenant_id: TenantId, timeline_id: TimelineId, lsn: Lsn) -> Utf8PathBuf { self.data_dir .join(Self::entry_filename(tenant_id, timeline_id, lsn)) } } /// The background task that does the job to prepare basebackups /// and manage the cache entries on disk. /// It is a separate struct from BasebackupCache to allow holding /// a mutable reference to this state without a mutex lock, /// while BasebackupCache is referenced by the clients. struct BackgroundTask { c: Arc<BasebackupCache>, config: BasebackupCacheConfig, tenant_manager: Arc<TenantManager>, cancel: CancellationToken, /// Number of the entries in the cache. /// This counter is used for metrics and applying cache limits. /// It generally should be equal to c.entries.len(), but it's calculated /// pessimistically for abnormal situations: if we encountered some errors /// during removing the entry from disk, we won't decrement this counter to /// make sure that we don't exceed the limit with "trashed" files on the disk. /// It will also count files in the data_dir that are not valid cache entries. entry_count: usize, /// Total size of all the entries on the disk. /// This counter is used for metrics and applying cache limits. /// Similar to entry_count, it is calculated pessimistically for abnormal situations. total_size_bytes: u64, prepare_ok_count: GenericCounter<AtomicU64>, prepare_skip_count: GenericCounter<AtomicU64>, prepare_err_count: GenericCounter<AtomicU64>, } impl BackgroundTask { fn tmp_dir(&self) -> Utf8PathBuf { self.c.data_dir.join("tmp") } fn entry_tmp_path( &self, tenant_id: TenantId, timeline_id: TimelineId, lsn: Lsn, ) -> Utf8PathBuf { self.tmp_dir() .join(BasebackupCache::entry_filename(tenant_id, timeline_id, lsn)) } fn parse_entry_filename(filename: &str) -> Option<(TenantId, TimelineId, Lsn)> { let parts: Vec<&str> = filename .strip_prefix("basebackup_")? .strip_suffix(".tar.gz")? .split('_') .collect(); if parts.len() != 3 { return None; } let tenant_id = parts[0].parse::<TenantId>().ok()?; let timeline_id = parts[1].parse::<TimelineId>().ok()?; let lsn = Lsn(u64::from_str_radix(parts[2], 16).ok()?); Some((tenant_id, timeline_id, lsn)) } // Recreate the tmp directory to clear all files in it. async fn clean_tmp_dir(&self) -> anyhow::Result<()> { let tmp_dir = self.tmp_dir(); if tmp_dir.exists() { tokio::fs::remove_dir_all(&tmp_dir).await?; } tokio::fs::create_dir_all(&tmp_dir).await?; Ok(()) } async fn cleanup(&mut self) -> anyhow::Result<()> { self.clean_tmp_dir().await?; // Leave only up-to-date entries. let entries_old = self.c.entries.lock().unwrap().clone(); let mut entries_new = HashMap::new(); for (tenant_shard_id, tenant_slot) in self.tenant_manager.list() { if !tenant_shard_id.is_shard_zero() { continue; } let TenantSlot::Attached(tenant) = tenant_slot else { continue; }; let tenant_id = tenant_shard_id.tenant_id; for timeline in tenant.list_timelines() { let tti = TenantTimelineId::new(tenant_id, timeline.timeline_id); if let Some(entry) = entries_old.get(&tti) { if timeline.get_last_record_lsn() <= entry.lsn { entries_new.insert(tti, entry.clone()); } } } } // Try to remove all entries that are not up-to-date. for (&tti, entry) in entries_old.iter() { if !entries_new.contains_key(&tti) { self.try_remove_entry(tti.tenant_id, tti.timeline_id, entry) .await; } } // Note: BackgroundTask is the only writer for self.c.entries, // so it couldn't have been modified concurrently. *self.c.entries.lock().unwrap() = entries_new; Ok(()) } async fn on_startup(&mut self) -> anyhow::Result<()> { // Create data_dir if it does not exist. tokio::fs::create_dir_all(&self.c.data_dir) .await .context("Failed to create basebackup cache data directory")?; self.clean_tmp_dir() .await .context("Failed to clean tmp directory")?; // Read existing entries from the data_dir and add them to in-memory state. let mut entries = HashMap::<TenantTimelineId, CacheEntry>::new(); let mut dir = tokio::fs::read_dir(&self.c.data_dir).await?; while let Some(dir_entry) = dir.next_entry().await? { let filename = dir_entry.file_name(); if filename == "tmp" { // Skip the tmp directory. continue; } let size_bytes = dir_entry .metadata() .await .map_err(|e| { anyhow::anyhow!("Failed to read metadata for file {:?}: {:?}", filename, e) })? .len(); self.entry_count += 1; BASEBACKUP_CACHE_ENTRIES.set(self.entry_count as u64); self.total_size_bytes += size_bytes; BASEBACKUP_CACHE_SIZE.set(self.total_size_bytes); let parsed = Self::parse_entry_filename(filename.to_string_lossy().as_ref()); let Some((tenant_id, timeline_id, lsn)) = parsed else { tracing::warn!("Invalid basebackup cache file name: {:?}", filename); continue; }; let cur_entry = CacheEntry { lsn, size_bytes }; let tti = TenantTimelineId::new(tenant_id, timeline_id); use std::collections::hash_map::Entry::*; match entries.entry(tti) { Occupied(mut entry) => { let found_entry = entry.get(); // Leave only the latest entry, remove the old one. if cur_entry.lsn < found_entry.lsn { self.try_remove_entry(tenant_id, timeline_id, &cur_entry) .await; } else if cur_entry.lsn > found_entry.lsn { self.try_remove_entry(tenant_id, timeline_id, found_entry) .await; entry.insert(cur_entry); } else { // Two different filenames parsed to the same timline_id and LSN. // Should never happen. return Err(anyhow::anyhow!( "Duplicate basebackup cache entry with the same LSN: {:?}", filename )); } } Vacant(entry) => { entry.insert(cur_entry); } } } *self.c.entries.lock().unwrap() = entries; Ok(()) } async fn run(mut self, mut prepare_receiver: BasebackupPrepareReceiver) { // Panic in the background is a safe fallback. // It will drop receivers and the cache will be effectively disabled. self.on_startup() .await .expect("Failed to initialize basebackup cache"); let mut cleanup_ticker = tokio::time::interval(self.config.cleanup_period); cleanup_ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); loop { tokio::select! { Some(req) = prepare_receiver.recv() => { BASEBACKUP_CACHE_PREPARE_QUEUE_SIZE.dec(); if let Err(err) = self.prepare_basebackup( req.tenant_shard_id, req.timeline_id, req.lsn, ).await { tracing::info!("Failed to prepare basebackup: {:#}", err); self.prepare_err_count.inc(); continue; } } _ = cleanup_ticker.tick() => { self.cleanup().await.unwrap_or_else(|e| { tracing::warn!("Failed to clean up basebackup cache: {:#}", e); }); } _ = self.cancel.cancelled() => { tracing::info!("BasebackupCache background task cancelled"); break; } } } } /// Try to remove an entry from disk. /// The caller is responsible for removing the entry from the in-memory state. /// Updates size counters and corresponding metrics. /// Ignores the filesystem errors as not-so-important, but the size counters /// are not decremented in this case, so the file will continue to be counted /// towards the size limits. async fn try_remove_entry( &mut self, tenant_id: TenantId, timeline_id: TimelineId, entry: &CacheEntry, ) { let entry_path = self.c.entry_path(tenant_id, timeline_id, entry.lsn); match tokio::fs::remove_file(&entry_path).await { Ok(_) => {} Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} Err(e) => { tracing::warn!( "Failed to remove basebackup cache file for tenant {} timeline {} LSN {}: {:#}", tenant_id, timeline_id, entry.lsn, e ); return; } } self.entry_count -= 1; BASEBACKUP_CACHE_ENTRIES.set(self.entry_count as u64); self.total_size_bytes -= entry.size_bytes; BASEBACKUP_CACHE_SIZE.set(self.total_size_bytes); } /// Insert the cache entry into in-memory state and update the size counters. /// Assumes that the file for the entry already exists on disk. /// If the entry already exists with previous LSN, it will be removed. async fn upsert_entry( &mut self, tenant_id: TenantId, timeline_id: TimelineId, entry: CacheEntry, ) { let tti = TenantTimelineId::new(tenant_id, timeline_id); self.entry_count += 1; BASEBACKUP_CACHE_ENTRIES.set(self.entry_count as u64); self.total_size_bytes += entry.size_bytes; BASEBACKUP_CACHE_SIZE.set(self.total_size_bytes); let old_entry = self.c.entries.lock().unwrap().insert(tti, entry); if let Some(old_entry) = old_entry { self.try_remove_entry(tenant_id, timeline_id, &old_entry) .await; } } /// Prepare a basebackup for the given timeline. /// /// If the basebackup already exists with a higher LSN or the timeline already /// has a higher last_record_lsn, skip the preparation. /// /// The basebackup is prepared in a temporary directory and then moved to the final /// location to make the operation atomic. async fn prepare_basebackup( &mut self, tenant_shard_id: TenantShardId, timeline_id: TimelineId, req_lsn: Lsn, ) -> anyhow::Result<()> { tracing::info!( tenant_id = %tenant_shard_id.tenant_id, %timeline_id, %req_lsn, "Preparing basebackup for timeline", ); let tti = TenantTimelineId::new(tenant_shard_id.tenant_id, timeline_id); // TODO(diko): I don't think we will hit the limit, // but if we do, it makes sense to try to evict oldest entries. here if self.entry_count >= self.config.max_size_entries { tracing::info!( %tenant_shard_id, %timeline_id, %req_lsn, "Basebackup cache is full (max_size_entries), skipping basebackup", ); self.prepare_skip_count.inc(); return Ok(()); } if self.total_size_bytes >= self.config.max_total_size_bytes { tracing::info!( %tenant_shard_id, %timeline_id, %req_lsn, "Basebackup cache is full (max_total_size_bytes), skipping basebackup", ); self.prepare_skip_count.inc(); return Ok(()); } { let entries = self.c.entries.lock().unwrap(); if let Some(entry) = entries.get(&tti) { if entry.lsn >= req_lsn { tracing::info!( %timeline_id, %req_lsn, %entry.lsn, "Basebackup entry already exists for timeline with higher LSN, skipping basebackup", ); self.prepare_skip_count.inc(); return Ok(()); } } } let tenant = self .tenant_manager .get_attached_tenant_shard(tenant_shard_id)?; let tenant_state = tenant.current_state(); if tenant_state != TenantState::Active { anyhow::bail!( "Tenant {} is not active, current state: {:?}", tenant_shard_id.tenant_id, tenant_state ) } let timeline = tenant.get_timeline(timeline_id, true)?; let last_record_lsn = timeline.get_last_record_lsn(); if last_record_lsn > req_lsn { tracing::info!( %timeline_id, %req_lsn, %last_record_lsn, "Timeline has a higher LSN than the requested one, skipping basebackup", ); self.prepare_skip_count.inc(); return Ok(()); } let entry_tmp_path = self.entry_tmp_path(tenant_shard_id.tenant_id, timeline_id, req_lsn); let res = self .prepare_basebackup_tmp(&entry_tmp_path, &timeline, req_lsn) .await; let entry = match res { Ok(entry) => entry, Err(err) => { tracing::info!("Failed to prepare basebackup tmp file: {:#}", err); // Try to clean up tmp file. If we fail, the background clean up task will take care of it. match tokio::fs::remove_file(&entry_tmp_path).await { Ok(_) => {} Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} Err(e) => { tracing::info!("Failed to remove basebackup tmp file: {:?}", e); } } return Err(err); } }; // Move the tmp file to the final location atomically. // The tmp file is fsynced, so it's guaranteed that we will not have a partial file // in the main directory. // It's not necessary to fsync the inode after renaming, because the worst case is that // the rename operation will be rolled back on the disk failure, the entry will disappear // from the main directory, and the entry access will cause a cache miss. let entry_path = self .c .entry_path(tenant_shard_id.tenant_id, timeline_id, req_lsn); tokio::fs::rename(&entry_tmp_path, &entry_path).await?; self.upsert_entry(tenant_shard_id.tenant_id, timeline_id, entry) .await; self.prepare_ok_count.inc(); Ok(()) } /// Prepares a basebackup in a temporary file. /// Guarantees that the tmp file is fsynced before returning. async fn prepare_basebackup_tmp( &self, entry_tmp_path: &Utf8Path, timeline: &Arc<Timeline>, req_lsn: Lsn, ) -> anyhow::Result<CacheEntry> { let ctx = RequestContext::new(TaskKind::BasebackupCache, DownloadBehavior::Download); let ctx = ctx.with_scope_timeline(timeline); let file = tokio::fs::File::create(entry_tmp_path).await?; let mut writer = BufWriter::new(file); // We may receive a request before the WAL record is applied to the timeline. // Wait for the requested LSN to be applied. timeline .wait_lsn( req_lsn, crate::tenant::timeline::WaitLsnWaiter::BaseBackupCache, crate::tenant::timeline::WaitLsnTimeout::Default, &ctx, ) .await?; send_basebackup_tarball( &mut writer, timeline, Some(req_lsn), None, false, false, // Level::Best because compression is not on the hot path of basebackup requests. // The decompression is almost not affected by the compression level. Some(async_compression::Level::Best), &ctx, ) .await?; writer.flush().await?; writer.into_inner().sync_all().await?; // TODO(diko): we can count it via Writer wrapper instead of a syscall. let size_bytes = tokio::fs::metadata(entry_tmp_path).await?.len(); Ok(CacheEntry { lsn: req_lsn, size_bytes, }) } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/assert_u64_eq_usize.rs
pageserver/src/assert_u64_eq_usize.rs
//! `u64`` and `usize`` aren't guaranteed to be identical in Rust, but life is much simpler if that's the case. pub(crate) const _ASSERT_U64_EQ_USIZE: () = { if std::mem::size_of::<usize>() != std::mem::size_of::<u64>() { panic!( "the traits defined in this module assume that usize and u64 can be converted to each other without loss of information" ); } }; pub(crate) trait U64IsUsize { fn into_usize(self) -> usize; } impl U64IsUsize for u64 { #[inline(always)] fn into_usize(self) -> usize { #[allow(clippy::let_unit_value)] let _ = _ASSERT_U64_EQ_USIZE; self as usize } } pub(crate) trait UsizeIsU64 { fn into_u64(self) -> u64; } impl UsizeIsU64 for usize { #[inline(always)] fn into_u64(self) -> u64 { #[allow(clippy::let_unit_value)] let _ = _ASSERT_U64_EQ_USIZE; self as u64 } } pub const fn u64_to_usize(x: u64) -> usize { #[allow(clippy::let_unit_value)] let _ = _ASSERT_U64_EQ_USIZE; x as usize }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/consumption_metrics.rs
pageserver/src/consumption_metrics.rs
//! Periodically collect consumption metrics for all active tenants //! and push them to a HTTP endpoint. use std::collections::HashMap; use std::sync::Arc; use std::time::{Duration, SystemTime}; use camino::Utf8PathBuf; use consumption_metrics::EventType; use itertools::Itertools as _; use pageserver_api::models::TenantState; use remote_storage::{GenericRemoteStorage, RemoteStorageConfig}; use reqwest::Url; use serde::{Deserialize, Serialize}; use tokio::time::Instant; use tokio_util::sync::CancellationToken; use tracing::*; use utils::id::NodeId; use crate::config::PageServerConf; use crate::consumption_metrics::metrics::MetricsKey; use crate::consumption_metrics::upload::KeyGen as _; use crate::context::{DownloadBehavior, RequestContext}; use crate::task_mgr::{self, BACKGROUND_RUNTIME, TaskKind}; use crate::tenant::mgr::TenantManager; use crate::tenant::size::CalculateSyntheticSizeError; use crate::tenant::tasks::BackgroundLoopKind; use crate::tenant::{LogicalSizeCalculationCause, TenantShard}; mod disk_cache; mod metrics; mod upload; const DEFAULT_HTTP_REPORTING_TIMEOUT: Duration = Duration::from_secs(60); /// Basically a key-value pair, but usually in a Vec except for [`Cache`]. /// /// This is as opposed to `consumption_metrics::Event` which is the externally communicated form. /// Difference is basically the missing idempotency key, which lives only for the duration of /// upload attempts. type RawMetric = (MetricsKey, (EventType, u64)); /// The new serializable metrics format #[derive(Serialize, Deserialize)] struct NewMetricsRoot { version: usize, metrics: Vec<NewRawMetric>, } impl NewMetricsRoot { pub fn is_v2_metrics(json_value: &serde_json::Value) -> bool { if let Some(ver) = json_value.get("version") { if let Some(2) = ver.as_u64() { return true; } } false } } /// The new serializable metrics format #[derive(Serialize)] struct NewMetricsRefRoot<'a> { version: usize, metrics: &'a [NewRawMetric], } impl<'a> NewMetricsRefRoot<'a> { fn new(metrics: &'a [NewRawMetric]) -> Self { Self { version: 2, metrics, } } } /// The new serializable metrics format #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] struct NewRawMetric { key: MetricsKey, kind: EventType, value: u64, // TODO: add generation field and check against generations } impl NewRawMetric { #[cfg(test)] fn to_kv_pair(&self) -> (MetricsKey, NewRawMetric) { (self.key, self.clone()) } } /// Caches the [`RawMetric`]s /// /// In practice, during startup, last sent values are stored here to be used in calculating new /// ones. After successful uploading, the cached values are updated to cache. This used to be used /// for deduplication, but that is no longer needed. type Cache = HashMap<MetricsKey, NewRawMetric>; pub async fn run( conf: &'static PageServerConf, tenant_manager: Arc<TenantManager>, cancel: CancellationToken, ) { let Some(metric_collection_endpoint) = conf.metric_collection_endpoint.as_ref() else { return; }; let local_disk_storage = conf.workdir.join("last_consumption_metrics.json"); let metrics_ctx = RequestContext::todo_child( TaskKind::MetricsCollection, // This task itself shouldn't download anything. // The actual size calculation does need downloads, and // creates a child context with the right DownloadBehavior. DownloadBehavior::Error, ); let collect_metrics = BACKGROUND_RUNTIME.spawn(task_mgr::exit_on_panic_or_error( "consumption metrics collection", collect_metrics( tenant_manager.clone(), metric_collection_endpoint, &conf.metric_collection_bucket, conf.metric_collection_interval, conf.id, local_disk_storage, cancel.clone(), metrics_ctx, ) .instrument(info_span!("metrics_collection")), )); let worker_ctx = RequestContext::todo_child(TaskKind::CalculateSyntheticSize, DownloadBehavior::Download); let synthetic_size_worker = BACKGROUND_RUNTIME.spawn(task_mgr::exit_on_panic_or_error( "synthetic size calculation", calculate_synthetic_size_worker( tenant_manager.clone(), conf.synthetic_size_calculation_interval, cancel.clone(), worker_ctx, ) .instrument(info_span!("synthetic_size_worker")), )); let (collect_metrics, synthetic_size_worker) = futures::future::join(collect_metrics, synthetic_size_worker).await; collect_metrics .expect("unreachable: exit_on_panic_or_error would catch the panic and exit the process"); synthetic_size_worker .expect("unreachable: exit_on_panic_or_error would catch the panic and exit the process"); } /// Main thread that serves metrics collection #[allow(clippy::too_many_arguments)] async fn collect_metrics( tenant_manager: Arc<TenantManager>, metric_collection_endpoint: &Url, metric_collection_bucket: &Option<RemoteStorageConfig>, metric_collection_interval: Duration, node_id: NodeId, local_disk_storage: Utf8PathBuf, cancel: CancellationToken, ctx: RequestContext, ) -> anyhow::Result<()> { let path: Arc<Utf8PathBuf> = Arc::new(local_disk_storage); let restore_and_reschedule = restore_and_reschedule(&path, metric_collection_interval); let mut cached_metrics = tokio::select! { _ = cancel.cancelled() => return Ok(()), ret = restore_and_reschedule => ret, }; // define client here to reuse it for all requests let client = reqwest::ClientBuilder::new() .timeout(DEFAULT_HTTP_REPORTING_TIMEOUT) .build() .expect("Failed to create http client with timeout"); let bucket_client = if let Some(bucket_config) = metric_collection_bucket { match GenericRemoteStorage::from_config(bucket_config).await { Ok(client) => Some(client), Err(e) => { // Non-fatal error: if we were given an invalid config, we will proceed // with sending metrics over the network, but not to S3. tracing::warn!("Invalid configuration for metric_collection_bucket: {e}"); None } } } else { None }; let node_id = node_id.to_string(); loop { let started_at = Instant::now(); // these are point in time, with variable "now" let metrics = metrics::collect_all_metrics(&tenant_manager, &cached_metrics, &ctx).await; // Pre-generate event idempotency keys, to reuse them across the bucket // and HTTP sinks. let idempotency_keys = std::iter::repeat_with(|| node_id.as_str().generate()) .take(metrics.len()) .collect_vec(); let metrics = Arc::new(metrics); // why not race cancellation here? because we are one of the last tasks, and if we are // already here, better to try to flush the new values. let flush = async { match disk_cache::flush_metrics_to_disk(&metrics, &path).await { Ok(()) => { tracing::debug!("flushed metrics to disk"); } Err(e) => { // idea here is that if someone creates a directory as our path, then they // might notice it from the logs before shutdown and remove it tracing::error!("failed to persist metrics to {path:?}: {e:#}"); } } if let Some(bucket_client) = &bucket_client { let res = upload::upload_metrics_bucket( bucket_client, &cancel, &node_id, &metrics, &idempotency_keys, ) .await; if let Err(e) = res { tracing::error!("failed to upload to remote storage: {e:#}"); } } }; let upload = async { let res = upload::upload_metrics_http( &client, metric_collection_endpoint, &cancel, &metrics, &mut cached_metrics, &idempotency_keys, ) .await; if let Err(e) = res { // serialization error which should never happen tracing::error!("failed to upload via HTTP due to {e:#}"); } }; // let these run concurrently let (_, _) = tokio::join!(flush, upload); crate::tenant::tasks::warn_when_period_overrun( started_at.elapsed(), metric_collection_interval, BackgroundLoopKind::ConsumptionMetricsCollectMetrics, ); let res = tokio::time::timeout_at(started_at + metric_collection_interval, cancel.cancelled()) .await; if res.is_ok() { return Ok(()); } } } /// Called on the first iteration in an attempt to join the metric uploading schedule from previous /// pageserver session. Pageserver is supposed to upload at intervals regardless of restarts. /// /// Cancellation safe. async fn restore_and_reschedule( path: &Arc<Utf8PathBuf>, metric_collection_interval: Duration, ) -> Cache { let (cached, earlier_metric_at) = match disk_cache::read_metrics_from_disk(path.clone()).await { Ok(found_some) => { // there is no min needed because we write these sequentially in // collect_all_metrics let earlier_metric_at = found_some .iter() .map(|item| item.kind.recorded_at()) .copied() .next(); let cached = found_some .into_iter() .map(|item| (item.key, item)) .collect::<Cache>(); (cached, earlier_metric_at) } Err(e) => { use std::io::{Error, ErrorKind}; let root = e.root_cause(); let maybe_ioerr = root.downcast_ref::<Error>(); let is_not_found = maybe_ioerr.is_some_and(|e| e.kind() == ErrorKind::NotFound); if !is_not_found { tracing::info!("failed to read any previous metrics from {path:?}: {e:#}"); } (HashMap::new(), None) } }; if let Some(earlier_metric_at) = earlier_metric_at { let earlier_metric_at: SystemTime = earlier_metric_at.into(); let error = reschedule(earlier_metric_at, metric_collection_interval).await; if let Some(error) = error { if error.as_secs() >= 60 { tracing::info!( error_ms = error.as_millis(), "startup scheduling error due to restart" ) } } } cached } async fn reschedule( earlier_metric_at: SystemTime, metric_collection_interval: Duration, ) -> Option<Duration> { let now = SystemTime::now(); match now.duration_since(earlier_metric_at) { Ok(from_last_send) if from_last_send < metric_collection_interval => { let sleep_for = metric_collection_interval - from_last_send; let deadline = std::time::Instant::now() + sleep_for; tokio::time::sleep_until(deadline.into()).await; let now = std::time::Instant::now(); // executor threads might be busy, add extra measurements Some(if now < deadline { deadline - now } else { now - deadline }) } Ok(from_last_send) => Some(from_last_send.saturating_sub(metric_collection_interval)), Err(_) => { tracing::warn!( ?now, ?earlier_metric_at, "oldest recorded metric is in future; first values will come out with inconsistent timestamps" ); earlier_metric_at.duration_since(now).ok() } } } /// Caclculate synthetic size for each active tenant async fn calculate_synthetic_size_worker( tenant_manager: Arc<TenantManager>, synthetic_size_calculation_interval: Duration, cancel: CancellationToken, ctx: RequestContext, ) -> anyhow::Result<()> { info!("starting calculate_synthetic_size_worker"); scopeguard::defer! { info!("calculate_synthetic_size_worker stopped"); }; loop { let started_at = Instant::now(); let tenants = match tenant_manager.list_tenants() { Ok(tenants) => tenants, Err(e) => { warn!("cannot get tenant list: {e:#}"); continue; } }; for (tenant_shard_id, tenant_state, _gen) in tenants { if tenant_state != TenantState::Active { continue; } if !tenant_shard_id.is_shard_zero() { // We only send consumption metrics from shard 0, so don't waste time calculating // synthetic size on other shards. continue; } let Ok(tenant) = tenant_manager.get_attached_tenant_shard(tenant_shard_id) else { continue; }; if !tenant.is_active() { continue; } // there is never any reason to exit calculate_synthetic_size_worker following any // return value -- we don't need to care about shutdown because no tenant is found when // pageserver is shut down. calculate_and_log(&tenant, &cancel, &ctx).await; } crate::tenant::tasks::warn_when_period_overrun( started_at.elapsed(), synthetic_size_calculation_interval, BackgroundLoopKind::ConsumptionMetricsSyntheticSizeWorker, ); let res = tokio::time::timeout_at( started_at + synthetic_size_calculation_interval, cancel.cancelled(), ) .await; if res.is_ok() { return Ok(()); } } } async fn calculate_and_log(tenant: &TenantShard, cancel: &CancellationToken, ctx: &RequestContext) { const CAUSE: LogicalSizeCalculationCause = LogicalSizeCalculationCause::ConsumptionMetricsSyntheticSize; // TODO should we use concurrent_background_tasks_rate_limit() here, like the other background tasks? // We can put in some prioritization for consumption metrics. // Same for the loop that fetches computed metrics. // By using the same limiter, we centralize metrics collection for "start" and "finished" counters, // which turns out is really handy to understand the system. match tenant.calculate_synthetic_size(CAUSE, cancel, ctx).await { Ok(_) => {} Err(CalculateSyntheticSizeError::Cancelled) => {} Err(e) => { let tenant_shard_id = tenant.tenant_shard_id(); error!("failed to calculate synthetic size for tenant {tenant_shard_id}: {e:#}"); } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/statvfs.rs
pageserver/src/statvfs.rs
//! Wrapper around nix::sys::statvfs::Statvfs that allows for mocking. use camino::Utf8Path; pub enum Statvfs { Real(nix::sys::statvfs::Statvfs), Mock(mock::Statvfs), } // NB: on macOS, the block count type of struct statvfs is u32. // The workaround seems to be to use the non-standard statfs64 call. // Sincce it should only be a problem on > 2TiB disks, let's ignore // the problem for now and upcast to u64. impl Statvfs { pub fn get(tenants_dir: &Utf8Path, mocked: Option<&mock::Behavior>) -> nix::Result<Self> { if let Some(mocked) = mocked { Ok(Statvfs::Mock(mock::get(tenants_dir, mocked)?)) } else { Ok(Statvfs::Real(nix::sys::statvfs::statvfs( tenants_dir.as_std_path(), )?)) } } // NB: allow() because the block count type is u32 on macOS. #[allow(clippy::useless_conversion, clippy::unnecessary_fallible_conversions)] pub fn blocks(&self) -> u64 { match self { Statvfs::Real(stat) => u64::try_from(stat.blocks()).unwrap(), Statvfs::Mock(stat) => stat.blocks, } } // NB: allow() because the block count type is u32 on macOS. #[allow(clippy::useless_conversion, clippy::unnecessary_fallible_conversions)] pub fn blocks_available(&self) -> u64 { match self { Statvfs::Real(stat) => u64::try_from(stat.blocks_available()).unwrap(), Statvfs::Mock(stat) => stat.blocks_available, } } pub fn fragment_size(&self) -> u64 { match self { Statvfs::Real(stat) => stat.fragment_size(), Statvfs::Mock(stat) => stat.fragment_size, } } pub fn block_size(&self) -> u64 { match self { Statvfs::Real(stat) => stat.block_size(), Statvfs::Mock(stat) => stat.block_size, } } /// Get the available and total bytes on the filesystem. pub fn get_avail_total_bytes(&self) -> (u64, u64) { // https://unix.stackexchange.com/a/703650 let blocksize = if self.fragment_size() > 0 { self.fragment_size() } else { self.block_size() }; // use blocks_available (b_avail) since, pageserver runs as unprivileged user let avail_bytes = self.blocks_available() * blocksize; let total_bytes = self.blocks() * blocksize; (avail_bytes, total_bytes) } } pub mod mock { use camino::Utf8Path; pub use pageserver_api::config::statvfs::mock::Behavior; use regex::Regex; use tracing::log::info; pub fn get(tenants_dir: &Utf8Path, behavior: &Behavior) -> nix::Result<Statvfs> { info!("running mocked statvfs"); match behavior { Behavior::Success { blocksize, total_blocks, name_filter, } => { let used_bytes = walk_dir_disk_usage(tenants_dir, name_filter.as_deref()).unwrap(); // round it up to the nearest block multiple let used_blocks = used_bytes.div_ceil(*blocksize); if used_blocks > *total_blocks { panic!( "mocking error: used_blocks > total_blocks: {used_blocks} > {total_blocks}" ); } let avail_blocks = total_blocks - used_blocks; Ok(Statvfs { blocks: *total_blocks, blocks_available: avail_blocks, fragment_size: *blocksize, block_size: *blocksize, }) } #[cfg(feature = "testing")] Behavior::Failure { mocked_error } => Err((*mocked_error).into()), } } fn walk_dir_disk_usage(path: &Utf8Path, name_filter: Option<&Regex>) -> anyhow::Result<u64> { let mut total = 0; for entry in walkdir::WalkDir::new(path) { let entry = entry?; if !entry.file_type().is_file() { continue; } if !name_filter .as_ref() .map(|filter| filter.is_match(entry.file_name().to_str().unwrap())) .unwrap_or(true) { continue; } let m = match entry.metadata() { Ok(m) => m, Err(e) if is_not_found(&e) => { // some temp file which got removed right as we are walking continue; } Err(e) => { return Err(anyhow::Error::new(e) .context(format!("get metadata of {:?}", entry.path()))); } }; total += m.len(); } Ok(total) } fn is_not_found(e: &walkdir::Error) -> bool { let Some(io_error) = e.io_error() else { return false; }; let kind = io_error.kind(); matches!(kind, std::io::ErrorKind::NotFound) } pub struct Statvfs { pub blocks: u64, pub blocks_available: u64, pub fragment_size: u64, pub block_size: u64, } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/lib.rs
pageserver/src/lib.rs
#![recursion_limit = "300"] #![deny(clippy::undocumented_unsafe_blocks)] mod auth; pub mod basebackup; pub mod basebackup_cache; pub mod config; pub mod consumption_metrics; pub mod context; pub mod controller_upcall_client; pub mod deletion_queue; pub mod disk_usage_eviction_task; pub mod feature_resolver; pub mod http; pub mod import_datadir; pub mod l0_flush; extern crate hyper0 as hyper; use futures::StreamExt; use futures::stream::FuturesUnordered; pub use pageserver_api::keyspace; use tokio_util::sync::CancellationToken; mod assert_u64_eq_usize; pub mod aux_file; pub mod metrics; pub mod page_cache; pub mod page_service; pub mod pgdatadir_mapping; pub mod span; pub(crate) mod statvfs; pub mod task_mgr; pub mod tenant; pub mod utilization; pub mod virtual_file; pub mod walingest; pub mod walredo; use camino::Utf8Path; use deletion_queue::DeletionQueue; use postgres_ffi::PgMajorVersion; use tenant::mgr::{BackgroundPurges, TenantManager}; use tenant::secondary; use tracing::{info, info_span}; /// Current storage format version /// /// This is embedded in the header of all the layer files. /// If you make any backwards-incompatible changes to the storage /// format, bump this! /// Note that TimelineMetadata uses its own version number to track /// backwards-compatible changes to the metadata format. pub const STORAGE_FORMAT_VERSION: u16 = 3; pub const DEFAULT_PG_VERSION: PgMajorVersion = PgMajorVersion::PG17; // Magic constants used to identify different kinds of files pub const IMAGE_FILE_MAGIC: u16 = 0x5A60; pub const DELTA_FILE_MAGIC: u16 = 0x5A61; // Target used for performance traces. pub const PERF_TRACE_TARGET: &str = "P"; static ZERO_PAGE: bytes::Bytes = bytes::Bytes::from_static(&[0u8; 8192]); pub use crate::metrics::preinitialize_metrics; pub struct CancellableTask { pub task: tokio::task::JoinHandle<()>, pub cancel: CancellationToken, } pub struct HttpEndpointListener(pub CancellableTask); pub struct HttpsEndpointListener(pub CancellableTask); pub struct ConsumptionMetricsTasks(pub CancellableTask); pub struct DiskUsageEvictionTask(pub CancellableTask); // HADRON pub struct MetricsCollectionTask(pub CancellableTask); impl CancellableTask { pub async fn shutdown(self) { self.cancel.cancel(); self.task.await.unwrap(); } } #[tracing::instrument(skip_all, fields(%exit_code))] #[allow(clippy::too_many_arguments)] pub async fn shutdown_pageserver( http_listener: HttpEndpointListener, https_listener: Option<HttpsEndpointListener>, page_service: page_service::Listener, grpc_task: Option<CancellableTask>, metrics_collection_task: MetricsCollectionTask, consumption_metrics_worker: ConsumptionMetricsTasks, disk_usage_eviction_task: Option<DiskUsageEvictionTask>, tenant_manager: &TenantManager, background_purges: BackgroundPurges, mut deletion_queue: DeletionQueue, secondary_controller_tasks: secondary::GlobalTasks, exit_code: i32, ) { use std::time::Duration; let started_at = std::time::Instant::now(); // If the orderly shutdown below takes too long, we still want to make // sure that all walredo processes are killed and wait()ed on by us, not systemd. // // (Leftover walredo processes are the hypothesized trigger for the systemd freezes // that we keep seeing in prod => https://github.com/neondatabase/cloud/issues/11387. // // We use a thread instead of a tokio task because the background runtime is likely busy // with the final flushing / uploads. This activity here has priority, and due to lack // of scheduling priority feature sin the tokio scheduler, using a separate thread is // an effective priority booster. let walredo_extraordinary_shutdown_thread_span = { let span = info_span!(parent: None, "walredo_extraordinary_shutdown_thread"); span.follows_from(tracing::Span::current()); span }; let walredo_extraordinary_shutdown_thread_cancel = CancellationToken::new(); let walredo_extraordinary_shutdown_thread = std::thread::spawn({ let walredo_extraordinary_shutdown_thread_cancel = walredo_extraordinary_shutdown_thread_cancel.clone(); move || { let rt = tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap(); let _entered = rt.enter(); let _entered = walredo_extraordinary_shutdown_thread_span.enter(); if let Ok(()) = rt.block_on(tokio::time::timeout( Duration::from_secs(8), walredo_extraordinary_shutdown_thread_cancel.cancelled(), )) { info!("cancellation requested"); return; } let managers = tenant::WALREDO_MANAGERS .lock() .unwrap() // prevents new walredo managers from being inserted .take() .expect("only we take()"); // Use FuturesUnordered to get in queue early for each manager's // heavier_once_cell semaphore wait list. // Also, for idle tenants that for some reason haven't // shut down yet, it's quite likely that we're not going // to get Poll::Pending once. let mut futs: FuturesUnordered<_> = managers .into_iter() .filter_map(|(_, mgr)| mgr.upgrade()) .map(|mgr| async move { tokio::task::unconstrained(mgr.shutdown()).await }) .collect(); info!(count=%futs.len(), "built FuturesUnordered"); let mut last_log_at = std::time::Instant::now(); #[derive(Debug, Default)] struct Results { initiated: u64, already: u64, } let mut results = Results::default(); while let Some(we_initiated) = rt.block_on(futs.next()) { if we_initiated { results.initiated += 1; } else { results.already += 1; } if last_log_at.elapsed() > Duration::from_millis(100) { info!(remaining=%futs.len(), ?results, "progress"); last_log_at = std::time::Instant::now(); } } info!(?results, "done"); } }); // Shut down the libpq endpoint task. This prevents new connections from // being accepted. let remaining_connections = timed( page_service.stop_accepting(), "shutdown LibpqEndpointListener", Duration::from_secs(1), ) .await; // Shut down the gRPC server task, including request handlers. if let Some(grpc_task) = grpc_task { timed( grpc_task.shutdown(), "shutdown gRPC PageRequestHandler", Duration::from_secs(3), ) .await; } // Shut down all the tenants. This flushes everything to disk and kills // the checkpoint and GC tasks. timed( tenant_manager.shutdown(), "shutdown all tenants", Duration::from_secs(5), ) .await; // Shut down any page service tasks: any in-progress work for particular timelines or tenants // should already have been canclled via mgr::shutdown_all_tenants timed( remaining_connections.shutdown(), "shutdown PageRequestHandlers", Duration::from_secs(1), ) .await; // Best effort to persist any outstanding deletions, to avoid leaking objects deletion_queue.shutdown(Duration::from_secs(5)).await; // HADRON timed( metrics_collection_task.0.shutdown(), "shutdown metrics collections metrics", Duration::from_secs(1), ) .await; timed( consumption_metrics_worker.0.shutdown(), "shutdown consumption metrics", Duration::from_secs(1), ) .await; timed( futures::future::OptionFuture::from(disk_usage_eviction_task.map(|t| t.0.shutdown())), "shutdown disk usage eviction", Duration::from_secs(1), ) .await; timed( background_purges.shutdown(), "shutdown background purges", Duration::from_secs(1), ) .await; if let Some(https_listener) = https_listener { timed( https_listener.0.shutdown(), "shutdown https", Duration::from_secs(1), ) .await; } // Shut down the HTTP endpoint last, so that you can still check the server's // status while it's shutting down. // FIXME: We should probably stop accepting commands like attach/detach earlier. timed( http_listener.0.shutdown(), "shutdown http", Duration::from_secs(1), ) .await; timed( secondary_controller_tasks.wait(), // cancellation happened in caller "secondary controller wait", Duration::from_secs(1), ) .await; // There should be nothing left, but let's be sure timed( task_mgr::shutdown_tasks(None, None, None), "shutdown leftovers", Duration::from_secs(1), ) .await; info!("cancel & join walredo_extraordinary_shutdown_thread"); walredo_extraordinary_shutdown_thread_cancel.cancel(); walredo_extraordinary_shutdown_thread.join().unwrap(); info!("walredo_extraordinary_shutdown_thread done"); info!( elapsed_ms = started_at.elapsed().as_millis(), "Shut down successfully completed" ); std::process::exit(exit_code); } /// Per-tenant configuration file. /// Full path: `tenants/<tenant_id>/config-v1`. pub(crate) const TENANT_LOCATION_CONFIG_NAME: &str = "config-v1"; /// Per-tenant copy of their remote heatmap, downloaded into the local /// tenant path while in secondary mode. pub(crate) const TENANT_HEATMAP_BASENAME: &str = "heatmap-v1.json"; /// A suffix used for various temporary files. Any temporary files found in the /// data directory at pageserver startup can be automatically removed. pub(crate) const TEMP_FILE_SUFFIX: &str = "___temp"; pub fn is_temporary(path: &Utf8Path) -> bool { match path.file_name() { Some(name) => name.ends_with(TEMP_FILE_SUFFIX), None => false, } } /// During pageserver startup, we need to order operations not to exhaust tokio worker threads by /// blocking. /// /// The instances of this value exist only during startup, otherwise `None` is provided, meaning no /// delaying is needed. #[derive(Clone)] pub struct InitializationOrder { /// Each initial tenant load task carries this until it is done loading timelines from remote storage pub initial_tenant_load_remote: Option<utils::completion::Completion>, /// Each initial tenant load task carries this until completion. pub initial_tenant_load: Option<utils::completion::Completion>, /// Barrier for when we can start any background jobs. /// /// This can be broken up later on, but right now there is just one class of a background job. pub background_jobs_can_start: utils::completion::Barrier, } /// Time the future with a warning when it exceeds a threshold. async fn timed<Fut: std::future::Future>( fut: Fut, name: &str, warn_at: std::time::Duration, ) -> <Fut as std::future::Future>::Output { let started = std::time::Instant::now(); let mut fut = std::pin::pin!(fut); match tokio::time::timeout(warn_at, &mut fut).await { Ok(ret) => { tracing::info!( stage = name, elapsed_ms = started.elapsed().as_millis(), "completed" ); ret } Err(_) => { tracing::info!( stage = name, elapsed_ms = started.elapsed().as_millis(), "still waiting, taking longer than expected..." ); let ret = fut.await; // this has a global allowed_errors tracing::warn!( stage = name, elapsed_ms = started.elapsed().as_millis(), "completed, took longer than expected" ); ret } } } /// Like [`timed`], but the warning timeout only starts after `cancel` has been cancelled. async fn timed_after_cancellation<Fut: std::future::Future>( fut: Fut, name: &str, warn_at: std::time::Duration, cancel: &CancellationToken, ) -> <Fut as std::future::Future>::Output { let mut fut = std::pin::pin!(fut); tokio::select! { _ = cancel.cancelled() => { timed(fut, name, warn_at).await } ret = &mut fut => { ret } } } #[cfg(test)] mod timed_tests { use std::time::Duration; use super::timed; #[tokio::test] async fn timed_completes_when_inner_future_completes() { // A future that completes on time should have its result returned let r1 = timed( async move { tokio::time::sleep(Duration::from_millis(10)).await; 123 }, "test 1", Duration::from_millis(50), ) .await; assert_eq!(r1, 123); // A future that completes too slowly should also have its result returned let r1 = timed( async move { tokio::time::sleep(Duration::from_millis(50)).await; 456 }, "test 1", Duration::from_millis(10), ) .await; assert_eq!(r1, 456); } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/disk_usage_eviction_task.rs
pageserver/src/disk_usage_eviction_task.rs
//! This module implements the pageserver-global disk-usage-based layer eviction task. //! //! # Mechanics //! //! Function `launch_disk_usage_global_eviction_task` starts a pageserver-global background //! loop that evicts layers in response to a shortage of available bytes //! in the $repo/tenants directory's filesystem. //! //! The loop runs periodically at a configurable `period`. //! //! Each loop iteration uses `statvfs` to determine filesystem-level space usage. //! It compares the returned usage data against two different types of thresholds. //! The iteration tries to evict layers until app-internal accounting says we should be below the thresholds. //! We cross-check this internal accounting with the real world by making another `statvfs` at the end of the iteration. //! We're good if that second statvfs shows that we're _actually_ below the configured thresholds. //! If we're still above one or more thresholds, we emit a warning log message, leaving it to the operator to investigate further. //! //! # Eviction Policy //! //! There are two thresholds: //! `max_usage_pct` is the relative available space, expressed in percent of the total filesystem space. //! If the actual usage is higher, the threshold is exceeded. //! `min_avail_bytes` is the absolute available space in bytes. //! If the actual usage is lower, the threshold is exceeded. //! If either of these thresholds is exceeded, the system is considered to have "disk pressure", and eviction //! is performed on the next iteration, to release disk space and bring the usage below the thresholds again. //! The iteration evicts layers in LRU fashion, but, with a weak reservation per tenant. //! The reservation is to keep the most recently accessed X bytes per tenant resident. //! If we cannot relieve pressure by evicting layers outside of the reservation, we //! start evicting layers that are part of the reservation, LRU first. //! //! The value for the per-tenant reservation is referred to as `tenant_min_resident_size` //! throughout the code, but, no actual variable carries that name. //! The per-tenant default value is the `max(tenant's layer file sizes, regardless of local or remote)`. //! The idea is to allow at least one layer to be resident per tenant, to ensure it can make forward progress //! during page reconstruction. //! An alternative default for all tenants can be specified in the `tenant_config` section of the config. //! Lastly, each tenant can have an override in their respective tenant config (`min_resident_size_override`). // Implementation notes: // - The `#[allow(dead_code)]` above various structs are to suppress warnings about only the Debug impl // reading these fields. We use the Debug impl for semi-structured logging, though. use std::sync::Arc; use std::time::SystemTime; use anyhow::Context; use pageserver_api::config::DiskUsageEvictionTaskConfig; use pageserver_api::shard::TenantShardId; use remote_storage::GenericRemoteStorage; use serde::Serialize; use tokio::time::Instant; use tokio_util::sync::CancellationToken; use tracing::{Instrument, debug, error, info, instrument, warn}; use utils::completion; use utils::id::TimelineId; use crate::config::PageServerConf; use crate::metrics::disk_usage_based_eviction::METRICS; use crate::task_mgr::{self, BACKGROUND_RUNTIME}; use crate::tenant::mgr::TenantManager; use crate::tenant::remote_timeline_client::LayerFileMetadata; use crate::tenant::secondary::SecondaryTenant; use crate::tenant::storage_layer::{ AsLayerDesc, EvictionError, Layer, LayerName, LayerVisibilityHint, }; use crate::tenant::tasks::sleep_random; use crate::{CancellableTask, DiskUsageEvictionTask}; /// Selects the sort order for eviction candidates *after* per tenant `min_resident_size` /// partitioning. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum EvictionOrder { /// Order the layers to be evicted by how recently they have been accessed relatively within /// the set of resident layers of a tenant. RelativeAccessed { /// Determines if the tenant with most layers should lose first. /// /// Having this enabled is currently the only reasonable option, because the order in which /// we read tenants is deterministic. If we find the need to use this as `false`, we need /// to ensure nondeterminism by adding in a random number to break the /// `relative_last_activity==0.0` ties. highest_layer_count_loses_first: bool, }, } impl From<pageserver_api::config::EvictionOrder> for EvictionOrder { fn from(value: pageserver_api::config::EvictionOrder) -> Self { match value { pageserver_api::config::EvictionOrder::RelativeAccessed { highest_layer_count_loses_first, } => Self::RelativeAccessed { highest_layer_count_loses_first, }, } } } impl EvictionOrder { fn sort(&self, candidates: &mut [(EvictionPartition, EvictionCandidate)]) { use EvictionOrder::*; match self { RelativeAccessed { .. } => candidates.sort_unstable_by_key(|(partition, candidate)| { (*partition, candidate.relative_last_activity) }), } } /// Called to fill in the [`EvictionCandidate::relative_last_activity`] while iterating tenants /// layers in **most** recently used order. fn relative_last_activity(&self, total: usize, index: usize) -> finite_f32::FiniteF32 { use EvictionOrder::*; match self { RelativeAccessed { highest_layer_count_loses_first, } => { // keeping the -1 or not decides if every tenant should lose their least recently accessed // layer OR if this should happen in the order of having highest layer count: let fudge = if *highest_layer_count_loses_first { // relative_last_activity vs. tenant layer count: // - 0.1..=1.0 (10 layers) // - 0.01..=1.0 (100 layers) // - 0.001..=1.0 (1000 layers) // // leading to evicting less of the smallest tenants. 0 } else { // use full 0.0..=1.0 range, which means even the smallest tenants could always lose a // layer. the actual ordering is unspecified: for 10k tenants on a pageserver it could // be that less than 10k layer evictions is enough, so we would not need to evict from // all tenants. // // as the tenant ordering is now deterministic this could hit the same tenants // disproportionetly on multiple invocations. alternative could be to remember how many // layers did we evict last time from this tenant, and inject that as an additional // fudge here. 1 }; let total = total.checked_sub(fudge).filter(|&x| x > 1).unwrap_or(1); let divider = total as f32; // most recently used is always (total - 0) / divider == 1.0 // least recently used depends on the fudge: // - (total - 1) - (total - 1) / total => 0 / total // - total - (total - 1) / total => 1 / total let distance = (total - index) as f32; finite_f32::FiniteF32::try_from_normalized(distance / divider) .unwrap_or_else(|val| { tracing::warn!(%fudge, "calculated invalid relative_last_activity for i={index}, total={total}: {val}"); finite_f32::FiniteF32::ZERO }) } } } } #[derive(Default)] pub struct State { /// Exclude http requests and background task from running at the same time. mutex: tokio::sync::Mutex<()>, } pub fn launch_disk_usage_global_eviction_task( conf: &'static PageServerConf, storage: GenericRemoteStorage, state: Arc<State>, tenant_manager: Arc<TenantManager>, background_jobs_barrier: completion::Barrier, ) -> Option<DiskUsageEvictionTask> { let task_config = &conf.disk_usage_based_eviction; if !task_config.enabled { info!("disk usage based eviction task not configured"); return None; }; info!("launching disk usage based eviction task"); let cancel = CancellationToken::new(); let task = BACKGROUND_RUNTIME.spawn(task_mgr::exit_on_panic_or_error( "disk usage based eviction", { let cancel = cancel.clone(); async move { // wait until initial load is complete, because we cannot evict from loading tenants. tokio::select! { _ = cancel.cancelled() => { return anyhow::Ok(()); }, _ = background_jobs_barrier.wait() => { } }; disk_usage_eviction_task(&state, task_config, &storage, tenant_manager, cancel) .await; anyhow::Ok(()) } }, )); Some(DiskUsageEvictionTask(CancellableTask { cancel, task })) } #[instrument(skip_all)] async fn disk_usage_eviction_task( state: &State, task_config: &DiskUsageEvictionTaskConfig, storage: &GenericRemoteStorage, tenant_manager: Arc<TenantManager>, cancel: CancellationToken, ) { scopeguard::defer! { info!("disk usage based eviction task finishing"); }; if sleep_random(task_config.period, &cancel).await.is_err() { return; } let mut iteration_no = 0; loop { iteration_no += 1; let start = Instant::now(); async { let res = disk_usage_eviction_task_iteration( state, task_config, storage, &tenant_manager, &cancel, ) .await; match res { Ok(()) => {} Err(e) => { // these stat failures are expected to be very rare warn!("iteration failed, unexpected error: {e:#}"); } } } .instrument(tracing::info_span!("iteration", iteration_no)) .await; let sleep_until = start + task_config.period; if tokio::time::timeout_at(sleep_until, cancel.cancelled()) .await .is_ok() { break; } } } pub trait Usage: Clone + Copy + std::fmt::Debug { fn has_pressure(&self) -> bool; fn add_available_bytes(&mut self, bytes: u64); } async fn disk_usage_eviction_task_iteration( state: &State, task_config: &DiskUsageEvictionTaskConfig, storage: &GenericRemoteStorage, tenant_manager: &Arc<TenantManager>, cancel: &CancellationToken, ) -> anyhow::Result<()> { let tenants_dir = tenant_manager.get_conf().tenants_path(); let usage_pre = filesystem_level_usage::get(&tenants_dir, task_config) .context("get filesystem-level disk usage before evictions")?; let res = disk_usage_eviction_task_iteration_impl( state, storage, usage_pre, tenant_manager, task_config.eviction_order.into(), cancel, ) .await; match res { Ok(outcome) => { debug!(?outcome, "disk_usage_eviction_iteration finished"); match outcome { IterationOutcome::NoPressure | IterationOutcome::Cancelled => { // nothing to do, select statement below will handle things } IterationOutcome::Finished(outcome) => { // Verify with statvfs whether we made any real progress let after = filesystem_level_usage::get(&tenants_dir, task_config) // It's quite unlikely to hit the error here. Keep the code simple and bail out. .context("get filesystem-level disk usage after evictions")?; debug!(?after, "disk usage"); if after.has_pressure() { // Don't bother doing an out-of-order iteration here now. // In practice, the task period is set to a value in the tens-of-seconds range, // which will cause another iteration to happen soon enough. // TODO: deltas between the three different usages would be helpful, // consider MiB, GiB, TiB warn!(?outcome, ?after, "disk usage still high"); } else { info!(?outcome, ?after, "disk usage pressure relieved"); } } } } Err(e) => { error!("disk_usage_eviction_iteration failed: {:#}", e); } } Ok(()) } #[derive(Debug, Serialize)] #[allow(clippy::large_enum_variant)] pub enum IterationOutcome<U> { NoPressure, Cancelled, Finished(IterationOutcomeFinished<U>), } #[derive(Debug, Serialize)] pub struct IterationOutcomeFinished<U> { /// The actual usage observed before we started the iteration. before: U, /// The expected value for `after`, according to internal accounting, after phase 1. planned: PlannedUsage<U>, /// The outcome of phase 2, where we actually do the evictions. /// /// If all layers that phase 1 planned to evict _can_ actually get evicted, this will /// be the same as `planned`. assumed: AssumedUsage<U>, } #[derive(Debug, Serialize)] struct AssumedUsage<U> { /// The expected value for `after`, after phase 2. projected_after: U, /// The layers we failed to evict during phase 2. failed: LayerCount, } #[derive(Debug, Serialize)] struct PlannedUsage<U> { respecting_tenant_min_resident_size: U, fallback_to_global_lru: Option<U>, } #[derive(Debug, Default, Serialize)] struct LayerCount { file_sizes: u64, count: usize, } pub(crate) async fn disk_usage_eviction_task_iteration_impl<U: Usage>( state: &State, _storage: &GenericRemoteStorage, usage_pre: U, tenant_manager: &Arc<TenantManager>, eviction_order: EvictionOrder, cancel: &CancellationToken, ) -> anyhow::Result<IterationOutcome<U>> { // use tokio's mutex to get a Sync guard (instead of std::sync::Mutex) let _g = state .mutex .try_lock() .map_err(|_| anyhow::anyhow!("iteration is already executing"))?; debug!(?usage_pre, "disk usage"); if !usage_pre.has_pressure() { return Ok(IterationOutcome::NoPressure); } warn!( ?usage_pre, "running disk usage based eviction due to pressure" ); let (candidates, collection_time) = { let started_at = std::time::Instant::now(); match collect_eviction_candidates(tenant_manager, eviction_order, cancel).await? { EvictionCandidates::Cancelled => { return Ok(IterationOutcome::Cancelled); } EvictionCandidates::Finished(partitioned) => (partitioned, started_at.elapsed()), } }; METRICS.layers_collected.inc_by(candidates.len() as u64); tracing::info!( elapsed_ms = collection_time.as_millis(), total_layers = candidates.len(), "collection completed" ); // Debug-log the list of candidates let now = SystemTime::now(); for (i, (partition, candidate)) in candidates.iter().enumerate() { let nth = i + 1; let total_candidates = candidates.len(); let size = candidate.layer.get_file_size(); let rel = candidate.relative_last_activity; debug!( "cand {nth}/{total_candidates}: size={size}, rel_last_activity={rel}, no_access_for={}us, partition={partition:?}, {}/{}/{}", now.duration_since(candidate.last_activity_ts) .unwrap() .as_micros(), candidate.layer.get_tenant_shard_id(), candidate.layer.get_timeline_id(), candidate.layer.get_name(), ); } // phase1: select victims to relieve pressure // // Walk through the list of candidates, until we have accumulated enough layers to get // us back under the pressure threshold. 'usage_planned' is updated so that it tracks // how much disk space would be used after evicting all the layers up to the current // point in the list. // // If we get far enough in the list that we start to evict layers that are below // the tenant's min-resident-size threshold, print a warning, and memorize the disk // usage at that point, in 'usage_planned_min_resident_size_respecting'. let (evicted_amount, usage_planned) = select_victims(&candidates, usage_pre).into_amount_and_planned(); METRICS.layers_selected.inc_by(evicted_amount as u64); // phase2: evict layers let mut js = tokio::task::JoinSet::new(); let limit = 1000; let mut evicted = candidates.into_iter().take(evicted_amount).fuse(); let mut consumed_all = false; // After the evictions, `usage_assumed` is the post-eviction usage, // according to internal accounting. let mut usage_assumed = usage_pre; let mut evictions_failed = LayerCount::default(); let evict_layers = async move { loop { let next = if js.len() >= limit || consumed_all { js.join_next().await } else if !js.is_empty() { // opportunistically consume ready result, one per each new evicted futures::future::FutureExt::now_or_never(js.join_next()).and_then(|x| x) } else { None }; if let Some(next) = next { match next { Ok(Ok(file_size)) => { METRICS.layers_evicted.inc(); /*BEGIN_HADRON */ METRICS.bytes_evicted.inc_by(file_size); /*END_HADRON */ usage_assumed.add_available_bytes(file_size); } Ok(Err(( file_size, EvictionError::NotFound | EvictionError::Downloaded | EvictionError::Timeout, ))) => { evictions_failed.file_sizes += file_size; evictions_failed.count += 1; } Err(je) if je.is_cancelled() => unreachable!("not used"), Err(je) if je.is_panic() => { /* already logged */ } Err(je) => tracing::error!("unknown JoinError: {je:?}"), } } if consumed_all && js.is_empty() { break; } // calling again when consumed_all is fine as evicted is fused. let Some((_partition, candidate)) = evicted.next() else { if !consumed_all { tracing::info!("all evictions started, waiting"); consumed_all = true; } continue; }; match candidate.layer { EvictionLayer::Attached(layer) => { let file_size = layer.layer_desc().file_size; js.spawn(async move { // have a low eviction waiting timeout because our LRU calculations go stale fast; // also individual layer evictions could hang because of bugs and we do not want to // pause disk_usage_based_eviction for such. let timeout = std::time::Duration::from_secs(5); match layer.evict_and_wait(timeout).await { Ok(()) => Ok(file_size), Err(e) => Err((file_size, e)), } }); } EvictionLayer::Secondary(layer) => { let file_size = layer.metadata.file_size; js.spawn(async move { layer .secondary_tenant .evict_layer(layer.timeline_id, layer.name) .await; Ok(file_size) }); } } tokio::task::yield_now().await; } (usage_assumed, evictions_failed) }; let started_at = std::time::Instant::now(); let evict_layers = async move { let mut evict_layers = std::pin::pin!(evict_layers); let maximum_expected = std::time::Duration::from_secs(10); let res = tokio::time::timeout(maximum_expected, &mut evict_layers).await; let tuple = if let Ok(tuple) = res { tuple } else { let elapsed = started_at.elapsed(); tracing::info!(elapsed_ms = elapsed.as_millis(), "still ongoing"); evict_layers.await }; let elapsed = started_at.elapsed(); tracing::info!(elapsed_ms = elapsed.as_millis(), "completed"); tuple }; let evict_layers = evict_layers.instrument(tracing::info_span!("evict_layers", layers=%evicted_amount)); let (usage_assumed, evictions_failed) = tokio::select! { tuple = evict_layers => { tuple }, _ = cancel.cancelled() => { // dropping joinset will abort all pending evict_and_waits and that is fine, our // requests will still stand return Ok(IterationOutcome::Cancelled); } }; Ok(IterationOutcome::Finished(IterationOutcomeFinished { before: usage_pre, planned: usage_planned, assumed: AssumedUsage { projected_after: usage_assumed, failed: evictions_failed, }, })) } #[derive(Clone)] pub(crate) struct EvictionSecondaryLayer { pub(crate) secondary_tenant: Arc<SecondaryTenant>, pub(crate) timeline_id: TimelineId, pub(crate) name: LayerName, pub(crate) metadata: LayerFileMetadata, } /// Full [`Layer`] objects are specific to tenants in attached mode. This type is a layer /// of indirection to store either a `Layer`, or a reference to a secondary tenant and a layer name. #[derive(Clone)] pub(crate) enum EvictionLayer { Attached(Layer), Secondary(EvictionSecondaryLayer), } impl From<Layer> for EvictionLayer { fn from(value: Layer) -> Self { Self::Attached(value) } } impl EvictionLayer { pub(crate) fn get_tenant_shard_id(&self) -> &TenantShardId { match self { Self::Attached(l) => &l.layer_desc().tenant_shard_id, Self::Secondary(sl) => sl.secondary_tenant.get_tenant_shard_id(), } } pub(crate) fn get_timeline_id(&self) -> &TimelineId { match self { Self::Attached(l) => &l.layer_desc().timeline_id, Self::Secondary(sl) => &sl.timeline_id, } } pub(crate) fn get_name(&self) -> LayerName { match self { Self::Attached(l) => l.layer_desc().layer_name(), Self::Secondary(sl) => sl.name.clone(), } } pub(crate) fn get_file_size(&self) -> u64 { match self { Self::Attached(l) => l.layer_desc().file_size, Self::Secondary(sl) => sl.metadata.file_size, } } } #[derive(Clone)] pub(crate) struct EvictionCandidate { pub(crate) layer: EvictionLayer, pub(crate) last_activity_ts: SystemTime, pub(crate) relative_last_activity: finite_f32::FiniteF32, pub(crate) visibility: LayerVisibilityHint, } impl std::fmt::Display for EvictionLayer { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Self::Attached(l) => l.fmt(f), Self::Secondary(sl) => { write!(f, "{}/{}", sl.timeline_id, sl.name) } } } } #[derive(Default)] pub(crate) struct DiskUsageEvictionInfo { /// Timeline's largest layer (remote or resident) pub max_layer_size: Option<u64>, /// Timeline's resident layers pub resident_layers: Vec<EvictionCandidate>, } impl std::fmt::Debug for EvictionCandidate { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // format the tv_sec, tv_nsec into rfc3339 in case someone is looking at it // having to allocate a string to this is bad, but it will rarely be formatted let ts = chrono::DateTime::<chrono::Utc>::from(self.last_activity_ts); let ts = ts.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true); struct DisplayIsDebug<'a, T>(&'a T); impl<T: std::fmt::Display> std::fmt::Debug for DisplayIsDebug<'_, T> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } f.debug_struct("LocalLayerInfoForDiskUsageEviction") .field("layer", &DisplayIsDebug(&self.layer)) .field("last_activity", &ts) .finish() } } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] enum EvictionPartition { // A layer that is un-wanted by the tenant: evict all these first, before considering // any other layers EvictNow, // Above the minimum size threshold: this layer is a candidate for eviction. Above, // Below the minimum size threshold: this layer should only be evicted if all the // tenants' layers above the minimum size threshold have already been considered. Below, } enum EvictionCandidates { Cancelled, Finished(Vec<(EvictionPartition, EvictionCandidate)>), } /// Gather the eviction candidates. /// /// The returned `Ok(EvictionCandidates::Finished(candidates))` is sorted in eviction /// order. A caller that evicts in that order, until pressure is relieved, implements /// the eviction policy outlined in the module comment. /// /// # Example with EvictionOrder::AbsoluteAccessed /// /// Imagine that there are two tenants, A and B, with five layers each, a-e. /// Each layer has size 100, and both tenant's min_resident_size is 150. /// The eviction order would be /// /// ```text /// partition last_activity_ts tenant/layer /// Above 18:30 A/c /// Above 19:00 A/b /// Above 18:29 B/c /// Above 19:05 B/b /// Above 20:00 B/a /// Above 20:03 A/a /// Below 20:30 A/d /// Below 20:40 B/d /// Below 20:45 B/e /// Below 20:58 A/e /// ``` /// /// Now, if we need to evict 300 bytes to relieve pressure, we'd evict `A/c, A/b, B/c`. /// They are all in the `Above` partition, so, we respected each tenant's min_resident_size. /// /// But, if we need to evict 900 bytes to relieve pressure, we'd evict /// `A/c, A/b, B/c, B/b, B/a, A/a, A/d, B/d, B/e`, reaching into the `Below` partition /// after exhauting the `Above` partition. /// So, we did not respect each tenant's min_resident_size. /// /// # Example with EvictionOrder::RelativeAccessed /// /// ```text /// partition relative_age last_activity_ts tenant/layer /// Above 0/4 18:30 A/c /// Above 0/4 18:29 B/c /// Above 1/4 19:00 A/b /// Above 1/4 19:05 B/b /// Above 2/4 20:00 B/a /// Above 2/4 20:03 A/a /// Below 3/4 20:30 A/d /// Below 3/4 20:40 B/d /// Below 4/4 20:45 B/e /// Below 4/4 20:58 A/e /// ``` /// /// With tenants having the same number of layers the picture does not change much. The same with /// A having many more layers **resident** (not all of them listed): /// /// ```text /// Above 0/100 18:30 A/c /// Above 0/4 18:29 B/c /// Above 1/100 19:00 A/b /// Above 2/100 20:03 A/a /// Above 3/100 20:03 A/nth_3 /// Above 4/100 20:03 A/nth_4 /// ... /// Above 1/4 19:05 B/b /// Above 25/100 20:04 A/nth_25 /// ... /// Above 2/4 20:00 B/a /// Above 50/100 20:10 A/nth_50 /// ... /// Below 3/4 20:40 B/d /// Below 99/100 20:30 A/nth_99 /// Below 4/4 20:45 B/e /// Below 100/100 20:58 A/nth_100 /// ``` /// /// Now it's easier to see that because A has grown fast it has more layers to get evicted. What is /// difficult to see is what happens on the next round assuming the evicting 23 from the above list /// relieves the pressure (22 A layers gone, 1 B layers gone) but a new fast growing tenant C has /// appeared: /// /// ```text /// Above 0/87 20:04 A/nth_23 /// Above 0/3 19:05 B/b /// Above 0/50 20:59 C/nth_0 /// Above 1/87 20:04 A/nth_24 /// Above 1/50 21:00 C/nth_1 /// Above 2/87 20:04 A/nth_25 /// ... /// Above 16/50 21:02 C/nth_16 /// Above 1/3 20:00 B/a /// Above 27/87 20:10 A/nth_50 /// ... /// Below 2/3 20:40 B/d /// Below 49/50 21:05 C/nth_49 /// Below 86/87 20:30 A/nth_99 /// Below 3/3 20:45 B/e /// Below 50/50 21:05 C/nth_50 /// Below 87/87 20:58 A/nth_100 /// ``` /// /// Now relieving pressure with 23 layers would cost: /// - tenant A 14 layers /// - tenant B 1 layer /// - tenant C 8 layers async fn collect_eviction_candidates( tenant_manager: &Arc<TenantManager>, eviction_order: EvictionOrder, cancel: &CancellationToken, ) -> anyhow::Result<EvictionCandidates> { const LOG_DURATION_THRESHOLD: std::time::Duration = std::time::Duration::from_secs(10); // get a snapshot of the list of tenants let tenants = tenant_manager .list_tenants() .context("get list of tenants")?; // TODO: avoid listing every layer in every tenant: this loop can block the executor, // and the resulting data structure can be huge. // (https://github.com/neondatabase/neon/issues/6224) let mut candidates = Vec::new(); for (tenant_id, _state, _gen) in tenants { if cancel.is_cancelled() { return Ok(EvictionCandidates::Cancelled); } let tenant = match tenant_manager.get_attached_tenant_shard(tenant_id) { Ok(tenant) if tenant.is_active() => tenant, Ok(_) => { debug!(tenant_id=%tenant_id.tenant_id, shard_id=%tenant_id.shard_slug(), "Tenant shard is not active"); continue; } Err(e) => { // this can happen if tenant has lifecycle transition after we fetched it debug!("failed to get tenant: {e:#}"); continue; } }; if tenant.cancel.is_cancelled() { info!(%tenant_id, "Skipping tenant for eviction, it is shutting down"); continue; } let started_at = std::time::Instant::now(); // collect layers from all timelines in this tenant // // If one of the timelines becomes `!is_active()` during the iteration, // for example because we're shutting down, then `max_layer_size` can be too small. // That's OK. This code only runs under a disk pressure situation, and being // a little unfair to tenants during shutdown in such a situation is tolerable. let mut tenant_candidates = Vec::new(); let mut max_layer_size = 0; for tl in tenant.list_timelines() { if !tl.is_active() { continue; } let info = tl.get_local_layers_for_disk_usage_eviction().await; debug!( tenant_id=%tl.tenant_shard_id.tenant_id, shard_id=%tl.tenant_shard_id.shard_slug(), timeline_id=%tl.timeline_id, "timeline resident layers count: {}", info.resident_layers.len() );
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/deletion_queue.rs
pageserver/src/deletion_queue.rs
mod deleter; mod list_writer; mod validator; use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; use anyhow::Context; use camino::Utf8PathBuf; use deleter::DeleterMessage; use list_writer::ListWriterQueueMessage; use pageserver_api::shard::TenantShardId; use remote_storage::{GenericRemoteStorage, RemotePath}; use serde::{Deserialize, Serialize}; use thiserror::Error; use tokio_util::sync::CancellationToken; use tracing::{Instrument, debug, error}; use utils::crashsafe::path_with_suffix_extension; use utils::generation::Generation; use utils::id::TimelineId; use utils::lsn::{AtomicLsn, Lsn}; use validator::ValidatorQueueMessage; use self::deleter::Deleter; use self::list_writer::{DeletionOp, ListWriter, RecoverOp}; use self::validator::Validator; use crate::config::PageServerConf; use crate::controller_upcall_client::StorageControllerUpcallApi; use crate::metrics; use crate::tenant::remote_timeline_client::{LayerFileMetadata, remote_timeline_path}; use crate::tenant::storage_layer::LayerName; use crate::virtual_file::{MaybeFatalIo, VirtualFile}; // TODO: configurable for how long to wait before executing deletions /// We aggregate object deletions from many tenants in one place, for several reasons: /// - Coalesce deletions into fewer DeleteObjects calls /// - Enable Tenant/Timeline lifetimes to be shorter than the time it takes /// to flush any outstanding deletions. /// - Globally control throughput of deletions, as these are a low priority task: do /// not compete with the same S3 clients/connections used for higher priority uploads. /// - Enable gating deletions on validation of a tenant's generation number, to make /// it safe to multi-attach tenants (see docs/rfcs/025-generation-numbers.md) /// /// There are two kinds of deletion: deferred and immediate. A deferred deletion /// may be intentionally delayed to protect passive readers of S3 data, and is /// subject to a generation number validation step. An immediate deletion is /// ready to execute immediately, and is only queued up so that it can be coalesced /// with other deletions in flight. /// /// Deferred deletions pass through three steps: /// - ListWriter: accumulate deletion requests from Timelines, and batch them up into /// DeletionLists, which are persisted to disk. /// - Validator: accumulate deletion lists, and validate them en-masse prior to passing /// the keys in the list onward for actual deletion. Also validate remote_consistent_lsn /// updates for running timelines. /// - Deleter: accumulate object keys that the validator has validated, and execute them in /// batches of 1000 keys via DeleteObjects. /// /// Non-deferred deletions, such as during timeline deletion, bypass the first /// two stages and are passed straight into the Deleter. /// /// Internally, each stage is joined by a channel to the next. On disk, there is only /// one queue (of DeletionLists), which is written by the frontend and consumed /// by the backend. #[derive(Clone)] pub struct DeletionQueue { client: DeletionQueueClient, // Parent cancellation token for the tokens passed into background workers cancel: CancellationToken, } /// Opaque wrapper around individual worker tasks, to avoid making the /// worker objects themselves public pub struct DeletionQueueWorkers<C> where C: StorageControllerUpcallApi + Send + Sync, { frontend: ListWriter, backend: Validator<C>, executor: Deleter, } impl<C> DeletionQueueWorkers<C> where C: StorageControllerUpcallApi + Send + Sync + 'static, { pub fn spawn_with(mut self, runtime: &tokio::runtime::Handle) -> tokio::task::JoinHandle<()> { let jh_frontend = runtime.spawn(async move { self.frontend .background() .instrument(tracing::info_span!(parent:None, "deletion frontend")) .await }); let jh_backend = runtime.spawn(async move { self.backend .background() .instrument(tracing::info_span!(parent:None, "deletion backend")) .await }); let jh_executor = runtime.spawn(async move { self.executor .background() .instrument(tracing::info_span!(parent:None, "deletion executor")) .await }); runtime.spawn({ async move { jh_frontend.await.expect("error joining frontend worker"); jh_backend.await.expect("error joining backend worker"); drop(jh_executor.await.expect("error joining executor worker")); } }) } } /// A FlushOp is just a oneshot channel, where we send the transmit side down /// another channel, and the receive side will receive a message when the channel /// we're flushing has reached the FlushOp we sent into it. /// /// The only extra behavior beyond the channel is that the notify() method does not /// return an error when the receive side has been dropped, because in this use case /// it is harmless (the code that initiated the flush no longer cares about the result). #[derive(Debug)] struct FlushOp { tx: tokio::sync::oneshot::Sender<()>, } impl FlushOp { fn new() -> (Self, tokio::sync::oneshot::Receiver<()>) { let (tx, rx) = tokio::sync::oneshot::channel::<()>(); (Self { tx }, rx) } fn notify(self) { if self.tx.send(()).is_err() { // oneshot channel closed. This is legal: a client could be destroyed while waiting for a flush. debug!("deletion queue flush from dropped client"); }; } } #[derive(Clone, Debug)] pub struct DeletionQueueClient { tx: tokio::sync::mpsc::UnboundedSender<ListWriterQueueMessage>, executor_tx: tokio::sync::mpsc::Sender<DeleterMessage>, lsn_table: Arc<std::sync::RwLock<VisibleLsnUpdates>>, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct TenantDeletionList { /// For each Timeline, a list of key fragments to append to the timeline remote path /// when reconstructing a full key timelines: HashMap<TimelineId, Vec<String>>, /// The generation in which this deletion was emitted: note that this may not be the /// same as the generation of any layers being deleted. The generation of the layer /// has already been absorbed into the keys in `objects` generation: Generation, } impl TenantDeletionList { pub(crate) fn len(&self) -> usize { self.timelines.values().map(|v| v.len()).sum() } } /// Files ending with this suffix will be ignored and erased /// during recovery as startup. const TEMP_SUFFIX: &str = "tmp"; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct DeletionList { /// Serialization version, for future use version: u8, /// Used for constructing a unique key for each deletion list we write out. sequence: u64, /// To avoid repeating tenant/timeline IDs in every key, we store keys in /// nested HashMaps by TenantTimelineID. Each Tenant only appears once /// with one unique generation ID: if someone tries to push a second generation /// ID for the same tenant, we will start a new DeletionList. tenants: HashMap<TenantShardId, TenantDeletionList>, /// Avoid having to walk `tenants` to calculate the number of keys in /// the nested deletion lists size: usize, /// Set to true when the list has undergone validation with the control /// plane and the remaining contents of `tenants` are valid. A list may /// also be implicitly marked valid by DeletionHeader.validated_sequence /// advancing to >= DeletionList.sequence #[serde(default)] #[serde(skip_serializing_if = "std::ops::Not::not")] validated: bool, } #[derive(Debug, Serialize, Deserialize)] struct DeletionHeader { /// Serialization version, for future use version: u8, /// The highest sequence number (inclusive) that has been validated. All deletion /// lists on disk with a sequence <= this value are safe to execute. validated_sequence: u64, } impl DeletionHeader { const VERSION_LATEST: u8 = 1; fn new(validated_sequence: u64) -> Self { Self { version: Self::VERSION_LATEST, validated_sequence, } } async fn save(&self, conf: &'static PageServerConf) -> anyhow::Result<()> { debug!("Saving deletion list header {:?}", self); let header_bytes = serde_json::to_vec(self).context("serialize deletion header")?; let header_path = conf.deletion_header_path(); let temp_path = path_with_suffix_extension(&header_path, TEMP_SUFFIX); VirtualFile::crashsafe_overwrite(header_path, temp_path, header_bytes) .await .maybe_fatal_err("save deletion header")?; Ok(()) } } impl DeletionList { const VERSION_LATEST: u8 = 1; fn new(sequence: u64) -> Self { Self { version: Self::VERSION_LATEST, sequence, tenants: HashMap::new(), size: 0, validated: false, } } fn is_empty(&self) -> bool { self.tenants.is_empty() } fn len(&self) -> usize { self.size } /// Returns true if the push was accepted, false if the caller must start a new /// deletion list. fn push( &mut self, tenant: &TenantShardId, timeline: &TimelineId, generation: Generation, objects: &mut Vec<RemotePath>, ) -> bool { if objects.is_empty() { // Avoid inserting an empty TimelineDeletionList: this preserves the property // that if we have no keys, then self.objects is empty (used in Self::is_empty) return true; } let tenant_entry = self .tenants .entry(*tenant) .or_insert_with(|| TenantDeletionList { timelines: HashMap::new(), generation, }); if tenant_entry.generation != generation { // Only one generation per tenant per list: signal to // caller to start a new list. return false; } let timeline_entry = tenant_entry.timelines.entry(*timeline).or_default(); let timeline_remote_path = remote_timeline_path(tenant, timeline); self.size += objects.len(); timeline_entry.extend(objects.drain(..).map(|p| { p.strip_prefix(&timeline_remote_path) .expect("Timeline paths always start with the timeline prefix") .to_string() })); true } fn into_remote_paths(self) -> Vec<RemotePath> { let mut result = Vec::new(); for (tenant, tenant_deletions) in self.tenants.into_iter() { for (timeline, timeline_layers) in tenant_deletions.timelines.into_iter() { let timeline_remote_path = remote_timeline_path(&tenant, &timeline); result.extend( timeline_layers .into_iter() .map(|l| timeline_remote_path.join(Utf8PathBuf::from(l))), ); } } result } async fn save(&self, conf: &'static PageServerConf) -> anyhow::Result<()> { let path = conf.deletion_list_path(self.sequence); let temp_path = path_with_suffix_extension(&path, TEMP_SUFFIX); let bytes = serde_json::to_vec(self).expect("Failed to serialize deletion list"); VirtualFile::crashsafe_overwrite(path, temp_path, bytes) .await .maybe_fatal_err("save deletion list") .map_err(Into::into) } } impl std::fmt::Display for DeletionList { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "DeletionList<seq={}, tenants={}, keys={}>", self.sequence, self.tenants.len(), self.size ) } } struct PendingLsn { projected: Lsn, result_slot: Arc<AtomicLsn>, } struct TenantLsnState { timelines: HashMap<TimelineId, PendingLsn>, // In what generation was the most recent update proposed? generation: Generation, } #[derive(Default)] struct VisibleLsnUpdates { tenants: HashMap<TenantShardId, TenantLsnState>, } impl VisibleLsnUpdates { fn new() -> Self { Self { tenants: HashMap::new(), } } } impl std::fmt::Debug for VisibleLsnUpdates { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "VisibleLsnUpdates({} tenants)", self.tenants.len()) } } #[derive(Error, Debug)] pub enum DeletionQueueError { #[error("Deletion queue unavailable during shutdown")] ShuttingDown, } impl DeletionQueueClient { /// This is cancel-safe. If you drop the future before it completes, the message /// is not pushed, although in the context of the deletion queue it doesn't matter: once /// we decide to do a deletion the decision is always final. fn do_push<T>( &self, queue: &tokio::sync::mpsc::UnboundedSender<T>, msg: T, ) -> Result<(), DeletionQueueError> { match queue.send(msg) { Ok(_) => Ok(()), Err(e) => { // This shouldn't happen, we should shut down all tenants before // we shut down the global delete queue. If we encounter a bug like this, // we may leak objects as deletions won't be processed. error!("Deletion queue closed while pushing, shutting down? ({e})"); Err(DeletionQueueError::ShuttingDown) } } } pub(crate) fn recover( &self, attached_tenants: HashMap<TenantShardId, Generation>, ) -> Result<(), DeletionQueueError> { self.do_push( &self.tx, ListWriterQueueMessage::Recover(RecoverOp { attached_tenants }), ) } /// When a Timeline wishes to update the remote_consistent_lsn that it exposes to the outside /// world, it must validate its generation number before doing so. Rather than do this synchronously, /// we allow the timeline to publish updates at will via this API, and then read back what LSN was most /// recently validated separately. /// /// In this function we publish the LSN to the `projected` field of the timeline's entry in the VisibleLsnUpdates. The /// backend will later wake up and notice that the tenant's generation requires validation. pub(crate) async fn update_remote_consistent_lsn( &self, tenant_shard_id: TenantShardId, timeline_id: TimelineId, current_generation: Generation, lsn: Lsn, result_slot: Arc<AtomicLsn>, ) { let mut locked = self .lsn_table .write() .expect("Lock should never be poisoned"); let tenant_entry = locked .tenants .entry(tenant_shard_id) .or_insert(TenantLsnState { timelines: HashMap::new(), generation: current_generation, }); if tenant_entry.generation != current_generation { // Generation might have changed if we were detached and then re-attached: in this case, // state from the previous generation cannot be trusted. tenant_entry.timelines.clear(); tenant_entry.generation = current_generation; } tenant_entry.timelines.insert( timeline_id, PendingLsn { projected: lsn, result_slot, }, ); } /// Submit a list of layers for deletion: this function will return before the deletion is /// persistent, but it may be executed at any time after this function enters: do not push /// layers until you're sure they can be deleted safely (i.e. remote metadata no longer /// references them). /// /// The `current_generation` is the generation of this pageserver's current attachment. The /// generations in `layers` are the generations in which those layers were written. pub(crate) fn push_layers( &self, tenant_shard_id: TenantShardId, timeline_id: TimelineId, current_generation: Generation, layers: Vec<(LayerName, LayerFileMetadata)>, ) -> Result<(), DeletionQueueError> { // None generations are not valid for attached tenants: they must always be attached in // a known generation. None generations are still permitted for layers in the index because // they may be historical. assert!(!current_generation.is_none()); metrics::DELETION_QUEUE .keys_submitted .inc_by(layers.len() as u64); self.do_push( &self.tx, ListWriterQueueMessage::Delete(DeletionOp { tenant_shard_id, timeline_id, layers, generation: current_generation, objects: Vec::new(), }), ) } /// This is cancel-safe. If you drop the future the flush may still happen in the background. async fn do_flush<T>( &self, queue: &tokio::sync::mpsc::UnboundedSender<T>, msg: T, rx: tokio::sync::oneshot::Receiver<()>, ) -> Result<(), DeletionQueueError> { self.do_push(queue, msg)?; if rx.await.is_err() { // This shouldn't happen if tenants are shut down before deletion queue. If we // encounter a bug like this, then a flusher will incorrectly believe it has flushed // when it hasn't, possibly leading to leaking objects. error!("Deletion queue dropped flush op while client was still waiting"); Err(DeletionQueueError::ShuttingDown) } else { Ok(()) } } /// Wait until all previous deletions are persistent (either executed, or written to a DeletionList) /// /// This is cancel-safe. If you drop the future the flush may still happen in the background. pub async fn flush(&self) -> Result<(), DeletionQueueError> { let (flush_op, rx) = FlushOp::new(); self.do_flush(&self.tx, ListWriterQueueMessage::Flush(flush_op), rx) .await } /// Issue a flush without waiting for it to complete. This is useful on advisory flushes where /// the caller wants to avoid the risk of waiting for lots of enqueued work, such as on tenant /// detach where flushing is nice but not necessary. /// /// This function provides no guarantees of work being done. pub fn flush_advisory(&self) { let (flush_op, _) = FlushOp::new(); // Transmit the flush message, ignoring any result (such as a closed channel during shutdown). drop(self.tx.send(ListWriterQueueMessage::FlushExecute(flush_op))); } // Wait until all previous deletions are executed pub(crate) async fn flush_execute(&self) -> Result<(), DeletionQueueError> { debug!("flush_execute: flushing to deletion lists..."); // Flush any buffered work to deletion lists self.flush().await?; // Flush the backend into the executor of deletion lists let (flush_op, rx) = FlushOp::new(); debug!("flush_execute: flushing backend..."); self.do_flush(&self.tx, ListWriterQueueMessage::FlushExecute(flush_op), rx) .await?; debug!("flush_execute: finished flushing backend..."); // Flush any immediate-mode deletions (the above backend flush will only flush // the executor if deletions had flowed through the backend) debug!("flush_execute: flushing execution..."); self.flush_immediate().await?; debug!("flush_execute: finished flushing execution..."); Ok(()) } /// This interface bypasses the persistent deletion queue, and any validation /// that this pageserver is still elegible to execute the deletions. It is for /// use in timeline deletions, where the control plane is telling us we may /// delete everything in the timeline. /// /// DO NOT USE THIS FROM GC OR COMPACTION CODE. Use the regular `push_layers`. pub(crate) async fn push_immediate( &self, objects: Vec<RemotePath>, ) -> Result<(), DeletionQueueError> { metrics::DELETION_QUEUE .keys_submitted .inc_by(objects.len() as u64); self.executor_tx .send(DeleterMessage::Delete(objects)) .await .map_err(|_| DeletionQueueError::ShuttingDown) } /// Companion to push_immediate. When this returns Ok, all prior objects sent /// into push_immediate have been deleted from remote storage. pub(crate) async fn flush_immediate(&self) -> Result<(), DeletionQueueError> { let (flush_op, rx) = FlushOp::new(); self.executor_tx .send(DeleterMessage::Flush(flush_op)) .await .map_err(|_| DeletionQueueError::ShuttingDown)?; rx.await.map_err(|_| DeletionQueueError::ShuttingDown) } } impl DeletionQueue { pub fn new_client(&self) -> DeletionQueueClient { self.client.clone() } /// Caller may use the returned object to construct clients with new_client. /// Caller should tokio::spawn the background() members of the two worker objects returned: /// we don't spawn those inside new() so that the caller can use their runtime/spans of choice. pub fn new<C>( remote_storage: GenericRemoteStorage, controller_upcall_client: C, conf: &'static PageServerConf, ) -> (Self, DeletionQueueWorkers<C>) where C: StorageControllerUpcallApi + Send + Sync, { // Unbounded channel: enables non-async functions to submit deletions. The actual length is // constrained by how promptly the ListWriter wakes up and drains it, which should be frequent // enough to avoid this taking pathologically large amount of memory. let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); // Shallow channel: it carries DeletionLists which each contain up to thousands of deletions let (backend_tx, backend_rx) = tokio::sync::mpsc::channel(16); // Shallow channel: it carries lists of paths, and we expect the main queueing to // happen in the backend (persistent), not in this queue. let (executor_tx, executor_rx) = tokio::sync::mpsc::channel(16); let lsn_table = Arc::new(std::sync::RwLock::new(VisibleLsnUpdates::new())); // The deletion queue has an independent cancellation token to // the general pageserver shutdown token, because it stays alive a bit // longer to flush after Tenants have all been torn down. let cancel = CancellationToken::new(); ( Self { client: DeletionQueueClient { tx, executor_tx: executor_tx.clone(), lsn_table: lsn_table.clone(), }, cancel: cancel.clone(), }, DeletionQueueWorkers { frontend: ListWriter::new(conf, rx, backend_tx, cancel.clone()), backend: Validator::new( conf, backend_rx, executor_tx, controller_upcall_client, lsn_table.clone(), cancel.clone(), ), executor: Deleter::new(remote_storage, executor_rx, cancel.clone()), }, ) } pub async fn shutdown(&mut self, timeout: Duration) { match tokio::time::timeout(timeout, self.client.flush()).await { Ok(Ok(())) => { tracing::info!("Deletion queue flushed successfully on shutdown") } Ok(Err(DeletionQueueError::ShuttingDown)) => { // This is not harmful for correctness, but is unexpected: the deletion // queue's workers should stay alive as long as there are any client handles instantiated. tracing::warn!("Deletion queue stopped prematurely"); } Err(_timeout) => { tracing::warn!("Timed out flushing deletion queue on shutdown") } } // We only cancel _after_ flushing: otherwise we would be shutting down the // components that do the flush. self.cancel.cancel(); } } #[cfg(test)] mod test { use std::io::ErrorKind; use std::time::Duration; use camino::Utf8Path; use hex_literal::hex; use pageserver_api::key::Key; use pageserver_api::models::ShardImportStatus; use pageserver_api::shard::ShardIndex; use pageserver_api::upcall_api::ReAttachResponseTenant; use remote_storage::{RemoteStorageConfig, RemoteStorageKind}; use tokio::task::JoinHandle; use tracing::info; use super::*; use crate::controller_upcall_client::RetryForeverError; use crate::tenant::harness::TenantHarness; use crate::tenant::storage_layer::DeltaLayerName; pub const TIMELINE_ID: TimelineId = TimelineId::from_array(hex!("11223344556677881122334455667788")); pub const EXAMPLE_LAYER_NAME: LayerName = LayerName::Delta(DeltaLayerName { key_range: Key::from_i128(0x0)..Key::from_i128(0xFFFFFFFFFFFFFFFF), lsn_range: Lsn(0x00000000016B59D8)..Lsn(0x00000000016B5A51), }); // When you need a second layer in a test. pub const EXAMPLE_LAYER_NAME_ALT: LayerName = LayerName::Delta(DeltaLayerName { key_range: Key::from_i128(0x0)..Key::from_i128(0xFFFFFFFFFFFFFFFF), lsn_range: Lsn(0x00000000016B5A51)..Lsn(0x00000000016B5A61), }); struct TestSetup { harness: TenantHarness, remote_fs_dir: Utf8PathBuf, storage: GenericRemoteStorage, mock_control_plane: MockStorageController, deletion_queue: DeletionQueue, worker_join: JoinHandle<()>, } impl TestSetup { /// Simulate a pageserver restart by destroying and recreating the deletion queue async fn restart(&mut self) { let (deletion_queue, workers) = DeletionQueue::new( self.storage.clone(), self.mock_control_plane.clone(), self.harness.conf, ); tracing::debug!("Spawning worker for new queue queue"); let worker_join = workers.spawn_with(&tokio::runtime::Handle::current()); let old_worker_join = std::mem::replace(&mut self.worker_join, worker_join); let old_deletion_queue = std::mem::replace(&mut self.deletion_queue, deletion_queue); tracing::debug!("Joining worker from previous queue"); old_deletion_queue.cancel.cancel(); old_worker_join .await .expect("Failed to join workers for previous deletion queue"); } fn set_latest_generation(&self, gen_: Generation) { let tenant_shard_id = self.harness.tenant_shard_id; self.mock_control_plane .latest_generation .lock() .unwrap() .insert(tenant_shard_id, gen_); } /// Returns remote layer file name, suitable for use in assert_remote_files fn write_remote_layer( &self, file_name: LayerName, gen_: Generation, ) -> anyhow::Result<String> { let tenant_shard_id = self.harness.tenant_shard_id; let relative_remote_path = remote_timeline_path(&tenant_shard_id, &TIMELINE_ID); let remote_timeline_path = self.remote_fs_dir.join(relative_remote_path.get_path()); std::fs::create_dir_all(&remote_timeline_path)?; let remote_layer_file_name = format!("{}{}", file_name, gen_.get_suffix()); let content: Vec<u8> = format!("placeholder contents of {file_name}").into(); std::fs::write( remote_timeline_path.join(remote_layer_file_name.clone()), content, )?; Ok(remote_layer_file_name) } } #[derive(Debug, Clone)] struct MockStorageController { pub latest_generation: std::sync::Arc<std::sync::Mutex<HashMap<TenantShardId, Generation>>>, } impl MockStorageController { fn new() -> Self { Self { latest_generation: Arc::default(), } } } impl StorageControllerUpcallApi for MockStorageController { async fn re_attach( &self, _conf: &PageServerConf, _empty_local_disk: bool, ) -> Result<HashMap<TenantShardId, ReAttachResponseTenant>, RetryForeverError> { unimplemented!() } async fn validate( &self, tenants: Vec<(TenantShardId, Generation)>, ) -> Result<HashMap<TenantShardId, bool>, RetryForeverError> { let mut result = HashMap::new(); let latest_generation = self.latest_generation.lock().unwrap(); for (tenant_shard_id, generation) in tenants { if let Some(latest) = latest_generation.get(&tenant_shard_id) { result.insert(tenant_shard_id, *latest == generation); } } Ok(result) } async fn put_timeline_import_status( &self, _tenant_shard_id: TenantShardId, _timeline_id: TimelineId, _generation: Generation, _status: pageserver_api::models::ShardImportStatus, ) -> Result<(), RetryForeverError> { unimplemented!() } async fn get_timeline_import_status( &self, _tenant_shard_id: TenantShardId, _timeline_id: TimelineId, _generation: Generation, ) -> Result<ShardImportStatus, RetryForeverError> { unimplemented!() } } async fn setup(test_name: &str) -> anyhow::Result<TestSetup> { let test_name = Box::leak(Box::new(format!("deletion_queue__{test_name}"))); let harness = TenantHarness::create(test_name).await?; // We do not load() the harness: we only need its config and remote_storage // Set up a GenericRemoteStorage targetting a directory let remote_fs_dir = harness.conf.workdir.join("remote_fs"); std::fs::create_dir_all(remote_fs_dir)?; let remote_fs_dir = harness.conf.workdir.join("remote_fs").canonicalize_utf8()?; let storage_config = RemoteStorageConfig { storage: RemoteStorageKind::LocalFs { local_path: remote_fs_dir.clone(), }, timeout: RemoteStorageConfig::DEFAULT_TIMEOUT, small_timeout: RemoteStorageConfig::DEFAULT_SMALL_TIMEOUT, }; let storage = GenericRemoteStorage::from_config(&storage_config) .await .unwrap(); let mock_control_plane = MockStorageController::new(); let (deletion_queue, worker) = DeletionQueue::new(storage.clone(), mock_control_plane.clone(), harness.conf); let worker_join = worker.spawn_with(&tokio::runtime::Handle::current()); Ok(TestSetup { harness, remote_fs_dir, storage, mock_control_plane, deletion_queue, worker_join, }) } // TODO: put this in a common location so that we can share with remote_timeline_client's tests fn assert_remote_files(expected: &[&str], remote_path: &Utf8Path) { let mut expected: Vec<String> = expected.iter().map(|x| String::from(*x)).collect(); expected.sort(); let mut found: Vec<String> = Vec::new(); let dir = match std::fs::read_dir(remote_path) { Ok(d) => d, Err(e) => { if e.kind() == ErrorKind::NotFound { if expected.is_empty() { // We are asserting prefix is empty: it is expected that the dir is missing return; } else { assert_eq!(expected, Vec::<String>::new()); unreachable!(); } } else { panic!("Unexpected error listing {remote_path}: {e}"); } } }; for entry in dir.flatten() { let entry_name = entry.file_name();
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/basebackup.rs
pageserver/src/basebackup.rs
//! //! Generate a tarball with files needed to bootstrap ComputeNode. //! //! TODO: this module has nothing to do with PostgreSQL pg_basebackup. //! It could use a better name. //! //! Stateless Postgres compute node is launched by sending a tarball //! which contains non-relational data (multixacts, clog, filenodemaps, twophase files), //! generated pg_control and dummy segment of WAL. //! This module is responsible for creation of such tarball //! from data stored in object storage. //! use std::fmt::Write as FmtWrite; use std::sync::Arc; use std::time::{Instant, SystemTime}; use anyhow::{Context, anyhow}; use async_compression::tokio::write::GzipEncoder; use bytes::{BufMut, Bytes, BytesMut}; use fail::fail_point; use pageserver_api::key::{Key, rel_block_to_key}; use pageserver_api::reltag::{RelTag, SlruKind}; use postgres_ffi::pg_constants::{PG_HBA, PGDATA_SPECIAL_FILES}; use postgres_ffi::{ BLCKSZ, PG_TLI, PgMajorVersion, RELSEG_SIZE, WAL_SEGMENT_SIZE, XLogFileName, dispatch_pgversion, pg_constants, }; use postgres_ffi_types::constants::{DEFAULTTABLESPACE_OID, GLOBALTABLESPACE_OID}; use postgres_ffi_types::forknum::{INIT_FORKNUM, MAIN_FORKNUM}; use tokio::io::{self, AsyncWrite, AsyncWriteExt as _}; use tokio_tar::{Builder, EntryType, Header}; use tracing::*; use utils::lsn::Lsn; use crate::context::RequestContext; use crate::pgdatadir_mapping::Version; use crate::tenant::storage_layer::IoConcurrency; use crate::tenant::timeline::{GetVectoredError, VersionedKeySpaceQuery}; use crate::tenant::{PageReconstructError, Timeline}; #[derive(Debug, thiserror::Error)] pub enum BasebackupError { #[error("basebackup pageserver error {0:#}")] Server(#[from] anyhow::Error), #[error("basebackup client error {0:#} when {1}")] Client(#[source] io::Error, &'static str), #[error("basebackup during shutdown")] Shutdown, } impl From<PageReconstructError> for BasebackupError { fn from(value: PageReconstructError) -> Self { match value { PageReconstructError::Cancelled => BasebackupError::Shutdown, err => BasebackupError::Server(err.into()), } } } impl From<GetVectoredError> for BasebackupError { fn from(value: GetVectoredError) -> Self { match value { GetVectoredError::Cancelled => BasebackupError::Shutdown, err => BasebackupError::Server(err.into()), } } } impl From<BasebackupError> for postgres_backend::QueryError { fn from(err: BasebackupError) -> Self { use postgres_backend::QueryError; use pq_proto::framed::ConnectionError; match err { BasebackupError::Client(err, _) => QueryError::Disconnected(ConnectionError::Io(err)), BasebackupError::Server(err) => QueryError::Other(err), BasebackupError::Shutdown => QueryError::Shutdown, } } } impl From<BasebackupError> for tonic::Status { fn from(err: BasebackupError) -> Self { use tonic::Code; let code = match &err { BasebackupError::Client(_, _) => Code::Cancelled, BasebackupError::Server(_) => Code::Internal, BasebackupError::Shutdown => Code::Unavailable, }; tonic::Status::new(code, err.to_string()) } } /// Create basebackup with non-rel data in it. /// Only include relational data if 'full_backup' is true. /// /// Currently we use empty 'req_lsn' in two cases: /// * During the basebackup right after timeline creation /// * When working without safekeepers. In this situation it is important to match the lsn /// we are taking basebackup on with the lsn that is used in pageserver's walreceiver /// to start the replication. #[allow(clippy::too_many_arguments)] pub async fn send_basebackup_tarball<'a, W>( write: &'a mut W, timeline: &'a Timeline, req_lsn: Option<Lsn>, prev_lsn: Option<Lsn>, full_backup: bool, replica: bool, gzip_level: Option<async_compression::Level>, ctx: &'a RequestContext, ) -> Result<(), BasebackupError> where W: AsyncWrite + Send + Sync + Unpin, { // Compute postgres doesn't have any previous WAL files, but the first // record that it's going to write needs to include the LSN of the // previous record (xl_prev). We include prev_record_lsn in the // "neon.signal" file, so that postgres can read it during startup. // // We don't keep full history of record boundaries in the page server, // however, only the predecessor of the latest record on each // timeline. So we can only provide prev_record_lsn when you take a // base backup at the end of the timeline, i.e. at last_record_lsn. // Even at the end of the timeline, we sometimes don't have a valid // prev_lsn value; that happens if the timeline was just branched from // an old LSN and it doesn't have any WAL of its own yet. We will set // prev_lsn to Lsn(0) if we cannot provide the correct value. let (backup_prev, lsn) = if let Some(req_lsn) = req_lsn { // Backup was requested at a particular LSN. The caller should've // already checked that it's a valid LSN. // If the requested point is the end of the timeline, we can // provide prev_lsn. (get_last_record_rlsn() might return it as // zero, though, if no WAL has been generated on this timeline // yet.) let end_of_timeline = timeline.get_last_record_rlsn(); if req_lsn == end_of_timeline.last { (end_of_timeline.prev, req_lsn) } else { (Lsn(0), req_lsn) } } else { // Backup was requested at end of the timeline. let end_of_timeline = timeline.get_last_record_rlsn(); (end_of_timeline.prev, end_of_timeline.last) }; // Consolidate the derived and the provided prev_lsn values let prev_record_lsn = if let Some(provided_prev_lsn) = prev_lsn { if backup_prev != Lsn(0) && backup_prev != provided_prev_lsn { return Err(BasebackupError::Server(anyhow!( "backup_prev {backup_prev} != provided_prev_lsn {provided_prev_lsn}" ))); } provided_prev_lsn } else { backup_prev }; info!( "taking basebackup lsn={lsn}, prev_lsn={prev_record_lsn} \ (full_backup={full_backup}, replica={replica}, gzip={gzip_level:?})", ); let span = info_span!("send_tarball", backup_lsn=%lsn); let io_concurrency = IoConcurrency::spawn_from_conf( timeline.conf.get_vectored_concurrent_io, timeline .gate .enter() .map_err(|_| BasebackupError::Shutdown)?, ); if let Some(gzip_level) = gzip_level { let mut encoder = GzipEncoder::with_quality(write, gzip_level); Basebackup { ar: Builder::new_non_terminated(&mut encoder), timeline, lsn, prev_record_lsn, full_backup, replica, ctx, io_concurrency, } .send_tarball() .instrument(span) .await?; encoder .shutdown() .await .map_err(|err| BasebackupError::Client(err, "gzip"))?; } else { Basebackup { ar: Builder::new_non_terminated(write), timeline, lsn, prev_record_lsn, full_backup, replica, ctx, io_concurrency, } .send_tarball() .instrument(span) .await?; } Ok(()) } /// This is short-living object only for the time of tarball creation, /// created mostly to avoid passing a lot of parameters between various functions /// used for constructing tarball. struct Basebackup<'a, W> where W: AsyncWrite + Send + Sync + Unpin, { ar: Builder<&'a mut W>, timeline: &'a Timeline, lsn: Lsn, prev_record_lsn: Lsn, full_backup: bool, replica: bool, ctx: &'a RequestContext, io_concurrency: IoConcurrency, } /// A sink that accepts SLRU blocks ordered by key and forwards /// full segments to the archive. struct SlruSegmentsBuilder<'a, 'b, W> where W: AsyncWrite + Send + Sync + Unpin, { ar: &'a mut Builder<&'b mut W>, buf: Vec<u8>, current_segment: Option<(SlruKind, u32)>, total_blocks: usize, } impl<'a, 'b, W> SlruSegmentsBuilder<'a, 'b, W> where W: AsyncWrite + Send + Sync + Unpin, { fn new(ar: &'a mut Builder<&'b mut W>) -> Self { Self { ar, buf: Vec::new(), current_segment: None, total_blocks: 0, } } async fn add_block(&mut self, key: &Key, block: Bytes) -> Result<(), BasebackupError> { let (kind, segno, _) = key.to_slru_block()?; match kind { SlruKind::Clog => { if !(block.len() == BLCKSZ as usize || block.len() == BLCKSZ as usize + 8) { return Err(BasebackupError::Server(anyhow!( "invalid SlruKind::Clog record: block.len()={}", block.len() ))); } } SlruKind::MultiXactMembers | SlruKind::MultiXactOffsets => { if block.len() != BLCKSZ as usize { return Err(BasebackupError::Server(anyhow!( "invalid {:?} record: block.len()={}", kind, block.len() ))); } } } let segment = (kind, segno); match self.current_segment { None => { self.current_segment = Some(segment); self.buf .extend_from_slice(block.slice(..BLCKSZ as usize).as_ref()); } Some(current_seg) if current_seg == segment => { self.buf .extend_from_slice(block.slice(..BLCKSZ as usize).as_ref()); } Some(_) => { self.flush().await?; self.current_segment = Some(segment); self.buf .extend_from_slice(block.slice(..BLCKSZ as usize).as_ref()); } } Ok(()) } async fn flush(&mut self) -> Result<(), BasebackupError> { let nblocks = self.buf.len() / BLCKSZ as usize; let (kind, segno) = self.current_segment.take().unwrap(); let segname = format!("{kind}/{segno:>04X}"); let header = new_tar_header(&segname, self.buf.len() as u64)?; self.ar .append(&header, self.buf.as_slice()) .await .map_err(|e| BasebackupError::Client(e, "flush"))?; self.total_blocks += nblocks; debug!("Added to basebackup slru {} relsize {}", segname, nblocks); self.buf.clear(); Ok(()) } async fn finish(mut self) -> Result<(), BasebackupError> { let res = if self.current_segment.is_none() || self.buf.is_empty() { Ok(()) } else { self.flush().await }; info!("Collected {} SLRU blocks", self.total_blocks); res } } impl<W> Basebackup<'_, W> where W: AsyncWrite + Send + Sync + Unpin, { async fn send_tarball(mut self) -> Result<(), BasebackupError> { // TODO include checksum // Construct the pg_control file from the persisted checkpoint and pg_control // information. But we only add this to the tarball at the end, so that if the // writing is interrupted half-way through, the resulting incomplete tarball will // be missing the pg_control file, which prevents PostgreSQL from starting up on // it. With proper error handling, you should never try to start up from an // incomplete basebackup in the first place, of course, but this is a nice little // extra safety measure. let checkpoint_bytes = self .timeline .get_checkpoint(self.lsn, self.ctx) .await .context("failed to get checkpoint bytes")?; let pg_control_bytes = self .timeline .get_control_file(self.lsn, self.ctx) .await .context("failed to get control bytes")?; let (pg_control_bytes, system_identifier, was_shutdown) = postgres_ffi::generate_pg_control( &pg_control_bytes, &checkpoint_bytes, self.lsn, self.timeline.pg_version, )?; let lazy_slru_download = self.timeline.get_lazy_slru_download() && !self.full_backup; let pgversion = self.timeline.pg_version; let subdirs = dispatch_pgversion!(pgversion, &pgv::bindings::PGDATA_SUBDIRS[..]); // Create pgdata subdirs structure for dir in subdirs.iter() { let header = new_tar_header_dir(dir)?; self.ar .append(&header, io::empty()) .await .map_err(|e| BasebackupError::Client(e, "send_tarball"))?; } // Send config files. for filepath in PGDATA_SPECIAL_FILES.iter() { if *filepath == "pg_hba.conf" { let data = PG_HBA.as_bytes(); let header = new_tar_header(filepath, data.len() as u64)?; self.ar .append(&header, data) .await .map_err(|e| BasebackupError::Client(e, "send_tarball,pg_hba.conf"))?; } else { let header = new_tar_header(filepath, 0)?; self.ar .append(&header, io::empty()) .await .map_err(|e| BasebackupError::Client(e, "send_tarball,add_config_file"))?; } } if !lazy_slru_download { // Gather non-relational files from object storage pages. let slru_partitions = self .timeline .get_slru_keyspace(Version::at(self.lsn), self.ctx) .await? .partition( self.timeline.get_shard_identity(), self.timeline.conf.max_get_vectored_keys.get() as u64 * BLCKSZ as u64, BLCKSZ as u64, ); let mut slru_builder = SlruSegmentsBuilder::new(&mut self.ar); for part in slru_partitions.parts { let query = VersionedKeySpaceQuery::uniform(part, self.lsn); let blocks = self .timeline .get_vectored(query, self.io_concurrency.clone(), self.ctx) .await?; for (key, block) in blocks { let block = block?; slru_builder.add_block(&key, block).await?; } } slru_builder.finish().await?; } let mut min_restart_lsn: Lsn = Lsn::MAX; let mut dbdir_cnt = 0; let mut rel_cnt = 0; // Create tablespace directories for ((spcnode, dbnode), has_relmap_file) in self.timeline.list_dbdirs(self.lsn, self.ctx).await? { self.add_dbdir(spcnode, dbnode, has_relmap_file).await?; dbdir_cnt += 1; // If full backup is requested, include all relation files. // Otherwise only include init forks of unlogged relations. let rels = self .timeline .list_rels(spcnode, dbnode, Version::at(self.lsn), self.ctx) .await?; for &rel in rels.iter() { rel_cnt += 1; // Send init fork as main fork to provide well formed empty // contents of UNLOGGED relations. Postgres copies it in // `reinit.c` during recovery. if rel.forknum == INIT_FORKNUM { // I doubt we need _init fork itself, but having it at least // serves as a marker relation is unlogged. self.add_rel(rel, rel).await?; self.add_rel(rel, rel.with_forknum(MAIN_FORKNUM)).await?; continue; } if self.full_backup { if rel.forknum == MAIN_FORKNUM && rels.contains(&rel.with_forknum(INIT_FORKNUM)) { // skip this, will include it when we reach the init fork continue; } self.add_rel(rel, rel).await?; } } } self.timeline .db_rel_count .store(Some(Arc::new((dbdir_cnt, rel_cnt)))); let start_time = Instant::now(); let aux_files = self .timeline .list_aux_files(self.lsn, self.ctx, self.io_concurrency.clone()) .await?; let aux_scan_time = start_time.elapsed(); let aux_estimated_size = aux_files .values() .map(|content| content.len()) .sum::<usize>(); info!( "Scanned {} aux files in {}ms, aux file content size = {}", aux_files.len(), aux_scan_time.as_millis(), aux_estimated_size ); for (path, content) in aux_files { if path.starts_with("pg_replslot") { // Do not create LR slots at standby because they are not used but prevent WAL truncation if self.replica { continue; } let offs = pg_constants::REPL_SLOT_ON_DISK_OFFSETOF_RESTART_LSN; let restart_lsn = Lsn(u64::from_le_bytes( content[offs..offs + 8].try_into().unwrap(), )); info!("Replication slot {} restart LSN={}", path, restart_lsn); min_restart_lsn = Lsn::min(min_restart_lsn, restart_lsn); } else if path == "pg_logical/replorigin_checkpoint" { // replorigin_checkoint is written only on compute shutdown, so it contains // deteriorated values. So we generate our own version of this file for the particular LSN // based on information about replorigins extracted from transaction commit records. // In future we will not generate AUX record for "pg_logical/replorigin_checkpoint" at all, // but now we should handle (skip) it for backward compatibility. continue; } else if path == "pg_stat/pgstat.stat" && !was_shutdown { // Drop statistic in case of abnormal termination, i.e. if we're not starting from the exact LSN // of a shutdown checkpoint. continue; } let header = new_tar_header(&path, content.len() as u64)?; self.ar .append(&header, &*content) .await .map_err(|e| BasebackupError::Client(e, "send_tarball,add_aux_file"))?; } if min_restart_lsn != Lsn::MAX { info!( "Min restart LSN for logical replication is {}", min_restart_lsn ); let data = min_restart_lsn.0.to_le_bytes(); let header = new_tar_header("restart.lsn", data.len() as u64)?; self.ar .append(&header, &data[..]) .await .map_err(|e| BasebackupError::Client(e, "send_tarball,restart.lsn"))?; } for xid in self .timeline .list_twophase_files(self.lsn, self.ctx) .await? { self.add_twophase_file(xid).await?; } let repl_origins = self .timeline .get_replorigins(self.lsn, self.ctx, self.io_concurrency.clone()) .await?; let n_origins = repl_origins.len(); if n_origins != 0 { // // Construct "pg_logical/replorigin_checkpoint" file based on information about replication origins // extracted from transaction commit record. We are using this file to pass information about replication // origins to compute to allow logical replication to restart from proper point. // let mut content = Vec::with_capacity(n_origins * 16 + 8); content.extend_from_slice(&pg_constants::REPLICATION_STATE_MAGIC.to_le_bytes()); for (origin_id, origin_lsn) in repl_origins { content.extend_from_slice(&origin_id.to_le_bytes()); content.extend_from_slice(&[0u8; 6]); // align to 8 bytes content.extend_from_slice(&origin_lsn.0.to_le_bytes()); } let crc32 = crc32c::crc32c(&content); content.extend_from_slice(&crc32.to_le_bytes()); let header = new_tar_header("pg_logical/replorigin_checkpoint", content.len() as u64)?; self.ar.append(&header, &*content).await.map_err(|e| { BasebackupError::Client(e, "send_tarball,pg_logical/replorigin_checkpoint") })?; } fail_point!("basebackup-before-control-file", |_| { Err(BasebackupError::Server(anyhow!( "failpoint basebackup-before-control-file" ))) }); // Last, add the pg_control file and bootstrap WAL segment. self.add_pgcontrol_file(pg_control_bytes, system_identifier) .await?; self.ar .finish() .await .map_err(|e| BasebackupError::Client(e, "send_tarball,finish"))?; debug!("all tarred up!"); Ok(()) } /// Add contents of relfilenode `src`, naming it as `dst`. async fn add_rel(&mut self, src: RelTag, dst: RelTag) -> Result<(), BasebackupError> { let nblocks = self .timeline .get_rel_size(src, Version::at(self.lsn), self.ctx) .await?; // If the relation is empty, create an empty file if nblocks == 0 { let file_name = dst.to_segfile_name(0); let header = new_tar_header(&file_name, 0)?; self.ar .append(&header, io::empty()) .await .map_err(|e| BasebackupError::Client(e, "add_rel,empty"))?; return Ok(()); } // Add a file for each chunk of blocks (aka segment) let mut startblk = 0; let mut seg = 0; while startblk < nblocks { let endblk = std::cmp::min(startblk + RELSEG_SIZE, nblocks); let mut segment_data: Vec<u8> = vec![]; for blknum in startblk..endblk { let img = self .timeline // TODO: investigate using get_vectored for the entire startblk..endblk range. // But this code path is not on the critical path for most basebackups (?). .get(rel_block_to_key(src, blknum), self.lsn, self.ctx) .await?; segment_data.extend_from_slice(&img[..]); } let file_name = dst.to_segfile_name(seg as u32); let header = new_tar_header(&file_name, segment_data.len() as u64)?; self.ar .append(&header, segment_data.as_slice()) .await .map_err(|e| BasebackupError::Client(e, "add_rel,segment"))?; seg += 1; startblk = endblk; } Ok(()) } // // Include database/tablespace directories. // // Each directory contains a PG_VERSION file, and the default database // directories also contain pg_filenode.map files. // async fn add_dbdir( &mut self, spcnode: u32, dbnode: u32, has_relmap_file: bool, ) -> Result<(), BasebackupError> { let relmap_img = if has_relmap_file { let img = self .timeline .get_relmap_file(spcnode, dbnode, Version::at(self.lsn), self.ctx) .await?; if img.len() != dispatch_pgversion!(self.timeline.pg_version, pgv::bindings::SIZEOF_RELMAPFILE) { return Err(BasebackupError::Server(anyhow!( "img.len() != SIZE_OF_RELMAPFILE, img.len()={}", img.len(), ))); } Some(img) } else { None }; if spcnode == GLOBALTABLESPACE_OID { let pg_version_str = self.timeline.pg_version.versionfile_string(); let header = new_tar_header("PG_VERSION", pg_version_str.len() as u64)?; self.ar .append(&header, pg_version_str.as_bytes()) .await .map_err(|e| BasebackupError::Client(e, "add_dbdir,PG_VERSION"))?; info!("timeline.pg_version {}", self.timeline.pg_version); if let Some(img) = relmap_img { // filenode map for global tablespace let header = new_tar_header("global/pg_filenode.map", img.len() as u64)?; self.ar .append(&header, &img[..]) .await .map_err(|e| BasebackupError::Client(e, "add_dbdir,global/pg_filenode.map"))?; } else { warn!("global/pg_filenode.map is missing"); } } else { // User defined tablespaces are not supported. However, as // a special case, if a tablespace/db directory is // completely empty, we can leave it out altogether. This // makes taking a base backup after the 'tablespace' // regression test pass, because the test drops the // created tablespaces after the tests. // // FIXME: this wouldn't be necessary, if we handled // XLOG_TBLSPC_DROP records. But we probably should just // throw an error on CREATE TABLESPACE in the first place. if !has_relmap_file && self .timeline .list_rels(spcnode, dbnode, Version::at(self.lsn), self.ctx) .await? .is_empty() { return Ok(()); } // User defined tablespaces are not supported if spcnode != DEFAULTTABLESPACE_OID { return Err(BasebackupError::Server(anyhow!( "spcnode != DEFAULTTABLESPACE_OID, spcnode={spcnode}" ))); } // Append dir path for each database let path = format!("base/{dbnode}"); let header = new_tar_header_dir(&path)?; self.ar .append(&header, io::empty()) .await .map_err(|e| BasebackupError::Client(e, "add_dbdir,base"))?; if let Some(img) = relmap_img { let dst_path = format!("base/{dbnode}/PG_VERSION"); let pg_version_str = self.timeline.pg_version.versionfile_string(); let header = new_tar_header(&dst_path, pg_version_str.len() as u64)?; self.ar .append(&header, pg_version_str.as_bytes()) .await .map_err(|e| BasebackupError::Client(e, "add_dbdir,base/PG_VERSION"))?; let relmap_path = format!("base/{dbnode}/pg_filenode.map"); let header = new_tar_header(&relmap_path, img.len() as u64)?; self.ar .append(&header, &img[..]) .await .map_err(|e| BasebackupError::Client(e, "add_dbdir,base/pg_filenode.map"))?; } }; Ok(()) } // // Extract twophase state files // async fn add_twophase_file(&mut self, xid: u64) -> Result<(), BasebackupError> { let img = self .timeline .get_twophase_file(xid, self.lsn, self.ctx) .await?; let mut buf = BytesMut::new(); buf.extend_from_slice(&img[..]); let crc = crc32c::crc32c(&img[..]); buf.put_u32_le(crc); let path = if self.timeline.pg_version < PgMajorVersion::PG17 { format!("pg_twophase/{xid:>08X}") } else { format!("pg_twophase/{xid:>016X}") }; let header = new_tar_header(&path, buf.len() as u64)?; self.ar .append(&header, &buf[..]) .await .map_err(|e| BasebackupError::Client(e, "add_twophase_file"))?; Ok(()) } // // Add generated pg_control file and bootstrap WAL segment. // Also send neon.signal and zenith.signal file with extra bootstrap data. // async fn add_pgcontrol_file( &mut self, pg_control_bytes: Bytes, system_identifier: u64, ) -> Result<(), BasebackupError> { // add neon.signal file let mut neon_signal = String::new(); if self.prev_record_lsn == Lsn(0) { if self.timeline.is_ancestor_lsn(self.lsn) { write!(neon_signal, "PREV LSN: none") .map_err(|e| BasebackupError::Server(e.into()))?; } else { write!(neon_signal, "PREV LSN: invalid") .map_err(|e| BasebackupError::Server(e.into()))?; } } else { write!(neon_signal, "PREV LSN: {}", self.prev_record_lsn) .map_err(|e| BasebackupError::Server(e.into()))?; } // TODO: Remove zenith.signal once all historical computes have been replaced // ... and thus support the neon.signal file. for signalfilename in ["neon.signal", "zenith.signal"] { self.ar .append( &new_tar_header(signalfilename, neon_signal.len() as u64)?, neon_signal.as_bytes(), ) .await .map_err(|e| BasebackupError::Client(e, "add_pgcontrol_file,neon.signal"))?; } //send pg_control let header = new_tar_header("global/pg_control", pg_control_bytes.len() as u64)?; self.ar .append(&header, &pg_control_bytes[..]) .await .map_err(|e| BasebackupError::Client(e, "add_pgcontrol_file,pg_control"))?; //send wal segment let segno = self.lsn.segment_number(WAL_SEGMENT_SIZE); let wal_file_name = XLogFileName(PG_TLI, segno, WAL_SEGMENT_SIZE); let wal_file_path = format!("pg_wal/{wal_file_name}"); let header = new_tar_header(&wal_file_path, WAL_SEGMENT_SIZE as u64)?; let wal_seg = postgres_ffi::generate_wal_segment( segno, system_identifier, self.timeline.pg_version, self.lsn, ) .map_err(|e| anyhow!(e).context("Failed generating wal segment"))?; if wal_seg.len() != WAL_SEGMENT_SIZE { return Err(BasebackupError::Server(anyhow!( "wal_seg.len() != WAL_SEGMENT_SIZE, wal_seg.len()={}", wal_seg.len() ))); } self.ar .append(&header, &wal_seg[..]) .await .map_err(|e| BasebackupError::Client(e, "add_pgcontrol_file,wal_segment"))?; Ok(()) } } // // Create new tarball entry header // fn new_tar_header(path: &str, size: u64) -> anyhow::Result<Header> { let mut header = Header::new_gnu(); header.set_size(size); header.set_path(path)?; header.set_mode(0b110000000); // -rw------- header.set_mtime( // use currenttime as last modified time SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() .as_secs(), ); header.set_cksum(); Ok(header) } fn new_tar_header_dir(path: &str) -> anyhow::Result<Header> { let mut header = Header::new_gnu(); header.set_size(0); header.set_path(path)?; header.set_mode(0o755); // -rw------- header.set_entry_type(EntryType::dir()); header.set_mtime( // use currenttime as last modified time SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() .as_secs(), ); header.set_cksum(); Ok(header) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/walredo.rs
pageserver/src/walredo.rs
//! //! WAL redo. This service runs PostgreSQL in a special wal_redo mode //! to apply given WAL records over an old page image and return new //! page image. //! //! We rely on Postgres to perform WAL redo for us. We launch a //! postgres process in special "wal redo" mode that's similar to //! single-user mode. We then pass the previous page image, if any, //! and all the WAL records we want to apply, to the postgres //! process. Then we get the page image back. Communication with the //! postgres process happens via stdin/stdout //! //! See pgxn/neon_walredo/walredoproc.c for the other side of //! this communication. //! //! The Postgres process is assumed to be secure against malicious WAL //! records. It achieves it by dropping privileges before replaying //! any WAL records, so that even if an attacker hijacks the Postgres //! process, he cannot escape out of it. /// Process lifecycle and abstracction for the IPC protocol. mod process; /// Code to apply [`NeonWalRecord`]s. pub(crate) mod apply_neon; use std::future::Future; use std::sync::Arc; use std::time::{Duration, Instant}; use anyhow::Context; use bytes::{Bytes, BytesMut}; use pageserver_api::key::Key; use pageserver_api::models::{WalRedoManagerProcessStatus, WalRedoManagerStatus}; use pageserver_api::shard::TenantShardId; use postgres_ffi::PgMajorVersion; use tracing::*; use utils::lsn::Lsn; use utils::sync::gate::GateError; use utils::sync::heavier_once_cell; use wal_decoder::models::record::NeonWalRecord; use crate::config::PageServerConf; use crate::metrics::{ WAL_REDO_BYTES_HISTOGRAM, WAL_REDO_PROCESS_LAUNCH_DURATION_HISTOGRAM, WAL_REDO_RECORDS_HISTOGRAM, WAL_REDO_TIME, }; /// The real implementation that uses a Postgres process to /// perform WAL replay. /// /// Only one thread can use the process at a time, that is controlled by the /// Mutex. In the future, we might want to launch a pool of processes to allow /// concurrent replay of multiple records. pub struct PostgresRedoManager { tenant_shard_id: TenantShardId, conf: &'static PageServerConf, last_redo_at: std::sync::Mutex<Option<Instant>>, /// We use [`heavier_once_cell`] for /// /// 1. coalescing the lazy spawning of walredo processes ([`ProcessOnceCell::Spawned`]) /// 2. prevent new processes from being spawned on [`Self::shutdown`] (=> [`ProcessOnceCell::ManagerShutDown`]). /// /// # Spawning /// /// Redo requests use the once cell to coalesce onto one call to [`process::WalRedoProcess::launch`]. /// /// Notably, requests don't use the [`heavier_once_cell::Guard`] to keep ahold of the /// their process object; we use [`Arc::clone`] for that. /// /// This is primarily because earlier implementations that didn't use [`heavier_once_cell`] /// had that behavior; it's probably unnecessary. /// The only merit of it is that if one walredo process encounters an error, /// it can take it out of rotation (= using [`heavier_once_cell::Guard::take_and_deinit`]. /// and retry redo, thereby starting the new process, while other redo tasks might /// still be using the old redo process. But, those other tasks will most likely /// encounter an error as well, and errors are an unexpected condition anyway. /// So, probably we could get rid of the `Arc` in the future. /// /// # Shutdown /// /// See [`Self::launched_processes`]. redo_process: heavier_once_cell::OnceCell<ProcessOnceCell>, /// Gate that is entered when launching a walredo process and held open /// until the process has been `kill()`ed and `wait()`ed upon. /// /// Manager shutdown waits for this gate to close after setting the /// [`ProcessOnceCell::ManagerShutDown`] state in [`Self::redo_process`]. /// /// This type of usage is a bit unusual because gates usually keep track of /// concurrent operations, e.g., every [`Self::request_redo`] that is inflight. /// But we use it here to keep track of the _processes_ that we have launched, /// which may outlive any individual redo request because /// - we keep walredo process around until its quiesced to amortize spawn cost and /// - the Arc may be held by multiple concurrent redo requests, so, just because /// you replace the [`Self::redo_process`] cell's content doesn't mean the /// process gets killed immediately. /// /// We could simplify this by getting rid of the [`Arc`]. /// See the comment on [`Self::redo_process`] for more details. launched_processes: utils::sync::gate::Gate, } /// See [`PostgresRedoManager::redo_process`]. enum ProcessOnceCell { Spawned(Arc<Process>), ManagerShutDown, } struct Process { process: process::WalRedoProcess, /// This field is last in this struct so the guard gets dropped _after_ [`Self::process`]. /// (Reminder: dropping [`Self::process`] synchronously sends SIGKILL and then `wait()`s for it to exit). _launched_processes_guard: utils::sync::gate::GateGuard, } impl std::ops::Deref for Process { type Target = process::WalRedoProcess; fn deref(&self) -> &Self::Target { &self.process } } #[derive(Debug, thiserror::Error)] pub enum Error { #[error("cancelled")] Cancelled, #[error(transparent)] Other(#[from] anyhow::Error), } macro_rules! bail { ($($arg:tt)*) => { return Err($crate::walredo::Error::Other(::anyhow::anyhow!($($arg)*))); } } #[derive(Debug, Clone, Copy)] pub enum RedoAttemptType { /// Used for the read path. Will fire critical errors and retry twice if failure. ReadPage, // Used for legacy compaction (only used in image compaction). Will fire critical errors and retry once if failure. LegacyCompaction, // Used for gc compaction. Will not fire critical errors and not retry. GcCompaction, } impl std::fmt::Display for RedoAttemptType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { RedoAttemptType::ReadPage => write!(f, "read page"), RedoAttemptType::LegacyCompaction => write!(f, "legacy compaction"), RedoAttemptType::GcCompaction => write!(f, "gc compaction"), } } } /// /// Public interface of WAL redo manager /// impl PostgresRedoManager { /// /// Request the WAL redo manager to apply some WAL records /// /// The WAL redo is handled by a separate thread, so this just sends a request /// to the thread and waits for response. /// /// # Cancel-Safety /// /// This method is cancellation-safe. pub async fn request_redo( &self, key: Key, lsn: Lsn, base_img: Option<(Lsn, Bytes)>, records: Vec<(Lsn, NeonWalRecord)>, pg_version: PgMajorVersion, redo_attempt_type: RedoAttemptType, ) -> Result<Bytes, Error> { if records.is_empty() { bail!("invalid WAL redo request with no records"); } let max_retry_attempts = match redo_attempt_type { RedoAttemptType::ReadPage => 2, RedoAttemptType::LegacyCompaction => 1, RedoAttemptType::GcCompaction => 0, }; let base_img_lsn = base_img.as_ref().map(|p| p.0).unwrap_or(Lsn::INVALID); let mut img = base_img.map(|p| p.1); let mut batch_neon = apply_neon::can_apply_in_neon(&records[0].1); let mut batch_start = 0; for (i, record) in records.iter().enumerate().skip(1) { let rec_neon = apply_neon::can_apply_in_neon(&record.1); if rec_neon != batch_neon { let result = if batch_neon { self.apply_batch_neon(key, lsn, img, &records[batch_start..i]) } else { self.apply_batch_postgres( key, lsn, img, base_img_lsn, &records[batch_start..i], self.conf.wal_redo_timeout, pg_version, max_retry_attempts, redo_attempt_type, ) .await }; img = Some(result?); batch_neon = rec_neon; batch_start = i; } } // last batch if batch_neon { self.apply_batch_neon(key, lsn, img, &records[batch_start..]) } else { self.apply_batch_postgres( key, lsn, img, base_img_lsn, &records[batch_start..], self.conf.wal_redo_timeout, pg_version, max_retry_attempts, redo_attempt_type, ) .await } } /// Do a ping request-response roundtrip. /// /// Not used in production, but by Rust benchmarks. /// /// # Cancel-Safety /// /// This method is cancellation-safe. pub async fn ping(&self, pg_version: PgMajorVersion) -> Result<(), Error> { self.do_with_walredo_process(pg_version, |proc| async move { proc.ping(Duration::from_secs(1)) .await .map_err(Error::Other) }) .await } pub fn status(&self) -> WalRedoManagerStatus { WalRedoManagerStatus { last_redo_at: { let at = *self.last_redo_at.lock().unwrap(); at.and_then(|at| { let age = at.elapsed(); // map any chrono errors silently to None here chrono::Utc::now().checked_sub_signed(chrono::Duration::from_std(age).ok()?) }) }, process: self.redo_process.get().and_then(|p| match &*p { ProcessOnceCell::Spawned(p) => Some(WalRedoManagerProcessStatus { pid: p.id() }), ProcessOnceCell::ManagerShutDown => None, }), } } } impl PostgresRedoManager { /// /// Create a new PostgresRedoManager. /// pub fn new( conf: &'static PageServerConf, tenant_shard_id: TenantShardId, ) -> PostgresRedoManager { // The actual process is launched lazily, on first request. PostgresRedoManager { tenant_shard_id, conf, last_redo_at: std::sync::Mutex::default(), redo_process: heavier_once_cell::OnceCell::default(), launched_processes: utils::sync::gate::Gate::default(), } } /// Shut down the WAL redo manager. /// /// Returns `true` if this call was the one that initiated shutdown. /// `true` may be observed by no caller if the first caller stops polling. /// /// After this future completes /// - no redo process is running /// - no new redo process will be spawned /// - redo requests that need walredo process will fail with [`Error::Cancelled`] /// - [`apply_neon`]-only redo requests may still work, but this may change in the future /// /// # Cancel-Safety /// /// This method is cancellation-safe. pub async fn shutdown(&self) -> bool { // prevent new processes from being spawned let maybe_permit = match self.redo_process.get_or_init_detached().await { Ok(guard) => { if matches!(&*guard, ProcessOnceCell::ManagerShutDown) { None } else { let (proc, permit) = guard.take_and_deinit(); drop(proc); // this just drops the Arc, its refcount may not be zero yet Some(permit) } } Err(permit) => Some(permit), }; let it_was_us = if let Some(permit) = maybe_permit { self.redo_process .set(ProcessOnceCell::ManagerShutDown, permit); true } else { false }; // wait for ongoing requests to drain and the refcounts of all Arc<WalRedoProcess> that // we ever launched to drop to zero, which when it happens synchronously kill()s & wait()s // for the underlying process. self.launched_processes.close().await; it_was_us } /// This type doesn't have its own background task to check for idleness: we /// rely on our owner calling this function periodically in its own housekeeping /// loops. pub(crate) fn maybe_quiesce(&self, idle_timeout: Duration) { if let Ok(g) = self.last_redo_at.try_lock() { if let Some(last_redo_at) = *g { if last_redo_at.elapsed() >= idle_timeout { drop(g); drop(self.redo_process.get().map(|guard| guard.take_and_deinit())); } } } } /// # Cancel-Safety /// /// This method is cancel-safe iff `closure` is cancel-safe. async fn do_with_walredo_process< F: FnOnce(Arc<Process>) -> Fut, Fut: Future<Output = Result<O, Error>>, O, >( &self, pg_version: PgMajorVersion, closure: F, ) -> Result<O, Error> { let proc: Arc<Process> = match self.redo_process.get_or_init_detached().await { Ok(guard) => match &*guard { ProcessOnceCell::Spawned(proc) => Arc::clone(proc), ProcessOnceCell::ManagerShutDown => { return Err(Error::Cancelled); } }, Err(permit) => { let start = Instant::now(); // acquire guard before spawning process, so that we don't spawn new processes // if the gate is already closed. let _launched_processes_guard = match self.launched_processes.enter() { Ok(guard) => guard, Err(GateError::GateClosed) => unreachable!( "shutdown sets the once cell to `ManagerShutDown` state before closing the gate" ), }; let proc = Arc::new(Process { process: process::WalRedoProcess::launch( self.conf, self.tenant_shard_id, pg_version, ) .context("launch walredo process")?, _launched_processes_guard, }); let duration = start.elapsed(); WAL_REDO_PROCESS_LAUNCH_DURATION_HISTOGRAM.observe(duration.as_secs_f64()); info!( elapsed_ms = duration.as_millis(), pid = proc.id(), "launched walredo process" ); self.redo_process .set(ProcessOnceCell::Spawned(Arc::clone(&proc)), permit); proc } }; // async closures are unstable, would support &Process let result = closure(proc.clone()).await; if result.is_err() { // Avoid concurrent callers hitting the same issue by taking `proc` out of the rotation. // Note that there may be other tasks concurrent with us that also hold `proc`. // We have to deal with that here. // Also read the doc comment on field `self.redo_process`. // // NB: there may still be other concurrent threads using `proc`. // The last one will send SIGKILL when the underlying Arc reaches refcount 0. // // NB: the drop impl blocks the dropping thread with a wait() system call for // the child process. In some ways the blocking is actually good: if we // deferred the waiting into the background / to tokio if we used `tokio::process`, // it could happen that if walredo always fails immediately, we spawn processes faster // than we can SIGKILL & `wait` for them to exit. By doing it the way we do here, // we limit this risk of run-away to at most $num_runtimes * $num_executor_threads. // This probably needs revisiting at some later point. match self.redo_process.get() { None => (), Some(guard) => { match &*guard { ProcessOnceCell::ManagerShutDown => {} ProcessOnceCell::Spawned(guard_proc) => { if Arc::ptr_eq(&proc, guard_proc) { // We're the first to observe an error from `proc`, it's our job to take it out of rotation. guard.take_and_deinit(); } else { // Another task already spawned another redo process (further up in this method) // and put it into `redo_process`. Do nothing, our view of the world is behind. } } } } } // The last task that does this `drop()` of `proc` will do a blocking `wait()` syscall. drop(proc); } result } /// /// Process one request for WAL redo using wal-redo postgres /// /// # Cancel-Safety /// /// Cancellation safe. #[allow(clippy::too_many_arguments)] async fn apply_batch_postgres( &self, key: Key, lsn: Lsn, base_img: Option<Bytes>, base_img_lsn: Lsn, records: &[(Lsn, NeonWalRecord)], wal_redo_timeout: Duration, pg_version: PgMajorVersion, max_retry_attempts: u32, redo_attempt_type: RedoAttemptType, ) -> Result<Bytes, Error> { *(self.last_redo_at.lock().unwrap()) = Some(Instant::now()); let (rel, blknum) = key.to_rel_block().context("invalid record")?; let mut n_attempts = 0u32; loop { let base_img = &base_img; let closure = |proc: Arc<Process>| async move { let started_at = std::time::Instant::now(); // Relational WAL records are applied using wal-redo-postgres let result = proc .apply_wal_records(rel, blknum, base_img, records, wal_redo_timeout) .await .context("apply_wal_records"); let duration = started_at.elapsed(); let len = records.len(); let nbytes = records.iter().fold(0, |acumulator, record| { acumulator + match &record.1 { NeonWalRecord::Postgres { rec, .. } => rec.len(), _ => unreachable!("Only PostgreSQL records are accepted in this batch"), } }); WAL_REDO_TIME.observe(duration.as_secs_f64()); WAL_REDO_RECORDS_HISTOGRAM.observe(len as f64); WAL_REDO_BYTES_HISTOGRAM.observe(nbytes as f64); debug!( "postgres applied {} WAL records ({} bytes) in {} us to reconstruct page image at LSN {}", len, nbytes, duration.as_micros(), lsn ); if let Err(e) = result.as_ref() { macro_rules! message { ($level:tt) => { $level!( "error applying {} WAL records {}..{} ({} bytes) to key {} during {}, from base image with LSN {} to reconstruct page image at LSN {} n_attempts={}: {:?}", records.len(), records.first().map(|p| p.0).unwrap_or(Lsn(0)), records.last().map(|p| p.0).unwrap_or(Lsn(0)), nbytes, key, redo_attempt_type, base_img_lsn, lsn, n_attempts, e, ) } } match redo_attempt_type { RedoAttemptType::ReadPage => message!(error), RedoAttemptType::LegacyCompaction => message!(error), RedoAttemptType::GcCompaction => message!(warn), } } result.map_err(Error::Other) }; let result = self.do_with_walredo_process(pg_version, closure).await; if result.is_ok() && n_attempts != 0 { info!(n_attempts, "retried walredo succeeded"); } n_attempts += 1; if n_attempts > max_retry_attempts || result.is_ok() { return result; } } } /// /// Process a batch of WAL records using bespoken Neon code. /// fn apply_batch_neon( &self, key: Key, lsn: Lsn, base_img: Option<Bytes>, records: &[(Lsn, NeonWalRecord)], ) -> Result<Bytes, Error> { let start_time = Instant::now(); let mut page = BytesMut::new(); if let Some(fpi) = base_img { // If full-page image is provided, then use it... page.extend_from_slice(&fpi[..]); } else { // All the current WAL record types that we can handle require a base image. bail!("invalid neon WAL redo request with no base image"); } // Apply all the WAL records in the batch for (record_lsn, record) in records.iter() { self.apply_record_neon(key, &mut page, *record_lsn, record)?; } // Success! let duration = start_time.elapsed(); // FIXME: using the same metric here creates a bimodal distribution by default, and because // there could be multiple batch sizes this would be N+1 modal. WAL_REDO_TIME.observe(duration.as_secs_f64()); debug!( "neon applied {} WAL records in {} us to reconstruct page image at LSN {}", records.len(), duration.as_micros(), lsn ); Ok(page.freeze()) } fn apply_record_neon( &self, key: Key, page: &mut BytesMut, record_lsn: Lsn, record: &NeonWalRecord, ) -> anyhow::Result<()> { apply_neon::apply_in_neon(record, record_lsn, key, page)?; Ok(()) } } #[cfg(test)] pub(crate) mod harness { use super::PostgresRedoManager; use crate::config::PageServerConf; use utils::{id::TenantId, shard::TenantShardId}; pub struct RedoHarness { // underscored because unused, except for removal at drop _repo_dir: camino_tempfile::Utf8TempDir, pub manager: PostgresRedoManager, tenant_shard_id: TenantShardId, } impl RedoHarness { pub fn new() -> anyhow::Result<Self> { crate::tenant::harness::setup_logging(); let repo_dir = camino_tempfile::tempdir()?; let conf = PageServerConf::dummy_conf(repo_dir.path().to_path_buf()); let conf = Box::leak(Box::new(conf)); let tenant_shard_id = TenantShardId::unsharded(TenantId::generate()); let manager = PostgresRedoManager::new(conf, tenant_shard_id); Ok(RedoHarness { _repo_dir: repo_dir, manager, tenant_shard_id, }) } pub fn span(&self) -> tracing::Span { tracing::info_span!("RedoHarness", tenant_id=%self.tenant_shard_id.tenant_id, shard_id=%self.tenant_shard_id.shard_slug()) } } } #[cfg(test)] mod tests { use std::str::FromStr; use bytes::Bytes; use pageserver_api::key::Key; use postgres_ffi::PgMajorVersion; use tracing::Instrument; use utils::lsn::Lsn; use wal_decoder::models::record::NeonWalRecord; use crate::walredo::RedoAttemptType; use crate::walredo::harness::RedoHarness; #[tokio::test] async fn test_ping() { let h = RedoHarness::new().unwrap(); h.manager .ping(PgMajorVersion::PG14) .instrument(h.span()) .await .expect("ping should work"); } #[tokio::test] async fn short_v14_redo() { let expected = std::fs::read("test_data/short_v14_redo.page").unwrap(); let h = RedoHarness::new().unwrap(); let page = h .manager .request_redo( Key { field1: 0, field2: 1663, field3: 13010, field4: 1259, field5: 0, field6: 0, }, Lsn::from_str("0/16E2408").unwrap(), None, short_records(), PgMajorVersion::PG14, RedoAttemptType::ReadPage, ) .instrument(h.span()) .await .unwrap(); assert_eq!(&expected, &*page); } #[tokio::test] async fn short_v14_fails_for_wrong_key_but_returns_zero_page() { let h = RedoHarness::new().unwrap(); let page = h .manager .request_redo( Key { field1: 0, field2: 1663, // key should be 13010 field3: 13130, field4: 1259, field5: 0, field6: 0, }, Lsn::from_str("0/16E2408").unwrap(), None, short_records(), PgMajorVersion::PG14, RedoAttemptType::ReadPage, ) .instrument(h.span()) .await .unwrap(); // TODO: there will be some stderr printout, which is forwarded to tracing that could // perhaps be captured as long as it's in the same thread. assert_eq!(page, crate::ZERO_PAGE); } #[tokio::test] async fn test_stderr() { let h = RedoHarness::new().unwrap(); h .manager .request_redo( Key::from_i128(0), Lsn::INVALID, None, short_records(), PgMajorVersion::PG16, /* 16 currently produces stderr output on startup, which adds a nice extra edge */ RedoAttemptType::ReadPage, ) .instrument(h.span()) .await .unwrap_err(); } #[allow(clippy::octal_escapes)] fn short_records() -> Vec<(Lsn, NeonWalRecord)> { vec![ ( Lsn::from_str("0/16A9388").unwrap(), NeonWalRecord::Postgres { will_init: true, rec: Bytes::from_static(b"j\x03\0\0\0\x04\0\0\xe8\x7fj\x01\0\0\0\0\0\n\0\0\xd0\x16\x13Y\0\x10\0\04\x03\xd4\0\x05\x7f\x06\0\0\xd22\0\0\xeb\x04\0\0\0\0\0\0\xff\x03\0\0\0\0\x80\xeca\x01\0\0\x01\0\xd4\0\xa0\x1d\0 \x04 \0\0\0\0/\0\x01\0\xa0\x9dX\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0.\0\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\00\x9f\x9a\x01P\x9e\xb2\x01\0\x04\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x02\0!\0\x01\x08 \xff\xff\xff?\0\0\0\0\0\0@\0\0another_table\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x98\x08\0\0\x02@\0\0\0\0\0\0\n\0\0\0\x02\0\0\0\0@\0\0\0\0\0\0\0\0\0\0\0\0\x80\xbf\0\0\0\0\0\0\0\0\0\0pr\x01\0\0\0\0\0\0\0\0\x01d\0\0\0\0\0\0\x04\0\0\x01\0\0\0\0\0\0\0\x0c\x02\0\0\0\0\0\0\0\0\0\0\0\0\0\0/\0!\x80\x03+ \xff\xff\xff\x7f\0\0\0\0\0\xdf\x04\0\0pg_type\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x0b\0\0\0G\0\0\0\0\0\0\0\n\0\0\0\x02\0\0\0\0\0\0\0\0\0\0\0\x0e\0\0\0\0@\x16D\x0e\0\0\0K\x10\0\0\x01\0pr \0\0\0\0\0\0\0\0\x01n\0\0\0\0\0\xd6\x02\0\0\x01\0\0\0[\x01\0\0\0\0\0\0\0\t\x04\0\0\x02\0\0\0\x01\0\0\0\n\0\0\0\n\0\0\0\x7f\0\0\0\0\0\0\0\n\0\0\0\x02\0\0\0\0\0\0C\x01\0\0\x15\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0.\0!\x80\x03+ \xff\xff\xff\x7f\0\0\0\0\0;\n\0\0pg_statistic\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x0b\0\0\0\xfd.\0\0\0\0\0\0\n\0\0\0\x02\0\0\0;\n\0\0\0\0\0\0\x13\0\0\0\0\0\xcbC\x13\0\0\0\x18\x0b\0\0\x01\0pr\x1f\0\0\0\0\0\0\0\0\x01n\0\0\0\0\0\xd6\x02\0\0\x01\0\0\0C\x01\0\0\0\0\0\0\0\t\x04\0\0\x01\0\0\0\x01\0\0\0\n\0\0\0\n\0\0\0\x7f\0\0\0\0\0\0\x02\0\x01") } ), ( Lsn::from_str("0/16D4080").unwrap(), NeonWalRecord::Postgres { will_init: false, rec: Bytes::from_static(b"\xbc\0\0\0\0\0\0\0h?m\x01\0\0\0\0p\n\0\09\x08\xa3\xea\0 \x8c\0\x7f\x06\0\0\xd22\0\0\xeb\x04\0\0\0\0\0\0\xff\x02\0@\0\0another_table\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x98\x08\0\0\x02@\0\0\0\0\0\0\n\0\0\0\x02\0\0\0\0@\0\0\0\0\0\0\x05\0\0\0\0@zD\x05\0\0\0\0\0\0\0\0\0pr\x01\0\0\0\0\0\0\0\0\x01d\0\0\0\0\0\0\x04\0\0\x01\0\0\0\x02\0") } ) ] } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/page_service.rs
pageserver/src/page_service.rs
//! The Page Service listens for client connections and serves their GetPage@LSN //! requests. use std::any::Any; use std::borrow::Cow; use std::num::NonZeroUsize; use std::os::fd::AsRawFd; use std::pin::Pin; use std::str::FromStr; use std::sync::Arc; use std::task::{Context, Poll}; use std::time::{Duration, Instant, SystemTime}; use std::{io, str}; use anyhow::{Context as _, bail}; use bytes::{Buf as _, BufMut as _, BytesMut}; use chrono::Utc; use futures::future::BoxFuture; use futures::stream::FuturesUnordered; use futures::{FutureExt, Stream, StreamExt as _}; use itertools::Itertools; use jsonwebtoken::TokenData; use once_cell::sync::OnceCell; use pageserver_api::config::{ GetVectoredConcurrentIo, PageServicePipeliningConfig, PageServicePipeliningConfigPipelined, PageServiceProtocolPipelinedBatchingStrategy, PageServiceProtocolPipelinedExecutionStrategy, }; use pageserver_api::key::rel_block_to_key; use pageserver_api::models::{PageTraceEvent, TenantState}; use pageserver_api::pagestream_api::{ self, PagestreamBeMessage, PagestreamDbSizeRequest, PagestreamDbSizeResponse, PagestreamErrorResponse, PagestreamExistsRequest, PagestreamExistsResponse, PagestreamFeMessage, PagestreamGetPageRequest, PagestreamGetSlruSegmentRequest, PagestreamGetSlruSegmentResponse, PagestreamNblocksRequest, PagestreamNblocksResponse, PagestreamProtocolVersion, PagestreamRequest, }; use pageserver_api::reltag::SlruKind; use pageserver_api::shard::TenantShardId; use pageserver_page_api::proto; use pageserver_page_api::{self as page_api, GetPageSplitter}; use postgres_backend::{ AuthType, PostgresBackend, PostgresBackendReader, QueryError, is_expected_io_error, }; use postgres_ffi::BLCKSZ; use postgres_ffi_types::constants::DEFAULTTABLESPACE_OID; use pq_proto::framed::ConnectionError; use pq_proto::{BeMessage, FeMessage, FeStartupPacket, RowDescriptor}; use smallvec::{SmallVec, smallvec}; use strum_macros::IntoStaticStr; use tokio::io::{AsyncRead, AsyncReadExt as _, AsyncWrite, AsyncWriteExt as _, BufWriter}; use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; use tonic::service::Interceptor as _; use tonic::transport::server::TcpConnectInfo; use tracing::*; use utils::auth::{Claims, Scope, SwappableJwtAuth}; use utils::id::{TenantId, TenantTimelineId, TimelineId}; use utils::logging::log_slow; use utils::lsn::Lsn; use utils::shard::ShardIndex; use utils::simple_rcu::RcuReadGuard; use utils::sync::gate::{Gate, GateGuard}; use utils::sync::spsc_fold; use utils::{failpoint_support, span_record}; use crate::auth::check_permission; use crate::basebackup::{self, BasebackupError}; use crate::config::PageServerConf; use crate::context::{ DownloadBehavior, PerfInstrumentFutureExt, RequestContext, RequestContextBuilder, }; use crate::feature_resolver::FeatureResolver; use crate::metrics::{ self, COMPUTE_COMMANDS_COUNTERS, ComputeCommandKind, GetPageBatchBreakReason, LIVE_CONNECTIONS, MISROUTED_PAGESTREAM_REQUESTS, PAGESTREAM_HANDLER_RESULTS_TOTAL, SmgrOpTimer, TimelineMetrics, }; use crate::pgdatadir_mapping::{LsnRange, Version}; use crate::span::{ debug_assert_current_span_has_tenant_and_timeline_id, debug_assert_current_span_has_tenant_and_timeline_id_no_shard_id, }; use crate::task_mgr::{self, COMPUTE_REQUEST_RUNTIME, TaskKind}; use crate::tenant::mgr::{ GetActiveTenantError, GetTenantError, ShardResolveResult, ShardSelector, TenantManager, }; use crate::tenant::storage_layer::IoConcurrency; use crate::tenant::timeline::handle::{Handle, HandleUpgradeError, WeakHandle}; use crate::tenant::timeline::{self, WaitLsnError, WaitLsnTimeout, WaitLsnWaiter}; use crate::tenant::{GetTimelineError, PageReconstructError, Timeline}; use crate::{CancellableTask, PERF_TRACE_TARGET, timed_after_cancellation}; /// How long we may wait for a [`crate::tenant::mgr::TenantSlot::InProgress`]` and/or a [`crate::tenant::TenantShard`] which /// is not yet in state [`TenantState::Active`]. /// /// NB: this is a different value than [`crate::http::routes::ACTIVE_TENANT_TIMEOUT`]. /// HADRON: reduced timeout and we will retry in Cache::get(). const ACTIVE_TENANT_TIMEOUT: Duration = Duration::from_millis(5000); /// Threshold at which to log slow GetPage requests. const LOG_SLOW_GETPAGE_THRESHOLD: Duration = Duration::from_secs(30); /// The idle time before sending TCP keepalive probes for gRPC connections. The /// interval and timeout between each probe is configured via sysctl. This /// allows detecting dead connections sooner. const GRPC_TCP_KEEPALIVE_TIME: Duration = Duration::from_secs(60); /// Whether to enable TCP nodelay for gRPC connections. This disables Nagle's /// algorithm, which can cause latency spikes for small messages. const GRPC_TCP_NODELAY: bool = true; /// The interval between HTTP2 keepalive pings. This allows shutting down server /// tasks when clients are unresponsive. const GRPC_HTTP2_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(30); /// The timeout for HTTP2 keepalive pings. Should be <= GRPC_KEEPALIVE_INTERVAL. const GRPC_HTTP2_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(20); /// Number of concurrent gRPC streams per TCP connection. We expect something /// like 8 GetPage streams per connections, plus any unary requests. const GRPC_MAX_CONCURRENT_STREAMS: u32 = 256; /////////////////////////////////////////////////////////////////////////////// pub struct Listener { cancel: CancellationToken, /// Cancel the listener task through `listen_cancel` to shut down the listener /// and get a handle on the existing connections. task: JoinHandle<Connections>, } pub struct Connections { cancel: CancellationToken, tasks: tokio::task::JoinSet<ConnectionHandlerResult>, gate: Gate, } pub fn spawn( conf: &'static PageServerConf, tenant_manager: Arc<TenantManager>, pg_auth: Option<Arc<SwappableJwtAuth>>, perf_trace_dispatch: Option<Dispatch>, tcp_listener: tokio::net::TcpListener, tls_config: Option<Arc<rustls::ServerConfig>>, feature_resolver: FeatureResolver, ) -> Listener { let cancel = CancellationToken::new(); let libpq_ctx = RequestContext::todo_child( TaskKind::LibpqEndpointListener, // listener task shouldn't need to download anything. (We will // create a separate sub-contexts for each connection, with their // own download behavior. This context is used only to listen and // accept connections.) DownloadBehavior::Error, ); let task = COMPUTE_REQUEST_RUNTIME.spawn(task_mgr::exit_on_panic_or_error( "libpq listener", libpq_listener_main( conf, tenant_manager, pg_auth, perf_trace_dispatch, tcp_listener, conf.pg_auth_type, tls_config, conf.page_service_pipelining.clone(), feature_resolver, libpq_ctx, cancel.clone(), ) .map(anyhow::Ok), )); Listener { cancel, task } } impl Listener { pub async fn stop_accepting(self) -> Connections { self.cancel.cancel(); self.task .await .expect("unreachable: we wrap the listener task in task_mgr::exit_on_panic_or_error") } } impl Connections { pub(crate) async fn shutdown(self) { let Self { cancel, mut tasks, gate, } = self; cancel.cancel(); while let Some(res) = tasks.join_next().await { Self::handle_connection_completion(res); } gate.close().await; } fn handle_connection_completion(res: Result<anyhow::Result<()>, tokio::task::JoinError>) { match res { Ok(Ok(())) => {} Ok(Err(e)) => error!("error in page_service connection task: {:?}", e), Err(e) => error!("page_service connection task panicked: {:?}", e), } } } /// /// Main loop of the page service. /// /// Listens for connections, and launches a new handler task for each. /// /// Returns Ok(()) upon cancellation via `cancel`, returning the set of /// open connections. /// #[allow(clippy::too_many_arguments)] pub async fn libpq_listener_main( conf: &'static PageServerConf, tenant_manager: Arc<TenantManager>, auth: Option<Arc<SwappableJwtAuth>>, perf_trace_dispatch: Option<Dispatch>, listener: tokio::net::TcpListener, auth_type: AuthType, tls_config: Option<Arc<rustls::ServerConfig>>, pipelining_config: PageServicePipeliningConfig, feature_resolver: FeatureResolver, listener_ctx: RequestContext, listener_cancel: CancellationToken, ) -> Connections { let connections_cancel = CancellationToken::new(); let connections_gate = Gate::default(); let mut connection_handler_tasks = tokio::task::JoinSet::default(); loop { let gate_guard = match connections_gate.enter() { Ok(guard) => guard, Err(_) => break, }; let accepted = tokio::select! { biased; _ = listener_cancel.cancelled() => break, next = connection_handler_tasks.join_next(), if !connection_handler_tasks.is_empty() => { let res = next.expect("we dont poll while empty"); Connections::handle_connection_completion(res); continue; } accepted = listener.accept() => accepted, }; match accepted { Ok((socket, peer_addr)) => { // Connection established. Spawn a new task to handle it. debug!("accepted connection from {}", peer_addr); let local_auth = auth.clone(); let connection_ctx = RequestContextBuilder::from(&listener_ctx) .task_kind(TaskKind::PageRequestHandler) .download_behavior(DownloadBehavior::Download) .perf_span_dispatch(perf_trace_dispatch.clone()) .detached_child(); connection_handler_tasks.spawn(page_service_conn_main( conf, tenant_manager.clone(), local_auth, socket, auth_type, tls_config.clone(), pipelining_config.clone(), feature_resolver.clone(), connection_ctx, connections_cancel.child_token(), gate_guard, )); } Err(err) => { // accept() failed. Log the error, and loop back to retry on next connection. error!("accept() failed: {:?}", err); } } } debug!("page_service listener loop terminated"); Connections { cancel: connections_cancel, tasks: connection_handler_tasks, gate: connections_gate, } } type ConnectionHandlerResult = anyhow::Result<()>; /// Perf root spans start at the per-request level, after shard routing. /// This struct carries connection-level information to the root perf span definition. #[derive(Clone, Default)] struct ConnectionPerfSpanFields { peer_addr: String, application_name: Option<String>, compute_mode: Option<String>, } #[instrument(skip_all, fields(peer_addr, application_name, compute_mode))] #[allow(clippy::too_many_arguments)] async fn page_service_conn_main( conf: &'static PageServerConf, tenant_manager: Arc<TenantManager>, auth: Option<Arc<SwappableJwtAuth>>, socket: tokio::net::TcpStream, auth_type: AuthType, tls_config: Option<Arc<rustls::ServerConfig>>, pipelining_config: PageServicePipeliningConfig, feature_resolver: FeatureResolver, connection_ctx: RequestContext, cancel: CancellationToken, gate_guard: GateGuard, ) -> ConnectionHandlerResult { let _guard = LIVE_CONNECTIONS .with_label_values(&["page_service"]) .guard(); socket .set_nodelay(true) .context("could not set TCP_NODELAY")?; let socket_fd = socket.as_raw_fd(); let peer_addr = socket.peer_addr().context("get peer address")?; let perf_span_fields = ConnectionPerfSpanFields { peer_addr: peer_addr.to_string(), application_name: None, // filled in later compute_mode: None, // filled in later }; tracing::Span::current().record("peer_addr", field::display(peer_addr)); // setup read timeout of 10 minutes. the timeout is rather arbitrary for requirements: // - long enough for most valid compute connections // - less than infinite to stop us from "leaking" connections to long-gone computes // // no write timeout is used, because the kernel is assumed to error writes after some time. let mut socket = tokio_io_timeout::TimeoutReader::new(socket); let default_timeout_ms = 10 * 60 * 1000; // 10 minutes by default let socket_timeout_ms = (|| { fail::fail_point!("simulated-bad-compute-connection", |avg_timeout_ms| { // Exponential distribution for simulating // poor network conditions, expect about avg_timeout_ms to be around 15 // in tests if let Some(avg_timeout_ms) = avg_timeout_ms { let avg = avg_timeout_ms.parse::<i64>().unwrap() as f32; let u = rand::random::<f32>(); ((1.0 - u).ln() / (-avg)) as u64 } else { default_timeout_ms } }); default_timeout_ms })(); // A timeout here does not mean the client died, it can happen if it's just idle for // a while: we will tear down this PageServerHandler and instantiate a new one if/when // they reconnect. socket.set_timeout(Some(std::time::Duration::from_millis(socket_timeout_ms))); let socket = Box::pin(socket); fail::fail_point!("ps::connection-start::pre-login"); // XXX: pgbackend.run() should take the connection_ctx, // and create a child per-query context when it invokes process_query. // But it's in a shared crate, so, we store connection_ctx inside PageServerHandler // and create the per-query context in process_query ourselves. let mut conn_handler = PageServerHandler::new( tenant_manager, auth, pipelining_config, conf.get_vectored_concurrent_io, perf_span_fields, connection_ctx, cancel.clone(), feature_resolver.clone(), gate_guard, ); let pgbackend = PostgresBackend::new_from_io(socket_fd, socket, peer_addr, auth_type, tls_config)?; match pgbackend.run(&mut conn_handler, &cancel).await { Ok(()) => { // we've been requested to shut down Ok(()) } Err(QueryError::Disconnected(ConnectionError::Io(io_error))) => { if is_expected_io_error(&io_error) { info!("Postgres client disconnected ({io_error})"); Ok(()) } else { let tenant_id = conn_handler.timeline_handles.as_ref().unwrap().tenant_id(); Err(io_error).context(format!( "Postgres connection error for tenant_id={tenant_id:?} client at peer_addr={peer_addr}" )) } } other => { let tenant_id = conn_handler.timeline_handles.as_ref().unwrap().tenant_id(); other.context(format!( "Postgres query error for tenant_id={tenant_id:?} client peer_addr={peer_addr}" )) } } } /// Page service connection handler. struct PageServerHandler { auth: Option<Arc<SwappableJwtAuth>>, claims: Option<Claims>, /// The context created for the lifetime of the connection /// services by this PageServerHandler. /// For each query received over the connection, /// `process_query` creates a child context from this one. connection_ctx: RequestContext, perf_span_fields: ConnectionPerfSpanFields, cancel: CancellationToken, /// None only while pagestream protocol is being processed. timeline_handles: Option<TimelineHandles>, pipelining_config: PageServicePipeliningConfig, get_vectored_concurrent_io: GetVectoredConcurrentIo, feature_resolver: FeatureResolver, gate_guard: GateGuard, } struct TimelineHandles { wrapper: TenantManagerWrapper, /// Note on size: the typical size of this map is 1. The largest size we expect /// to see is the number of shards divided by the number of pageservers (typically < 2), /// or the ratio used when splitting shards (i.e. how many children created from one) /// parent shard, where a "large" number might be ~8. handles: timeline::handle::Cache<TenantManagerTypes>, } impl TimelineHandles { fn new(tenant_manager: Arc<TenantManager>) -> Self { Self { wrapper: TenantManagerWrapper { tenant_manager, tenant_id: OnceCell::new(), }, handles: Default::default(), } } async fn get( &mut self, tenant_id: TenantId, timeline_id: TimelineId, shard_selector: ShardSelector, ) -> Result<Handle<TenantManagerTypes>, GetActiveTimelineError> { if *self.wrapper.tenant_id.get_or_init(|| tenant_id) != tenant_id { return Err(GetActiveTimelineError::Tenant( GetActiveTenantError::SwitchedTenant, )); } self.handles .get(timeline_id, shard_selector, &self.wrapper) .await } fn tenant_id(&self) -> Option<TenantId> { self.wrapper.tenant_id.get().copied() } } pub(crate) struct TenantManagerWrapper { tenant_manager: Arc<TenantManager>, // We do not support switching tenant_id on a connection at this point. // We can can add support for this later if needed without changing // the protocol. tenant_id: once_cell::sync::OnceCell<TenantId>, } pub(crate) struct TenantManagerTypes; impl timeline::handle::Types for TenantManagerTypes { type TenantManager = TenantManagerWrapper; type Timeline = TenantManagerCacheItem; } pub(crate) struct TenantManagerCacheItem { pub(crate) timeline: Arc<Timeline>, // allow() for cheap propagation through RequestContext inside a task #[allow(clippy::redundant_allocation)] pub(crate) metrics: Arc<Arc<TimelineMetrics>>, #[allow(dead_code)] // we store it to keep the gate open pub(crate) gate_guard: GateGuard, } impl std::ops::Deref for TenantManagerCacheItem { type Target = Arc<Timeline>; fn deref(&self) -> &Self::Target { &self.timeline } } impl timeline::handle::Timeline<TenantManagerTypes> for TenantManagerCacheItem { fn shard_timeline_id(&self) -> timeline::handle::ShardTimelineId { Timeline::shard_timeline_id(&self.timeline) } fn per_timeline_state(&self) -> &timeline::handle::PerTimelineState<TenantManagerTypes> { &self.timeline.handles } fn get_shard_identity(&self) -> &pageserver_api::shard::ShardIdentity { Timeline::get_shard_identity(&self.timeline) } } impl timeline::handle::TenantManager<TenantManagerTypes> for TenantManagerWrapper { async fn resolve( &self, timeline_id: TimelineId, shard_selector: ShardSelector, ) -> Result<TenantManagerCacheItem, GetActiveTimelineError> { let tenant_id = self.tenant_id.get().expect("we set this in get()"); let timeout = ACTIVE_TENANT_TIMEOUT; let wait_start = Instant::now(); let deadline = wait_start + timeout; let tenant_shard = loop { let resolved = self .tenant_manager .resolve_attached_shard(tenant_id, shard_selector); match resolved { ShardResolveResult::Found(tenant_shard) => break tenant_shard, ShardResolveResult::NotFound => { MISROUTED_PAGESTREAM_REQUESTS.inc(); return Err(GetActiveTimelineError::Tenant( GetActiveTenantError::NotFound(GetTenantError::NotFound(*tenant_id)), )); } ShardResolveResult::InProgress(barrier) => { // We can't authoritatively answer right now: wait for InProgress state // to end, then try again tokio::select! { _ = barrier.wait() => { // The barrier completed: proceed around the loop to try looking up again }, _ = tokio::time::sleep(deadline.duration_since(Instant::now())) => { return Err(GetActiveTimelineError::Tenant(GetActiveTenantError::WaitForActiveTimeout { latest_state: None, wait_time: timeout, })); } } } }; }; tracing::debug!("Waiting for tenant to enter active state..."); tenant_shard .wait_to_become_active(deadline.duration_since(Instant::now())) .await .map_err(GetActiveTimelineError::Tenant)?; let timeline = tenant_shard .get_timeline(timeline_id, true) .map_err(GetActiveTimelineError::Timeline)?; let gate_guard = match timeline.gate.enter() { Ok(guard) => guard, Err(_) => { return Err(GetActiveTimelineError::Timeline( GetTimelineError::ShuttingDown, )); } }; let metrics = Arc::new(Arc::clone(&timeline.metrics)); Ok(TenantManagerCacheItem { timeline, metrics, gate_guard, }) } } /// Whether to hold the applied GC cutoff guard when processing GetPage requests. /// This is determined once at the start of pagestream subprotocol handling based on /// feature flags, configuration, and test conditions. #[derive(Debug, Clone, Copy)] enum HoldAppliedGcCutoffGuard { Yes, No, } #[derive(thiserror::Error, Debug)] enum PageStreamError { /// We encountered an error that should prompt the client to reconnect: /// in practice this means we drop the connection without sending a response. #[error("Reconnect required: {0}")] Reconnect(Cow<'static, str>), /// We were instructed to shutdown while processing the query #[error("Shutting down")] Shutdown, /// Something went wrong reading a page: this likely indicates a pageserver bug #[error("Read error")] Read(#[source] PageReconstructError), /// Ran out of time waiting for an LSN #[error("LSN timeout: {0}")] LsnTimeout(WaitLsnError), /// The entity required to serve the request (tenant or timeline) is not found, /// or is not found in a suitable state to serve a request. #[error("Not found: {0}")] NotFound(Cow<'static, str>), /// Request asked for something that doesn't make sense, like an invalid LSN #[error("Bad request: {0}")] BadRequest(Cow<'static, str>), } impl From<PageStreamError> for tonic::Status { fn from(err: PageStreamError) -> Self { use tonic::Code; let message = err.to_string(); let code = match err { PageStreamError::Reconnect(_) => Code::Unavailable, PageStreamError::Shutdown => Code::Unavailable, PageStreamError::Read(err) => match err { PageReconstructError::Cancelled => Code::Unavailable, PageReconstructError::MissingKey(_) => Code::NotFound, PageReconstructError::AncestorLsnTimeout(err) => tonic::Status::from(err).code(), PageReconstructError::Other(_) => Code::Internal, PageReconstructError::WalRedo(_) => Code::Internal, }, PageStreamError::LsnTimeout(err) => tonic::Status::from(err).code(), PageStreamError::NotFound(_) => Code::NotFound, PageStreamError::BadRequest(_) => Code::InvalidArgument, }; tonic::Status::new(code, message) } } impl From<PageReconstructError> for PageStreamError { fn from(value: PageReconstructError) -> Self { match value { PageReconstructError::Cancelled => Self::Shutdown, e => Self::Read(e), } } } impl From<GetActiveTimelineError> for PageStreamError { fn from(value: GetActiveTimelineError) -> Self { match value { GetActiveTimelineError::Tenant(GetActiveTenantError::Cancelled) | GetActiveTimelineError::Tenant(GetActiveTenantError::WillNotBecomeActive( TenantState::Stopping { .. }, )) | GetActiveTimelineError::Timeline(GetTimelineError::ShuttingDown) => Self::Shutdown, GetActiveTimelineError::Tenant(e) => Self::NotFound(format!("{e}").into()), GetActiveTimelineError::Timeline(e) => Self::NotFound(format!("{e}").into()), } } } impl From<WaitLsnError> for PageStreamError { fn from(value: WaitLsnError) -> Self { match value { e @ WaitLsnError::Timeout(_) => Self::LsnTimeout(e), WaitLsnError::Shutdown => Self::Shutdown, e @ WaitLsnError::BadState { .. } => Self::Reconnect(format!("{e}").into()), } } } impl From<WaitLsnError> for QueryError { fn from(value: WaitLsnError) -> Self { match value { e @ WaitLsnError::Timeout(_) => Self::Other(anyhow::Error::new(e)), WaitLsnError::Shutdown => Self::Shutdown, WaitLsnError::BadState { .. } => Self::Reconnect, } } } #[derive(thiserror::Error, Debug)] struct BatchedPageStreamError { req: PagestreamRequest, err: PageStreamError, } impl std::fmt::Display for BatchedPageStreamError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.err.fmt(f) } } struct BatchedGetPageRequest { req: PagestreamGetPageRequest, timer: SmgrOpTimer, lsn_range: LsnRange, ctx: RequestContext, // If the request is perf enabled, this contains a context // with a perf span tracking the time spent waiting for the executor. batch_wait_ctx: Option<RequestContext>, } #[cfg(feature = "testing")] struct BatchedTestRequest { req: pagestream_api::PagestreamTestRequest, timer: SmgrOpTimer, } /// NB: we only hold [`timeline::handle::WeakHandle`] inside this enum, /// so that we don't keep the [`Timeline::gate`] open while the batch /// is being built up inside the [`spsc_fold`] (pagestream pipelining). #[derive(IntoStaticStr)] #[allow(clippy::large_enum_variant)] enum BatchedFeMessage { Exists { span: Span, timer: SmgrOpTimer, shard: WeakHandle<TenantManagerTypes>, req: PagestreamExistsRequest, }, Nblocks { span: Span, timer: SmgrOpTimer, shard: WeakHandle<TenantManagerTypes>, req: PagestreamNblocksRequest, }, GetPage { span: Span, shard: WeakHandle<TenantManagerTypes>, applied_gc_cutoff_guard: Option<RcuReadGuard<Lsn>>, pages: SmallVec<[BatchedGetPageRequest; 1]>, batch_break_reason: GetPageBatchBreakReason, }, DbSize { span: Span, timer: SmgrOpTimer, shard: WeakHandle<TenantManagerTypes>, req: PagestreamDbSizeRequest, }, GetSlruSegment { span: Span, timer: SmgrOpTimer, shard: WeakHandle<TenantManagerTypes>, req: PagestreamGetSlruSegmentRequest, }, #[cfg(feature = "testing")] Test { span: Span, shard: WeakHandle<TenantManagerTypes>, requests: Vec<BatchedTestRequest>, }, RespondError { span: Span, error: BatchedPageStreamError, }, } impl BatchedFeMessage { fn as_static_str(&self) -> &'static str { self.into() } fn observe_execution_start(&mut self, at: Instant) { match self { BatchedFeMessage::Exists { timer, .. } | BatchedFeMessage::Nblocks { timer, .. } | BatchedFeMessage::DbSize { timer, .. } | BatchedFeMessage::GetSlruSegment { timer, .. } => { timer.observe_execution_start(at); } BatchedFeMessage::GetPage { pages, .. } => { for page in pages { page.timer.observe_execution_start(at); } } #[cfg(feature = "testing")] BatchedFeMessage::Test { requests, .. } => { for req in requests { req.timer.observe_execution_start(at); } } BatchedFeMessage::RespondError { .. } => {} } } fn should_break_batch( &self, other: &BatchedFeMessage, max_batch_size: NonZeroUsize, batching_strategy: PageServiceProtocolPipelinedBatchingStrategy, ) -> Option<GetPageBatchBreakReason> { match (self, other) { ( BatchedFeMessage::GetPage { shard: accum_shard, pages: accum_pages, .. }, BatchedFeMessage::GetPage { shard: this_shard, pages: this_pages, .. }, ) => { assert_eq!(this_pages.len(), 1); if accum_pages.len() >= max_batch_size.get() { trace!(%max_batch_size, "stopping batching because of batch size"); assert_eq!(accum_pages.len(), max_batch_size.get()); return Some(GetPageBatchBreakReason::BatchFull); } if !accum_shard.is_same_handle_as(this_shard) { trace!("stopping batching because timeline object mismatch"); // TODO: we _could_ batch & execute each shard seperately (and in parallel). // But the current logic for keeping responses in order does not support that. return Some(GetPageBatchBreakReason::NonUniformTimeline); } match batching_strategy { PageServiceProtocolPipelinedBatchingStrategy::UniformLsn => { if let Some(last_in_batch) = accum_pages.last() { if last_in_batch.lsn_range.effective_lsn != this_pages[0].lsn_range.effective_lsn { trace!( accum_lsn = %last_in_batch.lsn_range.effective_lsn, this_lsn = %this_pages[0].lsn_range.effective_lsn, "stopping batching because LSN changed" ); return Some(GetPageBatchBreakReason::NonUniformLsn); } } } PageServiceProtocolPipelinedBatchingStrategy::ScatteredLsn => { // The read path doesn't curently support serving the same page at different LSNs. // While technically possible, it's uncertain if the complexity is worth it. // Break the batch if such a case is encountered. let same_page_different_lsn = accum_pages.iter().any(|batched| { batched.req.rel == this_pages[0].req.rel && batched.req.blkno == this_pages[0].req.blkno && batched.lsn_range.effective_lsn != this_pages[0].lsn_range.effective_lsn }); if same_page_different_lsn { trace!( rel=%this_pages[0].req.rel, blkno=%this_pages[0].req.blkno, lsn=%this_pages[0].lsn_range.effective_lsn, "stopping batching because same page was requested at different LSNs" ); return Some(GetPageBatchBreakReason::SamePageAtDifferentLsn); } } } None } #[cfg(feature = "testing")] ( BatchedFeMessage::Test {
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/task_mgr.rs
pageserver/src/task_mgr.rs
//! //! This module provides centralized handling of tokio tasks in the Page Server. //! //! We provide a few basic facilities: //! - A global registry of tasks that lists what kind of tasks they are, and //! which tenant or timeline they are working on //! //! - The ability to request a task to shut down. //! //! //! # How it works? //! //! There is a global hashmap of all the tasks (`TASKS`). Whenever a new //! task is spawned, a PageServerTask entry is added there, and when a //! task dies, it removes itself from the hashmap. If you want to kill a //! task, you can scan the hashmap to find it. //! //! # Task shutdown //! //! To kill a task, we rely on co-operation from the victim. Each task is //! expected to periodically call the `is_shutdown_requested()` function, and //! if it returns true, exit gracefully. In addition to that, when waiting for //! the network or other long-running operation, you can use //! `shutdown_watcher()` function to get a Future that will become ready if //! the current task has been requested to shut down. You can use that with //! Tokio select!(). //! //! TODO: This would be a good place to also handle panics in a somewhat sane way. //! Depending on what task panics, we might want to kill the whole server, or //! only a single tenant or timeline. //! use std::collections::HashMap; use std::fmt; use std::future::Future; use std::num::NonZeroUsize; use std::panic::AssertUnwindSafe; use std::str::FromStr; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::time::Duration; use futures::FutureExt; use once_cell::sync::Lazy; use pageserver_api::shard::TenantShardId; use tokio::task::JoinHandle; use tokio::task_local; use tokio_util::sync::CancellationToken; use tracing::{debug, error, info, warn}; use utils::env; use utils::id::TimelineId; use crate::metrics::set_tokio_runtime_setup; // // There are four runtimes: // // Compute request runtime // - used to handle connections from compute nodes. Any tasks related to satisfying // GetPage requests, base backups, import, and other such compute node operations // are handled by the Compute request runtime // - page_service.rs // - this includes layer downloads from remote storage, if a layer is needed to // satisfy a GetPage request // // Management request runtime // - used to handle HTTP API requests // // WAL receiver runtime: // - used to handle WAL receiver connections. // - and to receiver updates from storage_broker // // Background runtime // - layer flushing // - garbage collection // - compaction // - remote storage uploads // - initial tenant loading // // Everything runs in a tokio task. If you spawn new tasks, spawn it using the correct // runtime. // // There might be situations when one task needs to wait for a task running in another // Runtime to finish. For example, if a background operation needs a layer from remote // storage, it will start to download it. If a background operation needs a remote layer, // and the download was already initiated by a GetPage request, the background task // will wait for the download - running in the Page server runtime - to finish. // Another example: the initial tenant loading tasks are launched in the background ops // runtime. If a GetPage request comes in before the load of a tenant has finished, the // GetPage request will wait for the tenant load to finish. // // The core Timeline code is synchronous, and uses a bunch of std Mutexes and RWLocks to // protect data structures. Let's keep it that way. Synchronous code is easier to debug // and analyze, and there's a lot of hairy, low-level, performance critical code there. // // It's nice to have different runtimes, so that you can quickly eyeball how much CPU // time each class of operations is taking, with 'top -H' or similar. // // It's also good to avoid hogging all threads that would be needed to process // other operations, if the upload tasks e.g. get blocked on locks. It shouldn't // happen, but still. // pub(crate) static TOKIO_WORKER_THREADS: Lazy<NonZeroUsize> = Lazy::new(|| { // replicates tokio-1.28.1::loom::sys::num_cpus which is not available publicly // tokio would had already panicked for parsing errors or NotUnicode // // this will be wrong if any of the runtimes gets their worker threads configured to something // else, but that has not been needed in a long time. NonZeroUsize::new( std::env::var("TOKIO_WORKER_THREADS") .map(|s| s.parse::<usize>().unwrap()) .unwrap_or_else(|_e| usize::max(2, num_cpus::get())), ) .expect("the max() ensures that this is not zero") }); enum TokioRuntimeMode { SingleThreaded, MultiThreaded { num_workers: NonZeroUsize }, } impl FromStr for TokioRuntimeMode { type Err = String; fn from_str(s: &str) -> Result<Self, Self::Err> { match s { "current_thread" => Ok(TokioRuntimeMode::SingleThreaded), s => match s.strip_prefix("multi_thread:") { Some("default") => Ok(TokioRuntimeMode::MultiThreaded { num_workers: *TOKIO_WORKER_THREADS, }), Some(suffix) => { let num_workers = suffix.parse::<NonZeroUsize>().map_err(|e| { format!( "invalid number of multi-threaded runtime workers ({suffix:?}): {e}", ) })?; Ok(TokioRuntimeMode::MultiThreaded { num_workers }) } None => Err(format!("invalid runtime config: {s:?}")), }, } } } static TOKIO_THREAD_STACK_SIZE: Lazy<NonZeroUsize> = Lazy::new(|| { env::var("NEON_PAGESERVER_TOKIO_THREAD_STACK_SIZE") // the default 2MiB are insufficent, especially in debug mode .unwrap_or_else(|| NonZeroUsize::new(4 * 1024 * 1024).unwrap()) }); static ONE_RUNTIME: Lazy<Option<tokio::runtime::Runtime>> = Lazy::new(|| { let thread_name = "pageserver-tokio"; let Some(mode) = env::var("NEON_PAGESERVER_USE_ONE_RUNTIME") else { // If the env var is not set, leave this static as None. set_tokio_runtime_setup( "multiple-runtimes", NUM_MULTIPLE_RUNTIMES .checked_mul(*TOKIO_WORKER_THREADS) .unwrap(), ); return None; }; Some(match mode { TokioRuntimeMode::SingleThreaded => { set_tokio_runtime_setup("one-runtime-single-threaded", NonZeroUsize::new(1).unwrap()); tokio::runtime::Builder::new_current_thread() .thread_name(thread_name) .enable_all() .thread_stack_size(TOKIO_THREAD_STACK_SIZE.get()) .build() .expect("failed to create one single runtime") } TokioRuntimeMode::MultiThreaded { num_workers } => { set_tokio_runtime_setup("one-runtime-multi-threaded", num_workers); tokio::runtime::Builder::new_multi_thread() .thread_name(thread_name) .enable_all() .worker_threads(num_workers.get()) .thread_stack_size(TOKIO_THREAD_STACK_SIZE.get()) .build() .expect("failed to create one multi-threaded runtime") } }) }); /// Declare a lazy static variable named `$varname` that will resolve /// to a tokio runtime handle. If the env var `NEON_PAGESERVER_USE_ONE_RUNTIME` /// is set, this will resolve to `ONE_RUNTIME`. Otherwise, the macro invocation /// declares a separate runtime and the lazy static variable `$varname` /// will resolve to that separate runtime. /// /// The result is is that `$varname.spawn()` will use `ONE_RUNTIME` if /// `NEON_PAGESERVER_USE_ONE_RUNTIME` is set, and will use the separate runtime /// otherwise. macro_rules! pageserver_runtime { ($varname:ident, $name:literal) => { pub static $varname: Lazy<&'static tokio::runtime::Runtime> = Lazy::new(|| { if let Some(runtime) = &*ONE_RUNTIME { return runtime; } static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| { tokio::runtime::Builder::new_multi_thread() .thread_name($name) .worker_threads(TOKIO_WORKER_THREADS.get()) .enable_all() .thread_stack_size(TOKIO_THREAD_STACK_SIZE.get()) .build() .expect(std::concat!("Failed to create runtime ", $name)) }); &*RUNTIME }); }; } pageserver_runtime!(COMPUTE_REQUEST_RUNTIME, "compute request worker"); pageserver_runtime!(MGMT_REQUEST_RUNTIME, "mgmt request worker"); pageserver_runtime!(WALRECEIVER_RUNTIME, "walreceiver worker"); pageserver_runtime!(BACKGROUND_RUNTIME, "background op worker"); // Bump this number when adding a new pageserver_runtime! const NUM_MULTIPLE_RUNTIMES: NonZeroUsize = NonZeroUsize::new(4).unwrap(); #[derive(Debug, Clone, Copy)] pub struct PageserverTaskId(u64); impl fmt::Display for PageserverTaskId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } /// Each task that we track is associated with a "task ID". It's just an /// increasing number that we assign. Note that it is different from tokio::task::Id. static NEXT_TASK_ID: AtomicU64 = AtomicU64::new(1); /// Global registry of tasks static TASKS: Lazy<Mutex<HashMap<u64, Arc<PageServerTask>>>> = Lazy::new(|| Mutex::new(HashMap::new())); task_local! { // This is a cancellation token which will be cancelled when a task needs to shut down. The // root token is kept in the global registry, so that anyone can send the signal to request // task shutdown. static SHUTDOWN_TOKEN: CancellationToken; // Each task holds reference to its own PageServerTask here. static CURRENT_TASK: Arc<PageServerTask>; } /// /// There are many kinds of tasks in the system. Some are associated with a particular /// tenant or timeline, while others are global. /// /// Note that we don't try to limit how many task of a certain kind can be running /// at the same time. /// #[derive( Debug, // NB: enumset::EnumSetType derives PartialEq, Eq, Clone, Copy enumset::EnumSetType, enum_map::Enum, serde::Serialize, serde::Deserialize, strum_macros::IntoStaticStr, strum_macros::EnumString, )] pub enum TaskKind { // Pageserver startup, i.e., `main` Startup, // libpq listener task. It just accepts connection and spawns a // PageRequestHandler task for each connection. LibpqEndpointListener, // HTTP endpoint listener. HttpEndpointListener, /// Task that handles a single page service connection. A PageRequestHandler /// task starts detached from any particular tenant or timeline, but it can /// be associated with one later, after receiving a command from the client. /// Also used for the gRPC page service API, including the main server task. PageRequestHandler, /// Manages the WAL receiver connection for one timeline. /// It subscribes to events from storage_broker and decides which safekeeper to connect to. /// Once the decision has been made, it establishes the connection using the `tokio-postgres` library. /// There is at most one connection at any given time. /// /// That `tokio-postgres` library represents a connection as two objects: a `Client` and a `Connection`. /// The `Client` object is what library users use to make requests & get responses. /// Internally, `Client` hands over requests to the `Connection` object. /// The `Connection` object is responsible for speaking the wire protocol. /// /// Walreceiver uses a legacy abstraction called `TaskHandle` to represent the activity of establishing and handling a connection. /// The `WalReceiverManager` task ensures that this `TaskHandle` task does not outlive the `WalReceiverManager` task. /// For the `RequestContext` that we hand to the TaskHandle, we use the [`WalReceiverConnectionHandler`] task kind. /// /// Once the connection is established, the `TaskHandle` task spawns a /// [`WalReceiverConnectionPoller`] task that is responsible for polling /// the `Connection` object. /// A `CancellationToken` created by the `TaskHandle` task ensures /// that the [`WalReceiverConnectionPoller`] task will cancel soon after as the `TaskHandle` is dropped. /// /// [`WalReceiverConnectionHandler`]: Self::WalReceiverConnectionHandler /// [`WalReceiverConnectionPoller`]: Self::WalReceiverConnectionPoller WalReceiverManager, /// The `TaskHandle` task that executes `handle_walreceiver_connection`. /// See the comment on [`WalReceiverManager`]. /// /// [`WalReceiverManager`]: Self::WalReceiverManager WalReceiverConnectionHandler, /// The task that polls the `tokio-postgres::Connection` object. /// Spawned by task [`WalReceiverConnectionHandler`](Self::WalReceiverConnectionHandler). /// See the comment on [`WalReceiverManager`](Self::WalReceiverManager). WalReceiverConnectionPoller, // Garbage collection worker. One per tenant GarbageCollector, // Compaction. One per tenant. Compaction, // Eviction. One per timeline. Eviction, // Tenant housekeeping (flush idle ephemeral layers, shut down idle walredo, etc.). TenantHousekeeping, /// See [`crate::disk_usage_eviction_task`]. DiskUsageEviction, /// See [`crate::tenant::secondary`]. SecondaryDownloads, /// See [`crate::tenant::secondary`]. SecondaryUploads, // Initial logical size calculation InitialLogicalSizeCalculation, OndemandLogicalSizeCalculation, // Task that flushes frozen in-memory layers to disk LayerFlushTask, // Task that uploads a file to remote storage RemoteUploadTask, // task that handles the initial downloading of all tenants InitialLoad, // task that handles attaching a tenant Attach, // Used mostly for background deletion from s3 TimelineDeletionWorker, // task that handhes metrics collection MetricsCollection, // task that drives downloading layers DownloadAllRemoteLayers, // Task that calculates synthetis size for all active tenants CalculateSyntheticSize, // A request that comes in via the pageserver HTTP API. MgmtRequest, DebugTool, EphemeralFilePreWarmPageCache, LayerDownload, #[cfg(test)] UnitTest, DetachAncestor, ImportPgdata, /// Background task of [`crate::basebackup_cache::BasebackupCache`]. /// Prepares basebackups and clears outdated entries. BasebackupCache, } #[derive(Default)] struct MutableTaskState { /// Handle for waiting for the task to exit. It can be None, if the /// the task has already exited. join_handle: Option<JoinHandle<()>>, } struct PageServerTask { task_id: PageserverTaskId, kind: TaskKind, name: String, // To request task shutdown, just cancel this token. cancel: CancellationToken, /// Tasks may optionally be launched for a particular tenant/timeline, enabling /// later cancelling tasks for that tenant/timeline in [`shutdown_tasks`] tenant_shard_id: TenantShardId, timeline_id: Option<TimelineId>, mutable: Mutex<MutableTaskState>, } /// Launch a new task /// Note: if shutdown_process_on_error is set to true failure /// of the task will lead to shutdown of entire process pub fn spawn<F>( runtime: &tokio::runtime::Handle, kind: TaskKind, tenant_shard_id: TenantShardId, timeline_id: Option<TimelineId>, name: &str, future: F, ) -> PageserverTaskId where F: Future<Output = anyhow::Result<()>> + Send + 'static, { let cancel = CancellationToken::new(); let task_id = NEXT_TASK_ID.fetch_add(1, Ordering::Relaxed); let task = Arc::new(PageServerTask { task_id: PageserverTaskId(task_id), kind, name: name.to_string(), cancel: cancel.clone(), tenant_shard_id, timeline_id, mutable: Mutex::new(MutableTaskState { join_handle: None }), }); TASKS.lock().unwrap().insert(task_id, Arc::clone(&task)); let mut task_mut = task.mutable.lock().unwrap(); let task_name = name.to_string(); let task_cloned = Arc::clone(&task); let join_handle = runtime.spawn(task_wrapper( task_name, task_id, task_cloned, cancel, future, )); task_mut.join_handle = Some(join_handle); drop(task_mut); // The task is now running. Nothing more to do here PageserverTaskId(task_id) } /// This wrapper function runs in a newly-spawned task. It initializes the /// task-local variables and calls the payload function. async fn task_wrapper<F>( task_name: String, task_id: u64, task: Arc<PageServerTask>, shutdown_token: CancellationToken, future: F, ) where F: Future<Output = anyhow::Result<()>> + Send + 'static, { debug!("Starting task '{}'", task_name); // wrap the future so we log panics and errors let tenant_shard_id = task.tenant_shard_id; let timeline_id = task.timeline_id; let fut = async move { // We use AssertUnwindSafe here so that the payload function // doesn't need to be UnwindSafe. We don't do anything after the // unwinding that would expose us to unwind-unsafe behavior. let result = AssertUnwindSafe(future).catch_unwind().await; match result { Ok(Ok(())) => { debug!("Task '{}' exited normally", task_name); } Ok(Err(err)) => { error!( "Task '{}' tenant_shard_id: {:?}, timeline_id: {:?} exited with error: {:?}", task_name, tenant_shard_id, timeline_id, err ); } Err(err) => { error!( "Task '{}' tenant_shard_id: {:?}, timeline_id: {:?} panicked: {:?}", task_name, tenant_shard_id, timeline_id, err ); } } }; // add the task-locals let fut = CURRENT_TASK.scope(task, fut); let fut = SHUTDOWN_TOKEN.scope(shutdown_token, fut); // poll future to completion fut.await; // Remove our entry from the global hashmap. TASKS .lock() .unwrap() .remove(&task_id) .expect("no task in registry"); } pub async fn exit_on_panic_or_error<T, E>( task_name: &'static str, future: impl Future<Output = Result<T, E>>, ) -> T where E: std::fmt::Debug, { // We use AssertUnwindSafe here so that the payload function // doesn't need to be UnwindSafe. We don't do anything after the // unwinding that would expose us to unwind-unsafe behavior. let result = AssertUnwindSafe(future).catch_unwind().await; match result { Ok(Ok(val)) => val, Ok(Err(err)) => { error!( task_name, "Task exited with error, exiting process: {err:?}" ); std::process::exit(1); } Err(panic_obj) => { error!(task_name, "Task panicked, exiting process: {panic_obj:?}"); std::process::exit(1); } } } /// Signal and wait for tasks to shut down. /// /// /// The arguments are used to select the tasks to kill. Any None arguments are /// ignored. For example, to shut down all WalReceiver tasks: /// /// shutdown_tasks(Some(TaskKind::WalReceiver), None, None) /// /// Or to shut down all tasks for given timeline: /// /// shutdown_tasks(None, Some(tenant_shard_id), Some(timeline_id)) /// pub async fn shutdown_tasks( kind: Option<TaskKind>, tenant_shard_id: Option<TenantShardId>, timeline_id: Option<TimelineId>, ) { let mut victim_tasks = Vec::new(); { let tasks = TASKS.lock().unwrap(); for task in tasks.values() { if (kind.is_none() || Some(task.kind) == kind) && (tenant_shard_id.is_none() || Some(task.tenant_shard_id) == tenant_shard_id) && (timeline_id.is_none() || task.timeline_id == timeline_id) { task.cancel.cancel(); victim_tasks.push(( Arc::clone(task), task.kind, task.tenant_shard_id, task.timeline_id, )); } } } let log_all = kind.is_none() && tenant_shard_id.is_none() && timeline_id.is_none(); for (task, task_kind, tenant_shard_id, timeline_id) in victim_tasks { let join_handle = { let mut task_mut = task.mutable.lock().unwrap(); task_mut.join_handle.take() }; if let Some(mut join_handle) = join_handle { if log_all { // warn to catch these in tests; there shouldn't be any warn!(name = task.name, tenant_shard_id = ?tenant_shard_id, timeline_id = ?timeline_id, kind = ?task_kind, "stopping left-over"); } const INITIAL_COMPLAIN_TIMEOUT: Duration = Duration::from_secs(1); const PERIODIC_COMPLAIN_TIMEOUT: Duration = Duration::from_secs(60); if tokio::time::timeout(INITIAL_COMPLAIN_TIMEOUT, &mut join_handle) .await .is_err() { // allow some time to elapse before logging to cut down the number of log // lines. info!("waiting for task {} to shut down", task.name); loop { tokio::select! { // we never handled this return value, but: // - we don't deschedule which would lead to is_cancelled // - panics are already logged (is_panicked) // - task errors are already logged in the wrapper _ = &mut join_handle => break, _ = tokio::time::sleep(PERIODIC_COMPLAIN_TIMEOUT) => info!("still waiting for task {} to shut down", task.name), } } info!("task {} completed", task.name); } } else { // Possibly one of: // * The task had not even fully started yet. // * It was shut down concurrently and already exited } } } pub fn current_task_kind() -> Option<TaskKind> { CURRENT_TASK.try_with(|ct| ct.kind).ok() } pub fn current_task_id() -> Option<PageserverTaskId> { CURRENT_TASK.try_with(|ct| ct.task_id).ok() } /// A Future that can be used to check if the current task has been requested to /// shut down. pub async fn shutdown_watcher() { let token = SHUTDOWN_TOKEN .try_with(|t| t.clone()) .expect("shutdown_watcher() called in an unexpected task or thread"); token.cancelled().await; } /// Clone the current task's cancellation token, which can be moved across tasks. /// /// When the task which is currently executing is shutdown, the cancellation token will be /// cancelled. It can however be moved to other tasks, such as `tokio::task::spawn_blocking` or /// `tokio::task::JoinSet::spawn`. pub fn shutdown_token() -> CancellationToken { let res = SHUTDOWN_TOKEN.try_with(|t| t.clone()); if cfg!(test) { // in tests this method is called from non-taskmgr spawned tasks, and that is all ok. res.unwrap_or_default() } else { res.expect("shutdown_token() called in an unexpected task or thread") } } /// Has the current task been requested to shut down? pub fn is_shutdown_requested() -> bool { if let Ok(true_or_false) = SHUTDOWN_TOKEN.try_with(|t| t.is_cancelled()) { true_or_false } else { if !cfg!(test) { warn!("is_shutdown_requested() called in an unexpected task or thread"); } false } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/span.rs
pageserver/src/span.rs
use utils::tracing_span_assert::check_fields_present; mod extractors { use utils::tracing_span_assert::ConstExtractor; pub(super) const TENANT_ID: ConstExtractor = ConstExtractor::new("tenant_id"); pub(super) const SHARD_ID: ConstExtractor = ConstExtractor::new("shard_id"); pub(super) const TIMELINE_ID: ConstExtractor = ConstExtractor::new("timeline_id"); } #[track_caller] pub(crate) fn debug_assert_current_span_has_tenant_id() { if cfg!(debug_assertions) { if let Err(missing) = check_fields_present!([&extractors::TENANT_ID, &extractors::SHARD_ID]) { panic!("missing extractors: {missing:?}") } } } #[track_caller] pub(crate) fn debug_assert_current_span_has_tenant_and_timeline_id() { if cfg!(debug_assertions) { if let Err(missing) = check_fields_present!([ &extractors::TENANT_ID, &extractors::SHARD_ID, &extractors::TIMELINE_ID, ]) { panic!("missing extractors: {missing:?}") } } } #[track_caller] pub(crate) fn debug_assert_current_span_has_tenant_and_timeline_id_no_shard_id() { if cfg!(debug_assertions) { if let Err(missing) = check_fields_present!([&extractors::TENANT_ID, &extractors::TIMELINE_ID,]) { panic!("missing extractors: {missing:?}") } } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/auth.rs
pageserver/src/auth.rs
use utils::auth::{AuthError, Claims, Scope}; use utils::id::TenantId; pub fn check_permission(claims: &Claims, tenant_id: Option<TenantId>) -> Result<(), AuthError> { match (&claims.scope, tenant_id) { (Scope::Tenant, None) => Err(AuthError( "Attempt to access management api with tenant scope. Permission denied".into(), )), (Scope::Tenant, Some(tenant_id)) => { if claims.tenant_id.unwrap() != tenant_id { return Err(AuthError("Tenant id mismatch. Permission denied".into())); } Ok(()) } (Scope::PageServerApi, None) => Ok(()), // access to management api for PageServerApi scope (Scope::PageServerApi, Some(_)) => Ok(()), // access to tenant api using PageServerApi scope ( Scope::Admin | Scope::SafekeeperData | Scope::GenerationsApi | Scope::Infra | Scope::Scrubber | Scope::ControllerPeer | Scope::TenantEndpoint, _, ) => Err(AuthError( format!( "JWT scope '{:?}' is ineligible for Pageserver auth", claims.scope ) .into(), )), } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/l0_flush.rs
pageserver/src/l0_flush.rs
use std::num::NonZeroUsize; use std::sync::Arc; #[derive(Debug, PartialEq, Eq, Clone)] pub enum L0FlushConfig { Direct { max_concurrency: NonZeroUsize }, } impl Default for L0FlushConfig { fn default() -> Self { Self::Direct { // TODO: using num_cpus results in different peak memory usage on different instance types. max_concurrency: NonZeroUsize::new(usize::max(1, num_cpus::get())).unwrap(), } } } impl From<pageserver_api::models::L0FlushConfig> for L0FlushConfig { fn from(config: pageserver_api::models::L0FlushConfig) -> Self { match config { pageserver_api::models::L0FlushConfig::Direct { max_concurrency } => { Self::Direct { max_concurrency } } } } } #[derive(Clone)] pub struct L0FlushGlobalState(Arc<Inner>); pub enum Inner { Direct { semaphore: tokio::sync::Semaphore }, } impl L0FlushGlobalState { pub fn new(config: L0FlushConfig) -> Self { match config { L0FlushConfig::Direct { max_concurrency } => { let semaphore = tokio::sync::Semaphore::new(max_concurrency.get()); Self(Arc::new(Inner::Direct { semaphore })) } } } pub fn inner(&self) -> &Arc<Inner> { &self.0 } }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/walingest.rs
pageserver/src/walingest.rs
//! //! Parse PostgreSQL WAL records and store them in a neon Timeline. //! //! The pipeline for ingesting WAL looks like this: //! //! WAL receiver -> [`wal_decoder`] -> WalIngest -> Repository //! //! The WAL receiver receives a stream of WAL from the WAL safekeepers. //! Records get decoded and interpreted in the [`wal_decoder`] module //! and then stored to the Repository by WalIngest. //! //! The neon Repository can store page versions in two formats: as //! page images, or a WAL records. [`wal_decoder::models::InterpretedWalRecord::from_bytes_filtered`] //! extracts page images out of some WAL records, but mostly it's WAL //! records. If a WAL record modifies multiple pages, WalIngest //! will call Repository::put_rel_wal_record or put_rel_page_image functions //! separately for each modified page. //! //! To reconstruct a page using a WAL record, the Repository calls the //! code in walredo.rs. walredo.rs passes most WAL records to the WAL //! redo Postgres process, but some records it can handle directly with //! bespoken Rust code. use std::backtrace::Backtrace; use std::collections::HashMap; use std::sync::atomic::AtomicBool; use std::sync::{Arc, OnceLock}; use std::time::{Duration, Instant, SystemTime}; use bytes::{Buf, Bytes}; use pageserver_api::key::{Key, rel_block_to_key}; use pageserver_api::reltag::{BlockNumber, RelTag, SlruKind}; use pageserver_api::shard::ShardIdentity; use postgres_ffi::walrecord::*; use postgres_ffi::{ PgMajorVersion, TransactionId, dispatch_pgversion, enum_pgversion, enum_pgversion_dispatch, fsm_logical_to_physical, pg_constants, }; use postgres_ffi_types::TimestampTz; use postgres_ffi_types::forknum::{FSM_FORKNUM, INIT_FORKNUM, MAIN_FORKNUM, VISIBILITYMAP_FORKNUM}; use tracing::*; use utils::bin_ser::{DeserializeError, SerializeError}; use utils::lsn::Lsn; use utils::rate_limit::RateLimit; use utils::{critical_timeline, failpoint_support}; use wal_decoder::models::record::NeonWalRecord; use wal_decoder::models::*; use crate::ZERO_PAGE; use crate::context::RequestContext; use crate::metrics::WAL_INGEST; use crate::pgdatadir_mapping::{DatadirModification, Version}; use crate::span::debug_assert_current_span_has_tenant_and_timeline_id; use crate::tenant::{PageReconstructError, Timeline}; enum_pgversion! {CheckPoint, pgv::CheckPoint} impl CheckPoint { fn encode(&self) -> Result<Bytes, SerializeError> { enum_pgversion_dispatch!(self, CheckPoint, cp, { cp.encode() }) } fn update_next_xid(&mut self, xid: u32) -> bool { enum_pgversion_dispatch!(self, CheckPoint, cp, { cp.update_next_xid(xid) }) } pub fn update_next_multixid(&mut self, multi_xid: u32, multi_offset: u32) -> bool { enum_pgversion_dispatch!(self, CheckPoint, cp, { cp.update_next_multixid(multi_xid, multi_offset) }) } } /// Temporary limitation of WAL lag warnings after attach /// /// After tenant attach, we want to limit WAL lag warnings because /// we don't look at the WAL until the attach is complete, which /// might take a while. pub struct WalLagCooldown { /// Until when should this limitation apply at all active_until: std::time::Instant, /// The maximum lag to suppress. Lags above this limit get reported anyways. max_lag: Duration, } impl WalLagCooldown { pub fn new(attach_start: Instant, attach_duration: Duration) -> Self { Self { active_until: attach_start + attach_duration * 3 + Duration::from_secs(120), max_lag: attach_duration * 2 + Duration::from_secs(60), } } } pub struct WalIngest { attach_wal_lag_cooldown: Arc<OnceLock<WalLagCooldown>>, shard: ShardIdentity, checkpoint: CheckPoint, checkpoint_modified: bool, warn_ingest_lag: WarnIngestLag, } struct WarnIngestLag { lag_msg_ratelimit: RateLimit, future_lsn_msg_ratelimit: RateLimit, timestamp_invalid_msg_ratelimit: RateLimit, } pub struct WalIngestError { pub backtrace: std::backtrace::Backtrace, pub kind: WalIngestErrorKind, } #[derive(thiserror::Error, Debug)] pub enum WalIngestErrorKind { #[error(transparent)] #[allow(private_interfaces)] PageReconstructError(#[from] PageReconstructError), #[error(transparent)] DeserializationFailure(#[from] DeserializeError), #[error(transparent)] SerializationFailure(#[from] SerializeError), #[error("the request contains data not supported by pageserver: {0} @ {1}")] InvalidKey(Key, Lsn), #[error("twophase file for xid {0} already exists")] FileAlreadyExists(u64), #[error("slru segment {0:?}/{1} already exists")] SlruAlreadyExists(SlruKind, u32), #[error("relation already exists")] RelationAlreadyExists(RelTag), #[error("invalid reldir key {0}")] InvalidRelDirKey(Key), #[error(transparent)] LogicalError(anyhow::Error), #[error(transparent)] EncodeAuxFileError(anyhow::Error), #[error(transparent)] MaybeRelSizeV2Error(anyhow::Error), #[error("timeline shutting down")] Cancelled, } impl<T> From<T> for WalIngestError where WalIngestErrorKind: From<T>, { fn from(value: T) -> Self { WalIngestError { backtrace: Backtrace::capture(), kind: WalIngestErrorKind::from(value), } } } impl std::error::Error for WalIngestError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { self.kind.source() } } impl core::fmt::Display for WalIngestError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.kind.fmt(f) } } impl core::fmt::Debug for WalIngestError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { if f.alternate() { f.debug_map() .key(&"backtrace") .value(&self.backtrace) .key(&"kind") .value(&self.kind) .finish() } else { writeln!(f, "Error: {:?}", self.kind)?; if self.backtrace.status() == std::backtrace::BacktraceStatus::Captured { writeln!(f, "Stack backtrace: {:?}", self.backtrace)?; } Ok(()) } } } #[macro_export] macro_rules! ensure_walingest { ($($t:tt)*) => { _ = || -> Result<(), anyhow::Error> { anyhow::ensure!($($t)*); Ok(()) }().map_err(WalIngestErrorKind::LogicalError)?; }; } impl WalIngest { pub async fn new( timeline: &Timeline, startpoint: Lsn, ctx: &RequestContext, ) -> Result<WalIngest, WalIngestError> { // Fetch the latest checkpoint into memory, so that we can compare with it // quickly in `ingest_record` and update it when it changes. let checkpoint_bytes = timeline.get_checkpoint(startpoint, ctx).await?; let pgversion = timeline.pg_version; let checkpoint = dispatch_pgversion!(pgversion, { let checkpoint = pgv::CheckPoint::decode(&checkpoint_bytes)?; trace!("CheckPoint.nextXid = {}", checkpoint.nextXid.value); <pgv::CheckPoint as Into<CheckPoint>>::into(checkpoint) }); Ok(WalIngest { shard: *timeline.get_shard_identity(), checkpoint, checkpoint_modified: false, attach_wal_lag_cooldown: timeline.attach_wal_lag_cooldown.clone(), warn_ingest_lag: WarnIngestLag { lag_msg_ratelimit: RateLimit::new(std::time::Duration::from_secs(10)), future_lsn_msg_ratelimit: RateLimit::new(std::time::Duration::from_secs(10)), timestamp_invalid_msg_ratelimit: RateLimit::new(std::time::Duration::from_secs(10)), }, }) } /// Ingest an interpreted PostgreSQL WAL record by doing writes to the underlying key value /// storage of a given timeline. /// /// This function updates `lsn` field of `DatadirModification` /// /// This function returns `true` if the record was ingested, and `false` if it was filtered out pub async fn ingest_record( &mut self, interpreted: InterpretedWalRecord, modification: &mut DatadirModification<'_>, ctx: &RequestContext, ) -> Result<bool, WalIngestError> { WAL_INGEST.records_received.inc(); let prev_len = modification.len(); modification.set_lsn(interpreted.next_record_lsn)?; if matches!(interpreted.flush_uncommitted, FlushUncommittedRecords::Yes) { // Records of this type should always be preceded by a commit(), as they // rely on reading data pages back from the Timeline. assert!(!modification.has_dirty_data()); } assert!(!self.checkpoint_modified); if interpreted.xid != pg_constants::INVALID_TRANSACTION_ID && self.checkpoint.update_next_xid(interpreted.xid) { self.checkpoint_modified = true; } failpoint_support::sleep_millis_async!("wal-ingest-record-sleep"); match interpreted.metadata_record { Some(MetadataRecord::Heapam(rec)) => match rec { HeapamRecord::ClearVmBits(clear_vm_bits) => { self.ingest_clear_vm_bits(clear_vm_bits, modification, ctx) .await?; } }, Some(MetadataRecord::Neonrmgr(rec)) => match rec { NeonrmgrRecord::ClearVmBits(clear_vm_bits) => { self.ingest_clear_vm_bits(clear_vm_bits, modification, ctx) .await?; } }, Some(MetadataRecord::Smgr(rec)) => match rec { SmgrRecord::Create(create) => { self.ingest_xlog_smgr_create(create, modification, ctx) .await?; } SmgrRecord::Truncate(truncate) => { self.ingest_xlog_smgr_truncate(truncate, modification, ctx) .await?; } }, Some(MetadataRecord::Dbase(rec)) => match rec { DbaseRecord::Create(create) => { self.ingest_xlog_dbase_create(create, modification, ctx) .await?; } DbaseRecord::Drop(drop) => { self.ingest_xlog_dbase_drop(drop, modification, ctx).await?; } }, Some(MetadataRecord::Clog(rec)) => match rec { ClogRecord::ZeroPage(zero_page) => { self.ingest_clog_zero_page(zero_page, modification, ctx) .await?; } ClogRecord::Truncate(truncate) => { self.ingest_clog_truncate(truncate, modification, ctx) .await?; } }, Some(MetadataRecord::Xact(rec)) => { self.ingest_xact_record(rec, modification, ctx).await?; } Some(MetadataRecord::MultiXact(rec)) => match rec { MultiXactRecord::ZeroPage(zero_page) => { self.ingest_multixact_zero_page(zero_page, modification, ctx) .await?; } MultiXactRecord::Create(create) => { self.ingest_multixact_create(modification, &create)?; } MultiXactRecord::Truncate(truncate) => { self.ingest_multixact_truncate(modification, &truncate, ctx) .await?; } }, Some(MetadataRecord::Relmap(rec)) => match rec { RelmapRecord::Update(update) => { self.ingest_relmap_update(update, modification, ctx).await?; } }, Some(MetadataRecord::Xlog(rec)) => match rec { XlogRecord::Raw(raw) => { self.ingest_raw_xlog_record(raw, modification, ctx).await?; } }, Some(MetadataRecord::LogicalMessage(rec)) => match rec { LogicalMessageRecord::Put(put) => { self.ingest_logical_message_put(put, modification, ctx) .await?; } #[cfg(feature = "testing")] LogicalMessageRecord::Failpoint => { // This is a convenient way to make the WAL ingestion pause at // particular point in the WAL. For more fine-grained control, // we could peek into the message and only pause if it contains // a particular string, for example, but this is enough for now. failpoint_support::sleep_millis_async!( "pageserver-wal-ingest-logical-message-sleep" ); } }, Some(MetadataRecord::Standby(rec)) => { self.ingest_standby_record(rec).unwrap(); } Some(MetadataRecord::Replorigin(rec)) => { self.ingest_replorigin_record(rec, modification).await?; } None => { // There are two cases through which we end up here: // 1. The resource manager for the original PG WAL record // is [`pg_constants::RM_TBLSPC_ID`]. This is not a supported // record type within Neon. // 2. The resource manager id was unknown to // [`wal_decoder::decoder::MetadataRecord::from_decoded`]. // TODO(vlad): Tighten this up more once we build confidence // that case (2) does not happen in the field. } } modification .ingest_batch(interpreted.batch, &self.shard, ctx) .await?; // If checkpoint data was updated, store the new version in the repository if self.checkpoint_modified { let new_checkpoint_bytes = self.checkpoint.encode()?; modification.put_checkpoint(new_checkpoint_bytes)?; self.checkpoint_modified = false; } // Note that at this point this record is only cached in the modification // until commit() is called to flush the data into the repository and update // the latest LSN. Ok(modification.len() > prev_len) } /// This is the same as AdjustToFullTransactionId(xid) in PostgreSQL fn adjust_to_full_transaction_id(&self, xid: TransactionId) -> Result<u64, WalIngestError> { let next_full_xid = enum_pgversion_dispatch!(&self.checkpoint, CheckPoint, cp, { cp.nextXid.value }); let next_xid = (next_full_xid) as u32; let mut epoch = (next_full_xid >> 32) as u32; if xid > next_xid { // Wraparound occurred, must be from a prev epoch. if epoch == 0 { Err(WalIngestErrorKind::LogicalError(anyhow::anyhow!( "apparent XID wraparound with prepared transaction XID {xid}, nextXid is {next_full_xid}" )))?; } epoch -= 1; } Ok(((epoch as u64) << 32) | xid as u64) } async fn ingest_clear_vm_bits( &mut self, clear_vm_bits: ClearVmBits, modification: &mut DatadirModification<'_>, ctx: &RequestContext, ) -> Result<(), WalIngestError> { let ClearVmBits { new_heap_blkno, old_heap_blkno, flags, vm_rel, } = clear_vm_bits; // Clear the VM bits if required. let mut new_vm_blk = new_heap_blkno.map(pg_constants::HEAPBLK_TO_MAPBLOCK); let mut old_vm_blk = old_heap_blkno.map(pg_constants::HEAPBLK_TO_MAPBLOCK); // VM bits can only be cleared on the shard(s) owning the VM relation, and must be within // its view of the VM relation size. Out of caution, error instead of failing WAL ingestion, // as there has historically been cases where PostgreSQL has cleared spurious VM pages. See: // https://github.com/neondatabase/neon/pull/10634. let Some(vm_size) = get_relsize(modification, vm_rel, ctx).await? else { critical_timeline!( modification.tline.tenant_shard_id, modification.tline.timeline_id, // Hadron: No need to raise the corruption flag here; the caller of `ingest_record()` will do it. None::<&AtomicBool>, "clear_vm_bits for unknown VM relation {vm_rel}" ); return Ok(()); }; if let Some(blknum) = new_vm_blk { if blknum >= vm_size { critical_timeline!( modification.tline.tenant_shard_id, modification.tline.timeline_id, // Hadron: No need to raise the corruption flag here; the caller of `ingest_record()` will do it. None::<&AtomicBool>, "new_vm_blk {blknum} not in {vm_rel} of size {vm_size}" ); new_vm_blk = None; } } if let Some(blknum) = old_vm_blk { if blknum >= vm_size { critical_timeline!( modification.tline.tenant_shard_id, modification.tline.timeline_id, // Hadron: No need to raise the corruption flag here; the caller of `ingest_record()` will do it. None::<&AtomicBool>, "old_vm_blk {blknum} not in {vm_rel} of size {vm_size}" ); old_vm_blk = None; } } if new_vm_blk.is_none() && old_vm_blk.is_none() { return Ok(()); } else if new_vm_blk == old_vm_blk { // An UPDATE record that needs to clear the bits for both old and the new page, both of // which reside on the same VM page. self.put_rel_wal_record( modification, vm_rel, new_vm_blk.unwrap(), NeonWalRecord::ClearVisibilityMapFlags { new_heap_blkno, old_heap_blkno, flags, }, ctx, ) .await?; } else { // Clear VM bits for one heap page, or for two pages that reside on different VM pages. if let Some(new_vm_blk) = new_vm_blk { self.put_rel_wal_record( modification, vm_rel, new_vm_blk, NeonWalRecord::ClearVisibilityMapFlags { new_heap_blkno, old_heap_blkno: None, flags, }, ctx, ) .await?; } if let Some(old_vm_blk) = old_vm_blk { self.put_rel_wal_record( modification, vm_rel, old_vm_blk, NeonWalRecord::ClearVisibilityMapFlags { new_heap_blkno: None, old_heap_blkno, flags, }, ctx, ) .await?; } } Ok(()) } /// Subroutine of ingest_record(), to handle an XLOG_DBASE_CREATE record. async fn ingest_xlog_dbase_create( &mut self, create: DbaseCreate, modification: &mut DatadirModification<'_>, ctx: &RequestContext, ) -> Result<(), WalIngestError> { let DbaseCreate { db_id, tablespace_id, src_db_id, src_tablespace_id, } = create; let rels = modification .tline .list_rels( src_tablespace_id, src_db_id, Version::Modified(modification), ctx, ) .await?; debug!("ingest_xlog_dbase_create: {} rels", rels.len()); // Copy relfilemap let filemap = modification .tline .get_relmap_file( src_tablespace_id, src_db_id, Version::Modified(modification), ctx, ) .await?; modification .put_relmap_file(tablespace_id, db_id, filemap, ctx) .await?; let mut num_rels_copied = 0; let mut num_blocks_copied = 0; for src_rel in rels { assert_eq!(src_rel.spcnode, src_tablespace_id); assert_eq!(src_rel.dbnode, src_db_id); let nblocks = modification .tline .get_rel_size(src_rel, Version::Modified(modification), ctx) .await?; let dst_rel = RelTag { spcnode: tablespace_id, dbnode: db_id, relnode: src_rel.relnode, forknum: src_rel.forknum, }; modification.put_rel_creation(dst_rel, nblocks, ctx).await?; // Copy content debug!("copying rel {} to {}, {} blocks", src_rel, dst_rel, nblocks); for blknum in 0..nblocks { // Sharding: // - src and dst are always on the same shard, because they differ only by dbNode, and // dbNode is not included in the hash inputs for sharding. // - This WAL command is replayed on all shards, but each shard only copies the blocks // that belong to it. let src_key = rel_block_to_key(src_rel, blknum); if !self.shard.is_key_local(&src_key) { debug!( "Skipping non-local key {} during XLOG_DBASE_CREATE", src_key ); continue; } debug!( "copying block {} from {} ({}) to {}", blknum, src_rel, src_key, dst_rel ); let content = modification .tline .get_rel_page_at_lsn( src_rel, blknum, Version::Modified(modification), ctx, crate::tenant::storage_layer::IoConcurrency::sequential(), ) .await?; modification.put_rel_page_image(dst_rel, blknum, content)?; num_blocks_copied += 1; } num_rels_copied += 1; } info!( "Created database {}/{}, copied {} blocks in {} rels", tablespace_id, db_id, num_blocks_copied, num_rels_copied ); Ok(()) } async fn ingest_xlog_dbase_drop( &mut self, dbase_drop: DbaseDrop, modification: &mut DatadirModification<'_>, ctx: &RequestContext, ) -> Result<(), WalIngestError> { let DbaseDrop { db_id, tablespace_ids, } = dbase_drop; for tablespace_id in tablespace_ids { trace!("Drop db {}, {}", tablespace_id, db_id); modification.drop_dbdir(tablespace_id, db_id, ctx).await?; } Ok(()) } async fn ingest_xlog_smgr_create( &mut self, create: SmgrCreate, modification: &mut DatadirModification<'_>, ctx: &RequestContext, ) -> Result<(), WalIngestError> { let SmgrCreate { rel } = create; self.put_rel_creation(modification, rel, ctx).await?; Ok(()) } /// Subroutine of ingest_record(), to handle an XLOG_SMGR_TRUNCATE record. /// /// This is the same logic as in PostgreSQL's smgr_redo() function. async fn ingest_xlog_smgr_truncate( &mut self, truncate: XlSmgrTruncate, modification: &mut DatadirModification<'_>, ctx: &RequestContext, ) -> Result<(), WalIngestError> { let XlSmgrTruncate { blkno, rnode, flags, } = truncate; let spcnode = rnode.spcnode; let dbnode = rnode.dbnode; let relnode = rnode.relnode; if flags & pg_constants::SMGR_TRUNCATE_HEAP != 0 { let rel = RelTag { spcnode, dbnode, relnode, forknum: MAIN_FORKNUM, }; self.put_rel_truncation(modification, rel, blkno, ctx) .await?; } if flags & pg_constants::SMGR_TRUNCATE_FSM != 0 { let rel = RelTag { spcnode, dbnode, relnode, forknum: FSM_FORKNUM, }; // Zero out the last remaining FSM page, if this shard owns it. We are not precise here, // and instead of digging in the FSM bitmap format we just clear the whole page. let fsm_logical_page_no = blkno / pg_constants::SLOTS_PER_FSM_PAGE; let mut fsm_physical_page_no = fsm_logical_to_physical(fsm_logical_page_no); if blkno % pg_constants::SLOTS_PER_FSM_PAGE != 0 && self .shard .is_key_local(&rel_block_to_key(rel, fsm_physical_page_no)) { modification.put_rel_page_image_zero(rel, fsm_physical_page_no)?; fsm_physical_page_no += 1; } // Truncate this shard's view of the FSM relation size, if it even has one. let nblocks = get_relsize(modification, rel, ctx).await?.unwrap_or(0); if nblocks > fsm_physical_page_no { self.put_rel_truncation(modification, rel, fsm_physical_page_no, ctx) .await?; } } if flags & pg_constants::SMGR_TRUNCATE_VM != 0 { let rel = RelTag { spcnode, dbnode, relnode, forknum: VISIBILITYMAP_FORKNUM, }; // last remaining block, byte, and bit let mut vm_page_no = blkno / (pg_constants::VM_HEAPBLOCKS_PER_PAGE as u32); let trunc_byte = blkno as usize % pg_constants::VM_HEAPBLOCKS_PER_PAGE / pg_constants::VM_HEAPBLOCKS_PER_BYTE; let trunc_offs = blkno as usize % pg_constants::VM_HEAPBLOCKS_PER_BYTE * pg_constants::VM_BITS_PER_HEAPBLOCK; // Unless the new size is exactly at a visibility map page boundary, the // tail bits in the last remaining map page, representing truncated heap // blocks, need to be cleared. This is not only tidy, but also necessary // because we don't get a chance to clear the bits if the heap is extended // again. Only do this on the shard that owns the page. if (trunc_byte != 0 || trunc_offs != 0) && self.shard.is_key_local(&rel_block_to_key(rel, vm_page_no)) { modification.put_rel_wal_record( rel, vm_page_no, NeonWalRecord::TruncateVisibilityMap { trunc_byte, trunc_offs, }, )?; vm_page_no += 1; } // Truncate this shard's view of the VM relation size, if it even has one. let nblocks = get_relsize(modification, rel, ctx).await?.unwrap_or(0); if nblocks > vm_page_no { self.put_rel_truncation(modification, rel, vm_page_no, ctx) .await?; } } Ok(()) } fn warn_on_ingest_lag( &mut self, conf: &crate::config::PageServerConf, wal_timestamp: TimestampTz, ) { debug_assert_current_span_has_tenant_and_timeline_id(); let now = SystemTime::now(); let rate_limits = &mut self.warn_ingest_lag; let ts = enum_pgversion_dispatch!(&self.checkpoint, CheckPoint, _cp, { pgv::xlog_utils::try_from_pg_timestamp(wal_timestamp) }); match ts { Ok(ts) => { match now.duration_since(ts) { Ok(lag) => { if lag > conf.wait_lsn_timeout { rate_limits.lag_msg_ratelimit.call2(|rate_limit_stats| { if let Some(cooldown) = self.attach_wal_lag_cooldown.get() { if std::time::Instant::now() < cooldown.active_until && lag <= cooldown.max_lag { return; } } else { // Still loading? We shouldn't be here } let lag = humantime::format_duration(lag); warn!(%rate_limit_stats, %lag, "ingesting record with timestamp lagging more than wait_lsn_timeout"); }) } } Err(e) => { let delta_t = e.duration(); // determined by prod victoriametrics query: 1000 * (timestamp(node_time_seconds{neon_service="pageserver"}) - node_time_seconds) // => https://www.robustperception.io/time-metric-from-the-node-exporter/ const IGNORED_DRIFT: Duration = Duration::from_millis(100); if delta_t > IGNORED_DRIFT { let delta_t = humantime::format_duration(delta_t); rate_limits.future_lsn_msg_ratelimit.call2(|rate_limit_stats| { warn!(%rate_limit_stats, %delta_t, "ingesting record with timestamp from future"); }) } } }; } Err(error) => { rate_limits.timestamp_invalid_msg_ratelimit.call2(|rate_limit_stats| { warn!(%rate_limit_stats, %error, "ingesting record with invalid timestamp, cannot calculate lag and will fail find-lsn-for-timestamp type queries"); }) } } } /// Subroutine of ingest_record(), to handle an XLOG_XACT_* records. /// async fn ingest_xact_record( &mut self, record: XactRecord, modification: &mut DatadirModification<'_>, ctx: &RequestContext, ) -> Result<(), WalIngestError> { let (xact_common, is_commit, is_prepared) = match record { XactRecord::Prepare(XactPrepare { xl_xid, data }) => { let xid: u64 = if modification.tline.pg_version >= PgMajorVersion::PG17 { self.adjust_to_full_transaction_id(xl_xid)? } else { xl_xid as u64 }; return modification.put_twophase_file(xid, data, ctx).await; } XactRecord::Commit(common) => (common, true, false), XactRecord::Abort(common) => (common, false, false), XactRecord::CommitPrepared(common) => (common, true, true), XactRecord::AbortPrepared(common) => (common, false, true), }; let XactCommon { parsed, origin_id, xl_xid, lsn, } = xact_common; // Record update of CLOG pages let mut pageno = parsed.xid / pg_constants::CLOG_XACTS_PER_PAGE; let mut segno = pageno / pg_constants::SLRU_PAGES_PER_SEGMENT; let mut rpageno = pageno % pg_constants::SLRU_PAGES_PER_SEGMENT; let mut page_xids: Vec<TransactionId> = vec![parsed.xid]; self.warn_on_ingest_lag(modification.tline.conf, parsed.xact_time); for subxact in &parsed.subxacts { let subxact_pageno = subxact / pg_constants::CLOG_XACTS_PER_PAGE; if subxact_pageno != pageno { // This subxact goes to different page. Write the record // for all the XIDs on the previous page, and continue // accumulating XIDs on this new page. modification.put_slru_wal_record( SlruKind::Clog, segno, rpageno, if is_commit {
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
true
neondatabase/neon
https://github.com/neondatabase/neon/blob/015b1c7cb3259a6fcd5039bc2bd46a462e163ae8/pageserver/src/import_datadir.rs
pageserver/src/import_datadir.rs
//! //! Import data and WAL from a PostgreSQL data directory and WAL segments into //! a neon Timeline. //! use std::path::{Path, PathBuf}; use anyhow::{Context, Result, bail, ensure}; use bytes::Bytes; use camino::Utf8Path; use futures::StreamExt; use pageserver_api::key::rel_block_to_key; use pageserver_api::reltag::{RelTag, SlruKind}; use postgres_ffi::relfile_utils::*; use postgres_ffi::waldecoder::WalStreamDecoder; use postgres_ffi::{ BLCKSZ, ControlFileData, DBState_DB_SHUTDOWNED, Oid, WAL_SEGMENT_SIZE, XLogFileName, pg_constants, }; use tokio::io::{AsyncRead, AsyncReadExt}; use tokio_tar::Archive; use tracing::*; use utils::lsn::Lsn; use wal_decoder::models::InterpretedWalRecord; use walkdir::WalkDir; use crate::context::RequestContext; use crate::metrics::WAL_INGEST; use crate::pgdatadir_mapping::*; use crate::tenant::Timeline; use crate::walingest::{WalIngest, WalIngestErrorKind}; // Returns checkpoint LSN from controlfile pub fn get_lsn_from_controlfile(path: &Utf8Path) -> Result<Lsn> { // Read control file to extract the LSN let controlfile_path = path.join("global").join("pg_control"); let controlfile_buf = std::fs::read(&controlfile_path) .with_context(|| format!("reading controlfile: {controlfile_path}"))?; let controlfile = ControlFileData::decode(&controlfile_buf)?; let lsn = controlfile.checkPoint; Ok(Lsn(lsn)) } /// /// Import all relation data pages from local disk into the repository. /// /// This is currently only used to import a cluster freshly created by initdb. /// The code that deals with the checkpoint would not work right if the /// cluster was not shut down cleanly. pub async fn import_timeline_from_postgres_datadir( tline: &Timeline, pgdata_path: &Utf8Path, pgdata_lsn: Lsn, ctx: &RequestContext, ) -> Result<()> { let mut pg_control: Option<ControlFileData> = None; // TODO this shoud be start_lsn, which is not necessarily equal to end_lsn (aka lsn) // Then fishing out pg_control would be unnecessary let mut modification = tline.begin_modification_for_import(pgdata_lsn); modification.init_empty()?; // Import all but pg_wal let all_but_wal = WalkDir::new(pgdata_path) .into_iter() .filter_entry(|entry| !entry.path().ends_with("pg_wal")); for entry in all_but_wal { let entry = entry?; let metadata = entry.metadata().expect("error getting dir entry metadata"); if metadata.is_file() { let absolute_path = entry.path(); let relative_path = absolute_path.strip_prefix(pgdata_path)?; let mut file = tokio::fs::File::open(absolute_path).await?; let len = metadata.len() as usize; if let Some(control_file) = import_file(&mut modification, relative_path, &mut file, len, ctx).await? { pg_control = Some(control_file); } modification.flush(ctx).await?; } } // We're done importing all the data files. modification.commit(ctx).await?; // We expect the Postgres server to be shut down cleanly. let pg_control = pg_control.context("pg_control file not found")?; ensure!( pg_control.state == DBState_DB_SHUTDOWNED, "Postgres cluster was not shut down cleanly" ); ensure!( pg_control.checkPointCopy.redo == pgdata_lsn.0, "unexpected checkpoint REDO pointer" ); // Import WAL. This is needed even when starting from a shutdown checkpoint, because // this reads the checkpoint record itself, advancing the tip of the timeline to // *after* the checkpoint record. And crucially, it initializes the 'prev_lsn'. import_wal( &pgdata_path.join("pg_wal"), tline, Lsn(pg_control.checkPointCopy.redo), pgdata_lsn, ctx, ) .await?; Ok(()) } // subroutine of import_timeline_from_postgres_datadir(), to load one relation file. async fn import_rel( modification: &mut DatadirModification<'_>, path: &Path, spcoid: Oid, dboid: Oid, reader: &mut (impl AsyncRead + Unpin), len: usize, ctx: &RequestContext, ) -> anyhow::Result<()> { // Does it look like a relation file? trace!("importing rel file {}", path.display()); let filename = &path .file_name() .expect("missing rel filename") .to_string_lossy(); let (relnode, forknum, segno) = parse_relfilename(filename).map_err(|e| { warn!("unrecognized file in postgres datadir: {:?} ({})", path, e); e })?; let mut buf: [u8; 8192] = [0u8; 8192]; ensure!(len % BLCKSZ as usize == 0); let nblocks = len / BLCKSZ as usize; let rel = RelTag { spcnode: spcoid, dbnode: dboid, relnode, forknum, }; let mut blknum: u32 = segno * (1024 * 1024 * 1024 / BLCKSZ as u32); // Call put_rel_creation for every segment of the relation, // because there is no guarantee about the order in which we are processing segments. // ignore "relation already exists" error // // FIXME: Keep track of which relations we've already created? // https://github.com/neondatabase/neon/issues/3309 if let Err(e) = modification .put_rel_creation(rel, nblocks as u32, ctx) .await { match e.kind { WalIngestErrorKind::RelationAlreadyExists(rel) => { debug!("Relation {rel} already exists. We must be extending it.") } _ => return Err(e.into()), } } loop { let r = reader.read_exact(&mut buf).await; match r { Ok(_) => { let key = rel_block_to_key(rel, blknum); if modification.tline.get_shard_identity().is_key_local(&key) { modification.put_rel_page_image(rel, blknum, Bytes::copy_from_slice(&buf))?; } } // TODO: UnexpectedEof is expected Err(err) => match err.kind() { std::io::ErrorKind::UnexpectedEof => { // reached EOF. That's expected. let relative_blknum = blknum - segno * (1024 * 1024 * 1024 / BLCKSZ as u32); ensure!(relative_blknum == nblocks as u32, "unexpected EOF"); break; } _ => { bail!("error reading file {}: {:#}", path.display(), err); } }, }; blknum += 1; } // Update relation size // // If we process rel segments out of order, // put_rel_extend will skip the update. modification.put_rel_extend(rel, blknum, ctx).await?; Ok(()) } /// Import an SLRU segment file /// async fn import_slru( modification: &mut DatadirModification<'_>, slru: SlruKind, path: &Path, reader: &mut (impl AsyncRead + Unpin), len: usize, ctx: &RequestContext, ) -> anyhow::Result<()> { info!("importing slru file {path:?}"); let mut buf: [u8; 8192] = [0u8; 8192]; let filename = &path .file_name() .with_context(|| format!("missing slru filename for path {path:?}"))? .to_string_lossy(); let segno = u32::from_str_radix(filename, 16)?; ensure!(len % BLCKSZ as usize == 0); // we assume SLRU block size is the same as BLCKSZ let nblocks = len / BLCKSZ as usize; ensure!(nblocks <= pg_constants::SLRU_PAGES_PER_SEGMENT as usize); modification .put_slru_segment_creation(slru, segno, nblocks as u32, ctx) .await?; let mut rpageno = 0; loop { let r = reader.read_exact(&mut buf).await; match r { Ok(_) => { modification.put_slru_page_image( slru, segno, rpageno, Bytes::copy_from_slice(&buf), )?; } // TODO: UnexpectedEof is expected Err(err) => match err.kind() { std::io::ErrorKind::UnexpectedEof => { // reached EOF. That's expected. ensure!(rpageno == nblocks as u32, "unexpected EOF"); break; } _ => { bail!("error reading file {}: {:#}", path.display(), err); } }, }; rpageno += 1; } Ok(()) } /// Scan PostgreSQL WAL files in given directory and load all records between /// 'startpoint' and 'endpoint' into the repository. async fn import_wal( walpath: &Utf8Path, tline: &Timeline, startpoint: Lsn, endpoint: Lsn, ctx: &RequestContext, ) -> anyhow::Result<()> { let mut waldecoder = WalStreamDecoder::new(startpoint, tline.pg_version); let mut segno = startpoint.segment_number(WAL_SEGMENT_SIZE); let mut offset = startpoint.segment_offset(WAL_SEGMENT_SIZE); let mut last_lsn = startpoint; let mut walingest = WalIngest::new(tline, startpoint, ctx).await?; let shard = vec![*tline.get_shard_identity()]; while last_lsn <= endpoint { // FIXME: assume postgresql tli 1 for now let filename = XLogFileName(1, segno, WAL_SEGMENT_SIZE); let mut buf = Vec::new(); // Read local file let mut path = walpath.join(&filename); // It could be as .partial if !PathBuf::from(&path).exists() { path = walpath.join(filename + ".partial"); } // Slurp the WAL file let mut file = std::fs::File::open(&path)?; if offset > 0 { use std::io::Seek; file.seek(std::io::SeekFrom::Start(offset as u64))?; } use std::io::Read; let nread = file.read_to_end(&mut buf)?; if nread != WAL_SEGMENT_SIZE - offset { // Maybe allow this for .partial files? error!("read only {} bytes from WAL file", nread); } waldecoder.feed_bytes(&buf); let mut nrecords = 0; let mut modification = tline.begin_modification_for_import(last_lsn); while last_lsn <= endpoint { if let Some((lsn, recdata)) = waldecoder.poll_decode()? { let interpreted = InterpretedWalRecord::from_bytes_filtered( recdata, &shard, lsn, tline.pg_version, )? .remove(tline.get_shard_identity()) .unwrap(); walingest .ingest_record(interpreted, &mut modification, ctx) .await?; WAL_INGEST.records_committed.inc(); modification.commit(ctx).await?; last_lsn = lsn; nrecords += 1; trace!("imported record at {} (end {})", lsn, endpoint); } } debug!("imported {} records up to {}", nrecords, last_lsn); segno += 1; offset = 0; } if last_lsn != startpoint { info!("reached end of WAL at {}", last_lsn); } else { info!("no WAL to import at {}", last_lsn); } Ok(()) } pub async fn import_basebackup_from_tar( tline: &Timeline, reader: &mut (impl AsyncRead + Send + Sync + Unpin), base_lsn: Lsn, ctx: &RequestContext, ) -> Result<()> { info!("importing base at {base_lsn}"); let mut modification = tline.begin_modification_for_import(base_lsn); modification.init_empty()?; let mut pg_control: Option<ControlFileData> = None; // Import base let mut entries = Archive::new(reader).entries()?; while let Some(base_tar_entry) = entries.next().await { let mut entry = base_tar_entry?; let header = entry.header(); let len = header.entry_size()? as usize; let file_path = header.path()?.into_owned(); match header.entry_type() { tokio_tar::EntryType::Regular => { if let Some(res) = import_file(&mut modification, file_path.as_ref(), &mut entry, len, ctx).await? { // We found the pg_control file. pg_control = Some(res); } modification.flush(ctx).await?; } tokio_tar::EntryType::Directory => { debug!("directory {:?}", file_path); } _ => { bail!( "entry {} in backup tar archive is of unexpected type: {:?}", file_path.display(), header.entry_type() ); } } } // sanity check: ensure that pg_control is loaded let _pg_control = pg_control.context("pg_control file not found")?; modification.commit(ctx).await?; Ok(()) } pub async fn import_wal_from_tar( tline: &Timeline, reader: &mut (impl AsyncRead + Send + Sync + Unpin), start_lsn: Lsn, end_lsn: Lsn, ctx: &RequestContext, ) -> Result<()> { // Set up walingest mutable state let mut waldecoder = WalStreamDecoder::new(start_lsn, tline.pg_version); let mut segno = start_lsn.segment_number(WAL_SEGMENT_SIZE); let mut offset = start_lsn.segment_offset(WAL_SEGMENT_SIZE); let mut last_lsn = start_lsn; let mut walingest = WalIngest::new(tline, start_lsn, ctx).await?; let shard = vec![*tline.get_shard_identity()]; // Ingest wal until end_lsn info!("importing wal until {}", end_lsn); let mut pg_wal_tar = Archive::new(reader); let mut pg_wal_entries = pg_wal_tar.entries()?; while last_lsn <= end_lsn { let bytes = { let mut entry = pg_wal_entries .next() .await .ok_or_else(|| anyhow::anyhow!("expected more wal"))??; let header = entry.header(); let file_path = header.path()?.into_owned(); match header.entry_type() { tokio_tar::EntryType::Regular => { // FIXME: assume postgresql tli 1 for now let expected_filename = XLogFileName(1, segno, WAL_SEGMENT_SIZE); let file_name = file_path .file_name() .expect("missing wal filename") .to_string_lossy(); ensure!(expected_filename == file_name); debug!("processing wal file {:?}", file_path); read_all_bytes(&mut entry).await? } tokio_tar::EntryType::Directory => { debug!("directory {:?}", file_path); continue; } _ => { bail!( "entry {} in WAL tar archive is of unexpected type: {:?}", file_path.display(), header.entry_type() ); } } }; waldecoder.feed_bytes(&bytes[offset..]); let mut modification = tline.begin_modification_for_import(last_lsn); while last_lsn <= end_lsn { if let Some((lsn, recdata)) = waldecoder.poll_decode()? { let interpreted = InterpretedWalRecord::from_bytes_filtered( recdata, &shard, lsn, tline.pg_version, )? .remove(tline.get_shard_identity()) .unwrap(); walingest .ingest_record(interpreted, &mut modification, ctx) .await?; modification.commit(ctx).await?; last_lsn = lsn; debug!("imported record at {} (end {})", lsn, end_lsn); } } debug!("imported records up to {}", last_lsn); segno += 1; offset = 0; } if last_lsn != start_lsn { info!("reached end of WAL at {}", last_lsn); } else { info!("there was no WAL to import at {}", last_lsn); } // Log any extra unused files while let Some(e) = pg_wal_entries.next().await { let entry = e?; let header = entry.header(); let file_path = header.path()?.into_owned(); info!("skipping {:?}", file_path); } Ok(()) } async fn import_file( modification: &mut DatadirModification<'_>, file_path: &Path, reader: &mut (impl AsyncRead + Send + Sync + Unpin), len: usize, ctx: &RequestContext, ) -> Result<Option<ControlFileData>> { let file_name = match file_path.file_name() { Some(name) => name.to_string_lossy(), None => return Ok(None), }; if file_name.starts_with('.') { // tar archives on macOs, created without COPYFILE_DISABLE=1 env var // will contain "fork files", skip them. return Ok(None); } if file_path.starts_with("global") { let spcnode = postgres_ffi_types::constants::GLOBALTABLESPACE_OID; let dbnode = 0; match file_name.as_ref() { "pg_control" => { let bytes = read_all_bytes(reader).await?; // Extract the checkpoint record and import it separately. let pg_control = ControlFileData::decode(&bytes[..])?; let checkpoint_bytes = pg_control.checkPointCopy.encode()?; modification.put_checkpoint(checkpoint_bytes)?; debug!("imported control file"); // Import it as ControlFile modification.put_control_file(bytes)?; return Ok(Some(pg_control)); } "pg_filenode.map" => { let bytes = read_all_bytes(reader).await?; modification .put_relmap_file(spcnode, dbnode, bytes, ctx) .await?; debug!("imported relmap file") } "PG_VERSION" => { debug!("ignored PG_VERSION file"); } _ => { import_rel(modification, file_path, spcnode, dbnode, reader, len, ctx).await?; debug!("imported rel creation"); } } } else if file_path.starts_with("base") { let spcnode = postgres_ffi_types::constants::DEFAULTTABLESPACE_OID; let dbnode: u32 = file_path .iter() .nth(1) .expect("invalid file path, expected dbnode") .to_string_lossy() .parse()?; match file_name.as_ref() { "pg_filenode.map" => { let bytes = read_all_bytes(reader).await?; modification .put_relmap_file(spcnode, dbnode, bytes, ctx) .await?; debug!("imported relmap file") } "PG_VERSION" => { debug!("ignored PG_VERSION file"); } _ => { import_rel(modification, file_path, spcnode, dbnode, reader, len, ctx).await?; debug!("imported rel creation"); } } } else if file_path.starts_with("pg_xact") { let slru = SlruKind::Clog; if modification.tline.tenant_shard_id.is_shard_zero() { import_slru(modification, slru, file_path, reader, len, ctx).await?; debug!("imported clog slru"); } } else if file_path.starts_with("pg_multixact/offsets") { let slru = SlruKind::MultiXactOffsets; if modification.tline.tenant_shard_id.is_shard_zero() { import_slru(modification, slru, file_path, reader, len, ctx).await?; debug!("imported multixact offsets slru"); } } else if file_path.starts_with("pg_multixact/members") { let slru = SlruKind::MultiXactMembers; if modification.tline.tenant_shard_id.is_shard_zero() { import_slru(modification, slru, file_path, reader, len, ctx).await?; debug!("imported multixact members slru"); } } else if file_path.starts_with("pg_twophase") { let bytes = read_all_bytes(reader).await?; // In PostgreSQL v17, this is a 64-bit FullTransactionid. In previous versions, // it's a 32-bit TransactionId, which fits in u64 anyway. let xid = u64::from_str_radix(file_name.as_ref(), 16)?; modification .put_twophase_file(xid, Bytes::copy_from_slice(&bytes[..]), ctx) .await?; debug!("imported twophase file"); } else if file_path.starts_with("pg_wal") { debug!("found wal file in base section. ignore it"); } else if file_path.starts_with("zenith.signal") || file_path.starts_with("neon.signal") { // Parse zenith signal file to set correct previous LSN let bytes = read_all_bytes(reader).await?; // neon.signal format is "PREV LSN: prev_lsn" // TODO write serialization and deserialization in the same place. let neon_signal = std::str::from_utf8(&bytes)?.trim(); let prev_lsn = match neon_signal { "PREV LSN: none" => Lsn(0), "PREV LSN: invalid" => Lsn(0), other => { let split = other.split(':').collect::<Vec<_>>(); split[1] .trim() .parse::<Lsn>() .context("can't parse neon.signal")? } }; // neon.signal is not necessarily the last file, that we handle // but it is ok to call `finish_write()`, because final `modification.commit()` // will update lsn once more to the final one. let writer = modification.tline.writer().await; writer.finish_write(prev_lsn); debug!("imported neon signal {}", prev_lsn); } else if file_path.starts_with("pg_tblspc") { // TODO Backups exported from neon won't have pg_tblspc, but we will need // this to import arbitrary postgres databases. bail!("Importing pg_tblspc is not implemented"); } else { debug!( "ignoring unrecognized file \"{}\" in tar archive", file_path.display() ); } Ok(None) } async fn read_all_bytes(reader: &mut (impl AsyncRead + Unpin)) -> Result<Bytes> { let mut buf: Vec<u8> = vec![]; reader.read_to_end(&mut buf).await?; Ok(Bytes::from(buf)) }
rust
Apache-2.0
015b1c7cb3259a6fcd5039bc2bd46a462e163ae8
2026-01-04T15:40:24.223849Z
false