LCOV - code coverage report
Current view: top level - libs/postgres_ffi/wal_craft/src - xlog_utils_test.rs (source / functions) Coverage Total Hit
Test: 32f4a56327bc9da697706839ed4836b2a00a408f.info Lines: 98.8 % 169 167
Test Date: 2024-02-07 07:37:29 Functions: 100.0 % 75 75

            Line data    Source code
       1              : //! Tests for postgres_ffi xlog_utils module. Put it here to break cyclic dependency.
       2              : 
       3              : use super::*;
       4              : use crate::{error, info};
       5              : use regex::Regex;
       6              : use std::cmp::min;
       7              : use std::fs::{self, File};
       8              : use std::io::Write;
       9              : use std::{env, str::FromStr};
      10              : use utils::const_assert;
      11              : use utils::lsn::Lsn;
      12              : 
      13           18 : fn init_logging() {
      14           18 :     let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(
      15           18 :         format!("crate=info,postgres_ffi::{PG_MAJORVERSION}::xlog_utils=trace"),
      16           18 :     ))
      17           18 :     .is_test(true)
      18           18 :     .try_init();
      19           18 : }
      20              : 
      21           18 : fn test_end_of_wal<C: crate::Crafter>(test_name: &str) {
      22           18 :     use crate::*;
      23           18 : 
      24           18 :     let pg_version = PG_MAJORVERSION[1..3].parse::<u32>().unwrap();
      25           18 : 
      26           18 :     // Craft some WAL
      27           18 :     let top_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
      28           18 :         .join("..")
      29           18 :         .join("..")
      30           18 :         .join("..");
      31           18 :     let cfg = Conf {
      32           18 :         pg_version,
      33           18 :         pg_distrib_dir: top_path.join("pg_install"),
      34           18 :         datadir: top_path.join(format!("test_output/{}-{PG_MAJORVERSION}", test_name)),
      35           18 :     };
      36           18 :     if cfg.datadir.exists() {
      37            9 :         fs::remove_dir_all(&cfg.datadir).unwrap();
      38            9 :     }
      39           18 :     cfg.initdb().unwrap();
      40           18 :     let srv = cfg.start_server().unwrap();
      41           18 :     let (intermediate_lsns, expected_end_of_wal_partial) =
      42           18 :         C::craft(&mut srv.connect_with_timeout().unwrap()).unwrap();
      43           18 :     let intermediate_lsns: Vec<Lsn> = intermediate_lsns
      44           18 :         .iter()
      45           24 :         .map(|&lsn| u64::from(lsn).into())
      46           18 :         .collect();
      47           18 :     let expected_end_of_wal: Lsn = u64::from(expected_end_of_wal_partial).into();
      48           18 :     srv.kill();
      49           18 : 
      50           18 :     // Check find_end_of_wal on the initial WAL
      51           18 :     let last_segment = cfg
      52           18 :         .wal_dir()
      53           18 :         .read_dir()
      54           18 :         .unwrap()
      55           48 :         .map(|f| f.unwrap().file_name().into_string().unwrap())
      56           48 :         .filter(|fname| IsXLogFileName(fname))
      57           18 :         .max()
      58           18 :         .unwrap();
      59           18 :     check_pg_waldump_end_of_wal(&cfg, &last_segment, expected_end_of_wal);
      60           42 :     for start_lsn in intermediate_lsns
      61           18 :         .iter()
      62           18 :         .chain(std::iter::once(&expected_end_of_wal))
      63              :     {
      64              :         // Erase all WAL before `start_lsn` to ensure it's not used by `find_end_of_wal`.
      65              :         // We assume that `start_lsn` is non-decreasing.
      66           42 :         info!(
      67           42 :             "Checking with start_lsn={}, erasing WAL before it",
      68              :             start_lsn
      69              :         );
      70          114 :         for file in fs::read_dir(cfg.wal_dir()).unwrap().flatten() {
      71          114 :             let fname = file.file_name().into_string().unwrap();
      72          114 :             if !IsXLogFileName(&fname) {
      73           42 :                 continue;
      74           72 :             }
      75           72 :             let (segno, _) = XLogFromFileName(&fname, WAL_SEGMENT_SIZE);
      76           72 :             let seg_start_lsn = XLogSegNoOffsetToRecPtr(segno, 0, WAL_SEGMENT_SIZE);
      77           72 :             if seg_start_lsn > u64::from(*start_lsn) {
      78           12 :                 continue;
      79           60 :             }
      80           60 :             let mut f = File::options().write(true).open(file.path()).unwrap();
      81           60 :             const ZEROS: [u8; WAL_SEGMENT_SIZE] = [0u8; WAL_SEGMENT_SIZE];
      82           60 :             f.write_all(
      83           60 :                 &ZEROS[0..min(
      84           60 :                     WAL_SEGMENT_SIZE,
      85           60 :                     (u64::from(*start_lsn) - seg_start_lsn) as usize,
      86           60 :                 )],
      87           60 :             )
      88           60 :             .unwrap();
      89              :         }
      90           42 :         check_end_of_wal(&cfg, &last_segment, *start_lsn, expected_end_of_wal);
      91              :     }
      92           18 : }
      93              : 
      94           18 : fn check_pg_waldump_end_of_wal(
      95           18 :     cfg: &crate::Conf,
      96           18 :     last_segment: &str,
      97           18 :     expected_end_of_wal: Lsn,
      98           18 : ) {
      99           18 :     // Get the actual end of WAL by pg_waldump
     100           18 :     let waldump_output = cfg
     101           18 :         .pg_waldump("000000010000000000000001", last_segment)
     102           18 :         .unwrap()
     103           18 :         .stderr;
     104           18 :     let waldump_output = std::str::from_utf8(&waldump_output).unwrap();
     105           18 :     let caps = match Regex::new(r"invalid record length at (.+):")
     106           18 :         .unwrap()
     107           18 :         .captures(waldump_output)
     108              :     {
     109           18 :         Some(caps) => caps,
     110              :         None => {
     111            0 :             error!("Unable to parse pg_waldump's stderr:\n{}", waldump_output);
     112            0 :             panic!();
     113              :         }
     114              :     };
     115           18 :     let waldump_wal_end = Lsn::from_str(caps.get(1).unwrap().as_str()).unwrap();
     116           18 :     info!(
     117           18 :         "waldump erred on {}, expected wal end at {}",
     118              :         waldump_wal_end, expected_end_of_wal
     119              :     );
     120           18 :     assert_eq!(waldump_wal_end, expected_end_of_wal);
     121           18 : }
     122              : 
     123           42 : fn check_end_of_wal(
     124           42 :     cfg: &crate::Conf,
     125           42 :     last_segment: &str,
     126           42 :     start_lsn: Lsn,
     127           42 :     expected_end_of_wal: Lsn,
     128           42 : ) {
     129           42 :     // Check end_of_wal on non-partial WAL segment (we treat it as fully populated)
     130           42 :     // let wal_end = find_end_of_wal(&cfg.wal_dir(), WAL_SEGMENT_SIZE, start_lsn).unwrap();
     131           42 :     // info!(
     132           42 :     //     "find_end_of_wal returned wal_end={} with non-partial WAL segment",
     133           42 :     //     wal_end
     134           42 :     // );
     135           42 :     // assert_eq!(wal_end, expected_end_of_wal_non_partial);
     136           42 : 
     137           42 :     // Rename file to partial to actually find last valid lsn, then rename it back.
     138           42 :     fs::rename(
     139           42 :         cfg.wal_dir().join(last_segment),
     140           42 :         cfg.wal_dir().join(format!("{}.partial", last_segment)),
     141           42 :     )
     142           42 :     .unwrap();
     143           42 :     let wal_end = find_end_of_wal(&cfg.wal_dir(), WAL_SEGMENT_SIZE, start_lsn).unwrap();
     144           42 :     info!(
     145           42 :         "find_end_of_wal returned wal_end={} with partial WAL segment",
     146              :         wal_end
     147              :     );
     148           42 :     assert_eq!(wal_end, expected_end_of_wal);
     149           42 :     fs::rename(
     150           42 :         cfg.wal_dir().join(format!("{}.partial", last_segment)),
     151           42 :         cfg.wal_dir().join(last_segment),
     152           42 :     )
     153           42 :     .unwrap();
     154           42 : }
     155              : 
     156              : const_assert!(WAL_SEGMENT_SIZE == 16 * 1024 * 1024);
     157              : 
     158            6 : #[test]
     159            6 : pub fn test_find_end_of_wal_simple() {
     160            6 :     init_logging();
     161            6 :     test_end_of_wal::<crate::Simple>("test_find_end_of_wal_simple");
     162            6 : }
     163              : 
     164            6 : #[test]
     165            6 : pub fn test_find_end_of_wal_crossing_segment_followed_by_small_one() {
     166            6 :     init_logging();
     167            6 :     test_end_of_wal::<crate::WalRecordCrossingSegmentFollowedBySmallOne>(
     168            6 :         "test_find_end_of_wal_crossing_segment_followed_by_small_one",
     169            6 :     );
     170            6 : }
     171              : 
     172            6 : #[test]
     173            6 : pub fn test_find_end_of_wal_last_crossing_segment() {
     174            6 :     init_logging();
     175            6 :     test_end_of_wal::<crate::LastWalRecordCrossingSegment>(
     176            6 :         "test_find_end_of_wal_last_crossing_segment",
     177            6 :     );
     178            6 : }
     179              : 
     180              : /// Check the math in update_next_xid
     181              : ///
     182              : /// NOTE: These checks are sensitive to the value of XID_CHECKPOINT_INTERVAL,
     183              : /// currently 1024.
     184            6 : #[test]
     185            6 : pub fn test_update_next_xid() {
     186            6 :     let checkpoint_buf = [0u8; std::mem::size_of::<CheckPoint>()];
     187            6 :     let mut checkpoint = CheckPoint::decode(&checkpoint_buf).unwrap();
     188            6 : 
     189            6 :     checkpoint.nextXid = FullTransactionId { value: 10 };
     190            6 :     assert_eq!(checkpoint.nextXid.value, 10);
     191              : 
     192              :     // The input XID gets rounded up to the next XID_CHECKPOINT_INTERVAL
     193              :     // boundary
     194            6 :     checkpoint.update_next_xid(100);
     195            6 :     assert_eq!(checkpoint.nextXid.value, 1024);
     196              : 
     197              :     // No change
     198            6 :     checkpoint.update_next_xid(500);
     199            6 :     assert_eq!(checkpoint.nextXid.value, 1024);
     200            6 :     checkpoint.update_next_xid(1023);
     201            6 :     assert_eq!(checkpoint.nextXid.value, 1024);
     202              : 
     203              :     // The function returns the *next* XID, given the highest XID seen so
     204              :     // far. So when we pass 1024, the nextXid gets bumped up to the next
     205              :     // XID_CHECKPOINT_INTERVAL boundary.
     206            6 :     checkpoint.update_next_xid(1024);
     207            6 :     assert_eq!(checkpoint.nextXid.value, 2048);
     208            6 : }
     209              : 
     210            6 : #[test]
     211            6 : pub fn test_encode_logical_message() {
     212            6 :     let expected = [
     213            6 :         64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 170, 34, 166, 227, 255,
     214            6 :         38, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 112, 114,
     215            6 :         101, 102, 105, 120, 0, 109, 101, 115, 115, 97, 103, 101,
     216            6 :     ];
     217            6 :     let actual = encode_logical_message("prefix", "message");
     218            6 :     assert_eq!(expected, actual[..]);
     219            6 : }
        

Generated by: LCOV version 2.1-beta