mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-04 00:56:43 +09:00
## Summary - Replaces "c4d primary + single n2 fallback family" with a **ranked pool of 6 x86_64 / 32-vCPU / ≥32 GB highcpu types** across 4 silicon lineages (c4d, c4, n4d, c2d, n2d, n2), selected **fastest-first** but **availability-aware** — a type that stocks out is demoted for a 15-min cooldown then re-probed. c4d stays the top pick whenever it can start; a render no longer stalls even if 2+ types are fully stocked out in us-central1. - One **pure preference module** (`encoding_worker_preference.ordered_candidates`) is now the single source of truth for candidate ranking, driving **both** runtime selection and deploy green selection so they can't drift. - Keeps the per-VM start/stop model (MIG deferred with documented triggers; no reservation). Full plan + rollout in `docs/archive/2026-08-15-encoding-instance-type-diversity-plan.md`. ## Changes - **New** `backend/services/encoding_worker_preference.py` — speed-rank + Firestore `capacity_state` cooldown; stdlib-only so `deploy_promote` imports it (with a shadow-proof by-path fallback). - **Runtime** (`encoding_service`, `encoding_worker_manager`): builder ranks primary + fallbacks; manager records/clears per-(type,zone) stockout; override routing now keys off an explicit `is_primary` flag instead of list position. - **Deploy** (`deploy_promote`, `ci.yml`): green selection uses the shared ranker + threads `capacity_state`. - **IaC** (`config.py`, tests): `FALLBACKS` broadened to 6 types (additive-only) with family-correct disk types (Titanium c4/c4d/n4/n4d = hyperdisk-balanced; c2d/n2/n2d = pd-balanced); secret schema gains optional `machine_type`. - **Packer** (`provision.sh`, `startup.sh`): bake full dep tree with **CPU-only torch** so fresh type VMs boot self-sufficient; build-time import verify. - **New** `benchmark_types.py` to measure per-type encode wall-time and refine the seeded `SPEED_RANK`. ## Testing - [x] Full backend unit suite: **3871 passed, 36 skipped** (emulator tests need external services) - [x] New/updated tests: preference module (16), capacity_state + `is_primary` manager tests, builder-ordering + non-list-JSON service tests, deploy ranking tests, infra disk-capability + pool-breadth invariants - [ ] Operator rollout (needs write creds): `pulumi up` (verify +8 create, 0 replace) → rebuild image (dep-bake) → grow fallback secret with `machine_type` → run `benchmark_types.py` ## Review - [x] Local CodeRabbit review completed (4 major findings fixed; cycle 2 clean) - [x] Review feedback addressed @coderabbitai ignore --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
115 lines
5.0 KiB
Python
115 lines
5.0 KiB
Python
"""Invariants for the encoding-worker capacity-fallback fleet config.
|
|
|
|
Guards the machine-family diversification added 2026-08-12 after a region-wide
|
|
c4d-highcpu-32 ZONE_RESOURCE_POOL_EXHAUSTED stockout (us-central1-a/-b/-c at once)
|
|
exhausted every same-family lane and forced slow local encoding (→ 524 on
|
|
preview, parked renders).
|
|
|
|
Run locally with: `pytest infrastructure/test_encoding_worker_config.py`
|
|
(infrastructure is not part of the CI backend test gate — Pulumi validates the
|
|
resource graph via `pulumi preview`).
|
|
"""
|
|
|
|
from config import EncodingWorkerConfig, MachineTypes
|
|
|
|
|
|
def test_fallback_fleet_has_machine_family_diversity():
|
|
"""At least one fallback must use a machine family OTHER than the primary,
|
|
otherwise a single-family stockout takes out every lane at once."""
|
|
families = {fb["machine_type"] for fb in EncodingWorkerConfig.FALLBACKS}
|
|
assert MachineTypes.ENCODING_WORKER in families
|
|
assert any(mt != MachineTypes.ENCODING_WORKER for mt in families), (
|
|
"fallback fleet is single-machine-family — a c4d stockout would exhaust "
|
|
"every lane (see incident 2026-08-12)"
|
|
)
|
|
assert MachineTypes.ENCODING_WORKER_ALT in families
|
|
|
|
|
|
def test_n2_fallbacks_use_pd_balanced_disk():
|
|
"""n2 does not support hyperdisk-balanced; those VMs must use pd-balanced,
|
|
else Pulumi/GCE rejects the instance at create time."""
|
|
for fb in EncodingWorkerConfig.FALLBACKS:
|
|
if fb["machine_type"].startswith("n2"):
|
|
assert fb["disk_type"] == "pd-balanced", fb
|
|
|
|
|
|
# Machine families and the ONLY boot-disk type each supports for these VMs.
|
|
# Next-gen Titanium families (c4/c4d/n4/n4d) support hyperdisk-balanced only;
|
|
# older families (c2d/n2/n2d) use pd-balanced. Mismatched disk_type = GCE rejects
|
|
# the instance at create time (the exact failure the fallback fleet must avoid).
|
|
_FAMILY_DISK_TYPE = {
|
|
"c4d": "hyperdisk-balanced",
|
|
"c4": "hyperdisk-balanced",
|
|
"n4d": "hyperdisk-balanced",
|
|
"n4": "hyperdisk-balanced",
|
|
"c2d": "pd-balanced",
|
|
"n2d": "pd-balanced",
|
|
"n2": "pd-balanced",
|
|
}
|
|
|
|
|
|
def _family(machine_type: str) -> str:
|
|
# Longest prefix wins so "n2d"/"c4d" aren't shadowed by "n2"/"c4".
|
|
return max(
|
|
(fam for fam in _FAMILY_DISK_TYPE if machine_type.startswith(fam)),
|
|
key=len,
|
|
default="",
|
|
)
|
|
|
|
|
|
def test_every_fallback_disk_type_matches_its_family_capability():
|
|
"""Each fallback's disk_type must be the one its machine family supports."""
|
|
for fb in EncodingWorkerConfig.FALLBACKS:
|
|
fam = _family(fb["machine_type"])
|
|
assert fam, f"unknown family for {fb['machine_type']}"
|
|
assert fb["disk_type"] == _FAMILY_DISK_TYPE[fam], fb
|
|
|
|
|
|
def test_pool_has_at_least_five_types_across_multiple_lineages():
|
|
"""The broadened pool must be ≥5 distinct machine types spanning ≥3 lineages
|
|
(AMD Turin / Intel Emerald / AMD Milan / Intel Cascade / AMD Rome) so a
|
|
newest-gen stockout can't exhaust every lane."""
|
|
types = {fb["machine_type"] for fb in EncodingWorkerConfig.FALLBACKS}
|
|
# primary (c4d) + the fallback types.
|
|
types.add(MachineTypes.ENCODING_WORKER)
|
|
assert len(types) >= 5, f"pool has only {len(types)} types: {types}"
|
|
families = {_family(mt) for mt in types}
|
|
assert len(families) >= 3, f"pool spans only {families}"
|
|
|
|
|
|
def test_zone_spread_avoids_same_type_same_zone():
|
|
"""No machine type should sit twice in the same zone (correlated stockout)."""
|
|
seen = set()
|
|
for fb in EncodingWorkerConfig.FALLBACKS:
|
|
key = (fb["machine_type"], fb["zone_suffix"])
|
|
assert key not in seen, f"duplicate (type,zone): {key}"
|
|
seen.add(key)
|
|
|
|
|
|
def test_fallback_names_and_ips_are_unique_and_aligned():
|
|
"""Names/IPs are zipped by position with FALLBACKS — they must stay aligned
|
|
and unique so no two VMs collide on a resource name or static IP."""
|
|
fb = EncodingWorkerConfig.FALLBACKS
|
|
suffixes = [f["suffix"] for f in fb]
|
|
assert len(set(suffixes)) == len(suffixes), "duplicate fallback suffix"
|
|
assert EncodingWorkerConfig.FALLBACK_VM_NAMES == [
|
|
EncodingWorkerConfig.fallback_vm_name(s) for s in suffixes
|
|
]
|
|
assert EncodingWorkerConfig.FALLBACK_IP_NAMES == [
|
|
EncodingWorkerConfig.fallback_ip_name(s) for s in suffixes
|
|
]
|
|
assert len(set(EncodingWorkerConfig.FALLBACK_VM_NAMES)) == len(fb)
|
|
assert len(set(EncodingWorkerConfig.FALLBACK_IP_NAMES)) == len(fb)
|
|
|
|
|
|
def test_original_c4d_fallbacks_unchanged():
|
|
"""The first two entries must remain the original c4d a/b fallbacks
|
|
(byte-identical) so `pulumi up` does NOT recreate the existing VMs —
|
|
recreating a c4d VM would need the very capacity we're trying to avoid
|
|
depending on."""
|
|
a, b = EncodingWorkerConfig.FALLBACKS[0], EncodingWorkerConfig.FALLBACKS[1]
|
|
assert a == {"suffix": "a", "zone_suffix": "a",
|
|
"machine_type": MachineTypes.ENCODING_WORKER, "disk_type": "hyperdisk-balanced"}
|
|
assert b == {"suffix": "b", "zone_suffix": "b",
|
|
"machine_type": MachineTypes.ENCODING_WORKER, "disk_type": "hyperdisk-balanced"}
|