LCOV - code coverage report
Current view: top level - storage_controller/src - compute_hook.rs (source / functions) Coverage Total Hit
Test: 4e30745f424539d3816b821c09fe7733c446c226.info Lines: 37.9 % 372 141
Test Date: 2024-06-19 13:20:49 Functions: 15.4 % 52 8

            Line data    Source code
       1              : use std::sync::Arc;
       2              : use std::{collections::HashMap, time::Duration};
       3              : 
       4              : use control_plane::endpoint::{ComputeControlPlane, EndpointStatus};
       5              : use control_plane::local_env::LocalEnv;
       6              : use futures::StreamExt;
       7              : use hyper::StatusCode;
       8              : use pageserver_api::shard::{ShardCount, ShardNumber, ShardStripeSize, TenantShardId};
       9              : use postgres_connection::parse_host_port;
      10              : use serde::{Deserialize, Serialize};
      11              : use tokio_util::sync::CancellationToken;
      12              : use tracing::{info_span, Instrument};
      13              : use utils::{
      14              :     backoff::{self},
      15              :     id::{NodeId, TenantId},
      16              : };
      17              : 
      18              : use crate::service::Config;
      19              : 
      20              : const SLOWDOWN_DELAY: Duration = Duration::from_secs(5);
      21              : 
      22              : const NOTIFY_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
      23              : 
      24              : pub(crate) const API_CONCURRENCY: usize = 32;
      25              : 
      26              : struct UnshardedComputeHookTenant {
      27              :     // Which node is this tenant attached to
      28              :     node_id: NodeId,
      29              : 
      30              :     // Must hold this lock to send a notification.
      31              :     send_lock: Arc<tokio::sync::Mutex<Option<ComputeHookNotifyRequest>>>,
      32              : }
      33              : struct ShardedComputeHookTenant {
      34              :     stripe_size: ShardStripeSize,
      35              :     shard_count: ShardCount,
      36              :     shards: Vec<(ShardNumber, NodeId)>,
      37              : 
      38              :     // Must hold this lock to send a notification.  The contents represent
      39              :     // the last successfully sent notification, and are used to coalesce multiple
      40              :     // updates by only sending when there is a chance since our last successful send.
      41              :     send_lock: Arc<tokio::sync::Mutex<Option<ComputeHookNotifyRequest>>>,
      42              : }
      43              : 
      44              : enum ComputeHookTenant {
      45              :     Unsharded(UnshardedComputeHookTenant),
      46              :     Sharded(ShardedComputeHookTenant),
      47              : }
      48              : 
      49              : impl ComputeHookTenant {
      50              :     /// Construct with at least one shard's information
      51            4 :     fn new(tenant_shard_id: TenantShardId, stripe_size: ShardStripeSize, node_id: NodeId) -> Self {
      52            4 :         if tenant_shard_id.shard_count.count() > 1 {
      53            2 :             Self::Sharded(ShardedComputeHookTenant {
      54            2 :                 shards: vec![(tenant_shard_id.shard_number, node_id)],
      55            2 :                 stripe_size,
      56            2 :                 shard_count: tenant_shard_id.shard_count,
      57            2 :                 send_lock: Arc::default(),
      58            2 :             })
      59              :         } else {
      60            2 :             Self::Unsharded(UnshardedComputeHookTenant {
      61            2 :                 node_id,
      62            2 :                 send_lock: Arc::default(),
      63            2 :             })
      64              :         }
      65            4 :     }
      66              : 
      67            8 :     fn get_send_lock(&self) -> &Arc<tokio::sync::Mutex<Option<ComputeHookNotifyRequest>>> {
      68            8 :         match self {
      69            4 :             Self::Unsharded(unsharded_tenant) => &unsharded_tenant.send_lock,
      70            4 :             Self::Sharded(sharded_tenant) => &sharded_tenant.send_lock,
      71              :         }
      72            8 :     }
      73              : 
      74              :     /// Set one shard's location.  If stripe size or shard count have changed, Self is reset
      75              :     /// and drops existing content.
      76            4 :     fn update(
      77            4 :         &mut self,
      78            4 :         tenant_shard_id: TenantShardId,
      79            4 :         stripe_size: ShardStripeSize,
      80            4 :         node_id: NodeId,
      81            4 :     ) {
      82            2 :         match self {
      83            2 :             Self::Unsharded(unsharded_tenant) if tenant_shard_id.shard_count.count() == 1 => {
      84            0 :                 unsharded_tenant.node_id = node_id
      85              :             }
      86            2 :             Self::Sharded(sharded_tenant)
      87            2 :                 if sharded_tenant.stripe_size == stripe_size
      88            2 :                     && sharded_tenant.shard_count == tenant_shard_id.shard_count =>
      89              :             {
      90            2 :                 if let Some(existing) = sharded_tenant
      91            2 :                     .shards
      92            2 :                     .iter()
      93            2 :                     .position(|s| s.0 == tenant_shard_id.shard_number)
      94            0 :                 {
      95            0 :                     sharded_tenant.shards.get_mut(existing).unwrap().1 = node_id;
      96            0 :                 } else {
      97            2 :                     sharded_tenant
      98            2 :                         .shards
      99            2 :                         .push((tenant_shard_id.shard_number, node_id));
     100            4 :                     sharded_tenant.shards.sort_by_key(|s| s.0)
     101              :                 }
     102              :             }
     103            2 :             _ => {
     104            2 :                 // Shard count changed: reset struct.
     105            2 :                 *self = Self::new(tenant_shard_id, stripe_size, node_id);
     106            2 :             }
     107              :         }
     108            4 :     }
     109              : }
     110              : 
     111            0 : #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
     112              : struct ComputeHookNotifyRequestShard {
     113              :     node_id: NodeId,
     114              :     shard_number: ShardNumber,
     115              : }
     116              : 
     117              : /// Request body that we send to the control plane to notify it of where a tenant is attached
     118            0 : #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
     119              : struct ComputeHookNotifyRequest {
     120              :     tenant_id: TenantId,
     121              :     stripe_size: Option<ShardStripeSize>,
     122              :     shards: Vec<ComputeHookNotifyRequestShard>,
     123              : }
     124              : 
     125              : /// Error type for attempts to call into the control plane compute notification hook
     126            0 : #[derive(thiserror::Error, Debug)]
     127              : pub(crate) enum NotifyError {
     128              :     // Request was not send successfully, e.g. transport error
     129              :     #[error("Sending request: {0}")]
     130              :     Request(#[from] reqwest::Error),
     131              :     // Request could not be serviced right now due to ongoing Operation in control plane, but should be possible soon.
     132              :     #[error("Control plane tenant busy")]
     133              :     Busy,
     134              :     // Explicit 429 response asking us to retry less frequently
     135              :     #[error("Control plane overloaded")]
     136              :     SlowDown,
     137              :     // A 503 response indicates the control plane can't handle the request right now
     138              :     #[error("Control plane unavailable (status {0})")]
     139              :     Unavailable(StatusCode),
     140              :     // API returned unexpected non-success status.  We will retry, but log a warning.
     141              :     #[error("Control plane returned unexpected status {0}")]
     142              :     Unexpected(StatusCode),
     143              :     // We shutdown while sending
     144              :     #[error("Shutting down")]
     145              :     ShuttingDown,
     146              :     // A response indicates we will never succeed, such as 400 or 404
     147              :     #[error("Non-retryable error {0}")]
     148              :     Fatal(StatusCode),
     149              : }
     150              : 
     151              : enum MaybeSendResult {
     152              :     // Please send this request while holding the lock, and if you succeed then write
     153              :     // the request into the lock.
     154              :     Transmit(
     155              :         (
     156              :             ComputeHookNotifyRequest,
     157              :             tokio::sync::OwnedMutexGuard<Option<ComputeHookNotifyRequest>>,
     158              :         ),
     159              :     ),
     160              :     // Something requires sending, but you must wait for a current sender then call again
     161              :     AwaitLock(Arc<tokio::sync::Mutex<Option<ComputeHookNotifyRequest>>>),
     162              :     // Nothing requires sending
     163              :     Noop,
     164              : }
     165              : 
     166              : impl ComputeHookTenant {
     167            8 :     fn maybe_send(
     168            8 :         &self,
     169            8 :         tenant_id: TenantId,
     170            8 :         lock: Option<tokio::sync::OwnedMutexGuard<Option<ComputeHookNotifyRequest>>>,
     171            8 :     ) -> MaybeSendResult {
     172            8 :         let locked = match lock {
     173            0 :             Some(already_locked) => already_locked,
     174              :             None => {
     175              :                 // Lock order: this _must_ be only a try_lock, because we are called inside of the [`ComputeHook::state`] lock.
     176            8 :                 let Ok(locked) = self.get_send_lock().clone().try_lock_owned() else {
     177            0 :                     return MaybeSendResult::AwaitLock(self.get_send_lock().clone());
     178              :                 };
     179            8 :                 locked
     180              :             }
     181              :         };
     182              : 
     183            8 :         let request = match self {
     184            4 :             Self::Unsharded(unsharded_tenant) => Some(ComputeHookNotifyRequest {
     185            4 :                 tenant_id,
     186            4 :                 shards: vec![ComputeHookNotifyRequestShard {
     187            4 :                     shard_number: ShardNumber(0),
     188            4 :                     node_id: unsharded_tenant.node_id,
     189            4 :                 }],
     190            4 :                 stripe_size: None,
     191            4 :             }),
     192            4 :             Self::Sharded(sharded_tenant)
     193            4 :                 if sharded_tenant.shards.len() == sharded_tenant.shard_count.count() as usize =>
     194            2 :             {
     195            2 :                 Some(ComputeHookNotifyRequest {
     196            2 :                     tenant_id,
     197            2 :                     shards: sharded_tenant
     198            2 :                         .shards
     199            2 :                         .iter()
     200            4 :                         .map(|(shard_number, node_id)| ComputeHookNotifyRequestShard {
     201            4 :                             shard_number: *shard_number,
     202            4 :                             node_id: *node_id,
     203            4 :                         })
     204            2 :                         .collect(),
     205            2 :                     stripe_size: Some(sharded_tenant.stripe_size),
     206            2 :                 })
     207              :             }
     208            2 :             Self::Sharded(sharded_tenant) => {
     209            2 :                 // Sharded tenant doesn't yet have information for all its shards
     210            2 : 
     211            2 :                 tracing::info!(
     212            0 :                     "ComputeHookTenant::maybe_send: not enough shards ({}/{})",
     213            0 :                     sharded_tenant.shards.len(),
     214            0 :                     sharded_tenant.shard_count.count()
     215              :                 );
     216            2 :                 None
     217              :             }
     218              :         };
     219              : 
     220            6 :         match request {
     221              :             None => {
     222              :                 // Not yet ready to emit a notification
     223            2 :                 tracing::info!("Tenant isn't yet ready to emit a notification");
     224            2 :                 MaybeSendResult::Noop
     225              :             }
     226            6 :             Some(request) if Some(&request) == locked.as_ref() => {
     227            2 :                 // No change from the last value successfully sent
     228            2 :                 MaybeSendResult::Noop
     229              :             }
     230            4 :             Some(request) => MaybeSendResult::Transmit((request, locked)),
     231              :         }
     232            8 :     }
     233              : }
     234              : 
     235              : /// The compute hook is a destination for notifications about changes to tenant:pageserver
     236              : /// mapping.  It aggregates updates for the shards in a tenant, and when appropriate reconfigures
     237              : /// the compute connection string.
     238              : pub(super) struct ComputeHook {
     239              :     config: Config,
     240              :     state: std::sync::Mutex<HashMap<TenantId, ComputeHookTenant>>,
     241              :     authorization_header: Option<String>,
     242              : 
     243              :     // Concurrency limiter, so that we do not overload the cloud control plane when updating
     244              :     // large numbers of tenants (e.g. when failing over after a node failure)
     245              :     api_concurrency: tokio::sync::Semaphore,
     246              : 
     247              :     // This lock is only used in testing enviroments, to serialize calls into neon_lock
     248              :     neon_local_lock: tokio::sync::Mutex<()>,
     249              : 
     250              :     // We share a client across all notifications to enable connection re-use etc when
     251              :     // sending large numbers of notifications
     252              :     client: reqwest::Client,
     253              : }
     254              : 
     255              : impl ComputeHook {
     256            0 :     pub(super) fn new(config: Config) -> Self {
     257            0 :         let authorization_header = config
     258            0 :             .control_plane_jwt_token
     259            0 :             .clone()
     260            0 :             .map(|jwt| format!("Bearer {}", jwt));
     261            0 : 
     262            0 :         let client = reqwest::ClientBuilder::new()
     263            0 :             .timeout(NOTIFY_REQUEST_TIMEOUT)
     264            0 :             .build()
     265            0 :             .expect("Failed to construct HTTP client");
     266            0 : 
     267            0 :         Self {
     268            0 :             state: Default::default(),
     269            0 :             config,
     270            0 :             authorization_header,
     271            0 :             neon_local_lock: Default::default(),
     272            0 :             api_concurrency: tokio::sync::Semaphore::new(API_CONCURRENCY),
     273            0 :             client,
     274            0 :         }
     275            0 :     }
     276              : 
     277              :     /// For test environments: use neon_local's LocalEnv to update compute
     278            0 :     async fn do_notify_local(
     279            0 :         &self,
     280            0 :         reconfigure_request: &ComputeHookNotifyRequest,
     281            0 :     ) -> anyhow::Result<()> {
     282              :         // neon_local updates are not safe to call concurrently, use a lock to serialize
     283              :         // all calls to this function
     284            0 :         let _locked = self.neon_local_lock.lock().await;
     285              : 
     286            0 :         let env = match LocalEnv::load_config() {
     287            0 :             Ok(e) => e,
     288            0 :             Err(e) => {
     289            0 :                 tracing::warn!("Couldn't load neon_local config, skipping compute update ({e})");
     290            0 :                 return Ok(());
     291              :             }
     292              :         };
     293            0 :         let cplane =
     294            0 :             ComputeControlPlane::load(env.clone()).expect("Error loading compute control plane");
     295            0 :         let ComputeHookNotifyRequest {
     296            0 :             tenant_id,
     297            0 :             shards,
     298            0 :             stripe_size,
     299            0 :         } = reconfigure_request;
     300            0 : 
     301            0 :         let compute_pageservers = shards
     302            0 :             .iter()
     303            0 :             .map(|shard| {
     304            0 :                 let ps_conf = env
     305            0 :                     .get_pageserver_conf(shard.node_id)
     306            0 :                     .expect("Unknown pageserver");
     307            0 :                 let (pg_host, pg_port) = parse_host_port(&ps_conf.listen_pg_addr)
     308            0 :                     .expect("Unable to parse listen_pg_addr");
     309            0 :                 (pg_host, pg_port.unwrap_or(5432))
     310            0 :             })
     311            0 :             .collect::<Vec<_>>();
     312              : 
     313            0 :         for (endpoint_name, endpoint) in &cplane.endpoints {
     314            0 :             if endpoint.tenant_id == *tenant_id && endpoint.status() == EndpointStatus::Running {
     315            0 :                 tracing::info!("Reconfiguring endpoint {}", endpoint_name,);
     316            0 :                 endpoint
     317            0 :                     .reconfigure(compute_pageservers.clone(), *stripe_size)
     318            0 :                     .await?;
     319            0 :             }
     320              :         }
     321              : 
     322            0 :         Ok(())
     323            0 :     }
     324              : 
     325            0 :     async fn do_notify_iteration(
     326            0 :         &self,
     327            0 :         url: &String,
     328            0 :         reconfigure_request: &ComputeHookNotifyRequest,
     329            0 :         cancel: &CancellationToken,
     330            0 :     ) -> Result<(), NotifyError> {
     331            0 :         let req = self.client.request(reqwest::Method::PUT, url);
     332            0 :         let req = if let Some(value) = &self.authorization_header {
     333            0 :             req.header(reqwest::header::AUTHORIZATION, value)
     334              :         } else {
     335            0 :             req
     336              :         };
     337              : 
     338            0 :         tracing::info!(
     339            0 :             "Sending notify request to {} ({:?})",
     340              :             url,
     341              :             reconfigure_request
     342              :         );
     343            0 :         let send_result = req.json(&reconfigure_request).send().await;
     344            0 :         let response = match send_result {
     345            0 :             Ok(r) => r,
     346            0 :             Err(e) => return Err(e.into()),
     347              :         };
     348              : 
     349              :         // Treat all 2xx responses as success
     350            0 :         if response.status() >= reqwest::StatusCode::OK
     351            0 :             && response.status() < reqwest::StatusCode::MULTIPLE_CHOICES
     352              :         {
     353            0 :             if response.status() != reqwest::StatusCode::OK {
     354              :                 // Non-200 2xx response: it doesn't make sense to retry, but this is unexpected, so
     355              :                 // log a warning.
     356            0 :                 tracing::warn!(
     357            0 :                     "Unexpected 2xx response code {} from control plane",
     358            0 :                     response.status()
     359              :                 );
     360            0 :             }
     361              : 
     362            0 :             return Ok(());
     363            0 :         }
     364            0 : 
     365            0 :         // Error response codes
     366            0 :         match response.status() {
     367              :             reqwest::StatusCode::TOO_MANY_REQUESTS => {
     368              :                 // TODO: 429 handling should be global: set some state visible to other requests
     369              :                 // so that they will delay before starting, rather than all notifications trying
     370              :                 // once before backing off.
     371            0 :                 tokio::time::timeout(SLOWDOWN_DELAY, cancel.cancelled())
     372            0 :                     .await
     373            0 :                     .ok();
     374            0 :                 Err(NotifyError::SlowDown)
     375              :             }
     376              :             reqwest::StatusCode::LOCKED => {
     377              :                 // We consider this fatal, because it's possible that the operation blocking the control one is
     378              :                 // also the one that is waiting for this reconcile.  We should let the reconciler calling
     379              :                 // this hook fail, to give control plane a chance to un-lock.
     380            0 :                 tracing::info!("Control plane reports tenant is locked, dropping out of notify");
     381            0 :                 Err(NotifyError::Busy)
     382              :             }
     383              :             reqwest::StatusCode::SERVICE_UNAVAILABLE => {
     384            0 :                 Err(NotifyError::Unavailable(StatusCode::SERVICE_UNAVAILABLE))
     385              :             }
     386              :             reqwest::StatusCode::GATEWAY_TIMEOUT => {
     387            0 :                 Err(NotifyError::Unavailable(StatusCode::GATEWAY_TIMEOUT))
     388              :             }
     389              :             reqwest::StatusCode::BAD_GATEWAY => {
     390            0 :                 Err(NotifyError::Unavailable(StatusCode::BAD_GATEWAY))
     391              :             }
     392              : 
     393            0 :             reqwest::StatusCode::BAD_REQUEST => Err(NotifyError::Fatal(StatusCode::BAD_REQUEST)),
     394            0 :             reqwest::StatusCode::UNAUTHORIZED => Err(NotifyError::Fatal(StatusCode::UNAUTHORIZED)),
     395            0 :             reqwest::StatusCode::FORBIDDEN => Err(NotifyError::Fatal(StatusCode::FORBIDDEN)),
     396            0 :             status => Err(NotifyError::Unexpected(
     397            0 :                 hyper::StatusCode::from_u16(status.as_u16())
     398            0 :                     .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
     399            0 :             )),
     400              :         }
     401            0 :     }
     402              : 
     403            0 :     async fn do_notify(
     404            0 :         &self,
     405            0 :         url: &String,
     406            0 :         reconfigure_request: &ComputeHookNotifyRequest,
     407            0 :         cancel: &CancellationToken,
     408            0 :     ) -> Result<(), NotifyError> {
     409              :         // We hold these semaphore units across all retries, rather than only across each
     410              :         // HTTP request: this is to preserve fairness and avoid a situation where a retry might
     411              :         // time out waiting for a semaphore.
     412            0 :         let _units = self
     413            0 :             .api_concurrency
     414            0 :             .acquire()
     415            0 :             .await
     416              :             // Interpret closed semaphore as shutdown
     417            0 :             .map_err(|_| NotifyError::ShuttingDown)?;
     418              : 
     419            0 :         backoff::retry(
     420            0 :             || self.do_notify_iteration(url, reconfigure_request, cancel),
     421            0 :             |e| {
     422            0 :                 matches!(
     423            0 :                     e,
     424              :                     NotifyError::Fatal(_) | NotifyError::Unexpected(_) | NotifyError::Busy
     425              :                 )
     426            0 :             },
     427            0 :             3,
     428            0 :             10,
     429            0 :             "Send compute notification",
     430            0 :             cancel,
     431            0 :         )
     432            0 :         .await
     433            0 :         .ok_or_else(|| NotifyError::ShuttingDown)
     434            0 :         .and_then(|x| x)
     435            0 :     }
     436              : 
     437              :     /// Synchronous phase: update the per-tenant state for the next intended notification
     438            0 :     fn notify_prepare(
     439            0 :         &self,
     440            0 :         tenant_shard_id: TenantShardId,
     441            0 :         node_id: NodeId,
     442            0 :         stripe_size: ShardStripeSize,
     443            0 :     ) -> MaybeSendResult {
     444            0 :         let mut state_locked = self.state.lock().unwrap();
     445              : 
     446              :         use std::collections::hash_map::Entry;
     447            0 :         let tenant = match state_locked.entry(tenant_shard_id.tenant_id) {
     448            0 :             Entry::Vacant(e) => e.insert(ComputeHookTenant::new(
     449            0 :                 tenant_shard_id,
     450            0 :                 stripe_size,
     451            0 :                 node_id,
     452            0 :             )),
     453            0 :             Entry::Occupied(e) => {
     454            0 :                 let tenant = e.into_mut();
     455            0 :                 tenant.update(tenant_shard_id, stripe_size, node_id);
     456            0 :                 tenant
     457              :             }
     458              :         };
     459            0 :         tenant.maybe_send(tenant_shard_id.tenant_id, None)
     460            0 :     }
     461              : 
     462            0 :     async fn notify_execute(
     463            0 :         &self,
     464            0 :         maybe_send_result: MaybeSendResult,
     465            0 :         tenant_shard_id: TenantShardId,
     466            0 :         cancel: &CancellationToken,
     467            0 :     ) -> Result<(), NotifyError> {
     468              :         // Process result: we may get an update to send, or we may have to wait for a lock
     469              :         // before trying again.
     470            0 :         let (request, mut send_lock_guard) = match maybe_send_result {
     471              :             MaybeSendResult::Noop => {
     472            0 :                 return Ok(());
     473              :             }
     474            0 :             MaybeSendResult::AwaitLock(send_lock) => {
     475            0 :                 let send_locked = tokio::select! {
     476              :                     guard = send_lock.lock_owned() => {guard},
     477              :                     _ = cancel.cancelled() => {
     478              :                         return Err(NotifyError::ShuttingDown)
     479              :                     }
     480              :                 };
     481              : 
     482              :                 // Lock order: maybe_send is called within the `[Self::state]` lock, and takes the send lock, but here
     483              :                 // we have acquired the send lock and take `[Self::state]` lock.  This is safe because maybe_send only uses
     484              :                 // try_lock.
     485            0 :                 let state_locked = self.state.lock().unwrap();
     486            0 :                 let Some(tenant) = state_locked.get(&tenant_shard_id.tenant_id) else {
     487            0 :                     return Ok(());
     488              :                 };
     489            0 :                 match tenant.maybe_send(tenant_shard_id.tenant_id, Some(send_locked)) {
     490              :                     MaybeSendResult::AwaitLock(_) => {
     491            0 :                         unreachable!("We supplied lock guard")
     492              :                     }
     493              :                     MaybeSendResult::Noop => {
     494            0 :                         return Ok(());
     495              :                     }
     496            0 :                     MaybeSendResult::Transmit((request, lock)) => (request, lock),
     497              :                 }
     498              :             }
     499            0 :             MaybeSendResult::Transmit((request, lock)) => (request, lock),
     500              :         };
     501              : 
     502            0 :         let result = if let Some(notify_url) = &self.config.compute_hook_url {
     503            0 :             self.do_notify(notify_url, &request, cancel).await
     504              :         } else {
     505            0 :             self.do_notify_local(&request).await.map_err(|e| {
     506            0 :                 // This path is for testing only, so munge the error into our prod-style error type.
     507            0 :                 tracing::error!("Local notification hook failed: {e}");
     508            0 :                 NotifyError::Fatal(StatusCode::INTERNAL_SERVER_ERROR)
     509            0 :             })
     510              :         };
     511              : 
     512            0 :         if result.is_ok() {
     513            0 :             // Before dropping the send lock, stash the request we just sent so that
     514            0 :             // subsequent callers can avoid redundantly re-sending the same thing.
     515            0 :             *send_lock_guard = Some(request);
     516            0 :         }
     517            0 :         result
     518            0 :     }
     519              : 
     520              :     /// Infallible synchronous fire-and-forget version of notify(), that sends its results to
     521              :     /// a channel.  Something should consume the channel and arrange to try notifying again
     522              :     /// if something failed.
     523            0 :     pub(super) fn notify_background(
     524            0 :         self: &Arc<Self>,
     525            0 :         notifications: Vec<(TenantShardId, NodeId, ShardStripeSize)>,
     526            0 :         result_tx: tokio::sync::mpsc::Sender<Result<(), (TenantShardId, NotifyError)>>,
     527            0 :         cancel: &CancellationToken,
     528            0 :     ) {
     529            0 :         let mut maybe_sends = Vec::new();
     530            0 :         for (tenant_shard_id, node_id, stripe_size) in notifications {
     531            0 :             let maybe_send_result = self.notify_prepare(tenant_shard_id, node_id, stripe_size);
     532            0 :             maybe_sends.push((tenant_shard_id, maybe_send_result))
     533              :         }
     534              : 
     535            0 :         let this = self.clone();
     536            0 :         let cancel = cancel.clone();
     537            0 : 
     538            0 :         tokio::task::spawn(async move {
     539            0 :             // Construct an async stream of futures to invoke the compute notify function: we do this
     540            0 :             // in order to subsequently use .buffered() on the stream to execute with bounded parallelism.  The
     541            0 :             // ComputeHook semaphore already limits concurrency, but this way we avoid constructing+polling lots of futures which
     542            0 :             // would mostly just be waiting on that semaphore.
     543            0 :             let mut stream = futures::stream::iter(maybe_sends)
     544            0 :                 .map(|(tenant_shard_id, maybe_send_result)| {
     545            0 :                     let this = this.clone();
     546            0 :                     let cancel = cancel.clone();
     547              : 
     548            0 :                     async move {
     549            0 :                         this
     550            0 :                             .notify_execute(maybe_send_result, tenant_shard_id, &cancel)
     551            0 :                             .await.map_err(|e| (tenant_shard_id, e))
     552            0 :                     }.instrument(info_span!(
     553            0 :                         "notify_background", tenant_id=%tenant_shard_id.tenant_id, shard_id=%tenant_shard_id.shard_slug()
     554              :                     ))
     555            0 :                 })
     556            0 :                 .buffered(API_CONCURRENCY);
     557              : 
     558            0 :             loop {
     559            0 :                 tokio::select! {
     560              :                     next = stream.next() => {
     561              :                         match next {
     562              :                             Some(r) => {
     563              :                                 result_tx.send(r).await.ok();
     564              :                             },
     565              :                             None => {
     566              :                                 tracing::info!("Finished sending background compute notifications");
     567              :                                 break;
     568              :                             }
     569              :                         }
     570              :                     },
     571              :                     _ = cancel.cancelled() => {
     572              :                         tracing::info!("Shutdown while running background compute notifications");
     573              :                         break;
     574              :                     }
     575            0 :                 };
     576            0 :             }
     577            0 :         });
     578            0 :     }
     579              : 
     580              :     /// Call this to notify the compute (postgres) tier of new pageservers to use
     581              :     /// for a tenant.  notify() is called by each shard individually, and this function
     582              :     /// will decide whether an update to the tenant is sent.  An update is sent on the
     583              :     /// condition that:
     584              :     /// - We know a pageserver for every shard.
     585              :     /// - All the shards have the same shard_count (i.e. we are not mid-split)
     586              :     ///
     587              :     /// Cancellation token enables callers to drop out, e.g. if calling from a Reconciler
     588              :     /// that is cancelled.
     589              :     ///
     590              :     /// This function is fallible, including in the case that the control plane is transiently
     591              :     /// unavailable.  A limited number of retries are done internally to efficiently hide short unavailability
     592              :     /// periods, but we don't retry forever.  The **caller** is responsible for handling failures and
     593              :     /// ensuring that they eventually call again to ensure that the compute is eventually notified of
     594              :     /// the proper pageserver nodes for a tenant.
     595            0 :     #[tracing::instrument(skip_all, fields(tenant_id=%tenant_shard_id.tenant_id, shard_id=%tenant_shard_id.shard_slug(), node_id))]
     596              :     pub(super) async fn notify(
     597              :         &self,
     598              :         tenant_shard_id: TenantShardId,
     599              :         node_id: NodeId,
     600              :         stripe_size: ShardStripeSize,
     601              :         cancel: &CancellationToken,
     602              :     ) -> Result<(), NotifyError> {
     603              :         let maybe_send_result = self.notify_prepare(tenant_shard_id, node_id, stripe_size);
     604              :         self.notify_execute(maybe_send_result, tenant_shard_id, cancel)
     605              :             .await
     606              :     }
     607              : }
     608              : 
     609              : #[cfg(test)]
     610              : pub(crate) mod tests {
     611              :     use pageserver_api::shard::{ShardCount, ShardNumber};
     612              :     use utils::id::TenantId;
     613              : 
     614              :     use super::*;
     615              : 
     616              :     #[test]
     617            2 :     fn tenant_updates() -> anyhow::Result<()> {
     618            2 :         let tenant_id = TenantId::generate();
     619            2 :         let mut tenant_state = ComputeHookTenant::new(
     620            2 :             TenantShardId {
     621            2 :                 tenant_id,
     622            2 :                 shard_count: ShardCount::new(0),
     623            2 :                 shard_number: ShardNumber(0),
     624            2 :             },
     625            2 :             ShardStripeSize(12345),
     626            2 :             NodeId(1),
     627            2 :         );
     628            2 : 
     629            2 :         // An unsharded tenant is always ready to emit a notification, but won't
     630            2 :         // send the same one twice
     631            2 :         let send_result = tenant_state.maybe_send(tenant_id, None);
     632            2 :         let MaybeSendResult::Transmit((request, mut guard)) = send_result else {
     633            0 :             anyhow::bail!("Wrong send result");
     634              :         };
     635            2 :         assert_eq!(request.shards.len(), 1);
     636            2 :         assert!(request.stripe_size.is_none());
     637              : 
     638              :         // Simulate successful send
     639            2 :         *guard = Some(request);
     640            2 :         drop(guard);
     641            2 : 
     642            2 :         // Try asking again: this should be a no-op
     643            2 :         let send_result = tenant_state.maybe_send(tenant_id, None);
     644            2 :         assert!(matches!(send_result, MaybeSendResult::Noop));
     645              : 
     646              :         // Writing the first shard of a multi-sharded situation (i.e. in a split)
     647              :         // resets the tenant state and puts it in an non-notifying state (need to
     648              :         // see all shards)
     649            2 :         tenant_state.update(
     650            2 :             TenantShardId {
     651            2 :                 tenant_id,
     652            2 :                 shard_count: ShardCount::new(2),
     653            2 :                 shard_number: ShardNumber(1),
     654            2 :             },
     655            2 :             ShardStripeSize(32768),
     656            2 :             NodeId(1),
     657            2 :         );
     658            2 :         assert!(matches!(
     659            2 :             tenant_state.maybe_send(tenant_id, None),
     660              :             MaybeSendResult::Noop
     661              :         ));
     662              : 
     663              :         // Writing the second shard makes it ready to notify
     664            2 :         tenant_state.update(
     665            2 :             TenantShardId {
     666            2 :                 tenant_id,
     667            2 :                 shard_count: ShardCount::new(2),
     668            2 :                 shard_number: ShardNumber(0),
     669            2 :             },
     670            2 :             ShardStripeSize(32768),
     671            2 :             NodeId(1),
     672            2 :         );
     673            2 : 
     674            2 :         let send_result = tenant_state.maybe_send(tenant_id, None);
     675            2 :         let MaybeSendResult::Transmit((request, mut guard)) = send_result else {
     676            0 :             anyhow::bail!("Wrong send result");
     677              :         };
     678            2 :         assert_eq!(request.shards.len(), 2);
     679            2 :         assert_eq!(request.stripe_size, Some(ShardStripeSize(32768)));
     680              : 
     681              :         // Simulate successful send
     682            2 :         *guard = Some(request);
     683            2 :         drop(guard);
     684            2 : 
     685            2 :         Ok(())
     686            2 :     }
     687              : }
        

Generated by: LCOV version 2.1-beta