Skip to main content

tembra_core/program/
mod.rs

1//! Language-neutral lifecycle programs. Adapters execute effects; this module decides their order.
2use crate::Reason;
3use crate::{
4    Code, Error, Host, Id, Migration, Owner, Resource, Result, Snapshot, Version,
5    host::{conflict, gone},
6};
7use std::collections::{BTreeMap, BTreeSet};
8
9#[derive(Clone, Debug)]
10pub struct Scope {
11    pub id: Id,
12    pub owner: Owner,
13    pub orb: Id,
14    pub parents: Vec<Id>,
15    pub dependencies: Vec<Id>,
16    pub resources: Vec<Id>,
17    pub state_version: Option<Version>,
18    pub migrate: bool,
19    pub active: bool,
20    pub closing: bool,
21}
22
23#[derive(Clone, Copy, Debug, Eq, PartialEq)]
24pub struct Replacement {
25    pub previous: Option<Id>,
26    pub next: Option<Id>,
27}
28
29/// These are execution requests, not policy hooks. Every platform runs the same program.
30#[derive(Clone, Debug, Eq, PartialEq)]
31pub enum Effect {
32    Stop {
33        scope: Id,
34        keep_listeners: bool,
35    },
36    Open {
37        resource: Id,
38    },
39    Close {
40        resource: Id,
41    },
42    Restore {
43        previous: Id,
44        resource: Id,
45    },
46    Prepare {
47        scope: Id,
48        previous: Option<Id>,
49    },
50    Validate {
51        scope: Id,
52        value: Id,
53    },
54    Initialize {
55        scope: Id,
56    },
57    Migrate {
58        scope: Id,
59        value: Option<Id>,
60        version: Version,
61    },
62    Adopt {
63        replacements: Vec<Replacement>,
64    },
65    Start {
66        scope: Id,
67    },
68    Drop {
69        scope: Id,
70    },
71}
72
73#[derive(Clone, Debug)]
74pub enum Step {
75    Execute(Vec<Effect>),
76    Wait { deadline: Option<u64> },
77    Prepared,
78    Complete,
79    Failed(Error),
80}
81
82#[derive(Clone, Debug)]
83pub struct Completion {
84    pub value: Option<Id>,
85    pub error: Option<Error>,
86}
87
88#[derive(Clone, Copy, Debug, PartialEq)]
89enum Phase {
90    Stop,
91    Gate,
92    CloseExclusive,
93    Open,
94    Prepare,
95    State,
96    Prepared,
97    Adopt,
98    Start,
99    Drop,
100    Complete,
101    AbortStop,
102    AbortClose,
103    Restore,
104    Resume,
105    AbortDrop,
106    Failed,
107}
108
109/// Only a change to one of these identities can unblock a waiting program.
110#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
111pub(crate) enum WaitKey {
112    Owner(Owner),
113    Scope(Id),
114    Object(Id),
115    Reference(Id),
116    HostScopes,
117    Resource(Id),
118    Program(Id),
119}
120
121#[derive(Clone, Debug)]
122pub(crate) struct Program {
123    pub(crate) ticket: Option<Id>,
124    pub(crate) replacements: Vec<Replacement>,
125    previous_scopes: BTreeMap<Id, Option<Id>>,
126    scope_set: BTreeSet<Id>,
127    scopes: Vec<Id>,
128    object: Option<Id>,
129    pub(crate) activation_scope: Option<Id>,
130    activation_owner: Option<Owner>,
131    phase: Phase,
132    pending: Vec<Effect>,
133    migrations: Vec<Migration>,
134    expected: BTreeMap<Id, Snapshot>,
135    objects: Vec<Id>,
136    orbs: Vec<Id>,
137    exclusive: Vec<Resource>,
138    retained: BTreeSet<Id>,
139    pub(crate) deadline: Option<u64>,
140    error: Option<Error>,
141    restored: BTreeMap<Id, Id>,
142    committed: bool,
143    running: bool,
144    cancelled_effects: bool,
145    pub(crate) waiting: BTreeSet<WaitKey>,
146}
147
148mod activation;
149mod adoption;
150mod cleanup;
151mod recovery;
152mod reload;
153mod scheduler;
154mod scopes;
155mod state;
156
157#[cfg(test)]
158mod tests;
159
160impl Program {
161    /// Schedule replacement recovery when the host closes.
162    pub(crate) fn close_reload(&mut self) -> bool {
163        if self.ticket.is_none() {
164            return false;
165        }
166        if !self.committed {
167            self.error.get_or_insert_with(|| {
168                Error::new(Code::Cancelled, Reason::ReloadAborted, "reload was aborted")
169            });
170        }
171        self.running = true;
172        true
173    }
174
175    /// Initialize one program with its scoped membership set.
176    fn new(scopes: Vec<Id>, phase: Phase) -> Self {
177        Self {
178            ticket: None,
179            replacements: Vec::new(),
180            previous_scopes: BTreeMap::new(),
181            scope_set: scopes.iter().copied().collect(),
182            scopes,
183            object: None,
184            activation_scope: None,
185            activation_owner: None,
186            phase,
187            pending: Vec::new(),
188            migrations: Vec::new(),
189            expected: BTreeMap::new(),
190            objects: Vec::new(),
191            orbs: Vec::new(),
192            exclusive: Vec::new(),
193            retained: BTreeSet::new(),
194            deadline: None,
195            error: None,
196            restored: BTreeMap::new(),
197            committed: false,
198            running: false,
199            cancelled_effects: false,
200            waiting: BTreeSet::new(),
201        }
202    }
203}