| 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_CORE_EXT_XDS_XDS_ENDPOINT_H |
| 18 |
#define GRPC_CORE_EXT_XDS_XDS_ENDPOINT_H |
| 19 |
|
| 20 |
#include <grpc/support/port_platform.h> |
| 21 |
|
| 22 |
#include <stdint.h> |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <map> |
| 26 |
#include <memory> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "absl/strings/string_view.h" |
| 32 |
#include "envoy/config/endpoint/v3/endpoint.upbdefs.h" |
| 33 |
#include "upb/def.h" |
| 34 |
|
| 35 |
#include "src/core/ext/xds/xds_client.h" |
| 36 |
#include "src/core/ext/xds/xds_client_stats.h" |
| 37 |
#include "src/core/ext/xds/xds_resource_type.h" |
| 38 |
#include "src/core/ext/xds/xds_resource_type_impl.h" |
| 39 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 40 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 41 |
#include "src/core/lib/resolver/server_address.h" |
| 42 |
|
| 43 |
namespace grpc_core { |
| 44 |
|
| 45 |
struct XdsEndpointResource : public XdsResourceType::ResourceData { |
| 46 |
struct Priority { |
| 47 |
struct Locality { |
| 48 |
RefCountedPtr<XdsLocalityName> name; |
| 49 |
uint32_t lb_weight; |
| 50 |
ServerAddressList endpoints; |
| 51 |
|
| 52 |
bool operator==(const Locality& other) const { |
| 53 |
return *name == *other.name && lb_weight == other.lb_weight && |
| 54 |
endpoints == other.endpoints; |
| 55 |
} |
| 56 |
bool operator!=(const Locality& other) const { return !(*this == other); } |
| 57 |
std::string ToString() const; |
| 58 |
}; |
| 59 |
|
| 60 |
std::map<XdsLocalityName*, Locality, XdsLocalityName::Less> localities; |
| 61 |
|
| 62 |
bool operator==(const Priority& other) const; |
| 63 |
std::string ToString() const; |
| 64 |
}; |
| 65 |
using PriorityList = std::vector<Priority>; |
| 66 |
|
| 67 |
// There are two phases of accessing this class's content: |
| 68 |
// 1. to initialize in the control plane combiner; |
| 69 |
// 2. to use in the data plane combiner. |
| 70 |
// So no additional synchronization is needed. |
| 71 |
class DropConfig : public RefCounted<DropConfig> { |
| 72 |
public: |
| 73 |
struct DropCategory { |
| 74 |
bool operator==(const DropCategory& other) const { |
| 75 |
return name == other.name && |
| 76 |
parts_per_million == other.parts_per_million; |
| 77 |
} |
| 78 |
|
| 79 |
std::string name; |
| 80 |
const uint32_t parts_per_million; |
| 81 |
}; |
| 82 |
|
| 83 |
using DropCategoryList = std::vector<DropCategory>; |
| 84 |
|
| 85 |
void AddCategory(std::string name, uint32_t parts_per_million) { |
| 86 |
drop_category_list_.emplace_back( |
| 87 |
DropCategory{std::move(name), parts_per_million}); |
| 88 |
if (parts_per_million == 1000000) drop_all_ = true; |
| 89 |
} |
| 90 |
|
| 91 |
// The only method invoked from outside the WorkSerializer (used in |
| 92 |
// the data plane). |
| 93 |
bool ShouldDrop(const std::string** category_name) const; |
| 94 |
|
| 95 |
const DropCategoryList& drop_category_list() const { |
| 96 |
return drop_category_list_; |
| 97 |
} |
| 98 |
|
| 99 |
bool drop_all() const { return drop_all_; } |
| 100 |
|
| 101 |
bool operator==(const DropConfig& other) const { |
| 102 |
return drop_category_list_ == other.drop_category_list_; |
| 103 |
} |
| 104 |
bool operator!=(const DropConfig& other) const { return !(*this == other); } |
| 105 |
|
| 106 |
std::string ToString() const; |
| 107 |
|
| 108 |
private: |
| 109 |
DropCategoryList drop_category_list_; |
| 110 |
bool drop_all_ = false; |
| 111 |
}; |
| 112 |
|
| 113 |
PriorityList priorities; |
| 114 |
RefCountedPtr<DropConfig> drop_config; |
| 115 |
|
| 116 |
bool operator==(const XdsEndpointResource& other) const { |
| 117 |
return priorities == other.priorities && *drop_config == *other.drop_config; |
| 118 |
} |
| 119 |
std::string ToString() const; |
| 120 |
}; |
| 121 |
|
| 122 |
class XdsEndpointResourceType |
| 123 |
: public XdsResourceTypeImpl<XdsEndpointResourceType, XdsEndpointResource> { |
| 124 |
public: |
| 125 |
absl::string_view type_url() const override { |
| 126 |
return "envoy.config.endpoint.v3.ClusterLoadAssignment"; |
| 127 |
} |
| 128 |
|
| 129 |
DecodeResult Decode(const XdsResourceType::DecodeContext& context, |
| 130 |
absl::string_view serialized_resource) const override; |
| 131 |
|
| 132 |
void InitUpbSymtab(XdsClient*, upb_DefPool* symtab) const override { |
| 133 |
envoy_config_endpoint_v3_ClusterLoadAssignment_getmsgdef(symtab); |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
} // namespace grpc_core |
| 138 |
|
| 139 |
#endif // GRPC_CORE_EXT_XDS_XDS_ENDPOINT_H |