Skip to content

Site limits: change a site's plan and memory/CPU overrides after create (db, agent, jobs, api, ui) - #40

Merged
techgopal merged 11 commits into
mainfrom
web-o1/setu-site-limits
Dec 4, 2026
Merged

techgopal merged 11 commits into
mainfrom
web-o1/setu-site-limits

Conversation

@techgopal

@techgopal techgopal commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Change a site's limits after it is created (queue-opus row "change-site-limits").

  • db: site_limits (per-site memory/CPU overrides; NULL follows the plan), limits_update job kind, update_plan; server capacity counts each site's effective memory.
  • jobs: the limits_update job rewrites the unit drop-in with the effective limits; write-unit's daemon-reload gives a running unit the new MemoryMax/CPUQuota without a restart, and a stopped unit takes them on its next start. Every job that writes a unit writes the effective limits. (No set-property --runtime: its 50-*.conf override would beat a later drop-in; review of d9c775e.)
  • api: GET/PUT /sites/{id}/limits; a plan edit re-applies limits to the sites that follow it (409 while any of them has an active job); openapi.json regenerated.
  • ui: limits form on the site page (plan, overrides, room left on the server, warning below the 24 h memory peak); the plan form says how many sites follow the plan.
  • tests: db repos, jobs limits_update, api limits + openapi, ui site_limits.test.tsx. Also fixes a latent flake in 4 api test files: unique() used the ULID timestamp prefix, so names made within ~256 ms collided (409 on plan create).
  • docs: CHANGELOG ## Unreleased entry; re-wraps the S-H1 upgrade note (review nit on release 0.1.9: version, changelog dated (0.1.8 was never tagged) #39).

Verification: cargo test --workspace --no-fail-fast all green (with Postgres), clippy -D warnings clean, SQLX_OFFLINE check clean, ui typecheck/lint clean, ui 104/104.

…apacity on effective memory

Migration 0009 adds site_limits (memory_mb, cpu_percent overrides over the
plan) and the limits_update job kind. Limits::change_with_job stores the
plan and overrides and queues the job in one transaction, after locking the
server and rechecking capacity with the site's own memory taken out. Site
creation now sums effective memory (override, else plan).

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
… jobs write the effective limits

Command::ApplyLimits -> helper apply-limits (systemctl set-property --runtime
MemoryMax/CPUQuota, no restart). limits_update job: write_unit with the
effective limits, then apply_limits when running (restart on an agent older
than 0.1.8). build_spec takes the site's effective limits (plan + overrides),
so provision, update and restore keep an override. agent_at_least moves to
setu-proto.

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
… the sites that follow it

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…; plan edit says how many sites follow

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…tamp prefix

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>

# Conflicts:
#	docs/AGENT.md
Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
@techgopal
techgopal marked this pull request as ready for review December 1, 2026 23:19
@techgopal

Copy link
Copy Markdown
Contributor Author

Diff review: changes requested (d9c775e)

Reviewer: Opus (rev-f2). Scope: whole PR, merge-base 5816869 .. d9c775e. The security paths (the apply-limits helper verb, PUT /sites/{id}/limits) still need their Fable review. This is not one.

P1: clippy and test-arm fail on the head. crates/db/tests/repos.rs:1947: let active = db.jobs().enqueue(...) is never read (-D unused-variables). Either assert on it (for example, that the 409 leaves this job the only active one) or bind it to _.

P2: a stale --runtime override beats the drop-in after a stop. apply-limits runs systemctl set-property --runtime, which writes /run/systemd/system.control/setu-site@<short>.service.d/50-MemoryMax.conf and 50-CPUQuota.conf. Drop-ins from all directories are applied in filename order, so 50-* comes after 10-setu.conf and wins until reboot. To reproduce: a running site gets 512 MB (write-unit plus apply, 50-MemoryMax=512M). The site is stopped. A PUT (or a plan edit) sets 1024 MB, and limits_update only writes the unit because status != running. On the next start the unit runs at 512 MB while the DB and the UI say 1024. I reproduced this with a user unit on systemd 259: DropInPaths=.../10-setu.conf .../user.control/.../50-MemoryMax.conf, MemoryMax=629145600 after the drop-in was rewritten to 1024M and the unit restarted. site-remove also leaves that runtime directory behind.
Also measured: rewriting 10-setu.conf and running daemon-reload (which write-unit already does) changes a running unit's MemoryMax and its cgroup memory.max with no set-property (512M -> 1073741824 in both). So the apply step may not be needed at all. Either drop apply-limits and keep write-unit as the single source (simplest; the restart fallback for agents older than 0.1.8 then goes too), or keep it and have write-unit and site-remove delete the unit's system.control drop-ins before daemon-reload. Add a test for the stop -> change -> start order either way.

