767a: per-space quota — store, admin API, enforcement #802

Closed
opened 2026-09-28 03:58:17 +00:00 by Cordy · 1 comment
Owner

Slice 1 of #767 (design pass: #767 comment). Server only; no UI.

Instructions

  1. Store: new package internal/spacequota, a copy of the spacemeta shape: Open(path), Get(id) (int64, bool), Set(id, bytes), Clear(id), All() map[string]int64, persisted via statestore. File spacequota.json next to spacemeta.json. Keyed by space ID.
  2. Registry: spaceids.Store.ByBackendDir(dir) (id string, rec Rec, ok bool), case-insensitive on BackendDir.
  3. Enforcement in internal/storage/quota:
    • Generalise the prefix: capPrefix(path) returns /home/<u> or /spaces/<d>; the usage cache credits both.
    • SetSpaceLimit(fn func(backendDir string) int64): 0 = uncapped.
    • Write: take the space room like the per-user room (min with MaxFileBytes), stream-capped. Overwrite netting: if the target exists, add its current size to the room (one Stat), so rewriting a file in a full space works. Apply the same netting to per-user.
    • Copy(src,dst): if dst's prefix is capped, size src (file size or treeSize) and refuse with ErrQuotaExceeded when it exceeds the room; credit on success.
    • Rename(src,dst): same check only when the src and dst prefixes differ.
    • SpaceUsage(ctx, backendDir) (used, limit int64, ok bool) for display; ok=false when uncapped.
  4. API (internal/api):
    • PUT /api/v1/spaces/{name}/quota {"bytes":N} (N ≥ 1) and DELETE /api/v1/spaces/{name}/quota.
    • Admin only (403 otherwise). Resolves both kinds like metaTarget does (store → registry → /spaces/<name> Stat → Ensure).
    • Audit space-set-quota / space-clear-quota.
    • GET /spaces entries and GET /spaces/{name}/meta gain quota and quotaUsed (live, only for capped spaces).
  5. Default: POST /api/v1/spaces sets 1<<30 on the new space's ID (both the Members and Mgr modes). Constant defaultSpaceQuota with a // #800 makes this an admin setting note.
  6. Wiring in cmd/cairnd/main.go: open the store, quotaDrv.SetSpaceLimit via ByBackendDir → spacequota.Get, pass Quota + SpaceUsage into SpacesAPI.

Tests (RED first)

  • Quota decorator: exact fit ok; 1 byte over → ErrQuotaExceeded; stream overshoot mid-write; cap below usage refuses a new file but allows an overwrite of equal or smaller size; Copy into a capped space refused when too big and allowed when it fits; Rename within a space never checked; a space write never charged to /home.
  • API: non-admin 403; set/clear round-trip; the list and meta carry quota/quotaUsed; create sets 1 GiB; rename keeps the cap.
Slice 1 of #767 (design pass: #767 comment). Server only; no UI. ## Instructions 1. **Store:** new package `internal/spacequota`, a copy of the spacemeta shape: `Open(path)`, `Get(id) (int64, bool)`, `Set(id, bytes)`, `Clear(id)`, `All() map[string]int64`, persisted via `statestore`. File `spacequota.json` next to `spacemeta.json`. Keyed by space **ID**. 2. **Registry:** `spaceids.Store.ByBackendDir(dir) (id string, rec Rec, ok bool)`, case-insensitive on `BackendDir`. 3. **Enforcement** in `internal/storage/quota`: - Generalise the prefix: `capPrefix(path)` returns `/home/<u>` or `/spaces/<d>`; the usage cache credits both. - `SetSpaceLimit(fn func(backendDir string) int64)`: `0` = uncapped. - `Write`: take the space room like the per-user room (min with MaxFileBytes), stream-capped. **Overwrite netting:** if the target exists, add its current size to the room (one `Stat`), so rewriting a file in a full space works. Apply the same netting to per-user. - `Copy(src,dst)`: if dst's prefix is capped, size src (file size or `treeSize`) and refuse with `ErrQuotaExceeded` when it exceeds the room; credit on success. - `Rename(src,dst)`: same check only when the src and dst prefixes differ. - `SpaceUsage(ctx, backendDir) (used, limit int64, ok bool)` for display; `ok=false` when uncapped. 4. **API** (`internal/api`): - `PUT /api/v1/spaces/{name}/quota` `{"bytes":N}` (N ≥ 1) and `DELETE /api/v1/spaces/{name}/quota`. - **Admin only** (403 otherwise). Resolves both kinds like `metaTarget` does (store → registry → `/spaces/<name>` Stat → `Ensure`). - Audit `space-set-quota` / `space-clear-quota`. - `GET /spaces` entries and `GET /spaces/{name}/meta` gain `quota` and `quotaUsed` (live, only for capped spaces). 5. **Default:** `POST /api/v1/spaces` sets `1<<30` on the new space's ID (both the Members and Mgr modes). Constant `defaultSpaceQuota` with a `// #800 makes this an admin setting` note. 6. **Wiring** in `cmd/cairnd/main.go`: open the store, `quotaDrv.SetSpaceLimit` via `ByBackendDir` → `spacequota.Get`, pass `Quota` + `SpaceUsage` into `SpacesAPI`. ## Tests (RED first) - Quota decorator: exact fit ok; 1 byte over → `ErrQuotaExceeded`; stream overshoot mid-write; cap below usage refuses a new file but allows an overwrite of equal or smaller size; Copy into a capped space refused when too big and allowed when it fits; Rename within a space never checked; a space write never charged to `/home`. - API: non-admin 403; set/clear round-trip; the list and meta carry quota/quotaUsed; create sets 1 GiB; rename keeps the cap.
Cordy closed this issue 2026-09-28 04:06:34 +00:00
Author
Owner

Shipped in v0.6.300 (PR #805, CI green), live on both dogfoods; both pods boot clean (no quota/store errors). Tests: RED 6f92b204 → green; go test ./... all ok, web 285/285.

Live behaviour check still pending: it needs a signed-in admin session (the redeploy logged Bao out) and Nikola's go-ahead to put a cap on a dogfood space. Existing spaces are unlimited, so nothing changes for them until a cap is set. The UI for caps is #803.

Shipped in **v0.6.300** (PR #805, CI green), live on both dogfoods; both pods boot clean (no quota/store errors). Tests: RED 6f92b204 → green; `go test ./...` all ok, web 285/285. Live behaviour check still pending: it needs a signed-in admin session (the redeploy logged Bao out) and Nikola's go-ahead to put a cap on a dogfood space. Existing spaces are unlimited, so nothing changes for them until a cap is set. The UI for caps is #803.
Sign in to join this conversation.
No labels
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: Cordy/Cairn#802
No description provided.