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
113 changes: 107 additions & 6 deletions apps/origin-src/src/queue/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! The landing queue: how a target's authority lands changes on it, one
//! acceptance at a time.
//! The landing queue: how a target's authority lands changes and other
//! targets on it, one acceptance at a time.
//!
//! Landing a change lands its stack: the head of each change below it that
//! isn't on the target yet, from the bottom, with an acceptance each. A
Expand Down Expand Up @@ -27,6 +27,14 @@
//! 5. **Otherwise** this node signs an acceptance naming what landed, the
//! evidence and the policy, and appends it to the target's chain.
//!
//! Landing another target works the same way: its tip is merged from the
//! newest acceptances both targets' histories share, and the requirements
//! apply to the merged snapshot. A merge that changes nothing lands
//! nothing if either tip is in the other's history: the target has the
//! source's already, or the source has nothing more. Otherwise it lands,
//! with no evidence then, so the target's history records the source's tip
//! and later merges start from it.
//!
//! The queue of each target lives in `queue/<target id>/`: a `lock` held
//! while landing, so landings on one target never race.

Expand All @@ -48,7 +56,7 @@ use crate::policy::{Policy, PolicyError, Requirement};
use crate::store::{Store, StoreError};
use crate::targets::Head;

use session::Session;
use session::{Candidate, Session};

/// The landing queues of a store's targets, as run by one node.
#[derive(Debug)]
Expand All @@ -63,6 +71,8 @@ pub struct Queue<'a, P = Native> {
pub enum Proposal {
/// A change, as of its head.
Change(ChangeId),
/// Another target, as of its tip.
Target(TargetName),
}

