Line data Source code
1 : //! Functions for handling per-tenant configuration options
2 : //!
3 : //! If tenant is created with --config option,
4 : //! the tenant-specific config will be stored in tenant's directory.
5 : //! Otherwise, global pageserver's config is used.
6 : //!
7 : //! If the tenant config file is corrupted, the tenant will be disabled.
8 : //! We cannot use global or default config instead, because wrong settings
9 : //! may lead to a data loss.
10 : //!
11 : pub(crate) use pageserver_api::config::TenantConfigToml as TenantConf;
12 : use pageserver_api::models::AuxFilePolicy;
13 : use pageserver_api::models::CompactionAlgorithmSettings;
14 : use pageserver_api::models::EvictionPolicy;
15 : use pageserver_api::models::{self, ThrottleConfig};
16 : use pageserver_api::shard::{ShardCount, ShardIdentity, ShardNumber, ShardStripeSize};
17 : use serde::de::IntoDeserializer;
18 : use serde::{Deserialize, Serialize};
19 : use serde_json::Value;
20 : use std::num::NonZeroU64;
21 : use std::time::Duration;
22 : use utils::generation::Generation;
23 :
24 0 : #[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
25 : pub(crate) enum AttachmentMode {
26 : /// Our generation is current as far as we know, and as far as we know we are the only attached
27 : /// pageserver. This is the "normal" attachment mode.
28 : Single,
29 : /// Our generation number is current as far as we know, but we are advised that another
30 : /// pageserver is still attached, and therefore to avoid executing deletions. This is
31 : /// the attachment mode of a pagesever that is the destination of a migration.
32 : Multi,
33 : /// Our generation number is superseded, or about to be superseded. We are advised
34 : /// to avoid remote storage writes if possible, and to avoid sending billing data. This
35 : /// is the attachment mode of a pageserver that is the origin of a migration.
36 : Stale,
37 : }
38 :
39 0 : #[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
40 : pub(crate) struct AttachedLocationConfig {
41 : pub(crate) generation: Generation,
42 : pub(crate) attach_mode: AttachmentMode,
43 : // TODO: add a flag to override AttachmentMode's policies under
44 : // disk pressure (i.e. unblock uploads under disk pressure in Stale
45 : // state, unblock deletions after timeout in Multi state)
46 : }
47 :
48 0 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
49 : pub(crate) struct SecondaryLocationConfig {
50 : /// If true, keep the local cache warm by polling remote storage
51 : pub(crate) warm: bool,
52 : }
53 :
54 0 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
55 : pub(crate) enum LocationMode {
56 : Attached(AttachedLocationConfig),
57 : Secondary(SecondaryLocationConfig),
58 : }
59 :
60 : /// Per-tenant, per-pageserver configuration. All pageservers use the same TenantConf,
61 : /// but have distinct LocationConf.
62 0 : #[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
63 : pub(crate) struct LocationConf {
64 : /// The location-specific part of the configuration, describes the operating
65 : /// mode of this pageserver for this tenant.
66 : pub(crate) mode: LocationMode,
67 :
68 : /// The detailed shard identity. This structure is already scoped within
69 : /// a TenantShardId, but we need the full ShardIdentity to enable calculating
70 : /// key->shard mappings.
71 : #[serde(default = "ShardIdentity::unsharded")]
72 : #[serde(skip_serializing_if = "ShardIdentity::is_unsharded")]
73 : pub(crate) shard: ShardIdentity,
74 :
75 : /// The pan-cluster tenant configuration, the same on all locations
76 : pub(crate) tenant_conf: TenantConfOpt,
77 : }
78 :
79 : impl std::fmt::Debug for LocationConf {
80 0 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 0 : match &self.mode {
82 0 : LocationMode::Attached(conf) => {
83 0 : write!(
84 0 : f,
85 0 : "Attached {:?}, gen={:?}",
86 0 : conf.attach_mode, conf.generation
87 0 : )
88 : }
89 0 : LocationMode::Secondary(conf) => {
90 0 : write!(f, "Secondary, warm={}", conf.warm)
91 : }
92 : }
93 0 : }
94 : }
95 :
96 : impl AttachedLocationConfig {
97 : /// Consult attachment mode to determine whether we are currently permitted
98 : /// to delete layers. This is only advisory, not required for data safety.
99 : /// See [`AttachmentMode`] for more context.
100 754 : pub(crate) fn may_delete_layers_hint(&self) -> bool {
101 754 : // TODO: add an override for disk pressure in AttachedLocationConfig,
102 754 : // and respect it here.
103 754 : match &self.attach_mode {
104 754 : AttachmentMode::Single => true,
105 : AttachmentMode::Multi | AttachmentMode::Stale => {
106 : // In Multi mode we avoid doing deletions because some other
107 : // attached pageserver might get 404 while trying to read
108 : // a layer we delete which is still referenced in their metadata.
109 : //
110 : // In Stale mode, we avoid doing deletions because we expect
111 : // that they would ultimately fail validation in the deletion
112 : // queue due to our stale generation.
113 0 : false
114 : }
115 : }
116 754 : }
117 :
118 : /// Whether we are currently hinted that it is worthwhile to upload layers.
119 : /// This is only advisory, not required for data safety.
120 : /// See [`AttachmentMode`] for more context.
121 0 : pub(crate) fn may_upload_layers_hint(&self) -> bool {
122 0 : // TODO: add an override for disk pressure in AttachedLocationConfig,
123 0 : // and respect it here.
124 0 : match &self.attach_mode {
125 0 : AttachmentMode::Single | AttachmentMode::Multi => true,
126 : AttachmentMode::Stale => {
127 : // In Stale mode, we avoid doing uploads because we expect that
128 : // our replacement pageserver will already have started its own
129 : // IndexPart that will never reference layers we upload: it is
130 : // wasteful.
131 0 : false
132 : }
133 : }
134 0 : }
135 : }
136 :
137 : impl LocationConf {
138 : /// For use when loading from a legacy configuration: presence of a tenant
139 : /// implies it is in AttachmentMode::Single, which used to be the only
140 : /// possible state. This function should eventually be removed.
141 186 : pub(crate) fn attached_single(
142 186 : tenant_conf: TenantConfOpt,
143 186 : generation: Generation,
144 186 : shard_params: &models::ShardParameters,
145 186 : ) -> Self {
146 186 : Self {
147 186 : mode: LocationMode::Attached(AttachedLocationConfig {
148 186 : generation,
149 186 : attach_mode: AttachmentMode::Single,
150 186 : }),
151 186 : shard: ShardIdentity::from_params(ShardNumber(0), shard_params),
152 186 : tenant_conf,
153 186 : }
154 186 : }
155 :
156 : /// For use when attaching/re-attaching: update the generation stored in this
157 : /// structure. If we were in a secondary state, promote to attached (posession
158 : /// of a fresh generation implies this).
159 0 : pub(crate) fn attach_in_generation(&mut self, mode: AttachmentMode, generation: Generation) {
160 0 : match &mut self.mode {
161 0 : LocationMode::Attached(attach_conf) => {
162 0 : attach_conf.generation = generation;
163 0 : attach_conf.attach_mode = mode;
164 0 : }
165 : LocationMode::Secondary(_) => {
166 : // We are promoted to attached by the control plane's re-attach response
167 0 : self.mode = LocationMode::Attached(AttachedLocationConfig {
168 0 : generation,
169 0 : attach_mode: mode,
170 0 : })
171 : }
172 : }
173 0 : }
174 :
175 0 : pub(crate) fn try_from(conf: &'_ models::LocationConfig) -> anyhow::Result<Self> {
176 0 : let tenant_conf = TenantConfOpt::try_from(&conf.tenant_conf)?;
177 :
178 0 : fn get_generation(conf: &'_ models::LocationConfig) -> Result<Generation, anyhow::Error> {
179 0 : conf.generation
180 0 : .map(Generation::new)
181 0 : .ok_or_else(|| anyhow::anyhow!("Generation must be set when attaching"))
182 0 : }
183 :
184 0 : let mode = match &conf.mode {
185 : models::LocationConfigMode::AttachedMulti => {
186 : LocationMode::Attached(AttachedLocationConfig {
187 0 : generation: get_generation(conf)?,
188 0 : attach_mode: AttachmentMode::Multi,
189 : })
190 : }
191 : models::LocationConfigMode::AttachedSingle => {
192 : LocationMode::Attached(AttachedLocationConfig {
193 0 : generation: get_generation(conf)?,
194 0 : attach_mode: AttachmentMode::Single,
195 : })
196 : }
197 : models::LocationConfigMode::AttachedStale => {
198 : LocationMode::Attached(AttachedLocationConfig {
199 0 : generation: get_generation(conf)?,
200 0 : attach_mode: AttachmentMode::Stale,
201 : })
202 : }
203 : models::LocationConfigMode::Secondary => {
204 0 : anyhow::ensure!(conf.generation.is_none());
205 :
206 0 : let warm = conf
207 0 : .secondary_conf
208 0 : .as_ref()
209 0 : .map(|c| c.warm)
210 0 : .unwrap_or(false);
211 0 : LocationMode::Secondary(SecondaryLocationConfig { warm })
212 : }
213 : models::LocationConfigMode::Detached => {
214 : // Should not have been called: API code should translate this mode
215 : // into a detach rather than trying to decode it as a LocationConf
216 0 : return Err(anyhow::anyhow!("Cannot decode a Detached configuration"));
217 : }
218 : };
219 :
220 0 : let shard = if conf.shard_count == 0 {
221 0 : ShardIdentity::unsharded()
222 : } else {
223 0 : ShardIdentity::new(
224 0 : ShardNumber(conf.shard_number),
225 0 : ShardCount::new(conf.shard_count),
226 0 : ShardStripeSize(conf.shard_stripe_size),
227 0 : )?
228 : };
229 :
230 0 : Ok(Self {
231 0 : shard,
232 0 : mode,
233 0 : tenant_conf,
234 0 : })
235 0 : }
236 : }
237 :
238 : impl Default for LocationConf {
239 : // TODO: this should be removed once tenant loading can guarantee that we are never
240 : // loading from a directory without a configuration.
241 : // => tech debt since https://github.com/neondatabase/neon/issues/1555
242 0 : fn default() -> Self {
243 0 : Self {
244 0 : mode: LocationMode::Attached(AttachedLocationConfig {
245 0 : generation: Generation::none(),
246 0 : attach_mode: AttachmentMode::Single,
247 0 : }),
248 0 : tenant_conf: TenantConfOpt::default(),
249 0 : shard: ShardIdentity::unsharded(),
250 0 : }
251 0 : }
252 : }
253 :
254 : /// Same as TenantConf, but this struct preserves the information about
255 : /// which parameters are set and which are not.
256 120 : #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
257 : pub struct TenantConfOpt {
258 : #[serde(skip_serializing_if = "Option::is_none")]
259 : #[serde(default)]
260 : pub checkpoint_distance: Option<u64>,
261 :
262 : #[serde(skip_serializing_if = "Option::is_none")]
263 : #[serde(with = "humantime_serde")]
264 : #[serde(default)]
265 : pub checkpoint_timeout: Option<Duration>,
266 :
267 : #[serde(skip_serializing_if = "Option::is_none")]
268 : #[serde(default)]
269 : pub compaction_target_size: Option<u64>,
270 :
271 : #[serde(skip_serializing_if = "Option::is_none")]
272 : #[serde(with = "humantime_serde")]
273 : #[serde(default)]
274 : pub compaction_period: Option<Duration>,
275 :
276 : #[serde(skip_serializing_if = "Option::is_none")]
277 : #[serde(default)]
278 : pub compaction_threshold: Option<usize>,
279 :
280 : #[serde(skip_serializing_if = "Option::is_none")]
281 : #[serde(default)]
282 : pub compaction_algorithm: Option<CompactionAlgorithmSettings>,
283 :
284 : #[serde(skip_serializing_if = "Option::is_none")]
285 : #[serde(default)]
286 : pub gc_horizon: Option<u64>,
287 :
288 : #[serde(skip_serializing_if = "Option::is_none")]
289 : #[serde(with = "humantime_serde")]
290 : #[serde(default)]
291 : pub gc_period: Option<Duration>,
292 :
293 : #[serde(skip_serializing_if = "Option::is_none")]
294 : #[serde(default)]
295 : pub image_creation_threshold: Option<usize>,
296 :
297 : #[serde(skip_serializing_if = "Option::is_none")]
298 : #[serde(with = "humantime_serde")]
299 : #[serde(default)]
300 : pub pitr_interval: Option<Duration>,
301 :
302 : #[serde(skip_serializing_if = "Option::is_none")]
303 : #[serde(with = "humantime_serde")]
304 : #[serde(default)]
305 : pub walreceiver_connect_timeout: Option<Duration>,
306 :
307 : #[serde(skip_serializing_if = "Option::is_none")]
308 : #[serde(with = "humantime_serde")]
309 : #[serde(default)]
310 : pub lagging_wal_timeout: Option<Duration>,
311 :
312 : #[serde(skip_serializing_if = "Option::is_none")]
313 : #[serde(default)]
314 : pub max_lsn_wal_lag: Option<NonZeroU64>,
315 :
316 : #[serde(skip_serializing_if = "Option::is_none")]
317 : #[serde(default)]
318 : pub eviction_policy: Option<EvictionPolicy>,
319 :
320 : #[serde(skip_serializing_if = "Option::is_none")]
321 : #[serde(default)]
322 : pub min_resident_size_override: Option<u64>,
323 :
324 : #[serde(skip_serializing_if = "Option::is_none")]
325 : #[serde(with = "humantime_serde")]
326 : #[serde(default)]
327 : pub evictions_low_residence_duration_metric_threshold: Option<Duration>,
328 :
329 : #[serde(skip_serializing_if = "Option::is_none")]
330 : #[serde(with = "humantime_serde")]
331 : #[serde(default)]
332 : pub heatmap_period: Option<Duration>,
333 :
334 : #[serde(skip_serializing_if = "Option::is_none")]
335 : #[serde(default)]
336 : pub lazy_slru_download: Option<bool>,
337 :
338 : #[serde(skip_serializing_if = "Option::is_none")]
339 : pub timeline_get_throttle: Option<pageserver_api::models::ThrottleConfig>,
340 :
341 : #[serde(skip_serializing_if = "Option::is_none")]
342 : pub image_layer_creation_check_threshold: Option<u8>,
343 :
344 : #[serde(skip_serializing_if = "Option::is_none")]
345 : #[serde(default)]
346 : pub switch_aux_file_policy: Option<AuxFilePolicy>,
347 :
348 : #[serde(skip_serializing_if = "Option::is_none")]
349 : #[serde(with = "humantime_serde")]
350 : #[serde(default)]
351 : pub lsn_lease_length: Option<Duration>,
352 :
353 : #[serde(skip_serializing_if = "Option::is_none")]
354 : #[serde(with = "humantime_serde")]
355 : #[serde(default)]
356 : pub lsn_lease_length_for_ts: Option<Duration>,
357 : }
358 :
359 : impl TenantConfOpt {
360 0 : pub fn merge(&self, global_conf: TenantConf) -> TenantConf {
361 0 : TenantConf {
362 0 : checkpoint_distance: self
363 0 : .checkpoint_distance
364 0 : .unwrap_or(global_conf.checkpoint_distance),
365 0 : checkpoint_timeout: self
366 0 : .checkpoint_timeout
367 0 : .unwrap_or(global_conf.checkpoint_timeout),
368 0 : compaction_target_size: self
369 0 : .compaction_target_size
370 0 : .unwrap_or(global_conf.compaction_target_size),
371 0 : compaction_period: self
372 0 : .compaction_period
373 0 : .unwrap_or(global_conf.compaction_period),
374 0 : compaction_threshold: self
375 0 : .compaction_threshold
376 0 : .unwrap_or(global_conf.compaction_threshold),
377 0 : compaction_algorithm: self
378 0 : .compaction_algorithm
379 0 : .as_ref()
380 0 : .unwrap_or(&global_conf.compaction_algorithm)
381 0 : .clone(),
382 0 : gc_horizon: self.gc_horizon.unwrap_or(global_conf.gc_horizon),
383 0 : gc_period: self.gc_period.unwrap_or(global_conf.gc_period),
384 0 : image_creation_threshold: self
385 0 : .image_creation_threshold
386 0 : .unwrap_or(global_conf.image_creation_threshold),
387 0 : pitr_interval: self.pitr_interval.unwrap_or(global_conf.pitr_interval),
388 0 : walreceiver_connect_timeout: self
389 0 : .walreceiver_connect_timeout
390 0 : .unwrap_or(global_conf.walreceiver_connect_timeout),
391 0 : lagging_wal_timeout: self
392 0 : .lagging_wal_timeout
393 0 : .unwrap_or(global_conf.lagging_wal_timeout),
394 0 : max_lsn_wal_lag: self.max_lsn_wal_lag.unwrap_or(global_conf.max_lsn_wal_lag),
395 0 : eviction_policy: self.eviction_policy.unwrap_or(global_conf.eviction_policy),
396 0 : min_resident_size_override: self
397 0 : .min_resident_size_override
398 0 : .or(global_conf.min_resident_size_override),
399 0 : evictions_low_residence_duration_metric_threshold: self
400 0 : .evictions_low_residence_duration_metric_threshold
401 0 : .unwrap_or(global_conf.evictions_low_residence_duration_metric_threshold),
402 0 : heatmap_period: self.heatmap_period.unwrap_or(global_conf.heatmap_period),
403 0 : lazy_slru_download: self
404 0 : .lazy_slru_download
405 0 : .unwrap_or(global_conf.lazy_slru_download),
406 0 : timeline_get_throttle: self
407 0 : .timeline_get_throttle
408 0 : .clone()
409 0 : .unwrap_or(global_conf.timeline_get_throttle),
410 0 : image_layer_creation_check_threshold: self
411 0 : .image_layer_creation_check_threshold
412 0 : .unwrap_or(global_conf.image_layer_creation_check_threshold),
413 0 : switch_aux_file_policy: self
414 0 : .switch_aux_file_policy
415 0 : .unwrap_or(global_conf.switch_aux_file_policy),
416 0 : lsn_lease_length: self
417 0 : .lsn_lease_length
418 0 : .unwrap_or(global_conf.lsn_lease_length),
419 0 : lsn_lease_length_for_ts: self
420 0 : .lsn_lease_length_for_ts
421 0 : .unwrap_or(global_conf.lsn_lease_length_for_ts),
422 0 : }
423 0 : }
424 : }
425 :
426 : impl TryFrom<&'_ models::TenantConfig> for TenantConfOpt {
427 : type Error = anyhow::Error;
428 :
429 4 : fn try_from(request_data: &'_ models::TenantConfig) -> Result<Self, Self::Error> {
430 : // Convert the request_data to a JSON Value
431 4 : let json_value: Value = serde_json::to_value(request_data)?;
432 :
433 : // Create a Deserializer from the JSON Value
434 4 : let deserializer = json_value.into_deserializer();
435 :
436 : // Use serde_path_to_error to deserialize the JSON Value into TenantConfOpt
437 4 : let tenant_conf: TenantConfOpt = serde_path_to_error::deserialize(deserializer)?;
438 :
439 2 : Ok(tenant_conf)
440 4 : }
441 : }
442 :
443 : /// This is a conversion from our internal tenant config object to the one used
444 : /// in external APIs.
445 : impl From<TenantConfOpt> for models::TenantConfig {
446 0 : fn from(value: TenantConfOpt) -> Self {
447 0 : fn humantime(d: Duration) -> String {
448 0 : format!("{}s", d.as_secs())
449 0 : }
450 0 : Self {
451 0 : checkpoint_distance: value.checkpoint_distance,
452 0 : checkpoint_timeout: value.checkpoint_timeout.map(humantime),
453 0 : compaction_algorithm: value.compaction_algorithm,
454 0 : compaction_target_size: value.compaction_target_size,
455 0 : compaction_period: value.compaction_period.map(humantime),
456 0 : compaction_threshold: value.compaction_threshold,
457 0 : gc_horizon: value.gc_horizon,
458 0 : gc_period: value.gc_period.map(humantime),
459 0 : image_creation_threshold: value.image_creation_threshold,
460 0 : pitr_interval: value.pitr_interval.map(humantime),
461 0 : walreceiver_connect_timeout: value.walreceiver_connect_timeout.map(humantime),
462 0 : lagging_wal_timeout: value.lagging_wal_timeout.map(humantime),
463 0 : max_lsn_wal_lag: value.max_lsn_wal_lag,
464 0 : eviction_policy: value.eviction_policy,
465 0 : min_resident_size_override: value.min_resident_size_override,
466 0 : evictions_low_residence_duration_metric_threshold: value
467 0 : .evictions_low_residence_duration_metric_threshold
468 0 : .map(humantime),
469 0 : heatmap_period: value.heatmap_period.map(humantime),
470 0 : lazy_slru_download: value.lazy_slru_download,
471 0 : timeline_get_throttle: value.timeline_get_throttle.map(ThrottleConfig::from),
472 0 : image_layer_creation_check_threshold: value.image_layer_creation_check_threshold,
473 0 : switch_aux_file_policy: value.switch_aux_file_policy,
474 0 : lsn_lease_length: value.lsn_lease_length.map(humantime),
475 0 : lsn_lease_length_for_ts: value.lsn_lease_length_for_ts.map(humantime),
476 0 : }
477 0 : }
478 : }
479 :
480 : #[cfg(test)]
481 : mod tests {
482 : use super::*;
483 : use models::TenantConfig;
484 :
485 : #[test]
486 2 : fn de_serializing_pageserver_config_omits_empty_values() {
487 2 : let small_conf = TenantConfOpt {
488 2 : gc_horizon: Some(42),
489 2 : ..TenantConfOpt::default()
490 2 : };
491 2 :
492 2 : let toml_form = toml_edit::ser::to_string(&small_conf).unwrap();
493 2 : assert_eq!(toml_form, "gc_horizon = 42\n");
494 2 : assert_eq!(small_conf, toml_edit::de::from_str(&toml_form).unwrap());
495 :
496 2 : let json_form = serde_json::to_string(&small_conf).unwrap();
497 2 : assert_eq!(json_form, "{\"gc_horizon\":42}");
498 2 : assert_eq!(small_conf, serde_json::from_str(&json_form).unwrap());
499 2 : }
500 :
501 : #[test]
502 2 : fn test_try_from_models_tenant_config_err() {
503 2 : let tenant_config = models::TenantConfig {
504 2 : lagging_wal_timeout: Some("5a".to_string()),
505 2 : ..TenantConfig::default()
506 2 : };
507 2 :
508 2 : let tenant_conf_opt = TenantConfOpt::try_from(&tenant_config);
509 2 :
510 2 : assert!(
511 2 : tenant_conf_opt.is_err(),
512 0 : "Suceeded to convert TenantConfig to TenantConfOpt"
513 : );
514 :
515 2 : let expected_error_str =
516 2 : "lagging_wal_timeout: invalid value: string \"5a\", expected a duration";
517 2 : assert_eq!(tenant_conf_opt.unwrap_err().to_string(), expected_error_str);
518 2 : }
519 :
520 : #[test]
521 2 : fn test_try_from_models_tenant_config_success() {
522 2 : let tenant_config = models::TenantConfig {
523 2 : lagging_wal_timeout: Some("5s".to_string()),
524 2 : ..TenantConfig::default()
525 2 : };
526 2 :
527 2 : let tenant_conf_opt = TenantConfOpt::try_from(&tenant_config).unwrap();
528 2 :
529 2 : assert_eq!(
530 2 : tenant_conf_opt.lagging_wal_timeout,
531 2 : Some(Duration::from_secs(5))
532 2 : );
533 2 : }
534 : }
|