Android-cuttlefish cvd tool
fallback_build_api.h
Go to the documentation of this file.
1//
2// Copyright (C) 2026 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <stddef.h>
19
20#include <memory>
21#include <string>
22#include <utility>
23#include <vector>
24
25#include "absl/log/log.h"
26
30
31namespace cuttlefish {
32
33// Serves each request from the first backend that succeeds, trying them in
34// order. A backend that cannot serve a request returns an error, and the next
35// backend gets the request. When every backend fails, the error of the last
36// one is returned.
37//
38// Example, preferring CAS over a plain Android Build download:
39//
40// FallbackBuildApi<DeviceBuildString, DeviceBuild> api({
41// &cas_downloader,
42// &android_build_api,
43// });
44//
45// The backends are not owned and must outlive this object.
46template <typename StringType, typename BuildType>
47class FallbackBuildApi : public BuildApi<StringType, BuildType> {
48 public:
50
51 explicit FallbackBuildApi(std::vector<Backend*> backends)
52 : backends_(std::move(backends)) {}
53
54 Result<BuildType> GetBuild(const StringType& build_string) override {
55 return CF_EXPECT(FirstSuccess<BuildType>(
56 [&](Backend& backend) { return backend.GetBuild(build_string); }));
57 }
58
59 Result<std::string> DownloadFile(const BuildType& build,
60 const std::string& target_directory,
61 const std::string& artifact_name) override {
62 return CF_EXPECT(FirstSuccess<std::string>([&](Backend& backend) {
63 return backend.DownloadFile(build, target_directory, artifact_name);
64 }));
65 }
66
68 const BuildType& build, const std::string& artifact_name) override {
69 return CF_EXPECT(
70 FirstSuccess<std::unique_ptr<ReaderSeeker>>([&](Backend& backend) {
71 return backend.FileReader(build, artifact_name);
72 }));
73 }
74
75 private:
76 template <typename T, typename Call>
77 Result<T> FirstSuccess(Call&& call) {
78 Result<T> result = CF_ERR("No backends to serve the request");
79 for (size_t i = 0; i < backends_.size(); ++i) {
80 if (i > 0) {
81 VLOG(1) << "Falling back to the next backend after: " << result.error();
82 }
83 result = call(*backends_[i]);
84 if (result.has_value()) {
85 return result;
86 }
87 }
88 return CF_EXPECT(std::move(result));
89 }
90
91 std::vector<Backend*> backends_;
92};
93
94} // namespace cuttlefish
#define VLOG(TAG)
Definition: adb_trace.h:52
Definition: build_api.h:27
virtual Result< std::unique_ptr< ReaderSeeker > > FileReader(const BuildType &, const std::string &artifact_name)=0
virtual Result< BuildType > GetBuild(const StringType &build_string)=0
virtual Result< std::string > DownloadFile(const BuildType &build, const std::string &target_directory, const std::string &artifact_name)=0
Definition: fallback_build_api.h:47
Result< std::unique_ptr< ReaderSeeker > > FileReader(const BuildType &build, const std::string &artifact_name) override
Definition: fallback_build_api.h:67
std::vector< Backend * > backends_
Definition: fallback_build_api.h:91
Result< T > FirstSuccess(Call &&call)
Definition: fallback_build_api.h:77
Result< BuildType > GetBuild(const StringType &build_string) override
Definition: fallback_build_api.h:54
Result< std::string > DownloadFile(const BuildType &build, const std::string &target_directory, const std::string &artifact_name) override
Definition: fallback_build_api.h:59
FallbackBuildApi(std::vector< Backend * > backends)
Definition: fallback_build_api.h:51
#define CF_ERR(MSG)
Definition: expect.h:48
#define CF_EXPECT(...)
Definition: expect.h:184
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30