Line data Source code
1 : use std::time::Duration;
2 :
3 : use pageserver_api::models::detach_ancestor::AncestorDetached;
4 : use pageserver_api::models::{
5 : DetachBehavior, LocationConfig, LocationConfigListResponse, LsnLease, PageserverUtilization,
6 : SecondaryProgress, TenantScanRemoteStorageResponse, TenantShardSplitRequest,
7 : TenantShardSplitResponse, TenantWaitLsnRequest, TimelineArchivalConfigRequest,
8 : TimelineCreateRequest, TimelineInfo, TopTenantShardsRequest, TopTenantShardsResponse,
9 : };
10 : use pageserver_api::shard::TenantShardId;
11 : use pageserver_client::BlockUnblock;
12 : use pageserver_client::mgmt_api::{Client, Result};
13 : use reqwest::StatusCode;
14 : use utils::id::{NodeId, TenantId, TimelineId};
15 : use utils::lsn::Lsn;
16 :
17 : /// Thin wrapper around [`pageserver_client::mgmt_api::Client`]. It allows the storage
18 : /// controller to collect metrics in a non-intrusive manner.
19 : #[derive(Debug, Clone)]
20 : pub(crate) struct PageserverClient {
21 : inner: Client,
22 : node_id_label: String,
23 : }
24 :
25 : macro_rules! measured_request {
26 : ($name:literal, $method:expr, $node_id: expr, $invoke:expr) => {{
27 : let labels = crate::metrics::PageserverRequestLabelGroup {
28 : pageserver_id: $node_id,
29 : path: $name,
30 : method: $method,
31 : };
32 :
33 : let latency = &crate::metrics::METRICS_REGISTRY
34 : .metrics_group
35 : .storage_controller_pageserver_request_latency;
36 : let _timer_guard = latency.start_timer(labels.clone());
37 :
38 : let res = $invoke;
39 :
40 : if res.is_err() {
41 : let error_counters = &crate::metrics::METRICS_REGISTRY
42 : .metrics_group
43 : .storage_controller_pageserver_request_error;
44 : error_counters.inc(labels)
45 : }
46 :
47 : res
48 : }};
49 : }
50 :
51 : impl PageserverClient {
52 0 : pub(crate) fn new(
53 0 : node_id: NodeId,
54 0 : raw_client: reqwest::Client,
55 0 : mgmt_api_endpoint: String,
56 0 : jwt: Option<&str>,
57 0 : ) -> Self {
58 0 : Self {
59 0 : inner: Client::new(raw_client, mgmt_api_endpoint, jwt),
60 0 : node_id_label: node_id.0.to_string(),
61 0 : }
62 0 : }
63 :
64 0 : pub(crate) async fn tenant_delete(&self, tenant_shard_id: TenantShardId) -> Result<StatusCode> {
65 0 : measured_request!(
66 0 : "tenant",
67 0 : crate::metrics::Method::Delete,
68 0 : &self.node_id_label,
69 0 : self.inner.tenant_delete(tenant_shard_id).await
70 : )
71 0 : }
72 :
73 0 : pub(crate) async fn tenant_time_travel_remote_storage(
74 0 : &self,
75 0 : tenant_shard_id: TenantShardId,
76 0 : timestamp: &str,
77 0 : done_if_after: &str,
78 0 : ) -> Result<()> {
79 0 : measured_request!(
80 0 : "tenant_time_travel_remote_storage",
81 0 : crate::metrics::Method::Put,
82 0 : &self.node_id_label,
83 0 : self.inner
84 0 : .tenant_time_travel_remote_storage(tenant_shard_id, timestamp, done_if_after)
85 0 : .await
86 : )
87 0 : }
88 :
89 0 : pub(crate) async fn tenant_scan_remote_storage(
90 0 : &self,
91 0 : tenant_id: TenantId,
92 0 : ) -> Result<TenantScanRemoteStorageResponse> {
93 0 : measured_request!(
94 0 : "tenant_scan_remote_storage",
95 0 : crate::metrics::Method::Get,
96 0 : &self.node_id_label,
97 0 : self.inner.tenant_scan_remote_storage(tenant_id).await
98 : )
99 0 : }
100 :
101 0 : pub(crate) async fn tenant_secondary_download(
102 0 : &self,
103 0 : tenant_id: TenantShardId,
104 0 : wait: Option<std::time::Duration>,
105 0 : ) -> Result<(StatusCode, SecondaryProgress)> {
106 0 : measured_request!(
107 0 : "tenant_secondary_download",
108 0 : crate::metrics::Method::Post,
109 0 : &self.node_id_label,
110 0 : self.inner.tenant_secondary_download(tenant_id, wait).await
111 : )
112 0 : }
113 :
114 0 : pub(crate) async fn tenant_secondary_status(
115 0 : &self,
116 0 : tenant_shard_id: TenantShardId,
117 0 : ) -> Result<SecondaryProgress> {
118 0 : measured_request!(
119 0 : "tenant_secondary_status",
120 0 : crate::metrics::Method::Get,
121 0 : &self.node_id_label,
122 0 : self.inner.tenant_secondary_status(tenant_shard_id).await
123 : )
124 0 : }
125 :
126 0 : pub(crate) async fn tenant_heatmap_upload(&self, tenant_id: TenantShardId) -> Result<()> {
127 0 : measured_request!(
128 0 : "tenant_heatmap_upload",
129 0 : crate::metrics::Method::Post,
130 0 : &self.node_id_label,
131 0 : self.inner.tenant_heatmap_upload(tenant_id).await
132 : )
133 0 : }
134 :
135 0 : pub(crate) async fn location_config(
136 0 : &self,
137 0 : tenant_shard_id: TenantShardId,
138 0 : config: LocationConfig,
139 0 : flush_ms: Option<std::time::Duration>,
140 0 : lazy: bool,
141 0 : ) -> Result<()> {
142 0 : measured_request!(
143 0 : "location_config",
144 0 : crate::metrics::Method::Put,
145 0 : &self.node_id_label,
146 0 : self.inner
147 0 : .location_config(tenant_shard_id, config, flush_ms, lazy)
148 0 : .await
149 : )
150 0 : }
151 :
152 0 : pub(crate) async fn list_location_config(&self) -> Result<LocationConfigListResponse> {
153 0 : measured_request!(
154 0 : "location_configs",
155 0 : crate::metrics::Method::Get,
156 0 : &self.node_id_label,
157 0 : self.inner.list_location_config().await
158 : )
159 0 : }
160 :
161 0 : pub(crate) async fn get_location_config(
162 0 : &self,
163 0 : tenant_shard_id: TenantShardId,
164 0 : ) -> Result<Option<LocationConfig>> {
165 0 : measured_request!(
166 0 : "location_config",
167 0 : crate::metrics::Method::Get,
168 0 : &self.node_id_label,
169 0 : self.inner.get_location_config(tenant_shard_id).await
170 : )
171 0 : }
172 :
173 0 : pub(crate) async fn timeline_create(
174 0 : &self,
175 0 : tenant_shard_id: TenantShardId,
176 0 : req: &TimelineCreateRequest,
177 0 : ) -> Result<TimelineInfo> {
178 0 : measured_request!(
179 0 : "timeline",
180 0 : crate::metrics::Method::Post,
181 0 : &self.node_id_label,
182 0 : self.inner.timeline_create(tenant_shard_id, req).await
183 : )
184 0 : }
185 :
186 0 : pub(crate) async fn timeline_delete(
187 0 : &self,
188 0 : tenant_shard_id: TenantShardId,
189 0 : timeline_id: TimelineId,
190 0 : ) -> Result<StatusCode> {
191 0 : measured_request!(
192 0 : "timeline",
193 0 : crate::metrics::Method::Delete,
194 0 : &self.node_id_label,
195 0 : self.inner
196 0 : .timeline_delete(tenant_shard_id, timeline_id)
197 0 : .await
198 : )
199 0 : }
200 :
201 0 : pub(crate) async fn timeline_lease_lsn(
202 0 : &self,
203 0 : tenant_shard_id: TenantShardId,
204 0 : timeline_id: TimelineId,
205 0 : lsn: Lsn,
206 0 : ) -> Result<LsnLease> {
207 0 : measured_request!(
208 0 : "timeline_lease_lsn",
209 0 : crate::metrics::Method::Post,
210 0 : &self.node_id_label,
211 0 : self.inner
212 0 : .timeline_init_lsn_lease(tenant_shard_id, timeline_id, lsn)
213 0 : .await
214 : )
215 0 : }
216 :
217 : #[allow(unused)]
218 0 : pub(crate) async fn timeline_detail(
219 0 : &self,
220 0 : tenant_shard_id: TenantShardId,
221 0 : timeline_id: TimelineId,
222 0 : ) -> Result<TimelineInfo> {
223 0 : measured_request!(
224 0 : "timeline_detail",
225 0 : crate::metrics::Method::Get,
226 0 : &self.node_id_label,
227 0 : self.inner
228 0 : .timeline_detail(tenant_shard_id, timeline_id)
229 0 : .await
230 : )
231 0 : }
232 :
233 0 : pub(crate) async fn tenant_shard_split(
234 0 : &self,
235 0 : tenant_shard_id: TenantShardId,
236 0 : req: TenantShardSplitRequest,
237 0 : ) -> Result<TenantShardSplitResponse> {
238 0 : measured_request!(
239 0 : "tenant_shard_split",
240 0 : crate::metrics::Method::Put,
241 0 : &self.node_id_label,
242 0 : self.inner.tenant_shard_split(tenant_shard_id, req).await
243 : )
244 0 : }
245 :
246 0 : pub(crate) async fn timeline_list(
247 0 : &self,
248 0 : tenant_shard_id: &TenantShardId,
249 0 : ) -> Result<Vec<TimelineInfo>> {
250 0 : measured_request!(
251 0 : "timelines",
252 0 : crate::metrics::Method::Get,
253 0 : &self.node_id_label,
254 0 : self.inner.timeline_list(tenant_shard_id).await
255 : )
256 0 : }
257 :
258 0 : pub(crate) async fn timeline_archival_config(
259 0 : &self,
260 0 : tenant_shard_id: TenantShardId,
261 0 : timeline_id: TimelineId,
262 0 : req: &TimelineArchivalConfigRequest,
263 0 : ) -> Result<()> {
264 0 : measured_request!(
265 0 : "timeline_archival_config",
266 0 : crate::metrics::Method::Put,
267 0 : &self.node_id_label,
268 0 : self.inner
269 0 : .timeline_archival_config(tenant_shard_id, timeline_id, req)
270 0 : .await
271 : )
272 0 : }
273 :
274 0 : pub(crate) async fn timeline_detach_ancestor(
275 0 : &self,
276 0 : tenant_shard_id: TenantShardId,
277 0 : timeline_id: TimelineId,
278 0 : behavior: Option<DetachBehavior>,
279 0 : ) -> Result<AncestorDetached> {
280 0 : measured_request!(
281 0 : "timeline_detach_ancestor",
282 0 : crate::metrics::Method::Put,
283 0 : &self.node_id_label,
284 0 : self.inner
285 0 : .timeline_detach_ancestor(tenant_shard_id, timeline_id, behavior)
286 0 : .await
287 : )
288 0 : }
289 :
290 0 : pub(crate) async fn timeline_block_unblock_gc(
291 0 : &self,
292 0 : tenant_shard_id: TenantShardId,
293 0 : timeline_id: TimelineId,
294 0 : dir: BlockUnblock,
295 0 : ) -> Result<()> {
296 : // measuring these makes no sense because we synchronize with the gc loop and remote
297 : // storage on block_gc so there should be huge outliers
298 0 : measured_request!(
299 0 : "timeline_block_unblock_gc",
300 0 : crate::metrics::Method::Post,
301 0 : &self.node_id_label,
302 0 : self.inner
303 0 : .timeline_block_unblock_gc(tenant_shard_id, timeline_id, dir)
304 0 : .await
305 : )
306 0 : }
307 :
308 0 : pub(crate) async fn timeline_download_heatmap_layers(
309 0 : &self,
310 0 : tenant_shard_id: TenantShardId,
311 0 : timeline_id: TimelineId,
312 0 : concurrency: Option<usize>,
313 0 : recurse: bool,
314 0 : ) -> Result<()> {
315 0 : measured_request!(
316 0 : "download_heatmap_layers",
317 0 : crate::metrics::Method::Post,
318 0 : &self.node_id_label,
319 0 : self.inner
320 0 : .timeline_download_heatmap_layers(
321 0 : tenant_shard_id,
322 0 : timeline_id,
323 0 : concurrency,
324 0 : recurse
325 0 : )
326 0 : .await
327 : )
328 0 : }
329 :
330 0 : pub(crate) async fn get_utilization(&self) -> Result<PageserverUtilization> {
331 0 : measured_request!(
332 0 : "utilization",
333 0 : crate::metrics::Method::Get,
334 0 : &self.node_id_label,
335 0 : self.inner.get_utilization().await
336 : )
337 0 : }
338 :
339 0 : pub(crate) async fn top_tenant_shards(
340 0 : &self,
341 0 : request: TopTenantShardsRequest,
342 0 : ) -> Result<TopTenantShardsResponse> {
343 0 : measured_request!(
344 0 : "top_tenants",
345 0 : crate::metrics::Method::Post,
346 0 : &self.node_id_label,
347 0 : self.inner.top_tenant_shards(request).await
348 : )
349 0 : }
350 :
351 0 : pub(crate) async fn wait_lsn(
352 0 : &self,
353 0 : tenant_shard_id: TenantShardId,
354 0 : request: TenantWaitLsnRequest,
355 0 : ) -> Result<StatusCode> {
356 0 : measured_request!(
357 0 : "wait_lsn",
358 0 : crate::metrics::Method::Post,
359 0 : &self.node_id_label,
360 0 : self.inner.wait_lsn(tenant_shard_id, request).await
361 : )
362 0 : }
363 :
364 0 : pub(crate) async fn activate_post_import(
365 0 : &self,
366 0 : tenant_shard_id: TenantShardId,
367 0 : timeline_id: TimelineId,
368 0 : timeline_activate_timeout: Duration,
369 0 : ) -> Result<TimelineInfo> {
370 0 : measured_request!(
371 0 : "activate_post_import",
372 0 : crate::metrics::Method::Put,
373 0 : &self.node_id_label,
374 0 : self.inner
375 0 : .activate_post_import(tenant_shard_id, timeline_id, timeline_activate_timeout)
376 0 : .await
377 : )
378 0 : }
379 :
380 0 : pub(crate) async fn update_feature_flag_spec(&self, spec: String) -> Result<()> {
381 0 : measured_request!(
382 0 : "update_feature_flag_spec",
383 0 : crate::metrics::Method::Post,
384 0 : &self.node_id_label,
385 0 : self.inner.update_feature_flag_spec(spec).await
386 : )
387 0 : }
388 : }
|