P2: a plan edit skips following sites whose job is in progress, and they keep the old limits. update_plan returns 409 for an active job only among sites in running/stopped (s.status IN ('running','stopped') filters queued before the active-job check). A following site in provisioning or updating has an active job that already read site_limits() (provision prepare, update run/env_step read it when they start). That job writes a drop-in with the old plan values, and no limits_update is ever queued for the site. Fix: run the active-job 409 over every site that follows the plan, whatever its status, and keep the status filter only for which sites get a job. Test it with ids[3] (provisioning) holding an active job.

Checked and fine: ApplyLimits is gated by agent_at_least(AGENT_0_1_8); moving agent_at_least into proto keeps the API behaviour. The helper validates short before the verb and refuses zero limits. The argv is fixed and the sudoers rule did not change. The PUT requires Operator (as aliases and noindex do), GET requires Viewer, and there is an audit row. Capacity is rechecked under FOR UPDATE on the site and the server; a concurrent second job is refused by jobs_one_active_per_site_idx. The migration's CHECKs match check_range. Every job that writes the unit now reads site_limits.

…rride to outlive a change); a plan edit waits for any following site's active job; db test reads its job

Review of #40 (d9c775e):
- P1: repos.rs bound an unused job; the test now asserts the 409 queued nothing.
- P2: apply-limits wrote /run/systemd/system.control 50-*.conf overrides that beat
  the rewritten drop-in after a stop -> change -> start. write-unit's daemon-reload
  already applies MemoryMax/CPUQuota to a running unit, so the helper verb, the
  ApplyLimits command and the old-agent restart fallback are gone; limits_update is
  write_unit, finish. Test: stop -> change -> start runs on the last limits.
