YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/gen_yrpc/proxy_generator.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/gen_yrpc/proxy_generator.h"
15
16
#include <google/protobuf/descriptor.h>
17
18
#include "yb/gen_yrpc/metric_descriptor.h"
19
#include "yb/gen_yrpc/model.h"
20
21
namespace yb {
22
namespace gen_yrpc {
23
24
namespace {
25
26
std::vector<MetricDescriptor> outbound_metrics = {
27
  {
28
    .name = "request_bytes",
29
    .prefix = "proxy_",
30
    .kind = "counter",
31
    .extra_args = "",
32
    .units = "yb::MetricUnit::kBytes",
33
    .description = "Bytes sent by",
34
  },
35
  {
36
    .name = "response_bytes",
37
    .prefix = "proxy_",
38
    .kind = "counter",
39
    .extra_args = "",
40
    .units = "yb::MetricUnit::kBytes",
41
    .description = "Bytes received in response to",
42
  },
43
};
44
45
} // namespace
46
47
0
void ProxyGenerator::Header(YBPrinter printer, const google::protobuf::FileDescriptor *file) {
48
0
  printer(
49
0
      "// THIS FILE IS AUTOGENERATED FROM $path$\n"
50
0
      "\n"
51
0
      "#ifndef $upper_case$_PROXY_DOT_H\n"
52
0
      "#define $upper_case$_PROXY_DOT_H\n"
53
0
      "\n"
54
0
      "#include \"$path_no_extension$.pb.h\"\n"
55
0
  );
56
57
0
  if (HasLightweightMethod(file, rpc::RpcSides::PROXY)) {
58
0
    printer("#include \"$path_no_extension$.messages.h\"\n");
59
0
  }
60
61
0
  printer(
62
0
    "\n"
63
0
      "#include \"yb/rpc/proxy.h\"\n"
64
0
      "#include \"yb/util/status.h\"\n"
65
0
      "#include \"yb/util/net/net_fwd.h\"\n"
66
0
      "\n"
67
0
      "namespace yb {\n"
68
0
      "namespace rpc {\n"
69
0
      "class Proxy;\n"
70
0
      "}\n"
71
0
      "}\n"
72
0
      "\n"
73
0
      "$open_namespace$"
74
0
      "\n"
75
0
      "\n"
76
0
  );
77
78
0
  for (int service_idx = 0; service_idx < file->service_count(); ++service_idx) {
79
0
    const auto* service = file->service(service_idx);
80
0
    ScopedSubstituter service_subs(printer, service);
81
82
0
    printer(
83
0
        "class $service_name$Proxy : public ::yb::rpc::ProxyBase {\n"
84
0
        " public:\n"
85
0
        "  $service_name$Proxy(\n"
86
0
        "      ::yb::rpc::ProxyCache* cache, const ::yb::HostPort& remote,\n"
87
0
        "      const ::yb::rpc::Protocol* protocol = nullptr,\n"
88
0
        "      const ::yb::MonoDelta& resolve_cache_timeout = ::yb::MonoDelta());\n"
89
0
    );
90
91
0
    for (int method_idx = 0; method_idx < service->method_count(); ++method_idx) {
92
0
      ScopedSubstituter method_subs(printer, service->method(method_idx), rpc::RpcSides::PROXY);
93
94
0
      printer(
95
0
          "\n"
96
0
          "  ::yb::Status $rpc_name$(const $request$ &req, $response$ *resp,\n"
97
0
          "                          ::yb::rpc::RpcController *controller) const;\n"
98
0
          "  void $rpc_name$Async(const $request$ &req,\n"
99
0
          "                       $response$ *response,\n"
100
0
          "                       ::yb::rpc::RpcController *controller,\n"
101
0
          "                       ::yb::rpc::ResponseCallback callback) const;\n"
102
0
      );
103
0
    }
104
105
0
    printer("};\n\n");
106
0
  }
107
108
0
  printer(
109
0
      "$close_namespace$"
110
0
      "\n"
111
0
      "#endif // $upper_case$_PROXY_DOT_H\n"
112
0
  );
113
0
}
114
115
0
void ProxyGenerator::Source(YBPrinter printer, const google::protobuf::FileDescriptor *file) {
116
0
  printer(
117
0
      "// THIS FILE IS AUTOGENERATED FROM $path$\n"
118
0
      "\n"
119
0
      "#include \"$path_no_extension$.proxy.h\"\n"
120
0
      "\n"
121
0
      "#include \"$path_no_extension$.service.h\"\n\n"
122
0
      "#include \"yb/rpc/proxy.h\"\n"
123
0
      "#include \"yb/rpc/outbound_call.h\"\n"
124
0
      "#include \"yb/util/metrics.h\"\n"
125
0
      "#include \"yb/util/net/sockaddr.h\"\n"
126
0
      "\n"
127
0
  );
128
129
0
  GenerateMetricDefines(printer, file, outbound_metrics);
130
131
0
  printer(
132
0
      "$open_namespace$\n\n"
133
0
      "namespace {\n\n"
134
0
  );
135
136
0
  for (int service_idx = 0; service_idx < file->service_count(); ++service_idx) {
137
0
    const auto* service = file->service(service_idx);
138
0
    ScopedSubstituter service_subs(printer, service);
139
0
    printer("const std::string kFull$service_name$Name = \"$original_full_service_name$\";\n\n");
140
141
0
    printer(
142
0
        "::yb::rpc::ProxyMetricsPtr Create$service_name$Metrics("
143
0
            "const scoped_refptr<MetricEntity>& entity) {\n"
144
0
        "  auto result = std::make_shared<"
145
0
            "::yb::rpc::ProxyMetricsImpl<$service_method_count$>>();\n"
146
0
    );
147
148
0
    GenerateMethodAssignments(
149
0
        printer, service, "result->value[to_underlying($service_method_enum$::$metric_enum_key$)]",
150
0
        false, outbound_metrics);
151
152
0
    printer(
153
0
        "  return result;\n}\n\n"
154
0
    );
155
0
  }
156
157
0
  printer(
158
0
      "\n} // namespace\n\n"
159
0
  );
160
161
0
  for (int service_idx = 0; service_idx < file->service_count(); ++service_idx) {
162
0
    const auto* service = file->service(service_idx);
163
0
    ScopedSubstituter service_subs(printer, service);
164
165
0
    printer(
166
0
        "$service_name$Proxy::$service_name$Proxy(\n"
167
0
        "    ::yb::rpc::ProxyCache* cache, const ::yb::HostPort& remote,\n"
168
0
        "    const ::yb::rpc::Protocol* protocol,\n"
169
0
        "    const ::yb::MonoDelta& resolve_cache_timeout)\n"
170
0
        "    : ProxyBase(kFull$service_name$Name, &Create$service_name$Metrics,\n"
171
0
        "                cache, remote, protocol, resolve_cache_timeout) {}\n\n"
172
0
    );
173
174
0
    for (int method_idx = 0; method_idx < service->method_count(); ++method_idx) {
175
0
      ScopedSubstituter method_subs(printer, service->method(method_idx), rpc::RpcSides::PROXY);
176
177
0
      printer(
178
0
          "::yb::Status $service_name$Proxy::$rpc_name$(\n"
179
0
          "    const $request$ &req, $response$ *resp, ::yb::rpc::RpcController *controller) "
180
0
              "const {\n"
181
0
          "  static ::yb::rpc::RemoteMethod method(\"$full_service_name$\", \"$rpc_name$\");\n"
182
0
          "  return proxy().SyncRequest(\n"
183
0
          "      &method, metrics<$service_method_count$>(static_cast<size_t>("
184
0
              "$service_method_enum$::$metric_enum_key$)), req, resp, controller);\n"
185
0
          "}\n"
186
0
          "\n"
187
0
          "void $service_name$Proxy::$rpc_name$Async(\n"
188
0
          "    const $request$ &req, $response$ *resp, ::yb::rpc::RpcController *controller,\n"
189
0
          "    ::yb::rpc::ResponseCallback callback) const {\n"
190
0
          "  static ::yb::rpc::RemoteMethod method(\"$full_service_name$\", \"$rpc_name$\");\n"
191
0
          "  proxy().AsyncRequest(\n"
192
0
          "      &method, metrics<$service_method_count$>(static_cast<size_t>("
193
0
              "$service_method_enum$::$metric_enum_key$)), req, resp, controller, "
194
0
              "std::move(callback));\n"
195
0
          "}\n"
196
0
          "\n"
197
0
      );
198
0
    }
199
0
  }
200
201
0
  printer("$close_namespace$");
202
0
}
203
204
} // namespace gen_yrpc
205
} // namespace yb