Skip to content

Commit

Permalink
Add retry for cockroachdb init to avoid a concurrency issue in Cockro…
Browse files Browse the repository at this point in the history
…achDB, specifically a TransactionRetryError.
  • Loading branch information
go-jet committed Oct 29, 2024
1 parent 49104d1 commit aaf705d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion tests/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ func initPostgresDB(dbType string, connectionString string) error {
for _, schemaName := range schemaNames {
fmt.Println("\nInitializing", schemaName, "schema...")

err = execFile(db, fmt.Sprintf("./testdata/init/%s/%s.sql", dbType, schemaName))
// retry add due to a concurrency issue in CockroachDB, specifically a TransactionRetryError
err = retry(3, func() error {
return execFile(db, fmt.Sprintf("./testdata/init/%s/%s.sql", dbType, schemaName))
})
if err != nil {
return fmt.Errorf("failed to execute sql file: %w", err)
}
Expand All @@ -183,6 +186,23 @@ func initPostgresDB(dbType string, connectionString string) error {
return nil
}

func retry(count int, f func() error) error {

for i := 0; i < count; i++ {
err := f()

if err == nil {
break
}

if i == count-1 {
return err
}
}

return nil
}

func execFile(db *sql.DB, sqlFilePath string) error {
testSampleSql, err := os.ReadFile(sqlFilePath) // #nosec G304
if err != nil {
Expand Down

0 comments on commit aaf705d

Please sign in to comment.