mirror of https://github.com/grpc/grpc.git
Merge pull request #23013 from markdroth/xds_config_selector
Refactor ServiceConfig code to pave the way for adding a ConfigSelector APIpull/23075/head
commit
afe7c74d6d
28 changed files with 423 additions and 240 deletions
@ -0,0 +1,68 @@ |
|||||||
|
//
|
||||||
|
// Copyright 2016 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H |
||||||
|
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include "src/core/ext/filters/client_channel/service_config.h" |
||||||
|
#include "src/core/ext/filters/client_channel/service_config_parser.h" |
||||||
|
#include "src/core/lib/channel/context.h" |
||||||
|
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
||||||
|
|
||||||
|
namespace grpc_core { |
||||||
|
|
||||||
|
/// When a service config is applied to a call in the client_channel_filter,
|
||||||
|
/// we create an instance of this object on the arena. A pointer to this
|
||||||
|
/// object is also stored in the call_context, so that future filters can
|
||||||
|
/// easily access method and global parameters for the call.
|
||||||
|
class ServiceConfigCallData { |
||||||
|
public: |
||||||
|
ServiceConfigCallData( |
||||||
|
RefCountedPtr<ServiceConfig> service_config, |
||||||
|
const ServiceConfigParser::ParsedConfigVector* method_configs, |
||||||
|
grpc_call_context_element* call_context) |
||||||
|
: service_config_(std::move(service_config)), |
||||||
|
method_configs_(method_configs) { |
||||||
|
call_context[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].value = this; |
||||||
|
call_context[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].destroy = Destroy; |
||||||
|
} |
||||||
|
|
||||||
|
ServiceConfig* service_config() { return service_config_.get(); } |
||||||
|
|
||||||
|
ServiceConfigParser::ParsedConfig* GetMethodParsedConfig(size_t index) const { |
||||||
|
return method_configs_ != nullptr ? (*method_configs_)[index].get() |
||||||
|
: nullptr; |
||||||
|
} |
||||||
|
|
||||||
|
ServiceConfigParser::ParsedConfig* GetGlobalParsedConfig(size_t index) const { |
||||||
|
return service_config_->GetGlobalParsedConfig(index); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
static void Destroy(void* ptr) { |
||||||
|
ServiceConfigCallData* self = static_cast<ServiceConfigCallData*>(ptr); |
||||||
|
self->~ServiceConfigCallData(); |
||||||
|
} |
||||||
|
|
||||||
|
RefCountedPtr<ServiceConfig> service_config_; |
||||||
|
const ServiceConfigParser::ParsedConfigVector* method_configs_ = nullptr; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace grpc_core
|
||||||
|
|
||||||
|
#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H */ |
@ -0,0 +1,87 @@ |
|||||||
|
//
|
||||||
|
// Copyright 2015 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include "src/core/ext/filters/client_channel/service_config_parser.h" |
||||||
|
|
||||||
|
#include <grpc/support/log.h> |
||||||
|
|
||||||
|
namespace grpc_core { |
||||||
|
|
||||||
|
namespace { |
||||||
|
typedef absl::InlinedVector<std::unique_ptr<ServiceConfigParser::Parser>, |
||||||
|
ServiceConfigParser::kNumPreallocatedParsers> |
||||||
|
ServiceConfigParserList; |
||||||
|
ServiceConfigParserList* g_registered_parsers; |
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void ServiceConfigParser::Init() { |
||||||
|
GPR_ASSERT(g_registered_parsers == nullptr); |
||||||
|
g_registered_parsers = new ServiceConfigParserList(); |
||||||
|
} |
||||||
|
|
||||||
|
void ServiceConfigParser::Shutdown() { |
||||||
|
delete g_registered_parsers; |
||||||
|
g_registered_parsers = nullptr; |
||||||
|
} |
||||||
|
|
||||||
|
size_t ServiceConfigParser::RegisterParser(std::unique_ptr<Parser> parser) { |
||||||
|
g_registered_parsers->push_back(std::move(parser)); |
||||||
|
return g_registered_parsers->size() - 1; |
||||||
|
} |
||||||
|
|
||||||
|
ServiceConfigParser::ParsedConfigVector |
||||||
|
ServiceConfigParser::ParseGlobalParameters(const Json& json, |
||||||
|
grpc_error** error) { |
||||||
|
ParsedConfigVector parsed_global_configs; |
||||||
|
std::vector<grpc_error*> error_list; |
||||||
|
for (size_t i = 0; i < g_registered_parsers->size(); i++) { |
||||||
|
grpc_error* parser_error = GRPC_ERROR_NONE; |
||||||
|
auto parsed_config = |
||||||
|
(*g_registered_parsers)[i]->ParseGlobalParams(json, &parser_error); |
||||||
|
if (parser_error != GRPC_ERROR_NONE) { |
||||||
|
error_list.push_back(parser_error); |
||||||
|
} |
||||||
|
parsed_global_configs.push_back(std::move(parsed_config)); |
||||||
|
} |
||||||
|
if (!error_list.empty()) { |
||||||
|
*error = GRPC_ERROR_CREATE_FROM_VECTOR("Global Params", &error_list); |
||||||
|
} |
||||||
|
return parsed_global_configs; |
||||||
|
} |
||||||
|
|
||||||
|
ServiceConfigParser::ParsedConfigVector |
||||||
|
ServiceConfigParser::ParsePerMethodParameters(const Json& json, |
||||||
|
grpc_error** error) { |
||||||
|
ParsedConfigVector parsed_method_configs; |
||||||
|
std::vector<grpc_error*> error_list; |
||||||
|
for (size_t i = 0; i < g_registered_parsers->size(); i++) { |
||||||
|
grpc_error* parser_error = GRPC_ERROR_NONE; |
||||||
|
auto parsed_config = |
||||||
|
(*g_registered_parsers)[i]->ParsePerMethodParams(json, &parser_error); |
||||||
|
if (parser_error != GRPC_ERROR_NONE) { |
||||||
|
error_list.push_back(parser_error); |
||||||
|
} |
||||||
|
parsed_method_configs.push_back(std::move(parsed_config)); |
||||||
|
} |
||||||
|
if (!error_list.empty()) { |
||||||
|
*error = GRPC_ERROR_CREATE_FROM_VECTOR("methodConfig", &error_list); |
||||||
|
} |
||||||
|
return parsed_method_configs; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace grpc_core
|
@ -0,0 +1,89 @@ |
|||||||
|
//
|
||||||
|
// Copyright 2016 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H |
||||||
|
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
|
||||||
|
#include "absl/container/inlined_vector.h" |
||||||
|
|
||||||
|
#include "src/core/lib/iomgr/error.h" |
||||||
|
#include "src/core/lib/json/json.h" |
||||||
|
|
||||||
|
namespace grpc_core { |
||||||
|
|
||||||
|
// Service config parser registry.
|
||||||
|
// See service_config.h for more information.
|
||||||
|
class ServiceConfigParser { |
||||||
|
public: |
||||||
|
/// This is the base class that all service config parsers MUST use to store
|
||||||
|
/// parsed service config data.
|
||||||
|
class ParsedConfig { |
||||||
|
public: |
||||||
|
virtual ~ParsedConfig() = default; |
||||||
|
}; |
||||||
|
|
||||||
|
/// This is the base class that all service config parsers should derive from.
|
||||||
|
class Parser { |
||||||
|
public: |
||||||
|
virtual ~Parser() = default; |
||||||
|
|
||||||
|
virtual std::unique_ptr<ParsedConfig> ParseGlobalParams( |
||||||
|
const Json& /* json */, grpc_error** error) { |
||||||
|
// Avoid unused parameter warning on debug-only parameter
|
||||||
|
(void)error; |
||||||
|
GPR_DEBUG_ASSERT(error != nullptr); |
||||||
|
return nullptr; |
||||||
|
} |
||||||
|
|
||||||
|
virtual std::unique_ptr<ParsedConfig> ParsePerMethodParams( |
||||||
|
const Json& /* json */, grpc_error** error) { |
||||||
|
// Avoid unused parameter warning on debug-only parameter
|
||||||
|
(void)error; |
||||||
|
GPR_DEBUG_ASSERT(error != nullptr); |
||||||
|
return nullptr; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
static constexpr int kNumPreallocatedParsers = 4; |
||||||
|
typedef absl::InlinedVector<std::unique_ptr<ParsedConfig>, |
||||||
|
kNumPreallocatedParsers> |
||||||
|
ParsedConfigVector; |
||||||
|
|
||||||
|
static void Init(); |
||||||
|
static void Shutdown(); |
||||||
|
|
||||||
|
/// Globally register a service config parser. On successful registration, it
|
||||||
|
/// returns the index at which the parser was registered. On failure, -1 is
|
||||||
|
/// returned. Each new service config update will go through all the
|
||||||
|
/// registered parser. Each parser is responsible for reading the service
|
||||||
|
/// config json and returning a parsed config. This parsed config can later be
|
||||||
|
/// retrieved using the same index that was returned at registration time.
|
||||||
|
static size_t RegisterParser(std::unique_ptr<Parser> parser); |
||||||
|
|
||||||
|
static ParsedConfigVector ParseGlobalParameters(const Json& json, |
||||||
|
grpc_error** error); |
||||||
|
|
||||||
|
static ParsedConfigVector ParsePerMethodParameters(const Json& json, |
||||||
|
grpc_error** error); |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace grpc_core
|
||||||
|
|
||||||
|
#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H */ |
Loading…
Reference in new issue