Skip to content

Commit

Permalink
Automatically prune the fetch cache after fetches
Browse files Browse the repository at this point in the history
Keeps the fetch cache from growing unbounded.

Test: TBD - run a fetch and use the new flag
  • Loading branch information
cjreynol committed Jan 16, 2025
1 parent 40fd1fd commit 03cefc0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions base/cvd/cuttlefish/host/commands/cvd/cache/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace cuttlefish {

inline constexpr int kDefaultCacheSizeGB = 25;

Result<std::string> EmptyCache(const std::string& cache_directory);
Result<std::string> GetCacheInfo(const std::string& cache_directory,
bool json_formatted);
Expand Down
2 changes: 0 additions & 2 deletions base/cvd/cuttlefish/host/commands/cvd/cli/commands/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ namespace cuttlefish {

namespace {

constexpr int kDefaultCacheSizeGB = 25;

constexpr char kSummaryHelpText[] = "Manage the files cached by cvd";

enum class Action {
Expand Down
10 changes: 9 additions & 1 deletion base/cvd/cuttlefish/host/commands/cvd/cli/commands/fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
#include "common/libs/utils/result.h"
#include "common/libs/utils/subprocess.h"
#include "common/libs/utils/tee_logging.h"
#include "host/commands/cvd/cache/cache.h"
#include "host/commands/cvd/cli/commands/command_handler.h"
#include "host/commands/cvd/cli/types.h"
#include "host/commands/cvd/fetch/fetch_cvd.h"
#include "host/commands/cvd/fetch/fetch_cvd_parser.h"
#include "host/commands/cvd/utils/common.h"

namespace cuttlefish {

Expand Down Expand Up @@ -60,7 +62,13 @@ Result<void> CvdFetchCommandHandler::Handle(const CommandRequest& request) {
ScopedTeeLogger logger(
LogToStderrAndFiles({log_file}, "", metadata_level, flags.verbosity));

CF_EXPECT(FetchCvdMain(flags));
Result<void> result = FetchCvdMain(flags);
if (flags.build_api_flags.enable_caching) {
LOG(INFO) << "Checking if fetch cache needs to be pruned";
CF_EXPECT(
PruneCache(PerUserCacheDir(), flags.build_api_flags.max_cache_size_GB));
}
CF_EXPECT(std::move(result));
return {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ std::vector<Flag> GetFlagsVector(FetchFlags& fetch_flags,
flags.emplace_back(
GflagsCompatFlag("enable_caching", build_api_flags.enable_caching)
.Help("Whether to enable local fetch file caching or not"));
flags.emplace_back(
GflagsCompatFlag("max_cache_size_GB", build_api_flags.max_cache_size_GB)
.Help("Max allowed size(in gigabytes) of the local fetch file cache. "
" If the cache grows beyond this size it will be pruned after "
"the fetches complete."));

CredentialFlags& credential_flags = build_api_flags.credential_flags;
flags.emplace_back(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
#pragma once

#include <chrono>
#include <cstddef>
#include <optional>
#include <string>
#include <vector>

#include <android-base/logging.h>

#include "common/libs/utils/result.h"
#include "host/commands/cvd/cache/cache.h"
#include "host/libs/web/android_build_api.h"
#include "host/libs/web/android_build_string.h"
#include "host/libs/web/cas/cas_downloader.h"
Expand Down Expand Up @@ -69,6 +71,7 @@ struct BuildApiFlags {
bool external_dns_resolver = kDefaultExternalDnsResolver;
std::string api_base_url = kAndroidBuildServiceUrl;
bool enable_caching = kDefaultEnableCaching;
std::size_t max_cache_size_GB = kDefaultCacheSizeGB;
CasDownloaderFlags cas_downloader_flags;
};

Expand Down

0 comments on commit 03cefc0

Please sign in to comment.