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 receive_wal;
25 : pub mod recovery;
26 : pub mod remove_wal;
27 : pub mod safekeeper;
28 : pub mod send_wal;
29 : pub mod state;
30 : pub mod timeline;
31 : pub mod timeline_eviction;
32 : pub mod timeline_guard;
33 : pub mod timeline_manager;
34 : pub mod timelines_set;
35 : pub mod wal_backup;
36 : pub mod wal_backup_partial;
37 : pub mod wal_service;
38 : pub mod wal_storage;
39 :
40 : mod timelines_global_map;
41 : use std::sync::Arc;
42 : pub use timelines_global_map::GlobalTimelines;
43 : use utils::auth::JwtAuth;
44 :
45 : pub mod defaults {
46 : pub use safekeeper_api::{
47 : DEFAULT_HTTP_LISTEN_ADDR, DEFAULT_HTTP_LISTEN_PORT, DEFAULT_PG_LISTEN_ADDR,
48 : DEFAULT_PG_LISTEN_PORT,
49 : };
50 :
51 : pub const DEFAULT_HEARTBEAT_TIMEOUT: &str = "5000ms";
52 : pub const DEFAULT_MAX_OFFLOADER_LAG_BYTES: u64 = 128 * (1 << 20);
53 : pub const DEFAULT_PARTIAL_BACKUP_TIMEOUT: &str = "15m";
54 : pub const DEFAULT_CONTROL_FILE_SAVE_INTERVAL: &str = "300s";
55 : pub const DEFAULT_PARTIAL_BACKUP_CONCURRENCY: &str = "5";
56 :
57 : // By default, our required residency before eviction is the same as the period that passes
58 : // before uploading a partial segment, so that in normal operation the eviction can happen
59 : // as soon as we have done the partial segment upload.
60 : pub const DEFAULT_EVICTION_MIN_RESIDENT: &str = DEFAULT_PARTIAL_BACKUP_TIMEOUT;
61 : }
62 :
63 : #[derive(Debug, Clone)]
64 : pub struct SafeKeeperConf {
65 : // Repository directory, relative to current working directory.
66 : // Normally, the safekeeper changes the current working directory
67 : // to the repository, and 'workdir' is always '.'. But we don't do
68 : // that during unit testing, because the current directory is global
69 : // to the process but different unit tests work on different
70 : // data directories to avoid clashing with each other.
71 : pub workdir: Utf8PathBuf,
72 : pub my_id: NodeId,
73 : pub listen_pg_addr: String,
74 : pub listen_pg_addr_tenant_only: Option<String>,
75 : pub listen_http_addr: String,
76 : pub advertise_pg_addr: Option<String>,
77 : pub availability_zone: Option<String>,
78 : pub no_sync: bool,
79 : pub broker_endpoint: Uri,
80 : pub broker_keepalive_interval: Duration,
81 : pub heartbeat_timeout: Duration,
82 : pub peer_recovery_enabled: bool,
83 : pub remote_storage: Option<RemoteStorageConfig>,
84 : pub max_offloader_lag_bytes: u64,
85 : pub backup_parallel_jobs: usize,
86 : pub wal_backup_enabled: bool,
87 : pub pg_auth: Option<Arc<JwtAuth>>,
88 : pub pg_tenant_only_auth: Option<Arc<JwtAuth>>,
89 : pub http_auth: Option<Arc<SwappableJwtAuth>>,
90 : /// JWT token to connect to other safekeepers with.
91 : pub sk_auth_token: Option<SecretString>,
92 : pub current_thread_runtime: bool,
93 : pub walsenders_keep_horizon: bool,
94 : pub partial_backup_enabled: bool,
95 : pub partial_backup_timeout: Duration,
96 : pub disable_periodic_broker_push: bool,
97 : pub enable_offload: bool,
98 : pub delete_offloaded_wal: bool,
99 : pub control_file_save_interval: Duration,
100 : pub partial_backup_concurrency: usize,
101 : pub eviction_min_resident: Duration,
102 : }
103 :
104 : impl SafeKeeperConf {
105 0 : pub fn is_wal_backup_enabled(&self) -> bool {
106 0 : self.remote_storage.is_some() && self.wal_backup_enabled
107 0 : }
108 : }
109 :
110 : impl SafeKeeperConf {
111 : #[cfg(test)]
112 4 : fn dummy() -> Self {
113 4 : SafeKeeperConf {
114 4 : workdir: Utf8PathBuf::from("./"),
115 4 : no_sync: false,
116 4 : listen_pg_addr: defaults::DEFAULT_PG_LISTEN_ADDR.to_string(),
117 4 : listen_pg_addr_tenant_only: None,
118 4 : listen_http_addr: defaults::DEFAULT_HTTP_LISTEN_ADDR.to_string(),
119 4 : advertise_pg_addr: None,
120 4 : availability_zone: None,
121 4 : remote_storage: None,
122 4 : my_id: NodeId(0),
123 4 : broker_endpoint: storage_broker::DEFAULT_ENDPOINT
124 4 : .parse()
125 4 : .expect("failed to parse default broker endpoint"),
126 4 : broker_keepalive_interval: Duration::from_secs(5),
127 4 : peer_recovery_enabled: true,
128 4 : wal_backup_enabled: true,
129 4 : backup_parallel_jobs: 1,
130 4 : pg_auth: None,
131 4 : pg_tenant_only_auth: None,
132 4 : http_auth: None,
133 4 : sk_auth_token: None,
134 4 : heartbeat_timeout: Duration::new(5, 0),
135 4 : max_offloader_lag_bytes: defaults::DEFAULT_MAX_OFFLOADER_LAG_BYTES,
136 4 : current_thread_runtime: false,
137 4 : walsenders_keep_horizon: false,
138 4 : partial_backup_enabled: false,
139 4 : partial_backup_timeout: Duration::from_secs(0),
140 4 : disable_periodic_broker_push: false,
141 4 : enable_offload: false,
142 4 : delete_offloaded_wal: false,
143 4 : control_file_save_interval: Duration::from_secs(1),
144 4 : partial_backup_concurrency: 1,
145 4 : eviction_min_resident: Duration::ZERO,
146 4 : }
147 4 : }
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 : });
|