summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/remote/bazel/bazel_ac_client.cpp
blob: 221537f25deca116b7811bc0e11ddd129cc859d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/buildtool/execution_api/remote/bazel/bazel_ac_client.hpp"

#include <algorithm>

#include <grpcpp/grpcpp.h>

#include "google/protobuf/repeated_ptr_field.h"
#include "src/buildtool/common/bazel_types.hpp"
#include "src/buildtool/common/remote/client_common.hpp"
#include "src/buildtool/common/remote/retry.hpp"
#include "src/buildtool/common/remote/retry_config.hpp"
#include "src/buildtool/logging/log_level.hpp"

BazelAcClient::BazelAcClient(
    std::string const& server,
    Port port,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RetryConfig const*> const& retry_config) noexcept
    : retry_config_{*retry_config} {
    stub_ = bazel_re::ActionCache::NewStub(
        CreateChannelWithCredentials(server, port, auth));
}

auto BazelAcClient::GetActionResult(
    std::string const& instance_name,
    bazel_re::Digest const& action_digest,
    bool inline_stdout,
    bool inline_stderr,
    std::vector<std::string> const& inline_output_files) noexcept
    -> std::optional<bazel_re::ActionResult> {
    bazel_re::GetActionResultRequest request{};
    request.set_instance_name(instance_name);
    (*request.mutable_action_digest()) = action_digest;
    request.set_inline_stdout(inline_stdout);
    request.set_inline_stderr(inline_stderr);
    std::copy(inline_output_files.begin(),
              inline_output_files.end(),
              pb::back_inserter(request.mutable_inline_output_files()));

    bazel_re::ActionResult response;
    auto [ok, status] = WithRetry(
        [this, &response, &request]() {
            grpc::ClientContext context;
            return stub_->GetActionResult(&context, request, &response);
        },
        retry_config_,
        logger_);
    if (not ok) {
        if (status.error_code() == grpc::StatusCode::NOT_FOUND) {
            logger_.Emit(
                LogLevel::Trace, "cache miss '{}'", status.error_message());
        }
        else {
            LogStatus(&logger_, LogLevel::Error, status);
        }
        return std::nullopt;
    }
    return response;
}