Repository navigation
[volumecache 10/12] Add disk cache and mtime tests #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
toloudis
merged 2 commits into
feature/agave2-integration
from
feature/volumecache-pr10-disk-cache-tests
Oct 8, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -326,3 +326,222 @@ TEST_CASE("CacheManager respects RAM limit and evicts LRU entries", "[cache]") | |
| REQUIRE(cache.findImage(makeSpec("b")) == nullptr); | ||
| } | ||
| } | ||
|
|
||
| TEST_CASE("CacheManager disk tier round-trips images and respects the disk cap", "[cache][disk]") | ||
| { | ||
| // 4x4x4x1 uint16 image -> 128 raw bytes. On-disk zarr representation is a | ||
| // few KB once chunk + metadata files are accounted for, so a 1 MB cap is | ||
| // comfortably oversized for the round-trip and clear-cache tests, and we | ||
| // size the cap explicitly down to a couple of images for the eviction | ||
| // test. Total disk usage per test is well under a megabyte. | ||
| const std::uint64_t oneImage = imageBytes(4, 4, 4, 1); | ||
|
|
||
| // Fresh temp dir + cache rooted at it for each section. | ||
| TempCacheDir tmp; | ||
| CacheManager cache(tmp.str()); | ||
|
|
||
| SECTION("Enabling the disk tier writes the AGAVE marker file") | ||
| { | ||
| cache.setConfig(diskConfig(oneImage * 4, 1ULL * 1024 * 1024)); | ||
|
|
||
| REQUIRE(std::filesystem::exists(tmp.path() / ".agave-cache-dir")); | ||
| } | ||
|
|
||
| SECTION("Round-trip: store, drop RAM, find reloads from disk with bit-identical data") | ||
| { | ||
| cache.setConfig(diskConfig(oneImage * 4, 1ULL * 1024 * 1024)); | ||
|
|
||
| auto img = makeImageWithPattern(4, 4, 4, 1); | ||
| auto spec = makeSpec("disk_roundtrip"); | ||
| cache.storeImage(spec, img); | ||
|
|
||
| // Drop RAM cache; disk cache survives. | ||
| cache.clearMemoryCache(); | ||
| cache.resetStats(); | ||
|
|
||
| auto found = cache.findImage(spec); | ||
| REQUIRE(found != nullptr); | ||
| // The reloaded image is a fresh instance, not the same shared_ptr. | ||
| REQUIRE(found.get() != img.get()); | ||
|
|
||
| auto stats = cache.getStats(); | ||
| REQUIRE(stats.diskHits == 1); | ||
| REQUIRE(stats.ramHits == 0); | ||
|
|
||
| REQUIRE(found->sizeX() == 4); | ||
| REQUIRE(found->sizeY() == 4); | ||
| REQUIRE(found->sizeZ() == 4); | ||
| REQUIRE(found->sizeC() == 1); | ||
|
|
||
| const std::uint64_t bytes = imageBytes(4, 4, 4, 1); | ||
| REQUIRE(std::memcmp(img->ptr(), found->ptr(), bytes) == 0); | ||
| } | ||
|
|
||
| SECTION("An image larger than maxDiskBytes is not written to disk") | ||
| { | ||
| // Cap is below a single image, so storeToDisk must refuse outright. | ||
| cache.setConfig(diskConfig(oneImage * 4, oneImage / 2)); | ||
|
|
||
| auto spec = makeSpec("too_big_for_disk"); | ||
| cache.storeImage(spec, makeImage(4, 4, 4, 1)); | ||
|
|
||
| // No entry subdirectory should have been created. | ||
| REQUIRE(countSubdirs(tmp.path()) == 0); | ||
|
|
||
| // Confirm that the data is in ram. | ||
| REQUIRE(cache.findImage(spec) != nullptr); | ||
| // Confirm that no disk writes occurred. | ||
| REQUIRE(cache.getStats().diskWrites == 0); | ||
|
|
||
| // Drop RAM to force a lookup from disk cache; with no entry on disk we | ||
| // should get a miss. | ||
| cache.clearMemoryCache(); | ||
| REQUIRE(cache.findImage(spec) == nullptr); | ||
| } | ||
|
|
||
| SECTION("Disk eviction removes the oldest entry to stay under the cap") | ||
| { | ||
| // Cap large enough for two entries' raw byte estimate but not three. | ||
| cache.setConfig(diskConfig(oneImage * 4, oneImage * 2)); | ||
|
|
||
| cache.storeImage(makeSpec("disk_a"), makeImage(4, 4, 4, 1)); | ||
| // Sleep briefly so lastAccess timestamps are distinct on fast disks. | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(5)); | ||
| cache.storeImage(makeSpec("disk_b"), makeImage(4, 4, 4, 1)); | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(5)); | ||
| cache.storeImage(makeSpec("disk_c"), makeImage(4, 4, 4, 1)); | ||
|
|
||
| // Drop RAM so finds have to go through the disk tier. | ||
| cache.clearMemoryCache(); | ||
|
|
||
| REQUIRE(cache.findImage(makeSpec("disk_a")) == nullptr); | ||
| REQUIRE(cache.findImage(makeSpec("disk_b")) != nullptr); | ||
| REQUIRE(cache.findImage(makeSpec("disk_c")) != nullptr); | ||
|
|
||
| // After eviction there should be at most two entry subdirectories on | ||
| // disk. | ||
| REQUIRE(countSubdirs(tmp.path()) <= 2); | ||
| } | ||
|
|
||
| SECTION("clearDiskCache removes entry subdirectories but keeps the marker") | ||
| { | ||
| cache.setConfig(diskConfig(oneImage * 4, 1ULL * 1024 * 1024)); | ||
|
|
||
| cache.storeImage(makeSpec("clear_a"), makeImage(4, 4, 4, 1)); | ||
| cache.storeImage(makeSpec("clear_b"), makeImage(4, 4, 4, 1)); | ||
| REQUIRE(countSubdirs(tmp.path()) >= 2); | ||
|
|
||
| cache.clearDiskCache(); | ||
|
|
||
| REQUIRE(countSubdirs(tmp.path()) == 0); | ||
| REQUIRE(std::filesystem::exists(tmp.path() / ".agave-cache-dir")); | ||
| } | ||
|
|
||
| SECTION("clearDiskCache refuses to touch a directory without the AGAVE marker") | ||
| { | ||
| cache.setConfig(diskConfig(oneImage * 4, 1ULL * 1024 * 1024)); | ||
|
|
||
| cache.storeImage(makeSpec("guarded_a"), makeImage(4, 4, 4, 1)); | ||
| int subdirsBefore = countSubdirs(tmp.path()); | ||
| REQUIRE(subdirsBefore >= 1); | ||
|
|
||
| // Simulate a directory that doesn't belong to AGAVE by removing the | ||
| // marker file. clearDiskCache must refuse. | ||
| std::error_code ec; | ||
| std::filesystem::remove(tmp.path() / ".agave-cache-dir", ec); | ||
| REQUIRE_FALSE(ec); | ||
|
|
||
| cache.clearDiskCache(); | ||
|
|
||
| REQUIRE(countSubdirs(tmp.path()) == subdirsBefore); | ||
| } | ||
|
|
||
| SECTION("A fresh cache instance rebuilds the disk index from an existing cache dir") | ||
| { | ||
| cache.setConfig(diskConfig(oneImage * 4, 1ULL * 1024 * 1024)); | ||
| cache.storeImage(makeSpec("persistent"), makeImage(4, 4, 4, 1)); | ||
|
|
||
| // Simulate a session restart: a brand-new CacheManager rooted at the same | ||
| // directory must rebuild its disk index on first use and serve the | ||
| // persisted entry from disk. | ||
| CacheManager restarted(tmp.str()); | ||
| restarted.setConfig(diskConfig(oneImage * 4, 1ULL * 1024 * 1024)); | ||
|
|
||
| auto found = restarted.findImage(makeSpec("persistent")); | ||
| REQUIRE(found != nullptr); | ||
| REQUIRE(restarted.getStats().diskHits == 1); | ||
| } | ||
| } | ||
|
|
||
| TEST_CASE("CacheManager cache directory is fixed at construction", "[cache][disk]") | ||
| { | ||
| const std::uint64_t oneImage = imageBytes(4, 4, 4, 1); | ||
|
|
||
| SECTION("getCacheDirectory reflects the constructed path") | ||
| { | ||
| TempCacheDir tmp; | ||
| CacheManager cache(tmp.str()); | ||
| REQUIRE(cache.getCacheDirectory() == tmp.str()); | ||
| } | ||
|
|
||
| SECTION("A cache constructed with no directory is RAM-only") | ||
| { | ||
| CacheManager cache; // empty cache directory | ||
| REQUIRE(cache.getCacheDirectory().empty()); | ||
|
|
||
| // Even with a disk-enabled config, an empty root keeps the disk tier inert. | ||
| cache.setConfig(diskConfig(oneImage * 4, 1ULL * 1024 * 1024)); | ||
|
|
||
| auto spec = makeSpec("no_dir"); | ||
| cache.storeImage(spec, makeImage(4, 4, 4, 1)); | ||
|
|
||
| // RAM still serves it within the session... | ||
| REQUIRE(cache.findImage(spec) != nullptr); | ||
|
|
||
| // ...but nothing was persisted to disk, so once RAM is dropped it misses. | ||
| cache.clearMemoryCache(); | ||
| REQUIRE(cache.findImage(spec) == nullptr); | ||
| } | ||
| } | ||
|
|
||
| TEST_CASE("CacheManager invalidates entries when the source file mtime changes", "[cache][mtime]") | ||
| { | ||
| // The purpose of this test is to verify that the CacheManager uses a different cache key | ||
| // when the source file's modification time changes. | ||
| // From a user perspective, this represents loading an image file that has been modified | ||
| // since the last time it was loaded and cached in AGAVE. | ||
| // The old entry will just sit there and never receive cache hits until eventually it is evicted. | ||
|
|
||
| CacheManager cache; | ||
|
|
||
| // Use a real file on disk so the cache key picks up its mtime via | ||
| // std::filesystem::last_write_time. We bump the file's mtime explicitly | ||
| // (rather than sleeping and rewriting) so the test is deterministic on | ||
| // filesystems with coarse mtime resolution. | ||
| static std::atomic<int> sCounter{ 0 }; | ||
| std::filesystem::path srcFile = std::filesystem::temp_directory_path() / | ||
| ("agave_cache_mtime_test_" + std::to_string(sCounter.fetch_add(1)) + ".bin"); | ||
| { | ||
| std::ofstream out(srcFile.string()); | ||
| out << "initial content"; | ||
| } | ||
|
|
||
| cache.setConfig(ramOnlyConfig(imageBytes(4, 4, 4, 1) * 4)); | ||
|
|
||
| LoadSpec spec; | ||
| spec.filepath = srcFile.string(); | ||
| cache.storeImage(spec, makeImage(4, 4, 4, 1)); | ||
| REQUIRE(cache.findImage(spec) != nullptr); | ||
|
|
||
| // Bump the file's last_write_time well into the future; subsequent | ||
| // makeKey calls now produce a different key and must miss. | ||
| std::error_code ec; | ||
| auto futureTime = std::filesystem::last_write_time(srcFile, ec) + std::chrono::seconds(60); | ||
| REQUIRE_FALSE(ec); | ||
| std::filesystem::last_write_time(srcFile, futureTime, ec); | ||
| REQUIRE_FALSE(ec); | ||
|
|
||
| REQUIRE(cache.findImage(spec) == nullptr); | ||
|
|
||
| std::filesystem::remove(srcFile, ec); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: Check that diskHits is 0? |
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Should this validate that the image was loaded correctly?