Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f0a4de5

Browse files
fix: use NoLock dbmem subqueries for GetWorkspaceAgentScriptTimingsByBuildID (#15405)
Closes coder/internal#202. Addresses the problem mentioned in the issue, and also makes sure subtests don't reuse `testing.T` from the main test.
1 parent 4ec6871 commit f0a4de5

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

coderd/database/dbmem/dbmem.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,19 @@ func (q *FakeQuerier) getOrganizationByIDNoLock(id uuid.UUID) (database.Organiza
11041104
return database.Organization{}, sql.ErrNoRows
11051105
}
11061106

1107+
func (q *FakeQuerier) getWorkspaceAgentScriptsByAgentIDsNoLock(ids []uuid.UUID) ([]database.WorkspaceAgentScript, error) {
1108+
scripts := make([]database.WorkspaceAgentScript, 0)
1109+
for _, script := range q.workspaceAgentScripts {
1110+
for _, id := range ids {
1111+
if script.WorkspaceAgentID == id {
1112+
scripts = append(scripts, script)
1113+
break
1114+
}
1115+
}
1116+
}
1117+
return scripts, nil
1118+
}
1119+
11071120
func (*FakeQuerier) AcquireLock(_ context.Context, _ int64) error {
11081121
return xerrors.New("AcquireLock must only be called within a transaction")
11091122
}
@@ -5854,12 +5867,12 @@ func (q *FakeQuerier) GetWorkspaceAgentScriptTimingsByBuildID(ctx context.Contex
58545867
q.mutex.RLock()
58555868
defer q.mutex.RUnlock()
58565869

5857-
build, err := q.GetWorkspaceBuildByID(ctx, id)
5870+
build, err := q.getWorkspaceBuildByIDNoLock(ctx, id)
58585871
if err != nil {
58595872
return nil, xerrors.Errorf("get build: %w", err)
58605873
}
58615874

5862-
resources, err := q.GetWorkspaceResourcesByJobID(ctx, build.JobID)
5875+
resources, err := q.getWorkspaceResourcesByJobIDNoLock(ctx, build.JobID)
58635876
if err != nil {
58645877
return nil, xerrors.Errorf("get resources: %w", err)
58655878
}
@@ -5868,7 +5881,7 @@ func (q *FakeQuerier) GetWorkspaceAgentScriptTimingsByBuildID(ctx context.Contex
58685881
resourceIDs = append(resourceIDs, res.ID)
58695882
}
58705883

5871-
agents, err := q.GetWorkspaceAgentsByResourceIDs(ctx, resourceIDs)
5884+
agents, err := q.getWorkspaceAgentsByResourceIDsNoLock(ctx, resourceIDs)
58725885
if err != nil {
58735886
return nil, xerrors.Errorf("get agents: %w", err)
58745887
}
@@ -5877,7 +5890,7 @@ func (q *FakeQuerier) GetWorkspaceAgentScriptTimingsByBuildID(ctx context.Contex
58775890
agentIDs = append(agentIDs, agent.ID)
58785891
}
58795892

5880-
scripts, err := q.GetWorkspaceAgentScriptsByAgentIDs(ctx, agentIDs)
5893+
scripts, err := q.getWorkspaceAgentScriptsByAgentIDsNoLock(agentIDs)
58815894
if err != nil {
58825895
return nil, xerrors.Errorf("get scripts: %w", err)
58835896
}
@@ -5933,16 +5946,7 @@ func (q *FakeQuerier) GetWorkspaceAgentScriptsByAgentIDs(_ context.Context, ids
59335946
q.mutex.RLock()
59345947
defer q.mutex.RUnlock()
59355948

5936-
scripts := make([]database.WorkspaceAgentScript, 0)
5937-
for _, script := range q.workspaceAgentScripts {
5938-
for _, id := range ids {
5939-
if script.WorkspaceAgentID == id {
5940-
scripts = append(scripts, script)
5941-
break
5942-
}
5943-
}
5944-
}
5945-
return scripts, nil
5949+
return q.getWorkspaceAgentScriptsByAgentIDsNoLock(ids)
59465950
}
59475951

59485952
func (q *FakeQuerier) GetWorkspaceAgentStats(_ context.Context, createdAfter time.Time) ([]database.GetWorkspaceAgentStatsRow, error) {

coderd/workspacebuilds_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
12191219

12201220
// Tests will run in parallel. To avoid conflicts and race conditions on the
12211221
// build number, each test will have its own workspace and build.
1222-
makeBuild := func() database.WorkspaceBuild {
1222+
makeBuild := func(t *testing.T) database.WorkspaceBuild {
12231223
ws := dbgen.Workspace(t, db, database.WorkspaceTable{
12241224
OwnerID: user.ID,
12251225
OrganizationID: owner.OrganizationID,
@@ -1260,7 +1260,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
12601260
t.Parallel()
12611261

12621262
// Given: a build with no timings
1263-
build := makeBuild()
1263+
build := makeBuild(t)
12641264

12651265
// When: fetching timings for the build
12661266
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
@@ -1277,7 +1277,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
12771277
t.Parallel()
12781278

12791279
// Given: a build with provisioner timings
1280-
build := makeBuild()
1280+
build := makeBuild(t)
12811281
provisionerTimings := dbgen.ProvisionerJobTimings(t, db, build, 5)
12821282

12831283
// When: fetching timings for the build
@@ -1305,7 +1305,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
13051305
t.Parallel()
13061306

13071307
// Given: a build with agent script timings
1308-
build := makeBuild()
1308+
build := makeBuild(t)
13091309
resource := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{
13101310
JobID: build.JobID,
13111311
})
@@ -1342,7 +1342,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
13421342
t.Parallel()
13431343

13441344
// Given: a build with no agent scripts
1345-
build := makeBuild()
1345+
build := makeBuild(t)
13461346
resource := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{
13471347
JobID: build.JobID,
13481348
})
@@ -1365,7 +1365,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
13651365
t.Parallel()
13661366

13671367
// Given: a build with no agents
1368-
build := makeBuild()
1368+
build := makeBuild(t)
13691369
dbgen.WorkspaceResource(t, db, database.WorkspaceResource{
13701370
JobID: build.JobID,
13711371
})
@@ -1385,7 +1385,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
13851385
t.Parallel()
13861386

13871387
// Given: a build with an agent
1388-
build := makeBuild()
1388+
build := makeBuild(t)
13891389
resource := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{
13901390
JobID: build.JobID,
13911391
})
@@ -1414,7 +1414,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
14141414
t.Parallel()
14151415

14161416
// Given: a build with multiple agents
1417-
build := makeBuild()
1417+
build := makeBuild(t)
14181418
resource := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{
14191419
JobID: build.JobID,
14201420
})

0 commit comments

Comments
 (0)