LCOV - code coverage report
Current view: top level - proxy/src/context - mod.rs (source / functions) Coverage Total Hit
Test: feead26e04cdef6e988ff1765b1cb7075eb48d3d.info Lines: 33.4 % 323 108
Test Date: 2025-02-28 12:11:00 Functions: 26.2 % 42 11

            Line data    Source code
       1              : //! Connection request monitoring contexts
       2              : 
       3              : use std::net::IpAddr;
       4              : 
       5              : use chrono::Utc;
       6              : use once_cell::sync::OnceCell;
       7              : use pq_proto::StartupMessageParams;
       8              : use smol_str::SmolStr;
       9              : use tokio::sync::mpsc;
      10              : use tracing::field::display;
      11              : use tracing::{Span, debug, error, info_span};
      12              : use try_lock::TryLock;
      13              : use uuid::Uuid;
      14              : 
      15              : use self::parquet::RequestData;
      16              : use crate::control_plane::messages::{ColdStartInfo, MetricsAuxInfo};
      17              : use crate::error::ErrorKind;
      18              : use crate::intern::{BranchIdInt, ProjectIdInt};
      19              : use crate::metrics::{
      20              :     ConnectOutcome, InvalidEndpointsGroup, LatencyTimer, Metrics, Protocol, Waiting,
      21              : };
      22              : use crate::protocol2::{ConnectionInfo, ConnectionInfoExtra};
      23              : use crate::types::{DbName, EndpointId, RoleName};
      24              : 
      25              : pub mod parquet;
      26              : 
      27              : pub(crate) static LOG_CHAN: OnceCell<mpsc::WeakUnboundedSender<RequestData>> = OnceCell::new();
      28              : pub(crate) static LOG_CHAN_DISCONNECT: OnceCell<mpsc::WeakUnboundedSender<RequestData>> =
      29              :     OnceCell::new();
      30              : 
      31              : /// Context data for a single request to connect to a database.
      32              : ///
      33              : /// This data should **not** be used for connection logic, only for observability and limiting purposes.
      34              : /// All connection logic should instead use strongly typed state machines, not a bunch of Options.
      35              : pub struct RequestContext(
      36              :     /// To allow easier use of the ctx object, we have interior mutability.
      37              :     /// I would typically use a RefCell but that would break the `Send` requirements
      38              :     /// so we need something with thread-safety. `TryLock` is a cheap alternative
      39              :     /// that offers similar semantics to a `RefCell` but with synchronisation.
      40              :     TryLock<RequestContextInner>,
      41              : );
      42              : 
      43              : struct RequestContextInner {
      44              :     pub(crate) conn_info: ConnectionInfo,
      45              :     pub(crate) session_id: Uuid,
      46              :     pub(crate) protocol: Protocol,
      47              :     first_packet: chrono::DateTime<Utc>,
      48              :     region: &'static str,
      49              :     pub(crate) span: Span,
      50              : 
      51              :     // filled in as they are discovered
      52              :     project: Option<ProjectIdInt>,
      53              :     branch: Option<BranchIdInt>,
      54              :     endpoint_id: Option<EndpointId>,
      55              :     dbname: Option<DbName>,
      56              :     user: Option<RoleName>,
      57              :     application: Option<SmolStr>,
      58              :     user_agent: Option<SmolStr>,
      59              :     error_kind: Option<ErrorKind>,
      60              :     pub(crate) auth_method: Option<AuthMethod>,
      61              :     jwt_issuer: Option<String>,
      62              :     success: bool,
      63              :     pub(crate) cold_start_info: ColdStartInfo,
      64              :     pg_options: Option<StartupMessageParams>,
      65              : 
      66              :     // extra
      67              :     // This sender is here to keep the request monitoring channel open while requests are taking place.
      68              :     sender: Option<mpsc::UnboundedSender<RequestData>>,
      69              :     // This sender is only used to log the length of session in case of success.
      70              :     disconnect_sender: Option<mpsc::UnboundedSender<RequestData>>,
      71              :     pub(crate) latency_timer: LatencyTimer,
      72              :     // Whether proxy decided that it's not a valid endpoint end rejected it before going to cplane.
      73              :     rejected: Option<bool>,
      74              :     disconnect_timestamp: Option<chrono::DateTime<Utc>>,
      75              : }
      76              : 
      77              : #[derive(Clone, Debug)]
      78              : pub(crate) enum AuthMethod {
      79              :     // aka passwordless, fka link
      80              :     ConsoleRedirect,
      81              :     ScramSha256,
      82              :     ScramSha256Plus,
      83              :     Cleartext,
      84              :     Jwt,
      85              : }
      86              : 
      87              : impl Clone for RequestContext {
      88            0 :     fn clone(&self) -> Self {
      89            0 :         let inner = self.0.try_lock().expect("should not deadlock");
      90            0 :         let new = RequestContextInner {
      91            0 :             conn_info: inner.conn_info.clone(),
      92            0 :             session_id: inner.session_id,
      93            0 :             protocol: inner.protocol,
      94            0 :             first_packet: inner.first_packet,
      95            0 :             region: inner.region,
      96            0 :             span: info_span!("background_task"),
      97              : 
      98            0 :             project: inner.project,
      99            0 :             branch: inner.branch,
     100            0 :             endpoint_id: inner.endpoint_id.clone(),
     101            0 :             dbname: inner.dbname.clone(),
     102            0 :             user: inner.user.clone(),
     103            0 :             application: inner.application.clone(),
     104            0 :             user_agent: inner.user_agent.clone(),
     105            0 :             error_kind: inner.error_kind,
     106            0 :             auth_method: inner.auth_method.clone(),
     107            0 :             jwt_issuer: inner.jwt_issuer.clone(),
     108            0 :             success: inner.success,
     109            0 :             rejected: inner.rejected,
     110            0 :             cold_start_info: inner.cold_start_info,
     111            0 :             pg_options: inner.pg_options.clone(),
     112            0 : 
     113            0 :             sender: None,
     114            0 :             disconnect_sender: None,
     115            0 :             latency_timer: LatencyTimer::noop(inner.protocol),
     116            0 :             disconnect_timestamp: inner.disconnect_timestamp,
     117            0 :         };
     118            0 : 
     119            0 :         Self(TryLock::new(new))
     120            0 :     }
     121              : }
     122              : 
     123              : impl RequestContext {
     124           70 :     pub fn new(
     125           70 :         session_id: Uuid,
     126           70 :         conn_info: ConnectionInfo,
     127           70 :         protocol: Protocol,
     128           70 :         region: &'static str,
     129           70 :     ) -> Self {
     130              :         // TODO: be careful with long lived spans
     131           70 :         let span = info_span!(
     132           70 :             "connect_request",
     133           70 :             %protocol,
     134           70 :             ?session_id,
     135           70 :             %conn_info,
     136           70 :             ep = tracing::field::Empty,
     137           70 :             role = tracing::field::Empty,
     138           70 :         );
     139              : 
     140           70 :         let inner = RequestContextInner {
     141           70 :             conn_info,
     142           70 :             session_id,
     143           70 :             protocol,
     144           70 :             first_packet: Utc::now(),
     145           70 :             region,
     146           70 :             span,
     147           70 : 
     148           70 :             project: None,
     149           70 :             branch: None,
     150           70 :             endpoint_id: None,
     151           70 :             dbname: None,
     152           70 :             user: None,
     153           70 :             application: None,
     154           70 :             user_agent: None,
     155           70 :             error_kind: None,
     156           70 :             auth_method: None,
     157           70 :             jwt_issuer: None,
     158           70 :             success: false,
     159           70 :             rejected: None,
     160           70 :             cold_start_info: ColdStartInfo::Unknown,
     161           70 :             pg_options: None,
     162           70 : 
     163           70 :             sender: LOG_CHAN.get().and_then(|tx| tx.upgrade()),
     164           70 :             disconnect_sender: LOG_CHAN_DISCONNECT.get().and_then(|tx| tx.upgrade()),
     165           70 :             latency_timer: LatencyTimer::new(protocol),
     166           70 :             disconnect_timestamp: None,
     167           70 :         };
     168           70 : 
     169           70 :         Self(TryLock::new(inner))
     170           70 :     }
     171              : 
     172              :     #[cfg(test)]
     173           70 :     pub(crate) fn test() -> Self {
     174              :         use std::net::SocketAddr;
     175           70 :         let ip = IpAddr::from([127, 0, 0, 1]);
     176           70 :         let addr = SocketAddr::new(ip, 5432);
     177           70 :         let conn_info = ConnectionInfo { addr, extra: None };
     178           70 :         RequestContext::new(Uuid::now_v7(), conn_info, Protocol::Tcp, "test")
     179           70 :     }
     180              : 
     181            0 :     pub(crate) fn console_application_name(&self) -> String {
     182            0 :         let this = self.0.try_lock().expect("should not deadlock");
     183            0 :         format!(
     184            0 :             "{}/{}",
     185            0 :             this.application.as_deref().unwrap_or_default(),
     186            0 :             this.protocol
     187            0 :         )
     188            0 :     }
     189              : 
     190            0 :     pub(crate) fn set_rejected(&self, rejected: bool) {
     191            0 :         let mut this = self.0.try_lock().expect("should not deadlock");
     192            0 :         this.rejected = Some(rejected);
     193            0 :     }
     194              : 
     195            0 :     pub(crate) fn set_cold_start_info(&self, info: ColdStartInfo) {
     196            0 :         self.0
     197            0 :             .try_lock()
     198            0 :             .expect("should not deadlock")
     199            0 :             .set_cold_start_info(info);
     200            0 :     }
     201              : 
     202            0 :     pub(crate) fn set_db_options(&self, options: StartupMessageParams) {
     203            0 :         let mut this = self.0.try_lock().expect("should not deadlock");
     204            0 :         this.set_application(options.get("application_name").map(SmolStr::from));
     205            0 :         if let Some(user) = options.get("user") {
     206            0 :             this.set_user(user.into());
     207            0 :         }
     208            0 :         if let Some(dbname) = options.get("database") {
     209            0 :             this.set_dbname(dbname.into());
     210            0 :         }
     211              : 
     212            0 :         this.pg_options = Some(options);
     213            0 :     }
     214              : 
     215            0 :     pub(crate) fn set_project(&self, x: MetricsAuxInfo) {
     216            0 :         let mut this = self.0.try_lock().expect("should not deadlock");
     217            0 :         if this.endpoint_id.is_none() {
     218            0 :             this.set_endpoint_id(x.endpoint_id.as_str().into());
     219            0 :         }
     220            0 :         this.branch = Some(x.branch_id);
     221            0 :         this.project = Some(x.project_id);
     222            0 :         this.set_cold_start_info(x.cold_start_info);
     223            0 :     }
     224              : 
     225            0 :     pub(crate) fn set_project_id(&self, project_id: ProjectIdInt) {
     226            0 :         let mut this = self.0.try_lock().expect("should not deadlock");
     227            0 :         this.project = Some(project_id);
     228            0 :     }
     229              : 
     230           28 :     pub(crate) fn set_endpoint_id(&self, endpoint_id: EndpointId) {
     231           28 :         self.0
     232           28 :             .try_lock()
     233           28 :             .expect("should not deadlock")
     234           28 :             .set_endpoint_id(endpoint_id);
     235           28 :     }
     236              : 
     237            0 :     pub(crate) fn set_dbname(&self, dbname: DbName) {
     238            0 :         self.0
     239            0 :             .try_lock()
     240            0 :             .expect("should not deadlock")
     241            0 :             .set_dbname(dbname);
     242            0 :     }
     243              : 
     244            0 :     pub(crate) fn set_user(&self, user: RoleName) {
     245            0 :         self.0
     246            0 :             .try_lock()
     247            0 :             .expect("should not deadlock")
     248            0 :             .set_user(user);
     249            0 :     }
     250              : 
     251            0 :     pub(crate) fn set_user_agent(&self, user_agent: Option<SmolStr>) {
     252            0 :         self.0
     253            0 :             .try_lock()
     254            0 :             .expect("should not deadlock")
     255            0 :             .set_user_agent(user_agent);
     256            0 :     }
     257              : 
     258           15 :     pub(crate) fn set_auth_method(&self, auth_method: AuthMethod) {
     259           15 :         let mut this = self.0.try_lock().expect("should not deadlock");
     260           15 :         this.auth_method = Some(auth_method);
     261           15 :     }
     262              : 
     263           12 :     pub(crate) fn set_jwt_issuer(&self, jwt_issuer: String) {
     264           12 :         let mut this = self.0.try_lock().expect("should not deadlock");
     265           12 :         this.jwt_issuer = Some(jwt_issuer);
     266           12 :     }
     267              : 
     268            0 :     pub fn has_private_peer_addr(&self) -> bool {
     269            0 :         self.0
     270            0 :             .try_lock()
     271            0 :             .expect("should not deadlock")
     272            0 :             .has_private_peer_addr()
     273            0 :     }
     274              : 
     275            0 :     pub(crate) fn set_error_kind(&self, kind: ErrorKind) {
     276            0 :         let mut this = self.0.try_lock().expect("should not deadlock");
     277            0 :         // Do not record errors from the private address to metrics.
     278            0 :         if !this.has_private_peer_addr() {
     279            0 :             Metrics::get().proxy.errors_total.inc(kind);
     280            0 :         }
     281            0 :         if let Some(ep) = &this.endpoint_id {
     282            0 :             let metric = &Metrics::get().proxy.endpoints_affected_by_errors;
     283            0 :             let label = metric.with_labels(kind);
     284            0 :             metric.get_metric(label).measure(ep);
     285            0 :         }
     286            0 :         this.error_kind = Some(kind);
     287            0 :     }
     288              : 
     289            0 :     pub fn set_success(&self) {
     290            0 :         let mut this = self.0.try_lock().expect("should not deadlock");
     291            0 :         this.success = true;
     292            0 :     }
     293              : 
     294            0 :     pub fn log_connect(self) -> DisconnectLogger {
     295            0 :         let mut this = self.0.into_inner();
     296            0 :         this.log_connect();
     297            0 : 
     298            0 :         // close current span.
     299            0 :         this.span = Span::none();
     300            0 : 
     301            0 :         DisconnectLogger(this)
     302            0 :     }
     303              : 
     304            0 :     pub(crate) fn protocol(&self) -> Protocol {
     305            0 :         self.0.try_lock().expect("should not deadlock").protocol
     306            0 :     }
     307              : 
     308            0 :     pub(crate) fn span(&self) -> Span {
     309            0 :         self.0.try_lock().expect("should not deadlock").span.clone()
     310            0 :     }
     311              : 
     312            0 :     pub(crate) fn session_id(&self) -> Uuid {
     313            0 :         self.0.try_lock().expect("should not deadlock").session_id
     314            0 :     }
     315              : 
     316            6 :     pub(crate) fn peer_addr(&self) -> IpAddr {
     317            6 :         self.0
     318            6 :             .try_lock()
     319            6 :             .expect("should not deadlock")
     320            6 :             .conn_info
     321            6 :             .addr
     322            6 :             .ip()
     323            6 :     }
     324              : 
     325            0 :     pub(crate) fn extra(&self) -> Option<ConnectionInfoExtra> {
     326            0 :         self.0
     327            0 :             .try_lock()
     328            0 :             .expect("should not deadlock")
     329            0 :             .conn_info
     330            0 :             .extra
     331            0 :             .clone()
     332            0 :     }
     333              : 
     334            0 :     pub(crate) fn cold_start_info(&self) -> ColdStartInfo {
     335            0 :         self.0
     336            0 :             .try_lock()
     337            0 :             .expect("should not deadlock")
     338            0 :             .cold_start_info
     339            0 :     }
     340              : 
     341           28 :     pub(crate) fn latency_timer_pause(&self, waiting_for: Waiting) -> LatencyTimerPause<'_> {
     342           28 :         LatencyTimerPause {
     343           28 :             ctx: self,
     344           28 :             start: tokio::time::Instant::now(),
     345           28 :             waiting_for,
     346           28 :         }
     347           28 :     }
     348              : 
     349            4 :     pub(crate) fn success(&self) {
     350            4 :         self.0
     351            4 :             .try_lock()
     352            4 :             .expect("should not deadlock")
     353            4 :             .latency_timer
     354            4 :             .success();
     355            4 :     }
     356              : }
     357              : 
     358              : pub(crate) struct LatencyTimerPause<'a> {
     359              :     ctx: &'a RequestContext,
     360              :     start: tokio::time::Instant,
     361              :     waiting_for: Waiting,
     362              : }
     363              : 
     364              : impl Drop for LatencyTimerPause<'_> {
     365           28 :     fn drop(&mut self) {
     366           28 :         self.ctx
     367           28 :             .0
     368           28 :             .try_lock()
     369           28 :             .expect("should not deadlock")
     370           28 :             .latency_timer
     371           28 :             .unpause(self.start, self.waiting_for);
     372           28 :     }
     373              : }
     374              : 
     375              : impl RequestContextInner {
     376            0 :     fn set_cold_start_info(&mut self, info: ColdStartInfo) {
     377            0 :         self.cold_start_info = info;
     378            0 :         self.latency_timer.cold_start_info(info);
     379            0 :     }
     380              : 
     381           28 :     fn set_endpoint_id(&mut self, endpoint_id: EndpointId) {
     382           28 :         if self.endpoint_id.is_none() {
     383           28 :             self.span.record("ep", display(&endpoint_id));
     384           28 :             let metric = &Metrics::get().proxy.connecting_endpoints;
     385           28 :             let label = metric.with_labels(self.protocol);
     386           28 :             metric.get_metric(label).measure(&endpoint_id);
     387           28 :             self.endpoint_id = Some(endpoint_id);
     388           28 :         }
     389           28 :     }
     390              : 
     391            0 :     fn set_application(&mut self, app: Option<SmolStr>) {
     392            0 :         if let Some(app) = app {
     393            0 :             self.application = Some(app);
     394            0 :         }
     395            0 :     }
     396              : 
     397            0 :     fn set_user_agent(&mut self, user_agent: Option<SmolStr>) {
     398            0 :         self.user_agent = user_agent;
     399            0 :     }
     400              : 
     401            0 :     fn set_dbname(&mut self, dbname: DbName) {
     402            0 :         self.dbname = Some(dbname);
     403            0 :     }
     404              : 
     405            0 :     fn set_user(&mut self, user: RoleName) {
     406            0 :         self.span.record("role", display(&user));
     407            0 :         self.user = Some(user);
     408            0 :     }
     409              : 
     410            0 :     fn has_private_peer_addr(&self) -> bool {
     411            0 :         match self.conn_info.addr.ip() {
     412            0 :             IpAddr::V4(ip) => ip.is_private(),
     413            0 :             IpAddr::V6(_) => false,
     414              :         }
     415            0 :     }
     416              : 
     417            0 :     fn log_connect(&mut self) {
     418            0 :         let outcome = if self.success {
     419            0 :             ConnectOutcome::Success
     420              :         } else {
     421            0 :             ConnectOutcome::Failed
     422              :         };
     423              : 
     424              :         // TODO: get rid of entirely/refactor
     425              :         // check for false positives
     426              :         // AND false negatives
     427            0 :         if let Some(rejected) = self.rejected {
     428            0 :             let ep = self
     429            0 :                 .endpoint_id
     430            0 :                 .as_ref()
     431            0 :                 .map(|x| x.as_str())
     432            0 :                 .unwrap_or_default();
     433            0 :             // This makes sense only if cache is disabled
     434            0 :             debug!(
     435              :                 ?outcome,
     436              :                 ?rejected,
     437              :                 ?ep,
     438            0 :                 "check endpoint is valid with outcome"
     439              :             );
     440            0 :             Metrics::get()
     441            0 :                 .proxy
     442            0 :                 .invalid_endpoints_total
     443            0 :                 .inc(InvalidEndpointsGroup {
     444            0 :                     protocol: self.protocol,
     445            0 :                     rejected: rejected.into(),
     446            0 :                     outcome,
     447            0 :                 });
     448            0 :         }
     449              : 
     450            0 :         if let Some(tx) = self.sender.take() {
     451              :             // If type changes, this error handling needs to be updated.
     452            0 :             let tx: mpsc::UnboundedSender<RequestData> = tx;
     453            0 :             if let Err(e) = tx.send(RequestData::from(&*self)) {
     454            0 :                 error!("log_connect channel send failed: {e}");
     455            0 :             }
     456            0 :         }
     457            0 :     }
     458              : 
     459            0 :     fn log_disconnect(&mut self) {
     460            0 :         // If we are here, it's guaranteed that the user successfully connected to the endpoint.
     461            0 :         // Here we log the length of the session.
     462            0 :         self.disconnect_timestamp = Some(Utc::now());
     463            0 :         if let Some(tx) = self.disconnect_sender.take() {
     464              :             // If type changes, this error handling needs to be updated.
     465            0 :             let tx: mpsc::UnboundedSender<RequestData> = tx;
     466            0 :             if let Err(e) = tx.send(RequestData::from(&*self)) {
     467            0 :                 error!("log_disconnect channel send failed: {e}");
     468            0 :             }
     469            0 :         }
     470            0 :     }
     471              : }
     472              : 
     473              : impl Drop for RequestContextInner {
     474           70 :     fn drop(&mut self) {
     475           70 :         if self.sender.is_some() {
     476            0 :             self.log_connect();
     477           70 :         }
     478           70 :     }
     479              : }
     480              : 
     481              : pub struct DisconnectLogger(RequestContextInner);
     482              : 
     483              : impl Drop for DisconnectLogger {
     484            0 :     fn drop(&mut self) {
     485            0 :         self.0.log_disconnect();
     486            0 :     }
     487              : }
        

Generated by: LCOV version 2.1-beta