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