Line data Source code
1 : //! `ComputeSpec` represents the contents of the spec.json file.
2 : //!
3 : //! The spec.json file is used to pass information to 'compute_ctl'. It contains
4 : //! all the information needed to start up the right version of PostgreSQL,
5 : //! and connect it to the storage nodes.
6 : use std::collections::HashMap;
7 :
8 : use regex::Regex;
9 : use remote_storage::RemotePath;
10 : use serde::{Deserialize, Serialize};
11 : use utils::id::{TenantId, TimelineId};
12 : use utils::lsn::Lsn;
13 :
14 : /// String type alias representing Postgres identifier and
15 : /// intended to be used for DB / role names.
16 : pub type PgIdent = String;
17 :
18 : /// String type alias representing Postgres extension version
19 : pub type ExtVersion = String;
20 :
21 6 : fn default_reconfigure_concurrency() -> usize {
22 6 : 1
23 6 : }
24 :
25 : /// Cluster spec or configuration represented as an optional number of
26 : /// delta operations + final cluster state description.
27 45 : #[derive(Clone, Debug, Default, Deserialize, Serialize)]
28 : pub struct ComputeSpec {
29 : pub format_version: f32,
30 :
31 : // The control plane also includes a 'timestamp' field in the JSON document,
32 : // but we don't use it for anything. Serde will ignore missing fields when
33 : // deserializing it.
34 : pub operation_uuid: Option<String>,
35 :
36 : /// Compute features to enable. These feature flags are provided, when we
37 : /// know all the details about client's compute, so they cannot be used
38 : /// to change `Empty` compute behavior.
39 : #[serde(default)]
40 : pub features: Vec<ComputeFeature>,
41 :
42 : /// If compute_ctl was passed `--resize-swap-on-bind`, a value of `Some(_)` instructs
43 : /// compute_ctl to `/neonvm/bin/resize-swap` with the given size, when the spec is first
44 : /// received.
45 : ///
46 : /// Both this field and `--resize-swap-on-bind` are required, so that the control plane's
47 : /// spec generation doesn't need to be aware of the actual compute it's running on, while
48 : /// guaranteeing gradual rollout of swap. Otherwise, without `--resize-swap-on-bind`, we could
49 : /// end up trying to resize swap in VMs without it -- or end up *not* resizing swap, thus
50 : /// giving every VM much more swap than it should have (32GiB).
51 : ///
52 : /// Eventually we may remove `--resize-swap-on-bind` and exclusively use `swap_size_bytes` for
53 : /// enabling the swap resizing behavior once rollout is complete.
54 : ///
55 : /// See neondatabase/cloud#12047 for more.
56 : #[serde(default)]
57 : pub swap_size_bytes: Option<u64>,
58 :
59 : /// If compute_ctl was passed `--set-disk-quota-for-fs`, a value of `Some(_)` instructs
60 : /// compute_ctl to run `/neonvm/bin/set-disk-quota` with the given size and fs, when the
61 : /// spec is first received.
62 : ///
63 : /// Both this field and `--set-disk-quota-for-fs` are required, so that the control plane's
64 : /// spec generation doesn't need to be aware of the actual compute it's running on, while
65 : /// guaranteeing gradual rollout of disk quota.
66 : #[serde(default)]
67 : pub disk_quota_bytes: Option<u64>,
68 :
69 : /// Disables the vm-monitor behavior that resizes LFC on upscale/downscale, instead relying on
70 : /// the initial size of LFC.
71 : ///
72 : /// This is intended for use when the LFC size is being overridden from the default but
73 : /// autoscaling is still enabled, and we don't want the vm-monitor to interfere with the custom
74 : /// LFC sizing.
75 : #[serde(default)]
76 : pub disable_lfc_resizing: Option<bool>,
77 :
78 : /// Expected cluster state at the end of transition process.
79 : pub cluster: Cluster,
80 : pub delta_operations: Option<Vec<DeltaOp>>,
81 :
82 : /// An optional hint that can be passed to speed up startup time if we know
83 : /// that no pg catalog mutations (like role creation, database creation,
84 : /// extension creation) need to be done on the actual database to start.
85 : #[serde(default)] // Default false
86 : pub skip_pg_catalog_updates: bool,
87 :
88 : // Information needed to connect to the storage layer.
89 : //
90 : // `tenant_id`, `timeline_id` and `pageserver_connstring` are always needed.
91 : //
92 : // Depending on `mode`, this can be a primary read-write node, a read-only
93 : // replica, or a read-only node pinned at an older LSN.
94 : // `safekeeper_connstrings` must be set for a primary.
95 : //
96 : // For backwards compatibility, the control plane may leave out all of
97 : // these, and instead set the "neon.tenant_id", "neon.timeline_id",
98 : // etc. GUCs in cluster.settings. TODO: Once the control plane has been
99 : // updated to fill these fields, we can make these non optional.
100 : pub tenant_id: Option<TenantId>,
101 : pub timeline_id: Option<TimelineId>,
102 : pub pageserver_connstring: Option<String>,
103 :
104 : /// Safekeeper membership config generation. It is put in
105 : /// neon.safekeepers GUC and serves two purposes:
106 : /// 1) Non zero value forces walproposer to use membership configurations.
107 : /// 2) If walproposer wants to update list of safekeepers to connect to
108 : /// taking them from some safekeeper mconf, it should check what value
109 : /// is newer by comparing the generation.
110 : ///
111 : /// Note: it could be SafekeeperGeneration, but this needs linking
112 : /// compute_ctl with postgres_ffi.
113 : #[serde(default)]
114 : pub safekeepers_generation: Option<u32>,
115 : #[serde(default)]
116 : pub safekeeper_connstrings: Vec<String>,
117 :
118 : #[serde(default)]
119 : pub mode: ComputeMode,
120 :
121 : /// If set, 'storage_auth_token' is used as the password to authenticate to
122 : /// the pageserver and safekeepers.
123 : pub storage_auth_token: Option<String>,
124 :
125 : // information about available remote extensions
126 : pub remote_extensions: Option<RemoteExtSpec>,
127 :
128 : pub pgbouncer_settings: Option<HashMap<String, String>>,
129 :
130 : // Stripe size for pageserver sharding, in pages
131 : #[serde(default)]
132 : pub shard_stripe_size: Option<usize>,
133 :
134 : /// Local Proxy configuration used for JWT authentication
135 : #[serde(default)]
136 : pub local_proxy_config: Option<LocalProxySpec>,
137 :
138 : /// Number of concurrent connections during the parallel RunInEachDatabase
139 : /// phase of the apply config process.
140 : ///
141 : /// We need a higher concurrency during reconfiguration in case of many DBs,
142 : /// but instance is already running and used by client. We can easily get out of
143 : /// `max_connections` limit, and the current code won't handle that.
144 : ///
145 : /// Default is 1, but also allow control plane to override this value for specific
146 : /// projects. It's also recommended to bump `superuser_reserved_connections` +=
147 : /// `reconfigure_concurrency` for such projects to ensure that we always have
148 : /// enough spare connections for reconfiguration process to succeed.
149 : #[serde(default = "default_reconfigure_concurrency")]
150 : pub reconfigure_concurrency: usize,
151 :
152 : /// If set to true, the compute_ctl will drop all subscriptions before starting the
153 : /// compute. This is needed when we start an endpoint on a branch, so that child
154 : /// would not compete with parent branch subscriptions
155 : /// over the same replication content from publisher.
156 : #[serde(default)] // Default false
157 : pub drop_subscriptions_before_start: bool,
158 : }
159 :
160 : /// Feature flag to signal `compute_ctl` to enable certain experimental functionality.
161 3 : #[derive(Serialize, Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
162 : #[serde(rename_all = "snake_case")]
163 : pub enum ComputeFeature {
164 : // XXX: Add more feature flags here.
165 : /// Enable the experimental activity monitor logic, which uses `pg_stat_database` to
166 : /// track short-lived connections as user activity.
167 : ActivityMonitorExperimental,
168 :
169 : /// Pre-install and initialize anon extension for every database in the cluster
170 : AnonExtension,
171 :
172 : /// This is a special feature flag that is used to represent unknown feature flags.
173 : /// Basically all unknown to enum flags are represented as this one. See unit test
174 : /// `parse_unknown_features()` for more details.
175 : #[serde(other)]
176 : UnknownFeature,
177 : }
178 :
179 44 : #[derive(Clone, Debug, Default, Deserialize, Serialize)]
180 : pub struct RemoteExtSpec {
181 : pub public_extensions: Option<Vec<String>>,
182 : pub custom_extensions: Option<Vec<String>>,
183 : pub library_index: HashMap<String, String>,
184 : pub extension_data: HashMap<String, ExtensionData>,
185 : }
186 :
187 30 : #[derive(Clone, Debug, Serialize, Deserialize)]
188 : pub struct ExtensionData {
189 : pub control_data: HashMap<String, String>,
190 : pub archive_path: String,
191 : }
192 :
193 : impl RemoteExtSpec {
194 6 : pub fn get_ext(
195 6 : &self,
196 6 : ext_name: &str,
197 6 : is_library: bool,
198 6 : build_tag: &str,
199 6 : pg_major_version: &str,
200 6 : ) -> anyhow::Result<(String, RemotePath)> {
201 6 : let mut real_ext_name = ext_name;
202 6 : if is_library {
203 : // sometimes library names might have a suffix like
204 : // library.so or library.so.3. We strip this off
205 : // because library_index is based on the name without the file extension
206 1 : let strip_lib_suffix = Regex::new(r"\.so.*").unwrap();
207 1 : let lib_raw_name = strip_lib_suffix.replace(real_ext_name, "").to_string();
208 1 :
209 1 : real_ext_name = self
210 1 : .library_index
211 1 : .get(&lib_raw_name)
212 1 : .ok_or(anyhow::anyhow!("library {} is not found", lib_raw_name))?;
213 5 : }
214 :
215 : // Check if extension is present in public or custom.
216 : // If not, then it is not allowed to be used by this compute.
217 6 : if !self
218 6 : .public_extensions
219 6 : .as_ref()
220 6 : .is_some_and(|exts| exts.iter().any(|e| e == real_ext_name))
221 4 : && !self
222 4 : .custom_extensions
223 4 : .as_ref()
224 4 : .is_some_and(|exts| exts.iter().any(|e| e == real_ext_name))
225 : {
226 3 : return Err(anyhow::anyhow!("extension {} is not found", real_ext_name));
227 3 : }
228 3 :
229 3 : match self.extension_data.get(real_ext_name) {
230 3 : Some(_ext_data) => {
231 3 : // Construct the path to the extension archive
232 3 : // BUILD_TAG/PG_MAJOR_VERSION/extensions/EXTENSION_NAME.tar.zst
233 3 : //
234 3 : // Keep it in sync with path generation in
235 3 : // https://github.com/neondatabase/build-custom-extensions/tree/main
236 3 : let archive_path_str =
237 3 : format!("{build_tag}/{pg_major_version}/extensions/{real_ext_name}.tar.zst");
238 3 : Ok((
239 3 : real_ext_name.to_string(),
240 3 : RemotePath::from_string(&archive_path_str)?,
241 : ))
242 : }
243 0 : None => Err(anyhow::anyhow!(
244 0 : "real_ext_name {} is not found",
245 0 : real_ext_name
246 0 : )),
247 : }
248 6 : }
249 : }
250 :
251 0 : #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
252 : pub enum ComputeMode {
253 : /// A read-write node
254 : #[default]
255 : Primary,
256 : /// A read-only node, pinned at a particular LSN
257 : Static(Lsn),
258 : /// A read-only node that follows the tip of the branch in hot standby mode
259 : ///
260 : /// Future versions may want to distinguish between replicas with hot standby
261 : /// feedback and other kinds of replication configurations.
262 : Replica,
263 : }
264 :
265 36 : #[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
266 : pub struct Cluster {
267 : pub cluster_id: Option<String>,
268 : pub name: Option<String>,
269 : pub state: Option<String>,
270 : pub roles: Vec<Role>,
271 : pub databases: Vec<Database>,
272 :
273 : /// Desired contents of 'postgresql.conf' file. (The 'compute_ctl'
274 : /// tool may add additional settings to the final file.)
275 : pub postgresql_conf: Option<String>,
276 :
277 : /// Additional settings that will be appended to the 'postgresql.conf' file.
278 : pub settings: GenericOptions,
279 : }
280 :
281 : /// Single cluster state changing operation that could not be represented as
282 : /// a static `Cluster` structure. For example:
283 : /// - DROP DATABASE
284 : /// - DROP ROLE
285 : /// - ALTER ROLE name RENAME TO new_name
286 : /// - ALTER DATABASE name RENAME TO new_name
287 60 : #[derive(Clone, Debug, Deserialize, Serialize)]
288 : pub struct DeltaOp {
289 : pub action: String,
290 : pub name: PgIdent,
291 : pub new_name: Option<PgIdent>,
292 : }
293 :
294 : /// Rust representation of Postgres role info with only those fields
295 : /// that matter for us.
296 90 : #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
297 : pub struct Role {
298 : pub name: PgIdent,
299 : pub encrypted_password: Option<String>,
300 : pub options: GenericOptions,
301 : }
302 :
303 : /// Rust representation of Postgres database info with only those fields
304 : /// that matter for us.
305 42 : #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
306 : pub struct Database {
307 : pub name: PgIdent,
308 : pub owner: PgIdent,
309 : pub options: GenericOptions,
310 : // These are derived flags, not present in the spec file.
311 : // They are never set by the control plane.
312 : #[serde(skip_deserializing, default)]
313 : pub restrict_conn: bool,
314 : #[serde(skip_deserializing, default)]
315 : pub invalid: bool,
316 : }
317 :
318 : /// Common type representing both SQL statement params with or without value,
319 : /// like `LOGIN` or `OWNER username` in the `CREATE/ALTER ROLE`, and config
320 : /// options like `wal_level = logical`.
321 468 : #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
322 : pub struct GenericOption {
323 : pub name: String,
324 : pub value: Option<String>,
325 : pub vartype: String,
326 : }
327 :
328 : /// Optional collection of `GenericOption`'s. Type alias allows us to
329 : /// declare a `trait` on it.
330 : pub type GenericOptions = Option<Vec<GenericOption>>;
331 :
332 : /// Configured the local_proxy application with the relevant JWKS and roles it should
333 : /// use for authorizing connect requests using JWT.
334 0 : #[derive(Clone, Debug, Deserialize, Serialize)]
335 : pub struct LocalProxySpec {
336 : #[serde(default)]
337 : #[serde(skip_serializing_if = "Option::is_none")]
338 : pub jwks: Option<Vec<JwksSettings>>,
339 : }
340 :
341 0 : #[derive(Clone, Debug, Deserialize, Serialize)]
342 : pub struct JwksSettings {
343 : pub id: String,
344 : pub role_names: Vec<String>,
345 : pub jwks_url: String,
346 : pub provider_name: String,
347 : pub jwt_audience: Option<String>,
348 : }
349 :
350 : #[cfg(test)]
351 : mod tests {
352 : use std::fs::File;
353 :
354 : use super::*;
355 :
356 : #[test]
357 1 : fn allow_installing_remote_extensions() {
358 1 : let rspec: RemoteExtSpec = serde_json::from_value(serde_json::json!({
359 1 : "public_extensions": null,
360 1 : "custom_extensions": null,
361 1 : "library_index": {},
362 1 : "extension_data": {},
363 1 : }))
364 1 : .unwrap();
365 1 :
366 1 : rspec
367 1 : .get_ext("ext", false, "latest", "v17")
368 1 : .expect_err("Extension should not be found");
369 1 :
370 1 : let rspec: RemoteExtSpec = serde_json::from_value(serde_json::json!({
371 1 : "public_extensions": [],
372 1 : "custom_extensions": null,
373 1 : "library_index": {},
374 1 : "extension_data": {},
375 1 : }))
376 1 : .unwrap();
377 1 :
378 1 : rspec
379 1 : .get_ext("ext", false, "latest", "v17")
380 1 : .expect_err("Extension should not be found");
381 1 :
382 1 : let rspec: RemoteExtSpec = serde_json::from_value(serde_json::json!({
383 1 : "public_extensions": [],
384 1 : "custom_extensions": [],
385 1 : "library_index": {
386 1 : "ext": "ext"
387 1 : },
388 1 : "extension_data": {
389 1 : "ext": {
390 1 : "control_data": {
391 1 : "ext.control": ""
392 1 : },
393 1 : "archive_path": ""
394 1 : }
395 1 : },
396 1 : }))
397 1 : .unwrap();
398 1 :
399 1 : rspec
400 1 : .get_ext("ext", false, "latest", "v17")
401 1 : .expect_err("Extension should not be found");
402 1 :
403 1 : let rspec: RemoteExtSpec = serde_json::from_value(serde_json::json!({
404 1 : "public_extensions": [],
405 1 : "custom_extensions": ["ext"],
406 1 : "library_index": {
407 1 : "ext": "ext"
408 1 : },
409 1 : "extension_data": {
410 1 : "ext": {
411 1 : "control_data": {
412 1 : "ext.control": ""
413 1 : },
414 1 : "archive_path": ""
415 1 : }
416 1 : },
417 1 : }))
418 1 : .unwrap();
419 1 :
420 1 : rspec
421 1 : .get_ext("ext", false, "latest", "v17")
422 1 : .expect("Extension should be found");
423 1 :
424 1 : let rspec: RemoteExtSpec = serde_json::from_value(serde_json::json!({
425 1 : "public_extensions": ["ext"],
426 1 : "custom_extensions": [],
427 1 : "library_index": {
428 1 : "extlib": "ext",
429 1 : },
430 1 : "extension_data": {
431 1 : "ext": {
432 1 : "control_data": {
433 1 : "ext.control": ""
434 1 : },
435 1 : "archive_path": ""
436 1 : }
437 1 : },
438 1 : }))
439 1 : .unwrap();
440 1 :
441 1 : rspec
442 1 : .get_ext("ext", false, "latest", "v17")
443 1 : .expect("Extension should be found");
444 1 :
445 1 : // test library index for the case when library name
446 1 : // doesn't match the extension name
447 1 : rspec
448 1 : .get_ext("extlib", true, "latest", "v17")
449 1 : .expect("Library should be found");
450 1 : }
451 :
452 : #[test]
453 1 : fn parse_spec_file() {
454 1 : let file = File::open("tests/cluster_spec.json").unwrap();
455 1 : let spec: ComputeSpec = serde_json::from_reader(file).unwrap();
456 1 :
457 1 : // Features list defaults to empty vector.
458 1 : assert!(spec.features.is_empty());
459 :
460 : // Reconfigure concurrency defaults to 1.
461 1 : assert_eq!(spec.reconfigure_concurrency, 1);
462 1 : }
463 :
464 : #[test]
465 1 : fn parse_unknown_fields() {
466 1 : // Forward compatibility test
467 1 : let file = File::open("tests/cluster_spec.json").unwrap();
468 1 : let mut json: serde_json::Value = serde_json::from_reader(file).unwrap();
469 1 : let ob = json.as_object_mut().unwrap();
470 1 : ob.insert("unknown_field_123123123".into(), "hello".into());
471 1 : let _spec: ComputeSpec = serde_json::from_value(json).unwrap();
472 1 : }
473 :
474 : #[test]
475 1 : fn parse_unknown_features() {
476 1 : // Test that unknown feature flags do not cause any errors.
477 1 : let file = File::open("tests/cluster_spec.json").unwrap();
478 1 : let mut json: serde_json::Value = serde_json::from_reader(file).unwrap();
479 1 : let ob = json.as_object_mut().unwrap();
480 1 :
481 1 : // Add unknown feature flags.
482 1 : let features = vec!["foo_bar_feature", "baz_feature"];
483 1 : ob.insert("features".into(), features.into());
484 1 :
485 1 : let spec: ComputeSpec = serde_json::from_value(json).unwrap();
486 1 :
487 1 : assert!(spec.features.len() == 2);
488 1 : assert!(spec.features.contains(&ComputeFeature::UnknownFeature));
489 1 : assert_eq!(spec.features, vec![ComputeFeature::UnknownFeature; 2]);
490 1 : }
491 :
492 : #[test]
493 1 : fn parse_known_features() {
494 1 : // Test that we can properly parse known feature flags.
495 1 : let file = File::open("tests/cluster_spec.json").unwrap();
496 1 : let mut json: serde_json::Value = serde_json::from_reader(file).unwrap();
497 1 : let ob = json.as_object_mut().unwrap();
498 1 :
499 1 : // Add known feature flags.
500 1 : let features = vec!["activity_monitor_experimental"];
501 1 : ob.insert("features".into(), features.into());
502 1 :
503 1 : let spec: ComputeSpec = serde_json::from_value(json).unwrap();
504 1 :
505 1 : assert_eq!(
506 1 : spec.features,
507 1 : vec![ComputeFeature::ActivityMonitorExperimental]
508 1 : );
509 1 : }
510 : }
|