From ac918801165b60c70cbb7d15399a760ec96bbcd4 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 18 Jul 2021 12:44:39 +0200 Subject: [PATCH] Store secondary storage hits in primary storage --- src/storage/Storage.cpp | 11 +++++++++++ test/suites/secondary_file.bash | 16 +++++++++------- test/suites/secondary_http.bash | 6 ++---- test/suites/secondary_redis.bash | 4 ++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/storage/Storage.cpp b/src/storage/Storage.cpp index 4f5203a56..90dd36eee 100644 --- a/src/storage/Storage.cpp +++ b/src/storage/Storage.cpp @@ -186,6 +186,17 @@ Storage::get(const Digest& key, const core::CacheEntryType type) } catch (const Error& e) { throw Fatal("Error writing to {}: {}", tmp_file.path, e.what()); } + + m_primary_storage.put(key, type, [&](const std::string& path) { + try { + Util::copy_file(tmp_file.path, path); + } catch (const Error& e) { + LOG("Failed to copy {} to {}: {}", tmp_file.path, path, e.what()); + // Don't indicate failure since get from primary storage was OK. + } + return true; + }); + return tmp_file.path; } diff --git a/test/suites/secondary_file.bash b/test/suites/secondary_file.bash index 57a79b8e9..90cf8adc8 100644 --- a/test/suites/secondary_file.bash +++ b/test/suites/secondary_file.bash @@ -1,6 +1,6 @@ SUITE_secondary_file_SETUP() { unset CCACHE_NODIRECT - export CCACHE_SECONDARY_STORAGE="file://$PWD/secondary" + export CCACHE_SECONDARY_STORAGE="file:$PWD/secondary" generate_code 1 test.c } @@ -29,8 +29,7 @@ SUITE_secondary_file() { $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 2 expect_stat 'cache miss' 1 - expect_stat 'files in cache' 0 - expect_stat 'files in cache' 0 + expect_stat 'files in cache' 2 # fetched from secondary expect_file_count 3 '*' secondary # CACHEDIR.TAG + result + manifest # ------------------------------------------------------------------------- @@ -54,17 +53,20 @@ SUITE_secondary_file() { $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 1 expect_stat 'cache miss' 1 - expect_stat 'files in cache' 0 + expect_stat 'files in cache' 2 # fetched from secondary expect_file_count 3 '*' secondary # CACHEDIR.TAG + result + manifest expect_file_count 3 '*' secondary_2 # CACHEDIR.TAG + result + manifest + $CCACHE -C >/dev/null + expect_stat 'files in cache' 0 + rm -r secondary/?? expect_file_count 1 '*' secondary # CACHEDIR.TAG $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 2 expect_stat 'cache miss' 1 - expect_stat 'files in cache' 0 + expect_stat 'files in cache' 2 # fetched from secondary_2 expect_file_count 1 '*' secondary # CACHEDIR.TAG expect_file_count 3 '*' secondary_2 # CACHEDIR.TAG + result + manifest @@ -86,14 +88,14 @@ SUITE_secondary_file() { $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 1 expect_stat 'cache miss' 1 - expect_stat 'files in cache' 0 + expect_stat 'files in cache' 2 # fetched from secondary expect_file_count 3 '*' secondary # CACHEDIR.TAG + result + manifest echo 'int x;' >> test.c $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 1 expect_stat 'cache miss' 2 - expect_stat 'files in cache' 2 + expect_stat 'files in cache' 4 expect_file_count 3 '*' secondary # CACHEDIR.TAG + result + manifest # ------------------------------------------------------------------------- diff --git a/test/suites/secondary_http.bash b/test/suites/secondary_http.bash index b99874882..f9974f417 100644 --- a/test/suites/secondary_http.bash +++ b/test/suites/secondary_http.bash @@ -64,8 +64,7 @@ SUITE_secondary_http() { $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 2 expect_stat 'cache miss' 1 - expect_stat 'files in cache' 0 - expect_stat 'files in cache' 0 + expect_stat 'files in cache' 2 # fetched from secondary expect_file_count 2 '*' secondary # result + manifest # ------------------------------------------------------------------------- @@ -134,8 +133,7 @@ SUITE_secondary_http() { $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 2 expect_stat 'cache miss' 1 - expect_stat 'files in cache' 0 - expect_stat 'files in cache' 0 + expect_stat 'files in cache' 2 # fetched from secondary expect_file_count 2 '*' secondary # result + manifest fi } diff --git a/test/suites/secondary_redis.bash b/test/suites/secondary_redis.bash index e2e107ee3..1894412f4 100644 --- a/test/suites/secondary_redis.bash +++ b/test/suites/secondary_redis.bash @@ -76,7 +76,7 @@ SUITE_secondary_redis() { $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 2 expect_stat 'cache miss' 1 - expect_stat 'files in cache' 0 + expect_stat 'files in cache' 2 # fetched from secondary expect_number_of_redis_cache_entries 2 "$redis_url" # result + manifest # ------------------------------------------------------------------------- @@ -109,6 +109,6 @@ SUITE_secondary_redis() { $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 2 expect_stat 'cache miss' 1 - expect_stat 'files in cache' 0 + expect_stat 'files in cache' 2 # fetched from secondary expect_number_of_redis_cache_entries 2 "$redis_url" # result + manifest } -- 2.47.3