Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions bridge/fleet_bridge/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def remove_entry(path: Path, section: str, name: str) -> bool:

def append_entry(path: Path, section: str, entry: dict[str, Any]) -> None:
"""Appends one entry to an `apps` list, keeping every other line as written. The entry
goes on one line in YAML flow style, under a block list, an empty list or a missing key."""
goes on one line in YAML flow style, under a block list, an empty list or a missing key.
It follows the list's last entry, so keys after the list stay after it."""
text = path.read_text()
root = yaml.compose(text)
node = next((v for k, v in root.value if k.value == section), None) if root else None
Expand All @@ -165,7 +166,13 @@ def append_entry(path: Path, section: str, entry: dict[str, Any]) -> None:
if _is_content(line):
end = offset
if not text[:end].endswith("\n"):
text, end = text + "\n", len(text) + 1
# The last entry ends mid-line, as a one-line flow entry does: the new one goes
# after the rest of that line, or after a newline added at the end of the file.
newline = text.find("\n", end)
if newline < 0:
text, end = text + "\n", len(text) + 1
else:
end = newline + 1
text = text[:end] + " " * (last.start_mark.column - 2) + item + text[end:]
elif isinstance(node, yaml.SequenceNode) and not node.value:
text = text[:node.start_mark.index].rstrip(" ") + "\n " + item + text[node.end_mark.index:]
Expand Down
18 changes: 18 additions & 0 deletions bridge/tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,24 @@ def test_append_entry_keeps_every_other_line(tmp_path, text):
assert line in p.read_text()


@pytest.mark.parametrize("text", [
'log_dir: /logs\napps:\n- name: web\n kind: app\nscheduler_admin: https://fleet:8905\n',
'apps:\n- {"name": "web", "kind": "app"}\nscheduler_admin: https://fleet:8905\n',
'apps:\n - {"name": "web", "kind": "app"} # placed by the scheduler\n# admin\nscheduler_admin: https://fleet:8905\n',
'scheduler_admin: https://fleet:8905\napps:\n- {"name": "web", "kind": "app"}',
])
def test_appended_entries_stay_in_the_list_before_the_keys_after_it(tmp_path, text):
p=tmp_path/"apps.yaml";p.write_text(text)
for name in ("first","second"):
node.append_entry(p,"apps",{"name":name,"kind":"app","namespace":"zkm","disabled":True,"image":"web:1"})
config=yaml.safe_load(p.read_text())
assert [a["name"] for a in config["apps"]]==["web","first","second"]
assert config["scheduler_admin"]=="https://fleet:8905"
assert {k:v for k,v in config.items() if k!="apps"}=={k:v for k,v in yaml.safe_load(text).items() if k!="apps"}
for line in text.splitlines():
assert line in p.read_text()


def test_create_adds_a_disabled_app_and_refuses_a_taken_name(executor):
entry={"name":"web","kind":"app","namespace":"a"*32,"disabled":True,"image":"web:1","port_base":50100,"gpus":1}
assert app_message(executor,"create-1",{"create":entry},app="web",ns="a"*32)==({"created":["web"]},None)
Expand Down
31 changes: 27 additions & 4 deletions crates/fleet-scheduler/src/workloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,26 @@ impl WorkloadController {
/// Drops a deployment record, its allocation already released. Nothing else removes one, and a
/// stale record can restart its app or claim its app's worker slot.
fn forget_deployment(&self, key: &str) -> anyhow::Result<()> {
self.inner.lock().deployments.remove(key);
let Some(intent) = self.inner.lock().deployments.remove(key) else {
return Ok(());
};
match std::fs::remove_file(self.dir.join("deployments").join(format!("{key}.json"))) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => {}
}
// Its node no longer runs the app, unless it has another entry of it.
let (app, namespace) = (intent.entry().app().to_owned(), intent.entry().namespace().to_owned());
let template = {
let i = self.inner.lock();
let elsewhere = i.deployments.values()
.any(|d| d.worker == intent.worker && d.entry().app() == app && d.entry().namespace() == namespace);
i.apps.get(&app).filter(|a| a.persistent && a.namespace == namespace && !elsewhere && a.workers.contains(&intent.worker)).cloned()
};
if let Some(mut template) = template {
template.workers.retain(|w| *w != intent.worker);
self.save("apps", &app, &template)?;
self.inner.lock().apps.insert(app, template);
}
tracing::info!(deployment = key, "deployment record removed: its node no longer configures it");
Ok(())
}
Expand Down Expand Up @@ -1282,6 +1297,13 @@ impl WorkloadController {
}
}
if !disabled {
// An app runs its replica count, wherever the replicas are: a start that would
// run more retires its deployment instead (a node back with a copy of an app
// that runs elsewhere since, say).
if self.beyond_replica_count(&c) {
self.retire(&format!("{}-{}", c.worker, s.name()))?;
continue;
}
let already_ready = {
let i = self.inner.lock();
i.workers.get(&c.worker).is_some_and(|w| {
Expand Down Expand Up @@ -2127,10 +2149,11 @@ async fn priority_of(request: Request) -> anyhow::Result<i32> {
}

impl WorkloadController {
/// The keys of `app`'s deployment records in `namespace`.
/// The keys of `app`'s deployment records in `namespace`, but those being retired.
pub(crate) fn app_deployments(&self, namespace: &str, app: &str) -> Vec<String> {
self.inner.lock().deployments.iter()
.filter(|(_, d)| d.entry().app() == app && d.entry().namespace() == namespace)
let i = self.inner.lock();
i.deployments.iter()
.filter(|(_, d)| d.entry().app() == app && d.entry().namespace() == namespace && !scaling::retiring(&i, d))
.map(|(k, _)| k.clone())
.collect()
}
Expand Down
Loading
Loading