Clean up the functional download cache after some months
PATCH 1/2 tests/functional: Set current time stamp of assets when using them
In reply to Thomas HuthFrom: Thomas Huth <[email protected]>
We are going to remove obsolete assets from the cache, so keep the time stamps of the assets that we use up-to-date to have a way to detect stale assets later.
--- tests/functional/qemu_test/asset.py | 8 ++++++++ 1 file changed, 8 insertions(+)
tests/functional/qemu_test/asset.py +8 -0
@@ -10,6 +10,7 @@ import os import stat import sys +import time import unittest import urllib.request from time import sleep
@@ -113,6 +114,11 @@ def _wait_for_other_download(self, tmp_cache_file): self.log.debug("Time out while waiting for %s!", tmp_cache_file) raise + def _save_time_stamp(self): + with open(self.cache_file.with_suffix(".stamp"), 'w', + encoding='utf-8') as fh: + fh.write(f"{int(time.time())}") + def fetch(self): if not self.cache_dir.exists(): self.cache_dir.mkdir(parents=True, exist_ok=True)
@@ -120,6 +126,7 @@ def fetch(self): if self.valid(): self.log.debug("Using cached asset %s for %s", self.cache_file, self.url) + self._save_time_stamp() return str(self.cache_file) if not self.fetchable():
@@ -208,6 +215,7 @@ def fetch(self): tmp_cache_file.unlink() raise AssetError(self, "Hash does not match %s" % self.hash) tmp_cache_file.replace(self.cache_file) + self._save_time_stamp() # Remove write perms to stop tests accidentally modifying them os.chmod(self.cache_file, stat.S_IRUSR | stat.S_IRGRP)
Signature · 1 line
2.51.0
PATCH 2/2 tests: Evict stale files in the functional download cache after a while
In reply to Thomas HuthFrom: Thomas Huth <[email protected]>
The download cache of the functional tests is currently only growing. But sometimes tests get removed or changed to use different assets, thus we should clean up the stale old assets after a while when they are not in use anymore. So add a script that looks at the time stamps of the assets and removes them if they haven't been touched for more than half of a year. Since there might also be some assets around that have been added to the cache before we added the time stamp files, assume a default time stamp that is close to the creation date of this patch, so that we don't delete these files too early.
--- MAINTAINERS | 1 + scripts/clean_functional_cache.py | 47 +++++++++++++++++++++++++++++++ tests/Makefile.include | 1 + 3 files changed, 49 insertions(+) create mode 100755 scripts/clean_functional_cache.py
MAINTAINERS +1 -0
@@ -4398,6 +4398,7 @@ M: Thomas Huth <[email protected]> R: Philippe Mathieu-Daudé <[email protected]> R: Daniel P. Berrange <[email protected]> F: docs/devel/testing/functional.rst +F: scripts/clean_functional_cache.py F: tests/functional/qemu_test/ Windows Hosted Continuous Integration
scripts/clean_functional_cache.py new file +47 -0
47 lines not on this page · raw message
tests/Makefile.include +1 -0
@@ -111,6 +111,7 @@ $(FUNCTIONAL_TARGETS): check-venv .PHONY: check-functional check-functional: check-venv @$(NINJA) precache-functional + @$(PYTHON) $(SRC_PATH)/scripts/clean_functional_cache.py @QEMU_TEST_NO_DOWNLOAD=1 $(MAKE) SPEED=thorough check-func check-func-quick .PHONY: check-func check-func-quick
Signature · 1 line
2.51.0
cover Clean up the functional download cache after some months
The download cache of the functional tests is currently only growing. But sometimes tests get removed or changed to use different assets, thus we should clean up the stale old assets after a while when they are not in use anymore.
This little patch series now introduces time stamps for the assets in the cache (first patch) that we update during each test run, so we can check these time stamps regularly and evict stale assets after half of a year not being used anymore (that should be enough time to still keep the assets around for a while in case you want to diagnose a problem that is still some months old).
Thomas Huth (2):
tests/functional: Set current time stamp of assets when using them
tests: Evict stale files in the functional download cache after a
while
MAINTAINERS | 1 + scripts/clean_functional_cache.py | 47 +++++++++++++++++++++++++++++ tests/Makefile.include | 1 + tests/functional/qemu_test/asset.py | 8 +++++ 4 files changed, 57 insertions(+) create mode 100755 scripts/clean_functional_cache.py
Signature · 1 line
2.51.0
Re: [PATCH 1/2] tests/functional: Set current time stamp of assets when using them
In reply to Thomas HuthQuoted 24 lines · Thomas Huth
From: Thomas Huth <[email protected]>
We are going to remove obsolete assets from the cache, so keep the time stamps of the assets that we use up-to-date to have a way to detect stale assets later.
Signed-off-by Thomas Huth <[email protected]>--- tests/functional/qemu_test/asset.py | 8 ++++++++ 1 file changed, 8 insertions(+)tests/functional/qemu_test/asset.py +1
-0@@ -10,6 +10,7 @@ import os import stat import sys +import time import unittest import urllib.request from time import sleep
tests/functional/qemu_test/asset.py def _wait_for_other_download(self, tmp_cache_file): · line 120
Quoted 7 lines
tests/functional/qemu_test/asset.py +4
-0@@ -113,6 +114,11 @@ def _wait_for_other_download(self, tmp_cache_file): self.log.debug("Time out while waiting for %s!", tmp_cache_file) raise + def _save_time_stamp(self): + with open(self.cache_file.with_suffix(".stamp"), 'w', + encoding='utf-8') as fh: + fh.write(f"{int(time.time())}")
Rather than creating a parallel timestamp file, it feels like we could just call 'os.utime(self.cache_file)' which will set atime + mtime to the current timestamp, which we can check later with os.stat().
Quoted 23 lines
tests/functional/qemu_test/asset.py +3
-0+ def fetch(self): if not self.cache_dir.exists(): self.cache_dir.mkdir(parents=True, exist_ok=True)@@ -120,6 +126,7 @@ def fetch(self): if self.valid(): self.log.debug("Using cached asset %s for %s", self.cache_file, self.url) + self._save_time_stamp() return str(self.cache_file) if not self.fetchable():@@ -208,6 +215,7 @@ def fetch(self): tmp_cache_file.unlink() raise AssetError(self, "Hash does not match %s" % self.hash) tmp_cache_file.replace(self.cache_file) + self._save_time_stamp() # Remove write perms to stop tests accidentally modifying them os.chmod(self.cache_file, stat.S_IRUSR | stat.S_IRGRP)Signature · 1 line
2.51.0
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 1/2] tests/functional: Set current time stamp of assets when using them
Quoted 28 lines · Daniel P. Berrangé
Quoted 27 lines · Thomas Huth
From: Thomas Huth <[email protected]>
We are going to remove obsolete assets from the cache, so keep the time stamps of the assets that we use up-to-date to have a way to detect stale assets later.
Signed-off-by Thomas Huth <[email protected]>--- tests/functional/qemu_test/asset.py | 8 ++++++++ 1 file changed, 8 insertions(+)tests/functional/qemu_test/asset.py +1
-0@@ -10,6 +10,7 @@ import os import stat import sys +import time import unittest import urllib.request from time import sleep@@ -113,6 +114,11 @@ def _wait_for_other_download(self, tmp_cache_file): self.log.debug("Time out while waiting for %s!", tmp_cache_file) raise
Quoted 8 lines
tests/functional/qemu_test/asset.py def _wait_for_other_download(self, tmp_cache_file): · line 120
Quoted 4 lines
tests/functional/qemu_test/asset.py +4
-0+ def _save_time_stamp(self): + with open(self.cache_file.with_suffix(".stamp"), 'w', + encoding='utf-8') as fh: + fh.write(f"{int(time.time())}")Rather than creating a parallel timestamp file, it feels like we could just call '
os.utime(self.cache_file)' which will set atime + mtime to the current timestamp, which we can check later withos.stat().
That was my first idea, too (sorry, I should maybe have mentioned it in the patch description), but it does not work: In the gitlab CI runners, the files get re-initialized from the runners cache each time the functional test job is started, so the atime and mtime is always close to the the current date there --> we would never expire files in the gitlab CI that way.
Thomas
Re: [PATCH 2/2] tests: Evict stale files in the functional download cache after a while
In reply to Thomas HuthQuoted 46 lines · Thomas Huth
From: Thomas Huth <[email protected]>
The download cache of the functional tests is currently only growing. But sometimes tests get removed or changed to use different assets, thus we should clean up the stale old assets after a while when they are not in use anymore. So add a script that looks at the time stamps of the assets and removes them if they haven't been touched for more than half of a year. Since there might also be some assets around that have been added to the cache before we added the time stamp files, assume a default time stamp that is close to the creation date of this patch, so that we don't delete these files too early.
Signed-off-by Thomas Huth <[email protected]>--- MAINTAINERS | 1 + scripts/clean_functional_cache.py | 47 +++++++++++++++++++++++++++++++ tests/Makefile.include | 1 + 3 files changed, 49 insertions(+) create mode 100755 scripts/clean_functional_cache.pyMAINTAINERS +1
-0@@ -4398,6 +4398,7 @@ M: Thomas Huth <[email protected]> R: Philippe Mathieu-Daudé <[email protected]> R: Daniel P. Berrange <[email protected]> F: docs/devel/testing/functional.rst +F: scripts/clean_functional_cache.py F: tests/functional/qemu_test/ Windows Hosted Continuous Integrationscripts/clean_functional_cache.py new file +8
-0@@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# +# SPDX-License-Identifier: GPL-2.0-or-later +# +"""Delete stale assets from the download cache of the functional tests""" + +import os +import stat
scripts/clean_functional_cache.py line 18
Quoted 10 lines
scripts/clean_functional_cache.py new file +10
-0+import sys +import time +from pathlib import Path + + +cache_dir_env = os.getenv('QEMU_TEST_CACHE_DIR') +if cache_dir_env: + cache_dir = Path(cache_dir_env, "download") +else: + cache_dir = Path(Path("~").expanduser(), ".cache", "qemu", "download")
This creates a Path object but then doesn't take advantage of any of its functionality, calling os. functions still....
scripts/clean_functional_cache.py line 20
Quoted 2 lines
scripts/clean_functional_cache.py new file +2
-0+ +if not os.path.exists(cache_dir):
cache_dir.exists()
scripts/clean_functional_cache.py line 26
Quoted 6 lines
scripts/clean_functional_cache.py new file +6
-0+ print(f"Cache dir {cache_dir} does not exist!", file=sys.stderr) + sys.exit(1) + +os.chdir(cache_dir) + +for file in os.listdir(cache_dir):
for file in cache_dir.iterdir():
scripts/clean_functional_cache.py line 27
Quoted 1 line
scripts/clean_functional_cache.py new file +1
-0+ filename = os.fsdecode(file)
Wouldn't be required since 'file' would be a Path object
scripts/clean_functional_cache.py line 29
Quoted 2 lines
scripts/clean_functional_cache.py new file +2
-0+ # Only consider the files that use a sha256 as filename: + if len(filename) != 64:
if len(file.name) != 64
scripts/clean_functional_cache.py line 34
Quoted 5 lines
scripts/clean_functional_cache.py new file +5
-0+ continue + + try: + with open(filename + ".stamp", "r", encoding='utf-8') as fh: + timestamp = int(fh.read())
timestamp = file.read_text()
scripts/clean_functional_cache.py line 39
Quoted 5 lines
scripts/clean_functional_cache.py new file +5
-0+ except FileNotFoundError: + # Assume it's an old file that was already in the cache before we + # added the code for evicting stale assets. Use the release date + # of QEMU v10.1 as a default timestamp. + timestamp = time.mktime((2025, 8, 26, 0, 0, 0, 0, 0, 0))
The prev patch will make the precache task create the .stamp for all files that are currently in use by the current branch. So the only thing this does is to prevent us deleting cached files that might still be needed by a different branch. There will be few of them, so if we prematurely delete a handful that's not a big deal. If we switch to checking mtime, this except won't even exist.
scripts/clean_functional_cache.py line 46
Quoted 7 lines
scripts/clean_functional_cache.py new file +7
-0+ + age = time.time() - timestamp + + # Delete files older than half of a year (183 days * 24h * 60m * 60s) + if age > 15811200: + print(f"Removing {cache_dir}/{filename}.") + os.chmod(filename, stat.S_IWRITE)
file.chmod(stat.S_IWRITE)
scripts/clean_functional_cache.py line 47
Quoted 1 line
scripts/clean_functional_cache.py new file +1
-0+ os.remove(filename)
file.unlink()
Quoted 15 lines
tests/Makefile.include +1
-0@@ -111,6 +111,7 @@ $(FUNCTIONAL_TARGETS): check-venv .PHONY: check-functional check-functional: check-venv @$(NINJA) precache-functional + @$(PYTHON) $(SRC_PATH)/scripts/clean_functional_cache.py @QEMU_TEST_NO_DOWNLOAD=1 $(MAKE) SPEED=thorough check-func check-func-quick .PHONY: check-func check-func-quickSignature · 1 line
2.51.0
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 1/2] tests/functional: Set current time stamp of assets when using them
In reply to Thomas HuthQuoted 37 lines · Thomas Huth
Quoted 27 lines · Daniel P. Berrangé
Quoted 26 lines · Thomas Huth
From: Thomas Huth <[email protected]>
We are going to remove obsolete assets from the cache, so keep the time stamps of the assets that we use up-to-date to have a way to detect stale assets later.
Signed-off-by Thomas Huth <[email protected]>--- tests/functional/qemu_test/asset.py | 8 ++++++++ 1 file changed, 8 insertions(+)tests/functional/qemu_test/asset.py +1
-0@@ -10,6 +10,7 @@ import os import stat import sys +import time import unittest import urllib.request from time import sleep@@ -113,6 +114,11 @@ def _wait_for_other_download(self, tmp_cache_file): self.log.debug("Time out while waiting for %s!", tmp_cache_file) raiseQuoted 8 lines
tests/functional/qemu_test/asset.py def _wait_for_other_download(self, tmp_cache_file): · line 120
Quoted 4 lines
tests/functional/qemu_test/asset.py +4
-0+ def _save_time_stamp(self): + with open(self.cache_file.with_suffix(".stamp"), 'w', + encoding='utf-8') as fh: + fh.write(f"{int(time.time())}")Rather than creating a parallel timestamp file, it feels like we could just call '
os.utime(self.cache_file)' which will set atime + mtime to the current timestamp, which we can check later withos.stat().
Quoted 6 lines
That was my first idea, too (sorry, I should maybe have mentioned it in the patch description), but it does not work: In the gitlab CI runners, the files get re-initialized from the runners cache each time the functional test job is started, so the atime and mtime is always close to the the current date there --> we would never expire files in the gitlab CI that way.
Oh well, I had assumed gitlab would preserve timestamps when taring/untaring files for the cache :-(
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 2/2] tests: Evict stale files in the functional download cache after a while
In reply to Daniel P. BerrangéQuoted 52 lines · Daniel P. Berrangé
Quoted 51 lines · Thomas Huth
From: Thomas Huth <[email protected]>
The download cache of the functional tests is currently only growing. But sometimes tests get removed or changed to use different assets, thus we should clean up the stale old assets after a while when they are not in use anymore. So add a script that looks at the time stamps of the assets and removes them if they haven't been touched for more than half of a year. Since there might also be some assets around that have been added to the cache before we added the time stamp files, assume a default time stamp that is close to the creation date of this patch, so that we don't delete these files too early.
Signed-off-by Thomas Huth <[email protected]>--- MAINTAINERS | 1 + scripts/clean_functional_cache.py | 47 +++++++++++++++++++++++++++++++ tests/Makefile.include | 1 + 3 files changed, 49 insertions(+)create mode 100755
scripts/clean_functional_cache.pyMAINTAINERS +1
-0@@ -4398,6 +4398,7 @@ M: Thomas Huth <[email protected]> R: Philippe Mathieu-Daudé <[email protected]> R: Daniel P. Berrange <[email protected]> F: docs/devel/testing/functional.rst +F: scripts/clean_functional_cache.py F: tests/functional/qemu_test/ Windows Hosted Continuous Integrationscripts/clean_functional_cache.py new file +13
-0@@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# +# SPDX-License-Identifier: GPL-2.0-or-later +# +"""Delete stale assets from the download cache of the functional tests""" + +import os +import stat +import sys +import time +from pathlib import Path + +
Quoted 8 lines
scripts/clean_functional_cache.py line 18
Quoted 5 lines
scripts/clean_functional_cache.py new file +5
-0+cache_dir_env = os.getenv('QEMU_TEST_CACHE_DIR') +if cache_dir_env: + cache_dir = Path(cache_dir_env, "download") +else: + cache_dir = Path(Path("~").expanduser(), ".cache", "qemu", "download")This creates a Path object but then doesn't take advantage of any of its functionality, calling os. functions still....
Ok, you got me, looks like I'm still a python ignorant after one year of hacking the functional testing framework ;-) Thanks for the hints how to do it better!
Quoted 5 lines
scripts/clean_functional_cache.py line 34
Quoted 3 lines
scripts/clean_functional_cache.py new file +3
-0+ try: + with open(filename + ".stamp", "r", encoding='utf-8') as fh: + timestamp = int(fh.read())timestamp =
file.read_text()
Hmm, but "file" points to the asset, not to the .stamp file, doesn't it?
Quoted 6 lines
scripts/clean_functional_cache.py line 39
Quoted 5 lines
scripts/clean_functional_cache.py new file +5
-0+ except FileNotFoundError: + # Assume it's an old file that was already in the cache before we + # added the code for evicting stale assets. Use the release date + # of QEMU v10.1 as a default timestamp. + timestamp = time.mktime((2025, 8, 26, 0, 0, 0, 0, 0, 0))
Quoted 6 lines
The prev patch will make the precache task create the .stamp for all files that are currently in use by the current branch. So the only thing this does is to prevent us deleting cached files that might still be needed by a different branch. There will be few of them, so if we prematurely delete a handful that's not a big deal. If we switch to checking mtime, this except won't even exist.
When hunting regressions that have been introduced recently, I often have to do bisecting on revisions from the previous 1 or 2 QEMU releases, so I'd prefer keeping the assets of the last few months, even if they have been removed from the master branch in a very recent commit.
Thomas
Re: [PATCH 2/2] tests: Evict stale files in the functional download cache after a while
Quoted 65 lines · Thomas Huth
Quoted 51 lines · Daniel P. Berrangé
Quoted 50 lines · Thomas Huth
From: Thomas Huth <[email protected]>
The download cache of the functional tests is currently only growing. But sometimes tests get removed or changed to use different assets, thus we should clean up the stale old assets after a while when they are not in use anymore. So add a script that looks at the time stamps of the assets and removes them if they haven't been touched for more than half of a year. Since there might also be some assets around that have been added to the cache before we added the time stamp files, assume a default time stamp that is close to the creation date of this patch, so that we don't delete these files too early.
Signed-off-by Thomas Huth <[email protected]>--- MAINTAINERS | 1 + scripts/clean_functional_cache.py | 47 +++++++++++++++++++++++++++++++ tests/Makefile.include | 1 + 3 files changed, 49 insertions(+)create mode 100755
scripts/clean_functional_cache.pyMAINTAINERS +1
-0@@ -4398,6 +4398,7 @@ M: Thomas Huth <[email protected]> R: Philippe Mathieu-Daudé <[email protected]> R: Daniel P. Berrange <[email protected]> F: docs/devel/testing/functional.rst +F: scripts/clean_functional_cache.py F: tests/functional/qemu_test/ Windows Hosted Continuous Integrationscripts/clean_functional_cache.py new file +13
-0@@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# +# SPDX-License-Identifier: GPL-2.0-or-later +# +"""Delete stale assets from the download cache of the functional tests""" + +import os +import stat +import sys +import time +from pathlib import Path + +Quoted 8 lines
scripts/clean_functional_cache.py line 18
Quoted 5 lines
scripts/clean_functional_cache.py new file +5
-0+cache_dir_env = os.getenv('QEMU_TEST_CACHE_DIR') +if cache_dir_env: + cache_dir = Path(cache_dir_env, "download") +else: + cache_dir = Path(Path("~").expanduser(), ".cache", "qemu", "download")This creates a Path object but then doesn't take advantage of any of its functionality, calling os. functions still....
Ok, you got me, looks like I'm still a python ignorant after one year of hacking the functional testing framework ;-) Thanks for the hints how to do it better!
Quoted 7 lines
Quoted 5 lines
scripts/clean_functional_cache.py line 34
Quoted 3 lines
scripts/clean_functional_cache.py new file +3
-0+ try: + with open(filename + ".stamp", "r", encoding='utf-8') as fh: + timestamp = int(fh.read())timestamp =
file.read_text()Hmm, but "file" points to the asset, not to the .stamp file, doesn't it?
Opps, yes, you'll need
file.with_stem(".stamp").read_text()
Quoted 13 lines
Quoted 6 lines
scripts/clean_functional_cache.py line 39
Quoted 5 lines
scripts/clean_functional_cache.py new file +5
-0+ except FileNotFoundError: + # Assume it's an old file that was already in the cache before we + # added the code for evicting stale assets. Use the release date + # of QEMU v10.1 as a default timestamp. + timestamp = time.mktime((2025, 8, 26, 0, 0, 0, 0, 0, 0))Quoted 6 lines
The prev patch will make the precache task create the .stamp for all files that are currently in use by the current branch. So the only thing this does is to prevent us deleting cached files that might still be needed by a different branch. There will be few of them, so if we prematurely delete a handful that's not a big deal. If we switch to checking mtime, this except won't even exist.
Quoted 4 lines
When hunting regressions that have been introduced recently, I often have to do bisecting on revisions from the previous 1 or 2 QEMU releases, so I'd prefer keeping the assets of the last few months, even if they have been removed from the master branch in a very recent commit.
Ok.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|