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
9 changes: 7 additions & 2 deletions apps/origin-src/benches/land.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ impl Repo {
}

fn land_on(&self, change: ChangeId, onto: &TargetName) {
let landings = self.queue().land_change(change, onto).unwrap();
let mut annotations = Annotations::open(&self.store).unwrap();
let landings = self
.queue()
.land_change(&mut annotations, change, onto)
.unwrap();
assert!(
matches!(landings[..], [Landing::Landed { .. }]),
"{landings:?}"
Expand Down Expand Up @@ -196,9 +200,10 @@ fn land_target_after_long_histories(bencher: Bencher<'_, '_>) {
repo.land_on(change, &feature);
})
.bench_local_values(|()| {
let mut annotations = Annotations::open(&repo.store).unwrap();
let landing = repo
.queue()
.land_target(&feature, &TargetName::MAIN)
.land_target(&mut annotations, &feature, &TargetName::MAIN)
.unwrap();
assert!(matches!(landing, Landing::Landed { .. }), "{landing:?}");
});
Expand Down
16 changes: 16 additions & 0 deletions apps/origin-src/src/annotations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ impl<'a, P: Platform> Annotations<'a, P> {
&self.index
}

/// Returns the index, so the next user can reuse it.
#[must_use]
pub fn into_index(self) -> Index {
self.index
}

/// Brings the index up to date with the records written since it was read.
/// See [`Index::refresh`].
///
/// # Errors
///
/// Fails like [`Index::refresh`].
pub fn refresh(&mut self) -> Result<(), StoreError> {
self.index.refresh(self.store)
}

/// Writes a statement and its payload. Issuing a statement the store
/// has is a no-op.
///
Expand Down
34 changes: 34 additions & 0 deletions apps/origin-src/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ impl Index {
Ok(Self::rebuild_locked(store, &index)?.index)
}

/// Catches up with the op log of `store`, as [`Index::open`] does, for a
/// process that keeps the index open. It reads only the op log entries
/// since the last catch-up. Reopens the index if the op log no longer has
/// those entries.
///
/// # Errors
///
/// Fails like [`Index::open`].
pub fn refresh<P: Platform>(&mut self, store: &Store<P>) -> Result<(), StoreError> {
if !self.catch_up(store)? {
*self = Self::open(store)?;
}
Ok(())
}

/// Reconstructs the index of `store` from its records, and replaces the
/// one on disk. Records the old index knew keep their arrival order.
/// Objects that are corrupt or don't decode are skipped, and reported on
Expand Down Expand Up @@ -1293,6 +1308,25 @@ mod tests {
);
}

#[test]
fn refreshing_catches_up_an_index_kept_open() {
let (_dir, store) = store();
let mut index = Index::open(&store).unwrap();
let first = revision(change(1), &[], "first");
store.write_all(slice::from_ref(&first)).unwrap();
index.refresh(&store).unwrap();
assert_eq!(index, Index::open(&store).unwrap());

// Once the op log dropped what it missed, it reads the index again.
for i in 0..100 {
let object = revision(change(2), &[], &format!("{i}"));
store.write_all(slice::from_ref(&object)).unwrap();
}
index.refresh(&store).unwrap();
assert_eq!(index.revisions(change(2)).len(), 100);
assert_eq!(index.heads(change(1)), [revision_id(&first)]);
}

