Built motion from commit 44377920.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / ext / filters / client_channel / lb_policy / xds / xds_load_balancer_api.h
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_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_LOAD_BALANCER_API_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_LOAD_BALANCER_API_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <grpc/slice_buffer.h>
25
26 #include "src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.h"
27 #include "src/core/ext/filters/client_channel/server_address.h"
28
29 namespace grpc_core {
30
31 struct XdsLocalityInfo {
32   bool operator==(const XdsLocalityInfo& other) const {
33     return *locality_name == *other.locality_name &&
34            serverlist == other.serverlist && lb_weight == other.lb_weight &&
35            priority == other.priority;
36   }
37
38   // This comparator only compares the locality names.
39   struct Less {
40     bool operator()(const XdsLocalityInfo& lhs,
41                     const XdsLocalityInfo& rhs) const {
42       return XdsLocalityName::Less()(lhs.locality_name, rhs.locality_name);
43     }
44   };
45
46   RefCountedPtr<XdsLocalityName> locality_name;
47   ServerAddressList serverlist;
48   uint32_t lb_weight;
49   uint32_t priority;
50 };
51
52 using XdsLocalityList = InlinedVector<XdsLocalityInfo, 1>;
53
54 // There are two phases of accessing this class's content:
55 // 1. to initialize in the control plane combiner;
56 // 2. to use in the data plane combiner.
57 // So no additional synchronization is needed.
58 class XdsDropConfig : public RefCounted<XdsDropConfig> {
59  public:
60   struct DropCategory {
61     bool operator==(const DropCategory& other) const {
62       return strcmp(name.get(), other.name.get()) == 0 &&
63              parts_per_million == other.parts_per_million;
64     }
65
66     UniquePtr<char> name;
67     const uint32_t parts_per_million;
68   };
69
70   using DropCategoryList = InlinedVector<DropCategory, 2>;
71
72   void AddCategory(UniquePtr<char> name, uint32_t parts_per_million) {
73     drop_category_list_.emplace_back(
74         DropCategory{std::move(name), parts_per_million});
75   }
76
77   // The only method invoked from the data plane combiner.
78   bool ShouldDrop(const UniquePtr<char>** category_name) const;
79
80   const DropCategoryList& drop_category_list() const {
81     return drop_category_list_;
82   }
83
84   bool operator==(const XdsDropConfig& other) const {
85     return drop_category_list_ == other.drop_category_list_;
86   }
87   bool operator!=(const XdsDropConfig& other) const {
88     return !(*this == other);
89   }
90
91  private:
92   DropCategoryList drop_category_list_;
93 };
94
95 struct XdsUpdate {
96   XdsLocalityList locality_list;
97   RefCountedPtr<XdsDropConfig> drop_config;
98   bool drop_all = false;
99 };
100
101 // Creates an EDS request querying \a service_name.
102 grpc_slice XdsEdsRequestCreateAndEncode(const char* service_name);
103
104 // Parses the EDS response and returns the args to update locality map. If there
105 // is any error, the output update is invalid.
106 grpc_error* XdsEdsResponseDecodeAndParse(const grpc_slice& encoded_response,
107                                          XdsUpdate* update);
108
109 // Creates an LRS request querying \a server_name.
110 grpc_slice XdsLrsRequestCreateAndEncode(const char* server_name);
111
112 // Creates an LRS request sending client-side load reports. If all the counters
113 // in \a client_stats are zero, returns empty slice.
114 grpc_slice XdsLrsRequestCreateAndEncode(const char* server_name,
115                                         XdsClientStats* client_stats);
116
117 // Parses the LRS response and returns the client-side load reporting interval.
118 // If there is any error (e.g., the found server name doesn't match \a
119 // expected_server_name), the output config is invalid.
120 grpc_error* XdsLrsResponseDecodeAndParse(const grpc_slice& encoded_response,
121                                          grpc_millis* load_reporting_interval,
122                                          const char* expected_server_name);
123
124 }  // namespace grpc_core
125
126 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_LOAD_BALANCER_API_H \
127         */