Skip to content

fix: Prevent autobuild executor from slowing down API requests #3726

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 4 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: Refactor autostart check, do it in Go
  • Loading branch information
mafredri committed Sep 2, 2022
commit 45e4141052ce78272916eebac09a2952e8ab7f73
38 changes: 23 additions & 15 deletions coderd/autobuild/executor/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package executor

import (
"context"
"database/sql"
"encoding/json"
"time"

Expand Down Expand Up @@ -101,36 +100,41 @@ func (e *Executor) runOnce(t time.Time) Stats {
// NOTE: If a workspace build is created with a given TTL and then the user either
// changes or unsets the TTL, the deadline for the workspace build will not
// have changed. This behavior is as expected per #2229.
eligibleWorkspaces, err := e.db.GetWorkspacesAutostart(e.ctx)
workspaces, err := e.db.GetWorkspaces(e.ctx, database.GetWorkspacesParams{
Deleted: false,
})
if err != nil {
e.log.Error(e.ctx, "get eligible workspaces for autostart or autostop", slog.Error(err))
e.log.Error(e.ctx, "get workspaces for autostart or autostop", slog.Error(err))
return stats
}

var eligibleWorkspaceIDs []uuid.UUID
for _, ws := range workspaces {
if isEligibleForAutoStartStop(ws) {
eligibleWorkspaceIDs = append(eligibleWorkspaceIDs, ws.ID)
}
}

// We only use errgroup here for convenience of API, not for early
// cancellation. This means we only return nil errors in th eg.Go.
eg := errgroup.Group{}
// Limit the concurrency to avoid overloading the database.
eg.SetLimit(10)

for _, ws := range eligibleWorkspaces {
ws := ws
log := e.log.With(slog.F("workspace_id", ws.ID))
for _, wsID := range eligibleWorkspaceIDs {
wsID := wsID
log := e.log.With(slog.F("workspace_id", wsID))

eg.Go(func() error {
err := e.db.InTx(func(db database.Store) error {
var err error

// Re-check eligibility since the first check was outside the
// transaction and the workspace settings may have changed.
ws, err = db.GetWorkspaceAutostart(e.ctx, ws.ID)
ws, err := db.GetWorkspaceByID(e.ctx, wsID)
if err != nil {
// Receiving ErrNoRows means the workspace settings changed
// and it is no longer eligible for autostart. Other errors
// means something went wrong.
if !xerrors.Is(err, sql.ErrNoRows) {
log.Error(e.ctx, "get workspace autostart failed", slog.Error(err))
}
log.Error(e.ctx, "get workspace autostart failed", slog.Error(err))
return nil
}
if !isEligibleForAutoStartStop(ws) {
return nil
}

Expand Down Expand Up @@ -191,6 +195,10 @@ func (e *Executor) runOnce(t time.Time) Stats {
return stats
}

func isEligibleForAutoStartStop(ws database.Workspace) bool {
return ws.AutostartSchedule.String != "" || ws.Ttl.Int64 > 0
}

func getNextTransition(
ws database.Workspace,
priorHistory database.WorkspaceBuild,
Expand Down
27 changes: 0 additions & 27 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,33 +588,6 @@ func (q *fakeQuerier) GetWorkspaceAppsByAgentIDs(_ context.Context, ids []uuid.U
return apps, nil
}

func (q *fakeQuerier) GetWorkspaceAutostart(_ context.Context, workspaceID uuid.UUID) (database.Workspace, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
for _, ws := range q.workspaces {
if ws.ID == workspaceID {
if ws.AutostartSchedule.String != "" || ws.Ttl.Valid {
return ws, nil
}
}
}
return database.Workspace{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetWorkspacesAutostart(_ context.Context) ([]database.Workspace, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
workspaces := make([]database.Workspace, 0)
for _, ws := range q.workspaces {
if ws.AutostartSchedule.String != "" {
workspaces = append(workspaces, ws)
} else if ws.Ttl.Valid {
workspaces = append(workspaces, ws)
}
}
return workspaces, nil
}

func (q *fakeQuerier) GetWorkspaceOwnerCountsByTemplateIDs(_ context.Context, templateIDs []uuid.UUID) ([]database.GetWorkspaceOwnerCountsByTemplateIDsRow, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
2 changes: 0 additions & 2 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 0 additions & 86 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,6 @@ WHERE
END
;

-- name: GetWorkspaceAutostart :one
SELECT
*
FROM
workspaces
WHERE
deleted = false
AND
(
id = @workspace_id
AND (
(autostart_schedule IS NOT NULL AND autostart_schedule <> '')
OR
(ttl IS NOT NULL AND ttl > 0)
)
);

-- name: GetWorkspacesAutostart :many
SELECT
*
FROM
workspaces
WHERE
deleted = false
AND
(
(autostart_schedule IS NOT NULL AND autostart_schedule <> '')
OR
(ttl IS NOT NULL AND ttl > 0)
);

-- name: GetWorkspaceByOwnerIDAndName :one
SELECT
*
Expand Down