diff --git a/releasenotes/notes/change-cache-prune-fix-eac72e164927c028.yaml b/releasenotes/notes/change-cache-prune-fix-eac72e164927c028.yaml new file mode 100644 index 0000000000..bb683071ad --- /dev/null +++ b/releasenotes/notes/change-cache-prune-fix-eac72e164927c028.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + A bug with the change cache cleanup routine which could have + caused items to be stuck in pipelines without running jobs has + been corrected. diff --git a/tests/unit/test_zk.py b/tests/unit/test_zk.py index c3c23eb422..97e8b130c2 100644 --- a/tests/unit/test_zk.py +++ b/tests/unit/test_zk.py @@ -1315,6 +1315,30 @@ class TestChangeCache(ZooKeeperBaseTestCase): invalid_key = ChangeKey('conn', 'project', 'change', 'invalid', '1') self.cache.delete(invalid_key) + def test_concurrent_delete(self): + change = DummyChange("project", {"foo": "bar"}) + key = ChangeKey('conn', 'project', 'change', 'foo', '1') + self.cache.set(key, change) + old_version = change.cache_version + # Simulate someone updating the cache after we decided to + # delete the entry + self.cache.set(key, change, old_version) + self.assertNotEqual(old_version, change.cache_version) + self.cache.delete(key, old_version) + # The change should still be in the cache + self.assertIsNotNone(self.cache.get(key)) + + def test_prune(self): + change1 = DummyChange("project", {"foo": "bar"}) + change2 = DummyChange("project", {"foo": "baz"}) + key1 = ChangeKey('conn', 'project', 'change', 'foo', '1') + key2 = ChangeKey('conn', 'project', 'change', 'foo', '2') + self.cache.set(key1, change1) + self.cache.set(key2, change2) + self.cache.prune([key1], max_age=0) + self.assertIsNotNone(self.cache.get(key1)) + self.assertIsNone(self.cache.get(key2)) + def test_concurrent_update(self): change = DummyChange("project", {"foo": "bar"}) key = ChangeKey('conn', 'project', 'change', 'foo', '1') diff --git a/zuul/zk/change_cache.py b/zuul/zk/change_cache.py index 69f806ebce..3d01b7bf06 100644 --- a/zuul/zk/change_cache.py +++ b/zuul/zk/change_cache.py @@ -201,11 +201,21 @@ class AbstractChangeCache(ZooKeeperSimpleBase, Iterable, abc.ABC): def prune(self, relevant, max_age=3600): # 1h cutoff_time = time.time() - max_age - outdated = {c.cache_stat.key for c in list(self._change_cache.values()) - if c.cache_stat.last_modified < cutoff_time} - to_prune = outdated - set(relevant) + outdated_versions = dict() + for c in list(self._change_cache.values()): + # Assign to a local variable so all 3 values we use are + # consistent in case the cache_stat is updated during this + # loop. + cache_stat = c.cache_stat + if cache_stat.last_modified >= cutoff_time: + # This entry isn't old enough to delete yet + continue + # Save the version we examined so we can make sure to only + # delete that version. + outdated_versions[cache_stat.key] = cache_stat.version + to_prune = set(outdated_versions.keys()) - set(relevant) for key in to_prune: - self.delete(key) + self.delete(key, outdated_versions[key]) def cleanup(self): valid_uuids = {c.cache_stat.uuid @@ -347,12 +357,20 @@ class AbstractChangeCache(ZooKeeperSimpleBase, Iterable, abc.ABC): change = self.get(key) return change - def delete(self, key): + def delete(self, key, version=-1): cache_path = self._cachePath(key._hash) # Only delete the cache entry and NOT the data node in order to # prevent race conditions with other consumers. The stale data # nodes will be removed by the periodic cleanup. - self.kazoo_client.delete(cache_path, recursive=True) + try: + self.kazoo_client.delete(cache_path, version) + except BadVersionError: + # Someone else may have written a new entry since we + # decided to delete this, so we should no longer delete + # the entry. + return + except NoNodeError: + pass with contextlib.suppress(KeyError): del self._change_cache[key._hash]