LCOV - differential code coverage report
Current view: top level - compute_tools/src - config.rs (source / functions) Coverage Total Hit UBC CBC
Current: cd44433dd675caa99df17a61b18949c8387e2242.info Lines: 90.8 % 76 69 7 69
Current Date: 2024-01-09 02:06:09 Functions: 100.0 % 4 4 4
Baseline: 66c52a629a0f4a503e193045e0df4c77139e344b.info
Baseline Date: 2024-01-08 15:34:46

           TLA  Line data    Source code
       1                 : use std::fs::{File, OpenOptions};
       2                 : use std::io;
       3                 : use std::io::prelude::*;
       4                 : use std::path::Path;
       5                 : 
       6                 : use anyhow::Result;
       7                 : 
       8                 : use crate::pg_helpers::escape_conf_value;
       9                 : use crate::pg_helpers::PgOptionsSerialize;
      10                 : use compute_api::spec::{ComputeMode, ComputeSpec};
      11                 : 
      12                 : /// Check that `line` is inside a text file and put it there if it is not.
      13                 : /// Create file if it doesn't exist.
      14 CBC         540 : pub fn line_in_file(path: &Path, line: &str) -> Result<bool> {
      15             540 :     let mut file = OpenOptions::new()
      16             540 :         .read(true)
      17             540 :         .write(true)
      18             540 :         .create(true)
      19             540 :         .append(false)
      20             540 :         .open(path)?;
      21             540 :     let buf = io::BufReader::new(&file);
      22             540 :     let mut count: usize = 0;
      23                 : 
      24           52631 :     for l in buf.lines() {
      25           52631 :         if l? == line {
      26               1 :             return Ok(false);
      27           52630 :         }
      28           52630 :         count = 1;
      29                 :     }
      30                 : 
      31             539 :     write!(file, "{}{}", "\n".repeat(count), line)?;
      32             539 :     Ok(true)
      33             540 : }
      34                 : 
      35                 : /// Create or completely rewrite configuration file specified by `path`
      36             761 : pub fn write_postgres_conf(
      37             761 :     path: &Path,
      38             761 :     spec: &ComputeSpec,
      39             761 :     extension_server_port: Option<u16>,
      40             761 : ) -> Result<()> {
      41                 :     // File::create() destroys the file content if it exists.
      42             761 :     let mut file = File::create(path)?;
      43                 : 
      44                 :     // Write the postgresql.conf content from the spec file as is.
      45             761 :     if let Some(conf) = &spec.cluster.postgresql_conf {
      46             761 :         writeln!(file, "{}", conf)?;
      47 UBC           0 :     }
      48                 : 
      49                 :     // Add options for connecting to storage
      50 CBC         761 :     writeln!(file, "# Neon storage settings")?;
      51             761 :     if let Some(s) = &spec.pageserver_connstring {
      52             761 :         writeln!(file, "neon.pageserver_connstring={}", escape_conf_value(s))?;
      53 UBC           0 :     }
      54 CBC         761 :     if !spec.safekeeper_connstrings.is_empty() {
      55             711 :         writeln!(
      56             711 :             file,
      57             711 :             "neon.safekeepers={}",
      58             711 :             escape_conf_value(&spec.safekeeper_connstrings.join(","))
      59             711 :         )?;
      60              50 :     }
      61             761 :     if let Some(s) = &spec.tenant_id {
      62             761 :         writeln!(file, "neon.tenant_id={}", escape_conf_value(&s.to_string()))?;
      63 UBC           0 :     }
      64 CBC         761 :     if let Some(s) = &spec.timeline_id {
      65             761 :         writeln!(
      66             761 :             file,
      67             761 :             "neon.timeline_id={}",
      68             761 :             escape_conf_value(&s.to_string())
      69             761 :         )?;
      70 UBC           0 :     }
      71                 : 
      72 CBC         761 :     match spec.mode {
      73             711 :         ComputeMode::Primary => {}
      74              48 :         ComputeMode::Static(lsn) => {
      75              48 :             // hot_standby is 'on' by default, but let's be explicit
      76              48 :             writeln!(file, "hot_standby=on")?;
      77              48 :             writeln!(file, "recovery_target_lsn='{lsn}'")?;
      78                 :         }
      79                 :         ComputeMode::Replica => {
      80                 :             // hot_standby is 'on' by default, but let's be explicit
      81               2 :             writeln!(file, "hot_standby=on")?;
      82                 :         }
      83                 :     }
      84                 : 
      85                 :     // If there are any extra options in the 'settings' field, append those
      86             761 :     if spec.cluster.settings.is_some() {
      87 UBC           0 :         writeln!(file, "# Managed by compute_ctl: begin")?;
      88               0 :         write!(file, "{}", spec.cluster.settings.as_pg_settings())?;
      89               0 :         writeln!(file, "# Managed by compute_ctl: end")?;
      90 CBC         761 :     }
      91                 : 
      92             761 :     if let Some(port) = extension_server_port {
      93             544 :         writeln!(file, "neon.extension_server_port={}", port)?;
      94             217 :     }
      95                 : 
      96                 :     // This is essential to keep this line at the end of the file,
      97                 :     // because it is intended to override any settings above.
      98             761 :     writeln!(file, "include_if_exists = 'compute_ctl_temp_override.conf'")?;
      99                 : 
     100             761 :     Ok(())
     101             761 : }
     102                 : 
     103                 : /// create file compute_ctl_temp_override.conf in pgdata_dir
     104                 : /// add provided options to this file
     105             221 : pub fn compute_ctl_temp_override_create(pgdata_path: &Path, options: &str) -> Result<()> {
     106             221 :     let path = pgdata_path.join("compute_ctl_temp_override.conf");
     107             221 :     let mut file = File::create(path)?;
     108             221 :     write!(file, "{}", options)?;
     109             221 :     Ok(())
     110             221 : }
     111                 : 
     112                 : /// remove file compute_ctl_temp_override.conf in pgdata_dir
     113             221 : pub fn compute_ctl_temp_override_remove(pgdata_path: &Path) -> Result<()> {
     114             221 :     let path = pgdata_path.join("compute_ctl_temp_override.conf");
     115             221 :     std::fs::remove_file(path)?;
     116             221 :     Ok(())
     117             221 : }
        

Generated by: LCOV version 2.1-beta