reconcile tick: create a placed agent whose deploy event was missed
The swarm's deploy event is core NATS. The wanted-state declaration lands before the build, so its create fails on no record, and only the event re-drives it; a hive that missed the event had nothing to retry. wanted.rs now remembers which agents the last declaration places with a container. Each tick, a placed agent with no container whose record says the builder pushed its closure gets its first deploy through JobQueue::insert_first_deploy_if_idle, which queues nothing while any node targets the agent. Record reads and creates back off like restarts, in a history of their own.
This commit is contained in:
parent
887ea26c56
commit
ad30b7ea31
2 changed files with 439 additions and 17 deletions
|
|
@ -98,6 +98,14 @@ pub struct RunningTransient {
|
|||
/// satisfy).
|
||||
type Sched = Scheduler<NodeKind, Resource>;
|
||||
|
||||
/// Whether any queued or running node targets `agent`.
|
||||
fn agent_busy(sched: &Sched, agent: &str) -> bool {
|
||||
sched
|
||||
.graph()
|
||||
.nodes()
|
||||
.any(|n| !n.state.is_terminal() && n.payload.agent() == agent)
|
||||
}
|
||||
|
||||
/// The queue. Lives on `Coordinator` (one per hive-c0re process); a single
|
||||
/// scheduler task ([`scheduler::run_worker`]) drives it.
|
||||
pub struct JobQueue {
|
||||
|
|
@ -224,6 +232,27 @@ impl JobQueue {
|
|||
Ok(Some(named))
|
||||
}
|
||||
|
||||
/// Queue `agent`'s first deploy unless any queued or running node targets
|
||||
/// it, by the same rule as [`Self::insert_reconcile_if_idle`].
|
||||
///
|
||||
/// `None` means the agent is busy: a first deploy, or any other work on it,
|
||||
/// is in flight. The check and the insert share one lock.
|
||||
///
|
||||
/// # Errors
|
||||
/// Propagates a graph-insert error.
|
||||
pub fn insert_first_deploy_if_idle(&self, agent: &str) -> anyhow::Result<Option<Vec<NodeId>>> {
|
||||
let mut inner = self.lock();
|
||||
if agent_busy(&inner, agent) {
|
||||
return Ok(None);
|
||||
}
|
||||
let named = inner
|
||||
.insert_job(None, |b| templates::first_deploy(b, agent))
|
||||
.map_err(|e| anyhow::anyhow!("job_queue: graph insert failed: {e}"))?;
|
||||
drop(inner);
|
||||
self.notify.notify_one();
|
||||
Ok(Some(named))
|
||||
}
|
||||
|
||||
/// Queue a lone `Reconcile` for `agent` unless any queued or running node
|
||||
/// already targets it.
|
||||
///
|
||||
|
|
@ -238,11 +267,7 @@ impl JobQueue {
|
|||
/// Propagates a graph-insert error.
|
||||
pub fn insert_reconcile_if_idle(&self, agent: &str) -> anyhow::Result<Option<Vec<NodeId>>> {
|
||||
let mut inner = self.lock();
|
||||
let busy = inner
|
||||
.graph()
|
||||
.nodes()
|
||||
.any(|n| !n.state.is_terminal() && n.payload.agent() == agent);
|
||||
if busy {
|
||||
if agent_busy(&inner, agent) {
|
||||
return Ok(None);
|
||||
}
|
||||
let targets = [(agent.to_owned(), false)];
|
||||
|
|
|
|||
|
|
@ -1541,6 +1541,39 @@ fn a_first_deploy_is_not_queued_twice_for_one_agent() {
|
|||
);
|
||||
}
|
||||
|
||||
/// The reconcile tick's create: not behind any live work on the agent, a
|
||||
/// rebuild included, and a finished node does not count.
|
||||
#[test]
|
||||
fn a_first_deploy_if_idle_waits_for_any_live_work_on_the_agent() {
|
||||
let q = JobQueue::new(1);
|
||||
insert(&q, |b| {
|
||||
let _ = rebuild(b, "busy");
|
||||
});
|
||||
assert!(
|
||||
q.insert_first_deploy_if_idle("busy")
|
||||
.expect("insert")
|
||||
.is_none(),
|
||||
"a live rebuild blocks it"
|
||||
);
|
||||
let first = q
|
||||
.insert_first_deploy_if_idle("newbie")
|
||||
.expect("insert")
|
||||
.expect("control: an idle agent gets its first deploy");
|
||||
assert!(
|
||||
q.insert_first_deploy_if_idle("newbie")
|
||||
.expect("insert")
|
||||
.is_none(),
|
||||
"the live first deploy blocks another"
|
||||
);
|
||||
assert!(q.cancel(first[0].get()), "cancel the queued first deploy");
|
||||
assert!(
|
||||
q.insert_first_deploy_if_idle("newbie")
|
||||
.expect("insert")
|
||||
.is_some(),
|
||||
"a terminal node does not count as busy"
|
||||
);
|
||||
}
|
||||
|
||||
/// The auto-update pass queues no second rebuild for an agent that has one in
|
||||
/// flight; another agent's rebuild does not count.
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue