11//! The landing queue: how a target's authority lands changes on it, one
2- //! acceptance at a time. It starts a repository's history with
3- //! [`Queue::init`], checking the policy it begins with.
2+ //! acceptance at a time.
3+ //!
4+ //! Landing a change lands its stack: the head of each change below it that
5+ //! isn't on the target yet, from the bottom, with an acceptance each. A
6+ //! change that diverged is refused. A stack stops at the first change that
7+ //! waits. For each revision:
8+ //!
9+ //! 1. **Rebase.** The changes it made to its base are merged onto the
10+ //! target's tip, file by file ([`Store::merge`]). A revision made on the
11+ //! tip lands as it is; any other is rebased into a new revision of its
12+ //! change, on the tip, by this node. A landing that would leave the
13+ //! target as it is lands nothing: the target already has the change.
14+ //! 2. **Conflicts.** If both sides changed a path, a `core.conflict`
15+ //! statement about the revision lists the paths, and the change waits,
16+ //! blocked, until a new revision of it merges.
17+ //! 3. **Policy.** The policy in force now must name this node as the
18+ //! target's authority.
19+ //! 4. **Otherwise** this node signs an acceptance naming what landed and
20+ //! the policy, and appends it to the target's chain.
21+ //!
22+ //! The queue of each target lives in `queue/<target id>/`: a `lock` held
23+ //! while landing, so landings on one target never race.
424
5- use origin_platform:: { Native , Platform } ;
25+ mod session;
26+
27+ use std:: fmt;
28+ use std:: fs;
29+ use std:: path:: PathBuf ;
30+
31+ use origin_platform:: { LockMode , LockedFile , Native , Platform } ;
632use origin_shared:: {
7- AcceptanceBody , AcceptanceId , EncodedObject , NodeKey , PrincipalName , Record , TargetName , Tree ,
33+ AcceptanceBody , AcceptanceId , ChangeId , EncodedObject , NodeKey , Principal , PrincipalName ,
34+ Record , RecordId , RepoPath , RevisionId , Source , StatementId , TargetId , TargetName , Tree ,
835 TreeId ,
936} ;
1037
38+ use crate :: annotations:: { AnnotateError , Annotations } ;
1139use crate :: policy:: { Policy , PolicyError } ;
1240use crate :: store:: { Store , StoreError } ;
41+ use crate :: targets:: Head ;
42+
43+ use session:: Session ;
1344
1445/// The landing queues of a store's targets, as run by one node.
1546#[ derive( Debug ) ]
@@ -19,13 +50,70 @@ pub struct Queue<'a, P = Native> {
1950 node : PrincipalName ,
2051}
2152
53+ /// What a landing lands.
54+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
55+ pub enum Proposal {
56+ /// A change, as of its head.
57+ Change ( ChangeId ) ,
58+ }
59+
60+ /// How a landing went.
61+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
62+ pub enum Landing {
63+ /// It landed.
64+ Landed {
65+ /// What landed.
66+ proposal : Proposal ,
67+ /// The revision the acceptance names.
68+ source : Source ,
69+ /// The acceptance.
70+ acceptance : Head ,
71+ } ,
72+ /// The target already has it, so nothing landed.
73+ Unchanged ( Proposal ) ,
74+ /// It waits.
75+ Pending ( Pending ) ,
76+ }
77+
78+ /// A landing that waits.
79+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
80+ pub struct Pending {
81+ /// The target it lands on.
82+ pub onto : TargetName ,
83+ /// What lands.
84+ pub proposal : Proposal ,
85+ /// The revision that didn't merge.
86+ pub subject : RecordId ,
87+ /// Why it waits.
88+ pub blocked : Blocked ,
89+ }
90+
91+ /// Why a landing waits.
92+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
93+ pub enum Blocked {
94+ /// Both sides changed these paths. The `core.conflict` statement lists
95+ /// them too.
96+ Conflict {
97+ /// The statement.
98+ statement : StatementId ,
99+ /// The paths, sorted.
100+ paths : Vec < RepoPath > ,
101+ } ,
102+ }
103+
22104impl < ' a , P : Platform > Queue < ' a , P > {
23105 /// The queues of `store`, run by the node `node`, which signs with
24106 /// `key`.
25107 pub fn new ( store : & ' a Store < P > , key : & ' a NodeKey , node : PrincipalName ) -> Self {
26108 Self { store, key, node }
27109 }
28110
111+ /// This node, which issues the statements and revisions the queue
112+ /// makes.
113+ fn principal ( & self ) -> Principal {
114+ Principal :: Node ( self . node . clone ( ) )
115+ }
116+
29117 /// Starts a repository's history: the `policy` target, holding `policy`
30118 /// as its `policy.toml`, and an empty `main`, both of which the policy
31119 /// must have this node order. Returns `main`'s first acceptance.
@@ -65,6 +153,36 @@ impl<'a, P: Platform> Queue<'a, P> {
65153 ) ?)
66154 }
67155
156+ /// Lands `change`, and the changes below it that `onto` doesn't have
157+ /// yet, from the bottom of the stack. Stops at the first that waits.
158+ ///
159+ /// # Errors
160+ ///
161+ /// Fails if this node isn't the target's authority, a change in the
162+ /// stack diverged, or the records cannot be read or written.
163+ pub fn land_change (
164+ & self ,
165+ change : ChangeId ,
166+ onto : & TargetName ,
167+ ) -> Result < Vec < Landing > , LandError > {
168+ let mut session = self . session ( onto) ?;
169+ let stack = session. stack ( change) ?;
170+ if stack. is_empty ( ) {
171+ return Ok ( vec ! [ Landing :: Unchanged ( Proposal :: Change ( change) ) ] ) ;
172+ }
173+ let proposal = Proposal :: Change ( change) ;
174+ let mut landings = Vec :: new ( ) ;
175+ for ( id, revision) in stack {
176+ let landing = session. land_revision ( & proposal, id, revision) ?;
177+ let waits = matches ! ( landing, Landing :: Pending ( _) ) ;
178+ landings. push ( landing) ;
179+ if waits {
180+ break ;
181+ }
182+ }
183+ Ok ( landings)
184+ }
185+
68186 /// The policy in `state`, about to become the policy in force, if it's
69187 /// a valid one that names an authority for `policy`, so the target
70188 /// never locks up.
@@ -78,6 +196,32 @@ impl<'a, P: Platform> Queue<'a, P> {
78196 Ok ( policy)
79197 }
80198
199+ /// The name of the lock file in each target's queue.
200+ const LOCK : & str = "lock" ;
201+
202+ /// The directory of `target`'s queue, created durably if it's missing.
203+ fn queue_dir ( & self , target : TargetId ) -> Result < PathBuf , StoreError > {
204+ let root = self . store . queue_dir ( ) ;
205+ let dir = root. join ( target. to_hex ( ) ) ;
206+ if !dir. is_dir ( ) {
207+ fs:: create_dir_all ( & dir) . map_err ( |source| StoreError :: io ( & dir, source) ) ?;
208+ for parent in [ & root, root. parent ( ) . unwrap_or ( & root) ] {
209+ self . store . sync_dir ( parent) ?;
210+ }
211+ }
212+ Ok ( dir)
213+ }
214+
215+ /// Locks the queue of `target`, waiting for the landing in progress.
216+ fn lock ( & self , target : & TargetName ) -> Result < LockedFile , StoreError > {
217+ let path = self . queue_dir ( target. id ( ) ) ?. join ( Self :: LOCK ) ;
218+ Ok ( LockedFile :: open (
219+ & self . store . platform ,
220+ & path,
221+ LockMode :: Exclusive ,
222+ ) ?)
223+ }
224+
81225 /// Checks that this node is the authority of `target` under `policy`,
82226 /// with the key it lists.
83227 fn authorize ( & self , policy : & Policy , target : & TargetName ) -> Result < ( ) , LandError > {
@@ -96,11 +240,43 @@ impl<'a, P: Platform> Queue<'a, P> {
96240 }
97241 Ok ( ( ) )
98242 }
243+
244+ /// Starts landing on `onto`: locks its queue, then reads the policy in
245+ /// force and the target's tip.
246+ fn session ( & self , onto : & TargetName ) -> Result < Session < ' _ , ' a , P > , LandError > {
247+ let lock = self . lock ( onto) ?;
248+ let tip = self
249+ . store
250+ . head ( onto) ?
251+ . ok_or_else ( || LandError :: UnknownTarget ( onto. clone ( ) ) ) ?;
252+ let ( policy_head, policy) = Policy :: in_force ( self . store ) ?;
253+ self . authorize ( & policy, onto) ?;
254+ Ok ( Session {
255+ queue : self ,
256+ onto : onto. clone ( ) ,
257+ policy_head,
258+ state : self . store . get ( tip. acceptance ) ?. body ( ) . state ,
259+ tip,
260+ annotations : Annotations :: open ( self . store ) ?,
261+ _lock : lock,
262+ } )
263+ }
264+ }
265+
266+ impl fmt:: Display for Proposal {
267+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
268+ match self {
269+ Self :: Change ( change) => write ! ( f, "change {}" , change. short( ) ) ,
270+ }
271+ }
99272}
100273
101274/// A failure to land.
102275#[ derive( Debug , thiserror:: Error ) ]
103276pub enum LandError {
277+ /// No target has this name.
278+ #[ error( "no target is named `{0}`" ) ]
279+ UnknownTarget ( TargetName ) ,
104280 /// The policy names no authority for the target.
105281 #[ error( "policy.toml names no authority for `{0}`: give it, or `policy`, a [targets] table" ) ]
106282 NoAuthority ( TargetName ) ,
@@ -120,10 +296,36 @@ pub enum LandError {
120296 /// The policy lists another key for this node.
121297 #[ error( "policy.toml lists another key for node:{0} than this node's" ) ]
122298 WrongKey ( PrincipalName ) ,
299+ /// No revision of the change is known.
300+ #[ error( "change {} has no revisions" , . 0 . short( ) ) ]
301+ UnknownChange ( ChangeId ) ,
302+ /// The change has several heads.
303+ #[ error(
304+ "change {} is divergent, so it cannot land: its heads are {}. Run `origin converge {}` \
305+ first",
306+ change. short( ) ,
307+ heads. iter( ) . map( RevisionId :: short) . collect:: <Vec <_>>( ) . join( ", " ) ,
308+ change. short( )
309+ ) ]
310+ Divergent {
311+ /// The change.
312+ change : ChangeId ,
313+ /// Its heads.
314+ heads : Vec < RevisionId > ,
315+ } ,
123316 /// Landing on `policy` would leave it without a valid policy.
124317 #[ error( "landing would leave `policy` without a valid policy.toml" ) ]
125318 InvalidPolicy ( #[ source] PolicyError ) ,
319+ /// The policy in force cannot be read.
320+ #[ error( transparent) ]
321+ Policy ( #[ from] PolicyError ) ,
322+ /// A conflict cannot be recorded.
323+ #[ error( transparent) ]
324+ Annotate ( #[ from] AnnotateError ) ,
126325 /// Records cannot be read or written.
127326 #[ error( transparent) ]
128327 Store ( #[ from] StoreError ) ,
129328}
329+
330+ #[ cfg( test) ]
331+ mod tests;
0 commit comments