• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "src/core/client_channel/client_channel_service_config.h"
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include <map>
22 #include <utility>
23 
24 #include "absl/status/status.h"
25 #include "absl/status/statusor.h"
26 #include "absl/strings/ascii.h"
27 #include "absl/strings/str_cat.h"
28 #include "absl/types/optional.h"
29 #include "src/core/load_balancing/lb_policy_registry.h"
30 
31 // As per the retry design, we do not allow more than 5 retry attempts.
32 #define MAX_MAX_RETRY_ATTEMPTS 5
33 
34 namespace grpc_core {
35 namespace internal {
36 
37 //
38 // ClientChannelGlobalParsedConfig::HealthCheckConfig
39 //
40 
41 const JsonLoaderInterface*
JsonLoader(const JsonArgs &)42 ClientChannelGlobalParsedConfig::HealthCheckConfig::JsonLoader(
43     const JsonArgs&) {
44   static const auto* loader =
45       JsonObjectLoader<HealthCheckConfig>()
46           .OptionalField("serviceName", &HealthCheckConfig::service_name)
47           .Finish();
48   return loader;
49 }
50 
51 //
52 // ClientChannelGlobalParsedConfig
53 //
54 
JsonLoader(const JsonArgs &)55 const JsonLoaderInterface* ClientChannelGlobalParsedConfig::JsonLoader(
56     const JsonArgs&) {
57   static const auto* loader =
58       JsonObjectLoader<ClientChannelGlobalParsedConfig>()
59           // Note: "loadBalancingConfig" requires special handling, so
60           // that field will be parsed in JsonPostLoad() instead.
61           .OptionalField(
62               "loadBalancingPolicy",
63               &ClientChannelGlobalParsedConfig::parsed_deprecated_lb_policy_)
64           .OptionalField("healthCheckConfig",
65                          &ClientChannelGlobalParsedConfig::health_check_config_)
66           .Finish();
67   return loader;
68 }
69 
JsonPostLoad(const Json & json,const JsonArgs &,ValidationErrors * errors)70 void ClientChannelGlobalParsedConfig::JsonPostLoad(const Json& json,
71                                                    const JsonArgs&,
72                                                    ValidationErrors* errors) {
73   const auto& lb_policy_registry =
74       CoreConfiguration::Get().lb_policy_registry();
75   // Parse LB config.
76   {
77     ValidationErrors::ScopedField field(errors, ".loadBalancingConfig");
78     auto it = json.object().find("loadBalancingConfig");
79     if (it != json.object().end()) {
80       auto config = lb_policy_registry.ParseLoadBalancingConfig(it->second);
81       if (!config.ok()) {
82         errors->AddError(config.status().message());
83       } else {
84         parsed_lb_config_ = std::move(*config);
85       }
86     }
87   }
88   // Sanity-check deprecated "loadBalancingPolicy" field.
89   if (!parsed_deprecated_lb_policy_.empty()) {
90     ValidationErrors::ScopedField field(errors, ".loadBalancingPolicy");
91     // Convert to lower-case.
92     absl::AsciiStrToLower(&parsed_deprecated_lb_policy_);
93     bool requires_config = false;
94     if (!lb_policy_registry.LoadBalancingPolicyExists(
95             parsed_deprecated_lb_policy_, &requires_config)) {
96       errors->AddError(absl::StrCat("unknown LB policy \"",
97                                     parsed_deprecated_lb_policy_, "\""));
98     } else if (requires_config) {
99       errors->AddError(absl::StrCat(
100           "LB policy \"", parsed_deprecated_lb_policy_,
101           "\" requires a config. Please use loadBalancingConfig instead."));
102     }
103   }
104 }
105 
106 //
107 // ClientChannelMethodParsedConfig
108 //
109 
JsonLoader(const JsonArgs &)110 const JsonLoaderInterface* ClientChannelMethodParsedConfig::JsonLoader(
111     const JsonArgs&) {
112   static const auto* loader =
113       JsonObjectLoader<ClientChannelMethodParsedConfig>()
114           .OptionalField("timeout", &ClientChannelMethodParsedConfig::timeout_)
115           .OptionalField("waitForReady",
116                          &ClientChannelMethodParsedConfig::wait_for_ready_)
117           .Finish();
118   return loader;
119 }
120 
121 //
122 // ClientChannelServiceConfigParser
123 //
124 
ParserIndex()125 size_t ClientChannelServiceConfigParser::ParserIndex() {
126   return CoreConfiguration::Get().service_config_parser().GetParserIndex(
127       parser_name());
128 }
129 
Register(CoreConfiguration::Builder * builder)130 void ClientChannelServiceConfigParser::Register(
131     CoreConfiguration::Builder* builder) {
132   builder->service_config_parser()->RegisterParser(
133       std::make_unique<ClientChannelServiceConfigParser>());
134 }
135 
136 std::unique_ptr<ServiceConfigParser::ParsedConfig>
ParseGlobalParams(const ChannelArgs &,const Json & json,ValidationErrors * errors)137 ClientChannelServiceConfigParser::ParseGlobalParams(const ChannelArgs& /*args*/,
138                                                     const Json& json,
139                                                     ValidationErrors* errors) {
140   return LoadFromJson<std::unique_ptr<ClientChannelGlobalParsedConfig>>(
141       json, JsonArgs(), errors);
142 }
143 
144 std::unique_ptr<ServiceConfigParser::ParsedConfig>
ParsePerMethodParams(const ChannelArgs &,const Json & json,ValidationErrors * errors)145 ClientChannelServiceConfigParser::ParsePerMethodParams(
146     const ChannelArgs& /*args*/, const Json& json, ValidationErrors* errors) {
147   return LoadFromJson<std::unique_ptr<ClientChannelMethodParsedConfig>>(
148       json, JsonArgs(), errors);
149 }
150 
151 }  // namespace internal
152 }  // namespace grpc_core
153