1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Which node runs a task, for assignment in a cluster
use std::collections::HashMap;
use super::{Task, TaskStatus};
use crate::store::TaskStore;
use crate::store::{Command, Reply};
impl TaskStore {
/**
* True when another node runs the task, so this node's tmux sessions say nothing about it. A
* task with no owner runs on the leader, as before assignment existed. Always false on one
* machine
**/
pub fn runs_elsewhere(&self, task: &Task) -> bool {
let Some(node) = self.node() else {
return false;
};
match task.owner_node.as_deref() {
Some(owner) => owner != node.name(),
None => !node.is_leader(),
}
}
/**
* True when startup reconciliation on this node may act on the task's worktree. A task another
* node holds is that node's. On a follower, a task with no owner is the leader's to decide,
* except a done one, whose worktree here only waits for removal
**/
pub fn reconciles_here(&self, task: &Task) -> bool {
!self.runs_elsewhere(task) || (task.owner_node.is_none() && task.status == TaskStatus::Done)
}
/// `runs_elsewhere` for a task known by its repo and id. False when the task is not found
pub async fn runs_elsewhere_by_ref(&self, repo: &str, task_id: &str) -> bool {
if self.node().is_none() {
return false;
}
match self.resolve_task_id(repo, task_id).await {
Ok(Some(id)) => self.get(id).await.is_ok_and(|t| self.runs_elsewhere(&t)),
_ => false,
}
}
/// How many tasks in `status` each node runs
pub async fn running_per_node(
&self,
status: TaskStatus,
) -> anyhow::Result<HashMap<String, usize>> {
let rows: Vec<(String, i64)> = sqlx::query_as(
"SELECT owner_node, COUNT(*) FROM tasks
WHERE status = ? AND owner_node IS NOT NULL GROUP BY owner_node",
)
.bind(status.as_str())
.fetch_all(&self.pool)
.await?;
Ok(rows
.into_iter()
.map(|(node, n)| (node, usize::try_from(n).unwrap_or(0)))
.collect())
}
/// The tasks in `status` that `node` runs, across every repo
pub async fn list_owned(&self, node: &str, status: TaskStatus) -> anyhow::Result<Vec<Task>> {
Ok(self
.list_all_by_status_global(status)
.await?
.into_iter()
.filter(|t| t.owner_node.as_deref() == Some(node))
.collect())
}
/// Renew this node's lease on every run it holds for `ttl_secs`. 0 ends them at once. Does
/// nothing on one machine
pub async fn renew_leases(&self, ttl_secs: i64) -> anyhow::Result<u64> {
let Some(node) = self.node() else {
return Ok(0);
};
match self
.write(Command::TaskRenewLeases {
node: node.name().to_string(),
secs: ttl_secs,
})
.await?
{
Reply::Count(n) => Ok(n),
other => anyhow::bail!("expected a count reply, got {other:?}"),
}
}
/// Give back a run `node` holds under `term`, see `apply::release`
pub async fn release_assignment(
&self,
id: i64,
node: &str,
term: i64,
status: TaskStatus,
) -> anyhow::Result<bool> {
self.write_bool(Command::TaskRelease {
id,
node: node.to_string(),
term,
status,
})
.await
}
}