1 // 2 // Copyright 2018 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_XDS_GRPC_XDS_ENDPOINT_H 18 #define GRPC_SRC_CORE_XDS_GRPC_XDS_ENDPOINT_H 19 20 #include <map> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "absl/base/thread_annotations.h" 26 #include "absl/random/random.h" 27 #include "src/core/resolver/endpoint_addresses.h" 28 #include "src/core/util/ref_counted.h" 29 #include "src/core/util/ref_counted_ptr.h" 30 #include "src/core/util/sync.h" 31 #include "src/core/xds/xds_client/xds_locality.h" 32 #include "src/core/xds/xds_client/xds_resource_type.h" 33 #include "src/core/xds/xds_client/xds_resource_type_impl.h" 34 35 // Per-endpoint channel arg key for xDS-configured HTTP CONNECT proxy. 36 #define GRPC_ARG_XDS_HTTP_PROXY "grpc.internal.xds_http_proxy" 37 38 namespace grpc_core { 39 40 struct XdsEndpointResource : public XdsResourceType::ResourceData { 41 struct Priority { 42 struct Locality { 43 RefCountedPtr<XdsLocalityName> name; 44 uint32_t lb_weight; 45 EndpointAddressesList endpoints; 46 47 bool operator==(const Locality& other) const { 48 return *name == *other.name && lb_weight == other.lb_weight && 49 endpoints == other.endpoints; 50 } 51 bool operator!=(const Locality& other) const { return !(*this == other); } 52 std::string ToString() const; 53 }; 54 55 std::map<XdsLocalityName*, Locality, XdsLocalityName::Less> localities; 56 57 bool operator==(const Priority& other) const; 58 bool operator!=(const Priority& other) const { return !(*this == other); } 59 std::string ToString() const; 60 }; 61 using PriorityList = std::vector<Priority>; 62 63 // There are two phases of accessing this class's content: 64 // 1. to initialize in the control plane combiner; 65 // 2. to use in the data plane combiner. 66 // So no additional synchronization is needed. 67 class DropConfig final : public RefCounted<DropConfig> { 68 public: 69 struct DropCategory { 70 bool operator==(const DropCategory& other) const { 71 return name == other.name && 72 parts_per_million == other.parts_per_million; 73 } 74 75 std::string name; 76 const uint32_t parts_per_million; 77 }; 78 79 using DropCategoryList = std::vector<DropCategory>; 80 AddCategoryXdsEndpointResource81 void AddCategory(std::string name, uint32_t parts_per_million) { 82 drop_category_list_.emplace_back( 83 DropCategory{std::move(name), parts_per_million}); 84 if (parts_per_million == 1000000) drop_all_ = true; 85 } 86 87 // The only method invoked from outside the WorkSerializer (used in 88 // the data plane). 89 bool ShouldDrop(const std::string** category_name); 90 drop_category_listXdsEndpointResource91 const DropCategoryList& drop_category_list() const { 92 return drop_category_list_; 93 } 94 drop_allXdsEndpointResource95 bool drop_all() const { return drop_all_; } 96 97 bool operator==(const DropConfig& other) const { 98 return drop_category_list_ == other.drop_category_list_; 99 } 100 bool operator!=(const DropConfig& other) const { return !(*this == other); } 101 102 std::string ToString() const; 103 104 private: 105 DropCategoryList drop_category_list_; 106 bool drop_all_ = false; 107 108 // TODO(roth): Consider using a separate thread-local BitGen for each CPU 109 // to avoid the need for this mutex. 110 Mutex mu_; 111 absl::BitGen bit_gen_ ABSL_GUARDED_BY(&mu_); 112 }; 113 114 PriorityList priorities; 115 RefCountedPtr<DropConfig> drop_config; 116 117 bool operator==(const XdsEndpointResource& other) const { 118 if (priorities != other.priorities) return false; 119 if (drop_config == nullptr) return other.drop_config == nullptr; 120 if (other.drop_config == nullptr) return false; 121 return *drop_config == *other.drop_config; 122 } 123 std::string ToString() const; 124 }; 125 126 } // namespace grpc_core 127 128 #endif // GRPC_SRC_CORE_XDS_GRPC_XDS_ENDPOINT_H 129