resolver_result_parsing.cc
Go to the documentation of this file.
1 //
2 // Copyright 2018 gRPC authors.
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 
18 
20 
21 #include <ctype.h>
22 
23 #include <algorithm>
24 #include <map>
25 #include <vector>
26 
27 #include "absl/memory/memory.h"
28 #include "absl/strings/str_cat.h"
29 #include "absl/types/optional.h"
30 
31 #include <grpc/support/log.h>
32 
35 
36 // As per the retry design, we do not allow more than 5 retry attempts.
37 #define MAX_MAX_RETRY_ATTEMPTS 5
38 
39 namespace grpc_core {
40 namespace internal {
41 
44  parser_name());
45 }
46 
49  builder->service_config_parser()->RegisterParser(
50  absl::make_unique<ClientChannelServiceConfigParser>());
51 }
52 
53 namespace {
54 
55 absl::optional<std::string> ParseHealthCheckConfig(const Json& field,
58  if (field.type() != Json::Type::OBJECT) {
60  "field:healthCheckConfig error:should be of type object");
61  return absl::nullopt;
62  }
63  std::vector<grpc_error_handle> error_list;
64  absl::optional<std::string> service_name;
65  auto it = field.object_value().find("serviceName");
66  if (it != field.object_value().end()) {
67  if (it->second.type() != Json::Type::STRING) {
68  error_list.push_back(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
69  "field:serviceName error:should be of type string"));
70  } else {
71  service_name = it->second.string_value();
72  }
73  }
74  *error =
75  GRPC_ERROR_CREATE_FROM_VECTOR("field:healthCheckConfig", &error_list);
76  return service_name;
77 }
78 
79 } // namespace
80 
81 std::unique_ptr<ServiceConfigParser::ParsedConfig>
83  const grpc_channel_args* /*args*/, const Json& json,
86  std::vector<grpc_error_handle> error_list;
87  // Parse LB config.
89  auto it = json.object_value().find("loadBalancingConfig");
90  if (it != json.object_value().end()) {
93  it->second, &parse_error);
95  std::vector<grpc_error_handle> lb_errors;
96  lb_errors.push_back(parse_error);
97  error_list.push_back(GRPC_ERROR_CREATE_FROM_VECTOR(
98  "field:loadBalancingConfig", &lb_errors));
99  }
100  }
101  // Parse deprecated LB policy.
102  std::string lb_policy_name;
103  it = json.object_value().find("loadBalancingPolicy");
104  if (it != json.object_value().end()) {
105  if (it->second.type() != Json::Type::STRING) {
106  error_list.push_back(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
107  "field:loadBalancingPolicy error:type should be string"));
108  } else {
109  lb_policy_name = it->second.string_value();
110  for (size_t i = 0; i < lb_policy_name.size(); ++i) {
111  lb_policy_name[i] = tolower(lb_policy_name[i]);
112  }
113  bool requires_config = false;
115  lb_policy_name.c_str(), &requires_config)) {
116  error_list.push_back(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
117  "field:loadBalancingPolicy error:Unknown lb policy"));
118  } else if (requires_config) {
119  error_list.push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
120  absl::StrCat("field:loadBalancingPolicy error:", lb_policy_name,
121  " requires a config. Please use loadBalancingConfig "
122  "instead.")));
123  }
124  }
125  }
126  // Parse health check config.
127  absl::optional<std::string> health_check_service_name;
128  it = json.object_value().find("healthCheckConfig");
129  if (it != json.object_value().end()) {
130  grpc_error_handle parsing_error = GRPC_ERROR_NONE;
131  health_check_service_name =
132  ParseHealthCheckConfig(it->second, &parsing_error);
133  if (!GRPC_ERROR_IS_NONE(parsing_error)) {
134  error_list.push_back(parsing_error);
135  }
136  }
137  *error = GRPC_ERROR_CREATE_FROM_VECTOR("Client channel global parser",
138  &error_list);
139  if (GRPC_ERROR_IS_NONE(*error)) {
140  return absl::make_unique<ClientChannelGlobalParsedConfig>(
141  std::move(parsed_lb_config), std::move(lb_policy_name),
142  std::move(health_check_service_name));
143  }
144  return nullptr;
145 }
146 
147 std::unique_ptr<ServiceConfigParser::ParsedConfig>
149  const grpc_channel_args* /*args*/, const Json& json,
152  std::vector<grpc_error_handle> error_list;
153  // Parse waitForReady.
155  auto it = json.object_value().find("waitForReady");
156  if (it != json.object_value().end()) {
157  if (it->second.type() == Json::Type::JSON_TRUE) {
158  wait_for_ready.emplace(true);
159  } else if (it->second.type() == Json::Type::JSON_FALSE) {
160  wait_for_ready.emplace(false);
161  } else {
162  error_list.push_back(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
163  "field:waitForReady error:Type should be true/false"));
164  }
165  }
166  // Parse timeout.
169  &error_list, false);
170  // Return result.
171  *error = GRPC_ERROR_CREATE_FROM_VECTOR("Client channel parser", &error_list);
172  if (GRPC_ERROR_IS_NONE(*error)) {
173  return absl::make_unique<ClientChannelMethodParsedConfig>(timeout,
175  }
176  return nullptr;
177 }
178 
179 } // namespace internal
180 } // namespace grpc_core
grpc_core::Json::Type::JSON_TRUE
@ JSON_TRUE
regen-readme.it
it
Definition: regen-readme.py:15
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:103
grpc_core::Json::Type::OBJECT
@ OBJECT
grpc_core::CoreConfiguration::service_config_parser
const ServiceConfigParser & service_config_parser() const
Definition: core_configuration.h:153
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::CoreConfiguration::Builder
Definition: core_configuration.h:41
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::Json::object_value
const Object & object_value() const
Definition: src/core/lib/json/json.h:177
grpc_core::Json::Type::JSON_FALSE
@ JSON_FALSE
grpc_core::internal::ClientChannelServiceConfigParser::ParsePerMethodParams
std::unique_ptr< ServiceConfigParser::ParsedConfig > ParsePerMethodParams(const grpc_channel_args *, const Json &json, grpc_error_handle *error) override
Definition: resolver_result_parsing.cc:148
grpc_channel_args
Definition: grpc_types.h:132
GRPC_ERROR_CREATE_FROM_VECTOR
#define GRPC_ERROR_CREATE_FROM_VECTOR(desc, error_list)
Definition: error.h:314
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::CoreConfiguration::Get
static const CoreConfiguration & Get()
Definition: core_configuration.h:82
Json
JSON (JavaScript Object Notation).
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:227
json_util.h
absl::optional< std::string >
grpc_core::ServiceConfigParser::GetParserIndex
size_t GetParserIndex(absl::string_view name) const
Definition: lib/service_config/service_config_parser.cc:92
grpc_core::internal::ClientChannelServiceConfigParser::Register
static void Register(CoreConfiguration::Builder *builder)
Definition: resolver_result_parsing.cc:47
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
grpc_core::internal::ClientChannelServiceConfigParser::parser_name
static absl::string_view parser_name()
Definition: resolver_result_parsing.h:105
grpc_core::LoadBalancingPolicyRegistry::ParseLoadBalancingConfig
static RefCountedPtr< LoadBalancingPolicy::Config > ParseLoadBalancingConfig(const Json &json, grpc_error_handle *error)
Definition: lb_policy_registry.cc:169
wait_for_ready
bool wait_for_ready
Definition: rls_end2end_test.cc:240
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
lb_policy_registry.h
grpc_core::internal::ClientChannelServiceConfigParser::ParseGlobalParams
std::unique_ptr< ServiceConfigParser::ParsedConfig > ParseGlobalParams(const grpc_channel_args *, const Json &json, grpc_error_handle *error) override
Definition: resolver_result_parsing.cc:82
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
internal
Definition: benchmark/test/output_test_helper.cc:20
grpc_core::LoadBalancingPolicyRegistry::LoadBalancingPolicyExists
static bool LoadBalancingPolicyExists(const char *name, bool *requires_config)
Definition: lb_policy_registry.cc:109
grpc_core::ParseJsonObjectFieldAsDuration
bool ParseJsonObjectFieldAsDuration(const Json::Object &object, absl::string_view field_name, Duration *output, std::vector< grpc_error_handle > *error_list, bool required)
Definition: src/core/lib/json/json_util.cc:107
grpc_error
Definition: error_internal.h:42
resolver_result_parsing.h
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
grpc_core::internal::ClientChannelServiceConfigParser::ParserIndex
static size_t ParserIndex()
Definition: resolver_result_parsing.cc:42
grpc_core::Json::Type::STRING
@ STRING
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
parse_error
@ parse_error
Definition: pem_info.c:88
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:11