synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-store / src / blockdev_a2_suite.rs
| 1 | //! The A2 crash suite (`segment_tests.rs`, compiled here a second time, unchanged) |
| 2 | //! against the R1 raw block store. |
| 3 | //! |
| 4 | //! The suite reaches the store through `super::*`, so this module provides what it |
| 5 | //! names: `tests::temp_store_dir` makes a store directory holding a zero-filled device |
| 6 | //! file ([`blockdev_tests::TEST_DEVICE_FILE`]), which [`SynapseStore::open`] opens as a |
| 7 | //! raw block store (the crash children, which run the original `segment_tests` module, |
| 8 | //! reopen through the same hook). `fs` and `segment` give the suite's file-level |
| 9 | //! damage (read, overwrite, empty, or delete a segment file; list segments and index |
| 10 | //! files) its device meaning: a segment file is the data area of a committed or |
| 11 | //! headed slot, deleting it erases the slot header, and the small files are the |
| 12 | //! device's named metadata objects. Everything else passes through to `std::fs`. |
| 13 | //! |
| 14 | //! Limit: the pre-A2 upgrade test copies directory fixtures (there are no pre-A2 raw |
| 15 | //! block stores), so in this module it exercises the directory store again. |
| 16 | |
| 17 | use super::*; |
| 18 | |
| 19 | // Compiled a second time on purpose: the suite runs unchanged against the device. |
| 20 | #[allow(clippy::duplicate_mod)] |
| 21 | #[path = "segment_tests.rs"] |
| 22 | mod segment_tests; |
| 23 | |
| 24 | /// Open the device in `dir`, if `dir` is a raw block test store. |
| 25 | fn device_in(dir: &Path) -> Option<Arc<blockdev::BlockDevice>> { |
| 26 | let mut cfg = blockdev_tests::redirect(dir)?; |
| 27 | cfg.format = FormatPolicy::OpenExisting; |
| 28 | Some(blockdev::BlockDevice::open_path_unlocked(&cfg).expect("open the test device")) |
| 29 | } |
| 30 | |
| 31 | use std::sync::Arc; |
| 32 | |
| 33 | pub(crate) mod tests { |
| 34 | pub(crate) use crate::tests::make_signed_changeset; |
| 35 | use std::path::PathBuf; |
| 36 | |
| 37 | /// A new store directory holding a zero-filled raw device file. |
| 38 | pub(crate) fn temp_store_dir(label: &str) -> PathBuf { |
| 39 | let dir = crate::tests::temp_store_dir(&format!("r1dev-{label}")); |
| 40 | crate::blockdev_tests::make_device_file( |
| 41 | &dir.join(crate::blockdev_tests::TEST_DEVICE_FILE), |
| 42 | crate::blockdev_tests::TEST_DEVICE_BYTES, |
| 43 | ); |
| 44 | dir |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub(crate) mod segment { |
| 49 | pub(crate) use crate::segment::*; |
| 50 | use crate::SynapseResult; |
| 51 | use std::path::Path; |
| 52 | |
| 53 | /// Segment ids present: on the device in `dir`, or as files. |
| 54 | pub(crate) fn list_segments(dir: &Path) -> SynapseResult<Vec<u64>> { |
| 55 | match super::device_in(dir) { |
| 56 | Some(dev) => Ok(dev.list_segments().expect("list device segments")), |
| 57 | None => crate::segment::list_segments(dir), |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub(crate) mod fs { |
| 63 | use std::ffi::OsString; |
| 64 | pub(crate) use std::fs::{read_to_string, remove_dir_all}; |
| 65 | use std::io::{Error, ErrorKind, Result}; |
| 66 | use std::path::{Path, PathBuf}; |
| 67 | use std::sync::Arc; |
| 68 | |
| 69 | enum Target { |
| 70 | Segment(Arc<crate::blockdev::BlockDevice>, u64), |
| 71 | Object(Arc<crate::blockdev::BlockDevice>, String), |
| 72 | Plain, |
| 73 | } |
| 74 | |
| 75 | fn target(p: &Path) -> Target { |
| 76 | let (Some(dir), Some(name)) = (p.parent(), p.file_name().and_then(|n| n.to_str())) else { |
| 77 | return Target::Plain; |
| 78 | }; |
| 79 | if name == crate::blockdev_tests::TEST_DEVICE_FILE || name == "acks.txt" { |
| 80 | return Target::Plain; |
| 81 | } |
| 82 | let Some(dev) = super::device_in(dir) else { |
| 83 | return Target::Plain; |
| 84 | }; |
| 85 | match crate::segment::parse_segment_file_name(name) { |
| 86 | Some(id) => Target::Segment(dev, id), |
| 87 | None => Target::Object(dev, name.to_string()), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn not_found(p: &Path) -> Error { |
| 92 | Error::new( |
| 93 | ErrorKind::NotFound, |
| 94 | format!("{} not on the device", p.display()), |
| 95 | ) |
| 96 | } |
| 97 | |
| 98 | pub(crate) fn read(p: impl AsRef<Path>) -> Result<Vec<u8>> { |
| 99 | let p = p.as_ref(); |
| 100 | match target(p) { |
| 101 | Target::Segment(dev, id) => { |
| 102 | if !dev.header_valid(id)? { |
| 103 | return Err(not_found(p)); |
| 104 | } |
| 105 | let len = dev.geometry().segment_bytes as u32; |
| 106 | dev.read_segment_at(id, 0, len) |
| 107 | } |
| 108 | Target::Object(dev, name) => dev.read_named(&name).ok_or_else(|| not_found(p)), |
| 109 | Target::Plain => std::fs::read(p), |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Overwrite a segment's data area from byte 0 (the rest zeroed) and flush; give it a |
| 114 | /// header if it has none. |
| 115 | pub(crate) fn write(p: impl AsRef<Path>, bytes: impl AsRef<[u8]>) -> Result<()> { |
| 116 | let (p, bytes) = (p.as_ref(), bytes.as_ref()); |
| 117 | match target(p) { |
| 118 | Target::Segment(dev, id) => { |
| 119 | let mut data = bytes.to_vec(); |
| 120 | data.resize(dev.geometry().segment_bytes as usize, 0); |
| 121 | if !dev.header_valid(id)? { |
| 122 | dev.write_segment_header(id)?; |
| 123 | } |
| 124 | dev.write_segment(id, 0, &data)?; |
| 125 | dev.flush() |
| 126 | } |
| 127 | Target::Object(dev, name) => dev.write_named(&name, bytes), |
| 128 | Target::Plain => std::fs::write(p, bytes), |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// Delete a segment (erase its slot header) or a metadata object. |
| 133 | pub(crate) fn remove_file(p: impl AsRef<Path>) -> Result<()> { |
| 134 | let p = p.as_ref(); |
| 135 | match target(p) { |
| 136 | Target::Segment(dev, id) => dev.erase_segment_header(id), |
| 137 | Target::Object(dev, name) => { |
| 138 | if dev.remove_named_where(|n| n == name)? { |
| 139 | Ok(()) |
| 140 | } else { |
| 141 | Err(not_found(p)) |
| 142 | } |
| 143 | } |
| 144 | Target::Plain => std::fs::remove_file(p), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | pub(crate) struct Entry { |
| 149 | name: OsString, |
| 150 | path: PathBuf, |
| 151 | dir: bool, |
| 152 | } |
| 153 | |
| 154 | pub(crate) struct Kind(bool); |
| 155 | |
| 156 | impl Kind { |
| 157 | pub(crate) fn is_dir(&self) -> bool { |
| 158 | self.0 |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | impl Entry { |
| 163 | pub(crate) fn file_name(&self) -> OsString { |
| 164 | self.name.clone() |
| 165 | } |
| 166 | pub(crate) fn path(&self) -> PathBuf { |
| 167 | self.path.clone() |
| 168 | } |
| 169 | pub(crate) fn file_type(&self) -> Result<Kind> { |
| 170 | Ok(Kind(self.dir)) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// Directory listing; a raw block test store lists its device's objects and segments. |
| 175 | pub(crate) fn read_dir(p: impl AsRef<Path>) -> Result<std::vec::IntoIter<Result<Entry>>> { |
| 176 | let p = p.as_ref(); |
| 177 | let mut out = Vec::new(); |
| 178 | if let Some(dev) = super::device_in(p) { |
| 179 | let mut names = dev.list_named(); |
| 180 | names.extend( |
| 181 | dev.list_segments()? |
| 182 | .into_iter() |
| 183 | .map(crate::segment::segment_file_name), |
| 184 | ); |
| 185 | for name in names { |
| 186 | out.push(Ok(Entry { |
| 187 | path: p.join(&name), |
| 188 | name: name.into(), |
| 189 | dir: false, |
| 190 | })); |
| 191 | } |
| 192 | return Ok(out.into_iter()); |
| 193 | } |
| 194 | for e in std::fs::read_dir(p)? { |
| 195 | let e = e?; |
| 196 | out.push(Ok(Entry { |
| 197 | name: e.file_name(), |
| 198 | path: e.path(), |
| 199 | dir: e.file_type()?.is_dir(), |
| 200 | })); |
| 201 | } |
| 202 | Ok(out.into_iter()) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /// Control: the stores the suite opens here are raw block stores (no segment files, |
| 207 | /// no blob directory, no metadata files next to the device). |
| 208 | #[test] |
| 209 | fn r1_suite_stores_are_raw_block_stores() { |
| 210 | let dir = tests::temp_store_dir("control"); |
| 211 | { |
| 212 | let mut s = SynapseStore::open( |
| 213 | &dir, |
| 214 | FsyncPolicy::GROUP_COMMIT_DEFAULT, |
| 215 | RecoveryPolicy::StrictFailClosed, |
| 216 | StoreLimits { |
| 217 | segment_bytes: 64 * 1024, |
| 218 | ..StoreLimits::default() |
| 219 | }, |
| 220 | ) |
| 221 | .unwrap(); |
| 222 | assert!(matches!(s.medium, Medium::Block(_))); |
| 223 | let key = ed25519_dalek::SigningKey::from_bytes(&[3; 32]); |
| 224 | let (env, blobs) = |
| 225 | tests::make_signed_changeset(&key, "control", "src/c.rs", b"control", vec![], 1000); |
| 226 | s.ingest_changeset(env, &blobs).unwrap(); |
| 227 | s.checkpoint_and_compact(1000).unwrap(); |
| 228 | } |
| 229 | let names: Vec<String> = std::fs::read_dir(&dir) |
| 230 | .unwrap() |
| 231 | .map(|e| e.unwrap().file_name().to_string_lossy().into_owned()) |
| 232 | .collect(); |
| 233 | assert_eq!(names, vec![blockdev_tests::TEST_DEVICE_FILE.to_string()]); |
| 234 | let dev = device_in(&dir).unwrap(); |
| 235 | assert_eq!(dev.list_segments().unwrap(), vec![1]); |
| 236 | assert!(dev.read_named("checkpoint.v2").is_some()); |
| 237 | assert!(dev.read_named("wal.log").is_some()); |
| 238 | let _ = std::fs::remove_dir_all(&dir); |
| 239 | } |