-
Notifications
You must be signed in to change notification settings - Fork 874
fix: move pubsub publishing out of database transactions to avoid conn exhaustion #17648
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
Open
dannykopping
wants to merge
6
commits into
main
Choose a base branch
from
dk/notify-outside-tx
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+155
−105
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba2f90a
fix: move pubsub publishing out of database transactions to avoid con…
dannykopping 74f5dd7
chore: commentary
dannykopping f6909ff
chore: polish
dannykopping 1c84fac
chore: augment reconciler test to validate that broken publish does n…
dannykopping 5117a5e
Merge branch 'main' of github.com:/coder/coder into dk/notify-outside-tx
dannykopping 42cc3f5
chore: make lint
dannykopping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||
"github.com/coder/coder/v2/coderd/util/slice" | ||
|
@@ -303,107 +304,125 @@ func TestPrebuildReconciliation(t *testing.T) { | |
for _, prebuildLatestTransition := range tc.prebuildLatestTransitions { | ||
for _, prebuildJobStatus := range tc.prebuildJobStatuses { | ||
for _, templateDeleted := range tc.templateDeleted { | ||
t.Run(fmt.Sprintf("%s - %s - %s", tc.name, prebuildLatestTransition, prebuildJobStatus), func(t *testing.T) { | ||
t.Parallel() | ||
t.Cleanup(func() { | ||
if t.Failed() { | ||
t.Logf("failed to run test: %s", tc.name) | ||
t.Logf("templateVersionActive: %t", templateVersionActive) | ||
t.Logf("prebuildLatestTransition: %s", prebuildLatestTransition) | ||
t.Logf("prebuildJobStatus: %s", prebuildJobStatus) | ||
for _, useBrokenPubsub := range []bool{true, false} { | ||
t.Run(fmt.Sprintf("%s - %s - %s - pubsub_broken=%v", tc.name, prebuildLatestTransition, prebuildJobStatus, useBrokenPubsub), func(t *testing.T) { | ||
t.Parallel() | ||
t.Cleanup(func() { | ||
if t.Failed() { | ||
t.Logf("failed to run test: %s", tc.name) | ||
t.Logf("templateVersionActive: %t", templateVersionActive) | ||
t.Logf("prebuildLatestTransition: %s", prebuildLatestTransition) | ||
t.Logf("prebuildJobStatus: %s", prebuildJobStatus) | ||
} | ||
}) | ||
clock := quartz.NewMock(t) | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
cfg := codersdk.PrebuildsConfig{} | ||
logger := slogtest.Make( | ||
t, &slogtest.Options{IgnoreErrors: true}, | ||
).Leveled(slog.LevelDebug) | ||
db, pubSub := dbtestutil.NewDB(t) | ||
if useBrokenPubsub { | ||
pubSub = &brokenPublisher{Pubsub: pubSub} | ||
} | ||
}) | ||
clock := quartz.NewMock(t) | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
cfg := codersdk.PrebuildsConfig{} | ||
logger := slogtest.Make( | ||
t, &slogtest.Options{IgnoreErrors: true}, | ||
).Leveled(slog.LevelDebug) | ||
db, pubSub := dbtestutil.NewDB(t) | ||
controller := prebuilds.NewStoreReconciler(db, pubSub, cfg, logger, quartz.NewMock(t), prometheus.NewRegistry()) | ||
|
||
ownerID := uuid.New() | ||
dbgen.User(t, db, database.User{ | ||
ID: ownerID, | ||
}) | ||
org, template := setupTestDBTemplate(t, db, ownerID, templateDeleted) | ||
templateVersionID := setupTestDBTemplateVersion( | ||
ctx, | ||
t, | ||
clock, | ||
db, | ||
pubSub, | ||
org.ID, | ||
ownerID, | ||
template.ID, | ||
) | ||
preset := setupTestDBPreset( | ||
t, | ||
db, | ||
templateVersionID, | ||
1, | ||
uuid.New().String(), | ||
) | ||
prebuild := setupTestDBPrebuild( | ||
t, | ||
clock, | ||
db, | ||
pubSub, | ||
prebuildLatestTransition, | ||
prebuildJobStatus, | ||
org.ID, | ||
preset, | ||
template.ID, | ||
templateVersionID, | ||
) | ||
|
||
if !templateVersionActive { | ||
// Create a new template version and mark it as active | ||
// This marks the template version that we care about as inactive | ||
setupTestDBTemplateVersion(ctx, t, clock, db, pubSub, org.ID, ownerID, template.ID) | ||
} | ||
|
||
// Run the reconciliation multiple times to ensure idempotency | ||
// 8 was arbitrary, but large enough to reasonably trust the result | ||
for i := 1; i <= 8; i++ { | ||
require.NoErrorf(t, controller.ReconcileAll(ctx), "failed on iteration %d", i) | ||
|
||
if tc.shouldCreateNewPrebuild != nil { | ||
newPrebuildCount := 0 | ||
workspaces, err := db.GetWorkspacesByTemplateID(ctx, template.ID) | ||
require.NoError(t, err) | ||
for _, workspace := range workspaces { | ||
if workspace.ID != prebuild.ID { | ||
newPrebuildCount++ | ||
|
||
controller := prebuilds.NewStoreReconciler(db, pubSub, cfg, logger, quartz.NewMock(t), prometheus.NewRegistry()) | ||
|
||
ownerID := uuid.New() | ||
dbgen.User(t, db, database.User{ | ||
ID: ownerID, | ||
}) | ||
org, template := setupTestDBTemplate(t, db, ownerID, templateDeleted) | ||
templateVersionID := setupTestDBTemplateVersion( | ||
ctx, | ||
t, | ||
clock, | ||
db, | ||
pubSub, | ||
org.ID, | ||
ownerID, | ||
template.ID, | ||
) | ||
preset := setupTestDBPreset( | ||
t, | ||
db, | ||
templateVersionID, | ||
1, | ||
uuid.New().String(), | ||
) | ||
prebuild := setupTestDBPrebuild( | ||
t, | ||
clock, | ||
db, | ||
pubSub, | ||
prebuildLatestTransition, | ||
prebuildJobStatus, | ||
org.ID, | ||
preset, | ||
template.ID, | ||
templateVersionID, | ||
) | ||
|
||
if !templateVersionActive { | ||
// Create a new template version and mark it as active | ||
// This marks the template version that we care about as inactive | ||
setupTestDBTemplateVersion(ctx, t, clock, db, pubSub, org.ID, ownerID, template.ID) | ||
} | ||
|
||
// Run the reconciliation multiple times to ensure idempotency | ||
// 8 was arbitrary, but large enough to reasonably trust the result | ||
for i := 1; i <= 8; i++ { | ||
require.NoErrorf(t, controller.ReconcileAll(ctx), "failed on iteration %d", i) | ||
|
||
if tc.shouldCreateNewPrebuild != nil { | ||
newPrebuildCount := 0 | ||
workspaces, err := db.GetWorkspacesByTemplateID(ctx, template.ID) | ||
require.NoError(t, err) | ||
for _, workspace := range workspaces { | ||
if workspace.ID != prebuild.ID { | ||
newPrebuildCount++ | ||
} | ||
} | ||
// This test configures a preset that desires one prebuild. | ||
// In cases where new prebuilds should be created, there should be exactly one. | ||
require.Equal(t, *tc.shouldCreateNewPrebuild, newPrebuildCount == 1) | ||
} | ||
// This test configures a preset that desires one prebuild. | ||
// In cases where new prebuilds should be created, there should be exactly one. | ||
require.Equal(t, *tc.shouldCreateNewPrebuild, newPrebuildCount == 1) | ||
} | ||
|
||
if tc.shouldDeleteOldPrebuild != nil { | ||
builds, err := db.GetWorkspaceBuildsByWorkspaceID(ctx, database.GetWorkspaceBuildsByWorkspaceIDParams{ | ||
WorkspaceID: prebuild.ID, | ||
}) | ||
require.NoError(t, err) | ||
if *tc.shouldDeleteOldPrebuild { | ||
require.Equal(t, 2, len(builds)) | ||
require.Equal(t, database.WorkspaceTransitionDelete, builds[0].Transition) | ||
} else { | ||
require.Equal(t, 1, len(builds)) | ||
require.Equal(t, prebuildLatestTransition, builds[0].Transition) | ||
if tc.shouldDeleteOldPrebuild != nil { | ||
builds, err := db.GetWorkspaceBuildsByWorkspaceID(ctx, database.GetWorkspaceBuildsByWorkspaceIDParams{ | ||
WorkspaceID: prebuild.ID, | ||
}) | ||
require.NoError(t, err) | ||
if *tc.shouldDeleteOldPrebuild { | ||
require.Equal(t, 2, len(builds)) | ||
require.Equal(t, database.WorkspaceTransitionDelete, builds[0].Transition) | ||
} else { | ||
require.Equal(t, 1, len(builds)) | ||
require.Equal(t, prebuildLatestTransition, builds[0].Transition) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// brokenPublisher is used to validate that Publish() calls which always fail do not affect the reconciler's behavior, | ||
// since the messages published are not essential but merely advisory. | ||
type brokenPublisher struct { | ||
pubsub.Pubsub | ||
} | ||
|
||
func (*brokenPublisher) Publish(event string, _ []byte) error { | ||
// I'm explicitly _not_ checking for EventJobPosted (coderd/database/provisionerjobs/provisionerjobs.go) since that | ||
// requires too much knowledge of the underlying implementation. | ||
return xerrors.Errorf("refusing to publish %q", event) | ||
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. Should we fail slowly instead? e.g. |
||
} | ||
|
||
func TestMultiplePresetsPerTemplateVersion(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thumb-suck; I want to protect against temporary network/database blips but also don't want to accumulate too many messages.