capacity

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package capacity decides, for each capacity group, how much waiting work it could serve and how many machines it should have. It is a pure function of a Snapshot: no I/O, no goroutines and no clock reads, and the same snapshot always gives the same answer whatever order its slices and maps arrive in.

The policy, in order:

  1. Absorb. Ready work that online, enabled, awake workers already have free cores for is taken off the demand first: workers in no group, then each group's online members, in group order. After them, each named group's waking members (asked to start, not registered yet, not disabled) absorb with all their cores, as pending capacity. Each worker's cores are spent once across all demand, and a retiring member absorbs nothing.
  2. Assign. What is left goes to the groups in priority order, each taking only work it could run, from queues it serves, up to the machines it could still add: a sized group's free slots, a named group's wakeable members. What one group cannot take passes to the next; work no group can run drives nothing.
  3. Retire. Online, awake members idle for at least the idle timeout that absorbed none of the demand are retire candidates, longest idle first, as many as can go without leaving fewer active members than min_machines or than the members kept (busy, idle too briefly, disabled, or absorbing demand).
  4. Size. The desired size counts the active members, those online and not retiring, and a named group's pending members as well: with demand to add machines for, the active members (retire candidates included), the pending ones and those machines; with none, the members kept. Growth is limited per evaluation by max_step_up and the scale-up cooldown, and the result is clamped to [min, max]. max_machines caps the machines running, so a retiring member still online counts toward it, and that cap wins over min_machines. Machines asked for that have not registered yet are held, so the size does not fall back while they boot: a step-up for one boot timeout, a named group's waking members until they register or their wake fails. New growth is measured from active members only, so machines that never register cost at most one step.
  5. Wake. A named group's wake candidates are its wakeable members (offline, not disabled, and awake, asleep, or wake_failed for at least twice the boot timeout) that could run work the group took, the smallest machine first (one reporting no cores last), then the one down longest, as many as the desired size has room for beside the active and pending members. When min_machines asks for more than that work does, any wakeable member makes up the rest.

All demand arithmetic is on task counts per step, never per task, so a step with a million ready tasks costs the same as one with a single task.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pending

func Pending(w store.Worker) bool

Pending reports whether w is pending capacity: a machine asked to start that has not registered yet, and is not disabled.

func Wakeable

func Wakeable(w store.Worker, bootTimeout time.Duration, now time.Time) bool

Wakeable reports whether w could be woken at now, bootTimeout being its group's: offline, not disabled, and awake or asleep, or wake_failed for at least wakeFailedBackoff boot timeouts. A wake_failed member with no recorded time is wakeable, and one recorded in the future holds for at most one backoff, as [within] does.

Types

type ActionRecord

type ActionRecord struct {
	// Action is wake, grow, retire, list or configure.
	Action string
	// Result is ok, failed, refused, unsupported, timeout or expired.
	Result  string
	Message string
	At      time.Time
}

ActionRecord is one provider action and its result.

type Degraded

type Degraded struct {
	Reason              string
	Since, BackoffUntil time.Time
}

Degraded is why a group stopped growing, and until when.

type DemandItem

type DemandItem struct {
	Demand store.StepDemand
	Job    store.Job
	Step   store.Step
}

DemandItem is one step's leasable ready tasks, with the job and step rows Snapshot.CanRun needs.

type Member

type Member struct {
	Worker store.Worker
	// CommittedCores is the CPU the worker holds across its assigned and
	// running tasks, an undeclared task counting as the whole machine.
	CommittedCores int
	// IdleSince is when the worker was first seen holding no task; nil while
	// it holds any.
	IdleSince *time.Time
}

Member is a worker as the policy sees it: one of a group's members, or a worker in no group.

type Mismatch

type Mismatch struct {
	WorkerID string
	// Reason names every way the member falls short, joined by "; ".
	Reason string
}

Mismatch is a member whose registration does not match its sized group's declared profile.

type Snapshot

type Snapshot struct {
	Now time.Time
	// Groups are the enabled groups, ordered priority descending, then name.
	Groups []store.CapacityGroup
	// Members are each group's members, online and offline, by group ID.
	Members map[string][]Member
	// Others are the online, enabled workers in no group.
	Others []Member
	Demand []DemandItem
	// CanRun reports whether w could ever run item's tasks, whatever its
	// current load. A nil CanRun runs nothing, so no demand counts.
	CanRun func(w store.Worker, item DemandItem) bool
	// LastStepUp is when each group, by ID, last stepped up. The caller keeps
	// the map between evaluations; Evaluate writes a group's entry when it
	// steps that group up (see [Evaluate]). A nil map records nothing, so no
	// cooldown applies and no machine counts as booting.
	LastStepUp map[string]time.Time
	// LastDesired is each group's DesiredMachines from the previous
	// evaluation, by group ID. The caller keeps the map between evaluations;
	// Evaluate writes every group's entry (see [Evaluate]). A nil map records
	// nothing, so no machine counts as booting.
	LastDesired map[string]int
}

Snapshot is everything one evaluation reads.

type Status

