GRPC Core  9.0.0
service_config.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_H
18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_H
19 
21 
24 
29 #include "src/core/lib/json/json.h"
31 
32 // The main purpose of the code here is to parse the service config in
33 // JSON form, which will look like this:
34 //
35 // {
36 // "loadBalancingPolicy": "string", // optional
37 // "methodConfig": [ // array of one or more method_config objects
38 // {
39 // "name": [ // array of one or more name objects
40 // {
41 // "service": "string", // required
42 // "method": "string", // optional
43 // }
44 // ],
45 // // remaining fields are optional.
46 // // see https://developers.google.com/protocol-buffers/docs/proto3#json
47 // // for format details.
48 // "waitForReady": bool,
49 // "timeout": "duration_string",
50 // "maxRequestMessageBytes": "int64_string",
51 // "maxResponseMessageBytes": "int64_string",
52 // }
53 // ]
54 // }
55 
56 namespace grpc_core {
57 
58 class ServiceConfig : public RefCounted<ServiceConfig> {
59  public:
62  class ParsedConfig {
63  public:
64  virtual ~ParsedConfig() = default;
65  };
66 
68  class Parser {
69  public:
70  virtual ~Parser() = default;
71 
72  virtual std::unique_ptr<ParsedConfig> ParseGlobalParams(
73  const grpc_json* /* json */, grpc_error** error) {
74  // Avoid unused parameter warning on debug-only parameter
75  (void)error;
76  GPR_DEBUG_ASSERT(error != nullptr);
77  return nullptr;
78  }
79 
80  virtual std::unique_ptr<ParsedConfig> ParsePerMethodParams(
81  const grpc_json* /* json */, grpc_error** error) {
82  // Avoid unused parameter warning on debug-only parameter
83  (void)error;
84  GPR_DEBUG_ASSERT(error != nullptr);
85  return nullptr;
86  }
87  };
88 
89  static constexpr int kNumPreallocatedParsers = 4;
92 
98  class CallData {
99  public:
100  CallData() = default;
102  : service_config_(std::move(svc_cfg)) {
103  if (service_config_ != nullptr) {
104  method_params_vector_ =
105  service_config_->GetMethodParsedConfigVector(path);
106  }
107  }
108 
109  ServiceConfig* service_config() { return service_config_.get(); }
110 
111  ParsedConfig* GetMethodParsedConfig(size_t index) const {
112  return method_params_vector_ != nullptr
113  ? (*method_params_vector_)[index].get()
114  : nullptr;
115  }
116 
117  ParsedConfig* GetGlobalParsedConfig(size_t index) const {
118  return service_config_->GetGlobalParsedConfig(index);
119  }
120 
121  private:
122  RefCountedPtr<ServiceConfig> service_config_;
123  const ParsedConfigVector* method_params_vector_ = nullptr;
124  };
125 
128  static RefCountedPtr<ServiceConfig> Create(const char* json,
129  grpc_error** error);
130 
131  // Takes ownership of \a json_tree.
133  grpc_core::UniquePtr<char> json_string, grpc_json* json_tree,
134  grpc_error** error);
135  ~ServiceConfig();
136 
137  const char* service_config_json() const { return service_config_json_.get(); }
138 
143  GPR_DEBUG_ASSERT(index < parsed_global_configs_.size());
144  return parsed_global_configs_[index].get();
145  }
146 
151 
158  static size_t RegisterParser(std::unique_ptr<Parser> parser);
159 
160  static void Init();
161 
162  static void Shutdown();
163 
164  private:
165  // Helper functions to parse the service config
166  grpc_error* ParseGlobalParams(const grpc_json* json_tree);
167  grpc_error* ParsePerMethodParams(const grpc_json* json_tree);
168 
169  // Returns the number of names specified in the method config \a json.
170  static int CountNamesInMethodConfig(grpc_json* json);
171 
172  // Returns a path string for the JSON name object specified by \a json.
173  // Returns null on error, and stores error in \a error.
174  static grpc_core::UniquePtr<char> ParseJsonMethodName(grpc_json* json,
175  grpc_error** error);
176 
177  grpc_error* ParseJsonMethodConfigToServiceConfigVectorTable(
178  const grpc_json* json,
180 
181  grpc_core::UniquePtr<char> service_config_json_;
182  grpc_core::UniquePtr<char> json_string_; // Underlying storage for json_tree.
183  grpc_json* json_tree_;
184 
186  parsed_global_configs_;
187  // A map from the method name to the parsed config vector. Note that we are
188  // using a raw pointer and not a unique pointer so that we can use the same
189  // vector for multiple names.
191  parsed_method_configs_table_;
192  // Storage for all the vectors that are being used in
193  // parsed_method_configs_table_.
195  parsed_method_config_vectors_storage_;
196 };
197 
198 } // namespace grpc_core
199 
200 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_H */
static void Init()
Definition: service_config.cc:320
static void Shutdown()
Definition: service_config.cc:325
Definition: inlined_vector.h:60
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1]. ...
Definition: slice.h:60
Definition: error_internal.h:39
This is the base class that all service config parsers MUST use to store parsed service config data...
Definition: service_config.h:62
const ParsedConfigVector * GetMethodParsedConfigVector(const grpc_slice &path)
Retrieves the vector of parsed configs for the method identified by path.
Definition: service_config.cc:290
ServiceConfig(grpc_core::UniquePtr< char > service_config_json, grpc_core::UniquePtr< char > json_string, grpc_json *json_tree, grpc_error **error)
Definition: service_config.cc:58
Round Robin Policy.
Definition: backend_metric.cc:24
ParsedConfig * GetMethodParsedConfig(size_t index) const
Definition: service_config.h:111
~ServiceConfig()
Definition: service_config.cc:216
virtual std::unique_ptr< ParsedConfig > ParsePerMethodParams(const grpc_json *, grpc_error **error)
Definition: service_config.h:80
ServiceConfig * service_config()
Definition: service_config.h:109
Definition: service_config.h:58
InlinedVector< std::unique_ptr< ParsedConfig >, kNumPreallocatedParsers > ParsedConfigVector
Definition: service_config.h:91
Definition: ref_counted_ptr.h:35
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: memory.h:45
virtual std::unique_ptr< ParsedConfig > ParseGlobalParams(const grpc_json *, grpc_error **error)
Definition: service_config.h:72
Definition: json.h:32
ParsedConfig * GetGlobalParsedConfig(size_t index)
Retrieves the global parsed config at index index.
Definition: service_config.h:142
static constexpr int kNumPreallocatedParsers
Definition: service_config.h:89
Definition: slice_hash_table.h:48
CallData(RefCountedPtr< ServiceConfig > svc_cfg, const grpc_slice &path)
Definition: service_config.h:101
Definition: ref_counted.h:248
ParsedConfig * GetGlobalParsedConfig(size_t index) const
Definition: service_config.h:117
static size_t RegisterParser(std::unique_ptr< Parser > parser)
Globally register a service config parser.
Definition: service_config.cc:315
When a service config is applied to a call in the client_channel_filter, we create an instance of thi...
Definition: service_config.h:98
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:103
static RefCountedPtr< ServiceConfig > Create(const char *json, grpc_error **error)
Creates a new service config from parsing json_string.
Definition: service_config.cc:43
This is the base class that all service config parsers should derive from.
Definition: service_config.h:68
const char * service_config_json() const
Definition: service_config.h:137