Line data Source code
1 : use crate::metrics::{
2 : HttpRequestLatencyLabelGroup, HttpRequestStatusLabelGroup, PageserverRequestLabelGroup,
3 : METRICS_REGISTRY,
4 : };
5 : use crate::reconciler::ReconcileError;
6 : use crate::service::{LeadershipStatus, Service, STARTUP_RECONCILE_TIMEOUT};
7 : use anyhow::Context;
8 : use futures::Future;
9 : use hyper::header::CONTENT_TYPE;
10 : use hyper::{Body, Request, Response};
11 : use hyper::{StatusCode, Uri};
12 : use metrics::{BuildInfo, NeonMetrics};
13 : use pageserver_api::controller_api::{
14 : MetadataHealthListOutdatedRequest, MetadataHealthListOutdatedResponse,
15 : MetadataHealthListUnhealthyResponse, MetadataHealthUpdateRequest, MetadataHealthUpdateResponse,
16 : TenantCreateRequest,
17 : };
18 : use pageserver_api::models::{
19 : TenantConfigRequest, TenantLocationConfigRequest, TenantShardSplitRequest,
20 : TenantTimeTravelRequest, TimelineArchivalConfigRequest, TimelineCreateRequest,
21 : };
22 : use pageserver_api::shard::TenantShardId;
23 : use pageserver_client::mgmt_api;
24 : use std::sync::Arc;
25 : use std::time::{Duration, Instant};
26 : use tokio_util::sync::CancellationToken;
27 : use utils::auth::{Scope, SwappableJwtAuth};
28 : use utils::failpoint_support::failpoints_handler;
29 : use utils::http::endpoint::{auth_middleware, check_permission_with, request_span};
30 : use utils::http::request::{must_get_query_param, parse_query_param, parse_request_param};
31 : use utils::id::{TenantId, TimelineId};
32 :
33 : use utils::{
34 : http::{
35 : endpoint::{self},
36 : error::ApiError,
37 : json::{json_request, json_response},
38 : RequestExt, RouterBuilder,
39 : },
40 : id::NodeId,
41 : };
42 :
43 : use pageserver_api::controller_api::{
44 : NodeAvailability, NodeConfigureRequest, NodeRegisterRequest, TenantPolicyRequest,
45 : TenantShardMigrateRequest,
46 : };
47 : use pageserver_api::upcall_api::{ReAttachRequest, ValidateRequest};
48 :
49 : use control_plane::storage_controller::{AttachHookRequest, InspectRequest};
50 :
51 : use routerify::Middleware;
52 :
53 : /// State available to HTTP request handlers
54 : pub struct HttpState {
55 : service: Arc<crate::service::Service>,
56 : auth: Option<Arc<SwappableJwtAuth>>,
57 : neon_metrics: NeonMetrics,
58 : allowlist_routes: Vec<Uri>,
59 : }
60 :
61 : impl HttpState {
62 0 : pub fn new(
63 0 : service: Arc<crate::service::Service>,
64 0 : auth: Option<Arc<SwappableJwtAuth>>,
65 0 : build_info: BuildInfo,
66 0 : ) -> Self {
67 0 : let allowlist_routes = ["/status", "/ready", "/metrics"]
68 0 : .iter()
69 0 : .map(|v| v.parse().unwrap())
70 0 : .collect::<Vec<_>>();
71 0 : Self {
72 0 : service,
73 0 : auth,
74 0 : neon_metrics: NeonMetrics::new(build_info),
75 0 : allowlist_routes,
76 0 : }
77 0 : }
78 : }
79 :
80 : #[inline(always)]
81 0 : fn get_state(request: &Request<Body>) -> &HttpState {
82 0 : request
83 0 : .data::<Arc<HttpState>>()
84 0 : .expect("unknown state type")
85 0 : .as_ref()
86 0 : }
87 :
88 : /// Pageserver calls into this on startup, to learn which tenants it should attach
89 0 : async fn handle_re_attach(mut req: Request<Body>) -> Result<Response<Body>, ApiError> {
90 0 : check_permissions(&req, Scope::GenerationsApi)?;
91 :
92 0 : let reattach_req = json_request::<ReAttachRequest>(&mut req).await?;
93 0 : let state = get_state(&req);
94 0 : json_response(StatusCode::OK, state.service.re_attach(reattach_req).await?)
95 0 : }
96 :
97 : /// Pageserver calls into this before doing deletions, to confirm that it still
98 : /// holds the latest generation for the tenants with deletions enqueued
99 0 : async fn handle_validate(mut req: Request<Body>) -> Result<Response<Body>, ApiError> {
100 0 : check_permissions(&req, Scope::GenerationsApi)?;
101 :
102 0 : let validate_req = json_request::<ValidateRequest>(&mut req).await?;
103 0 : let state = get_state(&req);
104 0 : json_response(StatusCode::OK, state.service.validate(validate_req))
105 0 : }
106 :
107 : /// Call into this before attaching a tenant to a pageserver, to acquire a generation number
108 : /// (in the real control plane this is unnecessary, because the same program is managing
109 : /// generation numbers and doing attachments).
110 0 : async fn handle_attach_hook(mut req: Request<Body>) -> Result<Response<Body>, ApiError> {
111 0 : check_permissions(&req, Scope::Admin)?;
112 :
113 0 : let attach_req = json_request::<AttachHookRequest>(&mut req).await?;
114 0 : let state = get_state(&req);
115 0 :
116 0 : json_response(
117 0 : StatusCode::OK,
118 0 : state
119 0 : .service
120 0 : .attach_hook(attach_req)
121 0 : .await
122 0 : .map_err(ApiError::InternalServerError)?,
123 : )
124 0 : }
125 :
126 0 : async fn handle_inspect(mut req: Request<Body>) -> Result<Response<Body>, ApiError> {
127 0 : check_permissions(&req, Scope::Admin)?;
128 :
129 0 : let inspect_req = json_request::<InspectRequest>(&mut req).await?;
130 :
131 0 : let state = get_state(&req);
132 0 :
133 0 : json_response(StatusCode::OK, state.service.inspect(inspect_req))
134 0 : }
135 :
136 0 : async fn handle_tenant_create(
137 0 : service: Arc<Service>,
138 0 : mut req: Request<Body>,
139 0 : ) -> Result<Response<Body>, ApiError> {
140 0 : check_permissions(&req, Scope::PageServerApi)?;
141 :
142 0 : let create_req = json_request::<TenantCreateRequest>(&mut req).await?;
143 :
144 : json_response(
145 : StatusCode::CREATED,
146 0 : service.tenant_create(create_req).await?,
147 : )
148 0 : }
149 :
150 0 : async fn handle_tenant_location_config(
151 0 : service: Arc<Service>,
152 0 : mut req: Request<Body>,
153 0 : ) -> Result<Response<Body>, ApiError> {
154 0 : let tenant_shard_id: TenantShardId = parse_request_param(&req, "tenant_shard_id")?;
155 0 : check_permissions(&req, Scope::PageServerApi)?;
156 :
157 0 : let config_req = json_request::<TenantLocationConfigRequest>(&mut req).await?;
158 : json_response(
159 : StatusCode::OK,
160 0 : service
161 0 : .tenant_location_config(tenant_shard_id, config_req)
162 0 : .await?,
163 : )
164 0 : }
165 :
166 0 : async fn handle_tenant_config_set(
167 0 : service: Arc<Service>,
168 0 : mut req: Request<Body>,
169 0 : ) -> Result<Response<Body>, ApiError> {
170 0 : check_permissions(&req, Scope::PageServerApi)?;
171 :
172 0 : let config_req = json_request::<TenantConfigRequest>(&mut req).await?;
173 :
174 0 : json_response(StatusCode::OK, service.tenant_config_set(config_req).await?)
175 0 : }
176 :
177 0 : async fn handle_tenant_config_get(
178 0 : service: Arc<Service>,
179 0 : req: Request<Body>,
180 0 : ) -> Result<Response<Body>, ApiError> {
181 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
182 0 : check_permissions(&req, Scope::PageServerApi)?;
183 :
184 0 : json_response(StatusCode::OK, service.tenant_config_get(tenant_id)?)
185 0 : }
186 :
187 0 : async fn handle_tenant_time_travel_remote_storage(
188 0 : service: Arc<Service>,
189 0 : mut req: Request<Body>,
190 0 : ) -> Result<Response<Body>, ApiError> {
191 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
192 0 : check_permissions(&req, Scope::PageServerApi)?;
193 :
194 0 : let time_travel_req = json_request::<TenantTimeTravelRequest>(&mut req).await?;
195 :
196 0 : let timestamp_raw = must_get_query_param(&req, "travel_to")?;
197 0 : let _timestamp = humantime::parse_rfc3339(×tamp_raw).map_err(|_e| {
198 0 : ApiError::BadRequest(anyhow::anyhow!(
199 0 : "Invalid time for travel_to: {timestamp_raw:?}"
200 0 : ))
201 0 : })?;
202 :
203 0 : let done_if_after_raw = must_get_query_param(&req, "done_if_after")?;
204 0 : let _done_if_after = humantime::parse_rfc3339(&done_if_after_raw).map_err(|_e| {
205 0 : ApiError::BadRequest(anyhow::anyhow!(
206 0 : "Invalid time for done_if_after: {done_if_after_raw:?}"
207 0 : ))
208 0 : })?;
209 :
210 0 : service
211 0 : .tenant_time_travel_remote_storage(
212 0 : &time_travel_req,
213 0 : tenant_id,
214 0 : timestamp_raw,
215 0 : done_if_after_raw,
216 0 : )
217 0 : .await?;
218 0 : json_response(StatusCode::OK, ())
219 0 : }
220 :
221 0 : fn map_reqwest_hyper_status(status: reqwest::StatusCode) -> Result<hyper::StatusCode, ApiError> {
222 0 : hyper::StatusCode::from_u16(status.as_u16())
223 0 : .context("invalid status code")
224 0 : .map_err(ApiError::InternalServerError)
225 0 : }
226 :
227 0 : async fn handle_tenant_secondary_download(
228 0 : service: Arc<Service>,
229 0 : req: Request<Body>,
230 0 : ) -> Result<Response<Body>, ApiError> {
231 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
232 0 : let wait = parse_query_param(&req, "wait_ms")?.map(Duration::from_millis);
233 :
234 0 : let (status, progress) = service.tenant_secondary_download(tenant_id, wait).await?;
235 0 : json_response(map_reqwest_hyper_status(status)?, progress)
236 0 : }
237 :
238 0 : async fn handle_tenant_delete(
239 0 : service: Arc<Service>,
240 0 : req: Request<Body>,
241 0 : ) -> Result<Response<Body>, ApiError> {
242 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
243 0 : check_permissions(&req, Scope::PageServerApi)?;
244 :
245 0 : let status_code = service
246 0 : .tenant_delete(tenant_id)
247 0 : .await
248 0 : .and_then(map_reqwest_hyper_status)?;
249 :
250 0 : if status_code == StatusCode::NOT_FOUND {
251 : // The pageserver uses 404 for successful deletion, but we use 200
252 0 : json_response(StatusCode::OK, ())
253 : } else {
254 0 : json_response(status_code, ())
255 : }
256 0 : }
257 :
258 0 : async fn handle_tenant_timeline_create(
259 0 : service: Arc<Service>,
260 0 : mut req: Request<Body>,
261 0 : ) -> Result<Response<Body>, ApiError> {
262 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
263 0 : check_permissions(&req, Scope::PageServerApi)?;
264 :
265 0 : let create_req = json_request::<TimelineCreateRequest>(&mut req).await?;
266 : json_response(
267 : StatusCode::CREATED,
268 0 : service
269 0 : .tenant_timeline_create(tenant_id, create_req)
270 0 : .await?,
271 : )
272 0 : }
273 :
274 0 : async fn handle_tenant_timeline_delete(
275 0 : service: Arc<Service>,
276 0 : req: Request<Body>,
277 0 : ) -> Result<Response<Body>, ApiError> {
278 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
279 0 : check_permissions(&req, Scope::PageServerApi)?;
280 :
281 0 : let timeline_id: TimelineId = parse_request_param(&req, "timeline_id")?;
282 :
283 : // For timeline deletions, which both implement an "initially return 202, then 404 once
284 : // we're done" semantic, we wrap with a retry loop to expose a simpler API upstream.
285 0 : async fn deletion_wrapper<R, F>(service: Arc<Service>, f: F) -> Result<Response<Body>, ApiError>
286 0 : where
287 0 : R: std::future::Future<Output = Result<StatusCode, ApiError>> + Send + 'static,
288 0 : F: Fn(Arc<Service>) -> R + Send + Sync + 'static,
289 0 : {
290 0 : let started_at = Instant::now();
291 0 : // To keep deletion reasonably snappy for small tenants, initially check after 1 second if deletion
292 0 : // completed.
293 0 : let mut retry_period = Duration::from_secs(1);
294 0 : // On subsequent retries, wait longer.
295 0 : let max_retry_period = Duration::from_secs(5);
296 0 : // Enable callers with a 30 second request timeout to reliably get a response
297 0 : let max_wait = Duration::from_secs(25);
298 :
299 : loop {
300 0 : let status = f(service.clone()).await?;
301 0 : match status {
302 : StatusCode::ACCEPTED => {
303 0 : tracing::info!("Deletion accepted, waiting to try again...");
304 0 : tokio::time::sleep(retry_period).await;
305 0 : retry_period = max_retry_period;
306 : }
307 : StatusCode::NOT_FOUND => {
308 0 : tracing::info!("Deletion complete");
309 0 : return json_response(StatusCode::OK, ());
310 : }
311 : _ => {
312 0 : tracing::warn!("Unexpected status {status}");
313 0 : return json_response(status, ());
314 : }
315 : }
316 :
317 0 : let now = Instant::now();
318 0 : if now + retry_period > started_at + max_wait {
319 0 : tracing::info!("Deletion timed out waiting for 404");
320 : // REQUEST_TIMEOUT would be more appropriate, but CONFLICT is already part of
321 : // the pageserver's swagger definition for this endpoint, and has the same desired
322 : // effect of causing the control plane to retry later.
323 0 : return json_response(StatusCode::CONFLICT, ());
324 0 : }
325 : }
326 0 : }
327 :
328 0 : deletion_wrapper(service, move |service| async move {
329 0 : service
330 0 : .tenant_timeline_delete(tenant_id, timeline_id)
331 0 : .await
332 0 : .and_then(map_reqwest_hyper_status)
333 0 : })
334 0 : .await
335 0 : }
336 :
337 0 : async fn handle_tenant_timeline_archival_config(
338 0 : service: Arc<Service>,
339 0 : mut req: Request<Body>,
340 0 : ) -> Result<Response<Body>, ApiError> {
341 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
342 0 : check_permissions(&req, Scope::PageServerApi)?;
343 :
344 0 : let timeline_id: TimelineId = parse_request_param(&req, "timeline_id")?;
345 :
346 0 : let create_req = json_request::<TimelineArchivalConfigRequest>(&mut req).await?;
347 :
348 0 : service
349 0 : .tenant_timeline_archival_config(tenant_id, timeline_id, create_req)
350 0 : .await?;
351 :
352 0 : json_response(StatusCode::OK, ())
353 0 : }
354 :
355 0 : async fn handle_tenant_timeline_detach_ancestor(
356 0 : service: Arc<Service>,
357 0 : req: Request<Body>,
358 0 : ) -> Result<Response<Body>, ApiError> {
359 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
360 0 : check_permissions(&req, Scope::PageServerApi)?;
361 :
362 0 : let timeline_id: TimelineId = parse_request_param(&req, "timeline_id")?;
363 :
364 0 : let res = service
365 0 : .tenant_timeline_detach_ancestor(tenant_id, timeline_id)
366 0 : .await?;
367 :
368 0 : json_response(StatusCode::OK, res)
369 0 : }
370 :
371 0 : async fn handle_tenant_timeline_passthrough(
372 0 : service: Arc<Service>,
373 0 : req: Request<Body>,
374 0 : ) -> Result<Response<Body>, ApiError> {
375 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
376 0 : check_permissions(&req, Scope::PageServerApi)?;
377 :
378 0 : let Some(path) = req.uri().path_and_query() else {
379 : // This should never happen, our request router only calls us if there is a path
380 0 : return Err(ApiError::BadRequest(anyhow::anyhow!("Missing path")));
381 : };
382 :
383 0 : tracing::info!("Proxying request for tenant {} ({})", tenant_id, path);
384 :
385 : // Find the node that holds shard zero
386 0 : let (node, tenant_shard_id) = service.tenant_shard0_node(tenant_id)?;
387 :
388 : // Callers will always pass an unsharded tenant ID. Before proxying, we must
389 : // rewrite this to a shard-aware shard zero ID.
390 0 : let path = format!("{}", path);
391 0 : let tenant_str = tenant_id.to_string();
392 0 : let tenant_shard_str = format!("{}", tenant_shard_id);
393 0 : let path = path.replace(&tenant_str, &tenant_shard_str);
394 0 :
395 0 : let latency = &METRICS_REGISTRY
396 0 : .metrics_group
397 0 : .storage_controller_passthrough_request_latency;
398 0 :
399 0 : // This is a bit awkward. We remove the param from the request
400 0 : // and join the words by '_' to get a label for the request.
401 0 : let just_path = path.replace(&tenant_shard_str, "");
402 0 : let path_label = just_path
403 0 : .split('/')
404 0 : .filter(|token| !token.is_empty())
405 0 : .collect::<Vec<_>>()
406 0 : .join("_");
407 0 : let labels = PageserverRequestLabelGroup {
408 0 : pageserver_id: &node.get_id().to_string(),
409 0 : path: &path_label,
410 0 : method: crate::metrics::Method::Get,
411 0 : };
412 0 :
413 0 : let _timer = latency.start_timer(labels.clone());
414 0 :
415 0 : let client = mgmt_api::Client::new(node.base_url(), service.get_config().jwt_token.as_deref());
416 0 : let resp = client.get_raw(path).await.map_err(|_e|
417 : // FIXME: give APiError a proper Unavailable variant. We return 503 here because
418 : // if we can't successfully send a request to the pageserver, we aren't available.
419 0 : ApiError::ShuttingDown)?;
420 :
421 0 : if !resp.status().is_success() {
422 0 : let error_counter = &METRICS_REGISTRY
423 0 : .metrics_group
424 0 : .storage_controller_passthrough_request_error;
425 0 : error_counter.inc(labels);
426 0 : }
427 :
428 : // We have a reqest::Response, would like a http::Response
429 0 : let mut builder = hyper::Response::builder().status(map_reqwest_hyper_status(resp.status())?);
430 0 : for (k, v) in resp.headers() {
431 0 : builder = builder.header(k.as_str(), v.as_bytes());
432 0 : }
433 :
434 0 : let response = builder
435 0 : .body(Body::wrap_stream(resp.bytes_stream()))
436 0 : .map_err(|e| ApiError::InternalServerError(e.into()))?;
437 :
438 0 : Ok(response)
439 0 : }
440 :
441 0 : async fn handle_tenant_locate(
442 0 : service: Arc<Service>,
443 0 : req: Request<Body>,
444 0 : ) -> Result<Response<Body>, ApiError> {
445 0 : check_permissions(&req, Scope::Admin)?;
446 :
447 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
448 0 : json_response(StatusCode::OK, service.tenant_locate(tenant_id)?)
449 0 : }
450 :
451 0 : async fn handle_tenant_describe(
452 0 : service: Arc<Service>,
453 0 : req: Request<Body>,
454 0 : ) -> Result<Response<Body>, ApiError> {
455 0 : check_permissions(&req, Scope::Scrubber)?;
456 :
457 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
458 0 : json_response(StatusCode::OK, service.tenant_describe(tenant_id)?)
459 0 : }
460 :
461 0 : async fn handle_tenant_list(
462 0 : service: Arc<Service>,
463 0 : req: Request<Body>,
464 0 : ) -> Result<Response<Body>, ApiError> {
465 0 : check_permissions(&req, Scope::Admin)?;
466 :
467 0 : json_response(StatusCode::OK, service.tenant_list())
468 0 : }
469 :
470 0 : async fn handle_node_register(mut req: Request<Body>) -> Result<Response<Body>, ApiError> {
471 0 : check_permissions(&req, Scope::Admin)?;
472 :
473 0 : let register_req = json_request::<NodeRegisterRequest>(&mut req).await?;
474 0 : let state = get_state(&req);
475 0 : state.service.node_register(register_req).await?;
476 0 : json_response(StatusCode::OK, ())
477 0 : }
478 :
479 0 : async fn handle_node_list(req: Request<Body>) -> Result<Response<Body>, ApiError> {
480 0 : check_permissions(&req, Scope::Admin)?;
481 :
482 0 : let state = get_state(&req);
483 0 : let nodes = state.service.node_list().await?;
484 0 : let api_nodes = nodes.into_iter().map(|n| n.describe()).collect::<Vec<_>>();
485 0 :
486 0 : json_response(StatusCode::OK, api_nodes)
487 0 : }
488 :
489 0 : async fn handle_node_drop(req: Request<Body>) -> Result<Response<Body>, ApiError> {
490 0 : check_permissions(&req, Scope::Admin)?;
491 :
492 0 : let state = get_state(&req);
493 0 : let node_id: NodeId = parse_request_param(&req, "node_id")?;
494 0 : json_response(StatusCode::OK, state.service.node_drop(node_id).await?)
495 0 : }
496 :
497 0 : async fn handle_node_delete(req: Request<Body>) -> Result<Response<Body>, ApiError> {
498 0 : check_permissions(&req, Scope::Admin)?;
499 :
500 0 : let state = get_state(&req);
501 0 : let node_id: NodeId = parse_request_param(&req, "node_id")?;
502 0 : json_response(StatusCode::OK, state.service.node_delete(node_id).await?)
503 0 : }
504 :
505 0 : async fn handle_node_configure(mut req: Request<Body>) -> Result<Response<Body>, ApiError> {
506 0 : check_permissions(&req, Scope::Admin)?;
507 :
508 0 : let node_id: NodeId = parse_request_param(&req, "node_id")?;
509 0 : let config_req = json_request::<NodeConfigureRequest>(&mut req).await?;
510 0 : if node_id != config_req.node_id {
511 0 : return Err(ApiError::BadRequest(anyhow::anyhow!(
512 0 : "Path and body node_id differ"
513 0 : )));
514 0 : }
515 0 : let state = get_state(&req);
516 0 :
517 0 : json_response(
518 0 : StatusCode::OK,
519 0 : state
520 0 : .service
521 0 : .external_node_configure(
522 0 : config_req.node_id,
523 0 : config_req.availability.map(NodeAvailability::from),
524 0 : config_req.scheduling,
525 0 : )
526 0 : .await?,
527 : )
528 0 : }
529 :
530 0 : async fn handle_node_status(req: Request<Body>) -> Result<Response<Body>, ApiError> {
531 0 : check_permissions(&req, Scope::Admin)?;
532 :
533 0 : let state = get_state(&req);
534 0 : let node_id: NodeId = parse_request_param(&req, "node_id")?;
535 :
536 0 : let node_status = state.service.get_node(node_id).await?;
537 :
538 0 : json_response(StatusCode::OK, node_status)
539 0 : }
540 :
541 0 : async fn handle_get_leader(req: Request<Body>) -> Result<Response<Body>, ApiError> {
542 0 : check_permissions(&req, Scope::Admin)?;
543 :
544 0 : let state = get_state(&req);
545 0 : let leader = state.service.get_leader().await.map_err(|err| {
546 0 : ApiError::InternalServerError(anyhow::anyhow!(
547 0 : "Failed to read leader from database: {err}"
548 0 : ))
549 0 : })?;
550 :
551 0 : json_response(StatusCode::OK, leader)
552 0 : }
553 :
554 0 : async fn handle_node_drain(req: Request<Body>) -> Result<Response<Body>, ApiError> {
555 0 : check_permissions(&req, Scope::Admin)?;
556 :
557 0 : let state = get_state(&req);
558 0 : let node_id: NodeId = parse_request_param(&req, "node_id")?;
559 :
560 0 : state.service.start_node_drain(node_id).await?;
561 :
562 0 : json_response(StatusCode::ACCEPTED, ())
563 0 : }
564 :
565 0 : async fn handle_cancel_node_drain(req: Request<Body>) -> Result<Response<Body>, ApiError> {
566 0 : check_permissions(&req, Scope::Admin)?;
567 :
568 0 : let state = get_state(&req);
569 0 : let node_id: NodeId = parse_request_param(&req, "node_id")?;
570 :
571 0 : state.service.cancel_node_drain(node_id).await?;
572 :
573 0 : json_response(StatusCode::ACCEPTED, ())
574 0 : }
575 :
576 0 : async fn handle_node_fill(req: Request<Body>) -> Result<Response<Body>, ApiError> {
577 0 : check_permissions(&req, Scope::Admin)?;
578 :
579 0 : let state = get_state(&req);
580 0 : let node_id: NodeId = parse_request_param(&req, "node_id")?;
581 :
582 0 : state.service.start_node_fill(node_id).await?;
583 :
584 0 : json_response(StatusCode::ACCEPTED, ())
585 0 : }
586 :
587 0 : async fn handle_cancel_node_fill(req: Request<Body>) -> Result<Response<Body>, ApiError> {
588 0 : check_permissions(&req, Scope::Admin)?;
589 :
590 0 : let state = get_state(&req);
591 0 : let node_id: NodeId = parse_request_param(&req, "node_id")?;
592 :
593 0 : state.service.cancel_node_fill(node_id).await?;
594 :
595 0 : json_response(StatusCode::ACCEPTED, ())
596 0 : }
597 :
598 0 : async fn handle_metadata_health_update(mut req: Request<Body>) -> Result<Response<Body>, ApiError> {
599 0 : check_permissions(&req, Scope::Scrubber)?;
600 :
601 0 : let update_req = json_request::<MetadataHealthUpdateRequest>(&mut req).await?;
602 0 : let state = get_state(&req);
603 0 :
604 0 : state.service.metadata_health_update(update_req).await?;
605 :
606 0 : json_response(StatusCode::OK, MetadataHealthUpdateResponse {})
607 0 : }
608 :
609 0 : async fn handle_metadata_health_list_unhealthy(
610 0 : req: Request<Body>,
611 0 : ) -> Result<Response<Body>, ApiError> {
612 0 : check_permissions(&req, Scope::Admin)?;
613 :
614 0 : let state = get_state(&req);
615 0 : let unhealthy_tenant_shards = state.service.metadata_health_list_unhealthy().await?;
616 :
617 0 : json_response(
618 0 : StatusCode::OK,
619 0 : MetadataHealthListUnhealthyResponse {
620 0 : unhealthy_tenant_shards,
621 0 : },
622 0 : )
623 0 : }
624 :
625 0 : async fn handle_metadata_health_list_outdated(
626 0 : mut req: Request<Body>,
627 0 : ) -> Result<Response<Body>, ApiError> {
628 0 : check_permissions(&req, Scope::Admin)?;
629 :
630 0 : let list_outdated_req = json_request::<MetadataHealthListOutdatedRequest>(&mut req).await?;
631 0 : let state = get_state(&req);
632 0 : let health_records = state
633 0 : .service
634 0 : .metadata_health_list_outdated(list_outdated_req.not_scrubbed_for)
635 0 : .await?;
636 :
637 0 : json_response(
638 0 : StatusCode::OK,
639 0 : MetadataHealthListOutdatedResponse { health_records },
640 0 : )
641 0 : }
642 :
643 0 : async fn handle_tenant_shard_split(
644 0 : service: Arc<Service>,
645 0 : mut req: Request<Body>,
646 0 : ) -> Result<Response<Body>, ApiError> {
647 0 : check_permissions(&req, Scope::Admin)?;
648 :
649 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
650 0 : let split_req = json_request::<TenantShardSplitRequest>(&mut req).await?;
651 :
652 : json_response(
653 : StatusCode::OK,
654 0 : service.tenant_shard_split(tenant_id, split_req).await?,
655 : )
656 0 : }
657 :
658 0 : async fn handle_tenant_shard_migrate(
659 0 : service: Arc<Service>,
660 0 : mut req: Request<Body>,
661 0 : ) -> Result<Response<Body>, ApiError> {
662 0 : check_permissions(&req, Scope::Admin)?;
663 :
664 0 : let tenant_shard_id: TenantShardId = parse_request_param(&req, "tenant_shard_id")?;
665 0 : let migrate_req = json_request::<TenantShardMigrateRequest>(&mut req).await?;
666 : json_response(
667 : StatusCode::OK,
668 0 : service
669 0 : .tenant_shard_migrate(tenant_shard_id, migrate_req)
670 0 : .await?,
671 : )
672 0 : }
673 :
674 0 : async fn handle_tenant_update_policy(mut req: Request<Body>) -> Result<Response<Body>, ApiError> {
675 0 : check_permissions(&req, Scope::Admin)?;
676 :
677 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
678 0 : let update_req = json_request::<TenantPolicyRequest>(&mut req).await?;
679 0 : let state = get_state(&req);
680 0 :
681 0 : json_response(
682 0 : StatusCode::OK,
683 0 : state
684 0 : .service
685 0 : .tenant_update_policy(tenant_id, update_req)
686 0 : .await?,
687 : )
688 0 : }
689 :
690 0 : async fn handle_step_down(req: Request<Body>) -> Result<Response<Body>, ApiError> {
691 0 : check_permissions(&req, Scope::Admin)?;
692 :
693 0 : let state = get_state(&req);
694 0 : json_response(StatusCode::OK, state.service.step_down().await)
695 0 : }
696 :
697 0 : async fn handle_tenant_drop(req: Request<Body>) -> Result<Response<Body>, ApiError> {
698 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
699 0 : check_permissions(&req, Scope::PageServerApi)?;
700 :
701 0 : let state = get_state(&req);
702 0 :
703 0 : json_response(StatusCode::OK, state.service.tenant_drop(tenant_id).await?)
704 0 : }
705 :
706 0 : async fn handle_tenant_import(req: Request<Body>) -> Result<Response<Body>, ApiError> {
707 0 : let tenant_id: TenantId = parse_request_param(&req, "tenant_id")?;
708 0 : check_permissions(&req, Scope::PageServerApi)?;
709 :
710 0 : let state = get_state(&req);
711 0 :
712 0 : json_response(
713 0 : StatusCode::OK,
714 0 : state.service.tenant_import(tenant_id).await?,
715 : )
716 0 : }
717 :
718 0 : async fn handle_tenants_dump(req: Request<Body>) -> Result<Response<Body>, ApiError> {
719 0 : check_permissions(&req, Scope::Admin)?;
720 :
721 0 : let state = get_state(&req);
722 0 : state.service.tenants_dump()
723 0 : }
724 :
725 0 : async fn handle_scheduler_dump(req: Request<Body>) -> Result<Response<Body>, ApiError> {
726 0 : check_permissions(&req, Scope::Admin)?;
727 :
728 0 : let state = get_state(&req);
729 0 : state.service.scheduler_dump()
730 0 : }
731 :
732 0 : async fn handle_consistency_check(req: Request<Body>) -> Result<Response<Body>, ApiError> {
733 0 : check_permissions(&req, Scope::Admin)?;
734 :
735 0 : let state = get_state(&req);
736 0 :
737 0 : json_response(StatusCode::OK, state.service.consistency_check().await?)
738 0 : }
739 :
740 0 : async fn handle_reconcile_all(req: Request<Body>) -> Result<Response<Body>, ApiError> {
741 0 : check_permissions(&req, Scope::Admin)?;
742 :
743 0 : let state = get_state(&req);
744 0 :
745 0 : json_response(StatusCode::OK, state.service.reconcile_all_now().await?)
746 0 : }
747 :
748 : /// Status endpoint is just used for checking that our HTTP listener is up
749 0 : async fn handle_status(_req: Request<Body>) -> Result<Response<Body>, ApiError> {
750 0 : json_response(StatusCode::OK, ())
751 0 : }
752 :
753 : /// Readiness endpoint indicates when we're done doing startup I/O (e.g. reconciling
754 : /// with remote pageserver nodes). This is intended for use as a kubernetes readiness probe.
755 0 : async fn handle_ready(req: Request<Body>) -> Result<Response<Body>, ApiError> {
756 0 : let state = get_state(&req);
757 0 : if state.service.startup_complete.is_ready() {
758 0 : json_response(StatusCode::OK, ())
759 : } else {
760 0 : json_response(StatusCode::SERVICE_UNAVAILABLE, ())
761 : }
762 0 : }
763 :
764 : impl From<ReconcileError> for ApiError {
765 0 : fn from(value: ReconcileError) -> Self {
766 0 : ApiError::Conflict(format!("Reconciliation error: {}", value))
767 0 : }
768 : }
769 :
770 : /// Common wrapper for request handlers that call into Service and will operate on tenants: they must only
771 : /// be allowed to run if Service has finished its initial reconciliation.
772 0 : async fn tenant_service_handler<R, H>(
773 0 : request: Request<Body>,
774 0 : handler: H,
775 0 : request_name: RequestName,
776 0 : ) -> R::Output
777 0 : where
778 0 : R: std::future::Future<Output = Result<Response<Body>, ApiError>> + Send + 'static,
779 0 : H: FnOnce(Arc<Service>, Request<Body>) -> R + Send + Sync + 'static,
780 0 : {
781 0 : let state = get_state(&request);
782 0 : let service = state.service.clone();
783 0 :
784 0 : let startup_complete = service.startup_complete.clone();
785 0 : if tokio::time::timeout(STARTUP_RECONCILE_TIMEOUT, startup_complete.wait())
786 0 : .await
787 0 : .is_err()
788 : {
789 : // This shouldn't happen: it is the responsibilty of [`Service::startup_reconcile`] to use appropriate
790 : // timeouts around its remote calls, to bound its runtime.
791 0 : return Err(ApiError::Timeout(
792 0 : "Timed out waiting for service readiness".into(),
793 0 : ));
794 0 : }
795 0 :
796 0 : named_request_span(
797 0 : request,
798 0 : |request| async move { handler(service, request).await },
799 0 : request_name,
800 0 : )
801 0 : .await
802 0 : }
803 :
804 : /// Check if the required scope is held in the request's token, or if the request has
805 : /// a token with 'admin' scope then always permit it.
806 0 : fn check_permissions(request: &Request<Body>, required_scope: Scope) -> Result<(), ApiError> {
807 0 : check_permission_with(request, |claims| {
808 0 : match crate::auth::check_permission(claims, required_scope) {
809 0 : Err(e) => match crate::auth::check_permission(claims, Scope::Admin) {
810 0 : Ok(()) => Ok(()),
811 0 : Err(_) => Err(e),
812 : },
813 0 : Ok(()) => Ok(()),
814 : }
815 0 : })
816 0 : }
817 :
818 : #[derive(Clone, Debug)]
819 : struct RequestMeta {
820 : method: hyper::http::Method,
821 : at: Instant,
822 : }
823 :
824 0 : pub fn prologue_leadership_status_check_middleware<
825 0 : B: hyper::body::HttpBody + Send + Sync + 'static,
826 0 : >() -> Middleware<B, ApiError> {
827 0 : Middleware::pre(move |req| async move {
828 0 : let state = get_state(&req);
829 0 : let leadership_status = state.service.get_leadership_status();
830 0 :
831 0 : enum AllowedRoutes<'a> {
832 0 : All,
833 0 : Some(Vec<&'a str>),
834 0 : }
835 0 :
836 0 : let allowed_routes = match leadership_status {
837 0 : LeadershipStatus::Leader => AllowedRoutes::All,
838 0 : LeadershipStatus::SteppedDown => {
839 0 : // TODO: does it make sense to allow /status here?
840 0 : AllowedRoutes::Some(["/control/v1/step_down", "/status", "/metrics"].to_vec())
841 0 : }
842 0 : LeadershipStatus::Candidate => {
843 0 : AllowedRoutes::Some(["/ready", "/status", "/metrics"].to_vec())
844 0 : }
845 0 : };
846 0 :
847 0 : let uri = req.uri().to_string();
848 0 : match allowed_routes {
849 0 : AllowedRoutes::All => Ok(req),
850 0 : AllowedRoutes::Some(allowed) if allowed.contains(&uri.as_str()) => Ok(req),
851 0 : _ => {
852 0 : tracing::info!(
853 0 : "Request {} not allowed due to current leadership state",
854 0 : req.uri()
855 0 : );
856 0 :
857 0 : Err(ApiError::ResourceUnavailable(
858 0 : format!("Current leadership status is {leadership_status}").into(),
859 0 : ))
860 0 : }
861 0 : }
862 0 : })
863 0 : }
864 :
865 0 : fn prologue_metrics_middleware<B: hyper::body::HttpBody + Send + Sync + 'static>(
866 0 : ) -> Middleware<B, ApiError> {
867 0 : Middleware::pre(move |req| async move {
868 0 : let meta = RequestMeta {
869 0 : method: req.method().clone(),
870 0 : at: Instant::now(),
871 0 : };
872 0 :
873 0 : req.set_context(meta);
874 0 :
875 0 : Ok(req)
876 0 : })
877 0 : }
878 :
879 0 : fn epilogue_metrics_middleware<B: hyper::body::HttpBody + Send + Sync + 'static>(
880 0 : ) -> Middleware<B, ApiError> {
881 0 : Middleware::post_with_info(move |resp, req_info| async move {
882 0 : let request_name = match req_info.context::<RequestName>() {
883 0 : Some(name) => name,
884 0 : None => {
885 0 : return Ok(resp);
886 0 : }
887 0 : };
888 0 :
889 0 : if let Some(meta) = req_info.context::<RequestMeta>() {
890 0 : let status = &crate::metrics::METRICS_REGISTRY
891 0 : .metrics_group
892 0 : .storage_controller_http_request_status;
893 0 : let latency = &crate::metrics::METRICS_REGISTRY
894 0 : .metrics_group
895 0 : .storage_controller_http_request_latency;
896 0 :
897 0 : status.inc(HttpRequestStatusLabelGroup {
898 0 : path: request_name.0,
899 0 : method: meta.method.clone().into(),
900 0 : status: crate::metrics::StatusCode(resp.status()),
901 0 : });
902 0 :
903 0 : latency.observe(
904 0 : HttpRequestLatencyLabelGroup {
905 0 : path: request_name.0,
906 0 : method: meta.method.into(),
907 0 : },
908 0 : meta.at.elapsed().as_secs_f64(),
909 0 : );
910 0 : }
911 0 : Ok(resp)
912 0 : })
913 0 : }
914 :
915 0 : pub async fn measured_metrics_handler(req: Request<Body>) -> Result<Response<Body>, ApiError> {
916 0 : pub const TEXT_FORMAT: &str = "text/plain; version=0.0.4";
917 0 :
918 0 : let state = get_state(&req);
919 0 : let payload = crate::metrics::METRICS_REGISTRY.encode(&state.neon_metrics);
920 0 : let response = Response::builder()
921 0 : .status(200)
922 0 : .header(CONTENT_TYPE, TEXT_FORMAT)
923 0 : .body(payload.into())
924 0 : .unwrap();
925 0 :
926 0 : Ok(response)
927 0 : }
928 :
929 : #[derive(Clone)]
930 : struct RequestName(&'static str);
931 :
932 0 : async fn named_request_span<R, H>(
933 0 : request: Request<Body>,
934 0 : handler: H,
935 0 : name: RequestName,
936 0 : ) -> R::Output
937 0 : where
938 0 : R: Future<Output = Result<Response<Body>, ApiError>> + Send + 'static,
939 0 : H: FnOnce(Request<Body>) -> R + Send + Sync + 'static,
940 0 : {
941 0 : request.set_context(name);
942 0 : request_span(request, handler).await
943 0 : }
944 :
945 0 : pub fn make_router(
946 0 : service: Arc<Service>,
947 0 : auth: Option<Arc<SwappableJwtAuth>>,
948 0 : build_info: BuildInfo,
949 0 : ) -> RouterBuilder<hyper::Body, ApiError> {
950 0 : let mut router = endpoint::make_router()
951 0 : .middleware(prologue_leadership_status_check_middleware())
952 0 : .middleware(prologue_metrics_middleware())
953 0 : .middleware(epilogue_metrics_middleware());
954 0 : if auth.is_some() {
955 0 : router = router.middleware(auth_middleware(|request| {
956 0 : let state = get_state(request);
957 0 : if state.allowlist_routes.contains(request.uri()) {
958 0 : None
959 : } else {
960 0 : state.auth.as_deref()
961 : }
962 0 : }));
963 0 : }
964 :
965 0 : router
966 0 : .data(Arc::new(HttpState::new(service, auth, build_info)))
967 0 : .get("/metrics", |r| {
968 0 : named_request_span(r, measured_metrics_handler, RequestName("metrics"))
969 0 : })
970 0 : // Non-prefixed generic endpoints (status, metrics)
971 0 : .get("/status", |r| {
972 0 : named_request_span(r, handle_status, RequestName("status"))
973 0 : })
974 0 : .get("/ready", |r| {
975 0 : named_request_span(r, handle_ready, RequestName("ready"))
976 0 : })
977 0 : // Upcalls for the pageserver: point the pageserver's `control_plane_api` config to this prefix
978 0 : .post("/upcall/v1/re-attach", |r| {
979 0 : named_request_span(r, handle_re_attach, RequestName("upcall_v1_reattach"))
980 0 : })
981 0 : .post("/upcall/v1/validate", |r| {
982 0 : named_request_span(r, handle_validate, RequestName("upcall_v1_validate"))
983 0 : })
984 0 : // Test/dev/debug endpoints
985 0 : .post("/debug/v1/attach-hook", |r| {
986 0 : named_request_span(r, handle_attach_hook, RequestName("debug_v1_attach_hook"))
987 0 : })
988 0 : .post("/debug/v1/inspect", |r| {
989 0 : named_request_span(r, handle_inspect, RequestName("debug_v1_inspect"))
990 0 : })
991 0 : .post("/debug/v1/tenant/:tenant_id/drop", |r| {
992 0 : named_request_span(r, handle_tenant_drop, RequestName("debug_v1_tenant_drop"))
993 0 : })
994 0 : .post("/debug/v1/node/:node_id/drop", |r| {
995 0 : named_request_span(r, handle_node_drop, RequestName("debug_v1_node_drop"))
996 0 : })
997 0 : .post("/debug/v1/tenant/:tenant_id/import", |r| {
998 0 : named_request_span(
999 0 : r,
1000 0 : handle_tenant_import,
1001 0 : RequestName("debug_v1_tenant_import"),
1002 0 : )
1003 0 : })
1004 0 : .get("/debug/v1/tenant", |r| {
1005 0 : named_request_span(r, handle_tenants_dump, RequestName("debug_v1_tenant"))
1006 0 : })
1007 0 : .get("/debug/v1/tenant/:tenant_id/locate", |r| {
1008 0 : tenant_service_handler(
1009 0 : r,
1010 0 : handle_tenant_locate,
1011 0 : RequestName("debug_v1_tenant_locate"),
1012 0 : )
1013 0 : })
1014 0 : .get("/debug/v1/scheduler", |r| {
1015 0 : named_request_span(r, handle_scheduler_dump, RequestName("debug_v1_scheduler"))
1016 0 : })
1017 0 : .post("/debug/v1/consistency_check", |r| {
1018 0 : named_request_span(
1019 0 : r,
1020 0 : handle_consistency_check,
1021 0 : RequestName("debug_v1_consistency_check"),
1022 0 : )
1023 0 : })
1024 0 : .post("/debug/v1/reconcile_all", |r| {
1025 0 : request_span(r, handle_reconcile_all)
1026 0 : })
1027 0 : .put("/debug/v1/failpoints", |r| {
1028 0 : request_span(r, |r| failpoints_handler(r, CancellationToken::new()))
1029 0 : })
1030 0 : // Node operations
1031 0 : .post("/control/v1/node", |r| {
1032 0 : named_request_span(r, handle_node_register, RequestName("control_v1_node"))
1033 0 : })
1034 0 : .delete("/control/v1/node/:node_id", |r| {
1035 0 : named_request_span(r, handle_node_delete, RequestName("control_v1_node_delete"))
1036 0 : })
1037 0 : .get("/control/v1/node", |r| {
1038 0 : named_request_span(r, handle_node_list, RequestName("control_v1_node"))
1039 0 : })
1040 0 : .put("/control/v1/node/:node_id/config", |r| {
1041 0 : named_request_span(
1042 0 : r,
1043 0 : handle_node_configure,
1044 0 : RequestName("control_v1_node_config"),
1045 0 : )
1046 0 : })
1047 0 : .get("/control/v1/node/:node_id", |r| {
1048 0 : named_request_span(r, handle_node_status, RequestName("control_v1_node_status"))
1049 0 : })
1050 0 : .get("/control/v1/leader", |r| {
1051 0 : named_request_span(r, handle_get_leader, RequestName("control_v1_get_leader"))
1052 0 : })
1053 0 : .put("/control/v1/node/:node_id/drain", |r| {
1054 0 : named_request_span(r, handle_node_drain, RequestName("control_v1_node_drain"))
1055 0 : })
1056 0 : .delete("/control/v1/node/:node_id/drain", |r| {
1057 0 : named_request_span(
1058 0 : r,
1059 0 : handle_cancel_node_drain,
1060 0 : RequestName("control_v1_cancel_node_drain"),
1061 0 : )
1062 0 : })
1063 0 : .put("/control/v1/node/:node_id/fill", |r| {
1064 0 : named_request_span(r, handle_node_fill, RequestName("control_v1_node_fill"))
1065 0 : })
1066 0 : .delete("/control/v1/node/:node_id/fill", |r| {
1067 0 : named_request_span(
1068 0 : r,
1069 0 : handle_cancel_node_fill,
1070 0 : RequestName("control_v1_cancel_node_fill"),
1071 0 : )
1072 0 : })
1073 0 : // Metadata health operations
1074 0 : .post("/control/v1/metadata_health/update", |r| {
1075 0 : named_request_span(
1076 0 : r,
1077 0 : handle_metadata_health_update,
1078 0 : RequestName("control_v1_metadata_health_update"),
1079 0 : )
1080 0 : })
1081 0 : .get("/control/v1/metadata_health/unhealthy", |r| {
1082 0 : named_request_span(
1083 0 : r,
1084 0 : handle_metadata_health_list_unhealthy,
1085 0 : RequestName("control_v1_metadata_health_list_unhealthy"),
1086 0 : )
1087 0 : })
1088 0 : .post("/control/v1/metadata_health/outdated", |r| {
1089 0 : named_request_span(
1090 0 : r,
1091 0 : handle_metadata_health_list_outdated,
1092 0 : RequestName("control_v1_metadata_health_list_outdated"),
1093 0 : )
1094 0 : })
1095 0 : // Tenant Shard operations
1096 0 : .put("/control/v1/tenant/:tenant_shard_id/migrate", |r| {
1097 0 : tenant_service_handler(
1098 0 : r,
1099 0 : handle_tenant_shard_migrate,
1100 0 : RequestName("control_v1_tenant_migrate"),
1101 0 : )
1102 0 : })
1103 0 : .put("/control/v1/tenant/:tenant_id/shard_split", |r| {
1104 0 : tenant_service_handler(
1105 0 : r,
1106 0 : handle_tenant_shard_split,
1107 0 : RequestName("control_v1_tenant_shard_split"),
1108 0 : )
1109 0 : })
1110 0 : .get("/control/v1/tenant/:tenant_id", |r| {
1111 0 : tenant_service_handler(
1112 0 : r,
1113 0 : handle_tenant_describe,
1114 0 : RequestName("control_v1_tenant_describe"),
1115 0 : )
1116 0 : })
1117 0 : .get("/control/v1/tenant", |r| {
1118 0 : tenant_service_handler(r, handle_tenant_list, RequestName("control_v1_tenant_list"))
1119 0 : })
1120 0 : .put("/control/v1/tenant/:tenant_id/policy", |r| {
1121 0 : named_request_span(
1122 0 : r,
1123 0 : handle_tenant_update_policy,
1124 0 : RequestName("control_v1_tenant_policy"),
1125 0 : )
1126 0 : })
1127 0 : .put("/control/v1/step_down", |r| {
1128 0 : named_request_span(r, handle_step_down, RequestName("control_v1_step_down"))
1129 0 : })
1130 0 : // Tenant operations
1131 0 : // The ^/v1/ endpoints act as a "Virtual Pageserver", enabling shard-naive clients to call into
1132 0 : // this service to manage tenants that actually consist of many tenant shards, as if they are a single entity.
1133 0 : .post("/v1/tenant", |r| {
1134 0 : tenant_service_handler(r, handle_tenant_create, RequestName("v1_tenant"))
1135 0 : })
1136 0 : .delete("/v1/tenant/:tenant_id", |r| {
1137 0 : tenant_service_handler(r, handle_tenant_delete, RequestName("v1_tenant"))
1138 0 : })
1139 0 : .put("/v1/tenant/config", |r| {
1140 0 : tenant_service_handler(r, handle_tenant_config_set, RequestName("v1_tenant_config"))
1141 0 : })
1142 0 : .get("/v1/tenant/:tenant_id/config", |r| {
1143 0 : tenant_service_handler(r, handle_tenant_config_get, RequestName("v1_tenant_config"))
1144 0 : })
1145 0 : .put("/v1/tenant/:tenant_shard_id/location_config", |r| {
1146 0 : tenant_service_handler(
1147 0 : r,
1148 0 : handle_tenant_location_config,
1149 0 : RequestName("v1_tenant_location_config"),
1150 0 : )
1151 0 : })
1152 0 : .put("/v1/tenant/:tenant_id/time_travel_remote_storage", |r| {
1153 0 : tenant_service_handler(
1154 0 : r,
1155 0 : handle_tenant_time_travel_remote_storage,
1156 0 : RequestName("v1_tenant_time_travel_remote_storage"),
1157 0 : )
1158 0 : })
1159 0 : .post("/v1/tenant/:tenant_id/secondary/download", |r| {
1160 0 : tenant_service_handler(
1161 0 : r,
1162 0 : handle_tenant_secondary_download,
1163 0 : RequestName("v1_tenant_secondary_download"),
1164 0 : )
1165 0 : })
1166 0 : // Timeline operations
1167 0 : .delete("/v1/tenant/:tenant_id/timeline/:timeline_id", |r| {
1168 0 : tenant_service_handler(
1169 0 : r,
1170 0 : handle_tenant_timeline_delete,
1171 0 : RequestName("v1_tenant_timeline"),
1172 0 : )
1173 0 : })
1174 0 : .post("/v1/tenant/:tenant_id/timeline", |r| {
1175 0 : tenant_service_handler(
1176 0 : r,
1177 0 : handle_tenant_timeline_create,
1178 0 : RequestName("v1_tenant_timeline"),
1179 0 : )
1180 0 : })
1181 0 : .post(
1182 0 : "/v1/tenant/:tenant_id/timeline/:timeline_id/archival_config",
1183 0 : |r| {
1184 0 : tenant_service_handler(
1185 0 : r,
1186 0 : handle_tenant_timeline_archival_config,
1187 0 : RequestName("v1_tenant_timeline_archival_config"),
1188 0 : )
1189 0 : },
1190 0 : )
1191 0 : .put(
1192 0 : "/v1/tenant/:tenant_id/timeline/:timeline_id/detach_ancestor",
1193 0 : |r| {
1194 0 : tenant_service_handler(
1195 0 : r,
1196 0 : handle_tenant_timeline_detach_ancestor,
1197 0 : RequestName("v1_tenant_timeline_detach_ancestor"),
1198 0 : )
1199 0 : },
1200 0 : )
1201 0 : // Tenant detail GET passthrough to shard zero:
1202 0 : .get("/v1/tenant/:tenant_id", |r| {
1203 0 : tenant_service_handler(
1204 0 : r,
1205 0 : handle_tenant_timeline_passthrough,
1206 0 : RequestName("v1_tenant_passthrough"),
1207 0 : )
1208 0 : })
1209 0 : // The `*` in the URL is a wildcard: any tenant/timeline GET APIs on the pageserver
1210 0 : // are implicitly exposed here. This must be last in the list to avoid
1211 0 : // taking precedence over other GET methods we might implement by hand.
1212 0 : .get("/v1/tenant/:tenant_id/*", |r| {
1213 0 : tenant_service_handler(
1214 0 : r,
1215 0 : handle_tenant_timeline_passthrough,
1216 0 : RequestName("v1_tenant_passthrough"),
1217 0 : )
1218 0 : })
1219 0 : }
|