type Status struct {
	GroupID, GroupName string
	// DemandCores is the cores of ready work assigned to the group: work it
	// could run that no idle worker absorbed and no higher-priority group
	// took. It never exceeds what the group's free machine slots could hold.
	DemandCores int
	// DesiredMachines is the total the group should have, counting the
	// machines present. While its demand calls for machines it is the active
	// members, retire candidates included, and a named group's pending
	// members, plus those machines; otherwise it is the members it keeps.
	// Either way at least min_machines and at most max_machines less the
	// retiring members still online, which count toward max_machines until
	// they are gone; that cap wins over min_machines. See [Evaluate] for how an
	// actuator reads it.
	DesiredMachines int
	// OnlineMembers and OfflineMembers count the group's members by liveness;
	// a disabled member counts in whichever it is. A retiring member online
	// counts as online, though sizing does not count it.
	OnlineMembers, OfflineMembers int
	// ActiveMembers is the online members that are not retiring: the members
	// sizing counts, and the ones a grow is measured from.
	ActiveMembers int
	// Waking, Retiring, Asleep and WakeFailed count the group's members by
	// power state, whatever their liveness and disabled flag.
	Waking, Retiring, Asleep, WakeFailed int
	// WakeCandidates are a named group's members to wake now, by worker ID, in
	// order: at most DesiredMachines less the active members (online, not
	// retiring) and the pending ones (waking, not disabled). Empty for a sized
	// group.
	WakeCandidates []string
	// RetireCandidates are worker IDs, longest idle first. Retiring all of
	// them never leaves fewer active members than min_machines, and never
	// retires a member that absorbed demand. Every candidate is awake.
	RetireCandidates []string
	// Mismatches are a sized group's online members whose registration falls
	// short of the group's profile, by worker ID. Evaluate leaves it empty;
	// the caller, which can see each member's registration, fills it. A
	// mismatched member is still a member.
	Mismatches  []Mismatch
	EvaluatedAt time.Time

	// Requested, Degraded and LastAction are what the actuator recorded for
	// the group: the size it last asked a sized group's provider for, why the
	// group is degraded (nil when it is not), and its latest provider action
	// (nil before the first). Evaluate leaves them empty.
	Requested  int
	Degraded   *Degraded
	LastAction *ActionRecord
}

Status is one group's evaluation.

func Evaluate

func Evaluate(s Snapshot) []Status

Evaluate returns one Status per group, in s.Groups order.

DesiredMachines is a total target: the machines the group should have, counting those already present. An incremental actuator reads a Status as two independent instructions:

  • Grow: a named group wakes the WakeCandidates, by name. A sized group adds DesiredMachines − ActiveMembers machines, less the machines it has already launched that have not registered yet, when that is positive.
  • Shrink: retire the RetireCandidates, by name.

It never subtracts the candidates as well: they are already counted in DesiredMachines while the group grows (they are present until retired), and already excluded from it while the group shrinks (DesiredMachines is then the members kept, so retiring the candidates brings the group to it). A count-driven autoscaler that cannot retire by name sets its capacity to DesiredMachines and may then remove the wrong machine when shrinking.

Machines that arrive unable to run the demand that called for them (an image missing what the profile promises) stay idle, so the demand stays and the group keeps growing. That growth is bounded by max_machines and paced by registration (below), and the sweep reports such members as profile mismatches; backing off is left to the providers that act on the signal.

active is the group's online members that are not retiring, and pending a named group's waking members that are not disabled (zero for a sized group). Before any limit, the desired size is active + pending + need when the group's demand calls for need > 0 more machines (need = DemandCores over one machine's cores, rounded up), and kept, the active members that are not retire candidates, when it calls for none. min_machines then raises it, so on a cold start the machines min asks for and the machines demand asks for are the same machines.

The limits compare two numbers. current adds the machines asked for but not yet registered to active: it is the larger of active + pending and, while the last step-up is within g's BootTimeout, LastDesired[g]. The desired size may then be:

  • outside the scale-up cooldown, at most max(current, active + MaxStepUp): what was asked for is held, and new growth is paced by machines that registered, so a step-up whose machines never register costs one step and does not compound;
  • inside the cooldown, at most current.

Falling demand lowers the desired size below both. min_machines and max_machines apply last, max_machines less the retiring members still online, since max_machines caps the machines running; that cap wins over min_machines. The free slots demand may fill are likewise max_machines less the online and pending members, retiring ones included. A recorded time up to one window (cooldown or boot timeout) in the future, as after the wall clock steps back, counts as inside that window.

It writes two entries per group g and nothing else:

  • s.LastDesired[g.ID] = the group's DesiredMachines, on every evaluation.
  • s.LastStepUp[g.ID] = s.Now, when the desired size exceeds current and g was not inside its scale-up cooldown. Holding machines that are still booting is not a step-up, and a step-up forced by min_machines during a cooldown does not restart the cooldown.

Directories

Path Synopsis
Package script runs an operator-configured capacity provider: an executable named by a `script:<name>` provider in the server's configuration.
Package script runs an operator-configured capacity provider: an executable named by a `script:<name>` provider in the server's configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL