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