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