| @@ -100,6 +100,77 @@ pub(crate) fn repo_log(dir: &FsPath, did: &str) -> PathBuf { |
| | crate::store::account_layout(dir, did).log(0) |
| | } |
| | |
| | /// A copy of the data directory `dir` as it is now, beside it: what a |
| | /// server started again would find, for one to start on while the server |
| | /// on `dir`, which holds its logs, goes on. |
| | #[cfg(test)] |
| | pub(crate) fn copied(dir: &FsPath) -> PathBuf { |
| | let to = dir.with_extension("copy"); |
| | let _ = std::fs::remove_dir_all(&to); |
| | let mut todo = vec![dir.to_path_buf()]; |
| | while let Some(d) = todo.pop() { |
| | std::fs::create_dir_all(to.join(d.strip_prefix(dir).unwrap())).unwrap(); |
| | for e in std::fs::read_dir(&d).unwrap() { |
| | let p = e.unwrap().path(); |
| | if p.is_dir() { |
| | todo.push(p); |
| | } else { |
| | std::fs::copy(&p, to.join(p.strip_prefix(dir).unwrap())).unwrap(); |
| | } |
| | } |
| | } |
| | to |
| | } |
| | |
| | /// What the log at `path` holds: its file's bytes, without the room after |
| | /// its frames, which a server running, stopped or killed leaves there |
| | /// (`pds_store`). |
| | #[cfg(test)] |
| | pub(crate) fn log_bytes(path: &FsPath) -> Vec<u8> { |
| | without_room(std::fs::read(path).unwrap()) |
| | } |
| | |
| | /// A log's bytes without the room after its frames: the zeros they end with. |
| | #[cfg(test)] |
| | fn without_room(mut log: Vec<u8>) -> Vec<u8> { |
| | log.truncate(log.iter().rposition(|&b| b != 0).map_or(0, |last| last + 1)); |
| | log |
| | } |
| | |
| | /// The logs opened on each data directory, in tests alone, where one |
| | /// process starts a server on a directory again and again while tasks of |
| | /// the server before still hold it: a start closes that server's logs |
| | /// first, as the restart of a process ends its writers (`Wal::close`). |
| | #[cfg(test)] |
| | static OPENED: Mutex<Vec<(PathBuf, std::sync::Weak<Wal>)>> = Mutex::new(Vec::new()); |
| | |
| | /// A log as the server holds it, opened on the data directory `dir`. |
| | fn held(dir: &FsPath, wal: Wal) -> Arc<Wal> { |
| | let wal = Arc::new(wal); |
| | #[cfg(test)] |
| | OPENED |
| | .lock() |
| | .unwrap_or_else(PoisonError::into_inner) |
| | .push((dir.to_path_buf(), Arc::downgrade(&wal))); |
| | let _ = dir; |
| | wal |
| | } |
| | |
| | /// Close every log a server before opened on `dir` and still holds. |
| | #[cfg(test)] |
| | fn stop_writers(dir: &FsPath) { |
| | OPENED |
| | .lock() |
| | .unwrap_or_else(PoisonError::into_inner) |
| | .retain(|(of, wal)| match wal.upgrade() { |
| | Some(wal) if of == dir => { |
| | wal.close(); |
| | false |
| | } |
| | held => held.is_some(), |
| | }); |
| | } |
| | |
| | /// An account as `accounts.log` has it. |
| | pub(crate) struct AccountEntry { |
| | pub did: String, |
| @@ -511,7 +582,7 @@ pub(crate) fn open_repo(ctx: &Arc<Ctx>, did: &str, key: &SigningKey) -> Result<R |
| | Ok(Recovered { |
| | store: Store { |
| | repo, |
| | wal: Arc::new(wal), |
| | wal: held(dir, wal), |
| | blobs: small.blobs, |
| | takedowns: small.takedowns, |
| | spaces: small.spaces, |
| @@ -538,7 +609,7 @@ pub(crate) fn new_repo(ctx: &Arc<Ctx>, did: &str, key: &SigningKey) -> io::Resul |
| | } |
| | Ok(Store { |
| | repo: Repo::new(did.to_string(), key.clone()), |
| | wal: Arc::new(wal), |
| | wal: held(&ctx.dir, wal), |
| | blobs: Default::default(), |
| | takedowns: Default::default(), |
| | spaces: Default::default(), |
| @@ -608,6 +679,8 @@ impl Service { |
| | /// were reserved (`events::SEQS`). |
| | pub(crate) fn open(dir: &FsPath, config: Config) -> Result<Service, String> { |
| | use std::os::unix::fs::PermissionsExt; |
| | #[cfg(test)] |
| | stop_writers(dir); |
| | pds_store::make_dirs(dir).map_err(|e| format!("{}: {e}", dir.display()))?; |
| | std::fs::set_permissions(dir, std::fs::Permissions::from_mode(pds_store::DIR_MODE)) |
| | .map_err(|e| format!("{}: {e}", dir.display()))?; |
| @@ -765,7 +838,7 @@ impl Service { |
| | // A data directory from before seqs were reserved: every account's |
| | // log is read for the greatest seq it holds. |
| | let upgrade = ceiling.is_none() && accounts.count() > 0; |
| | let log = Arc::new(log); |
| | let log = held(dir, log); |
| | let logged_events = lifecycle |
| | .events |
| | .drain(..) |
| @@ -1072,8 +1145,22 @@ mod tests { |
| | .collect() |
| | } |
| | |
| | /// Every file in the data directory, and its bytes. |
| | /// Every file in the data directory, and its bytes: of a log, those it |
| | /// holds, without the room after its frames (`pds_store`), as a power |
| | /// cut the room did not outlive leaves it. |
| | fn files(dir: &Path) -> BTreeMap<PathBuf, Vec<u8>> { |
| | let mut files = files_whole(dir); |
| | for (_, bytes) in files |
| | .iter_mut() |
| | .filter(|(p, _)| p.extension().is_some_and(|e| e == "log")) |
| | { |
| | *bytes = super::without_room(std::mem::take(bytes)); |
| | } |
| | files |
| | } |
| | |
| | /// Every file in the data directory, and its bytes, room and all. |
| | fn files_whole(dir: &Path) -> BTreeMap<PathBuf, Vec<u8>> { |
| | let mut out = BTreeMap::new(); |
| | let mut todo = vec![dir.to_path_buf()]; |
| | while let Some(d) = todo.pop() { |
| @@ -1096,7 +1183,7 @@ mod tests { |
| | /// so that the logs recovery makes durable are mostly durable already. |
| | fn put_files(dir: &Path, files: &BTreeMap<PathBuf, Vec<u8>>) { |
| | let have = if dir.exists() { |
| | self::files(dir) |
| | files_whole(dir) |
| | } else { |
| | BTreeMap::new() |
| | }; |
| @@ -1110,6 +1197,12 @@ mod tests { |
| | } |
| | } |
| | |
| | /// A log, or the first of one, with `room` zeros after it: as a kill |
| | /// leaves a log written to again (`pds_store`). |
| | fn with_room(log: &[u8], room: usize) -> Vec<u8> { |
| | [log, &vec![0u8; room]].concat() |
| | } |
| | |
| | /// A step of the script: the files after it, and the state. |
| | struct Step { |
| | files: BTreeMap<PathBuf, Vec<u8>>, |
| @@ -1223,6 +1316,62 @@ mod tests { |
| | let _ = std::fs::remove_dir_all(&dir); |
| | } |
| | |
| | /// A data directory written before logs had room, each log its frames |
| | /// alone, is started on and carried on from. Its logs are given room as |
| | /// they are written to, which no count of their bytes includes; and a |
| | /// server killed with that room in them starts as one killed without. |
| | #[tokio::test] |
| | async fn logs_with_no_room_are_carried_on() { |
| | let plc = plc().await; |
| | let dir = scratch("room-src"); |
| | let (steps, accounts) = script(&dir, &plc).await; |
| | let last = steps.last().unwrap(); |
| | let work = scratch("room-work"); |
| | put_files(&work, &last.files); |
| | assert_eq!(files_whole(&work), last.files); |
| | let s = open(&work, &plc); |
| | assert_eq!(state(&s), last.state); |
| | for n in 0..3 { |
| | for (did, token) in &accounts { |
| | let body = json!({ "repo": did, "collection": "a.b.c", "rkey": format!("more{n}"), "record": record(n) }); |
| | call(&s, "com.atproto.repo.putRecord", Some(token), body) |
| | .await |
| | .unwrap(); |
| | } |
| | } |
| | let (killed, bare) = (files_whole(&work), files(&work)); |
| | for (did, _) in &accounts { |
| | let log = repo_log(Path::new(""), did); |
| | let (file, frames) = (killed[&log].len(), bare[&log].len()); |
| | assert!(file > frames, "{file} bytes of file, {frames} of frames"); |
| | assert_eq!(store::scan(&killed[&log]).1, frames); |
| | // The log's bytes are its frames', by its own count and by its file. |
| | let counted = s.account(did).unwrap().store.log_bytes(); |
| | assert_eq!(counted, frames as u64); |
| | assert_eq!(crate::directory::files_bytes(&work, did), counted); |
| | } |
| | let after = state(&s); |
| | drop(s); |
| | for (name, disk) in [("room-killed", &killed), ("room-bare", &bare)] { |
| | let again = scratch(name); |
| | put_files(&again, disk); |
| | let s = open(&again, &plc); |
| | assert_eq!(state(&s), after, "{name}"); |
| | let (bob, token) = &accounts[1]; |
| | let body = |
| | json!({ "repo": bob, "collection": "a.b.c", "rkey": "after", "record": record(9) }); |
| | call(&s, "com.atproto.repo.putRecord", Some(token), body) |
| | .await |
| | .unwrap(); |
| | let now = state(&s); |
| | drop(s); |
| | assert_eq!(state(&open(&again, &plc)), now, "{name}"); |
| | let _ = std::fs::remove_dir_all(&again); |
| | } |
| | let _ = std::fs::remove_dir_all(&dir); |
| | let _ = std::fs::remove_dir_all(&work); |
| | } |
| | |
| | /// Logs whose frames are intact but whose entries are not what the |
| | /// server wrote are refused with a reason, not a panic. |
| | #[tokio::test] |
| @@ -1454,8 +1603,14 @@ mod tests { |
| | call(&s, "com.atproto.repo.putRecord", Some(token), body) |
| | .await |
| | .unwrap(); |
| | let again = open(&work, &plc); |
| | assert_eq!(state(&again), state(&s), "{} cut at {cut}", path.display()); |
| | let now = state(&s); |
| | drop(s); |
| | assert_eq!( |
| | state(&open(&work, &plc)), |
| | now, |
| | "{} cut at {cut}", |
| | path.display() |
| | ); |
| | } |
| | } |
| | // A damaged byte anywhere in the frame drops it. |
| @@ -1476,6 +1631,78 @@ mod tests { |
| | let _ = std::fs::remove_dir_all(&work); |
| | } |
| | |
| | /// A frame written over room and cut at every byte, the room after the |
| | /// cut as it was: the shortest write the script made to an account's |
| | /// log, and the one it made to the service log alone. Recovery must |
| | /// give the state before that write or after it, and keep working, as |
| | /// where the file ends at the cut |
| | /// (`torn_writes_recover_to_before_or_after`). |
| | #[tokio::test] |
| | async fn writes_torn_in_room_recover_to_before_or_after() { |
| | let plc = plc().await; |
| | let dir = scratch("torn-room-src"); |
| | let (steps, accounts) = script(&dir, &plc).await; |
| | let (bob, token) = &accounts[1]; |
| | let work = scratch("torn-room-work"); |
| | let service = PathBuf::from(ACCOUNTS_LOG_FILE); |
| | // Of the steps that change one log, the service log or an account's, |
| | // the one of each kind whose frame is shortest. |
| | let mut shortest: BTreeMap<bool, (usize, &[Step], &PathBuf)> = BTreeMap::new(); |
| | for w in steps.windows(2).skip(2) { |
| | let changed: Vec<&PathBuf> = w[1] |
| | .files |
| | .keys() |
| | .filter(|p| w[0].files.get(*p) != w[1].files.get(*p)) |
| | .collect(); |
| | let [path] = changed[..] else { continue }; |
| | let len = w[1].files[path].len() - w[0].files[path].len(); |
| | let of = shortest.entry(*path == service).or_insert((len, w, path)); |
| | if len < of.0 { |
| | *of = (len, w, path); |
| | } |
| | } |
| | assert_eq!(shortest.len(), 2, "a write to each kind of log"); |
| | let mut cuts = 0; |
| | for (_, w, path) in shortest.into_values() { |
| | let (from, to) = (w[0].files[path].len(), w[1].files[path].len()); |
| | let whole = &w[1].files[path]; |
| | assert!(whole.starts_with(&w[0].files[path])); |
| | for cut in from..=to { |
| | // What of the frame reached the disk, then zeros to past |
| | // where it ends. |
| | let mut files = w[1].files.clone(); |
| | files.insert(path.clone(), with_room(&whole[..cut], to - cut + 1500)); |
| | put_files(&work, &files); |
| | let s = open(&work, &plc); |
| | let want = if cut == to { &w[1].state } else { &w[0].state }; |
| | assert_eq!( |
| | &state(&s), |
| | want, |
| | "{} cut at {cut} of {from}..{to}", |
| | path.display() |
| | ); |
| | cuts += 1; |
| | if cut % 61 == 0 || cut == from + 1 || cut == to { |
| | let body = json!({ "repo": bob, "collection": "a.b.c", "rkey": "after", "record": { "n": 9 } }); |
| | call(&s, "com.atproto.repo.putRecord", Some(token), body) |
| | .await |
| | .unwrap(); |
| | let now = state(&s); |
| | drop(s); |
| | assert_eq!( |
| | state(&open(&work, &plc)), |
| | now, |
| | "{} cut at {cut}", |
| | path.display() |
| | ); |
| | } |
| | } |
| | } |
| | assert!(cuts > 500, "{cuts}"); |
| | let _ = std::fs::remove_dir_all(&dir); |
| | let _ = std::fs::remove_dir_all(&work); |
| | } |
| | |
| | /// Cut `accounts.log` at every byte of each frame a change to sessions or |
| | /// app passwords wrote (and flip a byte in it): recovery must give the |
| | /// state before that change or after it. Each kind of entry is cut: the |
| @@ -1663,8 +1890,10 @@ mod tests { |
| | ) |
| | .await |
| | .unwrap(); |
| | assert_eq!(state(&open(&work, &plc)), state(&s)); |
| | assert_ne!(state(&s).1[&alice], steps.last().unwrap().state.1[&alice]); |
| | let now = state(&s); |
| | drop(s); |
| | assert_eq!(state(&open(&work, &plc)), now); |
| | assert_ne!(now.1[&alice], steps.last().unwrap().state.1[&alice]); |
| | let _ = std::fs::remove_dir_all(&dir); |
| | let _ = std::fs::remove_dir_all(&work); |
| | } |
| @@ -1874,7 +2103,9 @@ mod tests { |
| | ) |
| | .await |
| | .unwrap(); |
| | assert_eq!(state(&open(&work, &plc)), state(&s)); |
| | let now = state(&s); |
| | drop(s); |
| | assert_eq!(state(&open(&work, &plc)), now); |
| | let _ = std::fs::remove_dir_all(&dir); |
| | let _ = std::fs::remove_dir_all(&sink); |
| | let _ = std::fs::remove_dir_all(&work); |
| @@ -2074,7 +2305,9 @@ mod tests { |
| | ) |
| | .await |
| | .unwrap(); |
| | assert_eq!(state(&open(&work, &plc)), state(&s)); |
| | let now = state(&s); |
| | drop(s); |
| | assert_eq!(state(&open(&work, &plc)), now); |
| | let _ = std::fs::remove_dir_all(&dir); |
| | let _ = std::fs::remove_dir_all(&sink); |
| | let _ = std::fs::remove_dir_all(&work); |
| @@ -2677,8 +2910,10 @@ mod tests { |
| | ) |
| | .await |
| | .unwrap(); |
| | assert_eq!(spaces(&open(&work, &plc)), spaces(&s)); |
| | assert_ne!(spaces(&s), steps.last().unwrap().1); |
| | let now = spaces(&s); |
| | drop(s); |
| | assert_eq!(spaces(&open(&work, &plc)), now); |
| | assert_ne!(now, steps.last().unwrap().1); |
| | let _ = std::fs::remove_dir_all(&dir); |
| | let _ = std::fs::remove_dir_all(&work); |
| | } |
| @@ -2760,9 +2995,10 @@ mod tests { |
| | let s = open(&work, &plc); |
| | assert_eq!(&kept(&s), &last.1); |
| | notify::keep(&s, &alice, ¬ify::frames_of_each_kind(&q, &m)[0]); |
| | assert_eq!(kept(&open(&work, &plc)), kept(&s)); |
| | assert_ne!(kept(&s), last.1); |
| | let now = kept(&s); |
| | drop(s); |
| | assert_eq!(kept(&open(&work, &plc)), now); |
| | assert_ne!(now, last.1); |
| | // A malformed entry of each kind is refused with a reason. |
| | let space = json!(q); |
| | for bad in [ |
| @@ -3139,8 +3375,9 @@ mod tests { |
| | assert!(kem.is_some()); |
| | assert_eq!(kem, after.state.0[bob.as_str()].2); |
| | if cut % 61 == 0 { |
| | let again = open(&work, &plc); |
| | assert_eq!(state(&again), state(&s)); |
| | let now = state(&s); |
| | drop(s); |
| | assert_eq!(state(&open(&work, &plc)), now); |
| | } |
| | } |
| | let _ = std::fs::remove_dir_all(&dir); |
| @@ -3288,7 +3525,9 @@ mod tests { |
| | assert_eq!(lost.len(), 1, "{got:?}"); |
| | assert_eq!(*s.events.subscribe().borrow(), n + 1); |
| | let first = state(&s); |
| | assert_eq!(state(&open(&work, &plc)), first); |
| | drop(s); |
| | let s = open(&work, &plc); |
| | assert_eq!(state(&s), first); |
| | // A new event follows on, and every restart after numbers them alike. |
| | let body = json!({ "repo": a, "collection": "a.b.c", "rkey": "new", "record": { "n": 1 } }); |
| | call(&s, "com.atproto.repo.putRecord", Some(ta), body) |
| @@ -4252,9 +4491,9 @@ mod tests { |
| | let put = |did: &str, rkey: &str, n: usize| json!({ "repo": did, "collection": "a.b.c", "rkey": rkey, "record": { "t": "x".repeat(n) } }); |
| | call(&s, "com.atproto.repo.putRecord", Some(&token), put(&did, "a", 10)).await.unwrap(); |
| | let log = dir.join(repo_log(Path::new(""), &did)); |
| | let len = std::fs::metadata(&log).unwrap().len(); |
| | let len = log_bytes(&log).len() as u64; |
| | let before = state(&s); |
| | let largest = files(&dir).values().map(|b| b.len() as u64).max().unwrap(); |
| | let largest = files_whole(&dir).values().map(|b| b.len() as u64).max().unwrap(); |
| | set_limit(Resource::Fsize, Some(largest.max(len) + 8000)); |
| | assert_eq!(call(&s, "com.atproto.repo.putRecord", Some(&token), put(&did, "b", 40_000)).await, Err("InternalServerError".into())); |
| | assert_eq!(std::fs::metadata(&log).unwrap().len(), len); |
| |