Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
18 changes: 16 additions & 2 deletions core/crates/layerfs-storage/src/encoding/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,22 @@ impl GroupCache {
Some(&entry.body)
}

/// Retains authenticated decoded input within the same byte allowance.
/// Duplicate immutable input is idempotent; changed or oversized input fails.
/// Borrows retained input without recording recency: for a guess that
/// must leave the cache as it found it (`read/hints.rs` `chain_ahead`).
pub(crate) fn peek(&self, pack_id: i64, group_number: usize) -> Option<&[u8]> {
self.bodies
.get(&(pack_id, group_number))
.map(|entry| entry.body.as_slice())
}

/// Retains decoded input within the same byte allowance, evicting the least
/// recently used bodies. What the input is depends on the owner: a reader's
/// or Save's cache holds bodies its decoding authenticated, while the
/// private per-call cache of `read/hints.rs` `chain_ahead` (filled through
/// `delta::read::cached_base`) holds unauthenticated bytes for guesses that
/// nothing trusts; no other owner reads it and it is dropped when that call
/// returns. Duplicate immutable input is idempotent; changed or oversized
/// input fails.
pub fn insert(
&mut self,
pack_id: i64,
Expand Down
19 changes: 17 additions & 2 deletions core/crates/layerfs-storage/src/encoding/delta/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
//! fit the budgets in the same way. Exactly one trial runs, with the pooled
//! producer and its match budget; the program is kept only when it is shorter
//! than the whole record, and for a directory page only when it saves at least a
//! quarter of it (`worth`). Every refusal stores the page whole, which
//! is what every Store before this form holds.
//! quarter of it (`worth`). A program against a re-based root is kept only when
//! it is at most half the whole record as well (`select::rebased_delta_kept`,
//! #758), so a page that drifts from its root starts a new chain instead of
//! anchoring every read of it to a growing program. Every refusal stores the
//! page whole, which is what every Store before this form holds.

use layerfs_content::{ObjectId, ObjectRole};

Expand Down Expand Up @@ -45,6 +48,10 @@ pub struct BranchCounters {
pub work_exceeded: u64,
/// Predecessors replaced by the whole branch at the root of their chain (#89).
pub rebased: u64,
/// Programs against a re-based root that `worth` keeps but that are longer
/// than half the whole record, stored whole instead (#758): only the
/// decisions the rule changed, as `DeltaCounters::rebased_full`.
pub rebased_full: u64,
/// Branches stored, by chain depth (whole is 0).
pub depths: super::select::DepthHistogram,
}
Expand All @@ -60,6 +67,7 @@ impl BranchCounters {
(&mut self.depth_capped, other.depth_capped),
(&mut self.work_exceeded, other.work_exceeded),
(&mut self.rebased, other.rebased),
(&mut self.rebased_full, other.rebased_full),
] {
*total = total.saturating_add(value);
}
Expand Down Expand Up @@ -115,6 +123,7 @@ fn trial(
}
let capacities = input.capacities;
let mut base_id = base_id;
let mut rebased = false;
let (mut base, mut chain) = acquire(input, location)?;
let capped = chain.depth >= capacities.metadata_delta_max_depth;
if capped {
Expand All @@ -133,6 +142,7 @@ fn trial(
return Err(crate::error::StorageError::Integrity("branch chain root"));
}
base_id = root.object_id;
rebased = true;
let branch = counters(input.counters, role);
branch.rebased = branch.rebased.saturating_add(1);
} else if capped {
Expand All @@ -154,6 +164,11 @@ fn trial(
if !worth(role, program.len(), full.record.len()) {
return Ok(None);
}
if rebased && !select::rebased_delta_kept(program.len(), full.record.len()) {
let branch = counters(input.counters, role);
branch.rebased_full = branch.rebased_full.saturating_add(1);
return Ok(None);
}
let branch = counters(input.counters, role);
branch.delta_branches = branch.delta_branches.saturating_add(1);
note_depth(&mut branch.depths, chain.depth.saturating_add(1));
Expand Down
138 changes: 104 additions & 34 deletions core/crates/layerfs-storage/src/encoding/delta/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub struct Resolver<'a> {
workspace: &'a mut DecompressionWorkspace,
counters: &'a mut ChainCounters,
packs_read: u64,
/// Identities the current resolution walked, for the hint fallback.
walked: Vec<ObjectId>,
}

impl<'a> Resolver<'a> {
Expand All @@ -116,6 +118,7 @@ impl<'a> Resolver<'a> {
workspace,
counters,
packs_read: 0,
walked: Vec::new(),
}
}

Expand Down Expand Up @@ -164,13 +167,38 @@ impl<'a> Resolver<'a> {
self.resolve_charged(root, false)
}

/// One resolution; when it fails in a way a stale delta-chain hint can
/// cause and a hinted locator was in use, it is rebuilt once from
/// authoritative locators (#758). A second failure stands.
fn resolve_charged(
&mut self,
root: ObjectLocation,
charge_self: bool,
) -> StorageResult<(Vec<u8>, ObjectId)> {
match self.resolve_once(root, charge_self) {
Err(error) if crate::read::hint_failure(&error) => {
let walked = std::mem::take(&mut self.walked);
if !self.connection.drop_hints(&walked)? {
return Err(error);
}
let root = self
.connection
.location(root.object_id, self.ceiling)?
.ok_or(StorageError::ObjectMissing(root.object_id))?;
self.resolve_once(root, charge_self)
}
other => other,
}
}

fn resolve_once(
&mut self,
root: ObjectLocation,
charge_self: bool,
) -> StorageResult<(Vec<u8>, ObjectId)> {
*self.counters = ChainCounters::default();
self.packs_read = 0;
self.walked = vec![root.object_id];
if root.pack_id > self.ceiling {
return Err(StorageError::VisibilityCeiling {
pack_id: root.pack_id,
Expand Down Expand Up @@ -248,6 +276,7 @@ impl<'a> Resolver<'a> {
return Err(StorageError::Integrity("dependency cycle"));
}
chain.push(current);
self.walked.push(current.object_id);
let Some(base) = self.base_of(&current)? else {
break;
};
Expand Down Expand Up @@ -508,7 +537,7 @@ pub fn stored_base(
view,
bytes: selected,
} = selected;
match header.lane {
let base = match header.lane {
PackLane::Ordinary => {
let body: &[u8] = match view.codec {
crate::pack::layout::GroupCodec::Raw => selected,
Expand All @@ -530,42 +559,82 @@ pub fn stored_base(
if body.len() != view.decoded_length {
return Err(StorageError::Integrity("group body length"));
}
let record = crate::encoding::decode::framed_record(body, location.record_number)?;
crate::encoding::delta::record::stored_base(
PackLane::Ordinary,
record,
location.canonical_length,
location.role,
)
record_base(PackLane::Ordinary, body, location)
}
PackLane::WholeFile => {
let record = crate::encoding::decode::framed_record(selected, location.record_number)?;
crate::encoding::delta::record::stored_base(
PackLane::WholeFile,
record,
location.canonical_length,
location.role,
)
}
PackLane::Chunk => {
let record = crate::encoding::decode::framed_record(selected, location.record_number)?;
crate::encoding::delta::record::stored_base(
PackLane::Chunk,
record,
location.canonical_length,
location.role,
)
}
PackLane::Singleton => {
let record = crate::encoding::decode::framed_record(selected, 0)?;
crate::encoding::delta::record::stored_base(
PackLane::Singleton,
record,
location.canonical_length,
location.role,
)
lane => record_base(lane, selected, location),
}?;
connection.bind_base(location, base)?;
Ok(base)
}

/// The base the record at `location` of `body` names: an ordinary lane's
/// decoded group body, or another lane's encoded group.
fn record_base(
lane: PackLane,
body: &[u8],
location: &ObjectLocation,
) -> StorageResult<Option<ObjectId>> {
let at = match lane {
PackLane::Singleton => 0,
_ => location.record_number,
};
let record = crate::encoding::decode::framed_record(body, at)?;
crate::encoding::delta::record::stored_base(
lane,
record,
location.canonical_length,
location.role,
)
}

/// The base the record at `location` names, read only from bytes `packs`
/// and `groups` already hold, leaving both as they were (#758): a guess
/// for a locate, never trusted, so any miss or malformed byte is `None`.
/// The record is not authenticated and `location` may be wrong. A group
/// neither `groups` nor the caller's own `decoded` cache holds is
/// decompressed, counted in `decodes` and kept in `decoded` (when it fits
/// that cache's byte bound), so the caller decodes it again only after
/// `decoded` evicted it or when it was too large to keep.
pub(crate) fn cached_base(
packs: &crate::encoding::PackCache,
groups: &GroupCache,
decoded: &mut GroupCache,
workspace: &mut DecompressionWorkspace,
decodes: &mut u64,
location: &ObjectLocation,
) -> Option<ObjectId> {
let selected = packs.peek_group(location.pack_id, location.group_number)?;
let fresh;
let body: &[u8] = match (selected.header.lane, selected.view.codec) {
(PackLane::Ordinary, crate::pack::layout::GroupCodec::Zstandard) => {
let key = (location.pack_id, location.group_number);
match groups.peek(key.0, key.1) {
Some(cached) => cached,
None if decoded.peek(key.0, key.1).is_some() => decoded.peek(key.0, key.1)?,
None => {
*decodes = decodes.saturating_add(1);
let body = workspace
.decompress_group(selected.bytes, selected.view.decoded_length)
.ok()?;
// A body over the bound is used once and not kept.
if body.len() > crate::policy::DECODED_GROUP_CACHE_BYTES {
fresh = body;
&fresh
} else {
decoded.insert(key.0, key.1, body).ok()?;
decoded.peek(key.0, key.1)?
}
}
}
}
_ => selected.bytes,
};
if selected.header.lane == PackLane::Ordinary && body.len() != selected.view.decoded_length {
return None;
}
record_base(selected.header.lane, body, location)
.ok()
.flatten()
}

/// Adds one resolved chain's work to an operation's totals.
Expand Down Expand Up @@ -593,6 +662,7 @@ fn group_of<'b>(
connection: &dyn Source,
location: &ObjectLocation,
) -> StorageResult<(crate::encoding::GroupSlice<'b>, bool)> {
connection.prefetch_chain(location, packs)?;
let (fetched, _) =
packs.acquire_groups(connection, location.pack_id, &[location.group_number])?;
let selected = packs
Expand Down
Loading