Skip to content

chore: improve build deadline code #19203

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 7, 2025
Merged

Conversation

deansheather
Copy link
Member

  • Adds/improves a lot of comments to make the autostop calculation code clearer
  • Changes the behavior of the enterprise template schedule store to match the behavior of the workspace TTL endpoint when the new TTL is zero
  • Fixes a bug in the workspace TTL endpoint where it could unset the build deadline, even though a max_deadline was specified
  • Adds a new constraint to the workspace_builds table that enforces the deadline is non-zero and below the max_deadline if it is set
  • Adds CHECK constraint enum generation to scripts/dbgen, used for testing the above constraint
  • Adds Dean and Danielle as CODEOWNERS for the autostop calculation code

- Adds/improves a lot of comments to make the autostop calculation code
  clearer
- Changes the behavior of the enterprise template schedule store to
  match the behavior of the workspace TTL endpoint when the new TTL is
  zero
- Fixes a bug in the workspace TTL endpoint where it could unset the
  build deadline, even though a max_deadline was specified
- Adds a new constraint to the workspace_builds table that enforces the
  deadline is non-zero and below the max_deadline if it is set
- Adds CHECK constraint enum generation to scripts/dbgen, used for
  testing the above constraint
- Adds Dean and Danielle as CODEOWNERS for the autostop calculation code
@@ -0,0 +1,239 @@
package main
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved all the constraint stuff to it's own file because it's quite large.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also refactored it into a reusable function rather than pasting the same code three times. I'll admit it's not the prettiest but it's probably good enough.

Copy link
Contributor

@DanielleMaywood DanielleMaywood left a comment

Choose a reason for hiding this comment

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

Looks good to me :shipit:

Copy link
Contributor

@ssncferreira ssncferreira left a comment

Choose a reason for hiding this comment

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

Overall LGTM, just a couple of nits, suggestions, and questions.
Thanks for adding the extra comments, they’re super helpful! 🙌

Comment on lines +95 to +105
// Deadline is intended as a cost saving measure, not as a hard policy. It is
// derived from either the workspace's TTL or the template's TTL, depending on
// the template's policy, to ensure workspaces are stopped when they are idle.
//
// MaxDeadline is intended as a compliance policy. It is derived from the
// template's autostop requirement to cap workspace uptime and effectively force
// people to update often.
//
// Note that only the build's CURRENT deadline property influences automation in
// the autobuild package. As stated above, the MaxDeadline property is only used
// to cap the value of a build's deadline.
Copy link
Contributor

Choose a reason for hiding this comment

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

🤩 really nice comment, thanks!

Comment on lines 1194 to 1197
// If the build has a max_deadline set, we still need to
// abide by it. It will either be zero (our target), or a
// value.
Deadline: build.MaxDeadline,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I follow the reasoning behind this change 🤔
According to the above comment:

// If autostop has been disabled, we want to remove the deadline from the
// existing workspace build (if there is one).
if !dbTTL.Valid {

So, shouldn't deadline be time.Time{},?

Copy link
Contributor

@ssncferreira ssncferreira Aug 6, 2025

Choose a reason for hiding this comment

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

Actually, if I understood correctly, Deadline is derived from TTL and MaxDeadline is derived from Autostop. In that case, when autostop is disabled, shouldn’t just MaxDeadline be unset? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

This code is correct, but I'll update the comment to make it clearer.

					// Use the max_deadline as the new build deadline. It will
					// either be zero (our target), or a non-zero value that we
					// need to abide by anyway due to template policy.
					//
					// Previously, we would always set the deadline to zero,
					// which was incorrect behavior. When max_deadline is
					// non-zero, deadline must be set to a non-zero value that
					// is less than max_deadline.
					//
					// Disabling TTL autostop (at a workspace or template level)
					// does not trump the template's autostop requirement.
					//
					// Refer to the comments on schedule.CalculateAutostop for
					// more information.

Copy link
Member Author

Choose a reason for hiding this comment

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

A new build with ttl: 0, autostop_requirement: enabled will end up with the deadlines of deadline: required_autostop_time, max_deadline: required_autostop_time (e.g. equal deadline and max_deadline).

So this matches that behavior.

@deansheather deansheather merged commit dc59885 into main Aug 7, 2025
31 checks passed
@deansheather deansheather deleted the dean/deadline-improvements branch August 7, 2025 01:00
@github-actions github-actions bot locked and limited conversation to collaborators Aug 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants