Skip to content

Allow num_instances = 0 to keep a fixed service deployed but stopped - #1321

Closed
jcasimir wants to merge 7 commits into
mirendev:mainfrom
jcasimir:fixed-num-instances-zero
Closed

jcasimir wants to merge 7 commits into
mirendev:mainfrom
jcasimir:fixed-num-instances-zero

Conversation

@jcasimir

@jcasimir jcasimir commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Why

There's no way to keep an app deployed but not running. Autoscaled web services already drain to zero, but the next request wakes them. Fixed services can't go to zero at all:

  • num_instances = 0 is rejected at parse time.
  • miren sandbox-pool set-desired <pool> 0 is undone within a minute, because the launcher's resync raises fixed pools back to their configured count.

That leaves miren app delete as the only way to park an app with a worker or an in-app database. Deleting throws away routes, env, and history.

Our case: we're moving apps between machines and want to keep the old copy deployed but stopped as a fallback, without deleting it.

What

Allow num_instances = 0 for fixed-mode services. The service stays deployed (its pool, disks, addons, routes, and env stay in place) and nothing boots until a deploy raises the count:

[services.worker.concurrency]
mode = "fixed"
num_instances = 0

Design choices worth flagging

  • Config, not a command. Stopping becomes a deploy, like every other change to how a service runs. A first-class miren app suspend / resume (a flag on the App that the launcher and activator respect) would give a resume that skips a deploy. That's a bigger change, and I'm happy to open a proposal for it separately. This PR is the smaller step that makes zero reachable at all.
  • An omitted key still errors. NumInstances becomes *int in appconfig, so mode = "fixed" with no num_instances fails with "required" instead of reading as 0. A forgotten line can't silently stop a service.
  • Disks and single-writer addons accept 0. A stopped service holds no lease and no writer. Apps with an in-app database are the main reason to want this.
  • MaxInstances() == 0 is the signal. FixedStrategy already returns its count from MinInstances/MaxInstances, so the launcher, activator, and pool manager each ask the strategy rather than parsing config again. Before this PR, fixed with 0 couldn't be stored: the client rejected it, hydration defaults to 1, and the empty-concurrency migration only fills blanks. So no existing app changes behavior.
  • New stopped health state. When every service is fixed at zero, health had nowhere to go but starting, so miren deploy waited out its timeout on a successful deploy. stopped is terminal for deploy, shows in app list, and marks services in app status. A service fixed at zero no longer counts as a pinned fixed service, so an app with an idle autoscale web and a stopped worker still reads as idle. Deploy events publish it as scaled_to_zero rather than adding a value to that vocabulary, the same way the no-service outcome maps onto task_only.

Touch points

Area Change
appconfig NumInstances *int; fixed mode requires it and allows 0; disk and single-writer checks accept 0
controllers/deployment/launcher.go Create or reset a stopped service's pool at 0; skip the deploy-verification boot and the readiness wait
controllers/sandboxpool/manager.go Don't count a stopped service's dying sandboxes as crashes. Otherwise cooldown floors a referenced pool to 1 and revives it
components/activator, servers/httpingress ErrServiceStopped up front instead of waiting out the 120s pool timeout; served as 503
servers/app, pkg/apphealth, cli/commands stopped health state, deploy verdict, list and status rendering
docs "Stopping a Service" in scaling.md; app.toml, disks, and addons notes

A separate fix along the way

9ebc795 fixes an existing bug. When a sandbox proves healthy, the pool manager clears ConsecutiveCrashCount and CooldownUntil, but it saves its changes through Encode(), which drops zero values. The reset never reached the store, so the next crash backed off from the stale streak. TestManagerHealthySandboxResetPersists fails on main and passes with the fix. The stopped-service guard depends on the same persistence, so it's in this PR. I'm happy to split it out if you'd rather take it on its own.

Tests

Each new test fails without its change and passes with it. I checked by reverting each production change and re-running.

  • appconfig: 0 accepted, an omitted key rejected as required, negative rejected; disks and miren-sqlite accept 0 and still reject >1
  • controllers/deployment: a new pool starts at 0; running → 0 stops without a verification boot; the resync leaves it at 0; raising the count starts it again
  • controllers/sandboxpool: a stopped pool with prior crash state and a young dead sandbox stays at 0 and clears its crash state; healthy-reset persistence (above)
  • components/activator: AcquireLease on a stopped service returns ErrServiceStopped in well under a second
  • servers/app, cli/commands: classify table (stopped alone and beside idle, starting, and running services), noteSpec, and the deploy decision

All touched packages pass locally with TZ=UTC. Two caveats: tests that need the etcd dev environment didn't run here. TestManagerLongStartupFailureBackoff fails on main outside UTC because it compares a UTC time against a Local one. golangci-lint v2.12.2 is clean on the touched packages.

🤖 Generated with Claude Code

jcasimir and others added 7 commits October 7, 2026 08:50
An omitted num_instances now fails with a required-key error instead of
reading as zero, so only an explicit 0 can stop a service. Miren disks and
single-writer addons accept 0 alongside 1, since a stopped service holds no
writer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The launcher creates or resets their pool at zero, skips the one-instance
boot it does for deploy verification, and doesn't wait for readiness on a
pool with nothing to boot. The minutely resync leaves them at zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The pool manager clears ConsecutiveCrashCount and CooldownUntil once a
sandbox has run long enough, but hands the change back through Encode(),
which drops zero values. The reset never reached the store, so the pool kept
its old streak and the next crash backed off from the stale count. Write
both fields explicitly, as the status counters already are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A sandbox that dies young while a num_instances = 0 service stops would be
counted as a crash. Cooldown then floors a referenced pool's desired count to
one, reviving the service the operator just stopped, and status reads crashed
for up to fifteen minutes. Clear crash state instead. The config lookup only
runs for a zero-desired pool that has crash state to clear.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The activator returned the pool at its zero cap and left the caller waiting
for capacity until the 120s pool timeout. It now returns ErrServiceStopped
up front, which httpingress serves as 503 and internal requests report as
the error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every-service-at-zero used to classify as starting, so miren deploy waited
out its timeout on a successful deploy. A new stopped health state covers it:
deploy finishes with a stopped verdict, app list shows it, and app status
marks the stopped services. A service fixed at zero no longer counts as a
pinned fixed service, so beside an idle autoscale web service the app still
reads as idle. Deploy events publish it as scaled_to_zero rather than adding
an outcome to that vocabulary.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jcasimir
jcasimir requested a review from a team as a code owner October 7, 2026 15:02
@evanphx

evanphx commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@jcasimir Interesting! I think I'd rather have an explicit "disabled" state rather than setting num_instances to 0. That allows the system to know that the user has disabled the app rather than there is some sort of broken state.

@jcasimir

jcasimir commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Test runners 3 and 4 failed with [setup failed] after 0.0s on packages this PR doesn't touch (components/etcd, network, components/runner, pkg/addon/mysql, …). All of them compile and vet cleanly on this branch locally, and the other three runners, lint, and build passed. Could someone with access rerun the failed jobs? Fork contributors can't trigger reruns.

@jcasimir

jcasimir commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@evanphx Agreed, an explicit state is better: it records that the operator turned the app off, which an app at zero instances can't tell you. I've reworked this as #1322, miren app disable / enable, a disabled flag on the App that the launcher, activator, and pool manager respect. It no longer touches num_instances. The crash-streak persistence fix moved over with it. Closing this one in favor of that.

@jcasimir jcasimir closed this Oct 7, 2026
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.

2 participants