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

Commitd062501

Browse files
committed
implement dbmem
1 parent434cd81 commitd062501

File tree

1 file changed

+81
-10
lines changed

1 file changed

+81
-10
lines changed

‎coderd/database/dbmem/dbmem.go‎

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,19 +1710,90 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentLogs(_ context.Context, threshold t
17101710
q.mutex.Lock()
17111711
deferq.mutex.Unlock()
17121712

1713-
varvalidLogs []database.WorkspaceAgentLog
1714-
for_,log:=rangeq.workspaceAgentLogs {
1715-
vartoBeDeletedbool
1716-
for_,agent:=rangeq.workspaceAgents {
1717-
ifagent.ID==log.AgentID&&agent.LastConnectedAt.Valid&&agent.LastConnectedAt.Time.Before(threshold) {
1718-
toBeDeleted=true
1719-
break
1720-
}
1713+
/*
1714+
WITH
1715+
latest_builds AS (
1716+
SELECT
1717+
workspace_id, max(build_number) AS max_build_number
1718+
FROM
1719+
workspace_builds
1720+
GROUP BY
1721+
workspace_id
1722+
),
1723+
*/
1724+
latestBuilds:=make(map[uuid.UUID]int32)
1725+
for_,wb:=rangeq.workspaceBuilds {
1726+
iflastBuildNumber,found:=latestBuilds[wb.WorkspaceID];found&&lastBuildNumber>wb.BuildNumber {
1727+
continue
17211728
}
1729+
// not found or newer build number
1730+
latestBuilds[wb.WorkspaceID]=wb.BuildNumber
1731+
}
17221732

1723-
if!toBeDeleted {
1724-
validLogs=append(validLogs,log)
1733+
/*
1734+
old_agents AS (
1735+
SELECT
1736+
wa.id
1737+
FROM
1738+
workspace_agents AS wa
1739+
JOIN
1740+
workspace_resources AS wr
1741+
ON
1742+
wa.resource_id = wr.id
1743+
JOIN
1744+
workspace_builds AS wb
1745+
ON
1746+
wb.job_id = wr.job_id
1747+
LEFT JOIN
1748+
latest_builds
1749+
ON
1750+
latest_builds.workspace_id = wb.workspace_id
1751+
AND
1752+
latest_builds.max_build_number = wb.build_number
1753+
WHERE
1754+
-- Filter out the latest builds for each workspace.
1755+
latest_builds.workspace_id IS NULL
1756+
AND CASE
1757+
-- If the last time the agent connected was before @threshold
1758+
WHEN wa.last_connected_at IS NOT NULL THEN
1759+
wa.last_connected_at < @threshold :: timestamptz
1760+
-- The agent never connected, and was created before @threshold
1761+
ELSE wa.created_at < @threshold :: timestamptz
1762+
END
1763+
)
1764+
*/
1765+
oldAgents:=make(map[uuid.UUID]struct{})
1766+
for_,wa:=rangeq.workspaceAgents {
1767+
for_,wr:=rangeq.workspaceResources {
1768+
ifwr.ID!=wa.ResourceID {
1769+
continue
1770+
}
1771+
for_,wb:=rangeq.workspaceBuilds {
1772+
ifwb.JobID!=wr.JobID {
1773+
continue
1774+
}
1775+
latestBuildNumber,found:=latestBuilds[wb.WorkspaceID]
1776+
if!found {
1777+
panic("workspaceBuilds got modified somehow while q was locked! This is a bug in dbmem!")
1778+
}
1779+
iflatestBuildNumber==wb.BuildNumber {
1780+
continue
1781+
}
1782+
ifwa.LastConnectedAt.Valid&&wa.LastConnectedAt.Time.Before(threshold)||wa.CreatedAt.Before(threshold) {
1783+
oldAgents[wa.ID]=struct{}{}
1784+
}
1785+
}
1786+
}
1787+
}
1788+
/*
1789+
DELETE FROM workspace_agent_logs WHERE agent_id IN (SELECT id FROM old_agents);
1790+
*/
1791+
varvalidLogs []database.WorkspaceAgentLog
1792+
for_,log:=rangeq.workspaceAgentLogs {
1793+
if_,found:=oldAgents[log.AgentID];found {
1794+
continue
17251795
}
1796+
validLogs=append(validLogs,log)
17261797
}
17271798
q.workspaceAgentLogs=validLogs
17281799
returnnil

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp