Skip to content

Commit 3ea6865

Browse files
committed
don't cache prebuilds
Signed-off-by: Callum Styan <callumstyan@gmail.com>
1 parent e99a8c5 commit 3ea6865

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

coderd/agentapi/api.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package agentapi
22

33
import (
44
"context"
5-
"database/sql"
65
"io"
76
"net"
87
"net/url"
@@ -61,10 +60,6 @@ type CachedWorkspaceFields struct {
6160
Name string
6261
OwnerUsername string
6362
TemplateName string
64-
65-
// Lifecycle fields needed for stats reporting
66-
AutostartSchedule sql.NullString
67-
DormantAt sql.NullTime
6863
}
6964

7065
// API implements the DRPC agent API interface from agent/proto. This struct is
@@ -132,23 +127,6 @@ func New(opts Options, workspace database.Workspace) *API {
132127

133128
api := &API{
134129
opts: opts,
135-
cachedWorkspaceFields: CachedWorkspaceFields{
136-
ID: workspace.ID,
137-
OwnerID: workspace.OwnerID,
138-
OrganizationID: workspace.OrganizationID,
139-
TemplateID: workspace.TemplateID,
140-
Name: workspace.Name,
141-
OwnerUsername: workspace.OwnerUsername,
142-
TemplateName: workspace.TemplateName,
143-
AutostartSchedule: sql.NullString{
144-
String: workspace.AutostartSchedule.String,
145-
Valid: workspace.AutostartSchedule.Valid,
146-
},
147-
DormantAt: sql.NullTime{
148-
Time: workspace.DormantAt.Time,
149-
Valid: workspace.DormantAt.Valid,
150-
},
151-
},
152130
mu: sync.Mutex{},
153131
}
154132

@@ -164,6 +142,20 @@ func New(opts Options, workspace database.Workspace) *API {
164142
WorkspaceID: opts.WorkspaceID,
165143
}
166144

145+
// Don't cache details for prebuilds, though the cached fields will eventually be updated
146+
// by the refresh routine once the prebuild workspace is claimed.
147+
if !workspace.IsPrebuild() {
148+
api.cachedWorkspaceFields = CachedWorkspaceFields{
149+
ID: workspace.ID,
150+
OwnerID: workspace.OwnerID,
151+
OrganizationID: workspace.OrganizationID,
152+
TemplateID: workspace.TemplateID,
153+
Name: workspace.Name,
154+
OwnerUsername: workspace.OwnerUsername,
155+
TemplateName: workspace.TemplateName,
156+
}
157+
}
158+
167159
api.AnnouncementBannerAPI = &AnnouncementBannerAPI{
168160
appearanceFetcher: opts.AppearanceFetcher,
169161
}
@@ -322,8 +314,6 @@ func (a *API) workspace() database.Workspace {
322314
Name: a.cachedWorkspaceFields.Name,
323315
OwnerUsername: a.cachedWorkspaceFields.OwnerUsername,
324316
TemplateName: a.cachedWorkspaceFields.TemplateName,
325-
AutostartSchedule: a.cachedWorkspaceFields.AutostartSchedule,
326-
DormantAt: a.cachedWorkspaceFields.DormantAt,
327317
}
328318
}
329319

@@ -345,12 +335,16 @@ func (a *API) refreshCachedWorkspace(ctx context.Context) {
345335
return
346336
}
347337

338+
if ws.IsPrebuild() {
339+
a.opts.Log.Debug(ctx, "workspace is a prebuild, not caching in AgentAPI")
340+
return
341+
}
342+
348343
// Update fields that can change during workspace lifecycle (e.g., prebuild claim)
349-
a.cachedWorkspaceFields.OwnerID = ws.OwnerID
350-
a.cachedWorkspaceFields.Name = ws.Name
351-
a.cachedWorkspaceFields.OwnerUsername = ws.OwnerUsername
352-
a.cachedWorkspaceFields.AutostartSchedule = ws.AutostartSchedule
353-
a.cachedWorkspaceFields.DormantAt = ws.DormantAt
344+
a.mu.Lock()
345+
a.cachedWorkspaceFields = CachedWorkspaceFields{}
346+
a.mu.Unlock()
347+
354348

355349
a.opts.Log.Debug(ctx, "refreshed cached workspace fields",
356350
slog.F("workspace_id", ws.ID),

coderd/agentapi/stats.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"golang.org/x/xerrors"
8+
"github.com/google/uuid"
89
"google.golang.org/protobuf/types/known/durationpb"
910

1011
"cdr.dev/slog"
@@ -50,6 +51,15 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
5051
// Construct workspace from cached fields to avoid DB query
5152
workspace := a.WorkspaceFn()
5253

54+
// If cache is empty (prebuild or invalid), fall back to DB
55+
if workspace.ID == uuid.Nil {
56+
ws, err := a.Database.GetWorkspaceByAgentID(ctx, workspaceAgent.ID)
57+
if err != nil {
58+
return nil, xerrors.Errorf("get workspace by agent ID %q: %w", workspaceAgent.ID, err)
59+
}
60+
workspace = ws
61+
}
62+
5363
a.Log.Debug(ctx, "read stats report",
5464
slog.F("interval", a.AgentStatsRefreshInterval),
5565
slog.F("workspace_id", workspace.ID),

0 commit comments

Comments
 (0)