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

Retry SQL delete calls on 409 #2811

Merged
merged 1 commit into from
Jan 8, 2019
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: 3 additions & 3 deletions google/resource_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"

"google.golang.org/api/sqladmin/v1beta4"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

func resourceSqlDatabase() *schema.Resource {
Expand Down Expand Up @@ -206,10 +206,10 @@ func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
defer mutexKV.Unlock(instanceMutexKey(project, instance_name))

var op *sqladmin.Operation
err = retryTime(func() error {
err = retryTimeDuration(func() error {
op, err = config.clientSqlAdmin.Databases.Delete(project, instance_name, database_name).Do()
return err
}, 5 /* minutes */)
}, d.Timeout(schema.TimeoutDelete))

if err != nil {
return fmt.Errorf("Error, failed to delete"+
Expand Down
8 changes: 6 additions & 2 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform/helper/validation"

"google.golang.org/api/googleapi"
"google.golang.org/api/sqladmin/v1beta4"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

const privateNetworkLinkRegex = "projects/(" + ProjectRegex + ")/global/networks/((?:[a-z](?:[-a-z0-9]*[a-z0-9])?))$"
Expand Down Expand Up @@ -782,7 +782,11 @@ func resourceSqlDatabaseInstanceDelete(d *schema.ResourceData, meta interface{})
defer mutexKV.Unlock(instanceMutexKey(project, v.(string)))
}

op, err := config.clientSqlAdmin.Instances.Delete(project, d.Get("name").(string)).Do()
var op *sqladmin.Operation
err = retryTimeDuration(func() error {
op, err = config.clientSqlAdmin.Instances.Delete(project, d.Get("name").(string)).Do()
return err
}, d.Timeout(schema.TimeoutDelete))

if err != nil {
return fmt.Errorf("Error, failed to delete instance %s: %s", d.Get("name").(string), err)
Expand Down
9 changes: 7 additions & 2 deletions google/resource_sql_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/sqladmin/v1beta4"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

func resourceSqlUser() *schema.Resource {
Expand Down Expand Up @@ -206,7 +206,12 @@ func resourceSqlUserDelete(d *schema.ResourceData, meta interface{}) error {

mutexKV.Lock(instanceMutexKey(project, instance))
defer mutexKV.Unlock(instanceMutexKey(project, instance))
op, err := config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do()

var op *sqladmin.Operation
err = retryTimeDuration(func() error {
op, err = config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do()
return err
}, d.Timeout(schema.TimeoutDelete))

if err != nil {
return fmt.Errorf("Error, failed to delete"+
Expand Down
3 changes: 2 additions & 1 deletion google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration) error {
}

func isRetryableError(err error) bool {
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
// 409's are retried because cloud sql throws a 409 when concurrent calls are made
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Code == 409 || gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
return true
}
return false
Expand Down