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_CLUSTER_H
18 #define GRPC_SRC_CORE_XDS_GRPC_XDS_CLUSTER_H
19
20 #include <string>
21 #include <vector>
22
23 #include "absl/container/flat_hash_map.h"
24 #include "absl/types/optional.h"
25 #include "absl/types/variant.h"
26 #include "src/core/load_balancing/outlier_detection/outlier_detection.h"
27 #include "src/core/util/json/json.h"
28 #include "src/core/xds/grpc/xds_common_types.h"
29 #include "src/core/xds/grpc/xds_health_status.h"
30 #include "src/core/xds/grpc/xds_metadata.h"
31 #include "src/core/xds/grpc/xds_server_grpc.h"
32 #include "src/core/xds/xds_client/xds_backend_metric_propagation.h"
33 #include "src/core/xds/xds_client/xds_resource_type.h"
34 #include "src/core/xds/xds_client/xds_resource_type_impl.h"
35
36 namespace grpc_core {
37
LrsServersEqual(const std::shared_ptr<const GrpcXdsServer> & lrs_server1,const std::shared_ptr<const GrpcXdsServer> & lrs_server2)38 inline bool LrsServersEqual(
39 const std::shared_ptr<const GrpcXdsServer>& lrs_server1,
40 const std::shared_ptr<const GrpcXdsServer>& lrs_server2) {
41 if (lrs_server1 == nullptr) return lrs_server2 == nullptr;
42 if (lrs_server2 == nullptr) return false;
43 // Neither one is null, so compare them.
44 return *lrs_server1 == *lrs_server2;
45 }
46
LrsBackendMetricPropagationEqual(const RefCountedPtr<const BackendMetricPropagation> & p1,const RefCountedPtr<const BackendMetricPropagation> & p2)47 inline bool LrsBackendMetricPropagationEqual(
48 const RefCountedPtr<const BackendMetricPropagation>& p1,
49 const RefCountedPtr<const BackendMetricPropagation>& p2) {
50 if (p1 == nullptr) return p2 == nullptr;
51 if (p2 == nullptr) return false;
52 // Neither one is null, so compare them.
53 return *p1 == *p2;
54 }
55
56 struct XdsClusterResource : public XdsResourceType::ResourceData {
57 struct Eds {
58 // If empty, defaults to the cluster name.
59 std::string eds_service_name;
60
61 bool operator==(const Eds& other) const {
62 return eds_service_name == other.eds_service_name;
63 }
64 };
65
66 struct LogicalDns {
67 // The hostname to lookup in DNS.
68 std::string hostname;
69
70 bool operator==(const LogicalDns& other) const {
71 return hostname == other.hostname;
72 }
73 };
74
75 struct Aggregate {
76 // Prioritized list of cluster names.
77 std::vector<std::string> prioritized_cluster_names;
78
79 bool operator==(const Aggregate& other) const {
80 return prioritized_cluster_names == other.prioritized_cluster_names;
81 }
82 };
83
84 absl::variant<Eds, LogicalDns, Aggregate> type;
85
86 // The LB policy to use for locality and endpoint picking.
87 Json::Array lb_policy_config;
88
89 // Note: Remaining fields are not used for aggregate clusters.
90
91 // The LRS server to use for load reporting.
92 // If null, load reporting will be disabled.
93 std::shared_ptr<const GrpcXdsServer> lrs_load_reporting_server;
94 // The set of metrics to propagate from ORCA to LRS.
95 RefCountedPtr<const BackendMetricPropagation> lrs_backend_metric_propagation;
96
97 bool use_http_connect = false;
98
99 // Tls Context used by clients
100 CommonTlsContext common_tls_context;
101
102 // Connection idle timeout. Currently used only for SSA.
103 Duration connection_idle_timeout = Duration::Hours(1);
104
105 // Maximum number of outstanding requests can be made to the upstream
106 // cluster.
107 uint32_t max_concurrent_requests = 1024;
108
109 absl::optional<OutlierDetectionConfig> outlier_detection;
110
111 XdsHealthStatusSet override_host_statuses;
112
113 XdsMetadataMap metadata;
114
115 bool operator==(const XdsClusterResource& other) const {
116 return type == other.type && lb_policy_config == other.lb_policy_config &&
117 LrsServersEqual(lrs_load_reporting_server,
118 other.lrs_load_reporting_server) &&
119 LrsBackendMetricPropagationEqual(
120 lrs_backend_metric_propagation,
121 other.lrs_backend_metric_propagation) &&
122 common_tls_context == other.common_tls_context &&
123 connection_idle_timeout == other.connection_idle_timeout &&
124 max_concurrent_requests == other.max_concurrent_requests &&
125 outlier_detection == other.outlier_detection &&
126 override_host_statuses == other.override_host_statuses &&
127 metadata == other.metadata;
128 }
129
130 std::string ToString() const;
131 };
132
133 } // namespace grpc_core
134
135 #endif // GRPC_SRC_CORE_XDS_GRPC_XDS_CLUSTER_H
136