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