Line data Source code
1 : //! Functions for handling page server configuration options
2 : //!
3 : //! Configuration options can be set in the pageserver.toml configuration
4 : //! file, or on the command line.
5 : //! See also `settings.md` for better description on every parameter.
6 :
7 : pub mod ignored_fields;
8 :
9 : use std::env;
10 : use std::num::NonZeroUsize;
11 : use std::sync::Arc;
12 : use std::time::Duration;
13 :
14 : use anyhow::{Context, bail, ensure};
15 : use camino::{Utf8Path, Utf8PathBuf};
16 : use once_cell::sync::OnceCell;
17 : use pageserver_api::config::{DiskUsageEvictionTaskConfig, MaxVectoredReadBytes};
18 : use pageserver_api::models::ImageCompressionAlgorithm;
19 : use pageserver_api::shard::TenantShardId;
20 : use pem::Pem;
21 : use postgres_backend::AuthType;
22 : use remote_storage::{RemotePath, RemoteStorageConfig};
23 : use reqwest::Url;
24 : use storage_broker::Uri;
25 : use utils::id::{NodeId, TimelineId};
26 : use utils::logging::{LogFormat, SecretString};
27 : use utils::postgres_client::PostgresClientProtocol;
28 :
29 : use crate::tenant::storage_layer::inmemory_layer::IndexEntry;
30 : use crate::tenant::{TENANTS_SEGMENT_NAME, TIMELINES_SEGMENT_NAME};
31 : use crate::virtual_file::io_engine;
32 : use crate::{TENANT_HEATMAP_BASENAME, TENANT_LOCATION_CONFIG_NAME, virtual_file};
33 :
34 : /// Global state of pageserver.
35 : ///
36 : /// It's mostly immutable configuration, but some semaphores and the
37 : /// like crept in over time and the name stuck.
38 : ///
39 : /// Instantiated by deserializing `pageserver.toml` into [`pageserver_api::config::ConfigToml`]
40 : /// and passing that to [`PageServerConf::parse_and_validate`].
41 : ///
42 : /// # Adding a New Field
43 : ///
44 : /// 1. Add the field to `pageserver_api::config::ConfigToml`.
45 : /// 2. Fix compiler errors (exhaustive destructuring will guide you).
46 : ///
47 : /// For fields that require additional validation or filling in of defaults at runtime,
48 : /// check for examples in the [`PageServerConf::parse_and_validate`] method.
49 : #[derive(Debug, Clone)]
50 : pub struct PageServerConf {
51 : // Identifier of that particular pageserver so e g safekeepers
52 : // can safely distinguish different pageservers
53 : pub id: NodeId,
54 :
55 : /// Example (default): 127.0.0.1:64000
56 : pub listen_pg_addr: String,
57 : /// Example (default): 127.0.0.1:9898
58 : pub listen_http_addr: String,
59 : /// Example: 127.0.0.1:9899
60 : pub listen_https_addr: Option<String>,
61 :
62 : /// Path to a file with certificate's private key for https API.
63 : /// Default: server.key
64 : pub ssl_key_file: Utf8PathBuf,
65 : /// Path to a file with a X509 certificate for https API.
66 : /// Default: server.crt
67 : pub ssl_cert_file: Utf8PathBuf,
68 : /// Period to reload certificate and private key from files.
69 : /// Default: 60s.
70 : pub ssl_cert_reload_period: Duration,
71 : /// Trusted root CA certificates to use in https APIs in PEM format.
72 : pub ssl_ca_certs: Vec<Pem>,
73 :
74 : /// Current availability zone. Used for traffic metrics.
75 : pub availability_zone: Option<String>,
76 :
77 : // Timeout when waiting for WAL receiver to catch up to an LSN given in a GetPage@LSN call.
78 : pub wait_lsn_timeout: Duration,
79 : // How long to wait for WAL redo to complete.
80 : pub wal_redo_timeout: Duration,
81 :
82 : pub superuser: String,
83 : pub locale: String,
84 :
85 : pub page_cache_size: usize,
86 : pub max_file_descriptors: usize,
87 :
88 : // Repository directory, relative to current working directory.
89 : // Normally, the page server changes the current working directory
90 : // to the repository, and 'workdir' is always '.'. But we don't do
91 : // that during unit testing, because the current directory is global
92 : // to the process but different unit tests work on different
93 : // repositories.
94 : pub workdir: Utf8PathBuf,
95 :
96 : pub pg_distrib_dir: Utf8PathBuf,
97 :
98 : // Authentication
99 : /// authentication method for the HTTP mgmt API
100 : pub http_auth_type: AuthType,
101 : /// authentication method for libpq connections from compute
102 : pub pg_auth_type: AuthType,
103 : /// Path to a file or directory containing public key(s) for verifying JWT tokens.
104 : /// Used for both mgmt and compute auth, if enabled.
105 : pub auth_validation_public_key_path: Option<Utf8PathBuf>,
106 :
107 : pub remote_storage_config: Option<RemoteStorageConfig>,
108 :
109 : pub default_tenant_conf: pageserver_api::config::TenantConfigToml,
110 :
111 : /// Storage broker endpoints to connect to.
112 : pub broker_endpoint: Uri,
113 : pub broker_keepalive_interval: Duration,
114 :
115 : pub log_format: LogFormat,
116 :
117 : /// Number of tenants which will be concurrently loaded from remote storage proactively on startup or attach.
118 : ///
119 : /// A lower value implicitly deprioritizes loading such tenants, vs. other work in the system.
120 : pub concurrent_tenant_warmup: ConfigurableSemaphore,
121 :
122 : /// Number of concurrent [`TenantShard::gather_size_inputs`](crate::tenant::TenantShard::gather_size_inputs) allowed.
123 : pub concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore,
124 : /// Limit of concurrent [`TenantShard::gather_size_inputs`] issued by module `eviction_task`.
125 : /// The number of permits is the same as `concurrent_tenant_size_logical_size_queries`.
126 : /// See the comment in `eviction_task` for details.
127 : ///
128 : /// [`TenantShard::gather_size_inputs`]: crate::tenant::TenantShard::gather_size_inputs
129 : pub eviction_task_immitated_concurrent_logical_size_queries: ConfigurableSemaphore,
130 :
131 : // How often to collect metrics and send them to the metrics endpoint.
132 : pub metric_collection_interval: Duration,
133 : // How often to send unchanged cached metrics to the metrics endpoint.
134 : pub metric_collection_endpoint: Option<Url>,
135 : pub metric_collection_bucket: Option<RemoteStorageConfig>,
136 : pub synthetic_size_calculation_interval: Duration,
137 :
138 : pub disk_usage_based_eviction: Option<DiskUsageEvictionTaskConfig>,
139 :
140 : pub test_remote_failures: u64,
141 :
142 : pub ondemand_download_behavior_treat_error_as_warn: bool,
143 :
144 : /// How long will background tasks be delayed at most after initial load of tenants.
145 : ///
146 : /// Our largest initialization completions are in the range of 100-200s, so perhaps 10s works
147 : /// as we now isolate initial loading, initial logical size calculation and background tasks.
148 : /// Smaller nodes will have background tasks "not running" for this long unless every timeline
149 : /// has it's initial logical size calculated. Not running background tasks for some seconds is
150 : /// not terrible.
151 : pub background_task_maximum_delay: Duration,
152 :
153 : pub control_plane_api: Url,
154 :
155 : /// JWT token for use with the control plane API.
156 : pub control_plane_api_token: Option<SecretString>,
157 :
158 : pub import_pgdata_upcall_api: Option<Url>,
159 : pub import_pgdata_upcall_api_token: Option<SecretString>,
160 : pub import_pgdata_aws_endpoint_url: Option<Url>,
161 :
162 : /// If true, pageserver will make best-effort to operate without a control plane: only
163 : /// for use in major incidents.
164 : pub control_plane_emergency_mode: bool,
165 :
166 : /// How many heatmap uploads may be done concurrency: lower values implicitly deprioritize
167 : /// heatmap uploads vs. other remote storage operations.
168 : pub heatmap_upload_concurrency: usize,
169 :
170 : /// How many remote storage downloads may be done for secondary tenants concurrently. Implicitly
171 : /// deprioritises secondary downloads vs. remote storage operations for attached tenants.
172 : pub secondary_download_concurrency: usize,
173 :
174 : /// Maximum number of WAL records to be ingested and committed at the same time
175 : pub ingest_batch_size: u64,
176 :
177 : pub virtual_file_io_engine: virtual_file::IoEngineKind,
178 :
179 : pub max_vectored_read_bytes: MaxVectoredReadBytes,
180 :
181 : pub image_compression: ImageCompressionAlgorithm,
182 :
183 : /// Whether to offload archived timelines automatically
184 : pub timeline_offloading: bool,
185 :
186 : /// How many bytes of ephemeral layer content will we allow per kilobyte of RAM. When this
187 : /// is exceeded, we start proactively closing ephemeral layers to limit the total amount
188 : /// of ephemeral data.
189 : ///
190 : /// Setting this to zero disables limits on total ephemeral layer size.
191 : pub ephemeral_bytes_per_memory_kb: usize,
192 :
193 : pub l0_flush: crate::l0_flush::L0FlushConfig,
194 :
195 : /// Direct IO settings
196 : pub virtual_file_io_mode: virtual_file::IoMode,
197 :
198 : /// Optionally disable disk syncs (unsafe!)
199 : pub no_sync: bool,
200 :
201 : pub wal_receiver_protocol: PostgresClientProtocol,
202 :
203 : pub page_service_pipelining: pageserver_api::config::PageServicePipeliningConfig,
204 :
205 : pub get_vectored_concurrent_io: pageserver_api::config::GetVectoredConcurrentIo,
206 :
207 : /// Enable read path debugging. If enabled, read key errors will print a backtrace of the layer
208 : /// files read.
209 : pub enable_read_path_debugging: bool,
210 :
211 : /// Interpreted protocol feature: if enabled, validate that the logical WAL received from
212 : /// safekeepers does not have gaps.
213 : pub validate_wal_contiguity: bool,
214 :
215 : /// When set, the previously written to disk heatmap is loaded on tenant attach and used
216 : /// to avoid clobbering the heatmap from new, cold, attached locations.
217 : pub load_previous_heatmap: bool,
218 :
219 : /// When set, include visible layers in the next uploaded heatmaps of an unarchived timeline.
220 : pub generate_unarchival_heatmap: bool,
221 :
222 : pub tracing: Option<pageserver_api::config::Tracing>,
223 :
224 : /// Enable TLS in page service API.
225 : /// Does not force TLS: the client negotiates TLS usage during the handshake.
226 : /// Uses key and certificate from ssl_key_file/ssl_cert_file.
227 : pub enable_tls_page_service_api: bool,
228 :
229 : /// Run in development mode, which disables certain safety checks
230 : /// such as authentication requirements for HTTP and PostgreSQL APIs.
231 : /// This is insecure and should only be used in development environments.
232 : pub dev_mode: bool,
233 :
234 : pub timeline_import_config: pageserver_api::config::TimelineImportConfig,
235 :
236 : pub basebackup_cache_config: Option<pageserver_api::config::BasebackupCacheConfig>,
237 : }
238 :
239 : /// Token for authentication to safekeepers
240 : ///
241 : /// We do not want to store this in a PageServerConf because the latter may be logged
242 : /// and/or serialized at a whim, while the token is secret. Currently this token is the
243 : /// same for accessing all tenants/timelines, but may become per-tenant/per-timeline in
244 : /// the future, more tokens and auth may arrive for storage broker, completely changing the logic.
245 : /// Hence, we resort to a global variable for now instead of passing the token from the
246 : /// startup code to the connection code through a dozen layers.
247 : pub static SAFEKEEPER_AUTH_TOKEN: OnceCell<Arc<String>> = OnceCell::new();
248 :
249 : impl PageServerConf {
250 : //
251 : // Repository paths, relative to workdir.
252 : //
253 :
254 3938 : pub fn tenants_path(&self) -> Utf8PathBuf {
255 3938 : self.workdir.join(TENANTS_SEGMENT_NAME)
256 3938 : }
257 :
258 36 : pub fn deletion_prefix(&self) -> Utf8PathBuf {
259 36 : self.workdir.join("deletion")
260 36 : }
261 :
262 0 : pub fn metadata_path(&self) -> Utf8PathBuf {
263 0 : self.workdir.join("metadata.json")
264 0 : }
265 :
266 0 : pub fn basebackup_cache_dir(&self) -> Utf8PathBuf {
267 0 : self.workdir.join("basebackup_cache")
268 0 : }
269 :
270 14 : pub fn deletion_list_path(&self, sequence: u64) -> Utf8PathBuf {
271 : // Encode a version in the filename, so that if we ever switch away from JSON we can
272 : // increment this.
273 : const VERSION: u8 = 1;
274 :
275 14 : self.deletion_prefix()
276 14 : .join(format!("{sequence:016x}-{VERSION:02x}.list"))
277 14 : }
278 :
279 12 : pub fn deletion_header_path(&self) -> Utf8PathBuf {
280 : // Encode a version in the filename, so that if we ever switch away from JSON we can
281 : // increment this.
282 : const VERSION: u8 = 1;
283 :
284 12 : self.deletion_prefix().join(format!("header-{VERSION:02x}"))
285 12 : }
286 :
287 3911 : pub fn tenant_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf {
288 3911 : self.tenants_path().join(tenant_shard_id.to_string())
289 3911 : }
290 :
291 : /// Points to a place in pageserver's local directory,
292 : /// where certain tenant's LocationConf be stored.
293 0 : pub(crate) fn tenant_location_config_path(
294 0 : &self,
295 0 : tenant_shard_id: &TenantShardId,
296 0 : ) -> Utf8PathBuf {
297 0 : self.tenant_path(tenant_shard_id)
298 0 : .join(TENANT_LOCATION_CONFIG_NAME)
299 0 : }
300 :
301 117 : pub(crate) fn tenant_heatmap_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf {
302 117 : self.tenant_path(tenant_shard_id)
303 117 : .join(TENANT_HEATMAP_BASENAME)
304 117 : }
305 :
306 3674 : pub fn timelines_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf {
307 3674 : self.tenant_path(tenant_shard_id)
308 3674 : .join(TIMELINES_SEGMENT_NAME)
309 3674 : }
310 :
311 3437 : pub fn timeline_path(
312 3437 : &self,
313 3437 : tenant_shard_id: &TenantShardId,
314 3437 : timeline_id: &TimelineId,
315 3437 : ) -> Utf8PathBuf {
316 3437 : self.timelines_path(tenant_shard_id)
317 3437 : .join(timeline_id.to_string())
318 3437 : }
319 :
320 : /// Turns storage remote path of a file into its local path.
321 0 : pub fn local_path(&self, remote_path: &RemotePath) -> Utf8PathBuf {
322 0 : remote_path.with_base(&self.workdir)
323 0 : }
324 :
325 : //
326 : // Postgres distribution paths
327 : //
328 12 : pub fn pg_distrib_dir(&self, pg_version: u32) -> anyhow::Result<Utf8PathBuf> {
329 12 : let path = self.pg_distrib_dir.clone();
330 12 :
331 12 : #[allow(clippy::manual_range_patterns)]
332 12 : match pg_version {
333 12 : 14 | 15 | 16 | 17 => Ok(path.join(format!("v{pg_version}"))),
334 0 : _ => bail!("Unsupported postgres version: {}", pg_version),
335 : }
336 12 : }
337 :
338 6 : pub fn pg_bin_dir(&self, pg_version: u32) -> anyhow::Result<Utf8PathBuf> {
339 6 : Ok(self.pg_distrib_dir(pg_version)?.join("bin"))
340 6 : }
341 6 : pub fn pg_lib_dir(&self, pg_version: u32) -> anyhow::Result<Utf8PathBuf> {
342 6 : Ok(self.pg_distrib_dir(pg_version)?.join("lib"))
343 6 : }
344 :
345 : /// Parse a configuration file (pageserver.toml) into a PageServerConf struct,
346 : /// validating the input and failing on errors.
347 : ///
348 : /// This leaves any options not present in the file in the built-in defaults.
349 127 : pub fn parse_and_validate(
350 127 : id: NodeId,
351 127 : config_toml: pageserver_api::config::ConfigToml,
352 127 : workdir: &Utf8Path,
353 127 : ) -> anyhow::Result<Self> {
354 127 : let pageserver_api::config::ConfigToml {
355 127 : listen_pg_addr,
356 127 : listen_http_addr,
357 127 : listen_https_addr,
358 127 : ssl_key_file,
359 127 : ssl_cert_file,
360 127 : ssl_cert_reload_period,
361 127 : ssl_ca_file,
362 127 : availability_zone,
363 127 : wait_lsn_timeout,
364 127 : wal_redo_timeout,
365 127 : superuser,
366 127 : locale,
367 127 : page_cache_size,
368 127 : max_file_descriptors,
369 127 : pg_distrib_dir,
370 127 : http_auth_type,
371 127 : pg_auth_type,
372 127 : auth_validation_public_key_path,
373 127 : remote_storage,
374 127 : broker_endpoint,
375 127 : broker_keepalive_interval,
376 127 : log_format,
377 127 : metric_collection_interval,
378 127 : metric_collection_endpoint,
379 127 : metric_collection_bucket,
380 127 : synthetic_size_calculation_interval,
381 127 : disk_usage_based_eviction,
382 127 : test_remote_failures,
383 127 : ondemand_download_behavior_treat_error_as_warn,
384 127 : background_task_maximum_delay,
385 127 : control_plane_api,
386 127 : control_plane_api_token,
387 127 : control_plane_emergency_mode,
388 127 : import_pgdata_upcall_api,
389 127 : import_pgdata_upcall_api_token,
390 127 : import_pgdata_aws_endpoint_url,
391 127 : heatmap_upload_concurrency,
392 127 : secondary_download_concurrency,
393 127 : ingest_batch_size,
394 127 : max_vectored_read_bytes,
395 127 : image_compression,
396 127 : timeline_offloading,
397 127 : ephemeral_bytes_per_memory_kb,
398 127 : l0_flush,
399 127 : virtual_file_io_mode,
400 127 : concurrent_tenant_warmup,
401 127 : concurrent_tenant_size_logical_size_queries,
402 127 : virtual_file_io_engine,
403 127 : tenant_config,
404 127 : no_sync,
405 127 : wal_receiver_protocol,
406 127 : page_service_pipelining,
407 127 : get_vectored_concurrent_io,
408 127 : enable_read_path_debugging,
409 127 : validate_wal_contiguity,
410 127 : load_previous_heatmap,
411 127 : generate_unarchival_heatmap,
412 127 : tracing,
413 127 : enable_tls_page_service_api,
414 127 : dev_mode,
415 127 : timeline_import_config,
416 127 : basebackup_cache_config,
417 127 : } = config_toml;
418 :
419 127 : let mut conf = PageServerConf {
420 : // ------------------------------------------------------------
421 : // fields that are already fully validated by the ConfigToml Deserialize impl
422 : // ------------------------------------------------------------
423 127 : listen_pg_addr,
424 127 : listen_http_addr,
425 127 : listen_https_addr,
426 127 : ssl_key_file,
427 127 : ssl_cert_file,
428 127 : ssl_cert_reload_period,
429 127 : availability_zone,
430 127 : wait_lsn_timeout,
431 127 : wal_redo_timeout,
432 127 : superuser,
433 127 : locale,
434 127 : page_cache_size,
435 127 : max_file_descriptors,
436 127 : http_auth_type,
437 127 : pg_auth_type,
438 127 : auth_validation_public_key_path,
439 127 : remote_storage_config: remote_storage,
440 127 : broker_endpoint,
441 127 : broker_keepalive_interval,
442 127 : log_format,
443 127 : metric_collection_interval,
444 127 : metric_collection_endpoint,
445 127 : metric_collection_bucket,
446 127 : synthetic_size_calculation_interval,
447 127 : disk_usage_based_eviction,
448 127 : test_remote_failures,
449 127 : ondemand_download_behavior_treat_error_as_warn,
450 127 : background_task_maximum_delay,
451 127 : control_plane_api: control_plane_api
452 127 : .ok_or_else(|| anyhow::anyhow!("`control_plane_api` must be set"))?,
453 127 : control_plane_emergency_mode,
454 127 : heatmap_upload_concurrency,
455 127 : secondary_download_concurrency,
456 127 : ingest_batch_size,
457 127 : max_vectored_read_bytes,
458 127 : image_compression,
459 127 : timeline_offloading,
460 127 : ephemeral_bytes_per_memory_kb,
461 127 : import_pgdata_upcall_api,
462 127 : import_pgdata_upcall_api_token: import_pgdata_upcall_api_token.map(SecretString::from),
463 127 : import_pgdata_aws_endpoint_url,
464 127 : wal_receiver_protocol,
465 127 : page_service_pipelining,
466 127 : get_vectored_concurrent_io,
467 127 : tracing,
468 127 : enable_tls_page_service_api,
469 127 : dev_mode,
470 127 : timeline_import_config,
471 127 : basebackup_cache_config,
472 127 :
473 127 : // ------------------------------------------------------------
474 127 : // fields that require additional validation or custom handling
475 127 : // ------------------------------------------------------------
476 127 : workdir: workdir.to_owned(),
477 127 : pg_distrib_dir: pg_distrib_dir.unwrap_or_else(|| {
478 2 : std::env::current_dir()
479 2 : .expect("current_dir() failed")
480 2 : .try_into()
481 2 : .expect("current_dir() is not a valid Utf8Path")
482 127 : }),
483 127 : control_plane_api_token: control_plane_api_token.map(SecretString::from),
484 127 : id,
485 127 : default_tenant_conf: tenant_config,
486 127 : concurrent_tenant_warmup: ConfigurableSemaphore::new(concurrent_tenant_warmup),
487 127 : concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore::new(
488 127 : concurrent_tenant_size_logical_size_queries,
489 127 : ),
490 127 : eviction_task_immitated_concurrent_logical_size_queries: ConfigurableSemaphore::new(
491 127 : // re-use `concurrent_tenant_size_logical_size_queries`
492 127 : concurrent_tenant_size_logical_size_queries,
493 127 : ),
494 127 : virtual_file_io_engine: match virtual_file_io_engine {
495 0 : Some(v) => v,
496 127 : None => match crate::virtual_file::io_engine_feature_test()
497 127 : .context("auto-detect virtual_file_io_engine")?
498 : {
499 127 : io_engine::FeatureTestResult::PlatformPreferred(v) => v, // make no noise
500 0 : io_engine::FeatureTestResult::Worse { engine, remark } => {
501 0 : // TODO: bubble this up to the caller so we can tracing::warn! it.
502 0 : eprintln!(
503 0 : "auto-detected IO engine is not platform-preferred: engine={engine:?} remark={remark:?}"
504 0 : );
505 0 : engine
506 : }
507 : },
508 : },
509 127 : l0_flush: l0_flush
510 127 : .map(crate::l0_flush::L0FlushConfig::from)
511 127 : .unwrap_or_default(),
512 127 : virtual_file_io_mode: virtual_file_io_mode.unwrap_or(virtual_file::IoMode::preferred()),
513 127 : no_sync: no_sync.unwrap_or(false),
514 127 : enable_read_path_debugging: enable_read_path_debugging.unwrap_or(false),
515 127 : validate_wal_contiguity: validate_wal_contiguity.unwrap_or(false),
516 127 : load_previous_heatmap: load_previous_heatmap.unwrap_or(true),
517 127 : generate_unarchival_heatmap: generate_unarchival_heatmap.unwrap_or(true),
518 127 : ssl_ca_certs: match ssl_ca_file {
519 0 : Some(ssl_ca_file) => {
520 0 : let buf = std::fs::read(ssl_ca_file)?;
521 0 : pem::parse_many(&buf)?
522 0 : .into_iter()
523 0 : .filter(|pem| pem.tag() == "CERTIFICATE")
524 0 : .collect()
525 : }
526 127 : None => Vec::new(),
527 : },
528 : };
529 :
530 : // ------------------------------------------------------------
531 : // custom validation code that covers more than one field in isolation
532 : // ------------------------------------------------------------
533 :
534 127 : if conf.http_auth_type == AuthType::NeonJWT || conf.pg_auth_type == AuthType::NeonJWT {
535 0 : let auth_validation_public_key_path = conf
536 0 : .auth_validation_public_key_path
537 0 : .get_or_insert_with(|| workdir.join("auth_public_key.pem"));
538 0 : ensure!(
539 0 : auth_validation_public_key_path.exists(),
540 0 : format!(
541 0 : "Can't find auth_validation_public_key at '{auth_validation_public_key_path}'",
542 0 : )
543 : );
544 127 : }
545 :
546 127 : if let Some(tracing_config) = conf.tracing.as_ref() {
547 1 : let ratio = &tracing_config.sampling_ratio;
548 1 : ensure!(
549 1 : ratio.denominator != 0 && ratio.denominator >= ratio.numerator,
550 1 : format!(
551 1 : "Invalid sampling ratio: {}/{}",
552 1 : ratio.numerator, ratio.denominator
553 1 : )
554 : );
555 :
556 0 : let url = Url::parse(&tracing_config.export_config.endpoint)
557 0 : .map_err(anyhow::Error::msg)
558 0 : .with_context(|| {
559 0 : format!(
560 0 : "tracing endpoint URL is invalid : {}",
561 0 : tracing_config.export_config.endpoint
562 0 : )
563 0 : })?;
564 :
565 0 : ensure!(
566 0 : url.scheme() == "http" || url.scheme() == "https",
567 0 : format!(
568 0 : "tracing endpoint URL must start with http:// or https://: {}",
569 0 : tracing_config.export_config.endpoint
570 0 : )
571 : );
572 126 : }
573 :
574 126 : IndexEntry::validate_checkpoint_distance(conf.default_tenant_conf.checkpoint_distance)
575 126 : .map_err(anyhow::Error::msg)
576 126 : .with_context(|| {
577 0 : format!(
578 0 : "effective checkpoint distance is unsupported: {}",
579 0 : conf.default_tenant_conf.checkpoint_distance
580 0 : )
581 126 : })?;
582 :
583 126 : Ok(conf)
584 127 : }
585 :
586 : #[cfg(test)]
587 125 : pub fn test_repo_dir(test_name: &str) -> Utf8PathBuf {
588 125 : let test_output_dir = std::env::var("TEST_OUTPUT").unwrap_or("../tmp_check".into());
589 125 :
590 125 : let test_id = uuid::Uuid::new_v4();
591 125 : Utf8PathBuf::from(format!("{test_output_dir}/test_{test_name}_{test_id}"))
592 125 : }
593 :
594 125 : pub fn dummy_conf(repo_dir: Utf8PathBuf) -> Self {
595 125 : let pg_distrib_dir = Utf8PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../pg_install");
596 125 :
597 125 : let config_toml = pageserver_api::config::ConfigToml {
598 125 : wait_lsn_timeout: Duration::from_secs(60),
599 125 : wal_redo_timeout: Duration::from_secs(60),
600 125 : pg_distrib_dir: Some(pg_distrib_dir),
601 125 : metric_collection_interval: Duration::from_secs(60),
602 125 : synthetic_size_calculation_interval: Duration::from_secs(60),
603 125 : background_task_maximum_delay: Duration::ZERO,
604 125 : load_previous_heatmap: Some(true),
605 125 : generate_unarchival_heatmap: Some(true),
606 125 : control_plane_api: Some(Url::parse("http://localhost:6666").unwrap()),
607 125 : ..Default::default()
608 125 : };
609 125 : PageServerConf::parse_and_validate(NodeId(0), config_toml, &repo_dir).unwrap()
610 125 : }
611 : }
612 :
613 0 : #[derive(serde::Deserialize, serde::Serialize)]
614 : pub struct PageserverIdentity {
615 : pub id: NodeId,
616 : }
617 :
618 : /// Configurable semaphore permits setting.
619 : ///
620 : /// Does not allow semaphore permits to be zero, because at runtime initially zero permits and empty
621 : /// semaphore cannot be distinguished, leading any feature using these to await forever (or until
622 : /// new permits are added).
623 : #[derive(Debug, Clone)]
624 : pub struct ConfigurableSemaphore {
625 : initial_permits: NonZeroUsize,
626 : inner: std::sync::Arc<tokio::sync::Semaphore>,
627 : }
628 :
629 : impl ConfigurableSemaphore {
630 : /// Initializse using a non-zero amount of permits.
631 : ///
632 : /// Require a non-zero initial permits, because using permits == 0 is a crude way to disable a
633 : /// feature such as [`TenantShard::gather_size_inputs`]. Otherwise any semaphore using future will
634 : /// behave like [`futures::future::pending`], just waiting until new permits are added.
635 : ///
636 : /// [`TenantShard::gather_size_inputs`]: crate::tenant::TenantShard::gather_size_inputs
637 381 : pub fn new(initial_permits: NonZeroUsize) -> Self {
638 381 : ConfigurableSemaphore {
639 381 : initial_permits,
640 381 : inner: std::sync::Arc::new(tokio::sync::Semaphore::new(initial_permits.get())),
641 381 : }
642 381 : }
643 :
644 : /// Returns the configured amount of permits.
645 0 : pub fn initial_permits(&self) -> NonZeroUsize {
646 0 : self.initial_permits
647 0 : }
648 : }
649 :
650 : impl PartialEq for ConfigurableSemaphore {
651 0 : fn eq(&self, other: &Self) -> bool {
652 0 : // the number of permits can be increased at runtime, so we cannot really fulfill the
653 0 : // PartialEq value equality otherwise
654 0 : self.initial_permits == other.initial_permits
655 0 : }
656 : }
657 :
658 : impl Eq for ConfigurableSemaphore {}
659 :
660 : impl ConfigurableSemaphore {
661 0 : pub fn inner(&self) -> &std::sync::Arc<tokio::sync::Semaphore> {
662 0 : &self.inner
663 0 : }
664 : }
665 :
666 : #[cfg(test)]
667 : mod tests {
668 :
669 : use camino::Utf8PathBuf;
670 : use utils::id::NodeId;
671 :
672 : use super::PageServerConf;
673 :
674 : #[test]
675 1 : fn test_minimal_config_toml_is_valid() {
676 1 : // The minimal valid config for running a pageserver:
677 1 : // - control_plane_api is mandatory, as pageservers cannot run in isolation
678 1 : // - we use Default impl of everything else in this situation
679 1 : let input = r#"
680 1 : control_plane_api = "http://localhost:6666"
681 1 : "#;
682 1 : let config_toml = toml_edit::de::from_str::<pageserver_api::config::ConfigToml>(input)
683 1 : .expect("empty config is valid");
684 1 : let workdir = Utf8PathBuf::from("/nonexistent");
685 1 : PageServerConf::parse_and_validate(NodeId(0), config_toml, &workdir)
686 1 : .expect("parse_and_validate");
687 1 : }
688 :
689 : #[test]
690 1 : fn test_config_tracing_endpoint_is_invalid() {
691 1 : let input = r#"
692 1 : control_plane_api = "http://localhost:6666"
693 1 :
694 1 : [tracing]
695 1 :
696 1 : sampling_ratio = { numerator = 1, denominator = 0 }
697 1 :
698 1 : [tracing.export_config]
699 1 : endpoint = "localhost:4317"
700 1 : protocol = "http-binary"
701 1 : timeout = "1ms"
702 1 : "#;
703 1 : let config_toml = toml_edit::de::from_str::<pageserver_api::config::ConfigToml>(input)
704 1 : .expect("config has valid fields");
705 1 : let workdir = Utf8PathBuf::from("/nonexistent");
706 1 : PageServerConf::parse_and_validate(NodeId(0), config_toml, &workdir)
707 1 : .expect_err("parse_and_validate should fail for endpoint without scheme");
708 1 : }
709 : }
|