Skip to content

Commit e8e397e

Browse files
modular-magicianRadoslaw Majkut
and
Radoslaw Majkut
authored
Retry all GCE's 403 per-minute quota exceeded errors (#5913) (#11508)
Co-authored-by: Radoslaw Majkut <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Radoslaw Majkut <[email protected]>
1 parent 2fd1089 commit e8e397e

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

.changelog/5913.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
provider: modified request retry logic to retry all per-minute quota limits returned with a 403 error code. Previously, only read requests were retried. This will generally affect Google Compute Engine resources.
3+
```

google/common_operation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func CommonRefreshFunc(w Waiter) resource.StateRefreshFunc {
111111
op, err := w.QueryOp()
112112
if err != nil {
113113
// Retry 404 when getting operation (not resource state)
114-
if isRetryableError(err, isNotFoundRetryableError("GET operation"), isOperationReadQuotaError) {
114+
if isRetryableError(err, isNotFoundRetryableError("GET operation")) {
115115
log.Printf("[DEBUG] Dismissed retryable error on GET operation %q: %s", w.OpName(), err)
116116
return nil, "done: false", nil
117117
}

google/error_retry_predicates.go

+10-17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"net"
88
"net/url"
9+
"regexp"
910
"strings"
1011

1112
"google.golang.org/api/googleapi"
@@ -45,7 +46,7 @@ var defaultErrorRetryPredicates = []RetryErrorPredicateFunc{
4546
// reads, causing significant failure for our CI and for large customers.
4647
// GCE returns the wrong error code, as this should be a 429, which we retry
4748
// already.
48-
is403ReadRequestsForMinuteError,
49+
is403QuotaExceededPerMinuteError,
4950
}
5051

5152
/** END GLOBAL ERROR RETRY PREDICATES HERE **/
@@ -127,15 +128,18 @@ func isSubnetworkUnreadyError(err error) (bool, string) {
127128

128129
// GCE (and possibly other APIs) incorrectly return a 403 rather than a 429 on
129130
// rate limits.
130-
func is403ReadRequestsForMinuteError(err error) (bool, string) {
131+
func is403QuotaExceededPerMinuteError(err error) (bool, string) {
131132
gerr, ok := err.(*googleapi.Error)
132133
if !ok {
133134
return false, ""
134135
}
135-
136-
if gerr.Code == 403 && strings.Contains(gerr.Body, "Quota exceeded for quota metric") && strings.Contains(gerr.Body, "Read requests per minute") {
137-
log.Printf("[DEBUG] Dismissed an error as retryable based on error code 403 and error message 'Quota exceeded for quota metric' on metric `Read requests per minute`: %s", err)
138-
return true, "Read requests per minute"
136+
var QuotaRegex = regexp.MustCompile(`Quota exceeded for quota metric '(?P<Metric>.*)' and limit '(?P<Limit>.* per minute)' of service`)
137+
if gerr.Code == 403 && QuotaRegex.MatchString(gerr.Body) {
138+
matches := QuotaRegex.FindStringSubmatch(gerr.Body)
139+
metric := matches[QuotaRegex.SubexpIndex("Metric")]
140+
limit := matches[QuotaRegex.SubexpIndex("Limit")]
141+
log.Printf("[DEBUG] Dismissed an error as retryable based on error code 403 and error message 'Quota exceeded for quota metric `%s`: %s", metric, err)
142+
return true, fmt.Sprintf("Waiting for quota limit %s to refresh", limit)
139143
}
140144
return false, ""
141145
}
@@ -260,17 +264,6 @@ func isBigqueryIAMQuotaError(err error) (bool, string) {
260264
return false, ""
261265
}
262266

263-
// Retry if operation returns a 403 with the message for
264-
// exceeding the quota limit for 'OperationReadGroup'
265-
func isOperationReadQuotaError(err error) (bool, string) {
266-
if gerr, ok := err.(*googleapi.Error); ok {
267-
if gerr.Code == 403 && strings.Contains(gerr.Body, "Quota exceeded for quota group") {
268-
return true, "Waiting for quota to refresh"
269-
}
270-
}
271-
return false, ""
272-
}
273-
274267
// Retry if Monitoring operation returns a 409 with a specific message for
275268
// concurrent operations.
276269
func isMonitoringConcurrentEditError(err error) (bool, string) {

google/error_retry_predicates_test.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,36 @@ func TestIsCommonRetryableErrorCode_otherError(t *testing.T) {
8484
func TestIsOperationReadQuotaError_quotaExceeded(t *testing.T) {
8585
err := googleapi.Error{
8686
Code: 403,
87-
Body: "Quota exceeded for quota group 'OperationReadGroup' and limit 'Operation read requests per 100 seconds' of service 'compute.googleapis.com' for consumer 'project_number:11111111'.",
87+
Body: "Quota exceeded for quota metric 'OperationReadGroup' and limit 'Operation read requests per minute' of service 'compute.googleapis.com' for consumer 'project_number:11111111'.",
8888
}
89-
isRetryable, _ := isOperationReadQuotaError(&err)
89+
isRetryable, _ := is403QuotaExceededPerMinuteError(&err)
9090
if !isRetryable {
9191
t.Errorf("Error not detected as retryable")
9292
}
9393
}
9494

95+
func TestIs403QuotaExceededPerMinuteError_perMinuteQuotaExceeded(t *testing.T) {
96+
err := googleapi.Error{
97+
Code: 403,
98+
Body: "Quota exceeded for quota metric 'Queries' and limit 'Queries per minute' of service 'compute.googleapis.com' for consumer 'project_number:11111111'.",
99+
}
100+
isRetryable, _ := is403QuotaExceededPerMinuteError(&err)
101+
if !isRetryable {
102+
t.Errorf("Error not detected as retryable")
103+
}
104+
}
105+
106+
func TestIs403QuotaExceededPerMinuteError_perDayQuotaExceededNotRetryable(t *testing.T) {
107+
err := googleapi.Error{
108+
Code: 403,
109+
Body: "Quota exceeded for quota metric 'Queries' and limit 'Queries per day' of service 'compute.googleapis.com' for consumer 'project_number:11111111'.",
110+
}
111+
isRetryable, _ := is403QuotaExceededPerMinuteError(&err)
112+
if isRetryable {
113+
t.Errorf("Error incorrectly detected as retryable")
114+
}
115+
}
116+
95117
func TestGRPCRetryable(t *testing.T) {
96118
code := codes.FailedPrecondition
97119
err := status.Error(code, "is retryable")

0 commit comments

Comments
 (0)