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

chore(redshift): retry schema does not exist errors in list schemas #208

Merged
merged 1 commit into from
Nov 1, 2024
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.17.39
github.com/aws/aws-sdk-go-v2/service/redshiftdata v1.30.1
github.com/aws/aws-sdk-go-v2/service/sts v1.32.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/databricks/databricks-sql-go v1.6.1
github.com/dlclark/regexp2 v1.11.4
github.com/gliderlabs/ssh v0.3.7
Expand Down Expand Up @@ -70,7 +71,6 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.24.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.0 // indirect
github.com/aws/smithy-go v1.22.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/continuity v0.4.3 // indirect
github.com/coreos/go-oidc/v3 v3.5.0 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
Expand Down
32 changes: 32 additions & 0 deletions sqlconnect/internal/redshift/schemaadmin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package redshift

import (
"context"
"regexp"
"time"

"github.com/cenkalti/backoff/v4"

"github.com/rudderlabs/sqlconnect-go/sqlconnect"
)

var schemaDoesNotExistRegex = regexp.MustCompile(`schema "(.*)" does not exist`)

func (db *DB) ListSchemas(ctx context.Context) ([]sqlconnect.SchemaRef, error) {
// If the list schemas query is executed while a schema is being deleted, the query will fail with a schema does not exist error.
retryableError := func(err error) bool {
return schemaDoesNotExistRegex.MatchString(err.Error())
}
return retryOperationWithData(ctx, func() ([]sqlconnect.SchemaRef, error) {
schemas, err := db.DB.ListSchemas(ctx)
if err != nil && !retryableError(err) {
return nil, backoff.Permanent(err)
}
return schemas, err
})
}

// retryOperationWithData retries the given operation with a constant backoff policy of 100ms for 10 times.
func retryOperationWithData[T any](ctx context.Context, o backoff.OperationWithData[T]) (T, error) {
return backoff.RetryWithData(o, backoff.WithMaxRetries(backoff.WithContext(backoff.NewConstantBackOff(100*time.Millisecond), ctx), 10))
}
11 changes: 11 additions & 0 deletions sqlconnect/internal/redshift/schemaadmin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package redshift

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSchemaDoesNotExistRegex(t *testing.T) {
require.True(t, schemaDoesNotExistRegex.MatchString(`ERROR: schema "tsqlcon_zvtquuiqulpz_1728651180" does not exist`))
}