Skip to content

[usage] Batch lookup Workspaces to fix too many placeholders error #10758

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

Merged
merged 1 commit into from
Jun 21, 2022
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
2 changes: 1 addition & 1 deletion components/usage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
github.com/stripe/stripe-go/v72 v72.114.0
gorm.io/datatypes v1.0.6
gorm.io/driver/mysql v1.3.3
gorm.io/gorm v1.23.5
Expand All @@ -88,7 +89,6 @@ require (
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stripe/stripe-go/v72 v72.114.0 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
Expand Down
4 changes: 2 additions & 2 deletions components/usage/pkg/db/dbtest/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
func NewWorkspace(t *testing.T, workspace db.Workspace) db.Workspace {
t.Helper()

id := generateWorkspaceID()
id := GenerateWorkspaceID()
if workspace.ID != "" {
id = workspace.ID
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func NewWorkspace(t *testing.T, workspace db.Workspace) db.Workspace {
}
}

func generateWorkspaceID() string {
func GenerateWorkspaceID() string {
return fmt.Sprintf("gitpodio-gitpod-%s", randSeq(11))
}

Expand Down
25 changes: 22 additions & 3 deletions components/usage/pkg/db/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/google/uuid"
"gorm.io/datatypes"
"gorm.io/gorm"
"math"
"time"
)

Expand Down Expand Up @@ -59,15 +60,33 @@ const (
WorkspaceType_Regular WorkspaceType = "regular"
)

const maxListBatchSize = 65535 // 2^16 - 1

func ListWorkspacesByID(ctx context.Context, conn *gorm.DB, ids []string) ([]Workspace, error) {
if len(ids) == 0 {
return nil, nil
}

var workspaces []Workspace
tx := conn.WithContext(ctx).Where(ids).Find(&workspaces)
if tx.Error != nil {
return nil, fmt.Errorf("failed to list workspaces by id: %w", tx.Error)

items := len(ids)
batches := int(math.Ceil(float64(items) / maxListBatchSize))
for i := 0; i < batches; i++ {
lower := i * maxListBatchSize
upper := (i + 1) * maxListBatchSize

if upper > items {
upper = items
}
Comment on lines +78 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own curiosity: In Go, list[lower:upper] is already safe against out-of-bounds, right? E.g. list[0:10] on a list with 4 items would return a list with 4 items without errors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In go, the upper bound from a slice is not safe for out of bounds, it panics https://goplay.tools/snippet/SgSiwcAPfXC


batchIDs := ids[lower:upper]
var results []Workspace
tx := conn.WithContext(ctx).Where(batchIDs).Find(&results)
if tx.Error != nil {
return nil, fmt.Errorf("failed to list workspaces by id: %w", tx.Error)
}

workspaces = append(workspaces, results...)
}

return workspaces, nil
Expand Down
14 changes: 14 additions & 0 deletions components/usage/pkg/db/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func TestListWorkspacesByID(t *testing.T) {
QueryIDs: []string{workspaces[0].ID, workspaces[1].ID},
Expected: 2,
},
{
Name: "over 2^16 - 1 IDs requires batching",
QueryIDs: append([]string{workspaces[0].ID, workspaces[1].ID}, generateWorkspaceIDs(65535)...),
Expected: 2,
},
} {
t.Run(scenario.Name, func(t *testing.T) {
results, err := db.ListWorkspacesByID(context.Background(), conn, scenario.QueryIDs)
Expand All @@ -135,3 +140,12 @@ func TestListWorkspacesByID(t *testing.T) {

}
}

func generateWorkspaceIDs(count int) []string {
var ids []string
for i := 0; i < count; i++ {
ids = append(ids, dbtest.GenerateWorkspaceID())
}

return ids
}