1 // 2 // 3 // Copyright 2018 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPC_SRC_CORE_XDS_XDS_CLIENT_XDS_LOCALITY_H 20 #define GRPC_SRC_CORE_XDS_XDS_CLIENT_XDS_LOCALITY_H 21 22 #include <string> 23 #include <utility> 24 25 #include "absl/strings/str_format.h" 26 #include "absl/strings/string_view.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/ref_counted_string.h" 31 #include "src/core/util/useful.h" 32 33 namespace grpc_core { 34 35 // An xDS locality name. 36 class XdsLocalityName final : public RefCounted<XdsLocalityName> { 37 public: 38 struct Less { operatorLess39 bool operator()(const XdsLocalityName* lhs, 40 const XdsLocalityName* rhs) const { 41 if (lhs == nullptr || rhs == nullptr) return QsortCompare(lhs, rhs); 42 return lhs->Compare(*rhs) < 0; 43 } 44 operatorLess45 bool operator()(const RefCountedPtr<XdsLocalityName>& lhs, 46 const RefCountedPtr<XdsLocalityName>& rhs) const { 47 return (*this)(lhs.get(), rhs.get()); 48 } 49 }; 50 XdsLocalityName(std::string region,std::string zone,std::string sub_zone)51 XdsLocalityName(std::string region, std::string zone, std::string sub_zone) 52 : region_(std::move(region)), 53 zone_(std::move(zone)), 54 sub_zone_(std::move(sub_zone)), 55 human_readable_string_( 56 absl::StrFormat("{region=\"%s\", zone=\"%s\", sub_zone=\"%s\"}", 57 region_, zone_, sub_zone_)) {} 58 59 bool operator==(const XdsLocalityName& other) const { 60 return region_ == other.region_ && zone_ == other.zone_ && 61 sub_zone_ == other.sub_zone_; 62 } 63 64 bool operator!=(const XdsLocalityName& other) const { 65 return !(*this == other); 66 } 67 Compare(const XdsLocalityName & other)68 int Compare(const XdsLocalityName& other) const { 69 int cmp_result = region_.compare(other.region_); 70 if (cmp_result != 0) return cmp_result; 71 cmp_result = zone_.compare(other.zone_); 72 if (cmp_result != 0) return cmp_result; 73 return sub_zone_.compare(other.sub_zone_); 74 } 75 region()76 const std::string& region() const { return region_; } zone()77 const std::string& zone() const { return zone_; } sub_zone()78 const std::string& sub_zone() const { return sub_zone_; } 79 human_readable_string()80 const RefCountedStringValue& human_readable_string() const { 81 return human_readable_string_; 82 } 83 84 // Channel args traits. ChannelArgName()85 static absl::string_view ChannelArgName() { 86 return GRPC_ARG_NO_SUBCHANNEL_PREFIX "xds_locality_name"; 87 } ChannelArgsCompare(const XdsLocalityName * a,const XdsLocalityName * b)88 static int ChannelArgsCompare(const XdsLocalityName* a, 89 const XdsLocalityName* b) { 90 return a->Compare(*b); 91 } 92 93 private: 94 std::string region_; 95 std::string zone_; 96 std::string sub_zone_; 97 RefCountedStringValue human_readable_string_; 98 }; 99 100 } // namespace grpc_core 101 102 #endif // GRPC_SRC_CORE_XDS_XDS_CLIENT_XDS_LOCALITY_H 103