sync: cleanup sleep+retry logic a bit

Make sure we print a message whenever we retry so it's clear to the
user why repo is pausing for a long time, and why repo might have
passed even though it displayed some errors earlier.

Also unify the sleep logic so we don't have two independent methods.
This makes it easier to reason about.

Also don't sleep if we're in the last iteration of the for loop.  It
doesn't make sense to and needlessly slows things down when there are
real errors.

Bug: https://crbug.com/gerrit/12494
Change-Id: Ifceace5b2dde75c2dac39ea5388527dd37376336
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/303402
Reviewed-by: Sam Saccone 🐐 <samccone@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger 2021-04-15 02:06:28 -04:00
parent ad8aa69772
commit 6823bc269d

View File

@ -2175,19 +2175,13 @@ class Project(object):
elif (gitcmd.stdout and elif (gitcmd.stdout and
'error:' in gitcmd.stdout and 'error:' in gitcmd.stdout and
'HTTP 429' in gitcmd.stdout): 'HTTP 429' in gitcmd.stdout):
if not quiet: # Fallthru to sleep+retry logic at the bottom.
print('429 received, sleeping: %s sec' % retry_cur_sleep, pass
file=sys.stderr)
time.sleep(retry_cur_sleep)
retry_cur_sleep = min(retry_exp_factor * retry_cur_sleep,
MAXIMUM_RETRY_SLEEP_SEC)
retry_cur_sleep *= (1 - random.uniform(-RETRY_JITTER_PERCENT,
RETRY_JITTER_PERCENT))
continue
# If this is not last attempt, try 'git remote prune'. # Try to prune remote branches once in case there are conflicts.
elif (try_n < retry_fetches - 1 and # For example, if the remote had refs/heads/upstream, but deleted that and
gitcmd.stdout and # now has refs/heads/upstream/foo.
elif (gitcmd.stdout and
'error:' in gitcmd.stdout and 'error:' in gitcmd.stdout and
'git remote prune' in gitcmd.stdout and 'git remote prune' in gitcmd.stdout and
not prune_tried): not prune_tried):
@ -2197,6 +2191,8 @@ class Project(object):
ret = prunecmd.Wait() ret = prunecmd.Wait()
if ret: if ret:
break break
output_redir.write('retrying fetch after pruning remote branches')
# Continue right away so we don't sleep as we shouldn't need to.
continue continue
elif current_branch_only and is_sha1 and ret == 128: elif current_branch_only and is_sha1 and ret == 128:
# Exit code 128 means "couldn't find the ref you asked for"; if we're # Exit code 128 means "couldn't find the ref you asked for"; if we're
@ -2206,9 +2202,17 @@ class Project(object):
elif ret < 0: elif ret < 0:
# Git died with a signal, exit immediately # Git died with a signal, exit immediately
break break
# Figure out how long to sleep before the next attempt, if there is one.
if not verbose: if not verbose:
print('\n%s:\n%s' % (self.name, gitcmd.stdout), file=sys.stderr) output_redir.write('\n%s:\n%s' % (self.name, gitcmd.stdout), file=sys.stderr)
time.sleep(random.randint(30, 45)) if try_n < retry_fetches - 1:
output_redir.write('sleeping %s seconds before retrying' % retry_cur_sleep)
time.sleep(retry_cur_sleep)
retry_cur_sleep = min(retry_exp_factor * retry_cur_sleep,
MAXIMUM_RETRY_SLEEP_SEC)
retry_cur_sleep *= (1 - random.uniform(-RETRY_JITTER_PERCENT,
RETRY_JITTER_PERCENT))
if initial: if initial:
if alt_dir: if alt_dir: