Line data Source code
1 : pub mod detach_ancestor;
2 : pub mod partitioning;
3 : pub mod utilization;
4 :
5 : #[cfg(feature = "testing")]
6 : use camino::Utf8PathBuf;
7 : pub use utilization::PageserverUtilization;
8 :
9 : use core::ops::Range;
10 : use std::{
11 : collections::HashMap,
12 : fmt::Display,
13 : io::{BufRead, Read},
14 : num::{NonZeroU32, NonZeroU64, NonZeroUsize},
15 : str::FromStr,
16 : time::{Duration, SystemTime},
17 : };
18 :
19 : use byteorder::{BigEndian, ReadBytesExt};
20 : use postgres_ffi::BLCKSZ;
21 : use serde::{Deserialize, Deserializer, Serialize, Serializer};
22 : use serde_with::serde_as;
23 : use utils::{
24 : completion,
25 : id::{NodeId, TenantId, TimelineId},
26 : lsn::Lsn,
27 : postgres_client::PostgresClientProtocol,
28 : serde_system_time,
29 : };
30 :
31 : use crate::{
32 : key::Key,
33 : reltag::RelTag,
34 : shard::{ShardCount, ShardStripeSize, TenantShardId},
35 : };
36 : use anyhow::bail;
37 : use bytes::{Buf, BufMut, Bytes, BytesMut};
38 :
39 : /// The state of a tenant in this pageserver.
40 : ///
41 : /// ```mermaid
42 : /// stateDiagram-v2
43 : ///
44 : /// [*] --> Attaching: spawn_attach()
45 : ///
46 : /// Attaching --> Activating: activate()
47 : /// Activating --> Active: infallible
48 : ///
49 : /// Attaching --> Broken: attach() failure
50 : ///
51 : /// Active --> Stopping: set_stopping(), part of shutdown & detach
52 : /// Stopping --> Broken: late error in remove_tenant_from_memory
53 : ///
54 : /// Broken --> [*]: ignore / detach / shutdown
55 : /// Stopping --> [*]: remove_from_memory complete
56 : ///
57 : /// Active --> Broken: cfg(testing)-only tenant break point
58 : /// ```
59 : #[derive(
60 : Clone,
61 : PartialEq,
62 : Eq,
63 0 : serde::Serialize,
64 1 : serde::Deserialize,
65 : strum_macros::Display,
66 : strum_macros::VariantNames,
67 : strum_macros::AsRefStr,
68 : strum_macros::IntoStaticStr,
69 : )]
70 : #[serde(tag = "slug", content = "data")]
71 : pub enum TenantState {
72 : /// This tenant is being attached to the pageserver.
73 : ///
74 : /// `set_stopping()` and `set_broken()` do not work in this state and wait for it to pass.
75 : Attaching,
76 : /// The tenant is transitioning from Loading/Attaching to Active.
77 : ///
78 : /// While in this state, the individual timelines are being activated.
79 : ///
80 : /// `set_stopping()` and `set_broken()` do not work in this state and wait for it to pass.
81 : Activating(ActivatingFrom),
82 : /// The tenant has finished activating and is open for business.
83 : ///
84 : /// Transitions out of this state are possible through `set_stopping()` and `set_broken()`.
85 : Active,
86 : /// The tenant is recognized by pageserver, but it is being detached or the
87 : /// system is being shut down.
88 : ///
89 : /// Transitions out of this state are possible through `set_broken()`.
90 : Stopping {
91 : // Because of https://github.com/serde-rs/serde/issues/2105 this has to be a named field,
92 : // otherwise it will not be skipped during deserialization
93 : #[serde(skip)]
94 : progress: completion::Barrier,
95 : },
96 : /// The tenant is recognized by the pageserver, but can no longer be used for
97 : /// any operations.
98 : ///
99 : /// If the tenant fails to load or attach, it will transition to this state
100 : /// and it is guaranteed that no background tasks are running in its name.
101 : ///
102 : /// The other way to transition into this state is from `Stopping` state
103 : /// through `set_broken()` called from `remove_tenant_from_memory()`. That happens
104 : /// if the cleanup future executed by `remove_tenant_from_memory()` fails.
105 : Broken { reason: String, backtrace: String },
106 : }
107 :
108 : impl TenantState {
109 0 : pub fn attachment_status(&self) -> TenantAttachmentStatus {
110 : use TenantAttachmentStatus::*;
111 :
112 : // Below TenantState::Activating is used as "transient" or "transparent" state for
113 : // attachment_status determining.
114 0 : match self {
115 : // The attach procedure writes the marker file before adding the Attaching tenant to the tenants map.
116 : // So, technically, we can return Attached here.
117 : // However, as soon as Console observes Attached, it will proceed with the Postgres-level health check.
118 : // But, our attach task might still be fetching the remote timelines, etc.
119 : // So, return `Maybe` while Attaching, making Console wait for the attach task to finish.
120 0 : Self::Attaching | Self::Activating(ActivatingFrom::Attaching) => Maybe,
121 : // We only reach Active after successful load / attach.
122 : // So, call atttachment status Attached.
123 0 : Self::Active => Attached,
124 : // If the (initial or resumed) attach procedure fails, the tenant becomes Broken.
125 : // However, it also becomes Broken if the regular load fails.
126 : // From Console's perspective there's no practical difference
127 : // because attachment_status is polled by console only during attach operation execution.
128 0 : Self::Broken { reason, .. } => Failed {
129 0 : reason: reason.to_owned(),
130 0 : },
131 : // Why is Stopping a Maybe case? Because, during pageserver shutdown,
132 : // we set the Stopping state irrespective of whether the tenant
133 : // has finished attaching or not.
134 0 : Self::Stopping { .. } => Maybe,
135 : }
136 0 : }
137 :
138 0 : pub fn broken_from_reason(reason: String) -> Self {
139 0 : let backtrace_str: String = format!("{}", std::backtrace::Backtrace::force_capture());
140 0 : Self::Broken {
141 0 : reason,
142 0 : backtrace: backtrace_str,
143 0 : }
144 0 : }
145 : }
146 :
147 : impl std::fmt::Debug for TenantState {
148 2 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149 2 : match self {
150 2 : Self::Broken { reason, backtrace } if !reason.is_empty() => {
151 2 : write!(f, "Broken due to: {reason}. Backtrace:\n{backtrace}")
152 : }
153 0 : _ => write!(f, "{self}"),
154 : }
155 2 : }
156 : }
157 :
158 : /// A temporary lease to a specific lsn inside a timeline.
159 : /// Access to the lsn is guaranteed by the pageserver until the expiration indicated by `valid_until`.
160 : #[serde_as]
161 0 : #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
162 : pub struct LsnLease {
163 : #[serde_as(as = "SystemTimeAsRfc3339Millis")]
164 : pub valid_until: SystemTime,
165 : }
166 :
167 : serde_with::serde_conv!(
168 : SystemTimeAsRfc3339Millis,
169 : SystemTime,
170 0 : |time: &SystemTime| humantime::format_rfc3339_millis(*time).to_string(),
171 0 : |value: String| -> Result<_, humantime::TimestampError> { humantime::parse_rfc3339(&value) }
172 : );
173 :
174 : impl LsnLease {
175 : /// The default length for an explicit LSN lease request (10 minutes).
176 : pub const DEFAULT_LENGTH: Duration = Duration::from_secs(10 * 60);
177 :
178 : /// The default length for an implicit LSN lease granted during
179 : /// `get_lsn_by_timestamp` request (1 minutes).
180 : pub const DEFAULT_LENGTH_FOR_TS: Duration = Duration::from_secs(60);
181 :
182 : /// Checks whether the lease is expired.
183 6 : pub fn is_expired(&self, now: &SystemTime) -> bool {
184 6 : now > &self.valid_until
185 6 : }
186 : }
187 :
188 : /// The only [`TenantState`] variants we could be `TenantState::Activating` from.
189 : ///
190 : /// XXX: We used to have more variants here, but now it's just one, which makes this rather
191 : /// useless. Remove, once we've checked that there's no client code left that looks at this.
192 1 : #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
193 : pub enum ActivatingFrom {
194 : /// Arrived to [`TenantState::Activating`] from [`TenantState::Attaching`]
195 : Attaching,
196 : }
197 :
198 : /// A state of a timeline in pageserver's memory.
199 0 : #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
200 : pub enum TimelineState {
201 : /// The timeline is recognized by the pageserver but is not yet operational.
202 : /// In particular, the walreceiver connection loop is not running for this timeline.
203 : /// It will eventually transition to state Active or Broken.
204 : Loading,
205 : /// The timeline is fully operational.
206 : /// It can be queried, and the walreceiver connection loop is running.
207 : Active,
208 : /// The timeline was previously Loading or Active but is shutting down.
209 : /// It cannot transition back into any other state.
210 : Stopping,
211 : /// The timeline is broken and not operational (previous states: Loading or Active).
212 : Broken { reason: String, backtrace: String },
213 : }
214 :
215 : #[serde_with::serde_as]
216 0 : #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
217 : pub struct CompactLsnRange {
218 : pub start: Lsn,
219 : pub end: Lsn,
220 : }
221 :
222 : #[serde_with::serde_as]
223 0 : #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
224 : pub struct CompactKeyRange {
225 : #[serde_as(as = "serde_with::DisplayFromStr")]
226 : pub start: Key,
227 : #[serde_as(as = "serde_with::DisplayFromStr")]
228 : pub end: Key,
229 : }
230 :
231 : impl From<Range<Lsn>> for CompactLsnRange {
232 6 : fn from(range: Range<Lsn>) -> Self {
233 6 : Self {
234 6 : start: range.start,
235 6 : end: range.end,
236 6 : }
237 6 : }
238 : }
239 :
240 : impl From<Range<Key>> for CompactKeyRange {
241 16 : fn from(range: Range<Key>) -> Self {
242 16 : Self {
243 16 : start: range.start,
244 16 : end: range.end,
245 16 : }
246 16 : }
247 : }
248 :
249 : impl From<CompactLsnRange> for Range<Lsn> {
250 10 : fn from(range: CompactLsnRange) -> Self {
251 10 : range.start..range.end
252 10 : }
253 : }
254 :
255 : impl From<CompactKeyRange> for Range<Key> {
256 16 : fn from(range: CompactKeyRange) -> Self {
257 16 : range.start..range.end
258 16 : }
259 : }
260 :
261 : impl CompactLsnRange {
262 4 : pub fn above(lsn: Lsn) -> Self {
263 4 : Self {
264 4 : start: lsn,
265 4 : end: Lsn::MAX,
266 4 : }
267 4 : }
268 : }
269 :
270 : #[derive(Debug, Clone, Serialize)]
271 : pub struct CompactInfoResponse {
272 : pub compact_key_range: Option<CompactKeyRange>,
273 : pub compact_lsn_range: Option<CompactLsnRange>,
274 : pub sub_compaction: bool,
275 : }
276 :
277 0 : #[derive(Serialize, Deserialize, Clone)]
278 : pub struct TimelineCreateRequest {
279 : pub new_timeline_id: TimelineId,
280 : #[serde(flatten)]
281 : pub mode: TimelineCreateRequestMode,
282 : }
283 :
284 0 : #[derive(Serialize, Deserialize, Clone)]
285 : #[serde(untagged)]
286 : pub enum TimelineCreateRequestMode {
287 : Branch {
288 : ancestor_timeline_id: TimelineId,
289 : #[serde(default)]
290 : ancestor_start_lsn: Option<Lsn>,
291 : // TODO: cplane sets this, but, the branching code always
292 : // inherits the ancestor's pg_version. Earlier code wasn't
293 : // using a flattened enum, so, it was an accepted field, and
294 : // we continue to accept it by having it here.
295 : pg_version: Option<u32>,
296 : },
297 : ImportPgdata {
298 : import_pgdata: TimelineCreateRequestModeImportPgdata,
299 : },
300 : // NB: Bootstrap is all-optional, and thus the serde(untagged) will cause serde to stop at Bootstrap.
301 : // (serde picks the first matching enum variant, in declaration order).
302 : Bootstrap {
303 : #[serde(default)]
304 : existing_initdb_timeline_id: Option<TimelineId>,
305 : pg_version: Option<u32>,
306 : },
307 : }
308 :
309 0 : #[derive(Serialize, Deserialize, Clone)]
310 : pub struct TimelineCreateRequestModeImportPgdata {
311 : pub location: ImportPgdataLocation,
312 : pub idempotency_key: ImportPgdataIdempotencyKey,
313 : }
314 :
315 0 : #[derive(Serialize, Deserialize, Clone, Debug)]
316 : pub enum ImportPgdataLocation {
317 : #[cfg(feature = "testing")]
318 : LocalFs { path: Utf8PathBuf },
319 : AwsS3 {
320 : region: String,
321 : bucket: String,
322 : /// A better name for this would be `prefix`; changing requires coordination with cplane.
323 : /// See <https://github.com/neondatabase/cloud/issues/20646>.
324 : key: String,
325 : },
326 : }
327 :
328 0 : #[derive(Serialize, Deserialize, Clone)]
329 : #[serde(transparent)]
330 : pub struct ImportPgdataIdempotencyKey(pub String);
331 :
332 : impl ImportPgdataIdempotencyKey {
333 0 : pub fn random() -> Self {
334 : use rand::{distributions::Alphanumeric, Rng};
335 0 : Self(
336 0 : rand::thread_rng()
337 0 : .sample_iter(&Alphanumeric)
338 0 : .take(20)
339 0 : .map(char::from)
340 0 : .collect(),
341 0 : )
342 0 : }
343 : }
344 :
345 0 : #[derive(Serialize, Deserialize, Clone)]
346 : pub struct LsnLeaseRequest {
347 : pub lsn: Lsn,
348 : }
349 :
350 0 : #[derive(Serialize, Deserialize)]
351 : pub struct TenantShardSplitRequest {
352 : pub new_shard_count: u8,
353 :
354 : // A tenant's stripe size is only meaningful the first time their shard count goes
355 : // above 1: therefore during a split from 1->N shards, we may modify the stripe size.
356 : //
357 : // If this is set while the stripe count is being increased from an already >1 value,
358 : // then the request will fail with 400.
359 : pub new_stripe_size: Option<ShardStripeSize>,
360 : }
361 :
362 0 : #[derive(Serialize, Deserialize)]
363 : pub struct TenantShardSplitResponse {
364 : pub new_shards: Vec<TenantShardId>,
365 : }
366 :
367 : /// Parameters that apply to all shards in a tenant. Used during tenant creation.
368 0 : #[derive(Serialize, Deserialize, Debug)]
369 : #[serde(deny_unknown_fields)]
370 : pub struct ShardParameters {
371 : pub count: ShardCount,
372 : pub stripe_size: ShardStripeSize,
373 : }
374 :
375 : impl ShardParameters {
376 : pub const DEFAULT_STRIPE_SIZE: ShardStripeSize = ShardStripeSize(256 * 1024 / 8);
377 :
378 0 : pub fn is_unsharded(&self) -> bool {
379 0 : self.count.is_unsharded()
380 0 : }
381 : }
382 :
383 : impl Default for ShardParameters {
384 197 : fn default() -> Self {
385 197 : Self {
386 197 : count: ShardCount::new(0),
387 197 : stripe_size: Self::DEFAULT_STRIPE_SIZE,
388 197 : }
389 197 : }
390 : }
391 :
392 : #[derive(Debug, Default, Clone, Eq, PartialEq)]
393 : pub enum FieldPatch<T> {
394 : Upsert(T),
395 : Remove,
396 : #[default]
397 : Noop,
398 : }
399 :
400 : impl<T> FieldPatch<T> {
401 48 : fn is_noop(&self) -> bool {
402 48 : matches!(self, FieldPatch::Noop)
403 48 : }
404 :
405 24 : pub fn apply(self, target: &mut Option<T>) {
406 24 : match self {
407 1 : Self::Upsert(v) => *target = Some(v),
408 1 : Self::Remove => *target = None,
409 22 : Self::Noop => {}
410 : }
411 24 : }
412 :
413 0 : pub fn map<U, E, F: FnOnce(T) -> Result<U, E>>(self, map: F) -> Result<FieldPatch<U>, E> {
414 0 : match self {
415 0 : Self::Upsert(v) => Ok(FieldPatch::<U>::Upsert(map(v)?)),
416 0 : Self::Remove => Ok(FieldPatch::<U>::Remove),
417 0 : Self::Noop => Ok(FieldPatch::<U>::Noop),
418 : }
419 0 : }
420 : }
421 :
422 : impl<'de, T: Deserialize<'de>> Deserialize<'de> for FieldPatch<T> {
423 2 : fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
424 2 : where
425 2 : D: Deserializer<'de>,
426 2 : {
427 2 : Option::deserialize(deserializer).map(|opt| match opt {
428 1 : None => FieldPatch::Remove,
429 1 : Some(val) => FieldPatch::Upsert(val),
430 2 : })
431 2 : }
432 : }
433 :
434 : impl<T: Serialize> Serialize for FieldPatch<T> {
435 2 : fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
436 2 : where
437 2 : S: Serializer,
438 2 : {
439 2 : match self {
440 1 : FieldPatch::Upsert(val) => serializer.serialize_some(val),
441 1 : FieldPatch::Remove => serializer.serialize_none(),
442 0 : FieldPatch::Noop => unreachable!(),
443 : }
444 2 : }
445 : }
446 :
447 2 : #[derive(Serialize, Deserialize, Debug, Default, Clone, Eq, PartialEq)]
448 : #[serde(default)]
449 : pub struct TenantConfigPatch {
450 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
451 : pub checkpoint_distance: FieldPatch<u64>,
452 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
453 : pub checkpoint_timeout: FieldPatch<String>,
454 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
455 : pub compaction_target_size: FieldPatch<u64>,
456 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
457 : pub compaction_period: FieldPatch<String>,
458 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
459 : pub compaction_threshold: FieldPatch<usize>,
460 : // defer parsing compaction_algorithm, like eviction_policy
461 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
462 : pub compaction_algorithm: FieldPatch<CompactionAlgorithmSettings>,
463 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
464 : pub gc_horizon: FieldPatch<u64>,
465 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
466 : pub gc_period: FieldPatch<String>,
467 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
468 : pub image_creation_threshold: FieldPatch<usize>,
469 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
470 : pub pitr_interval: FieldPatch<String>,
471 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
472 : pub walreceiver_connect_timeout: FieldPatch<String>,
473 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
474 : pub lagging_wal_timeout: FieldPatch<String>,
475 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
476 : pub max_lsn_wal_lag: FieldPatch<NonZeroU64>,
477 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
478 : pub eviction_policy: FieldPatch<EvictionPolicy>,
479 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
480 : pub min_resident_size_override: FieldPatch<u64>,
481 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
482 : pub evictions_low_residence_duration_metric_threshold: FieldPatch<String>,
483 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
484 : pub heatmap_period: FieldPatch<String>,
485 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
486 : pub lazy_slru_download: FieldPatch<bool>,
487 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
488 : pub timeline_get_throttle: FieldPatch<ThrottleConfig>,
489 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
490 : pub image_layer_creation_check_threshold: FieldPatch<u8>,
491 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
492 : pub lsn_lease_length: FieldPatch<String>,
493 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
494 : pub lsn_lease_length_for_ts: FieldPatch<String>,
495 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
496 : pub timeline_offloading: FieldPatch<bool>,
497 : #[serde(skip_serializing_if = "FieldPatch::is_noop")]
498 : pub wal_receiver_protocol_override: FieldPatch<PostgresClientProtocol>,
499 : }
500 :
501 : /// An alternative representation of `pageserver::tenant::TenantConf` with
502 : /// simpler types.
503 0 : #[derive(Serialize, Deserialize, Debug, Default, Clone, Eq, PartialEq)]
504 : pub struct TenantConfig {
505 : pub checkpoint_distance: Option<u64>,
506 : pub checkpoint_timeout: Option<String>,
507 : pub compaction_target_size: Option<u64>,
508 : pub compaction_period: Option<String>,
509 : pub compaction_threshold: Option<usize>,
510 : // defer parsing compaction_algorithm, like eviction_policy
511 : pub compaction_algorithm: Option<CompactionAlgorithmSettings>,
512 : pub gc_horizon: Option<u64>,
513 : pub gc_period: Option<String>,
514 : pub image_creation_threshold: Option<usize>,
515 : pub pitr_interval: Option<String>,
516 : pub walreceiver_connect_timeout: Option<String>,
517 : pub lagging_wal_timeout: Option<String>,
518 : pub max_lsn_wal_lag: Option<NonZeroU64>,
519 : pub eviction_policy: Option<EvictionPolicy>,
520 : pub min_resident_size_override: Option<u64>,
521 : pub evictions_low_residence_duration_metric_threshold: Option<String>,
522 : pub heatmap_period: Option<String>,
523 : pub lazy_slru_download: Option<bool>,
524 : pub timeline_get_throttle: Option<ThrottleConfig>,
525 : pub image_layer_creation_check_threshold: Option<u8>,
526 : pub lsn_lease_length: Option<String>,
527 : pub lsn_lease_length_for_ts: Option<String>,
528 : pub timeline_offloading: Option<bool>,
529 : pub wal_receiver_protocol_override: Option<PostgresClientProtocol>,
530 : }
531 :
532 : impl TenantConfig {
533 1 : pub fn apply_patch(self, patch: TenantConfigPatch) -> TenantConfig {
534 1 : let Self {
535 1 : mut checkpoint_distance,
536 1 : mut checkpoint_timeout,
537 1 : mut compaction_target_size,
538 1 : mut compaction_period,
539 1 : mut compaction_threshold,
540 1 : mut compaction_algorithm,
541 1 : mut gc_horizon,
542 1 : mut gc_period,
543 1 : mut image_creation_threshold,
544 1 : mut pitr_interval,
545 1 : mut walreceiver_connect_timeout,
546 1 : mut lagging_wal_timeout,
547 1 : mut max_lsn_wal_lag,
548 1 : mut eviction_policy,
549 1 : mut min_resident_size_override,
550 1 : mut evictions_low_residence_duration_metric_threshold,
551 1 : mut heatmap_period,
552 1 : mut lazy_slru_download,
553 1 : mut timeline_get_throttle,
554 1 : mut image_layer_creation_check_threshold,
555 1 : mut lsn_lease_length,
556 1 : mut lsn_lease_length_for_ts,
557 1 : mut timeline_offloading,
558 1 : mut wal_receiver_protocol_override,
559 1 : } = self;
560 1 :
561 1 : patch.checkpoint_distance.apply(&mut checkpoint_distance);
562 1 : patch.checkpoint_timeout.apply(&mut checkpoint_timeout);
563 1 : patch
564 1 : .compaction_target_size
565 1 : .apply(&mut compaction_target_size);
566 1 : patch.compaction_period.apply(&mut compaction_period);
567 1 : patch.compaction_threshold.apply(&mut compaction_threshold);
568 1 : patch.compaction_algorithm.apply(&mut compaction_algorithm);
569 1 : patch.gc_horizon.apply(&mut gc_horizon);
570 1 : patch.gc_period.apply(&mut gc_period);
571 1 : patch
572 1 : .image_creation_threshold
573 1 : .apply(&mut image_creation_threshold);
574 1 : patch.pitr_interval.apply(&mut pitr_interval);
575 1 : patch
576 1 : .walreceiver_connect_timeout
577 1 : .apply(&mut walreceiver_connect_timeout);
578 1 : patch.lagging_wal_timeout.apply(&mut lagging_wal_timeout);
579 1 : patch.max_lsn_wal_lag.apply(&mut max_lsn_wal_lag);
580 1 : patch.eviction_policy.apply(&mut eviction_policy);
581 1 : patch
582 1 : .min_resident_size_override
583 1 : .apply(&mut min_resident_size_override);
584 1 : patch
585 1 : .evictions_low_residence_duration_metric_threshold
586 1 : .apply(&mut evictions_low_residence_duration_metric_threshold);
587 1 : patch.heatmap_period.apply(&mut heatmap_period);
588 1 : patch.lazy_slru_download.apply(&mut lazy_slru_download);
589 1 : patch
590 1 : .timeline_get_throttle
591 1 : .apply(&mut timeline_get_throttle);
592 1 : patch
593 1 : .image_layer_creation_check_threshold
594 1 : .apply(&mut image_layer_creation_check_threshold);
595 1 : patch.lsn_lease_length.apply(&mut lsn_lease_length);
596 1 : patch
597 1 : .lsn_lease_length_for_ts
598 1 : .apply(&mut lsn_lease_length_for_ts);
599 1 : patch.timeline_offloading.apply(&mut timeline_offloading);
600 1 : patch
601 1 : .wal_receiver_protocol_override
602 1 : .apply(&mut wal_receiver_protocol_override);
603 1 :
604 1 : Self {
605 1 : checkpoint_distance,
606 1 : checkpoint_timeout,
607 1 : compaction_target_size,
608 1 : compaction_period,
609 1 : compaction_threshold,
610 1 : compaction_algorithm,
611 1 : gc_horizon,
612 1 : gc_period,
613 1 : image_creation_threshold,
614 1 : pitr_interval,
615 1 : walreceiver_connect_timeout,
616 1 : lagging_wal_timeout,
617 1 : max_lsn_wal_lag,
618 1 : eviction_policy,
619 1 : min_resident_size_override,
620 1 : evictions_low_residence_duration_metric_threshold,
621 1 : heatmap_period,
622 1 : lazy_slru_download,
623 1 : timeline_get_throttle,
624 1 : image_layer_creation_check_threshold,
625 1 : lsn_lease_length,
626 1 : lsn_lease_length_for_ts,
627 1 : timeline_offloading,
628 1 : wal_receiver_protocol_override,
629 1 : }
630 1 : }
631 : }
632 :
633 : /// The policy for the aux file storage.
634 : ///
635 : /// It can be switched through `switch_aux_file_policy` tenant config.
636 : /// When the first aux file written, the policy will be persisted in the
637 : /// `index_part.json` file and has a limited migration path.
638 : ///
639 : /// Currently, we only allow the following migration path:
640 : ///
641 : /// Unset -> V1
642 : /// -> V2
643 : /// -> CrossValidation -> V2
644 : #[derive(
645 : Eq,
646 : PartialEq,
647 : Debug,
648 : Copy,
649 : Clone,
650 0 : strum_macros::EnumString,
651 : strum_macros::Display,
652 2 : serde_with::DeserializeFromStr,
653 : serde_with::SerializeDisplay,
654 : )]
655 : #[strum(serialize_all = "kebab-case")]
656 : pub enum AuxFilePolicy {
657 : /// V1 aux file policy: store everything in AUX_FILE_KEY
658 : #[strum(ascii_case_insensitive)]
659 : V1,
660 : /// V2 aux file policy: store in the AUX_FILE keyspace
661 : #[strum(ascii_case_insensitive)]
662 : V2,
663 : /// Cross validation runs both formats on the write path and does validation
664 : /// on the read path.
665 : #[strum(ascii_case_insensitive)]
666 : CrossValidation,
667 : }
668 :
669 0 : #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
670 : #[serde(tag = "kind")]
671 : pub enum EvictionPolicy {
672 : NoEviction,
673 : LayerAccessThreshold(EvictionPolicyLayerAccessThreshold),
674 : OnlyImitiate(EvictionPolicyLayerAccessThreshold),
675 : }
676 :
677 : impl EvictionPolicy {
678 0 : pub fn discriminant_str(&self) -> &'static str {
679 0 : match self {
680 0 : EvictionPolicy::NoEviction => "NoEviction",
681 0 : EvictionPolicy::LayerAccessThreshold(_) => "LayerAccessThreshold",
682 0 : EvictionPolicy::OnlyImitiate(_) => "OnlyImitiate",
683 : }
684 0 : }
685 : }
686 :
687 : #[derive(
688 : Eq,
689 : PartialEq,
690 : Debug,
691 : Copy,
692 : Clone,
693 0 : strum_macros::EnumString,
694 : strum_macros::Display,
695 0 : serde_with::DeserializeFromStr,
696 : serde_with::SerializeDisplay,
697 : )]
698 : #[strum(serialize_all = "kebab-case")]
699 : pub enum CompactionAlgorithm {
700 : Legacy,
701 : Tiered,
702 : }
703 :
704 : #[derive(
705 4 : Debug, Clone, Copy, PartialEq, Eq, serde_with::DeserializeFromStr, serde_with::SerializeDisplay,
706 : )]
707 : pub enum ImageCompressionAlgorithm {
708 : // Disabled for writes, support decompressing during read path
709 : Disabled,
710 : /// Zstandard compression. Level 0 means and None mean the same (default level). Levels can be negative as well.
711 : /// For details, see the [manual](http://facebook.github.io/zstd/zstd_manual.html).
712 : Zstd {
713 : level: Option<i8>,
714 : },
715 : }
716 :
717 : impl FromStr for ImageCompressionAlgorithm {
718 : type Err = anyhow::Error;
719 8 : fn from_str(s: &str) -> Result<Self, Self::Err> {
720 8 : let mut components = s.split(['(', ')']);
721 8 : let first = components
722 8 : .next()
723 8 : .ok_or_else(|| anyhow::anyhow!("empty string"))?;
724 8 : match first {
725 8 : "disabled" => Ok(ImageCompressionAlgorithm::Disabled),
726 6 : "zstd" => {
727 6 : let level = if let Some(v) = components.next() {
728 4 : let v: i8 = v.parse()?;
729 4 : Some(v)
730 : } else {
731 2 : None
732 : };
733 :
734 6 : Ok(ImageCompressionAlgorithm::Zstd { level })
735 : }
736 0 : _ => anyhow::bail!("invalid specifier '{first}'"),
737 : }
738 8 : }
739 : }
740 :
741 : impl Display for ImageCompressionAlgorithm {
742 12 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
743 12 : match self {
744 3 : ImageCompressionAlgorithm::Disabled => write!(f, "disabled"),
745 9 : ImageCompressionAlgorithm::Zstd { level } => {
746 9 : if let Some(level) = level {
747 6 : write!(f, "zstd({})", level)
748 : } else {
749 3 : write!(f, "zstd")
750 : }
751 : }
752 : }
753 12 : }
754 : }
755 :
756 0 : #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
757 : pub struct CompactionAlgorithmSettings {
758 : pub kind: CompactionAlgorithm,
759 : }
760 :
761 4 : #[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
762 : #[serde(tag = "mode", rename_all = "kebab-case", deny_unknown_fields)]
763 : pub enum L0FlushConfig {
764 : #[serde(rename_all = "snake_case")]
765 : Direct { max_concurrency: NonZeroUsize },
766 : }
767 :
768 0 : #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
769 : pub struct EvictionPolicyLayerAccessThreshold {
770 : #[serde(with = "humantime_serde")]
771 : pub period: Duration,
772 : #[serde(with = "humantime_serde")]
773 : pub threshold: Duration,
774 : }
775 :
776 6 : #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
777 : pub struct ThrottleConfig {
778 : /// See [`ThrottleConfigTaskKinds`] for why we do the serde `rename`.
779 : #[serde(rename = "task_kinds")]
780 : pub enabled: ThrottleConfigTaskKinds,
781 : pub initial: u32,
782 : #[serde(with = "humantime_serde")]
783 : pub refill_interval: Duration,
784 : pub refill_amount: NonZeroU32,
785 : pub max: u32,
786 : }
787 :
788 : /// Before <https://github.com/neondatabase/neon/pull/9962>
789 : /// the throttle was a per `Timeline::get`/`Timeline::get_vectored` call.
790 : /// The `task_kinds` field controlled which Pageserver "Task Kind"s
791 : /// were subject to the throttle.
792 : ///
793 : /// After that PR, the throttle is applied at pagestream request level
794 : /// and the `task_kinds` field does not apply since the only task kind
795 : /// that us subject to the throttle is that of the page service.
796 : ///
797 : /// However, we don't want to make a breaking config change right now
798 : /// because it means we have to migrate all the tenant configs.
799 : /// This will be done in a future PR.
800 : ///
801 : /// In the meantime, we use emptiness / non-emptsiness of the `task_kinds`
802 : /// field to determine if the throttle is enabled or not.
803 1 : #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
804 : #[serde(transparent)]
805 : pub struct ThrottleConfigTaskKinds(Vec<String>);
806 :
807 : impl ThrottleConfigTaskKinds {
808 403 : pub fn disabled() -> Self {
809 403 : Self(vec![])
810 403 : }
811 198 : pub fn is_enabled(&self) -> bool {
812 198 : !self.0.is_empty()
813 198 : }
814 : }
815 :
816 : impl ThrottleConfig {
817 403 : pub fn disabled() -> Self {
818 403 : Self {
819 403 : enabled: ThrottleConfigTaskKinds::disabled(),
820 403 : // other values don't matter with emtpy `task_kinds`.
821 403 : initial: 0,
822 403 : refill_interval: Duration::from_millis(1),
823 403 : refill_amount: NonZeroU32::new(1).unwrap(),
824 403 : max: 1,
825 403 : }
826 403 : }
827 : /// The requests per second allowed by the given config.
828 0 : pub fn steady_rps(&self) -> f64 {
829 0 : (self.refill_amount.get() as f64) / (self.refill_interval.as_secs_f64())
830 0 : }
831 : }
832 :
833 : #[cfg(test)]
834 : mod throttle_config_tests {
835 : use super::*;
836 :
837 : #[test]
838 1 : fn test_disabled_is_disabled() {
839 1 : let config = ThrottleConfig::disabled();
840 1 : assert!(!config.enabled.is_enabled());
841 1 : }
842 : #[test]
843 1 : fn test_enabled_backwards_compat() {
844 1 : let input = serde_json::json!({
845 1 : "task_kinds": ["PageRequestHandler"],
846 1 : "initial": 40000,
847 1 : "refill_interval": "50ms",
848 1 : "refill_amount": 1000,
849 1 : "max": 40000,
850 1 : "fair": true
851 1 : });
852 1 : let config: ThrottleConfig = serde_json::from_value(input).unwrap();
853 1 : assert!(config.enabled.is_enabled());
854 1 : }
855 : }
856 :
857 : /// A flattened analog of a `pagesever::tenant::LocationMode`, which
858 : /// lists out all possible states (and the virtual "Detached" state)
859 : /// in a flat form rather than using rust-style enums.
860 0 : #[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq)]
861 : pub enum LocationConfigMode {
862 : AttachedSingle,
863 : AttachedMulti,
864 : AttachedStale,
865 : Secondary,
866 : Detached,
867 : }
868 :
869 0 : #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
870 : pub struct LocationConfigSecondary {
871 : pub warm: bool,
872 : }
873 :
874 : /// An alternative representation of `pageserver::tenant::LocationConf`,
875 : /// for use in external-facing APIs.
876 0 : #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
877 : pub struct LocationConfig {
878 : pub mode: LocationConfigMode,
879 : /// If attaching, in what generation?
880 : #[serde(default)]
881 : pub generation: Option<u32>,
882 :
883 : // If requesting mode `Secondary`, configuration for that.
884 : #[serde(default)]
885 : pub secondary_conf: Option<LocationConfigSecondary>,
886 :
887 : // Shard parameters: if shard_count is nonzero, then other shard_* fields
888 : // must be set accurately.
889 : #[serde(default)]
890 : pub shard_number: u8,
891 : #[serde(default)]
892 : pub shard_count: u8,
893 : #[serde(default)]
894 : pub shard_stripe_size: u32,
895 :
896 : // This configuration only affects attached mode, but should be provided irrespective
897 : // of the mode, as a secondary location might transition on startup if the response
898 : // to the `/re-attach` control plane API requests it.
899 : pub tenant_conf: TenantConfig,
900 : }
901 :
902 0 : #[derive(Serialize, Deserialize)]
903 : pub struct LocationConfigListResponse {
904 : pub tenant_shards: Vec<(TenantShardId, Option<LocationConfig>)>,
905 : }
906 :
907 : #[derive(Serialize)]
908 : pub struct StatusResponse {
909 : pub id: NodeId,
910 : }
911 :
912 0 : #[derive(Serialize, Deserialize, Debug)]
913 : #[serde(deny_unknown_fields)]
914 : pub struct TenantLocationConfigRequest {
915 : #[serde(flatten)]
916 : pub config: LocationConfig, // as we have a flattened field, we should reject all unknown fields in it
917 : }
918 :
919 0 : #[derive(Serialize, Deserialize, Debug)]
920 : #[serde(deny_unknown_fields)]
921 : pub struct TenantTimeTravelRequest {
922 : pub shard_counts: Vec<ShardCount>,
923 : }
924 :
925 0 : #[derive(Serialize, Deserialize, Debug)]
926 : #[serde(deny_unknown_fields)]
927 : pub struct TenantShardLocation {
928 : pub shard_id: TenantShardId,
929 : pub node_id: NodeId,
930 : }
931 :
932 0 : #[derive(Serialize, Deserialize, Debug)]
933 : #[serde(deny_unknown_fields)]
934 : pub struct TenantLocationConfigResponse {
935 : pub shards: Vec<TenantShardLocation>,
936 : // If the shards' ShardCount count is >1, stripe_size will be set.
937 : pub stripe_size: Option<ShardStripeSize>,
938 : }
939 :
940 2 : #[derive(Serialize, Deserialize, Debug)]
941 : #[serde(deny_unknown_fields)]
942 : pub struct TenantConfigRequest {
943 : pub tenant_id: TenantId,
944 : #[serde(flatten)]
945 : pub config: TenantConfig, // as we have a flattened field, we should reject all unknown fields in it
946 : }
947 :
948 : impl std::ops::Deref for TenantConfigRequest {
949 : type Target = TenantConfig;
950 :
951 0 : fn deref(&self) -> &Self::Target {
952 0 : &self.config
953 0 : }
954 : }
955 :
956 : impl TenantConfigRequest {
957 0 : pub fn new(tenant_id: TenantId) -> TenantConfigRequest {
958 0 : let config = TenantConfig::default();
959 0 : TenantConfigRequest { tenant_id, config }
960 0 : }
961 : }
962 :
963 3 : #[derive(Serialize, Deserialize, Debug)]
964 : #[serde(deny_unknown_fields)]
965 : pub struct TenantConfigPatchRequest {
966 : pub tenant_id: TenantId,
967 : #[serde(flatten)]
968 : pub config: TenantConfigPatch, // as we have a flattened field, we should reject all unknown fields in it
969 : }
970 :
971 : /// See [`TenantState::attachment_status`] and the OpenAPI docs for context.
972 0 : #[derive(Serialize, Deserialize, Clone)]
973 : #[serde(tag = "slug", content = "data", rename_all = "snake_case")]
974 : pub enum TenantAttachmentStatus {
975 : Maybe,
976 : Attached,
977 : Failed { reason: String },
978 : }
979 :
980 0 : #[derive(Serialize, Deserialize, Clone)]
981 : pub struct TenantInfo {
982 : pub id: TenantShardId,
983 : // NB: intentionally not part of OpenAPI, we don't want to commit to a specific set of TenantState's
984 : pub state: TenantState,
985 : /// Sum of the size of all layer files.
986 : /// If a layer is present in both local FS and S3, it counts only once.
987 : pub current_physical_size: Option<u64>, // physical size is only included in `tenant_status` endpoint
988 : pub attachment_status: TenantAttachmentStatus,
989 : pub generation: u32,
990 :
991 : /// Opaque explanation if gc is being blocked.
992 : ///
993 : /// Only looked up for the individual tenant detail, not the listing. This is purely for
994 : /// debugging, not included in openapi.
995 : #[serde(skip_serializing_if = "Option::is_none")]
996 : pub gc_blocking: Option<String>,
997 : }
998 :
999 0 : #[derive(Serialize, Deserialize, Clone)]
1000 : pub struct TenantDetails {
1001 : #[serde(flatten)]
1002 : pub tenant_info: TenantInfo,
1003 :
1004 : pub walredo: Option<WalRedoManagerStatus>,
1005 :
1006 : pub timelines: Vec<TimelineId>,
1007 : }
1008 :
1009 0 : #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Debug)]
1010 : pub enum TimelineArchivalState {
1011 : Archived,
1012 : Unarchived,
1013 : }
1014 :
1015 0 : #[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
1016 : pub struct TimelineArchivalConfigRequest {
1017 : pub state: TimelineArchivalState,
1018 : }
1019 :
1020 0 : #[derive(Debug, Serialize, Deserialize, Clone)]
1021 : pub struct TimelinesInfoAndOffloaded {
1022 : pub timelines: Vec<TimelineInfo>,
1023 : pub offloaded: Vec<OffloadedTimelineInfo>,
1024 : }
1025 :
1026 : /// Analog of [`TimelineInfo`] for offloaded timelines.
1027 0 : #[derive(Debug, Serialize, Deserialize, Clone)]
1028 : pub struct OffloadedTimelineInfo {
1029 : pub tenant_id: TenantShardId,
1030 : pub timeline_id: TimelineId,
1031 : /// Whether the timeline has a parent it has been branched off from or not
1032 : pub ancestor_timeline_id: Option<TimelineId>,
1033 : /// Whether to retain the branch lsn at the ancestor or not
1034 : pub ancestor_retain_lsn: Option<Lsn>,
1035 : /// The time point when the timeline was archived
1036 : pub archived_at: chrono::DateTime<chrono::Utc>,
1037 : }
1038 :
1039 : /// This represents the output of the "timeline_detail" and "timeline_list" API calls.
1040 0 : #[derive(Debug, Serialize, Deserialize, Clone)]
1041 : pub struct TimelineInfo {
1042 : pub tenant_id: TenantShardId,
1043 : pub timeline_id: TimelineId,
1044 :
1045 : pub ancestor_timeline_id: Option<TimelineId>,
1046 : pub ancestor_lsn: Option<Lsn>,
1047 : pub last_record_lsn: Lsn,
1048 : pub prev_record_lsn: Option<Lsn>,
1049 : pub latest_gc_cutoff_lsn: Lsn,
1050 : pub disk_consistent_lsn: Lsn,
1051 :
1052 : /// The LSN that we have succesfully uploaded to remote storage
1053 : pub remote_consistent_lsn: Lsn,
1054 :
1055 : /// The LSN that we are advertizing to safekeepers
1056 : pub remote_consistent_lsn_visible: Lsn,
1057 :
1058 : /// The LSN from the start of the root timeline (never changes)
1059 : pub initdb_lsn: Lsn,
1060 :
1061 : pub current_logical_size: u64,
1062 : pub current_logical_size_is_accurate: bool,
1063 :
1064 : pub directory_entries_counts: Vec<u64>,
1065 :
1066 : /// Sum of the size of all layer files.
1067 : /// If a layer is present in both local FS and S3, it counts only once.
1068 : pub current_physical_size: Option<u64>, // is None when timeline is Unloaded
1069 : pub current_logical_size_non_incremental: Option<u64>,
1070 :
1071 : /// How many bytes of WAL are within this branch's pitr_interval. If the pitr_interval goes
1072 : /// beyond the branch's branch point, we only count up to the branch point.
1073 : pub pitr_history_size: u64,
1074 :
1075 : /// Whether this branch's branch point is within its ancestor's PITR interval (i.e. any
1076 : /// ancestor data used by this branch would have been retained anyway). If this is false, then
1077 : /// this branch may be imposing a cost on the ancestor by causing it to retain layers that it would
1078 : /// otherwise be able to GC.
1079 : pub within_ancestor_pitr: bool,
1080 :
1081 : pub timeline_dir_layer_file_size_sum: Option<u64>,
1082 :
1083 : pub wal_source_connstr: Option<String>,
1084 : pub last_received_msg_lsn: Option<Lsn>,
1085 : /// the timestamp (in microseconds) of the last received message
1086 : pub last_received_msg_ts: Option<u128>,
1087 : pub pg_version: u32,
1088 :
1089 : pub state: TimelineState,
1090 :
1091 : pub walreceiver_status: String,
1092 :
1093 : // ALWAYS add new fields at the end of the struct with `Option` to ensure forward/backward compatibility.
1094 : // Backward compatibility: you will get a JSON not containing the newly-added field.
1095 : // Forward compatibility: a previous version of the pageserver will receive a JSON. serde::Deserialize does
1096 : // not deny unknown fields by default so it's safe to set the field to some value, though it won't be
1097 : // read.
1098 : pub is_archived: Option<bool>,
1099 : }
1100 :
1101 0 : #[derive(Debug, Clone, Serialize, Deserialize)]
1102 : pub struct LayerMapInfo {
1103 : pub in_memory_layers: Vec<InMemoryLayerInfo>,
1104 : pub historic_layers: Vec<HistoricLayerInfo>,
1105 : }
1106 :
1107 : /// The residence status of a layer
1108 0 : #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
1109 : pub enum LayerResidenceStatus {
1110 : /// Residence status for a layer file that exists locally.
1111 : /// It may also exist on the remote, we don't care here.
1112 : Resident,
1113 : /// Residence status for a layer file that only exists on the remote.
1114 : Evicted,
1115 : }
1116 :
1117 : #[serde_as]
1118 0 : #[derive(Debug, Clone, Serialize, Deserialize)]
1119 : pub struct LayerAccessStats {
1120 : #[serde_as(as = "serde_with::TimestampMilliSeconds")]
1121 : pub access_time: SystemTime,
1122 :
1123 : #[serde_as(as = "serde_with::TimestampMilliSeconds")]
1124 : pub residence_time: SystemTime,
1125 :
1126 : pub visible: bool,
1127 : }
1128 :
1129 0 : #[derive(Debug, Clone, Serialize, Deserialize)]
1130 : #[serde(tag = "kind")]
1131 : pub enum InMemoryLayerInfo {
1132 : Open { lsn_start: Lsn },
1133 : Frozen { lsn_start: Lsn, lsn_end: Lsn },
1134 : }
1135 :
1136 0 : #[derive(Debug, Clone, Serialize, Deserialize)]
1137 : #[serde(tag = "kind")]
1138 : pub enum HistoricLayerInfo {
1139 : Delta {
1140 : layer_file_name: String,
1141 : layer_file_size: u64,
1142 :
1143 : lsn_start: Lsn,
1144 : lsn_end: Lsn,
1145 : remote: bool,
1146 : access_stats: LayerAccessStats,
1147 :
1148 : l0: bool,
1149 : },
1150 : Image {
1151 : layer_file_name: String,
1152 : layer_file_size: u64,
1153 :
1154 : lsn_start: Lsn,
1155 : remote: bool,
1156 : access_stats: LayerAccessStats,
1157 : },
1158 : }
1159 :
1160 : impl HistoricLayerInfo {
1161 0 : pub fn layer_file_name(&self) -> &str {
1162 0 : match self {
1163 : HistoricLayerInfo::Delta {
1164 0 : layer_file_name, ..
1165 0 : } => layer_file_name,
1166 : HistoricLayerInfo::Image {
1167 0 : layer_file_name, ..
1168 0 : } => layer_file_name,
1169 : }
1170 0 : }
1171 0 : pub fn is_remote(&self) -> bool {
1172 0 : match self {
1173 0 : HistoricLayerInfo::Delta { remote, .. } => *remote,
1174 0 : HistoricLayerInfo::Image { remote, .. } => *remote,
1175 : }
1176 0 : }
1177 0 : pub fn set_remote(&mut self, value: bool) {
1178 0 : let field = match self {
1179 0 : HistoricLayerInfo::Delta { remote, .. } => remote,
1180 0 : HistoricLayerInfo::Image { remote, .. } => remote,
1181 : };
1182 0 : *field = value;
1183 0 : }
1184 0 : pub fn layer_file_size(&self) -> u64 {
1185 0 : match self {
1186 : HistoricLayerInfo::Delta {
1187 0 : layer_file_size, ..
1188 0 : } => *layer_file_size,
1189 : HistoricLayerInfo::Image {
1190 0 : layer_file_size, ..
1191 0 : } => *layer_file_size,
1192 : }
1193 0 : }
1194 : }
1195 :
1196 0 : #[derive(Debug, Serialize, Deserialize)]
1197 : pub struct DownloadRemoteLayersTaskSpawnRequest {
1198 : pub max_concurrent_downloads: NonZeroUsize,
1199 : }
1200 :
1201 0 : #[derive(Debug, Serialize, Deserialize)]
1202 : pub struct IngestAuxFilesRequest {
1203 : pub aux_files: HashMap<String, String>,
1204 : }
1205 :
1206 0 : #[derive(Debug, Serialize, Deserialize)]
1207 : pub struct ListAuxFilesRequest {
1208 : pub lsn: Lsn,
1209 : }
1210 :
1211 0 : #[derive(Debug, Serialize, Deserialize, Clone)]
1212 : pub struct DownloadRemoteLayersTaskInfo {
1213 : pub task_id: String,
1214 : pub state: DownloadRemoteLayersTaskState,
1215 : pub total_layer_count: u64, // stable once `completed`
1216 : pub successful_download_count: u64, // stable once `completed`
1217 : pub failed_download_count: u64, // stable once `completed`
1218 : }
1219 :
1220 0 : #[derive(Debug, Serialize, Deserialize, Clone)]
1221 : pub enum DownloadRemoteLayersTaskState {
1222 : Running,
1223 : Completed,
1224 : ShutDown,
1225 : }
1226 :
1227 0 : #[derive(Debug, Serialize, Deserialize)]
1228 : pub struct TimelineGcRequest {
1229 : pub gc_horizon: Option<u64>,
1230 : }
1231 :
1232 0 : #[derive(Debug, Clone, Serialize, Deserialize)]
1233 : pub struct WalRedoManagerProcessStatus {
1234 : pub pid: u32,
1235 : }
1236 :
1237 0 : #[derive(Debug, Clone, Serialize, Deserialize)]
1238 : pub struct WalRedoManagerStatus {
1239 : pub last_redo_at: Option<chrono::DateTime<chrono::Utc>>,
1240 : pub process: Option<WalRedoManagerProcessStatus>,
1241 : }
1242 :
1243 : /// The progress of a secondary tenant.
1244 : ///
1245 : /// It is mostly useful when doing a long running download: e.g. initiating
1246 : /// a download job, timing out while waiting for it to run, and then inspecting this status to understand
1247 : /// what's happening.
1248 0 : #[derive(Default, Debug, Serialize, Deserialize, Clone)]
1249 : pub struct SecondaryProgress {
1250 : /// The remote storage LastModified time of the heatmap object we last downloaded.
1251 : pub heatmap_mtime: Option<serde_system_time::SystemTime>,
1252 :
1253 : /// The number of layers currently on-disk
1254 : pub layers_downloaded: usize,
1255 : /// The number of layers in the most recently seen heatmap
1256 : pub layers_total: usize,
1257 :
1258 : /// The number of layer bytes currently on-disk
1259 : pub bytes_downloaded: u64,
1260 : /// The number of layer bytes in the most recently seen heatmap
1261 : pub bytes_total: u64,
1262 : }
1263 :
1264 0 : #[derive(Serialize, Deserialize, Debug)]
1265 : pub struct TenantScanRemoteStorageShard {
1266 : pub tenant_shard_id: TenantShardId,
1267 : pub generation: Option<u32>,
1268 : }
1269 :
1270 0 : #[derive(Serialize, Deserialize, Debug, Default)]
1271 : pub struct TenantScanRemoteStorageResponse {
1272 : pub shards: Vec<TenantScanRemoteStorageShard>,
1273 : }
1274 :
1275 0 : #[derive(Serialize, Deserialize, Debug, Clone)]
1276 : #[serde(rename_all = "snake_case")]
1277 : pub enum TenantSorting {
1278 : ResidentSize,
1279 : MaxLogicalSize,
1280 : }
1281 :
1282 : impl Default for TenantSorting {
1283 0 : fn default() -> Self {
1284 0 : Self::ResidentSize
1285 0 : }
1286 : }
1287 :
1288 0 : #[derive(Serialize, Deserialize, Debug, Clone)]
1289 : pub struct TopTenantShardsRequest {
1290 : // How would you like to sort the tenants?
1291 : pub order_by: TenantSorting,
1292 :
1293 : // How many results?
1294 : pub limit: usize,
1295 :
1296 : // Omit tenants with more than this many shards (e.g. if this is the max number of shards
1297 : // that the caller would ever split to)
1298 : pub where_shards_lt: Option<ShardCount>,
1299 :
1300 : // Omit tenants where the ordering metric is less than this (this is an optimization to
1301 : // let us quickly exclude numerous tiny shards)
1302 : pub where_gt: Option<u64>,
1303 : }
1304 :
1305 0 : #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
1306 : pub struct TopTenantShardItem {
1307 : pub id: TenantShardId,
1308 :
1309 : /// Total size of layers on local disk for all timelines in this tenant
1310 : pub resident_size: u64,
1311 :
1312 : /// Total size of layers in remote storage for all timelines in this tenant
1313 : pub physical_size: u64,
1314 :
1315 : /// The largest logical size of a timeline within this tenant
1316 : pub max_logical_size: u64,
1317 : }
1318 :
1319 0 : #[derive(Serialize, Deserialize, Debug, Default)]
1320 : pub struct TopTenantShardsResponse {
1321 : pub shards: Vec<TopTenantShardItem>,
1322 : }
1323 :
1324 : pub mod virtual_file {
1325 : #[derive(
1326 : Copy,
1327 : Clone,
1328 : PartialEq,
1329 : Eq,
1330 : Hash,
1331 0 : strum_macros::EnumString,
1332 : strum_macros::Display,
1333 0 : serde_with::DeserializeFromStr,
1334 : serde_with::SerializeDisplay,
1335 : Debug,
1336 : )]
1337 : #[strum(serialize_all = "kebab-case")]
1338 : pub enum IoEngineKind {
1339 : StdFs,
1340 : #[cfg(target_os = "linux")]
1341 : TokioEpollUring,
1342 : }
1343 :
1344 : /// Direct IO modes for a pageserver.
1345 : #[derive(
1346 : Copy,
1347 : Clone,
1348 : PartialEq,
1349 : Eq,
1350 : Hash,
1351 0 : strum_macros::EnumString,
1352 : strum_macros::Display,
1353 0 : serde_with::DeserializeFromStr,
1354 : serde_with::SerializeDisplay,
1355 : Debug,
1356 : )]
1357 : #[strum(serialize_all = "kebab-case")]
1358 : #[repr(u8)]
1359 : pub enum IoMode {
1360 : /// Uses buffered IO.
1361 : Buffered,
1362 : /// Uses direct IO, error out if the operation fails.
1363 : #[cfg(target_os = "linux")]
1364 : Direct,
1365 : }
1366 :
1367 : impl IoMode {
1368 214 : pub const fn preferred() -> Self {
1369 214 : Self::Buffered
1370 214 : }
1371 : }
1372 :
1373 : impl TryFrom<u8> for IoMode {
1374 : type Error = u8;
1375 :
1376 2508 : fn try_from(value: u8) -> Result<Self, Self::Error> {
1377 2508 : Ok(match value {
1378 2508 : v if v == (IoMode::Buffered as u8) => IoMode::Buffered,
1379 : #[cfg(target_os = "linux")]
1380 0 : v if v == (IoMode::Direct as u8) => IoMode::Direct,
1381 0 : x => return Err(x),
1382 : })
1383 2508 : }
1384 : }
1385 : }
1386 :
1387 0 : #[derive(Debug, Clone, Serialize, Deserialize)]
1388 : pub struct ScanDisposableKeysResponse {
1389 : pub disposable_count: usize,
1390 : pub not_disposable_count: usize,
1391 : }
1392 :
1393 : // Wrapped in libpq CopyData
1394 : #[derive(PartialEq, Eq, Debug)]
1395 : pub enum PagestreamFeMessage {
1396 : Exists(PagestreamExistsRequest),
1397 : Nblocks(PagestreamNblocksRequest),
1398 : GetPage(PagestreamGetPageRequest),
1399 : DbSize(PagestreamDbSizeRequest),
1400 : GetSlruSegment(PagestreamGetSlruSegmentRequest),
1401 : }
1402 :
1403 : // Wrapped in libpq CopyData
1404 : #[derive(strum_macros::EnumProperty)]
1405 : pub enum PagestreamBeMessage {
1406 : Exists(PagestreamExistsResponse),
1407 : Nblocks(PagestreamNblocksResponse),
1408 : GetPage(PagestreamGetPageResponse),
1409 : Error(PagestreamErrorResponse),
1410 : DbSize(PagestreamDbSizeResponse),
1411 : GetSlruSegment(PagestreamGetSlruSegmentResponse),
1412 : }
1413 :
1414 : // Keep in sync with `pagestore_client.h`
1415 : #[repr(u8)]
1416 : enum PagestreamBeMessageTag {
1417 : Exists = 100,
1418 : Nblocks = 101,
1419 : GetPage = 102,
1420 : Error = 103,
1421 : DbSize = 104,
1422 : GetSlruSegment = 105,
1423 : }
1424 : impl TryFrom<u8> for PagestreamBeMessageTag {
1425 : type Error = u8;
1426 0 : fn try_from(value: u8) -> Result<Self, u8> {
1427 0 : match value {
1428 0 : 100 => Ok(PagestreamBeMessageTag::Exists),
1429 0 : 101 => Ok(PagestreamBeMessageTag::Nblocks),
1430 0 : 102 => Ok(PagestreamBeMessageTag::GetPage),
1431 0 : 103 => Ok(PagestreamBeMessageTag::Error),
1432 0 : 104 => Ok(PagestreamBeMessageTag::DbSize),
1433 0 : 105 => Ok(PagestreamBeMessageTag::GetSlruSegment),
1434 0 : _ => Err(value),
1435 : }
1436 0 : }
1437 : }
1438 :
1439 : // A GetPage request contains two LSN values:
1440 : //
1441 : // request_lsn: Get the page version at this point in time. Lsn::Max is a special value that means
1442 : // "get the latest version present". It's used by the primary server, which knows that no one else
1443 : // is writing WAL. 'not_modified_since' must be set to a proper value even if request_lsn is
1444 : // Lsn::Max. Standby servers use the current replay LSN as the request LSN.
1445 : //
1446 : // not_modified_since: Hint to the pageserver that the client knows that the page has not been
1447 : // modified between 'not_modified_since' and the request LSN. It's always correct to set
1448 : // 'not_modified_since equal' to 'request_lsn' (unless Lsn::Max is used as the 'request_lsn'), but
1449 : // passing an earlier LSN can speed up the request, by allowing the pageserver to process the
1450 : // request without waiting for 'request_lsn' to arrive.
1451 : //
1452 : // The now-defunct V1 interface contained only one LSN, and a boolean 'latest' flag. The V1 interface was
1453 : // sufficient for the primary; the 'lsn' was equivalent to the 'not_modified_since' value, and
1454 : // 'latest' was set to true. The V2 interface was added because there was no correct way for a
1455 : // standby to request a page at a particular non-latest LSN, and also include the
1456 : // 'not_modified_since' hint. That led to an awkward choice of either using an old LSN in the
1457 : // request, if the standby knows that the page hasn't been modified since, and risk getting an error
1458 : // if that LSN has fallen behind the GC horizon, or requesting the current replay LSN, which could
1459 : // require the pageserver unnecessarily to wait for the WAL to arrive up to that point. The new V2
1460 : // interface allows sending both LSNs, and let the pageserver do the right thing. There was no
1461 : // difference in the responses between V1 and V2.
1462 : //
1463 : // V3 version of protocol adds request ID to all requests. This request ID is also included in response
1464 : // as well as other fields from requests, which allows to verify that we receive response for our request.
1465 : // We copy fields from request to response to make checking more reliable: request ID is formed from process ID
1466 : // and local counter, so in principle there can be duplicated requests IDs if process PID is reused.
1467 : //
1468 : #[derive(Debug, PartialEq, Eq, Clone, Copy)]
1469 : pub enum PagestreamProtocolVersion {
1470 : V2,
1471 : V3,
1472 : }
1473 :
1474 : pub type RequestId = u64;
1475 :
1476 : #[derive(Debug, PartialEq, Eq, Clone, Copy)]
1477 : pub struct PagestreamRequest {
1478 : pub reqid: RequestId,
1479 : pub request_lsn: Lsn,
1480 : pub not_modified_since: Lsn,
1481 : }
1482 :
1483 : #[derive(Debug, PartialEq, Eq, Clone, Copy)]
1484 : pub struct PagestreamExistsRequest {
1485 : pub hdr: PagestreamRequest,
1486 : pub rel: RelTag,
1487 : }
1488 :
1489 : #[derive(Debug, PartialEq, Eq, Clone, Copy)]
1490 : pub struct PagestreamNblocksRequest {
1491 : pub hdr: PagestreamRequest,
1492 : pub rel: RelTag,
1493 : }
1494 :
1495 : #[derive(Debug, PartialEq, Eq, Clone, Copy)]
1496 : pub struct PagestreamGetPageRequest {
1497 : pub hdr: PagestreamRequest,
1498 : pub rel: RelTag,
1499 : pub blkno: u32,
1500 : }
1501 :
1502 : #[derive(Debug, PartialEq, Eq, Clone, Copy)]
1503 : pub struct PagestreamDbSizeRequest {
1504 : pub hdr: PagestreamRequest,
1505 : pub dbnode: u32,
1506 : }
1507 :
1508 : #[derive(Debug, PartialEq, Eq, Clone, Copy)]
1509 : pub struct PagestreamGetSlruSegmentRequest {
1510 : pub hdr: PagestreamRequest,
1511 : pub kind: u8,
1512 : pub segno: u32,
1513 : }
1514 :
1515 : #[derive(Debug)]
1516 : pub struct PagestreamExistsResponse {
1517 : pub req: PagestreamExistsRequest,
1518 : pub exists: bool,
1519 : }
1520 :
1521 : #[derive(Debug)]
1522 : pub struct PagestreamNblocksResponse {
1523 : pub req: PagestreamNblocksRequest,
1524 : pub n_blocks: u32,
1525 : }
1526 :
1527 : #[derive(Debug)]
1528 : pub struct PagestreamGetPageResponse {
1529 : pub req: PagestreamGetPageRequest,
1530 : pub page: Bytes,
1531 : }
1532 :
1533 : #[derive(Debug)]
1534 : pub struct PagestreamGetSlruSegmentResponse {
1535 : pub req: PagestreamGetSlruSegmentRequest,
1536 : pub segment: Bytes,
1537 : }
1538 :
1539 : #[derive(Debug)]
1540 : pub struct PagestreamErrorResponse {
1541 : pub req: PagestreamRequest,
1542 : pub message: String,
1543 : }
1544 :
1545 : #[derive(Debug)]
1546 : pub struct PagestreamDbSizeResponse {
1547 : pub req: PagestreamDbSizeRequest,
1548 : pub db_size: i64,
1549 : }
1550 :
1551 : // This is a cut-down version of TenantHistorySize from the pageserver crate, omitting fields
1552 : // that require pageserver-internal types. It is sufficient to get the total size.
1553 0 : #[derive(Serialize, Deserialize, Debug)]
1554 : pub struct TenantHistorySize {
1555 : pub id: TenantId,
1556 : /// Size is a mixture of WAL and logical size, so the unit is bytes.
1557 : ///
1558 : /// Will be none if `?inputs_only=true` was given.
1559 : pub size: Option<u64>,
1560 : }
1561 :
1562 : impl PagestreamFeMessage {
1563 : /// Serialize a compute -> pageserver message. This is currently only used in testing
1564 : /// tools. Always uses protocol version 3.
1565 4 : pub fn serialize(&self) -> Bytes {
1566 4 : let mut bytes = BytesMut::new();
1567 4 :
1568 4 : match self {
1569 1 : Self::Exists(req) => {
1570 1 : bytes.put_u8(0);
1571 1 : bytes.put_u64(req.hdr.reqid);
1572 1 : bytes.put_u64(req.hdr.request_lsn.0);
1573 1 : bytes.put_u64(req.hdr.not_modified_since.0);
1574 1 : bytes.put_u32(req.rel.spcnode);
1575 1 : bytes.put_u32(req.rel.dbnode);
1576 1 : bytes.put_u32(req.rel.relnode);
1577 1 : bytes.put_u8(req.rel.forknum);
1578 1 : }
1579 :
1580 1 : Self::Nblocks(req) => {
1581 1 : bytes.put_u8(1);
1582 1 : bytes.put_u64(req.hdr.reqid);
1583 1 : bytes.put_u64(req.hdr.request_lsn.0);
1584 1 : bytes.put_u64(req.hdr.not_modified_since.0);
1585 1 : bytes.put_u32(req.rel.spcnode);
1586 1 : bytes.put_u32(req.rel.dbnode);
1587 1 : bytes.put_u32(req.rel.relnode);
1588 1 : bytes.put_u8(req.rel.forknum);
1589 1 : }
1590 :
1591 1 : Self::GetPage(req) => {
1592 1 : bytes.put_u8(2);
1593 1 : bytes.put_u64(req.hdr.reqid);
1594 1 : bytes.put_u64(req.hdr.request_lsn.0);
1595 1 : bytes.put_u64(req.hdr.not_modified_since.0);
1596 1 : bytes.put_u32(req.rel.spcnode);
1597 1 : bytes.put_u32(req.rel.dbnode);
1598 1 : bytes.put_u32(req.rel.relnode);
1599 1 : bytes.put_u8(req.rel.forknum);
1600 1 : bytes.put_u32(req.blkno);
1601 1 : }
1602 :
1603 1 : Self::DbSize(req) => {
1604 1 : bytes.put_u8(3);
1605 1 : bytes.put_u64(req.hdr.reqid);
1606 1 : bytes.put_u64(req.hdr.request_lsn.0);
1607 1 : bytes.put_u64(req.hdr.not_modified_since.0);
1608 1 : bytes.put_u32(req.dbnode);
1609 1 : }
1610 :
1611 0 : Self::GetSlruSegment(req) => {
1612 0 : bytes.put_u8(4);
1613 0 : bytes.put_u64(req.hdr.reqid);
1614 0 : bytes.put_u64(req.hdr.request_lsn.0);
1615 0 : bytes.put_u64(req.hdr.not_modified_since.0);
1616 0 : bytes.put_u8(req.kind);
1617 0 : bytes.put_u32(req.segno);
1618 0 : }
1619 : }
1620 :
1621 4 : bytes.into()
1622 4 : }
1623 :
1624 4 : pub fn parse<R: std::io::Read>(
1625 4 : body: &mut R,
1626 4 : protocol_version: PagestreamProtocolVersion,
1627 4 : ) -> anyhow::Result<PagestreamFeMessage> {
1628 : // these correspond to the NeonMessageTag enum in pagestore_client.h
1629 : //
1630 : // TODO: consider using protobuf or serde bincode for less error prone
1631 : // serialization.
1632 4 : let msg_tag = body.read_u8()?;
1633 4 : let (reqid, request_lsn, not_modified_since) = match protocol_version {
1634 : PagestreamProtocolVersion::V2 => (
1635 : 0,
1636 0 : Lsn::from(body.read_u64::<BigEndian>()?),
1637 0 : Lsn::from(body.read_u64::<BigEndian>()?),
1638 : ),
1639 : PagestreamProtocolVersion::V3 => (
1640 4 : body.read_u64::<BigEndian>()?,
1641 4 : Lsn::from(body.read_u64::<BigEndian>()?),
1642 4 : Lsn::from(body.read_u64::<BigEndian>()?),
1643 : ),
1644 : };
1645 :
1646 4 : match msg_tag {
1647 : 0 => Ok(PagestreamFeMessage::Exists(PagestreamExistsRequest {
1648 1 : hdr: PagestreamRequest {
1649 1 : reqid,
1650 1 : request_lsn,
1651 1 : not_modified_since,
1652 1 : },
1653 1 : rel: RelTag {
1654 1 : spcnode: body.read_u32::<BigEndian>()?,
1655 1 : dbnode: body.read_u32::<BigEndian>()?,
1656 1 : relnode: body.read_u32::<BigEndian>()?,
1657 1 : forknum: body.read_u8()?,
1658 : },
1659 : })),
1660 : 1 => Ok(PagestreamFeMessage::Nblocks(PagestreamNblocksRequest {
1661 1 : hdr: PagestreamRequest {
1662 1 : reqid,
1663 1 : request_lsn,
1664 1 : not_modified_since,
1665 1 : },
1666 1 : rel: RelTag {
1667 1 : spcnode: body.read_u32::<BigEndian>()?,
1668 1 : dbnode: body.read_u32::<BigEndian>()?,
1669 1 : relnode: body.read_u32::<BigEndian>()?,
1670 1 : forknum: body.read_u8()?,
1671 : },
1672 : })),
1673 : 2 => Ok(PagestreamFeMessage::GetPage(PagestreamGetPageRequest {
1674 1 : hdr: PagestreamRequest {
1675 1 : reqid,
1676 1 : request_lsn,
1677 1 : not_modified_since,
1678 1 : },
1679 1 : rel: RelTag {
1680 1 : spcnode: body.read_u32::<BigEndian>()?,
1681 1 : dbnode: body.read_u32::<BigEndian>()?,
1682 1 : relnode: body.read_u32::<BigEndian>()?,
1683 1 : forknum: body.read_u8()?,
1684 : },
1685 1 : blkno: body.read_u32::<BigEndian>()?,
1686 : })),
1687 : 3 => Ok(PagestreamFeMessage::DbSize(PagestreamDbSizeRequest {
1688 1 : hdr: PagestreamRequest {
1689 1 : reqid,
1690 1 : request_lsn,
1691 1 : not_modified_since,
1692 1 : },
1693 1 : dbnode: body.read_u32::<BigEndian>()?,
1694 : })),
1695 : 4 => Ok(PagestreamFeMessage::GetSlruSegment(
1696 : PagestreamGetSlruSegmentRequest {
1697 0 : hdr: PagestreamRequest {
1698 0 : reqid,
1699 0 : request_lsn,
1700 0 : not_modified_since,
1701 0 : },
1702 0 : kind: body.read_u8()?,
1703 0 : segno: body.read_u32::<BigEndian>()?,
1704 : },
1705 : )),
1706 0 : _ => bail!("unknown smgr message tag: {:?}", msg_tag),
1707 : }
1708 4 : }
1709 : }
1710 :
1711 : impl PagestreamBeMessage {
1712 0 : pub fn serialize(&self, protocol_version: PagestreamProtocolVersion) -> Bytes {
1713 0 : let mut bytes = BytesMut::new();
1714 :
1715 : use PagestreamBeMessageTag as Tag;
1716 0 : match protocol_version {
1717 : PagestreamProtocolVersion::V2 => {
1718 0 : match self {
1719 0 : Self::Exists(resp) => {
1720 0 : bytes.put_u8(Tag::Exists as u8);
1721 0 : bytes.put_u8(resp.exists as u8);
1722 0 : }
1723 :
1724 0 : Self::Nblocks(resp) => {
1725 0 : bytes.put_u8(Tag::Nblocks as u8);
1726 0 : bytes.put_u32(resp.n_blocks);
1727 0 : }
1728 :
1729 0 : Self::GetPage(resp) => {
1730 0 : bytes.put_u8(Tag::GetPage as u8);
1731 0 : bytes.put(&resp.page[..])
1732 : }
1733 :
1734 0 : Self::Error(resp) => {
1735 0 : bytes.put_u8(Tag::Error as u8);
1736 0 : bytes.put(resp.message.as_bytes());
1737 0 : bytes.put_u8(0); // null terminator
1738 0 : }
1739 0 : Self::DbSize(resp) => {
1740 0 : bytes.put_u8(Tag::DbSize as u8);
1741 0 : bytes.put_i64(resp.db_size);
1742 0 : }
1743 :
1744 0 : Self::GetSlruSegment(resp) => {
1745 0 : bytes.put_u8(Tag::GetSlruSegment as u8);
1746 0 : bytes.put_u32((resp.segment.len() / BLCKSZ as usize) as u32);
1747 0 : bytes.put(&resp.segment[..]);
1748 0 : }
1749 : }
1750 : }
1751 : PagestreamProtocolVersion::V3 => {
1752 0 : match self {
1753 0 : Self::Exists(resp) => {
1754 0 : bytes.put_u8(Tag::Exists as u8);
1755 0 : bytes.put_u64(resp.req.hdr.reqid);
1756 0 : bytes.put_u64(resp.req.hdr.request_lsn.0);
1757 0 : bytes.put_u64(resp.req.hdr.not_modified_since.0);
1758 0 : bytes.put_u32(resp.req.rel.spcnode);
1759 0 : bytes.put_u32(resp.req.rel.dbnode);
1760 0 : bytes.put_u32(resp.req.rel.relnode);
1761 0 : bytes.put_u8(resp.req.rel.forknum);
1762 0 : bytes.put_u8(resp.exists as u8);
1763 0 : }
1764 :
1765 0 : Self::Nblocks(resp) => {
1766 0 : bytes.put_u8(Tag::Nblocks as u8);
1767 0 : bytes.put_u64(resp.req.hdr.reqid);
1768 0 : bytes.put_u64(resp.req.hdr.request_lsn.0);
1769 0 : bytes.put_u64(resp.req.hdr.not_modified_since.0);
1770 0 : bytes.put_u32(resp.req.rel.spcnode);
1771 0 : bytes.put_u32(resp.req.rel.dbnode);
1772 0 : bytes.put_u32(resp.req.rel.relnode);
1773 0 : bytes.put_u8(resp.req.rel.forknum);
1774 0 : bytes.put_u32(resp.n_blocks);
1775 0 : }
1776 :
1777 0 : Self::GetPage(resp) => {
1778 0 : bytes.put_u8(Tag::GetPage as u8);
1779 0 : bytes.put_u64(resp.req.hdr.reqid);
1780 0 : bytes.put_u64(resp.req.hdr.request_lsn.0);
1781 0 : bytes.put_u64(resp.req.hdr.not_modified_since.0);
1782 0 : bytes.put_u32(resp.req.rel.spcnode);
1783 0 : bytes.put_u32(resp.req.rel.dbnode);
1784 0 : bytes.put_u32(resp.req.rel.relnode);
1785 0 : bytes.put_u8(resp.req.rel.forknum);
1786 0 : bytes.put_u32(resp.req.blkno);
1787 0 : bytes.put(&resp.page[..])
1788 : }
1789 :
1790 0 : Self::Error(resp) => {
1791 0 : bytes.put_u8(Tag::Error as u8);
1792 0 : bytes.put_u64(resp.req.reqid);
1793 0 : bytes.put_u64(resp.req.request_lsn.0);
1794 0 : bytes.put_u64(resp.req.not_modified_since.0);
1795 0 : bytes.put(resp.message.as_bytes());
1796 0 : bytes.put_u8(0); // null terminator
1797 0 : }
1798 0 : Self::DbSize(resp) => {
1799 0 : bytes.put_u8(Tag::DbSize as u8);
1800 0 : bytes.put_u64(resp.req.hdr.reqid);
1801 0 : bytes.put_u64(resp.req.hdr.request_lsn.0);
1802 0 : bytes.put_u64(resp.req.hdr.not_modified_since.0);
1803 0 : bytes.put_u32(resp.req.dbnode);
1804 0 : bytes.put_i64(resp.db_size);
1805 0 : }
1806 :
1807 0 : Self::GetSlruSegment(resp) => {
1808 0 : bytes.put_u8(Tag::GetSlruSegment as u8);
1809 0 : bytes.put_u64(resp.req.hdr.reqid);
1810 0 : bytes.put_u64(resp.req.hdr.request_lsn.0);
1811 0 : bytes.put_u64(resp.req.hdr.not_modified_since.0);
1812 0 : bytes.put_u8(resp.req.kind);
1813 0 : bytes.put_u32(resp.req.segno);
1814 0 : bytes.put_u32((resp.segment.len() / BLCKSZ as usize) as u32);
1815 0 : bytes.put(&resp.segment[..]);
1816 0 : }
1817 : }
1818 : }
1819 : }
1820 0 : bytes.into()
1821 0 : }
1822 :
1823 0 : pub fn deserialize(buf: Bytes) -> anyhow::Result<Self> {
1824 0 : let mut buf = buf.reader();
1825 0 : let msg_tag = buf.read_u8()?;
1826 :
1827 : use PagestreamBeMessageTag as Tag;
1828 0 : let ok =
1829 0 : match Tag::try_from(msg_tag).map_err(|tag: u8| anyhow::anyhow!("invalid tag {tag}"))? {
1830 : Tag::Exists => {
1831 0 : let reqid = buf.read_u64::<BigEndian>()?;
1832 0 : let request_lsn = Lsn(buf.read_u64::<BigEndian>()?);
1833 0 : let not_modified_since = Lsn(buf.read_u64::<BigEndian>()?);
1834 0 : let rel = RelTag {
1835 0 : spcnode: buf.read_u32::<BigEndian>()?,
1836 0 : dbnode: buf.read_u32::<BigEndian>()?,
1837 0 : relnode: buf.read_u32::<BigEndian>()?,
1838 0 : forknum: buf.read_u8()?,
1839 : };
1840 0 : let exists = buf.read_u8()? != 0;
1841 0 : Self::Exists(PagestreamExistsResponse {
1842 0 : req: PagestreamExistsRequest {
1843 0 : hdr: PagestreamRequest {
1844 0 : reqid,
1845 0 : request_lsn,
1846 0 : not_modified_since,
1847 0 : },
1848 0 : rel,
1849 0 : },
1850 0 : exists,
1851 0 : })
1852 : }
1853 : Tag::Nblocks => {
1854 0 : let reqid = buf.read_u64::<BigEndian>()?;
1855 0 : let request_lsn = Lsn(buf.read_u64::<BigEndian>()?);
1856 0 : let not_modified_since = Lsn(buf.read_u64::<BigEndian>()?);
1857 0 : let rel = RelTag {
1858 0 : spcnode: buf.read_u32::<BigEndian>()?,
1859 0 : dbnode: buf.read_u32::<BigEndian>()?,
1860 0 : relnode: buf.read_u32::<BigEndian>()?,
1861 0 : forknum: buf.read_u8()?,
1862 : };
1863 0 : let n_blocks = buf.read_u32::<BigEndian>()?;
1864 0 : Self::Nblocks(PagestreamNblocksResponse {
1865 0 : req: PagestreamNblocksRequest {
1866 0 : hdr: PagestreamRequest {
1867 0 : reqid,
1868 0 : request_lsn,
1869 0 : not_modified_since,
1870 0 : },
1871 0 : rel,
1872 0 : },
1873 0 : n_blocks,
1874 0 : })
1875 : }
1876 : Tag::GetPage => {
1877 0 : let reqid = buf.read_u64::<BigEndian>()?;
1878 0 : let request_lsn = Lsn(buf.read_u64::<BigEndian>()?);
1879 0 : let not_modified_since = Lsn(buf.read_u64::<BigEndian>()?);
1880 0 : let rel = RelTag {
1881 0 : spcnode: buf.read_u32::<BigEndian>()?,
1882 0 : dbnode: buf.read_u32::<BigEndian>()?,
1883 0 : relnode: buf.read_u32::<BigEndian>()?,
1884 0 : forknum: buf.read_u8()?,
1885 : };
1886 0 : let blkno = buf.read_u32::<BigEndian>()?;
1887 0 : let mut page = vec![0; 8192]; // TODO: use MaybeUninit
1888 0 : buf.read_exact(&mut page)?;
1889 0 : Self::GetPage(PagestreamGetPageResponse {
1890 0 : req: PagestreamGetPageRequest {
1891 0 : hdr: PagestreamRequest {
1892 0 : reqid,
1893 0 : request_lsn,
1894 0 : not_modified_since,
1895 0 : },
1896 0 : rel,
1897 0 : blkno,
1898 0 : },
1899 0 : page: page.into(),
1900 0 : })
1901 : }
1902 : Tag::Error => {
1903 0 : let reqid = buf.read_u64::<BigEndian>()?;
1904 0 : let request_lsn = Lsn(buf.read_u64::<BigEndian>()?);
1905 0 : let not_modified_since = Lsn(buf.read_u64::<BigEndian>()?);
1906 0 : let mut msg = Vec::new();
1907 0 : buf.read_until(0, &mut msg)?;
1908 0 : let cstring = std::ffi::CString::from_vec_with_nul(msg)?;
1909 0 : let rust_str = cstring.to_str()?;
1910 0 : Self::Error(PagestreamErrorResponse {
1911 0 : req: PagestreamRequest {
1912 0 : reqid,
1913 0 : request_lsn,
1914 0 : not_modified_since,
1915 0 : },
1916 0 : message: rust_str.to_owned(),
1917 0 : })
1918 : }
1919 : Tag::DbSize => {
1920 0 : let reqid = buf.read_u64::<BigEndian>()?;
1921 0 : let request_lsn = Lsn(buf.read_u64::<BigEndian>()?);
1922 0 : let not_modified_since = Lsn(buf.read_u64::<BigEndian>()?);
1923 0 : let dbnode = buf.read_u32::<BigEndian>()?;
1924 0 : let db_size = buf.read_i64::<BigEndian>()?;
1925 0 : Self::DbSize(PagestreamDbSizeResponse {
1926 0 : req: PagestreamDbSizeRequest {
1927 0 : hdr: PagestreamRequest {
1928 0 : reqid,
1929 0 : request_lsn,
1930 0 : not_modified_since,
1931 0 : },
1932 0 : dbnode,
1933 0 : },
1934 0 : db_size,
1935 0 : })
1936 : }
1937 : Tag::GetSlruSegment => {
1938 0 : let reqid = buf.read_u64::<BigEndian>()?;
1939 0 : let request_lsn = Lsn(buf.read_u64::<BigEndian>()?);
1940 0 : let not_modified_since = Lsn(buf.read_u64::<BigEndian>()?);
1941 0 : let kind = buf.read_u8()?;
1942 0 : let segno = buf.read_u32::<BigEndian>()?;
1943 0 : let n_blocks = buf.read_u32::<BigEndian>()?;
1944 0 : let mut segment = vec![0; n_blocks as usize * BLCKSZ as usize];
1945 0 : buf.read_exact(&mut segment)?;
1946 0 : Self::GetSlruSegment(PagestreamGetSlruSegmentResponse {
1947 0 : req: PagestreamGetSlruSegmentRequest {
1948 0 : hdr: PagestreamRequest {
1949 0 : reqid,
1950 0 : request_lsn,
1951 0 : not_modified_since,
1952 0 : },
1953 0 : kind,
1954 0 : segno,
1955 0 : },
1956 0 : segment: segment.into(),
1957 0 : })
1958 : }
1959 : };
1960 0 : let remaining = buf.into_inner();
1961 0 : if !remaining.is_empty() {
1962 0 : anyhow::bail!(
1963 0 : "remaining bytes in msg with tag={msg_tag}: {}",
1964 0 : remaining.len()
1965 0 : );
1966 0 : }
1967 0 : Ok(ok)
1968 0 : }
1969 :
1970 0 : pub fn kind(&self) -> &'static str {
1971 0 : match self {
1972 0 : Self::Exists(_) => "Exists",
1973 0 : Self::Nblocks(_) => "Nblocks",
1974 0 : Self::GetPage(_) => "GetPage",
1975 0 : Self::Error(_) => "Error",
1976 0 : Self::DbSize(_) => "DbSize",
1977 0 : Self::GetSlruSegment(_) => "GetSlruSegment",
1978 : }
1979 0 : }
1980 : }
1981 :
1982 : #[cfg(test)]
1983 : mod tests {
1984 : use serde_json::json;
1985 : use std::str::FromStr;
1986 :
1987 : use super::*;
1988 :
1989 : #[test]
1990 1 : fn test_pagestream() {
1991 1 : // Test serialization/deserialization of PagestreamFeMessage
1992 1 : let messages = vec![
1993 1 : PagestreamFeMessage::Exists(PagestreamExistsRequest {
1994 1 : hdr: PagestreamRequest {
1995 1 : reqid: 0,
1996 1 : request_lsn: Lsn(4),
1997 1 : not_modified_since: Lsn(3),
1998 1 : },
1999 1 : rel: RelTag {
2000 1 : forknum: 1,
2001 1 : spcnode: 2,
2002 1 : dbnode: 3,
2003 1 : relnode: 4,
2004 1 : },
2005 1 : }),
2006 1 : PagestreamFeMessage::Nblocks(PagestreamNblocksRequest {
2007 1 : hdr: PagestreamRequest {
2008 1 : reqid: 0,
2009 1 : request_lsn: Lsn(4),
2010 1 : not_modified_since: Lsn(4),
2011 1 : },
2012 1 : rel: RelTag {
2013 1 : forknum: 1,
2014 1 : spcnode: 2,
2015 1 : dbnode: 3,
2016 1 : relnode: 4,
2017 1 : },
2018 1 : }),
2019 1 : PagestreamFeMessage::GetPage(PagestreamGetPageRequest {
2020 1 : hdr: PagestreamRequest {
2021 1 : reqid: 0,
2022 1 : request_lsn: Lsn(4),
2023 1 : not_modified_since: Lsn(3),
2024 1 : },
2025 1 : rel: RelTag {
2026 1 : forknum: 1,
2027 1 : spcnode: 2,
2028 1 : dbnode: 3,
2029 1 : relnode: 4,
2030 1 : },
2031 1 : blkno: 7,
2032 1 : }),
2033 1 : PagestreamFeMessage::DbSize(PagestreamDbSizeRequest {
2034 1 : hdr: PagestreamRequest {
2035 1 : reqid: 0,
2036 1 : request_lsn: Lsn(4),
2037 1 : not_modified_since: Lsn(3),
2038 1 : },
2039 1 : dbnode: 7,
2040 1 : }),
2041 1 : ];
2042 5 : for msg in messages {
2043 4 : let bytes = msg.serialize();
2044 4 : let reconstructed =
2045 4 : PagestreamFeMessage::parse(&mut bytes.reader(), PagestreamProtocolVersion::V3)
2046 4 : .unwrap();
2047 4 : assert!(msg == reconstructed);
2048 : }
2049 1 : }
2050 :
2051 : #[test]
2052 1 : fn test_tenantinfo_serde() {
2053 1 : // Test serialization/deserialization of TenantInfo
2054 1 : let original_active = TenantInfo {
2055 1 : id: TenantShardId::unsharded(TenantId::generate()),
2056 1 : state: TenantState::Active,
2057 1 : current_physical_size: Some(42),
2058 1 : attachment_status: TenantAttachmentStatus::Attached,
2059 1 : generation: 1,
2060 1 : gc_blocking: None,
2061 1 : };
2062 1 : let expected_active = json!({
2063 1 : "id": original_active.id.to_string(),
2064 1 : "state": {
2065 1 : "slug": "Active",
2066 1 : },
2067 1 : "current_physical_size": 42,
2068 1 : "attachment_status": {
2069 1 : "slug":"attached",
2070 1 : },
2071 1 : "generation" : 1
2072 1 : });
2073 1 :
2074 1 : let original_broken = TenantInfo {
2075 1 : id: TenantShardId::unsharded(TenantId::generate()),
2076 1 : state: TenantState::Broken {
2077 1 : reason: "reason".into(),
2078 1 : backtrace: "backtrace info".into(),
2079 1 : },
2080 1 : current_physical_size: Some(42),
2081 1 : attachment_status: TenantAttachmentStatus::Attached,
2082 1 : generation: 1,
2083 1 : gc_blocking: None,
2084 1 : };
2085 1 : let expected_broken = json!({
2086 1 : "id": original_broken.id.to_string(),
2087 1 : "state": {
2088 1 : "slug": "Broken",
2089 1 : "data": {
2090 1 : "backtrace": "backtrace info",
2091 1 : "reason": "reason",
2092 1 : }
2093 1 : },
2094 1 : "current_physical_size": 42,
2095 1 : "attachment_status": {
2096 1 : "slug":"attached",
2097 1 : },
2098 1 : "generation" : 1
2099 1 : });
2100 1 :
2101 1 : assert_eq!(
2102 1 : serde_json::to_value(&original_active).unwrap(),
2103 1 : expected_active
2104 1 : );
2105 :
2106 1 : assert_eq!(
2107 1 : serde_json::to_value(&original_broken).unwrap(),
2108 1 : expected_broken
2109 1 : );
2110 1 : assert!(format!("{:?}", &original_broken.state).contains("reason"));
2111 1 : assert!(format!("{:?}", &original_broken.state).contains("backtrace info"));
2112 1 : }
2113 :
2114 : #[test]
2115 1 : fn test_reject_unknown_field() {
2116 1 : let id = TenantId::generate();
2117 1 : let config_request = json!({
2118 1 : "tenant_id": id.to_string(),
2119 1 : "unknown_field": "unknown_value".to_string(),
2120 1 : });
2121 1 : let err = serde_json::from_value::<TenantConfigRequest>(config_request).unwrap_err();
2122 1 : assert!(
2123 1 : err.to_string().contains("unknown field `unknown_field`"),
2124 0 : "expect unknown field `unknown_field` error, got: {}",
2125 : err
2126 : );
2127 1 : }
2128 :
2129 : #[test]
2130 1 : fn tenantstatus_activating_serde() {
2131 1 : let states = [TenantState::Activating(ActivatingFrom::Attaching)];
2132 1 : let expected = "[{\"slug\":\"Activating\",\"data\":\"Attaching\"}]";
2133 1 :
2134 1 : let actual = serde_json::to_string(&states).unwrap();
2135 1 :
2136 1 : assert_eq!(actual, expected);
2137 :
2138 1 : let parsed = serde_json::from_str::<Vec<TenantState>>(&actual).unwrap();
2139 1 :
2140 1 : assert_eq!(states.as_slice(), &parsed);
2141 1 : }
2142 :
2143 : #[test]
2144 1 : fn tenantstatus_activating_strum() {
2145 1 : // tests added, because we use these for metrics
2146 1 : let examples = [
2147 1 : (line!(), TenantState::Attaching, "Attaching"),
2148 1 : (
2149 1 : line!(),
2150 1 : TenantState::Activating(ActivatingFrom::Attaching),
2151 1 : "Activating",
2152 1 : ),
2153 1 : (line!(), TenantState::Active, "Active"),
2154 1 : (
2155 1 : line!(),
2156 1 : TenantState::Stopping {
2157 1 : progress: utils::completion::Barrier::default(),
2158 1 : },
2159 1 : "Stopping",
2160 1 : ),
2161 1 : (
2162 1 : line!(),
2163 1 : TenantState::Broken {
2164 1 : reason: "Example".into(),
2165 1 : backtrace: "Looooong backtrace".into(),
2166 1 : },
2167 1 : "Broken",
2168 1 : ),
2169 1 : ];
2170 :
2171 6 : for (line, rendered, expected) in examples {
2172 5 : let actual: &'static str = rendered.into();
2173 5 : assert_eq!(actual, expected, "example on {line}");
2174 : }
2175 1 : }
2176 :
2177 : #[test]
2178 1 : fn test_image_compression_algorithm_parsing() {
2179 : use ImageCompressionAlgorithm::*;
2180 1 : let cases = [
2181 1 : ("disabled", Disabled),
2182 1 : ("zstd", Zstd { level: None }),
2183 1 : ("zstd(18)", Zstd { level: Some(18) }),
2184 1 : ("zstd(-3)", Zstd { level: Some(-3) }),
2185 1 : ];
2186 :
2187 5 : for (display, expected) in cases {
2188 4 : assert_eq!(
2189 4 : ImageCompressionAlgorithm::from_str(display).unwrap(),
2190 : expected,
2191 0 : "parsing works"
2192 : );
2193 4 : assert_eq!(format!("{expected}"), display, "Display FromStr roundtrip");
2194 :
2195 4 : let ser = serde_json::to_string(&expected).expect("serialization");
2196 4 : assert_eq!(
2197 4 : serde_json::from_str::<ImageCompressionAlgorithm>(&ser).unwrap(),
2198 : expected,
2199 0 : "serde roundtrip"
2200 : );
2201 :
2202 4 : assert_eq!(
2203 4 : serde_json::Value::String(display.to_string()),
2204 4 : serde_json::to_value(expected).unwrap(),
2205 0 : "Display is the serde serialization"
2206 : );
2207 : }
2208 1 : }
2209 :
2210 : #[test]
2211 1 : fn test_tenant_config_patch_request_serde() {
2212 1 : let patch_request = TenantConfigPatchRequest {
2213 1 : tenant_id: TenantId::from_str("17c6d121946a61e5ab0fe5a2fd4d8215").unwrap(),
2214 1 : config: TenantConfigPatch {
2215 1 : checkpoint_distance: FieldPatch::Upsert(42),
2216 1 : gc_horizon: FieldPatch::Remove,
2217 1 : compaction_threshold: FieldPatch::Noop,
2218 1 : ..TenantConfigPatch::default()
2219 1 : },
2220 1 : };
2221 1 :
2222 1 : let json = serde_json::to_string(&patch_request).unwrap();
2223 1 :
2224 1 : let expected = r#"{"tenant_id":"17c6d121946a61e5ab0fe5a2fd4d8215","checkpoint_distance":42,"gc_horizon":null}"#;
2225 1 : assert_eq!(json, expected);
2226 :
2227 1 : let decoded: TenantConfigPatchRequest = serde_json::from_str(&json).unwrap();
2228 1 : assert_eq!(decoded.tenant_id, patch_request.tenant_id);
2229 1 : assert_eq!(decoded.config, patch_request.config);
2230 :
2231 : // Now apply the patch to a config to demonstrate semantics
2232 :
2233 1 : let base = TenantConfig {
2234 1 : checkpoint_distance: Some(28),
2235 1 : gc_horizon: Some(100),
2236 1 : compaction_target_size: Some(1024),
2237 1 : ..Default::default()
2238 1 : };
2239 1 :
2240 1 : let expected = TenantConfig {
2241 1 : checkpoint_distance: Some(42),
2242 1 : gc_horizon: None,
2243 1 : ..base.clone()
2244 1 : };
2245 1 :
2246 1 : let patched = base.apply_patch(decoded.config);
2247 1 :
2248 1 : assert_eq!(patched, expected);
2249 1 : }
2250 : }
|