Skip to content

Commit

Permalink
add init record + make test concise
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 12, 2024
1 parent 6e6356d commit 96b9a47
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
36 changes: 19 additions & 17 deletions pageserver/src/tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6703,7 +6703,7 @@ mod tests {

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

fn get_key(id: u32) -> Key {
Expand Down Expand Up @@ -6875,35 +6875,35 @@ mod tests {
(
get_key(1),
Lsn(0x20),
Value::WalRecord(NeonWalRecord::Test {
append: ",0x20".to_string(),
clear: false,
}),
Value::WalRecord(NeonWalRecord::wal_append(",0x20")),
),
(
get_key(1),
Lsn(0x30),
Value::WalRecord(NeonWalRecord::Test {
append: ",0x30".to_string(),
clear: false,
}),
Value::WalRecord(NeonWalRecord::wal_append(",0x30")),
),
(get_key(2), Lsn(0x10), Value::Image("0x10".into())),
(
get_key(2),
Lsn(0x20),
Value::WalRecord(NeonWalRecord::Test {
append: ",0x20".to_string(),
clear: false,
}),
Value::WalRecord(NeonWalRecord::wal_append(",0x20")),
),
(
get_key(2),
Lsn(0x30),
Value::WalRecord(NeonWalRecord::Test {
append: ",0x30".to_string(),
clear: false,
}),
Value::WalRecord(NeonWalRecord::wal_append(",0x30")),
),
(get_key(3), Lsn(0x10), Value::Image("0x10".into())),
(
get_key(3),
Lsn(0x20),
Value::WalRecord(NeonWalRecord::wal_clear()),
),
(get_key(4), Lsn(0x10), Value::Image("0x10".into())),
(
get_key(4),
Lsn(0x20),
Value::WalRecord(NeonWalRecord::wal_init()),
),
];
let image1 = vec![(get_key(1), "0x10".into())];
Expand All @@ -6928,6 +6928,8 @@ mod tests {
tline.get(get_key(2), Lsn(0x50), &ctx).await?,
Bytes::from_static(b"0x10,0x20,0x30")
);
// assert_eq!(tline.get(get_key(3), Lsn(0x50), &ctx).await?, Bytes::new());
// assert_eq!(tline.get(get_key(4), Lsn(0x50), &ctx).await?, Bytes::new());

Ok(())
}
Expand Down
40 changes: 38 additions & 2 deletions pageserver/src/walrecord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ pub enum NeonWalRecord {

#[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 },
Test {
/// Append a string to the image.
append: String,
/// Clear the image before appending.
clear: bool,
/// Treat this record as an init record. `clear` should be set to true if this field is set
/// to true. This record does not need the history WALs to reconstruct. See [`NeonWalRecord::will_init`] and
/// its references in `timeline.rs`.
will_init: bool,
},
}

impl NeonWalRecord {
Expand All @@ -63,11 +72,38 @@ impl NeonWalRecord {
match self {
NeonWalRecord::Postgres { will_init, rec: _ } => *will_init,
#[cfg(test)]
NeonWalRecord::Test { clear, .. } => *clear,
NeonWalRecord::Test { will_init, .. } => *will_init,
// None of the special neon record types currently initialize the page
_ => false,
}
}

#[cfg(test)]
pub(crate) fn wal_append(s: impl AsRef<str>) -> Self {
Self::Test {
append: s.as_ref().to_string(),
clear: false,
will_init: false,
}
}

#[cfg(test)]
pub(crate) fn wal_clear() -> Self {
Self::Test {
append: "".to_string(),
clear: true,
will_init: false,
}
}

#[cfg(test)]
pub(crate) fn wal_init() -> Self {
Self::Test {
append: "".to_string(),
clear: true,
will_init: true,
}
}
}

/// DecodedBkpBlock represents per-page data contained in a WAL record.
Expand Down
12 changes: 9 additions & 3 deletions pageserver/src/walredo/apply_neon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,18 @@ pub(crate) fn apply_in_neon(
dir.ser_into(&mut writer)?;
}
#[cfg(test)]
NeonWalRecord::Test { append, clear } => {
NeonWalRecord::Test {
append,
clear,
will_init,
} => {
if *will_init {
assert!(*clear, "init record must be clear to ensure correctness");
}
if *clear {
page.clear();
} else {
page.put_slice(append.as_bytes());
}
page.put_slice(append.as_bytes());
}
}
Ok(())
Expand Down

0 comments on commit 96b9a47

Please sign in to comment.