Skip to content
Merged
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
20 changes: 17 additions & 3 deletions apps/origin-src/benches/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,22 @@ fn promotions_cached(bencher: Bencher<'_, '_>) {
let (_dir, store, target) = promotion_store();
let issuer: Principal = "node:queue".parse().unwrap();
let mut annotations = Annotations::open(&store).unwrap();
assert_eq!(annotations.promotions(target, &issuer).unwrap().len(), 9);
assert_eq!(
annotations
.promotions(RecordId::Revision(target), &issuer)
.unwrap()
.len(),
9
);
let index = Index::open(&store).unwrap();
bencher
.counter(ItemsCount::new(STATEMENTS))
.with_inputs(|| Annotations::new(&store, index.clone()))
.bench_local_refs(|annotations| annotations.promotions(target, &issuer).unwrap());
.bench_local_refs(|annotations| {
annotations
.promotions(RecordId::Revision(target), &issuer)
.unwrap()
});
}

/// The first search, which computes and remembers the digest of each of
Expand All @@ -202,5 +212,9 @@ fn promotions_uncached(bencher: Bencher<'_, '_>) {
fs::remove_file(&log).unwrap();
Annotations::open(&store).unwrap()
})
.bench_local_refs(|annotations| annotations.promotions(target, &issuer).unwrap());
.bench_local_refs(|annotations| {
annotations
.promotions(RecordId::Revision(target), &issuer)
.unwrap()
});
}
6 changes: 4 additions & 2 deletions apps/origin-src/src/annotations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ mod tests {
use std::sync::Barrier;
use std::thread;

use origin_shared::{Base, BlobId, Digest, Inputs, ObjectId, RepoPath, Revision, Verdict};
use origin_shared::{
Base, BlobId, Digest, Inputs, ObjectId, RepoPath, Revision, RevisionId, Verdict,
};

use super::*;
use crate::tree::tests::build;
Expand Down Expand Up @@ -440,7 +442,7 @@ mod tests {
let promoter = principal("node:queue");
Annotations::open(store)
.unwrap()
.promote(revision, &promoter)
.promote(RecordId::Revision(revision), &promoter)
.unwrap()
.into_iter()
.map(|(_, statement)| {
Expand Down
40 changes: 21 additions & 19 deletions apps/origin-src/src/annotations/promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use std::collections::HashSet;

use origin_platform::Platform;
use origin_shared::{Principal, Record, RecordId, RevisionId, Statement, StatementId, TreeId};
use origin_shared::{Principal, Record, RecordId, Statement, StatementId, TreeId};

use super::Annotations;
use crate::store::{Store, StoreError};
Expand Down Expand Up @@ -140,25 +140,27 @@ impl<P: Platform> Annotations<'_, P> {
self.index.remember_digests(self.store, &digests.concat())
}

/// The statements promoting results to `revision`, as `issuer` would
/// issue them, without writing them, sorted by kind: for each
/// [promotion group](Statement::promotion_group), the latest statement
/// about another record with the same input digest as the revision,
/// unless its issuer superseded it. Groups whose kind and issuer the
/// revision already has a result of, directly or promoted, are skipped:
/// a result that ran there wins.
/// Returns the statements that would promote results to `subject`, as
/// `issuer` would issue them, without writing them, sorted by kind. For
/// each [promotion group](Statement::promotion_group), it picks the latest
/// statement about another record whose input digest equals the subject's,
/// unless its issuer superseded it. It skips a group if the subject already
/// has a result of that kind from that issuer, direct or promoted: a result
/// that ran on the subject wins. A statement has no snapshot, so nothing is
/// promoted to one.
///
/// # Errors
///
/// Fails if a record or tree cannot be read, or computed digests cannot
/// be remembered.
/// Fails if a record or tree cannot be read, or the computed digests cannot
/// be saved to the index.
pub fn promotions(
&mut self,
revision: RevisionId,
subject: RecordId,
issuer: &Principal,
) -> Result<Vec<Statement>, StoreError> {
let subject = RecordId::Revision(revision);
let snapshot = self.store.get(revision)?.snapshot;
let Some(snapshot) = self.store.snapshot_of(subject)? else {
return Ok(Vec::new());
};
self.compute_digests()?;

// The results the revision has that hold, by kind and original
Expand Down Expand Up @@ -203,19 +205,19 @@ impl<P: Platform> Annotations<'_, P> {
Ok(promotions)
}

/// Issues the [`promotions`](Self::promotions) to `revision` as
/// `issuer`, returning them.
/// Issues the [`promotions`](Self::promotions) to `subject` as `issuer` and
/// returns them.
///
/// # Errors
///
/// Fails like [`Annotations::promotions`], or if the statements cannot
/// be written.
/// Fails like [`Annotations::promotions`], or if the statements cannot be
/// written.
pub fn promote(
&mut self,
revision: RevisionId,
subject: RecordId,
issuer: &Principal,
) -> Result<Vec<(StatementId, Statement)>, StoreError> {
let promotions = self.promotions(revision, issuer)?;
let promotions = self.promotions(subject, issuer)?;
let encoded: Vec<_> = promotions.iter().map(Statement::encode).collect();
self.store.write_all(&encoded)?;
Ok(encoded
Expand Down
20 changes: 15 additions & 5 deletions apps/origin-src/src/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
//! statement about the revision lists the paths, and the change waits
//! until a new revision of it merges.
//! 3. **Policy.** The policy in force must name this node as the target's
//! authority.
//! 4. **Otherwise** this node signs an acceptance naming what landed and
//! the policy, and appends it to the target's chain.
//! authority. Each of the target's requirements needs a
//! [claim](crate::annotations::Claim) about the revision that lands.
//! Results about other records with the same input digest are promoted
//! to it first, and a valid promotion claims what its original does, as
//! the original's issuer.
//! 4. **Missing evidence.** The landing waits until the statements are
//! issued and the landing is retried.
//! 5. **Otherwise** this node signs an acceptance naming what landed, the
//! evidence and the policy, and appends it to the target's chain.
//!
//! Each target's queue lives in `queue/<target id>/`. It holds a `lock`,
//! taken while landing so landings on one target never race.
Expand All @@ -38,7 +44,7 @@ use origin_shared::{
};

use crate::annotations::{AnnotateError, Annotations};
use crate::policy::{Policy, PolicyError};
use crate::policy::{Policy, PolicyError, Requirement};
use crate::store::{Store, StoreError};
use crate::targets::Head;

Expand Down Expand Up @@ -84,7 +90,8 @@ pub struct Pending {
pub onto: TargetName,
/// What lands.
pub proposal: Proposal,
/// The revision that didn't merge.
/// What the evidence must be about: the revision that would land. For a
/// conflict, the revision that did not merge.
pub subject: RecordId,
/// Why it waits.
pub blocked: Blocked,
Expand All @@ -93,6 +100,8 @@ pub struct Pending {
/// Why a landing waits.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Blocked {
/// The subject lacks these statements, which the target requires.
Evidence(Vec<Requirement>),
/// Both sides changed these paths. The `core.conflict` statement lists
/// them too.
Conflict {
Expand Down Expand Up @@ -260,6 +269,7 @@ impl<'a, P: Platform> Queue<'a, P> {
Ok(Session {
queue: self,
onto: onto.clone(),
policy,
policy_head,
state: self.store.get(tip.acceptance)?.body().state,
tip,
Expand Down
82 changes: 73 additions & 9 deletions apps/origin-src/src/queue/session.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
//! Landing one candidate: rebasing it onto the target's tip, then appending
//! its acceptance, or keeping it waiting on a conflict.
//! Landing one candidate: rebasing it onto the target's tip, checking the
//! evidence the policy requires, then appending its acceptance, or keeping
//! it waiting.

use std::collections::HashSet;

use origin_platform::{LockedFile, Platform};
use origin_shared::{
AcceptanceBody, AcceptanceId, Base, ChangeId, EncodedObject, Kind, Record, RecordId, Revision,
RevisionId, Source, TargetName, TreeId,
RevisionId, Source, StatementId, TargetName, TreeId,
};

use crate::annotations::Annotations;
use crate::annotations::{AnnotateError, Annotations, Claim};
use crate::conflict::Conflicts;
use crate::policy::{Policy, Requirement};
use crate::store::{Store, StoreError};
use crate::targets::Head;

Expand All @@ -20,7 +22,8 @@ use super::{Blocked, LandError, Landing, Pending, Proposal, Queue};
pub(super) struct Session<'q, 'a, P> {
pub(super) queue: &'q Queue<'a, P>,
pub(super) onto: TargetName,
/// The acceptance of `policy` holding the policy in force.
pub(super) policy: Policy,
/// The acceptance of `policy` that holds `policy`.
pub(super) policy_head: AcceptanceId,
/// The target's latest acceptance, and its snapshot.
pub(super) tip: Head,
Expand All @@ -33,7 +36,12 @@ pub(super) struct Session<'q, 'a, P> {
pub(super) struct Candidate {
/// What lands.
pub(super) proposal: Proposal,
/// What the queue keeps waiting, if the candidate waits: the landing of the
/// stack it is in.
pub(super) kept_as: Proposal,
pub(super) source: Source,
/// What the evidence must be about.
pub(super) subject: RecordId,
/// The target's snapshot once it lands.
pub(super) state: TreeId,
/// The trees the merge made, to write first.
Expand Down Expand Up @@ -146,7 +154,9 @@ impl<P: Platform> Session<'_, '_, P> {
if revision.base == tip {
return self.submit(Candidate {
proposal,
kept_as: stack.clone(),
source: Source::Revision(id),
subject: RecordId::Revision(id),
state: merge.tree,
trees: merge.trees,
revision: None,
Expand All @@ -163,29 +173,68 @@ impl<P: Platform> Session<'_, '_, P> {
let rebased_id = RevisionId::from(rebased.id());
self.submit(Candidate {
proposal,
kept_as: stack.clone(),
source: Source::Revision(rebased_id),
subject: RecordId::Revision(rebased_id),
state: merge.tree,
trees: merge.trees,
revision: Some(rebased),
})
}

/// Lands `candidate`. Writes the records in layers, each durable before the
/// next refers to it: first the trees, then the rebased revision and the
/// acceptance.
/// Checks the policy for `candidate`, and lands it if its evidence is there
/// or else keeps it waiting. Writes the records in layers, each durable
/// before the next refers to it: the trees, then the rebased revision, then
/// the promoted statements and the acceptance.
pub(super) fn submit(&mut self, candidate: Candidate) -> Result<Landing, LandError> {
let store = self.queue.store;
if !candidate.trees.is_empty() {
store.write_all(&candidate.trees)?;
}
let mut records: Vec<EncodedObject> = candidate.revision.into_iter().collect();
let requirements = self.policy.requirements(&self.onto).to_vec();
let mut evidence = Vec::new();
let mut missing = Vec::new();
if !requirements.is_empty() {
// Promotion reads the revision.
store.write_all(&records)?;
records.clear();
let promotions = self
.annotations
.promotions(candidate.subject, &self.queue.principal())?;
let mut claims = self.annotations.claims(candidate.subject)?;
for promotion in promotions {
let encoded = promotion.encode();
let claim = store.claim(encoded.id().into(), promotion)?;
claims.push(claim.map_err(AnnotateError::from)?);
records.push(encoded);
}
for requirement in requirements {
match self.evidence(&claims, &requirement) {
Some(id) if !evidence.contains(&id) => evidence.push(id),
Some(_) => {}
None => missing.push(requirement),
}
}
}
if !missing.is_empty() {
store.write_all(&records)?;
let pending = Pending {
onto: self.onto.clone(),
proposal: candidate.kept_as,
subject: candidate.subject,
blocked: Blocked::Evidence(missing),
};
return Ok(Landing::Pending(pending));
}

let acceptance = AcceptanceBody {
target: self.onto.id(),
seq: self.tip.seq + 1,
prev: Some(self.tip.acceptance),
state: candidate.state,
landed: vec![candidate.source],
evidence: Vec::new(),
evidence,
policy: Some(self.policy_head),
}
.sign(self.queue.key);
Expand All @@ -204,6 +253,21 @@ impl<P: Platform> Session<'_, '_, P> {
})
}

/// Returns the first of `claims` that meets `requirement`: of the required
/// kind and verdict, from an issuer the policy admits. For a promotion, the
/// original's issuer counts.
fn evidence(&self, claims: &[Claim], requirement: &Requirement) -> Option<StatementId> {
claims
.iter()
.find(|claim| {
let claimed = claim.claimed();
claimed.kind == requirement.kind
&& claimed.verdict == Some(requirement.verdict)
&& self.policy.admits(&requirement.issuer, &claimed.issuer)
})
.map(Claim::id)
}

/// Blocks a landing on `conflicts` and keeps it waiting. Issues a
/// `core.conflict` statement about `subject` listing them, unless this
/// node's latest one already lists the same.
Expand Down
4 changes: 3 additions & 1 deletion apps/origin-src/src/queue/tests/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ fn conflicts_block_until_a_revision_merges() {
panic!("{blocked:?}")
};
assert_eq!(pending.subject, RecordId::Revision(two));
let Blocked::Conflict { statement, paths } = &pending.blocked;
let Blocked::Conflict { statement, paths } = &pending.blocked else {
panic!("{pending:?}")
};
assert_eq!(paths, &[RepoPath::new("shared").unwrap()]);
let conflict = repo.store.get(*statement).unwrap();
assert_eq!(conflict.kind, Kind::CONFLICT);
Expand Down
Loading
Loading