Files
karaoke-gen/infrastructure/compute/encoding_worker_vm.py
T
Andrew Beveridge ☄️andClaude Opus 4.8 ec99164a05 feat(infra): diversify encoding-worker fallbacks with n2-highcpu-32 (capacity resilience) (#903)
## Problem

A **region-wide `c4d-highcpu-32` capacity stockout**
(`ZONE_RESOURCE_POOL_EXHAUSTED` across `us-central1-a`, `-b`, and `-c`
simultaneously, 2026-08-12) took out **every** encoding-worker lane at
once, because the primary blue-green pair *and* both capacity fallbacks
are all the same machine family.

Consequences observed in prod:
- **Lyrics-review preview** (`POST /api/review/{id}/preview-video`) fell
back to slow local Cloud-Run encoding (~130–160 s), which **exceeds
Cloudflare's ~100 s edge timeout** → users got a **524 / "NetworkError
when attempting to fetch resource"** even though the backend actually
returned 200.
- **Final renders** parked in `RENDER_PENDING_CAPACITY` and could only
complete once c4d capacity returned (24 h auto-retry window).

Prior work (May 2026) diversified fallbacks across **zones** but not
**machine family** — and a stockout is per *(machine family × region)*,
so every zone of a family can go at once.

## Fix (Option B)

Add **`n2-highcpu-32`** capacity fallbacks
(`encoding-worker-fallback-n2c` in `us-central1-c`, `-n2f` in
`us-central1-f`) — an independent, much deeper capacity pool a c4d
stockout cannot exhaust.

**Runtime is unchanged** — `ensure_any_running` already iterates
arbitrary `{vm,zone,ip}` candidates, `start_vm(zone=)` is zone-generic,
and the idle-shutdown function is secret-driven. This is purely IaC + a
secret version:

- `infrastructure/config.py` — `EncodingWorkerConfig.FALLBACKS` now
carries per-VM `machine_type` + `disk_type` (n2 uses `pd-balanced`; it
doesn't support hyperdisk-balanced). Existing `a`/`b` entries kept
**byte-identical** so `pulumi up` does not replace the live c4d VMs.
- `infrastructure/compute/encoding_worker_vm.py` — build fallback
IPs/VMs from `FALLBACKS`.
- Tests: config invariants + n2 idle-shutdown regression (**29 pass**).
- Docs: `LESSONS-LEARNED`, `TROUBLESHOOTING`, plan doc.

## Rollout status (already applied locally)

- ✅ `pulumi up` (targeted) applied to `prod`: created 2 n2 IPs + 2 n2
VMs (stopped). Verified additive — the existing c4d VMs were **not**
touched. (A pre-existing unrelated `runner-manager` drift from another
worktree was deliberately **not** applied.)
- ✅ `encoding-worker-fallback-vms` secret updated to v2 with the two n2
entries (picked up by Cloud Run / `video-encoding-job` / idle function
at next instance start).
- ✅ Smoke-tested: n2 VM boots the encoding-worker image and serves
`/health`.

## Testing

- `pytest infrastructure/test_encoding_worker_config.py
infrastructure/functions/encoding_worker_idle/test_idle_shutdown.py` →
29 passed.
- `pulumi preview` → `+4 to create`, no replacement of existing
encoding-worker resources.

## Follow-up (separate)

Preview is synchronous and can outlive the ~100 s edge timeout → opaque
524. Durable fix is making preview async/polling (like render), or
fail-fast to local when all VM starts stock out. Tracked in the plan
doc.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

@coderabbitai ignore

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-12 12:07:53 -04:00

193 lines
8.0 KiB
Python

"""
Encoding Worker VM resources — blue-green deployment pair.
Manages two identical VMs (a/b) for zero-downtime deployments.
Only one is active (primary) at a time; the other is stopped (secondary).
Both auto-shutdown when idle to minimize cost.
See docs/superpowers/specs/2026-03-24-blue-green-encoding-worker-design.md
"""
import pulumi
from pulumi_gcp import compute, serviceaccount
from config import REGION, ENCODING_WORKER_ZONE, PROJECT_ID, MachineTypes, DiskSizes, EncodingWorkerConfig
from .startup_scripts import read_script
def create_encoding_worker_ips() -> list[compute.Address]:
"""Create static IPs for both encoding worker VMs."""
ips = []
for name in EncodingWorkerConfig.IP_NAMES:
ip = compute.Address(
name,
name=name,
region=REGION,
address_type="EXTERNAL",
description=f"Static external IP for {name}",
)
ips.append(ip)
return ips
def create_encoding_worker_vms(
ips: list[compute.Address],
service_account: serviceaccount.Account,
) -> list[compute.Instance]:
"""Create the blue-green encoding worker VM pair."""
startup_script = read_script("encoding_worker.sh")
custom_image = f"projects/{PROJECT_ID}/global/images/family/encoding-worker"
vms = []
for vm_name, ip in zip(EncodingWorkerConfig.VM_NAMES, ips):
vm = compute.Instance(
vm_name,
name=vm_name,
machine_type=MachineTypes.ENCODING_WORKER,
zone=ENCODING_WORKER_ZONE,
boot_disk=compute.InstanceBootDiskArgs(
initialize_params=compute.InstanceBootDiskInitializeParamsArgs(
image=custom_image,
size=DiskSizes.ENCODING_WORKER,
type="hyperdisk-balanced",
# NOTE: Hyperdisk provisioned IOPS/throughput are deliberately
# NOT set here. Specifying them forces a full VM *replacement*
# (the provider treats boot-disk init params as immutable), and
# recreating a primary depends on us-central1-c capacity — the
# exact shortage the fallback fleet exists to absorb. So the
# disks are pinned to the Hyperdisk Balanced free baseline
# (3000 IOPS / 140 MB/s) live via `gcloud compute disks update`
# instead — non-disruptive, and pulumi does not manage this
# attribute so there is no drift. They were over-provisioned at
# 3600 IOPS / 290 MB/s (~$8.70/disk/mo wasted). Re-apply the
# gcloud tune after any worker-image rebuild that recreates the
# disks. See docs/archive/2026-06-14-gcp-cost-reduction-plan.md.
),
),
network_interfaces=[compute.InstanceNetworkInterfaceArgs(
network="default",
access_configs=[compute.InstanceNetworkInterfaceAccessConfigArgs(
nat_ip=ip.address,
)],
)],
service_account=compute.InstanceServiceAccountArgs(
email=service_account.email,
scopes=["cloud-platform"],
),
metadata_startup_script=startup_script,
tags=["encoding-worker"],
allow_stopping_for_update=True,
advanced_machine_features=compute.InstanceAdvancedMachineFeaturesArgs(
threads_per_core=2,
),
)
vms.append(vm)
return vms
def create_encoding_worker_fallback_ips() -> list[compute.Address]:
"""Create static IPs for the capacity-fallback VMs.
These IPs back the fallback fleet that absorbs a primary-family stockout —
both alternate zones AND an alternate machine family (n2-highcpu-32) so a
region-wide c4d-highcpu-32 ZONE_RESOURCE_POOL_EXHAUSTED can't take out every
lane. One IP per entry in EncodingWorkerConfig.FALLBACKS (order-aligned).
"""
ips = []
for fb in EncodingWorkerConfig.FALLBACKS:
ip_name = EncodingWorkerConfig.fallback_ip_name(fb["suffix"])
ip = compute.Address(
ip_name,
name=ip_name,
region=REGION,
address_type="EXTERNAL",
# Keep this string byte-identical to the originally-deployed value so
# `pulumi up` does not diff (and needlessly churn) the existing a/b IPs.
description=f"Static external IP for {ip_name} (zone-fallback)",
)
ips.append(ip)
return ips
def create_encoding_worker_fallback_vms(
ips: list[compute.Address],
service_account: serviceaccount.Account,
) -> list[compute.Instance]:
"""Create capacity-fallback encoding worker VMs.
Provisioned stopped — only started by the application when the primary zone
rejects starts with ZONE_RESOURCE_POOL_EXHAUSTED. The fleet diversifies
across both alternate zones AND an alternate machine family
(n2-highcpu-32) so a region-wide c4d-highcpu-32 stockout cannot exhaust
every lane at once (incident 2026-08-12). Each entry's machine_type and
disk_type come from EncodingWorkerConfig.FALLBACKS — n2 uses pd-balanced
because it does not support hyperdisk-balanced. Cost when stopped is just
the boot disk (~$10/mo each).
"""
startup_script = read_script("encoding_worker.sh")
custom_image = f"projects/{PROJECT_ID}/global/images/family/encoding-worker"
vms = []
for fb, ip in zip(EncodingWorkerConfig.FALLBACKS, ips):
vm_name = EncodingWorkerConfig.fallback_vm_name(fb["suffix"])
zone = f"{REGION}-{fb['zone_suffix']}"
# Hyperdisk provisioned IOPS/throughput are deliberately NOT set: setting
# them forces a full VM replacement (boot-disk init params are immutable),
# and recreating a c4d VM depends on the exact capacity the fallback fleet
# exists to absorb. hyperdisk-balanced disks are pinned to the free
# baseline (3000 IOPS / 140 MB/s) live via `gcloud compute disks update`
# after any image rebuild that recreates them; pd-balanced (n2) needs no
# such tuning. See docs/archive/2026-06-14-gcp-cost-reduction-plan.md.
vm = compute.Instance(
vm_name,
name=vm_name,
machine_type=fb["machine_type"],
zone=zone,
boot_disk=compute.InstanceBootDiskArgs(
initialize_params=compute.InstanceBootDiskInitializeParamsArgs(
image=custom_image,
size=DiskSizes.ENCODING_WORKER,
type=fb["disk_type"],
),
),
network_interfaces=[compute.InstanceNetworkInterfaceArgs(
network="default",
access_configs=[compute.InstanceNetworkInterfaceAccessConfigArgs(
nat_ip=ip.address,
)],
)],
service_account=compute.InstanceServiceAccountArgs(
email=service_account.email,
scopes=["cloud-platform"],
),
metadata_startup_script=startup_script,
tags=["encoding-worker"],
allow_stopping_for_update=True,
advanced_machine_features=compute.InstanceAdvancedMachineFeaturesArgs(
threads_per_core=2,
),
# Pulumi default is "running" — we want fallbacks stopped at create.
desired_status="TERMINATED",
)
vms.append(vm)
return vms
def create_encoding_worker_firewall() -> compute.Firewall:
"""Create firewall rules for encoding worker VMs.
Opens port 8080 for the HTTP API. Both VMs share the same
firewall rule via the 'encoding-worker' network tag.
"""
return compute.Firewall(
"encoding-worker-firewall",
name="encoding-worker-allow-http",
network="default",
allows=[
compute.FirewallAllowArgs(protocol="tcp", ports=["8080"]),
],
source_ranges=["0.0.0.0/0"],
target_tags=["encoding-worker"],
description="Allow HTTP access to encoding workers (auth required)",
)