Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/object_store_regression_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55785,3 +55785,96 @@ fn an_append_leaves_every_cached_range_of_the_page_alone() {
"the index lost cached ranges that no write intersected"
);
}

fn span_read_options() -> StoreOptions {
StoreOptions {
chunk_size: 1 << 20,
max_extent_bytes: 1 << 12,
page_size: 1 << 12,
..test_options()
}
}

/// Two objects appended in turn share a chunk, so each one's extents are separated by the
/// other's. The oracle is the bytes the test appended, not anything the store reports.
fn interleaved_appends(store: &mut MatrixObjectStore, rounds: usize) -> (Vec<u8>, Vec<u8>) {
store.create_bucket("bucket").unwrap();
let (mut a, mut b) = (Vec::new(), Vec::new());
for round in 0..rounds {
let piece_a: Vec<u8> = (0..90).map(|i| (round * 7 + i) as u8).collect();
let piece_b: Vec<u8> = (0..130).map(|i| (round * 13 + i + 5) as u8).collect();
store
.append_object("bucket", "a", piece_a.clone(), "application/octet-stream")
.unwrap();
store
.append_object("bucket", "b", piece_b.clone(), "application/octet-stream")
.unwrap();
a.extend_from_slice(&piece_a);
b.extend_from_slice(&piece_b);
}
(a, b)
}

#[test]
fn a_whole_object_read_of_interleaved_appends_reads_the_chunk_once_per_span() {
let mut store = MatrixObjectStore::new(span_read_options()).unwrap();
let (a, b) = interleaved_appends(&mut store, 200);
let extents = store.head_object("bucket", "a").unwrap().extents.len();
assert!(extents >= 200, "one extent per append, got {extents}");

let (read, stats) = store.get_object_with_read_stats("bucket", "a").unwrap();
assert_eq!(read.data, a, "the bytes read are the bytes appended");
println!("extents={extents} chunk_reads={}", stats.chunk_reads);
assert!(
(stats.chunk_reads as usize) * 4 < extents,
"{extents} extents were read with {} chunk reads: a span should serve many of them",
stats.chunk_reads
);
assert_eq!(store.get_object("bucket", "b").unwrap().data, b);
}

#[test]
fn a_span_read_does_not_depend_on_the_bytes_between_its_extents() {
let mut store = MatrixObjectStore::new(span_read_options()).unwrap();
let (a, _b) = interleaved_appends(&mut store, 120);
store.delete_object("bucket", "b").unwrap();
assert_eq!(
store.get_object("bucket", "a").unwrap().data,
a,
"deleting the neighbour left this object's own bytes readable"
);
let (partial, _) = store
.get_object_range("bucket", "a", 1000, 5000)
.map(|o| (o.data, ()))
.unwrap();
assert_eq!(partial, a[1000..6000].to_vec());
}

