Debian Codegraph @

src/core/lib/service_config/service_config_impl.cc

raw
1 //
2 // Copyright 2022 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 <grpc/support/port_platform.h>
18
19 #include "src/core/lib/service_config/service_config_impl.h"
20
21 #include <string.h>
22
23 #include <string>
24 #include <utility>
25
26 #include "absl/status/statusor.h"
27 #include "absl/strings/str_cat.h"
28 #include "absl/types/optional.h"
29
30 #include "src/core/lib/config/core_configuration.h"
31 #include "src/core/lib/gprpp/memory.h"
32 #include "src/core/lib/gprpp/validation_errors.h"
33 #include "src/core/lib/json/json.h"
34 #include "src/core/lib/json/json_args.h"
35 #include "src/core/lib/json/json_object_loader.h"
36 #include "src/core/lib/service_config/service_config_parser.h"
37 #include "src/core/lib/slice/slice.h"
38 #include "src/core/lib/slice/slice_internal.h"
39
40 namespace grpc_core {
41
42 namespace {
43
44 struct MethodConfig {
45 struct Name {
46 absl::optional<std::string> service;
47 absl::optional<std::string> method;
48
49 static const JsonLoaderInterface* JsonLoader(const JsonArgs&) {
50 static const auto* loader = JsonObjectLoader<Name>()
51 .OptionalField("service", &Name::service)
52 .OptionalField("method", &Name::method)
53 .Finish();
54 return loader;
55 }
56
57 void JsonPostLoad(const Json&, const JsonArgs&, ValidationErrors* errors) {
58 if (!service.has_value() && method.has_value()) {
59 errors->AddError("method name populated without service name");
60 }
61 }
62
63 std::string Path() const {
64 if (!service.has_value() || service->empty()) return "";
65 return absl::StrCat("/", *service, "/",
66 method.has_value() ? *method : "");
67 }
68 };
69
70 std::vector<Name> names;
71
72 static const JsonLoaderInterface* JsonLoader(const JsonArgs&) {
73 static const auto* loader = JsonObjectLoader<MethodConfig>()
74 .OptionalField("name", &MethodConfig::names)
75 .Finish();
76 return loader;
77 }
78 };
79
80 } // namespace
81
82 absl::StatusOr<RefCountedPtr<ServiceConfig>> ServiceConfigImpl::Create(
83 const ChannelArgs& args, absl::string_view json_string) {
84 auto json = Json::Parse(json_string);
85 if (!json.ok()) return json.status();
86 ValidationErrors errors;
87 auto service_config = Create(args, *json, json_string, &errors);
88 if (!errors.ok()) return errors.status("errors validating service config");
89 return service_config;
90 }
91
92 RefCountedPtr<ServiceConfig> ServiceConfigImpl::Create(
93 const ChannelArgs& args, const Json& json, ValidationErrors* errors) {
94 return Create(args, json, json.Dump(), errors);
95 }
96
97 RefCountedPtr<ServiceConfig> ServiceConfigImpl::Create(
98 const ChannelArgs& args, const Json& json, absl::string_view json_string,
99 ValidationErrors* errors) {
100 if (json.type() != Json::Type::OBJECT) {
101 errors->AddError("is not an object");
102 return nullptr;
103 }
104 auto service_config = MakeRefCounted<ServiceConfigImpl>();
105 service_config->json_string_ = std::string(json_string);
106 // Parse global parameters.
107 service_config->parsed_global_configs_ =
108 CoreConfiguration::Get().service_config_parser().ParseGlobalParameters(
109 args, json, errors);
110 // Parse per-method parameters.
111 auto method_configs = LoadJsonObjectField<std::vector<Json::Object>>(
112 json.object_value(), JsonArgs(), "methodConfig", errors,
113 /*required=*/false);
114 if (method_configs.has_value()) {
115 service_config->parsed_method_config_vectors_storage_.reserve(
116 method_configs->size());
117 for (size_t i = 0; i < method_configs->size(); ++i) {
118 const Json::Object& method_config_json = (*method_configs)[i];
119 ValidationErrors::ScopedField field(
120 errors, absl::StrCat(".methodConfig[", i, "]"));
121 // Have each parser read this method config.
122 auto parsed_configs =
123 CoreConfiguration::Get()
124 .service_config_parser()
125 .ParsePerMethodParameters(args, method_config_json, errors);
126 // Store the parsed configs.
127 service_config->parsed_method_config_vectors_storage_.push_back(
128 std::move(parsed_configs));
129 const ServiceConfigParser::ParsedConfigVector* vector_ptr =
130 &service_config->parsed_method_config_vectors_storage_.back();
131 // Parse the names.
132 auto method_config =
133 LoadFromJson<MethodConfig>(method_config_json, JsonArgs(), errors);
134 for (size_t j = 0; j < method_config.names.size(); ++j) {
135 ValidationErrors::ScopedField field(errors,
136 absl::StrCat(".name[", j, "]"));
137 std::string path = method_config.names[j].Path();
138 if (path.empty()) {
139 if (service_config->default_method_config_vector_ != nullptr) {
140 errors->AddError("duplicate default method config");
141 }
142 service_config->default_method_config_vector_ = vector_ptr;
143 } else {
144 grpc_slice key = grpc_slice_from_cpp_string(std::move(path));
145 // If the key is not already present in the map, this will
146 // store a ref to the key in the map.
147 auto& value = service_config->parsed_method_configs_map_[key];
148 if (value != nullptr) {
149 errors->AddError(absl::StrCat("multiple method configs for path ",
150 StringViewFromSlice(key)));
151 // The map entry already existed, so we need to unref the
152 // key we just created.
153 CSliceUnref(key);
154 } else {
155 value = vector_ptr;
156 }
157 }
158 }
159 }
160 }
161 return service_config;
162 }
163
164 ServiceConfigImpl::~ServiceConfigImpl() {
165 for (auto& p : parsed_method_configs_map_) {
166 CSliceUnref(p.first);
167 }
168 }
169
170 const ServiceConfigParser::ParsedConfigVector*
171 ServiceConfigImpl::GetMethodParsedConfigVector(const grpc_slice& path) const {
172 if (parsed_method_configs_map_.empty()) {
173 return default_method_config_vector_;
174 }
175 // Try looking up the full path in the map.
176 auto it = parsed_method_configs_map_.find(path);
177 if (it != parsed_method_configs_map_.end()) return it->second;
178 // If we didn't find a match for the path, try looking for a wildcard
179 // entry (i.e., change "/service/method" to "/service/").
180 UniquePtr<char> path_str(grpc_slice_to_c_string(path));
181 char* sep = strrchr(path_str.get(), '/');
182 if (sep == nullptr) return nullptr; // Shouldn't ever happen.
183 sep[1] = '\0';
184 grpc_slice wildcard_path = grpc_slice_from_static_string(path_str.get());
185 it = parsed_method_configs_map_.find(wildcard_path);
186 if (it != parsed_method_configs_map_.end()) return it->second;
187 // Try default method config, if set.
188 return default_method_config_vector_;
189 }
190
191 } // namespace grpc_core