From 8adc114e61f96c930fce0ab957476034fa6b9ae0 Mon Sep 17 00:00:00 2001 From: Sam De Roeck Date: Sat, 4 May 2024 19:31:36 -0700 Subject: [PATCH] feat(binary-cache): Add write-back support to binary cache --- include/vcpkg/base/expected.h | 8 +- include/vcpkg/base/optional.h | 4 +- include/vcpkg/base/util.h | 13 + include/vcpkg/binarycaching.h | 83 +++-- src/vcpkg-test/binarycaching.cpp | 2 + src/vcpkg-test/configparser.cpp | 140 +++++--- src/vcpkg/binarycaching.cpp | 595 ++++++++++++++++++++----------- src/vcpkg/commands.install.cpp | 6 +- 8 files changed, 558 insertions(+), 293 deletions(-) diff --git a/include/vcpkg/base/expected.h b/include/vcpkg/base/expected.h index 49ced29d1c..d58c7dfaa9 100644 --- a/include/vcpkg/base/expected.h +++ b/include/vcpkg/base/expected.h @@ -102,8 +102,8 @@ namespace vcpkg { } - ExpectedT(const ExpectedT& other) noexcept( - std::is_nothrow_copy_constructible_v&& std::is_nothrow_copy_constructible_v>) + ExpectedT(const ExpectedT& other) noexcept(std::is_nothrow_copy_constructible_v && + std::is_nothrow_copy_constructible_v>) : value_is_error(other.value_is_error) { if (value_is_error) @@ -116,8 +116,8 @@ namespace vcpkg } } - ExpectedT(ExpectedT&& other) noexcept( - std::is_nothrow_move_constructible_v&& std::is_nothrow_move_constructible_v>) + ExpectedT(ExpectedT&& other) noexcept(std::is_nothrow_move_constructible_v && + std::is_nothrow_move_constructible_v>) : value_is_error(other.value_is_error) { if (value_is_error) diff --git a/include/vcpkg/base/optional.h b/include/vcpkg/base/optional.h index 50a186866d..12c192957c 100644 --- a/include/vcpkg/base/optional.h +++ b/include/vcpkg/base/optional.h @@ -76,8 +76,8 @@ namespace vcpkg } } - OptionalStorage& operator=(const OptionalStorage& o) noexcept( - std::is_nothrow_copy_constructible_v&& std::is_nothrow_copy_assignable_v) + OptionalStorage& operator=(const OptionalStorage& o) noexcept(std::is_nothrow_copy_constructible_v && + std::is_nothrow_copy_assignable_v) { if (m_is_present && o.m_is_present) { diff --git a/include/vcpkg/base/util.h b/include/vcpkg/base/util.h index e619df7ee8..94c2af29c5 100644 --- a/include/vcpkg/base/util.h +++ b/include/vcpkg/base/util.h @@ -44,6 +44,19 @@ namespace vcpkg::Util return false; } + template + std::vector> filtered_copy(const Vec& container, const Filter&& filter) + { + std::vector> ret; + for (auto&& item : container) + { + if (filter(item)) + { + ret.push_back(item); + } + } + return ret; + } template bool contains(const Vec& container, const Key& item) { diff --git a/include/vcpkg/binarycaching.h b/include/vcpkg/binarycaching.h index 5c70cf04d1..d572ca4b73 100644 --- a/include/vcpkg/binarycaching.h +++ b/include/vcpkg/binarycaching.h @@ -25,18 +25,26 @@ namespace vcpkg { + /// Unique identifier for a provider + using ProviderId = size_t; + struct CacheStatus { + using ReaderProviders = std::vector; + bool should_attempt_precheck(const IReadBinaryProvider* sender) const noexcept; bool should_attempt_restore(const IReadBinaryProvider* sender) const noexcept; + bool should_attempt_write_back(ProviderId provider_id) const noexcept; bool is_unavailable(const IReadBinaryProvider* sender) const noexcept; const IReadBinaryProvider* get_available_provider() const noexcept; + ReaderProviders& get_unavailable_providers() noexcept; bool is_restored() const noexcept; void mark_unavailable(const IReadBinaryProvider* sender); void mark_available(const IReadBinaryProvider* sender) noexcept; void mark_restored() noexcept; + void mark_written_back(ProviderId provider_id) noexcept; private: CacheStatusState m_status = CacheStatusState::unknown; @@ -75,7 +83,9 @@ namespace vcpkg /// Called upon a successful build of `action` to store those contents in the binary cache. /// returns the number of successful uploads - virtual size_t push_success(const BinaryPackageWriteInfo& request, MessageSink& msg_sink) = 0; + virtual size_t push_success(const BinaryPackageWriteInfo& request, + MessageSink& msg_sink, + CacheStatus& cache_status) = 0; virtual bool needs_nuspec_data() const = 0; virtual bool needs_zip_file() const = 0; @@ -103,6 +113,11 @@ namespace vcpkg virtual LocalizedString restored_message(size_t count, std::chrono::high_resolution_clock::duration elapsed) const = 0; + + /// Unique identifier for this provider. + /// + /// Used by the cache to exclude cache providers during the write-back phase. + virtual ProviderId id() const = 0; }; struct UrlTemplate @@ -114,6 +129,10 @@ namespace vcpkg std::string instantiate_variables(const BinaryPackageReadInfo& info) const; }; + struct GithubActionsInfo + { + }; + struct NuGetRepoInfo { std::string repo; @@ -121,37 +140,51 @@ namespace vcpkg std::string commit; }; + enum class CacheType + { + Read, + Write, + ReadWrite + }; + + template + struct CacheProvider + { + ProviderId id; + T source; + CacheType cache_type; + + [[nodiscard]] constexpr bool is_read() const noexcept + { + return cache_type == CacheType::Read || cache_type == CacheType::ReadWrite; + } + + [[nodiscard]] constexpr bool is_write() const noexcept + { + return cache_type == CacheType::Write || cache_type == CacheType::ReadWrite; + } + }; + + template + using ProviderList = std::vector>; + struct BinaryConfigParserState { + ProviderId provider_count = 0; bool nuget_interactive = false; std::set binary_cache_providers; std::string nugettimeout = "100"; - std::vector archives_to_read; - std::vector archives_to_write; - - std::vector url_templates_to_get; - std::vector url_templates_to_put; - - std::vector gcs_read_prefixes; - std::vector gcs_write_prefixes; - - std::vector aws_read_prefixes; - std::vector aws_write_prefixes; + ProviderList archives; + ProviderList url_templates; + ProviderList gcs_prefixes; + ProviderList aws_prefixes; bool aws_no_sign_request = false; - - std::vector cos_read_prefixes; - std::vector cos_write_prefixes; - - bool gha_write = false; - bool gha_read = false; - - std::vector sources_to_read; - std::vector sources_to_write; - - std::vector configs_to_read; - std::vector configs_to_write; + ProviderList cos_prefixes; + Optional> gha; + ProviderList sources; + ProviderList configs; std::vector secrets; @@ -182,7 +215,7 @@ namespace vcpkg /// executing `actions`. void fetch(View actions); - bool is_restored(const InstallPlanAction& ipa) const; + Optional cache_status(const InstallPlanAction& ipa) const; /// Checks whether the `actions` are present in the cache, without restoring them. Used by CI to determine /// missing packages. diff --git a/src/vcpkg-test/binarycaching.cpp b/src/vcpkg-test/binarycaching.cpp index 64800c976a..f5c180f79f 100644 --- a/src/vcpkg-test/binarycaching.cpp +++ b/src/vcpkg-test/binarycaching.cpp @@ -36,6 +36,8 @@ struct KnowNothingBinaryProvider : IReadBinaryProvider { return LocalizedString::from_raw("Nothing"); } + + ProviderId id() const override { return 1; } }; TEST_CASE ("CacheStatus operations", "[BinaryCache]") diff --git a/src/vcpkg-test/configparser.cpp b/src/vcpkg-test/configparser.cpp index 05cc8470ee..29f11055bf 100644 --- a/src/vcpkg-test/configparser.cpp +++ b/src/vcpkg-test/configparser.cpp @@ -14,19 +14,39 @@ using namespace vcpkg; namespace { + template + constexpr size_t count_read_providers(const ProviderList& providers) + { + return std::count_if( + providers.cbegin(), providers.cend(), [](const CacheProvider& provider) { return provider.is_read(); }); + } + + template + constexpr size_t count_write_providers(const ProviderList& providers) + { + return std::count_if( + providers.cbegin(), providers.cend(), [](const CacheProvider& provider) { return provider.is_write(); }); + } + + template + constexpr bool has_provider_source(const ProviderList& providers, F&& f) + { + return providers.cend() != std::find_if(providers.cbegin(), providers.cend(), f); + } + void validate_readonly_url(const BinaryConfigParserState& state, StringView url) { auto extended_url = url.to_string() + "/{sha}.zip?sas"; - CHECK(state.url_templates_to_put.empty()); - CHECK(state.url_templates_to_get.size() == 1); - CHECK(state.url_templates_to_get.front().url_template == extended_url); + CHECK(count_write_providers(state.url_templates) == 0); + CHECK(count_read_providers(state.url_templates) == 1); + CHECK(state.url_templates.front().source.url_template == extended_url); } void validate_readonly_sources(const BinaryConfigParserState& state, StringView sources) { - CHECK(state.sources_to_write.empty()); - CHECK(state.sources_to_read.size() == 1); - CHECK(state.sources_to_read.front() == sources); + CHECK(count_write_providers(state.sources) == 0); + CHECK(count_read_providers(state.sources) == 1); + CHECK(state.sources.front().source == sources); } } @@ -71,23 +91,25 @@ TEST_CASE ("BinaryConfigParser files provider", "[binaryconfigparser]") auto state = parsed.value_or_exit(VCPKG_LINE_INFO); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"files"}}); - REQUIRE(!state.archives_to_read.empty()); - REQUIRE(!Util::Vectors::contains(state.archives_to_write, ABSOLUTE_PATH)); + REQUIRE(count_read_providers(state.archives) > 0); + REQUIRE(!has_provider_source(state.archives, [&](const CacheProvider& provider) { + return provider.is_write() && provider.source == ABSOLUTE_PATH; + })); } { auto parsed = parse_binary_provider_configs("files," ABSOLUTE_PATH ",write", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"files"}}); - REQUIRE(!state.archives_to_write.empty()); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("files," ABSOLUTE_PATH ",readwrite", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"files"}}); - REQUIRE(!state.archives_to_write.empty()); - REQUIRE(!state.archives_to_read.empty()); + REQUIRE(count_read_providers(state.archives) > 0); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("files," ABSOLUTE_PATH ",readwrite,extra", {}); @@ -134,13 +156,14 @@ TEST_CASE ("BinaryConfigParser nuget source provider", "[binaryconfigparser]") auto parsed = parse_binary_provider_configs("nuget," ABSOLUTE_PATH ",readwrite", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - CHECK(state.sources_to_read.size() == 1); - CHECK(state.sources_to_write.size() == 1); - CHECK(state.sources_to_read.front() == state.sources_to_write.front()); - CHECK(state.sources_to_read.front() == ABSOLUTE_PATH); + CHECK(count_read_providers(state.sources) == 1); + CHECK(count_write_providers(state.sources) == 1); + CHECK(state.sources.front().cache_type == CacheType::ReadWrite); + CHECK(state.sources.front().source == ABSOLUTE_PATH); + CHECK(state.sources.size() == 1); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"nuget"}}); - REQUIRE(!state.archives_to_write.empty()); - REQUIRE(!state.archives_to_read.empty()); + REQUIRE(count_read_providers(state.archives) > 0); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("nuget," ABSOLUTE_PATH ",readwrite,extra", {}); @@ -217,33 +240,33 @@ TEST_CASE ("BinaryConfigParser nuget config provider", "[binaryconfigparser]") auto parsed = parse_binary_provider_configs("nugetconfig," ABSOLUTE_PATH ",read", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - CHECK(state.configs_to_write.empty()); - CHECK(state.configs_to_read.size() == 1); - CHECK(state.configs_to_read.front() == ABSOLUTE_PATH); + CHECK(count_read_providers(state.configs) == 1); + CHECK(count_write_providers(state.configs) == 0); + CHECK(state.configs.front().source == ABSOLUTE_PATH); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"nuget"}}); - REQUIRE(!state.archives_to_read.empty()); + REQUIRE(count_read_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("nugetconfig," ABSOLUTE_PATH ",write", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - CHECK(state.configs_to_read.empty()); - CHECK(state.configs_to_write.size() == 1); - CHECK(state.configs_to_write.front() == ABSOLUTE_PATH); + CHECK(count_read_providers(state.configs) == 0); + CHECK(count_write_providers(state.configs) == 1); + CHECK(state.configs.front().source == ABSOLUTE_PATH); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"nuget"}}); - REQUIRE(!state.archives_to_write.empty()); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("nugetconfig," ABSOLUTE_PATH ",readwrite", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - CHECK(state.configs_to_read.size() == 1); - CHECK(state.configs_to_write.size() == 1); - CHECK(state.configs_to_read.front() == state.configs_to_write.front()); - CHECK(state.configs_to_read.front() == ABSOLUTE_PATH); + CHECK(count_read_providers(state.configs) == 1); + CHECK(count_write_providers(state.configs) == 1); + CHECK(state.configs.front().cache_type == CacheType::ReadWrite); + CHECK(state.configs.front().source == ABSOLUTE_PATH); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"nuget"}}); - REQUIRE(!state.archives_to_write.empty()); - REQUIRE(!state.archives_to_read.empty()); + REQUIRE(count_read_providers(state.archives) > 0); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("nugetconfig," ABSOLUTE_PATH ",readwrite,extra", {}); @@ -268,18 +291,18 @@ TEST_CASE ("BinaryConfigParser default provider", "[binaryconfigparser]") { auto parsed = parse_binary_provider_configs("default,read", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - REQUIRE(!state.archives_to_read.empty()); + REQUIRE(count_read_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("default,readwrite", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - REQUIRE(!state.archives_to_read.empty()); - REQUIRE(!state.archives_to_write.empty()); + REQUIRE(count_read_providers(state.archives) > 0); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("default,write", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - REQUIRE(!state.archives_to_write.empty()); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("default,read,extra", {}); @@ -451,31 +474,30 @@ TEST_CASE ("BinaryConfigParser azblob provider", "[binaryconfigparser]") REQUIRE(state.binary_cache_providers == std::set{{"azblob"}, {"default"}}); validate_readonly_url(state, "https://azure/container"); REQUIRE(state.secrets == std::vector{"sas"}); - REQUIRE(!state.archives_to_read.empty()); + REQUIRE(count_read_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("x-azblob,https://azure/container,sas,write", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); REQUIRE(state.binary_cache_providers == std::set{{"azblob"}, {"default"}}); - CHECK(state.url_templates_to_get.empty()); - CHECK(state.url_templates_to_put.size() == 1); - CHECK(state.url_templates_to_put.front().url_template == "https://azure/container/{sha}.zip?sas"); + CHECK(count_read_providers(state.url_templates) == 0); + CHECK(count_write_providers(state.url_templates) == 1); + CHECK(state.url_templates.front().source.url_template == "https://azure/container/{sha}.zip?sas"); REQUIRE(state.secrets == std::vector{"sas"}); - REQUIRE(!state.archives_to_write.empty()); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("x-azblob,https://azure/container,sas,readwrite", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); REQUIRE(state.binary_cache_providers == std::set{{"azblob"}, {"default"}}); - CHECK(state.url_templates_to_get.size() == 1); - CHECK(state.url_templates_to_get.front().url_template == "https://azure/container/{sha}.zip?sas"); - CHECK(state.url_templates_to_put.size() == 1); - CHECK(state.url_templates_to_put.front().url_template == "https://azure/container/{sha}.zip?sas"); + CHECK(count_read_providers(state.url_templates) == 1); + CHECK(count_write_providers(state.url_templates) == 1); + CHECK(state.url_templates.front().source.url_template == "https://azure/container/{sha}.zip?sas"); REQUIRE(state.secrets == std::vector{"sas"}); - REQUIRE(!state.archives_to_read.empty()); - REQUIRE(!state.archives_to_write.empty()); + REQUIRE(count_read_providers(state.archives) > 0); + REQUIRE(count_write_providers(state.archives) > 0); } } @@ -485,14 +507,16 @@ TEST_CASE ("BinaryConfigParser GCS provider", "[binaryconfigparser]") auto parsed = parse_binary_provider_configs("x-gcs,gs://my-bucket/", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - REQUIRE(state.gcs_read_prefixes == std::vector{"gs://my-bucket/"}); + REQUIRE(count_read_providers(state.gcs_prefixes) == 1); + REQUIRE(state.gcs_prefixes.front().source == "gs://my-bucket/"); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"gcs"}}); } { auto parsed = parse_binary_provider_configs("x-gcs,gs://my-bucket/my-folder", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); - REQUIRE(state.gcs_read_prefixes == std::vector{"gs://my-bucket/my-folder/"}); + REQUIRE(count_read_providers(state.gcs_prefixes) == 1); + REQUIRE(state.gcs_prefixes.front().source == "gs://my-bucket/my-folder/"); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"gcs"}}); } { @@ -508,26 +532,30 @@ TEST_CASE ("BinaryConfigParser GCS provider", "[binaryconfigparser]") auto state = parsed.value_or_exit(VCPKG_LINE_INFO); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"gcs"}}); - REQUIRE(state.gcs_read_prefixes == std::vector{"gs://my-bucket/my-folder/"}); - REQUIRE(!state.archives_to_read.empty()); + REQUIRE(count_read_providers(state.gcs_prefixes) == 1); + REQUIRE(state.gcs_prefixes.front().source == "gs://my-bucket/my-folder/"); + REQUIRE(count_read_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("x-gcs,gs://my-bucket/my-folder,write", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"gcs"}}); - REQUIRE(state.gcs_write_prefixes == std::vector{"gs://my-bucket/my-folder/"}); - REQUIRE(!state.archives_to_write.empty()); + REQUIRE(count_write_providers(state.gcs_prefixes) == 1); + REQUIRE(state.gcs_prefixes.front().source == "gs://my-bucket/my-folder/"); + REQUIRE(count_write_providers(state.archives) > 0); } { auto parsed = parse_binary_provider_configs("x-gcs,gs://my-bucket/my-folder,readwrite", {}); auto state = parsed.value_or_exit(VCPKG_LINE_INFO); REQUIRE(state.binary_cache_providers == std::set{{"default"}, {"gcs"}}); - REQUIRE(state.gcs_write_prefixes == std::vector{"gs://my-bucket/my-folder/"}); - REQUIRE(state.gcs_read_prefixes == std::vector{"gs://my-bucket/my-folder/"}); - REQUIRE(!state.archives_to_write.empty()); - REQUIRE(!state.archives_to_read.empty()); + REQUIRE(count_read_providers(state.gcs_prefixes) == 1); + REQUIRE(count_write_providers(state.gcs_prefixes) == 1); + REQUIRE(state.gcs_prefixes.size() == 1); + REQUIRE(state.gcs_prefixes.front().source == "gs://my-bucket/my-folder/"); + REQUIRE(count_read_providers(state.archives) > 0); + REQUIRE(count_write_providers(state.archives) > 0); } } diff --git a/src/vcpkg/binarycaching.cpp b/src/vcpkg/binarycaching.cpp index ab1b2b2420..6d2296f345 100644 --- a/src/vcpkg/binarycaching.cpp +++ b/src/vcpkg/binarycaching.cpp @@ -39,16 +39,14 @@ namespace void parse_segments(std::vector>& out_segments); std::vector>> parse_all_segments(); - template - void handle_readwrite(std::vector& read, - std::vector& write, - T&& t, - const std::vector>& segments, - size_t segment_idx) + template + void parse_cache_type(const std::vector>& segments, + const size_t segment_idx, + F&& f) { if (segment_idx >= segments.size()) { - read.push_back(std::move(t)); + f(CacheType::Read); return; } @@ -56,56 +54,71 @@ namespace if (mode == "read") { - read.push_back(std::move(t)); + return f(CacheType::Read); } - else if (mode == "write") + if (mode == "write") { - write.push_back(std::move(t)); + return f(CacheType::Write); } - else if (mode == "readwrite") + if (mode == "readwrite") { - read.push_back(t); - write.push_back(std::move(t)); - } - else - { - return add_error(msg::format(msgExpectedReadWriteReadWrite), segments[segment_idx].first); + return f(CacheType::ReadWrite); } + + return add_error(msg::format(msgExpectedReadWriteReadWrite), segments[segment_idx].first); } - void handle_readwrite(bool& read, - bool& write, + template + void handle_readwrite(std::vector& read, + std::vector& write, + T&& t, const std::vector>& segments, - size_t segment_idx) + const size_t segment_idx) { - if (segment_idx >= segments.size()) - { - read = true; - return; - } - - auto& mode = segments[segment_idx].second; + parse_cache_type(segments, segment_idx, [&](const CacheType cache_type) { + switch (cache_type) + { + case CacheType::Read: read.push_back(std::forward(t)); break; + case CacheType::Write: write.push_back(std::forward(t)); break; + case CacheType::ReadWrite: + read.push_back(t); + write.push_back(std::forward(t)); + break; + } + }); + } - if (mode == "read") - { - read = true; - } - else if (mode == "write") - { - write = true; - } - else if (mode == "readwrite") - { - read = true; - write = true; - } - else - { - return add_error(msg::format(msgExpectedReadWriteReadWrite), segments[segment_idx].first); - } + template + void handle_readwrite(ProviderList& providers, + ProviderId& next_provider_id, + T&& t, + const std::vector>& segments, + const size_t segment_idx) + { + parse_cache_type(segments, segment_idx, [&](const CacheType cache_type) { + providers.push_back(CacheProvider{next_provider_id++, std::forward(t), cache_type}); + }); } }; + template + constexpr bool is_read_provider(const CacheProvider& provider) + { + return provider.is_read(); + } + + template + constexpr bool is_write_provider(const CacheProvider& provider) + { + return provider.is_write(); + } + + template + constexpr bool has_read_provider(const ProviderList& providers) + { + return providers.cend() != std::find_if(providers.cbegin(), providers.cend(), is_read_provider); + } + void ConfigSegmentsParser::parse_segments(std::vector>& segments) { for (;;) @@ -213,17 +226,26 @@ namespace struct FilesWriteBinaryProvider : IWriteBinaryProvider { - FilesWriteBinaryProvider(const Filesystem& fs, std::vector&& dirs) : m_fs(fs), m_dirs(std::move(dirs)) { } + FilesWriteBinaryProvider(const Filesystem& fs, ProviderList&& dirs) + : m_fs(fs), m_dirs(std::move(dirs)) { } - size_t push_success(const BinaryPackageWriteInfo& request, MessageSink& msg_sink) override + size_t push_success(const BinaryPackageWriteInfo& request, + MessageSink& msg_sink, + CacheStatus& cache_status) override { const auto& zip_path = request.zip_path.value_or_exit(VCPKG_LINE_INFO); const auto archive_subpath = files_archive_subpath(request.package_abi); size_t count_stored = 0; - for (const auto& archives_root_dir : m_dirs) + for (const auto& [provider_id, source, _] : m_dirs) { - const auto archive_path = archives_root_dir / archive_subpath; + if (!cache_status.should_attempt_write_back(provider_id)) + { + /// We already have this in the cache, so skip it + continue; + } + + const auto archive_path = source / archive_subpath; std::error_code ec; m_fs.create_directories(archive_path.parent_path(), IgnoreErrors{}); m_fs.copy_file(zip_path, archive_path, CopyOptions::overwrite_existing, ec); @@ -237,6 +259,7 @@ namespace else { count_stored++; + cache_status.mark_written_back(provider_id); } } return count_stored; @@ -247,7 +270,7 @@ namespace private: const Filesystem& m_fs; - std::vector m_dirs; + ProviderList m_dirs; }; enum class RemoveWhen @@ -270,7 +293,10 @@ namespace // - IReadBinaryProvider::precheck() struct ZipReadBinaryProvider : IReadBinaryProvider { - ZipReadBinaryProvider(ZipTool zip, const Filesystem& fs) : m_zip(std::move(zip)), m_fs(fs) { } + ZipReadBinaryProvider(ProviderId provider_id, ZipTool zip, const Filesystem& fs) + : m_provider_id(provider_id), m_zip(std::move(zip)), m_fs(fs) + { + } void fetch(View actions, Span out_status) const override { @@ -324,15 +350,18 @@ namespace virtual void acquire_zips(View actions, Span> out_zips) const = 0; + ProviderId id() const override { return m_provider_id; } + protected: + ProviderId m_provider_id; ZipTool m_zip; const Filesystem& m_fs; }; struct FilesReadBinaryProvider : ZipReadBinaryProvider { - FilesReadBinaryProvider(ZipTool zip, const Filesystem& fs, Path&& dir) - : ZipReadBinaryProvider(std::move(zip), fs), m_dir(std::move(dir)) + FilesReadBinaryProvider(ProviderId provider_id, ZipTool zip, const Filesystem& fs, Path&& dir) + : ZipReadBinaryProvider(provider_id, std::move(zip), fs), m_dir(std::move(dir)) { } @@ -382,23 +411,34 @@ namespace struct HTTPPutBinaryProvider : IWriteBinaryProvider { HTTPPutBinaryProvider(const Filesystem& fs, - std::vector&& urls, + ProviderList&& urls, const std::vector& secrets) : m_fs(fs), m_urls(std::move(urls)), m_secrets(secrets) { } - size_t push_success(const BinaryPackageWriteInfo& request, MessageSink& msg_sink) override + size_t push_success(const BinaryPackageWriteInfo& request, + MessageSink& msg_sink, + CacheStatus& cache_status) override { if (!request.zip_path) return 0; const auto& zip_path = *request.zip_path.get(); size_t count_stored = 0; for (auto&& templ : m_urls) { - auto url = templ.instantiate_variables(request); - auto maybe_success = put_file(m_fs, url, m_secrets, templ.headers, zip_path); + if (!cache_status.should_attempt_write_back(templ.id)) + { + /// We already have this in the cache, so skip it + continue; + } + + auto url = templ.source.instantiate_variables(request); + auto maybe_success = put_file(m_fs, url, m_secrets, templ.source.headers, zip_path); if (maybe_success) + { count_stored++; + cache_status.mark_written_back(templ.id); + } else msg_sink.println(Color::warning, maybe_success.error()); } @@ -410,18 +450,19 @@ namespace private: const Filesystem& m_fs; - std::vector m_urls; + ProviderList m_urls; std::vector m_secrets; }; struct HttpGetBinaryProvider : ZipReadBinaryProvider { - HttpGetBinaryProvider(ZipTool zip, + HttpGetBinaryProvider(ProviderId provider_id, + ZipTool zip, const Filesystem& fs, const Path& buildtrees, UrlTemplate&& url_template, const std::vector& secrets) - : ZipReadBinaryProvider(std::move(zip), fs) + : ZipReadBinaryProvider(provider_id, std::move(zip), fs) , m_buildtrees(buildtrees) , m_url_template(std::move(url_template)) , m_secrets(secrets) @@ -482,7 +523,7 @@ namespace std::string value; }; - NuGetSource nuget_sources_arg(View sources) { return {"-Source", Strings::join(";", sources)}; } + NuGetSource nuget_source_arg(const std::string& source) { return {"-Source", source}; } NuGetSource nuget_configfile_arg(const Path& config_path) { return {"-ConfigFile", config_path.native()}; } struct NuGetTool @@ -643,11 +684,12 @@ namespace struct NugetReadBinaryProvider : IReadBinaryProvider, private NugetBaseBinaryProvider { - NugetReadBinaryProvider(const NugetBaseBinaryProvider& base, NuGetSource src) - : NugetBaseBinaryProvider(base), m_src(std::move(src)) + NugetReadBinaryProvider(ProviderId provider_id, const NugetBaseBinaryProvider& base, NuGetSource src) + : NugetBaseBinaryProvider(base), m_provider_id(provider_id), m_src(std::move(src)) { } + ProviderId m_provider_id; NuGetSource m_src; static std::string generate_packages_config(View refs) @@ -704,24 +746,28 @@ namespace } } } + + ProviderId id() const override { return m_provider_id; } }; struct NugetBinaryPushProvider : IWriteBinaryProvider, private NugetBaseBinaryProvider { NugetBinaryPushProvider(const NugetBaseBinaryProvider& base, - std::vector&& sources, - std::vector&& configs) + ProviderList&& sources, + ProviderList&& configs) : NugetBaseBinaryProvider(base), m_sources(std::move(sources)), m_configs(std::move(configs)) { } - std::vector m_sources; - std::vector m_configs; + ProviderList m_sources; + ProviderList m_configs; bool needs_nuspec_data() const override { return true; } bool needs_zip_file() const override { return false; } - size_t push_success(const BinaryPackageWriteInfo& request, MessageSink& msg_sink) override + size_t push_success(const BinaryPackageWriteInfo& request, + MessageSink& msg_sink, + CacheStatus& cache_status) override { auto& spec = request.spec; @@ -746,31 +792,49 @@ namespace auto nupkg_path = m_buildtrees / make_nugetref(request, m_nuget_prefix).nupkg_filename(); for (auto&& write_src : m_sources) { - msg_sink.println( - msgUploadingBinariesToVendor, msg::spec = spec, msg::vendor = "NuGet", msg::path = write_src); - if (!m_cmd.push(msg_sink, nupkg_path, nuget_sources_arg({&write_src, 1}))) + if (!cache_status.should_attempt_write_back(write_src.id)) + { + /// We already have this in the cache, so skip it + continue; + } + + msg_sink.println(msgUploadingBinariesToVendor, + msg::spec = spec, + msg::vendor = "NuGet", + msg::path = write_src.source); + if (!m_cmd.push(msg_sink, nupkg_path, nuget_source_arg(write_src.source))) { msg_sink.println( - Color::error, msgPushingVendorFailed, msg::vendor = "NuGet", msg::path = write_src); + Color::error, msgPushingVendorFailed, msg::vendor = "NuGet", msg::path = write_src.source); } else { + cache_status.mark_written_back(write_src.id); count_stored++; } } for (auto&& write_cfg : m_configs) { + if (!cache_status.should_attempt_write_back(write_cfg.id)) + { + /// We already have this in the cache, so skip it + continue; + } + msg_sink.println(msgUploadingBinariesToVendor, msg::spec = spec, msg::vendor = "NuGet config", - msg::path = write_cfg); - if (!m_cmd.push(msg_sink, nupkg_path, nuget_configfile_arg(write_cfg))) + msg::path = write_cfg.source); + if (!m_cmd.push(msg_sink, nupkg_path, nuget_configfile_arg(write_cfg.source))) { - msg_sink.println( - Color::error, msgPushingVendorFailed, msg::vendor = "NuGet config", msg::path = write_cfg); + msg_sink.println(Color::error, + msgPushingVendorFailed, + msg::vendor = "NuGet config", + msg::path = write_cfg.source); } else { + cache_status.mark_written_back(write_cfg.id); count_stored++; } } @@ -782,9 +846,13 @@ namespace struct GHABinaryProvider : ZipReadBinaryProvider { - GHABinaryProvider( - ZipTool zip, const Filesystem& fs, const Path& buildtrees, const std::string& url, const std::string& token) - : ZipReadBinaryProvider(std::move(zip), fs) + GHABinaryProvider(ProviderId provider_id, + ZipTool zip, + const Filesystem& fs, + const Path& buildtrees, + const std::string& url, + const std::string& token) + : ZipReadBinaryProvider(provider_id, std::move(zip), fs) , m_buildtrees(buildtrees) , m_url(url + "_apis/artifactcache/cache") , m_secrets() @@ -862,8 +930,14 @@ namespace struct GHABinaryPushProvider : IWriteBinaryProvider { - GHABinaryPushProvider(const Filesystem& fs, const std::string& url, const std::string& token) - : m_fs(fs), m_url(url + "_apis/artifactcache/caches"), m_token_header("Authorization: Bearer " + token) + GHABinaryPushProvider(ProviderId provider_id, + const Filesystem& fs, + const std::string& url, + const std::string& token) + : m_provider_id(provider_id) + , m_fs(fs) + , m_url(url + "_apis/artifactcache/caches") + , m_token_header("Authorization: Bearer " + token) { } @@ -896,10 +970,16 @@ namespace return {}; } - size_t push_success(const BinaryPackageWriteInfo& request, MessageSink&) override + size_t push_success(const BinaryPackageWriteInfo& request, MessageSink&, CacheStatus& cache_status) override { if (!request.zip_path) return 0; + if (!cache_status.should_attempt_write_back(m_provider_id)) + { + /// We already have this in the cache, so skip it + return 0; + } + const auto& zip_path = *request.zip_path.get(); const ElapsedTimer timer; const auto& abi = request.package_abi; @@ -931,6 +1011,7 @@ namespace if (res) { ++upload_count; + cache_status.mark_written_back(m_provider_id); } else { @@ -944,6 +1025,7 @@ namespace bool needs_nuspec_data() const override { return false; } bool needs_zip_file() const override { return true; } + ProviderId m_provider_id; const Filesystem& m_fs; std::string m_url; std::string m_token_header; @@ -964,12 +1046,13 @@ namespace struct ObjectStorageProvider : ZipReadBinaryProvider { - ObjectStorageProvider(ZipTool zip, + ObjectStorageProvider(ProviderId provider_id, + ZipTool zip, const Filesystem& fs, const Path& buildtrees, std::string&& prefix, const std::shared_ptr& tool) - : ZipReadBinaryProvider(std::move(zip), fs) + : ZipReadBinaryProvider(provider_id, std::move(zip), fs) , m_buildtrees(buildtrees) , m_prefix(std::move(prefix)) , m_tool(tool) @@ -1030,7 +1113,7 @@ namespace }; struct ObjectStoragePushProvider : IWriteBinaryProvider { - ObjectStoragePushProvider(std::vector&& prefixes, std::shared_ptr tool) + ObjectStoragePushProvider(ProviderList&& prefixes, std::shared_ptr tool) : m_prefixes(std::move(prefixes)), m_tool(std::move(tool)) { } @@ -1040,17 +1123,26 @@ namespace return Strings::concat(prefix, abi, ".zip"); } - size_t push_success(const BinaryPackageWriteInfo& request, MessageSink& msg_sink) override + size_t push_success(const BinaryPackageWriteInfo& request, + MessageSink& msg_sink, + CacheStatus& cache_status) override { if (!request.zip_path) return 0; const auto& zip_path = *request.zip_path.get(); size_t upload_count = 0; - for (const auto& prefix : m_prefixes) + for (const auto& [provider_id, source, _] : m_prefixes) { - auto res = m_tool->upload_file(make_object_path(prefix, request.package_abi), zip_path); + if (!cache_status.should_attempt_write_back(provider_id)) + { + /// We already have this in the cache, so skip it + continue; + } + + auto res = m_tool->upload_file(make_object_path(source, request.package_abi), zip_path); if (res) { ++upload_count; + cache_status.mark_written_back(provider_id); } else { @@ -1063,7 +1155,7 @@ namespace bool needs_nuspec_data() const override { return false; } bool needs_zip_file() const override { return true; } - std::vector m_prefixes; + ProviderList m_prefixes; std::shared_ptr m_tool; }; @@ -1274,7 +1366,7 @@ namespace segments[1].first); } - handle_readwrite(state->archives_to_read, state->archives_to_write, std::move(p), segments, 2); + handle_readwrite(state->archives, state->provider_count, std::move(p), segments, 2); if (segments.size() > 3) { return add_error( @@ -1311,7 +1403,7 @@ namespace segments[1].first); } - handle_readwrite(state->configs_to_read, state->configs_to_write, std::move(p), segments, 2); + handle_readwrite(state->configs, state->provider_count, std::move(p), segments, 2); if (segments.size() > 3) { return add_error( @@ -1336,7 +1428,7 @@ namespace msg::format(msgInvalidArgumentRequiresSourceArgument, msg::binary_source = "nuget")); } - handle_readwrite(state->sources_to_read, state->sources_to_write, std::move(p), segments, 2); + handle_readwrite(state->sources, state->provider_count, std::move(p), segments, 2); if (segments.size() > 3) { return add_error( @@ -1376,9 +1468,16 @@ namespace return add_error(LocalizedString{maybe_home.error()}, segments[0].first); } - handle_readwrite( - state->archives_to_read, state->archives_to_write, Path(*maybe_home.get()), segments, 1); - state->binary_cache_providers.insert("default"); + parse_cache_type(segments, 1, [&](const CacheType cache_type) { + if (Util::Sets::contains(state->binary_cache_providers, "default")) + { + return; + } + + state->archives.push_back( + CacheProvider{state->provider_count++, Path(*maybe_home.get()), cache_type}); + state->binary_cache_providers.insert("default"); + }); } else if (segments[0].second == "x-azblob") { @@ -1426,14 +1525,14 @@ namespace p.append(segments[2].second); state->secrets.push_back(segments[2].second); UrlTemplate url_template = {p}; - bool read = false, write = false; - handle_readwrite(read, write, segments, 3); - if (read) state->url_templates_to_get.push_back(url_template); - auto headers = azure_blob_headers(); - url_template.headers.assign(headers.begin(), headers.end()); - if (write) state->url_templates_to_put.push_back(url_template); + parse_cache_type(segments, 3, [&](const CacheType url_cache_type) { + auto headers = azure_blob_headers(); + url_template.headers.assign(headers.begin(), headers.end()); + state->url_templates.push_back( + CacheProvider{state->provider_count, url_template, url_cache_type}); - state->binary_cache_providers.insert("azblob"); + state->binary_cache_providers.insert("azblob"); + }); } else if (segments[0].second == "x-gcs") { @@ -1465,7 +1564,7 @@ namespace p.push_back('/'); } - handle_readwrite(state->gcs_read_prefixes, state->gcs_write_prefixes, std::move(p), segments, 2); + handle_readwrite(state->gcs_prefixes, state->provider_count, std::move(p), segments, 2); state->binary_cache_providers.insert("gcs"); } @@ -1499,7 +1598,7 @@ namespace p.push_back('/'); } - handle_readwrite(state->aws_read_prefixes, state->aws_write_prefixes, std::move(p), segments, 2); + handle_readwrite(state->aws_prefixes, state->provider_count, std::move(p), segments, 2); state->binary_cache_providers.insert("aws"); } @@ -1554,7 +1653,7 @@ namespace p.push_back('/'); } - handle_readwrite(state->cos_read_prefixes, state->cos_write_prefixes, std::move(p), segments, 2); + handle_readwrite(state->cos_prefixes, state->provider_count, std::move(p), segments, 2); state->binary_cache_providers.insert("cos"); } else if (segments[0].second == "x-gha") @@ -1567,9 +1666,11 @@ namespace segments[2].first); } - handle_readwrite(state->gha_read, state->gha_write, segments, 1); - - state->binary_cache_providers.insert("gha"); + parse_cache_type(segments, 1, [&](const CacheType gha_cache_type) { + state->gha.emplace( + CacheProvider{state->provider_count++, GithubActionsInfo{}, gha_cache_type}); + state->binary_cache_providers.insert("gha"); + }); } else if (segments[0].second == "http") { @@ -1606,8 +1707,7 @@ namespace url_template.headers.push_back(segments[3].second); } - handle_readwrite( - state->url_templates_to_get, state->url_templates_to_put, std::move(url_template), segments, 2); + handle_readwrite(state->url_templates, state->provider_count, std::move(url_template), segments, 2); state->binary_cache_providers.insert("http"); } else @@ -1892,30 +1992,31 @@ namespace vcpkg ret.nuget_prefix = s.nuget_prefix; std::shared_ptr gcs_tool; - if (!s.gcs_read_prefixes.empty() || !s.gcs_write_prefixes.empty()) + if (!s.gcs_prefixes.empty()) { gcs_tool = std::make_shared(tools, out_sink); } std::shared_ptr aws_tool; - if (!s.aws_read_prefixes.empty() || !s.aws_write_prefixes.empty()) + if (!s.aws_prefixes.empty()) { aws_tool = std::make_shared(tools, out_sink, s.aws_no_sign_request); } std::shared_ptr cos_tool; - if (!s.cos_read_prefixes.empty() || !s.cos_write_prefixes.empty()) + if (!s.cos_prefixes.empty()) { cos_tool = std::make_shared(tools, out_sink); } - if (s.gha_read || s.gha_write) + if (s.gha.has_value()) { if (!args.actions_cache_url.has_value() || !args.actions_runtime_token.has_value()) return msg::format_error(msgGHAParametersMissing, msg::url = "https://learn.microsoft.com/vcpkg/users/binarycaching#gha"); } - if (!s.archives_to_read.empty() || !s.url_templates_to_get.empty() || !s.gcs_read_prefixes.empty() || - !s.aws_read_prefixes.empty() || !s.cos_read_prefixes.empty() || s.gha_read) + if (has_read_provider(s.archives) || has_read_provider(s.url_templates) || + has_read_provider(s.gcs_prefixes) || has_read_provider(s.aws_prefixes) || + has_read_provider(s.cos_prefixes) || (s.gha.has_value() && s.gha.get()->is_read())) { auto maybe_zip_tool = ZipTool::make(tools, out_sink); if (!maybe_zip_tool.has_value()) @@ -1924,88 +2025,123 @@ namespace vcpkg } const auto& zip_tool = *maybe_zip_tool.get(); - for (auto&& dir : s.archives_to_read) + for (auto dir : s.archives) + { + if (dir.is_read()) + { + ret.read.push_back( + std::make_unique(dir.id, zip_tool, fs, std::move(dir.source))); + } + } + auto write_dirs = Util::Vectors::filtered_copy(s.archives, is_write_provider); + if (!write_dirs.empty()) { - ret.read.push_back(std::make_unique(zip_tool, fs, std::move(dir))); + ret.write.push_back(std::make_unique(fs, std::move(write_dirs))); } - for (auto&& url : s.url_templates_to_get) + for (auto url : s.url_templates) + { + if (url.cache_type == CacheType::Read || url.cache_type == CacheType::ReadWrite) + { + ret.read.push_back(std::make_unique( + url.id, zip_tool, fs, buildtrees, std::move(url.source), s.secrets)); + } + } + auto write_url_templates = + Util::Vectors::filtered_copy(s.url_templates, is_write_provider); + if (!write_url_templates.empty()) { - ret.read.push_back( - std::make_unique(zip_tool, fs, buildtrees, std::move(url), s.secrets)); + ret.write.push_back( + std::make_unique(fs, std::move(write_url_templates), s.secrets)); } - for (auto&& prefix : s.gcs_read_prefixes) + for (auto prefix : s.gcs_prefixes) + { + if (prefix.cache_type == CacheType::Read || prefix.cache_type == CacheType::ReadWrite) + { + ret.read.push_back(std::make_unique( + prefix.id, zip_tool, fs, buildtrees, std::move(prefix.source), gcs_tool)); + } + } + auto gcs_write_prefixes = Util::Vectors::filtered_copy(s.gcs_prefixes, is_write_provider); + if (!gcs_write_prefixes.empty()) { - ret.read.push_back( - std::make_unique(zip_tool, fs, buildtrees, std::move(prefix), gcs_tool)); + ret.write.push_back( + std::make_unique(std::move(gcs_write_prefixes), gcs_tool)); } - for (auto&& prefix : s.aws_read_prefixes) + for (auto prefix : s.aws_prefixes) { - ret.read.push_back( - std::make_unique(zip_tool, fs, buildtrees, std::move(prefix), aws_tool)); + if (prefix.cache_type == CacheType::Read || prefix.cache_type == CacheType::ReadWrite) + { + ret.read.push_back(std::make_unique( + prefix.id, zip_tool, fs, buildtrees, std::move(prefix.source), aws_tool)); + } + } + auto aws_write_prefixes = Util::Vectors::filtered_copy(s.aws_prefixes, is_write_provider); + if (!aws_write_prefixes.empty()) + { + ret.write.push_back( + std::make_unique(std::move(aws_write_prefixes), aws_tool)); } - for (auto&& prefix : s.cos_read_prefixes) + for (auto prefix : s.cos_prefixes) + { + ret.read.push_back(std::make_unique( + prefix.id, zip_tool, fs, buildtrees, std::move(prefix.source), cos_tool)); + } + auto cos_write_prefixes = Util::Vectors::filtered_copy(s.cos_prefixes, is_write_provider); + if (!cos_write_prefixes.empty()) { - ret.read.push_back( - std::make_unique(zip_tool, fs, buildtrees, std::move(prefix), cos_tool)); + ret.write.push_back( + std::make_unique(std::move(cos_write_prefixes), cos_tool)); } - if (s.gha_read) + if (s.gha.has_value()) { - const auto& url = *args.actions_cache_url.get(); - const auto& token = *args.actions_runtime_token.get(); - ret.read.push_back(std::make_unique(zip_tool, fs, buildtrees, url, token)); + const CacheProvider& gha = *s.gha.get(); + if (gha.is_read()) + { + const auto& url = *args.actions_cache_url.get(); + const auto& token = *args.actions_runtime_token.get(); + ret.read.push_back( + std::make_unique(gha.id, zip_tool, fs, buildtrees, url, token)); + } + if (gha.is_write()) + { + const auto& url = *args.actions_cache_url.get(); + const auto& token = *args.actions_runtime_token.get(); + ret.write.push_back(std::make_unique(gha.id, fs, url, token)); + } } } - if (!s.archives_to_write.empty()) - { - ret.write.push_back(std::make_unique(fs, std::move(s.archives_to_write))); - } - if (!s.url_templates_to_put.empty()) - { - ret.write.push_back( - std::make_unique(fs, std::move(s.url_templates_to_put), s.secrets)); - } - if (!s.gcs_write_prefixes.empty()) - { - ret.write.push_back( - std::make_unique(std::move(s.gcs_write_prefixes), gcs_tool)); - } - if (!s.aws_write_prefixes.empty()) - { - ret.write.push_back( - std::make_unique(std::move(s.aws_write_prefixes), aws_tool)); - } - if (!s.cos_write_prefixes.empty()) - { - ret.write.push_back( - std::make_unique(std::move(s.cos_write_prefixes), cos_tool)); - } - if (s.gha_write) - { - const auto& url = *args.actions_cache_url.get(); - const auto& token = *args.actions_runtime_token.get(); - ret.write.push_back(std::make_unique(fs, url, token)); - } - if (!s.sources_to_read.empty() || !s.configs_to_read.empty() || !s.sources_to_write.empty() || - !s.configs_to_write.empty()) + if (!s.sources.empty() || !s.configs.empty()) { NugetBaseBinaryProvider nuget_base( fs, NuGetTool(tools, out_sink, s), paths.packages(), buildtrees, s.nuget_prefix); - if (!s.sources_to_read.empty()) - ret.read.push_back( - std::make_unique(nuget_base, nuget_sources_arg(s.sources_to_read))); - for (auto&& config : s.configs_to_read) - ret.read.push_back( - std::make_unique(nuget_base, nuget_configfile_arg(config))); - if (!s.sources_to_write.empty() || !s.configs_to_write.empty()) + + auto sources_to_read = Util::Vectors::filtered_copy(s.sources, is_read_provider); + for (auto&& source : sources_to_read) + { + ret.read.push_back(std::make_unique( + source.id, nuget_base, nuget_source_arg(source.source))); + } + + auto configs_to_read = Util::Vectors::filtered_copy(s.configs, is_read_provider); + for (auto&& config : configs_to_read) + { + ret.read.push_back(std::make_unique( + config.id, nuget_base, nuget_configfile_arg(config.source))); + } + + auto sources_to_write = Util::Vectors::filtered_copy(s.sources, is_write_provider); + auto configs_to_write = Util::Vectors::filtered_copy(s.configs, is_write_provider); + + if (!sources_to_write.empty() || !configs_to_write.empty()) { ret.write.push_back(std::make_unique( - nuget_base, std::move(s.sources_to_write), std::move(s.configs_to_write))); + nuget_base, std::move(sources_to_write), std::move(configs_to_write))); } } } @@ -2014,44 +2150,81 @@ namespace vcpkg ReadOnlyBinaryCache::ReadOnlyBinaryCache(BinaryProviders&& providers) : m_config(std::move(providers)) { } - void ReadOnlyBinaryCache::fetch(View actions) + void ReadOnlyBinaryCache::fetch(const View actions) { std::vector action_ptrs; - std::vector restores; std::vector statuses; + std::vector> provider_actions; + + /// Get the overall list of actions to perform. + for (const auto& action : actions) + { + if (action.has_package_abi()) + { + action_ptrs.push_back(&action); + } + } + + if (action_ptrs.empty()) return; + + /// Precheck each read provider to see if it can provide the package. for (auto&& provider : m_config.read) { - action_ptrs.clear(); - restores.clear(); - statuses.clear(); - for (size_t i = 0; i < actions.size(); ++i) + provider_actions.emplace_back(); + + std::vector availabilities(action_ptrs.size(), CacheAvailability::unknown); + provider->precheck(action_ptrs, availabilities); + + for (size_t i = 0; i < action_ptrs.size(); ++i) { - if (actions[i].package_abi()) + CacheStatus& cache_status = m_status[*action_ptrs[i]->package_abi().get()]; + switch (availabilities[i]) { - CacheStatus& status = m_status[*actions[i].package_abi().get()]; - if (status.should_attempt_restore(provider.get())) + case CacheAvailability::available: { - action_ptrs.push_back(&actions[i]); - restores.push_back(RestoreResult::unavailable); - statuses.push_back(&status); + provider_actions.back().push_back(action_ptrs[i]); + break; + } + case CacheAvailability::unknown: [[fallthrough]]; + case CacheAvailability::unavailable: + { + /// Mark as unavailable, so the write-back happens properly. + cache_status.mark_unavailable(provider.get()); + break; } } } - if (action_ptrs.empty()) continue; + } + + for (size_t provider_idx = 0; provider_idx < m_config.read.size(); ++provider_idx) + { + auto& actions_for_provider = provider_actions[provider_idx]; + if (actions_for_provider.empty()) continue; + + auto& provider = m_config.read[provider_idx]; + std::vector restores{actions_for_provider.size(), RestoreResult::unavailable}; ElapsedTimer timer; - provider->fetch(action_ptrs, restores); + provider->fetch(actions_for_provider, restores); size_t num_restored = 0; for (size_t i = 0; i < restores.size(); ++i) { - if (restores[i] == RestoreResult::unavailable) - { - statuses[i]->mark_unavailable(provider.get()); - } - else + CacheStatus& cache_status = m_status[*actions_for_provider[i]->package_abi().get()]; + switch (restores[i]) { - statuses[i]->mark_restored(); - ++num_restored; + case RestoreResult::unavailable: + { + // TODO @sadroeck - report relevant error + // msg::println_error( + // msg::format(msgEnvStrFailedToExtract).append_raw('\n').append(LocalizedString::from_raw(input))); + Checks::exit_fail(VCPKG_LINE_INFO); + } + case RestoreResult::restored: + { + cache_status.mark_restored(); + ++num_restored; + break; + } } } msg::println(provider->restored_message( @@ -2059,14 +2232,14 @@ namespace vcpkg } } - bool ReadOnlyBinaryCache::is_restored(const InstallPlanAction& action) const + Optional ReadOnlyBinaryCache::cache_status(const InstallPlanAction& action) const { if (auto abi = action.package_abi().get()) { auto it = m_status.find(*abi); - if (it != m_status.end()) return it->second.is_restored(); + if (it != m_status.end()) return it->second; } - return false; + return nullopt; } std::vector ReadOnlyBinaryCache::precheck(View actions) @@ -2100,6 +2273,7 @@ namespace vcpkg for (size_t i = 0; i < action_ptrs.size(); ++i) { auto&& this_status = m_status[*action_ptrs[i]->package_abi().get()]; + if (cache_result[i] == CacheAvailability::available) { this_status.mark_available(provider.get()); @@ -2149,12 +2323,9 @@ namespace vcpkg { if (auto abi = action.package_abi().get()) { - bool restored = m_status[*abi].is_restored(); - // Purge all status information on push_success (cache invalidation) - // - push_success may delete packages/ (invalidate restore) - // - push_success may make the package available from providers (invalidate unavailable) - m_status.erase(*abi); - if (!restored && !m_config.write.empty()) + CacheStatus& cache_status = m_status[*abi]; + + if (!cache_status.get_unavailable_providers().empty()) { ElapsedTimer timer; BinaryPackageWriteInfo request{action}; @@ -2187,7 +2358,7 @@ namespace vcpkg { if (!provider->needs_zip_file() || request.zip_path.has_value()) { - num_destinations += provider->push_success(request, out_sink); + num_destinations += provider->push_success(request, out_sink, cache_status); } } if (request.zip_path) @@ -2226,6 +2397,11 @@ namespace vcpkg default: Checks::unreachable(VCPKG_LINE_INFO); } } + bool CacheStatus::should_attempt_write_back(ProviderId provider_id) const noexcept + { + return Util::Vectors::contains( + m_known_unavailable_providers, provider_id, [](const auto* p, ProviderId id) { return p->id() == id; }); + } bool CacheStatus::is_unavailable(const IReadBinaryProvider* sender) const noexcept { @@ -2241,6 +2417,7 @@ namespace vcpkg m_known_unavailable_providers.push_back(sender); } } + void CacheStatus::mark_available(const IReadBinaryProvider* sender) noexcept { switch (m_status) @@ -2259,12 +2436,16 @@ namespace vcpkg { switch (m_status) { - case CacheStatusState::unknown: m_known_unavailable_providers.clear(); [[fallthrough]]; + case CacheStatusState::unknown: [[fallthrough]]; case CacheStatusState::available: m_status = CacheStatusState::restored; break; case CacheStatusState::restored: break; default: Checks::unreachable(VCPKG_LINE_INFO); } } + void CacheStatus::mark_written_back(ProviderId provider_id) noexcept + { + Util::erase_remove_if(m_known_unavailable_providers, [=](const auto* p) { return p->id() == provider_id; }); + } const IReadBinaryProvider* CacheStatus::get_available_provider() const noexcept { @@ -2276,6 +2457,10 @@ namespace vcpkg default: Checks::unreachable(VCPKG_LINE_INFO); } } + CacheStatus::ReaderProviders& CacheStatus::get_unavailable_providers() noexcept + { + return m_known_unavailable_providers; + } void BinaryConfigParserState::clear() { diff --git a/src/vcpkg/commands.install.cpp b/src/vcpkg/commands.install.cpp index 4e800b5b0c..d497bcbb35 100644 --- a/src/vcpkg/commands.install.cpp +++ b/src/vcpkg/commands.install.cpp @@ -341,12 +341,16 @@ namespace vcpkg if (plan_type == InstallPlanType::BUILD_AND_INSTALL) { std::unique_ptr bcf; - if (binary_cache.is_restored(action)) + if (Optional status = binary_cache.cache_status(action); + status.has_value() && status.get()->is_restored()) { auto maybe_bcf = Paragraphs::try_load_cached_package( fs, action.package_dir.value_or_exit(VCPKG_LINE_INFO), action.spec); bcf = std::make_unique(std::move(maybe_bcf).value_or_exit(VCPKG_LINE_INFO)); all_dependencies_satisfied = true; + + /// Write back the cached packages to other write-capable binary caches. + binary_cache.push_success(build_options.clean_packages, action); } else if (build_options.build_missing == BuildMissing::No) {