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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! What the engine keeps of one listed repository: its last listing of queued jobs, when it is listed next, and per
//! pool, the health that holds the pool's runners back and what the pool's next job waits for. It owns those values and must not call
//! GitHub or Docker, nor log: the engine decides what each listing, failure and plan does to them.
use std::collections::BTreeMap;
use super::listing::Listing;
use super::listing_pace::ListingPace;
use super::pool_health::PoolHealth;
use crate::scheduler::Waits;
/// A repository's state, shared by its pools but for their health and what they wait for.
///
/// Nothing of it survives a restart. A reload keeps it for a repository the config still lists, but drops the health
/// of a pool whose runners change and the waits of a pool gone; a repository it removes drains with no listing, and
/// one it adds back starts afresh, due at once.
#[derive(Debug, Default)]
pub(super) struct RepositoryState {
/// Its queued jobs as its last listing showed them.
listing: Listing,
/// When it is listed next (ADR 0128).
pace: ListingPace,
/// Its pools by profile, each kept while it has a health or a wait.
pools: BTreeMap<String, PoolState>,
}
/// One pool's two halves, each with its own lifetime: a wait that ends keeps the health, and a reload that drops the
/// health keeps the wait.
#[derive(Debug, Default)]
struct PoolState {
/// The pool's runners held back after they failed: a broken pool does not hold back the others (ADR 0058). Made
/// by the first runner that leaves or fails, kept until a reload changes the pool's runners.
health: Option<PoolHealth>,
/// What the pool's next job has waited for and since when (ADR 0096): from the first plan of a tick or a pass
/// that held it, not one a status runs, until a runner of the pool is in its slot, the pool may no longer start
/// one, or a listing that succeeded no longer shows the job. A plan that holds it for a slot or for the other
/// resource does not make it younger, nor does a listing that failed. With them, whether a barrier has served the
/// pool (ADR 0098), until a listing that succeeded shows its queue empty or the pool may no longer start a runner.
waits: Option<Waits>,
}
impl PoolState {
fn is_empty(&self) -> bool {
self.health.is_none() && self.waits.is_none()
}
fn health_mut(&mut self) -> &mut PoolHealth {
self.health.get_or_insert_default()
}
fn forget_health(&mut self) {
self.health = None;
}
fn waits_mut(&mut self) -> Option<&mut Waits> {
self.waits.as_mut()
}
fn waits_entry(&mut self) -> &mut Waits {
self.waits.get_or_insert_default()
}
fn end_waits(&mut self) {
self.waits = None;
}
#[cfg(test)]
fn health_if_any(&mut self) -> Option<&mut PoolHealth> {
self.health.as_mut()
}
}
impl RepositoryState {
/// Its queued jobs as its last listing showed them.
pub(super) fn listing(&self) -> &Listing {
&self.listing
}
/// Its listing, for a listing's answer or a stop.
pub(super) fn listing_mut(&mut self) -> &mut Listing {
&mut self.listing
}
/// When it is listed next.
pub(super) fn pace(&self) -> &ListingPace {
&self.pace
}
/// When it is listed next, for a probe of it, a start or a release of its runners, or its listing's answer.
pub(super) fn pace_mut(&mut self) -> &mut ListingPace {
&mut self.pace
}
/// The health of its pool in `profile`, if a runner there left or failed since.
pub(super) fn health(&self, profile: &str) -> Option<&PoolHealth> {
self.pools.get(profile)?.health.as_ref()
}
/// The health of its pool in `profile`, made healthy if it has none.
pub(super) fn health_mut(&mut self, profile: &str) -> &mut PoolHealth {
self.pools
.entry(profile.to_owned())
.or_default()
.health_mut()
}
/// Drops the health of each pool `keep` does not keep; their waits stay.
pub(super) fn forget_health(&mut self, mut keep: impl FnMut(&str) -> bool) {
for (profile, pool) in &mut self.pools {
if !keep(profile) {
pool.forget_health();
}
}
self.pools.retain(|_, pool| !pool.is_empty());
}
/// What its pool in `profile` waits for, if anything.
pub(super) fn waits(&self, profile: &str) -> Option<&Waits> {
self.pools.get(profile)?.waits.as_ref()
}
/// What its pool in `profile` waits for, if anything, to change.
pub(super) fn waits_mut(&mut self, profile: &str) -> Option<&mut Waits> {
self.pools.get_mut(profile)?.waits_mut()
}
/// What its pool in `profile` waits for, from nothing if it waits for nothing yet.
pub(super) fn waits_entry(&mut self, profile: &str) -> &mut Waits {
self.pools
.entry(profile.to_owned())
.or_default()
.waits_entry()
}
/// Its pool in `profile` waits for nothing; its health stays.
pub(super) fn end_waits(&mut self, profile: &str) {
if let Some(pool) = self.pools.get_mut(profile) {
pool.end_waits();
if pool.is_empty() {
self.pools.remove(profile);
}
}
}
/// Ends the waits of each pool `keep` does not keep; their health stays.
pub(super) fn forget_waits(&mut self, mut keep: impl FnMut(&str) -> bool) {
for (profile, pool) in &mut self.pools {
if !keep(profile) {
pool.end_waits();
}
}
self.pools.retain(|_, pool| !pool.is_empty());
}
/// What each of its pools that waits waits for.
pub(super) fn all_waits(&self) -> impl Iterator<Item = &Waits> {
self.pools.values().filter_map(|pool| pool.waits.as_ref())
}
/// What each of its pools that waits waits for, to change.
pub(super) fn all_waits_mut(&mut self) -> impl Iterator<Item = &mut Waits> {
self.pools.values_mut().filter_map(PoolState::waits_mut)
}
/// Each pool's health, to change.
#[cfg(test)]
pub(super) fn all_health_mut(&mut self) -> impl Iterator<Item = &mut PoolHealth> {
self.pools.values_mut().filter_map(PoolState::health_if_any)
}
}