feat(quota): limit CI build minutes per runner class #105
No reviewers
Labels
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
codefloe/forgejo-codefloe!105
Loading…
Reference in a new issue
No description provided.
Delete branch "t3code/resolve-issue-96"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Implements #96, phase 4 of the CI build minutes quota work, on top of #100 (the ledger), #102 (the evaluator) and #104 (the external CI interface).
The motivating case is cost.
A macOS native runner costs several times what a shared Linux one does, and an owner should be able to hold a generous Linux budget and a small macOS one at the same time.
Everything needed for that was already in place:
classhas been onquota_ci_usageandquota_ci_recordsince phase 1 and both producers have been filling it in, so there is history to enforce against, no backfill and no schema change.What was missing is that no limit subject addressed a class.
What changed
Three new subjects, on a second axis
The class subtree is a second axis under
time:ci:allrather than a nesting of the source subtree: a job has both a source and a class, so both are evaluated and either can refuse it.The class list is a small closed set, matching the ledger's own.
windowsis included alongside the two the issue names, because accrual has been recording it since phase 1 and a class the ledger can write but no rule can name would be a trap.A class outside the set, which an external reporter may still book, accrues against
time:ci:alllike any other minute; it just has no dedicated subject to be capped by.The class axis caps, it does not grant
This is the one design decision worth reviewing.
Upstream's rule is that belonging to a quota group grants exactly what its rules mention, so a subject no rule mentions is refused.
Applied to the class axis that would break every setup written before this change: an owner in a group carrying a
time:ci:actionsrule would have their Linux jobs refused for want of atime:ci:class:linuxrule nobody had any reason to write.So
GroupList.AssessCapinmodels/quota/group_codefloe.gobinds only where an administrator wrote a rule naming the class outright.Rules that reach a class through
time:ci:allare left to the source axis, which evaluates them already.Within what is left the ordinary precedence applies: the strictest rule in a group binds, the most generous group wins.
time:ci:allis still registered as the parent of each class subject, so the hierarchy reads the way the issue draws it and a total rule still covers a class the way it covers a source.Enforcement
quota_ci.Evaluatenow takes a class as well as a source and returns aDecisionthat says whether a refusal was class-scoped.For Forgejo Actions the class is the class of the runner asking for work, so a class limit binds in
PickTaskrather than at run creation, where no runner is known and which class each job lands on has not been decided.A job refused by a class rule is cancelled on its own, carrying a new
WarningCodeCIQuotaClassExceededon its run that names the class, and the rest of the run goes ahead on the budget it has.Cancelling the whole run would charge a Linux job for a macOS limit; leaving the job queued instead would have the pick loop offer it again on every poll.
A refusal of the owner's CI time as a whole still takes the whole run, exactly as before.
The in-flight estimate is split by class
InFlightSecondsByClassresolves the class of each running task from its runner's labels, through the same function accrual uses when the task finishes, so the estimate and the bill cannot disagree.Without the split a macOS budget of a couple of hours would be hours past spent before the first job finished and reached the ledger, which is exactly the window a small class budget has to be right about.
Surfaces
GET /api/v1/quota/ci/check?repo=owner/name&class=macosnow answers against the class instead of validating and ignoring it, andremaining_secondsis whichever of the two budgets is smaller.A class-scoped refusal names the class in
reason, since a scheduler that can place the work elsewhere needs to know it was refused the machine rather than the minutes.The CI minutes card and
GET /api/v1/user/quota/cigain a breakdown by class, built from the same numbers the rules are evaluated against so that it includes work still running and cannot disagree with the breakdown by source next to it.Notes for the reviewer
Per-class cost multipliers are deliberately out of scope, as the issue asks.
The only edit outside the fork's own files, besides the two enforcement call sites, is one
casearm inmodels/actions/pre_execution_warnings.gorouting the new warning code to the fork's translator.Checklist
Tests for Go changes
*_test.gofor unit tests.tests/integrationdirectory if it involves interactions with a live Forgejo server.make lint-go,make lint-locale,make generate-swaggerand the quota unit and integration suites before pushing.Unit tests cover the new subjects and their units,
AssessCap(silent where no rule names the subject, binding where one does, unlimited rules, most-permissive-group-wins), the two-axis decision (a class cap refusing while the other class runs, a total refusing every class, the smaller remaining budget, an unknown class), per-class in-flight estimation and the new warning's rendering.Integration tests cover the motivating case end to end: a macOS runner is refused and only its job is cancelled while the Linux job of the same run is picked up and billed to its own class, plus the regression guard that a group granting CI time by source alone still runs work of every class.
Documentation
custom/conf/app.example.ininext to[quota.ci.default].Release notes
Request changes:
addCIQuotaWarningdeduplicates class-scoped warnings only byWarningCodeCIQuotaClassExceededatservices/actions/codefloe_quota_ci.go:98.If a run has, for example, both macOS and Windows jobs and both class budgets are exhausted, the first refusal records the shared warning code.
The second job is still cancelled, but its warning is discarded because the code is already present.
The run can therefore tell the user that only macOS jobs were affected even though Windows jobs were cancelled too.
Please deduplicate class-scoped warnings by both code and class, or aggregate all refused classes into one warning.
The focused package tests otherwise pass, and the two-axis evaluator, in-flight accounting, and job-scoped cancellation match the issue requirements.
3d0cf93e5f9df63151b6Fixed in
9df63151b6(amended, so the PR stays a single commit).You are right, and the second half of it was worse than the warning:
clearCIQuotaWarningdeleted only the first entry per code, so a rerun of a run carrying two class warnings would have left one behind to describe the previous attempt.Deduplicated by code and class rather than aggregated into one warning. Two refusals by two different budgets are two different sentences, each with its own rule name and its own used/limit pair, and the run view already renders one line per warning; folding them into one would have meant either dropping the rule names or inventing a message that lists them.
addCIQuotaWarningnow askscarriesCIQuotaWarning, which forWarningCodeCIQuotaClassExceededcompares the class in the warning's details rather than only the code. The details survive the round trip through the database as JSON, so the comparison is against astringand falls through safely if it is anything else. Every other code still dedupes per code: those describe the run as a whole.clearCIQuotaWarningnow walks the codes backwards and removes every quota entry, keeping the details in step.Tests, both of which fail against the previous commit and pass against this one:
TestQuotaCIWarningIsKeptPerClassinservices/actions/codefloe_quota_ci_test.go— two classes get two warnings, the same class twice gets one, details read back as JSON still dedupe, and a rerun clears both while leaving an unrelatedWarningCodePermissionsand its details untouched.TestActionsQuotaCIClassLimitBlocksOnlyThatClassnow pushes a run withlinux,macandwinjobs and exhausts both the macOS and the Windows budgets. Both runners are refused, both jobs are cancelled, the run names both classes in order, and the Linux job is still picked up and billed to its own class.make lint-gois clean and the quota unit and integration suites pass.