- Notifications
You must be signed in to change notification settings - Fork1k
feat(coderd/database/dbpurge): retain most recent agent build logs#14460
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
9451962
0c14a3e
0af051e
dd97358
2017434
725eeb0
591dd91
0f8ab25
5c77b5e
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1710,19 +1710,90 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentLogs(_ context.Context, threshold t | ||
q.mutex.Lock() | ||
defer q.mutex.Unlock() | ||
/* | ||
WITH | ||
latest_builds AS ( | ||
SELECT | ||
workspace_id, max(build_number) AS max_build_number | ||
FROM | ||
workspace_builds | ||
GROUP BY | ||
workspace_id | ||
), | ||
*/ | ||
latestBuilds := make(map[uuid.UUID]int32) | ||
for _, wb := range q.workspaceBuilds { | ||
if lastBuildNumber, found := latestBuilds[wb.WorkspaceID]; found && lastBuildNumber > wb.BuildNumber { | ||
continue | ||
} | ||
// not found or newer build number | ||
latestBuilds[wb.WorkspaceID] = wb.BuildNumber | ||
} | ||
/* | ||
old_agents AS ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is really nice! I'm concerned it'll go stale... | ||
SELECT | ||
wa.id | ||
FROM | ||
workspace_agents AS wa | ||
JOIN | ||
workspace_resources AS wr | ||
ON | ||
wa.resource_id = wr.id | ||
JOIN | ||
workspace_builds AS wb | ||
ON | ||
wb.job_id = wr.job_id | ||
LEFT JOIN | ||
latest_builds | ||
ON | ||
latest_builds.workspace_id = wb.workspace_id | ||
AND | ||
latest_builds.max_build_number = wb.build_number | ||
WHERE | ||
-- Filter out the latest builds for each workspace. | ||
latest_builds.workspace_id IS NULL | ||
AND CASE | ||
-- If the last time the agent connected was before @threshold | ||
WHEN wa.last_connected_at IS NOT NULL THEN | ||
wa.last_connected_at < @threshold :: timestamptz | ||
-- The agent never connected, and was created before @threshold | ||
ELSE wa.created_at < @threshold :: timestamptz | ||
END | ||
) | ||
*/ | ||
oldAgents := make(map[uuid.UUID]struct{}) | ||
for _, wa := range q.workspaceAgents { | ||
for _, wr := range q.workspaceResources { | ||
if wr.ID != wa.ResourceID { | ||
continue | ||
} | ||
for _, wb := range q.workspaceBuilds { | ||
if wb.JobID != wr.JobID { | ||
continue | ||
} | ||
latestBuildNumber, found := latestBuilds[wb.WorkspaceID] | ||
if !found { | ||
panic("workspaceBuilds got modified somehow while q was locked! This is a bug in dbmem!") | ||
} | ||
if latestBuildNumber == wb.BuildNumber { | ||
continue | ||
} | ||
if wa.LastConnectedAt.Valid && wa.LastConnectedAt.Time.Before(threshold) || wa.CreatedAt.Before(threshold) { | ||
oldAgents[wa.ID] = struct{}{} | ||
} | ||
} | ||
} | ||
} | ||
/* | ||
DELETE FROM workspace_agent_logs WHERE agent_id IN (SELECT id FROM old_agents); | ||
*/ | ||
var validLogs []database.WorkspaceAgentLog | ||
for _, log := range q.workspaceAgentLogs { | ||
if _, found := oldAgents[log.AgentID]; found { | ||
continue | ||
} | ||
validLogs = append(validLogs, log) | ||
} | ||
q.workspaceAgentLogs = validLogs | ||
return nil | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -11,6 +11,7 @@ import ( | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||
"github.com/coder/quartz" | ||
) | ||
@@ -30,7 +31,8 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz. | ||
//nolint:gocritic // The system purges old db records without user input. | ||
ctx = dbauthz.AsSystemRestricted(ctx) | ||
// Start the ticker with the initial delay. | ||
ticker := clk.NewTicker(delay) | ||
Comment on lines +34 to +35 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Review: I found this to be the source of a race condition while testing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What was the race condition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It may actually be the same race condition you mentioned below, just moved around slightly. | ||
doTick := func(start time.Time) { | ||
defer ticker.Reset(delay) | ||
// Start a transaction to grab advisory lock, we don't want to run | ||
@@ -47,7 +49,8 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz. | ||
return nil | ||
} | ||
deleteOldWorkspaceAgentLogsBefore := start.Add(-maxAgentLogAge) | ||
if err := tx.DeleteOldWorkspaceAgentLogs(ctx, deleteOldWorkspaceAgentLogsBefore); err != nil { | ||
return xerrors.Errorf("failed to delete old workspace agent logs: %w", err) | ||
} | ||
if err := tx.DeleteOldWorkspaceAgentStats(ctx); err != nil { | ||
@@ -72,13 +75,15 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz. | ||
go func() { | ||
defer close(closed) | ||
defer ticker.Stop() | ||
// Force an initial tick. | ||
doTick(dbtime.Time(clk.Now()).UTC()) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case tick := <-ticker.C: | ||
ticker.Stop() | ||
doTick(dbtime.Time(tick).UTC()) | ||
} | ||
} | ||
}() | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.