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
20 changes: 15 additions & 5 deletions scripts/ci/seed_derived_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,21 @@ def locate(prefix: str, revision: str) -> tuple[str, int | None]:
the nearest of another width, whose extra module work is still far less
than a cold build.
"""
revisions = lineage(revision)
found = nearest_of_any_width(prefix, lineage(revision))
if found:
return found
return scoped(prefix) + revision, None


def nearest_of_any_width(prefix: str, revisions: list[str]) -> tuple[str, int] | None:
"""The nearest seed of this width over REVISIONS, else the nearest of the
first SEEDED_JOB_WIDTHS width that has one. PREFIX is unscoped."""
own = swift_jobs()
for jobs in (own, *(width for width in SEEDED_JOB_WIDTHS if width != own)):
found = nearest(scoped(prefix, jobs), revisions)
if found:
return found
return scoped(prefix, own) + revision, None
return None


def beside(derived: Path, suffix: str) -> Path:
Expand Down Expand Up @@ -425,7 +433,7 @@ def record_source(store: Path, prefix: str) -> None:


def prefetch(store: Path, revision: str) -> dict[str, object]:
"""Download REVISION's nearest seed of this width into STORE/seeds, unless it is there."""
"""Download the seed `adopt` would pick for REVISION into STORE/seeds, unless it is there."""
try:
source = json.loads((store / SEED_SOURCE).read_text())
except (OSError, ValueError):
Expand All @@ -439,9 +447,11 @@ def prefetch(store: Path, revision: str) -> dict[str, object]:
os.environ.setdefault(name, value)
cache = store / "seeds"
os.environ["CMUX_SEED_LOCAL_CACHE"] = str(cache)
found = nearest(scoped(prefix), lineage(revision))
# The key `adopt` would pick (locate): this width's nearest seed, else another width's. A Mac of a
# width nothing seeds at (the 10-core light minis) otherwise never prefetched at all.
found = nearest_of_any_width(prefix, lineage(revision))
if found is None:
return {"fetched": "false", "reason": "no seed of this width in REVISION's history"}
return {"fetched": "false", "reason": "no seed of any seeded width in REVISION's history"}
key, distance = found
if cached(key):
# Nothing new lands, but a job may have filled the disk since: prune.
Expand Down
26 changes: 25 additions & 1 deletion tests/test_seed_derived_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_a_seed_job_records_its_prefix_so_the_mac_prefetches_between_jobs(self):
with mock.patch.dict(os.environ), mock.patch.object(seed, "lineage", return_value=["head"]), \
mock.patch.object(seed, "seed_exists", return_value=False):
self.assertEqual(seed.prefetch(self.root / "cmux-ci-2", "head")["reason"],
"no seed of this width in REVISION's history")
"no seed of any seeded width in REVISION's history")
# Without a local cache nothing is recorded; a bad prefix is ignored.
(self.root / "cmux-ci-2" / seed.SEED_SOURCE).unlink()
self.assertEqual(seed.main(["seed", "keep", str(self.derived), "k", prefix]), 0)
Expand Down Expand Up @@ -304,6 +304,30 @@ def fake_fetch(derived, exact, prefix):
self.assertEqual([p.name for p in (store / "seeds").iterdir()], [key])
self.assertEqual(os.environ["CI_CACHE_R2_PUBLIC_URL"], "https://cache.test")

def test_prefetch_falls_back_to_the_width_adopt_would_take(self):
"""A 10-core light mini has no seeds of its own width: it keeps the
seed adopt would fall back to (12 before 6 before 14), and still
prefers its own width when one exists, however far."""
os.environ["CMUX_SEED_SWIFT_JOBS"] = "10"
store = self.prefetch_store()
prefix = "admission-derived-data-v1-macOS-ARM64-fp-"
exists = {prefix + "j14-head", prefix + "j12-p1", prefix + "j6-head"}

def fake_fetch(derived, exact, _prefix):
staging = derived.with_name(derived.name + ".seed")
(staging / "Build").mkdir(parents=True)
(staging / seed.MANIFEST).write_text("{}")
return exact

with mock.patch.object(seed, "lineage", return_value=["head", "p1", "p2"]), \
mock.patch.object(seed, "seed_exists", side_effect=lambda k: k in exists), \
mock.patch.object(seed, "fetch", side_effect=fake_fetch):
got = seed.prefetch(store, "head")
self.assertEqual((got["fetched"], got["key"], got["distance"]), ("true", prefix + "j12-p1", 1))
self.assertEqual(seed.locate(prefix, "head"), (prefix + "j12-p1", 1), "the same key adopt takes")
exists.add(prefix + "j10-p2")
self.assertEqual(seed.prefetch(store, "head")["key"], prefix + "j10-p2")

def test_prefetch_never_replaces_a_copy_a_job_kept_meanwhile(self):
store = self.prefetch_store()
key = "admission-derived-data-v1-macOS-ARM64-fp-j6-head"
Expand Down
Loading