LCOV - code coverage report
Current view: top level - pageserver/src - config.rs (source / functions) Coverage Total Hit
Test: 42f947419473a288706e86ecdf7c2863d760d5d7.info Lines: 78.5 % 975 765
Test Date: 2024-08-02 21:34:27 Functions: 49.7 % 145 72

            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              : use anyhow::{anyhow, bail, ensure, Context, Result};
       8              : use pageserver_api::{models::ImageCompressionAlgorithm, shard::TenantShardId};
       9              : use remote_storage::{RemotePath, RemoteStorageConfig};
      10              : use serde::de::IntoDeserializer;
      11              : use serde::{self, Deserialize};
      12              : use std::env;
      13              : use storage_broker::Uri;
      14              : use utils::crashsafe::path_with_suffix_extension;
      15              : use utils::logging::SecretString;
      16              : 
      17              : use once_cell::sync::OnceCell;
      18              : use reqwest::Url;
      19              : use std::num::NonZeroUsize;
      20              : use std::str::FromStr;
      21              : use std::sync::Arc;
      22              : use std::time::Duration;
      23              : use toml_edit::{Document, Item};
      24              : 
      25              : use camino::{Utf8Path, Utf8PathBuf};
      26              : use postgres_backend::AuthType;
      27              : use utils::{
      28              :     id::{NodeId, TimelineId},
      29              :     logging::LogFormat,
      30              : };
      31              : 
      32              : use crate::tenant::timeline::compaction::CompactL0Phase1ValueAccess;
      33              : use crate::tenant::vectored_blob_io::MaxVectoredReadBytes;
      34              : use crate::tenant::{config::TenantConfOpt, timeline::GetImpl};
      35              : use crate::tenant::{TENANTS_SEGMENT_NAME, TIMELINES_SEGMENT_NAME};
      36              : use crate::{disk_usage_eviction_task::DiskUsageEvictionTaskConfig, virtual_file::io_engine};
      37              : use crate::{l0_flush::L0FlushConfig, tenant::timeline::GetVectoredImpl};
      38              : use crate::{tenant::config::TenantConf, virtual_file};
      39              : use crate::{TENANT_HEATMAP_BASENAME, TENANT_LOCATION_CONFIG_NAME, TIMELINE_DELETE_MARK_SUFFIX};
      40              : 
      41              : use self::defaults::DEFAULT_CONCURRENT_TENANT_WARMUP;
      42              : 
      43              : use self::defaults::DEFAULT_VIRTUAL_FILE_IO_ENGINE;
      44              : 
      45              : pub mod defaults {
      46              :     use crate::tenant::config::defaults::*;
      47              :     use const_format::formatcp;
      48              : 
      49              :     pub use pageserver_api::config::{
      50              :         DEFAULT_HTTP_LISTEN_ADDR, DEFAULT_HTTP_LISTEN_PORT, DEFAULT_PG_LISTEN_ADDR,
      51              :         DEFAULT_PG_LISTEN_PORT,
      52              :     };
      53              :     use pageserver_api::models::ImageCompressionAlgorithm;
      54              :     pub use storage_broker::DEFAULT_ENDPOINT as BROKER_DEFAULT_ENDPOINT;
      55              : 
      56              :     pub const DEFAULT_WAIT_LSN_TIMEOUT: &str = "300 s";
      57              :     pub const DEFAULT_WAL_REDO_TIMEOUT: &str = "60 s";
      58              : 
      59              :     pub const DEFAULT_SUPERUSER: &str = "cloud_admin";
      60              : 
      61              :     pub const DEFAULT_PAGE_CACHE_SIZE: usize = 8192;
      62              :     pub const DEFAULT_MAX_FILE_DESCRIPTORS: usize = 100;
      63              : 
      64              :     pub const DEFAULT_LOG_FORMAT: &str = "plain";
      65              : 
      66              :     pub const DEFAULT_CONCURRENT_TENANT_WARMUP: usize = 8;
      67              : 
      68              :     pub const DEFAULT_CONCURRENT_TENANT_SIZE_LOGICAL_SIZE_QUERIES: usize =
      69              :         super::ConfigurableSemaphore::DEFAULT_INITIAL.get();
      70              : 
      71              :     pub const DEFAULT_METRIC_COLLECTION_INTERVAL: &str = "10 min";
      72              :     pub const DEFAULT_METRIC_COLLECTION_ENDPOINT: Option<reqwest::Url> = None;
      73              :     pub const DEFAULT_SYNTHETIC_SIZE_CALCULATION_INTERVAL: &str = "10 min";
      74              :     pub const DEFAULT_BACKGROUND_TASK_MAXIMUM_DELAY: &str = "10s";
      75              : 
      76              :     pub const DEFAULT_HEATMAP_UPLOAD_CONCURRENCY: usize = 8;
      77              :     pub const DEFAULT_SECONDARY_DOWNLOAD_CONCURRENCY: usize = 1;
      78              : 
      79              :     pub const DEFAULT_INGEST_BATCH_SIZE: u64 = 100;
      80              : 
      81              :     #[cfg(target_os = "linux")]
      82              :     pub const DEFAULT_VIRTUAL_FILE_IO_ENGINE: &str = "tokio-epoll-uring";
      83              : 
      84              :     #[cfg(not(target_os = "linux"))]
      85              :     pub const DEFAULT_VIRTUAL_FILE_IO_ENGINE: &str = "std-fs";
      86              : 
      87              :     pub const DEFAULT_GET_VECTORED_IMPL: &str = "vectored";
      88              : 
      89              :     pub const DEFAULT_GET_IMPL: &str = "vectored";
      90              : 
      91              :     pub const DEFAULT_MAX_VECTORED_READ_BYTES: usize = 128 * 1024; // 128 KiB
      92              : 
      93              :     pub const DEFAULT_IMAGE_COMPRESSION: ImageCompressionAlgorithm =
      94              :         ImageCompressionAlgorithm::Disabled;
      95              : 
      96              :     pub const DEFAULT_VALIDATE_VECTORED_GET: bool = false;
      97              : 
      98              :     pub const DEFAULT_EPHEMERAL_BYTES_PER_MEMORY_KB: usize = 0;
      99              : 
     100              :     ///
     101              :     /// Default built-in configuration file.
     102              :     ///
     103              :     pub const DEFAULT_CONFIG_FILE: &str = formatcp!(
     104              :         r#"
     105              : # Initial configuration file created by 'pageserver --init'
     106              : #listen_pg_addr = '{DEFAULT_PG_LISTEN_ADDR}'
     107              : #listen_http_addr = '{DEFAULT_HTTP_LISTEN_ADDR}'
     108              : 
     109              : #wait_lsn_timeout = '{DEFAULT_WAIT_LSN_TIMEOUT}'
     110              : #wal_redo_timeout = '{DEFAULT_WAL_REDO_TIMEOUT}'
     111              : 
     112              : #page_cache_size = {DEFAULT_PAGE_CACHE_SIZE}
     113              : #max_file_descriptors = {DEFAULT_MAX_FILE_DESCRIPTORS}
     114              : 
     115              : # initial superuser role name to use when creating a new tenant
     116              : #initial_superuser_name = '{DEFAULT_SUPERUSER}'
     117              : 
     118              : #broker_endpoint = '{BROKER_DEFAULT_ENDPOINT}'
     119              : 
     120              : #log_format = '{DEFAULT_LOG_FORMAT}'
     121              : 
     122              : #concurrent_tenant_size_logical_size_queries = '{DEFAULT_CONCURRENT_TENANT_SIZE_LOGICAL_SIZE_QUERIES}'
     123              : #concurrent_tenant_warmup = '{DEFAULT_CONCURRENT_TENANT_WARMUP}'
     124              : 
     125              : #metric_collection_interval = '{DEFAULT_METRIC_COLLECTION_INTERVAL}'
     126              : #synthetic_size_calculation_interval = '{DEFAULT_SYNTHETIC_SIZE_CALCULATION_INTERVAL}'
     127              : 
     128              : #disk_usage_based_eviction = {{ max_usage_pct = .., min_avail_bytes = .., period = "10s"}}
     129              : 
     130              : #background_task_maximum_delay = '{DEFAULT_BACKGROUND_TASK_MAXIMUM_DELAY}'
     131              : 
     132              : #ingest_batch_size = {DEFAULT_INGEST_BATCH_SIZE}
     133              : 
     134              : #virtual_file_io_engine = '{DEFAULT_VIRTUAL_FILE_IO_ENGINE}'
     135              : 
     136              : #get_vectored_impl = '{DEFAULT_GET_VECTORED_IMPL}'
     137              : 
     138              : #get_impl = '{DEFAULT_GET_IMPL}'
     139              : 
     140              : #max_vectored_read_bytes = '{DEFAULT_MAX_VECTORED_READ_BYTES}'
     141              : 
     142              : #validate_vectored_get = '{DEFAULT_VALIDATE_VECTORED_GET}'
     143              : 
     144              : [tenant_config]
     145              : #checkpoint_distance = {DEFAULT_CHECKPOINT_DISTANCE} # in bytes
     146              : #checkpoint_timeout = {DEFAULT_CHECKPOINT_TIMEOUT}
     147              : #compaction_target_size = {DEFAULT_COMPACTION_TARGET_SIZE} # in bytes
     148              : #compaction_period = '{DEFAULT_COMPACTION_PERIOD}'
     149              : #compaction_threshold = {DEFAULT_COMPACTION_THRESHOLD}
     150              : 
     151              : #gc_period = '{DEFAULT_GC_PERIOD}'
     152              : #gc_horizon = {DEFAULT_GC_HORIZON}
     153              : #image_creation_threshold = {DEFAULT_IMAGE_CREATION_THRESHOLD}
     154              : #pitr_interval = '{DEFAULT_PITR_INTERVAL}'
     155              : 
     156              : #min_resident_size_override = .. # in bytes
     157              : #evictions_low_residence_duration_metric_threshold = '{DEFAULT_EVICTIONS_LOW_RESIDENCE_DURATION_METRIC_THRESHOLD}'
     158              : 
     159              : #heatmap_upload_concurrency = {DEFAULT_HEATMAP_UPLOAD_CONCURRENCY}
     160              : #secondary_download_concurrency = {DEFAULT_SECONDARY_DOWNLOAD_CONCURRENCY}
     161              : 
     162              : #ephemeral_bytes_per_memory_kb = {DEFAULT_EPHEMERAL_BYTES_PER_MEMORY_KB}
     163              : 
     164              : #[remote_storage]
     165              : 
     166              : "#
     167              :     );
     168              : }
     169              : 
     170              : #[derive(Debug, Clone, PartialEq, Eq)]
     171              : pub struct PageServerConf {
     172              :     // Identifier of that particular pageserver so e g safekeepers
     173              :     // can safely distinguish different pageservers
     174              :     pub id: NodeId,
     175              : 
     176              :     /// Example (default): 127.0.0.1:64000
     177              :     pub listen_pg_addr: String,
     178              :     /// Example (default): 127.0.0.1:9898
     179              :     pub listen_http_addr: String,
     180              : 
     181              :     /// Current availability zone. Used for traffic metrics.
     182              :     pub availability_zone: Option<String>,
     183              : 
     184              :     // Timeout when waiting for WAL receiver to catch up to an LSN given in a GetPage@LSN call.
     185              :     pub wait_lsn_timeout: Duration,
     186              :     // How long to wait for WAL redo to complete.
     187              :     pub wal_redo_timeout: Duration,
     188              : 
     189              :     pub superuser: String,
     190              : 
     191              :     pub page_cache_size: usize,
     192              :     pub max_file_descriptors: usize,
     193              : 
     194              :     // Repository directory, relative to current working directory.
     195              :     // Normally, the page server changes the current working directory
     196              :     // to the repository, and 'workdir' is always '.'. But we don't do
     197              :     // that during unit testing, because the current directory is global
     198              :     // to the process but different unit tests work on different
     199              :     // repositories.
     200              :     pub workdir: Utf8PathBuf,
     201              : 
     202              :     pub pg_distrib_dir: Utf8PathBuf,
     203              : 
     204              :     // Authentication
     205              :     /// authentication method for the HTTP mgmt API
     206              :     pub http_auth_type: AuthType,
     207              :     /// authentication method for libpq connections from compute
     208              :     pub pg_auth_type: AuthType,
     209              :     /// Path to a file or directory containing public key(s) for verifying JWT tokens.
     210              :     /// Used for both mgmt and compute auth, if enabled.
     211              :     pub auth_validation_public_key_path: Option<Utf8PathBuf>,
     212              : 
     213              :     pub remote_storage_config: Option<RemoteStorageConfig>,
     214              : 
     215              :     pub default_tenant_conf: TenantConf,
     216              : 
     217              :     /// Storage broker endpoints to connect to.
     218              :     pub broker_endpoint: Uri,
     219              :     pub broker_keepalive_interval: Duration,
     220              : 
     221              :     pub log_format: LogFormat,
     222              : 
     223              :     /// Number of tenants which will be concurrently loaded from remote storage proactively on startup or attach.
     224              :     ///
     225              :     /// A lower value implicitly deprioritizes loading such tenants, vs. other work in the system.
     226              :     pub concurrent_tenant_warmup: ConfigurableSemaphore,
     227              : 
     228              :     /// Number of concurrent [`Tenant::gather_size_inputs`](crate::tenant::Tenant::gather_size_inputs) allowed.
     229              :     pub concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore,
     230              :     /// Limit of concurrent [`Tenant::gather_size_inputs`] issued by module `eviction_task`.
     231              :     /// The number of permits is the same as `concurrent_tenant_size_logical_size_queries`.
     232              :     /// See the comment in `eviction_task` for details.
     233              :     ///
     234              :     /// [`Tenant::gather_size_inputs`]: crate::tenant::Tenant::gather_size_inputs
     235              :     pub eviction_task_immitated_concurrent_logical_size_queries: ConfigurableSemaphore,
     236              : 
     237              :     // How often to collect metrics and send them to the metrics endpoint.
     238              :     pub metric_collection_interval: Duration,
     239              :     // How often to send unchanged cached metrics to the metrics endpoint.
     240              :     pub metric_collection_endpoint: Option<Url>,
     241              :     pub metric_collection_bucket: Option<RemoteStorageConfig>,
     242              :     pub synthetic_size_calculation_interval: Duration,
     243              : 
     244              :     pub disk_usage_based_eviction: Option<DiskUsageEvictionTaskConfig>,
     245              : 
     246              :     pub test_remote_failures: u64,
     247              : 
     248              :     pub ondemand_download_behavior_treat_error_as_warn: bool,
     249              : 
     250              :     /// How long will background tasks be delayed at most after initial load of tenants.
     251              :     ///
     252              :     /// Our largest initialization completions are in the range of 100-200s, so perhaps 10s works
     253              :     /// as we now isolate initial loading, initial logical size calculation and background tasks.
     254              :     /// Smaller nodes will have background tasks "not running" for this long unless every timeline
     255              :     /// has it's initial logical size calculated. Not running background tasks for some seconds is
     256              :     /// not terrible.
     257              :     pub background_task_maximum_delay: Duration,
     258              : 
     259              :     pub control_plane_api: Option<Url>,
     260              : 
     261              :     /// JWT token for use with the control plane API.
     262              :     pub control_plane_api_token: Option<SecretString>,
     263              : 
     264              :     /// If true, pageserver will make best-effort to operate without a control plane: only
     265              :     /// for use in major incidents.
     266              :     pub control_plane_emergency_mode: bool,
     267              : 
     268              :     /// How many heatmap uploads may be done concurrency: lower values implicitly deprioritize
     269              :     /// heatmap uploads vs. other remote storage operations.
     270              :     pub heatmap_upload_concurrency: usize,
     271              : 
     272              :     /// How many remote storage downloads may be done for secondary tenants concurrently.  Implicitly
     273              :     /// deprioritises secondary downloads vs. remote storage operations for attached tenants.
     274              :     pub secondary_download_concurrency: usize,
     275              : 
     276              :     /// Maximum number of WAL records to be ingested and committed at the same time
     277              :     pub ingest_batch_size: u64,
     278              : 
     279              :     pub virtual_file_io_engine: virtual_file::IoEngineKind,
     280              : 
     281              :     pub get_vectored_impl: GetVectoredImpl,
     282              : 
     283              :     pub get_impl: GetImpl,
     284              : 
     285              :     pub max_vectored_read_bytes: MaxVectoredReadBytes,
     286              : 
     287              :     pub validate_vectored_get: bool,
     288              : 
     289              :     pub image_compression: ImageCompressionAlgorithm,
     290              : 
     291              :     /// How many bytes of ephemeral layer content will we allow per kilobyte of RAM.  When this
     292              :     /// is exceeded, we start proactively closing ephemeral layers to limit the total amount
     293              :     /// of ephemeral data.
     294              :     ///
     295              :     /// Setting this to zero disables limits on total ephemeral layer size.
     296              :     pub ephemeral_bytes_per_memory_kb: usize,
     297              : 
     298              :     pub l0_flush: L0FlushConfig,
     299              : 
     300              :     /// This flag is temporary and will be removed after gradual rollout.
     301              :     /// See <https://github.com/neondatabase/neon/issues/8184>.
     302              :     pub compact_level0_phase1_value_access: CompactL0Phase1ValueAccess,
     303              : }
     304              : 
     305              : /// We do not want to store this in a PageServerConf because the latter may be logged
     306              : /// and/or serialized at a whim, while the token is secret. Currently this token is the
     307              : /// same for accessing all tenants/timelines, but may become per-tenant/per-timeline in
     308              : /// the future, more tokens and auth may arrive for storage broker, completely changing the logic.
     309              : /// Hence, we resort to a global variable for now instead of passing the token from the
     310              : /// startup code to the connection code through a dozen layers.
     311              : pub static SAFEKEEPER_AUTH_TOKEN: OnceCell<Arc<String>> = OnceCell::new();
     312              : 
     313              : // use dedicated enum for builder to better indicate the intention
     314              : // and avoid possible confusion with nested options
     315              : #[derive(Clone, Default)]
     316              : pub enum BuilderValue<T> {
     317              :     Set(T),
     318              :     #[default]
     319              :     NotSet,
     320              : }
     321              : 
     322              : impl<T: Clone> BuilderValue<T> {
     323          672 :     pub fn ok_or(&self, field_name: &'static str, default: BuilderValue<T>) -> anyhow::Result<T> {
     324          672 :         match self {
     325          182 :             Self::Set(v) => Ok(v.clone()),
     326          490 :             Self::NotSet => match default {
     327          490 :                 BuilderValue::Set(v) => Ok(v.clone()),
     328              :                 BuilderValue::NotSet => {
     329            0 :                     anyhow::bail!("missing config value {field_name:?}")
     330              :                 }
     331              :             },
     332              :         }
     333          672 :     }
     334              : }
     335              : 
     336              : // needed to simplify config construction
     337              : #[derive(Default)]
     338              : struct PageServerConfigBuilder {
     339              :     listen_pg_addr: BuilderValue<String>,
     340              : 
     341              :     listen_http_addr: BuilderValue<String>,
     342              : 
     343              :     availability_zone: BuilderValue<Option<String>>,
     344              : 
     345              :     wait_lsn_timeout: BuilderValue<Duration>,
     346              :     wal_redo_timeout: BuilderValue<Duration>,
     347              : 
     348              :     superuser: BuilderValue<String>,
     349              : 
     350              :     page_cache_size: BuilderValue<usize>,
     351              :     max_file_descriptors: BuilderValue<usize>,
     352              : 
     353              :     workdir: BuilderValue<Utf8PathBuf>,
     354              : 
     355              :     pg_distrib_dir: BuilderValue<Utf8PathBuf>,
     356              : 
     357              :     http_auth_type: BuilderValue<AuthType>,
     358              :     pg_auth_type: BuilderValue<AuthType>,
     359              : 
     360              :     //
     361              :     auth_validation_public_key_path: BuilderValue<Option<Utf8PathBuf>>,
     362              :     remote_storage_config: BuilderValue<Option<RemoteStorageConfig>>,
     363              : 
     364              :     broker_endpoint: BuilderValue<Uri>,
     365              :     broker_keepalive_interval: BuilderValue<Duration>,
     366              : 
     367              :     log_format: BuilderValue<LogFormat>,
     368              : 
     369              :     concurrent_tenant_warmup: BuilderValue<NonZeroUsize>,
     370              :     concurrent_tenant_size_logical_size_queries: BuilderValue<NonZeroUsize>,
     371              : 
     372              :     metric_collection_interval: BuilderValue<Duration>,
     373              :     metric_collection_endpoint: BuilderValue<Option<Url>>,
     374              :     synthetic_size_calculation_interval: BuilderValue<Duration>,
     375              :     metric_collection_bucket: BuilderValue<Option<RemoteStorageConfig>>,
     376              : 
     377              :     disk_usage_based_eviction: BuilderValue<Option<DiskUsageEvictionTaskConfig>>,
     378              : 
     379              :     test_remote_failures: BuilderValue<u64>,
     380              : 
     381              :     ondemand_download_behavior_treat_error_as_warn: BuilderValue<bool>,
     382              : 
     383              :     background_task_maximum_delay: BuilderValue<Duration>,
     384              : 
     385              :     control_plane_api: BuilderValue<Option<Url>>,
     386              :     control_plane_api_token: BuilderValue<Option<SecretString>>,
     387              :     control_plane_emergency_mode: BuilderValue<bool>,
     388              : 
     389              :     heatmap_upload_concurrency: BuilderValue<usize>,
     390              :     secondary_download_concurrency: BuilderValue<usize>,
     391              : 
     392              :     ingest_batch_size: BuilderValue<u64>,
     393              : 
     394              :     virtual_file_io_engine: BuilderValue<virtual_file::IoEngineKind>,
     395              : 
     396              :     get_vectored_impl: BuilderValue<GetVectoredImpl>,
     397              : 
     398              :     get_impl: BuilderValue<GetImpl>,
     399              : 
     400              :     max_vectored_read_bytes: BuilderValue<MaxVectoredReadBytes>,
     401              : 
     402              :     validate_vectored_get: BuilderValue<bool>,
     403              : 
     404              :     image_compression: BuilderValue<ImageCompressionAlgorithm>,
     405              : 
     406              :     ephemeral_bytes_per_memory_kb: BuilderValue<usize>,
     407              : 
     408              :     l0_flush: BuilderValue<L0FlushConfig>,
     409              : 
     410              :     compact_level0_phase1_value_access: BuilderValue<CompactL0Phase1ValueAccess>,
     411              : }
     412              : 
     413              : impl PageServerConfigBuilder {
     414           18 :     fn new() -> Self {
     415           18 :         Self::default()
     416           18 :     }
     417              : 
     418              :     #[inline(always)]
     419           16 :     fn default_values() -> Self {
     420           16 :         use self::BuilderValue::*;
     421           16 :         use defaults::*;
     422           16 :         Self {
     423           16 :             listen_pg_addr: Set(DEFAULT_PG_LISTEN_ADDR.to_string()),
     424           16 :             listen_http_addr: Set(DEFAULT_HTTP_LISTEN_ADDR.to_string()),
     425           16 :             availability_zone: Set(None),
     426           16 :             wait_lsn_timeout: Set(humantime::parse_duration(DEFAULT_WAIT_LSN_TIMEOUT)
     427           16 :                 .expect("cannot parse default wait lsn timeout")),
     428           16 :             wal_redo_timeout: Set(humantime::parse_duration(DEFAULT_WAL_REDO_TIMEOUT)
     429           16 :                 .expect("cannot parse default wal redo timeout")),
     430           16 :             superuser: Set(DEFAULT_SUPERUSER.to_string()),
     431           16 :             page_cache_size: Set(DEFAULT_PAGE_CACHE_SIZE),
     432           16 :             max_file_descriptors: Set(DEFAULT_MAX_FILE_DESCRIPTORS),
     433           16 :             workdir: Set(Utf8PathBuf::new()),
     434           16 :             pg_distrib_dir: Set(Utf8PathBuf::from_path_buf(
     435           16 :                 env::current_dir().expect("cannot access current directory"),
     436           16 :             )
     437           16 :             .expect("non-Unicode path")
     438           16 :             .join("pg_install")),
     439           16 :             http_auth_type: Set(AuthType::Trust),
     440           16 :             pg_auth_type: Set(AuthType::Trust),
     441           16 :             auth_validation_public_key_path: Set(None),
     442           16 :             remote_storage_config: Set(None),
     443           16 :             broker_endpoint: Set(storage_broker::DEFAULT_ENDPOINT
     444           16 :                 .parse()
     445           16 :                 .expect("failed to parse default broker endpoint")),
     446           16 :             broker_keepalive_interval: Set(humantime::parse_duration(
     447           16 :                 storage_broker::DEFAULT_KEEPALIVE_INTERVAL,
     448           16 :             )
     449           16 :             .expect("cannot parse default keepalive interval")),
     450           16 :             log_format: Set(LogFormat::from_str(DEFAULT_LOG_FORMAT).unwrap()),
     451           16 : 
     452           16 :             concurrent_tenant_warmup: Set(NonZeroUsize::new(DEFAULT_CONCURRENT_TENANT_WARMUP)
     453           16 :                 .expect("Invalid default constant")),
     454           16 :             concurrent_tenant_size_logical_size_queries: Set(
     455           16 :                 ConfigurableSemaphore::DEFAULT_INITIAL,
     456           16 :             ),
     457           16 :             metric_collection_interval: Set(humantime::parse_duration(
     458           16 :                 DEFAULT_METRIC_COLLECTION_INTERVAL,
     459           16 :             )
     460           16 :             .expect("cannot parse default metric collection interval")),
     461           16 :             synthetic_size_calculation_interval: Set(humantime::parse_duration(
     462           16 :                 DEFAULT_SYNTHETIC_SIZE_CALCULATION_INTERVAL,
     463           16 :             )
     464           16 :             .expect("cannot parse default synthetic size calculation interval")),
     465           16 :             metric_collection_endpoint: Set(DEFAULT_METRIC_COLLECTION_ENDPOINT),
     466           16 : 
     467           16 :             metric_collection_bucket: Set(None),
     468           16 : 
     469           16 :             disk_usage_based_eviction: Set(None),
     470           16 : 
     471           16 :             test_remote_failures: Set(0),
     472           16 : 
     473           16 :             ondemand_download_behavior_treat_error_as_warn: Set(false),
     474           16 : 
     475           16 :             background_task_maximum_delay: Set(humantime::parse_duration(
     476           16 :                 DEFAULT_BACKGROUND_TASK_MAXIMUM_DELAY,
     477           16 :             )
     478           16 :             .unwrap()),
     479           16 : 
     480           16 :             control_plane_api: Set(None),
     481           16 :             control_plane_api_token: Set(None),
     482           16 :             control_plane_emergency_mode: Set(false),
     483           16 : 
     484           16 :             heatmap_upload_concurrency: Set(DEFAULT_HEATMAP_UPLOAD_CONCURRENCY),
     485           16 :             secondary_download_concurrency: Set(DEFAULT_SECONDARY_DOWNLOAD_CONCURRENCY),
     486           16 : 
     487           16 :             ingest_batch_size: Set(DEFAULT_INGEST_BATCH_SIZE),
     488           16 : 
     489           16 :             virtual_file_io_engine: Set(DEFAULT_VIRTUAL_FILE_IO_ENGINE.parse().unwrap()),
     490           16 : 
     491           16 :             get_vectored_impl: Set(DEFAULT_GET_VECTORED_IMPL.parse().unwrap()),
     492           16 :             get_impl: Set(DEFAULT_GET_IMPL.parse().unwrap()),
     493           16 :             max_vectored_read_bytes: Set(MaxVectoredReadBytes(
     494           16 :                 NonZeroUsize::new(DEFAULT_MAX_VECTORED_READ_BYTES).unwrap(),
     495           16 :             )),
     496           16 :             image_compression: Set(DEFAULT_IMAGE_COMPRESSION),
     497           16 :             validate_vectored_get: Set(DEFAULT_VALIDATE_VECTORED_GET),
     498           16 :             ephemeral_bytes_per_memory_kb: Set(DEFAULT_EPHEMERAL_BYTES_PER_MEMORY_KB),
     499           16 :             l0_flush: Set(L0FlushConfig::default()),
     500           16 :             compact_level0_phase1_value_access: Set(CompactL0Phase1ValueAccess::default()),
     501           16 :         }
     502           16 :     }
     503              : }
     504              : 
     505              : impl PageServerConfigBuilder {
     506           10 :     pub fn listen_pg_addr(&mut self, listen_pg_addr: String) {
     507           10 :         self.listen_pg_addr = BuilderValue::Set(listen_pg_addr)
     508           10 :     }
     509              : 
     510           10 :     pub fn listen_http_addr(&mut self, listen_http_addr: String) {
     511           10 :         self.listen_http_addr = BuilderValue::Set(listen_http_addr)
     512           10 :     }
     513              : 
     514            0 :     pub fn availability_zone(&mut self, availability_zone: Option<String>) {
     515            0 :         self.availability_zone = BuilderValue::Set(availability_zone)
     516            0 :     }
     517              : 
     518           10 :     pub fn wait_lsn_timeout(&mut self, wait_lsn_timeout: Duration) {
     519           10 :         self.wait_lsn_timeout = BuilderValue::Set(wait_lsn_timeout)
     520           10 :     }
     521              : 
     522           10 :     pub fn wal_redo_timeout(&mut self, wal_redo_timeout: Duration) {
     523           10 :         self.wal_redo_timeout = BuilderValue::Set(wal_redo_timeout)
     524           10 :     }
     525              : 
     526           10 :     pub fn superuser(&mut self, superuser: String) {
     527           10 :         self.superuser = BuilderValue::Set(superuser)
     528           10 :     }
     529              : 
     530           10 :     pub fn page_cache_size(&mut self, page_cache_size: usize) {
     531           10 :         self.page_cache_size = BuilderValue::Set(page_cache_size)
     532           10 :     }
     533              : 
     534           10 :     pub fn max_file_descriptors(&mut self, max_file_descriptors: usize) {
     535           10 :         self.max_file_descriptors = BuilderValue::Set(max_file_descriptors)
     536           10 :     }
     537              : 
     538           18 :     pub fn workdir(&mut self, workdir: Utf8PathBuf) {
     539           18 :         self.workdir = BuilderValue::Set(workdir)
     540           18 :     }
     541              : 
     542           16 :     pub fn pg_distrib_dir(&mut self, pg_distrib_dir: Utf8PathBuf) {
     543           16 :         self.pg_distrib_dir = BuilderValue::Set(pg_distrib_dir)
     544           16 :     }
     545              : 
     546            0 :     pub fn http_auth_type(&mut self, auth_type: AuthType) {
     547            0 :         self.http_auth_type = BuilderValue::Set(auth_type)
     548            0 :     }
     549              : 
     550            0 :     pub fn pg_auth_type(&mut self, auth_type: AuthType) {
     551            0 :         self.pg_auth_type = BuilderValue::Set(auth_type)
     552            0 :     }
     553              : 
     554            0 :     pub fn auth_validation_public_key_path(
     555            0 :         &mut self,
     556            0 :         auth_validation_public_key_path: Option<Utf8PathBuf>,
     557            0 :     ) {
     558            0 :         self.auth_validation_public_key_path = BuilderValue::Set(auth_validation_public_key_path)
     559            0 :     }
     560              : 
     561            8 :     pub fn remote_storage_config(&mut self, remote_storage_config: Option<RemoteStorageConfig>) {
     562            8 :         self.remote_storage_config = BuilderValue::Set(remote_storage_config)
     563            8 :     }
     564              : 
     565           12 :     pub fn broker_endpoint(&mut self, broker_endpoint: Uri) {
     566           12 :         self.broker_endpoint = BuilderValue::Set(broker_endpoint)
     567           12 :     }
     568              : 
     569            0 :     pub fn broker_keepalive_interval(&mut self, broker_keepalive_interval: Duration) {
     570            0 :         self.broker_keepalive_interval = BuilderValue::Set(broker_keepalive_interval)
     571            0 :     }
     572              : 
     573           10 :     pub fn log_format(&mut self, log_format: LogFormat) {
     574           10 :         self.log_format = BuilderValue::Set(log_format)
     575           10 :     }
     576              : 
     577            0 :     pub fn concurrent_tenant_warmup(&mut self, u: NonZeroUsize) {
     578            0 :         self.concurrent_tenant_warmup = BuilderValue::Set(u);
     579            0 :     }
     580              : 
     581            0 :     pub fn concurrent_tenant_size_logical_size_queries(&mut self, u: NonZeroUsize) {
     582            0 :         self.concurrent_tenant_size_logical_size_queries = BuilderValue::Set(u);
     583            0 :     }
     584              : 
     585           14 :     pub fn metric_collection_interval(&mut self, metric_collection_interval: Duration) {
     586           14 :         self.metric_collection_interval = BuilderValue::Set(metric_collection_interval)
     587           14 :     }
     588              : 
     589           14 :     pub fn metric_collection_endpoint(&mut self, metric_collection_endpoint: Option<Url>) {
     590           14 :         self.metric_collection_endpoint = BuilderValue::Set(metric_collection_endpoint)
     591           14 :     }
     592              : 
     593            0 :     pub fn metric_collection_bucket(
     594            0 :         &mut self,
     595            0 :         metric_collection_bucket: Option<RemoteStorageConfig>,
     596            0 :     ) {
     597            0 :         self.metric_collection_bucket = BuilderValue::Set(metric_collection_bucket)
     598            0 :     }
     599              : 
     600           10 :     pub fn synthetic_size_calculation_interval(
     601           10 :         &mut self,
     602           10 :         synthetic_size_calculation_interval: Duration,
     603           10 :     ) {
     604           10 :         self.synthetic_size_calculation_interval =
     605           10 :             BuilderValue::Set(synthetic_size_calculation_interval)
     606           10 :     }
     607              : 
     608            0 :     pub fn test_remote_failures(&mut self, fail_first: u64) {
     609            0 :         self.test_remote_failures = BuilderValue::Set(fail_first);
     610            0 :     }
     611              : 
     612            2 :     pub fn disk_usage_based_eviction(&mut self, value: Option<DiskUsageEvictionTaskConfig>) {
     613            2 :         self.disk_usage_based_eviction = BuilderValue::Set(value);
     614            2 :     }
     615              : 
     616            0 :     pub fn ondemand_download_behavior_treat_error_as_warn(
     617            0 :         &mut self,
     618            0 :         ondemand_download_behavior_treat_error_as_warn: bool,
     619            0 :     ) {
     620            0 :         self.ondemand_download_behavior_treat_error_as_warn =
     621            0 :             BuilderValue::Set(ondemand_download_behavior_treat_error_as_warn);
     622            0 :     }
     623              : 
     624           10 :     pub fn background_task_maximum_delay(&mut self, delay: Duration) {
     625           10 :         self.background_task_maximum_delay = BuilderValue::Set(delay);
     626           10 :     }
     627              : 
     628            0 :     pub fn control_plane_api(&mut self, api: Option<Url>) {
     629            0 :         self.control_plane_api = BuilderValue::Set(api)
     630            0 :     }
     631              : 
     632            0 :     pub fn control_plane_api_token(&mut self, token: Option<SecretString>) {
     633            0 :         self.control_plane_api_token = BuilderValue::Set(token)
     634            0 :     }
     635              : 
     636            0 :     pub fn control_plane_emergency_mode(&mut self, enabled: bool) {
     637            0 :         self.control_plane_emergency_mode = BuilderValue::Set(enabled)
     638            0 :     }
     639              : 
     640            0 :     pub fn heatmap_upload_concurrency(&mut self, value: usize) {
     641            0 :         self.heatmap_upload_concurrency = BuilderValue::Set(value)
     642            0 :     }
     643              : 
     644            0 :     pub fn secondary_download_concurrency(&mut self, value: usize) {
     645            0 :         self.secondary_download_concurrency = BuilderValue::Set(value)
     646            0 :     }
     647              : 
     648            0 :     pub fn ingest_batch_size(&mut self, ingest_batch_size: u64) {
     649            0 :         self.ingest_batch_size = BuilderValue::Set(ingest_batch_size)
     650            0 :     }
     651              : 
     652            0 :     pub fn virtual_file_io_engine(&mut self, value: virtual_file::IoEngineKind) {
     653            0 :         self.virtual_file_io_engine = BuilderValue::Set(value);
     654            0 :     }
     655              : 
     656            0 :     pub fn get_vectored_impl(&mut self, value: GetVectoredImpl) {
     657            0 :         self.get_vectored_impl = BuilderValue::Set(value);
     658            0 :     }
     659              : 
     660            0 :     pub fn get_impl(&mut self, value: GetImpl) {
     661            0 :         self.get_impl = BuilderValue::Set(value);
     662            0 :     }
     663              : 
     664            0 :     pub fn get_max_vectored_read_bytes(&mut self, value: MaxVectoredReadBytes) {
     665            0 :         self.max_vectored_read_bytes = BuilderValue::Set(value);
     666            0 :     }
     667              : 
     668            0 :     pub fn get_validate_vectored_get(&mut self, value: bool) {
     669            0 :         self.validate_vectored_get = BuilderValue::Set(value);
     670            0 :     }
     671              : 
     672            0 :     pub fn get_image_compression(&mut self, value: ImageCompressionAlgorithm) {
     673            0 :         self.image_compression = BuilderValue::Set(value);
     674            0 :     }
     675              : 
     676            0 :     pub fn get_ephemeral_bytes_per_memory_kb(&mut self, value: usize) {
     677            0 :         self.ephemeral_bytes_per_memory_kb = BuilderValue::Set(value);
     678            0 :     }
     679              : 
     680            0 :     pub fn l0_flush(&mut self, value: L0FlushConfig) {
     681            0 :         self.l0_flush = BuilderValue::Set(value);
     682            0 :     }
     683              : 
     684            0 :     pub fn compact_level0_phase1_value_access(&mut self, value: CompactL0Phase1ValueAccess) {
     685            0 :         self.compact_level0_phase1_value_access = BuilderValue::Set(value);
     686            0 :     }
     687              : 
     688           16 :     pub fn build(self, id: NodeId) -> anyhow::Result<PageServerConf> {
     689           16 :         let default = Self::default_values();
     690           16 : 
     691           16 :         macro_rules! conf {
     692           16 :             (USING DEFAULT { $($field:ident,)* } CUSTOM LOGIC { $($custom_field:ident : $custom_value:expr,)* } ) => {
     693           16 :                 PageServerConf {
     694           16 :                     $(
     695           16 :                         $field: self.$field.ok_or(stringify!($field), default.$field)?,
     696           16 :                     )*
     697           16 :                     $(
     698           16 :                         $custom_field: $custom_value,
     699           16 :                     )*
     700           16 :                 }
     701           16 :             };
     702           16 :         }
     703           16 : 
     704           16 :         Ok(conf!(
     705              :             USING DEFAULT
     706              :             {
     707              :                 listen_pg_addr,
     708              :                 listen_http_addr,
     709              :                 availability_zone,
     710              :                 wait_lsn_timeout,
     711              :                 wal_redo_timeout,
     712              :                 superuser,
     713              :                 page_cache_size,
     714              :                 max_file_descriptors,
     715              :                 workdir,
     716              :                 pg_distrib_dir,
     717              :                 http_auth_type,
     718              :                 pg_auth_type,
     719              :                 auth_validation_public_key_path,
     720              :                 remote_storage_config,
     721              :                 broker_endpoint,
     722              :                 broker_keepalive_interval,
     723              :                 log_format,
     724              :                 metric_collection_interval,
     725              :                 metric_collection_endpoint,
     726              :                 metric_collection_bucket,
     727              :                 synthetic_size_calculation_interval,
     728              :                 disk_usage_based_eviction,
     729              :                 test_remote_failures,
     730              :                 ondemand_download_behavior_treat_error_as_warn,
     731              :                 background_task_maximum_delay,
     732              :                 control_plane_api,
     733              :                 control_plane_api_token,
     734              :                 control_plane_emergency_mode,
     735              :                 heatmap_upload_concurrency,
     736              :                 secondary_download_concurrency,
     737              :                 ingest_batch_size,
     738              :                 get_vectored_impl,
     739              :                 get_impl,
     740              :                 max_vectored_read_bytes,
     741              :                 validate_vectored_get,
     742              :                 image_compression,
     743              :                 ephemeral_bytes_per_memory_kb,
     744              :                 l0_flush,
     745              :                 compact_level0_phase1_value_access,
     746              :             }
     747              :             CUSTOM LOGIC
     748              :             {
     749           16 :                 id: id,
     750           16 :                 // TenantConf is handled separately
     751           16 :                 default_tenant_conf: TenantConf::default(),
     752           16 :                 concurrent_tenant_warmup: ConfigurableSemaphore::new({
     753           16 :                     self
     754           16 :                         .concurrent_tenant_warmup
     755           16 :                         .ok_or("concurrent_tenant_warmpup",
     756           16 :                                default.concurrent_tenant_warmup)?
     757              :                 }),
     758              :                 concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore::new(
     759           16 :                     self
     760           16 :                         .concurrent_tenant_size_logical_size_queries
     761           16 :                         .ok_or("concurrent_tenant_size_logical_size_queries",
     762           16 :                                default.concurrent_tenant_size_logical_size_queries.clone())?
     763              :                 ),
     764              :                 eviction_task_immitated_concurrent_logical_size_queries: ConfigurableSemaphore::new(
     765              :                     // re-use `concurrent_tenant_size_logical_size_queries`
     766           16 :                     self
     767           16 :                         .concurrent_tenant_size_logical_size_queries
     768           16 :                         .ok_or("eviction_task_immitated_concurrent_logical_size_queries",
     769           16 :                                default.concurrent_tenant_size_logical_size_queries.clone())?,
     770              :                 ),
     771           16 :                 virtual_file_io_engine: match self.virtual_file_io_engine {
     772            0 :                     BuilderValue::Set(v) => v,
     773           16 :                     BuilderValue::NotSet => match crate::virtual_file::io_engine_feature_test().context("auto-detect virtual_file_io_engine")? {
     774           16 :                         io_engine::FeatureTestResult::PlatformPreferred(v) => v, // make no noise
     775            0 :                         io_engine::FeatureTestResult::Worse { engine, remark } => {
     776            0 :                             // TODO: bubble this up to the caller so we can tracing::warn! it.
     777            0 :                             eprintln!("auto-detected IO engine is not platform-preferred: engine={engine:?} remark={remark:?}");
     778            0 :                             engine
     779              :                         }
     780              :                     },
     781              :                 },
     782              :             }
     783              :         ))
     784           16 :     }
     785              : }
     786              : 
     787              : impl PageServerConf {
     788              :     //
     789              :     // Repository paths, relative to workdir.
     790              :     //
     791              : 
     792         6502 :     pub fn tenants_path(&self) -> Utf8PathBuf {
     793         6502 :         self.workdir.join(TENANTS_SEGMENT_NAME)
     794         6502 :     }
     795              : 
     796           72 :     pub fn deletion_prefix(&self) -> Utf8PathBuf {
     797           72 :         self.workdir.join("deletion")
     798           72 :     }
     799              : 
     800            0 :     pub fn metadata_path(&self) -> Utf8PathBuf {
     801            0 :         self.workdir.join("metadata.json")
     802            0 :     }
     803              : 
     804           28 :     pub fn deletion_list_path(&self, sequence: u64) -> Utf8PathBuf {
     805           28 :         // Encode a version in the filename, so that if we ever switch away from JSON we can
     806           28 :         // increment this.
     807           28 :         const VERSION: u8 = 1;
     808           28 : 
     809           28 :         self.deletion_prefix()
     810           28 :             .join(format!("{sequence:016x}-{VERSION:02x}.list"))
     811           28 :     }
     812              : 
     813           24 :     pub fn deletion_header_path(&self) -> Utf8PathBuf {
     814           24 :         // Encode a version in the filename, so that if we ever switch away from JSON we can
     815           24 :         // increment this.
     816           24 :         const VERSION: u8 = 1;
     817           24 : 
     818           24 :         self.deletion_prefix().join(format!("header-{VERSION:02x}"))
     819           24 :     }
     820              : 
     821         6502 :     pub fn tenant_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf {
     822         6502 :         self.tenants_path().join(tenant_shard_id.to_string())
     823         6502 :     }
     824              : 
     825              :     /// Points to a place in pageserver's local directory,
     826              :     /// where certain tenant's LocationConf be stored.
     827            0 :     pub(crate) fn tenant_location_config_path(
     828            0 :         &self,
     829            0 :         tenant_shard_id: &TenantShardId,
     830            0 :     ) -> Utf8PathBuf {
     831            0 :         self.tenant_path(tenant_shard_id)
     832            0 :             .join(TENANT_LOCATION_CONFIG_NAME)
     833            0 :     }
     834              : 
     835            0 :     pub(crate) fn tenant_heatmap_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf {
     836            0 :         self.tenant_path(tenant_shard_id)
     837            0 :             .join(TENANT_HEATMAP_BASENAME)
     838            0 :     }
     839              : 
     840         6324 :     pub fn timelines_path(&self, tenant_shard_id: &TenantShardId) -> Utf8PathBuf {
     841         6324 :         self.tenant_path(tenant_shard_id)
     842         6324 :             .join(TIMELINES_SEGMENT_NAME)
     843         6324 :     }
     844              : 
     845         5972 :     pub fn timeline_path(
     846         5972 :         &self,
     847         5972 :         tenant_shard_id: &TenantShardId,
     848         5972 :         timeline_id: &TimelineId,
     849         5972 :     ) -> Utf8PathBuf {
     850         5972 :         self.timelines_path(tenant_shard_id)
     851         5972 :             .join(timeline_id.to_string())
     852         5972 :     }
     853              : 
     854            0 :     pub(crate) fn timeline_delete_mark_file_path(
     855            0 :         &self,
     856            0 :         tenant_shard_id: TenantShardId,
     857            0 :         timeline_id: TimelineId,
     858            0 :     ) -> Utf8PathBuf {
     859            0 :         path_with_suffix_extension(
     860            0 :             self.timeline_path(&tenant_shard_id, &timeline_id),
     861            0 :             TIMELINE_DELETE_MARK_SUFFIX,
     862            0 :         )
     863            0 :     }
     864              : 
     865              :     /// Turns storage remote path of a file into its local path.
     866            0 :     pub fn local_path(&self, remote_path: &RemotePath) -> Utf8PathBuf {
     867            0 :         remote_path.with_base(&self.workdir)
     868            0 :     }
     869              : 
     870              :     //
     871              :     // Postgres distribution paths
     872              :     //
     873           16 :     pub fn pg_distrib_dir(&self, pg_version: u32) -> anyhow::Result<Utf8PathBuf> {
     874           16 :         let path = self.pg_distrib_dir.clone();
     875           16 : 
     876           16 :         #[allow(clippy::manual_range_patterns)]
     877           16 :         match pg_version {
     878           16 :             14 | 15 | 16 => Ok(path.join(format!("v{pg_version}"))),
     879            0 :             _ => bail!("Unsupported postgres version: {}", pg_version),
     880              :         }
     881           16 :     }
     882              : 
     883            8 :     pub fn pg_bin_dir(&self, pg_version: u32) -> anyhow::Result<Utf8PathBuf> {
     884            8 :         Ok(self.pg_distrib_dir(pg_version)?.join("bin"))
     885            8 :     }
     886            8 :     pub fn pg_lib_dir(&self, pg_version: u32) -> anyhow::Result<Utf8PathBuf> {
     887            8 :         Ok(self.pg_distrib_dir(pg_version)?.join("lib"))
     888            8 :     }
     889              : 
     890              :     /// Parse a configuration file (pageserver.toml) into a PageServerConf struct,
     891              :     /// validating the input and failing on errors.
     892              :     ///
     893              :     /// This leaves any options not present in the file in the built-in defaults.
     894           18 :     pub fn parse_and_validate(
     895           18 :         node_id: NodeId,
     896           18 :         toml: &Document,
     897           18 :         workdir: &Utf8Path,
     898           18 :     ) -> anyhow::Result<Self> {
     899           18 :         let mut builder = PageServerConfigBuilder::new();
     900           18 :         builder.workdir(workdir.to_owned());
     901           18 : 
     902           18 :         let mut t_conf = TenantConfOpt::default();
     903              : 
     904          172 :         for (key, item) in toml.iter() {
     905          172 :             match key {
     906          172 :                 "listen_pg_addr" => builder.listen_pg_addr(parse_toml_string(key, item)?),
     907          162 :                 "listen_http_addr" => builder.listen_http_addr(parse_toml_string(key, item)?),
     908          152 :                 "availability_zone" => builder.availability_zone(Some(parse_toml_string(key, item)?)),
     909          152 :                 "wait_lsn_timeout" => builder.wait_lsn_timeout(parse_toml_duration(key, item)?),
     910          142 :                 "wal_redo_timeout" => builder.wal_redo_timeout(parse_toml_duration(key, item)?),
     911          132 :                 "initial_superuser_name" => builder.superuser(parse_toml_string(key, item)?),
     912          122 :                 "page_cache_size" => builder.page_cache_size(parse_toml_u64(key, item)? as usize),
     913          112 :                 "max_file_descriptors" => {
     914           10 :                     builder.max_file_descriptors(parse_toml_u64(key, item)? as usize)
     915              :                 }
     916          102 :                 "pg_distrib_dir" => {
     917           16 :                     builder.pg_distrib_dir(Utf8PathBuf::from(parse_toml_string(key, item)?))
     918              :                 }
     919           86 :                 "auth_validation_public_key_path" => builder.auth_validation_public_key_path(Some(
     920            0 :                     Utf8PathBuf::from(parse_toml_string(key, item)?),
     921              :                 )),
     922           86 :                 "http_auth_type" => builder.http_auth_type(parse_toml_from_str(key, item)?),
     923           86 :                 "pg_auth_type" => builder.pg_auth_type(parse_toml_from_str(key, item)?),
     924           86 :                 "remote_storage" => {
     925           10 :                     builder.remote_storage_config(Some(RemoteStorageConfig::from_toml(item).context("remote_storage")?))
     926              :                 }
     927           76 :                 "tenant_config" => {
     928            4 :                     t_conf = TenantConfOpt::try_from(item.to_owned()).context(format!("failed to parse: '{key}'"))?;
     929              :                 }
     930           72 :                 "broker_endpoint" => builder.broker_endpoint(parse_toml_string(key, item)?.parse().context("failed to parse broker endpoint")?),
     931           60 :                 "broker_keepalive_interval" => builder.broker_keepalive_interval(parse_toml_duration(key, item)?),
     932           60 :                 "log_format" => builder.log_format(
     933           10 :                     LogFormat::from_config(&parse_toml_string(key, item)?)?
     934              :                 ),
     935           50 :                 "concurrent_tenant_warmup" => builder.concurrent_tenant_warmup({
     936            0 :                     let input = parse_toml_string(key, item)?;
     937            0 :                     let permits = input.parse::<usize>().context("expected a number of initial permits, not {s:?}")?;
     938            0 :                     NonZeroUsize::new(permits).context("initial semaphore permits out of range: 0, use other configuration to disable a feature")?
     939              :                 }),
     940           50 :                 "concurrent_tenant_size_logical_size_queries" => builder.concurrent_tenant_size_logical_size_queries({
     941            0 :                     let input = parse_toml_string(key, item)?;
     942            0 :                     let permits = input.parse::<usize>().context("expected a number of initial permits, not {s:?}")?;
     943            0 :                     NonZeroUsize::new(permits).context("initial semaphore permits out of range: 0, use other configuration to disable a feature")?
     944              :                 }),
     945           50 :                 "metric_collection_interval" => builder.metric_collection_interval(parse_toml_duration(key, item)?),
     946           36 :                 "metric_collection_endpoint" => {
     947           14 :                     let endpoint = parse_toml_string(key, item)?.parse().context("failed to parse metric_collection_endpoint")?;
     948           14 :                     builder.metric_collection_endpoint(Some(endpoint));
     949              :                 },
     950           22 :                 "metric_collection_bucket" => {
     951            0 :                     builder.metric_collection_bucket(Some(RemoteStorageConfig::from_toml(item)?))
     952              :                 }
     953           22 :                 "synthetic_size_calculation_interval" =>
     954           10 :                     builder.synthetic_size_calculation_interval(parse_toml_duration(key, item)?),
     955           12 :                 "test_remote_failures" => builder.test_remote_failures(parse_toml_u64(key, item)?),
     956           12 :                 "disk_usage_based_eviction" => {
     957            2 :                     tracing::info!("disk_usage_based_eviction: {:#?}", &item);
     958            2 :                     builder.disk_usage_based_eviction(
     959            2 :                         deserialize_from_item("disk_usage_based_eviction", item)
     960            2 :                             .context("parse disk_usage_based_eviction")?
     961              :                     )
     962              :                 },
     963           10 :                 "ondemand_download_behavior_treat_error_as_warn" => builder.ondemand_download_behavior_treat_error_as_warn(parse_toml_bool(key, item)?),
     964           10 :                 "background_task_maximum_delay" => builder.background_task_maximum_delay(parse_toml_duration(key, item)?),
     965            0 :                 "control_plane_api" => {
     966            0 :                     let parsed = parse_toml_string(key, item)?;
     967            0 :                     if parsed.is_empty() {
     968            0 :                         builder.control_plane_api(None)
     969              :                     } else {
     970            0 :                         builder.control_plane_api(Some(parsed.parse().context("failed to parse control plane URL")?))
     971              :                     }
     972              :                 },
     973            0 :                 "control_plane_api_token" => {
     974            0 :                     let parsed = parse_toml_string(key, item)?;
     975            0 :                     if parsed.is_empty() {
     976            0 :                         builder.control_plane_api_token(None)
     977              :                     } else {
     978            0 :                         builder.control_plane_api_token(Some(parsed.into()))
     979              :                     }
     980              :                 },
     981            0 :                 "control_plane_emergency_mode" => {
     982            0 :                     builder.control_plane_emergency_mode(parse_toml_bool(key, item)?)
     983              :                 },
     984            0 :                 "heatmap_upload_concurrency" => {
     985            0 :                     builder.heatmap_upload_concurrency(parse_toml_u64(key, item)? as usize)
     986              :                 },
     987            0 :                 "secondary_download_concurrency" => {
     988            0 :                     builder.secondary_download_concurrency(parse_toml_u64(key, item)? as usize)
     989              :                 },
     990            0 :                 "ingest_batch_size" => builder.ingest_batch_size(parse_toml_u64(key, item)?),
     991            0 :                 "virtual_file_io_engine" => {
     992            0 :                     builder.virtual_file_io_engine(parse_toml_from_str("virtual_file_io_engine", item)?)
     993              :                 }
     994            0 :                 "get_vectored_impl" => {
     995            0 :                     builder.get_vectored_impl(parse_toml_from_str("get_vectored_impl", item)?)
     996              :                 }
     997            0 :                 "get_impl" => {
     998            0 :                     builder.get_impl(parse_toml_from_str("get_impl", item)?)
     999              :                 }
    1000            0 :                 "max_vectored_read_bytes" => {
    1001            0 :                     let bytes = parse_toml_u64("max_vectored_read_bytes", item)? as usize;
    1002            0 :                     builder.get_max_vectored_read_bytes(
    1003            0 :                         MaxVectoredReadBytes(
    1004            0 :                             NonZeroUsize::new(bytes).expect("Max byte size of vectored read must be greater than 0")))
    1005              :                 }
    1006            0 :                 "validate_vectored_get" => {
    1007            0 :                     builder.get_validate_vectored_get(parse_toml_bool("validate_vectored_get", item)?)
    1008              :                 }
    1009            0 :                 "image_compression" => {
    1010            0 :                     builder.get_image_compression(parse_toml_from_str("image_compression", item)?)
    1011              :                 }
    1012            0 :                 "ephemeral_bytes_per_memory_kb" => {
    1013            0 :                     builder.get_ephemeral_bytes_per_memory_kb(parse_toml_u64("ephemeral_bytes_per_memory_kb", item)? as usize)
    1014              :                 }
    1015            0 :                 "l0_flush" => {
    1016            0 :                     builder.l0_flush(utils::toml_edit_ext::deserialize_item(item).context("l0_flush")?)
    1017              :                 }
    1018            0 :                 "compact_level0_phase1_value_access" => {
    1019            0 :                     builder.compact_level0_phase1_value_access(utils::toml_edit_ext::deserialize_item(item).context("compact_level0_phase1_value_access")?)
    1020              :                 }
    1021            0 :                 _ => bail!("unrecognized pageserver option '{key}'"),
    1022              :             }
    1023              :         }
    1024              : 
    1025           16 :         let mut conf = builder.build(node_id).context("invalid config")?;
    1026              : 
    1027           16 :         if conf.http_auth_type == AuthType::NeonJWT || conf.pg_auth_type == AuthType::NeonJWT {
    1028            0 :             let auth_validation_public_key_path = conf
    1029            0 :                 .auth_validation_public_key_path
    1030            0 :                 .get_or_insert_with(|| workdir.join("auth_public_key.pem"));
    1031            0 :             ensure!(
    1032            0 :                 auth_validation_public_key_path.exists(),
    1033            0 :                 format!(
    1034            0 :                     "Can't find auth_validation_public_key at '{auth_validation_public_key_path}'",
    1035            0 :                 )
    1036              :             );
    1037           16 :         }
    1038              : 
    1039           16 :         conf.default_tenant_conf = t_conf.merge(TenantConf::default());
    1040           16 : 
    1041           16 :         Ok(conf)
    1042           18 :     }
    1043              : 
    1044              :     #[cfg(test)]
    1045          186 :     pub fn test_repo_dir(test_name: &str) -> Utf8PathBuf {
    1046          186 :         let test_output_dir = std::env::var("TEST_OUTPUT").unwrap_or("../tmp_check".into());
    1047          186 :         Utf8PathBuf::from(format!("{test_output_dir}/test_{test_name}"))
    1048          186 :     }
    1049              : 
    1050          182 :     pub fn dummy_conf(repo_dir: Utf8PathBuf) -> Self {
    1051          182 :         let pg_distrib_dir = Utf8PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../pg_install");
    1052          182 : 
    1053          182 :         PageServerConf {
    1054          182 :             id: NodeId(0),
    1055          182 :             wait_lsn_timeout: Duration::from_secs(60),
    1056          182 :             wal_redo_timeout: Duration::from_secs(60),
    1057          182 :             page_cache_size: defaults::DEFAULT_PAGE_CACHE_SIZE,
    1058          182 :             max_file_descriptors: defaults::DEFAULT_MAX_FILE_DESCRIPTORS,
    1059          182 :             listen_pg_addr: defaults::DEFAULT_PG_LISTEN_ADDR.to_string(),
    1060          182 :             listen_http_addr: defaults::DEFAULT_HTTP_LISTEN_ADDR.to_string(),
    1061          182 :             availability_zone: None,
    1062          182 :             superuser: "cloud_admin".to_string(),
    1063          182 :             workdir: repo_dir,
    1064          182 :             pg_distrib_dir,
    1065          182 :             http_auth_type: AuthType::Trust,
    1066          182 :             pg_auth_type: AuthType::Trust,
    1067          182 :             auth_validation_public_key_path: None,
    1068          182 :             remote_storage_config: None,
    1069          182 :             default_tenant_conf: TenantConf::default(),
    1070          182 :             broker_endpoint: storage_broker::DEFAULT_ENDPOINT.parse().unwrap(),
    1071          182 :             broker_keepalive_interval: Duration::from_secs(5000),
    1072          182 :             log_format: LogFormat::from_str(defaults::DEFAULT_LOG_FORMAT).unwrap(),
    1073          182 :             concurrent_tenant_warmup: ConfigurableSemaphore::new(
    1074          182 :                 NonZeroUsize::new(DEFAULT_CONCURRENT_TENANT_WARMUP)
    1075          182 :                     .expect("Invalid default constant"),
    1076          182 :             ),
    1077          182 :             concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore::default(),
    1078          182 :             eviction_task_immitated_concurrent_logical_size_queries: ConfigurableSemaphore::default(
    1079          182 :             ),
    1080          182 :             metric_collection_interval: Duration::from_secs(60),
    1081          182 :             metric_collection_endpoint: defaults::DEFAULT_METRIC_COLLECTION_ENDPOINT,
    1082          182 :             metric_collection_bucket: None,
    1083          182 :             synthetic_size_calculation_interval: Duration::from_secs(60),
    1084          182 :             disk_usage_based_eviction: None,
    1085          182 :             test_remote_failures: 0,
    1086          182 :             ondemand_download_behavior_treat_error_as_warn: false,
    1087          182 :             background_task_maximum_delay: Duration::ZERO,
    1088          182 :             control_plane_api: None,
    1089          182 :             control_plane_api_token: None,
    1090          182 :             control_plane_emergency_mode: false,
    1091          182 :             heatmap_upload_concurrency: defaults::DEFAULT_HEATMAP_UPLOAD_CONCURRENCY,
    1092          182 :             secondary_download_concurrency: defaults::DEFAULT_SECONDARY_DOWNLOAD_CONCURRENCY,
    1093          182 :             ingest_batch_size: defaults::DEFAULT_INGEST_BATCH_SIZE,
    1094          182 :             virtual_file_io_engine: DEFAULT_VIRTUAL_FILE_IO_ENGINE.parse().unwrap(),
    1095          182 :             get_vectored_impl: defaults::DEFAULT_GET_VECTORED_IMPL.parse().unwrap(),
    1096          182 :             get_impl: defaults::DEFAULT_GET_IMPL.parse().unwrap(),
    1097          182 :             max_vectored_read_bytes: MaxVectoredReadBytes(
    1098          182 :                 NonZeroUsize::new(defaults::DEFAULT_MAX_VECTORED_READ_BYTES)
    1099          182 :                     .expect("Invalid default constant"),
    1100          182 :             ),
    1101          182 :             image_compression: defaults::DEFAULT_IMAGE_COMPRESSION,
    1102          182 :             validate_vectored_get: defaults::DEFAULT_VALIDATE_VECTORED_GET,
    1103          182 :             ephemeral_bytes_per_memory_kb: defaults::DEFAULT_EPHEMERAL_BYTES_PER_MEMORY_KB,
    1104          182 :             l0_flush: L0FlushConfig::default(),
    1105          182 :             compact_level0_phase1_value_access: CompactL0Phase1ValueAccess::default(),
    1106          182 :         }
    1107          182 :     }
    1108              : }
    1109              : 
    1110            0 : #[derive(Deserialize)]
    1111              : #[serde(deny_unknown_fields)]
    1112              : pub struct PageserverIdentity {
    1113              :     pub id: NodeId,
    1114              : }
    1115              : 
    1116              : // Helper functions to parse a toml Item
    1117              : 
    1118           82 : fn parse_toml_string(name: &str, item: &Item) -> Result<String> {
    1119           82 :     let s = item
    1120           82 :         .as_str()
    1121           82 :         .with_context(|| format!("configure option {name} is not a string"))?;
    1122           82 :     Ok(s.to_string())
    1123           82 : }
    1124              : 
    1125           20 : fn parse_toml_u64(name: &str, item: &Item) -> Result<u64> {
    1126              :     // A toml integer is signed, so it cannot represent the full range of an u64. That's OK
    1127              :     // for our use, though.
    1128           20 :     let i: i64 = item
    1129           20 :         .as_integer()
    1130           20 :         .with_context(|| format!("configure option {name} is not an integer"))?;
    1131           20 :     if i < 0 {
    1132            0 :         bail!("configure option {name} cannot be negative");
    1133           20 :     }
    1134           20 :     Ok(i as u64)
    1135           20 : }
    1136              : 
    1137            0 : fn parse_toml_bool(name: &str, item: &Item) -> Result<bool> {
    1138            0 :     item.as_bool()
    1139            0 :         .with_context(|| format!("configure option {name} is not a bool"))
    1140            0 : }
    1141              : 
    1142           54 : fn parse_toml_duration(name: &str, item: &Item) -> Result<Duration> {
    1143           54 :     let s = item
    1144           54 :         .as_str()
    1145           54 :         .with_context(|| format!("configure option {name} is not a string"))?;
    1146              : 
    1147           54 :     Ok(humantime::parse_duration(s)?)
    1148           54 : }
    1149              : 
    1150            0 : fn parse_toml_from_str<T>(name: &str, item: &Item) -> anyhow::Result<T>
    1151            0 : where
    1152            0 :     T: FromStr,
    1153            0 :     <T as FromStr>::Err: std::fmt::Display,
    1154            0 : {
    1155            0 :     let v = item
    1156            0 :         .as_str()
    1157            0 :         .with_context(|| format!("configure option {name} is not a string"))?;
    1158            0 :     T::from_str(v).map_err(|e| {
    1159            0 :         anyhow!(
    1160            0 :             "Failed to parse string as {parse_type} for configure option {name}: {e}",
    1161            0 :             parse_type = stringify!(T)
    1162            0 :         )
    1163            0 :     })
    1164            0 : }
    1165              : 
    1166            2 : fn deserialize_from_item<T>(name: &str, item: &Item) -> anyhow::Result<T>
    1167            2 : where
    1168            2 :     T: serde::de::DeserializeOwned,
    1169            2 : {
    1170              :     // ValueDeserializer::new is not public, so use the ValueDeserializer's documented way
    1171            2 :     let deserializer = match item.clone().into_value() {
    1172            2 :         Ok(value) => value.into_deserializer(),
    1173            0 :         Err(item) => anyhow::bail!("toml_edit::Item '{item}' is not a toml_edit::Value"),
    1174              :     };
    1175            2 :     T::deserialize(deserializer).with_context(|| format!("deserializing item for node {name}"))
    1176            2 : }
    1177              : 
    1178              : /// Configurable semaphore permits setting.
    1179              : ///
    1180              : /// Does not allow semaphore permits to be zero, because at runtime initially zero permits and empty
    1181              : /// semaphore cannot be distinguished, leading any feature using these to await forever (or until
    1182              : /// new permits are added).
    1183              : #[derive(Debug, Clone)]
    1184              : pub struct ConfigurableSemaphore {
    1185              :     initial_permits: NonZeroUsize,
    1186              :     inner: std::sync::Arc<tokio::sync::Semaphore>,
    1187              : }
    1188              : 
    1189              : impl ConfigurableSemaphore {
    1190              :     pub const DEFAULT_INITIAL: NonZeroUsize = match NonZeroUsize::new(1) {
    1191              :         Some(x) => x,
    1192              :         None => panic!("const unwrap is not yet stable"),
    1193              :     };
    1194              : 
    1195              :     /// Initializse using a non-zero amount of permits.
    1196              :     ///
    1197              :     /// Require a non-zero initial permits, because using permits == 0 is a crude way to disable a
    1198              :     /// feature such as [`Tenant::gather_size_inputs`]. Otherwise any semaphore using future will
    1199              :     /// behave like [`futures::future::pending`], just waiting until new permits are added.
    1200              :     ///
    1201              :     /// [`Tenant::gather_size_inputs`]: crate::tenant::Tenant::gather_size_inputs
    1202          606 :     pub fn new(initial_permits: NonZeroUsize) -> Self {
    1203          606 :         ConfigurableSemaphore {
    1204          606 :             initial_permits,
    1205          606 :             inner: std::sync::Arc::new(tokio::sync::Semaphore::new(initial_permits.get())),
    1206          606 :         }
    1207          606 :     }
    1208              : 
    1209              :     /// Returns the configured amount of permits.
    1210            0 :     pub fn initial_permits(&self) -> NonZeroUsize {
    1211            0 :         self.initial_permits
    1212            0 :     }
    1213              : }
    1214              : 
    1215              : impl Default for ConfigurableSemaphore {
    1216          372 :     fn default() -> Self {
    1217          372 :         Self::new(Self::DEFAULT_INITIAL)
    1218          372 :     }
    1219              : }
    1220              : 
    1221              : impl PartialEq for ConfigurableSemaphore {
    1222           12 :     fn eq(&self, other: &Self) -> bool {
    1223           12 :         // the number of permits can be increased at runtime, so we cannot really fulfill the
    1224           12 :         // PartialEq value equality otherwise
    1225           12 :         self.initial_permits == other.initial_permits
    1226           12 :     }
    1227              : }
    1228              : 
    1229              : impl Eq for ConfigurableSemaphore {}
    1230              : 
    1231              : impl ConfigurableSemaphore {
    1232            0 :     pub fn inner(&self) -> &std::sync::Arc<tokio::sync::Semaphore> {
    1233            0 :         &self.inner
    1234            0 :     }
    1235              : }
    1236              : 
    1237              : #[cfg(test)]
    1238              : mod tests {
    1239              :     use std::{fs, num::NonZeroU32};
    1240              : 
    1241              :     use camino_tempfile::{tempdir, Utf8TempDir};
    1242              :     use pageserver_api::models::EvictionPolicy;
    1243              :     use remote_storage::{RemoteStorageKind, S3Config};
    1244              :     use utils::serde_percent::Percent;
    1245              : 
    1246              :     use super::*;
    1247              :     use crate::DEFAULT_PG_VERSION;
    1248              : 
    1249              :     const ALL_BASE_VALUES_TOML: &str = r#"
    1250              : # Initial configuration file created by 'pageserver --init'
    1251              : 
    1252              : listen_pg_addr = '127.0.0.1:64000'
    1253              : listen_http_addr = '127.0.0.1:9898'
    1254              : 
    1255              : wait_lsn_timeout = '111 s'
    1256              : wal_redo_timeout = '111 s'
    1257              : 
    1258              : page_cache_size = 444
    1259              : max_file_descriptors = 333
    1260              : 
    1261              : # initial superuser role name to use when creating a new tenant
    1262              : initial_superuser_name = 'zzzz'
    1263              : 
    1264              : metric_collection_interval = '222 s'
    1265              : metric_collection_endpoint = 'http://localhost:80/metrics'
    1266              : synthetic_size_calculation_interval = '333 s'
    1267              : 
    1268              : log_format = 'json'
    1269              : background_task_maximum_delay = '334 s'
    1270              : 
    1271              : "#;
    1272              : 
    1273              :     #[test]
    1274            2 :     fn parse_defaults() -> anyhow::Result<()> {
    1275            2 :         let tempdir = tempdir()?;
    1276            2 :         let (workdir, pg_distrib_dir) = prepare_fs(&tempdir)?;
    1277            2 :         let broker_endpoint = storage_broker::DEFAULT_ENDPOINT;
    1278            2 :         // we have to create dummy values to overcome the validation errors
    1279            2 :         let config_string =
    1280            2 :             format!("pg_distrib_dir='{pg_distrib_dir}'\nbroker_endpoint = '{broker_endpoint}'",);
    1281            2 :         let toml = config_string.parse()?;
    1282              : 
    1283            2 :         let parsed_config = PageServerConf::parse_and_validate(NodeId(10), &toml, &workdir)
    1284            2 :             .unwrap_or_else(|e| panic!("Failed to parse config '{config_string}', reason: {e:?}"));
    1285            2 : 
    1286            2 :         assert_eq!(
    1287            2 :             parsed_config,
    1288            2 :             PageServerConf {
    1289            2 :                 id: NodeId(10),
    1290            2 :                 listen_pg_addr: defaults::DEFAULT_PG_LISTEN_ADDR.to_string(),
    1291            2 :                 listen_http_addr: defaults::DEFAULT_HTTP_LISTEN_ADDR.to_string(),
    1292            2 :                 availability_zone: None,
    1293            2 :                 wait_lsn_timeout: humantime::parse_duration(defaults::DEFAULT_WAIT_LSN_TIMEOUT)?,
    1294            2 :                 wal_redo_timeout: humantime::parse_duration(defaults::DEFAULT_WAL_REDO_TIMEOUT)?,
    1295            2 :                 superuser: defaults::DEFAULT_SUPERUSER.to_string(),
    1296            2 :                 page_cache_size: defaults::DEFAULT_PAGE_CACHE_SIZE,
    1297            2 :                 max_file_descriptors: defaults::DEFAULT_MAX_FILE_DESCRIPTORS,
    1298            2 :                 workdir,
    1299            2 :                 pg_distrib_dir,
    1300            2 :                 http_auth_type: AuthType::Trust,
    1301            2 :                 pg_auth_type: AuthType::Trust,
    1302            2 :                 auth_validation_public_key_path: None,
    1303            2 :                 remote_storage_config: None,
    1304            2 :                 default_tenant_conf: TenantConf::default(),
    1305            2 :                 broker_endpoint: storage_broker::DEFAULT_ENDPOINT.parse().unwrap(),
    1306            2 :                 broker_keepalive_interval: humantime::parse_duration(
    1307            2 :                     storage_broker::DEFAULT_KEEPALIVE_INTERVAL
    1308            2 :                 )?,
    1309            2 :                 log_format: LogFormat::from_str(defaults::DEFAULT_LOG_FORMAT).unwrap(),
    1310            2 :                 concurrent_tenant_warmup: ConfigurableSemaphore::new(
    1311            2 :                     NonZeroUsize::new(DEFAULT_CONCURRENT_TENANT_WARMUP).unwrap()
    1312            2 :                 ),
    1313            2 :                 concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore::default(),
    1314            2 :                 eviction_task_immitated_concurrent_logical_size_queries:
    1315            2 :                     ConfigurableSemaphore::default(),
    1316            2 :                 metric_collection_interval: humantime::parse_duration(
    1317            2 :                     defaults::DEFAULT_METRIC_COLLECTION_INTERVAL
    1318            2 :                 )?,
    1319            2 :                 metric_collection_endpoint: defaults::DEFAULT_METRIC_COLLECTION_ENDPOINT,
    1320            2 :                 metric_collection_bucket: None,
    1321            2 :                 synthetic_size_calculation_interval: humantime::parse_duration(
    1322            2 :                     defaults::DEFAULT_SYNTHETIC_SIZE_CALCULATION_INTERVAL
    1323            2 :                 )?,
    1324            2 :                 disk_usage_based_eviction: None,
    1325            2 :                 test_remote_failures: 0,
    1326            2 :                 ondemand_download_behavior_treat_error_as_warn: false,
    1327            2 :                 background_task_maximum_delay: humantime::parse_duration(
    1328            2 :                     defaults::DEFAULT_BACKGROUND_TASK_MAXIMUM_DELAY
    1329            2 :                 )?,
    1330            2 :                 control_plane_api: None,
    1331            2 :                 control_plane_api_token: None,
    1332            2 :                 control_plane_emergency_mode: false,
    1333            2 :                 heatmap_upload_concurrency: defaults::DEFAULT_HEATMAP_UPLOAD_CONCURRENCY,
    1334            2 :                 secondary_download_concurrency: defaults::DEFAULT_SECONDARY_DOWNLOAD_CONCURRENCY,
    1335            2 :                 ingest_batch_size: defaults::DEFAULT_INGEST_BATCH_SIZE,
    1336            2 :                 virtual_file_io_engine: DEFAULT_VIRTUAL_FILE_IO_ENGINE.parse().unwrap(),
    1337            2 :                 get_vectored_impl: defaults::DEFAULT_GET_VECTORED_IMPL.parse().unwrap(),
    1338            2 :                 get_impl: defaults::DEFAULT_GET_IMPL.parse().unwrap(),
    1339            2 :                 max_vectored_read_bytes: MaxVectoredReadBytes(
    1340            2 :                     NonZeroUsize::new(defaults::DEFAULT_MAX_VECTORED_READ_BYTES)
    1341            2 :                         .expect("Invalid default constant")
    1342            2 :                 ),
    1343            2 :                 validate_vectored_get: defaults::DEFAULT_VALIDATE_VECTORED_GET,
    1344            2 :                 image_compression: defaults::DEFAULT_IMAGE_COMPRESSION,
    1345            2 :                 ephemeral_bytes_per_memory_kb: defaults::DEFAULT_EPHEMERAL_BYTES_PER_MEMORY_KB,
    1346            2 :                 l0_flush: L0FlushConfig::default(),
    1347            2 :                 compact_level0_phase1_value_access: CompactL0Phase1ValueAccess::default(),
    1348              :             },
    1349            0 :             "Correct defaults should be used when no config values are provided"
    1350              :         );
    1351              : 
    1352            2 :         Ok(())
    1353            2 :     }
    1354              : 
    1355              :     #[test]
    1356            2 :     fn parse_basic_config() -> anyhow::Result<()> {
    1357            2 :         let tempdir = tempdir()?;
    1358            2 :         let (workdir, pg_distrib_dir) = prepare_fs(&tempdir)?;
    1359            2 :         let broker_endpoint = storage_broker::DEFAULT_ENDPOINT;
    1360            2 : 
    1361            2 :         let config_string = format!(
    1362            2 :             "{ALL_BASE_VALUES_TOML}pg_distrib_dir='{pg_distrib_dir}'\nbroker_endpoint = '{broker_endpoint}'",
    1363            2 :         );
    1364            2 :         let toml = config_string.parse()?;
    1365              : 
    1366            2 :         let parsed_config = PageServerConf::parse_and_validate(NodeId(10), &toml, &workdir)
    1367            2 :             .unwrap_or_else(|e| panic!("Failed to parse config '{config_string}', reason: {e:?}"));
    1368            2 : 
    1369            2 :         assert_eq!(
    1370            2 :             parsed_config,
    1371            2 :             PageServerConf {
    1372            2 :                 id: NodeId(10),
    1373            2 :                 listen_pg_addr: "127.0.0.1:64000".to_string(),
    1374            2 :                 listen_http_addr: "127.0.0.1:9898".to_string(),
    1375            2 :                 availability_zone: None,
    1376            2 :                 wait_lsn_timeout: Duration::from_secs(111),
    1377            2 :                 wal_redo_timeout: Duration::from_secs(111),
    1378            2 :                 superuser: "zzzz".to_string(),
    1379            2 :                 page_cache_size: 444,
    1380            2 :                 max_file_descriptors: 333,
    1381            2 :                 workdir,
    1382            2 :                 pg_distrib_dir,
    1383            2 :                 http_auth_type: AuthType::Trust,
    1384            2 :                 pg_auth_type: AuthType::Trust,
    1385            2 :                 auth_validation_public_key_path: None,
    1386            2 :                 remote_storage_config: None,
    1387            2 :                 default_tenant_conf: TenantConf::default(),
    1388            2 :                 broker_endpoint: storage_broker::DEFAULT_ENDPOINT.parse().unwrap(),
    1389            2 :                 broker_keepalive_interval: Duration::from_secs(5),
    1390            2 :                 log_format: LogFormat::Json,
    1391            2 :                 concurrent_tenant_warmup: ConfigurableSemaphore::new(
    1392            2 :                     NonZeroUsize::new(DEFAULT_CONCURRENT_TENANT_WARMUP).unwrap()
    1393            2 :                 ),
    1394            2 :                 concurrent_tenant_size_logical_size_queries: ConfigurableSemaphore::default(),
    1395            2 :                 eviction_task_immitated_concurrent_logical_size_queries:
    1396            2 :                     ConfigurableSemaphore::default(),
    1397            2 :                 metric_collection_interval: Duration::from_secs(222),
    1398            2 :                 metric_collection_endpoint: Some(Url::parse("http://localhost:80/metrics")?),
    1399            2 :                 metric_collection_bucket: None,
    1400            2 :                 synthetic_size_calculation_interval: Duration::from_secs(333),
    1401            2 :                 disk_usage_based_eviction: None,
    1402            2 :                 test_remote_failures: 0,
    1403            2 :                 ondemand_download_behavior_treat_error_as_warn: false,
    1404            2 :                 background_task_maximum_delay: Duration::from_secs(334),
    1405            2 :                 control_plane_api: None,
    1406            2 :                 control_plane_api_token: None,
    1407            2 :                 control_plane_emergency_mode: false,
    1408            2 :                 heatmap_upload_concurrency: defaults::DEFAULT_HEATMAP_UPLOAD_CONCURRENCY,
    1409            2 :                 secondary_download_concurrency: defaults::DEFAULT_SECONDARY_DOWNLOAD_CONCURRENCY,
    1410            2 :                 ingest_batch_size: 100,
    1411            2 :                 virtual_file_io_engine: DEFAULT_VIRTUAL_FILE_IO_ENGINE.parse().unwrap(),
    1412            2 :                 get_vectored_impl: defaults::DEFAULT_GET_VECTORED_IMPL.parse().unwrap(),
    1413            2 :                 get_impl: defaults::DEFAULT_GET_IMPL.parse().unwrap(),
    1414            2 :                 max_vectored_read_bytes: MaxVectoredReadBytes(
    1415            2 :                     NonZeroUsize::new(defaults::DEFAULT_MAX_VECTORED_READ_BYTES)
    1416            2 :                         .expect("Invalid default constant")
    1417            2 :                 ),
    1418            2 :                 validate_vectored_get: defaults::DEFAULT_VALIDATE_VECTORED_GET,
    1419            2 :                 image_compression: defaults::DEFAULT_IMAGE_COMPRESSION,
    1420            2 :                 ephemeral_bytes_per_memory_kb: defaults::DEFAULT_EPHEMERAL_BYTES_PER_MEMORY_KB,
    1421            2 :                 l0_flush: L0FlushConfig::default(),
    1422            2 :                 compact_level0_phase1_value_access: CompactL0Phase1ValueAccess::default(),
    1423              :             },
    1424            0 :             "Should be able to parse all basic config values correctly"
    1425              :         );
    1426              : 
    1427            2 :         Ok(())
    1428            2 :     }
    1429              : 
    1430              :     #[test]
    1431            2 :     fn parse_remote_fs_storage_config() -> anyhow::Result<()> {
    1432            2 :         let tempdir = tempdir()?;
    1433            2 :         let (workdir, pg_distrib_dir) = prepare_fs(&tempdir)?;
    1434            2 :         let broker_endpoint = "http://127.0.0.1:7777";
    1435            2 : 
    1436            2 :         let local_storage_path = tempdir.path().join("local_remote_storage");
    1437            2 : 
    1438            2 :         let identical_toml_declarations = &[
    1439            2 :             format!(
    1440            2 :                 r#"[remote_storage]
    1441            2 : local_path = '{local_storage_path}'"#,
    1442            2 :             ),
    1443            2 :             format!("remote_storage={{local_path='{local_storage_path}'}}"),
    1444            2 :         ];
    1445              : 
    1446            6 :         for remote_storage_config_str in identical_toml_declarations {
    1447            4 :             let config_string = format!(
    1448            4 :                 r#"{ALL_BASE_VALUES_TOML}
    1449            4 : pg_distrib_dir='{pg_distrib_dir}'
    1450            4 : broker_endpoint = '{broker_endpoint}'
    1451            4 : 
    1452            4 : {remote_storage_config_str}"#,
    1453            4 :             );
    1454              : 
    1455            4 :             let toml = config_string.parse()?;
    1456              : 
    1457            4 :             let parsed_remote_storage_config =
    1458            4 :                 PageServerConf::parse_and_validate(NodeId(10), &toml, &workdir)
    1459            4 :                     .unwrap_or_else(|e| {
    1460            0 :                         panic!("Failed to parse config '{config_string}', reason: {e:?}")
    1461            4 :                     })
    1462            4 :                     .remote_storage_config
    1463            4 :                     .expect("Should have remote storage config for the local FS");
    1464            4 : 
    1465            4 :             assert_eq!(
    1466            4 :                 parsed_remote_storage_config,
    1467            4 :                 RemoteStorageConfig {
    1468            4 :                     storage: RemoteStorageKind::LocalFs { local_path: local_storage_path.clone() },
    1469            4 :                     timeout: RemoteStorageConfig::DEFAULT_TIMEOUT,
    1470            4 :                 },
    1471            0 :                 "Remote storage config should correctly parse the local FS config and fill other storage defaults"
    1472              :             );
    1473              :         }
    1474            2 :         Ok(())
    1475            2 :     }
    1476              : 
    1477              :     #[test]
    1478            2 :     fn parse_remote_s3_storage_config() -> anyhow::Result<()> {
    1479            2 :         let tempdir = tempdir()?;
    1480            2 :         let (workdir, pg_distrib_dir) = prepare_fs(&tempdir)?;
    1481              : 
    1482            2 :         let bucket_name = "some-sample-bucket".to_string();
    1483            2 :         let bucket_region = "eu-north-1".to_string();
    1484            2 :         let prefix_in_bucket = "test_prefix".to_string();
    1485            2 :         let endpoint = "http://localhost:5000".to_string();
    1486            2 :         let max_concurrent_syncs = NonZeroUsize::new(111).unwrap();
    1487            2 :         let max_sync_errors = NonZeroU32::new(222).unwrap();
    1488            2 :         let s3_concurrency_limit = NonZeroUsize::new(333).unwrap();
    1489            2 :         let broker_endpoint = "http://127.0.0.1:7777";
    1490            2 : 
    1491            2 :         let identical_toml_declarations = &[
    1492            2 :             format!(
    1493            2 :                 r#"[remote_storage]
    1494            2 : max_concurrent_syncs = {max_concurrent_syncs}
    1495            2 : max_sync_errors = {max_sync_errors}
    1496            2 : bucket_name = '{bucket_name}'
    1497            2 : bucket_region = '{bucket_region}'
    1498            2 : prefix_in_bucket = '{prefix_in_bucket}'
    1499            2 : endpoint = '{endpoint}'
    1500            2 : concurrency_limit = {s3_concurrency_limit}"#
    1501            2 :             ),
    1502            2 :             format!(
    1503            2 :                 "remote_storage={{max_concurrent_syncs={max_concurrent_syncs}, max_sync_errors={max_sync_errors}, bucket_name='{bucket_name}',\
    1504            2 :                 bucket_region='{bucket_region}', prefix_in_bucket='{prefix_in_bucket}', endpoint='{endpoint}', concurrency_limit={s3_concurrency_limit}}}",
    1505            2 :             ),
    1506            2 :         ];
    1507              : 
    1508            6 :         for remote_storage_config_str in identical_toml_declarations {
    1509            4 :             let config_string = format!(
    1510            4 :                 r#"{ALL_BASE_VALUES_TOML}
    1511            4 : pg_distrib_dir='{pg_distrib_dir}'
    1512            4 : broker_endpoint = '{broker_endpoint}'
    1513            4 : 
    1514            4 : {remote_storage_config_str}"#,
    1515            4 :             );
    1516              : 
    1517            4 :             let toml = config_string.parse()?;
    1518              : 
    1519            4 :             let parsed_remote_storage_config =
    1520            4 :                 PageServerConf::parse_and_validate(NodeId(10), &toml, &workdir)
    1521            4 :                     .unwrap_or_else(|e| {
    1522            0 :                         panic!("Failed to parse config '{config_string}', reason: {e:?}")
    1523            4 :                     })
    1524            4 :                     .remote_storage_config
    1525            4 :                     .expect("Should have remote storage config for S3");
    1526            4 : 
    1527            4 :             assert_eq!(
    1528            4 :                 parsed_remote_storage_config,
    1529            4 :                 RemoteStorageConfig {
    1530            4 :                     storage: RemoteStorageKind::AwsS3(S3Config {
    1531            4 :                         bucket_name: bucket_name.clone(),
    1532            4 :                         bucket_region: bucket_region.clone(),
    1533            4 :                         prefix_in_bucket: Some(prefix_in_bucket.clone()),
    1534            4 :                         endpoint: Some(endpoint.clone()),
    1535            4 :                         concurrency_limit: s3_concurrency_limit,
    1536            4 :                         max_keys_per_list_response: None,
    1537            4 :                         upload_storage_class: None,
    1538            4 :                     }),
    1539            4 :                     timeout: RemoteStorageConfig::DEFAULT_TIMEOUT,
    1540            4 :                 },
    1541            0 :                 "Remote storage config should correctly parse the S3 config"
    1542              :             );
    1543              :         }
    1544            2 :         Ok(())
    1545            2 :     }
    1546              : 
    1547              :     #[test]
    1548            2 :     fn parse_incorrect_tenant_config() -> anyhow::Result<()> {
    1549            2 :         let config_string = r#"
    1550            2 :             [tenant_config]
    1551            2 :             checkpoint_distance = -1 # supposed to be an u64
    1552            2 :         "#
    1553            2 :         .to_string();
    1554              : 
    1555            2 :         let toml: Document = config_string.parse()?;
    1556            2 :         let item = toml.get("tenant_config").unwrap();
    1557            2 :         let error = TenantConfOpt::try_from(item.to_owned()).unwrap_err();
    1558            2 : 
    1559            2 :         let expected_error_str = "checkpoint_distance: invalid value: integer `-1`, expected u64";
    1560            2 :         assert_eq!(error.to_string(), expected_error_str);
    1561              : 
    1562            2 :         Ok(())
    1563            2 :     }
    1564              : 
    1565              :     #[test]
    1566            2 :     fn parse_override_tenant_config() -> anyhow::Result<()> {
    1567            2 :         let config_string = r#"tenant_config={ min_resident_size_override =  400 }"#.to_string();
    1568              : 
    1569            2 :         let toml: Document = config_string.parse()?;
    1570            2 :         let item = toml.get("tenant_config").unwrap();
    1571            2 :         let conf = TenantConfOpt::try_from(item.to_owned()).unwrap();
    1572            2 : 
    1573            2 :         assert_eq!(conf.min_resident_size_override, Some(400));
    1574              : 
    1575            2 :         Ok(())
    1576            2 :     }
    1577              : 
    1578              :     #[test]
    1579            2 :     fn eviction_pageserver_config_parse() -> anyhow::Result<()> {
    1580            2 :         let tempdir = tempdir()?;
    1581            2 :         let (workdir, pg_distrib_dir) = prepare_fs(&tempdir)?;
    1582              : 
    1583            2 :         let pageserver_conf_toml = format!(
    1584            2 :             r#"pg_distrib_dir = "{pg_distrib_dir}"
    1585            2 : metric_collection_endpoint = "http://sample.url"
    1586            2 : metric_collection_interval = "10min"
    1587            2 : 
    1588            2 : [disk_usage_based_eviction]
    1589            2 : max_usage_pct = 80
    1590            2 : min_avail_bytes = 0
    1591            2 : period = "10s"
    1592            2 : 
    1593            2 : [tenant_config]
    1594            2 : evictions_low_residence_duration_metric_threshold = "20m"
    1595            2 : 
    1596            2 : [tenant_config.eviction_policy]
    1597            2 : kind = "LayerAccessThreshold"
    1598            2 : period = "20m"
    1599            2 : threshold = "20m"
    1600            2 : "#,
    1601            2 :         );
    1602            2 :         let toml: Document = pageserver_conf_toml.parse()?;
    1603            2 :         let conf = PageServerConf::parse_and_validate(NodeId(333), &toml, &workdir)?;
    1604              : 
    1605            2 :         assert_eq!(conf.pg_distrib_dir, pg_distrib_dir);
    1606            2 :         assert_eq!(
    1607            2 :             conf.metric_collection_endpoint,
    1608            2 :             Some("http://sample.url".parse().unwrap())
    1609            2 :         );
    1610            2 :         assert_eq!(
    1611            2 :             conf.metric_collection_interval,
    1612            2 :             Duration::from_secs(10 * 60)
    1613            2 :         );
    1614            2 :         assert_eq!(
    1615            2 :             conf.default_tenant_conf
    1616            2 :                 .evictions_low_residence_duration_metric_threshold,
    1617            2 :             Duration::from_secs(20 * 60)
    1618            2 :         );
    1619              : 
    1620              :         // Assert that the node id provided by the indentity file (threaded
    1621              :         // through the call to [`PageServerConf::parse_and_validate`] is
    1622              :         // used.
    1623            2 :         assert_eq!(conf.id, NodeId(333));
    1624            2 :         assert_eq!(
    1625            2 :             conf.disk_usage_based_eviction,
    1626            2 :             Some(DiskUsageEvictionTaskConfig {
    1627            2 :                 max_usage_pct: Percent::new(80).unwrap(),
    1628            2 :                 min_avail_bytes: 0,
    1629            2 :                 period: Duration::from_secs(10),
    1630            2 :                 #[cfg(feature = "testing")]
    1631            2 :                 mock_statvfs: None,
    1632            2 :                 eviction_order: Default::default(),
    1633            2 :             })
    1634            2 :         );
    1635              : 
    1636            2 :         match &conf.default_tenant_conf.eviction_policy {
    1637            2 :             EvictionPolicy::LayerAccessThreshold(eviction_threshold) => {
    1638            2 :                 assert_eq!(eviction_threshold.period, Duration::from_secs(20 * 60));
    1639            2 :                 assert_eq!(eviction_threshold.threshold, Duration::from_secs(20 * 60));
    1640              :             }
    1641            0 :             other => unreachable!("Unexpected eviction policy tenant settings: {other:?}"),
    1642              :         }
    1643              : 
    1644            2 :         Ok(())
    1645            2 :     }
    1646              : 
    1647              :     #[test]
    1648            2 :     fn parse_imitation_only_pageserver_config() {
    1649            2 :         let tempdir = tempdir().unwrap();
    1650            2 :         let (workdir, pg_distrib_dir) = prepare_fs(&tempdir).unwrap();
    1651            2 : 
    1652            2 :         let pageserver_conf_toml = format!(
    1653            2 :             r#"pg_distrib_dir = "{pg_distrib_dir}"
    1654            2 : metric_collection_endpoint = "http://sample.url"
    1655            2 : metric_collection_interval = "10min"
    1656            2 : 
    1657            2 : [tenant_config]
    1658            2 : evictions_low_residence_duration_metric_threshold = "20m"
    1659            2 : 
    1660            2 : [tenant_config.eviction_policy]
    1661            2 : kind = "OnlyImitiate"
    1662            2 : period = "20m"
    1663            2 : threshold = "20m"
    1664            2 : "#,
    1665            2 :         );
    1666            2 :         let toml: Document = pageserver_conf_toml.parse().unwrap();
    1667            2 :         let conf = PageServerConf::parse_and_validate(NodeId(222), &toml, &workdir).unwrap();
    1668            2 : 
    1669            2 :         match &conf.default_tenant_conf.eviction_policy {
    1670            2 :             EvictionPolicy::OnlyImitiate(t) => {
    1671            2 :                 assert_eq!(t.period, Duration::from_secs(20 * 60));
    1672            2 :                 assert_eq!(t.threshold, Duration::from_secs(20 * 60));
    1673              :             }
    1674            0 :             other => unreachable!("Unexpected eviction policy tenant settings: {other:?}"),
    1675              :         }
    1676            2 :     }
    1677              : 
    1678              :     #[test]
    1679            2 :     fn empty_remote_storage_is_error() {
    1680            2 :         let tempdir = tempdir().unwrap();
    1681            2 :         let (workdir, _) = prepare_fs(&tempdir).unwrap();
    1682            2 :         let input = r#"
    1683            2 : remote_storage = {}
    1684            2 :         "#;
    1685            2 :         let doc = toml_edit::Document::from_str(input).unwrap();
    1686            2 :         let err = PageServerConf::parse_and_validate(NodeId(222), &doc, &workdir)
    1687            2 :             .expect_err("empty remote_storage field should fail, don't specify it if you want no remote_storage");
    1688            2 :         assert!(format!("{err}").contains("remote_storage"), "{err}");
    1689            2 :     }
    1690              : 
    1691           14 :     fn prepare_fs(tempdir: &Utf8TempDir) -> anyhow::Result<(Utf8PathBuf, Utf8PathBuf)> {
    1692           14 :         let tempdir_path = tempdir.path();
    1693           14 : 
    1694           14 :         let workdir = tempdir_path.join("workdir");
    1695           14 :         fs::create_dir_all(&workdir)?;
    1696              : 
    1697           14 :         let pg_distrib_dir = tempdir_path.join("pg_distrib");
    1698           14 :         let pg_distrib_dir_versioned = pg_distrib_dir.join(format!("v{DEFAULT_PG_VERSION}"));
    1699           14 :         fs::create_dir_all(&pg_distrib_dir_versioned)?;
    1700           14 :         let postgres_bin_dir = pg_distrib_dir_versioned.join("bin");
    1701           14 :         fs::create_dir_all(&postgres_bin_dir)?;
    1702           14 :         fs::write(postgres_bin_dir.join("postgres"), "I'm postgres, trust me")?;
    1703              : 
    1704           14 :         Ok((workdir, pg_distrib_dir))
    1705           14 :     }
    1706              : }
        

Generated by: LCOV version 2.1-beta