Scheduling algorithms and constraints
The scheduler chooses work for each pool by balancing fair share or bid value with queue limits, gang requirements, node capacity, shared resources, pricing, preemption, and retry state. A scheduling round builds one view of demand and running work, then turns it into placements or preemptions for the parent scheduling system.
Scheduling decision model
The scheduler evaluates one pool at a time. It combines queued demand with running demand, builds the pool's capacity view, and asks the queue scheduler to offer candidate gangs. A gang is one scheduling unit. A job without gang membership is a gang with one member.
The main decision path is:
flowchart TD
A[Queued and running demand] --> B[Pool capacity and queue state]
B --> C[Queue and gang candidates]
C --> D[Fair-share or bid ordering]
D --> E[Round and queue constraints]
E --> F[Gang and shared-resource checks]
F --> G[Node fit and preemption]
G --> H[Placement or preemption result]
Scheduling rounds
Each pool round tracks capacity used and work added during the round. The round stops when its time, resource, or global rate limits prevent more new work. A queue limit can stop one queue while other queues continue.
The scheduler uses policy time for scheduling decisions that must be repeatable, such as round timestamps and rate-limiter refills. The default policy clock uses host time. A supplied policy clock does not control host cancellation, hard scheduling timeouts, soft scheduling budgets, or computation-duration measurements.
The scheduler can enforce both a global maximum scheduling rate and a per-queue rate. A gang consumes the relevant admission allowance as one unit. A separate pool limit can restrict fair-share preemptions. That limit blocks work that needs eviction but does not block work that fits in free capacity.
References: internal/…/scheduling_algo.go• L82-87 internal/…/scheduling_algo.go• L108-134 internal/…/scheduling_algo.go• L155-169 internal/…/scheduling_algo.go• L251-295 internal/…/preempting_queue_scheduler.go• L83-91 internal/…/scheduling.go• L45-50 internal/…/scheduling.go• L98-123 internal/…/scheduling.go• L578-603 internal/…/policy_clock_test.go• L26-40 internal/…/policy_clock_test.go• L250-295 internal/…/policy_clock_test.go• L381-404
Queue demand and fairness
Fair-share scheduling compares each active queue's allocation with its demand-capped share. The scheduler groups demand and allocation by queue and priority class so that fairness and queue limits use the same resource view. Queue weight comes from its priority factor or an active override.
The scheduler distributes fair share by priority class. A queue that needs less than its share gives the unused part to other active queues. Running demand counts as demand because current work already affects the queue's share. A cordoned queue does not add new queued demand to placement, but its running work remains in the scheduling view.
Queued demand can come from a demand aggregate instead of a full queue scan. When comparison is enabled, the scan remains authoritative and the aggregate only reports differences. When aggregate demand is the source, running demand still comes from the scheduling scan, and priority classes present only in the aggregate remain active for fair-share setup.
References: internal/…/scheduling_algo.go• L606-667 internal/…/scheduling_algo.go• L670-814 internal/…/scheduling.go• L222-260 internal/…/scheduling.go• L262-417 internal/…/scheduling_algo_test.go• L1619-1807
Candidate ordering
The queue scheduler compares the next eligible gang from each queue. Fair-share mode favors queues below their adjusted share, then considers current allocation cost, candidate cost, priority, job size preference, and stable queue order. The scheduler can prefer home work over away work when pool policy enables it. Market mode uses bid value as its main order.
Ordering only states which candidate the scheduler tries first. It does not guarantee placement. A candidate that fails a constraint can be skipped so another candidate can use the remaining capacity. A lookback limit can bound new queued work considered at once while still allowing evicted work to re-enter the candidate stream.
References: internal/…/queue_scheduler_test.go• L997-1155 internal/…/queue_scheduler_test.go• L1247-1470 internal/…/queue_scheduler_test.go• L50-95 internal/…/scheduling_algo.go• L714-773
Gang scheduling
A gang is admitted and placed as one unit. The scheduler gathers the active members required by the gang shape, checks the full request, and does not publish a partial success when one member cannot be placed.
Gang admission
Before node placement, the scheduler checks limits that apply to the complete gang. These include round resources, global and queue rate tokens, burst sizes, queue state, resource caps, and floating-resource capacity. A gang can fail admission even when a node has enough CPU or memory because the pool or queue cannot admit the complete unit.
When a gang includes away work, pool policy can allow or forbid that form of scheduling. A node-uniform gang must also find enough matching nodes in one required label value. If the full shape cannot fit, the scheduler leaves the gang unscheduled rather than placing only the members that fit.
References: internal/…/queue_scheduler_test.go• L121-147 internal/…/queue_scheduler_test.go• L387-429 internal/…/scheduling_algo_test.go• L988-1039 internal/…/scheduling_algo.go• L1046-1065
Gang placement
The scheduler tests gang members in a shared scheduling view. Each successful member changes the view used for the remaining members, so the result represents one complete placement decision. If any required member fails, the gang does not produce a successful partial result.
References: internal/…/scheduling_algo_test.go• L1041-1093 internal/…/queue_scheduler_test.go• L387-429
Resource constraints
Resource constraints operate at pool, queue, shared-resource, and node levels. Higher-level checks limit how much work can enter a round. Node checks decide whether a specific request can run on a specific node.
Node resources
Node fit requires static compatibility and available dynamic capacity. Static compatibility includes labels, taints, tolerations, affinity, and other placement rules. Dynamic capacity includes resources used by current jobs, resources reserved by other pools, and the configured pod-slot resource.
The scheduler uses only healthy, responsive, and non-cordoned executors for new placement. Existing work on an unschedulable or overallocated node remains visible so the scheduler can account for it and reschedule it when required. Work from other pools is marked as unavailable on shared nodes to prevent double allocation.
References: internal/…/scheduling_algo.go• L428-550 internal/…/scheduling_algo.go• L1131-1210 internal/…/scheduling_algo.go• L1213-1302 internal/…/queue_scheduler_test.go• L295-373 internal/…/scheduling_algo_test.go• L580-643
Floating resources
A floating resource is pool-wide capacity that is not tied to one node. It represents a shared limit such as access to an external service. The scheduler adds available floating capacity to the pool capacity view and checks a home gang's declared demand before placement.
A gang is rejected when its shared-resource demand would exceed the pool limit. Away work is not checked against the current pool's floating-resource limit because its home pool owns that admission decision. This check limits scheduling claims. It does not verify use outside the scheduler.
References: internal/…/scheduling_algo.go• L547-558 internal/…/scheduling.go• L649-672 internal/…/scheduling_algo_test.go• L361-429
Queue and pool limits
Pool and queue limits cap the resources that new work can claim. Limits can apply to the whole round, a priority class, or a queue's share of a pool resource. The scheduler applies these caps before node placement, so free node capacity does not bypass a higher-level limit.
References: internal/…/scheduling_algo.go• L904-998 internal/…/queue_scheduler_test.go• L149-210 internal/…/scheduling_algo_test.go• L767-868
Pricing and value
Market-driven pools use bid value to rank work that competes for scarce capacity. The market order still obeys gang admission, shared-resource, node-fit, and preemption rules, so a high bid cannot make an impossible placement valid.
Market selection
Higher bids receive earlier consideration. Equal bids favor work that is already running over queued work, which limits needless preemption. Stable tie rules and rotation prevent one queue from taking all equal-priced capacity. Home protection can also let home work outrank a higher-priced away candidate.
When a placement crosses the pool's spot-price cutoff, the crossing bid determines the cutoff and a competing bid can set the billing override. This pricing result describes the selected allocation. It does not replace the placement decision.
References: internal/…/scheduling_algo.go• L1031-1043 internal/…/scheduling_algo.go• L1120-1128 internal/…/scheduling_algo.go• L1011-1023 internal/…/scheduling_algo.go• L1100-1108 internal/…/queue_scheduler_test.go• L699-717
Indicative value
The scheduler can estimate whether a configured gang shape could run and the minimum price needed under the current market view. The estimate uses key capacity checks, but it is not a later placement guarantee.
The idealised value view treats pool capacity as one theoretical node. It measures an upper bound and does not account for job-specific placement rules or gang uniformity. Live scheduling records realised value separately after placement.
References: internal/…/scheduling_algo.go• L1045-1065 internal/…/preempting_queue_scheduler.go• L643-665 internal/…/scheduling_algo.go• L1120-1128
Preemption
Preemption releases capacity by removing work that can yield. The scheduler selects incoming work and the work to remove as one result, then updates resource and queue accounting together. Non-preemptible work remains protected.
Preemption strategies
Urgency-based preemption can protect a higher-priority request within or across queues. Fair-share preemption can move capacity toward queues that are below their share. Pool settings can enable one path, the other path, both paths, or neither path.
The scheduler first balances capacity by evicting eligible work, then tries evicted work and new work. It can evict work from oversubscribed nodes and make a second scheduling pass for the affected work. An optional fairness optimiser can run after these passes in non-market pools. It compares possible placements by the fairness gain for incoming work and the fairness cost imposed on queues that lose work. It rejects a placement when the improvement does not pass the configured threshold.
References: internal/…/scheduling_algo.go• L866-895 internal/…/preempting_queue_scheduler.go• L83-247 internal/…/preempting_queue_scheduler.go• L668-711 internal/…/scheduling_algo_test.go• L870-987 internal/…/queue_scheduler_test.go• L742-947
Preemption eligibility
Only active, preemptible work can be evicted. The scheduler also excludes work that exceeds the configured maximum eviction size, work already evicted in the current attempt, and work at a higher priority than the incoming job.
Free capacity remains usable when the fair-share preemption rate limit is exhausted. The limit applies to eviction. It does not apply to ordinary placement.
References: internal/…/preempting_queue_scheduler.go• L96-138 internal/…/scheduling.go• L578-603 internal/…/scheduling_algo_test.go• L43-92 internal/…/queue_scheduler_test.go• L806-947 internal/…/queue_scheduler_test.go• L950-980
Gang preemption
Gang preemption is all-or-nothing for the active members of the affected gang. If one member is selected for eviction, the scheduler finds the other active members and treats the gang as the eviction unit. This prevents a partial gang from remaining allocated while its sibling work is retried.
References: internal/…/preempting_queue_scheduler.go• L316-346 internal/…/preempting_queue_scheduler.go• L357-425 internal/…/preempting_queue_scheduler.go• L461-550 internal/…/scheduling_algo_test.go• L1041-1083 internal/…/queue_scheduler_test.go• L1487-1506
Retry constraints
Retry policy controls whether a failed run becomes future queued work. It is separate from queue ordering and node selection, but its result changes the candidates that later rounds can evaluate.
Retry policy
The scheduling algorithm distinguishes ordinary placement from work returned after eviction or reconciliation. Evicted work is considered before new work when the scheduler is recovering capacity. A job already marked as preempted in the current round is not offered again in that round.
The scheduler preserves gang integrity during re-entry. If one gang member is preempted or fails reconciliation, the related gang members can be handled together so the gang does not remain partly active.
References: internal/…/preempting_queue_scheduler.go• L149-223 internal/…/preempting_queue_scheduler.go• L357-425 internal/…/queue_scheduler_test.go• L1247-1506 internal/…/scheduling.go• L466-508 internal/…/scheduling_algo_test.go• L714-766
Retry re-entry
A retry that changes resource needs or node avoidance must pass placement checks before it returns to the queue. If the changed request fits no node, the scheduler does not requeue it. A valid retry re-enters normal queue, gang, shared-resource, and node checks in a later round.
References: internal/…/scheduling_algo.go• L381-433 internal/…/queue_scheduler_test.go• L1326-1470
Change checklist
When these files or sections are changed, remember to consider these:
- Keep aggregate and scan demand aligned by queue and priority class. Preserve the rule that comparison treats the scan as authoritative.
- Keep home and away allocation separate from other-pool reservations so fairness and node capacity do not double-count work.
- Preserve gang-wide admission, placement, and preemption when changing candidate or node ordering.
- Keep executor health filtering separate from accounting for work already running on stale, lagging, cordoned, or unschedulable nodes.
- Keep queue and pool rate limits distinct from resource caps. A gang must consume each applicable allowance as one unit.
- Keep floating-resource capacity in the pool total used for both admission and value estimates.
- Keep fair-share preemption marking and rate-limit accounting idempotent for each job in a round.
- Preserve the distinction between evicted work and new work when applying lookback and timeout limits.
- Keep policy-clock reads separate from host-time cancellation, soft budgets, and computation-duration measurements.
- Keep the fairness optimiser disabled for market-driven pools unless its market behavior is defined.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Aggregate demand differs from the queue scan | Fair-share input can be wrong if the aggregate silently replaces live demand | Comparison records the mismatch and the scan remains authoritative |
| A gang exceeds a queue or pool limit | The full gang stays out of placement even when nodes have free capacity | Admission checks apply limits to the complete gang |
| One gang member cannot fit | The scheduler does not publish a partial gang placement | Gang scheduling requires the complete candidate to succeed |
| A node has free CPU but no pod slot | The request remains unschedulable on that node | Node fit includes the configured pod-slot resource |
| Floating-resource demand exceeds pool capacity | The gang is rejected before node placement | Pool-wide declared demand is checked first |
| The fair-share preemption limit is exhausted and no free capacity exists | A placement that needs eviction does not proceed | The limit applies to eviction while free-capacity placement remains allowed |
| A candidate was already preempted in the round | It can be scheduled again and cause duplicate work if it is offered again | The scheduler marks preempted jobs and skips them |
| A gang loses only one active member | The gang becomes partially allocated | Gang members are treated as one eviction or recovery unit |
| A retry no longer fits any node | The failed request can return to the queue without a viable placement | The scheduler checks the changed request before re-entry |
| Policy time is used for a host-time budget or cancellation check | A fake or replay clock can make the round end too early or too late | Policy time is limited to scheduling decisions; operational limits use host time |
Key files
| File | Why to read it |
|---|---|
| internal/…/scheduling_algo.go | Builds pool demand, capacity, queue context, node state, limits, and the scheduling result |
| internal/…/scheduling.go | Holds round state, fair-share calculations, allocation accounting, and preemption limits |
| internal/…/preempting_queue_scheduler.go | Coordinates eviction, rescheduling, new placement, optional optimisation, and market pricing |
| internal/…/policy_clock_test.go | Shows the boundary between policy time and host time for rate limits, preemption, cancellation, and budgets |
| internal/…/queue_scheduler_test.go | Shows queue ordering, fairness, gangs, rate limits, node constraints, preemption, and timeout behavior |
| internal/…/scheduling_algo_test.go | Shows pool-level scheduling, floating resources, executor filtering, gang behavior, preemption, reconciliation, and demand aggregation |
| Scheduling system | Parent page for the scheduler cycle and service boundaries |
| Scheduler state and ingestion | Related page for the state that feeds scheduling decisions |