LCOV - code coverage report
Current view: top level - safekeeper/src - lib.rs (source / functions) Coverage Total Hit
Test: 1b0a6a0c05cee5a7de360813c8034804e105ce1c.info Lines: 58.0 % 69 40
Test Date: 2025-03-12 00:01:28 Functions: 33.3 % 6 2

            Line data    Source code
       1              : #![deny(clippy::undocumented_unsafe_blocks)]
       2              : 
       3              : extern crate hyper0 as hyper;
       4              : 
       5              : use std::time::Duration;
       6              : 
       7              : use camino::Utf8PathBuf;
       8              : use once_cell::sync::Lazy;
       9              : use remote_storage::RemoteStorageConfig;
      10              : use storage_broker::Uri;
      11              : use tokio::runtime::Runtime;
      12              : use utils::auth::SwappableJwtAuth;
      13              : use utils::id::NodeId;
      14              : use utils::logging::SecretString;
      15              : 
      16              : mod auth;
      17              : pub mod broker;
      18              : pub mod control_file;
      19              : pub mod control_file_upgrade;
      20              : pub mod copy_timeline;
      21              : pub mod debug_dump;
      22              : pub mod handler;
      23              : pub mod http;
      24              : pub mod metrics;
      25              : pub mod patch_control_file;
      26              : pub mod pull_timeline;
      27              : pub mod rate_limit;
      28              : pub mod receive_wal;
      29              : pub mod recovery;
      30              : pub mod remove_wal;
      31              : pub mod safekeeper;
      32              : pub mod send_interpreted_wal;
      33              : pub mod send_wal;
      34              : pub mod state;
      35              : pub mod timeline;
      36              : pub mod timeline_eviction;
      37              : pub mod timeline_guard;
      38              : pub mod timeline_manager;
      39              : pub mod timelines_set;
      40              : pub mod wal_backup;
      41              : pub mod wal_backup_partial;
      42              : pub mod wal_reader_stream;
      43              : pub mod wal_service;
      44              : pub mod wal_storage;
      45              : 
      46              : #[cfg(any(test, feature = "benchmarking"))]
      47              : pub mod test_utils;
      48              : 
      49              : mod timelines_global_map;
      50              : use std::sync::Arc;
      51              : 
      52              : pub use timelines_global_map::GlobalTimelines;
      53              : use utils::auth::JwtAuth;
      54              : 
      55              : pub mod defaults {
      56              :     pub use safekeeper_api::{
      57              :         DEFAULT_HTTP_LISTEN_ADDR, DEFAULT_HTTP_LISTEN_PORT, DEFAULT_PG_LISTEN_ADDR,
      58              :         DEFAULT_PG_LISTEN_PORT,
      59              :     };
      60              : 
      61              :     pub const DEFAULT_HEARTBEAT_TIMEOUT: &str = "5000ms";
      62              :     pub const DEFAULT_MAX_OFFLOADER_LAG_BYTES: u64 = 128 * (1 << 20);
      63              :     pub const DEFAULT_PARTIAL_BACKUP_TIMEOUT: &str = "15m";
      64              :     pub const DEFAULT_CONTROL_FILE_SAVE_INTERVAL: &str = "300s";
      65              :     pub const DEFAULT_PARTIAL_BACKUP_CONCURRENCY: &str = "5";
      66              :     pub const DEFAULT_EVICTION_CONCURRENCY: usize = 2;
      67              : 
      68              :     // By default, our required residency before eviction is the same as the period that passes
      69              :     // before uploading a partial segment, so that in normal operation the eviction can happen
      70              :     // as soon as we have done the partial segment upload.
      71              :     pub const DEFAULT_EVICTION_MIN_RESIDENT: &str = DEFAULT_PARTIAL_BACKUP_TIMEOUT;
      72              : }
      73              : 
      74              : #[derive(Debug, Clone)]
      75              : pub struct SafeKeeperConf {
      76              :     // Repository directory, relative to current working directory.
      77              :     // Normally, the safekeeper changes the current working directory
      78              :     // to the repository, and 'workdir' is always '.'. But we don't do
      79              :     // that during unit testing, because the current directory is global
      80              :     // to the process but different unit tests work on different
      81              :     // data directories to avoid clashing with each other.
      82              :     pub workdir: Utf8PathBuf,
      83              :     pub my_id: NodeId,
      84              :     pub listen_pg_addr: String,
      85              :     pub listen_pg_addr_tenant_only: Option<String>,
      86              :     pub listen_http_addr: String,
      87              :     pub advertise_pg_addr: Option<String>,
      88              :     pub availability_zone: Option<String>,
      89              :     pub no_sync: bool,
      90              :     pub broker_endpoint: Uri,
      91              :     pub broker_keepalive_interval: Duration,
      92              :     pub heartbeat_timeout: Duration,
      93              :     pub peer_recovery_enabled: bool,
      94              :     pub remote_storage: Option<RemoteStorageConfig>,
      95              :     pub max_offloader_lag_bytes: u64,
      96              :     pub backup_parallel_jobs: usize,
      97              :     pub wal_backup_enabled: bool,
      98              :     pub pg_auth: Option<Arc<JwtAuth>>,
      99              :     pub pg_tenant_only_auth: Option<Arc<JwtAuth>>,
     100              :     pub http_auth: Option<Arc<SwappableJwtAuth>>,
     101              :     /// JWT token to connect to other safekeepers with.
     102              :     pub sk_auth_token: Option<SecretString>,
     103              :     pub current_thread_runtime: bool,
     104              :     pub walsenders_keep_horizon: bool,
     105              :     pub partial_backup_timeout: Duration,
     106              :     pub disable_periodic_broker_push: bool,
     107              :     pub enable_offload: bool,
     108              :     pub delete_offloaded_wal: bool,
     109              :     pub control_file_save_interval: Duration,
     110              :     pub partial_backup_concurrency: usize,
     111              :     pub eviction_min_resident: Duration,
     112              :     pub wal_reader_fanout: bool,
     113              :     pub max_delta_for_fanout: Option<u64>,
     114              : }
     115              : 
     116              : impl SafeKeeperConf {
     117           77 :     pub fn is_wal_backup_enabled(&self) -> bool {
     118           77 :         self.remote_storage.is_some() && self.wal_backup_enabled
     119           77 :     }
     120              : }
     121              : 
     122              : impl SafeKeeperConf {
     123           10 :     pub fn dummy() -> Self {
     124           10 :         SafeKeeperConf {
     125           10 :             workdir: Utf8PathBuf::from("./"),
     126           10 :             no_sync: false,
     127           10 :             listen_pg_addr: defaults::DEFAULT_PG_LISTEN_ADDR.to_string(),
     128           10 :             listen_pg_addr_tenant_only: None,
     129           10 :             listen_http_addr: defaults::DEFAULT_HTTP_LISTEN_ADDR.to_string(),
     130           10 :             advertise_pg_addr: None,
     131           10 :             availability_zone: None,
     132           10 :             remote_storage: None,
     133           10 :             my_id: NodeId(0),
     134           10 :             broker_endpoint: storage_broker::DEFAULT_ENDPOINT
     135           10 :                 .parse()
     136           10 :                 .expect("failed to parse default broker endpoint"),
     137           10 :             broker_keepalive_interval: Duration::from_secs(5),
     138           10 :             peer_recovery_enabled: true,
     139           10 :             wal_backup_enabled: true,
     140           10 :             backup_parallel_jobs: 1,
     141           10 :             pg_auth: None,
     142           10 :             pg_tenant_only_auth: None,
     143           10 :             http_auth: None,
     144           10 :             sk_auth_token: None,
     145           10 :             heartbeat_timeout: Duration::new(5, 0),
     146           10 :             max_offloader_lag_bytes: defaults::DEFAULT_MAX_OFFLOADER_LAG_BYTES,
     147           10 :             current_thread_runtime: false,
     148           10 :             walsenders_keep_horizon: false,
     149           10 :             partial_backup_timeout: Duration::from_secs(0),
     150           10 :             disable_periodic_broker_push: false,
     151           10 :             enable_offload: false,
     152           10 :             delete_offloaded_wal: false,
     153           10 :             control_file_save_interval: Duration::from_secs(1),
     154           10 :             partial_backup_concurrency: 1,
     155           10 :             eviction_min_resident: Duration::ZERO,
     156           10 :             wal_reader_fanout: false,
     157           10 :             max_delta_for_fanout: None,
     158           10 :         }
     159           10 :     }
     160              : }
     161              : 
     162              : // Tokio runtimes.
     163            0 : pub static WAL_SERVICE_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
     164            0 :     tokio::runtime::Builder::new_multi_thread()
     165            0 :         .thread_name("WAL service worker")
     166            0 :         .enable_all()
     167            0 :         .build()
     168            0 :         .expect("Failed to create WAL service runtime")
     169            0 : });
     170              : 
     171            0 : pub static HTTP_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
     172            0 :     tokio::runtime::Builder::new_multi_thread()
     173            0 :         .thread_name("HTTP worker")
     174            0 :         .enable_all()
     175            0 :         .build()
     176            0 :         .expect("Failed to create HTTP runtime")
     177            0 : });
     178              : 
     179            0 : pub static BROKER_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
     180            0 :     tokio::runtime::Builder::new_multi_thread()
     181            0 :         .thread_name("broker worker")
     182            0 :         .worker_threads(2) // there are only 2 tasks, having more threads doesn't make sense
     183            0 :         .enable_all()
     184            0 :         .build()
     185            0 :         .expect("Failed to create broker runtime")
     186            0 : });
     187              : 
     188            0 : pub static WAL_BACKUP_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
     189            0 :     tokio::runtime::Builder::new_multi_thread()
     190            0 :         .thread_name("WAL backup worker")
     191            0 :         .enable_all()
     192            0 :         .build()
     193            0 :         .expect("Failed to create WAL backup runtime")
     194            0 : });
        

Generated by: LCOV version 2.1-beta