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
75 changes: 70 additions & 5 deletions apps/origin-src/benches/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ fn inputs(paths: &[&str], definition: &str) -> Inputs {
.iter()
.map(|path| RepoPath::new(path).unwrap())
.collect(),
target_paths: Vec::new(),
definition: Digest::of(definition.as_bytes()),
toolchain: Digest::of(b"rustc 1.99.0"),
external: false,
}
}

/// The input digest of the 20k-file fixture, for inputs naming two
/// directories, a file and a missing path. Each run reads the trees anew.
#[divan::bench(sample_count = 50)]
fn input_digest(bencher: Bencher<'_, '_>) {
/// A store holding the snapshot of the 20k-file fixture, with a
/// `checks.toml`, and the inputs the digest benches declare: two
/// directories, a file and a missing path.
fn digest_store() -> (tempfile::TempDir, Store, TreeId, Inputs) {
let dir = tempfile::tempdir().unwrap();
FixtureTree::LARGE.write(dir.path()).unwrap();
fs::write(
dir.path().join("checks.toml"),
"[checks.test]\ncommand = \"true\"\n",
)
.unwrap();
let store = Store::new(dir.path().join(".origin"), Native::default());
store.init().unwrap();
let (snapshot, _) = Worktree::new(dir.path(), &store)
Expand All @@ -58,9 +64,68 @@ fn input_digest(bencher: Bencher<'_, '_>) {
&["dir0", "dir5/sub3", "dir12/sub7/file7820.rs", "missing"],
"cargo test",
);
(dir, store, snapshot, inputs)
}

/// The input digest of the fixture's snapshot. Each run reads the trees
/// anew.
#[divan::bench(sample_count = 50)]
fn input_digest(bencher: Bencher<'_, '_>) {
let (_dir, store, snapshot, inputs) = digest_store();
let trees = store.input_trees(RecordId::Snapshot(snapshot)).unwrap();
bencher.bench(|| {
Snapshots::new(&store)
.input_digest(snapshot, &inputs)
.input_digest(&mut trees.unwrap(), &inputs)
.unwrap()
});
}

/// The input digest of a revision three deep in a stack on an acceptance
/// of the fixture, which also reads `checks.toml` from the target: each
/// run reads the revision, the stack down to the acceptance, its state
/// and the trees anew.
#[divan::bench(sample_count = 50)]
fn input_digest_on_target(bencher: Bencher<'_, '_>) {
let (_dir, store, snapshot, inputs) = digest_store();
let inputs = Inputs {
target_paths: vec![RepoPath::new("checks.toml").unwrap()],
..inputs
};
let acceptance = AcceptanceBody {
target: TargetName::MAIN.id(),
seq: 0,
prev: None,
state: snapshot,
landed: Vec::new(),
evidence: Vec::new(),
policy: None,
}
.sign(&NodeKey::from_bytes(&[1; 32]))
.encode();
store.write_all(std::slice::from_ref(&acceptance)).unwrap();
let mut base = Base::Acceptance(acceptance.id().into());
let mut top = None;
for change in 0..3 {
let revision = Revision {
change: ChangeId::from_bytes([change; 16]),
predecessors: Vec::new(),
base,
deps: Vec::new(),
snapshot,
description: EncodedObject::blob(b"").id().into(),
author: "user:bench".parse().unwrap(),
}
.encode();
store.write_all(std::slice::from_ref(&revision)).unwrap();
let id = RevisionId::from(revision.id());
(base, top) = (Base::Revision(id), Some(id));
}
let subject = RecordId::Revision(top.unwrap());
bencher.bench(|| {
let mut trees = store.input_trees(subject).unwrap().unwrap();
Snapshots::new(&store)
.input_digest(&mut trees, &inputs)
.unwrap()
.unwrap()
});
}
Expand Down
16 changes: 15 additions & 1 deletion apps/origin-src/src/annotations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,25 @@ mod tests {
change: u8,
predecessors: &[RevisionId],
files: &[(&str, &str)],
) -> RevisionId {
revision_on_state(store, target, &[], change, predecessors, files)
}

/// Writes a revision like [`revision_on`], on a first acceptance whose
/// state holds `state`.
pub(super) fn revision_on_state(
store: &Store,
target: &TargetName,
state: &[(&str, &str)],
change: u8,
predecessors: &[RevisionId],
files: &[(&str, &str)],
) -> RevisionId {
let acceptance = AcceptanceBody {
target: target.id(),
seq: 0,
prev: None,
state: build(store, &[]),
state: build(store, state),
landed: Vec::new(),
evidence: Vec::new(),
policy: None,
Expand Down Expand Up @@ -496,6 +509,7 @@ mod tests {
.iter()
.map(|path| RepoPath::new(path).unwrap())
.collect(),
target_paths: Vec::new(),
definition: Digest::of(kind.as_bytes()),
toolchain: Digest::of(b"rustc 1.99.0"),
external,
Expand Down
111 changes: 93 additions & 18 deletions apps/origin-src/src/annotations/promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::collections::HashSet;
use origin_platform::Platform;
use origin_shared::api::Annotation;
use origin_shared::{
Acceptance, Base, Principal, Record, RecordId, Statement, StatementId, TargetId,
Acceptance, Base, Principal, Record, RecordId, Statement, StatementId, TargetId, TreeId,
};

use super::Annotations;
Expand Down Expand Up @@ -137,14 +137,11 @@ impl<P: Platform> Annotations<'_, P> {
let Some(inputs) = &statement.inputs else {
continue;
};
let digest = self
.store
.snapshot_of(statement.subject)
.and_then(|snapshot| {
snapshot
.map(|snapshot| snapshots.input_digest(snapshot, inputs))
.transpose()
});
let digest = match self.store.input_trees(statement.subject) {
Ok(Some(mut trees)) => snapshots.input_digest(&mut trees, inputs),
Ok(None) => Ok(None),
Err(error) => Err(error),
};
match digest {
Ok(Some(digest)) => digests.push((id, digest)),
Ok(None) | Err(StoreError::NotFound(_)) => {}
Expand Down Expand Up @@ -179,8 +176,8 @@ impl<P: Platform> Annotations<'_, P> {
issuer: &Principal,
mut sees: impl FnMut(RecordId) -> Result<bool, StoreError>,
) -> Result<Vec<Statement>, StoreError> {
let (Some(snapshot), Some(target)) = (
self.store.snapshot_of(subject)?,
let (Some(mut trees), Some(target)) = (
self.store.input_trees(subject)?,
self.store.target_of(subject)?,
) else {
return Ok(Vec::new());
Expand All @@ -206,7 +203,9 @@ impl<P: Platform> Annotations<'_, P> {
if held.contains(&(first.kind.clone(), first.issuer.clone())) {
continue;
}
let digest = snapshots.input_digest(snapshot, inputs)?;
let Some(digest) = snapshots.input_digest(&mut trees, inputs)? else {
continue;
};
for &id in statements.iter().rev() {
if self.index.digest(id) != Some(digest) {
continue;
Expand Down Expand Up @@ -308,15 +307,16 @@ impl<P: Platform> Store<P> {
if target.is_none() || target != self.target_of(original.subject)? {
return Ok(Err(InvalidPromotion::OtherTarget));
}
let (Some(here), Some(there), Some(inputs)) = (
self.snapshot_of(statement.subject)?,
self.snapshot_of(original.subject)?,
let (Some(mut here), Some(mut there), Some(inputs)) = (
self.input_trees(statement.subject)?,
self.input_trees(original.subject)?,
&original.inputs,
) else {
return Ok(Err(InvalidPromotion::NoSnapshot));
};
let mut snapshots = Snapshots::new(self);
if snapshots.input_digest(here, inputs)? != snapshots.input_digest(there, inputs)? {
let here = snapshots.input_digest(&mut here, inputs)?;
if here.is_none() || here != snapshots.input_digest(&mut there, inputs)? {
return Ok(Err(InvalidPromotion::InputsDiffer));
}
Ok(Ok(Claim {
Expand Down Expand Up @@ -346,6 +346,19 @@ impl<P: Platform> Store<P> {
.map(|acceptance| acceptance.body().target))
}

/// The state of the target a revision based on `base` is on: that of
/// the acceptance it leads to, as [`Store::target_of`] finds it, or
/// `None` if the store lacks a record on the way.
///
/// # Errors
///
/// Fails if a record cannot be read.
pub fn base_state(&self, base: Base) -> Result<Option<TreeId>, StoreError> {
Ok(self
.base_acceptance(base)?
.map(|acceptance| acceptance.body().state))
}

/// The acceptance `base` leads to, through the revisions below it, or
/// `None` if the store lacks a record on the way.
fn base_acceptance(&self, mut base: Base) -> Result<Option<Acceptance>, StoreError> {
Expand Down Expand Up @@ -391,15 +404,77 @@ pub enum InvalidPromotion {

#[cfg(test)]
mod tests {
use origin_shared::{EncodedObject, Kind, TargetName, Verdict};
use origin_shared::{
AcceptanceId, AttachmentId, ChangeId, Digest, EncodedObject, Inputs, Kind, ObjectId,
RepoPath, Revision, RevisionId, TargetName, Verdict,
};

use super::*;
use crate::annotations::AnnotateError;
use crate::annotations::tests::{
FILES, check, issue, principal, promote, revision, revision_on, store,
FILES, check, issue, principal, promote, revision, revision_on, revision_on_state, store,
};
use crate::index::Index;

#[test]
fn target_paths_are_read_from_the_base() {
let (_dir, store) = store();
let inputs = Inputs {
paths: vec![RepoPath::new("src").unwrap()],
target_paths: vec![RepoPath::new("checks.toml").unwrap()],
definition: Digest::of(b"cargo test"),
toolchain: Digest::of(b"rustc 1.99.0"),
external: false,
};
let digest = |revision: RevisionId| {
let mut trees = store.input_trees(RecordId::Revision(revision)).unwrap();
Snapshots::new(&store)
.input_digest(trees.as_mut().unwrap(), &inputs)
.unwrap()
};
let files = [("src/lib.rs", "v1"), ("checks.toml", "its own")];
let on = |state: &[(&str, &str)]| {
revision_on_state(&store, &TargetName::MAIN, state, 1, &[], &files)
};
let below = on(&[("checks.toml", "old"), ("src/lib.rs", "v0")]);
let old = digest(below);
assert!(old.is_some());
assert_ne!(
digest(on(&[("checks.toml", "new"), ("src/lib.rs", "v0")])),
old,
"the base's checks.toml is an input"
);
assert_eq!(
digest(on(&[
("checks.toml", "old"),
("src/lib.rs", "v2"),
("docs", "x")
])),
old,
"the rest of the base isn't"
);

// A stack is on its bottom's base, and a revision whose base the
// store lacks on none.
let based = |base, change| {
let revision = Revision {
change: ChangeId::from_bytes([change; 16]),
predecessors: Vec::new(),
base,
deps: Vec::new(),
snapshot: store.get(below).unwrap().snapshot,
description: AttachmentId::from(EncodedObject::attachment(b"").id()),
author: principal("user:alice"),
}
.encode();
store.write_all(std::slice::from_ref(&revision)).unwrap();
RevisionId::from(revision.id())
};
assert_eq!(digest(based(Base::Revision(below), 2)), old);
let missing = AcceptanceId::from(ObjectId::from_bytes([7; 32]));
assert_eq!(digest(based(Base::Acceptance(missing), 3)), None);
}

#[test]
fn results_promote_within_their_target_only() {
let (_dir, store) = store();
Expand Down
1 change: 1 addition & 0 deletions apps/origin-src/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ impl AnnotateArgs {
let digest = |arg: &Option<DigestArg>| arg.map_or_else(|| Digest::of(b""), |arg| arg.0);
(!self.inputs.is_empty()).then(|| Inputs {
paths: self.inputs.clone(),
target_paths: Vec::new(),
definition: digest(&self.definition),
toolchain: digest(&self.toolchain),
external: self.external,
Expand Down
1 change: 1 addition & 0 deletions apps/origin-src/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ mod tests {
.unwrap();
result.inputs = Some(Inputs {
paths: Vec::new(),
target_paths: Vec::new(),
definition: Digest::of(b"cargo test"),
toolchain: Digest::of(b"rustc"),
external: false,
Expand Down
1 change: 1 addition & 0 deletions apps/origin-src/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,7 @@ grants = { all = "none", "user:ada" = "write" }
verdict: Some(origin_shared::Verdict::Pass),
inputs: Some(origin_shared::Inputs {
paths: vec![path("README.md")],
target_paths: Vec::new(),
definition: origin_shared::Digest::of(b"test"),
toolchain: origin_shared::Digest::of(b""),
external: false,
Expand Down
1 change: 1 addition & 0 deletions apps/origin-src/src/queue/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub(crate) fn ci_test(subject: RevisionId, issuer: &str) -> Statement {
issuer: issuer.parse().unwrap(),
inputs: Some(Inputs {
paths: vec![RepoPath::new("src").unwrap()],
target_paths: Vec::new(),
definition: Digest::of(b"cargo test"),
toolchain: Digest::of(b"rustc"),
external: false,
Expand Down
Loading