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 on firestore index create 409 with 'underlying data changed' #16670

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
3 changes: 3 additions & 0 deletions .changelog/9570.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
firestore: retried resource creation for error 409 with the text "Please retry, underlying data changed" in `google_firestore_index`
```
6 changes: 3 additions & 3 deletions google/services/firestore/resource_firestore_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func resourceFirestoreIndexCreate(d *schema.ResourceData, meta interface{}) erro
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409CrossTransactionContetion},
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409Retry},
})
if err != nil {
return fmt.Errorf("Error creating Index: %s", err)
Expand Down Expand Up @@ -328,7 +328,7 @@ func resourceFirestoreIndexRead(d *schema.ResourceData, meta interface{}) error
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409CrossTransactionContetion},
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409Retry},
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("FirestoreIndex %q", d.Id()))
Expand Down Expand Up @@ -390,7 +390,7 @@ func resourceFirestoreIndexDelete(d *schema.ResourceData, meta interface{}) erro
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutDelete),
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409CrossTransactionContetion},
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409Retry},
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, "Index")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func testAccCheckFirestoreIndexDestroyProducer(t *testing.T) func(s *terraform.S
Project: billingProject,
RawURL: url,
UserAgent: config.UserAgent,
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409CrossTransactionContetion},
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409Retry},
})
if err == nil {
return fmt.Errorf("FirestoreIndex still exists at %s", url)
Expand Down
10 changes: 7 additions & 3 deletions google/transport/error_retry_predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,15 @@ func FirestoreField409RetryUnderlyingDataChanged(err error) (bool, string) {
}

// relevant for firestore in datastore mode
func FirestoreIndex409CrossTransactionContetion(err error) (bool, string) {
if gerr, ok := err.(*googleapi.Error); ok {
if gerr.Code == 409 && strings.Contains(gerr.Body, "Aborted due to cross-transaction contention") {
func FirestoreIndex409Retry(err error) (bool, string) {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 409 {
if strings.Contains(gerr.Body, "Aborted due to cross-transaction contention") {
return true, "aborted due to cross-transaction contention - retrying"
}

if strings.Contains(gerr.Body, "Please retry, underlying data changed") {
return true, "underlying data changed - retrying"
}
}
return false, ""
}
Expand Down
13 changes: 12 additions & 1 deletion google/transport/error_retry_predicates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,18 @@ func TestFirestoreIndex409_crossTransactionContetion(t *testing.T) {
Code: 409,
Body: "Aborted due to cross-transaction contention",
}
isRetryable, _ := FirestoreIndex409CrossTransactionContetion(&err)
isRetryable, _ := FirestoreIndex409Retry(&err)
if !isRetryable {
t.Errorf("Error not detected as retryable")
}
}

func TestFirestoreIndex409_retryUnderlyingDataChanged(t *testing.T) {
err := googleapi.Error{
Code: 409,
Body: "Please retry, underlying data changed",
}
isRetryable, _ := FirestoreIndex409Retry(&err)
if !isRetryable {
t.Errorf("Error not detected as retryable")
}
Expand Down