- P2: update_plan's active-job 409 now covers every following site whatever its
  status (a provisioning/updating site's job already read the old plan); only
  running/stopped sites get a job. Test with the provisioning site.

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
@techgopal

Copy link
Copy Markdown
Contributor Author

c4caf51 diff review (full PR, security paths): changes requested

  1. [P2] crates/db/src/repo/limits.rs:206: change_with_job reads the target plan's memory_mb without a lock. If an Owner raises that plan while an Operator moves a site onto it, the two transactions can interleave like this. The PUT reads the old memory. update_plan doesn't see the site on the plan yet (not committed), so it skips that server's capacity check and the active-job 409. Both commit, and the limits_update then writes the new, larger MemoryMax past the server's capacity. Fix: read the plan FOR SHARE first, before the site and server locks (the same plan -> server order as update_plan). Add a db test that holds the plan's lock while change_with_job runs.

Nits (non-blocking): sites.create_with has the same unlocked plan read (older than this PR); the site.limits_update audit row has no old -> new values; the setu-internal plan doc still describes the removed apply-limits step.

Fixed since the last round: no --runtime override remains (limits come only from the drop-in, and the stop -> change -> start order is tested), and a plan edit now waits for an active job on any site that follows the plan.

…nt plan edit waits and rechecks the moved site's server (Fable review P2)

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
@techgopal

Copy link
Copy Markdown
Contributor Author

f44d4a5 delta review (c4caf51..f44d4a5): Diff review passed (f44d4a5)

Reviewer: Fable (rev-f2). Scope: the one fix commit against last round's P2.

  • P2 fixed: change_with_job reads the target plan FOR SHARE before the site and server locks. Order plan -> site -> server; update_plan takes plan -> servers and never locks sites, so no deadlock. A plan edit that commits first is seen by the locked re-read; a change that commits first makes update_plan see the moved site, check its server and return the 409 for its queued job.
  • limits_change_checks_a_plan_raise_that_holds_the_plan fails without the fix (the change would read 512 and succeed) and asserts nothing was stored.

Non-blocking nits from last round still open: sites.create_with's unlocked plan read; the setu-internal plan doc still names apply-limits. CI was pending at review time.

Review file: krishiv-internal ops/reviews/review-web-o1-setu-40-f44d4a527-fable.md (c227d911f).

@techgopal

Copy link
Copy Markdown
Contributor Author

f44d4a5 delta review (d9c775e..f44d4a5, Opus): Diff review passed (f44d4a5)

All three findings of the d9c775e review are fixed: the 409 test asserts its job, apply-limits and the runtime override are gone (drop-in + daemon-reload is the only source; re-measured on systemd 259 that a running unit takes both MemoryMax and CPUQuota, cgroup included, without a restart), and a plan edit waits for any following site's active job. The plan FOR SHARE read in change_with_job is correct and tested. CI green. Nits only (setu-internal plan doc, create_with follow-up).

…concurrent plan raise waits and is counted; NewSite.plan_memory_mb removed (#40 review nit)

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
@techgopal

Copy link
Copy Markdown
Contributor Author

6b57bf1 delta review (f44d4a5..6b57bf1, security paths): Diff review passed (6b57bf1)

Scope: the one commit. create_with now reads the plan FOR SHARE first in its transaction and admits against that value in both places; NewSite.plan_memory_mb is gone everywhere. Lock order checked for a cycle: create is plan -> hosts -> server, change_with_job plan -> site -> server, update_plan plan -> servers with no hosts lock; the other holders of the hosts lock take no plan or server lock. The new wait test fails without the lock (the create returns before the 300 ms check). Nothing was run here; CI is green on this head.

No findings. One nit, not blocking and older than this commit:

  • crates/db/tests/repos.rs, create_site_counts_control_plane_overhead, the last step ("Auto-pick skips the control plane's server") passes on a tie, not on the rule. Free memory is 1700 - 512 - 512 = 676 on the control plane's server and 1700 - 1024 = 676 on the plain one, and a 64 MB site fits both (1344 on the control plane's). max_by_key returns the last equal maximum in ORDER BY s.id, so the plain server wins because its ULID is later; two servers made in the same millisecond order at random. Auto-picking on the 512 plan before d.example.com is created (1792 > 1700 against 1280) would let only the filter choose.

…le, not by a tie in free memory (#40 review 6b57bf1 nit)

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
@techgopal

Copy link
Copy Markdown
Contributor Author

e0dae9f delta review (6b57bf1..e0dae9f, security paths, Fable): Diff review passed (e0dae9f)

Scope: the one commit, test only (crates/db/tests/repos.rs, +5/-8). No findings.

  • The auto-pick step of create_site_counts_control_plane_overhead is now decided by the rule: for a 512 site, 512 + 512 + 256 + 512 = 1792 > 1700 on the control plane's server and 1280 on the plain one, so fits leaves one candidate; free_mb is 676 against 1188, so the ranking has no tie and the order of the server ids no longer matters.
  • Note, no change asked: a change that dropped the control-plane term from both fits and free_mb would tie again and usually still pick the plain server (joined second); the step before (explicit server, NoCapacity) catches it.

Read only, nothing run here. CI on this head: fmt, clippy, deny, test-arm, e2e-arm green; test (x86) was still running when this was written and must be green before the merge.

Review file: krishiv-internal ops/reviews/review-web-o1-setu-40-e0dae9f16-fable.md.

@techgopal

Copy link
Copy Markdown
Contributor Author

e0dae9f delta review (f44d4a5..e0dae9f, Opus): Diff review passed (e0dae9f)

  • 6b57bf1: create_with reads the plan FOR SHARE first, and NewSite.plan_memory_mb is gone. Lock order is plan -> hosts -> server, which matches update_plan (plan -> servers in id order) and change_with_job (plan -> site -> server), so there is no cycle. An unknown plan gives not_found.
  • e0dae9f: the auto-pick step is now decided by capacity (1792 > 1700 on the control plane's server, 1280 on the plain one), no longer by a 676/676 tie.
  • Verified: create_site db tests 6/6 on e0dae9f. With FOR SHARE removed, create_site_checks_a_plan_raise_that_holds_the_plan fails (the site is created at the old 512), so the test catches the regression. CI is green.

Nits (non-blocking):

  1. That test's "the create did not wait" assert cannot fail. The site INSERT's FK check takes FOR KEY SHARE on the plan, so it blocks on the edit's FOR UPDATE with or without the fix. The NoCapacity assert is what catches it. Reword the comment or drop that assert.
  2. No test pins the control-plane term in free_mb, i.e. ranking among servers that all fit. Example: control plane with 0 used vs plain with 256 used, at 64 MB: plain wins at 1444 vs 1188, but the control plane would win without the term. Predates this PR.
  3. Carried over: the 409 wording when a site moves off a plan while that plan is being edited, and apply-limits in the setu-internal plan doc.

Review: ops/reviews/review-web-o1-setu-40-e0dae9f16.md (krishiv-internal)

techgopal added a commit that referenced this pull request Dec 3, 2026
Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
@techgopal
techgopal merged commit 2a4356a into main Dec 4, 2026
14 checks passed
techgopal added a commit that referenced this pull request Dec 5, 2026
Conflicts: agent postgres.rs, tests/postgres.rs, tests/restore_pg.rs,
AGENT.md and OPERATIONS.md on main were earlier states of this branch
(363518f, 0adf3f9), so this branch's versions are kept. CHANGELOG: the
0.1.9 section is main's as released; Unreleased gets main's site-limits
entry and this branch's later entries (S-L11 moved out of 0.1.9, where
the merge had placed it). The branch's migration 0009_agent_command_priority
is now 0010 (main's 0009_site_limits came first); a test NewSite drops the
plan_memory_mb field #40 removed.

Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
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.

1 participant