use std::time::{Duration, SystemTime};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use super::*;
use crate::admission::{Predictor, host};
use crate::scheduler::engine::Occupant;
use crate::scheduler::engine::admission::Admitting;
use crate::scheduler::engine::history::{Capture, Found};
use crate::scheduler::engine::open_waits::WaitFor;
use crate::scheduler::engine::slot::{Holder, Slot};
use crate::scheduler::engine::upkeep::Due;
use crate::scheduler::{Hold, Key, Wait, Waits};
use crate::store::LogPosition;
#[derive(Debug, Clone, Copy)]
pub(super) struct HoldFacts {
pub(super) held: Option<Hold>,
pub(super) failures: u32,
pub(super) idle_exits: u32,
}
impl<G: GithubApi, D: DockerApi> Engine<G, D> {
pub(super) fn occupants_mut(&mut self) -> impl Iterator<Item = &mut Occupant> {
self.slots
.values_mut()
.flatten()
.filter_map(Slot::occupant_mut)
}
pub(super) fn pool_hold(&self, repository: &str, profile: &str) -> Option<HoldFacts> {
let hold = self.repositories.listed(repository)?.health(profile)?;
Some(HoldFacts {
held: hold.held(),
failures: hold.failures(),
idle_exits: hold.idle_exits(),
})
}
pub(super) fn pool_is_quiet(&self, repository: &str, profile: &str, now: SystemTime) -> bool {
self.repositories
.listed(repository)
.and_then(|state| state.health(profile))
.is_none_or(|hold| hold.idle_exits() == 0 && !hold.holding(now))
}
pub(super) fn set_streaks(
&mut self,
repository: &str,
profile: &str,
failures: u32,
idle_exits: u32,
) {
let state = self.repositories.listed_mut(repository).unwrap();
state.health_mut(profile).set_streaks(failures, idle_exits);
}
pub(super) fn set_capture(&mut self, runner: &str, capture: JoinHandle<Capture>) {
self.watches.replace_capture(runner, capture);
}
pub(super) fn found_sender(&self) -> mpsc::UnboundedSender<Found> {
self.found.0.clone()
}
pub(super) fn has_claims(&self, repository: &str) -> bool {
self.claims.has(repository)
}
pub(super) fn ended_here(&self, runner: u64) -> bool {
self.registrations.is_ended_here(runner)
}
pub(super) fn unnamed_claims(&self, repository: &str) -> usize {
self.claims.unnamed_of(repository)
}
pub(super) fn set_held(&mut self, repository: &str, profile: &str, held: Hold) {
let state = self.repositories.listed_mut(repository).unwrap();
state.health_mut(profile).set_held(held);
}
pub(super) fn clear_holds(&mut self) {
for (_, state) in self.repositories.iter_listed_mut() {
for hold in state.all_health_mut() {
hold.expire(SystemTime::UNIX_EPOCH);
}
}
}
pub(super) fn expire_holds(&mut self, past: SystemTime) {
for (_, state) in self.repositories.iter_listed_mut() {
for hold in state.all_health_mut() {
hold.expire(past);
}
}
}
pub(super) fn slot_mut(&mut self, slot: &crate::scheduler::SlotRef) -> &mut Slot {
&mut self.slots.get_mut(&slot.profile).unwrap()[slot.index]
}
pub(super) fn any_starting(&self) -> bool {
self.slots
.values()
.flatten()
.any(|slot| matches!(slot.holder(), Holder::Starting(_)))
}
pub(super) fn expire_github_limit(&mut self, past: SystemTime) {
if let Some(until) = self.github_limited_until.as_mut() {
*until = past;
}
}
pub(super) fn set_queued(&mut self, repository: &str, jobs: Vec<QueuedJob>) {
self.repositories
.listed_mut(repository)
.unwrap()
.listing_mut()
.replace_jobs(jobs);
}
pub(super) fn set_listed(&mut self, repository: &str, jobs: Vec<QueuedJob>) {
let state = self.repositories.listed_mut(repository).unwrap();
state.listing_mut().answer(jobs);
}
pub(super) fn listed_repositories(&self) -> Vec<ListedFacts<'_>> {
self.repositories
.iter_listed()
.filter_map(|(name, state)| {
Some(ListedFacts {
name,
queued: state.listing().listed()?,
waits: state.all_waits().copied().collect(),
})
})
.collect()
}
pub(super) fn waits_of(&self, repository: &str, profile: &str) -> Option<Waits> {
self.repositories
.listed(repository)?
.waits(profile)
.copied()
}
pub(super) fn keeps_no_waits(&self, repository: &str) -> bool {
self.repositories
.listed(repository)
.unwrap()
.all_waits()
.next()
.is_none()
}
pub(super) fn set_memory_wait(&mut self, repository: &str, profile: &str, wait: Wait) {
let state = self.repositories.listed_mut(repository).unwrap();
let waits = state.waits_entry(profile);
*waits = waits.with_memory(wait);
}
pub(super) fn set_cpu_wait(&mut self, repository: &str, profile: &str, wait: Wait) {
let state = self.repositories.listed_mut(repository).unwrap();
let waits = state.waits_entry(profile);
*waits = waits.with_cpu(wait);
}
pub(super) fn age_waits(&mut self, by: Duration) {
for (_, state) in self.repositories.iter_listed_mut() {
for waits in state.all_waits_mut() {
if let Some(wait) = waits.memory_mut() {
wait.since -= by;
}
if let Some(wait) = waits.cpu_mut() {
wait.since -= by;
}
}
}
}
pub(super) fn reseeding(&self, profile: &str) -> bool {
self.upkeep.reseed_runs(profile)
}
pub(super) fn no_reseeds(&self) -> bool {
self.upkeep.seed_memories().is_empty()
}
pub(super) fn seed_memories(&self) -> BTreeMap<String, u64> {
self.upkeep.seed_memories()
}
pub(super) fn seed_runs(&mut self, profile: &str, memory: u64) {
self.upkeep
.reseed_started(profile, tokio::spawn(std::future::pending()), memory);
}
pub(super) fn is_retired(&self, profile: &str) -> bool {
self.retired.contains_key(profile)
}
pub(super) fn reseed_is_requested(&self, profile: &str) -> bool {
self.upkeep.is_requested(profile)
}
pub(super) fn ask_reseed(&mut self, profile: &str) {
self.upkeep.request(profile);
}
pub(super) fn make_reseed_due(&mut self, profile: &str) {
self.upkeep.mark(profile, Due::Changed);
}
pub(super) fn is_leftover(&self, container: &str) -> bool {
self.leftovers.is_removing(container)
}
pub(super) fn no_leftovers(&self) -> bool {
self.leftovers.to_remove().is_empty()
}
pub(super) fn abort_captures(&mut self) {
self.watches.abort_all();
}
pub(super) fn age_running_reads(&mut self, by: Duration) {
self.running_reads.age(by);
}
pub(super) fn age_ended_uploads(&mut self, by: Duration) {
for ended in self.ended_uploads.values_mut() {
ended.ended_at -= by;
}
}
pub(super) fn ended_uploads_waiting(&self) -> BTreeSet<String> {
self.ended_uploads
.iter()
.filter(|(_, ended)| {
ended
.ended_at
.elapsed()
.is_ok_and(|waited| waited < crate::scheduler::engine::build_cache::ENDED_WAIT)
})
.map(|(runner, _)| runner.clone())
.collect()
}
pub(super) fn build_cache_runners(&self) -> Vec<String> {
self.build_cache
.as_ref()
.map(crate::buildcache::BuildCache::attached)
.unwrap_or_default()
}
pub(super) fn launches(&self) -> crate::scheduler::engine::start::Launches {
self.launches.clone()
}
pub(super) fn ended_upload_runners(&self) -> BTreeSet<String> {
self.ended_uploads.keys().cloned().collect()
}
pub(super) fn no_ended_uploads(&self) -> bool {
self.ended_uploads.is_empty()
}
pub(super) fn host_is_short(&self) -> bool {
self.measured.host().is_short()
}
pub(super) fn set_host_short(&mut self) {
self.measured.set_host_short();
}
pub(super) fn waits_for_a_slot(&self, job: u64) -> bool {
self.measured.waiting(WaitFor::Slot).contains(&job)
}
pub(super) fn waits_for_the_host(&self, job: u64) -> bool {
self.measured.waiting(WaitFor::Host).contains(&job)
}
pub(super) fn wait_for_the_host(&mut self, job: u64) {
self.measured.wait_for_the_host(job);
}
pub(super) fn raise(&mut self, runner: String) {
self.measured.raise(runner);
}
pub(super) fn raised(&self) -> &[String] {
self.measured.raised()
}
pub(super) fn measure_is_due(&self, profile: &str) -> bool {
self.upkeep.measure_is_due(profile)
}
pub(super) fn forget_predictor(&mut self) {
self.measured.set_predictor(None);
}
pub(super) fn set_predictor(&mut self, predictor: Predictor, read_at: SystemTime) {
self.measured.set_predictor(Some((predictor, read_at)));
}
pub(super) fn no_recordings(&self) -> bool {
!self.records.is_recording()
}
pub(super) fn no_shells(&self) -> bool {
!self.records.has_shells()
}
pub(super) fn is_closing(&self, job: u64) -> bool {
self.ledger.is_closing(job)
}
pub(super) fn is_followed_as_queued(&self, job: u64) -> bool {
self.ledger.is_queued(job)
}
pub(super) fn age_followed_jobs(&mut self, by: Duration) {
self.ledger.age(by);
}
pub(super) fn set_closing_since(&mut self, job: u64, ms: u64) {
self.ledger.found_gone_at(job, ms);
}
pub(super) fn set_recapture_resumes(&mut self, position: LogPosition) {
self.watches.resume_every_break_at(position);
}
pub(super) fn runner_prefix(&self) -> &str {
&self.runner_prefix
}
pub(super) fn make_trim_due(&mut self) {
self.trim_due = true;
}
pub(super) fn expire_cancelling(&mut self) {
let now = tokio::time::Instant::now();
for occupant in self
.slots
.values_mut()
.flatten()
.filter_map(Slot::occupant_mut)
{
occupant.engagement.expire_cancel(now);
}
}
pub(super) fn make_unread_due(&mut self) {
self.leftovers.make_due();
}
pub(super) fn state_counts(&self) -> StateCounts {
StateCounts {
closing: self.ledger.closing().len(),
ended_uploads: self.ended_uploads.len(),
cancelling: self
.slots
.values()
.flatten()
.filter_map(Slot::occupant)
.filter(|occupant| occupant.engagement.is_cancelling())
.count(),
busy_orphans: self.registrations.orphans(),
oversized: self.upkeep.oversized(),
unread: self.leftovers.unread(),
split_waits: self
.repositories
.iter_listed()
.flat_map(|(_, state)| state.all_waits())
.filter(|waits| {
waits
.memory()
.zip(waits.cpu())
.is_some_and(|(a, b)| a.job != b.job)
})
.count(),
captures_with_recaptures: self.watches.live_after_breaks(),
}
}
}
pub(super) fn set_plan_short(plan: &mut Admitting, short: bool) {
plan.remembered = host::Remembered::testing(short, plan.remembered.was_stalled());
}
#[derive(Debug, Clone, Copy, Default)]
pub(super) struct StateCounts {
pub(super) closing: usize,
pub(super) ended_uploads: usize,
pub(super) cancelling: usize,
pub(super) busy_orphans: usize,
pub(super) oversized: usize,
pub(super) unread: usize,
pub(super) split_waits: usize,
pub(super) captures_with_recaptures: usize,
}
pub(super) struct ListedFacts<'engine> {
pub(super) name: &'engine str,
pub(super) queued: &'engine [QueuedJob],
pub(super) waits: Vec<Waits>,
}
impl Occupant {
pub(super) fn job_found_at(&self) -> Option<SystemTime> {
self.engagement.job_found_at()
}
pub(super) fn found_job_at(&mut self, at: SystemTime) {
self.engagement.found_job_at(at);
}
pub(super) fn age_job_start(&mut self, by: Duration) {
self.engagement.age_job_start(by);
}
pub(super) fn set_listed_busy(&mut self) {
self.engagement.set_listed_busy();
}
pub(super) fn has_taken(&mut self, job: u64) {
self.engagement.has_taken(job);
}
pub(super) fn set_key(&mut self, key: Key) {
self.engagement.set_key(key);
}
pub(super) fn was_started_here(&self) -> bool {
self.engagement.was_started_here()
}
}