Skip to content

Commit 3074547

Browse files
authored
perf(enterprise): remove expensive GetWorkspaces query from entitlements (#19747)
Closes: coder/internal#964 This PR addresses the significant database load issue where the `GetWorkspaces` query was causing performance problems in the license entitlements code.
1 parent d527f91 commit 3074547

File tree

2 files changed

+8
-49
lines changed

2 files changed

+8
-49
lines changed

enterprise/coderd/license/license.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,6 @@ func Entitlements(
9696
return codersdk.Entitlements{}, xerrors.Errorf("query active user count: %w", err)
9797
}
9898

99-
// nolint:gocritic // Getting external workspaces is a system function.
100-
externalWorkspaces, err := db.GetWorkspaces(dbauthz.AsSystemRestricted(ctx), database.GetWorkspacesParams{
101-
HasExternalAgent: sql.NullBool{
102-
Bool: true,
103-
Valid: true,
104-
},
105-
})
106-
if err != nil {
107-
return codersdk.Entitlements{}, xerrors.Errorf("query external workspaces: %w", err)
108-
}
109-
11099
// nolint:gocritic // Getting external templates is a system function.
111100
externalTemplates, err := db.GetTemplatesWithFilter(dbauthz.AsSystemRestricted(ctx), database.GetTemplatesWithFilterParams{
112101
HasExternalAgent: sql.NullBool{
@@ -119,11 +108,10 @@ func Entitlements(
119108
}
120109

121110
entitlements, err := LicensesEntitlements(ctx, now, licenses, enablements, keys, FeatureArguments{
122-
ActiveUserCount: activeUserCount,
123-
ReplicaCount: replicaCount,
124-
ExternalAuthCount: externalAuthCount,
125-
ExternalWorkspaceCount: int64(len(externalWorkspaces)),
126-
ExternalTemplateCount: int64(len(externalTemplates)),
111+
ActiveUserCount: activeUserCount,
112+
ReplicaCount: replicaCount,
113+
ExternalAuthCount: externalAuthCount,
114+
ExternalTemplateCount: int64(len(externalTemplates)),
127115
ManagedAgentCountFn: func(ctx context.Context, startTime time.Time, endTime time.Time) (int64, error) {
128116
// This is not super accurate, as the start and end times will be
129117
// truncated to the date in UTC timezone. This is an optimization
@@ -149,11 +137,10 @@ func Entitlements(
149137
}
150138

151139
type FeatureArguments struct {
152-
ActiveUserCount int64
153-
ReplicaCount int
154-
ExternalAuthCount int
155-
ExternalWorkspaceCount int64
156-
ExternalTemplateCount int64
140+
ActiveUserCount int64
141+
ReplicaCount int
142+
ExternalAuthCount int
143+
ExternalTemplateCount int64
157144
// Unfortunately, managed agent count is not a simple count of the current
158145
// state of the world, but a count between two points in time determined by
159146
// the licenses.
@@ -477,18 +464,6 @@ func LicensesEntitlements(
477464
}
478465
}
479466

480-
if featureArguments.ExternalWorkspaceCount > 0 {
481-
feature := entitlements.Features[codersdk.FeatureWorkspaceExternalAgent]
482-
switch feature.Entitlement {
483-
case codersdk.EntitlementNotEntitled:
484-
entitlements.Errors = append(entitlements.Errors,
485-
"You have external workspaces but your license is not entitled to this feature.")
486-
case codersdk.EntitlementGracePeriod:
487-
entitlements.Warnings = append(entitlements.Warnings,
488-
"You have external workspaces but your license is expired.")
489-
}
490-
}
491-
492467
if featureArguments.ExternalTemplateCount > 0 {
493468
feature := entitlements.Features[codersdk.FeatureWorkspaceExternalAgent]
494469
switch feature.Entitlement {

enterprise/coderd/license/license_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,6 @@ func TestEntitlements(t *testing.T) {
843843
return true
844844
})).
845845
Return(int64(175), nil)
846-
mDB.EXPECT().
847-
GetWorkspaces(gomock.Any(), gomock.Any()).
848-
Return([]database.GetWorkspacesRow{}, nil)
849846
mDB.EXPECT().
850847
GetTemplatesWithFilter(gomock.Any(), gomock.Any()).
851848
Return([]database.Template{}, nil)
@@ -1236,19 +1233,6 @@ func TestLicenseEntitlements(t *testing.T) {
12361233
assert.Equal(t, int64(200), *feature.Actual)
12371234
},
12381235
},
1239-
{
1240-
Name: "ExternalWorkspace",
1241-
Licenses: []*coderdenttest.LicenseOptions{
1242-
enterpriseLicense().UserLimit(100),
1243-
},
1244-
Arguments: license.FeatureArguments{
1245-
ExternalWorkspaceCount: 1,
1246-
},
1247-
AssertEntitlements: func(t *testing.T, entitlements codersdk.Entitlements) {
1248-
assert.Equal(t, codersdk.EntitlementEntitled, entitlements.Features[codersdk.FeatureWorkspaceExternalAgent].Entitlement)
1249-
assert.True(t, entitlements.Features[codersdk.FeatureWorkspaceExternalAgent].Enabled)
1250-
},
1251-
},
12521236
{
12531237
Name: "ExternalTemplate",
12541238
Licenses: []*coderdenttest.LicenseOptions{

0 commit comments

Comments
 (0)