Skip to main content

orch/cluster/
command.rs

1use serde::{Deserialize, Serialize};
2use sqlx::SqliteConnection;
3
4use crate::store::control::MemoryEntry;
5use crate::store::dependencies::{self, NewDependency};
6use crate::store::jobs::{self, JobState};
7use crate::store::kv;
8use crate::store::tasks;
9use crate::store::tasks::{NewTask, StoreRouteCmd, TaskStatus, UpsertExternalCmd, apply};
10
11type Updates = Vec<(String, serde_json::Value)>;
12
13/// One replicated write. The proposer stamps `now`, so every node applies the same value
14#[derive(Serialize, Deserialize, Clone, Debug)]
15pub struct Request {
16    pub now: String,
17    pub cmd: Command,
18}
19
20#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
21#[serde(tag = "op", rename_all = "snake_case")]
22pub enum Command {
23    KvSet {
24        key: String,
25        value: String,
26    },
27    KvIncrement {
28        key: String,
29    },
30    KvInsertIfAbsent {
31        key: String,
32        value: String,
33    },
34    KvDelete {
35        key: String,
36    },
37    TaskCreate {
38        new: NewTask,
39        internal: bool,
40    },
41    TaskUpsertExternal(UpsertExternalCmd),
42    TaskUpdateStatusAndFields {
43        id: i64,
44        status: TaskStatus,
45        updates: Updates,
46    },
47    TaskUpdateStatus {
48        id: i64,
49        status: TaskStatus,
50        expected: Option<TaskStatus>,
51    },
52    TaskUpdateExternalId {
53        id: i64,
54        external_id: String,
55    },
56    TaskTouch {
57        id: i64,
58    },
59    TaskResetToNew {
60        id: i64,
61        #[serde(default)]
62        keep_branch: bool,
63    },
64    TaskSetFields {
65        id: i64,
66        updates: Updates,
67        silent: bool,
68    },
69    TaskIncrement {
70        id: i64,
71        field: String,
72    },
73    TaskResetCounters {
74        id: i64,
75        failure_only: bool,
76    },
77    TaskStoreRoute(StoreRouteCmd),
78    TaskSetEstimateIfZero {
79        id: i64,
80        estimate: u8,
81    },
82    TaskAppendMemory {
83        id: i64,
84        entry: MemoryEntry,
85    },
86    TaskMarkCleaned {
87        id: i64,
88    },
89    MoveRepo {
90        old: String,
91        new: String,
92    },
93    DepReplace {
94        task_id: i64,
95        rows: Vec<NewDependency>,
96    },
97    DepLinkKnown {
98        task_id: i64,
99        repo: String,
100        number: i64,
101    },
102    DepBlockClaim {
103        task_id: i64,
104    },
105    JobStateUpsert(JobState),
106    ChannelSubscribe {
107        channel: String,
108        thread_id: String,
109        repo: String,
110        topic_id: String,
111    },
112    ChannelUnsubscribe {
113        channel: String,
114        thread_id: String,
115        repo: String,
116    },
117}
118
119/// A failed command is `Err` on every node, so one bad command cannot stop the state machine
120#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
121pub enum Reply {
122    Done,
123    Bool(bool),
124    Int(i64),
125    Count(u64),
126    Value(String),
127    Unmet(Option<Vec<String>>),
128    Err(String),
129}
130
131impl Command {
132    /// A write through the log may not touch this node's own state: a local kv key, such as its
133    /// Raft position, or a local task column, such as the worktree path that cleanup removes
134    pub(crate) fn check_replicable(&self) -> anyhow::Result<()> {
135        let key = match self {
136            Command::KvSet { key, .. }
137            | Command::KvIncrement { key }
138            | Command::KvInsertIfAbsent { key, .. }
139            | Command::KvDelete { key } => Some(key),
140            _ => None,
141        };
142        if let Some(key) = key {
143            anyhow::ensure!(
144                !kv::is_local(key),
145                "{key} is local to each node, never replicated"
146            );
147        }
148
149        let updates = match self {
150            Command::TaskSetFields { updates, .. }
151            | Command::TaskUpdateStatusAndFields { updates, .. } => &updates[..],
152            _ => &[],
153        };
154        if let Some((col, _)) = updates.iter().find(|(col, _)| tasks::is_local_column(col)) {
155            anyhow::bail!("{col} is local to each node, never replicated");
156        }
157
158        Ok(())
159    }
160
161    /// `replicated` is true when Raft applies the command, so new ids come from the replicated
162    /// rows. A direct write keeps SQLite's AUTOINCREMENT, as orch on one machine always has
163    pub(crate) async fn apply(
164        self,
165        conn: &mut SqliteConnection,
166        now: &str,
167        replicated: bool,
168    ) -> anyhow::Result<Reply> {
169        if replicated {
170            self.check_replicable()?;
171        }
172
173        Ok(match self {
174            Command::KvSet { key, value } => {
175                kv::set(conn, now, &key, &value).await?;
176                Reply::Done
177            }
178            Command::KvIncrement { key } => Reply::Count(kv::increment(conn, now, &key).await?),
179            Command::KvInsertIfAbsent { key, value } => {
180                Reply::Value(kv::insert_if_absent(conn, now, &key, &value).await?)
181            }
182            Command::KvDelete { key } => {
183                kv::delete(conn, &key).await?;
184                Reply::Done
185            }
186            Command::TaskCreate { new, internal } => {
187                Reply::Int(apply::create(conn, now, &new, internal, replicated).await?)
188            }
189            Command::TaskUpsertExternal(ext) => {
190                Reply::Int(apply::upsert_external(conn, now, &ext, replicated).await?)
191            }
192            Command::TaskUpdateStatusAndFields {
193                id,
194                status,
195                updates,
196            } => {
197                apply::update_status_and_fields(conn, now, id, status, &updates).await?;
198                Reply::Done
199            }
200            Command::TaskUpdateStatus {
201                id,
202                status,
203                expected,
204            } => Reply::Bool(apply::update_status(conn, now, id, status, expected).await?),
205            Command::TaskUpdateExternalId { id, external_id } => {
206                apply::update_external_id(conn, now, id, &external_id).await?;
207                Reply::Done
208            }
209            Command::TaskTouch { id } => {
210                apply::touch(conn, now, id).await?;
211                Reply::Done
212            }
213            Command::TaskResetToNew { id, keep_branch } => {
214                apply::reset_to_new(conn, now, id, keep_branch).await?;
215                Reply::Done
216            }
217            Command::TaskSetFields {
218                id,
219                updates,
220                silent,
221            } => {
222                apply::set_fields(conn, now, id, &updates, silent).await?;
223                Reply::Done
224            }
225            Command::TaskIncrement { id, field } => {
226                Reply::Int(apply::increment(conn, now, id, &field).await?)
227            }
228            Command::TaskResetCounters { id, failure_only } => {
229                apply::reset_counters(conn, now, id, failure_only).await?;
230                Reply::Done
231            }
232            Command::TaskStoreRoute(route) => {
233                apply::store_route(conn, now, &route).await?;
234                Reply::Done
235            }
236            Command::TaskSetEstimateIfZero { id, estimate } => {
237                Reply::Bool(apply::set_estimate_if_zero(conn, now, id, estimate).await?)
238            }
239            Command::TaskAppendMemory { id, entry } => {
240                apply::append_memory(conn, now, id, &entry).await?;
241                Reply::Done
242            }
243            Command::TaskMarkCleaned { id } => {
244                apply::mark_cleaned(conn, now, id).await?;
245                Reply::Done
246            }
247            Command::MoveRepo { old, new } => {
248                let moved = dependencies::apply::move_repo(conn, &old, &new).await?;
249                Reply::Int(i64::try_from(moved)?)
250            }
251            Command::DepReplace { task_id, rows } => {
252                dependencies::apply::replace_dependencies(conn, task_id, &rows).await?;
253                Reply::Done
254            }
255            Command::DepLinkKnown {
256                task_id,
257                repo,
258                number,
259            } => {
260                dependencies::apply::link_known_task(conn, task_id, &repo, number).await?;
261                Reply::Done
262            }
263            Command::DepBlockClaim { task_id } => {
264                Reply::Unmet(dependencies::apply::block_claim(conn, now, task_id).await?)
265            }
266            Command::JobStateUpsert(state) => {
267                jobs::apply::upsert_job_state(conn, &state).await?;
268                Reply::Done
269            }
270            Command::ChannelSubscribe {
271                channel,
272                thread_id,
273                repo,
274                topic_id,
275            } => {
276                jobs::apply::subscribe(conn, &channel, &thread_id, &repo, &topic_id).await?;
277                Reply::Done
278            }
279            Command::ChannelUnsubscribe {
280                channel,
281                thread_id,
282                repo,
283            } => {
284                jobs::apply::unsubscribe(conn, &channel, &thread_id, &repo).await?;
285                Reply::Done
286            }
287        })
288    }
289}