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::TimelineCreateRequest;
22 : use safekeeper_api::models::{SkTimelineInfo, TimelineCopyRequest};
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 : /// List all (not deleted) timelines.
118 0 : async fn timeline_list_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
119 0 : check_permission(&request, None)?;
120 0 : let res: Vec<TenantTimelineId> = GlobalTimelines::get_all()
121 0 : .iter()
122 0 : .map(|tli| tli.ttid)
123 0 : .collect();
124 0 : json_response(StatusCode::OK, res)
125 0 : }
126 :
127 : /// Report info about timeline.
128 0 : async fn timeline_status_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
129 0 : let ttid = TenantTimelineId::new(
130 0 : parse_request_param(&request, "tenant_id")?,
131 0 : parse_request_param(&request, "timeline_id")?,
132 : );
133 0 : check_permission(&request, Some(ttid.tenant_id))?;
134 :
135 0 : let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
136 0 : let (inmem, state) = tli.get_state().await;
137 0 : let flush_lsn = tli.get_flush_lsn().await;
138 :
139 0 : let last_log_term = state.acceptor_state.get_last_log_term(flush_lsn);
140 0 : let term_history = state
141 0 : .acceptor_state
142 0 : .term_history
143 0 : .0
144 0 : .into_iter()
145 0 : .map(|ts| TermSwitchApiEntry {
146 0 : term: ts.term,
147 0 : lsn: ts.lsn,
148 0 : })
149 0 : .collect();
150 0 : let acc_state = AcceptorStateStatus {
151 0 : term: state.acceptor_state.term,
152 0 : epoch: last_log_term,
153 0 : term_history,
154 0 : };
155 0 :
156 0 : let conf = get_conf(&request);
157 : // Note: we report in memory values which can be lost.
158 0 : let status = TimelineStatus {
159 0 : tenant_id: ttid.tenant_id,
160 0 : timeline_id: ttid.timeline_id,
161 0 : acceptor_state: acc_state,
162 0 : pg_info: state.server,
163 0 : flush_lsn,
164 0 : timeline_start_lsn: state.timeline_start_lsn,
165 0 : local_start_lsn: state.local_start_lsn,
166 0 : commit_lsn: inmem.commit_lsn,
167 0 : backup_lsn: inmem.backup_lsn,
168 0 : peer_horizon_lsn: inmem.peer_horizon_lsn,
169 0 : remote_consistent_lsn: inmem.remote_consistent_lsn,
170 0 : peers: tli.get_peers(conf).await,
171 0 : walsenders: tli.get_walsenders().get_all(),
172 0 : walreceivers: tli.get_walreceivers().get_all(),
173 0 : };
174 0 : json_response(StatusCode::OK, status)
175 0 : }
176 :
177 0 : async fn timeline_create_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
178 0 : let request_data: TimelineCreateRequest = json_request(&mut request).await?;
179 :
180 0 : let ttid = TenantTimelineId {
181 0 : tenant_id: request_data.tenant_id,
182 0 : timeline_id: request_data.timeline_id,
183 0 : };
184 0 : check_permission(&request, Some(ttid.tenant_id))?;
185 :
186 0 : let server_info = ServerInfo {
187 0 : pg_version: request_data.pg_version,
188 0 : system_id: request_data.system_id.unwrap_or(0),
189 0 : wal_seg_size: request_data.wal_seg_size.unwrap_or(WAL_SEGMENT_SIZE as u32),
190 0 : };
191 0 : let local_start_lsn = request_data.local_start_lsn.unwrap_or_else(|| {
192 0 : request_data
193 0 : .commit_lsn
194 0 : .segment_lsn(server_info.wal_seg_size as usize)
195 0 : });
196 0 : GlobalTimelines::create(ttid, server_info, request_data.commit_lsn, local_start_lsn)
197 0 : .await
198 0 : .map_err(ApiError::InternalServerError)?;
199 :
200 0 : json_response(StatusCode::OK, ())
201 0 : }
202 :
203 : /// Pull timeline from peer safekeeper instances.
204 0 : async fn timeline_pull_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
205 0 : check_permission(&request, None)?;
206 :
207 0 : let data: pull_timeline::Request = json_request(&mut request).await?;
208 0 : let conf = get_conf(&request);
209 :
210 0 : let resp = pull_timeline::handle_request(data, conf.sk_auth_token.clone())
211 0 : .await
212 0 : .map_err(ApiError::InternalServerError)?;
213 0 : json_response(StatusCode::OK, resp)
214 0 : }
215 :
216 : /// Stream tar archive with all timeline data.
217 0 : async fn timeline_snapshot_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
218 0 : let destination = parse_request_param(&request, "destination_id")?;
219 0 : let ttid = TenantTimelineId::new(
220 0 : parse_request_param(&request, "tenant_id")?,
221 0 : parse_request_param(&request, "timeline_id")?,
222 : );
223 0 : check_permission(&request, Some(ttid.tenant_id))?;
224 :
225 0 : let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
226 : // Note: with evicted timelines it should work better then de-evict them and
227 : // stream; probably start_snapshot would copy partial s3 file to dest path
228 : // and stream control file, or return WalResidentTimeline if timeline is not
229 : // evicted.
230 0 : let tli = tli
231 0 : .wal_residence_guard()
232 0 : .await
233 0 : .map_err(ApiError::InternalServerError)?;
234 :
235 : // To stream the body use wrap_stream which wants Stream of Result<Bytes>,
236 : // so create the chan and write to it in another task.
237 0 : let (tx, rx) = mpsc::channel(1);
238 0 :
239 0 : let conf = get_conf(&request);
240 0 : task::spawn(pull_timeline::stream_snapshot(
241 0 : tli,
242 0 : conf.my_id,
243 0 : destination,
244 0 : tx,
245 0 : ));
246 0 :
247 0 : let rx_stream = ReceiverStream::new(rx);
248 0 : let body = Body::wrap_stream(rx_stream);
249 0 :
250 0 : let response = Response::builder()
251 0 : .status(200)
252 0 : .header(hyper::header::CONTENT_TYPE, "application/octet-stream")
253 0 : .body(body)
254 0 : .unwrap();
255 0 :
256 0 : Ok(response)
257 0 : }
258 :
259 0 : async fn timeline_copy_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
260 0 : check_permission(&request, None)?;
261 :
262 0 : let request_data: TimelineCopyRequest = json_request(&mut request).await?;
263 0 : let ttid = TenantTimelineId::new(
264 0 : parse_request_param(&request, "tenant_id")?,
265 0 : parse_request_param(&request, "source_timeline_id")?,
266 : );
267 :
268 0 : let source = GlobalTimelines::get(ttid)?;
269 :
270 0 : copy_timeline::handle_request(copy_timeline::Request{
271 0 : source,
272 0 : until_lsn: request_data.until_lsn,
273 0 : destination_ttid: TenantTimelineId::new(ttid.tenant_id, request_data.target_timeline_id),
274 0 : })
275 0 : .instrument(info_span!("copy_timeline", from=%ttid, to=%request_data.target_timeline_id, until_lsn=%request_data.until_lsn))
276 0 : .await
277 0 : .map_err(ApiError::InternalServerError)?;
278 :
279 0 : json_response(StatusCode::OK, ())
280 0 : }
281 :
282 0 : async fn timeline_digest_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
283 0 : let ttid = TenantTimelineId::new(
284 0 : parse_request_param(&request, "tenant_id")?,
285 0 : parse_request_param(&request, "timeline_id")?,
286 : );
287 0 : check_permission(&request, Some(ttid.tenant_id))?;
288 :
289 0 : let from_lsn: Option<Lsn> = parse_query_param(&request, "from_lsn")?;
290 0 : let until_lsn: Option<Lsn> = parse_query_param(&request, "until_lsn")?;
291 :
292 0 : let request = TimelineDigestRequest {
293 0 : from_lsn: from_lsn.ok_or(ApiError::BadRequest(anyhow::anyhow!(
294 0 : "from_lsn is required"
295 0 : )))?,
296 0 : until_lsn: until_lsn.ok_or(ApiError::BadRequest(anyhow::anyhow!(
297 0 : "until_lsn is required"
298 0 : )))?,
299 : };
300 :
301 0 : let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
302 0 : let tli = tli
303 0 : .wal_residence_guard()
304 0 : .await
305 0 : .map_err(ApiError::InternalServerError)?;
306 :
307 0 : let response = debug_dump::calculate_digest(&tli, request)
308 0 : .await
309 0 : .map_err(ApiError::InternalServerError)?;
310 0 : json_response(StatusCode::OK, response)
311 0 : }
312 :
313 : /// Force persist control file.
314 0 : async fn timeline_checkpoint_handler(request: Request<Body>) -> Result<Response<Body>, ApiError> {
315 0 : check_permission(&request, None)?;
316 :
317 0 : let ttid = TenantTimelineId::new(
318 0 : parse_request_param(&request, "tenant_id")?,
319 0 : parse_request_param(&request, "timeline_id")?,
320 : );
321 :
322 0 : let tli = GlobalTimelines::get(ttid)?;
323 0 : tli.write_shared_state()
324 0 : .await
325 : .sk
326 0 : .state_mut()
327 0 : .flush()
328 0 : .await
329 0 : .map_err(ApiError::InternalServerError)?;
330 0 : json_response(StatusCode::OK, ())
331 0 : }
332 :
333 : /// Deactivates the timeline and removes its data directory.
334 0 : async fn timeline_delete_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
335 0 : let ttid = TenantTimelineId::new(
336 0 : parse_request_param(&request, "tenant_id")?,
337 0 : parse_request_param(&request, "timeline_id")?,
338 : );
339 0 : let only_local = parse_query_param(&request, "only_local")?.unwrap_or(false);
340 0 : check_permission(&request, Some(ttid.tenant_id))?;
341 0 : ensure_no_body(&mut request).await?;
342 : // FIXME: `delete_force` can fail from both internal errors and bad requests. Add better
343 : // error handling here when we're able to.
344 0 : let resp = GlobalTimelines::delete(&ttid, only_local)
345 0 : .await
346 0 : .map_err(ApiError::InternalServerError)?;
347 0 : json_response(StatusCode::OK, resp)
348 0 : }
349 :
350 : /// Deactivates all timelines for the tenant and removes its data directory.
351 : /// See `timeline_delete_handler`.
352 0 : async fn tenant_delete_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
353 0 : let tenant_id = parse_request_param(&request, "tenant_id")?;
354 0 : let only_local = parse_query_param(&request, "only_local")?.unwrap_or(false);
355 0 : check_permission(&request, Some(tenant_id))?;
356 0 : ensure_no_body(&mut request).await?;
357 : // FIXME: `delete_force_all_for_tenant` can return an error for multiple different reasons;
358 : // Using an `InternalServerError` should be fixed when the types support it
359 0 : let delete_info = GlobalTimelines::delete_force_all_for_tenant(&tenant_id, only_local)
360 0 : .await
361 0 : .map_err(ApiError::InternalServerError)?;
362 0 : json_response(
363 0 : StatusCode::OK,
364 0 : delete_info
365 0 : .iter()
366 0 : .map(|(ttid, resp)| (format!("{}", ttid.timeline_id), *resp))
367 0 : .collect::<HashMap<String, TimelineDeleteForceResult>>(),
368 0 : )
369 0 : }
370 :
371 : /// Used only in tests to hand craft required data.
372 0 : async fn record_safekeeper_info(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
373 0 : let ttid = TenantTimelineId::new(
374 0 : parse_request_param(&request, "tenant_id")?,
375 0 : parse_request_param(&request, "timeline_id")?,
376 : );
377 0 : check_permission(&request, Some(ttid.tenant_id))?;
378 0 : let sk_info: SkTimelineInfo = json_request(&mut request).await?;
379 0 : let proto_sk_info = SafekeeperTimelineInfo {
380 0 : safekeeper_id: 0,
381 0 : tenant_timeline_id: Some(ProtoTenantTimelineId {
382 0 : tenant_id: ttid.tenant_id.as_ref().to_owned(),
383 0 : timeline_id: ttid.timeline_id.as_ref().to_owned(),
384 0 : }),
385 0 : term: sk_info.term.unwrap_or(0),
386 0 : last_log_term: sk_info.last_log_term.unwrap_or(0),
387 0 : flush_lsn: sk_info.flush_lsn.0,
388 0 : commit_lsn: sk_info.commit_lsn.0,
389 0 : remote_consistent_lsn: sk_info.remote_consistent_lsn.0,
390 0 : peer_horizon_lsn: sk_info.peer_horizon_lsn.0,
391 0 : safekeeper_connstr: sk_info.safekeeper_connstr.unwrap_or_else(|| "".to_owned()),
392 0 : http_connstr: sk_info.http_connstr.unwrap_or_else(|| "".to_owned()),
393 0 : backup_lsn: sk_info.backup_lsn.0,
394 0 : local_start_lsn: sk_info.local_start_lsn.0,
395 0 : availability_zone: None,
396 0 : standby_horizon: sk_info.standby_horizon.0,
397 0 : };
398 :
399 0 : let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
400 0 : tli.record_safekeeper_info(proto_sk_info)
401 0 : .await
402 0 : .map_err(ApiError::InternalServerError)?;
403 :
404 0 : json_response(StatusCode::OK, ())
405 0 : }
406 :
407 0 : fn parse_kv_str<E: fmt::Display, T: FromStr<Err = E>>(k: &str, v: &str) -> Result<T, ApiError> {
408 0 : v.parse()
409 0 : .map_err(|e| ApiError::BadRequest(anyhow::anyhow!("cannot parse {k}: {e}")))
410 0 : }
411 :
412 : /// Dump debug info about all available safekeeper state.
413 0 : async fn dump_debug_handler(mut request: Request<Body>) -> Result<Response<Body>, ApiError> {
414 0 : check_permission(&request, None)?;
415 0 : ensure_no_body(&mut request).await?;
416 :
417 0 : let mut dump_all: Option<bool> = None;
418 0 : let mut dump_control_file: Option<bool> = None;
419 0 : let mut dump_memory: Option<bool> = None;
420 0 : let mut dump_disk_content: Option<bool> = None;
421 0 : let mut dump_term_history: Option<bool> = None;
422 0 : let mut tenant_id: Option<TenantId> = None;
423 0 : let mut timeline_id: Option<TimelineId> = None;
424 0 :
425 0 : let query = request.uri().query().unwrap_or("");
426 0 : let mut values = url::form_urlencoded::parse(query.as_bytes());
427 :
428 0 : for (k, v) in &mut values {
429 0 : match k.as_ref() {
430 0 : "dump_all" => dump_all = Some(parse_kv_str(&k, &v)?),
431 0 : "dump_control_file" => dump_control_file = Some(parse_kv_str(&k, &v)?),
432 0 : "dump_memory" => dump_memory = Some(parse_kv_str(&k, &v)?),
433 0 : "dump_disk_content" => dump_disk_content = Some(parse_kv_str(&k, &v)?),
434 0 : "dump_term_history" => dump_term_history = Some(parse_kv_str(&k, &v)?),
435 0 : "tenant_id" => tenant_id = Some(parse_kv_str(&k, &v)?),
436 0 : "timeline_id" => timeline_id = Some(parse_kv_str(&k, &v)?),
437 0 : _ => Err(ApiError::BadRequest(anyhow::anyhow!(
438 0 : "Unknown query parameter: {}",
439 0 : k
440 0 : )))?,
441 : }
442 : }
443 :
444 0 : let dump_all = dump_all.unwrap_or(false);
445 0 : let dump_control_file = dump_control_file.unwrap_or(dump_all);
446 0 : let dump_memory = dump_memory.unwrap_or(dump_all);
447 0 : let dump_disk_content = dump_disk_content.unwrap_or(dump_all);
448 0 : let dump_term_history = dump_term_history.unwrap_or(true);
449 0 :
450 0 : let args = debug_dump::Args {
451 0 : dump_all,
452 0 : dump_control_file,
453 0 : dump_memory,
454 0 : dump_disk_content,
455 0 : dump_term_history,
456 0 : tenant_id,
457 0 : timeline_id,
458 0 : };
459 :
460 0 : let resp = debug_dump::build(args)
461 0 : .await
462 0 : .map_err(ApiError::InternalServerError)?;
463 :
464 0 : let started_at = std::time::Instant::now();
465 0 :
466 0 : let (tx, rx) = mpsc::channel(1);
467 0 :
468 0 : let body = Body::wrap_stream(ReceiverStream::new(rx));
469 0 :
470 0 : let mut writer = ChannelWriter::new(128 * 1024, tx);
471 0 :
472 0 : let response = Response::builder()
473 0 : .status(200)
474 0 : .header(hyper::header::CONTENT_TYPE, "application/octet-stream")
475 0 : .body(body)
476 0 : .unwrap();
477 :
478 0 : let span = info_span!("blocking");
479 0 : tokio::task::spawn_blocking(move || {
480 0 : let _span = span.entered();
481 0 :
482 0 : let res = serde_json::to_writer(&mut writer, &resp)
483 0 : .map_err(std::io::Error::from)
484 0 : .and_then(|_| writer.flush());
485 0 :
486 0 : match res {
487 : Ok(()) => {
488 0 : tracing::info!(
489 0 : bytes = writer.flushed_bytes(),
490 0 : elapsed_ms = started_at.elapsed().as_millis(),
491 0 : "responded /v1/debug_dump"
492 : );
493 : }
494 0 : Err(e) => {
495 0 : tracing::warn!("failed to write out /v1/debug_dump response: {e:#}");
496 : // semantics of this error are quite... unclear. we want to error the stream out to
497 : // abort the response to somehow notify the client that we failed.
498 : //
499 : // though, most likely the reason for failure is that the receiver is already gone.
500 0 : drop(
501 0 : writer
502 0 : .tx
503 0 : .blocking_send(Err(std::io::ErrorKind::BrokenPipe.into())),
504 0 : );
505 : }
506 : }
507 0 : });
508 0 :
509 0 : Ok(response)
510 0 : }
511 :
512 0 : async fn patch_control_file_handler(
513 0 : mut request: Request<Body>,
514 0 : ) -> Result<Response<Body>, ApiError> {
515 0 : check_permission(&request, None)?;
516 :
517 0 : let ttid = TenantTimelineId::new(
518 0 : parse_request_param(&request, "tenant_id")?,
519 0 : parse_request_param(&request, "timeline_id")?,
520 : );
521 :
522 0 : let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
523 :
524 0 : let patch_request: patch_control_file::Request = json_request(&mut request).await?;
525 0 : let response = patch_control_file::handle_request(tli, patch_request)
526 0 : .await
527 0 : .map_err(ApiError::InternalServerError)?;
528 :
529 0 : json_response(StatusCode::OK, response)
530 0 : }
531 :
532 : /// Safekeeper http router.
533 0 : pub fn make_router(conf: SafeKeeperConf) -> RouterBuilder<hyper::Body, ApiError> {
534 0 : let mut router = endpoint::make_router();
535 0 : if conf.http_auth.is_some() {
536 0 : router = router.middleware(auth_middleware(|request| {
537 0 : #[allow(clippy::mutable_key_type)]
538 0 : static ALLOWLIST_ROUTES: Lazy<HashSet<Uri>> = Lazy::new(|| {
539 0 : ["/v1/status", "/metrics"]
540 0 : .iter()
541 0 : .map(|v| v.parse().unwrap())
542 0 : .collect()
543 0 : });
544 0 : if ALLOWLIST_ROUTES.contains(request.uri()) {
545 0 : None
546 : } else {
547 : // Option<Arc<SwappableJwtAuth>> is always provided as data below, hence unwrap().
548 0 : request
549 0 : .data::<Option<Arc<SwappableJwtAuth>>>()
550 0 : .unwrap()
551 0 : .as_deref()
552 : }
553 0 : }))
554 0 : }
555 :
556 : // NB: on any changes do not forget to update the OpenAPI spec
557 : // located nearby (/safekeeper/src/http/openapi_spec.yaml).
558 0 : let auth = conf.http_auth.clone();
559 0 : router
560 0 : .data(Arc::new(conf))
561 0 : .data(auth)
562 0 : .get("/metrics", |r| request_span(r, prometheus_metrics_handler))
563 0 : .get("/v1/status", |r| request_span(r, status_handler))
564 0 : .put("/v1/failpoints", |r| {
565 0 : request_span(r, move |r| async {
566 0 : check_permission(&r, None)?;
567 0 : let cancel = CancellationToken::new();
568 0 : failpoints_handler(r, cancel).await
569 0 : })
570 0 : })
571 0 : // Will be used in the future instead of implicit timeline creation
572 0 : .post("/v1/tenant/timeline", |r| {
573 0 : request_span(r, timeline_create_handler)
574 0 : })
575 0 : .get("/v1/tenant/timeline", |r| {
576 0 : request_span(r, timeline_list_handler)
577 0 : })
578 0 : .get("/v1/tenant/:tenant_id/timeline/:timeline_id", |r| {
579 0 : request_span(r, timeline_status_handler)
580 0 : })
581 0 : .delete("/v1/tenant/:tenant_id/timeline/:timeline_id", |r| {
582 0 : request_span(r, timeline_delete_handler)
583 0 : })
584 0 : .delete("/v1/tenant/:tenant_id", |r| {
585 0 : request_span(r, tenant_delete_handler)
586 0 : })
587 0 : .get(
588 0 : "/v1/tenant/:tenant_id/timeline/:timeline_id/snapshot/:destination_id",
589 0 : |r| request_span(r, timeline_snapshot_handler),
590 0 : )
591 0 : .post("/v1/pull_timeline", |r| {
592 0 : request_span(r, timeline_pull_handler)
593 0 : })
594 0 : .post(
595 0 : "/v1/tenant/:tenant_id/timeline/:source_timeline_id/copy",
596 0 : |r| request_span(r, timeline_copy_handler),
597 0 : )
598 0 : .patch(
599 0 : "/v1/tenant/:tenant_id/timeline/:timeline_id/control_file",
600 0 : |r| request_span(r, patch_control_file_handler),
601 0 : )
602 0 : .post(
603 0 : "/v1/tenant/:tenant_id/timeline/:timeline_id/checkpoint",
604 0 : |r| request_span(r, timeline_checkpoint_handler),
605 0 : )
606 0 : // for tests
607 0 : .post("/v1/record_safekeeper_info/:tenant_id/:timeline_id", |r| {
608 0 : request_span(r, record_safekeeper_info)
609 0 : })
610 0 : .get("/v1/debug_dump", |r| request_span(r, dump_debug_handler))
611 0 : .get("/v1/tenant/:tenant_id/timeline/:timeline_id/digest", |r| {
612 0 : request_span(r, timeline_digest_handler)
613 0 : })
614 0 : }
615 :
616 : #[cfg(test)]
617 : mod tests {
618 : use super::*;
619 :
620 : #[test]
621 2 : fn test_term_switch_entry_api_serialize() {
622 2 : let state = AcceptorStateStatus {
623 2 : term: 1,
624 2 : epoch: 1,
625 2 : term_history: vec![TermSwitchApiEntry {
626 2 : term: 1,
627 2 : lsn: Lsn(0x16FFDDDD),
628 2 : }],
629 2 : };
630 2 : let json = serde_json::to_string(&state).unwrap();
631 2 : assert_eq!(
632 2 : json,
633 2 : "{\"term\":1,\"epoch\":1,\"term_history\":[{\"term\":1,\"lsn\":\"0/16FFDDDD\"}]}"
634 2 : );
635 2 : }
636 : }
|