///
#[test]
#[ignore]
fn bench_whole_object_read_of_appended_object() {
for rounds in [250usize, 1000, 4000] {
let mut store = MatrixObjectStore::new(StoreOptions {
chunk_size: 64 << 20,
max_extent_bytes: 1 << 16,
page_size: 1 << 20,
..test_options()
})
.unwrap();
let (a, _b) = interleaved_appends(&mut store, rounds);
let extents = store.head_object("bucket", "a").unwrap().extents.len();
let reps = 20usize;
let start = std::time::Instant::now();
for _ in 0..reps {
let read = store.get_object("bucket", "a").unwrap();
assert_eq!(read.data.len(), a.len());
}
let per = start.elapsed().as_secs_f64() * 1e3 / reps as f64;
println!(
"rounds={rounds} extents={extents} bytes={} get_ms={per:.3} MB/s={:.1}",
a.len(),
a.len() as f64 / 1048576.0 / (per / 1e3)
);
}
}
98 changes: 98 additions & 0 deletions src/store/core/raft_apply_persistence/extent_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ impl MatrixObjectStore {
let mut stats = ChunkReadStats::default();
let mut chunk_reads = 0u64;
let mut object_cursor = 0u64;
// A SPAN READ SERVES SEVERAL EXTENTS WITH ONE QUORUM READ. Every extent read pays a whole
// chunk-range read -- admission, the page map, the head stats and a replica vote that
// hashes what it returns -- and an object built by appends has one extent per append, so
// a whole-object read of it was paying that fixed price once per append. It is only
// taken when the caller set no payload or chunk-count budget, because a span reads the
// bytes between two extents and a budget is a promise about what a read touches.
let spans_allowed = max_payload_bytes == 0 && max_chunk_read_count == 0;
let mut pending: Vec<(ObjectExtent, u64, u64)> = Vec::new();
let mut pending_end = 0u64;
macro_rules! flush_pending {
() => {
if !pending.is_empty() {
let batch = std::mem::take(&mut pending);
self.read_extent_batch(&batch, &mut out, &mut stats, &mut chunk_reads)?;
}
};
}
for extent in extents {
let extent_start = object_cursor;
let extent_end = object_cursor.checked_add(extent.length()).ok_or_else(|| {
Expand All @@ -344,6 +361,24 @@ impl MatrixObjectStore {
let read_end = range_end.min(extent_end);
let local_offset = read_start - extent_start;
let local_len = read_end - read_start;
if spans_allowed {
let abs = extent.offset() + local_offset;
let joins = match pending.first() {
Some((first, first_abs, _)) => {
first.chunk_id == extent.chunk_id
&& abs >= pending_end
&& abs - pending_end <= SPAN_MAX_GAP_BYTES
&& abs + local_len - *first_abs <= SPAN_MAX_BYTES
}
None => true,
};
if !joins {
flush_pending!();
}
pending.push((extent.clone(), abs, local_len));
pending_end = abs + local_len;
continue;
}
let remaining_budget = if max_payload_bytes == 0 {
0
} else {
Expand All @@ -365,6 +400,69 @@ impl MatrixObjectStore {
merge_page_read_stats(&mut stats, read_stats);
out.extend_from_slice(&payload);
}
flush_pending!();
Ok((out, stats))
}

/// Read one batch of consecutive extents of one chunk with a single chunk-range read when
/// that is possible, and one read per extent when it is not.
///
/// The batch is `(extent, absolute chunk offset, length)` in object order. A batch of one,
/// and a span that cannot be read, take the per-extent path -- the bytes between two extents
/// belong to another object that may since have been discarded, and a failed read of them
/// must not fail a read of this object, whose own bytes are all still there.
fn read_extent_batch(
&self,
batch: &[(ObjectExtent, u64, u64)],
out: &mut Vec<u8>,
stats: &mut ChunkReadStats,
chunk_reads: &mut u64,
) -> Result<(), ObjectError> {
if batch.len() > 1 {
let span_start = batch[0].1;
let (_, last_abs, last_len) = &batch[batch.len() - 1];
let span_len = last_abs + last_len - span_start;
if let Ok((span, read_stats)) = self.chunk_service.read_chunk_range_bounded_by_chunks(
&batch[0].0.chunk_id,
span_start,
span_len,
0,
0,
) {
let sliced = batch.iter().all(|(_, abs, len)| {
let from = abs - span_start;
usize::try_from(from + len).map_or(false, |end| end <= span.len())
});
if sliced {
*chunk_reads = chunk_reads.saturating_add(read_stats.chunk_reads);
merge_page_read_stats(stats, read_stats);
for (_, abs, len) in batch {
let from = (abs - span_start) as usize;
out.extend_from_slice(&span[from..from + *len as usize]);
}
return Ok(());
}
}
}
for (extent, abs, len) in batch {
let (payload, read_stats) = self.chunk_service.read_chunk_range_bounded_by_chunks(
&extent.chunk_id,
*abs,
*len,
0,
0,
)?;
*chunk_reads = chunk_reads.saturating_add(read_stats.chunk_reads);
merge_page_read_stats(stats, read_stats);
out.extend_from_slice(&payload);
}
Ok(())
}
}

/// The widest gap between two extents of one chunk that a span read will read across. Appends to
/// two objects that share a chunk interleave, so the neighbours of an object's extents are
/// usually another object's appends, a few hundred bytes each.
const SPAN_MAX_GAP_BYTES: u64 = 4096;
/// The longest span one read covers: a page, which is what one replica read already serves.
const SPAN_MAX_BYTES: u64 = 1 << 20;
Loading