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