Skip to content

Commit

Permalink
Add retries to datastore index when under contention. (#3615) (#2154)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jun 10, 2020
1 parent 953ef47 commit 4e268e1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/3615.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
datastore: Added retries to `google_datastore_index` requests when under contention.
```
2 changes: 1 addition & 1 deletion google-beta/datastore_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (w *DatastoreOperationWaiter) QueryOp() (interface{}, error) {
}
// Returns the proper get.
url := fmt.Sprintf("https://datastore.googleapis.com/v1/%s", w.CommonOperationWaiter.Op.Name)
return sendRequest(w.Config, "GET", w.Project, url, nil)
return sendRequest(w.Config, "GET", w.Project, url, nil, datastoreIndex409Contention)
}

func createDatastoreWaiter(config *Config, op map[string]interface{}, project, activity string) (*DatastoreOperationWaiter, error) {
Expand Down
9 changes: 9 additions & 0 deletions google-beta/error_retry_predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,12 @@ func isCloudFunctionsSourceCodeError(err error) (bool, string) {
}
return false, ""
}

func datastoreIndex409Contention(err error) (bool, string) {
if gerr, ok := err.(*googleapi.Error); ok {
if gerr.Code == 409 && strings.Contains(gerr.Body, "too much contention") {
return true, "too much contention - waiting for less activity"
}
}
return false, ""
}
6 changes: 3 additions & 3 deletions google-beta/resource_datastore_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func resourceDatastoreIndexCreate(d *schema.ResourceData, meta interface{}) erro
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "POST", project, url, obj, d.Timeout(schema.TimeoutCreate))
res, err := sendRequestWithTimeout(config, "POST", project, url, obj, d.Timeout(schema.TimeoutCreate), datastoreIndex409Contention)
if err != nil {
return fmt.Errorf("Error creating Index: %s", err)
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func resourceDatastoreIndexRead(d *schema.ResourceData, meta interface{}) error
if err != nil {
return err
}
res, err := sendRequest(config, "GET", project, url, nil)
res, err := sendRequest(config, "GET", project, url, nil, datastoreIndex409Contention)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("DatastoreIndex %q", d.Id()))
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func resourceDatastoreIndexDelete(d *schema.ResourceData, meta interface{}) erro
var obj map[string]interface{}
log.Printf("[DEBUG] Deleting Index %q", d.Id())

res, err := sendRequestWithTimeout(config, "DELETE", project, url, obj, d.Timeout(schema.TimeoutDelete))
res, err := sendRequestWithTimeout(config, "DELETE", project, url, obj, d.Timeout(schema.TimeoutDelete), datastoreIndex409Contention)
if err != nil {
return handleNotFoundError(err, d, "Index")
}
Expand Down
2 changes: 1 addition & 1 deletion google-beta/resource_datastore_index_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func testAccCheckDatastoreIndexDestroyProducer(t *testing.T) func(s *terraform.S
return err
}

_, err = sendRequest(config, "GET", "", url, nil)
_, err = sendRequest(config, "GET", "", url, nil, datastoreIndex409Contention)
if err == nil {
return fmt.Errorf("DatastoreIndex still exists at %s", url)
}
Expand Down

0 comments on commit 4e268e1

Please sign in to comment.