xref: /aosp_15_r20/external/grpc-grpc/src/core/load_balancing/address_filtering.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 #ifndef GRPC_SRC_CORE_LOAD_BALANCING_ADDRESS_FILTERING_H
18 #define GRPC_SRC_CORE_LOAD_BALANCING_ADDRESS_FILTERING_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <map>
23 #include <memory>
24 #include <utility>
25 #include <vector>
26 
27 #include "absl/status/statusor.h"
28 #include "absl/strings/string_view.h"
29 
30 #include "src/core/lib/gprpp/ref_counted.h"
31 #include "src/core/lib/gprpp/ref_counted_string.h"
32 #include "src/core/resolver/endpoint_addresses.h"
33 
34 // The resolver returns a flat list of addresses.  When a hierarchy of
35 // LB policies is in use, each leaf of the hierarchy will need a
36 // different subset of those addresses.  This library provides a
37 // mechanism for determining which address is passed to which leaf
38 // policy.
39 //
40 // Each address will have an associated path that indicates which child
41 // it should be sent to at each level of the hierarchy to wind up at the
42 // right leaf policy.  Each LB policy will look at the first element of
43 // the path of each address to determine which child to send the address
44 // to.  It will then remove that first element when passing the address
45 // down to its child.
46 //
47 // For example, consider the following LB policy hierarchy:
48 //
49 // - priority
50 //   - child0 (weighted_target)
51 //     - localityA (round_robin)
52 //     - localityB (round_robin)
53 //   - child1 (weighted_target)
54 //     - localityC (round_robin)
55 //     - localityD (round_robin)
56 //
57 // Now consider the following addresses:
58 // - 10.0.0.1:80 path=["child0", "localityA"]
59 // - 10.0.0.2:80 path=["child0", "localityB"]
60 // - 10.0.0.3:80 path=["child1", "localityC"]
61 // - 10.0.0.4:80 path=["child1", "localityD"]
62 //
63 // The priority policy will split this up into two lists, one for each
64 // of its children:
65 // - child0:
66 //   - 10.0.0.1:80 path=["localityA"]
67 //   - 10.0.0.2:80 path=["localityB"]
68 // - child1:
69 //   - 10.0.0.3:80 path=["localityC"]
70 //   - 10.0.0.4:80 path=["localityD"]
71 //
72 // The weighted_target policy for child0 will split its list up into two
73 // lists, one for each of its children:
74 // - localityA:
75 //   - 10.0.0.1:80 path=[]
76 // - localityB:
77 //   - 10.0.0.2:80 path=[]
78 //
79 // Similarly, the weighted_target policy for child1 will split its list
80 // up into two lists, one for each of its children:
81 // - localityC:
82 //   - 10.0.0.3:80 path=[]
83 // - localityD:
84 //   - 10.0.0.4:80 path=[]
85 
86 namespace grpc_core {
87 
88 // An address channel arg containing the hierarchical path
89 // to be associated with the address.
90 class HierarchicalPathArg final : public RefCounted<HierarchicalPathArg> {
91  public:
HierarchicalPathArg(std::vector<RefCountedStringValue> path)92   explicit HierarchicalPathArg(std::vector<RefCountedStringValue> path)
93       : path_(std::move(path)) {}
94 
95   // Channel arg traits methods.
96   static absl::string_view ChannelArgName();
97   static int ChannelArgsCompare(const HierarchicalPathArg* a,
98                                 const HierarchicalPathArg* b);
99 
path()100   const std::vector<RefCountedStringValue>& path() const { return path_; }
101 
102  private:
103   std::vector<RefCountedStringValue> path_;
104 };
105 
106 // A map from the next path element to the endpoint addresses that fall
107 // under that path element.
108 using HierarchicalAddressMap =
109     std::map<RefCountedStringValue, std::shared_ptr<EndpointAddressesIterator>,
110              RefCountedStringValueLessThan>;
111 
112 // Splits up the addresses into a separate list for each child.
113 absl::StatusOr<HierarchicalAddressMap> MakeHierarchicalAddressMap(
114     absl::StatusOr<std::shared_ptr<EndpointAddressesIterator>> addresses);
115 
116 }  // namespace grpc_core
117 
118 #endif  // GRPC_SRC_CORE_LOAD_BALANCING_ADDRESS_FILTERING_H
119