Line data Source code
1 : //!
2 : //! Generate a tarball with files needed to bootstrap ComputeNode.
3 : //!
4 : //! TODO: this module has nothing to do with PostgreSQL pg_basebackup.
5 : //! It could use a better name.
6 : //!
7 : //! Stateless Postgres compute node is launched by sending a tarball
8 : //! which contains non-relational data (multixacts, clog, filenodemaps, twophase files),
9 : //! generated pg_control and dummy segment of WAL.
10 : //! This module is responsible for creation of such tarball
11 : //! from data stored in object storage.
12 : //!
13 : use anyhow::{anyhow, Context};
14 : use bytes::{BufMut, Bytes, BytesMut};
15 : use fail::fail_point;
16 : use pageserver_api::key::{key_to_slru_block, Key};
17 : use postgres_ffi::pg_constants;
18 : use std::fmt::Write as FmtWrite;
19 : use std::time::SystemTime;
20 : use tokio::io;
21 : use tokio::io::AsyncWrite;
22 : use tracing::*;
23 :
24 : use tokio_tar::{Builder, EntryType, Header};
25 :
26 : use crate::context::RequestContext;
27 : use crate::pgdatadir_mapping::Version;
28 : use crate::tenant::Timeline;
29 : use pageserver_api::reltag::{RelTag, SlruKind};
30 :
31 : use postgres_ffi::dispatch_pgversion;
32 : use postgres_ffi::pg_constants::{DEFAULTTABLESPACE_OID, GLOBALTABLESPACE_OID};
33 : use postgres_ffi::pg_constants::{PGDATA_SPECIAL_FILES, PGDATA_SUBDIRS, PG_HBA};
34 : use postgres_ffi::relfile_utils::{INIT_FORKNUM, MAIN_FORKNUM};
35 : use postgres_ffi::TransactionId;
36 : use postgres_ffi::XLogFileName;
37 : use postgres_ffi::PG_TLI;
38 : use postgres_ffi::{BLCKSZ, RELSEG_SIZE, WAL_SEGMENT_SIZE};
39 : use utils::lsn::Lsn;
40 :
41 0 : #[derive(Debug, thiserror::Error)]
42 : pub enum BasebackupError {
43 : #[error("basebackup pageserver error {0:#}")]
44 : Server(#[from] anyhow::Error),
45 : #[error("basebackup client error {0:#}")]
46 : Client(#[source] io::Error),
47 : }
48 :
49 : /// Create basebackup with non-rel data in it.
50 : /// Only include relational data if 'full_backup' is true.
51 : ///
52 : /// Currently we use empty 'req_lsn' in two cases:
53 : /// * During the basebackup right after timeline creation
54 : /// * When working without safekeepers. In this situation it is important to match the lsn
55 : /// we are taking basebackup on with the lsn that is used in pageserver's walreceiver
56 : /// to start the replication.
57 0 : pub async fn send_basebackup_tarball<'a, W>(
58 0 : write: &'a mut W,
59 0 : timeline: &'a Timeline,
60 0 : req_lsn: Option<Lsn>,
61 0 : prev_lsn: Option<Lsn>,
62 0 : full_backup: bool,
63 0 : ctx: &'a RequestContext,
64 0 : ) -> Result<(), BasebackupError>
65 0 : where
66 0 : W: AsyncWrite + Send + Sync + Unpin,
67 0 : {
68 : // Compute postgres doesn't have any previous WAL files, but the first
69 : // record that it's going to write needs to include the LSN of the
70 : // previous record (xl_prev). We include prev_record_lsn in the
71 : // "zenith.signal" file, so that postgres can read it during startup.
72 : //
73 : // We don't keep full history of record boundaries in the page server,
74 : // however, only the predecessor of the latest record on each
75 : // timeline. So we can only provide prev_record_lsn when you take a
76 : // base backup at the end of the timeline, i.e. at last_record_lsn.
77 : // Even at the end of the timeline, we sometimes don't have a valid
78 : // prev_lsn value; that happens if the timeline was just branched from
79 : // an old LSN and it doesn't have any WAL of its own yet. We will set
80 : // prev_lsn to Lsn(0) if we cannot provide the correct value.
81 0 : let (backup_prev, backup_lsn) = if let Some(req_lsn) = req_lsn {
82 : // Backup was requested at a particular LSN. The caller should've
83 : // already checked that it's a valid LSN.
84 :
85 : // If the requested point is the end of the timeline, we can
86 : // provide prev_lsn. (get_last_record_rlsn() might return it as
87 : // zero, though, if no WAL has been generated on this timeline
88 : // yet.)
89 0 : let end_of_timeline = timeline.get_last_record_rlsn();
90 0 : if req_lsn == end_of_timeline.last {
91 0 : (end_of_timeline.prev, req_lsn)
92 : } else {
93 0 : (Lsn(0), req_lsn)
94 : }
95 : } else {
96 : // Backup was requested at end of the timeline.
97 0 : let end_of_timeline = timeline.get_last_record_rlsn();
98 0 : (end_of_timeline.prev, end_of_timeline.last)
99 : };
100 :
101 : // Consolidate the derived and the provided prev_lsn values
102 0 : let prev_lsn = if let Some(provided_prev_lsn) = prev_lsn {
103 0 : if backup_prev != Lsn(0) && backup_prev != provided_prev_lsn {
104 0 : return Err(BasebackupError::Server(anyhow!(
105 0 : "backup_prev {backup_prev} != provided_prev_lsn {provided_prev_lsn}"
106 0 : )));
107 0 : }
108 0 : provided_prev_lsn
109 : } else {
110 0 : backup_prev
111 : };
112 :
113 0 : info!(
114 0 : "taking basebackup lsn={}, prev_lsn={} (full_backup={})",
115 : backup_lsn, prev_lsn, full_backup
116 : );
117 :
118 0 : let basebackup = Basebackup {
119 0 : ar: Builder::new_non_terminated(write),
120 0 : timeline,
121 0 : lsn: backup_lsn,
122 0 : prev_record_lsn: prev_lsn,
123 0 : full_backup,
124 0 : ctx,
125 0 : };
126 0 : basebackup
127 0 : .send_tarball()
128 0 : .instrument(info_span!("send_tarball", backup_lsn=%backup_lsn))
129 0 : .await
130 0 : }
131 :
132 : /// This is short-living object only for the time of tarball creation,
133 : /// created mostly to avoid passing a lot of parameters between various functions
134 : /// used for constructing tarball.
135 : struct Basebackup<'a, W>
136 : where
137 : W: AsyncWrite + Send + Sync + Unpin,
138 : {
139 : ar: Builder<&'a mut W>,
140 : timeline: &'a Timeline,
141 : lsn: Lsn,
142 : prev_record_lsn: Lsn,
143 : full_backup: bool,
144 : ctx: &'a RequestContext,
145 : }
146 :
147 : /// A sink that accepts SLRU blocks ordered by key and forwards
148 : /// full segments to the archive.
149 : struct SlruSegmentsBuilder<'a, 'b, W>
150 : where
151 : W: AsyncWrite + Send + Sync + Unpin,
152 : {
153 : ar: &'a mut Builder<&'b mut W>,
154 : buf: Vec<u8>,
155 : current_segment: Option<(SlruKind, u32)>,
156 : total_blocks: usize,
157 : }
158 :
159 : impl<'a, 'b, W> SlruSegmentsBuilder<'a, 'b, W>
160 : where
161 : W: AsyncWrite + Send + Sync + Unpin,
162 : {
163 0 : fn new(ar: &'a mut Builder<&'b mut W>) -> Self {
164 0 : Self {
165 0 : ar,
166 0 : buf: Vec::new(),
167 0 : current_segment: None,
168 0 : total_blocks: 0,
169 0 : }
170 0 : }
171 :
172 0 : async fn add_block(&mut self, key: &Key, block: Bytes) -> Result<(), BasebackupError> {
173 0 : let (kind, segno, _) = key_to_slru_block(*key)?;
174 :
175 0 : match kind {
176 : SlruKind::Clog => {
177 0 : if !(block.len() == BLCKSZ as usize || block.len() == BLCKSZ as usize + 8) {
178 0 : return Err(BasebackupError::Server(anyhow!(
179 0 : "invalid SlruKind::Clog record: block.len()={}",
180 0 : block.len()
181 0 : )));
182 0 : }
183 : }
184 : SlruKind::MultiXactMembers | SlruKind::MultiXactOffsets => {
185 0 : if block.len() != BLCKSZ as usize {
186 0 : return Err(BasebackupError::Server(anyhow!(
187 0 : "invalid {:?} record: block.len()={}",
188 0 : kind,
189 0 : block.len()
190 0 : )));
191 0 : }
192 : }
193 : }
194 :
195 0 : let segment = (kind, segno);
196 0 : match self.current_segment {
197 0 : None => {
198 0 : self.current_segment = Some(segment);
199 0 : self.buf
200 0 : .extend_from_slice(block.slice(..BLCKSZ as usize).as_ref());
201 0 : }
202 0 : Some(current_seg) if current_seg == segment => {
203 0 : self.buf
204 0 : .extend_from_slice(block.slice(..BLCKSZ as usize).as_ref());
205 0 : }
206 : Some(_) => {
207 0 : self.flush().await?;
208 :
209 0 : self.current_segment = Some(segment);
210 0 : self.buf
211 0 : .extend_from_slice(block.slice(..BLCKSZ as usize).as_ref());
212 : }
213 : }
214 :
215 0 : Ok(())
216 0 : }
217 :
218 0 : async fn flush(&mut self) -> Result<(), BasebackupError> {
219 0 : let nblocks = self.buf.len() / BLCKSZ as usize;
220 0 : let (kind, segno) = self.current_segment.take().unwrap();
221 0 : let segname = format!("{}/{:>04X}", kind.to_str(), segno);
222 0 : let header = new_tar_header(&segname, self.buf.len() as u64)?;
223 0 : self.ar
224 0 : .append(&header, self.buf.as_slice())
225 0 : .await
226 0 : .map_err(BasebackupError::Client)?;
227 :
228 0 : self.total_blocks += nblocks;
229 0 : debug!("Added to basebackup slru {} relsize {}", segname, nblocks);
230 :
231 0 : self.buf.clear();
232 0 :
233 0 : Ok(())
234 0 : }
235 :
236 0 : async fn finish(mut self) -> Result<(), BasebackupError> {
237 0 : let res = if self.current_segment.is_none() || self.buf.is_empty() {
238 0 : Ok(())
239 : } else {
240 0 : self.flush().await
241 : };
242 :
243 0 : info!("Collected {} SLRU blocks", self.total_blocks);
244 :
245 0 : res
246 0 : }
247 : }
248 :
249 : impl<'a, W> Basebackup<'a, W>
250 : where
251 : W: AsyncWrite + Send + Sync + Unpin,
252 : {
253 0 : async fn send_tarball(mut self) -> Result<(), BasebackupError> {
254 : // TODO include checksum
255 :
256 0 : let lazy_slru_download = self.timeline.get_lazy_slru_download() && !self.full_backup;
257 :
258 : // Create pgdata subdirs structure
259 0 : for dir in PGDATA_SUBDIRS.iter() {
260 0 : let header = new_tar_header_dir(dir)?;
261 0 : self.ar
262 0 : .append(&header, &mut io::empty())
263 0 : .await
264 0 : .context("could not add directory to basebackup tarball")?;
265 : }
266 :
267 : // Send config files.
268 0 : for filepath in PGDATA_SPECIAL_FILES.iter() {
269 0 : if *filepath == "pg_hba.conf" {
270 0 : let data = PG_HBA.as_bytes();
271 0 : let header = new_tar_header(filepath, data.len() as u64)?;
272 0 : self.ar
273 0 : .append(&header, data)
274 0 : .await
275 0 : .context("could not add config file to basebackup tarball")?;
276 : } else {
277 0 : let header = new_tar_header(filepath, 0)?;
278 0 : self.ar
279 0 : .append(&header, &mut io::empty())
280 0 : .await
281 0 : .context("could not add config file to basebackup tarball")?;
282 : }
283 : }
284 0 : if !lazy_slru_download {
285 : // Gather non-relational files from object storage pages.
286 0 : let slru_partitions = self
287 0 : .timeline
288 0 : .get_slru_keyspace(Version::Lsn(self.lsn), self.ctx)
289 0 : .await
290 0 : .map_err(|e| BasebackupError::Server(e.into()))?
291 0 : .partition(
292 0 : self.timeline.get_shard_identity(),
293 0 : Timeline::MAX_GET_VECTORED_KEYS * BLCKSZ as u64,
294 0 : );
295 0 :
296 0 : let mut slru_builder = SlruSegmentsBuilder::new(&mut self.ar);
297 :
298 0 : for part in slru_partitions.parts {
299 0 : let blocks = self
300 0 : .timeline
301 0 : .get_vectored(part, self.lsn, self.ctx)
302 0 : .await
303 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
304 :
305 0 : for (key, block) in blocks {
306 0 : let block = block.map_err(|e| BasebackupError::Server(e.into()))?;
307 0 : slru_builder.add_block(&key, block).await?;
308 : }
309 : }
310 0 : slru_builder.finish().await?;
311 0 : }
312 :
313 0 : let mut min_restart_lsn: Lsn = Lsn::MAX;
314 : // Create tablespace directories
315 0 : for ((spcnode, dbnode), has_relmap_file) in self
316 0 : .timeline
317 0 : .list_dbdirs(self.lsn, self.ctx)
318 0 : .await
319 0 : .map_err(|e| BasebackupError::Server(e.into()))?
320 : {
321 0 : self.add_dbdir(spcnode, dbnode, has_relmap_file).await?;
322 :
323 : // If full backup is requested, include all relation files.
324 : // Otherwise only include init forks of unlogged relations.
325 0 : let rels = self
326 0 : .timeline
327 0 : .list_rels(spcnode, dbnode, Version::Lsn(self.lsn), self.ctx)
328 0 : .await
329 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
330 0 : for &rel in rels.iter() {
331 : // Send init fork as main fork to provide well formed empty
332 : // contents of UNLOGGED relations. Postgres copies it in
333 : // `reinit.c` during recovery.
334 0 : if rel.forknum == INIT_FORKNUM {
335 : // I doubt we need _init fork itself, but having it at least
336 : // serves as a marker relation is unlogged.
337 0 : self.add_rel(rel, rel).await?;
338 0 : self.add_rel(rel, rel.with_forknum(MAIN_FORKNUM)).await?;
339 0 : continue;
340 0 : }
341 0 :
342 0 : if self.full_backup {
343 0 : if rel.forknum == MAIN_FORKNUM && rels.contains(&rel.with_forknum(INIT_FORKNUM))
344 : {
345 : // skip this, will include it when we reach the init fork
346 0 : continue;
347 0 : }
348 0 : self.add_rel(rel, rel).await?;
349 0 : }
350 : }
351 :
352 0 : for (path, content) in self
353 0 : .timeline
354 0 : .list_aux_files(self.lsn, self.ctx)
355 0 : .await
356 0 : .map_err(|e| BasebackupError::Server(e.into()))?
357 : {
358 0 : if path.starts_with("pg_replslot") {
359 0 : let offs = pg_constants::REPL_SLOT_ON_DISK_OFFSETOF_RESTART_LSN;
360 0 : let restart_lsn = Lsn(u64::from_le_bytes(
361 0 : content[offs..offs + 8].try_into().unwrap(),
362 0 : ));
363 0 : info!("Replication slot {} restart LSN={}", path, restart_lsn);
364 0 : min_restart_lsn = Lsn::min(min_restart_lsn, restart_lsn);
365 0 : }
366 0 : let header = new_tar_header(&path, content.len() as u64)?;
367 0 : self.ar
368 0 : .append(&header, &*content)
369 0 : .await
370 0 : .context("could not add aux file to basebackup tarball")?;
371 : }
372 : }
373 0 : if min_restart_lsn != Lsn::MAX {
374 0 : info!(
375 0 : "Min restart LSN for logical replication is {}",
376 : min_restart_lsn
377 : );
378 0 : let data = min_restart_lsn.0.to_le_bytes();
379 0 : let header = new_tar_header("restart.lsn", data.len() as u64)?;
380 0 : self.ar
381 0 : .append(&header, &data[..])
382 0 : .await
383 0 : .context("could not add restart.lsn file to basebackup tarball")?;
384 0 : }
385 0 : for xid in self
386 0 : .timeline
387 0 : .list_twophase_files(self.lsn, self.ctx)
388 0 : .await
389 0 : .map_err(|e| BasebackupError::Server(e.into()))?
390 : {
391 0 : self.add_twophase_file(xid).await?;
392 : }
393 :
394 0 : fail_point!("basebackup-before-control-file", |_| {
395 0 : Err(BasebackupError::Server(anyhow!(
396 0 : "failpoint basebackup-before-control-file"
397 0 : )))
398 0 : });
399 :
400 : // Generate pg_control and bootstrap WAL segment.
401 0 : self.add_pgcontrol_file().await?;
402 0 : self.ar.finish().await.map_err(BasebackupError::Client)?;
403 0 : debug!("all tarred up!");
404 0 : Ok(())
405 0 : }
406 :
407 : /// Add contents of relfilenode `src`, naming it as `dst`.
408 0 : async fn add_rel(&mut self, src: RelTag, dst: RelTag) -> Result<(), BasebackupError> {
409 0 : let nblocks = self
410 0 : .timeline
411 0 : .get_rel_size(src, Version::Lsn(self.lsn), self.ctx)
412 0 : .await
413 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
414 :
415 : // If the relation is empty, create an empty file
416 0 : if nblocks == 0 {
417 0 : let file_name = dst.to_segfile_name(0);
418 0 : let header = new_tar_header(&file_name, 0)?;
419 0 : self.ar
420 0 : .append(&header, &mut io::empty())
421 0 : .await
422 0 : .map_err(BasebackupError::Client)?;
423 0 : return Ok(());
424 0 : }
425 0 :
426 0 : // Add a file for each chunk of blocks (aka segment)
427 0 : let mut startblk = 0;
428 0 : let mut seg = 0;
429 0 : while startblk < nblocks {
430 0 : let endblk = std::cmp::min(startblk + RELSEG_SIZE, nblocks);
431 0 :
432 0 : let mut segment_data: Vec<u8> = vec![];
433 0 : for blknum in startblk..endblk {
434 0 : let img = self
435 0 : .timeline
436 0 : .get_rel_page_at_lsn(src, blknum, Version::Lsn(self.lsn), self.ctx)
437 0 : .await
438 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
439 0 : segment_data.extend_from_slice(&img[..]);
440 : }
441 :
442 0 : let file_name = dst.to_segfile_name(seg as u32);
443 0 : let header = new_tar_header(&file_name, segment_data.len() as u64)?;
444 0 : self.ar
445 0 : .append(&header, segment_data.as_slice())
446 0 : .await
447 0 : .map_err(BasebackupError::Client)?;
448 :
449 0 : seg += 1;
450 0 : startblk = endblk;
451 : }
452 :
453 0 : Ok(())
454 0 : }
455 :
456 : //
457 : // Include database/tablespace directories.
458 : //
459 : // Each directory contains a PG_VERSION file, and the default database
460 : // directories also contain pg_filenode.map files.
461 : //
462 0 : async fn add_dbdir(
463 0 : &mut self,
464 0 : spcnode: u32,
465 0 : dbnode: u32,
466 0 : has_relmap_file: bool,
467 0 : ) -> Result<(), BasebackupError> {
468 0 : let relmap_img = if has_relmap_file {
469 0 : let img = self
470 0 : .timeline
471 0 : .get_relmap_file(spcnode, dbnode, Version::Lsn(self.lsn), self.ctx)
472 0 : .await
473 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
474 :
475 0 : if img.len()
476 0 : != dispatch_pgversion!(self.timeline.pg_version, pgv::bindings::SIZEOF_RELMAPFILE)
477 : {
478 0 : return Err(BasebackupError::Server(anyhow!(
479 0 : "img.len() != SIZE_OF_RELMAPFILE, img.len()={}",
480 0 : img.len(),
481 0 : )));
482 0 : }
483 0 :
484 0 : Some(img)
485 : } else {
486 0 : None
487 : };
488 :
489 0 : if spcnode == GLOBALTABLESPACE_OID {
490 0 : let pg_version_str = match self.timeline.pg_version {
491 0 : 14 | 15 => self.timeline.pg_version.to_string(),
492 0 : ver => format!("{ver}\x0A"),
493 : };
494 0 : let header = new_tar_header("PG_VERSION", pg_version_str.len() as u64)?;
495 0 : self.ar
496 0 : .append(&header, pg_version_str.as_bytes())
497 0 : .await
498 0 : .map_err(BasebackupError::Client)?;
499 :
500 0 : info!("timeline.pg_version {}", self.timeline.pg_version);
501 :
502 0 : if let Some(img) = relmap_img {
503 : // filenode map for global tablespace
504 0 : let header = new_tar_header("global/pg_filenode.map", img.len() as u64)?;
505 0 : self.ar
506 0 : .append(&header, &img[..])
507 0 : .await
508 0 : .map_err(BasebackupError::Client)?;
509 : } else {
510 0 : warn!("global/pg_filenode.map is missing");
511 : }
512 : } else {
513 : // User defined tablespaces are not supported. However, as
514 : // a special case, if a tablespace/db directory is
515 : // completely empty, we can leave it out altogether. This
516 : // makes taking a base backup after the 'tablespace'
517 : // regression test pass, because the test drops the
518 : // created tablespaces after the tests.
519 : //
520 : // FIXME: this wouldn't be necessary, if we handled
521 : // XLOG_TBLSPC_DROP records. But we probably should just
522 : // throw an error on CREATE TABLESPACE in the first place.
523 0 : if !has_relmap_file
524 0 : && self
525 0 : .timeline
526 0 : .list_rels(spcnode, dbnode, Version::Lsn(self.lsn), self.ctx)
527 0 : .await
528 0 : .map_err(|e| BasebackupError::Server(e.into()))?
529 0 : .is_empty()
530 : {
531 0 : return Ok(());
532 0 : }
533 0 : // User defined tablespaces are not supported
534 0 : if spcnode != DEFAULTTABLESPACE_OID {
535 0 : return Err(BasebackupError::Server(anyhow!(
536 0 : "spcnode != DEFAULTTABLESPACE_OID, spcnode={spcnode}"
537 0 : )));
538 0 : }
539 0 :
540 0 : // Append dir path for each database
541 0 : let path = format!("base/{}", dbnode);
542 0 : let header = new_tar_header_dir(&path)?;
543 0 : self.ar
544 0 : .append(&header, &mut io::empty())
545 0 : .await
546 0 : .map_err(BasebackupError::Client)?;
547 :
548 0 : if let Some(img) = relmap_img {
549 0 : let dst_path = format!("base/{}/PG_VERSION", dbnode);
550 :
551 0 : let pg_version_str = match self.timeline.pg_version {
552 0 : 14 | 15 => self.timeline.pg_version.to_string(),
553 0 : ver => format!("{ver}\x0A"),
554 : };
555 0 : let header = new_tar_header(&dst_path, pg_version_str.len() as u64)?;
556 0 : self.ar
557 0 : .append(&header, pg_version_str.as_bytes())
558 0 : .await
559 0 : .map_err(BasebackupError::Client)?;
560 :
561 0 : let relmap_path = format!("base/{}/pg_filenode.map", dbnode);
562 0 : let header = new_tar_header(&relmap_path, img.len() as u64)?;
563 0 : self.ar
564 0 : .append(&header, &img[..])
565 0 : .await
566 0 : .map_err(BasebackupError::Client)?;
567 0 : }
568 : };
569 0 : Ok(())
570 0 : }
571 :
572 : //
573 : // Extract twophase state files
574 : //
575 0 : async fn add_twophase_file(&mut self, xid: TransactionId) -> Result<(), BasebackupError> {
576 0 : let img = self
577 0 : .timeline
578 0 : .get_twophase_file(xid, self.lsn, self.ctx)
579 0 : .await
580 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
581 :
582 0 : let mut buf = BytesMut::new();
583 0 : buf.extend_from_slice(&img[..]);
584 0 : let crc = crc32c::crc32c(&img[..]);
585 0 : buf.put_u32_le(crc);
586 0 : let path = format!("pg_twophase/{:>08X}", xid);
587 0 : let header = new_tar_header(&path, buf.len() as u64)?;
588 0 : self.ar
589 0 : .append(&header, &buf[..])
590 0 : .await
591 0 : .map_err(BasebackupError::Client)?;
592 :
593 0 : Ok(())
594 0 : }
595 :
596 : //
597 : // Add generated pg_control file and bootstrap WAL segment.
598 : // Also send zenith.signal file with extra bootstrap data.
599 : //
600 0 : async fn add_pgcontrol_file(&mut self) -> Result<(), BasebackupError> {
601 0 : // add zenith.signal file
602 0 : let mut zenith_signal = String::new();
603 0 : if self.prev_record_lsn == Lsn(0) {
604 0 : if self.lsn == self.timeline.get_ancestor_lsn() {
605 0 : write!(zenith_signal, "PREV LSN: none")
606 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
607 : } else {
608 0 : write!(zenith_signal, "PREV LSN: invalid")
609 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
610 : }
611 : } else {
612 0 : write!(zenith_signal, "PREV LSN: {}", self.prev_record_lsn)
613 0 : .map_err(|e| BasebackupError::Server(e.into()))?;
614 : }
615 0 : self.ar
616 0 : .append(
617 0 : &new_tar_header("zenith.signal", zenith_signal.len() as u64)?,
618 0 : zenith_signal.as_bytes(),
619 : )
620 0 : .await
621 0 : .map_err(BasebackupError::Client)?;
622 :
623 0 : let checkpoint_bytes = self
624 0 : .timeline
625 0 : .get_checkpoint(self.lsn, self.ctx)
626 0 : .await
627 0 : .context("failed to get checkpoint bytes")?;
628 0 : let pg_control_bytes = self
629 0 : .timeline
630 0 : .get_control_file(self.lsn, self.ctx)
631 0 : .await
632 0 : .context("failed get control bytes")?;
633 :
634 0 : let (pg_control_bytes, system_identifier) = postgres_ffi::generate_pg_control(
635 0 : &pg_control_bytes,
636 0 : &checkpoint_bytes,
637 0 : self.lsn,
638 0 : self.timeline.pg_version,
639 0 : )?;
640 :
641 : //send pg_control
642 0 : let header = new_tar_header("global/pg_control", pg_control_bytes.len() as u64)?;
643 0 : self.ar
644 0 : .append(&header, &pg_control_bytes[..])
645 0 : .await
646 0 : .map_err(BasebackupError::Client)?;
647 :
648 : //send wal segment
649 0 : let segno = self.lsn.segment_number(WAL_SEGMENT_SIZE);
650 0 : let wal_file_name = XLogFileName(PG_TLI, segno, WAL_SEGMENT_SIZE);
651 0 : let wal_file_path = format!("pg_wal/{}", wal_file_name);
652 0 : let header = new_tar_header(&wal_file_path, WAL_SEGMENT_SIZE as u64)?;
653 :
654 0 : let wal_seg = postgres_ffi::generate_wal_segment(
655 0 : segno,
656 0 : system_identifier,
657 0 : self.timeline.pg_version,
658 0 : self.lsn,
659 0 : )
660 0 : .map_err(|e| anyhow!(e).context("Failed generating wal segment"))?;
661 0 : if wal_seg.len() != WAL_SEGMENT_SIZE {
662 0 : return Err(BasebackupError::Server(anyhow!(
663 0 : "wal_seg.len() != WAL_SEGMENT_SIZE, wal_seg.len()={}",
664 0 : wal_seg.len()
665 0 : )));
666 0 : }
667 0 : self.ar
668 0 : .append(&header, &wal_seg[..])
669 0 : .await
670 0 : .map_err(BasebackupError::Client)?;
671 0 : Ok(())
672 0 : }
673 : }
674 :
675 : //
676 : // Create new tarball entry header
677 : //
678 0 : fn new_tar_header(path: &str, size: u64) -> anyhow::Result<Header> {
679 0 : let mut header = Header::new_gnu();
680 0 : header.set_size(size);
681 0 : header.set_path(path)?;
682 0 : header.set_mode(0b110000000); // -rw-------
683 0 : header.set_mtime(
684 0 : // use currenttime as last modified time
685 0 : SystemTime::now()
686 0 : .duration_since(SystemTime::UNIX_EPOCH)
687 0 : .unwrap()
688 0 : .as_secs(),
689 0 : );
690 0 : header.set_cksum();
691 0 : Ok(header)
692 0 : }
693 :
694 0 : fn new_tar_header_dir(path: &str) -> anyhow::Result<Header> {
695 0 : let mut header = Header::new_gnu();
696 0 : header.set_size(0);
697 0 : header.set_path(path)?;
698 0 : header.set_mode(0o755); // -rw-------
699 0 : header.set_entry_type(EntryType::dir());
700 0 : header.set_mtime(
701 0 : // use currenttime as last modified time
702 0 : SystemTime::now()
703 0 : .duration_since(SystemTime::UNIX_EPOCH)
704 0 : .unwrap()
705 0 : .as_secs(),
706 0 : );
707 0 : header.set_cksum();
708 0 : Ok(header)
709 0 : }
|