#[test]
fn rejects_records_that_do_not_decode() {
let (_dir, store) = store();
Expand Down
24 changes: 17 additions & 7 deletions apps/origin-src/src/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ impl<'a, P: Platform> Queue<'a, P> {
/// from the bottom of the stack up, and returns the outcome of each. Stops
/// at the first one that waits, which the queue keeps as a landing of
/// `change`, so retrying it lands the rest of the stack once that one can.
/// Statements are read through `annotations`, whose index is brought up to
/// date first, so a caller that keeps one open avoids reading it again.
///
/// # Errors
///
Expand All @@ -136,10 +138,11 @@ impl<'a, P: Platform> Queue<'a, P> {
/// cannot be read or written.
pub fn land_change(
&self,
annotations: &mut Annotations<'a, P>,
change: ChangeId,
onto: &TargetName,
) -> Result<Vec<Landing>, LandError> {
let mut session = self.session(onto)?;
let mut session = self.session(annotations, onto)?;
let stack = session.stack(change)?;
if stack.is_empty() {
session.forget(&Proposal::Change(change))?;
Expand All @@ -158,7 +161,8 @@ impl<'a, P: Platform> Queue<'a, P> {
Ok(landings)
}

/// Lands the tip of `source` on `onto`.
/// Lands the tip of `source` on `onto`, reading statements through
/// `annotations` as [`Queue::land_change`] does.
///
/// # Errors
///
Expand All @@ -167,13 +171,14 @@ impl<'a, P: Platform> Queue<'a, P> {
/// written.
pub fn land_target(
&self,
annotations: &mut Annotations<'a, P>,
source: &TargetName,
onto: &TargetName,
) -> Result<Landing, LandError> {
if source == onto {
return Err(LandError::SameTarget(onto.clone()));
}
let mut session = self.session(onto)?;
let mut session = self.session(annotations, onto)?;
let tip = self
.store
.head(source)?
Expand Down Expand Up @@ -343,10 +348,15 @@ impl<'a, P: Platform> Queue<'a, P> {
Ok(())
}

/// Starts landing on `onto`: locks its queue, then reads the policy in
/// force and the target's tip.
fn session(&self, onto: &TargetName) -> Result<Session<'_, 'a, P>, LandError> {
/// Starts landing on `onto`: locks its queue, reads the policy in force and
/// the target's tip, and brings `annotations` up to date.
fn session<'q>(
&'q self,
annotations: &'q mut Annotations<'a, P>,
onto: &TargetName,
) -> Result<Session<'q, 'a, P>, LandError> {
let lock = self.lock(onto)?;
annotations.refresh()?;
let tip = self
.store
.head(onto)?
Expand All @@ -367,7 +377,7 @@ impl<'a, P: Platform> Queue<'a, P> {
policy_head,
state: self.store.get(tip.acceptance)?.body().state,
tip,
annotations: Annotations::open(self.store)?,
annotations,
_lock: lock,
})
}
Expand Down
4 changes: 3 additions & 1 deletion apps/origin-src/src/queue/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ pub(super) struct Session<'q, 'a, P> {
/// The target's latest acceptance, and its snapshot.
pub(super) tip: Head,
pub(super) state: TreeId,
pub(super) annotations: Annotations<'a, P>,
/// The statements, through an index brought up to date when the lock was
/// taken.
pub(super) annotations: &'q mut Annotations<'a, P>,
pub(super) _lock: LockedFile,
}

Expand Down
20 changes: 16 additions & 4 deletions apps/origin-src/src/queue/tests/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ fn refuses_divergent_and_abandoned_changes() {
let main = TargetName::MAIN;
let error = repo
.queue()
.land_change(ChangeId::from_bytes([1; 16]), &main)
.land_change(
&mut repo.annotations(),
ChangeId::from_bytes([1; 16]),
&main,
)
.unwrap_err();
assert!(matches!(error, LandError::Divergent { .. }), "{error}");

Expand All @@ -97,7 +101,11 @@ fn refuses_divergent_and_abandoned_changes() {
repo.store.write_all(&[abandon.encode()]).unwrap();
let error = repo
.queue()
.land_change(ChangeId::from_bytes([2; 16]), &main)
.land_change(
&mut repo.annotations(),
ChangeId::from_bytes([2; 16]),
&main,
)
.unwrap_err();
assert!(matches!(error, LandError::Abandoned(_)), "{error}");
}
Expand All @@ -108,12 +116,16 @@ fn only_the_authority_lands() {
repo.change_on_main(1, &[("a", "a")]);
let change = ChangeId::from_bytes([1; 16]);
let other = Queue::new(&repo.store, &repo.key, PrincipalName::new("other").unwrap());
let error = other.land_change(change, &TargetName::MAIN).unwrap_err();
let error = other
.land_change(&mut repo.annotations(), change, &TargetName::MAIN)
.unwrap_err();
assert!(matches!(error, LandError::NotAuthority { .. }), "{error}");

let impostor = NodeKey::from_bytes(&[8; 32]);
let queue = Queue::new(&repo.store, &impostor, PrincipalName::new("queue").unwrap());
let error = queue.land_change(change, &TargetName::MAIN).unwrap_err();
let error = queue
.land_change(&mut repo.annotations(), change, &TargetName::MAIN)
.unwrap_err();
assert!(matches!(error, LandError::WrongKey(_)), "{error}");
assert_eq!(repo.tip(&TargetName::MAIN).seq, 0);
}
Expand Down
6 changes: 5 additions & 1 deletion apps/origin-src/src/queue/tests/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ fn unresolved_conflicts_never_land() {
);
let error = repo
.queue()
.land_change(ChangeId::from_bytes([1; 16]), &TargetName::MAIN)
.land_change(
&mut repo.annotations(),
ChangeId::from_bytes([1; 16]),
&TargetName::MAIN,
)
.unwrap_err();
assert!(
matches!(&error, LandError::Unresolved { paths, .. } if *paths == [path.clone()]),
Expand Down
10 changes: 9 additions & 1 deletion apps/origin-src/src/queue/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,17 @@ impl Repo {

pub(crate) fn land(&self, change: u8) -> Vec<Landing> {
self.queue()
.land_change(ChangeId::from_bytes([change; 16]), &TargetName::MAIN)
.land_change(
&mut self.annotations(),
ChangeId::from_bytes([change; 16]),
&TargetName::MAIN,
)
.unwrap()
}

pub(crate) fn annotations(&self) -> Annotations<'_> {
Annotations::open(&self.store).unwrap()
}
}

fn change(id: u8) -> Proposal {
Expand Down
26 changes: 20 additions & 6 deletions apps/origin-src/src/queue/tests/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ fn collection_applies_a_policy_landed_while_it_waits() {
repo.revision(2, &[], tip, &[("policy.toml", &longer)]);
let landed = repo
.queue()
.land_change(ChangeId::from_bytes([2; 16]), &TargetName::POLICY)
.land_change(
&mut repo.annotations(),
ChangeId::from_bytes([2; 16]),
&TargetName::POLICY,
)
.unwrap();
assert!(matches!(landed[..], [Landing::Landed { .. }]), "{landed:?}");
drop(held);
Expand All @@ -82,9 +86,11 @@ fn policy_landings_keep_a_valid_policy() {
let tip = Base::Acceptance(repo.tip(&policy).acceptance);
for (change, toml) in [(1, "not = [valid"), (2, "[nodes]\n")] {
repo.revision(change, &[], tip, &[("policy.toml", toml)]);
let result = repo
.queue()
.land_change(ChangeId::from_bytes([change; 16]), &policy);
let result = repo.queue().land_change(
&mut repo.annotations(),
ChangeId::from_bytes([change; 16]),
&policy,
);
assert!(
matches!(
result,
Expand Down Expand Up @@ -113,7 +119,11 @@ fn policy_landings_keep_a_valid_policy() {
.unwrap();
let landings = repo
.queue()
.land_change(ChangeId::from_bytes([3; 16]), &policy)
.land_change(
&mut repo.annotations(),
ChangeId::from_bytes([3; 16]),
&policy,
)
.unwrap();
assert!(
matches!(landings[..], [Landing::Landed { .. }]),
Expand Down Expand Up @@ -146,7 +156,11 @@ fn each_policy_landing_is_governed_by_the_one_before() {
repo.revision(2, &[], Base::Revision(first), &[("policy.toml", &open)]);
let landings = repo
.queue()
.land_change(ChangeId::from_bytes([2; 16]), &policy)
.land_change(
&mut repo.annotations(),
ChangeId::from_bytes([2; 16]),
&policy,
)
.unwrap();
assert_eq!(landed(&landings[0]), first);
assert!(matches!(landings[1], Landing::Pending(_)), "{landings:?}");
Expand Down
Loading
Loading