sync: Fix how sync times for shared projects are recorded

https://gerrit.googlesource.com/git-repo/+/d947858325ae70ff9c0b2f463a9e8c4ffd00002a introduced a moving average of fetch times in 2012.

The code does not handle shared projects, and averages times based on project names which is incorrect.

Change-Id: I9926122cdb1ecf201887a81e96f5f816d3c2f72a
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/373574
Tested-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
This commit is contained in:
Gavin Mak 2023-05-10 20:41:12 +00:00 committed by LUCI
parent 3e3340d94f
commit 041f97725a

View File

@ -1805,44 +1805,40 @@ class _FetchTimes(object):
def __init__(self, manifest): def __init__(self, manifest):
self._path = os.path.join(manifest.repodir, ".repo_fetchtimes.json") self._path = os.path.join(manifest.repodir, ".repo_fetchtimes.json")
self._times = None self._saved = None
self._seen = set() self._seen = {}
def Get(self, project): def Get(self, project):
self._Load() self._Load()
return self._times.get(project.name, _ONE_DAY_S) return self._saved.get(project.name, _ONE_DAY_S)
def Set(self, project, t): def Set(self, project, t):
self._Load()
name = project.name name = project.name
old = self._times.get(name, t)
self._seen.add(name) # For shared projects, save the longest time.
a = self._ALPHA self._seen[name] = max(self._seen.get(name, 0), t)
self._times[name] = (a * t) + ((1 - a) * old)
def _Load(self): def _Load(self):
if self._times is None: if self._saved is None:
try: try:
with open(self._path) as f: with open(self._path) as f:
self._times = json.load(f) self._saved = json.load(f)
except (IOError, ValueError): except (IOError, ValueError):
platform_utils.remove(self._path, missing_ok=True) platform_utils.remove(self._path, missing_ok=True)
self._times = {} self._saved = {}
def Save(self): def Save(self):
if self._times is None: if self._saved is None:
return return
to_delete = [] for name, t in self._seen.items():
for name in self._times: # Keep a moving average across the previous/current sync runs.
if name not in self._seen: old = self._saved.get(name, t)
to_delete.append(name) self._seen[name] = (self._ALPHA * t) + ((1 - self._ALPHA) * old)
for name in to_delete:
del self._times[name]
try: try:
with open(self._path, "w") as f: with open(self._path, "w") as f:
json.dump(self._times, f, indent=2) json.dump(self._seen, f, indent=2)
except (IOError, TypeError): except (IOError, TypeError):
platform_utils.remove(self._path, missing_ok=True) platform_utils.remove(self._path, missing_ok=True)