Debian Codegraph @

src/core/ext/xds/xds_api.h

raw
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_API_H
18 #define GRPC_CORE_EXT_XDS_XDS_API_H
19
20 #include <grpc/support/port_platform.h>
21
22 #include <stddef.h>
23
24 #include <map>
25 #include <set>
26 #include <string>
27 #include <utility>
28 #include <vector>
29
30 #include "absl/status/status.h"
31 #include "absl/strings/string_view.h"
32 #include "envoy/admin/v3/config_dump_shared.upb.h"
33 #include "upb/arena.h"
34 #include "upb/def.hpp"
35
36 #include "src/core/ext/xds/xds_bootstrap.h"
37 #include "src/core/ext/xds/xds_client_stats.h"
38 #include "src/core/lib/debug/trace.h"
39 #include "src/core/lib/gprpp/ref_counted_ptr.h"
40 #include "src/core/lib/gprpp/time.h"
41
42 namespace grpc_core {
43
44 class XdsClient;
45
46 // TODO(roth): When we have time, split this into multiple pieces:
47 // - ADS request/response handling
48 // - LRS request/response handling
49 // - CSDS response generation
50 class XdsApi {
51 public:
52 // Interface defined by caller and passed to ParseAdsResponse().
53 class AdsResponseParserInterface {
54 public:
55 struct AdsResponseFields {
56 std::string type_url;
57 std::string version;
58 std::string nonce;
59 size_t num_resources;
60 };
61
62 virtual ~AdsResponseParserInterface() = default;
63
64 // Called when the top-level ADS fields are parsed.
65 // If this returns non-OK, parsing will stop, and the individual
66 // resources will not be processed.
67 virtual absl::Status ProcessAdsResponseFields(AdsResponseFields fields) = 0;
68
69 // Called to parse each individual resource in the ADS response.
70 // Note that resource_name is non-empty only when the resource was
71 // wrapped in a Resource wrapper proto.
72 virtual void ParseResource(upb_Arena* arena, size_t idx,
73 absl::string_view type_url,
74 absl::string_view resource_name,
75 absl::string_view serialized_resource) = 0;
76
77 // Called when a resource is wrapped in a Resource wrapper proto but
78 // we fail to deserialize the wrapper proto.
79 virtual void ResourceWrapperParsingFailed(size_t idx) = 0;
80 };
81
82 struct ClusterLoadReport {
83 XdsClusterDropStats::Snapshot dropped_requests;
84 std::map<RefCountedPtr<XdsLocalityName>, XdsClusterLocalityStats::Snapshot,
85 XdsLocalityName::Less>
86 locality_stats;
87 Duration load_report_interval;
88 };
89 using ClusterLoadReportMap = std::map<
90 std::pair<std::string /*cluster_name*/, std::string /*eds_service_name*/>,
91 ClusterLoadReport>;
92
93 // The metadata of the xDS resource; used by the xDS config dump.
94 struct ResourceMetadata {
95 // Resource status from the view of a xDS client, which tells the
96 // synchronization status between the xDS client and the xDS server.
97 enum ClientResourceStatus {
98 // Client requested this resource but hasn't received any update from
99 // management server. The client will not fail requests, but will queue
100 // them
101 // until update arrives or the client times out waiting for the resource.
102 REQUESTED = 1,
103 // This resource has been requested by the client but has either not been
104 // delivered by the server or was previously delivered by the server and
105 // then subsequently removed from resources provided by the server.
106 DOES_NOT_EXIST,
107 // Client received this resource and replied with ACK.
108 ACKED,
109 // Client received this resource and replied with NACK.
110 NACKED
111 };
112
113 // The client status of this resource.
114 ClientResourceStatus client_status = REQUESTED;
115 // The serialized bytes of the last successfully updated raw xDS resource.
116 std::string serialized_proto;
117 // The timestamp when the resource was last successfully updated.
118 Timestamp update_time;
119 // The last successfully updated version of the resource.
120 std::string version;
121 // The rejected version string of the last failed update attempt.
122 std::string failed_version;
123 // Details about the last failed update attempt.
124 std::string failed_details;
125 // Timestamp of the last failed update attempt.
126 Timestamp failed_update_time;
127 };
128 using ResourceMetadataMap =
129 std::map<std::string /*resource_name*/, const ResourceMetadata*>;
130 using ResourceTypeMetadataMap =
131 std::map<absl::string_view /*type_url*/, ResourceMetadataMap>;
132 static_assert(static_cast<ResourceMetadata::ClientResourceStatus>(
133 envoy_admin_v3_REQUESTED) ==
134 ResourceMetadata::ClientResourceStatus::REQUESTED,
135 "");
136 static_assert(static_cast<ResourceMetadata::ClientResourceStatus>(
137 envoy_admin_v3_DOES_NOT_EXIST) ==
138 ResourceMetadata::ClientResourceStatus::DOES_NOT_EXIST,
139 "");
140 static_assert(static_cast<ResourceMetadata::ClientResourceStatus>(
141 envoy_admin_v3_ACKED) ==
142 ResourceMetadata::ClientResourceStatus::ACKED,
143 "");
144 static_assert(static_cast<ResourceMetadata::ClientResourceStatus>(
145 envoy_admin_v3_NACKED) ==
146 ResourceMetadata::ClientResourceStatus::NACKED,
147 "");
148
149 XdsApi(XdsClient* client, TraceFlag* tracer, const XdsBootstrap::Node* node,
150 upb::SymbolTable* symtab);
151
152 // Creates an ADS request.
153 std::string CreateAdsRequest(absl::string_view type_url,
154 absl::string_view version,
155 absl::string_view nonce,
156 const std::vector<std::string>& resource_names,
157 absl::Status status, bool populate_node);
158
159 // Returns non-OK when failing to deserialize response message.
160 // Otherwise, all events are reported to the parser.
161 absl::Status ParseAdsResponse(absl::string_view encoded_response,
162 AdsResponseParserInterface* parser);
163
164 // Creates an initial LRS request.
165 std::string CreateLrsInitialRequest();
166
167 // Creates an LRS request sending a client-side load report.
168 std::string CreateLrsRequest(ClusterLoadReportMap cluster_load_report_map);
169
170 // Parses the LRS response and populates send_all_clusters,
171 // cluster_names, and load_reporting_interval.
172 absl::Status ParseLrsResponse(absl::string_view encoded_response,
173 bool* send_all_clusters,
174 std::set<std::string>* cluster_names,
175 Duration* load_reporting_interval);
176
177 // Assemble the client config proto message and return the serialized result.
178 std::string AssembleClientConfig(
179 const ResourceTypeMetadataMap& resource_type_metadata_map);
180
181 private:
182 XdsClient* client_;
183 TraceFlag* tracer_;
184 const XdsBootstrap::Node* node_; // Do not own.
185 upb::SymbolTable* symtab_; // Do not own.
186 const std::string user_agent_name_;
187 const std::string user_agent_version_;
188 };
189
190 } // namespace grpc_core
191
192 #endif // GRPC_CORE_EXT_XDS_XDS_API_H