Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: implement scheduling mechanism for prebuilds#18126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

evgeniy-scherbina
Copy link
Contributor

@evgeniy-scherbinaevgeniy-scherbina commentedMay 30, 2025
edited
Loading

Closescoder/internal#312
Depends oncoder/terraform-provider-coder#408

This PR adds support for defining anautoscaling block for prebuilds, allowing number of desired instances to scale dynamically based on a schedule.

Example usage:

data "coder_workspace_preset" "us-nix" {  ...    prebuilds = {    instances = 0                  # default to 0 instances        scheduling = {      timezone = "UTC"             # a single timezone is used for simplicity            # Scale to 3 instances during the work week      schedule {        cron = "* 8-18 * * 1-5"    # from 8AM–6:59PM, Mon–Fri, UTC        instances = 3              # scale to 3 instances      }            # Scale to 1 instance on Saturdays for urgent support queries      schedule {        cron = "* 8-14 * * 6"      # from 8AM–2:59PM, Sat, UTC        instances = 1              # scale to 1 instance      }    }  }}

Behavior

  • Multipleschedule blocks perprebuilds block are supported.
  • If the current time matches any defined autoscaling schedule, the corresponding number of instances is used.
  • If no schedule matches, thedefault instance count (prebuilds.instances) is used as a fallback.

Why

This feature allows prebuild instance capacity to adapt to predictable usage patterns, such as:

  • Scaling up during business hours or high-demand periods
  • Reducing capacity during off-hours to save resources

Cron specification

The cron specification is interpreted as acontinuous time range.

For example, the expression:

* 9-18 * * 1-5

is intended to represent a continuous range from09:00 to 18:59, Monday through Friday.

However, due to minor implementation imprecision, it is currently interpreted as a range from08:59:00 to 18:58:59, Monday through Friday.

This slight discrepancy arises because the evaluation is based on whether a specificpoint in time falls within the range, using thegithub.com/coder/coder/v2/coderd/schedule/cron library, which performs per-minute matching rather than strict range evaluation.

@evgeniy-scherbinaevgeniy-scherbinaforce-pushed theyevhenii/prebuilds-autoscaling-mechanism branch from606894f toff0e813CompareMay 30, 2025 12:25
@evgeniy-scherbinaevgeniy-scherbina changed the titleImplement autoscaling mechanism for prebuildsfeat: implement autoscaling mechanism for prebuildsMay 30, 2025
@evgeniy-scherbinaevgeniy-scherbinaforce-pushed theyevhenii/prebuilds-autoscaling-mechanism branch 5 times, most recently from9af5e02 tobcfbb04CompareJune 6, 2025 19:53
@evgeniy-scherbinaevgeniy-scherbinaforce-pushed theyevhenii/prebuilds-autoscaling-mechanism branch 4 times, most recently from3a25178 toe0d1de7CompareJune 11, 2025 19:05
@@ -0,0 +1,12 @@
-- Add autoscaling_timezone column to template_version_presets table
ALTER TABLE template_version_presets
ADD COLUMN autoscaling_timezone TEXT DEFAULT 'UTC' NOT NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why do we need a default here, if the provider is not defining a default?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It'snot null, so it requires default. You're suggestingDEFAULT ''?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

it was defensive mechanism to make sure we don't fail here ontime.LoadLocation(p.Preset.SchedulingTimezone):

func (pPresetSnapshot)CalculateDesiredInstances(at time.Time)int32 {iflen(p.PrebuildSchedules)==0 {// If no schedules are defined, fall back to the default desired instance countreturnp.Preset.DesiredInstances.Int32}// Validate that the provided timezone is valid_,err:=time.LoadLocation(p.Preset.SchedulingTimezone)iferr!=nil {p.logger.Error(context.Background(),"invalid timezone in prebuild scheduling configuration",slog.F("preset_id",p.Preset.ID),slog.F("timezone",p.Preset.SchedulingTimezone),slog.Error(err))// If timezone is invalid, fall back to the default desired instance countreturnp.Preset.DesiredInstances.Int32}}

It shouldn't happen, because iflen(p.PrebuildSchedules) > 0 -SchedulingTimezone should be set and valid according to validation rules intf-provider-coder. Even if this happens we should return default - so we should be safe.

But I addedDEFAULT 'UTC' as another layer of defense.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Final decision: changed to'' instead ofUTC

@spikecurtis
Copy link
Contributor

The "continuous time" interpretation of the crontab makes it, I guess, relatively simple to specify spans that start and end on the hour, but consider attempting to program a span like 7:45am - 9:10am. You'd need 3 spans

45-59 7 * * 1-5
* 8 * * 1-5
0-10 9 * * 1-5

Are we just kind of assuming that operators likely only want hourly precision?

@evgeniy-scherbina
Copy link
ContributorAuthor

The "continuous time" interpretation of the crontab makes it, I guess, relatively simple to specify spans that start and end on the hour, but consider attempting to program a span like 7:45am - 9:10am. You'd need 3 spans

45-59 7 * * 1-5* 8 * * 1-50-10 9 * * 1-5

Are we just kind of assuming that operators likely only want hourly precision?

@spikecurtis

Minutes must always be* according to our spec and validation rules.

If we allow specific minute ranges (e.g.,0-30 8-9 * * *) it will be interpreted as multiple disjoint intervals (e.g., 08:00–08:30 and 09:00–09:30), which is unintuitive and likely unexpected for operators.

Therefore, the minimum supported granularity is one hour.

There was a long discussion, and majority voted for this approach. Alternative approaches are described here:https://www.notion.so/coderhq/Implement-autoscaling-mechanism-201d579be5928054837dceb8358bfed3?source=copy_link#207d579be5928059b5ded1778841fba2

spikecurtis reacted with thumbs up emoji

Copy link
Contributor

@dannykoppingdannykopping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Excellent work@evgeniy-scherbina!
I suspectcoder/terraform-provider-coder#408 might prompt a naming change, but I'm happy to approve as-is.

RunningPrebuilds []database.GetRunningPrebuiltWorkspacesRow
PrebuildsInProgress []database.CountInProgressPrebuildsRow
Backoffs []database.GetPresetsBackoffRow
HardLimitedPresetsMap map[uuid.UUID]database.GetPresetsAtFailureLimitRow
clock quartz.Clock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't mind either approach, but let's pick one 👍

@evgeniy-scherbinaevgeniy-scherbina changed the titlefeat: implement autoscaling mechanism for prebuildsfeat: implement scheduling mechanism for prebuildsJun 18, 2025
@evgeniy-scherbinaevgeniy-scherbina merged commit0f6ca55 intomainJun 19, 2025
35 of 37 checks passed
@evgeniy-scherbinaevgeniy-scherbina deleted the yevhenii/prebuilds-autoscaling-mechanism branchJune 19, 2025 15:08
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsJun 19, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@dannykoppingdannykoppingdannykopping approved these changes

@johnstcnjohnstcnjohnstcn left review comments

@spikecurtisspikecurtisAwaiting requested review from spikecurtisspikecurtis is a code owner

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

coderd: implement autoscaling mechanism
4 participants
@evgeniy-scherbina@spikecurtis@dannykopping@johnstcn

[8]ページ先頭

©2009-2025 Movatter.jp