summaryrefslogtreecommitdiffstats
path: root/subcmds/sync.py
diff options
context:
space:
mode:
authorGavin Mak <gavinmak@google.com>2023-05-10 20:41:12 +0000
committerLUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-05-10 21:14:57 +0000
commit041f97725a8b647b82fbeba971cb2caf84da68c7 (patch)
treec6b4a462340c0bb0b1832619a1c15015fd405e15 /subcmds/sync.py
parent3e3340d94f8a680156ceaa81faca1c3b8863c6ac (diff)
downloadgit-repo-041f97725a8b647b82fbeba971cb2caf84da68c7.tar.gz
sync: Fix how sync times for shared projects are recordedv2.33
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>
Diffstat (limited to 'subcmds/sync.py')
-rw-r--r--subcmds/sync.py34
1 files changed, 15 insertions, 19 deletions
diff --git a/subcmds/sync.py b/subcmds/sync.py
index 324f15b5..68a076df 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -1805,44 +1805,40 @@ class _FetchTimes(object):
1805 1805
1806 def __init__(self, manifest): 1806 def __init__(self, manifest):
1807 self._path = os.path.join(manifest.repodir, ".repo_fetchtimes.json") 1807 self._path = os.path.join(manifest.repodir, ".repo_fetchtimes.json")
1808 self._times = None 1808 self._saved = None
1809 self._seen = set() 1809 self._seen = {}
1810 1810
1811 def Get(self, project): 1811 def Get(self, project):
1812 self._Load() 1812 self._Load()
1813 return self._times.get(project.name, _ONE_DAY_S) 1813 return self._saved.get(project.name, _ONE_DAY_S)
1814 1814
1815 def Set(self, project, t): 1815 def Set(self, project, t):
1816 self._Load()
1817 name = project.name 1816 name = project.name
1818 old = self._times.get(name, t) 1817
1819 self._seen.add(name) 1818 # For shared projects, save the longest time.
1820 a = self._ALPHA 1819 self._seen[name] = max(self._seen.get(name, 0), t)
1821 self._times[name] = (a * t) + ((1 - a) * old)
1822 1820
1823 def _Load(self): 1821 def _Load(self):
1824 if self._times is None: 1822 if self._saved is None:
1825 try: 1823 try:
1826 with open(self._path) as f: 1824 with open(self._path) as f:
1827 self._times = json.load(f) 1825 self._saved = json.load(f)
1828 except (IOError, ValueError): 1826 except (IOError, ValueError):
1829 platform_utils.remove(self._path, missing_ok=True) 1827 platform_utils.remove(self._path, missing_ok=True)
1830 self._times = {} 1828 self._saved = {}
1831 1829
1832 def Save(self): 1830 def Save(self):
1833 if self._times is None: 1831 if self._saved is None:
1834 return 1832 return
1835 1833
1836 to_delete = [] 1834 for name, t in self._seen.items():
1837 for name in self._times: 1835 # Keep a moving average across the previous/current sync runs.
1838 if name not in self._seen: 1836 old = self._saved.get(name, t)
1839 to_delete.append(name) 1837 self._seen[name] = (self._ALPHA * t) + ((1 - self._ALPHA) * old)
1840 for name in to_delete:
1841 del self._times[name]
1842 1838
1843 try: 1839 try:
1844 with open(self._path, "w") as f: 1840 with open(self._path, "w") as f:
1845 json.dump(self._times, f, indent=2) 1841 json.dump(self._seen, f, indent=2)
1846 except (IOError, TypeError): 1842 except (IOError, TypeError):
1847 platform_utils.remove(self._path, missing_ok=True) 1843 platform_utils.remove(self._path, missing_ok=True)
1848 1844