Skip to content

Create pipeline network groups per stage region, close SSH, require every instance, price the budget (#929, #930) - #957

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/929
Oct 17, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/929

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #929
Fixes #930

Stacked on PR A (#942, fix/910), which is stacked on #931 (fix/912). This PR's base is fix/910. Merge in order: #931, then #942, then this.

Cost safety. Found by reading the code and tested with fakes and the in-process Substrate emulator. No AWS resources, launches or deploys.

#929: network groups in each stage's region; SSH closed

What was wrong. createStreamingSecurityGroup and createPlacementGroup used the Lambda's own region (us-east-1). RunInstances then ran in each stage's region with those group IDs. Groups are regional, so a streaming or EFA stage anywhere else would fail at launch. Cleanup deleted the groups in the Lambda's region, consistently with that. Separately, the group opened tcp/22 to 0.0.0.0/0.

What changed.

  • setupNetworkResources creates a security group in every stage region, because the group goes on every stage instance. It creates a placement group in every EFA stage's region. Both are recorded in the new PipelineState.SecurityGroups and PlacementGroups maps, keyed by region.
  • A group is recorded as soon as it exists. A setup that fails part way now also runs network cleanup.
  • buildStageRunInstancesInput uses the stage's own region's group.
  • cleanupNetworkResources deletes each group in its own region. AWS calls go through a netEC2ForRegion seam.
  • Pipelines in flight across the upgrade only have the old single SecurityGroupID/PlacementGroupID. Those fields are read only for stages in the Lambda's region, and are cleaned up there.
  • Ingress is now tcp 50000-60000 and SSH, both from the group itself. Nothing is open to an address range.

#930: every instance must complete; the budget is priced

Per-instance markers.

  • The stage runner writes <prefix>/stages/<stage>/<instance-index>/COMPLETE|FAILED (pipeline.StageInstanceMarkerKey), with the index taken from the instance's tag. The user data's fallback FAILED marker does the same.
  • checkStageOutcome completes a stage only when all instance_count instances have written COMPLETE. It fails the stage on any FAILED, or on any instance that has gone without writing COMPLETE: a spot interruption, say. The old rule waited for all instances to be gone. A completed instance that has since terminated itself does not count as a failure.
  • Legacy stage-level markers, from instances launched before this, are still read. FAILED always counts. COMPLETE counts only for a single-instance stage, where it means the same thing.

Pricing.

  • calculateStageCost charged a flat $0.10 per instance-hour, and only when a stage finished. Each stage is now priced once, at launch, at its on-demand rate. The lookup goes through spawnaws.ResolveDisplayPrice (truffle: live Price List or its static table, the same authority --cost-limit uses). The rate and its source are recorded on StageState.
  • Spot stages are priced at on-demand too. That is an upper bound, so the budget is never under-enforced because the spot price moved.
  • The budget is checked on every poll against an estimate: finished stages, plus in-flight stages' elapsed hours × instances × rate. Once every stage has finished there is nothing left to stop, so the pipeline is reported as it ended rather than cancelled.
  • With max_cost_usd set, a stage whose price cannot be determined fails before launching anything, following pkg/aws's rule that a limit that cannot be priced cannot be enforced. Without a budget, the stage launches unpriced and a warning is logged.
  • The figure is labelled an estimate everywhere it reaches a user. Notifications say "Estimated cost: $X (estimate: on-demand rates; spot is billed less; EBS and data transfer not included)". spawn pipeline status prints "Estimated cost … (finished stages, at on-demand rates)", and status/list use an "EST COST" column.
  • template.yaml grants pricing:GetProducts. Without it truffle falls back to its static table.

Tests

New file regions_markers_cost_test.go, plus an extension to the bash e2e from #942.

  • Network. A pipeline with stages in eu-west-1 and us-west-2 and a Lambda in us-east-1:
    • each region gets its own group; the placement group exists only in the EFA region; nothing is created in us-east-1;
    • each stage launches with its own region's group, and cleanup deletes each group in its region;
    • every ingress rule is from the group itself, with no CIDR, and SSH between members still works;
    • the legacy group is used only in the Lambda's region;
    • a partial setup stays recorded.
  • Markers. A 4-instance stage:
    • stays running at 3/4 COMPLETE, and its dependent stays pending; at 4/4 it completes;
    • one FAILED fails it, even with the other three complete;
    • one terminated instance without COMPLETE fails it, but a completed instance that terminated does not;
    • legacy markers are read as described above;
    • each instance's fallback key is distinct.
  • E2E. The failing stage in the real-spored bash test now runs as instance 1 of 2. spored writes stages/train/1/FAILED from the instance's tag, and checkStageOutcome fails the stage on it. The success test also asserts that checkStageOutcome returns completed.
  • Budget.
    • Four p4d.24xlarge at $32.77/h, running for an hour, trip a $100 budget with no stage finished; all four are terminated and the stage's estimated cost is over $100. With the old flat rate, the cost is $0.40.
    • Under budget, nothing is cancelled.
    • With a budget, an unpriceable stage launches nothing and errors; without one it launches.
    • Notifications say "Estimated cost", never "Total cost".

Every gate was watched failing against a compiling revert (19 reverts, each go vet-clean):

  • network: SG in the Lambda region; PG in the Lambda region; launch using the wrong region's SG; SSH open to 0.0.0.0/0; legacy SG used in any region; cleanup in the Lambda region; SG not recorded;
  • markers: first instance completes the stage; legacy COMPLETE accepted for multi-instance stages; legacy FAILED ignored; instance FAILED ignored; the "all gone" rule; fallback at the stage-level key; runner at the stage-level key; runner not given its instance index;
  • budget: flat $0.10 rate; running stages not counted; unpriceable stage launched under a budget; "Total cost" label.

One addition has no gate: the cleanup after a failed setup is in handler, which reads DynamoDB and S3 directly.

Commands run.

  • make check passes except the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker.
  • make check-nested passes (9 modules).
  • go test -count=1 ./lambda/pipeline-orchestrator/ ./pkg/pipeline/ passes (full, not short), and the orchestrator package also passes at -short -count=5.
  • golangci-lint shows nothing new in the touched packages.
  • GOOS=linux GOARCH=arm64 builds the Lambda.

Deployment (not done here)

Deploy after #931 and #942. The template changed again, adding pricing:GetProducts:

cd lambda/pipeline-orchestrator
AWS_PROFILE=spore-host-infra make deploy
AWS_PROFILE=spore-host-infra make -C ../.. lambda-versions-deployed

Per-instance markers are written by spored, so a spored release containing #942 and this PR must be published to spawn-binaries-* along with the deploy. Until then, released spored writes the legacy stage-level key, and only single-instance stages complete. Multi-instance stages would wait for per-instance COMPLETE markers, fail when their instances exit, and be terminated. That is safe but wrong, so ship spored and the Lambda together. The CLI only changes labels here.

Caveats and follow-ups

…very instance, price the budget (#929, #930)

Lambda's own region, and RunInstances in each stage's region was handed
those groups, which do not exist there. The groups are now created in
every region that needs one (the security group in every stage region,
since it goes on every instance; the placement group in every EFA
stage's region), recorded per region in PipelineState, used by each
stage's launch from its own region, and deleted in that region. A group
is recorded as soon as it exists, and a setup that fails part way is
cleaned up. The pre-#929 single fields are read only for in-flight
pipelines, in the Lambda's region. SSH was open to 0.0.0.0/0; it, like
the streaming ports, is now allowed only from the group itself.

instance_count > 1 the first to finish completed the stage and its
siblings' FAILED went unread. Markers are per instance
(StageInstanceMarkerKey), written by the runner and the user data
fallback with the instance's index. The stage completes when every
instance has written COMPLETE and fails on any FAILED or on any
instance gone without COMPLETE. Legacy stage-level markers are still
read (COMPLETE only for single-instance stages).

The budget charged a flat $0.10/instance-hour, only at stage
completion. Stages are now priced once at their on-demand rate via
pkg/aws ResolveDisplayPrice (truffle; spot priced at on-demand, an
upper bound), and the budget is checked each poll against finished
stages plus running stages' elapsed instance-hours. With a budget, an
unpriceable stage fails before launching. Notifications and `spawn
pipeline status/list` label the figure an estimate. The template grants
pricing:GetProducts.

Tests: per-region create/use/delete with fake regional EC2, ingress
limited to the group, legacy fallback, partial setup recorded; 4-instance
stage completes only at 4/4 and its dependent waits, one FAILED or one
vanished instance fails it, legacy markers; a p4d stage running an hour
trips a $100 budget, unpriceable-with-budget launches nothing, cost
labelled. The bash e2e now runs a failing stage as instance 1 of 2 and
checks the per-instance FAILED and the orchestrator's verdict. Each gate
was watched failing against a compiling revert.
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.66667% with 68 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
lambda/pipeline-orchestrator/main.go 72.60% 41 Missing and 22 partials ⚠️
cmd/pipeline.go 0.00% 4 Missing ⚠️
pkg/pipeline/stage_runner.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 0e530b2 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/929 branch October 17, 2026 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant