Line data Source code
1 : use std::fmt::Write as FmtWrite;
2 : use std::fs::{File, OpenOptions};
3 : use std::io;
4 : use std::io::Write;
5 : use std::io::prelude::*;
6 : use std::path::Path;
7 :
8 : use anyhow::Result;
9 : use compute_api::spec::{ComputeMode, ComputeSpec, GenericOption};
10 :
11 : use crate::pg_helpers::{GenericOptionExt, PgOptionsSerialize, escape_conf_value};
12 :
13 : /// Check that `line` is inside a text file and put it there if it is not.
14 : /// Create file if it doesn't exist.
15 3 : pub fn line_in_file(path: &Path, line: &str) -> Result<bool> {
16 3 : let mut file = OpenOptions::new()
17 3 : .read(true)
18 3 : .write(true)
19 3 : .create(true)
20 3 : .append(false)
21 3 : .truncate(false)
22 3 : .open(path)?;
23 3 : let buf = io::BufReader::new(&file);
24 3 : let mut count: usize = 0;
25 :
26 5 : for l in buf.lines() {
27 5 : if l? == line {
28 1 : return Ok(false);
29 4 : }
30 4 : count = 1;
31 : }
32 :
33 2 : write!(file, "{}{}", "\n".repeat(count), line)?;
34 2 : Ok(true)
35 3 : }
36 :
37 : /// Create or completely rewrite configuration file specified by `path`
38 0 : pub fn write_postgres_conf(
39 0 : path: &Path,
40 0 : spec: &ComputeSpec,
41 0 : extension_server_port: u16,
42 0 : ) -> Result<()> {
43 : // File::create() destroys the file content if it exists.
44 0 : let mut file = File::create(path)?;
45 :
46 : // Write the postgresql.conf content from the spec file as is.
47 0 : if let Some(conf) = &spec.cluster.postgresql_conf {
48 0 : writeln!(file, "{}", conf)?;
49 0 : }
50 :
51 : // Add options for connecting to storage
52 0 : writeln!(file, "# Neon storage settings")?;
53 0 : if let Some(s) = &spec.pageserver_connstring {
54 0 : writeln!(file, "neon.pageserver_connstring={}", escape_conf_value(s))?;
55 0 : }
56 0 : if let Some(stripe_size) = spec.shard_stripe_size {
57 0 : writeln!(file, "neon.stripe_size={stripe_size}")?;
58 0 : }
59 0 : if !spec.safekeeper_connstrings.is_empty() {
60 0 : let mut neon_safekeepers_value = String::new();
61 0 : tracing::info!(
62 0 : "safekeepers_connstrings is not zero, gen: {:?}",
63 : spec.safekeepers_generation
64 : );
65 : // If generation is given, prepend sk list with g#number:
66 0 : if let Some(generation) = spec.safekeepers_generation {
67 0 : write!(neon_safekeepers_value, "g#{}:", generation)?;
68 0 : }
69 0 : neon_safekeepers_value.push_str(&spec.safekeeper_connstrings.join(","));
70 0 : writeln!(
71 0 : file,
72 0 : "neon.safekeepers={}",
73 0 : escape_conf_value(&neon_safekeepers_value)
74 0 : )?;
75 0 : }
76 0 : if let Some(s) = &spec.tenant_id {
77 0 : writeln!(file, "neon.tenant_id={}", escape_conf_value(&s.to_string()))?;
78 0 : }
79 0 : if let Some(s) = &spec.timeline_id {
80 0 : writeln!(
81 0 : file,
82 0 : "neon.timeline_id={}",
83 0 : escape_conf_value(&s.to_string())
84 0 : )?;
85 0 : }
86 :
87 : // Locales
88 0 : if cfg!(target_os = "macos") {
89 0 : writeln!(file, "lc_messages='C'")?;
90 0 : writeln!(file, "lc_monetary='C'")?;
91 0 : writeln!(file, "lc_time='C'")?;
92 0 : writeln!(file, "lc_numeric='C'")?;
93 : } else {
94 0 : writeln!(file, "lc_messages='C.UTF-8'")?;
95 0 : writeln!(file, "lc_monetary='C.UTF-8'")?;
96 0 : writeln!(file, "lc_time='C.UTF-8'")?;
97 0 : writeln!(file, "lc_numeric='C.UTF-8'")?;
98 : }
99 :
100 0 : match spec.mode {
101 0 : ComputeMode::Primary => {}
102 0 : ComputeMode::Static(lsn) => {
103 0 : // hot_standby is 'on' by default, but let's be explicit
104 0 : writeln!(file, "hot_standby=on")?;
105 0 : writeln!(file, "recovery_target_lsn='{lsn}'")?;
106 : }
107 : ComputeMode::Replica => {
108 : // hot_standby is 'on' by default, but let's be explicit
109 0 : writeln!(file, "hot_standby=on")?;
110 : }
111 : }
112 :
113 0 : if cfg!(target_os = "linux") {
114 : // Check /proc/sys/vm/overcommit_memory -- if it equals 2 (i.e. linux memory overcommit is
115 : // disabled), then the control plane has enabled swap and we should set
116 : // dynamic_shared_memory_type = 'mmap'.
117 : //
118 : // This is (maybe?) temporary - for more, see https://github.com/neondatabase/cloud/issues/12047.
119 0 : let overcommit_memory_contents = std::fs::read_to_string("/proc/sys/vm/overcommit_memory")
120 0 : // ignore any errors - they may be expected to occur under certain situations (e.g. when
121 0 : // not running in Linux).
122 0 : .unwrap_or_else(|_| String::new());
123 0 : if overcommit_memory_contents.trim() == "2" {
124 0 : let opt = GenericOption {
125 0 : name: "dynamic_shared_memory_type".to_owned(),
126 0 : value: Some("mmap".to_owned()),
127 0 : vartype: "enum".to_owned(),
128 0 : };
129 0 :
130 0 : writeln!(file, "{}", opt.to_pg_setting())?;
131 0 : }
132 0 : }
133 :
134 : // If there are any extra options in the 'settings' field, append those
135 0 : if spec.cluster.settings.is_some() {
136 0 : writeln!(file, "# Managed by compute_ctl: begin")?;
137 0 : write!(file, "{}", spec.cluster.settings.as_pg_settings())?;
138 0 : writeln!(file, "# Managed by compute_ctl: end")?;
139 0 : }
140 :
141 0 : writeln!(file, "neon.extension_server_port={}", extension_server_port)?;
142 :
143 0 : if spec.drop_subscriptions_before_start {
144 0 : writeln!(file, "neon.disable_logical_replication_subscribers=true")?;
145 : } else {
146 : // be explicit about the default value
147 0 : writeln!(file, "neon.disable_logical_replication_subscribers=false")?;
148 : }
149 :
150 : // This is essential to keep this line at the end of the file,
151 : // because it is intended to override any settings above.
152 0 : writeln!(file, "include_if_exists = 'compute_ctl_temp_override.conf'")?;
153 :
154 0 : Ok(())
155 0 : }
156 :
157 0 : pub fn with_compute_ctl_tmp_override<F>(pgdata_path: &Path, options: &str, exec: F) -> Result<()>
158 0 : where
159 0 : F: FnOnce() -> Result<()>,
160 0 : {
161 0 : let path = pgdata_path.join("compute_ctl_temp_override.conf");
162 0 : let mut file = File::create(path)?;
163 0 : write!(file, "{}", options)?;
164 :
165 0 : let res = exec();
166 0 :
167 0 : file.set_len(0)?;
168 :
169 0 : res
170 0 : }
|