From 52ec03fc4ddb88058ff55ee8c4daf86e8d426878 Mon Sep 17 00:00:00 2001 From: Chris Stephens Date: Mon, 7 Jan 2019 16:50:48 -0800 Subject: [PATCH] Retry SQL delete calls on 409 (#1169) Merged PR #1169. --- build/terraform | 2 +- build/terraform-beta | 2 +- third_party/terraform/resources/resource_sql_database.go | 6 +++--- .../resources/resource_sql_database_instance.go.erb | 8 ++++++-- third_party/terraform/resources/resource_sql_user.go | 9 +++++++-- third_party/terraform/utils/utils.go | 3 ++- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/build/terraform b/build/terraform index 43d40f4065e5..b31626a1a248 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 43d40f4065e5bc1098f77ac10412f3a6fe2d887d +Subproject commit b31626a1a248f8a9a6e0e8a81b082da3bd219f08 diff --git a/build/terraform-beta b/build/terraform-beta index 26b13d754ab9..9f429e37f7e0 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 26b13d754ab91ecd189422e2a16b389a903bd79c +Subproject commit 9f429e37f7e0778ab404381e47ac36e6a36fbd95 diff --git a/third_party/terraform/resources/resource_sql_database.go b/third_party/terraform/resources/resource_sql_database.go index ffaf48cc301b..68f39b1e2c4c 100644 --- a/third_party/terraform/resources/resource_sql_database.go +++ b/third_party/terraform/resources/resource_sql_database.go @@ -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 { @@ -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"+ diff --git a/third_party/terraform/resources/resource_sql_database_instance.go.erb b/third_party/terraform/resources/resource_sql_database_instance.go.erb index 33f7fc928065..81689ed04b4e 100644 --- a/third_party/terraform/resources/resource_sql_database_instance.go.erb +++ b/third_party/terraform/resources/resource_sql_database_instance.go.erb @@ -14,7 +14,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])?))$" @@ -783,7 +783,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) diff --git a/third_party/terraform/resources/resource_sql_user.go b/third_party/terraform/resources/resource_sql_user.go index d11702c5fdb6..30089f517665 100644 --- a/third_party/terraform/resources/resource_sql_user.go +++ b/third_party/terraform/resources/resource_sql_user.go @@ -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 { @@ -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"+ diff --git a/third_party/terraform/utils/utils.go b/third_party/terraform/utils/utils.go index 305ee7ef86c2..2ada16b09791 100644 --- a/third_party/terraform/utils/utils.go +++ b/third_party/terraform/utils/utils.go @@ -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