• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2020 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 "src/core/load_balancing/address_filtering.h"
18 
19 #include <grpc/support/port_platform.h>
20 #include <stddef.h>
21 
22 #include <utility>
23 
24 #include "absl/functional/function_ref.h"
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/iomgr/resolved_address.h"
27 #include "src/core/util/ref_counted_ptr.h"
28 
29 namespace grpc_core {
30 
ChannelArgName()31 absl::string_view HierarchicalPathArg::ChannelArgName() {
32   return GRPC_ARG_NO_SUBCHANNEL_PREFIX "address.hierarchical_path";
33 }
34 
ChannelArgsCompare(const HierarchicalPathArg * a,const HierarchicalPathArg * b)35 int HierarchicalPathArg::ChannelArgsCompare(const HierarchicalPathArg* a,
36                                             const HierarchicalPathArg* b) {
37   for (size_t i = 0; i < a->path_.size(); ++i) {
38     if (b->path_.size() == i) return 1;
39     int r = a->path_[i].as_string_view().compare(b->path_[i].as_string_view());
40     if (r != 0) return r;
41   }
42   if (b->path_.size() > a->path_.size()) return -1;
43   return 0;
44 }
45 
46 namespace {
47 
48 class HierarchicalAddressIterator final : public EndpointAddressesIterator {
49  public:
HierarchicalAddressIterator(std::shared_ptr<EndpointAddressesIterator> parent_it,RefCountedStringValue child_name)50   HierarchicalAddressIterator(
51       std::shared_ptr<EndpointAddressesIterator> parent_it,
52       RefCountedStringValue child_name)
53       : parent_it_(std::move(parent_it)), child_name_(std::move(child_name)) {}
54 
ForEach(absl::FunctionRef<void (const EndpointAddresses &)> callback) const55   void ForEach(absl::FunctionRef<void(const EndpointAddresses&)> callback)
56       const override {
57     RefCountedPtr<HierarchicalPathArg> remaining_path_attr;
58     parent_it_->ForEach([&](const EndpointAddresses& endpoint) {
59       const auto* path_arg = endpoint.args().GetObject<HierarchicalPathArg>();
60       if (path_arg == nullptr) return;
61       const std::vector<RefCountedStringValue>& path = path_arg->path();
62       auto it = path.begin();
63       if (it == path.end()) return;
64       if (*it != child_name_) return;
65       ChannelArgs args = endpoint.args();
66       ++it;
67       if (it != path.end()) {
68         std::vector<RefCountedStringValue> remaining_path(it, path.end());
69         if (remaining_path_attr == nullptr ||
70             remaining_path_attr->path() != remaining_path) {
71           remaining_path_attr =
72               MakeRefCounted<HierarchicalPathArg>(std::move(remaining_path));
73         }
74         args = args.SetObject(remaining_path_attr);
75       }
76       callback(EndpointAddresses(endpoint.addresses(), args));
77     });
78   }
79 
80  private:
81   std::shared_ptr<EndpointAddressesIterator> parent_it_;
82   RefCountedStringValue child_name_;
83 };
84 
85 }  // namespace
86 
MakeHierarchicalAddressMap(absl::StatusOr<std::shared_ptr<EndpointAddressesIterator>> addresses)87 absl::StatusOr<HierarchicalAddressMap> MakeHierarchicalAddressMap(
88     absl::StatusOr<std::shared_ptr<EndpointAddressesIterator>> addresses) {
89   if (!addresses.ok()) return addresses.status();
90   HierarchicalAddressMap result;
91   (*addresses)->ForEach([&](const EndpointAddresses& endpoint) {
92     const auto* path_arg = endpoint.args().GetObject<HierarchicalPathArg>();
93     if (path_arg == nullptr) return;
94     const std::vector<RefCountedStringValue>& path = path_arg->path();
95     auto it = path.begin();
96     if (it == path.end()) return;
97     auto& target_list = result[*it];
98     if (target_list == nullptr) {
99       target_list =
100           std::make_shared<HierarchicalAddressIterator>(*addresses, *it);
101     }
102   });
103   return result;
104 }
105 
106 }  // namespace grpc_core
107