LCOV - code coverage report
Current view: top level - safekeeper/src - lib.rs (source / functions) Coverage Total Hit
Test: b4ae4c4857f9ef3e144e982a35ee23bc84c71983.info Lines: 52.2 % 67 35
Test Date: 2024-10-22 22:13:45 Functions: 16.7 % 6 1

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

Generated by: LCOV version 2.1-beta