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_enabled: bool,
97 : pub partial_backup_timeout: Duration,
98 : pub disable_periodic_broker_push: bool,
99 : pub enable_offload: bool,
100 : pub delete_offloaded_wal: bool,
101 : pub control_file_save_interval: Duration,
102 : pub partial_backup_concurrency: usize,
103 : pub eviction_min_resident: Duration,
104 : }
105 :
106 : impl SafeKeeperConf {
107 0 : pub fn is_wal_backup_enabled(&self) -> bool {
108 0 : self.remote_storage.is_some() && self.wal_backup_enabled
109 0 : }
110 : }
111 :
112 : impl SafeKeeperConf {
113 : #[cfg(test)]
114 4 : fn dummy() -> Self {
115 4 : SafeKeeperConf {
116 4 : workdir: Utf8PathBuf::from("./"),
117 4 : no_sync: false,
118 4 : listen_pg_addr: defaults::DEFAULT_PG_LISTEN_ADDR.to_string(),
119 4 : listen_pg_addr_tenant_only: None,
120 4 : listen_http_addr: defaults::DEFAULT_HTTP_LISTEN_ADDR.to_string(),
121 4 : advertise_pg_addr: None,
122 4 : availability_zone: None,
123 4 : remote_storage: None,
124 4 : my_id: NodeId(0),
125 4 : broker_endpoint: storage_broker::DEFAULT_ENDPOINT
126 4 : .parse()
127 4 : .expect("failed to parse default broker endpoint"),
128 4 : broker_keepalive_interval: Duration::from_secs(5),
129 4 : peer_recovery_enabled: true,
130 4 : wal_backup_enabled: true,
131 4 : backup_parallel_jobs: 1,
132 4 : pg_auth: None,
133 4 : pg_tenant_only_auth: None,
134 4 : http_auth: None,
135 4 : sk_auth_token: None,
136 4 : heartbeat_timeout: Duration::new(5, 0),
137 4 : max_offloader_lag_bytes: defaults::DEFAULT_MAX_OFFLOADER_LAG_BYTES,
138 4 : current_thread_runtime: false,
139 4 : walsenders_keep_horizon: false,
140 4 : partial_backup_enabled: false,
141 4 : partial_backup_timeout: Duration::from_secs(0),
142 4 : disable_periodic_broker_push: false,
143 4 : enable_offload: false,
144 4 : delete_offloaded_wal: false,
145 4 : control_file_save_interval: Duration::from_secs(1),
146 4 : partial_backup_concurrency: 1,
147 4 : eviction_min_resident: Duration::ZERO,
148 4 : }
149 4 : }
150 : }
151 :
152 : // Tokio runtimes.
153 0 : pub static WAL_SERVICE_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
154 0 : tokio::runtime::Builder::new_multi_thread()
155 0 : .thread_name("WAL service worker")
156 0 : .enable_all()
157 0 : .build()
158 0 : .expect("Failed to create WAL service runtime")
159 0 : });
160 :
161 0 : pub static HTTP_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
162 0 : tokio::runtime::Builder::new_multi_thread()
163 0 : .thread_name("HTTP worker")
164 0 : .enable_all()
165 0 : .build()
166 0 : .expect("Failed to create WAL service runtime")
167 0 : });
168 :
169 0 : pub static BROKER_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
170 0 : tokio::runtime::Builder::new_multi_thread()
171 0 : .thread_name("broker worker")
172 0 : .worker_threads(2) // there are only 2 tasks, having more threads doesn't make sense
173 0 : .enable_all()
174 0 : .build()
175 0 : .expect("Failed to create broker runtime")
176 0 : });
177 :
178 0 : pub static WAL_BACKUP_RUNTIME: Lazy<Runtime> = Lazy::new(|| {
179 0 : tokio::runtime::Builder::new_multi_thread()
180 0 : .thread_name("WAL backup worker")
181 0 : .enable_all()
182 0 : .build()
183 0 : .expect("Failed to create WAL backup runtime")
184 0 : });
|