Skip to content
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

[usage] FindRunningWorkspaceInstances: Make sure we use an index #18807

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions components/gitpod-db/go/workspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ func FindRunningWorkspaceInstances(ctx context.Context, conn *gorm.DB) ([]Worksp
var instancesInBatch []WorkspaceInstanceForUsage

tx := queryWorkspaceInstanceForUsage(ctx, conn).
Where("wsi.stoppingTime = ?", "").
Where("wsi.usageAttributionId != ?", "").
Where("wsi.phasePersisted = ?", "running").
// We are only interested in instances that have been started within the last 10 days.
Where("wsi.startedTime > ?", TimeToISO8601(time.Now().Add(-10*24*time.Hour))).
// All other selectors are there to ensure data quality
Where("wsi.stoppingTime = ?", "").
Where("wsi.usageAttributionId != ?", "").
FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error {
instances = append(instances, instancesInBatch...)
return nil
Expand Down
19 changes: 11 additions & 8 deletions components/gitpod-db/go/workspace_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,22 @@ func TestFindRunningWorkspace(t *testing.T) {
}),
// one unstopped before 10 days ago
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
WorkspaceID: workspace.ID,
StartedTime: db.NewVarCharTime(moreThan10DaysAgo),
WorkspaceID: workspace.ID,
StartedTime: db.NewVarCharTime(moreThan10DaysAgo),
PhasePersisted: "running",
}),
// Two running instances
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspace.ID,
StartedTime: db.NewVarCharTime(tenMinAgo),
ID: uuid.New(),
WorkspaceID: workspace.ID,
StartedTime: db.NewVarCharTime(tenMinAgo),
PhasePersisted: "running",
}),
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspace.ID,
StartedTime: db.NewVarCharTime(tenMinAgo),
ID: uuid.New(),
WorkspaceID: workspace.ID,
StartedTime: db.NewVarCharTime(tenMinAgo),
PhasePersisted: "running",
}),
}

Expand Down
6 changes: 3 additions & 3 deletions components/usage/pkg/apiv1/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,13 @@ func reconcileUsage(instances []db.WorkspaceInstanceForUsage, drafts []db.Usage,

instancesByID := dedupeWorkspaceInstancesForUsage(instances)

draftsByWorkspaceID := map[uuid.UUID]db.Usage{}
draftsByInstanceID := map[uuid.UUID]db.Usage{}
for _, draft := range drafts {
draftsByWorkspaceID[*draft.WorkspaceInstanceID] = draft
draftsByInstanceID[*draft.WorkspaceInstanceID] = draft
}

for instanceID, instance := range instancesByID {
if usage, exists := draftsByWorkspaceID[instanceID]; exists {
if usage, exists := draftsByInstanceID[instanceID]; exists {
updatedUsage, err := updateUsageFromInstance(instance, usage, pricer, now)
if err != nil {
return nil, nil, fmt.Errorf("failed to construct updated usage record: %w", err)
Expand Down
Loading