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 pem::Pem;
10 : use remote_storage::RemoteStorageConfig;
11 : use storage_broker::Uri;
12 : use tokio::runtime::Runtime;
13 : use url::Url;
14 : use utils::auth::SwappableJwtAuth;
15 : use utils::id::NodeId;
16 : use utils::logging::SecretString;
17 :
18 : mod auth;
19 : pub mod broker;
20 : pub mod control_file;
21 : pub mod control_file_upgrade;
22 : pub mod copy_timeline;
23 : pub mod debug_dump;
24 : pub mod hadron;
25 : pub mod handler;
26 : pub mod http;
27 : pub mod metrics;
28 : pub mod patch_control_file;
29 : pub mod pull_timeline;
30 : pub mod rate_limit;
31 : pub mod receive_wal;
32 : pub mod recovery;
33 : pub mod remove_wal;
34 : pub mod safekeeper;
35 : pub mod send_interpreted_wal;
36 : pub mod send_wal;
37 : pub mod state;
38 : pub mod timeline;
39 : pub mod timeline_eviction;
40 : pub mod timeline_guard;
41 : pub mod timeline_manager;
42 : pub mod timelines_set;
43 : pub mod wal_backup;
44 : pub mod wal_backup_partial;
45 : pub mod wal_reader_stream;
46 : pub mod wal_service;
47 : pub mod wal_storage;
48 :
49 : #[cfg(any(test, feature = "benchmarking"))]
50 : pub mod test_utils;
51 :
52 : mod timelines_global_map;
53 : use std::sync::Arc;
54 :
55 : pub use timelines_global_map::GlobalTimelines;
56 : use utils::auth::JwtAuth;
57 :
58 : pub mod defaults {
59 : pub use safekeeper_api::{
60 : DEFAULT_HTTP_LISTEN_ADDR, DEFAULT_HTTP_LISTEN_PORT, DEFAULT_PG_LISTEN_ADDR,
61 : DEFAULT_PG_LISTEN_PORT,
62 : };
63 :
64 : pub const DEFAULT_HEARTBEAT_TIMEOUT: &str = "5000ms";
65 : pub const DEFAULT_MAX_OFFLOADER_LAG_BYTES: u64 = 128 * (1 << 20);
66 : /* BEGIN_HADRON */
67 : // Default leader re-elect is 0(disabled). SK will re-elect leader if the current leader is lagging this many bytes.
68 : pub const DEFAULT_MAX_REELECT_OFFLOADER_LAG_BYTES: u64 = 0;
69 : // Default disk usage limit is 0 (disabled). It means each timeline by default can use up to this many WAL
70 : // disk space on this SK until SK begins to reject WALs.
71 : pub const DEFAULT_MAX_TIMELINE_DISK_USAGE_BYTES: u64 = 0;
72 : /* END_HADRON */
73 : pub const DEFAULT_PARTIAL_BACKUP_TIMEOUT: &str = "15m";
74 : pub const DEFAULT_CONTROL_FILE_SAVE_INTERVAL: &str = "300s";
75 : pub const DEFAULT_PARTIAL_BACKUP_CONCURRENCY: &str = "5";
76 : pub const DEFAULT_EVICTION_CONCURRENCY: usize = 2;
77 :
78 : // By default, our required residency before eviction is the same as the period that passes
79 : // before uploading a partial segment, so that in normal operation the eviction can happen
80 : // as soon as we have done the partial segment upload.
81 : pub const DEFAULT_EVICTION_MIN_RESIDENT: &str = DEFAULT_PARTIAL_BACKUP_TIMEOUT;
82 :
83 : pub const DEFAULT_SSL_KEY_FILE: &str = "server.key";
84 : pub const DEFAULT_SSL_CERT_FILE: &str = "server.crt";
85 : pub const DEFAULT_SSL_CERT_RELOAD_PERIOD: &str = "60s";
86 : }
87 :
88 : #[derive(Debug, Clone)]
89 : pub struct SafeKeeperConf {
90 : // Repository directory, relative to current working directory.
91 : // Normally, the safekeeper changes the current working directory
92 : // to the repository, and 'workdir' is always '.'. But we don't do
93 : // that during unit testing, because the current directory is global
94 : // to the process but different unit tests work on different
95 : // data directories to avoid clashing with each other.
96 : pub workdir: Utf8PathBuf,
97 : pub my_id: NodeId,
98 : pub listen_pg_addr: String,
99 : pub listen_pg_addr_tenant_only: Option<String>,
100 : pub listen_http_addr: String,
101 : pub listen_https_addr: Option<String>,
102 : pub advertise_pg_addr: Option<String>,
103 : pub availability_zone: Option<String>,
104 : pub no_sync: bool,
105 : /* BEGIN_HADRON */
106 : pub advertise_pg_addr_tenant_only: Option<String>,
107 : pub enable_pull_timeline_on_startup: bool,
108 : pub hcc_base_url: Option<Url>,
109 : /* END_HADRON */
110 : pub broker_endpoint: Uri,
111 : pub broker_keepalive_interval: Duration,
112 : pub heartbeat_timeout: Duration,
113 : pub peer_recovery_enabled: bool,
114 : pub remote_storage: Option<RemoteStorageConfig>,
115 : pub max_offloader_lag_bytes: u64,
116 : /* BEGIN_HADRON */
117 : pub max_reelect_offloader_lag_bytes: u64,
118 : pub max_timeline_disk_usage_bytes: u64,
119 : /* END_HADRON */
120 : pub backup_parallel_jobs: usize,
121 : pub wal_backup_enabled: bool,
122 : pub pg_auth: Option<Arc<JwtAuth>>,
123 : pub pg_tenant_only_auth: Option<Arc<JwtAuth>>,
124 : pub http_auth: Option<Arc<SwappableJwtAuth>>,
125 : /// JWT token to connect to other safekeepers with.
126 : pub sk_auth_token: Option<SecretString>,
127 : pub current_thread_runtime: bool,
128 : pub walsenders_keep_horizon: bool,
129 : pub partial_backup_timeout: Duration,
130 : pub disable_periodic_broker_push: bool,
131 : pub enable_offload: bool,
132 : pub delete_offloaded_wal: bool,
133 : pub control_file_save_interval: Duration,
134 : pub partial_backup_concurrency: usize,
135 : pub eviction_min_resident: Duration,
136 : pub wal_reader_fanout: bool,
137 : pub max_delta_for_fanout: Option<u64>,
138 : pub ssl_key_file: Utf8PathBuf,
139 : pub ssl_cert_file: Utf8PathBuf,
140 : pub ssl_cert_reload_period: Duration,
141 : pub ssl_ca_certs: Vec<Pem>,
142 : pub use_https_safekeeper_api: bool,
143 : pub enable_tls_wal_service_api: bool,
144 : pub force_metric_collection_on_scrape: bool,
145 : }
146 :
147 : impl SafeKeeperConf {
148 11 : pub fn dummy() -> Self {
149 11 : SafeKeeperConf {
150 11 : workdir: Utf8PathBuf::from("./"),
151 11 : no_sync: false,
152 11 : listen_pg_addr: defaults::DEFAULT_PG_LISTEN_ADDR.to_string(),
153 11 : listen_pg_addr_tenant_only: None,
154 11 : listen_http_addr: defaults::DEFAULT_HTTP_LISTEN_ADDR.to_string(),
155 11 : listen_https_addr: None,
156 11 : advertise_pg_addr: None,
157 11 : availability_zone: None,
158 11 : remote_storage: None,
159 11 : my_id: NodeId(0),
160 11 : broker_endpoint: storage_broker::DEFAULT_ENDPOINT
161 11 : .parse()
162 11 : .expect("failed to parse default broker endpoint"),
163 11 : broker_keepalive_interval: Duration::from_secs(5),
164 11 : peer_recovery_enabled: true,
165 11 : wal_backup_enabled: true,
166 11 : backup_parallel_jobs: 1,
167 11 : pg_auth: None,
168 11 : pg_tenant_only_auth: None,
169 11 : http_auth: None,
170 11 : sk_auth_token: None,
171 11 : heartbeat_timeout: Duration::new(5, 0),
172 11 : max_offloader_lag_bytes: defaults::DEFAULT_MAX_OFFLOADER_LAG_BYTES,
173 11 : /* BEGIN_HADRON */
174 11 : max_reelect_offloader_lag_bytes: defaults::DEFAULT_MAX_REELECT_OFFLOADER_LAG_BYTES,
175 11 : max_timeline_disk_usage_bytes: defaults::DEFAULT_MAX_TIMELINE_DISK_USAGE_BYTES,
176 11 : /* END_HADRON */
177 11 : current_thread_runtime: false,
178 11 : walsenders_keep_horizon: false,
179 11 : partial_backup_timeout: Duration::from_secs(0),
180 11 : disable_periodic_broker_push: false,
181 11 : enable_offload: false,
182 11 : delete_offloaded_wal: false,
183 11 : control_file_save_interval: Duration::from_secs(1),
184 11 : partial_backup_concurrency: 1,
185 11 : eviction_min_resident: Duration::ZERO,
186 11 : wal_reader_fanout: false,
187 11 : max_delta_for_fanout: None,
188 11 : ssl_key_file: Utf8PathBuf::from(defaults::DEFAULT_SSL_KEY_FILE),
189 11 : ssl_cert_file: Utf8PathBuf::from(defaults::DEFAULT_SSL_CERT_FILE),
190 11 : ssl_cert_reload_period: Duration::from_secs(60),
191 11 : ssl_ca_certs: Vec::new(),
192 11 : use_https_safekeeper_api: false,
193 11 : enable_tls_wal_service_api: false,
194 11 : force_metric_collection_on_scrape: true,
195 11 : /* BEGIN_HADRON */
196 11 : advertise_pg_addr_tenant_only: None,
197 11 : enable_pull_timeline_on_startup: false,
198 11 : hcc_base_url: None,
199 11 : /* END_HADRON */
200 11 : }
201 11 : }
202 : }
203 :
204 : // Tokio runtimes.
205 0 : pub static WAL_SERVICE_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
206 0 : tokio::runtime::Builder::new_multi_thread()
207 0 : .thread_name("WAL service worker")
208 0 : .enable_all()
209 0 : .build()
210 0 : .expect("Failed to create WAL service runtime")
211 0 : });
212 :
213 0 : pub static HTTP_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
214 0 : tokio::runtime::Builder::new_multi_thread()
215 0 : .thread_name("HTTP worker")
216 0 : .enable_all()
217 0 : .build()
218 0 : .expect("Failed to create HTTP runtime")
219 0 : });
220 :
221 0 : pub static BROKER_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
222 0 : tokio::runtime::Builder::new_multi_thread()
223 0 : .thread_name("broker worker")
224 0 : .worker_threads(2) // there are only 2 tasks, having more threads doesn't make sense
225 0 : .enable_all()
226 0 : .build()
227 0 : .expect("Failed to create broker runtime")
228 0 : });
229 :
230 0 : pub static WAL_BACKUP_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
231 0 : tokio::runtime::Builder::new_multi_thread()
232 0 : .thread_name("WAL backup worker")
233 0 : .enable_all()
234 0 : .build()
235 0 : .expect("Failed to create WAL backup runtime")
236 0 : });
237 :
238 0 : pub static BACKGROUND_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
239 0 : tokio::runtime::Builder::new_multi_thread()
240 0 : .thread_name("background worker")
241 0 : .worker_threads(1) // there is only one task now (ssl certificate reloading), having more threads doesn't make sense
242 0 : .enable_all()
243 0 : .build()
244 0 : .expect("Failed to create background runtime")
245 0 : });
|