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

[release-3.27] iam: separate backoffs, add jitter, and increase for conflicts #2

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 16 additions & 7 deletions google/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"math/rand"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -79,13 +80,19 @@ func iamPolicyReadModifyWrite(updater ResourceIamUpdater, modify iamPolicyModify
mutexKV.Lock(mutexKey)
defer mutexKV.Unlock(mutexKey)

backoff := time.Second
// Used for introducing jitter in backoffs
rand.Seed(time.Now().UTC().UnixNano())

readBackoff := time.Second
conflictBackoff := time.Second
for {
log.Printf("[DEBUG]: Retrieving policy for %s\n", updater.DescribeResource())
p, err := updater.GetResourceIamPolicy()
if isGoogleApiErrorWithCode(err, 429) {
log.Printf("[DEBUG] 429 while attempting to read policy for %s, waiting %v before attempting again", updater.DescribeResource(), backoff)
time.Sleep(backoff)
readBackoffWithJitter := readBackoff + time.Duration(rand.Intn(1000))*time.Millisecond
log.Printf("[DEBUG] 429 while attempting to read policy for %s, waiting %v before attempting again", updater.DescribeResource(), readBackoffWithJitter)
time.Sleep(readBackoffWithJitter)
readBackoff = readBackoff * 2
continue
} else if err != nil {
return err
Expand Down Expand Up @@ -140,14 +147,16 @@ func iamPolicyReadModifyWrite(updater ResourceIamUpdater, modify iamPolicyModify
break
}
if isConflictError(err) {
log.Printf("[DEBUG]: Concurrent policy changes, restarting read-modify-write after %s\n", backoff)
time.Sleep(backoff)
backoff = backoff * 2
if backoff > 30*time.Second {
conflictBackoffWithJitter := conflictBackoff + time.Duration(rand.Intn(1000))*time.Millisecond
log.Printf("[DEBUG]: Concurrent policy changes, restarting read-modify-write after %v\n", conflictBackoffWithJitter)
time.Sleep(conflictBackoffWithJitter)
conflictBackoff = conflictBackoff * 2
if conflictBackoff > 5*time.Minute {
return errwrap.Wrapf(fmt.Sprintf("Error applying IAM policy to %s: Too many conflicts. Latest error: {{err}}", updater.DescribeResource()), err)
}
continue
}

return errwrap.Wrapf(fmt.Sprintf("Error applying IAM policy for %s: {{err}}", updater.DescribeResource()), err)
}
log.Printf("[DEBUG]: Set policy for %s", updater.DescribeResource())
Expand Down