|
|
@ -18,64 +18,77 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "src/core/ext/filters/client_channel/lb_policy/address_filtering.h" |
|
|
|
#include "src/core/ext/filters/client_channel/lb_policy/address_filtering.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "absl/strings/str_cat.h" |
|
|
|
|
|
|
|
#include "absl/strings/str_join.h" |
|
|
|
|
|
|
|
|
|
|
|
#include "src/core/lib/channel/channel_args.h" |
|
|
|
#include "src/core/lib/channel/channel_args.h" |
|
|
|
|
|
|
|
|
|
|
|
#define GRPC_ARG_HIERARCHICAL_PATH "grpc.internal.address.hierarchical_path" |
|
|
|
#define GRPC_ARG_HIERARCHICAL_PATH "grpc.internal.address.hierarchical_path" |
|
|
|
|
|
|
|
|
|
|
|
namespace grpc_core { |
|
|
|
namespace grpc_core { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char* kHierarchicalPathAttributeKey = "hierarchical_path"; |
|
|
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
|
|
void* HierarchicalPathCopy(void* p) { |
|
|
|
class HierarchicalPathAttribute : public ServerAddress::AttributeInterface { |
|
|
|
std::vector<std::string>* path = static_cast<std::vector<std::string>*>(p); |
|
|
|
public: |
|
|
|
return static_cast<void*>(new std::vector<std::string>(*path)); |
|
|
|
explicit HierarchicalPathAttribute(std::vector<std::string> path) |
|
|
|
} |
|
|
|
: path_(std::move(path)) {} |
|
|
|
|
|
|
|
|
|
|
|
void HierarchicalPathDestroy(void* p) { |
|
|
|
std::unique_ptr<AttributeInterface> Copy() const override { |
|
|
|
std::vector<std::string>* path = static_cast<std::vector<std::string>*>(p); |
|
|
|
return absl::make_unique<HierarchicalPathAttribute>(path_); |
|
|
|
delete path; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int HierarchicalPathCompare(void* p1, void* p2) { |
|
|
|
int Cmp(const AttributeInterface* other) const override { |
|
|
|
std::vector<std::string>* path1 = static_cast<std::vector<std::string>*>(p1); |
|
|
|
const std::vector<std::string>& other_path = |
|
|
|
std::vector<std::string>* path2 = static_cast<std::vector<std::string>*>(p2); |
|
|
|
static_cast<const HierarchicalPathAttribute*>(other)->path_; |
|
|
|
for (size_t i = 0; i < path1->size(); ++i) { |
|
|
|
for (size_t i = 0; i < path_.size(); ++i) { |
|
|
|
if (path2->size() == i) return 1; |
|
|
|
if (other_path.size() == i) return 1; |
|
|
|
int r = (*path1)[i].compare((*path2)[i]); |
|
|
|
int r = path_[i].compare(other_path[i]); |
|
|
|
if (r != 0) return r; |
|
|
|
if (r != 0) return r; |
|
|
|
} |
|
|
|
} |
|
|
|
if (path2->size() > path1->size()) return -1; |
|
|
|
if (other_path.size() > path_.size()) return -1; |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string ToString() const override { |
|
|
|
|
|
|
|
return absl::StrCat("[", absl::StrJoin(path_, ", "), "]"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const grpc_arg_pointer_vtable hierarchical_path_arg_vtable = { |
|
|
|
const std::vector<std::string>& path() const { return path_; } |
|
|
|
HierarchicalPathCopy, HierarchicalPathDestroy, HierarchicalPathCompare}; |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
std::vector<std::string> path_; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
grpc_arg MakeHierarchicalPathArg(const std::vector<std::string>& path) { |
|
|
|
std::unique_ptr<ServerAddress::AttributeInterface> |
|
|
|
return grpc_channel_arg_pointer_create( |
|
|
|
MakeHierarchicalPathAttribute(std::vector<std::string> path) { |
|
|
|
const_cast<char*>(GRPC_ARG_HIERARCHICAL_PATH), |
|
|
|
return absl::make_unique<HierarchicalPathAttribute>(std::move(path)); |
|
|
|
const_cast<std::vector<std::string>*>(&path), |
|
|
|
|
|
|
|
&hierarchical_path_arg_vtable); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
HierarchicalAddressMap MakeHierarchicalAddressMap( |
|
|
|
HierarchicalAddressMap MakeHierarchicalAddressMap( |
|
|
|
const ServerAddressList& addresses) { |
|
|
|
const ServerAddressList& addresses) { |
|
|
|
HierarchicalAddressMap result; |
|
|
|
HierarchicalAddressMap result; |
|
|
|
for (const ServerAddress& address : addresses) { |
|
|
|
for (const ServerAddress& address : addresses) { |
|
|
|
auto* path = grpc_channel_args_find_pointer<std::vector<std::string>>( |
|
|
|
const HierarchicalPathAttribute* path_attribute = |
|
|
|
address.args(), GRPC_ARG_HIERARCHICAL_PATH); |
|
|
|
static_cast<const HierarchicalPathAttribute*>( |
|
|
|
if (path == nullptr || path->empty()) continue; |
|
|
|
address.GetAttribute(kHierarchicalPathAttributeKey)); |
|
|
|
auto it = path->begin(); |
|
|
|
if (path_attribute == nullptr) continue; |
|
|
|
|
|
|
|
const std::vector<std::string>& path = path_attribute->path(); |
|
|
|
|
|
|
|
auto it = path.begin(); |
|
|
|
ServerAddressList& target_list = result[*it]; |
|
|
|
ServerAddressList& target_list = result[*it]; |
|
|
|
|
|
|
|
std::unique_ptr<HierarchicalPathAttribute> new_attribute; |
|
|
|
++it; |
|
|
|
++it; |
|
|
|
std::vector<std::string> remaining_path(it, path->end()); |
|
|
|
if (it != path.end()) { |
|
|
|
const char* name_to_remove = GRPC_ARG_HIERARCHICAL_PATH; |
|
|
|
std::vector<std::string> remaining_path(it, path.end()); |
|
|
|
grpc_arg new_arg = MakeHierarchicalPathArg(remaining_path); |
|
|
|
new_attribute = absl::make_unique<HierarchicalPathAttribute>( |
|
|
|
grpc_channel_args* new_args = grpc_channel_args_copy_and_add_and_remove( |
|
|
|
std::move(remaining_path)); |
|
|
|
address.args(), &name_to_remove, 1, &new_arg, 1); |
|
|
|
} |
|
|
|
target_list.emplace_back(address.address(), new_args); |
|
|
|
target_list.emplace_back(address.WithAttribute( |
|
|
|
|
|
|
|
kHierarchicalPathAttributeKey, std::move(new_attribute))); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|