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