LCOV - code coverage report
Current view: top level - compute_tools/src/bin - fast_import.rs (source / functions) Coverage Total Hit
Test: d427f6f28cacba33c85e3c88c25b596f8e14b0f1.info Lines: 0.0 % 447 0
Test Date: 2025-02-15 14:15:22 Functions: 0.0 % 46 0

            Line data    Source code
       1              : //! This program dumps a remote Postgres database into a local Postgres database
       2              : //! and uploads the resulting PGDATA into object storage for import into a Timeline.
       3              : //!
       4              : //! # Context, Architecture, Design
       5              : //!
       6              : //! See cloud.git Fast Imports RFC (<https://github.com/neondatabase/cloud/pull/19799>)
       7              : //! for the full picture.
       8              : //! The RFC describing the storage pieces of importing the PGDATA dump into a Timeline
       9              : //! is publicly accessible at <https://github.com/neondatabase/neon/pull/9538>.
      10              : //!
      11              : //! # This is a Prototype!
      12              : //!
      13              : //! This program is part of a prototype feature and not yet used in production.
      14              : //!
      15              : //! The cloud.git RFC contains lots of suggestions for improving e2e throughput
      16              : //! of this step of the timeline import process.
      17              : //!
      18              : //! # Local Testing
      19              : //!
      20              : //! - Comment out most of the pgxns in compute-node.Dockerfile to speed up the build.
      21              : //! - Build the image with the following command:
      22              : //!
      23              : //! ```bash
      24              : //! docker buildx build --platform linux/amd64 --build-arg DEBIAN_VERSION=bullseye --build-arg GIT_VERSION=local --build-arg PG_VERSION=v14 --build-arg BUILD_TAG="$(date --iso-8601=s -u)" -t localhost:3030/localregistry/compute-node-v14:latest -f compute/compute-node.Dockerfile .
      25              : //! docker push localhost:3030/localregistry/compute-node-v14:latest
      26              : //! ```
      27              : 
      28              : use anyhow::{bail, Context};
      29              : use aws_config::BehaviorVersion;
      30              : use camino::{Utf8Path, Utf8PathBuf};
      31              : use clap::{Parser, Subcommand};
      32              : use compute_tools::extension_server::{get_pg_version, PostgresMajorVersion};
      33              : use nix::unistd::Pid;
      34              : use tracing::{error, info, info_span, warn, Instrument};
      35              : use utils::fs_ext::is_directory_empty;
      36              : 
      37              : #[path = "fast_import/aws_s3_sync.rs"]
      38              : mod aws_s3_sync;
      39              : #[path = "fast_import/child_stdio_to_log.rs"]
      40              : mod child_stdio_to_log;
      41              : #[path = "fast_import/s3_uri.rs"]
      42              : mod s3_uri;
      43              : 
      44              : const PG_WAIT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(600);
      45              : const PG_WAIT_RETRY_INTERVAL: std::time::Duration = std::time::Duration::from_millis(300);
      46              : 
      47              : #[derive(Subcommand, Debug)]
      48              : enum Command {
      49              :     /// Runs local postgres (neon binary), restores into it,
      50              :     /// uploads pgdata to s3 to be consumed by pageservers
      51              :     Pgdata {
      52              :         /// Raw connection string to the source database. Used only in tests,
      53              :         /// real scenario uses encrypted connection string in spec.json from s3.
      54              :         #[clap(long)]
      55              :         source_connection_string: Option<String>,
      56              :         /// If specified, will not shut down the local postgres after the import. Used in local testing
      57              :         #[clap(short, long)]
      58            0 :         interactive: bool,
      59              :         /// Port to run postgres on. Default is 5432.
      60            0 :         #[clap(long, default_value_t = 5432)]
      61            0 :         pg_port: u16, // port to run postgres on, 5432 is default
      62              : 
      63              :         /// Number of CPUs in the system. This is used to configure # of
      64              :         /// parallel worker processes, for index creation.
      65              :         #[clap(long, env = "NEON_IMPORTER_NUM_CPUS")]
      66              :         num_cpus: Option<usize>,
      67              : 
      68              :         /// Amount of RAM in the system. This is used to configure shared_buffers
      69              :         /// and maintenance_work_mem.
      70              :         #[clap(long, env = "NEON_IMPORTER_MEMORY_MB")]
      71              :         memory_mb: Option<usize>,
      72              :     },
      73              : 
      74              :     /// Runs pg_dump-pg_restore from source to destination without running local postgres.
      75              :     DumpRestore {
      76              :         /// Raw connection string to the source database. Used only in tests,
      77              :         /// real scenario uses encrypted connection string in spec.json from s3.
      78              :         #[clap(long)]
      79              :         source_connection_string: Option<String>,
      80              :         /// Raw connection string to the destination database. Used only in tests,
      81              :         /// real scenario uses encrypted connection string in spec.json from s3.
      82              :         #[clap(long)]
      83              :         destination_connection_string: Option<String>,
      84              :     },
      85              : }
      86              : 
      87              : #[derive(clap::Parser)]
      88              : struct Args {
      89              :     #[clap(long, env = "NEON_IMPORTER_WORKDIR")]
      90            0 :     working_directory: Utf8PathBuf,
      91              :     #[clap(long, env = "NEON_IMPORTER_S3_PREFIX")]
      92              :     s3_prefix: Option<s3_uri::S3Uri>,
      93              :     #[clap(long, env = "NEON_IMPORTER_PG_BIN_DIR")]
      94            0 :     pg_bin_dir: Utf8PathBuf,
      95              :     #[clap(long, env = "NEON_IMPORTER_PG_LIB_DIR")]
      96            0 :     pg_lib_dir: Utf8PathBuf,
      97              : 
      98              :     #[clap(subcommand)]
      99              :     command: Command,
     100              : }
     101              : 
     102              : #[serde_with::serde_as]
     103            0 : #[derive(serde::Deserialize)]
     104              : struct Spec {
     105              :     encryption_secret: EncryptionSecret,
     106              :     #[serde_as(as = "serde_with::base64::Base64")]
     107              :     source_connstring_ciphertext_base64: Vec<u8>,
     108              :     #[serde_as(as = "Option<serde_with::base64::Base64>")]
     109              :     destination_connstring_ciphertext_base64: Option<Vec<u8>>,
     110              : }
     111              : 
     112            0 : #[derive(serde::Deserialize)]
     113              : enum EncryptionSecret {
     114              :     #[allow(clippy::upper_case_acronyms)]
     115              :     KMS { key_id: String },
     116              : }
     117              : 
     118              : // copied from pageserver_api::config::defaults::DEFAULT_LOCALE to avoid dependency just for a constant
     119              : const DEFAULT_LOCALE: &str = if cfg!(target_os = "macos") {
     120              :     "C"
     121              : } else {
     122              :     "C.UTF-8"
     123              : };
     124              : 
     125            0 : async fn decode_connstring(
     126            0 :     kms_client: &aws_sdk_kms::Client,
     127            0 :     key_id: &String,
     128            0 :     connstring_ciphertext_base64: Vec<u8>,
     129            0 : ) -> Result<String, anyhow::Error> {
     130            0 :     let mut output = kms_client
     131            0 :         .decrypt()
     132            0 :         .key_id(key_id)
     133            0 :         .ciphertext_blob(aws_sdk_s3::primitives::Blob::new(
     134            0 :             connstring_ciphertext_base64,
     135            0 :         ))
     136            0 :         .send()
     137            0 :         .await
     138            0 :         .context("decrypt connection string")?;
     139              : 
     140            0 :     let plaintext = output
     141            0 :         .plaintext
     142            0 :         .take()
     143            0 :         .context("get plaintext connection string")?;
     144              : 
     145            0 :     String::from_utf8(plaintext.into_inner()).context("parse connection string as utf8")
     146            0 : }
     147              : 
     148              : struct PostgresProcess {
     149              :     pgdata_dir: Utf8PathBuf,
     150              :     pg_bin_dir: Utf8PathBuf,
     151              :     pgbin: Utf8PathBuf,
     152              :     pg_lib_dir: Utf8PathBuf,
     153              :     postgres_proc: Option<tokio::process::Child>,
     154              : }
     155              : 
     156              : impl PostgresProcess {
     157            0 :     fn new(pgdata_dir: Utf8PathBuf, pg_bin_dir: Utf8PathBuf, pg_lib_dir: Utf8PathBuf) -> Self {
     158            0 :         Self {
     159            0 :             pgdata_dir,
     160            0 :             pgbin: pg_bin_dir.join("postgres"),
     161            0 :             pg_bin_dir,
     162            0 :             pg_lib_dir,
     163            0 :             postgres_proc: None,
     164            0 :         }
     165            0 :     }
     166              : 
     167            0 :     async fn prepare(&self, initdb_user: &str) -> Result<(), anyhow::Error> {
     168            0 :         tokio::fs::create_dir(&self.pgdata_dir)
     169            0 :             .await
     170            0 :             .context("create pgdata directory")?;
     171              : 
     172            0 :         let pg_version = match get_pg_version(self.pgbin.as_ref()) {
     173            0 :             PostgresMajorVersion::V14 => 14,
     174            0 :             PostgresMajorVersion::V15 => 15,
     175            0 :             PostgresMajorVersion::V16 => 16,
     176            0 :             PostgresMajorVersion::V17 => 17,
     177              :         };
     178            0 :         postgres_initdb::do_run_initdb(postgres_initdb::RunInitdbArgs {
     179            0 :             superuser: initdb_user,
     180            0 :             locale: DEFAULT_LOCALE, // XXX: this shouldn't be hard-coded,
     181            0 :             pg_version,
     182            0 :             initdb_bin: self.pg_bin_dir.join("initdb").as_ref(),
     183            0 :             library_search_path: &self.pg_lib_dir, // TODO: is this right? Prob works in compute image, not sure about neon_local.
     184            0 :             pgdata: &self.pgdata_dir,
     185            0 :         })
     186            0 :         .await
     187            0 :         .context("initdb")
     188            0 :     }
     189              : 
     190            0 :     async fn start(
     191            0 :         &mut self,
     192            0 :         initdb_user: &str,
     193            0 :         port: u16,
     194            0 :         nproc: usize,
     195            0 :         memory_mb: usize,
     196            0 :     ) -> Result<&tokio::process::Child, anyhow::Error> {
     197            0 :         self.prepare(initdb_user).await?;
     198              : 
     199              :         // Somewhat arbitrarily, use 10 % of memory for shared buffer cache, 70% for
     200              :         // maintenance_work_mem (i.e. for sorting during index creation), and leave the rest
     201              :         // available for misc other stuff that PostgreSQL uses memory for.
     202            0 :         let shared_buffers_mb = ((memory_mb as f32) * 0.10) as usize;
     203            0 :         let maintenance_work_mem_mb = ((memory_mb as f32) * 0.70) as usize;
     204              : 
     205              :         //
     206              :         // Launch postgres process
     207              :         //
     208            0 :         let mut proc = tokio::process::Command::new(&self.pgbin)
     209            0 :             .arg("-D")
     210            0 :             .arg(&self.pgdata_dir)
     211            0 :             .args(["-p", &format!("{port}")])
     212            0 :             .args(["-c", "wal_level=minimal"])
     213            0 :             .args(["-c", &format!("shared_buffers={shared_buffers_mb}MB")])
     214            0 :             .args(["-c", "shared_buffers=10GB"])
     215            0 :             .args(["-c", "max_wal_senders=0"])
     216            0 :             .args(["-c", "fsync=off"])
     217            0 :             .args(["-c", "full_page_writes=off"])
     218            0 :             .args(["-c", "synchronous_commit=off"])
     219            0 :             .args([
     220            0 :                 "-c",
     221            0 :                 &format!("maintenance_work_mem={maintenance_work_mem_mb}MB"),
     222            0 :             ])
     223            0 :             .args(["-c", &format!("max_parallel_maintenance_workers={nproc}")])
     224            0 :             .args(["-c", &format!("max_parallel_workers={nproc}")])
     225            0 :             .args(["-c", &format!("max_parallel_workers_per_gather={nproc}")])
     226            0 :             .args(["-c", &format!("max_worker_processes={nproc}")])
     227            0 :             .args(["-c", "effective_io_concurrency=100"])
     228            0 :             .env_clear()
     229            0 :             .env("LD_LIBRARY_PATH", &self.pg_lib_dir)
     230            0 :             .env(
     231            0 :                 "ASAN_OPTIONS",
     232            0 :                 std::env::var("ASAN_OPTIONS").unwrap_or_default(),
     233            0 :             )
     234            0 :             .env(
     235            0 :                 "UBSAN_OPTIONS",
     236            0 :                 std::env::var("UBSAN_OPTIONS").unwrap_or_default(),
     237            0 :             )
     238            0 :             .stdout(std::process::Stdio::piped())
     239            0 :             .stderr(std::process::Stdio::piped())
     240            0 :             .spawn()
     241            0 :             .context("spawn postgres")?;
     242              : 
     243            0 :         info!("spawned postgres, waiting for it to become ready");
     244              :         tokio::spawn(
     245            0 :             child_stdio_to_log::relay_process_output(proc.stdout.take(), proc.stderr.take())
     246            0 :                 .instrument(info_span!("postgres")),
     247              :         );
     248              : 
     249            0 :         self.postgres_proc = Some(proc);
     250            0 :         Ok(self.postgres_proc.as_ref().unwrap())
     251            0 :     }
     252              : 
     253            0 :     async fn shutdown(&mut self) -> Result<(), anyhow::Error> {
     254            0 :         let proc: &mut tokio::process::Child = self.postgres_proc.as_mut().unwrap();
     255            0 :         info!("shutdown postgres");
     256            0 :         nix::sys::signal::kill(
     257            0 :             Pid::from_raw(i32::try_from(proc.id().unwrap()).expect("convert child pid to i32")),
     258            0 :             nix::sys::signal::SIGTERM,
     259            0 :         )
     260            0 :         .context("signal postgres to shut down")?;
     261            0 :         proc.wait()
     262            0 :             .await
     263            0 :             .context("wait for postgres to shut down")
     264            0 :             .map(|_| ())
     265            0 :     }
     266              : }
     267              : 
     268            0 : async fn wait_until_ready(connstring: String, create_dbname: String) {
     269            0 :     // Create neondb database in the running postgres
     270            0 :     let start_time = std::time::Instant::now();
     271              : 
     272              :     loop {
     273            0 :         if start_time.elapsed() > PG_WAIT_TIMEOUT {
     274            0 :             error!(
     275            0 :                 "timeout exceeded: failed to poll postgres and create database within 10 minutes"
     276              :             );
     277            0 :             std::process::exit(1);
     278            0 :         }
     279            0 : 
     280            0 :         match tokio_postgres::connect(
     281            0 :             &connstring.replace("dbname=neondb", "dbname=postgres"),
     282            0 :             tokio_postgres::NoTls,
     283            0 :         )
     284            0 :         .await
     285              :         {
     286            0 :             Ok((client, connection)) => {
     287            0 :                 // Spawn the connection handling task to maintain the connection
     288            0 :                 tokio::spawn(async move {
     289            0 :                     if let Err(e) = connection.await {
     290            0 :                         warn!("connection error: {}", e);
     291            0 :                     }
     292            0 :                 });
     293            0 : 
     294            0 :                 match client
     295            0 :                     .simple_query(format!("CREATE DATABASE {create_dbname};").as_str())
     296            0 :                     .await
     297              :                 {
     298              :                     Ok(_) => {
     299            0 :                         info!("created {} database", create_dbname);
     300            0 :                         break;
     301              :                     }
     302            0 :                     Err(e) => {
     303            0 :                         warn!(
     304            0 :                             "failed to create database: {}, retying in {}s",
     305            0 :                             e,
     306            0 :                             PG_WAIT_RETRY_INTERVAL.as_secs_f32()
     307              :                         );
     308            0 :                         tokio::time::sleep(PG_WAIT_RETRY_INTERVAL).await;
     309            0 :                         continue;
     310              :                     }
     311              :                 }
     312              :             }
     313              :             Err(_) => {
     314            0 :                 info!(
     315            0 :                     "postgres not ready yet, retrying in {}s",
     316            0 :                     PG_WAIT_RETRY_INTERVAL.as_secs_f32()
     317              :                 );
     318            0 :                 tokio::time::sleep(PG_WAIT_RETRY_INTERVAL).await;
     319            0 :                 continue;
     320              :             }
     321              :         }
     322              :     }
     323            0 : }
     324              : 
     325            0 : async fn run_dump_restore(
     326            0 :     workdir: Utf8PathBuf,
     327            0 :     pg_bin_dir: Utf8PathBuf,
     328            0 :     pg_lib_dir: Utf8PathBuf,
     329            0 :     source_connstring: String,
     330            0 :     destination_connstring: String,
     331            0 : ) -> Result<(), anyhow::Error> {
     332            0 :     let dumpdir = workdir.join("dumpdir");
     333            0 : 
     334            0 :     let common_args = [
     335            0 :         // schema mapping (prob suffices to specify them on one side)
     336            0 :         "--no-owner".to_string(),
     337            0 :         "--no-privileges".to_string(),
     338            0 :         "--no-publications".to_string(),
     339            0 :         "--no-security-labels".to_string(),
     340            0 :         "--no-subscriptions".to_string(),
     341            0 :         "--no-tablespaces".to_string(),
     342            0 :         // format
     343            0 :         "--format".to_string(),
     344            0 :         "directory".to_string(),
     345            0 :         // concurrency
     346            0 :         "--jobs".to_string(),
     347            0 :         num_cpus::get().to_string(),
     348            0 :         // progress updates
     349            0 :         "--verbose".to_string(),
     350            0 :     ];
     351            0 : 
     352            0 :     info!("dump into the working directory");
     353              :     {
     354            0 :         let mut pg_dump = tokio::process::Command::new(pg_bin_dir.join("pg_dump"))
     355            0 :             .args(&common_args)
     356            0 :             .arg("-f")
     357            0 :             .arg(&dumpdir)
     358            0 :             .arg("--no-sync")
     359            0 :             // POSITIONAL args
     360            0 :             // source db (db name included in connection string)
     361            0 :             .arg(&source_connstring)
     362            0 :             // how we run it
     363            0 :             .env_clear()
     364            0 :             .env("LD_LIBRARY_PATH", &pg_lib_dir)
     365            0 :             .kill_on_drop(true)
     366            0 :             .stdout(std::process::Stdio::piped())
     367            0 :             .stderr(std::process::Stdio::piped())
     368            0 :             .spawn()
     369            0 :             .context("spawn pg_dump")?;
     370              : 
     371            0 :         info!(pid=%pg_dump.id().unwrap(), "spawned pg_dump");
     372              : 
     373              :         tokio::spawn(
     374            0 :             child_stdio_to_log::relay_process_output(pg_dump.stdout.take(), pg_dump.stderr.take())
     375            0 :                 .instrument(info_span!("pg_dump")),
     376              :         );
     377              : 
     378            0 :         let st = pg_dump.wait().await.context("wait for pg_dump")?;
     379            0 :         info!(status=?st, "pg_dump exited");
     380            0 :         if !st.success() {
     381            0 :             error!(status=%st, "pg_dump failed, restore will likely fail as well");
     382            0 :             bail!("pg_dump failed");
     383            0 :         }
     384              :     }
     385              : 
     386              :     // TODO: maybe do it in a streaming way, plenty of internal research done on this already
     387              :     // TODO: do the unlogged table trick
     388              :     {
     389            0 :         let mut pg_restore = tokio::process::Command::new(pg_bin_dir.join("pg_restore"))
     390            0 :             .args(&common_args)
     391            0 :             .arg("-d")
     392            0 :             .arg(&destination_connstring)
     393            0 :             // POSITIONAL args
     394            0 :             .arg(&dumpdir)
     395            0 :             // how we run it
     396            0 :             .env_clear()
     397            0 :             .env("LD_LIBRARY_PATH", &pg_lib_dir)
     398            0 :             .kill_on_drop(true)
     399            0 :             .stdout(std::process::Stdio::piped())
     400            0 :             .stderr(std::process::Stdio::piped())
     401            0 :             .spawn()
     402            0 :             .context("spawn pg_restore")?;
     403              : 
     404            0 :         info!(pid=%pg_restore.id().unwrap(), "spawned pg_restore");
     405              :         tokio::spawn(
     406            0 :             child_stdio_to_log::relay_process_output(
     407            0 :                 pg_restore.stdout.take(),
     408            0 :                 pg_restore.stderr.take(),
     409            0 :             )
     410            0 :             .instrument(info_span!("pg_restore")),
     411              :         );
     412            0 :         let st = pg_restore.wait().await.context("wait for pg_restore")?;
     413            0 :         info!(status=?st, "pg_restore exited");
     414            0 :         if !st.success() {
     415            0 :             error!(status=%st, "pg_restore failed, restore will likely fail as well");
     416            0 :             bail!("pg_restore failed");
     417            0 :         }
     418            0 :     }
     419            0 : 
     420            0 :     Ok(())
     421            0 : }
     422              : 
     423              : #[allow(clippy::too_many_arguments)]
     424            0 : async fn cmd_pgdata(
     425            0 :     kms_client: Option<aws_sdk_kms::Client>,
     426            0 :     maybe_s3_prefix: Option<s3_uri::S3Uri>,
     427            0 :     maybe_spec: Option<Spec>,
     428            0 :     source_connection_string: Option<String>,
     429            0 :     interactive: bool,
     430            0 :     pg_port: u16,
     431            0 :     workdir: Utf8PathBuf,
     432            0 :     pg_bin_dir: Utf8PathBuf,
     433            0 :     pg_lib_dir: Utf8PathBuf,
     434            0 :     num_cpus: Option<usize>,
     435            0 :     memory_mb: Option<usize>,
     436            0 : ) -> Result<(), anyhow::Error> {
     437            0 :     if maybe_spec.is_none() && source_connection_string.is_none() {
     438            0 :         bail!("spec must be provided for pgdata command");
     439            0 :     }
     440            0 :     if maybe_spec.is_some() && source_connection_string.is_some() {
     441            0 :         bail!("only one of spec or source_connection_string can be provided");
     442            0 :     }
     443              : 
     444            0 :     let source_connection_string = if let Some(spec) = maybe_spec {
     445            0 :         match spec.encryption_secret {
     446            0 :             EncryptionSecret::KMS { key_id } => {
     447            0 :                 decode_connstring(
     448            0 :                     kms_client.as_ref().unwrap(),
     449            0 :                     &key_id,
     450            0 :                     spec.source_connstring_ciphertext_base64,
     451            0 :                 )
     452            0 :                 .await?
     453              :             }
     454              :         }
     455              :     } else {
     456            0 :         source_connection_string.unwrap()
     457              :     };
     458              : 
     459            0 :     let superuser = "cloud_admin";
     460            0 :     let destination_connstring = format!(
     461            0 :         "host=localhost port={} user={} dbname=neondb",
     462            0 :         pg_port, superuser
     463            0 :     );
     464            0 : 
     465            0 :     let pgdata_dir = workdir.join("pgdata");
     466            0 :     let mut proc = PostgresProcess::new(pgdata_dir.clone(), pg_bin_dir.clone(), pg_lib_dir.clone());
     467            0 :     let nproc = num_cpus.unwrap_or_else(num_cpus::get);
     468            0 :     let memory_mb = memory_mb.unwrap_or(256);
     469            0 :     proc.start(superuser, pg_port, nproc, memory_mb).await?;
     470            0 :     wait_until_ready(destination_connstring.clone(), "neondb".to_string()).await;
     471              : 
     472            0 :     run_dump_restore(
     473            0 :         workdir.clone(),
     474            0 :         pg_bin_dir,
     475            0 :         pg_lib_dir,
     476            0 :         source_connection_string,
     477            0 :         destination_connstring,
     478            0 :     )
     479            0 :     .await?;
     480              : 
     481              :     // If interactive mode, wait for Ctrl+C
     482            0 :     if interactive {
     483            0 :         info!("Running in interactive mode. Press Ctrl+C to shut down.");
     484            0 :         tokio::signal::ctrl_c().await.context("wait for ctrl-c")?;
     485            0 :     }
     486              : 
     487            0 :     proc.shutdown().await?;
     488              : 
     489              :     // Only sync if s3_prefix was specified
     490            0 :     if let Some(s3_prefix) = maybe_s3_prefix {
     491            0 :         info!("upload pgdata");
     492            0 :         aws_s3_sync::sync(Utf8Path::new(&pgdata_dir), &s3_prefix.append("/pgdata/"))
     493            0 :             .await
     494            0 :             .context("sync dump directory to destination")?;
     495              : 
     496            0 :         info!("write status");
     497              :         {
     498            0 :             let status_dir = workdir.join("status");
     499            0 :             std::fs::create_dir(&status_dir).context("create status directory")?;
     500            0 :             let status_file = status_dir.join("pgdata");
     501            0 :             std::fs::write(&status_file, serde_json::json!({"done": true}).to_string())
     502            0 :                 .context("write status file")?;
     503            0 :             aws_s3_sync::sync(&status_dir, &s3_prefix.append("/status/"))
     504            0 :                 .await
     505            0 :                 .context("sync status directory to destination")?;
     506              :         }
     507            0 :     }
     508              : 
     509            0 :     Ok(())
     510            0 : }
     511              : 
     512            0 : async fn cmd_dumprestore(
     513            0 :     kms_client: Option<aws_sdk_kms::Client>,
     514            0 :     maybe_spec: Option<Spec>,
     515            0 :     source_connection_string: Option<String>,
     516            0 :     destination_connection_string: Option<String>,
     517            0 :     workdir: Utf8PathBuf,
     518            0 :     pg_bin_dir: Utf8PathBuf,
     519            0 :     pg_lib_dir: Utf8PathBuf,
     520            0 : ) -> Result<(), anyhow::Error> {
     521            0 :     let (source_connstring, destination_connstring) = if let Some(spec) = maybe_spec {
     522            0 :         match spec.encryption_secret {
     523            0 :             EncryptionSecret::KMS { key_id } => {
     524            0 :                 let source = decode_connstring(
     525            0 :                     kms_client.as_ref().unwrap(),
     526            0 :                     &key_id,
     527            0 :                     spec.source_connstring_ciphertext_base64,
     528            0 :                 )
     529            0 :                 .await?;
     530              : 
     531            0 :                 let dest = if let Some(dest_ciphertext) =
     532            0 :                     spec.destination_connstring_ciphertext_base64
     533              :                 {
     534            0 :                     decode_connstring(kms_client.as_ref().unwrap(), &key_id, dest_ciphertext)
     535            0 :                         .await?
     536              :                 } else {
     537            0 :                     bail!("destination connection string must be provided in spec for dump_restore command");
     538              :                 };
     539              : 
     540            0 :                 (source, dest)
     541              :             }
     542              :         }
     543              :     } else {
     544              :         (
     545            0 :             source_connection_string.unwrap(),
     546            0 :             if let Some(val) = destination_connection_string {
     547            0 :                 val
     548              :             } else {
     549            0 :                 bail!("destination connection string must be provided for dump_restore command");
     550              :             },
     551              :         )
     552              :     };
     553              : 
     554            0 :     run_dump_restore(
     555            0 :         workdir,
     556            0 :         pg_bin_dir,
     557            0 :         pg_lib_dir,
     558            0 :         source_connstring,
     559            0 :         destination_connstring,
     560            0 :     )
     561            0 :     .await
     562            0 : }
     563              : 
     564              : #[tokio::main]
     565            0 : pub(crate) async fn main() -> anyhow::Result<()> {
     566            0 :     utils::logging::init(
     567            0 :         utils::logging::LogFormat::Json,
     568            0 :         utils::logging::TracingErrorLayerEnablement::EnableWithRustLogFilter,
     569            0 :         utils::logging::Output::Stdout,
     570            0 :     )?;
     571            0 : 
     572            0 :     info!("starting");
     573            0 : 
     574            0 :     let args = Args::parse();
     575            0 : 
     576            0 :     // Initialize AWS clients only if s3_prefix is specified
     577            0 :     let (aws_config, kms_client) = if args.s3_prefix.is_some() {
     578            0 :         let config = aws_config::load_defaults(BehaviorVersion::v2024_03_28()).await;
     579            0 :         let kms = aws_sdk_kms::Client::new(&config);
     580            0 :         (Some(config), Some(kms))
     581            0 :     } else {
     582            0 :         (None, None)
     583            0 :     };
     584            0 : 
     585            0 :     let spec: Option<Spec> = if let Some(s3_prefix) = &args.s3_prefix {
     586            0 :         let spec_key = s3_prefix.append("/spec.json");
     587            0 :         let s3_client = aws_sdk_s3::Client::new(aws_config.as_ref().unwrap());
     588            0 :         let object = s3_client
     589            0 :             .get_object()
     590            0 :             .bucket(&spec_key.bucket)
     591            0 :             .key(spec_key.key)
     592            0 :             .send()
     593            0 :             .await
     594            0 :             .context("get spec from s3")?
     595            0 :             .body
     596            0 :             .collect()
     597            0 :             .await
     598            0 :             .context("download spec body")?;
     599            0 :         serde_json::from_slice(&object.into_bytes()).context("parse spec as json")?
     600            0 :     } else {
     601            0 :         None
     602            0 :     };
     603            0 : 
     604            0 :     match tokio::fs::create_dir(&args.working_directory).await {
     605            0 :         Ok(()) => {}
     606            0 :         Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
     607            0 :             if !is_directory_empty(&args.working_directory)
     608            0 :                 .await
     609            0 :                 .context("check if working directory is empty")?
     610            0 :             {
     611            0 :                 bail!("working directory is not empty");
     612            0 :             } else {
     613            0 :                 // ok
     614            0 :             }
     615            0 :         }
     616            0 :         Err(e) => return Err(anyhow::Error::new(e).context("create working directory")),
     617            0 :     }
     618            0 : 
     619            0 :     match args.command {
     620            0 :         Command::Pgdata {
     621            0 :             source_connection_string,
     622            0 :             interactive,
     623            0 :             pg_port,
     624            0 :             num_cpus,
     625            0 :             memory_mb,
     626            0 :         } => {
     627            0 :             cmd_pgdata(
     628            0 :                 kms_client,
     629            0 :                 args.s3_prefix,
     630            0 :                 spec,
     631            0 :                 source_connection_string,
     632            0 :                 interactive,
     633            0 :                 pg_port,
     634            0 :                 args.working_directory,
     635            0 :                 args.pg_bin_dir,
     636            0 :                 args.pg_lib_dir,
     637            0 :                 num_cpus,
     638            0 :                 memory_mb,
     639            0 :             )
     640            0 :             .await?;
     641            0 :         }
     642            0 :         Command::DumpRestore {
     643            0 :             source_connection_string,
     644            0 :             destination_connection_string,
     645            0 :         } => {
     646            0 :             cmd_dumprestore(
     647            0 :                 kms_client,
     648            0 :                 spec,
     649            0 :                 source_connection_string,
     650            0 :                 destination_connection_string,
     651            0 :                 args.working_directory,
     652            0 :                 args.pg_bin_dir,
     653            0 :                 args.pg_lib_dir,
     654            0 :             )
     655            0 :             .await?;
     656            0 :         }
     657            0 :     }
     658            0 : 
     659            0 :     Ok(())
     660            0 : }
        

Generated by: LCOV version 2.1-beta