LCOV - code coverage report
Current view: top level - pageserver/src/http - routes.rs (source / functions) Coverage Total Hit
Test: 190869232aac3a234374e5bb62582e91cf5f5818.info Lines: 0.0 % 1641 0
Test Date: 2024-02-23 13:21:27 Functions: 0.0 % 641 0

            Line data    Source code
       1              : //!
       2              : //! Management HTTP API
       3              : //!
       4              : use std::collections::HashMap;
       5              : use std::str::FromStr;
       6              : use std::sync::Arc;
       7              : use std::time::Duration;
       8              : 
       9              : use anyhow::{anyhow, Context, Result};
      10              : use enumset::EnumSet;
      11              : use futures::TryFutureExt;
      12              : use humantime::format_rfc3339;
      13              : use hyper::header;
      14              : use hyper::StatusCode;
      15              : use hyper::{Body, Request, Response, Uri};
      16              : use metrics::launch_timestamp::LaunchTimestamp;
      17              : use pageserver_api::models::LocationConfigListResponse;
      18              : use pageserver_api::models::ShardParameters;
      19              : use pageserver_api::models::TenantDetails;
      20              : use pageserver_api::models::TenantLocationConfigResponse;
      21              : use pageserver_api::models::TenantShardLocation;
      22              : use pageserver_api::models::TenantShardSplitRequest;
      23              : use pageserver_api::models::TenantShardSplitResponse;
      24              : use pageserver_api::models::TenantState;
      25              : use pageserver_api::models::{
      26              :     DownloadRemoteLayersTaskSpawnRequest, LocationConfigMode, TenantAttachRequest,
      27              :     TenantLoadRequest, TenantLocationConfigRequest,
      28              : };
      29              : use pageserver_api::shard::ShardCount;
      30              : use pageserver_api::shard::TenantShardId;
      31              : use remote_storage::GenericRemoteStorage;
      32              : use remote_storage::TimeTravelError;
      33              : use tenant_size_model::{SizeResult, StorageModel};
      34              : use tokio_util::sync::CancellationToken;
      35              : use tracing::*;
      36              : use utils::auth::JwtAuth;
      37              : use utils::failpoint_support::failpoints_handler;
      38              : use utils::http::endpoint::request_span;
      39              : use utils::http::json::json_request_or_empty_body;
      40              : use utils::http::request::{get_request_param, must_get_query_param, parse_query_param};
      41              : 
      42              : use crate::context::{DownloadBehavior, RequestContext};
      43              : use crate::deletion_queue::DeletionQueueClient;
      44              : use crate::metrics::{StorageTimeOperation, STORAGE_TIME_GLOBAL};
      45              : use crate::pgdatadir_mapping::LsnForTimestamp;
      46              : use crate::task_mgr::TaskKind;
      47              : use crate::tenant::config::{LocationConf, TenantConfOpt};
      48              : use crate::tenant::mgr::GetActiveTenantError;
      49              : use crate::tenant::mgr::{
      50              :     GetTenantError, SetNewTenantConfigError, TenantManager, TenantMapError, TenantMapInsertError,
      51              :     TenantSlotError, TenantSlotUpsertError, TenantStateError,
      52              : };
      53              : use crate::tenant::mgr::{TenantSlot, UpsertLocationError};
      54              : use crate::tenant::remote_timeline_client;
      55              : use crate::tenant::secondary::SecondaryController;
      56              : use crate::tenant::size::ModelInputs;
      57              : use crate::tenant::storage_layer::LayerAccessStatsReset;
      58              : use crate::tenant::timeline::CompactFlags;
      59              : use crate::tenant::timeline::Timeline;
      60              : use crate::tenant::SpawnMode;
      61              : use crate::tenant::{LogicalSizeCalculationCause, PageReconstructError};
      62              : use crate::{config::PageServerConf, tenant::mgr};
      63              : use crate::{disk_usage_eviction_task, tenant};
      64              : use pageserver_api::models::{
      65              :     StatusResponse, TenantConfigRequest, TenantCreateRequest, TenantCreateResponse, TenantInfo,
      66              :     TimelineCreateRequest, TimelineGcRequest, TimelineInfo,
      67              : };
      68              : use utils::{
      69              :     auth::SwappableJwtAuth,
      70              :     generation::Generation,
      71              :     http::{
      72              :         endpoint::{self, attach_openapi_ui, auth_middleware, check_permission_with},
      73              :         error::{ApiError, HttpErrorBody},
      74              :         json::{json_request, json_response},
      75              :         request::parse_request_param,
      76              :         RequestExt, RouterBuilder,
      77              :     },
      78              :     id::{TenantId, TimelineId},
      79              :     lsn::Lsn,
      80              : };
      81              : 
      82              : // For APIs that require an Active tenant, how long should we block waiting for that state?
      83              : // This is not functionally necessary (clients will retry), but avoids generating a lot of
      84              : // failed API calls while tenants are activating.
      85              : #[cfg(not(feature = "testing"))]
      86              : pub(crate) const ACTIVE_TENANT_TIMEOUT: Duration = Duration::from_millis(5000);
      87              : 
      88              : // Tests run on slow/oversubscribed nodes, and may need to wait much longer for tenants to
      89              : // finish attaching, if calls to remote storage are slow.
      90              : #[cfg(feature = "testing")]
      91              : pub(crate) const ACTIVE_TENANT_TIMEOUT: Duration = Duration::from_millis(30000);
      92              : 
      93              : pub struct State {
      94              :     conf: &'static PageServerConf,
      95              :     tenant_manager: Arc<TenantManager>,
      96              :     auth: Option<Arc<SwappableJwtAuth>>,
      97              :     allowlist_routes: Vec<Uri>,
      98              :     remote_storage: Option<GenericRemoteStorage>,
      99              :     broker_client: storage_broker::BrokerClientChannel,
     100              :     disk_usage_eviction_state: Arc<disk_usage_eviction_task::State>,
     101              :     deletion_queue_client: DeletionQueueClient,
     102              :     secondary_controller: SecondaryController,
     103              :     latest_utilization: tokio::sync::Mutex<Option<(std::time::Instant, bytes::Bytes)>>,
     104              : }
     105              : 
     106              : impl State {
     107              :     #[allow(clippy::too_many_arguments)]
     108            0 :     pub fn new(
     109            0 :         conf: &'static PageServerConf,
     110            0 :         tenant_manager: Arc<TenantManager>,
     111            0 :         auth: Option<Arc<SwappableJwtAuth>>,
     112            0 :         remote_storage: Option<GenericRemoteStorage>,
     113            0 :         broker_client: storage_broker::BrokerClientChannel,
     114            0 :         disk_usage_eviction_state: Arc<disk_usage_eviction_task::State>,
     115            0 :         deletion_queue_client: DeletionQueueClient,
     116            0 :         secondary_controller: SecondaryController,
     117            0 :     ) -> anyhow::Result<Self> {
     118            0 :         let allowlist_routes = ["/v1/status", "/v1/doc", "/swagger.yml", "/metrics"]
     119            0 :             .iter()
     120            0 :             .map(|v| v.parse().unwrap())
     121            0 :             .collect::<Vec<_>>();
     122            0 :         Ok(Self {
     123            0 :             conf,
     124            0 :             tenant_manager,
     125            0 :             auth,
     126            0 :             allowlist_routes,
     127            0 :             remote_storage,
     128            0 :             broker_client,
     129            0 :             disk_usage_eviction_state,
     130            0 :             deletion_queue_client,
     131            0 :             secondary_controller,
     132            0 :             latest_utilization: Default::default(),
     133            0 :         })
     134            0 :     }
     135              : }
     136              : 
     137              : #[inline(always)]
     138            0 : fn get_state(request: &Request<Body>) -> &State {
     139            0 :     request
     140            0 :         .data::<Arc<State>>()
     141            0 :         .expect("unknown state type")
     142            0 :         .as_ref()
     143            0 : }
     144              : 
     145              : #[inline(always)]
     146            0 : fn get_config(request: &Request<Body>) -> &'static PageServerConf {
     147            0 :     get_state(request).conf
     148            0 : }
     149              : 
     150              : /// Check that the requester is authorized to operate on given tenant
     151            0 : fn check_permission(request: &Request<Body>, tenant_id: Option<TenantId>) -> Result<(), ApiError> {
     152            0 :     check_permission_with(request, |claims| {
     153            0 :         crate::auth::check_permission(claims, tenant_id)
     154            0 :     })
     155            0 : }
     156              : 
     157              : impl From<PageReconstructError> for ApiError {
     158            0 :     fn from(pre: PageReconstructError) -> ApiError {
     159            0 :         match pre {
     160            0 :             PageReconstructError::Other(pre) => ApiError::InternalServerError(pre),
     161              :             PageReconstructError::Cancelled => {
     162            0 :                 ApiError::InternalServerError(anyhow::anyhow!("request was cancelled"))
     163              :             }
     164              :             PageReconstructError::AncestorStopping(_) => {
     165            0 :                 ApiError::ResourceUnavailable(format!("{pre}").into())
     166              :             }
     167            0 :             PageReconstructError::AncestorLsnTimeout(e) => ApiError::Timeout(format!("{e}").into()),
     168            0 :             PageReconstructError::WalRedo(pre) => ApiError::InternalServerError(pre),
     169              :         }
     170            0 :     }
     171              : }
     172              : 
     173              : impl From<TenantMapInsertError> for ApiError {
     174            0 :     fn from(tmie: TenantMapInsertError) -> ApiError {
     175            0 :         match tmie {
     176            0 :             TenantMapInsertError::SlotError(e) => e.into(),
     177            0 :             TenantMapInsertError::SlotUpsertError(e) => e.into(),
     178            0 :             TenantMapInsertError::Other(e) => ApiError::InternalServerError(e),
     179              :         }
     180            0 :     }
     181              : }
     182              : 
     183              : impl From<TenantSlotError> for ApiError {
     184            0 :     fn from(e: TenantSlotError) -> ApiError {
     185            0 :         use TenantSlotError::*;
     186            0 :         match e {
     187            0 :             NotFound(tenant_id) => {
     188            0 :                 ApiError::NotFound(anyhow::anyhow!("NotFound: tenant {tenant_id}").into())
     189              :             }
     190            0 :             e @ AlreadyExists(_, _) => ApiError::Conflict(format!("{e}")),
     191              :             InProgress => {
     192            0 :                 ApiError::ResourceUnavailable("Tenant is being modified concurrently".into())
     193              :             }
     194            0 :             MapState(e) => e.into(),
     195              :         }
     196            0 :     }
     197              : }
     198              : 
     199              : impl From<TenantSlotUpsertError> for ApiError {
     200            0 :     fn from(e: TenantSlotUpsertError) -> ApiError {
     201            0 :         use TenantSlotUpsertError::*;
     202            0 :         match e {
     203            0 :             InternalError(e) => ApiError::InternalServerError(anyhow::anyhow!("{e}")),
     204            0 :             MapState(e) => e.into(),
     205            0 :             ShuttingDown(_) => ApiError::ShuttingDown,
     206              :         }
     207            0 :     }
     208              : }
     209              : 
     210              : impl From<UpsertLocationError> for ApiError {
     211            0 :     fn from(e: UpsertLocationError) -> ApiError {
     212            0 :         use UpsertLocationError::*;
     213            0 :         match e {
     214            0 :             BadRequest(e) => ApiError::BadRequest(e),
     215            0 :             Unavailable(_) => ApiError::ShuttingDown,
     216            0 :             e @ InProgress => ApiError::Conflict(format!("{e}")),
     217            0 :             Flush(e) | Other(e) => ApiError::InternalServerError(e),
     218              :         }
     219            0 :     }
     220              : }
     221              : 
     222              : impl From<TenantMapError> for ApiError {
     223            0 :     fn from(e: TenantMapError) -> ApiError {
     224            0 :         use TenantMapError::*;
     225            0 :         match e {
     226              :             StillInitializing | ShuttingDown => {
     227            0 :                 ApiError::ResourceUnavailable(format!("{e}").into())
     228            0 :             }
     229            0 :         }
     230            0 :     }
     231              : }
     232              : 
     233              : impl From<TenantStateError> for ApiError {
     234            0 :     fn from(tse: TenantStateError) -> ApiError {
     235            0 :         match tse {
     236              :             TenantStateError::IsStopping(_) => {
     237            0 :                 ApiError::ResourceUnavailable("Tenant is stopping".into())
     238              :             }
     239            0 :             TenantStateError::SlotError(e) => e.into(),
     240            0 :             TenantStateError::SlotUpsertError(e) => e.into(),
     241            0 :             TenantStateError::Other(e) => ApiError::InternalServerError(anyhow!(e)),
     242              :         }
     243            0 :     }
     244              : }
     245              : 
     246              : impl From<GetTenantError> for ApiError {
     247            0 :     fn from(tse: GetTenantError) -> ApiError {
     248            0 :         match tse {
     249            0 :             GetTenantError::NotFound(tid) => ApiError::NotFound(anyhow!("tenant {}", tid).into()),
     250            0 :             GetTenantError::Broken(reason) => {
     251            0 :                 ApiError::InternalServerError(anyhow!("tenant is broken: {}", reason))
     252              :             }
     253              :             GetTenantError::NotActive(_) => {
     254              :                 // Why is this not `ApiError::NotFound`?
     255              :                 // Because we must be careful to never return 404 for a tenant if it does
     256              :                 // in fact exist locally. If we did, the caller could draw the conclusion
     257              :                 // that it can attach the tenant to another PS and we'd be in split-brain.
     258              :                 //
     259              :                 // (We can produce this variant only in `mgr::get_tenant(..., active=true)` calls).
     260            0 :                 ApiError::ResourceUnavailable("Tenant not yet active".into())
     261              :             }
     262            0 :             GetTenantError::MapState(e) => ApiError::ResourceUnavailable(format!("{e}").into()),
     263              :         }
     264            0 :     }
     265              : }
     266              : 
     267              : impl From<GetActiveTenantError> for ApiError {
     268            0 :     fn from(e: GetActiveTenantError) -> ApiError {
     269            0 :         match e {
     270            0 :             GetActiveTenantError::WillNotBecomeActive(_) => ApiError::Conflict(format!("{}", e)),
     271            0 :             GetActiveTenantError::Cancelled => ApiError::ShuttingDown,
     272            0 :             GetActiveTenantError::NotFound(gte) => gte.into(),
     273              :             GetActiveTenantError::WaitForActiveTimeout { .. } => {
     274            0 :                 ApiError::ResourceUnavailable(format!("{}", e).into())
     275              :             }
     276              :         }
     277            0 :     }
     278              : }
     279              : 
     280              : impl From<SetNewTenantConfigError> for ApiError {
     281            0 :     fn from(e: SetNewTenantConfigError) -> ApiError {
     282            0 :         match e {
     283            0 :             SetNewTenantConfigError::GetTenant(tid) => {
     284            0 :                 ApiError::NotFound(anyhow!("tenant {}", tid).into())
     285              :             }
     286            0 :             e @ (SetNewTenantConfigError::Persist(_) | SetNewTenantConfigError::Other(_)) => {
     287            0 :                 ApiError::InternalServerError(anyhow::Error::new(e))
     288              :             }
     289              :         }
     290            0 :     }
     291              : }
     292              : 
     293              : impl From<crate::tenant::DeleteTimelineError> for ApiError {
     294            0 :     fn from(value: crate::tenant::DeleteTimelineError) -> Self {
     295            0 :         use crate::tenant::DeleteTimelineError::*;
     296            0 :         match value {
     297            0 :             NotFound => ApiError::NotFound(anyhow::anyhow!("timeline not found").into()),
     298            0 :             HasChildren(children) => ApiError::PreconditionFailed(
     299            0 :                 format!("Cannot delete timeline which has child timelines: {children:?}")
     300            0 :                     .into_boxed_str(),
     301            0 :             ),
     302            0 :             a @ AlreadyInProgress(_) => ApiError::Conflict(a.to_string()),
     303            0 :             Other(e) => ApiError::InternalServerError(e),
     304              :         }
     305            0 :     }
     306              : }
     307              : 
     308              : impl From<crate::tenant::mgr::DeleteTimelineError> for ApiError {
     309            0 :     fn from(value: crate::tenant::mgr::DeleteTimelineError) -> Self {
     310              :         use crate::tenant::mgr::DeleteTimelineError::*;
     311            0 :         match value {
     312              :             // Report Precondition failed so client can distinguish between
     313              :             // "tenant is missing" case from "timeline is missing"
     314            0 :             Tenant(GetTenantError::NotFound(..)) => ApiError::PreconditionFailed(
     315            0 :                 "Requested tenant is missing".to_owned().into_boxed_str(),
     316            0 :             ),
     317            0 :             Tenant(t) => ApiError::from(t),
     318            0 :             Timeline(t) => ApiError::from(t),
     319              :         }
     320            0 :     }
     321              : }
     322              : 
     323              : impl From<crate::tenant::delete::DeleteTenantError> for ApiError {
     324            0 :     fn from(value: crate::tenant::delete::DeleteTenantError) -> Self {
     325            0 :         use crate::tenant::delete::DeleteTenantError::*;
     326            0 :         match value {
     327            0 :             Get(g) => ApiError::from(g),
     328            0 :             e @ AlreadyInProgress => ApiError::Conflict(e.to_string()),
     329            0 :             Timeline(t) => ApiError::from(t),
     330            0 :             NotAttached => ApiError::NotFound(anyhow::anyhow!("Tenant is not attached").into()),
     331            0 :             SlotError(e) => e.into(),
     332            0 :             SlotUpsertError(e) => e.into(),
     333            0 :             Other(o) => ApiError::InternalServerError(o),
     334            0 :             e @ InvalidState(_) => ApiError::PreconditionFailed(e.to_string().into_boxed_str()),
     335            0 :             Cancelled => ApiError::ShuttingDown,
     336              :         }
     337            0 :     }
     338              : }
     339              : 
     340              : // Helper function to construct a TimelineInfo struct for a timeline
     341            0 : async fn build_timeline_info(
     342            0 :     timeline: &Arc<Timeline>,
     343            0 :     include_non_incremental_logical_size: bool,
     344            0 :     force_await_initial_logical_size: bool,
     345            0 :     ctx: &RequestContext,
     346            0 : ) -> anyhow::Result<TimelineInfo> {
     347            0 :     crate::tenant::debug_assert_current_span_has_tenant_and_timeline_id();
     348            0 : 
     349            0 :     if force_await_initial_logical_size {
     350            0 :         timeline.clone().await_initial_logical_size().await
     351            0 :     }
     352              : 
     353            0 :     let mut info = build_timeline_info_common(
     354            0 :         timeline,
     355            0 :         ctx,
     356            0 :         tenant::timeline::GetLogicalSizePriority::Background,
     357            0 :     )
     358            0 :     .await?;
     359            0 :     if include_non_incremental_logical_size {
     360              :         // XXX we should be using spawn_ondemand_logical_size_calculation here.
     361              :         // Otherwise, if someone deletes the timeline / detaches the tenant while
     362              :         // we're executing this function, we will outlive the timeline on-disk state.
     363              :         info.current_logical_size_non_incremental = Some(
     364            0 :             timeline
     365            0 :                 .get_current_logical_size_non_incremental(info.last_record_lsn, ctx)
     366            0 :                 .await?,
     367              :         );
     368            0 :     }
     369            0 :     Ok(info)
     370            0 : }
     371              : 
     372            0 : async fn build_timeline_info_common(
     373            0 :     timeline: &Arc<Timeline>,
     374            0 :     ctx: &RequestContext,
     375            0 :     logical_size_task_priority: tenant::timeline::GetLogicalSizePriority,
     376            0 : ) -> anyhow::Result<TimelineInfo> {
     377            0 :     crate::tenant::debug_assert_current_span_has_tenant_and_timeline_id();
     378            0 :     let initdb_lsn = timeline.initdb_lsn;
     379            0 :     let last_record_lsn = timeline.get_last_record_lsn();
     380            0 :     let (wal_source_connstr, last_received_msg_lsn, last_received_msg_ts) = {
     381            0 :         let guard = timeline.last_received_wal.lock().unwrap();
     382            0 :         if let Some(info) = guard.as_ref() {
     383            0 :             (
     384            0 :                 Some(format!("{:?}", info.wal_source_connconf)), // Password is hidden, but it's for statistics only.
     385            0 :                 Some(info.last_received_msg_lsn),
     386            0 :                 Some(info.last_received_msg_ts),
     387            0 :             )
     388              :         } else {
     389            0 :             (None, None, None)
     390              :         }
     391              :     };
     392              : 
     393            0 :     let ancestor_timeline_id = timeline.get_ancestor_timeline_id();
     394            0 :     let ancestor_lsn = match timeline.get_ancestor_lsn() {
     395            0 :         Lsn(0) => None,
     396            0 :         lsn @ Lsn(_) => Some(lsn),
     397              :     };
     398            0 :     let current_logical_size = timeline.get_current_logical_size(logical_size_task_priority, ctx);
     399            0 :     let current_physical_size = Some(timeline.layer_size_sum().await);
     400            0 :     let state = timeline.current_state();
     401            0 :     let remote_consistent_lsn_projected = timeline
     402            0 :         .get_remote_consistent_lsn_projected()
     403            0 :         .unwrap_or(Lsn(0));
     404            0 :     let remote_consistent_lsn_visible = timeline
     405            0 :         .get_remote_consistent_lsn_visible()
     406            0 :         .unwrap_or(Lsn(0));
     407            0 : 
     408            0 :     let walreceiver_status = timeline.walreceiver_status();
     409              : 
     410            0 :     let info = TimelineInfo {
     411            0 :         tenant_id: timeline.tenant_shard_id,
     412            0 :         timeline_id: timeline.timeline_id,
     413            0 :         ancestor_timeline_id,
     414            0 :         ancestor_lsn,
     415            0 :         disk_consistent_lsn: timeline.get_disk_consistent_lsn(),
     416            0 :         remote_consistent_lsn: remote_consistent_lsn_projected,
     417            0 :         remote_consistent_lsn_visible,
     418            0 :         initdb_lsn,
     419            0 :         last_record_lsn,
     420            0 :         prev_record_lsn: Some(timeline.get_prev_record_lsn()),
     421            0 :         latest_gc_cutoff_lsn: *timeline.get_latest_gc_cutoff_lsn(),
     422            0 :         current_logical_size: current_logical_size.size_dont_care_about_accuracy(),
     423            0 :         current_logical_size_is_accurate: match current_logical_size.accuracy() {
     424            0 :             tenant::timeline::logical_size::Accuracy::Approximate => false,
     425            0 :             tenant::timeline::logical_size::Accuracy::Exact => true,
     426              :         },
     427            0 :         directory_entries_counts: timeline.get_directory_metrics().to_vec(),
     428            0 :         current_physical_size,
     429            0 :         current_logical_size_non_incremental: None,
     430            0 :         timeline_dir_layer_file_size_sum: None,
     431            0 :         wal_source_connstr,
     432            0 :         last_received_msg_lsn,
     433            0 :         last_received_msg_ts,
     434            0 :         pg_version: timeline.pg_version,
     435            0 : 
     436            0 :         state,
     437            0 : 
     438            0 :         walreceiver_status,
     439            0 :     };
     440            0 :     Ok(info)
     441            0 : }
     442              : 
     443              : // healthcheck handler
     444            0 : async fn status_handler(
     445            0 :     request: Request<Body>,
     446            0 :     _cancel: CancellationToken,
     447            0 : ) -> Result<Response<Body>, ApiError> {
     448            0 :     check_permission(&request, None)?;
     449            0 :     let config = get_config(&request);
     450            0 :     json_response(StatusCode::OK, StatusResponse { id: config.id })
     451            0 : }
     452              : 
     453            0 : async fn reload_auth_validation_keys_handler(
     454            0 :     request: Request<Body>,
     455            0 :     _cancel: CancellationToken,
     456            0 : ) -> Result<Response<Body>, ApiError> {
     457            0 :     check_permission(&request, None)?;
     458            0 :     let config = get_config(&request);
     459            0 :     let state = get_state(&request);
     460            0 :     let Some(shared_auth) = &state.auth else {
     461            0 :         return json_response(StatusCode::BAD_REQUEST, ());
     462              :     };
     463              :     // unwrap is ok because check is performed when creating config, so path is set and exists
     464            0 :     let key_path = config.auth_validation_public_key_path.as_ref().unwrap();
     465            0 :     info!("Reloading public key(s) for verifying JWT tokens from {key_path:?}");
     466              : 
     467            0 :     match JwtAuth::from_key_path(key_path) {
     468            0 :         Ok(new_auth) => {
     469            0 :             shared_auth.swap(new_auth);
     470            0 :             json_response(StatusCode::OK, ())
     471              :         }
     472            0 :         Err(e) => {
     473            0 :             warn!("Error reloading public keys from {key_path:?}: {e:}");
     474            0 :             json_response(StatusCode::INTERNAL_SERVER_ERROR, ())
     475              :         }
     476              :     }
     477            0 : }
     478              : 
     479            0 : async fn timeline_create_handler(
     480            0 :     mut request: Request<Body>,
     481            0 :     _cancel: CancellationToken,
     482            0 : ) -> Result<Response<Body>, ApiError> {
     483            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     484            0 :     let request_data: TimelineCreateRequest = json_request(&mut request).await?;
     485            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     486              : 
     487            0 :     let new_timeline_id = request_data.new_timeline_id;
     488            0 : 
     489            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Error);
     490            0 : 
     491            0 :     let state = get_state(&request);
     492              : 
     493            0 :     async {
     494            0 :         let tenant = state
     495            0 :             .tenant_manager
     496            0 :             .get_attached_tenant_shard(tenant_shard_id, false)?;
     497              : 
     498            0 :         tenant.wait_to_become_active(ACTIVE_TENANT_TIMEOUT).await?;
     499              : 
     500            0 :         if let Some(ancestor_id) = request_data.ancestor_timeline_id.as_ref() {
     501            0 :             tracing::info!(%ancestor_id, "starting to branch");
     502              :         } else {
     503            0 :             tracing::info!("bootstrapping");
     504              :         }
     505              : 
     506            0 :         match tenant
     507            0 :             .create_timeline(
     508            0 :                 new_timeline_id,
     509            0 :                 request_data.ancestor_timeline_id,
     510            0 :                 request_data.ancestor_start_lsn,
     511            0 :                 request_data.pg_version.unwrap_or(crate::DEFAULT_PG_VERSION),
     512            0 :                 request_data.existing_initdb_timeline_id,
     513            0 :                 state.broker_client.clone(),
     514            0 :                 &ctx,
     515            0 :             )
     516            0 :             .await
     517              :         {
     518            0 :             Ok(new_timeline) => {
     519              :                 // Created. Construct a TimelineInfo for it.
     520            0 :                 let timeline_info = build_timeline_info_common(
     521            0 :                     &new_timeline,
     522            0 :                     &ctx,
     523            0 :                     tenant::timeline::GetLogicalSizePriority::User,
     524            0 :                 )
     525            0 :                 .await
     526            0 :                 .map_err(ApiError::InternalServerError)?;
     527            0 :                 json_response(StatusCode::CREATED, timeline_info)
     528              :             }
     529            0 :             Err(_) if tenant.cancel.is_cancelled() => {
     530            0 :                 // In case we get some ugly error type during shutdown, cast it into a clean 503.
     531            0 :                 json_response(
     532            0 :                     StatusCode::SERVICE_UNAVAILABLE,
     533            0 :                     HttpErrorBody::from_msg("Tenant shutting down".to_string()),
     534            0 :                 )
     535              :             }
     536              :             Err(
     537              :                 tenant::CreateTimelineError::Conflict
     538              :                 | tenant::CreateTimelineError::AlreadyCreating,
     539            0 :             ) => json_response(StatusCode::CONFLICT, ()),
     540            0 :             Err(tenant::CreateTimelineError::AncestorLsn(err)) => json_response(
     541            0 :                 StatusCode::NOT_ACCEPTABLE,
     542            0 :                 HttpErrorBody::from_msg(format!("{err:#}")),
     543            0 :             ),
     544            0 :             Err(e @ tenant::CreateTimelineError::AncestorNotActive) => json_response(
     545            0 :                 StatusCode::SERVICE_UNAVAILABLE,
     546            0 :                 HttpErrorBody::from_msg(e.to_string()),
     547            0 :             ),
     548            0 :             Err(tenant::CreateTimelineError::ShuttingDown) => json_response(
     549            0 :                 StatusCode::SERVICE_UNAVAILABLE,
     550            0 :                 HttpErrorBody::from_msg("tenant shutting down".to_string()),
     551            0 :             ),
     552            0 :             Err(tenant::CreateTimelineError::Other(err)) => Err(ApiError::InternalServerError(err)),
     553              :         }
     554            0 :     }
     555              :     .instrument(info_span!("timeline_create",
     556              :         tenant_id = %tenant_shard_id.tenant_id,
     557            0 :         shard_id = %tenant_shard_id.shard_slug(),
     558              :         timeline_id = %new_timeline_id,
     559              :         lsn=?request_data.ancestor_start_lsn,
     560              :         pg_version=?request_data.pg_version
     561              :     ))
     562            0 :     .await
     563            0 : }
     564              : 
     565            0 : async fn timeline_list_handler(
     566            0 :     request: Request<Body>,
     567            0 :     _cancel: CancellationToken,
     568            0 : ) -> Result<Response<Body>, ApiError> {
     569            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     570            0 :     let include_non_incremental_logical_size: Option<bool> =
     571            0 :         parse_query_param(&request, "include-non-incremental-logical-size")?;
     572            0 :     let force_await_initial_logical_size: Option<bool> =
     573            0 :         parse_query_param(&request, "force-await-initial-logical-size")?;
     574            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     575              : 
     576            0 :     let state = get_state(&request);
     577            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
     578              : 
     579            0 :     let response_data = async {
     580            0 :         let tenant = state
     581            0 :             .tenant_manager
     582            0 :             .get_attached_tenant_shard(tenant_shard_id, false)?;
     583              : 
     584            0 :         tenant.wait_to_become_active(ACTIVE_TENANT_TIMEOUT).await?;
     585              : 
     586            0 :         let timelines = tenant.list_timelines();
     587            0 : 
     588            0 :         let mut response_data = Vec::with_capacity(timelines.len());
     589            0 :         for timeline in timelines {
     590            0 :             let timeline_info = build_timeline_info(
     591            0 :                 &timeline,
     592            0 :                 include_non_incremental_logical_size.unwrap_or(false),
     593            0 :                 force_await_initial_logical_size.unwrap_or(false),
     594            0 :                 &ctx,
     595            0 :             )
     596            0 :             .instrument(info_span!("build_timeline_info", timeline_id = %timeline.timeline_id))
     597            0 :             .await
     598            0 :             .context("Failed to convert tenant timeline {timeline_id} into the local one: {e:?}")
     599            0 :             .map_err(ApiError::InternalServerError)?;
     600              : 
     601            0 :             response_data.push(timeline_info);
     602              :         }
     603            0 :         Ok::<Vec<TimelineInfo>, ApiError>(response_data)
     604            0 :     }
     605              :     .instrument(info_span!("timeline_list",
     606              :                 tenant_id = %tenant_shard_id.tenant_id,
     607            0 :                 shard_id = %tenant_shard_id.shard_slug()))
     608            0 :     .await?;
     609              : 
     610            0 :     json_response(StatusCode::OK, response_data)
     611            0 : }
     612              : 
     613            0 : async fn timeline_preserve_initdb_handler(
     614            0 :     request: Request<Body>,
     615            0 :     _cancel: CancellationToken,
     616            0 : ) -> Result<Response<Body>, ApiError> {
     617            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     618            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
     619            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     620              : 
     621              :     // Part of the process for disaster recovery from safekeeper-stored WAL:
     622              :     // If we don't recover into a new timeline but want to keep the timeline ID,
     623              :     // then the initdb archive is deleted. This endpoint copies it to a different
     624              :     // location where timeline recreation cand find it.
     625              : 
     626            0 :     async {
     627            0 :         let tenant = mgr::get_tenant(tenant_shard_id, false)?;
     628              : 
     629            0 :         let timeline = tenant
     630            0 :             .get_timeline(timeline_id, false)
     631            0 :             .map_err(|e| ApiError::NotFound(e.into()))?;
     632              : 
     633            0 :         timeline
     634            0 :             .preserve_initdb_archive()
     635            0 :             .await
     636            0 :             .context("preserving initdb archive")
     637            0 :             .map_err(ApiError::InternalServerError)?;
     638              : 
     639            0 :         Ok::<_, ApiError>(())
     640            0 :     }
     641              :     .instrument(info_span!("timeline_preserve_initdb_archive",
     642              :                 tenant_id = %tenant_shard_id.tenant_id,
     643            0 :                 shard_id = %tenant_shard_id.shard_slug(),
     644              :                 %timeline_id))
     645            0 :     .await?;
     646              : 
     647            0 :     json_response(StatusCode::OK, ())
     648            0 : }
     649              : 
     650            0 : async fn timeline_detail_handler(
     651            0 :     request: Request<Body>,
     652            0 :     _cancel: CancellationToken,
     653            0 : ) -> Result<Response<Body>, ApiError> {
     654            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     655            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
     656            0 :     let include_non_incremental_logical_size: Option<bool> =
     657            0 :         parse_query_param(&request, "include-non-incremental-logical-size")?;
     658            0 :     let force_await_initial_logical_size: Option<bool> =
     659            0 :         parse_query_param(&request, "force-await-initial-logical-size")?;
     660            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     661              : 
     662              :     // Logical size calculation needs downloading.
     663            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
     664              : 
     665            0 :     let timeline_info = async {
     666            0 :         let tenant = mgr::get_tenant(tenant_shard_id, true)?;
     667              : 
     668            0 :         let timeline = tenant
     669            0 :             .get_timeline(timeline_id, false)
     670            0 :             .map_err(|e| ApiError::NotFound(e.into()))?;
     671              : 
     672            0 :         let timeline_info = build_timeline_info(
     673            0 :             &timeline,
     674            0 :             include_non_incremental_logical_size.unwrap_or(false),
     675            0 :             force_await_initial_logical_size.unwrap_or(false),
     676            0 :             &ctx,
     677            0 :         )
     678            0 :         .await
     679            0 :         .context("get local timeline info")
     680            0 :         .map_err(ApiError::InternalServerError)?;
     681              : 
     682            0 :         Ok::<_, ApiError>(timeline_info)
     683            0 :     }
     684              :     .instrument(info_span!("timeline_detail",
     685              :                 tenant_id = %tenant_shard_id.tenant_id,
     686            0 :                 shard_id = %tenant_shard_id.shard_slug(),
     687              :                 %timeline_id))
     688            0 :     .await?;
     689              : 
     690            0 :     json_response(StatusCode::OK, timeline_info)
     691            0 : }
     692              : 
     693            0 : async fn get_lsn_by_timestamp_handler(
     694            0 :     request: Request<Body>,
     695            0 :     cancel: CancellationToken,
     696            0 : ) -> Result<Response<Body>, ApiError> {
     697            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     698            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     699              : 
     700            0 :     if !tenant_shard_id.is_zero() {
     701              :         // Requires SLRU contents, which are only stored on shard zero
     702            0 :         return Err(ApiError::BadRequest(anyhow!(
     703            0 :             "Size calculations are only available on shard zero"
     704            0 :         )));
     705            0 :     }
     706              : 
     707            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
     708            0 :     let timestamp_raw = must_get_query_param(&request, "timestamp")?;
     709            0 :     let timestamp = humantime::parse_rfc3339(&timestamp_raw)
     710            0 :         .with_context(|| format!("Invalid time: {:?}", timestamp_raw))
     711            0 :         .map_err(ApiError::BadRequest)?;
     712            0 :     let timestamp_pg = postgres_ffi::to_pg_timestamp(timestamp);
     713            0 : 
     714            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
     715            0 :     let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
     716            0 :     let result = timeline
     717            0 :         .find_lsn_for_timestamp(timestamp_pg, &cancel, &ctx)
     718            0 :         .await?;
     719            0 :     #[derive(serde::Serialize, Debug)]
     720              :     struct Result {
     721              :         lsn: Lsn,
     722              :         kind: &'static str,
     723              :     }
     724            0 :     let (lsn, kind) = match result {
     725            0 :         LsnForTimestamp::Present(lsn) => (lsn, "present"),
     726            0 :         LsnForTimestamp::Future(lsn) => (lsn, "future"),
     727            0 :         LsnForTimestamp::Past(lsn) => (lsn, "past"),
     728            0 :         LsnForTimestamp::NoData(lsn) => (lsn, "nodata"),
     729              :     };
     730            0 :     let result = Result { lsn, kind };
     731            0 :     tracing::info!(
     732            0 :         lsn=?result.lsn,
     733            0 :         kind=%result.kind,
     734            0 :         timestamp=%timestamp_raw,
     735            0 :         "lsn_by_timestamp finished"
     736            0 :     );
     737            0 :     json_response(StatusCode::OK, result)
     738            0 : }
     739              : 
     740            0 : async fn get_timestamp_of_lsn_handler(
     741            0 :     request: Request<Body>,
     742            0 :     _cancel: CancellationToken,
     743            0 : ) -> Result<Response<Body>, ApiError> {
     744            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     745            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     746              : 
     747            0 :     if !tenant_shard_id.is_zero() {
     748              :         // Requires SLRU contents, which are only stored on shard zero
     749            0 :         return Err(ApiError::BadRequest(anyhow!(
     750            0 :             "Size calculations are only available on shard zero"
     751            0 :         )));
     752            0 :     }
     753              : 
     754            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
     755              : 
     756            0 :     let lsn_str = must_get_query_param(&request, "lsn")?;
     757            0 :     let lsn = Lsn::from_str(&lsn_str)
     758            0 :         .with_context(|| format!("Invalid LSN: {lsn_str:?}"))
     759            0 :         .map_err(ApiError::BadRequest)?;
     760              : 
     761            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
     762            0 :     let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
     763            0 :     let result = timeline.get_timestamp_for_lsn(lsn, &ctx).await?;
     764              : 
     765            0 :     match result {
     766            0 :         Some(time) => {
     767            0 :             let time = format_rfc3339(postgres_ffi::from_pg_timestamp(time)).to_string();
     768            0 :             json_response(StatusCode::OK, time)
     769              :         }
     770            0 :         None => json_response(StatusCode::NOT_FOUND, ()),
     771              :     }
     772            0 : }
     773              : 
     774            0 : async fn tenant_attach_handler(
     775            0 :     mut request: Request<Body>,
     776            0 :     _cancel: CancellationToken,
     777            0 : ) -> Result<Response<Body>, ApiError> {
     778            0 :     let tenant_id: TenantId = parse_request_param(&request, "tenant_id")?;
     779            0 :     check_permission(&request, Some(tenant_id))?;
     780              : 
     781            0 :     let maybe_body: Option<TenantAttachRequest> = json_request_or_empty_body(&mut request).await?;
     782            0 :     let tenant_conf = match &maybe_body {
     783            0 :         Some(request) => TenantConfOpt::try_from(&*request.config).map_err(ApiError::BadRequest)?,
     784            0 :         None => TenantConfOpt::default(),
     785              :     };
     786              : 
     787            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Warn);
     788              : 
     789            0 :     info!("Handling tenant attach {tenant_id}");
     790              : 
     791            0 :     let state = get_state(&request);
     792              : 
     793            0 :     let generation = get_request_generation(state, maybe_body.as_ref().and_then(|r| r.generation))?;
     794              : 
     795            0 :     if state.remote_storage.is_none() {
     796            0 :         return Err(ApiError::BadRequest(anyhow!(
     797            0 :             "attach_tenant is not possible because pageserver was configured without remote storage"
     798            0 :         )));
     799            0 :     }
     800            0 : 
     801            0 :     let tenant_shard_id = TenantShardId::unsharded(tenant_id);
     802            0 :     let shard_params = ShardParameters::default();
     803            0 :     let location_conf = LocationConf::attached_single(tenant_conf, generation, &shard_params);
     804              : 
     805            0 :     let tenant = state
     806            0 :         .tenant_manager
     807            0 :         .upsert_location(
     808            0 :             tenant_shard_id,
     809            0 :             location_conf,
     810            0 :             None,
     811            0 :             SpawnMode::Normal,
     812            0 :             &ctx,
     813            0 :         )
     814            0 :         .await?;
     815              : 
     816            0 :     let Some(tenant) = tenant else {
     817              :         // This should never happen: indicates a bug in upsert_location
     818            0 :         return Err(ApiError::InternalServerError(anyhow::anyhow!(
     819            0 :             "Upsert succeeded but didn't return tenant!"
     820            0 :         )));
     821              :     };
     822              : 
     823              :     // We might have successfully constructed a Tenant, but it could still
     824              :     // end up in a broken state:
     825              :     if let TenantState::Broken {
     826            0 :         reason,
     827              :         backtrace: _,
     828            0 :     } = tenant.current_state()
     829              :     {
     830            0 :         return Err(ApiError::InternalServerError(anyhow::anyhow!(
     831            0 :             "Tenant state is Broken: {reason}"
     832            0 :         )));
     833            0 :     }
     834            0 : 
     835            0 :     json_response(StatusCode::ACCEPTED, ())
     836            0 : }
     837              : 
     838            0 : async fn timeline_delete_handler(
     839            0 :     request: Request<Body>,
     840            0 :     _cancel: CancellationToken,
     841            0 : ) -> Result<Response<Body>, ApiError> {
     842            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     843            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
     844            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     845              : 
     846            0 :     let state = get_state(&request);
     847              : 
     848            0 :     let tenant = state
     849            0 :         .tenant_manager
     850            0 :         .get_attached_tenant_shard(tenant_shard_id, false)
     851            0 :         .map_err(|e| {
     852            0 :             match e {
     853              :                 // GetTenantError has a built-in conversion to ApiError, but in this context we don't
     854              :                 // want to treat missing tenants as 404, to avoid ambiguity with successful deletions.
     855            0 :                 GetTenantError::NotFound(_) => ApiError::PreconditionFailed(
     856            0 :                     "Requested tenant is missing".to_string().into_boxed_str(),
     857            0 :                 ),
     858            0 :                 e => e.into(),
     859              :             }
     860            0 :         })?;
     861            0 :     tenant.wait_to_become_active(ACTIVE_TENANT_TIMEOUT).await?;
     862            0 :     tenant.delete_timeline(timeline_id).instrument(info_span!("timeline_delete", tenant_id=%tenant_shard_id.tenant_id, shard_id=%tenant_shard_id.shard_slug(), %timeline_id))
     863            0 :         .await?;
     864              : 
     865            0 :     json_response(StatusCode::ACCEPTED, ())
     866            0 : }
     867              : 
     868            0 : async fn tenant_detach_handler(
     869            0 :     request: Request<Body>,
     870            0 :     _cancel: CancellationToken,
     871            0 : ) -> Result<Response<Body>, ApiError> {
     872            0 :     let tenant_id: TenantId = parse_request_param(&request, "tenant_id")?;
     873            0 :     check_permission(&request, Some(tenant_id))?;
     874            0 :     let detach_ignored: Option<bool> = parse_query_param(&request, "detach_ignored")?;
     875              : 
     876              :     // This is a legacy API (`/location_conf` is the replacement).  It only supports unsharded tenants
     877            0 :     let tenant_shard_id = TenantShardId::unsharded(tenant_id);
     878            0 : 
     879            0 :     let state = get_state(&request);
     880            0 :     let conf = state.conf;
     881            0 :     mgr::detach_tenant(
     882            0 :         conf,
     883            0 :         tenant_shard_id,
     884            0 :         detach_ignored.unwrap_or(false),
     885            0 :         &state.deletion_queue_client,
     886            0 :     )
     887            0 :     .instrument(info_span!("tenant_detach", %tenant_id, shard_id=%tenant_shard_id.shard_slug()))
     888            0 :     .await?;
     889              : 
     890            0 :     json_response(StatusCode::OK, ())
     891            0 : }
     892              : 
     893            0 : async fn tenant_reset_handler(
     894            0 :     request: Request<Body>,
     895            0 :     _cancel: CancellationToken,
     896            0 : ) -> Result<Response<Body>, ApiError> {
     897            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     898            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     899              : 
     900            0 :     let drop_cache: Option<bool> = parse_query_param(&request, "drop_cache")?;
     901              : 
     902            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Warn);
     903            0 :     let state = get_state(&request);
     904            0 :     state
     905            0 :         .tenant_manager
     906            0 :         .reset_tenant(tenant_shard_id, drop_cache.unwrap_or(false), &ctx)
     907            0 :         .await
     908            0 :         .map_err(ApiError::InternalServerError)?;
     909              : 
     910            0 :     json_response(StatusCode::OK, ())
     911            0 : }
     912              : 
     913            0 : async fn tenant_load_handler(
     914            0 :     mut request: Request<Body>,
     915            0 :     _cancel: CancellationToken,
     916            0 : ) -> Result<Response<Body>, ApiError> {
     917            0 :     let tenant_id: TenantId = parse_request_param(&request, "tenant_id")?;
     918            0 :     check_permission(&request, Some(tenant_id))?;
     919              : 
     920            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Warn);
     921              : 
     922            0 :     let maybe_body: Option<TenantLoadRequest> = json_request_or_empty_body(&mut request).await?;
     923              : 
     924            0 :     let state = get_state(&request);
     925              : 
     926              :     // The /load request is only usable when control_plane_api is not set.  Once it is set, callers
     927              :     // should always use /attach instead.
     928            0 :     let generation = get_request_generation(state, maybe_body.as_ref().and_then(|r| r.generation))?;
     929              : 
     930            0 :     mgr::load_tenant(
     931            0 :         state.conf,
     932            0 :         tenant_id,
     933            0 :         generation,
     934            0 :         state.broker_client.clone(),
     935            0 :         state.remote_storage.clone(),
     936            0 :         state.deletion_queue_client.clone(),
     937            0 :         &ctx,
     938            0 :     )
     939            0 :     .instrument(info_span!("load", %tenant_id))
     940            0 :     .await?;
     941              : 
     942            0 :     json_response(StatusCode::ACCEPTED, ())
     943            0 : }
     944              : 
     945            0 : async fn tenant_ignore_handler(
     946            0 :     request: Request<Body>,
     947            0 :     _cancel: CancellationToken,
     948            0 : ) -> Result<Response<Body>, ApiError> {
     949            0 :     let tenant_id: TenantId = parse_request_param(&request, "tenant_id")?;
     950            0 :     check_permission(&request, Some(tenant_id))?;
     951              : 
     952            0 :     let state = get_state(&request);
     953            0 :     let conf = state.conf;
     954            0 :     mgr::ignore_tenant(conf, tenant_id)
     955            0 :         .instrument(info_span!("ignore_tenant", %tenant_id))
     956            0 :         .await?;
     957              : 
     958            0 :     json_response(StatusCode::OK, ())
     959            0 : }
     960              : 
     961            0 : async fn tenant_list_handler(
     962            0 :     request: Request<Body>,
     963            0 :     _cancel: CancellationToken,
     964            0 : ) -> Result<Response<Body>, ApiError> {
     965            0 :     check_permission(&request, None)?;
     966              : 
     967            0 :     let response_data = mgr::list_tenants()
     968            0 :         .instrument(info_span!("tenant_list"))
     969            0 :         .await
     970            0 :         .map_err(|_| {
     971            0 :             ApiError::ResourceUnavailable("Tenant map is initializing or shutting down".into())
     972            0 :         })?
     973            0 :         .iter()
     974            0 :         .map(|(id, state, gen)| TenantInfo {
     975            0 :             id: *id,
     976            0 :             state: state.clone(),
     977            0 :             current_physical_size: None,
     978            0 :             attachment_status: state.attachment_status(),
     979            0 :             generation: (*gen).into(),
     980            0 :         })
     981            0 :         .collect::<Vec<TenantInfo>>();
     982            0 : 
     983            0 :     json_response(StatusCode::OK, response_data)
     984            0 : }
     985              : 
     986            0 : async fn tenant_status(
     987            0 :     request: Request<Body>,
     988            0 :     _cancel: CancellationToken,
     989            0 : ) -> Result<Response<Body>, ApiError> {
     990            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
     991            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
     992              : 
     993            0 :     let tenant_info = async {
     994            0 :         let tenant = mgr::get_tenant(tenant_shard_id, false)?;
     995              : 
     996              :         // Calculate total physical size of all timelines
     997            0 :         let mut current_physical_size = 0;
     998            0 :         for timeline in tenant.list_timelines().iter() {
     999            0 :             current_physical_size += timeline.layer_size_sum().await;
    1000              :         }
    1001              : 
    1002            0 :         let state = tenant.current_state();
    1003            0 :         Result::<_, ApiError>::Ok(TenantDetails {
    1004            0 :             tenant_info: TenantInfo {
    1005            0 :                 id: tenant_shard_id,
    1006            0 :                 state: state.clone(),
    1007            0 :                 current_physical_size: Some(current_physical_size),
    1008            0 :                 attachment_status: state.attachment_status(),
    1009            0 :                 generation: tenant.generation().into(),
    1010            0 :             },
    1011            0 :             walredo: tenant.wal_redo_manager_status(),
    1012            0 :             timelines: tenant.list_timeline_ids(),
    1013            0 :         })
    1014            0 :     }
    1015              :     .instrument(info_span!("tenant_status_handler",
    1016              :                 tenant_id = %tenant_shard_id.tenant_id,
    1017            0 :                 shard_id = %tenant_shard_id.shard_slug()))
    1018            0 :     .await?;
    1019              : 
    1020            0 :     json_response(StatusCode::OK, tenant_info)
    1021            0 : }
    1022              : 
    1023            0 : async fn tenant_delete_handler(
    1024            0 :     request: Request<Body>,
    1025            0 :     _cancel: CancellationToken,
    1026            0 : ) -> Result<Response<Body>, ApiError> {
    1027              :     // TODO openapi spec
    1028            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1029            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1030              : 
    1031            0 :     let state = get_state(&request);
    1032            0 : 
    1033            0 :     state
    1034            0 :         .tenant_manager
    1035            0 :         .delete_tenant(tenant_shard_id, ACTIVE_TENANT_TIMEOUT)
    1036              :         .instrument(info_span!("tenant_delete_handler",
    1037              :             tenant_id = %tenant_shard_id.tenant_id,
    1038            0 :             shard_id = %tenant_shard_id.shard_slug()
    1039              :         ))
    1040            0 :         .await?;
    1041              : 
    1042            0 :     json_response(StatusCode::ACCEPTED, ())
    1043            0 : }
    1044              : 
    1045              : /// HTTP endpoint to query the current tenant_size of a tenant.
    1046              : ///
    1047              : /// This is not used by consumption metrics under [`crate::consumption_metrics`], but can be used
    1048              : /// to debug any of the calculations. Requires `tenant_id` request parameter, supports
    1049              : /// `inputs_only=true|false` (default false) which supports debugging failure to calculate model
    1050              : /// values.
    1051              : ///
    1052              : /// 'retention_period' query parameter overrides the cutoff that is used to calculate the size
    1053              : /// (only if it is shorter than the real cutoff).
    1054              : ///
    1055              : /// Note: we don't update the cached size and prometheus metric here.
    1056              : /// The retention period might be different, and it's nice to have a method to just calculate it
    1057              : /// without modifying anything anyway.
    1058            0 : async fn tenant_size_handler(
    1059            0 :     request: Request<Body>,
    1060            0 :     cancel: CancellationToken,
    1061            0 : ) -> Result<Response<Body>, ApiError> {
    1062            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1063            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1064            0 :     let inputs_only: Option<bool> = parse_query_param(&request, "inputs_only")?;
    1065            0 :     let retention_period: Option<u64> = parse_query_param(&request, "retention_period")?;
    1066            0 :     let headers = request.headers();
    1067            0 : 
    1068            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
    1069            0 :     let tenant = mgr::get_tenant(tenant_shard_id, true)?;
    1070              : 
    1071            0 :     if !tenant_shard_id.is_zero() {
    1072            0 :         return Err(ApiError::BadRequest(anyhow!(
    1073            0 :             "Size calculations are only available on shard zero"
    1074            0 :         )));
    1075            0 :     }
    1076              : 
    1077              :     // this can be long operation
    1078            0 :     let inputs = tenant
    1079            0 :         .gather_size_inputs(
    1080            0 :             retention_period,
    1081            0 :             LogicalSizeCalculationCause::TenantSizeHandler,
    1082            0 :             &cancel,
    1083            0 :             &ctx,
    1084            0 :         )
    1085            0 :         .await
    1086            0 :         .map_err(ApiError::InternalServerError)?;
    1087              : 
    1088            0 :     let mut sizes = None;
    1089            0 :     let accepts_html = headers
    1090            0 :         .get(header::ACCEPT)
    1091            0 :         .map(|v| v == "text/html")
    1092            0 :         .unwrap_or_default();
    1093            0 :     if !inputs_only.unwrap_or(false) {
    1094            0 :         let storage_model = inputs
    1095            0 :             .calculate_model()
    1096            0 :             .map_err(ApiError::InternalServerError)?;
    1097            0 :         let size = storage_model.calculate();
    1098            0 : 
    1099            0 :         // If request header expects html, return html
    1100            0 :         if accepts_html {
    1101            0 :             return synthetic_size_html_response(inputs, storage_model, size);
    1102            0 :         }
    1103            0 :         sizes = Some(size);
    1104            0 :     } else if accepts_html {
    1105            0 :         return Err(ApiError::BadRequest(anyhow!(
    1106            0 :             "inputs_only parameter is incompatible with html output request"
    1107            0 :         )));
    1108            0 :     }
    1109              : 
    1110              :     /// The type resides in the pageserver not to expose `ModelInputs`.
    1111            0 :     #[derive(serde::Serialize)]
    1112              :     struct TenantHistorySize {
    1113              :         id: TenantId,
    1114              :         /// Size is a mixture of WAL and logical size, so the unit is bytes.
    1115              :         ///
    1116              :         /// Will be none if `?inputs_only=true` was given.
    1117              :         size: Option<u64>,
    1118              :         /// Size of each segment used in the model.
    1119              :         /// Will be null if `?inputs_only=true` was given.
    1120              :         segment_sizes: Option<Vec<tenant_size_model::SegmentSizeResult>>,
    1121              :         inputs: crate::tenant::size::ModelInputs,
    1122              :     }
    1123              : 
    1124            0 :     json_response(
    1125            0 :         StatusCode::OK,
    1126            0 :         TenantHistorySize {
    1127            0 :             id: tenant_shard_id.tenant_id,
    1128            0 :             size: sizes.as_ref().map(|x| x.total_size),
    1129            0 :             segment_sizes: sizes.map(|x| x.segments),
    1130            0 :             inputs,
    1131            0 :         },
    1132            0 :     )
    1133            0 : }
    1134              : 
    1135            0 : async fn tenant_shard_split_handler(
    1136            0 :     mut request: Request<Body>,
    1137            0 :     _cancel: CancellationToken,
    1138            0 : ) -> Result<Response<Body>, ApiError> {
    1139            0 :     let req: TenantShardSplitRequest = json_request(&mut request).await?;
    1140              : 
    1141            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1142            0 :     let state = get_state(&request);
    1143            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Warn);
    1144              : 
    1145            0 :     let new_shards = state
    1146            0 :         .tenant_manager
    1147            0 :         .shard_split(tenant_shard_id, ShardCount::new(req.new_shard_count), &ctx)
    1148            0 :         .await
    1149            0 :         .map_err(ApiError::InternalServerError)?;
    1150              : 
    1151            0 :     json_response(StatusCode::OK, TenantShardSplitResponse { new_shards })
    1152            0 : }
    1153              : 
    1154            0 : async fn layer_map_info_handler(
    1155            0 :     request: Request<Body>,
    1156            0 :     _cancel: CancellationToken,
    1157            0 : ) -> Result<Response<Body>, ApiError> {
    1158            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1159            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1160            0 :     let reset: LayerAccessStatsReset =
    1161            0 :         parse_query_param(&request, "reset")?.unwrap_or(LayerAccessStatsReset::NoReset);
    1162            0 : 
    1163            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1164              : 
    1165            0 :     let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1166            0 :     let layer_map_info = timeline.layer_map_info(reset).await;
    1167              : 
    1168            0 :     json_response(StatusCode::OK, layer_map_info)
    1169            0 : }
    1170              : 
    1171            0 : async fn layer_download_handler(
    1172            0 :     request: Request<Body>,
    1173            0 :     _cancel: CancellationToken,
    1174            0 : ) -> Result<Response<Body>, ApiError> {
    1175            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1176            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1177            0 :     let layer_file_name = get_request_param(&request, "layer_file_name")?;
    1178            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1179              : 
    1180            0 :     let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1181            0 :     let downloaded = timeline
    1182            0 :         .download_layer(layer_file_name)
    1183            0 :         .await
    1184            0 :         .map_err(ApiError::InternalServerError)?;
    1185              : 
    1186            0 :     match downloaded {
    1187            0 :         Some(true) => json_response(StatusCode::OK, ()),
    1188            0 :         Some(false) => json_response(StatusCode::NOT_MODIFIED, ()),
    1189            0 :         None => json_response(
    1190            0 :             StatusCode::BAD_REQUEST,
    1191            0 :             format!("Layer {tenant_shard_id}/{timeline_id}/{layer_file_name} not found"),
    1192            0 :         ),
    1193              :     }
    1194            0 : }
    1195              : 
    1196            0 : async fn evict_timeline_layer_handler(
    1197            0 :     request: Request<Body>,
    1198            0 :     _cancel: CancellationToken,
    1199            0 : ) -> Result<Response<Body>, ApiError> {
    1200            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1201            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1202            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1203            0 :     let layer_file_name = get_request_param(&request, "layer_file_name")?;
    1204              : 
    1205            0 :     let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1206            0 :     let evicted = timeline
    1207            0 :         .evict_layer(layer_file_name)
    1208            0 :         .await
    1209            0 :         .map_err(ApiError::InternalServerError)?;
    1210              : 
    1211            0 :     match evicted {
    1212            0 :         Some(true) => json_response(StatusCode::OK, ()),
    1213            0 :         Some(false) => json_response(StatusCode::NOT_MODIFIED, ()),
    1214            0 :         None => json_response(
    1215            0 :             StatusCode::BAD_REQUEST,
    1216            0 :             format!("Layer {tenant_shard_id}/{timeline_id}/{layer_file_name} not found"),
    1217            0 :         ),
    1218              :     }
    1219            0 : }
    1220              : 
    1221              : /// Get tenant_size SVG graph along with the JSON data.
    1222            0 : fn synthetic_size_html_response(
    1223            0 :     inputs: ModelInputs,
    1224            0 :     storage_model: StorageModel,
    1225            0 :     sizes: SizeResult,
    1226            0 : ) -> Result<Response<Body>, ApiError> {
    1227            0 :     let mut timeline_ids: Vec<String> = Vec::new();
    1228            0 :     let mut timeline_map: HashMap<TimelineId, usize> = HashMap::new();
    1229            0 :     for (index, ti) in inputs.timeline_inputs.iter().enumerate() {
    1230            0 :         timeline_map.insert(ti.timeline_id, index);
    1231            0 :         timeline_ids.push(ti.timeline_id.to_string());
    1232            0 :     }
    1233            0 :     let seg_to_branch: Vec<usize> = inputs
    1234            0 :         .segments
    1235            0 :         .iter()
    1236            0 :         .map(|seg| *timeline_map.get(&seg.timeline_id).unwrap())
    1237            0 :         .collect();
    1238              : 
    1239            0 :     let svg =
    1240            0 :         tenant_size_model::svg::draw_svg(&storage_model, &timeline_ids, &seg_to_branch, &sizes)
    1241            0 :             .map_err(ApiError::InternalServerError)?;
    1242              : 
    1243            0 :     let mut response = String::new();
    1244            0 : 
    1245            0 :     use std::fmt::Write;
    1246            0 :     write!(response, "<html>\n<body>\n").unwrap();
    1247            0 :     write!(response, "<div>\n{svg}\n</div>").unwrap();
    1248            0 :     writeln!(response, "Project size: {}", sizes.total_size).unwrap();
    1249            0 :     writeln!(response, "<pre>").unwrap();
    1250            0 :     writeln!(
    1251            0 :         response,
    1252            0 :         "{}",
    1253            0 :         serde_json::to_string_pretty(&inputs).unwrap()
    1254            0 :     )
    1255            0 :     .unwrap();
    1256            0 :     writeln!(
    1257            0 :         response,
    1258            0 :         "{}",
    1259            0 :         serde_json::to_string_pretty(&sizes.segments).unwrap()
    1260            0 :     )
    1261            0 :     .unwrap();
    1262            0 :     writeln!(response, "</pre>").unwrap();
    1263            0 :     write!(response, "</body>\n</html>\n").unwrap();
    1264            0 : 
    1265            0 :     html_response(StatusCode::OK, response)
    1266            0 : }
    1267              : 
    1268            0 : pub fn html_response(status: StatusCode, data: String) -> Result<Response<Body>, ApiError> {
    1269            0 :     let response = Response::builder()
    1270            0 :         .status(status)
    1271            0 :         .header(header::CONTENT_TYPE, "text/html")
    1272            0 :         .body(Body::from(data.as_bytes().to_vec()))
    1273            0 :         .map_err(|e| ApiError::InternalServerError(e.into()))?;
    1274            0 :     Ok(response)
    1275            0 : }
    1276              : 
    1277              : /// Helper for requests that may take a generation, which is mandatory
    1278              : /// when control_plane_api is set, but otherwise defaults to Generation::none()
    1279            0 : fn get_request_generation(state: &State, req_gen: Option<u32>) -> Result<Generation, ApiError> {
    1280            0 :     if state.conf.control_plane_api.is_some() {
    1281            0 :         req_gen
    1282            0 :             .map(Generation::new)
    1283            0 :             .ok_or(ApiError::BadRequest(anyhow!(
    1284            0 :                 "generation attribute missing"
    1285            0 :             )))
    1286              :     } else {
    1287              :         // Legacy mode: all tenants operate with no generation
    1288            0 :         Ok(Generation::none())
    1289              :     }
    1290            0 : }
    1291              : 
    1292            0 : async fn tenant_create_handler(
    1293            0 :     mut request: Request<Body>,
    1294            0 :     _cancel: CancellationToken,
    1295            0 : ) -> Result<Response<Body>, ApiError> {
    1296            0 :     let request_data: TenantCreateRequest = json_request(&mut request).await?;
    1297            0 :     let target_tenant_id = request_data.new_tenant_id;
    1298            0 :     check_permission(&request, None)?;
    1299              : 
    1300            0 :     let _timer = STORAGE_TIME_GLOBAL
    1301            0 :         .get_metric_with_label_values(&[StorageTimeOperation::CreateTenant.into()])
    1302            0 :         .expect("bug")
    1303            0 :         .start_timer();
    1304              : 
    1305            0 :     let tenant_conf =
    1306            0 :         TenantConfOpt::try_from(&request_data.config).map_err(ApiError::BadRequest)?;
    1307              : 
    1308            0 :     let state = get_state(&request);
    1309              : 
    1310            0 :     let generation = get_request_generation(state, request_data.generation)?;
    1311              : 
    1312            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Warn);
    1313            0 : 
    1314            0 :     let location_conf =
    1315            0 :         LocationConf::attached_single(tenant_conf, generation, &request_data.shard_parameters);
    1316              : 
    1317            0 :     let new_tenant = state
    1318            0 :         .tenant_manager
    1319            0 :         .upsert_location(
    1320            0 :             target_tenant_id,
    1321            0 :             location_conf,
    1322            0 :             None,
    1323            0 :             SpawnMode::Create,
    1324            0 :             &ctx,
    1325            0 :         )
    1326            0 :         .await?;
    1327              : 
    1328            0 :     let Some(new_tenant) = new_tenant else {
    1329              :         // This should never happen: indicates a bug in upsert_location
    1330            0 :         return Err(ApiError::InternalServerError(anyhow::anyhow!(
    1331            0 :             "Upsert succeeded but didn't return tenant!"
    1332            0 :         )));
    1333              :     };
    1334              :     // We created the tenant. Existing API semantics are that the tenant
    1335              :     // is Active when this function returns.
    1336            0 :     new_tenant
    1337            0 :         .wait_to_become_active(ACTIVE_TENANT_TIMEOUT)
    1338            0 :         .await?;
    1339              : 
    1340            0 :     json_response(
    1341            0 :         StatusCode::CREATED,
    1342            0 :         TenantCreateResponse(new_tenant.tenant_shard_id().tenant_id),
    1343            0 :     )
    1344            0 : }
    1345              : 
    1346            0 : async fn get_tenant_config_handler(
    1347            0 :     request: Request<Body>,
    1348            0 :     _cancel: CancellationToken,
    1349            0 : ) -> Result<Response<Body>, ApiError> {
    1350            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1351            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1352              : 
    1353            0 :     let tenant = mgr::get_tenant(tenant_shard_id, false)?;
    1354              : 
    1355            0 :     let response = HashMap::from([
    1356              :         (
    1357              :             "tenant_specific_overrides",
    1358            0 :             serde_json::to_value(tenant.tenant_specific_overrides())
    1359            0 :                 .context("serializing tenant specific overrides")
    1360            0 :                 .map_err(ApiError::InternalServerError)?,
    1361              :         ),
    1362              :         (
    1363            0 :             "effective_config",
    1364            0 :             serde_json::to_value(tenant.effective_config())
    1365            0 :                 .context("serializing effective config")
    1366            0 :                 .map_err(ApiError::InternalServerError)?,
    1367              :         ),
    1368              :     ]);
    1369              : 
    1370            0 :     json_response(StatusCode::OK, response)
    1371            0 : }
    1372              : 
    1373            0 : async fn update_tenant_config_handler(
    1374            0 :     mut request: Request<Body>,
    1375            0 :     _cancel: CancellationToken,
    1376            0 : ) -> Result<Response<Body>, ApiError> {
    1377            0 :     let request_data: TenantConfigRequest = json_request(&mut request).await?;
    1378            0 :     let tenant_id = request_data.tenant_id;
    1379            0 :     check_permission(&request, Some(tenant_id))?;
    1380              : 
    1381            0 :     let tenant_conf =
    1382            0 :         TenantConfOpt::try_from(&request_data.config).map_err(ApiError::BadRequest)?;
    1383              : 
    1384            0 :     let state = get_state(&request);
    1385            0 :     mgr::set_new_tenant_config(state.conf, tenant_conf, tenant_id)
    1386            0 :         .instrument(info_span!("tenant_config", %tenant_id))
    1387            0 :         .await?;
    1388              : 
    1389            0 :     json_response(StatusCode::OK, ())
    1390            0 : }
    1391              : 
    1392            0 : async fn put_tenant_location_config_handler(
    1393            0 :     mut request: Request<Body>,
    1394            0 :     _cancel: CancellationToken,
    1395            0 : ) -> Result<Response<Body>, ApiError> {
    1396            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1397              : 
    1398            0 :     let request_data: TenantLocationConfigRequest = json_request(&mut request).await?;
    1399            0 :     let flush = parse_query_param(&request, "flush_ms")?.map(Duration::from_millis);
    1400            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1401              : 
    1402            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Warn);
    1403            0 :     let state = get_state(&request);
    1404            0 :     let conf = state.conf;
    1405            0 : 
    1406            0 :     // The `Detached` state is special, it doesn't upsert a tenant, it removes
    1407            0 :     // its local disk content and drops it from memory.
    1408            0 :     if let LocationConfigMode::Detached = request_data.config.mode {
    1409            0 :         if let Err(e) =
    1410            0 :             mgr::detach_tenant(conf, tenant_shard_id, true, &state.deletion_queue_client)
    1411              :                 .instrument(info_span!("tenant_detach",
    1412              :                     tenant_id = %tenant_shard_id.tenant_id,
    1413            0 :                     shard_id = %tenant_shard_id.shard_slug()
    1414              :                 ))
    1415            0 :                 .await
    1416              :         {
    1417            0 :             match e {
    1418            0 :                 TenantStateError::SlotError(TenantSlotError::NotFound(_)) => {
    1419            0 :                     // This API is idempotent: a NotFound on a detach is fine.
    1420            0 :                 }
    1421            0 :                 _ => return Err(e.into()),
    1422              :             }
    1423            0 :         }
    1424            0 :         return json_response(StatusCode::OK, ());
    1425            0 :     }
    1426              : 
    1427            0 :     let location_conf =
    1428            0 :         LocationConf::try_from(&request_data.config).map_err(ApiError::BadRequest)?;
    1429              : 
    1430            0 :     let attached = state
    1431            0 :         .tenant_manager
    1432            0 :         .upsert_location(
    1433            0 :             tenant_shard_id,
    1434            0 :             location_conf,
    1435            0 :             flush,
    1436            0 :             tenant::SpawnMode::Normal,
    1437            0 :             &ctx,
    1438            0 :         )
    1439            0 :         .await?
    1440            0 :         .is_some();
    1441              : 
    1442            0 :     if let Some(_flush_ms) = flush {
    1443            0 :         match state
    1444            0 :             .secondary_controller
    1445            0 :             .upload_tenant(tenant_shard_id)
    1446            0 :             .await
    1447              :         {
    1448              :             Ok(()) => {
    1449            0 :                 tracing::info!("Uploaded heatmap during flush");
    1450              :             }
    1451            0 :             Err(e) => {
    1452            0 :                 tracing::warn!("Failed to flush heatmap: {e}");
    1453              :             }
    1454              :         }
    1455              :     } else {
    1456            0 :         tracing::info!("No flush requested when configuring");
    1457              :     }
    1458              : 
    1459              :     // This API returns a vector of pageservers where the tenant is attached: this is
    1460              :     // primarily for use in the sharding service.  For compatibilty, we also return this
    1461              :     // when called directly on a pageserver, but the payload is always zero or one shards.
    1462            0 :     let mut response = TenantLocationConfigResponse { shards: Vec::new() };
    1463            0 :     if attached {
    1464            0 :         response.shards.push(TenantShardLocation {
    1465            0 :             shard_id: tenant_shard_id,
    1466            0 :             node_id: state.conf.id,
    1467            0 :         })
    1468            0 :     }
    1469              : 
    1470            0 :     json_response(StatusCode::OK, response)
    1471            0 : }
    1472              : 
    1473            0 : async fn list_location_config_handler(
    1474            0 :     request: Request<Body>,
    1475            0 :     _cancel: CancellationToken,
    1476            0 : ) -> Result<Response<Body>, ApiError> {
    1477            0 :     let state = get_state(&request);
    1478            0 :     let slots = state.tenant_manager.list();
    1479            0 :     let result = LocationConfigListResponse {
    1480            0 :         tenant_shards: slots
    1481            0 :             .into_iter()
    1482            0 :             .map(|(tenant_shard_id, slot)| {
    1483            0 :                 let v = match slot {
    1484            0 :                     TenantSlot::Attached(t) => Some(t.get_location_conf()),
    1485            0 :                     TenantSlot::Secondary(s) => Some(s.get_location_conf()),
    1486            0 :                     TenantSlot::InProgress(_) => None,
    1487              :                 };
    1488            0 :                 (tenant_shard_id, v)
    1489            0 :             })
    1490            0 :             .collect(),
    1491            0 :     };
    1492            0 :     json_response(StatusCode::OK, result)
    1493            0 : }
    1494              : 
    1495              : // Do a time travel recovery on the given tenant/tenant shard. Tenant needs to be detached
    1496              : // (from all pageservers) as it invalidates consistency assumptions.
    1497            0 : async fn tenant_time_travel_remote_storage_handler(
    1498            0 :     request: Request<Body>,
    1499            0 :     cancel: CancellationToken,
    1500            0 : ) -> Result<Response<Body>, ApiError> {
    1501            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1502              : 
    1503            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1504              : 
    1505            0 :     let timestamp_raw = must_get_query_param(&request, "travel_to")?;
    1506            0 :     let timestamp = humantime::parse_rfc3339(&timestamp_raw)
    1507            0 :         .with_context(|| format!("Invalid time for travel_to: {timestamp_raw:?}"))
    1508            0 :         .map_err(ApiError::BadRequest)?;
    1509              : 
    1510            0 :     let done_if_after_raw = must_get_query_param(&request, "done_if_after")?;
    1511            0 :     let done_if_after = humantime::parse_rfc3339(&done_if_after_raw)
    1512            0 :         .with_context(|| format!("Invalid time for done_if_after: {done_if_after_raw:?}"))
    1513            0 :         .map_err(ApiError::BadRequest)?;
    1514              : 
    1515              :     // This is just a sanity check to fend off naive wrong usages of the API:
    1516              :     // the tenant needs to be detached *everywhere*
    1517            0 :     let state = get_state(&request);
    1518            0 :     let we_manage_tenant = state.tenant_manager.manages_tenant_shard(tenant_shard_id);
    1519            0 :     if we_manage_tenant {
    1520            0 :         return Err(ApiError::BadRequest(anyhow!(
    1521            0 :             "Tenant {tenant_shard_id} is already attached at this pageserver"
    1522            0 :         )));
    1523            0 :     }
    1524              : 
    1525            0 :     let Some(storage) = state.remote_storage.as_ref() else {
    1526            0 :         return Err(ApiError::InternalServerError(anyhow::anyhow!(
    1527            0 :             "remote storage not configured, cannot run time travel"
    1528            0 :         )));
    1529              :     };
    1530              : 
    1531            0 :     if timestamp > done_if_after {
    1532            0 :         return Err(ApiError::BadRequest(anyhow!(
    1533            0 :             "The done_if_after timestamp comes before the timestamp to recover to"
    1534            0 :         )));
    1535            0 :     }
    1536              : 
    1537            0 :     tracing::info!("Issuing time travel request internally. timestamp={timestamp_raw}, done_if_after={done_if_after_raw}");
    1538              : 
    1539            0 :     remote_timeline_client::upload::time_travel_recover_tenant(
    1540            0 :         storage,
    1541            0 :         &tenant_shard_id,
    1542            0 :         timestamp,
    1543            0 :         done_if_after,
    1544            0 :         &cancel,
    1545            0 :     )
    1546            0 :     .await
    1547            0 :     .map_err(|e| match e {
    1548            0 :         TimeTravelError::BadInput(e) => {
    1549            0 :             warn!("bad input error: {e}");
    1550            0 :             ApiError::BadRequest(anyhow!("bad input error"))
    1551              :         }
    1552              :         TimeTravelError::Unimplemented => {
    1553            0 :             ApiError::BadRequest(anyhow!("unimplemented for the configured remote storage"))
    1554              :         }
    1555            0 :         TimeTravelError::Cancelled => ApiError::InternalServerError(anyhow!("cancelled")),
    1556              :         TimeTravelError::TooManyVersions => {
    1557            0 :             ApiError::InternalServerError(anyhow!("too many versions in remote storage"))
    1558              :         }
    1559            0 :         TimeTravelError::Other(e) => {
    1560            0 :             warn!("internal error: {e}");
    1561            0 :             ApiError::InternalServerError(anyhow!("internal error"))
    1562              :         }
    1563            0 :     })?;
    1564              : 
    1565            0 :     json_response(StatusCode::OK, ())
    1566            0 : }
    1567              : 
    1568              : /// Testing helper to transition a tenant to [`crate::tenant::TenantState::Broken`].
    1569            0 : async fn handle_tenant_break(
    1570            0 :     r: Request<Body>,
    1571            0 :     _cancel: CancellationToken,
    1572            0 : ) -> Result<Response<Body>, ApiError> {
    1573            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&r, "tenant_shard_id")?;
    1574              : 
    1575            0 :     let tenant = crate::tenant::mgr::get_tenant(tenant_shard_id, true)
    1576            0 :         .map_err(|_| ApiError::Conflict(String::from("no active tenant found")))?;
    1577              : 
    1578            0 :     tenant.set_broken("broken from test".to_owned()).await;
    1579              : 
    1580            0 :     json_response(StatusCode::OK, ())
    1581            0 : }
    1582              : 
    1583              : // Run GC immediately on given timeline.
    1584            0 : async fn timeline_gc_handler(
    1585            0 :     mut request: Request<Body>,
    1586            0 :     cancel: CancellationToken,
    1587            0 : ) -> Result<Response<Body>, ApiError> {
    1588            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1589            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1590            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1591              : 
    1592            0 :     let gc_req: TimelineGcRequest = json_request(&mut request).await?;
    1593              : 
    1594            0 :     let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
    1595            0 :     let wait_task_done =
    1596            0 :         mgr::immediate_gc(tenant_shard_id, timeline_id, gc_req, cancel, &ctx).await?;
    1597            0 :     let gc_result = wait_task_done
    1598            0 :         .await
    1599            0 :         .context("wait for gc task")
    1600            0 :         .map_err(ApiError::InternalServerError)?
    1601            0 :         .map_err(ApiError::InternalServerError)?;
    1602              : 
    1603            0 :     json_response(StatusCode::OK, gc_result)
    1604            0 : }
    1605              : 
    1606              : // Run compaction immediately on given timeline.
    1607            0 : async fn timeline_compact_handler(
    1608            0 :     request: Request<Body>,
    1609            0 :     cancel: CancellationToken,
    1610            0 : ) -> Result<Response<Body>, ApiError> {
    1611            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1612            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1613            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1614              : 
    1615            0 :     let mut flags = EnumSet::empty();
    1616            0 :     if Some(true) == parse_query_param::<_, bool>(&request, "force_repartition")? {
    1617            0 :         flags |= CompactFlags::ForceRepartition;
    1618            0 :     }
    1619            0 :     async {
    1620            0 :         let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
    1621            0 :         let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1622            0 :         timeline
    1623            0 :             .compact(&cancel, flags, &ctx)
    1624            0 :             .await
    1625            0 :             .map_err(|e| ApiError::InternalServerError(e.into()))?;
    1626            0 :         json_response(StatusCode::OK, ())
    1627            0 :     }
    1628            0 :     .instrument(info_span!("manual_compaction", tenant_id = %tenant_shard_id.tenant_id, shard_id = %tenant_shard_id.shard_slug(), %timeline_id))
    1629            0 :     .await
    1630            0 : }
    1631              : 
    1632              : // Run checkpoint immediately on given timeline.
    1633            0 : async fn timeline_checkpoint_handler(
    1634            0 :     request: Request<Body>,
    1635            0 :     cancel: CancellationToken,
    1636            0 : ) -> Result<Response<Body>, ApiError> {
    1637            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1638            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1639            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1640              : 
    1641            0 :     let mut flags = EnumSet::empty();
    1642            0 :     if Some(true) == parse_query_param::<_, bool>(&request, "force_repartition")? {
    1643            0 :         flags |= CompactFlags::ForceRepartition;
    1644            0 :     }
    1645            0 :     async {
    1646            0 :         let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
    1647            0 :         let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1648            0 :         timeline
    1649            0 :             .freeze_and_flush()
    1650            0 :             .await
    1651            0 :             .map_err(ApiError::InternalServerError)?;
    1652            0 :         timeline
    1653            0 :             .compact(&cancel, flags, &ctx)
    1654            0 :             .await
    1655            0 :             .map_err(|e| ApiError::InternalServerError(e.into()))?;
    1656              : 
    1657            0 :         json_response(StatusCode::OK, ())
    1658            0 :     }
    1659            0 :     .instrument(info_span!("manual_checkpoint", tenant_id = %tenant_shard_id.tenant_id, shard_id = %tenant_shard_id.shard_slug(), %timeline_id))
    1660            0 :     .await
    1661            0 : }
    1662              : 
    1663            0 : async fn timeline_download_remote_layers_handler_post(
    1664            0 :     mut request: Request<Body>,
    1665            0 :     _cancel: CancellationToken,
    1666            0 : ) -> Result<Response<Body>, ApiError> {
    1667            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1668            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1669            0 :     let body: DownloadRemoteLayersTaskSpawnRequest = json_request(&mut request).await?;
    1670            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1671              : 
    1672            0 :     let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1673            0 :     match timeline.spawn_download_all_remote_layers(body).await {
    1674            0 :         Ok(st) => json_response(StatusCode::ACCEPTED, st),
    1675            0 :         Err(st) => json_response(StatusCode::CONFLICT, st),
    1676              :     }
    1677            0 : }
    1678              : 
    1679            0 : async fn timeline_download_remote_layers_handler_get(
    1680            0 :     request: Request<Body>,
    1681            0 :     _cancel: CancellationToken,
    1682            0 : ) -> Result<Response<Body>, ApiError> {
    1683            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1684            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1685            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1686              : 
    1687            0 :     let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1688            0 :     let info = timeline
    1689            0 :         .get_download_all_remote_layers_task_info()
    1690            0 :         .context("task never started since last pageserver process start")
    1691            0 :         .map_err(|e| ApiError::NotFound(e.into()))?;
    1692            0 :     json_response(StatusCode::OK, info)
    1693            0 : }
    1694              : 
    1695            0 : async fn deletion_queue_flush(
    1696            0 :     r: Request<Body>,
    1697            0 :     cancel: CancellationToken,
    1698            0 : ) -> Result<Response<Body>, ApiError> {
    1699            0 :     let state = get_state(&r);
    1700            0 : 
    1701            0 :     if state.remote_storage.is_none() {
    1702              :         // Nothing to do if remote storage is disabled.
    1703            0 :         return json_response(StatusCode::OK, ());
    1704            0 :     }
    1705              : 
    1706            0 :     let execute = parse_query_param(&r, "execute")?.unwrap_or(false);
    1707            0 : 
    1708            0 :     let flush = async {
    1709            0 :         if execute {
    1710            0 :             state.deletion_queue_client.flush_execute().await
    1711              :         } else {
    1712            0 :             state.deletion_queue_client.flush().await
    1713              :         }
    1714            0 :     }
    1715              :     // DeletionQueueError's only case is shutting down.
    1716            0 :     .map_err(|_| ApiError::ShuttingDown);
    1717            0 : 
    1718            0 :     tokio::select! {
    1719            0 :         res = flush => {
    1720            0 :             res.map(|()| json_response(StatusCode::OK, ()))?
    1721              :         }
    1722              :         _ = cancel.cancelled() => {
    1723              :             Err(ApiError::ShuttingDown)
    1724              :         }
    1725              :     }
    1726            0 : }
    1727              : 
    1728              : /// Try if `GetPage@Lsn` is successful, useful for manual debugging.
    1729            0 : async fn getpage_at_lsn_handler(
    1730            0 :     request: Request<Body>,
    1731            0 :     _cancel: CancellationToken,
    1732            0 : ) -> Result<Response<Body>, ApiError> {
    1733            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1734            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1735            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1736              : 
    1737              :     struct Key(crate::repository::Key);
    1738              : 
    1739              :     impl std::str::FromStr for Key {
    1740              :         type Err = anyhow::Error;
    1741              : 
    1742            0 :         fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
    1743            0 :             crate::repository::Key::from_hex(s).map(Key)
    1744            0 :         }
    1745              :     }
    1746              : 
    1747            0 :     let key: Key = parse_query_param(&request, "key")?
    1748            0 :         .ok_or_else(|| ApiError::BadRequest(anyhow!("missing 'key' query parameter")))?;
    1749            0 :     let lsn: Lsn = parse_query_param(&request, "lsn")?
    1750            0 :         .ok_or_else(|| ApiError::BadRequest(anyhow!("missing 'lsn' query parameter")))?;
    1751              : 
    1752            0 :     async {
    1753            0 :         let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
    1754            0 :         let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1755              : 
    1756            0 :         let page = timeline.get(key.0, lsn, &ctx).await?;
    1757              : 
    1758            0 :         Result::<_, ApiError>::Ok(
    1759            0 :             Response::builder()
    1760            0 :                 .status(StatusCode::OK)
    1761            0 :                 .header(header::CONTENT_TYPE, "application/octet-stream")
    1762            0 :                 .body(hyper::Body::from(page))
    1763            0 :                 .unwrap(),
    1764            0 :         )
    1765            0 :     }
    1766            0 :     .instrument(info_span!("timeline_get", tenant_id = %tenant_shard_id.tenant_id, shard_id = %tenant_shard_id.shard_slug(), %timeline_id))
    1767            0 :     .await
    1768            0 : }
    1769              : 
    1770            0 : async fn timeline_collect_keyspace(
    1771            0 :     request: Request<Body>,
    1772            0 :     _cancel: CancellationToken,
    1773            0 : ) -> Result<Response<Body>, ApiError> {
    1774            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1775            0 :     let timeline_id: TimelineId = parse_request_param(&request, "timeline_id")?;
    1776            0 :     check_permission(&request, Some(tenant_shard_id.tenant_id))?;
    1777              : 
    1778            0 :     let at_lsn: Option<Lsn> = parse_query_param(&request, "at_lsn")?;
    1779              : 
    1780            0 :     async {
    1781            0 :         let ctx = RequestContext::new(TaskKind::MgmtRequest, DownloadBehavior::Download);
    1782            0 :         let timeline = active_timeline_of_active_tenant(tenant_shard_id, timeline_id).await?;
    1783            0 :         let at_lsn = at_lsn.unwrap_or_else(|| timeline.get_last_record_lsn());
    1784            0 :         let keys = timeline
    1785            0 :             .collect_keyspace(at_lsn, &ctx)
    1786            0 :             .await
    1787            0 :             .map_err(|e| ApiError::InternalServerError(e.into()))?;
    1788              : 
    1789            0 :         let res = pageserver_api::models::partitioning::Partitioning { keys, at_lsn };
    1790            0 : 
    1791            0 :         json_response(StatusCode::OK, res)
    1792            0 :     }
    1793            0 :     .instrument(info_span!("timeline_collect_keyspace", tenant_id = %tenant_shard_id.tenant_id, shard_id = %tenant_shard_id.shard_slug(), %timeline_id))
    1794            0 :     .await
    1795            0 : }
    1796              : 
    1797            0 : async fn active_timeline_of_active_tenant(
    1798            0 :     tenant_shard_id: TenantShardId,
    1799            0 :     timeline_id: TimelineId,
    1800            0 : ) -> Result<Arc<Timeline>, ApiError> {
    1801            0 :     let tenant = mgr::get_tenant(tenant_shard_id, true)?;
    1802            0 :     tenant
    1803            0 :         .get_timeline(timeline_id, true)
    1804            0 :         .map_err(|e| ApiError::NotFound(e.into()))
    1805            0 : }
    1806              : 
    1807            0 : async fn always_panic_handler(
    1808            0 :     req: Request<Body>,
    1809            0 :     _cancel: CancellationToken,
    1810            0 : ) -> Result<Response<Body>, ApiError> {
    1811            0 :     // Deliberately cause a panic to exercise the panic hook registered via std::panic::set_hook().
    1812            0 :     // For pageserver, the relevant panic hook is `tracing_panic_hook` , and the `sentry` crate's wrapper around it.
    1813            0 :     // Use catch_unwind to ensure that tokio nor hyper are distracted by our panic.
    1814            0 :     let query = req.uri().query();
    1815            0 :     let _ = std::panic::catch_unwind(|| {
    1816            0 :         panic!("unconditional panic for testing panic hook integration; request query: {query:?}")
    1817            0 :     });
    1818            0 :     json_response(StatusCode::NO_CONTENT, ())
    1819            0 : }
    1820              : 
    1821            0 : async fn disk_usage_eviction_run(
    1822            0 :     mut r: Request<Body>,
    1823            0 :     cancel: CancellationToken,
    1824            0 : ) -> Result<Response<Body>, ApiError> {
    1825            0 :     check_permission(&r, None)?;
    1826              : 
    1827            0 :     #[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
    1828              :     struct Config {
    1829              :         /// How many bytes to evict before reporting that pressure is relieved.
    1830              :         evict_bytes: u64,
    1831              : 
    1832              :         #[serde(default)]
    1833              :         eviction_order: crate::disk_usage_eviction_task::EvictionOrder,
    1834              :     }
    1835              : 
    1836            0 :     #[derive(Debug, Clone, Copy, serde::Serialize)]
    1837              :     struct Usage {
    1838              :         // remains unchanged after instantiation of the struct
    1839              :         evict_bytes: u64,
    1840              :         // updated by `add_available_bytes`
    1841              :         freed_bytes: u64,
    1842              :     }
    1843              : 
    1844              :     impl crate::disk_usage_eviction_task::Usage for Usage {
    1845            0 :         fn has_pressure(&self) -> bool {
    1846            0 :             self.evict_bytes > self.freed_bytes
    1847            0 :         }
    1848              : 
    1849            0 :         fn add_available_bytes(&mut self, bytes: u64) {
    1850            0 :             self.freed_bytes += bytes;
    1851            0 :         }
    1852              :     }
    1853              : 
    1854            0 :     let config = json_request::<Config>(&mut r).await?;
    1855              : 
    1856            0 :     let usage = Usage {
    1857            0 :         evict_bytes: config.evict_bytes,
    1858            0 :         freed_bytes: 0,
    1859            0 :     };
    1860            0 : 
    1861            0 :     let state = get_state(&r);
    1862              : 
    1863            0 :     let Some(storage) = state.remote_storage.as_ref() else {
    1864            0 :         return Err(ApiError::InternalServerError(anyhow::anyhow!(
    1865            0 :             "remote storage not configured, cannot run eviction iteration"
    1866            0 :         )));
    1867              :     };
    1868              : 
    1869            0 :     let eviction_state = state.disk_usage_eviction_state.clone();
    1870              : 
    1871            0 :     let res = crate::disk_usage_eviction_task::disk_usage_eviction_task_iteration_impl(
    1872            0 :         &eviction_state,
    1873            0 :         storage,
    1874            0 :         usage,
    1875            0 :         &state.tenant_manager,
    1876            0 :         config.eviction_order,
    1877            0 :         &cancel,
    1878            0 :     )
    1879            0 :     .await;
    1880              : 
    1881            0 :     info!(?res, "disk_usage_eviction_task_iteration_impl finished");
    1882              : 
    1883            0 :     let res = res.map_err(ApiError::InternalServerError)?;
    1884              : 
    1885            0 :     json_response(StatusCode::OK, res)
    1886            0 : }
    1887              : 
    1888            0 : async fn secondary_upload_handler(
    1889            0 :     request: Request<Body>,
    1890            0 :     _cancel: CancellationToken,
    1891            0 : ) -> Result<Response<Body>, ApiError> {
    1892            0 :     let state = get_state(&request);
    1893            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1894            0 :     state
    1895            0 :         .secondary_controller
    1896            0 :         .upload_tenant(tenant_shard_id)
    1897            0 :         .await
    1898            0 :         .map_err(ApiError::InternalServerError)?;
    1899              : 
    1900            0 :     json_response(StatusCode::OK, ())
    1901            0 : }
    1902              : 
    1903            0 : async fn secondary_download_handler(
    1904            0 :     request: Request<Body>,
    1905            0 :     _cancel: CancellationToken,
    1906            0 : ) -> Result<Response<Body>, ApiError> {
    1907            0 :     let state = get_state(&request);
    1908            0 :     let tenant_shard_id: TenantShardId = parse_request_param(&request, "tenant_shard_id")?;
    1909            0 :     state
    1910            0 :         .secondary_controller
    1911            0 :         .download_tenant(tenant_shard_id)
    1912            0 :         .await
    1913            0 :         .map_err(ApiError::InternalServerError)?;
    1914              : 
    1915            0 :     json_response(StatusCode::OK, ())
    1916            0 : }
    1917              : 
    1918            0 : async fn handler_404(_: Request<Body>) -> Result<Response<Body>, ApiError> {
    1919            0 :     json_response(
    1920            0 :         StatusCode::NOT_FOUND,
    1921            0 :         HttpErrorBody::from_msg("page not found".to_owned()),
    1922            0 :     )
    1923            0 : }
    1924              : 
    1925            0 : async fn post_tracing_event_handler(
    1926            0 :     mut r: Request<Body>,
    1927            0 :     _cancel: CancellationToken,
    1928            0 : ) -> Result<Response<Body>, ApiError> {
    1929            0 :     #[derive(Debug, serde::Deserialize)]
    1930              :     #[serde(rename_all = "lowercase")]
    1931              :     enum Level {
    1932              :         Error,
    1933              :         Warn,
    1934              :         Info,
    1935              :         Debug,
    1936              :         Trace,
    1937              :     }
    1938            0 :     #[derive(Debug, serde::Deserialize)]
    1939              :     struct Request {
    1940              :         level: Level,
    1941              :         message: String,
    1942              :     }
    1943            0 :     let body: Request = json_request(&mut r)
    1944            0 :         .await
    1945            0 :         .map_err(|_| ApiError::BadRequest(anyhow::anyhow!("invalid JSON body")))?;
    1946              : 
    1947            0 :     match body.level {
    1948            0 :         Level::Error => tracing::error!(?body.message),
    1949            0 :         Level::Warn => tracing::warn!(?body.message),
    1950            0 :         Level::Info => tracing::info!(?body.message),
    1951            0 :         Level::Debug => tracing::debug!(?body.message),
    1952            0 :         Level::Trace => tracing::trace!(?body.message),
    1953              :     }
    1954              : 
    1955            0 :     json_response(StatusCode::OK, ())
    1956            0 : }
    1957              : 
    1958            0 : async fn put_io_engine_handler(
    1959            0 :     mut r: Request<Body>,
    1960            0 :     _cancel: CancellationToken,
    1961            0 : ) -> Result<Response<Body>, ApiError> {
    1962            0 :     check_permission(&r, None)?;
    1963            0 :     let kind: crate::virtual_file::IoEngineKind = json_request(&mut r).await?;
    1964            0 :     crate::virtual_file::io_engine::set(kind);
    1965            0 :     json_response(StatusCode::OK, ())
    1966            0 : }
    1967              : 
    1968              : /// Polled by control plane.
    1969              : ///
    1970              : /// See [`crate::utilization`].
    1971            0 : async fn get_utilization(
    1972            0 :     r: Request<Body>,
    1973            0 :     _cancel: CancellationToken,
    1974            0 : ) -> Result<Response<Body>, ApiError> {
    1975            0 :     // this probably could be completely public, but lets make that change later.
    1976            0 :     check_permission(&r, None)?;
    1977              : 
    1978            0 :     let state = get_state(&r);
    1979            0 :     let mut g = state.latest_utilization.lock().await;
    1980              : 
    1981            0 :     let regenerate_every = Duration::from_secs(1);
    1982            0 :     let still_valid = g
    1983            0 :         .as_ref()
    1984            0 :         .is_some_and(|(captured_at, _)| captured_at.elapsed() < regenerate_every);
    1985            0 : 
    1986            0 :     // avoid needless statvfs calls even though those should be non-blocking fast.
    1987            0 :     // regenerate at most 1Hz to allow polling at any rate.
    1988            0 :     if !still_valid {
    1989            0 :         let path = state.conf.tenants_path();
    1990            0 :         let doc = crate::utilization::regenerate(path.as_std_path())
    1991            0 :             .map_err(ApiError::InternalServerError)?;
    1992              : 
    1993            0 :         let mut buf = Vec::new();
    1994            0 :         serde_json::to_writer(&mut buf, &doc)
    1995            0 :             .context("serialize")
    1996            0 :             .map_err(ApiError::InternalServerError)?;
    1997              : 
    1998            0 :         let body = bytes::Bytes::from(buf);
    1999            0 : 
    2000            0 :         *g = Some((std::time::Instant::now(), body));
    2001            0 :     }
    2002              : 
    2003              :     // hyper 0.14 doesn't yet have Response::clone so this is a bit of extra legwork
    2004            0 :     let cached = g.as_ref().expect("just set").1.clone();
    2005            0 : 
    2006            0 :     Response::builder()
    2007            0 :         .header(hyper::http::header::CONTENT_TYPE, "application/json")
    2008            0 :         // thought of using http date header, but that is second precision which does not give any
    2009            0 :         // debugging aid
    2010            0 :         .status(StatusCode::OK)
    2011            0 :         .body(hyper::Body::from(cached))
    2012            0 :         .context("build response")
    2013            0 :         .map_err(ApiError::InternalServerError)
    2014            0 : }
    2015              : 
    2016              : /// Common functionality of all the HTTP API handlers.
    2017              : ///
    2018              : /// - Adds a tracing span to each request (by `request_span`)
    2019              : /// - Logs the request depending on the request method (by `request_span`)
    2020              : /// - Logs the response if it was not successful (by `request_span`
    2021              : /// - Shields the handler function from async cancellations. Hyper can drop the handler
    2022              : ///   Future if the connection to the client is lost, but most of the pageserver code is
    2023              : ///   not async cancellation safe. This converts the dropped future into a graceful cancellation
    2024              : ///   request with a CancellationToken.
    2025            0 : async fn api_handler<R, H>(request: Request<Body>, handler: H) -> Result<Response<Body>, ApiError>
    2026            0 : where
    2027            0 :     R: std::future::Future<Output = Result<Response<Body>, ApiError>> + Send + 'static,
    2028            0 :     H: FnOnce(Request<Body>, CancellationToken) -> R + Send + Sync + 'static,
    2029            0 : {
    2030            0 :     // Spawn a new task to handle the request, to protect the handler from unexpected
    2031            0 :     // async cancellations. Most pageserver functions are not async cancellation safe.
    2032            0 :     // We arm a drop-guard, so that if Hyper drops the Future, we signal the task
    2033            0 :     // with the cancellation token.
    2034            0 :     let token = CancellationToken::new();
    2035            0 :     let cancel_guard = token.clone().drop_guard();
    2036            0 :     let result = request_span(request, move |r| async {
    2037            0 :         let handle = tokio::spawn(
    2038            0 :             async {
    2039            0 :                 let token_cloned = token.clone();
    2040            0 :                 let result = handler(r, token).await;
    2041            0 :                 if token_cloned.is_cancelled() {
    2042              :                     // dropguard has executed: we will never turn this result into response.
    2043              :                     //
    2044              :                     // at least temporarily do {:?} logging; these failures are rare enough but
    2045              :                     // could hide difficult errors.
    2046            0 :                     match &result {
    2047            0 :                         Ok(response) => {
    2048            0 :                             let status = response.status();
    2049            0 :                             info!(%status, "Cancelled request finished successfully")
    2050              :                         }
    2051            0 :                         Err(e) => error!("Cancelled request finished with an error: {e:?}"),
    2052              :                     }
    2053            0 :                 }
    2054              :                 // only logging for cancelled panicked request handlers is the tracing_panic_hook,
    2055              :                 // which should suffice.
    2056              :                 //
    2057              :                 // there is still a chance to lose the result due to race between
    2058              :                 // returning from here and the actual connection closing happening
    2059              :                 // before outer task gets to execute. leaving that up for #5815.
    2060            0 :                 result
    2061            0 :             }
    2062            0 :             .in_current_span(),
    2063            0 :         );
    2064            0 : 
    2065            0 :         match handle.await {
    2066              :             // TODO: never actually return Err from here, always Ok(...) so that we can log
    2067              :             // spanned errors. Call api_error_handler instead and return appropriate Body.
    2068            0 :             Ok(result) => result,
    2069            0 :             Err(e) => {
    2070            0 :                 // The handler task panicked. We have a global panic handler that logs the
    2071            0 :                 // panic with its backtrace, so no need to log that here. Only log a brief
    2072            0 :                 // message to make it clear that we returned the error to the client.
    2073            0 :                 error!("HTTP request handler task panicked: {e:#}");
    2074              : 
    2075              :                 // Don't return an Error here, because then fallback error handler that was
    2076              :                 // installed in make_router() will print the error. Instead, construct the
    2077              :                 // HTTP error response and return that.
    2078            0 :                 Ok(
    2079            0 :                     ApiError::InternalServerError(anyhow!("HTTP request handler task panicked"))
    2080            0 :                         .into_response(),
    2081            0 :                 )
    2082              :             }
    2083              :         }
    2084            0 :     })
    2085            0 :     .await;
    2086              : 
    2087            0 :     cancel_guard.disarm();
    2088            0 : 
    2089            0 :     result
    2090            0 : }
    2091              : 
    2092              : /// Like api_handler, but returns an error response if the server is built without
    2093              : /// the 'testing' feature.
    2094            0 : async fn testing_api_handler<R, H>(
    2095            0 :     desc: &str,
    2096            0 :     request: Request<Body>,
    2097            0 :     handler: H,
    2098            0 : ) -> Result<Response<Body>, ApiError>
    2099            0 : where
    2100            0 :     R: std::future::Future<Output = Result<Response<Body>, ApiError>> + Send + 'static,
    2101            0 :     H: FnOnce(Request<Body>, CancellationToken) -> R + Send + Sync + 'static,
    2102            0 : {
    2103            0 :     if cfg!(feature = "testing") {
    2104            0 :         api_handler(request, handler).await
    2105              :     } else {
    2106            0 :         std::future::ready(Err(ApiError::BadRequest(anyhow!(
    2107            0 :             "Cannot {desc} because pageserver was compiled without testing APIs",
    2108            0 :         ))))
    2109            0 :         .await
    2110              :     }
    2111            0 : }
    2112              : 
    2113            0 : pub fn make_router(
    2114            0 :     state: Arc<State>,
    2115            0 :     launch_ts: &'static LaunchTimestamp,
    2116            0 :     auth: Option<Arc<SwappableJwtAuth>>,
    2117            0 : ) -> anyhow::Result<RouterBuilder<hyper::Body, ApiError>> {
    2118            0 :     let spec = include_bytes!("openapi_spec.yml");
    2119            0 :     let mut router = attach_openapi_ui(endpoint::make_router(), spec, "/swagger.yml", "/v1/doc");
    2120            0 :     if auth.is_some() {
    2121            0 :         router = router.middleware(auth_middleware(|request| {
    2122            0 :             let state = get_state(request);
    2123            0 :             if state.allowlist_routes.contains(request.uri()) {
    2124            0 :                 None
    2125              :             } else {
    2126            0 :                 state.auth.as_deref()
    2127              :             }
    2128            0 :         }))
    2129            0 :     }
    2130              : 
    2131            0 :     router = router.middleware(
    2132            0 :         endpoint::add_response_header_middleware(
    2133            0 :             "PAGESERVER_LAUNCH_TIMESTAMP",
    2134            0 :             &launch_ts.to_string(),
    2135            0 :         )
    2136            0 :         .expect("construct launch timestamp header middleware"),
    2137            0 :     );
    2138            0 : 
    2139            0 :     Ok(router
    2140            0 :         .data(state)
    2141            0 :         .get("/v1/status", |r| api_handler(r, status_handler))
    2142            0 :         .put("/v1/failpoints", |r| {
    2143            0 :             testing_api_handler("manage failpoints", r, failpoints_handler)
    2144            0 :         })
    2145            0 :         .post("/v1/reload_auth_validation_keys", |r| {
    2146            0 :             api_handler(r, reload_auth_validation_keys_handler)
    2147            0 :         })
    2148            0 :         .get("/v1/tenant", |r| api_handler(r, tenant_list_handler))
    2149            0 :         .post("/v1/tenant", |r| api_handler(r, tenant_create_handler))
    2150            0 :         .get("/v1/tenant/:tenant_shard_id", |r| {
    2151            0 :             api_handler(r, tenant_status)
    2152            0 :         })
    2153            0 :         .delete("/v1/tenant/:tenant_shard_id", |r| {
    2154            0 :             api_handler(r, tenant_delete_handler)
    2155            0 :         })
    2156            0 :         .get("/v1/tenant/:tenant_shard_id/synthetic_size", |r| {
    2157            0 :             api_handler(r, tenant_size_handler)
    2158            0 :         })
    2159            0 :         .put("/v1/tenant/config", |r| {
    2160            0 :             api_handler(r, update_tenant_config_handler)
    2161            0 :         })
    2162            0 :         .put("/v1/tenant/:tenant_shard_id/shard_split", |r| {
    2163            0 :             api_handler(r, tenant_shard_split_handler)
    2164            0 :         })
    2165            0 :         .get("/v1/tenant/:tenant_shard_id/config", |r| {
    2166            0 :             api_handler(r, get_tenant_config_handler)
    2167            0 :         })
    2168            0 :         .put("/v1/tenant/:tenant_shard_id/location_config", |r| {
    2169            0 :             api_handler(r, put_tenant_location_config_handler)
    2170            0 :         })
    2171            0 :         .get("/v1/location_config", |r| {
    2172            0 :             api_handler(r, list_location_config_handler)
    2173            0 :         })
    2174            0 :         .put(
    2175            0 :             "/v1/tenant/:tenant_shard_id/time_travel_remote_storage",
    2176            0 :             |r| api_handler(r, tenant_time_travel_remote_storage_handler),
    2177            0 :         )
    2178            0 :         .get("/v1/tenant/:tenant_shard_id/timeline", |r| {
    2179            0 :             api_handler(r, timeline_list_handler)
    2180            0 :         })
    2181            0 :         .post("/v1/tenant/:tenant_shard_id/timeline", |r| {
    2182            0 :             api_handler(r, timeline_create_handler)
    2183            0 :         })
    2184            0 :         .post("/v1/tenant/:tenant_id/attach", |r| {
    2185            0 :             api_handler(r, tenant_attach_handler)
    2186            0 :         })
    2187            0 :         .post("/v1/tenant/:tenant_id/detach", |r| {
    2188            0 :             api_handler(r, tenant_detach_handler)
    2189            0 :         })
    2190            0 :         .post("/v1/tenant/:tenant_shard_id/reset", |r| {
    2191            0 :             api_handler(r, tenant_reset_handler)
    2192            0 :         })
    2193            0 :         .post("/v1/tenant/:tenant_id/load", |r| {
    2194            0 :             api_handler(r, tenant_load_handler)
    2195            0 :         })
    2196            0 :         .post("/v1/tenant/:tenant_id/ignore", |r| {
    2197            0 :             api_handler(r, tenant_ignore_handler)
    2198            0 :         })
    2199            0 :         .post(
    2200            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/preserve_initdb_archive",
    2201            0 :             |r| api_handler(r, timeline_preserve_initdb_handler),
    2202            0 :         )
    2203            0 :         .get("/v1/tenant/:tenant_shard_id/timeline/:timeline_id", |r| {
    2204            0 :             api_handler(r, timeline_detail_handler)
    2205            0 :         })
    2206            0 :         .get(
    2207            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/get_lsn_by_timestamp",
    2208            0 :             |r| api_handler(r, get_lsn_by_timestamp_handler),
    2209            0 :         )
    2210            0 :         .get(
    2211            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/get_timestamp_of_lsn",
    2212            0 :             |r| api_handler(r, get_timestamp_of_lsn_handler),
    2213            0 :         )
    2214            0 :         .put(
    2215            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/do_gc",
    2216            0 :             |r| api_handler(r, timeline_gc_handler),
    2217            0 :         )
    2218            0 :         .put(
    2219            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/compact",
    2220            0 :             |r| testing_api_handler("run timeline compaction", r, timeline_compact_handler),
    2221            0 :         )
    2222            0 :         .put(
    2223            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/checkpoint",
    2224            0 :             |r| testing_api_handler("run timeline checkpoint", r, timeline_checkpoint_handler),
    2225            0 :         )
    2226            0 :         .post(
    2227            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/download_remote_layers",
    2228            0 :             |r| api_handler(r, timeline_download_remote_layers_handler_post),
    2229            0 :         )
    2230            0 :         .get(
    2231            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/download_remote_layers",
    2232            0 :             |r| api_handler(r, timeline_download_remote_layers_handler_get),
    2233            0 :         )
    2234            0 :         .delete("/v1/tenant/:tenant_shard_id/timeline/:timeline_id", |r| {
    2235            0 :             api_handler(r, timeline_delete_handler)
    2236            0 :         })
    2237            0 :         .get(
    2238            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/layer",
    2239            0 :             |r| api_handler(r, layer_map_info_handler),
    2240            0 :         )
    2241            0 :         .get(
    2242            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/layer/:layer_file_name",
    2243            0 :             |r| api_handler(r, layer_download_handler),
    2244            0 :         )
    2245            0 :         .delete(
    2246            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/layer/:layer_file_name",
    2247            0 :             |r| api_handler(r, evict_timeline_layer_handler),
    2248            0 :         )
    2249            0 :         .post("/v1/tenant/:tenant_shard_id/heatmap_upload", |r| {
    2250            0 :             api_handler(r, secondary_upload_handler)
    2251            0 :         })
    2252            0 :         .put("/v1/disk_usage_eviction/run", |r| {
    2253            0 :             api_handler(r, disk_usage_eviction_run)
    2254            0 :         })
    2255            0 :         .put("/v1/deletion_queue/flush", |r| {
    2256            0 :             api_handler(r, deletion_queue_flush)
    2257            0 :         })
    2258            0 :         .post("/v1/tenant/:tenant_shard_id/secondary/download", |r| {
    2259            0 :             api_handler(r, secondary_download_handler)
    2260            0 :         })
    2261            0 :         .put("/v1/tenant/:tenant_shard_id/break", |r| {
    2262            0 :             testing_api_handler("set tenant state to broken", r, handle_tenant_break)
    2263            0 :         })
    2264            0 :         .get("/v1/panic", |r| api_handler(r, always_panic_handler))
    2265            0 :         .post("/v1/tracing/event", |r| {
    2266            0 :             testing_api_handler("emit a tracing event", r, post_tracing_event_handler)
    2267            0 :         })
    2268            0 :         .get(
    2269            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/getpage",
    2270            0 :             |r| testing_api_handler("getpage@lsn", r, getpage_at_lsn_handler),
    2271            0 :         )
    2272            0 :         .get(
    2273            0 :             "/v1/tenant/:tenant_shard_id/timeline/:timeline_id/keyspace",
    2274            0 :             |r| api_handler(r, timeline_collect_keyspace),
    2275            0 :         )
    2276            0 :         .put("/v1/io_engine", |r| api_handler(r, put_io_engine_handler))
    2277            0 :         .get("/v1/utilization", |r| api_handler(r, get_utilization))
    2278            0 :         .any(handler_404))
    2279            0 : }
        

Generated by: LCOV version 2.1-beta