Skip to content

Commit

Permalink
test(pageserver): add test wal record for unit testing
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi Z <[email protected]>
  • Loading branch information
skyzh committed Jun 11, 2024
1 parent 7121db3 commit 6e6356d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
76 changes: 75 additions & 1 deletion pageserver/src/tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4041,6 +4041,7 @@ mod tests {
use crate::repository::{Key, Value};
use crate::tenant::harness::*;
use crate::tenant::timeline::CompactFlags;
use crate::walrecord::NeonWalRecord;
use crate::DEFAULT_PG_VERSION;
use bytes::{Bytes, BytesMut};
use hex_literal::hex;
Expand Down Expand Up @@ -6701,7 +6702,7 @@ mod tests {
}

#[tokio::test]
async fn test_simple_bottom_most_compaction() -> anyhow::Result<()> {
async fn test_simple_bottom_most_compaction_images() -> anyhow::Result<()> {
let harness = TenantHarness::create("test_simple_bottom_most_compaction")?;
let (tenant, ctx) = harness.load().await;

Expand Down Expand Up @@ -6857,4 +6858,77 @@ mod tests {

Ok(())
}

#[tokio::test]
async fn test_neon_test_record() -> anyhow::Result<()> {
let harness = TenantHarness::create("test_neon_test_record")?;
let (tenant, ctx) = harness.load().await;

fn get_key(id: u32) -> Key {
// using aux key here b/c they are guaranteed to be inside `collect_keyspace`.
let mut key = Key::from_hex("620000000033333333444444445500000000").unwrap();
key.field6 = id;
key
}

let delta1 = vec![
(
get_key(1),
Lsn(0x20),
Value::WalRecord(NeonWalRecord::Test {
append: ",0x20".to_string(),
clear: false,
}),
),
(
get_key(1),
Lsn(0x30),
Value::WalRecord(NeonWalRecord::Test {
append: ",0x30".to_string(),
clear: false,
}),
),
(get_key(2), Lsn(0x10), Value::Image("0x10".into())),
(
get_key(2),
Lsn(0x20),
Value::WalRecord(NeonWalRecord::Test {
append: ",0x20".to_string(),
clear: false,
}),
),
(
get_key(2),
Lsn(0x30),
Value::WalRecord(NeonWalRecord::Test {
append: ",0x30".to_string(),
clear: false,
}),
),
];
let image1 = vec![(get_key(1), "0x10".into())];

let tline = tenant
.create_test_timeline_with_layers(
TIMELINE_ID,
Lsn(0x10),
DEFAULT_PG_VERSION,
&ctx,
vec![delta1], // delta layers
vec![(Lsn(0x10), image1)], // image layers
Lsn(0x50),
)
.await?;

assert_eq!(
tline.get(get_key(1), Lsn(0x50), &ctx).await?,
Bytes::from_static(b"0x10,0x20,0x30")
);
assert_eq!(
tline.get(get_key(2), Lsn(0x50), &ctx).await?,
Bytes::from_static(b"0x10,0x20,0x30")
);

Ok(())
}
}
7 changes: 6 additions & 1 deletion pageserver/src/walrecord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub enum NeonWalRecord {
file_path: String,
content: Option<Bytes>,
},

#[cfg(test)]
/// A testing record for unit testing purposes. It supports append data to an existing image, or clear it.
Test { append: String, clear: bool },
}

impl NeonWalRecord {
Expand All @@ -58,7 +62,8 @@ impl NeonWalRecord {
// If you change this function, you'll also need to change ValueBytes::will_init
match self {
NeonWalRecord::Postgres { will_init, rec: _ } => *will_init,

#[cfg(test)]
NeonWalRecord::Test { clear, .. } => *clear,
// None of the special neon record types currently initialize the page
_ => false,
}
Expand Down
8 changes: 8 additions & 0 deletions pageserver/src/walredo/apply_neon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ pub(crate) fn apply_in_neon(
let mut writer = page.writer();
dir.ser_into(&mut writer)?;
}
#[cfg(test)]
NeonWalRecord::Test { append, clear } => {
if *clear {
page.clear();
} else {
page.put_slice(append.as_bytes());
}
}
}
Ok(())
}
Expand Down

0 comments on commit 6e6356d

Please sign in to comment.