Repository navigation
[webkitscmpy] Invalidate cache when branches move backward - #76262
JonWBedard wants to merge 1 commit into
Conversation
https://bugs.webkit.org/show_bug.cgi?id=326753 rdar://189460513 Reviewed by NOBODY (OOPS!). Handle case where a branch move backward. Particularly common when contributors stage a change on main and then move that change to a PR branch. * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py: (Git.Cache.load): (Git.Cache._fill): (Git.Cache.populate): (Git.Cache.clear): (Git): * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/test/git_unittest.py:
|
EWS run on current version of this PR (hash d2b36d4) Details |
gsnedders
left a comment
There was a problem hiding this comment.
I don't think one walk can tell whether commits have left the default branch: it can't distinguish a rewind from local main simply being ahead of or behind origin/main, which happens all the time in the normal git-webkit workflow (more in the inline comments). origin/main doesn't rewind, so the real case is git-webkit pr: it moves the commit to eng/…, then runs git branch -f main remotes/origin/main, leaving the commit on no default-branch ref:
So I think detection needs to ask about every default-branch ref: local main plus each of source_remotes()'s main, by explicit ref name rather than a refs/remotes/*/main wildcard. A single git rev-list can list exactly the cached commits that are on none of them, so only those need removing (more in the inline comment on _fill(None)).
Even then, this only takes effect when something repopulates main. commit() only populates the cache on a miss, and a stale N@main is a hit, so straight after git-webkit pr a lookup still says N@main until another branch gets populated (see the thread on #76103).
I've put tests up as a PR against this branch: JonWBedard#2. Three pass without this PR and fail with it (quoted inline), and one covers the git-webkit pr case, failing without it and passing with it.
|
|
||
| self._fill(self.repo.default_branch) | ||
| for branch in self._ordered_commits.keys(): | ||
| for branch in list(self._ordered_commits.keys()): |
There was a problem hiding this comment.
We're copying this… so we can remove items from it during iteration.
Nit: I might be tempted to add a comment to explain this, especially given the indirection via d.
Though I do wonder if we should actually be looking at the union of keys: self._ordered_commits.keys() | self._ordered_revisions.keys() | self._last_populated.keys() (which would also handle the avoiding copying!).
| self._fill(branch) | ||
| except BaseException: | ||
| pass | ||
| except (IOError, OSError, ValueError, KeyError): |
There was a problem hiding this comment.
IOError = OSError since Python 3.3, so you don't need both.
The tuple also misses some damaged-but-valid-JSON cases the old BaseException caught: IndexError from _fill when revisions[branch] is shorter than hashes[branch], and TypeError/AttributeError when an entry isn't the expected type. Those now escape Cache.__init__, so local.Git(path) raises instead of falling back to populating:
webkitscmpy.test.git_unittest.TestGit.test_cache_file_revisions_shorter_than_hashes erred:
...
File "Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py", line 113, in _fill
if self._ordered_revisions[branch][index]:
IndexError: list index out of range
Exception is probably right here.
| except BaseException: | ||
| pass | ||
| except (IOError, OSError, ValueError, KeyError): | ||
| self.repo.log("Failed to load identifier cache from '{}'".format(self.path)) |
There was a problem hiding this comment.
I'd be tempted to split this up into smaller try/except blocks, with more specific error messages?
We have two largely separate things in the try block:
- Loading
self._ordered_commitsandself._ordered_revisionsfrom the JSON file. - Making sure everything is consistent and refilling the cache.
It probably makes sense to treat these two separately?
Whichever way it's split, the failure path should also reset _ordered_commits, _ordered_revisions and both identifier maps, rather than leaving whatever was half-loaded.
| for branch in self._ordered_commits.keys(): | ||
| if branch != default_branch: | ||
| self._fill(branch) |
There was a problem hiding this comment.
Is default_branch the only special case, or should we do all production branches first, maybe linked to priority order so that we build older versions first (thinking especially of safari-7625.1-branch before safari-7625.1.2-branch).
I guess currently we don't do this in the rest of the code, so that's probably not a change for now, and this code is fine?
| return | ||
| # Only bail early if the branch hasn't moved. If our tip is a cached commit which isn't the last | ||
| # one we know of, the branch was moved backwards (reset, rebase) and the cache must be truncated. | ||
| if intersected and len(hashes) <= 1 and self._ordered_commits[branch] and self._ordered_commits[branch][-1] == hashes[0]: |
There was a problem hiding this comment.
This is where a walk stopping at a cached commit that isn't the cached tip becomes a truncation. That also happens when nothing has been rewound:
-
Ahead: you've committed on
mainbut not yet rungit-webkit pr. Populating any other branch walksorigin/mainfirst, which stops short of the unpushed commit, so it's dropped. Thepopulate(main)straight after is skipped because_last_populated['main']was just set, so it's only added back by a later lookup:WebKit/Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py
Lines 118 to 120 in cbf0c78
-
Behind: after a
git fetchthat doesn't update localmain, walking localmaindrops every commit that's only onorigin/main, though they're still on the default branch:webkitscmpy.test.git_unittest.TestGit.test_cache_local_default_branch_behind_remote failed: ... AssertionError: None != '5@main'
Lookups stay correct, since commit() falls back to git on a miss, but each drop costs a full rebuild (see my comment on _fill(None)). I think the decision to truncate needs to come from all the default-branch refs, not this one walk.
| self._branch = None | ||
| self.cache = self.Cache(self) if self.root_path and cached else None | ||
| self.default_remote = 'origin' | ||
| self.cache = self.Cache(self) if self.root_path and cached else None |
There was a problem hiding this comment.
It might be worth making explicit why this reorder had to happen.
| def _fill(self, branch=None): | ||
| default_branch = self.repo.default_branch | ||
|
|
||
| # Without a branch, rebuild identifiers for every branch from scratch |
There was a problem hiding this comment.
This feels entirely too aggressive — we shouldn't need to drop everything?
Only the commits that are no longer on any default-branch ref need dropping, and git can list exactly those, starting from the cached main tip (self._ordered_commits[default_branch][-1], before populate() changes it):
git rev-list --ignore-missing <cached main tip> --not refs/heads/main refs/remotes/origin/main …
with one refs/remotes/<remote>/main for each of source_remotes(). --ignore-missing covers remotes without a main. Every cached main entry is an ancestor of that tip, so this covers the whole list, but it only walks the part no ref has. That's empty when local main is just ahead of or behind origin/main, and is only the moved commit after git-webkit pr. It takes about 10–15 ms on a full WebKit checkout, and only needs running when populate()'s walk stops short of the cached tip.
Then only those hashes need removing from _hash_to_identifiers and _revisions_to_identifiers, plus re-_filling any branch whose branch point is among them. Everything before them keeps its identifier.
As it is, with a WebKit-sized main (~323k) this takes about 0.5 s, against microseconds when only the tail needs filling, and it's reachable from to_identifier/to_hash on a miss. In the ahead case (see my comment on the early return in populate()) it happens on every lookup that populates another branch, recomputing all of main when nothing on it has changed:
webkitscmpy.test.git_unittest.TestGit.test_cache_local_default_branch_ahead_of_remote failed:
...
AssertionError: 8 != 13
(that's the identifiers computed beyond loading the cache: 2 for the branch's own commits, against 7).
|
Closing in favor of #77838, which is a different way of solving this problem. |
d2b36d4
d2b36d4