diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2023-02-24 15:49:14 +0100 |
---|---|---|
committer | Oliver Reiche <oliver.reiche@huawei.com> | 2023-03-13 17:28:59 +0100 |
commit | 42b452e72f536b63bac080880b8daa481099793c (patch) | |
tree | 0af1a60e5e0fefb5572df38e092def3d8ac74c05 /src/buildtool/execution_api/execution_service | |
parent | 546bf5beebf2eb75e7b325f8f18969b4dd34a169 (diff) | |
download | justbuild-42b452e72f536b63bac080880b8daa481099793c.tar.gz |
Storage: Reworked storage and garbage collection
The improved GC implementation uses refactored storage
classes instead of directly accessing "unknown" file paths.
The required storage class refactoring is quite substantial
and outlined in the following paragraphs.
The module `buildtool/file_system` was extended by:
- `ObjectCAS`: a plain CAS implementation for
reading/writing blobs and computing digests for a given
`ObjectType`. Depending on that type, files written to the
file system may have different properties (e.g., the x-bit
set) or the digest may be computed differently (e.g., tree
digests in non-compatible mode).
A new module `buildtool/storage` was introduced containing:
- `LocalCAS`: provides a common interface for the "logical
CAS", which internally combines three `ObjectCAS`s, one
for each `ObjectType` (file, executable, tree).
- `LocalAC`: implements the action cache, which needs the
`LocalCAS` for storing cache values.
- `TargetCache`: implements the high-level target cache,
which also needs the `LocalCAS` for storing cache values.
- `LocalStorage`: combines the storage classes `LocalCAS`,
`LocalAC`, and `TargetCache`. Those are initialized with
settings from `StorageConfig`, such as the build root base
path or number of generations for the garbage collector.
`LocalStorage` is templated with a Boolean parameter
`kDoGlobalUplink`, which indicates that, on every
read/write access, the garbage collector should be used
for uplinking across all generations (global).
- `GarbageCollector`: responsible for garbage collection and
the global uplinking across all generations. To do so, it
employs instances of `LocalStorage` with `kDoGlobalUplink`
set to false, in order to avoid endless recursion. The
actual (local) uplinking within two single generations is
performed by the corresponding storage class (e.g.,
`TargetCache` implements uplinking of target cache entries
between two target cache generations etc.). Thereby, the
actual knowledge how data should be uplinked is
implemented by the instance that is responsible for
creating the data in the first place.
Diffstat (limited to 'src/buildtool/execution_api/execution_service')
9 files changed, 47 insertions, 47 deletions
diff --git a/src/buildtool/execution_api/execution_service/TARGETS b/src/buildtool/execution_api/execution_service/TARGETS index 739e32e2..42de3e7b 100644 --- a/src/buildtool/execution_api/execution_service/TARGETS +++ b/src/buildtool/execution_api/execution_service/TARGETS @@ -13,7 +13,7 @@ , "private-deps": [ ["@", "fmt", "", "fmt"] , ["@", "gsl-lite", "", "gsl-lite"] - , ["src/buildtool/execution_api/local", "garbage_collector"] + , ["src/buildtool/storage", "storage"] , ["src/buildtool/file_system", "file_system_manager"] , "operation_cache" ] @@ -31,8 +31,7 @@ , ["src/buildtool/logging", "logging"] , ["src/buildtool/common", "bazel_types"] ] - , "private-deps": - [["src/buildtool/execution_api/local", "garbage_collector"]] + , "private-deps": [["src/buildtool/storage", "storage"]] } , "cas_server": { "type": ["@", "rules", "CC", "library"] @@ -49,7 +48,7 @@ , "private-deps": [ ["src/buildtool/compatibility", "compatibility"] , ["@", "fmt", "", "fmt"] - , ["src/buildtool/execution_api/local", "garbage_collector"] + , ["src/buildtool/storage", "storage"] ] } , "server_implementation": @@ -89,7 +88,7 @@ , ["src/buildtool/execution_api/common", "bytestream-common"] , ["src/utils/cpp", "tmp_dir"] , ["@", "fmt", "", "fmt"] - , ["src/buildtool/execution_api/local", "garbage_collector"] + , ["src/buildtool/storage", "storage"] ] } , "capabilities_server": diff --git a/src/buildtool/execution_api/execution_service/ac_server.cpp b/src/buildtool/execution_api/execution_service/ac_server.cpp index 8746a0cd..f8c5be4f 100644 --- a/src/buildtool/execution_api/execution_service/ac_server.cpp +++ b/src/buildtool/execution_api/execution_service/ac_server.cpp @@ -15,7 +15,7 @@ #include "src/buildtool/execution_api/execution_service/ac_server.hpp" #include "fmt/format.h" -#include "src/buildtool/execution_api/local/garbage_collector.hpp" +#include "src/buildtool/storage/garbage_collector.hpp" auto ActionCacheServiceImpl::GetActionResult( ::grpc::ServerContext* /*context*/, @@ -30,7 +30,7 @@ auto ActionCacheServiceImpl::GetActionResult( logger_.Emit(LogLevel::Error, str); return grpc::Status{grpc::StatusCode::INTERNAL, str}; } - auto x = ac_.CachedResult(request->action_digest()); + auto x = storage_->ActionCache().CachedResult(request->action_digest()); if (!x) { return grpc::Status{ grpc::StatusCode::NOT_FOUND, diff --git a/src/buildtool/execution_api/execution_service/ac_server.hpp b/src/buildtool/execution_api/execution_service/ac_server.hpp index bea61841..ce31e0ce 100644 --- a/src/buildtool/execution_api/execution_service/ac_server.hpp +++ b/src/buildtool/execution_api/execution_service/ac_server.hpp @@ -17,8 +17,8 @@ #include "build/bazel/remote/execution/v2/remote_execution.grpc.pb.h" #include "src/buildtool/common/bazel_types.hpp" -#include "src/buildtool/execution_api/local/local_ac.hpp" #include "src/buildtool/logging/logger.hpp" +#include "src/buildtool/storage/storage.hpp" class ActionCacheServiceImpl final : public bazel_re::ActionCache::Service { public: @@ -60,8 +60,7 @@ class ActionCacheServiceImpl final : public bazel_re::ActionCache::Service { ::bazel_re::ActionResult* response) -> ::grpc::Status override; private: - LocalCAS<ObjectType::File> cas_{}; - LocalAC ac_{&cas_}; + gsl::not_null<Storage const*> storage_ = &Storage::Instance(); Logger logger_{"execution-service"}; }; diff --git a/src/buildtool/execution_api/execution_service/bytestream_server.cpp b/src/buildtool/execution_api/execution_service/bytestream_server.cpp index 37bfebc6..234c4adc 100644 --- a/src/buildtool/execution_api/execution_service/bytestream_server.cpp +++ b/src/buildtool/execution_api/execution_service/bytestream_server.cpp @@ -21,7 +21,7 @@ #include "fmt/format.h" #include "src/buildtool/compatibility/native_support.hpp" #include "src/buildtool/execution_api/common/bytestream_common.hpp" -#include "src/buildtool/execution_api/local/garbage_collector.hpp" +#include "src/buildtool/storage/garbage_collector.hpp" #include "src/utils/cpp/tmp_dir.hpp" namespace { @@ -64,11 +64,12 @@ auto BytestreamServiceImpl::Read( if (NativeSupport::IsTree(*hash)) { ArtifactDigest dgst{NativeSupport::Unprefix(*hash), 0, true}; - path = storage_.TreePath(static_cast<bazel_re::Digest>(dgst)); + path = storage_->CAS().TreePath(static_cast<bazel_re::Digest>(dgst)); } else { ArtifactDigest dgst{NativeSupport::Unprefix(*hash), 0, false}; - path = storage_.BlobPath(static_cast<bazel_re::Digest>(dgst), false); + path = storage_->CAS().BlobPath(static_cast<bazel_re::Digest>(dgst), + false); } if (!path) { auto str = fmt::format("could not find {}", *hash); @@ -135,14 +136,14 @@ auto BytestreamServiceImpl::Write( return grpc::Status{grpc::StatusCode::INTERNAL, str}; } if (NativeSupport::IsTree(*hash)) { - if (not storage_.StoreTree(tmp)) { + if (not storage_->CAS().StoreTree(tmp)) { auto str = fmt::format("could not store tree {}", *hash); logger_.Emit(LogLevel::Error, str); return ::grpc::Status{::grpc::StatusCode::INVALID_ARGUMENT, str}; } } else { - if (not storage_.StoreBlob(tmp)) { + if (not storage_->CAS().StoreBlob(tmp, /*is_executable=*/false)) { auto str = fmt::format("could not store blob {}", *hash); logger_.Emit(LogLevel::Error, str); return ::grpc::Status{::grpc::StatusCode::INVALID_ARGUMENT, str}; diff --git a/src/buildtool/execution_api/execution_service/bytestream_server.hpp b/src/buildtool/execution_api/execution_service/bytestream_server.hpp index e2627328..b69b5344 100644 --- a/src/buildtool/execution_api/execution_service/bytestream_server.hpp +++ b/src/buildtool/execution_api/execution_service/bytestream_server.hpp @@ -16,8 +16,8 @@ #define BYTESTREAM_SERVER_HPP #include "google/bytestream/bytestream.grpc.pb.h" -#include "src/buildtool/execution_api/local/local_storage.hpp" #include "src/buildtool/logging/logger.hpp" +#include "src/buildtool/storage/storage.hpp" class BytestreamServiceImpl : public ::google::bytestream::ByteStream::Service { public: @@ -76,7 +76,7 @@ class BytestreamServiceImpl : public ::google::bytestream::ByteStream::Service { -> ::grpc::Status override; private: - LocalStorage storage_{}; + gsl::not_null<Storage const*> storage_ = &Storage::Instance(); Logger logger_{"execution-service:bytestream"}; }; diff --git a/src/buildtool/execution_api/execution_service/cas_server.cpp b/src/buildtool/execution_api/execution_service/cas_server.cpp index 07b9f3bf..7815f557 100644 --- a/src/buildtool/execution_api/execution_service/cas_server.cpp +++ b/src/buildtool/execution_api/execution_service/cas_server.cpp @@ -16,7 +16,7 @@ #include "fmt/format.h" #include "src/buildtool/compatibility/native_support.hpp" -#include "src/buildtool/execution_api/local/garbage_collector.hpp" +#include "src/buildtool/storage/garbage_collector.hpp" static constexpr std::size_t kJustHashLength = 42; static constexpr std::size_t kSHA256Length = 64; @@ -51,12 +51,12 @@ auto CASServiceImpl::FindMissingBlobs( } logger_.Emit(LogLevel::Trace, "FindMissingBlobs: {}", hash); if (NativeSupport::IsTree(hash)) { - if (!storage_.TreePath(x)) { + if (!storage_->CAS().TreePath(x)) { auto* d = response->add_missing_blob_digests(); d->CopyFrom(x); } } - else if (!storage_.BlobPath(x, false)) { + else if (!storage_->CAS().BlobPath(x, false)) { auto* d = response->add_missing_blob_digests(); d->CopyFrom(x); } @@ -99,7 +99,7 @@ auto CASServiceImpl::BatchUpdateBlobs( auto* r = response->add_responses(); r->mutable_digest()->CopyFrom(x.digest()); if (NativeSupport::IsTree(hash)) { - auto const& dgst = storage_.StoreTree(x.data()); + auto const& dgst = storage_->CAS().StoreTree(x.data()); if (!dgst) { auto const& str = fmt::format( "BatchUpdateBlobs: could not upload tree {}", hash); @@ -111,7 +111,7 @@ auto CASServiceImpl::BatchUpdateBlobs( } } else { - auto const& dgst = storage_.StoreBlob(x.data(), false); + auto const& dgst = storage_->CAS().StoreBlob(x.data(), false); if (!dgst) { auto const& str = fmt::format( "BatchUpdateBlobs: could not upload blob {}", hash); @@ -141,10 +141,10 @@ auto CASServiceImpl::BatchReadBlobs( r->mutable_digest()->CopyFrom(digest); std::optional<std::filesystem::path> path; if (NativeSupport::IsTree(digest.hash())) { - path = storage_.TreePath(digest); + path = storage_->CAS().TreePath(digest); } else { - path = storage_.BlobPath(digest, false); + path = storage_->CAS().BlobPath(digest, false); } if (!path) { google::rpc::Status status; diff --git a/src/buildtool/execution_api/execution_service/cas_server.hpp b/src/buildtool/execution_api/execution_service/cas_server.hpp index b3c11706..520263b6 100644 --- a/src/buildtool/execution_api/execution_service/cas_server.hpp +++ b/src/buildtool/execution_api/execution_service/cas_server.hpp @@ -16,8 +16,8 @@ #define CAS_SERVER_HPP #include "build/bazel/remote/execution/v2/remote_execution.grpc.pb.h" #include "src/buildtool/common/bazel_types.hpp" -#include "src/buildtool/execution_api/local/local_storage.hpp" #include "src/buildtool/logging/logger.hpp" +#include "src/buildtool/storage/storage.hpp" class CASServiceImpl final : public bazel_re::ContentAddressableStorage::Service { @@ -119,7 +119,7 @@ class CASServiceImpl final std::string const& computed) const noexcept -> std::optional<std::string>; - LocalStorage storage_{}; + gsl::not_null<Storage const*> storage_ = &Storage::Instance(); Logger logger_{"execution-service"}; }; #endif diff --git a/src/buildtool/execution_api/execution_service/execution_server.cpp b/src/buildtool/execution_api/execution_service/execution_server.cpp index 5f21f5ac..ed22e023 100644 --- a/src/buildtool/execution_api/execution_service/execution_server.cpp +++ b/src/buildtool/execution_api/execution_service/execution_server.cpp @@ -24,8 +24,8 @@ #include "fmt/format.h" #include "gsl-lite/gsl-lite.hpp" #include "src/buildtool/execution_api/execution_service/operation_cache.hpp" -#include "src/buildtool/execution_api/local/garbage_collector.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" +#include "src/buildtool/storage/garbage_collector.hpp" static void UpdateTimeStamp(::google::longrunning::Operation* op) { ::google::protobuf::Timestamp t; @@ -40,7 +40,7 @@ auto ExecutionServiceImpl::GetAction(::bazel_re::ExecuteRequest const* request) const noexcept -> std::pair<std::optional<::bazel_re::Action>, std::optional<std::string>> { // get action description - auto path = storage_.BlobPath(request->action_digest(), false); + auto path = storage_->CAS().BlobPath(request->action_digest(), false); if (!path) { auto str = fmt::format("could not retrieve blob {} from cas", request->action_digest().hash()); @@ -59,8 +59,8 @@ auto ExecutionServiceImpl::GetAction(::bazel_re::ExecuteRequest const* request) } path = Compatibility::IsCompatible() - ? storage_.BlobPath(action.input_root_digest(), false) - : storage_.TreePath(action.input_root_digest()); + ? storage_->CAS().BlobPath(action.input_root_digest(), false) + : storage_->CAS().TreePath(action.input_root_digest()); if (!path) { auto str = fmt::format("could not retrieve input root {} from cas", @@ -75,7 +75,7 @@ auto ExecutionServiceImpl::GetCommand(::bazel_re::Action const& action) const noexcept -> std::pair<std::optional<::bazel_re::Command>, std::optional<std::string>> { - auto path = storage_.BlobPath(action.command_digest(), false); + auto path = storage_->CAS().BlobPath(action.command_digest(), false); if (!path) { auto str = fmt::format("could not retrieve blob {} from cas", action.command_digest().hash()); @@ -141,10 +141,10 @@ auto ExecutionServiceImpl::GetIExecutionAction( } static auto GetDirectoryFromDigest(::bazel_re::Digest const& digest, - LocalStorage const& storage) noexcept + Storage const& storage) noexcept -> std::optional<::bazel_re::Directory> { // determine directory path from digest - auto const& path = storage.BlobPath(digest, /*is_executable=*/false); + auto const& path = storage.CAS().BlobPath(digest, /*is_executable=*/false); if (not path) { return std::nullopt; } @@ -166,7 +166,7 @@ static auto GetDirectoryFromDigest(::bazel_re::Digest const& digest, // NOLINTNEXTLINE(misc-no-recursion) static auto CollectChildDirectoriesRecursively( ::bazel_re::Directory const& root, - LocalStorage const& storage, + Storage const& storage, gsl::not_null<std::unordered_map<::bazel_re::Digest, ::bazel_re::Directory>*> map) noexcept -> bool { @@ -196,7 +196,7 @@ static auto CollectChildDirectoriesRecursively( } static auto GetChildrenFromDirectory(::bazel_re::Directory const& root, - LocalStorage const& storage) noexcept + Storage const& storage) noexcept -> std::optional<std::vector<::bazel_re::Directory>> { // determine child directories std::unordered_map<::bazel_re::Digest, ::bazel_re::Directory> map{}; @@ -232,7 +232,7 @@ static auto GetChildrenFromDirectory(::bazel_re::Directory const& root, static auto CreateTreeDigestFromDirectoryDigest( ::bazel_re::Digest const& dir_digest, - LocalStorage const& storage) noexcept -> std::optional<::bazel_re::Digest> { + Storage const& storage) noexcept -> std::optional<::bazel_re::Digest> { // determine root directory message auto root = GetDirectoryFromDigest(dir_digest, storage); if (not root) { @@ -256,7 +256,8 @@ static auto CreateTreeDigestFromDirectoryDigest( // serialize and store tree message auto content = tree.SerializeAsString(); - auto tree_digest = storage.StoreBlob(content, /*is_executable=*/false); + auto tree_digest = + storage.CAS().StoreBlob(content, /*is_executable=*/false); if (not tree_digest) { return std::nullopt; } @@ -266,7 +267,7 @@ static auto CreateTreeDigestFromDirectoryDigest( static auto AddOutputPaths(::bazel_re::ExecuteResponse* response, IExecutionResponse::Ptr const& execution, - LocalStorage const& storage) noexcept -> bool { + Storage const& storage) noexcept -> bool { auto const& size = static_cast<int>(execution->Artifacts().size()); response->mutable_result()->mutable_output_files()->Reserve(size); response->mutable_result()->mutable_output_directories()->Reserve(size); @@ -311,7 +312,7 @@ auto ExecutionServiceImpl::AddResult( IExecutionResponse::Ptr const& i_execution_response, std::string const& action_hash) const noexcept -> std::optional<std::string> { - if (not AddOutputPaths(response, i_execution_response, storage_)) { + if (not AddOutputPaths(response, i_execution_response, *storage_)) { auto str = fmt::format("Error in creating output paths of action {}", action_hash); logger_.Emit(LogLevel::Error, str); @@ -320,8 +321,8 @@ auto ExecutionServiceImpl::AddResult( auto* result = response->mutable_result(); result->set_exit_code(i_execution_response->ExitCode()); if (i_execution_response->HasStdErr()) { - auto dgst = storage_.StoreBlob(i_execution_response->StdErr(), - /*is_executable=*/false); + auto dgst = storage_->CAS().StoreBlob(i_execution_response->StdErr(), + /*is_executable=*/false); if (!dgst) { auto str = fmt::format("Could not store stderr of action {}", action_hash); @@ -331,8 +332,8 @@ auto ExecutionServiceImpl::AddResult( result->mutable_stderr_digest()->CopyFrom(*dgst); } if (i_execution_response->HasStdOut()) { - auto dgst = storage_.StoreBlob(i_execution_response->StdOut(), - /*is_executable=*/false); + auto dgst = storage_->CAS().StoreBlob(i_execution_response->StdOut(), + /*is_executable=*/false); if (!dgst) { auto str = fmt::format("Could not store stdout of action {}", action_hash); @@ -375,8 +376,8 @@ auto ExecutionServiceImpl::StoreActionResult( ::bazel_re::Action const& action) const noexcept -> std::optional<std::string> { if (i_execution_response->ExitCode() == 0 && !action.do_not_cache() && - !storage_.StoreActionResult(request->action_digest(), - execute_response.result())) { + !storage_->ActionCache().StoreResult(request->action_digest(), + execute_response.result())) { auto str = fmt::format("Could not store action result for action {}", request->action_digest().hash()); logger_.Emit(LogLevel::Error, str); diff --git a/src/buildtool/execution_api/execution_service/execution_server.hpp b/src/buildtool/execution_api/execution_service/execution_server.hpp index 65ace208..6d99fb6a 100644 --- a/src/buildtool/execution_api/execution_service/execution_server.hpp +++ b/src/buildtool/execution_api/execution_service/execution_server.hpp @@ -18,8 +18,8 @@ #include "build/bazel/remote/execution/v2/remote_execution.grpc.pb.h" #include "src/buildtool/common/bazel_types.hpp" #include "src/buildtool/execution_api/local/local_api.hpp" -#include "src/buildtool/execution_api/local/local_storage.hpp" #include "src/buildtool/logging/logger.hpp" +#include "src/buildtool/storage/storage.hpp" class ExecutionServiceImpl final : public bazel_re::Execution::Service { public: @@ -107,7 +107,7 @@ class ExecutionServiceImpl final : public bazel_re::Execution::Service { -> ::grpc::Status override; private: - LocalStorage storage_{}; + gsl::not_null<Storage const*> storage_ = &Storage::Instance(); IExecutionApi::Ptr api_{new LocalApi()}; Logger logger_{"execution-service"}; |