Skip to content

Cost spot instances at their spot price in spawn status (#896) - #916

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/896
Oct 15, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/896

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Root causes

  1. Spot costed at on-demand. Every cost figure (spored's compute/cumulative/effective rate, spawn's max compute cost and billable-resources row) read spawn:price-per-hour, which is the on-demand rate resolved at launch for --cost-limit. No spot price was ever recorded, and the (spot) label was only cosmetic.
  2. 0s of compute at 47s elapsed. spored status is a fresh process (NewAgentForQuery), so its startTime is "now". TotalComputeSeconds() came out as the spawn:compute-seconds checkpoint plus about 0. That checkpoint is 0 until the daemon's first write, and after that it lags by up to 5 minutes. The same wrong clock made the "boot" term equal to all of elapsed and kept "stopped" from ever showing. The effective rate was then $0 / elapsed, compared against on-demand: "100% lower".
  3. Mixed zones. spored (on the instance, which can't know the operator's zone) printed UTC. spawn's own Termination deadline printed time.Local.

Fixes

  • Spot price. pkg/aws: a spot Launch looks up DescribeSpotPriceHistory for the instance's own AZ, type and product (mapped from PlatformDetails). Unmapped platforms such as Windows+SQL or BYOL are reported as unknown, not priced as Linux. It then tags spawn:spot-price-per-hour. This is best-effort and never fails the launch. New exported SpotPriceTag, SpotPrice, (*Client).CurrentSpotPrice (additive only; make api-snapshot committed).
    • spored reads the tag (Config.SpotPricePerHour) and charges compute at it when it is set. The JSON gains rate_per_hour and rate_basis, and price_per_hour stays as the on-demand baseline. A "Spot rate" line sits next to the unchanged "On-demand rate" line.
    • spawn status resolves one rate for its own lines: the tag first, then (for instances launched before this change) the current spot price using the caller's credentials, then on-demand labelled "spot price unknown, so this overstates". Max compute cost shows the spot figure with the on-demand bound next to it.
    • --cost-limit enforcement and the "Cost limit" line stay on the on-demand rate. That is what enforcement measures, and it can only overstate spot spend.
  • Running time. The daemon publishes its compute clock ({instance_id, started_at, base_seconds}) to /run/spored/compute-clock.json. /run is tmpfs, so the file is cleared by a stop/start. spored status adopts the clock after checking it: the instance id must match, the start must not be in the future, and it must not predate the current boot (/proc/uptime). The billed time is compute plus the boot gap, because EC2 bills from running.
    • Without a clock (older daemon, Windows), status labels the figure "as of the last checkpoint" and suppresses the boot/stopped split, which would otherwise be computed on the wrong clock.
    • Compute and cumulative cost show — until there is at least 1s of running time. The effective rate and its saving require the live clock, a known EBS rate and 1 minute elapsed, and are computed from unrounded figures. A ratio of zeros can no longer read as a saving.
  • One time format. formatStatusTime gives local time with UTC in parentheses (2026-10-15 10:59 MST (17:59 UTC), and just UTC when local is UTC). spawn re-renders spored's YYYY-MM-DD HH:MM UTC timestamps (Started, TTL) into that format for display only; the notices still parse the original text. Termination deadline and the lifecycle-mismatch warning use the same helper. spored's TTL line becomes — terminates … to avoid nested parentheses.

Tests

All unit tests, with mocked EC2 and stub providers. No AWS resources were created.

  • cmd/spored/status_cost_test.go: spot costed at spot (issue's m8a.4xlarge numbers), with the cost limit still on-demand. Spot with no recorded price is labelled. The query process counts the daemon's running time (47s scenario). No running time means no saving, on both the checkpoint path and the live-clock path.
  • pkg/agent/computeclock_test.go: a query agent is not live until it adopts a clock. Adoption is checked through the file round trip, and the test confirms it refuses another instance, a pre-boot clock, a future clock, a clock with no start and a negative base.
  • pkg/aws/spotprice_test.go: the instance's own AZ is used, the newest observation wins, a failure writes no tag, the product mapping is correct, and the instance is re-described when the response is incomplete.
  • cmd/status_spot_time_test.go: billable row and max cost use the spot price; lookup and labelled fallback work; on-demand instances make no lookup; formatStatusTime behaves as specified; every timestamp on the composed screen (spored lines in both old and new formats, deadline block, mismatch warning) uses the single format.
  • Each new test was watched failing against a compiling revert of the line it guards (rule 1). The assertions come from the issue's stated intent (rule 5).
  • make check passes, apart from TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker. That test needs a running Docker daemon, which was not available locally, and the change doesn't touch it. Cross-compiled vet passes for GOOS=windows and GOOS=linux.

Caveats

  • The compute-clock fix needs the new spored on the instance (launch, or spawn upgrade-spored). Older agents keep their old output, but spawn's own lines (time format, max cost, billable row) are fixed regardless.
  • The spot price is the one at launch; spot prices move. It is labelled as such.
  • Boot gaps of earlier boots (before a stop/start) are still not in the carried-over base.

Fixes #896

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Three problems on one status screen:

- Spot instances were labelled "(spot)" but costed at the on-demand rate.
  A spot launch now records spawn:spot-price-per-hour (DescribeSpotPriceHistory
  for the instance's AZ, type and platform); spored status and spawn's own
  max-cost and billable-resources lines use it. Older instances get the current
  spot price via the caller's credentials; with neither, the on-demand rate is
  used and labelled as an overstatement. --cost-limit stays on-demand.
- `spored status` is a fresh process and counted compute from its own start,
  leaving only the spawn:compute-seconds checkpoint (0 on a new instance) and
  a boot term equal to all of elapsed. The daemon now publishes its compute
  clock in /run/spored and status adopts it (validated against instance id
  and boot time). Boot time is billed. Cost shows "—" with no running time;
  the effective rate needs a minute and the live clock, so a ratio of zeros
  never reads as a saving.
- The TTL line was UTC and the termination deadline local. Every time on the
  screen is now "local (UTC)", including spored's re-rendered timestamps.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spawn status: spot instances are costed at the on-demand rate; brand-new instances show $0.00 / '100% lower' effective rate

1 participant