| 1 | //! client — the `ReplicaClient` trait + a generic conformance suite. |
| 2 | //! |
| 3 | //! Ported from [email protected] `replica_client.go`. The trait is the storage |
| 4 | //! abstraction every backend implements; `run_client_suite` is the reusable |
| 5 | //! conformance test that each backend (T6 file, T7 object_store) must pass. |
| 6 | //! |
| 7 | //! # DECISION: buffered I/O for the KEEP scope |
| 8 | //! Go's `ReplicaClient` uses `io.Reader`/`io.ReadCloser`. We take/return owned |
| 9 | //! byte buffers (`&[u8]` / `Vec<u8>`) instead. The `(offset, size)` parameters on |
| 10 | //! `open_ltx_file` preserve the partial-read capability the restore path relies |
| 11 | //! on (fetching just the page-index tail), so this is a buffering choice, not a |
| 12 | //! capability loss. L0 files are bounded in size; streaming large snapshots is a |
| 13 | //! noted follow-on (logged in OPEN_QUESTIONS). |
| 14 | |
| 15 | use crate::error::Result; |
| 16 | use crate::ltx::{self, FileInfo, Header, HEADER_FLAG_NO_CHECKSUM, VERSION}; |
| 17 | use crate::TXID; |
| 18 | use async_trait::async_trait; |
| 19 | |
| 20 | pub mod file; |
| 21 | pub mod object_store; |
| 22 | |
| 23 | /// Client for reading and writing LTX files on a replica backend. |
| 24 | /// |
| 25 | /// Ported from the `ReplicaClient` interface (replica_client.go:19-51). Methods |
| 26 | /// take a compaction `level` (0 = L0, the only level in the one-shot scope). |
| 27 | #[async_trait] |
| 28 | pub trait ReplicaClient: Send + Sync { |
| 29 | /// The backend type string (e.g. `"file"`, `"s3"`). |
| 30 | fn type_name(&self) -> &str; |
| 31 | |
| 32 | /// Initializes the client (idempotent; default no-op). |
| 33 | async fn init(&self) -> Result<()> { |
| 34 | Ok(()) |
| 35 | } |
| 36 | |
| 37 | /// Returns all LTX files for `level`, sorted ascending by `min_txid`, that |
| 38 | /// start at or after `seek`. `use_metadata=true` requests accurate header |
| 39 | /// timestamps (for timestamp-based restore) rather than fast listing times. |
| 40 | async fn ltx_files(&self, level: i32, seek: TXID, use_metadata: bool) -> Result<Vec<FileInfo>>; |
| 41 | |
| 42 | /// Reads `size` bytes of an LTX file starting at `offset`. `size == 0` means |
| 43 | /// read to end of file. Returns an `io::ErrorKind::NotFound` error (wrapped) |
| 44 | /// if the file does not exist. |
| 45 | async fn open_ltx_file( |
| 46 | &self, |
| 47 | level: i32, |
| 48 | min_txid: TXID, |
| 49 | max_txid: TXID, |
| 50 | offset: i64, |
| 51 | size: i64, |
| 52 | ) -> Result<Vec<u8>>; |
| 53 | |
| 54 | /// Writes an LTX file to the replica and returns its metadata. |
| 55 | async fn write_ltx_file( |
| 56 | &self, |
| 57 | level: i32, |
| 58 | min_txid: TXID, |
| 59 | max_txid: TXID, |
| 60 | data: &[u8], |
| 61 | ) -> Result<FileInfo>; |
| 62 | |
| 63 | /// Deletes the given LTX files. |
| 64 | async fn delete_ltx_files(&self, files: &[FileInfo]) -> Result<()>; |
| 65 | |
| 66 | /// Deletes all files on the replica. |
| 67 | async fn delete_all(&self) -> Result<()>; |
| 68 | } |
| 69 | |
| 70 | /// Builds a small, valid L0 LTX file for tests. Files use the `NoChecksum` flag |
| 71 | /// (matching real litestream L0 WAL-segment files), so any `min_txid`/`max_txid` |
| 72 | /// is valid without a pre-apply checksum. One 512-byte page filled with `seed`. |
| 73 | pub fn make_test_ltx_file(min_txid: TXID, max_txid: TXID, seed: u8) -> Vec<u8> { |
| 74 | let page_size: u32 = 512; |
| 75 | let pages = vec![(1u32, vec![seed; page_size as usize])]; |
| 76 | let header = Header { |
| 77 | version: VERSION, |
| 78 | flags: HEADER_FLAG_NO_CHECKSUM, |
| 79 | page_size, |
| 80 | commit: 1, |
| 81 | min_txid, |
| 82 | max_txid, |
| 83 | timestamp: 0, |
| 84 | pre_apply_checksum: 0, |
| 85 | wal_offset: 0, |
| 86 | wal_size: 0, |
| 87 | wal_salt1: 0, |
| 88 | wal_salt2: 0, |
| 89 | node_id: 0, |
| 90 | }; |
| 91 | ltx::encode_file(&header, &pages, 0).expect("encode test ltx file") |
| 92 | } |
| 93 | |
| 94 | /// Generic conformance suite every `ReplicaClient` backend must pass: empty |
| 95 | /// state, write, listing order, seek filtering, full + partial reads, not-found, |
| 96 | /// and deletion. Call from an async test; panics with a descriptive message on |
| 97 | /// the first violation. |
| 98 | /// |
| 99 | /// Ported from the shared `TestReplicaClient_*` behaviors in replica_client_test.go. |
| 100 | pub async fn run_client_suite<C: ReplicaClient>(client: &C) { |
| 101 | // 1. Starts empty. |
| 102 | client.delete_all().await.expect("delete_all"); |
| 103 | let files = client |
| 104 | .ltx_files(0, TXID(0), false) |
| 105 | .await |
| 106 | .expect("list empty"); |
| 107 | assert!(files.is_empty(), "replica should start empty"); |
| 108 | |
| 109 | // 2. Write a snapshot (txid 1) plus four incremental L0 files. |
| 110 | let mut written: Vec<(FileInfo, Vec<u8>)> = Vec::new(); |
| 111 | for i in 1u64..=5 { |
| 112 | let data = make_test_ltx_file(TXID(i), TXID(i), i as u8); |
| 113 | let info = client |
| 114 | .write_ltx_file(0, TXID(i), TXID(i), &data) |
| 115 | .await |
| 116 | .unwrap_or_else(|e| panic!("write txid {i}: {e}")); |
| 117 | assert_eq!(info.min_txid, TXID(i), "written min txid"); |
| 118 | assert_eq!(info.max_txid, TXID(i), "written max txid"); |
| 119 | assert_eq!(info.size, data.len() as i64, "written size"); |
| 120 | written.push((info, data)); |
| 121 | } |
| 122 | |
| 123 | // 3. Listing returns all files, ascending by min txid (the brief's sort |
| 124 | // invariant — must hold regardless of backend listing order). |
| 125 | let listed = client.ltx_files(0, TXID(0), false).await.expect("list all"); |
| 126 | assert_eq!(listed.len(), 5, "all files listed"); |
| 127 | let order: Vec<u64> = listed.iter().map(|f| f.min_txid.0).collect(); |
| 128 | assert_eq!( |
| 129 | order, |
| 130 | vec![1, 2, 3, 4, 5], |
| 131 | "files sorted ascending by min txid" |
| 132 | ); |
| 133 | |
| 134 | // 4. Seek filters to files starting at or after the given txid. |
| 135 | let seeked = client |
| 136 | .ltx_files(0, TXID(3), false) |
| 137 | .await |
| 138 | .expect("seek list"); |
| 139 | let order: Vec<u64> = seeked.iter().map(|f| f.min_txid.0).collect(); |
| 140 | assert_eq!(order, vec![3, 4, 5], "seek=3 yields txids >= 3"); |
| 141 | |
| 142 | // 5. Full read returns the exact written bytes, which still decode + verify. |
| 143 | for (info, data) in &written { |
| 144 | let got = client |
| 145 | .open_ltx_file(0, info.min_txid, info.max_txid, 0, 0) |
| 146 | .await |
| 147 | .expect("full read"); |
| 148 | assert_eq!(&got, data, "full read matches written bytes"); |
| 149 | ltx::decode_file(&got).expect("round-tripped file still verifies"); |
| 150 | } |
| 151 | |
| 152 | // 6. Partial reads: an explicit (offset,size) window, and size==0 → to EOF. |
| 153 | let (info, data) = &written[0]; |
| 154 | let mid = client |
| 155 | .open_ltx_file(0, info.min_txid, info.max_txid, 4, 10) |
| 156 | .await |
| 157 | .expect("partial read"); |
| 158 | assert_eq!(mid.as_slice(), &data[4..14], "offset+size window"); |
| 159 | let tail_off = (data.len() - 16) as i64; |
| 160 | let tail = client |
| 161 | .open_ltx_file(0, info.min_txid, info.max_txid, tail_off, 0) |
| 162 | .await |
| 163 | .expect("tail read"); |
| 164 | assert_eq!( |
| 165 | tail.as_slice(), |
| 166 | &data[data.len() - 16..], |
| 167 | "size==0 reads to EOF" |
| 168 | ); |
| 169 | |
| 170 | // 7. Opening a missing file is an error. |
| 171 | let missing = client.open_ltx_file(0, TXID(99), TXID(99), 0, 0).await; |
| 172 | assert!(missing.is_err(), "missing file must error"); |
| 173 | |
| 174 | // 8. Deleting a subset removes exactly those files. |
| 175 | let to_delete: Vec<FileInfo> = listed.iter().take(2).cloned().collect(); |
| 176 | client |
| 177 | .delete_ltx_files(&to_delete) |
| 178 | .await |
| 179 | .expect("delete subset"); |
| 180 | let after = client |
| 181 | .ltx_files(0, TXID(0), false) |
| 182 | .await |
| 183 | .expect("list after delete"); |
| 184 | assert_eq!(after.len(), 3, "two files deleted"); |
| 185 | let order: Vec<u64> = after.iter().map(|f| f.min_txid.0).collect(); |
| 186 | assert_eq!(order, vec![3, 4, 5], "the first two were removed"); |
| 187 | |
| 188 | // 9. delete_all clears everything. |
| 189 | client.delete_all().await.expect("delete_all"); |
| 190 | let empty = client |
| 191 | .ltx_files(0, TXID(0), false) |
| 192 | .await |
| 193 | .expect("list empty"); |
| 194 | assert!(empty.is_empty(), "delete_all clears the replica"); |
| 195 | } |