LCOV - differential code coverage report
Current view: top level - libs/postgres_ffi/src - xlog_utils.rs (source / functions) Coverage Total Hit UBC CBC
Current: cd44433dd675caa99df17a61b18949c8387e2242.info Lines: 95.6 % 320 306 14 306
Current Date: 2024-01-09 02:06:09 Functions: 75.2 % 109 82 27 82
Baseline: 66c52a629a0f4a503e193045e0df4c77139e344b.info
Baseline Date: 2024-01-08 15:34:46

           TLA  Line data    Source code
       1                 : //
       2                 : // This file contains common utilities for dealing with PostgreSQL WAL files and
       3                 : // LSNs.
       4                 : //
       5                 : // Many of these functions have been copied from PostgreSQL, and rewritten in
       6                 : // Rust. That's why they don't follow the usual Rust naming conventions, they
       7                 : // have been named the same as the corresponding PostgreSQL functions instead.
       8                 : //
       9                 : 
      10                 : use crc32c::crc32c_append;
      11                 : 
      12                 : use super::super::waldecoder::WalStreamDecoder;
      13                 : use super::bindings::{
      14                 :     CheckPoint, ControlFileData, DBState_DB_SHUTDOWNED, FullTransactionId, TimeLineID, TimestampTz,
      15                 :     XLogLongPageHeaderData, XLogPageHeaderData, XLogRecPtr, XLogRecord, XLogSegNo, XLOG_PAGE_MAGIC,
      16                 : };
      17                 : use super::PG_MAJORVERSION;
      18                 : use crate::pg_constants;
      19                 : use crate::PG_TLI;
      20                 : use crate::{uint32, uint64, Oid};
      21                 : use crate::{WAL_SEGMENT_SIZE, XLOG_BLCKSZ};
      22                 : 
      23                 : use bytes::BytesMut;
      24                 : use bytes::{Buf, Bytes};
      25                 : 
      26                 : use log::*;
      27                 : 
      28                 : use serde::Serialize;
      29                 : use std::fs::File;
      30                 : use std::io::prelude::*;
      31                 : use std::io::ErrorKind;
      32                 : use std::io::SeekFrom;
      33                 : use std::path::{Path, PathBuf};
      34                 : use std::time::SystemTime;
      35                 : use utils::bin_ser::DeserializeError;
      36                 : use utils::bin_ser::SerializeError;
      37                 : 
      38                 : use utils::lsn::Lsn;
      39                 : 
      40                 : pub const XLOG_FNAME_LEN: usize = 24;
      41                 : pub const XLP_FIRST_IS_CONTRECORD: u16 = 0x0001;
      42                 : pub const XLP_REM_LEN_OFFS: usize = 2 + 2 + 4 + 8;
      43                 : pub const XLOG_RECORD_CRC_OFFS: usize = 4 + 4 + 8 + 1 + 1 + 2;
      44                 : 
      45                 : pub const XLOG_SIZE_OF_XLOG_SHORT_PHD: usize = std::mem::size_of::<XLogPageHeaderData>();
      46                 : pub const XLOG_SIZE_OF_XLOG_LONG_PHD: usize = std::mem::size_of::<XLogLongPageHeaderData>();
      47                 : pub const XLOG_SIZE_OF_XLOG_RECORD: usize = std::mem::size_of::<XLogRecord>();
      48                 : #[allow(clippy::identity_op)]
      49                 : pub const SIZE_OF_XLOG_RECORD_DATA_HEADER_SHORT: usize = 1 * 2;
      50                 : 
      51                 : /// Interval of checkpointing metadata file. We should store metadata file to enforce
      52                 : /// predicate that checkpoint.nextXid is larger than any XID in WAL.
      53                 : /// But flushing checkpoint file for each transaction seems to be too expensive,
      54                 : /// so XID_CHECKPOINT_INTERVAL is used to forward align nextXid and so perform
      55                 : /// metadata checkpoint only once per XID_CHECKPOINT_INTERVAL transactions.
      56                 : /// XID_CHECKPOINT_INTERVAL should not be larger than BLCKSZ*CLOG_XACTS_PER_BYTE
      57                 : /// in order to let CLOG_TRUNCATE mechanism correctly extend CLOG.
      58                 : const XID_CHECKPOINT_INTERVAL: u32 = 1024;
      59                 : 
      60 CBC       10692 : pub fn XLogSegmentsPerXLogId(wal_segsz_bytes: usize) -> XLogSegNo {
      61           10692 :     (0x100000000u64 / wal_segsz_bytes as u64) as XLogSegNo
      62           10692 : }
      63                 : 
      64             626 : pub fn XLogSegNoOffsetToRecPtr(
      65             626 :     segno: XLogSegNo,
      66             626 :     offset: u32,
      67             626 :     wal_segsz_bytes: usize,
      68             626 : ) -> XLogRecPtr {
      69             626 :     segno * (wal_segsz_bytes as u64) + (offset as u64)
      70             626 : }
      71                 : 
      72            5221 : pub fn XLogFileName(tli: TimeLineID, logSegNo: XLogSegNo, wal_segsz_bytes: usize) -> String {
      73            5221 :     format!(
      74            5221 :         "{:>08X}{:>08X}{:>08X}",
      75            5221 :         tli,
      76            5221 :         logSegNo / XLogSegmentsPerXLogId(wal_segsz_bytes),
      77            5221 :         logSegNo % XLogSegmentsPerXLogId(wal_segsz_bytes)
      78            5221 :     )
      79            5221 : }
      80                 : 
      81             250 : pub fn XLogFromFileName(fname: &str, wal_seg_size: usize) -> (XLogSegNo, TimeLineID) {
      82             250 :     let tli = u32::from_str_radix(&fname[0..8], 16).unwrap();
      83             250 :     let log = u32::from_str_radix(&fname[8..16], 16).unwrap() as XLogSegNo;
      84             250 :     let seg = u32::from_str_radix(&fname[16..24], 16).unwrap() as XLogSegNo;
      85             250 :     (log * XLogSegmentsPerXLogId(wal_seg_size) + seg, tli)
      86             250 : }
      87                 : 
      88             957 : pub fn IsXLogFileName(fname: &str) -> bool {
      89            6360 :     return fname.len() == XLOG_FNAME_LEN && fname.chars().all(|c| c.is_ascii_hexdigit());
      90             957 : }
      91                 : 
      92             662 : pub fn IsPartialXLogFileName(fname: &str) -> bool {
      93             662 :     fname.ends_with(".partial") && IsXLogFileName(&fname[0..fname.len() - 8])
      94             662 : }
      95                 : 
      96                 : /// If LSN points to the beginning of the page, then shift it to first record,
      97                 : /// otherwise align on 8-bytes boundary (required for WAL records)
      98            1270 : pub fn normalize_lsn(lsn: Lsn, seg_sz: usize) -> Lsn {
      99            1270 :     if lsn.0 % XLOG_BLCKSZ as u64 == 0 {
     100              13 :         let hdr_size = if lsn.0 % seg_sz as u64 == 0 {
     101               6 :             XLOG_SIZE_OF_XLOG_LONG_PHD
     102                 :         } else {
     103               7 :             XLOG_SIZE_OF_XLOG_SHORT_PHD
     104                 :         };
     105              13 :         lsn + hdr_size as u64
     106                 :     } else {
     107            1257 :         lsn.align()
     108                 :     }
     109            1270 : }
     110                 : 
     111             557 : pub fn generate_pg_control(
     112             557 :     pg_control_bytes: &[u8],
     113             557 :     checkpoint_bytes: &[u8],
     114             557 :     lsn: Lsn,
     115             557 : ) -> anyhow::Result<(Bytes, u64)> {
     116             557 :     let mut pg_control = ControlFileData::decode(pg_control_bytes)?;
     117             557 :     let mut checkpoint = CheckPoint::decode(checkpoint_bytes)?;
     118                 : 
     119                 :     // Generate new pg_control needed for bootstrap
     120             557 :     checkpoint.redo = normalize_lsn(lsn, WAL_SEGMENT_SIZE).0;
     121             557 : 
     122             557 :     //reset some fields we don't want to preserve
     123             557 :     //TODO Check this.
     124             557 :     //We may need to determine the value from twophase data.
     125             557 :     checkpoint.oldestActiveXid = 0;
     126             557 : 
     127             557 :     //save new values in pg_control
     128             557 :     pg_control.checkPoint = 0;
     129             557 :     pg_control.checkPointCopy = checkpoint;
     130             557 :     pg_control.state = DBState_DB_SHUTDOWNED;
     131             557 : 
     132             557 :     Ok((pg_control.encode(), pg_control.system_identifier))
     133             557 : }
     134                 : 
     135          571794 : pub fn get_current_timestamp() -> TimestampTz {
     136          571794 :     to_pg_timestamp(SystemTime::now())
     137          571794 : }
     138                 : 
     139                 : // Module to reduce the scope of the constants
     140                 : mod timestamp_conversions {
     141                 :     use std::time::Duration;
     142                 : 
     143                 :     use super::*;
     144                 : 
     145                 :     const UNIX_EPOCH_JDATE: u64 = 2440588; // == date2j(1970, 1, 1)
     146                 :     const POSTGRES_EPOCH_JDATE: u64 = 2451545; // == date2j(2000, 1, 1)
     147                 :     const SECS_PER_DAY: u64 = 86400;
     148                 :     const USECS_PER_SEC: u64 = 1000000;
     149                 :     const SECS_DIFF_UNIX_TO_POSTGRES_EPOCH: u64 =
     150                 :         (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
     151                 : 
     152          572001 :     pub fn to_pg_timestamp(time: SystemTime) -> TimestampTz {
     153          572001 :         match time.duration_since(SystemTime::UNIX_EPOCH) {
     154          572001 :             Ok(n) => {
     155          572001 :                 ((n.as_secs() - SECS_DIFF_UNIX_TO_POSTGRES_EPOCH) * USECS_PER_SEC
     156          572001 :                     + n.subsec_micros() as u64) as i64
     157                 :             }
     158 UBC           0 :             Err(_) => panic!("SystemTime before UNIX EPOCH!"),
     159                 :         }
     160 CBC      572001 :     }
     161                 : 
     162              16 :     pub fn from_pg_timestamp(time: TimestampTz) -> SystemTime {
     163              16 :         let time: u64 = time
     164              16 :             .try_into()
     165              16 :             .expect("timestamp before millenium (postgres epoch)");
     166              16 :         let since_unix_epoch = time + SECS_DIFF_UNIX_TO_POSTGRES_EPOCH * USECS_PER_SEC;
     167              16 :         SystemTime::UNIX_EPOCH
     168              16 :             .checked_add(Duration::from_micros(since_unix_epoch))
     169              16 :             .expect("SystemTime overflow")
     170              16 :     }
     171                 : }
     172                 : 
     173                 : pub use timestamp_conversions::{from_pg_timestamp, to_pg_timestamp};
     174                 : 
     175                 : // Returns (aligned) end_lsn of the last record in data_dir with WAL segments.
     176                 : // start_lsn must point to some previously known record boundary (beginning of
     177                 : // the next record). If no valid record after is found, start_lsn is returned
     178                 : // back.
     179             206 : pub fn find_end_of_wal(
     180             206 :     data_dir: &Path,
     181             206 :     wal_seg_size: usize,
     182             206 :     start_lsn: Lsn, // start reading WAL at this point; must point at record start_lsn.
     183             206 : ) -> anyhow::Result<Lsn> {
     184             206 :     let mut result = start_lsn;
     185             206 :     let mut curr_lsn = start_lsn;
     186             206 :     let mut buf = [0u8; XLOG_BLCKSZ];
     187             206 :     let pg_version = PG_MAJORVERSION[1..3].parse::<u32>().unwrap();
     188             206 :     debug!("find_end_of_wal PG_VERSION: {}", pg_version);
     189                 : 
     190             206 :     let mut decoder = WalStreamDecoder::new(start_lsn, pg_version);
     191                 : 
     192                 :     // loop over segments
     193             235 :     loop {
     194             235 :         let segno = curr_lsn.segment_number(wal_seg_size);
     195             235 :         let seg_file_name = XLogFileName(PG_TLI, segno, wal_seg_size);
     196             235 :         let seg_file_path = data_dir.join(seg_file_name);
     197             235 :         match open_wal_segment(&seg_file_path)? {
     198                 :             None => {
     199                 :                 // no more segments
     200               5 :                 debug!(
     201 UBC           0 :                     "find_end_of_wal reached end at {:?}, segment {:?} doesn't exist",
     202                 :                     result, seg_file_path
     203                 :                 );
     204 CBC           5 :                 return Ok(result);
     205                 :             }
     206             230 :             Some(mut segment) => {
     207             230 :                 let seg_offs = curr_lsn.segment_offset(wal_seg_size);
     208             230 :                 segment.seek(SeekFrom::Start(seg_offs as u64))?;
     209                 :                 // loop inside segment
     210                 :                 loop {
     211           57612 :                     let bytes_read = segment.read(&mut buf)?;
     212           57612 :                     if bytes_read == 0 {
     213              29 :                         break; // EOF
     214           57583 :                     }
     215           57583 :                     curr_lsn += bytes_read as u64;
     216           57583 :                     decoder.feed_bytes(&buf[0..bytes_read]);
     217                 : 
     218                 :                     // advance result past all completely read records
     219                 :                     loop {
     220         3926788 :                         match decoder.poll_decode() {
     221         3869205 :                             Ok(Some(record)) => result = record.0,
     222             201 :                             Err(e) => {
     223             201 :                                 debug!(
     224              21 :                                     "find_end_of_wal reached end at {:?}, decode error: {:?}",
     225                 :                                     result, e
     226                 :                                 );
     227             201 :                                 return Ok(result);
     228                 :                             }
     229           57382 :                             Ok(None) => break, // need more data
     230                 :                         }
     231                 :                     }
     232                 :                 }
     233                 :             }
     234                 :         }
     235                 :     }
     236             206 : }
     237                 : 
     238                 : // Open .partial or full WAL segment file, if present.
     239             235 : fn open_wal_segment(seg_file_path: &Path) -> anyhow::Result<Option<File>> {
     240             235 :     let mut partial_path = seg_file_path.to_owned();
     241             235 :     partial_path.set_extension("partial");
     242             235 :     match File::open(partial_path) {
     243             201 :         Ok(file) => Ok(Some(file)),
     244              34 :         Err(e) => match e.kind() {
     245                 :             ErrorKind::NotFound => {
     246                 :                 // .partial not found, try full
     247              34 :                 match File::open(seg_file_path) {
     248              29 :                     Ok(file) => Ok(Some(file)),
     249               5 :                     Err(e) => match e.kind() {
     250               5 :                         ErrorKind::NotFound => Ok(None),
     251 UBC           0 :                         _ => Err(e.into()),
     252                 :                     },
     253                 :                 }
     254                 :             }
     255               0 :             _ => Err(e.into()),
     256                 :         },
     257                 :     }
     258 CBC         235 : }
     259                 : 
     260 UBC           0 : pub fn main() {
     261               0 :     let mut data_dir = PathBuf::new();
     262               0 :     data_dir.push(".");
     263               0 :     let wal_end = find_end_of_wal(&data_dir, WAL_SEGMENT_SIZE, Lsn(0)).unwrap();
     264               0 :     println!("wal_end={:?}", wal_end);
     265               0 : }
     266                 : 
     267                 : impl XLogRecord {
     268 CBC   123896199 :     pub fn from_slice(buf: &[u8]) -> Result<XLogRecord, DeserializeError> {
     269       123896199 :         use utils::bin_ser::LeSer;
     270       123896199 :         XLogRecord::des(buf)
     271       123896199 :     }
     272                 : 
     273        47422616 :     pub fn from_bytes<B: Buf>(buf: &mut B) -> Result<XLogRecord, DeserializeError> {
     274        47422616 :         use utils::bin_ser::LeSer;
     275        47422616 :         XLogRecord::des_from(&mut buf.reader())
     276        47422616 :     }
     277                 : 
     278              12 :     pub fn encode(&self) -> Result<Bytes, SerializeError> {
     279              12 :         use utils::bin_ser::LeSer;
     280              12 :         Ok(self.ser()?.into())
     281              12 :     }
     282                 : 
     283                 :     // Is this record an XLOG_SWITCH record? They need some special processing,
     284       123896198 :     pub fn is_xlog_switch_record(&self) -> bool {
     285       123896198 :         self.xl_info == pg_constants::XLOG_SWITCH && self.xl_rmid == pg_constants::RM_XLOG_ID
     286       123896198 :     }
     287                 : }
     288                 : 
     289                 : impl XLogPageHeaderData {
     290         2658586 :     pub fn from_bytes<B: Buf>(buf: &mut B) -> Result<XLogPageHeaderData, DeserializeError> {
     291         2658586 :         use utils::bin_ser::LeSer;
     292         2658586 :         XLogPageHeaderData::des_from(&mut buf.reader())
     293         2658586 :     }
     294                 : 
     295             559 :     pub fn encode(&self) -> Result<Bytes, SerializeError> {
     296             559 :         use utils::bin_ser::LeSer;
     297             559 :         self.ser().map(|b| b.into())
     298             559 :     }
     299                 : }
     300                 : 
     301                 : impl XLogLongPageHeaderData {
     302            1271 :     pub fn from_bytes<B: Buf>(buf: &mut B) -> Result<XLogLongPageHeaderData, DeserializeError> {
     303            1271 :         use utils::bin_ser::LeSer;
     304            1271 :         XLogLongPageHeaderData::des_from(&mut buf.reader())
     305            1271 :     }
     306                 : 
     307             562 :     pub fn encode(&self) -> Result<Bytes, SerializeError> {
     308             562 :         use utils::bin_ser::LeSer;
     309             562 :         self.ser().map(|b| b.into())
     310             562 :     }
     311                 : }
     312                 : 
     313                 : pub const SIZEOF_CHECKPOINT: usize = std::mem::size_of::<CheckPoint>();
     314                 : 
     315                 : impl CheckPoint {
     316           28328 :     pub fn encode(&self) -> Result<Bytes, SerializeError> {
     317           28328 :         use utils::bin_ser::LeSer;
     318           28328 :         Ok(self.ser()?.into())
     319           28328 :     }
     320                 : 
     321            2371 :     pub fn decode(buf: &[u8]) -> Result<CheckPoint, DeserializeError> {
     322            2371 :         use utils::bin_ser::LeSer;
     323            2371 :         CheckPoint::des(buf)
     324            2371 :     }
     325                 : 
     326                 :     /// Update next XID based on provided new_xid and stored epoch.
     327                 :     /// Next XID should be greater than new_xid. This handles 32-bit
     328                 :     /// XID wraparound correctly.
     329                 :     ///
     330                 :     /// Returns 'true' if the XID was updated.
     331        47446672 :     pub fn update_next_xid(&mut self, xid: u32) -> bool {
     332        47446672 :         // nextXid should nw greater than any XID in WAL, so increment provided XID and check for wraparround.
     333        47446672 :         let mut new_xid = std::cmp::max(xid + 1, pg_constants::FIRST_NORMAL_TRANSACTION_ID);
     334        47446672 :         // To reduce number of metadata checkpoints, we forward align XID on XID_CHECKPOINT_INTERVAL.
     335        47446672 :         // XID_CHECKPOINT_INTERVAL should not be larger than BLCKSZ*CLOG_XACTS_PER_BYTE
     336        47446672 :         new_xid =
     337        47446672 :             new_xid.wrapping_add(XID_CHECKPOINT_INTERVAL - 1) & !(XID_CHECKPOINT_INTERVAL - 1);
     338        47446672 :         let full_xid = self.nextXid.value;
     339        47446672 :         let old_xid = full_xid as u32;
     340        47446672 :         if new_xid.wrapping_sub(old_xid) as i32 > 0 {
     341            3409 :             let mut epoch = full_xid >> 32;
     342            3409 :             if new_xid < old_xid {
     343 UBC           0 :                 // wrap-around
     344               0 :                 epoch += 1;
     345 CBC        3409 :             }
     346            3409 :             let nextXid = (epoch << 32) | new_xid as u64;
     347            3409 : 
     348            3409 :             if nextXid != self.nextXid.value {
     349            3409 :                 self.nextXid = FullTransactionId { value: nextXid };
     350            3409 :                 return true;
     351 UBC           0 :             }
     352 CBC    47443263 :         }
     353        47443263 :         false
     354        47446672 :     }
     355                 : }
     356                 : 
     357                 : /// Generate new, empty WAL segment, with correct block headers at the first
     358                 : /// page of the segment and the page that contains the given LSN.
     359                 : /// We need this segment to start compute node.
     360             562 : pub fn generate_wal_segment(segno: u64, system_id: u64, lsn: Lsn) -> Result<Bytes, SerializeError> {
     361             562 :     let mut seg_buf = BytesMut::with_capacity(WAL_SEGMENT_SIZE);
     362             562 : 
     363             562 :     let pageaddr = XLogSegNoOffsetToRecPtr(segno, 0, WAL_SEGMENT_SIZE);
     364             562 : 
     365             562 :     let page_off = lsn.block_offset();
     366             562 :     let seg_off = lsn.segment_offset(WAL_SEGMENT_SIZE);
     367             562 : 
     368             562 :     let first_page_only = seg_off < XLOG_BLCKSZ;
     369             562 :     let (shdr_rem_len, infoflags) = if first_page_only {
     370               3 :         (seg_off, pg_constants::XLP_FIRST_IS_CONTRECORD)
     371                 :     } else {
     372             559 :         (0, 0)
     373                 :     };
     374                 : 
     375             562 :     let hdr = XLogLongPageHeaderData {
     376             562 :         std: {
     377             562 :             XLogPageHeaderData {
     378             562 :                 xlp_magic: XLOG_PAGE_MAGIC as u16,
     379             562 :                 xlp_info: pg_constants::XLP_LONG_HEADER | infoflags,
     380             562 :                 xlp_tli: PG_TLI,
     381             562 :                 xlp_pageaddr: pageaddr,
     382             562 :                 xlp_rem_len: shdr_rem_len as u32,
     383             562 :                 ..Default::default() // Put 0 in padding fields.
     384             562 :             }
     385             562 :         },
     386             562 :         xlp_sysid: system_id,
     387             562 :         xlp_seg_size: WAL_SEGMENT_SIZE as u32,
     388             562 :         xlp_xlog_blcksz: XLOG_BLCKSZ as u32,
     389             562 :     };
     390                 : 
     391             562 :     let hdr_bytes = hdr.encode()?;
     392             562 :     seg_buf.extend_from_slice(&hdr_bytes);
     393             562 : 
     394             562 :     //zero out the rest of the file
     395             562 :     seg_buf.resize(WAL_SEGMENT_SIZE, 0);
     396             562 : 
     397             562 :     if !first_page_only {
     398             559 :         let block_offset = lsn.page_offset_in_segment(WAL_SEGMENT_SIZE) as usize;
     399             559 :         let header = XLogPageHeaderData {
     400             559 :             xlp_magic: XLOG_PAGE_MAGIC as u16,
     401             559 :             xlp_info: if page_off >= pg_constants::SIZE_OF_PAGE_HEADER as u64 {
     402             557 :                 pg_constants::XLP_FIRST_IS_CONTRECORD
     403                 :             } else {
     404               2 :                 0
     405                 :             },
     406                 :             xlp_tli: PG_TLI,
     407             559 :             xlp_pageaddr: lsn.page_lsn().0,
     408             559 :             xlp_rem_len: if page_off >= pg_constants::SIZE_OF_PAGE_HEADER as u64 {
     409             557 :                 page_off as u32
     410                 :             } else {
     411               2 :                 0u32
     412                 :             },
     413             559 :             ..Default::default() // Put 0 in padding fields.
     414                 :         };
     415             559 :         let hdr_bytes = header.encode()?;
     416                 : 
     417             559 :         debug_assert!(seg_buf.len() > block_offset + hdr_bytes.len());
     418             559 :         debug_assert_ne!(block_offset, 0);
     419                 : 
     420             559 :         seg_buf[block_offset..block_offset + hdr_bytes.len()].copy_from_slice(&hdr_bytes[..]);
     421               3 :     }
     422                 : 
     423             562 :     Ok(seg_buf.freeze())
     424             562 : }
     425                 : 
     426                 : #[repr(C)]
     427              12 : #[derive(Serialize)]
     428                 : struct XlLogicalMessage {
     429                 :     db_id: Oid,
     430                 :     transactional: uint32, // bool, takes 4 bytes due to alignment in C structures
     431                 :     prefix_size: uint64,
     432                 :     message_size: uint64,
     433                 : }
     434                 : 
     435                 : impl XlLogicalMessage {
     436               6 :     pub fn encode(&self) -> Bytes {
     437               6 :         use utils::bin_ser::LeSer;
     438               6 :         self.ser().unwrap().into()
     439               6 :     }
     440                 : }
     441                 : 
     442                 : /// Create new WAL record for non-transactional logical message.
     443                 : /// Used for creating artificial WAL for tests, as LogicalMessage
     444                 : /// record is basically no-op.
     445                 : ///
     446                 : /// NOTE: This leaves the xl_prev field zero. The safekeeper and
     447                 : /// pageserver tolerate that, but PostgreSQL does not.
     448               6 : pub fn encode_logical_message(prefix: &str, message: &str) -> Vec<u8> {
     449               6 :     let mut prefix_bytes: Vec<u8> = Vec::with_capacity(prefix.len() + 1);
     450               6 :     prefix_bytes.write_all(prefix.as_bytes()).unwrap();
     451               6 :     prefix_bytes.push(0);
     452               6 : 
     453               6 :     let message_bytes = message.as_bytes();
     454               6 : 
     455               6 :     let logical_message = XlLogicalMessage {
     456               6 :         db_id: 0,
     457               6 :         transactional: 0,
     458               6 :         prefix_size: prefix_bytes.len() as u64,
     459               6 :         message_size: message_bytes.len() as u64,
     460               6 :     };
     461               6 : 
     462               6 :     let mainrdata = logical_message.encode();
     463               6 :     let mainrdata_len: usize = mainrdata.len() + prefix_bytes.len() + message_bytes.len();
     464                 :     // only short mainrdata is supported for now
     465               6 :     assert!(mainrdata_len <= 255);
     466               6 :     let mainrdata_len = mainrdata_len as u8;
     467               6 : 
     468               6 :     let mut data: Vec<u8> = vec![pg_constants::XLR_BLOCK_ID_DATA_SHORT, mainrdata_len];
     469               6 :     data.extend_from_slice(&mainrdata);
     470               6 :     data.extend_from_slice(&prefix_bytes);
     471               6 :     data.extend_from_slice(message_bytes);
     472               6 : 
     473               6 :     let total_len = XLOG_SIZE_OF_XLOG_RECORD + data.len();
     474               6 : 
     475               6 :     let mut header = XLogRecord {
     476               6 :         xl_tot_len: total_len as u32,
     477               6 :         xl_xid: 0,
     478               6 :         xl_prev: 0,
     479               6 :         xl_info: 0,
     480               6 :         xl_rmid: 21,
     481               6 :         __bindgen_padding_0: [0u8; 2usize],
     482               6 :         xl_crc: 0, // crc will be calculated later
     483               6 :     };
     484               6 : 
     485               6 :     let header_bytes = header.encode().expect("failed to encode header");
     486               6 :     let crc = crc32c_append(0, &data);
     487               6 :     let crc = crc32c_append(crc, &header_bytes[0..XLOG_RECORD_CRC_OFFS]);
     488               6 :     header.xl_crc = crc;
     489               6 : 
     490               6 :     let mut wal: Vec<u8> = Vec::new();
     491               6 :     wal.extend_from_slice(&header.encode().expect("failed to encode header"));
     492               6 :     wal.extend_from_slice(&data);
     493               6 : 
     494               6 :     // WAL start position must be aligned at 8 bytes,
     495               6 :     // this will add padding for the next WAL record.
     496               6 :     const PADDING: usize = 8;
     497               6 :     let padding_rem = wal.len() % PADDING;
     498               6 :     if padding_rem != 0 {
     499 UBC           0 :         wal.resize(wal.len() + PADDING - padding_rem, 0);
     500 CBC           6 :     }
     501                 : 
     502               6 :     wal
     503               6 : }
     504                 : 
     505                 : #[cfg(test)]
     506                 : mod tests {
     507                 :     use super::*;
     508                 : 
     509               3 :     #[test]
     510               3 :     fn test_ts_conversion() {
     511               3 :         let now = SystemTime::now();
     512               3 :         let round_trip = from_pg_timestamp(to_pg_timestamp(now));
     513               3 : 
     514               3 :         let now_since = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
     515               3 :         let round_trip_since = round_trip.duration_since(SystemTime::UNIX_EPOCH).unwrap();
     516               3 :         assert_eq!(now_since.as_micros(), round_trip_since.as_micros());
     517                 : 
     518               3 :         let now_pg = get_current_timestamp();
     519               3 :         let round_trip_pg = to_pg_timestamp(from_pg_timestamp(now_pg));
     520               3 : 
     521               3 :         assert_eq!(now_pg, round_trip_pg);
     522               3 :     }
     523                 : 
     524                 :     // If you need to craft WAL and write tests for this module, put it at wal_craft crate.
     525                 : }
        

Generated by: LCOV version 2.1-beta