/// How a landing went.
Expand All @@ -72,7 +82,7 @@ pub enum Landing {
Landed {
/// What landed.
proposal: Proposal,
/// The revision the acceptance names.
/// The revision or acceptance the acceptance names.
source: Source,
/// The acceptance.
acceptance: Head,
Expand All @@ -90,8 +100,9 @@ pub struct Pending {
pub onto: TargetName,
/// What lands.
pub proposal: Proposal,
/// What the evidence must be about: the revision that would land. A
/// conflict is about the revision that didn't merge.
/// What the evidence must be about: the revision that would land, or
/// the merged snapshot of a target. A conflict is about the revision,
/// or the acceptance, that didn't merge.
pub subject: RecordId,
/// Why it waits.
pub blocked: Blocked,
Expand Down Expand Up @@ -195,6 +206,89 @@ impl<'a, P: Platform> Queue<'a, P> {
Ok(landings)
}

/// Lands the tip of `source` on `onto`.
///
/// # Errors
///
/// Fails if either target doesn't exist or they're the same, this node
/// isn't the authority of `onto`, or the records cannot be read or
/// written.
pub fn land_target(
&self,
source: &TargetName,
onto: &TargetName,
) -> Result<Landing, LandError> {
if source == onto {
return Err(LandError::SameTarget(onto.clone()));
}
let mut session = self.session(onto)?;
let tip = self
.store
.head(source)?
.ok_or_else(|| LandError::UnknownTarget(source.clone()))?;
let state = self.store.get(tip.acceptance)?.body().state;
let tips = [session.tip.acceptance, tip.acceptance];
let history = session.history(&tips)?;
let base = history.merge_base(self.store, &tips)?;
let merge = self.store.merge(base, state, session.state)?;
let proposal = Proposal::Target(source.clone());
if !merge.conflicts.is_empty() {
let subject = RecordId::Acceptance(tip.acceptance);
return session.block(proposal, subject, &merge.conflicts);
}
// A merge that changes nothing has nothing to record either when
// one tip is in the other's history: the target has the source's
// already, or the source has nothing the target lacks.
let [ours, theirs] = tips;
let either = history.descends(ours, theirs) || history.descends(theirs, ours);
if merge.tree == session.state && either {
return Ok(Landing::Unchanged(proposal));
}
// Even a merge that changes nothing lands, so that the tip is in
// the target's history, and later merges start from it.
session.submit(Candidate {
kept_as: proposal.clone(),
proposal,
source: Source::Target(tip.acceptance),
subject: RecordId::Snapshot(merge.tree),
state: merge.tree,
trees: merge.trees,
revision: None,
})
}

/// Creates the target `name` at the tip of `from`, with a first
/// acceptance landing it.
///
/// # Errors
///
/// Fails if `name` exists, `from` doesn't, this node wouldn't be the new
/// target's authority, or the records cannot be read or written.
pub fn create_target(&self, name: &TargetName, from: &TargetName) -> Result<Head, LandError> {
let _lock = self.lock(name)?;
if self.store.head(name)?.is_some() {
return Err(LandError::TargetExists(name.clone()));
}
let (policy_head, policy) = Policy::in_force(self.store)?;
self.authorize(&policy, name)?;
let from = self
.store
.head(from)?
.ok_or_else(|| LandError::UnknownTarget(from.clone()))?;
let acceptance = AcceptanceBody {
target: name.id(),
seq: 0,
prev: None,
state: self.store.get(from.acceptance)?.body().state,
landed: vec![Source::Target(from.acceptance)],
evidence: Vec::new(),
policy: Some(policy_head),
}
.sign(self.key);
let acceptance = self.store.append(name, &acceptance)?;
Ok(Head { seq: 0, acceptance })
}

/// The policy in `state`, about to become the policy in force, if it's
/// a valid one that names an authority for `policy`, so the target
/// never locks up.
Expand Down Expand Up @@ -287,6 +381,7 @@ impl fmt::Display for Proposal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Change(change) => write!(f, "change {}", change.short()),
Self::Target(target) => write!(f, "`{target}`"),
}
}
}
Expand All @@ -297,6 +392,12 @@ pub enum LandError {
/// No target has this name.
#[error("no target is named `{0}`")]
UnknownTarget(TargetName),
/// `target new` was given a name already taken.
#[error("a target is already named `{0}`")]
TargetExists(TargetName),
/// A target was landed on itself.
#[error("cannot land `{0}` on itself")]
SameTarget(TargetName),
/// The policy names no authority for the target.
#[error("policy.toml names no authority for `{0}`: give it, or `policy`, a [targets] table")]
NoAuthority(TargetName),
Expand Down
35 changes: 34 additions & 1 deletion apps/origin-src/src/queue/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use origin_shared::{
RevisionId, Source, StatementId, TargetName, TreeId,
};

use crate::ancestry::Ancestry;
use crate::annotations::{AnnotateError, Annotations, Claim};
use crate::conflict::Conflicts;
use crate::policy::{Policy, PolicyError, Requirement};
Expand Down Expand Up @@ -199,7 +200,13 @@ impl<P: Platform> Session<'_, '_, P> {
None
};
let mut records: Vec<EncodedObject> = candidate.revision.into_iter().collect();
let requirements = self.policy.requirements(&self.onto).to_vec();
// A landing that changes nothing, which only records another
// target's tip, needs no evidence.
let requirements = if candidate.state == self.state {
Vec::new()
} else {
self.policy.requirements(&self.onto).to_vec()
};
let mut evidence = Vec::new();
let mut missing = Vec::new();
if !requirements.is_empty() {
Expand Down Expand Up @@ -316,4 +323,30 @@ impl<P: Platform> Session<'_, '_, P> {
};
Ok(Landing::Pending(pending))
}

/// The history of `tips`, the target's and another target's, to merge
/// that one from: the state of the newest acceptance both histories
/// share, or the [merge](Ancestry::merge_base) of several, if each
/// lacks another, as after targets landed each other. An acceptance's
/// history is the one before it, and the acceptances of other targets
/// it landed: a target's first lands the one it was created from, so
/// targets created from the same one share it.
///
/// The nearest shared acceptance can be an older one, through a target
/// that forked early and landed recently, so both histories are read
/// whole, each acceptance once.
pub(super) fn history(
&self,
tips: &[AcceptanceId],
) -> Result<Ancestry<AcceptanceId>, StoreError> {
Ancestry::walk(tips, |id| {
let acceptance = self.store().get(id)?;
let body = acceptance.body();
let landed = body.landed.iter().filter_map(|source| match *source {
Source::Target(landed) => Some(landed),
Source::Revision(_) => None,
});
Ok::<_, StoreError>((landed.chain(body.prev).collect(), body.state))
})
}
}
7 changes: 4 additions & 3 deletions apps/origin-src/src/queue/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ mod changes;
mod conflicts;
mod evidence;
mod policy;
mod targets;

use origin_platform::Native;
use origin_shared::{
Base, BlobId, ChangeId, Digest, EncodedObject, Inputs, Kind, NodeKey, PrincipalName, Record,
RecordId, RepoPath, Revision, RevisionId, Source, Statement, StatementId, TargetName, TreeId,
Verdict,
AcceptanceBody, Base, BlobId, ChangeId, Digest, EncodedObject, Inputs, Kind, NodeKey,
PrincipalName, Record, RecordId, RepoPath, Revision, RevisionId, Source, Statement,
StatementId, TargetName, TreeId, Verdict,
};

use super::*;
Expand Down
Loading