Skip to content

Commit

Permalink
Retry on firestore index create 409 with 'cross-transaction contentio…
Browse files Browse the repository at this point in the history
…n' (#9515) (#16618)

[upstream:0126c3d731f79d8e38905271a8430b3d1676544f]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Nov 28, 2023
1 parent 37d155c commit 0f301c7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
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")
}
}

0 comments on commit 0f301c7

Please sign in to comment.