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 'cross-transaction contention' #16618

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/9515.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 "Aborted due to cross-transaction contention" in `google_firestore_index `
```
41 changes: 22 additions & 19 deletions google/services/firestore/resource_firestore_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@ func resourceFirestoreIndexCreate(d *schema.ResourceData, meta interface{}) erro
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409CrossTransactionContetion},
})
if err != nil {
return fmt.Errorf("Error creating Index: %s", err)
Expand Down Expand Up @@ -322,11 +323,12 @@ func resourceFirestoreIndexRead(d *schema.ResourceData, meta interface{}) error
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409CrossTransactionContetion},
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("FirestoreIndex %q", d.Id()))
Expand Down Expand Up @@ -381,13 +383,14 @@ func resourceFirestoreIndexDelete(d *schema.ResourceData, meta interface{}) erro
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "DELETE",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutDelete),
Config: config,
Method: "DELETE",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutDelete),
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409CrossTransactionContetion},
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, "Index")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ func testAccCheckFirestoreIndexDestroyProducer(t *testing.T) func(s *terraform.S
}

_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: config.UserAgent,
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: config.UserAgent,
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.FirestoreIndex409CrossTransactionContetion},
})
if err == nil {
return fmt.Errorf("FirestoreIndex still exists at %s", url)
Expand Down
10 changes: 10 additions & 0 deletions google/transport/error_retry_predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ func FirestoreField409RetryUnderlyingDataChanged(err error) (bool, string) {
return false, ""
}

// 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") {
return true, "aborted due to cross-transaction contention - retrying"
}
}
return false, ""
}

func IapClient409Operation(err error) (bool, string) {
if gerr, ok := err.(*googleapi.Error); ok {
if gerr.Code == 409 && strings.Contains(strings.ToLower(gerr.Body), "operation was aborted") {
Expand Down
11 changes: 11 additions & 0 deletions google/transport/error_retry_predicates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,14 @@ func TestFirestoreField409_retryUnderlyingDataChanged(t *testing.T) {
t.Errorf("Error not detected as retryable")
}
}

func TestFirestoreIndex409_crossTransactionContetion(t *testing.T) {
err := googleapi.Error{
Code: 409,
Body: "Aborted due to cross-transaction contention",
}
isRetryable, _ := FirestoreIndex409CrossTransactionContetion(&err)
if !isRetryable {
t.Errorf("Error not detected as retryable")
}
}