LCOV - code coverage report
Current view: top level - safekeeper/src/http - routes.rs (source / functions) Coverage Total Hit
Test: b9d67f908f91f00e353a27440ba89f642a869959.info Lines: 3.1 % 478 15
Test Date: 2024-11-19 21:44:13 Functions: 1.0 % 99 1

            Line data    Source code
       1              : use hyper::{Body, Request, Response, StatusCode, Uri};
       2              : use once_cell::sync::Lazy;
       3              : use serde::{Deserialize, Serialize};
       4              : use std::collections::{HashMap, HashSet};
       5              : use std::fmt;
       6              : use std::io::Write as _;
       7              : use std::str::FromStr;
       8              : use std::sync::Arc;
       9              : use storage_broker::proto::SafekeeperTimelineInfo;
      10              : use storage_broker::proto::TenantTimelineId as ProtoTenantTimelineId;
      11              : use tokio::sync::mpsc;
      12              : use tokio::task;
      13              : use tokio_stream::wrappers::ReceiverStream;
      14              : use tokio_util::sync::CancellationToken;
      15              : use tracing::{info_span, Instrument};
      16              : use utils::failpoint_support::failpoints_handler;
      17              : use utils::http::endpoint::{prometheus_metrics_handler, request_span, ChannelWriter};
      18              : use utils::http::request::parse_query_param;
      19              : 
      20              : use postgres_ffi::WAL_SEGMENT_SIZE;
      21              : use safekeeper_api::models::{SkTimelineInfo, TimelineCopyRequest};
      22              : use safekeeper_api::models::{TimelineCreateRequest, TimelineTermBumpRequest};
      23              : use utils::{
      24              :     auth::SwappableJwtAuth,
      25              :     http::{
      26              :         endpoint::{self, auth_middleware, check_permission_with},
      27              :         error::ApiError,
      28              :         json::{json_request, json_response},
      29              :         request::{ensure_no_body, parse_request_param},
      30              :         RequestExt, RouterBuilder,
      31              :     },
      32              :     id::{NodeId, TenantId, TenantTimelineId, TimelineId},
      33              :     lsn::Lsn,
      34              : };
      35              : 
      36              : use crate::debug_dump::TimelineDigestRequest;
      37              : use crate::receive_wal::WalReceiverState;
      38              : use crate::safekeeper::Term;
      39              : use crate::safekeeper::{ServerInfo, TermLsn};
      40              : use crate::send_wal::WalSenderState;
      41              : use crate::timeline::PeerInfo;
      42              : use crate::timelines_global_map::TimelineDeleteForceResult;
      43              : use crate::GlobalTimelines;
      44              : use crate::SafeKeeperConf;
      45              : use crate::{copy_timeline, debug_dump, patch_control_file, pull_timeline};
      46              : 
      47              : #[derive(Debug, Serialize)]
      48              : struct SafekeeperStatus {
      49              :     id: NodeId,
      50              : }
      51              : 
      52              : /// Healthcheck handler.
      53            0 : async fn status_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
      54            0 :     check_permission(&request, None)?;
      55            0 :     let conf = get_conf(&request);
      56            0 :     let status = SafekeeperStatus { id: conf.my_id };
      57            0 :     json_response(StatusCode::OK, status)
      58            0 : }
      59              : 
      60            0 : fn get_conf(request: &Request<Body>) -> &SafeKeeperConf {
      61            0 :     request
      62            0 :         .data::<Arc<SafeKeeperConf>>()
      63            0 :         .expect("unknown state type")
      64            0 :         .as_ref()
      65            0 : }
      66              : 
      67              : /// Same as TermLsn, but serializes LSN using display serializer
      68              : /// in Postgres format, i.e. 0/FFFFFFFF. Used only for the API response.
      69            0 : #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
      70              : pub struct TermSwitchApiEntry {
      71              :     pub term: Term,
      72              :     pub lsn: Lsn,
      73              : }
      74              : 
      75              : impl From<TermSwitchApiEntry> for TermLsn {
      76            0 :     fn from(api_val: TermSwitchApiEntry) -> Self {
      77            0 :         TermLsn {
      78            0 :             term: api_val.term,
      79            0 :             lsn: api_val.lsn,
      80            0 :         }
      81            0 :     }
      82              : }
      83              : 
      84              : /// Augment AcceptorState with last_log_term for convenience
      85            0 : #[derive(Debug, Serialize, Deserialize)]
      86              : pub struct AcceptorStateStatus {
      87              :     pub term: Term,
      88              :     pub epoch: Term, // aka last_log_term
      89              :     pub term_history: Vec<TermSwitchApiEntry>,
      90              : }
      91              : 
      92              : /// Info about timeline on safekeeper ready for reporting.
      93            0 : #[derive(Debug, Serialize, Deserialize)]
      94              : pub struct TimelineStatus {
      95              :     pub tenant_id: TenantId,
      96              :     pub timeline_id: TimelineId,
      97              :     pub acceptor_state: AcceptorStateStatus,
      98              :     pub pg_info: ServerInfo,
      99              :     pub flush_lsn: Lsn,
     100              :     pub timeline_start_lsn: Lsn,
     101              :     pub local_start_lsn: Lsn,
     102              :     pub commit_lsn: Lsn,
     103              :     pub backup_lsn: Lsn,
     104              :     pub peer_horizon_lsn: Lsn,
     105              :     pub remote_consistent_lsn: Lsn,
     106              :     pub peers: Vec<PeerInfo>,
     107              :     pub walsenders: Vec<WalSenderState>,
     108              :     pub walreceivers: Vec<WalReceiverState>,
     109              : }
     110              : 
     111            0 : fn check_permission(request: &Request<Body>, tenant_id: Option<TenantId>) -> Result<(), ApiError> {
     112            0 :     check_permission_with(request, |claims| {
     113            0 :         crate::auth::check_permission(claims, tenant_id)
     114            0 :     })
     115            0 : }
     116              : 
     117              : /// Deactivates all timelines for the tenant and removes its data directory.
     118              : /// See `timeline_delete_handler`.
     119            0 : async fn tenant_delete_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
     120            0 :     let tenant_id = parse_request_param(&request, "tenant_id")?;
     121            0 :     let only_local = parse_query_param(&request, "only_local")?.unwrap_or(false);
     122            0 :     check_permission(&request, Some(tenant_id))?;
     123            0 :     ensure_no_body(&mut request).await?;
     124              :     // FIXME: `delete_force_all_for_tenant` can return an error for multiple different reasons;
     125              :     // Using an `InternalServerError` should be fixed when the types support it
     126            0 :     let delete_info = GlobalTimelines::delete_force_all_for_tenant(&tenant_id, only_local)
     127            0 :         .await
     128            0 :         .map_err(ApiError::InternalServerError)?;
     129            0 :     json_response(
     130            0 :         StatusCode::OK,
     131            0 :         delete_info
     132            0 :             .iter()
     133            0 :             .map(|(ttid, resp)| (format!("{}", ttid.timeline_id), *resp))
     134            0 :             .collect::<HashMap<String, TimelineDeleteForceResult>>(),
     135            0 :     )
     136            0 : }
     137              : 
     138            0 : async fn timeline_create_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
     139            0 :     let request_data: TimelineCreateRequest = json_request(&mut request).await?;
     140              : 
     141            0 :     let ttid = TenantTimelineId {
     142            0 :         tenant_id: request_data.tenant_id,
     143            0 :         timeline_id: request_data.timeline_id,
     144            0 :     };
     145            0 :     check_permission(&request, Some(ttid.tenant_id))?;
     146              : 
     147            0 :     let server_info = ServerInfo {
     148            0 :         pg_version: request_data.pg_version,
     149            0 :         system_id: request_data.system_id.unwrap_or(0),
     150            0 :         wal_seg_size: request_data.wal_seg_size.unwrap_or(WAL_SEGMENT_SIZE as u32),
     151            0 :     };
     152            0 :     let local_start_lsn = request_data.local_start_lsn.unwrap_or_else(|| {
     153            0 :         request_data
     154            0 :             .commit_lsn
     155            0 :             .segment_lsn(server_info.wal_seg_size as usize)
     156            0 :     });
     157            0 :     GlobalTimelines::create(ttid, server_info, request_data.commit_lsn, local_start_lsn)
     158            0 :         .await
     159            0 :         .map_err(ApiError::InternalServerError)?;
     160              : 
     161            0 :     json_response(StatusCode::OK, ())
     162            0 : }
     163              : 
     164              : /// List all (not deleted) timelines.
     165              : /// Note: it is possible to do the same with debug_dump.
     166            0 : async fn timeline_list_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
     167            0 :     check_permission(&request, None)?;
     168            0 :     let res: Vec<TenantTimelineId> = GlobalTimelines::get_all()
     169            0 :         .iter()
     170            0 :         .map(|tli| tli.ttid)
     171            0 :         .collect();
     172            0 :     json_response(StatusCode::OK, res)
     173            0 : }
     174              : 
     175              : /// Report info about timeline.
     176            0 : async fn timeline_status_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
     177            0 :     let ttid = TenantTimelineId::new(
     178            0 :         parse_request_param(&request, "tenant_id")?,
     179            0 :         parse_request_param(&request, "timeline_id")?,
     180              :     );
     181            0 :     check_permission(&request, Some(ttid.tenant_id))?;
     182              : 
     183            0 :     let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
     184            0 :     let (inmem, state) = tli.get_state().await;
     185            0 :     let flush_lsn = tli.get_flush_lsn().await;
     186              : 
     187            0 :     let last_log_term = state.acceptor_state.get_last_log_term(flush_lsn);
     188            0 :     let term_history = state
     189            0 :         .acceptor_state
     190            0 :         .term_history
     191            0 :         .0
     192            0 :         .into_iter()
     193            0 :         .map(|ts| TermSwitchApiEntry {
     194            0 :             term: ts.term,
     195            0 :             lsn: ts.lsn,
     196            0 :         })
     197            0 :         .collect();
     198            0 :     let acc_state = AcceptorStateStatus {
     199            0 :         term: state.acceptor_state.term,
     200            0 :         epoch: last_log_term,
     201            0 :         term_history,
     202            0 :     };
     203            0 : 
     204            0 :     let conf = get_conf(&request);
     205              :     // Note: we report in memory values which can be lost.
     206            0 :     let status = TimelineStatus {
     207            0 :         tenant_id: ttid.tenant_id,
     208            0 :         timeline_id: ttid.timeline_id,
     209            0 :         acceptor_state: acc_state,
     210            0 :         pg_info: state.server,
     211            0 :         flush_lsn,
     212            0 :         timeline_start_lsn: state.timeline_start_lsn,
     213            0 :         local_start_lsn: state.local_start_lsn,
     214            0 :         commit_lsn: inmem.commit_lsn,
     215            0 :         backup_lsn: inmem.backup_lsn,
     216            0 :         peer_horizon_lsn: inmem.peer_horizon_lsn,
     217            0 :         remote_consistent_lsn: inmem.remote_consistent_lsn,
     218            0 :         peers: tli.get_peers(conf).await,
     219            0 :         walsenders: tli.get_walsenders().get_all(),
     220            0 :         walreceivers: tli.get_walreceivers().get_all(),
     221            0 :     };
     222            0 :     json_response(StatusCode::OK, status)
     223            0 : }
     224              : 
     225              : /// Deactivates the timeline and removes its data directory.
     226            0 : async fn timeline_delete_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
     227            0 :     let ttid = TenantTimelineId::new(
     228            0 :         parse_request_param(&request, "tenant_id")?,
     229            0 :         parse_request_param(&request, "timeline_id")?,
     230              :     );
     231            0 :     let only_local = parse_query_param(&request, "only_local")?.unwrap_or(false);
     232            0 :     check_permission(&request, Some(ttid.tenant_id))?;
     233            0 :     ensure_no_body(&mut request).await?;
     234              :     // FIXME: `delete_force` can fail from both internal errors and bad requests. Add better
     235              :     // error handling here when we're able to.
     236            0 :     let resp = GlobalTimelines::delete(&ttid, only_local)
     237            0 :         .await
     238            0 :         .map_err(ApiError::InternalServerError)?;
     239            0 :     json_response(StatusCode::OK, resp)
     240            0 : }
     241              : 
     242              : /// Pull timeline from peer safekeeper instances.
     243            0 : async fn timeline_pull_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
     244            0 :     check_permission(&request, None)?;
     245              : 
     246            0 :     let data: pull_timeline::Request = json_request(&mut request).await?;
     247            0 :     let conf = get_conf(&request);
     248              : 
     249            0 :     let resp = pull_timeline::handle_request(data, conf.sk_auth_token.clone())
     250            0 :         .await
     251            0 :         .map_err(ApiError::InternalServerError)?;
     252            0 :     json_response(StatusCode::OK, resp)
     253            0 : }
     254              : 
     255              : /// Stream tar archive with all timeline data.
     256            0 : async fn timeline_snapshot_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
     257            0 :     let destination = parse_request_param(&request, "destination_id")?;
     258            0 :     let ttid = TenantTimelineId::new(
     259            0 :         parse_request_param(&request, "tenant_id")?,
     260            0 :         parse_request_param(&request, "timeline_id")?,
     261              :     );
     262            0 :     check_permission(&request, Some(ttid.tenant_id))?;
     263              : 
     264            0 :     let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
     265              : 
     266              :     // To stream the body use wrap_stream which wants Stream of Result<Bytes>,
     267              :     // so create the chan and write to it in another task.
     268            0 :     let (tx, rx) = mpsc::channel(1);
     269            0 : 
     270            0 :     let conf = get_conf(&request);
     271            0 :     task::spawn(pull_timeline::stream_snapshot(
     272            0 :         tli,
     273            0 :         conf.my_id,
     274            0 :         destination,
     275            0 :         tx,
     276            0 :     ));
     277            0 : 
     278            0 :     let rx_stream = ReceiverStream::new(rx);
     279            0 :     let body = Body::wrap_stream(rx_stream);
     280            0 : 
     281            0 :     let response = Response::builder()
     282            0 :         .status(200)
     283            0 :         .header(hyper::header::CONTENT_TYPE, "application/octet-stream")
     284            0 :         .body(body)
     285            0 :         .unwrap();
     286            0 : 
     287            0 :     Ok(response)
     288            0 : }
     289              : 
     290            0 : async fn timeline_copy_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
     291            0 :     check_permission(&request, None)?;
     292              : 
     293            0 :     let request_data: TimelineCopyRequest = json_request(&mut request).await?;
     294            0 :     let ttid = TenantTimelineId::new(
     295            0 :         parse_request_param(&request, "tenant_id")?,
     296            0 :         parse_request_param(&request, "source_timeline_id")?,
     297              :     );
     298              : 
     299            0 :     let source = GlobalTimelines::get(ttid)?;
     300              : 
     301            0 :     copy_timeline::handle_request(copy_timeline::Request{
     302            0 :         source,
     303            0 :         until_lsn: request_data.until_lsn,
     304            0 :         destination_ttid: TenantTimelineId::new(ttid.tenant_id, request_data.target_timeline_id),
     305            0 :     })
     306            0 :         .instrument(info_span!("copy_timeline", from=%ttid, to=%request_data.target_timeline_id, until_lsn=%request_data.until_lsn))
     307            0 :         .await
     308            0 :         .map_err(ApiError::InternalServerError)?;
     309              : 
     310            0 :     json_response(StatusCode::OK, ())
     311            0 : }
     312              : 
     313            0 : async fn patch_control_file_handler(
     314            0 :     mut request: Request<Body>,
     315            0 : ) -> Result<Response<Body>, ApiError> {
     316            0 :     check_permission(&request, None)?;
     317              : 
     318            0 :     let ttid = TenantTimelineId::new(
     319            0 :         parse_request_param(&request, "tenant_id")?,
     320            0 :         parse_request_param(&request, "timeline_id")?,
     321              :     );
     322              : 
     323            0 :     let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
     324              : 
     325            0 :     let patch_request: patch_control_file::Request = json_request(&mut request).await?;
     326            0 :     let response = patch_control_file::handle_request(tli, patch_request)
     327            0 :         .await
     328            0 :         .map_err(ApiError::InternalServerError)?;
     329              : 
     330            0 :     json_response(StatusCode::OK, response)
     331            0 : }
     332              : 
     333              : /// Force persist control file.
     334            0 : async fn timeline_checkpoint_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
     335            0 :     check_permission(&request, None)?;
     336              : 
     337            0 :     let ttid = TenantTimelineId::new(
     338            0 :         parse_request_param(&request, "tenant_id")?,
     339            0 :         parse_request_param(&request, "timeline_id")?,
     340              :     );
     341              : 
     342            0 :     let tli = GlobalTimelines::get(ttid)?;
     343            0 :     tli.write_shared_state()
     344            0 :         .await
     345              :         .sk
     346            0 :         .state_mut()
     347            0 :         .flush()
     348            0 :         .await
     349            0 :         .map_err(ApiError::InternalServerError)?;
     350            0 :     json_response(StatusCode::OK, ())
     351            0 : }
     352              : 
     353            0 : async fn timeline_digest_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
     354            0 :     let ttid = TenantTimelineId::new(
     355            0 :         parse_request_param(&request, "tenant_id")?,
     356            0 :         parse_request_param(&request, "timeline_id")?,
     357              :     );
     358            0 :     check_permission(&request, Some(ttid.tenant_id))?;
     359              : 
     360            0 :     let from_lsn: Option<Lsn> = parse_query_param(&request, "from_lsn")?;
     361            0 :     let until_lsn: Option<Lsn> = parse_query_param(&request, "until_lsn")?;
     362              : 
     363            0 :     let request = TimelineDigestRequest {
     364            0 :         from_lsn: from_lsn.ok_or(ApiError::BadRequest(anyhow::anyhow!(
     365            0 :             "from_lsn is required"
     366            0 :         )))?,
     367            0 :         until_lsn: until_lsn.ok_or(ApiError::BadRequest(anyhow::anyhow!(
     368            0 :             "until_lsn is required"
     369            0 :         )))?,
     370              :     };
     371              : 
     372            0 :     let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
     373            0 :     let tli = tli
     374            0 :         .wal_residence_guard()
     375            0 :         .await
     376            0 :         .map_err(ApiError::InternalServerError)?;
     377              : 
     378            0 :     let response = debug_dump::calculate_digest(&tli, request)
     379            0 :         .await
     380            0 :         .map_err(ApiError::InternalServerError)?;
     381            0 :     json_response(StatusCode::OK, response)
     382            0 : }
     383              : 
     384              : /// Unevict timeline and remove uploaded partial segment(s) from the remote storage.
     385              : /// Successfull response returns list of segments existed before the deletion.
     386              : /// Aimed for one-off usage not normally needed.
     387            0 : async fn timeline_backup_partial_reset(request: Request<Body>) -> Result<Response<Body>, ApiError> {
     388            0 :     let ttid = TenantTimelineId::new(
     389            0 :         parse_request_param(&request, "tenant_id")?,
     390            0 :         parse_request_param(&request, "timeline_id")?,
     391              :     );
     392            0 :     check_permission(&request, Some(ttid.tenant_id))?;
     393              : 
     394            0 :     let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
     395              : 
     396            0 :     let response = tli
     397            0 :         .backup_partial_reset()
     398            0 :         .await
     399            0 :         .map_err(ApiError::InternalServerError)?;
     400            0 :     json_response(StatusCode::OK, response)
     401            0 : }
     402              : 
     403              : /// Make term at least as high as one in request. If one in request is None,
     404              : /// increment current one.
     405            0 : async fn timeline_term_bump_handler(
     406            0 :     mut request: Request<Body>,
     407            0 : ) -> Result<Response<Body>, ApiError> {
     408            0 :     let ttid = TenantTimelineId::new(
     409            0 :         parse_request_param(&request, "tenant_id")?,
     410            0 :         parse_request_param(&request, "timeline_id")?,
     411              :     );
     412            0 :     check_permission(&request, Some(ttid.tenant_id))?;
     413              : 
     414            0 :     let request_data: TimelineTermBumpRequest = json_request(&mut request).await?;
     415              : 
     416            0 :     let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
     417            0 :     let response = tli
     418            0 :         .term_bump(request_data.term)
     419            0 :         .await
     420            0 :         .map_err(ApiError::InternalServerError)?;
     421              : 
     422            0 :     json_response(StatusCode::OK, response)
     423            0 : }
     424              : 
     425              : /// Used only in tests to hand craft required data.
     426            0 : async fn record_safekeeper_info(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
     427            0 :     let ttid = TenantTimelineId::new(
     428            0 :         parse_request_param(&request, "tenant_id")?,
     429            0 :         parse_request_param(&request, "timeline_id")?,
     430              :     );
     431            0 :     check_permission(&request, Some(ttid.tenant_id))?;
     432            0 :     let sk_info: SkTimelineInfo = json_request(&mut request).await?;
     433            0 :     let proto_sk_info = SafekeeperTimelineInfo {
     434            0 :         safekeeper_id: 0,
     435            0 :         tenant_timeline_id: Some(ProtoTenantTimelineId {
     436            0 :             tenant_id: ttid.tenant_id.as_ref().to_owned(),
     437            0 :             timeline_id: ttid.timeline_id.as_ref().to_owned(),
     438            0 :         }),
     439            0 :         term: sk_info.term.unwrap_or(0),
     440            0 :         last_log_term: sk_info.last_log_term.unwrap_or(0),
     441            0 :         flush_lsn: sk_info.flush_lsn.0,
     442            0 :         commit_lsn: sk_info.commit_lsn.0,
     443            0 :         remote_consistent_lsn: sk_info.remote_consistent_lsn.0,
     444            0 :         peer_horizon_lsn: sk_info.peer_horizon_lsn.0,
     445            0 :         safekeeper_connstr: sk_info.safekeeper_connstr.unwrap_or_else(|| "".to_owned()),
     446            0 :         http_connstr: sk_info.http_connstr.unwrap_or_else(|| "".to_owned()),
     447            0 :         backup_lsn: sk_info.backup_lsn.0,
     448            0 :         local_start_lsn: sk_info.local_start_lsn.0,
     449            0 :         availability_zone: None,
     450            0 :         standby_horizon: sk_info.standby_horizon.0,
     451            0 :     };
     452              : 
     453            0 :     let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
     454            0 :     tli.record_safekeeper_info(proto_sk_info)
     455            0 :         .await
     456            0 :         .map_err(ApiError::InternalServerError)?;
     457              : 
     458            0 :     json_response(StatusCode::OK, ())
     459            0 : }
     460              : 
     461            0 : fn parse_kv_str<E: fmt::Display, T: FromStr<Err = E>>(k: &str, v: &str) -> Result<T, ApiError> {
     462            0 :     v.parse()
     463            0 :         .map_err(|e| ApiError::BadRequest(anyhow::anyhow!("cannot parse {k}: {e}")))
     464            0 : }
     465              : 
     466              : /// Dump debug info about all available safekeeper state.
     467            0 : async fn dump_debug_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
     468            0 :     check_permission(&request, None)?;
     469            0 :     ensure_no_body(&mut request).await?;
     470              : 
     471            0 :     let mut dump_all: Option<bool> = None;
     472            0 :     let mut dump_control_file: Option<bool> = None;
     473            0 :     let mut dump_memory: Option<bool> = None;
     474            0 :     let mut dump_disk_content: Option<bool> = None;
     475            0 :     let mut dump_term_history: Option<bool> = None;
     476            0 :     let mut dump_wal_last_modified: Option<bool> = None;
     477            0 :     let mut tenant_id: Option<TenantId> = None;
     478            0 :     let mut timeline_id: Option<TimelineId> = None;
     479            0 : 
     480            0 :     let query = request.uri().query().unwrap_or("");
     481            0 :     let mut values = url::form_urlencoded::parse(query.as_bytes());
     482              : 
     483            0 :     for (k, v) in &mut values {
     484            0 :         match k.as_ref() {
     485            0 :             "dump_all" => dump_all = Some(parse_kv_str(&k, &v)?),
     486            0 :             "dump_control_file" => dump_control_file = Some(parse_kv_str(&k, &v)?),
     487            0 :             "dump_memory" => dump_memory = Some(parse_kv_str(&k, &v)?),
     488            0 :             "dump_disk_content" => dump_disk_content = Some(parse_kv_str(&k, &v)?),
     489            0 :             "dump_term_history" => dump_term_history = Some(parse_kv_str(&k, &v)?),
     490            0 :             "dump_wal_last_modified" => dump_wal_last_modified = Some(parse_kv_str(&k, &v)?),
     491            0 :             "tenant_id" => tenant_id = Some(parse_kv_str(&k, &v)?),
     492            0 :             "timeline_id" => timeline_id = Some(parse_kv_str(&k, &v)?),
     493            0 :             _ => Err(ApiError::BadRequest(anyhow::anyhow!(
     494            0 :                 "Unknown query parameter: {}",
     495            0 :                 k
     496            0 :             )))?,
     497              :         }
     498              :     }
     499              : 
     500            0 :     let dump_all = dump_all.unwrap_or(false);
     501            0 :     let dump_control_file = dump_control_file.unwrap_or(dump_all);
     502            0 :     let dump_memory = dump_memory.unwrap_or(dump_all);
     503            0 :     let dump_disk_content = dump_disk_content.unwrap_or(dump_all);
     504            0 :     let dump_term_history = dump_term_history.unwrap_or(true);
     505            0 :     let dump_wal_last_modified = dump_wal_last_modified.unwrap_or(dump_all);
     506            0 : 
     507            0 :     let args = debug_dump::Args {
     508            0 :         dump_all,
     509            0 :         dump_control_file,
     510            0 :         dump_memory,
     511            0 :         dump_disk_content,
     512            0 :         dump_term_history,
     513            0 :         dump_wal_last_modified,
     514            0 :         tenant_id,
     515            0 :         timeline_id,
     516            0 :     };
     517              : 
     518            0 :     let resp = debug_dump::build(args)
     519            0 :         .await
     520            0 :         .map_err(ApiError::InternalServerError)?;
     521              : 
     522            0 :     let started_at = std::time::Instant::now();
     523            0 : 
     524            0 :     let (tx, rx) = mpsc::channel(1);
     525            0 : 
     526            0 :     let body = Body::wrap_stream(ReceiverStream::new(rx));
     527            0 : 
     528            0 :     let mut writer = ChannelWriter::new(128 * 1024, tx);
     529            0 : 
     530            0 :     let response = Response::builder()
     531            0 :         .status(200)
     532            0 :         .header(hyper::header::CONTENT_TYPE, "application/octet-stream")
     533            0 :         .body(body)
     534            0 :         .unwrap();
     535              : 
     536            0 :     let span = info_span!("blocking");
     537            0 :     tokio::task::spawn_blocking(move || {
     538            0 :         let _span = span.entered();
     539            0 : 
     540            0 :         let res = serde_json::to_writer(&mut writer, &resp)
     541            0 :             .map_err(std::io::Error::from)
     542            0 :             .and_then(|_| writer.flush());
     543            0 : 
     544            0 :         match res {
     545              :             Ok(()) => {
     546            0 :                 tracing::info!(
     547            0 :                     bytes = writer.flushed_bytes(),
     548            0 :                     elapsed_ms = started_at.elapsed().as_millis(),
     549            0 :                     "responded /v1/debug_dump"
     550              :                 );
     551              :             }
     552            0 :             Err(e) => {
     553            0 :                 tracing::warn!("failed to write out /v1/debug_dump response: {e:#}");
     554              :                 // semantics of this error are quite... unclear. we want to error the stream out to
     555              :                 // abort the response to somehow notify the client that we failed.
     556              :                 //
     557              :                 // though, most likely the reason for failure is that the receiver is already gone.
     558            0 :                 drop(
     559            0 :                     writer
     560            0 :                         .tx
     561            0 :                         .blocking_send(Err(std::io::ErrorKind::BrokenPipe.into())),
     562            0 :                 );
     563              :             }
     564              :         }
     565            0 :     });
     566            0 : 
     567            0 :     Ok(response)
     568            0 : }
     569              : 
     570              : /// Safekeeper http router.
     571            0 : pub fn make_router(conf: SafeKeeperConf) -> RouterBuilder<hyper::Body, ApiError> {
     572            0 :     let mut router = endpoint::make_router();
     573            0 :     if conf.http_auth.is_some() {
     574            0 :         router = router.middleware(auth_middleware(|request| {
     575              :             #[allow(clippy::mutable_key_type)]
     576            0 :             static ALLOWLIST_ROUTES: Lazy<HashSet<Uri>> = Lazy::new(|| {
     577            0 :                 ["/v1/status", "/metrics"]
     578            0 :                     .iter()
     579            0 :                     .map(|v| v.parse().unwrap())
     580            0 :                     .collect()
     581            0 :             });
     582            0 :             if ALLOWLIST_ROUTES.contains(request.uri()) {
     583            0 :                 None
     584              :             } else {
     585              :                 // Option<Arc<SwappableJwtAuth>> is always provided as data below, hence unwrap().
     586            0 :                 request
     587            0 :                     .data::<Option<Arc<SwappableJwtAuth>>>()
     588            0 :                     .unwrap()
     589            0 :                     .as_deref()
     590              :             }
     591            0 :         }))
     592            0 :     }
     593              : 
     594              :     // NB: on any changes do not forget to update the OpenAPI spec
     595              :     // located nearby (/safekeeper/src/http/openapi_spec.yaml).
     596            0 :     let auth = conf.http_auth.clone();
     597            0 :     router
     598            0 :         .data(Arc::new(conf))
     599            0 :         .data(auth)
     600            0 :         .get("/metrics", |r| request_span(r, prometheus_metrics_handler))
     601            0 :         .get("/v1/status", |r| request_span(r, status_handler))
     602            0 :         .put("/v1/failpoints", |r| {
     603            0 :             request_span(r, move |r| async {
     604            0 :                 check_permission(&r, None)?;
     605            0 :                 let cancel = CancellationToken::new();
     606            0 :                 failpoints_handler(r, cancel).await
     607            0 :             })
     608            0 :         })
     609            0 :         .delete("/v1/tenant/:tenant_id", |r| {
     610            0 :             request_span(r, tenant_delete_handler)
     611            0 :         })
     612            0 :         // Will be used in the future instead of implicit timeline creation
     613            0 :         .post("/v1/tenant/timeline", |r| {
     614            0 :             request_span(r, timeline_create_handler)
     615            0 :         })
     616            0 :         .get("/v1/tenant/timeline", |r| {
     617            0 :             request_span(r, timeline_list_handler)
     618            0 :         })
     619            0 :         .get("/v1/tenant/:tenant_id/timeline/:timeline_id", |r| {
     620            0 :             request_span(r, timeline_status_handler)
     621            0 :         })
     622            0 :         .delete("/v1/tenant/:tenant_id/timeline/:timeline_id", |r| {
     623            0 :             request_span(r, timeline_delete_handler)
     624            0 :         })
     625            0 :         .post("/v1/pull_timeline", |r| {
     626            0 :             request_span(r, timeline_pull_handler)
     627            0 :         })
     628            0 :         .get(
     629            0 :             "/v1/tenant/:tenant_id/timeline/:timeline_id/snapshot/:destination_id",
     630            0 :             |r| request_span(r, timeline_snapshot_handler),
     631            0 :         )
     632            0 :         .post(
     633            0 :             "/v1/tenant/:tenant_id/timeline/:source_timeline_id/copy",
     634            0 :             |r| request_span(r, timeline_copy_handler),
     635            0 :         )
     636            0 :         .patch(
     637            0 :             "/v1/tenant/:tenant_id/timeline/:timeline_id/control_file",
     638            0 :             |r| request_span(r, patch_control_file_handler),
     639            0 :         )
     640            0 :         .post(
     641            0 :             "/v1/tenant/:tenant_id/timeline/:timeline_id/checkpoint",
     642            0 :             |r| request_span(r, timeline_checkpoint_handler),
     643            0 :         )
     644            0 :         .get("/v1/tenant/:tenant_id/timeline/:timeline_id/digest", |r| {
     645            0 :             request_span(r, timeline_digest_handler)
     646            0 :         })
     647            0 :         .post(
     648            0 :             "/v1/tenant/:tenant_id/timeline/:timeline_id/backup_partial_reset",
     649            0 :             |r| request_span(r, timeline_backup_partial_reset),
     650            0 :         )
     651            0 :         .post(
     652            0 :             "/v1/tenant/:tenant_id/timeline/:timeline_id/term_bump",
     653            0 :             |r| request_span(r, timeline_term_bump_handler),
     654            0 :         )
     655            0 :         .post("/v1/record_safekeeper_info/:tenant_id/:timeline_id", |r| {
     656            0 :             request_span(r, record_safekeeper_info)
     657            0 :         })
     658            0 :         .get("/v1/debug_dump", |r| request_span(r, dump_debug_handler))
     659            0 : }
     660              : 
     661              : #[cfg(test)]
     662              : mod tests {
     663              :     use super::*;
     664              : 
     665              :     #[test]
     666            1 :     fn test_term_switch_entry_api_serialize() {
     667            1 :         let state = AcceptorStateStatus {
     668            1 :             term: 1,
     669            1 :             epoch: 1,
     670            1 :             term_history: vec![TermSwitchApiEntry {
     671            1 :                 term: 1,
     672            1 :                 lsn: Lsn(0x16FFDDDD),
     673            1 :             }],
     674            1 :         };
     675            1 :         let json = serde_json::to_string(&state).unwrap();
     676            1 :         assert_eq!(
     677            1 :             json,
     678            1 :             "{\"term\":1,\"epoch\":1,\"term_history\":[{\"term\":1,\"lsn\":\"0/16FFDDDD\"}]}"
     679            1 :         );
     680            1 :     }
     681              : }
        

Generated by: LCOV version 2.1-beta