| 1 |
// |
| 2 |
// Copyright 2015 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/load_balancing/lb_policy_registry.h" |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <map> |
| 23 |
#include <string> |
| 24 |
#include <utility> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "absl/status/status.h" |
| 28 |
#include "absl/strings/str_cat.h" |
| 29 |
#include "absl/strings/str_format.h" |
| 30 |
#include "absl/strings/str_join.h" |
| 31 |
#include "absl/strings/string_view.h" |
| 32 |
|
| 33 |
#include <grpc/support/log.h> |
| 34 |
|
| 35 |
#include "src/core/lib/load_balancing/lb_policy.h" |
| 36 |
|
| 37 |
namespace grpc_core { |
| 38 |
|
| 39 |
// |
| 40 |
// LoadBalancingPolicyRegistry::Builder |
| 41 |
// |
| 42 |
|
| 43 |
void LoadBalancingPolicyRegistry::Builder::RegisterLoadBalancingPolicyFactory( |
| 44 |
std::unique_ptr<LoadBalancingPolicyFactory> factory) { |
| 45 |
gpr_log(GPR_DEBUG, "registering LB policy factory for \"%s\"", |
| 46 |
std::string(factory->name()).c_str()); |
| 47 |
GPR_ASSERT(factories_.find(factory->name()) == factories_.end()); |
| 48 |
factories_.emplace(factory->name(), std::move(factory)); |
| 49 |
} |
| 50 |
|
| 51 |
LoadBalancingPolicyRegistry LoadBalancingPolicyRegistry::Builder::Build() { |
| 52 |
LoadBalancingPolicyRegistry out; |
| 53 |
out.factories_ = std::move(factories_); |
| 54 |
return out; |
| 55 |
} |
| 56 |
|
| 57 |
// |
| 58 |
// LoadBalancingPolicyRegistry |
| 59 |
// |
| 60 |
|
| 61 |
LoadBalancingPolicyFactory* |
| 62 |
LoadBalancingPolicyRegistry::GetLoadBalancingPolicyFactory( |
| 63 |
absl::string_view name) const { |
| 64 |
auto it = factories_.find(name); |
| 65 |
if (it == factories_.end()) return nullptr; |
| 66 |
return it->second.get(); |
| 67 |
} |
| 68 |
|
| 69 |
OrphanablePtr<LoadBalancingPolicy> |
| 70 |
LoadBalancingPolicyRegistry::CreateLoadBalancingPolicy( |
| 71 |
absl::string_view name, LoadBalancingPolicy::Args args) const { |
| 72 |
// Find factory. |
| 73 |
LoadBalancingPolicyFactory* factory = GetLoadBalancingPolicyFactory(name); |
| 74 |
if (factory == nullptr) return nullptr; // Specified name not found. |
| 75 |
// Create policy via factory. |
| 76 |
return factory->CreateLoadBalancingPolicy(std::move(args)); |
| 77 |
} |
| 78 |
|
| 79 |
bool LoadBalancingPolicyRegistry::LoadBalancingPolicyExists( |
| 80 |
absl::string_view name, bool* requires_config) const { |
| 81 |
auto* factory = GetLoadBalancingPolicyFactory(name); |
| 82 |
if (factory == nullptr) return false; |
| 83 |
// If requested, check if the load balancing policy allows an empty config. |
| 84 |
if (requires_config != nullptr) { |
| 85 |
auto config = factory->ParseLoadBalancingConfig(Json()); |
| 86 |
*requires_config = !config.ok(); |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
// Returns the JSON node of policy (with both policy name and config content) |
| 92 |
// given the JSON node of a LoadBalancingConfig array. |
| 93 |
absl::StatusOr<Json::Object::const_iterator> |
| 94 |
LoadBalancingPolicyRegistry::ParseLoadBalancingConfigHelper( |
| 95 |
const Json& lb_config_array) const { |
| 96 |
if (lb_config_array.type() != Json::Type::ARRAY) { |
| 97 |
return absl::InvalidArgumentError("type should be array"); |
| 98 |
} |
| 99 |
// Find the first LB policy that this client supports. |
| 100 |
std::vector<absl::string_view> policies_tried; |
| 101 |
for (const Json& lb_config : lb_config_array.array_value()) { |
| 102 |
if (lb_config.type() != Json::Type::OBJECT) { |
| 103 |
return absl::InvalidArgumentError("child entry should be of type object"); |
| 104 |
} |
| 105 |
if (lb_config.object_value().empty()) { |
| 106 |
return absl::InvalidArgumentError("no policy found in child entry"); |
| 107 |
} |
| 108 |
if (lb_config.object_value().size() > 1) { |
| 109 |
return absl::InvalidArgumentError("oneOf violation"); |
| 110 |
} |
| 111 |
auto it = lb_config.object_value().begin(); |
| 112 |
if (it->second.type() != Json::Type::OBJECT) { |
| 113 |
return absl::InvalidArgumentError("child entry should be of type object"); |
| 114 |
} |
| 115 |
// If we support this policy, then select it. |
| 116 |
if (LoadBalancingPolicyRegistry::LoadBalancingPolicyExists( |
| 117 |
it->first.c_str(), nullptr)) { |
| 118 |
return it; |
| 119 |
} |
| 120 |
policies_tried.push_back(it->first); |
| 121 |
} |
| 122 |
return absl::FailedPreconditionError(absl::StrCat( |
| 123 |
"No known policies in list: ", absl::StrJoin(policies_tried, " "))); |
| 124 |
} |
| 125 |
|
| 126 |
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
| 127 |
LoadBalancingPolicyRegistry::ParseLoadBalancingConfig(const Json& json) const { |
| 128 |
auto policy = ParseLoadBalancingConfigHelper(json); |
| 129 |
if (!policy.ok()) return policy.status(); |
| 130 |
// Find factory. |
| 131 |
LoadBalancingPolicyFactory* factory = |
| 132 |
GetLoadBalancingPolicyFactory((*policy)->first.c_str()); |
| 133 |
if (factory == nullptr) { |
| 134 |
return absl::FailedPreconditionError(absl::StrFormat( |
| 135 |
"Factory not found for policy \"%s\"", (*policy)->first)); |
| 136 |
} |
| 137 |
// Parse load balancing config via factory. |
| 138 |
return factory->ParseLoadBalancingConfig((*policy)->second); |
| 139 |
} |
| 140 |
|
| 141 |
} // namespace grpc_core |