David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 19 | /// Implementation of the gRPC LB policy. |
| 20 | /// |
| 21 | /// This policy takes as input a list of resolved addresses, which must |
| 22 | /// include at least one balancer address. |
| 23 | /// |
| 24 | /// An internal channel (\a lb_channel_) is created for the addresses |
| 25 | /// from that are balancers. This channel behaves just like a regular |
| 26 | /// channel that uses pick_first to select from the list of balancer |
| 27 | /// addresses. |
| 28 | /// |
| 29 | /// The first time the policy gets a request for a pick, a ping, or to exit |
| 30 | /// the idle state, \a StartPickingLocked() is called. This method is |
| 31 | /// responsible for instantiating the internal *streaming* call to the LB |
| 32 | /// server (whichever address pick_first chose). The call will be complete |
| 33 | /// when either the balancer sends status or when we cancel the call (e.g., |
| 34 | /// because we are shutting down). In needed, we retry the call. If we |
| 35 | /// received at least one valid message from the server, a new call attempt |
| 36 | /// will be made immediately; otherwise, we apply back-off delays between |
| 37 | /// attempts. |
| 38 | /// |
| 39 | /// We maintain an internal round_robin policy instance for distributing |
| 40 | /// requests across backends. Whenever we receive a new serverlist from |
| 41 | /// the balancer, we update the round_robin policy with the new list of |
| 42 | /// addresses. If we cannot communicate with the balancer on startup, |
| 43 | /// however, we may enter fallback mode, in which case we will populate |
| 44 | /// the RR policy's addresses from the backend addresses returned by the |
| 45 | /// resolver. |
| 46 | /// |
| 47 | /// Once an RR policy instance is in place (and getting updated as described), |
| 48 | /// calls for a pick, a ping, or a cancellation will be serviced right |
| 49 | /// away by forwarding them to the RR instance. Any time there's no RR |
| 50 | /// policy available (i.e., right after the creation of the gRPCLB policy), |
| 51 | /// pick and ping requests are added to a list of pending picks and pings |
| 52 | /// to be flushed and serviced when the RR policy instance becomes available. |
| 53 | /// |
| 54 | /// \see https://github.com/grpc/grpc/blob/master/doc/load-balancing.md for the |
| 55 | /// high level design and details. |
David Garcia Quintas | 8b3b97f | 2016-07-15 07:46:47 -0700 | [diff] [blame] | 56 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 57 | // With the addition of a libuv endpoint, sockaddr.h now includes uv.h when |
| 58 | // using that endpoint. Because of various transitive includes in uv.h, |
| 59 | // including windows.h on Windows, uv.h must be included before other system |
| 60 | // headers. Therefore, sockaddr.h must always be included first. |
Alexander Polcyn | db3e898 | 2018-02-21 16:59:24 -0800 | [diff] [blame] | 61 | #include <grpc/support/port_platform.h> |
| 62 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 63 | #include "src/core/lib/iomgr/sockaddr.h" |
kpayson64 | 539f506 | 2018-03-12 19:16:30 -0700 | [diff] [blame] | 64 | #include "src/core/lib/iomgr/socket_utils.h" |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 65 | |
Yash Tibrewal | fcd26bc | 2017-09-25 15:08:28 -0700 | [diff] [blame] | 66 | #include <inttypes.h> |
Mark D. Roth | 64d922a | 2017-05-03 12:52:04 -0700 | [diff] [blame] | 67 | #include <limits.h> |
David Garcia Quintas | 22e8f1d | 2016-06-15 23:53:00 -0700 | [diff] [blame] | 68 | #include <string.h> |
| 69 | |
| 70 | #include <grpc/byte_buffer_reader.h> |
| 71 | #include <grpc/grpc.h> |
| 72 | #include <grpc/support/alloc.h> |
David Garcia Quintas | 22e8f1d | 2016-06-15 23:53:00 -0700 | [diff] [blame] | 73 | #include <grpc/support/string_util.h> |
David Garcia Quintas | 6909922 | 2016-10-03 11:28:37 -0700 | [diff] [blame] | 74 | #include <grpc/support/time.h> |
David Garcia Quintas | 22e8f1d | 2016-06-15 23:53:00 -0700 | [diff] [blame] | 75 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 76 | #include "src/core/ext/filters/client_channel/client_channel.h" |
| 77 | #include "src/core/ext/filters/client_channel/client_channel_factory.h" |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 78 | #include "src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.h" |
Yihua Zhang | 392dad7 | 2018-05-03 20:12:20 -0700 | [diff] [blame] | 79 | #include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 80 | #include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h" |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 81 | #include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 82 | #include "src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h" |
Craig Tiller | d52e22f | 2017-04-02 16:22:52 -0700 | [diff] [blame] | 83 | #include "src/core/ext/filters/client_channel/lb_policy_factory.h" |
| 84 | #include "src/core/ext/filters/client_channel/lb_policy_registry.h" |
| 85 | #include "src/core/ext/filters/client_channel/parse_address.h" |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 86 | #include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h" |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 87 | #include "src/core/ext/filters/client_channel/subchannel_index.h" |
Craig Tiller | c0df1c0 | 2017-07-17 16:12:33 -0700 | [diff] [blame] | 88 | #include "src/core/lib/backoff/backoff.h" |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 89 | #include "src/core/lib/channel/channel_args.h" |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 90 | #include "src/core/lib/channel/channel_stack.h" |
Vijay Pai | ae376bf | 2018-01-25 22:54:02 -0800 | [diff] [blame] | 91 | #include "src/core/lib/gpr/host_port.h" |
Mark D. Roth | dbdf495 | 2018-01-18 11:21:12 -0800 | [diff] [blame] | 92 | #include "src/core/lib/gpr/string.h" |
Mark D. Roth | 4f2b0fd | 2018-01-19 12:12:23 -0800 | [diff] [blame] | 93 | #include "src/core/lib/gprpp/manual_constructor.h" |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 94 | #include "src/core/lib/gprpp/memory.h" |
| 95 | #include "src/core/lib/gprpp/orphanable.h" |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 96 | #include "src/core/lib/gprpp/ref_counted_ptr.h" |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 97 | #include "src/core/lib/iomgr/combiner.h" |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 98 | #include "src/core/lib/iomgr/sockaddr.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 99 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 100 | #include "src/core/lib/iomgr/timer.h" |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 101 | #include "src/core/lib/slice/slice_hash_table.h" |
Craig Tiller | 18b4ba3 | 2016-11-09 15:23:42 -0800 | [diff] [blame] | 102 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 103 | #include "src/core/lib/slice/slice_string_helpers.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 104 | #include "src/core/lib/surface/call.h" |
| 105 | #include "src/core/lib/surface/channel.h" |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 106 | #include "src/core/lib/surface/channel_init.h" |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 107 | #include "src/core/lib/transport/static_metadata.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 108 | |
David Garcia Quintas | 1edfb95 | 2016-11-22 17:15:34 -0800 | [diff] [blame] | 109 | #define GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS 1 |
| 110 | #define GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER 1.6 |
| 111 | #define GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS 120 |
| 112 | #define GRPC_GRPCLB_RECONNECT_JITTER 0.2 |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 113 | #define GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS 10000 |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 114 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 115 | namespace grpc_core { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 116 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 117 | TraceFlag grpc_lb_glb_trace(false, "glb"); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 118 | |
Vijay Pai | 849bd73 | 2018-01-02 23:30:47 +0000 | [diff] [blame] | 119 | namespace { |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 120 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 121 | class GrpcLb : public LoadBalancingPolicy { |
| 122 | public: |
| 123 | GrpcLb(const grpc_lb_addresses* addresses, const Args& args); |
| 124 | |
| 125 | void UpdateLocked(const grpc_channel_args& args) override; |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 126 | bool PickLocked(PickState* pick, grpc_error** error) override; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 127 | void CancelPickLocked(PickState* pick, grpc_error* error) override; |
| 128 | void CancelMatchingPicksLocked(uint32_t initial_metadata_flags_mask, |
| 129 | uint32_t initial_metadata_flags_eq, |
| 130 | grpc_error* error) override; |
| 131 | void NotifyOnStateChangeLocked(grpc_connectivity_state* state, |
| 132 | grpc_closure* closure) override; |
| 133 | grpc_connectivity_state CheckConnectivityLocked( |
| 134 | grpc_error** connectivity_error) override; |
| 135 | void HandOffPendingPicksLocked(LoadBalancingPolicy* new_policy) override; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 136 | void ExitIdleLocked() override; |
ncteisen | 82e9cb6 | 2018-07-12 17:42:36 -0700 | [diff] [blame] | 137 | void FillChildRefsForChannelz(ChildRefsList* child_subchannels, |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 138 | ChildRefsList* child_channels) override; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 139 | |
| 140 | private: |
| 141 | /// Linked list of pending pick requests. It stores all information needed to |
| 142 | /// eventually call (Round Robin's) pick() on them. They mainly stay pending |
| 143 | /// waiting for the RR policy to be created. |
| 144 | /// |
| 145 | /// Note that when a pick is sent to the RR policy, we inject our own |
| 146 | /// on_complete callback, so that we can intercept the result before |
| 147 | /// invoking the original on_complete callback. This allows us to set the |
| 148 | /// LB token metadata and add client_stats to the call context. |
| 149 | /// See \a pending_pick_complete() for details. |
| 150 | struct PendingPick { |
| 151 | // The grpclb instance that created the wrapping. This instance is not |
| 152 | // owned; reference counts are untouched. It's used only for logging |
| 153 | // purposes. |
| 154 | GrpcLb* grpclb_policy; |
| 155 | // The original pick. |
| 156 | PickState* pick; |
| 157 | // Our on_complete closure and the original one. |
| 158 | grpc_closure on_complete; |
| 159 | grpc_closure* original_on_complete; |
| 160 | // The LB token associated with the pick. This is set via user_data in |
| 161 | // the pick. |
| 162 | grpc_mdelem lb_token; |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 163 | // Stats for client-side load reporting. |
| 164 | RefCountedPtr<GrpcLbClientStats> client_stats; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 165 | // Next pending pick. |
| 166 | PendingPick* next = nullptr; |
| 167 | }; |
| 168 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 169 | /// Contains a call to the LB server and all the data related to the call. |
| 170 | class BalancerCallState |
| 171 | : public InternallyRefCountedWithTracing<BalancerCallState> { |
| 172 | public: |
| 173 | explicit BalancerCallState( |
| 174 | RefCountedPtr<LoadBalancingPolicy> parent_grpclb_policy); |
| 175 | |
| 176 | // It's the caller's responsibility to ensure that Orphan() is called from |
| 177 | // inside the combiner. |
| 178 | void Orphan() override; |
| 179 | |
| 180 | void StartQuery(); |
| 181 | |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 182 | GrpcLbClientStats* client_stats() const { return client_stats_.get(); } |
| 183 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 184 | bool seen_initial_response() const { return seen_initial_response_; } |
| 185 | |
| 186 | private: |
Mark D. Roth | 9635a04 | 2018-04-20 10:44:17 -0700 | [diff] [blame] | 187 | // So Delete() can access our private dtor. |
| 188 | template <typename T> |
| 189 | friend void grpc_core::Delete(T*); |
| 190 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 191 | ~BalancerCallState(); |
| 192 | |
| 193 | GrpcLb* grpclb_policy() const { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 194 | return static_cast<GrpcLb*>(grpclb_policy_.get()); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void ScheduleNextClientLoadReportLocked(); |
| 198 | void SendClientLoadReportLocked(); |
| 199 | |
| 200 | static bool LoadReportCountersAreZero(grpc_grpclb_request* request); |
| 201 | |
| 202 | static void MaybeSendClientLoadReportLocked(void* arg, grpc_error* error); |
| 203 | static void ClientLoadReportDoneLocked(void* arg, grpc_error* error); |
| 204 | static void OnInitialRequestSentLocked(void* arg, grpc_error* error); |
| 205 | static void OnBalancerMessageReceivedLocked(void* arg, grpc_error* error); |
| 206 | static void OnBalancerStatusReceivedLocked(void* arg, grpc_error* error); |
| 207 | |
| 208 | // The owning LB policy. |
| 209 | RefCountedPtr<LoadBalancingPolicy> grpclb_policy_; |
| 210 | |
| 211 | // The streaming call to the LB server. Always non-NULL. |
| 212 | grpc_call* lb_call_ = nullptr; |
| 213 | |
| 214 | // recv_initial_metadata |
| 215 | grpc_metadata_array lb_initial_metadata_recv_; |
| 216 | |
| 217 | // send_message |
| 218 | grpc_byte_buffer* send_message_payload_ = nullptr; |
| 219 | grpc_closure lb_on_initial_request_sent_; |
| 220 | |
| 221 | // recv_message |
| 222 | grpc_byte_buffer* recv_message_payload_ = nullptr; |
| 223 | grpc_closure lb_on_balancer_message_received_; |
| 224 | bool seen_initial_response_ = false; |
| 225 | |
| 226 | // recv_trailing_metadata |
| 227 | grpc_closure lb_on_balancer_status_received_; |
| 228 | grpc_metadata_array lb_trailing_metadata_recv_; |
| 229 | grpc_status_code lb_call_status_; |
| 230 | grpc_slice lb_call_status_details_; |
| 231 | |
| 232 | // The stats for client-side load reporting associated with this LB call. |
| 233 | // Created after the first serverlist is received. |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 234 | RefCountedPtr<GrpcLbClientStats> client_stats_; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 235 | grpc_millis client_stats_report_interval_ = 0; |
| 236 | grpc_timer client_load_report_timer_; |
| 237 | bool client_load_report_timer_callback_pending_ = false; |
| 238 | bool last_client_load_report_counters_were_zero_ = false; |
| 239 | bool client_load_report_is_due_ = false; |
| 240 | // The closure used for either the load report timer or the callback for |
| 241 | // completion of sending the load report. |
| 242 | grpc_closure client_load_report_closure_; |
| 243 | }; |
| 244 | |
| 245 | ~GrpcLb(); |
| 246 | |
| 247 | void ShutdownLocked() override; |
| 248 | |
| 249 | // Helper function used in ctor and UpdateLocked(). |
| 250 | void ProcessChannelArgsLocked(const grpc_channel_args& args); |
| 251 | |
| 252 | // Methods for dealing with the balancer channel and call. |
| 253 | void StartPickingLocked(); |
| 254 | void StartBalancerCallLocked(); |
| 255 | static void OnFallbackTimerLocked(void* arg, grpc_error* error); |
| 256 | void StartBalancerCallRetryTimerLocked(); |
| 257 | static void OnBalancerCallRetryTimerLocked(void* arg, grpc_error* error); |
| 258 | static void OnBalancerChannelConnectivityChangedLocked(void* arg, |
| 259 | grpc_error* error); |
| 260 | |
| 261 | // Pending pick methods. |
| 262 | static void PendingPickSetMetadataAndContext(PendingPick* pp); |
| 263 | PendingPick* PendingPickCreate(PickState* pick); |
| 264 | void AddPendingPick(PendingPick* pp); |
| 265 | static void OnPendingPickComplete(void* arg, grpc_error* error); |
| 266 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 267 | // Methods for dealing with the RR policy. |
| 268 | void CreateOrUpdateRoundRobinPolicyLocked(); |
| 269 | grpc_channel_args* CreateRoundRobinPolicyArgsLocked(); |
| 270 | void CreateRoundRobinPolicyLocked(const Args& args); |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 271 | bool PickFromRoundRobinPolicyLocked(bool force_async, PendingPick* pp, |
| 272 | grpc_error** error); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 273 | void UpdateConnectivityStateFromRoundRobinPolicyLocked( |
| 274 | grpc_error* rr_state_error); |
| 275 | static void OnRoundRobinConnectivityChangedLocked(void* arg, |
| 276 | grpc_error* error); |
| 277 | static void OnRoundRobinRequestReresolutionLocked(void* arg, |
| 278 | grpc_error* error); |
| 279 | |
| 280 | // Who the client is trying to communicate with. |
| 281 | const char* server_name_ = nullptr; |
| 282 | |
| 283 | // Current channel args from the resolver. |
| 284 | grpc_channel_args* args_ = nullptr; |
| 285 | |
| 286 | // Internal state. |
| 287 | bool started_picking_ = false; |
| 288 | bool shutting_down_ = false; |
| 289 | grpc_connectivity_state_tracker state_tracker_; |
| 290 | |
| 291 | // The channel for communicating with the LB server. |
| 292 | grpc_channel* lb_channel_ = nullptr; |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 293 | // Mutex to protect the channel to the LB server. This is used when |
| 294 | // processing a channelz request. |
| 295 | gpr_mu lb_channel_mu_; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 296 | grpc_connectivity_state lb_channel_connectivity_; |
| 297 | grpc_closure lb_channel_on_connectivity_changed_; |
| 298 | // Are we already watching the LB channel's connectivity? |
| 299 | bool watching_lb_channel_ = false; |
| 300 | // Response generator to inject address updates into lb_channel_. |
| 301 | RefCountedPtr<FakeResolverResponseGenerator> response_generator_; |
| 302 | |
| 303 | // The data associated with the current LB call. It holds a ref to this LB |
| 304 | // policy. It's initialized every time we query for backends. It's reset to |
| 305 | // NULL whenever the current LB call is no longer needed (e.g., the LB policy |
| 306 | // is shutting down, or the LB call has ended). A non-NULL lb_calld_ always |
| 307 | // contains a non-NULL lb_call_. |
| 308 | OrphanablePtr<BalancerCallState> lb_calld_; |
| 309 | // Timeout in milliseconds for the LB call. 0 means no deadline. |
| 310 | int lb_call_timeout_ms_ = 0; |
| 311 | // Balancer call retry state. |
| 312 | BackOff lb_call_backoff_; |
| 313 | bool retry_timer_callback_pending_ = false; |
| 314 | grpc_timer lb_call_retry_timer_; |
| 315 | grpc_closure lb_on_call_retry_; |
| 316 | |
| 317 | // The deserialized response from the balancer. May be nullptr until one |
| 318 | // such response has arrived. |
| 319 | grpc_grpclb_serverlist* serverlist_ = nullptr; |
| 320 | // Index into serverlist for next pick. |
| 321 | // If the server at this index is a drop, we return a drop. |
| 322 | // Otherwise, we delegate to the RR policy. |
| 323 | size_t serverlist_index_ = 0; |
| 324 | |
| 325 | // Timeout in milliseconds for before using fallback backend addresses. |
| 326 | // 0 means not using fallback. |
| 327 | int lb_fallback_timeout_ms_ = 0; |
| 328 | // The backend addresses from the resolver. |
| 329 | grpc_lb_addresses* fallback_backend_addresses_ = nullptr; |
| 330 | // Fallback timer. |
| 331 | bool fallback_timer_callback_pending_ = false; |
| 332 | grpc_timer lb_fallback_timer_; |
| 333 | grpc_closure lb_on_fallback_; |
| 334 | |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 335 | // Pending picks that are waiting on the RR policy's connectivity. |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 336 | PendingPick* pending_picks_ = nullptr; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 337 | |
| 338 | // The RR policy to use for the backends. |
| 339 | OrphanablePtr<LoadBalancingPolicy> rr_policy_; |
| 340 | grpc_connectivity_state rr_connectivity_state_; |
| 341 | grpc_closure on_rr_connectivity_changed_; |
| 342 | grpc_closure on_rr_request_reresolution_; |
Vijay Pai | 849bd73 | 2018-01-02 23:30:47 +0000 | [diff] [blame] | 343 | }; |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 344 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 345 | // |
| 346 | // serverlist parsing code |
| 347 | // |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 348 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 349 | // vtable for LB tokens in grpc_lb_addresses |
| 350 | void* lb_token_copy(void* token) { |
| 351 | return token == nullptr |
| 352 | ? nullptr |
| 353 | : (void*)GRPC_MDELEM_REF(grpc_mdelem{(uintptr_t)token}).payload; |
| 354 | } |
| 355 | void lb_token_destroy(void* token) { |
| 356 | if (token != nullptr) { |
| 357 | GRPC_MDELEM_UNREF(grpc_mdelem{(uintptr_t)token}); |
Juanli Shen | 8e4c9d3 | 2018-01-23 16:26:39 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 360 | int lb_token_cmp(void* token1, void* token2) { |
| 361 | if (token1 > token2) return 1; |
| 362 | if (token1 < token2) return -1; |
| 363 | return 0; |
Juanli Shen | 8e4c9d3 | 2018-01-23 16:26:39 -0800 | [diff] [blame] | 364 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 365 | const grpc_lb_user_data_vtable lb_token_vtable = { |
| 366 | lb_token_copy, lb_token_destroy, lb_token_cmp}; |
Juanli Shen | 8e4c9d3 | 2018-01-23 16:26:39 -0800 | [diff] [blame] | 367 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 368 | // Returns the backend addresses extracted from the given addresses. |
| 369 | grpc_lb_addresses* ExtractBackendAddresses(const grpc_lb_addresses* addresses) { |
| 370 | // First pass: count the number of backend addresses. |
| 371 | size_t num_backends = 0; |
| 372 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 373 | if (!addresses->addresses[i].is_balancer) { |
| 374 | ++num_backends; |
Mark D. Roth | 83d5cd6 | 2018-01-11 08:56:53 -0800 | [diff] [blame] | 375 | } |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 376 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 377 | // Second pass: actually populate the addresses and (empty) LB tokens. |
| 378 | grpc_lb_addresses* backend_addresses = |
| 379 | grpc_lb_addresses_create(num_backends, &lb_token_vtable); |
| 380 | size_t num_copied = 0; |
| 381 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 382 | if (addresses->addresses[i].is_balancer) continue; |
| 383 | const grpc_resolved_address* addr = &addresses->addresses[i].address; |
| 384 | grpc_lb_addresses_set_address(backend_addresses, num_copied, &addr->addr, |
| 385 | addr->len, false /* is_balancer */, |
| 386 | nullptr /* balancer_name */, |
| 387 | (void*)GRPC_MDELEM_LB_TOKEN_EMPTY.payload); |
| 388 | ++num_copied; |
| 389 | } |
| 390 | return backend_addresses; |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 393 | bool IsServerValid(const grpc_grpclb_server* server, size_t idx, bool log) { |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 394 | if (server->drop) return false; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 395 | const grpc_grpclb_ip_address* ip = &server->ip_address; |
Yash Tibrewal | 7f51ba8 | 2018-04-12 13:21:20 -0700 | [diff] [blame] | 396 | if (GPR_UNLIKELY(server->port >> 16 != 0)) { |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 397 | if (log) { |
| 398 | gpr_log(GPR_ERROR, |
Jan Tattermusch | 2b39808 | 2016-10-07 14:40:30 +0200 | [diff] [blame] | 399 | "Invalid port '%d' at index %lu of serverlist. Ignoring.", |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 400 | server->port, (unsigned long)idx); |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 401 | } |
| 402 | return false; |
| 403 | } |
Yash Tibrewal | 7f51ba8 | 2018-04-12 13:21:20 -0700 | [diff] [blame] | 404 | if (GPR_UNLIKELY(ip->size != 4 && ip->size != 16)) { |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 405 | if (log) { |
| 406 | gpr_log(GPR_ERROR, |
Jan Tattermusch | 2b39808 | 2016-10-07 14:40:30 +0200 | [diff] [blame] | 407 | "Expected IP to be 4 or 16 bytes, got %d at index %lu of " |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 408 | "serverlist. Ignoring", |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 409 | ip->size, (unsigned long)idx); |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 410 | } |
| 411 | return false; |
| 412 | } |
| 413 | return true; |
| 414 | } |
| 415 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 416 | void ParseServer(const grpc_grpclb_server* server, |
| 417 | grpc_resolved_address* addr) { |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 418 | memset(addr, 0, sizeof(*addr)); |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 419 | if (server->drop) return; |
kpayson64 | 539f506 | 2018-03-12 19:16:30 -0700 | [diff] [blame] | 420 | const uint16_t netorder_port = grpc_htons((uint16_t)server->port); |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 421 | /* the addresses are given in binary format (a in(6)_addr struct) in |
| 422 | * server->ip_address.bytes. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 423 | const grpc_grpclb_ip_address* ip = &server->ip_address; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 424 | if (ip->size == 4) { |
Yash Tibrewal | 44a1588 | 2018-03-14 18:41:33 -0700 | [diff] [blame] | 425 | addr->len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in)); |
kpayson64 | 539f506 | 2018-03-12 19:16:30 -0700 | [diff] [blame] | 426 | grpc_sockaddr_in* addr4 = reinterpret_cast<grpc_sockaddr_in*>(&addr->addr); |
| 427 | addr4->sin_family = GRPC_AF_INET; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 428 | memcpy(&addr4->sin_addr, ip->bytes, ip->size); |
| 429 | addr4->sin_port = netorder_port; |
| 430 | } else if (ip->size == 16) { |
Yash Tibrewal | 44a1588 | 2018-03-14 18:41:33 -0700 | [diff] [blame] | 431 | addr->len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6)); |
kpayson64 | 539f506 | 2018-03-12 19:16:30 -0700 | [diff] [blame] | 432 | grpc_sockaddr_in6* addr6 = (grpc_sockaddr_in6*)&addr->addr; |
| 433 | addr6->sin6_family = GRPC_AF_INET6; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 434 | memcpy(&addr6->sin6_addr, ip->bytes, ip->size); |
| 435 | addr6->sin6_port = netorder_port; |
| 436 | } |
| 437 | } |
| 438 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 439 | // Returns addresses extracted from \a serverlist. |
| 440 | grpc_lb_addresses* ProcessServerlist(const grpc_grpclb_serverlist* serverlist) { |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 441 | size_t num_valid = 0; |
| 442 | /* first pass: count how many are valid in order to allocate the necessary |
| 443 | * memory in a single block */ |
| 444 | for (size_t i = 0; i < serverlist->num_servers; ++i) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 445 | if (IsServerValid(serverlist->servers[i], i, true)) ++num_valid; |
David Garcia Quintas | b8b384a | 2016-08-23 21:10:29 -0700 | [diff] [blame] | 446 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 447 | grpc_lb_addresses* lb_addresses = |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 448 | grpc_lb_addresses_create(num_valid, &lb_token_vtable); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 449 | /* second pass: actually populate the addresses and LB tokens (aka user data |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 450 | * to the outside world) to be read by the RR policy during its creation. |
| 451 | * Given that the validity tests are very cheap, they are performed again |
| 452 | * instead of marking the valid ones during the first pass, as this would |
| 453 | * incurr in an allocation due to the arbitrary number of server */ |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 454 | size_t addr_idx = 0; |
| 455 | for (size_t sl_idx = 0; sl_idx < serverlist->num_servers; ++sl_idx) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 456 | const grpc_grpclb_server* server = serverlist->servers[sl_idx]; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 457 | if (!IsServerValid(serverlist->servers[sl_idx], sl_idx, false)) continue; |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 458 | GPR_ASSERT(addr_idx < num_valid); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 459 | /* address processing */ |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 460 | grpc_resolved_address addr; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 461 | ParseServer(server, &addr); |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 462 | /* lb token processing */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 463 | void* user_data; |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 464 | if (server->has_load_balance_token) { |
David Garcia Quintas | 0baf1dc | 2016-10-28 04:44:01 +0200 | [diff] [blame] | 465 | const size_t lb_token_max_length = |
| 466 | GPR_ARRAY_SIZE(server->load_balance_token); |
| 467 | const size_t lb_token_length = |
| 468 | strnlen(server->load_balance_token, lb_token_max_length); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 469 | grpc_slice lb_token_mdstr = grpc_slice_from_copied_buffer( |
| 470 | server->load_balance_token, lb_token_length); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 471 | user_data = |
| 472 | (void*)grpc_mdelem_from_slices(GRPC_MDSTR_LB_TOKEN, lb_token_mdstr) |
| 473 | .payload; |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 474 | } else { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 475 | char* uri = grpc_sockaddr_to_uri(&addr); |
David Garcia Quintas | 850cbaa | 2016-11-15 15:13:35 -0800 | [diff] [blame] | 476 | gpr_log(GPR_INFO, |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 477 | "Missing LB token for backend address '%s'. The empty token will " |
| 478 | "be used instead", |
David Garcia Quintas | 850cbaa | 2016-11-15 15:13:35 -0800 | [diff] [blame] | 479 | uri); |
| 480 | gpr_free(uri); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 481 | user_data = (void*)GRPC_MDELEM_LB_TOKEN_EMPTY.payload; |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 482 | } |
Mark D. Roth | 64f1f8d | 2016-09-16 09:00:09 -0700 | [diff] [blame] | 483 | grpc_lb_addresses_set_address(lb_addresses, addr_idx, &addr.addr, addr.len, |
| 484 | false /* is_balancer */, |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 485 | nullptr /* balancer_name */, user_data); |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 486 | ++addr_idx; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 487 | } |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 488 | GPR_ASSERT(addr_idx == num_valid); |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 489 | return lb_addresses; |
| 490 | } |
| 491 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 492 | // |
| 493 | // GrpcLb::BalancerCallState |
| 494 | // |
| 495 | |
| 496 | GrpcLb::BalancerCallState::BalancerCallState( |
| 497 | RefCountedPtr<LoadBalancingPolicy> parent_grpclb_policy) |
| 498 | : InternallyRefCountedWithTracing<BalancerCallState>(&grpc_lb_glb_trace), |
| 499 | grpclb_policy_(std::move(parent_grpclb_policy)) { |
| 500 | GPR_ASSERT(grpclb_policy_ != nullptr); |
| 501 | GPR_ASSERT(!grpclb_policy()->shutting_down_); |
| 502 | // Init the LB call. Note that the LB call will progress every time there's |
| 503 | // activity in grpclb_policy_->interested_parties(), which is comprised of |
| 504 | // the polling entities from client_channel. |
| 505 | GPR_ASSERT(grpclb_policy()->server_name_ != nullptr); |
| 506 | GPR_ASSERT(grpclb_policy()->server_name_[0] != '\0'); |
David Garcia Quintas | c7c0d69 | 2018-03-10 17:27:15 -0800 | [diff] [blame] | 507 | const grpc_millis deadline = |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 508 | grpclb_policy()->lb_call_timeout_ms_ == 0 |
| 509 | ? GRPC_MILLIS_INF_FUTURE |
| 510 | : ExecCtx::Get()->Now() + grpclb_policy()->lb_call_timeout_ms_; |
| 511 | lb_call_ = grpc_channel_create_pollset_set_call( |
| 512 | grpclb_policy()->lb_channel_, nullptr, GRPC_PROPAGATE_DEFAULTS, |
| 513 | grpclb_policy_->interested_parties(), |
| 514 | GRPC_MDSTR_SLASH_GRPC_DOT_LB_DOT_V1_DOT_LOADBALANCER_SLASH_BALANCELOAD, |
David Garcia Quintas | c7c0d69 | 2018-03-10 17:27:15 -0800 | [diff] [blame] | 515 | nullptr, deadline, nullptr); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 516 | // Init the LB call request payload. |
| 517 | grpc_grpclb_request* request = |
| 518 | grpc_grpclb_request_create(grpclb_policy()->server_name_); |
| 519 | grpc_slice request_payload_slice = grpc_grpclb_request_encode(request); |
| 520 | send_message_payload_ = |
| 521 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 522 | grpc_slice_unref_internal(request_payload_slice); |
| 523 | grpc_grpclb_request_destroy(request); |
| 524 | // Init other data associated with the LB call. |
| 525 | grpc_metadata_array_init(&lb_initial_metadata_recv_); |
| 526 | grpc_metadata_array_init(&lb_trailing_metadata_recv_); |
| 527 | GRPC_CLOSURE_INIT(&lb_on_initial_request_sent_, OnInitialRequestSentLocked, |
| 528 | this, grpc_combiner_scheduler(grpclb_policy()->combiner())); |
| 529 | GRPC_CLOSURE_INIT(&lb_on_balancer_message_received_, |
| 530 | OnBalancerMessageReceivedLocked, this, |
| 531 | grpc_combiner_scheduler(grpclb_policy()->combiner())); |
| 532 | GRPC_CLOSURE_INIT(&lb_on_balancer_status_received_, |
| 533 | OnBalancerStatusReceivedLocked, this, |
| 534 | grpc_combiner_scheduler(grpclb_policy()->combiner())); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 537 | GrpcLb::BalancerCallState::~BalancerCallState() { |
| 538 | GPR_ASSERT(lb_call_ != nullptr); |
| 539 | grpc_call_unref(lb_call_); |
| 540 | grpc_metadata_array_destroy(&lb_initial_metadata_recv_); |
| 541 | grpc_metadata_array_destroy(&lb_trailing_metadata_recv_); |
| 542 | grpc_byte_buffer_destroy(send_message_payload_); |
| 543 | grpc_byte_buffer_destroy(recv_message_payload_); |
| 544 | grpc_slice_unref_internal(lb_call_status_details_); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void GrpcLb::BalancerCallState::Orphan() { |
| 548 | GPR_ASSERT(lb_call_ != nullptr); |
| 549 | // If we are here because grpclb_policy wants to cancel the call, |
| 550 | // lb_on_balancer_status_received_ will complete the cancellation and clean |
| 551 | // up. Otherwise, we are here because grpclb_policy has to orphan a failed |
| 552 | // call, then the following cancellation will be a no-op. |
| 553 | grpc_call_cancel(lb_call_, nullptr); |
| 554 | if (client_load_report_timer_callback_pending_) { |
| 555 | grpc_timer_cancel(&client_load_report_timer_); |
| 556 | } |
| 557 | // Note that the initial ref is hold by lb_on_balancer_status_received_ |
| 558 | // instead of the caller of this function. So the corresponding unref happens |
| 559 | // in lb_on_balancer_status_received_ instead of here. |
| 560 | } |
| 561 | |
| 562 | void GrpcLb::BalancerCallState::StartQuery() { |
| 563 | GPR_ASSERT(lb_call_ != nullptr); |
| 564 | if (grpc_lb_glb_trace.enabled()) { |
| 565 | gpr_log(GPR_INFO, |
| 566 | "[grpclb %p] Starting LB call (lb_calld: %p, lb_call: %p)", |
| 567 | grpclb_policy_.get(), this, lb_call_); |
| 568 | } |
| 569 | // Create the ops. |
| 570 | grpc_call_error call_error; |
| 571 | grpc_op ops[3]; |
| 572 | memset(ops, 0, sizeof(ops)); |
| 573 | // Op: send initial metadata. |
| 574 | grpc_op* op = ops; |
| 575 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 576 | op->data.send_initial_metadata.count = 0; |
| 577 | op->flags = 0; |
| 578 | op->reserved = nullptr; |
| 579 | op++; |
| 580 | // Op: send request message. |
| 581 | GPR_ASSERT(send_message_payload_ != nullptr); |
| 582 | op->op = GRPC_OP_SEND_MESSAGE; |
| 583 | op->data.send_message.send_message = send_message_payload_; |
| 584 | op->flags = 0; |
| 585 | op->reserved = nullptr; |
| 586 | op++; |
| 587 | // TODO(roth): We currently track this ref manually. Once the |
| 588 | // ClosureRef API is ready, we should pass the RefCountedPtr<> along |
| 589 | // with the callback. |
| 590 | auto self = Ref(DEBUG_LOCATION, "on_initial_request_sent"); |
| 591 | self.release(); |
| 592 | call_error = grpc_call_start_batch_and_execute( |
| 593 | lb_call_, ops, (size_t)(op - ops), &lb_on_initial_request_sent_); |
| 594 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 595 | // Op: recv initial metadata. |
| 596 | op = ops; |
| 597 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 598 | op->data.recv_initial_metadata.recv_initial_metadata = |
| 599 | &lb_initial_metadata_recv_; |
| 600 | op->flags = 0; |
| 601 | op->reserved = nullptr; |
| 602 | op++; |
| 603 | // Op: recv response. |
| 604 | op->op = GRPC_OP_RECV_MESSAGE; |
| 605 | op->data.recv_message.recv_message = &recv_message_payload_; |
| 606 | op->flags = 0; |
| 607 | op->reserved = nullptr; |
| 608 | op++; |
| 609 | // TODO(roth): We currently track this ref manually. Once the |
| 610 | // ClosureRef API is ready, we should pass the RefCountedPtr<> along |
| 611 | // with the callback. |
| 612 | self = Ref(DEBUG_LOCATION, "on_message_received"); |
| 613 | self.release(); |
| 614 | call_error = grpc_call_start_batch_and_execute( |
| 615 | lb_call_, ops, (size_t)(op - ops), &lb_on_balancer_message_received_); |
| 616 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 617 | // Op: recv server status. |
| 618 | op = ops; |
| 619 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 620 | op->data.recv_status_on_client.trailing_metadata = |
| 621 | &lb_trailing_metadata_recv_; |
| 622 | op->data.recv_status_on_client.status = &lb_call_status_; |
| 623 | op->data.recv_status_on_client.status_details = &lb_call_status_details_; |
| 624 | op->flags = 0; |
| 625 | op->reserved = nullptr; |
| 626 | op++; |
| 627 | // This callback signals the end of the LB call, so it relies on the initial |
| 628 | // ref instead of a new ref. When it's invoked, it's the initial ref that is |
| 629 | // unreffed. |
| 630 | call_error = grpc_call_start_batch_and_execute( |
| 631 | lb_call_, ops, (size_t)(op - ops), &lb_on_balancer_status_received_); |
| 632 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 633 | }; |
| 634 | |
| 635 | void GrpcLb::BalancerCallState::ScheduleNextClientLoadReportLocked() { |
| 636 | const grpc_millis next_client_load_report_time = |
| 637 | ExecCtx::Get()->Now() + client_stats_report_interval_; |
| 638 | GRPC_CLOSURE_INIT(&client_load_report_closure_, |
| 639 | MaybeSendClientLoadReportLocked, this, |
| 640 | grpc_combiner_scheduler(grpclb_policy()->combiner())); |
| 641 | grpc_timer_init(&client_load_report_timer_, next_client_load_report_time, |
| 642 | &client_load_report_closure_); |
| 643 | client_load_report_timer_callback_pending_ = true; |
| 644 | } |
| 645 | |
| 646 | void GrpcLb::BalancerCallState::MaybeSendClientLoadReportLocked( |
| 647 | void* arg, grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 648 | BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 649 | GrpcLb* grpclb_policy = lb_calld->grpclb_policy(); |
| 650 | lb_calld->client_load_report_timer_callback_pending_ = false; |
| 651 | if (error != GRPC_ERROR_NONE || lb_calld != grpclb_policy->lb_calld_.get()) { |
| 652 | lb_calld->Unref(DEBUG_LOCATION, "client_load_report"); |
| 653 | return; |
| 654 | } |
| 655 | // If we've already sent the initial request, then we can go ahead and send |
| 656 | // the load report. Otherwise, we need to wait until the initial request has |
| 657 | // been sent to send this (see OnInitialRequestSentLocked()). |
| 658 | if (lb_calld->send_message_payload_ == nullptr) { |
| 659 | lb_calld->SendClientLoadReportLocked(); |
| 660 | } else { |
| 661 | lb_calld->client_load_report_is_due_ = true; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | bool GrpcLb::BalancerCallState::LoadReportCountersAreZero( |
| 666 | grpc_grpclb_request* request) { |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 667 | GrpcLbClientStats::DroppedCallCounts* drop_entries = |
| 668 | static_cast<GrpcLbClientStats::DroppedCallCounts*>( |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 669 | request->client_stats.calls_finished_with_drop.arg); |
| 670 | return request->client_stats.num_calls_started == 0 && |
| 671 | request->client_stats.num_calls_finished == 0 && |
| 672 | request->client_stats.num_calls_finished_with_client_failed_to_send == |
| 673 | 0 && |
| 674 | request->client_stats.num_calls_finished_known_received == 0 && |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 675 | (drop_entries == nullptr || drop_entries->size() == 0); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | void GrpcLb::BalancerCallState::SendClientLoadReportLocked() { |
| 679 | // Construct message payload. |
| 680 | GPR_ASSERT(send_message_payload_ == nullptr); |
| 681 | grpc_grpclb_request* request = |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 682 | grpc_grpclb_load_report_request_create_locked(client_stats_.get()); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 683 | // Skip client load report if the counters were all zero in the last |
| 684 | // report and they are still zero in this one. |
| 685 | if (LoadReportCountersAreZero(request)) { |
| 686 | if (last_client_load_report_counters_were_zero_) { |
| 687 | grpc_grpclb_request_destroy(request); |
| 688 | ScheduleNextClientLoadReportLocked(); |
| 689 | return; |
| 690 | } |
| 691 | last_client_load_report_counters_were_zero_ = true; |
| 692 | } else { |
| 693 | last_client_load_report_counters_were_zero_ = false; |
| 694 | } |
| 695 | grpc_slice request_payload_slice = grpc_grpclb_request_encode(request); |
| 696 | send_message_payload_ = |
| 697 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 698 | grpc_slice_unref_internal(request_payload_slice); |
| 699 | grpc_grpclb_request_destroy(request); |
| 700 | // Send the report. |
| 701 | grpc_op op; |
| 702 | memset(&op, 0, sizeof(op)); |
| 703 | op.op = GRPC_OP_SEND_MESSAGE; |
| 704 | op.data.send_message.send_message = send_message_payload_; |
| 705 | GRPC_CLOSURE_INIT(&client_load_report_closure_, ClientLoadReportDoneLocked, |
| 706 | this, grpc_combiner_scheduler(grpclb_policy()->combiner())); |
| 707 | grpc_call_error call_error = grpc_call_start_batch_and_execute( |
| 708 | lb_call_, &op, 1, &client_load_report_closure_); |
Yash Tibrewal | 7f51ba8 | 2018-04-12 13:21:20 -0700 | [diff] [blame] | 709 | if (GPR_UNLIKELY(call_error != GRPC_CALL_OK)) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 710 | gpr_log(GPR_ERROR, "[grpclb %p] call_error=%d", grpclb_policy_.get(), |
| 711 | call_error); |
| 712 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | void GrpcLb::BalancerCallState::ClientLoadReportDoneLocked(void* arg, |
| 717 | grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 718 | BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 719 | GrpcLb* grpclb_policy = lb_calld->grpclb_policy(); |
| 720 | grpc_byte_buffer_destroy(lb_calld->send_message_payload_); |
| 721 | lb_calld->send_message_payload_ = nullptr; |
| 722 | if (error != GRPC_ERROR_NONE || lb_calld != grpclb_policy->lb_calld_.get()) { |
| 723 | lb_calld->Unref(DEBUG_LOCATION, "client_load_report"); |
| 724 | return; |
| 725 | } |
| 726 | lb_calld->ScheduleNextClientLoadReportLocked(); |
| 727 | } |
| 728 | |
| 729 | void GrpcLb::BalancerCallState::OnInitialRequestSentLocked(void* arg, |
| 730 | grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 731 | BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 732 | grpc_byte_buffer_destroy(lb_calld->send_message_payload_); |
| 733 | lb_calld->send_message_payload_ = nullptr; |
| 734 | // If we attempted to send a client load report before the initial request was |
| 735 | // sent (and this lb_calld is still in use), send the load report now. |
| 736 | if (lb_calld->client_load_report_is_due_ && |
| 737 | lb_calld == lb_calld->grpclb_policy()->lb_calld_.get()) { |
| 738 | lb_calld->SendClientLoadReportLocked(); |
| 739 | lb_calld->client_load_report_is_due_ = false; |
| 740 | } |
| 741 | lb_calld->Unref(DEBUG_LOCATION, "on_initial_request_sent"); |
| 742 | } |
| 743 | |
| 744 | void GrpcLb::BalancerCallState::OnBalancerMessageReceivedLocked( |
| 745 | void* arg, grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 746 | BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 747 | GrpcLb* grpclb_policy = lb_calld->grpclb_policy(); |
| 748 | // Empty payload means the LB call was cancelled. |
| 749 | if (lb_calld != grpclb_policy->lb_calld_.get() || |
| 750 | lb_calld->recv_message_payload_ == nullptr) { |
| 751 | lb_calld->Unref(DEBUG_LOCATION, "on_message_received"); |
| 752 | return; |
| 753 | } |
| 754 | grpc_byte_buffer_reader bbr; |
| 755 | grpc_byte_buffer_reader_init(&bbr, lb_calld->recv_message_payload_); |
| 756 | grpc_slice response_slice = grpc_byte_buffer_reader_readall(&bbr); |
| 757 | grpc_byte_buffer_reader_destroy(&bbr); |
| 758 | grpc_byte_buffer_destroy(lb_calld->recv_message_payload_); |
| 759 | lb_calld->recv_message_payload_ = nullptr; |
| 760 | grpc_grpclb_initial_response* initial_response; |
| 761 | grpc_grpclb_serverlist* serverlist; |
| 762 | if (!lb_calld->seen_initial_response_ && |
| 763 | (initial_response = grpc_grpclb_initial_response_parse(response_slice)) != |
| 764 | nullptr) { |
| 765 | // Have NOT seen initial response, look for initial response. |
| 766 | if (initial_response->has_client_stats_report_interval) { |
| 767 | lb_calld->client_stats_report_interval_ = GPR_MAX( |
| 768 | GPR_MS_PER_SEC, grpc_grpclb_duration_to_millis( |
| 769 | &initial_response->client_stats_report_interval)); |
| 770 | if (grpc_lb_glb_trace.enabled()) { |
| 771 | gpr_log(GPR_INFO, |
| 772 | "[grpclb %p] Received initial LB response message; " |
Sree Kuchibhotla | 1dd12c0 | 2018-04-11 18:05:48 -0700 | [diff] [blame] | 773 | "client load reporting interval = %" PRId64 " milliseconds", |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 774 | grpclb_policy, lb_calld->client_stats_report_interval_); |
| 775 | } |
| 776 | } else if (grpc_lb_glb_trace.enabled()) { |
| 777 | gpr_log(GPR_INFO, |
| 778 | "[grpclb %p] Received initial LB response message; client load " |
| 779 | "reporting NOT enabled", |
| 780 | grpclb_policy); |
| 781 | } |
| 782 | grpc_grpclb_initial_response_destroy(initial_response); |
| 783 | lb_calld->seen_initial_response_ = true; |
| 784 | } else if ((serverlist = grpc_grpclb_response_parse_serverlist( |
| 785 | response_slice)) != nullptr) { |
| 786 | // Have seen initial response, look for serverlist. |
| 787 | GPR_ASSERT(lb_calld->lb_call_ != nullptr); |
| 788 | if (grpc_lb_glb_trace.enabled()) { |
| 789 | gpr_log(GPR_INFO, |
| 790 | "[grpclb %p] Serverlist with %" PRIuPTR " servers received", |
| 791 | grpclb_policy, serverlist->num_servers); |
| 792 | for (size_t i = 0; i < serverlist->num_servers; ++i) { |
| 793 | grpc_resolved_address addr; |
| 794 | ParseServer(serverlist->servers[i], &addr); |
| 795 | char* ipport; |
| 796 | grpc_sockaddr_to_string(&ipport, &addr, false); |
| 797 | gpr_log(GPR_INFO, "[grpclb %p] Serverlist[%" PRIuPTR "]: %s", |
| 798 | grpclb_policy, i, ipport); |
| 799 | gpr_free(ipport); |
| 800 | } |
| 801 | } |
| 802 | /* update serverlist */ |
| 803 | if (serverlist->num_servers > 0) { |
| 804 | // Start sending client load report only after we start using the |
| 805 | // serverlist returned from the current LB call. |
| 806 | if (lb_calld->client_stats_report_interval_ > 0 && |
| 807 | lb_calld->client_stats_ == nullptr) { |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 808 | lb_calld->client_stats_.reset(New<GrpcLbClientStats>()); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 809 | // TODO(roth): We currently track this ref manually. Once the |
| 810 | // ClosureRef API is ready, we should pass the RefCountedPtr<> along |
| 811 | // with the callback. |
| 812 | auto self = lb_calld->Ref(DEBUG_LOCATION, "client_load_report"); |
| 813 | self.release(); |
| 814 | lb_calld->ScheduleNextClientLoadReportLocked(); |
| 815 | } |
| 816 | if (grpc_grpclb_serverlist_equals(grpclb_policy->serverlist_, |
| 817 | serverlist)) { |
| 818 | if (grpc_lb_glb_trace.enabled()) { |
| 819 | gpr_log(GPR_INFO, |
| 820 | "[grpclb %p] Incoming server list identical to current, " |
| 821 | "ignoring.", |
| 822 | grpclb_policy); |
| 823 | } |
| 824 | grpc_grpclb_destroy_serverlist(serverlist); |
| 825 | } else { /* new serverlist */ |
| 826 | if (grpclb_policy->serverlist_ != nullptr) { |
| 827 | /* dispose of the old serverlist */ |
| 828 | grpc_grpclb_destroy_serverlist(grpclb_policy->serverlist_); |
| 829 | } else { |
| 830 | /* or dispose of the fallback */ |
| 831 | grpc_lb_addresses_destroy(grpclb_policy->fallback_backend_addresses_); |
| 832 | grpclb_policy->fallback_backend_addresses_ = nullptr; |
| 833 | if (grpclb_policy->fallback_timer_callback_pending_) { |
| 834 | grpc_timer_cancel(&grpclb_policy->lb_fallback_timer_); |
| 835 | } |
| 836 | } |
| 837 | // and update the copy in the GrpcLb instance. This |
| 838 | // serverlist instance will be destroyed either upon the next |
| 839 | // update or when the GrpcLb instance is destroyed. |
| 840 | grpclb_policy->serverlist_ = serverlist; |
| 841 | grpclb_policy->serverlist_index_ = 0; |
| 842 | grpclb_policy->CreateOrUpdateRoundRobinPolicyLocked(); |
| 843 | } |
| 844 | } else { |
| 845 | if (grpc_lb_glb_trace.enabled()) { |
| 846 | gpr_log(GPR_INFO, "[grpclb %p] Received empty server list, ignoring.", |
| 847 | grpclb_policy); |
| 848 | } |
| 849 | grpc_grpclb_destroy_serverlist(serverlist); |
| 850 | } |
| 851 | } else { |
| 852 | // No valid initial response or serverlist found. |
| 853 | gpr_log(GPR_ERROR, |
| 854 | "[grpclb %p] Invalid LB response received: '%s'. Ignoring.", |
| 855 | grpclb_policy, |
| 856 | grpc_dump_slice(response_slice, GPR_DUMP_ASCII | GPR_DUMP_HEX)); |
| 857 | } |
| 858 | grpc_slice_unref_internal(response_slice); |
| 859 | if (!grpclb_policy->shutting_down_) { |
| 860 | // Keep listening for serverlist updates. |
| 861 | grpc_op op; |
| 862 | memset(&op, 0, sizeof(op)); |
| 863 | op.op = GRPC_OP_RECV_MESSAGE; |
| 864 | op.data.recv_message.recv_message = &lb_calld->recv_message_payload_; |
| 865 | op.flags = 0; |
| 866 | op.reserved = nullptr; |
| 867 | // Reuse the "OnBalancerMessageReceivedLocked" ref taken in StartQuery(). |
| 868 | const grpc_call_error call_error = grpc_call_start_batch_and_execute( |
| 869 | lb_calld->lb_call_, &op, 1, |
| 870 | &lb_calld->lb_on_balancer_message_received_); |
| 871 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 872 | } else { |
| 873 | lb_calld->Unref(DEBUG_LOCATION, "on_message_received+grpclb_shutdown"); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | void GrpcLb::BalancerCallState::OnBalancerStatusReceivedLocked( |
| 878 | void* arg, grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 879 | BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 880 | GrpcLb* grpclb_policy = lb_calld->grpclb_policy(); |
| 881 | GPR_ASSERT(lb_calld->lb_call_ != nullptr); |
| 882 | if (grpc_lb_glb_trace.enabled()) { |
| 883 | char* status_details = |
| 884 | grpc_slice_to_c_string(lb_calld->lb_call_status_details_); |
| 885 | gpr_log(GPR_INFO, |
| 886 | "[grpclb %p] Status from LB server received. Status = %d, details " |
| 887 | "= '%s', (lb_calld: %p, lb_call: %p), error '%s'", |
| 888 | grpclb_policy, lb_calld->lb_call_status_, status_details, lb_calld, |
| 889 | lb_calld->lb_call_, grpc_error_string(error)); |
| 890 | gpr_free(status_details); |
| 891 | } |
| 892 | grpclb_policy->TryReresolutionLocked(&grpc_lb_glb_trace, GRPC_ERROR_NONE); |
| 893 | // If this lb_calld is still in use, this call ended because of a failure so |
| 894 | // we want to retry connecting. Otherwise, we have deliberately ended this |
| 895 | // call and no further action is required. |
| 896 | if (lb_calld == grpclb_policy->lb_calld_.get()) { |
| 897 | grpclb_policy->lb_calld_.reset(); |
| 898 | GPR_ASSERT(!grpclb_policy->shutting_down_); |
| 899 | if (lb_calld->seen_initial_response_) { |
| 900 | // If we lose connection to the LB server, reset the backoff and restart |
| 901 | // the LB call immediately. |
| 902 | grpclb_policy->lb_call_backoff_.Reset(); |
| 903 | grpclb_policy->StartBalancerCallLocked(); |
| 904 | } else { |
| 905 | // If this LB call fails establishing any connection to the LB server, |
| 906 | // retry later. |
| 907 | grpclb_policy->StartBalancerCallRetryTimerLocked(); |
| 908 | } |
| 909 | } |
| 910 | lb_calld->Unref(DEBUG_LOCATION, "lb_call_ended"); |
| 911 | } |
| 912 | |
| 913 | // |
| 914 | // helper code for creating balancer channel |
| 915 | // |
| 916 | |
Mark D. Roth | bd0f151 | 2018-02-20 10:28:22 -0800 | [diff] [blame] | 917 | grpc_lb_addresses* ExtractBalancerAddresses( |
| 918 | const grpc_lb_addresses* addresses) { |
| 919 | size_t num_grpclb_addrs = 0; |
| 920 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 921 | if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs; |
| 922 | } |
| 923 | // There must be at least one balancer address, or else the |
| 924 | // client_channel would not have chosen this LB policy. |
| 925 | GPR_ASSERT(num_grpclb_addrs > 0); |
| 926 | grpc_lb_addresses* lb_addresses = |
| 927 | grpc_lb_addresses_create(num_grpclb_addrs, nullptr); |
| 928 | size_t lb_addresses_idx = 0; |
| 929 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 930 | if (!addresses->addresses[i].is_balancer) continue; |
Yash Tibrewal | 7f51ba8 | 2018-04-12 13:21:20 -0700 | [diff] [blame] | 931 | if (GPR_UNLIKELY(addresses->addresses[i].user_data != nullptr)) { |
Mark D. Roth | bd0f151 | 2018-02-20 10:28:22 -0800 | [diff] [blame] | 932 | gpr_log(GPR_ERROR, |
| 933 | "This LB policy doesn't support user data. It will be ignored"); |
| 934 | } |
| 935 | grpc_lb_addresses_set_address( |
| 936 | lb_addresses, lb_addresses_idx++, addresses->addresses[i].address.addr, |
| 937 | addresses->addresses[i].address.len, false /* is balancer */, |
| 938 | addresses->addresses[i].balancer_name, nullptr /* user data */); |
| 939 | } |
| 940 | GPR_ASSERT(num_grpclb_addrs == lb_addresses_idx); |
| 941 | return lb_addresses; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* Returns the channel args for the LB channel, used to create a bidirectional |
| 945 | * stream for the reception of load balancing updates. |
| 946 | * |
| 947 | * Inputs: |
| 948 | * - \a addresses: corresponding to the balancers. |
| 949 | * - \a response_generator: in order to propagate updates from the resolver |
| 950 | * above the grpclb policy. |
| 951 | * - \a args: other args inherited from the grpclb policy. */ |
| 952 | grpc_channel_args* BuildBalancerChannelArgs( |
| 953 | const grpc_lb_addresses* addresses, |
| 954 | FakeResolverResponseGenerator* response_generator, |
| 955 | const grpc_channel_args* args) { |
Mark D. Roth | bd0f151 | 2018-02-20 10:28:22 -0800 | [diff] [blame] | 956 | grpc_lb_addresses* lb_addresses = ExtractBalancerAddresses(addresses); |
| 957 | // Channel args to remove. |
| 958 | static const char* args_to_remove[] = { |
| 959 | // LB policy name, since we want to use the default (pick_first) in |
| 960 | // the LB channel. |
| 961 | GRPC_ARG_LB_POLICY_NAME, |
| 962 | // The channel arg for the server URI, since that will be different for |
| 963 | // the LB channel than for the parent channel. The client channel |
| 964 | // factory will re-add this arg with the right value. |
| 965 | GRPC_ARG_SERVER_URI, |
| 966 | // The resolved addresses, which will be generated by the name resolver |
| 967 | // used in the LB channel. Note that the LB channel will use the fake |
| 968 | // resolver, so this won't actually generate a query to DNS (or some |
| 969 | // other name service). However, the addresses returned by the fake |
| 970 | // resolver will have is_balancer=false, whereas our own addresses have |
| 971 | // is_balancer=true. We need the LB channel to return addresses with |
| 972 | // is_balancer=false so that it does not wind up recursively using the |
| 973 | // grpclb LB policy, as per the special case logic in client_channel.c. |
| 974 | GRPC_ARG_LB_ADDRESSES, |
| 975 | // The fake resolver response generator, because we are replacing it |
| 976 | // with the one from the grpclb policy, used to propagate updates to |
| 977 | // the LB channel. |
| 978 | GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR, |
David Garcia Quintas | c7c0d69 | 2018-03-10 17:27:15 -0800 | [diff] [blame] | 979 | // The LB channel should use the authority indicated by the target |
| 980 | // authority table (see \a grpc_lb_policy_grpclb_modify_lb_channel_args), |
| 981 | // as opposed to the authority from the parent channel. |
| 982 | GRPC_ARG_DEFAULT_AUTHORITY, |
David Garcia Quintas | 7b84b3d | 2018-03-12 12:08:30 -0700 | [diff] [blame] | 983 | // Just as for \a GRPC_ARG_DEFAULT_AUTHORITY, the LB channel should be |
| 984 | // treated as a stand-alone channel and not inherit this argument from the |
| 985 | // args of the parent channel. |
| 986 | GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, |
Mark D. Roth | bd0f151 | 2018-02-20 10:28:22 -0800 | [diff] [blame] | 987 | }; |
| 988 | // Channel args to add. |
| 989 | const grpc_arg args_to_add[] = { |
| 990 | // New LB addresses. |
| 991 | // Note that we pass these in both when creating the LB channel |
| 992 | // and via the fake resolver. The latter is what actually gets used. |
| 993 | grpc_lb_addresses_create_channel_arg(lb_addresses), |
| 994 | // The fake resolver response generator, which we use to inject |
| 995 | // address updates into the LB channel. |
| 996 | grpc_core::FakeResolverResponseGenerator::MakeChannelArg( |
| 997 | response_generator), |
Yihua Zhang | 392dad7 | 2018-05-03 20:12:20 -0700 | [diff] [blame] | 998 | // A channel arg indicating the target is a grpclb load balancer. |
| 999 | grpc_channel_arg_integer_create( |
| 1000 | const_cast<char*>(GRPC_ARG_ADDRESS_IS_GRPCLB_LOAD_BALANCER), 1), |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 1001 | // A channel arg indicating this is an internal channels, aka it is |
| 1002 | // owned by components in Core, not by the user application. |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 1003 | grpc_channel_arg_integer_create( |
| 1004 | const_cast<char*>(GRPC_ARG_CHANNELZ_CHANNEL_IS_INTERNAL_CHANNEL), 1), |
Mark D. Roth | bd0f151 | 2018-02-20 10:28:22 -0800 | [diff] [blame] | 1005 | }; |
| 1006 | // Construct channel args. |
| 1007 | grpc_channel_args* new_args = grpc_channel_args_copy_and_add_and_remove( |
| 1008 | args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove), args_to_add, |
| 1009 | GPR_ARRAY_SIZE(args_to_add)); |
| 1010 | // Make any necessary modifications for security. |
| 1011 | new_args = grpc_lb_policy_grpclb_modify_lb_channel_args(new_args); |
| 1012 | // Clean up. |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1013 | grpc_lb_addresses_destroy(lb_addresses); |
Mark D. Roth | bd0f151 | 2018-02-20 10:28:22 -0800 | [diff] [blame] | 1014 | return new_args; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | // |
| 1018 | // ctor and dtor |
| 1019 | // |
| 1020 | |
| 1021 | GrpcLb::GrpcLb(const grpc_lb_addresses* addresses, |
| 1022 | const LoadBalancingPolicy::Args& args) |
| 1023 | : LoadBalancingPolicy(args), |
| 1024 | response_generator_(MakeRefCounted<FakeResolverResponseGenerator>()), |
| 1025 | lb_call_backoff_( |
| 1026 | BackOff::Options() |
| 1027 | .set_initial_backoff(GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS * |
| 1028 | 1000) |
| 1029 | .set_multiplier(GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER) |
| 1030 | .set_jitter(GRPC_GRPCLB_RECONNECT_JITTER) |
| 1031 | .set_max_backoff(GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS * |
| 1032 | 1000)) { |
| 1033 | // Initialization. |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 1034 | gpr_mu_init(&lb_channel_mu_); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1035 | grpc_subchannel_index_ref(); |
| 1036 | GRPC_CLOSURE_INIT(&lb_channel_on_connectivity_changed_, |
| 1037 | &GrpcLb::OnBalancerChannelConnectivityChangedLocked, this, |
| 1038 | grpc_combiner_scheduler(args.combiner)); |
| 1039 | GRPC_CLOSURE_INIT(&on_rr_connectivity_changed_, |
| 1040 | &GrpcLb::OnRoundRobinConnectivityChangedLocked, this, |
| 1041 | grpc_combiner_scheduler(args.combiner)); |
| 1042 | GRPC_CLOSURE_INIT(&on_rr_request_reresolution_, |
| 1043 | &GrpcLb::OnRoundRobinRequestReresolutionLocked, this, |
| 1044 | grpc_combiner_scheduler(args.combiner)); |
| 1045 | grpc_connectivity_state_init(&state_tracker_, GRPC_CHANNEL_IDLE, "grpclb"); |
| 1046 | // Record server name. |
Noah Eisen | 7ea8a60 | 2018-06-14 11:43:18 -0400 | [diff] [blame] | 1047 | const grpc_arg* arg = grpc_channel_args_find(args.args, GRPC_ARG_SERVER_URI); |
| 1048 | const char* server_uri = grpc_channel_arg_get_string(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1049 | GPR_ASSERT(server_uri != nullptr); |
| 1050 | grpc_uri* uri = grpc_uri_parse(server_uri, true); |
| 1051 | GPR_ASSERT(uri->path[0] != '\0'); |
| 1052 | server_name_ = gpr_strdup(uri->path[0] == '/' ? uri->path + 1 : uri->path); |
| 1053 | if (grpc_lb_glb_trace.enabled()) { |
| 1054 | gpr_log(GPR_INFO, |
| 1055 | "[grpclb %p] Will use '%s' as the server name for LB request.", |
| 1056 | this, server_name_); |
| 1057 | } |
| 1058 | grpc_uri_destroy(uri); |
| 1059 | // Record LB call timeout. |
Noah Eisen | 7ea8a60 | 2018-06-14 11:43:18 -0400 | [diff] [blame] | 1060 | arg = grpc_channel_args_find(args.args, GRPC_ARG_GRPCLB_CALL_TIMEOUT_MS); |
| 1061 | lb_call_timeout_ms_ = grpc_channel_arg_get_integer(arg, {0, 0, INT_MAX}); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1062 | // Record fallback timeout. |
Noah Eisen | 7ea8a60 | 2018-06-14 11:43:18 -0400 | [diff] [blame] | 1063 | arg = grpc_channel_args_find(args.args, GRPC_ARG_GRPCLB_FALLBACK_TIMEOUT_MS); |
| 1064 | lb_fallback_timeout_ms_ = grpc_channel_arg_get_integer( |
| 1065 | arg, {GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS, 0, INT_MAX}); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1066 | // Process channel args. |
| 1067 | ProcessChannelArgsLocked(*args.args); |
| 1068 | } |
| 1069 | |
| 1070 | GrpcLb::~GrpcLb() { |
| 1071 | GPR_ASSERT(pending_picks_ == nullptr); |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 1072 | gpr_mu_destroy(&lb_channel_mu_); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1073 | gpr_free((void*)server_name_); |
| 1074 | grpc_channel_args_destroy(args_); |
| 1075 | grpc_connectivity_state_destroy(&state_tracker_); |
| 1076 | if (serverlist_ != nullptr) { |
| 1077 | grpc_grpclb_destroy_serverlist(serverlist_); |
| 1078 | } |
| 1079 | if (fallback_backend_addresses_ != nullptr) { |
| 1080 | grpc_lb_addresses_destroy(fallback_backend_addresses_); |
| 1081 | } |
| 1082 | grpc_subchannel_index_unref(); |
| 1083 | } |
| 1084 | |
| 1085 | void GrpcLb::ShutdownLocked() { |
| 1086 | grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel shutdown"); |
| 1087 | shutting_down_ = true; |
| 1088 | lb_calld_.reset(); |
| 1089 | if (retry_timer_callback_pending_) { |
| 1090 | grpc_timer_cancel(&lb_call_retry_timer_); |
| 1091 | } |
| 1092 | if (fallback_timer_callback_pending_) { |
| 1093 | grpc_timer_cancel(&lb_fallback_timer_); |
| 1094 | } |
| 1095 | rr_policy_.reset(); |
| 1096 | TryReresolutionLocked(&grpc_lb_glb_trace, GRPC_ERROR_CANCELLED); |
| 1097 | // We destroy the LB channel here instead of in our destructor because |
| 1098 | // destroying the channel triggers a last callback to |
| 1099 | // OnBalancerChannelConnectivityChangedLocked(), and we need to be |
| 1100 | // alive when that callback is invoked. |
| 1101 | if (lb_channel_ != nullptr) { |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 1102 | gpr_mu_lock(&lb_channel_mu_); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1103 | grpc_channel_destroy(lb_channel_); |
| 1104 | lb_channel_ = nullptr; |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 1105 | gpr_mu_unlock(&lb_channel_mu_); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1106 | } |
| 1107 | grpc_connectivity_state_set(&state_tracker_, GRPC_CHANNEL_SHUTDOWN, |
| 1108 | GRPC_ERROR_REF(error), "grpclb_shutdown"); |
| 1109 | // Clear pending picks. |
| 1110 | PendingPick* pp; |
| 1111 | while ((pp = pending_picks_) != nullptr) { |
| 1112 | pending_picks_ = pp->next; |
| 1113 | pp->pick->connected_subchannel.reset(); |
| 1114 | // Note: pp is deleted in this callback. |
| 1115 | GRPC_CLOSURE_SCHED(&pp->on_complete, GRPC_ERROR_REF(error)); |
| 1116 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1117 | GRPC_ERROR_UNREF(error); |
| 1118 | } |
| 1119 | |
| 1120 | // |
| 1121 | // public methods |
| 1122 | // |
| 1123 | |
| 1124 | void GrpcLb::HandOffPendingPicksLocked(LoadBalancingPolicy* new_policy) { |
| 1125 | PendingPick* pp; |
| 1126 | while ((pp = pending_picks_) != nullptr) { |
| 1127 | pending_picks_ = pp->next; |
| 1128 | pp->pick->on_complete = pp->original_on_complete; |
| 1129 | pp->pick->user_data = nullptr; |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1130 | grpc_error* error = GRPC_ERROR_NONE; |
| 1131 | if (new_policy->PickLocked(pp->pick, &error)) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1132 | // Synchronous return; schedule closure. |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1133 | GRPC_CLOSURE_SCHED(pp->pick->on_complete, error); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1134 | } |
| 1135 | Delete(pp); |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | // Cancel a specific pending pick. |
| 1140 | // |
| 1141 | // A grpclb pick progresses as follows: |
| 1142 | // - If there's a Round Robin policy (rr_policy_) available, it'll be |
| 1143 | // handed over to the RR policy (in CreateRoundRobinPolicyLocked()). From |
| 1144 | // that point onwards, it'll be RR's responsibility. For cancellations, that |
| 1145 | // implies the pick needs also be cancelled by the RR instance. |
| 1146 | // - Otherwise, without an RR instance, picks stay pending at this policy's |
| 1147 | // level (grpclb), inside the pending_picks_ list. To cancel these, |
| 1148 | // we invoke the completion closure and set the pick's connected |
| 1149 | // subchannel to nullptr right here. |
| 1150 | void GrpcLb::CancelPickLocked(PickState* pick, grpc_error* error) { |
| 1151 | PendingPick* pp = pending_picks_; |
| 1152 | pending_picks_ = nullptr; |
| 1153 | while (pp != nullptr) { |
| 1154 | PendingPick* next = pp->next; |
| 1155 | if (pp->pick == pick) { |
| 1156 | pick->connected_subchannel.reset(); |
| 1157 | // Note: pp is deleted in this callback. |
| 1158 | GRPC_CLOSURE_SCHED(&pp->on_complete, |
| 1159 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1160 | "Pick Cancelled", &error, 1)); |
| 1161 | } else { |
| 1162 | pp->next = pending_picks_; |
| 1163 | pending_picks_ = pp; |
| 1164 | } |
| 1165 | pp = next; |
| 1166 | } |
| 1167 | if (rr_policy_ != nullptr) { |
| 1168 | rr_policy_->CancelPickLocked(pick, GRPC_ERROR_REF(error)); |
| 1169 | } |
| 1170 | GRPC_ERROR_UNREF(error); |
| 1171 | } |
| 1172 | |
| 1173 | // Cancel all pending picks. |
| 1174 | // |
| 1175 | // A grpclb pick progresses as follows: |
| 1176 | // - If there's a Round Robin policy (rr_policy_) available, it'll be |
| 1177 | // handed over to the RR policy (in CreateRoundRobinPolicyLocked()). From |
| 1178 | // that point onwards, it'll be RR's responsibility. For cancellations, that |
| 1179 | // implies the pick needs also be cancelled by the RR instance. |
| 1180 | // - Otherwise, without an RR instance, picks stay pending at this policy's |
| 1181 | // level (grpclb), inside the pending_picks_ list. To cancel these, |
| 1182 | // we invoke the completion closure and set the pick's connected |
| 1183 | // subchannel to nullptr right here. |
| 1184 | void GrpcLb::CancelMatchingPicksLocked(uint32_t initial_metadata_flags_mask, |
| 1185 | uint32_t initial_metadata_flags_eq, |
| 1186 | grpc_error* error) { |
| 1187 | PendingPick* pp = pending_picks_; |
| 1188 | pending_picks_ = nullptr; |
| 1189 | while (pp != nullptr) { |
| 1190 | PendingPick* next = pp->next; |
| 1191 | if ((pp->pick->initial_metadata_flags & initial_metadata_flags_mask) == |
| 1192 | initial_metadata_flags_eq) { |
| 1193 | // Note: pp is deleted in this callback. |
| 1194 | GRPC_CLOSURE_SCHED(&pp->on_complete, |
| 1195 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1196 | "Pick Cancelled", &error, 1)); |
| 1197 | } else { |
| 1198 | pp->next = pending_picks_; |
| 1199 | pending_picks_ = pp; |
| 1200 | } |
| 1201 | pp = next; |
| 1202 | } |
| 1203 | if (rr_policy_ != nullptr) { |
| 1204 | rr_policy_->CancelMatchingPicksLocked(initial_metadata_flags_mask, |
| 1205 | initial_metadata_flags_eq, |
| 1206 | GRPC_ERROR_REF(error)); |
| 1207 | } |
| 1208 | GRPC_ERROR_UNREF(error); |
| 1209 | } |
| 1210 | |
| 1211 | void GrpcLb::ExitIdleLocked() { |
| 1212 | if (!started_picking_) { |
| 1213 | StartPickingLocked(); |
| 1214 | } |
| 1215 | } |
| 1216 | |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1217 | bool GrpcLb::PickLocked(PickState* pick, grpc_error** error) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1218 | PendingPick* pp = PendingPickCreate(pick); |
| 1219 | bool pick_done = false; |
| 1220 | if (rr_policy_ != nullptr) { |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1221 | if (grpc_lb_glb_trace.enabled()) { |
| 1222 | gpr_log(GPR_INFO, "[grpclb %p] about to PICK from RR %p", this, |
| 1223 | rr_policy_.get()); |
| 1224 | } |
| 1225 | pick_done = |
| 1226 | PickFromRoundRobinPolicyLocked(false /* force_async */, pp, error); |
| 1227 | } else { // rr_policy_ == NULL |
| 1228 | if (pick->on_complete == nullptr) { |
| 1229 | *error = GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 1230 | "No pick result available but synchronous result required."); |
| 1231 | pick_done = true; |
| 1232 | } else { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1233 | if (grpc_lb_glb_trace.enabled()) { |
| 1234 | gpr_log(GPR_INFO, |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1235 | "[grpclb %p] No RR policy. Adding to grpclb's pending picks", |
| 1236 | this); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1237 | } |
| 1238 | AddPendingPick(pp); |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1239 | if (!started_picking_) { |
| 1240 | StartPickingLocked(); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1241 | } |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1242 | pick_done = false; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1243 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1244 | } |
| 1245 | return pick_done; |
| 1246 | } |
| 1247 | |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 1248 | void GrpcLb::FillChildRefsForChannelz(ChildRefsList* child_subchannels, |
| 1249 | ChildRefsList* child_channels) { |
| 1250 | // delegate to the RoundRobin to fill the children subchannels. |
| 1251 | rr_policy_->FillChildRefsForChannelz(child_subchannels, child_channels); |
| 1252 | mu_guard guard(&lb_channel_mu_); |
| 1253 | if (lb_channel_ != nullptr) { |
| 1254 | grpc_core::channelz::ChannelNode* channel_node = |
| 1255 | grpc_channel_get_channelz_node(lb_channel_); |
| 1256 | if (channel_node != nullptr) { |
| 1257 | child_channels->push_back(channel_node->channel_uuid()); |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1262 | grpc_connectivity_state GrpcLb::CheckConnectivityLocked( |
| 1263 | grpc_error** connectivity_error) { |
| 1264 | return grpc_connectivity_state_get(&state_tracker_, connectivity_error); |
| 1265 | } |
| 1266 | |
| 1267 | void GrpcLb::NotifyOnStateChangeLocked(grpc_connectivity_state* current, |
| 1268 | grpc_closure* notify) { |
| 1269 | grpc_connectivity_state_notify_on_state_change(&state_tracker_, current, |
| 1270 | notify); |
| 1271 | } |
| 1272 | |
| 1273 | void GrpcLb::ProcessChannelArgsLocked(const grpc_channel_args& args) { |
Noah Eisen | 7ea8a60 | 2018-06-14 11:43:18 -0400 | [diff] [blame] | 1274 | const grpc_arg* arg = grpc_channel_args_find(&args, GRPC_ARG_LB_ADDRESSES); |
| 1275 | if (GPR_UNLIKELY(arg == nullptr || arg->type != GRPC_ARG_POINTER)) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1276 | // Ignore this update. |
| 1277 | gpr_log( |
| 1278 | GPR_ERROR, |
| 1279 | "[grpclb %p] No valid LB addresses channel arg in update, ignoring.", |
| 1280 | this); |
| 1281 | return; |
| 1282 | } |
Noah Eisen | 7ea8a60 | 2018-06-14 11:43:18 -0400 | [diff] [blame] | 1283 | const grpc_lb_addresses* addresses = |
| 1284 | static_cast<const grpc_lb_addresses*>(arg->value.pointer.p); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1285 | // Update fallback address list. |
| 1286 | if (fallback_backend_addresses_ != nullptr) { |
| 1287 | grpc_lb_addresses_destroy(fallback_backend_addresses_); |
| 1288 | } |
| 1289 | fallback_backend_addresses_ = ExtractBackendAddresses(addresses); |
| 1290 | // Make sure that GRPC_ARG_LB_POLICY_NAME is set in channel args, |
| 1291 | // since we use this to trigger the client_load_reporting filter. |
| 1292 | static const char* args_to_remove[] = {GRPC_ARG_LB_POLICY_NAME}; |
| 1293 | grpc_arg new_arg = grpc_channel_arg_string_create( |
| 1294 | (char*)GRPC_ARG_LB_POLICY_NAME, (char*)"grpclb"); |
| 1295 | grpc_channel_args_destroy(args_); |
| 1296 | args_ = grpc_channel_args_copy_and_add_and_remove( |
| 1297 | &args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove), &new_arg, 1); |
| 1298 | // Construct args for balancer channel. |
| 1299 | grpc_channel_args* lb_channel_args = |
| 1300 | BuildBalancerChannelArgs(addresses, response_generator_.get(), &args); |
| 1301 | // Create balancer channel if needed. |
| 1302 | if (lb_channel_ == nullptr) { |
| 1303 | char* uri_str; |
| 1304 | gpr_asprintf(&uri_str, "fake:///%s", server_name_); |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 1305 | gpr_mu_lock(&lb_channel_mu_); |
Mark D. Roth | bd0f151 | 2018-02-20 10:28:22 -0800 | [diff] [blame] | 1306 | lb_channel_ = grpc_client_channel_factory_create_channel( |
| 1307 | client_channel_factory(), uri_str, |
| 1308 | GRPC_CLIENT_CHANNEL_TYPE_LOAD_BALANCING, lb_channel_args); |
ncteisen | d47779b | 2018-07-18 11:46:06 -0700 | [diff] [blame] | 1309 | gpr_mu_unlock(&lb_channel_mu_); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1310 | GPR_ASSERT(lb_channel_ != nullptr); |
| 1311 | gpr_free(uri_str); |
| 1312 | } |
| 1313 | // Propagate updates to the LB channel (pick_first) through the fake |
| 1314 | // resolver. |
| 1315 | response_generator_->SetResponse(lb_channel_args); |
| 1316 | grpc_channel_args_destroy(lb_channel_args); |
| 1317 | } |
| 1318 | |
| 1319 | void GrpcLb::UpdateLocked(const grpc_channel_args& args) { |
| 1320 | ProcessChannelArgsLocked(args); |
| 1321 | // If fallback is configured and the RR policy already exists, update |
| 1322 | // it with the new fallback addresses. |
| 1323 | if (lb_fallback_timeout_ms_ > 0 && rr_policy_ != nullptr) { |
| 1324 | CreateOrUpdateRoundRobinPolicyLocked(); |
| 1325 | } |
| 1326 | // Start watching the LB channel connectivity for connection, if not |
| 1327 | // already doing so. |
| 1328 | if (!watching_lb_channel_) { |
| 1329 | lb_channel_connectivity_ = grpc_channel_check_connectivity_state( |
| 1330 | lb_channel_, true /* try to connect */); |
| 1331 | grpc_channel_element* client_channel_elem = grpc_channel_stack_last_element( |
| 1332 | grpc_channel_get_channel_stack(lb_channel_)); |
| 1333 | GPR_ASSERT(client_channel_elem->filter == &grpc_client_channel_filter); |
| 1334 | watching_lb_channel_ = true; |
| 1335 | // TODO(roth): We currently track this ref manually. Once the |
| 1336 | // ClosureRef API is ready, we should pass the RefCountedPtr<> along |
| 1337 | // with the callback. |
| 1338 | auto self = Ref(DEBUG_LOCATION, "watch_lb_channel_connectivity"); |
| 1339 | self.release(); |
| 1340 | grpc_client_channel_watch_connectivity_state( |
| 1341 | client_channel_elem, |
| 1342 | grpc_polling_entity_create_from_pollset_set(interested_parties()), |
| 1343 | &lb_channel_connectivity_, &lb_channel_on_connectivity_changed_, |
| 1344 | nullptr); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | // |
| 1349 | // code for balancer channel and call |
| 1350 | // |
| 1351 | |
| 1352 | void GrpcLb::StartPickingLocked() { |
| 1353 | // Start a timer to fall back. |
| 1354 | if (lb_fallback_timeout_ms_ > 0 && serverlist_ == nullptr && |
| 1355 | !fallback_timer_callback_pending_) { |
| 1356 | grpc_millis deadline = ExecCtx::Get()->Now() + lb_fallback_timeout_ms_; |
| 1357 | // TODO(roth): We currently track this ref manually. Once the |
| 1358 | // ClosureRef API is ready, we should pass the RefCountedPtr<> along |
| 1359 | // with the callback. |
| 1360 | auto self = Ref(DEBUG_LOCATION, "on_fallback_timer"); |
| 1361 | self.release(); |
| 1362 | GRPC_CLOSURE_INIT(&lb_on_fallback_, &GrpcLb::OnFallbackTimerLocked, this, |
| 1363 | grpc_combiner_scheduler(combiner())); |
| 1364 | fallback_timer_callback_pending_ = true; |
| 1365 | grpc_timer_init(&lb_fallback_timer_, deadline, &lb_on_fallback_); |
| 1366 | } |
| 1367 | started_picking_ = true; |
| 1368 | StartBalancerCallLocked(); |
| 1369 | } |
| 1370 | |
| 1371 | void GrpcLb::StartBalancerCallLocked() { |
| 1372 | GPR_ASSERT(lb_channel_ != nullptr); |
| 1373 | if (shutting_down_) return; |
| 1374 | // Init the LB call data. |
| 1375 | GPR_ASSERT(lb_calld_ == nullptr); |
| 1376 | lb_calld_ = MakeOrphanable<BalancerCallState>(Ref()); |
| 1377 | if (grpc_lb_glb_trace.enabled()) { |
| 1378 | gpr_log(GPR_INFO, |
| 1379 | "[grpclb %p] Query for backends (lb_channel: %p, lb_calld: %p)", |
| 1380 | this, lb_channel_, lb_calld_.get()); |
| 1381 | } |
| 1382 | lb_calld_->StartQuery(); |
| 1383 | } |
| 1384 | |
| 1385 | void GrpcLb::OnFallbackTimerLocked(void* arg, grpc_error* error) { |
| 1386 | GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg); |
| 1387 | grpclb_policy->fallback_timer_callback_pending_ = false; |
| 1388 | // If we receive a serverlist after the timer fires but before this callback |
| 1389 | // actually runs, don't fall back. |
| 1390 | if (grpclb_policy->serverlist_ == nullptr && !grpclb_policy->shutting_down_ && |
| 1391 | error == GRPC_ERROR_NONE) { |
| 1392 | if (grpc_lb_glb_trace.enabled()) { |
| 1393 | gpr_log(GPR_INFO, |
| 1394 | "[grpclb %p] Falling back to use backends from resolver", |
| 1395 | grpclb_policy); |
| 1396 | } |
| 1397 | GPR_ASSERT(grpclb_policy->fallback_backend_addresses_ != nullptr); |
| 1398 | grpclb_policy->CreateOrUpdateRoundRobinPolicyLocked(); |
| 1399 | } |
| 1400 | grpclb_policy->Unref(DEBUG_LOCATION, "on_fallback_timer"); |
| 1401 | } |
| 1402 | |
| 1403 | void GrpcLb::StartBalancerCallRetryTimerLocked() { |
| 1404 | grpc_millis next_try = lb_call_backoff_.NextAttemptTime(); |
| 1405 | if (grpc_lb_glb_trace.enabled()) { |
Mark D. Roth | 48854d2 | 2018-04-25 13:05:26 -0700 | [diff] [blame] | 1406 | gpr_log(GPR_INFO, "[grpclb %p] Connection to LB server lost...", this); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1407 | grpc_millis timeout = next_try - ExecCtx::Get()->Now(); |
| 1408 | if (timeout > 0) { |
Sree Kuchibhotla | 6d5c2c2 | 2018-05-08 10:24:30 -0700 | [diff] [blame] | 1409 | gpr_log(GPR_INFO, "[grpclb %p] ... retry_timer_active in %" PRId64 "ms.", |
Mark D. Roth | 48854d2 | 2018-04-25 13:05:26 -0700 | [diff] [blame] | 1410 | this, timeout); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1411 | } else { |
Mark D. Roth | 48854d2 | 2018-04-25 13:05:26 -0700 | [diff] [blame] | 1412 | gpr_log(GPR_INFO, "[grpclb %p] ... retry_timer_active immediately.", |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1413 | this); |
| 1414 | } |
| 1415 | } |
| 1416 | // TODO(roth): We currently track this ref manually. Once the |
| 1417 | // ClosureRef API is ready, we should pass the RefCountedPtr<> along |
| 1418 | // with the callback. |
| 1419 | auto self = Ref(DEBUG_LOCATION, "on_balancer_call_retry_timer"); |
| 1420 | self.release(); |
| 1421 | GRPC_CLOSURE_INIT(&lb_on_call_retry_, &GrpcLb::OnBalancerCallRetryTimerLocked, |
| 1422 | this, grpc_combiner_scheduler(combiner())); |
| 1423 | retry_timer_callback_pending_ = true; |
| 1424 | grpc_timer_init(&lb_call_retry_timer_, next_try, &lb_on_call_retry_); |
| 1425 | } |
| 1426 | |
| 1427 | void GrpcLb::OnBalancerCallRetryTimerLocked(void* arg, grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 1428 | GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1429 | grpclb_policy->retry_timer_callback_pending_ = false; |
| 1430 | if (!grpclb_policy->shutting_down_ && error == GRPC_ERROR_NONE && |
| 1431 | grpclb_policy->lb_calld_ == nullptr) { |
| 1432 | if (grpc_lb_glb_trace.enabled()) { |
| 1433 | gpr_log(GPR_INFO, "[grpclb %p] Restarting call to LB server", |
| 1434 | grpclb_policy); |
| 1435 | } |
| 1436 | grpclb_policy->StartBalancerCallLocked(); |
| 1437 | } |
| 1438 | grpclb_policy->Unref(DEBUG_LOCATION, "on_balancer_call_retry_timer"); |
| 1439 | } |
| 1440 | |
| 1441 | // Invoked as part of the update process. It continues watching the LB channel |
| 1442 | // until it shuts down or becomes READY. It's invoked even if the LB channel |
| 1443 | // stayed READY throughout the update (for example if the update is identical). |
| 1444 | void GrpcLb::OnBalancerChannelConnectivityChangedLocked(void* arg, |
| 1445 | grpc_error* error) { |
| 1446 | GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg); |
| 1447 | if (grpclb_policy->shutting_down_) goto done; |
| 1448 | // Re-initialize the lb_call. This should also take care of updating the |
| 1449 | // embedded RR policy. Note that the current RR policy, if any, will stay in |
| 1450 | // effect until an update from the new lb_call is received. |
| 1451 | switch (grpclb_policy->lb_channel_connectivity_) { |
| 1452 | case GRPC_CHANNEL_CONNECTING: |
| 1453 | case GRPC_CHANNEL_TRANSIENT_FAILURE: { |
| 1454 | // Keep watching the LB channel. |
| 1455 | grpc_channel_element* client_channel_elem = |
| 1456 | grpc_channel_stack_last_element( |
| 1457 | grpc_channel_get_channel_stack(grpclb_policy->lb_channel_)); |
| 1458 | GPR_ASSERT(client_channel_elem->filter == &grpc_client_channel_filter); |
| 1459 | grpc_client_channel_watch_connectivity_state( |
| 1460 | client_channel_elem, |
| 1461 | grpc_polling_entity_create_from_pollset_set( |
| 1462 | grpclb_policy->interested_parties()), |
| 1463 | &grpclb_policy->lb_channel_connectivity_, |
| 1464 | &grpclb_policy->lb_channel_on_connectivity_changed_, nullptr); |
| 1465 | break; |
| 1466 | } |
| 1467 | // The LB channel may be IDLE because it's shut down before the update. |
| 1468 | // Restart the LB call to kick the LB channel into gear. |
| 1469 | case GRPC_CHANNEL_IDLE: |
| 1470 | case GRPC_CHANNEL_READY: |
| 1471 | grpclb_policy->lb_calld_.reset(); |
| 1472 | if (grpclb_policy->started_picking_) { |
| 1473 | if (grpclb_policy->retry_timer_callback_pending_) { |
| 1474 | grpc_timer_cancel(&grpclb_policy->lb_call_retry_timer_); |
| 1475 | } |
| 1476 | grpclb_policy->lb_call_backoff_.Reset(); |
| 1477 | grpclb_policy->StartBalancerCallLocked(); |
| 1478 | } |
| 1479 | // Fall through. |
| 1480 | case GRPC_CHANNEL_SHUTDOWN: |
| 1481 | done: |
| 1482 | grpclb_policy->watching_lb_channel_ = false; |
| 1483 | grpclb_policy->Unref(DEBUG_LOCATION, |
| 1484 | "watch_lb_channel_connectivity_cb_shutdown"); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | // |
| 1489 | // PendingPick |
| 1490 | // |
| 1491 | |
| 1492 | // Adds lb_token of selected subchannel (address) to the call's initial |
| 1493 | // metadata. |
| 1494 | grpc_error* AddLbTokenToInitialMetadata( |
| 1495 | grpc_mdelem lb_token, grpc_linked_mdelem* lb_token_mdelem_storage, |
| 1496 | grpc_metadata_batch* initial_metadata) { |
| 1497 | GPR_ASSERT(lb_token_mdelem_storage != nullptr); |
| 1498 | GPR_ASSERT(!GRPC_MDISNULL(lb_token)); |
| 1499 | return grpc_metadata_batch_add_tail(initial_metadata, lb_token_mdelem_storage, |
| 1500 | lb_token); |
| 1501 | } |
| 1502 | |
| 1503 | // Destroy function used when embedding client stats in call context. |
| 1504 | void DestroyClientStats(void* arg) { |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 1505 | static_cast<GrpcLbClientStats*>(arg)->Unref(); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | void GrpcLb::PendingPickSetMetadataAndContext(PendingPick* pp) { |
| 1509 | /* if connected_subchannel is nullptr, no pick has been made by the RR |
| 1510 | * policy (e.g., all addresses failed to connect). There won't be any |
| 1511 | * user_data/token available */ |
| 1512 | if (pp->pick->connected_subchannel != nullptr) { |
Yash Tibrewal | 7f51ba8 | 2018-04-12 13:21:20 -0700 | [diff] [blame] | 1513 | if (GPR_LIKELY(!GRPC_MDISNULL(pp->lb_token))) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1514 | AddLbTokenToInitialMetadata(GRPC_MDELEM_REF(pp->lb_token), |
| 1515 | &pp->pick->lb_token_mdelem_storage, |
| 1516 | pp->pick->initial_metadata); |
| 1517 | } else { |
| 1518 | gpr_log(GPR_ERROR, |
| 1519 | "[grpclb %p] No LB token for connected subchannel pick %p", |
| 1520 | pp->grpclb_policy, pp->pick); |
| 1521 | abort(); |
| 1522 | } |
| 1523 | // Pass on client stats via context. Passes ownership of the reference. |
| 1524 | if (pp->client_stats != nullptr) { |
| 1525 | pp->pick->subchannel_call_context[GRPC_GRPCLB_CLIENT_STATS].value = |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 1526 | pp->client_stats.release(); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1527 | pp->pick->subchannel_call_context[GRPC_GRPCLB_CLIENT_STATS].destroy = |
| 1528 | DestroyClientStats; |
| 1529 | } |
| 1530 | } else { |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 1531 | pp->client_stats.reset(); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | /* The \a on_complete closure passed as part of the pick requires keeping a |
| 1536 | * reference to its associated round robin instance. We wrap this closure in |
| 1537 | * order to unref the round robin instance upon its invocation */ |
| 1538 | void GrpcLb::OnPendingPickComplete(void* arg, grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 1539 | PendingPick* pp = static_cast<PendingPick*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1540 | PendingPickSetMetadataAndContext(pp); |
| 1541 | GRPC_CLOSURE_SCHED(pp->original_on_complete, GRPC_ERROR_REF(error)); |
| 1542 | Delete(pp); |
| 1543 | } |
| 1544 | |
| 1545 | GrpcLb::PendingPick* GrpcLb::PendingPickCreate(PickState* pick) { |
| 1546 | PendingPick* pp = New<PendingPick>(); |
| 1547 | pp->grpclb_policy = this; |
| 1548 | pp->pick = pick; |
| 1549 | GRPC_CLOSURE_INIT(&pp->on_complete, &GrpcLb::OnPendingPickComplete, pp, |
| 1550 | grpc_schedule_on_exec_ctx); |
| 1551 | pp->original_on_complete = pick->on_complete; |
| 1552 | pick->on_complete = &pp->on_complete; |
| 1553 | return pp; |
| 1554 | } |
| 1555 | |
| 1556 | void GrpcLb::AddPendingPick(PendingPick* pp) { |
| 1557 | pp->next = pending_picks_; |
| 1558 | pending_picks_ = pp; |
| 1559 | } |
| 1560 | |
| 1561 | // |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1562 | // code for interacting with the RR policy |
| 1563 | // |
| 1564 | |
| 1565 | // Performs a pick over \a rr_policy_. Given that a pick can return |
| 1566 | // immediately (ignoring its completion callback), we need to perform the |
| 1567 | // cleanups this callback would otherwise be responsible for. |
| 1568 | // If \a force_async is true, then we will manually schedule the |
| 1569 | // completion callback even if the pick is available immediately. |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1570 | bool GrpcLb::PickFromRoundRobinPolicyLocked(bool force_async, PendingPick* pp, |
| 1571 | grpc_error** error) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1572 | // Check for drops if we are not using fallback backend addresses. |
| 1573 | if (serverlist_ != nullptr) { |
| 1574 | // Look at the index into the serverlist to see if we should drop this call. |
| 1575 | grpc_grpclb_server* server = serverlist_->servers[serverlist_index_++]; |
| 1576 | if (serverlist_index_ == serverlist_->num_servers) { |
| 1577 | serverlist_index_ = 0; // Wrap-around. |
| 1578 | } |
| 1579 | if (server->drop) { |
| 1580 | // Update client load reporting stats to indicate the number of |
| 1581 | // dropped calls. Note that we have to do this here instead of in |
| 1582 | // the client_load_reporting filter, because we do not create a |
| 1583 | // subchannel call (and therefore no client_load_reporting filter) |
| 1584 | // for dropped calls. |
| 1585 | if (lb_calld_ != nullptr && lb_calld_->client_stats() != nullptr) { |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 1586 | lb_calld_->client_stats()->AddCallDroppedLocked( |
| 1587 | server->load_balance_token); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1588 | } |
| 1589 | if (force_async) { |
| 1590 | GRPC_CLOSURE_SCHED(pp->original_on_complete, GRPC_ERROR_NONE); |
| 1591 | Delete(pp); |
| 1592 | return false; |
| 1593 | } |
| 1594 | Delete(pp); |
| 1595 | return true; |
| 1596 | } |
| 1597 | } |
| 1598 | // Set client_stats and user_data. |
| 1599 | if (lb_calld_ != nullptr && lb_calld_->client_stats() != nullptr) { |
Mark D. Roth | 290d35e | 2018-05-17 14:15:36 -0700 | [diff] [blame] | 1600 | pp->client_stats = lb_calld_->client_stats()->Ref(); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1601 | } |
| 1602 | GPR_ASSERT(pp->pick->user_data == nullptr); |
| 1603 | pp->pick->user_data = (void**)&pp->lb_token; |
| 1604 | // Pick via the RR policy. |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1605 | bool pick_done = rr_policy_->PickLocked(pp->pick, error); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1606 | if (pick_done) { |
| 1607 | PendingPickSetMetadataAndContext(pp); |
| 1608 | if (force_async) { |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1609 | GRPC_CLOSURE_SCHED(pp->original_on_complete, *error); |
| 1610 | *error = GRPC_ERROR_NONE; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1611 | pick_done = false; |
| 1612 | } |
| 1613 | Delete(pp); |
| 1614 | } |
| 1615 | // else, the pending pick will be registered and taken care of by the |
| 1616 | // pending pick list inside the RR policy. Eventually, |
| 1617 | // OnPendingPickComplete() will be called, which will (among other |
| 1618 | // things) add the LB token to the call's initial metadata. |
| 1619 | return pick_done; |
| 1620 | } |
| 1621 | |
| 1622 | void GrpcLb::CreateRoundRobinPolicyLocked(const Args& args) { |
| 1623 | GPR_ASSERT(rr_policy_ == nullptr); |
| 1624 | rr_policy_ = LoadBalancingPolicyRegistry::CreateLoadBalancingPolicy( |
| 1625 | "round_robin", args); |
Yash Tibrewal | 7f51ba8 | 2018-04-12 13:21:20 -0700 | [diff] [blame] | 1626 | if (GPR_UNLIKELY(rr_policy_ == nullptr)) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1627 | gpr_log(GPR_ERROR, "[grpclb %p] Failure creating a RoundRobin policy", |
| 1628 | this); |
| 1629 | return; |
| 1630 | } |
| 1631 | // TODO(roth): We currently track this ref manually. Once the new |
| 1632 | // ClosureRef API is done, pass the RefCountedPtr<> along with the closure. |
| 1633 | auto self = Ref(DEBUG_LOCATION, "on_rr_reresolution_requested"); |
| 1634 | self.release(); |
| 1635 | rr_policy_->SetReresolutionClosureLocked(&on_rr_request_reresolution_); |
| 1636 | grpc_error* rr_state_error = nullptr; |
| 1637 | rr_connectivity_state_ = rr_policy_->CheckConnectivityLocked(&rr_state_error); |
| 1638 | // Connectivity state is a function of the RR policy updated/created. |
| 1639 | UpdateConnectivityStateFromRoundRobinPolicyLocked(rr_state_error); |
| 1640 | // Add the gRPC LB's interested_parties pollset_set to that of the newly |
| 1641 | // created RR policy. This will make the RR policy progress upon activity on |
| 1642 | // gRPC LB, which in turn is tied to the application's call. |
| 1643 | grpc_pollset_set_add_pollset_set(rr_policy_->interested_parties(), |
| 1644 | interested_parties()); |
| 1645 | // Subscribe to changes to the connectivity of the new RR. |
| 1646 | // TODO(roth): We currently track this ref manually. Once the new |
| 1647 | // ClosureRef API is done, pass the RefCountedPtr<> along with the closure. |
| 1648 | self = Ref(DEBUG_LOCATION, "on_rr_connectivity_changed"); |
| 1649 | self.release(); |
| 1650 | rr_policy_->NotifyOnStateChangeLocked(&rr_connectivity_state_, |
| 1651 | &on_rr_connectivity_changed_); |
| 1652 | rr_policy_->ExitIdleLocked(); |
| 1653 | // Send pending picks to RR policy. |
| 1654 | PendingPick* pp; |
| 1655 | while ((pp = pending_picks_)) { |
| 1656 | pending_picks_ = pp->next; |
| 1657 | if (grpc_lb_glb_trace.enabled()) { |
| 1658 | gpr_log(GPR_INFO, |
| 1659 | "[grpclb %p] Pending pick about to (async) PICK from RR %p", this, |
| 1660 | rr_policy_.get()); |
| 1661 | } |
Mark D. Roth | f22dc13 | 2018-07-30 12:29:50 -0700 | [diff] [blame] | 1662 | grpc_error* error = GRPC_ERROR_NONE; |
| 1663 | PickFromRoundRobinPolicyLocked(true /* force_async */, pp, &error); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | grpc_channel_args* GrpcLb::CreateRoundRobinPolicyArgsLocked() { |
| 1668 | grpc_lb_addresses* addresses; |
Yihua Zhang | 392dad7 | 2018-05-03 20:12:20 -0700 | [diff] [blame] | 1669 | bool is_backend_from_grpclb_load_balancer = false; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1670 | if (serverlist_ != nullptr) { |
| 1671 | GPR_ASSERT(serverlist_->num_servers > 0); |
| 1672 | addresses = ProcessServerlist(serverlist_); |
Yihua Zhang | 392dad7 | 2018-05-03 20:12:20 -0700 | [diff] [blame] | 1673 | is_backend_from_grpclb_load_balancer = true; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1674 | } else { |
| 1675 | // If CreateOrUpdateRoundRobinPolicyLocked() is invoked when we haven't |
| 1676 | // received any serverlist from the balancer, we use the fallback backends |
| 1677 | // returned by the resolver. Note that the fallback backend list may be |
| 1678 | // empty, in which case the new round_robin policy will keep the requested |
| 1679 | // picks pending. |
| 1680 | GPR_ASSERT(fallback_backend_addresses_ != nullptr); |
| 1681 | addresses = grpc_lb_addresses_copy(fallback_backend_addresses_); |
| 1682 | } |
| 1683 | GPR_ASSERT(addresses != nullptr); |
| 1684 | // Replace the LB addresses in the channel args that we pass down to |
| 1685 | // the subchannel. |
| 1686 | static const char* keys_to_remove[] = {GRPC_ARG_LB_ADDRESSES}; |
Yihua Zhang | 392dad7 | 2018-05-03 20:12:20 -0700 | [diff] [blame] | 1687 | const grpc_arg args_to_add[] = { |
| 1688 | grpc_lb_addresses_create_channel_arg(addresses), |
| 1689 | // A channel arg indicating if the target is a backend inferred from a |
| 1690 | // grpclb load balancer. |
| 1691 | grpc_channel_arg_integer_create( |
| 1692 | const_cast<char*>( |
| 1693 | GRPC_ARG_ADDRESS_IS_BACKEND_FROM_GRPCLB_LOAD_BALANCER), |
| 1694 | is_backend_from_grpclb_load_balancer), |
| 1695 | }; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1696 | grpc_channel_args* args = grpc_channel_args_copy_and_add_and_remove( |
Yihua Zhang | 392dad7 | 2018-05-03 20:12:20 -0700 | [diff] [blame] | 1697 | args_, keys_to_remove, GPR_ARRAY_SIZE(keys_to_remove), args_to_add, |
| 1698 | GPR_ARRAY_SIZE(args_to_add)); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1699 | grpc_lb_addresses_destroy(addresses); |
| 1700 | return args; |
| 1701 | } |
| 1702 | |
| 1703 | void GrpcLb::CreateOrUpdateRoundRobinPolicyLocked() { |
| 1704 | if (shutting_down_) return; |
| 1705 | grpc_channel_args* args = CreateRoundRobinPolicyArgsLocked(); |
| 1706 | GPR_ASSERT(args != nullptr); |
| 1707 | if (rr_policy_ != nullptr) { |
| 1708 | if (grpc_lb_glb_trace.enabled()) { |
Mark D. Roth | 48854d2 | 2018-04-25 13:05:26 -0700 | [diff] [blame] | 1709 | gpr_log(GPR_INFO, "[grpclb %p] Updating RR policy %p", this, |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1710 | rr_policy_.get()); |
| 1711 | } |
| 1712 | rr_policy_->UpdateLocked(*args); |
| 1713 | } else { |
| 1714 | LoadBalancingPolicy::Args lb_policy_args; |
| 1715 | lb_policy_args.combiner = combiner(); |
| 1716 | lb_policy_args.client_channel_factory = client_channel_factory(); |
| 1717 | lb_policy_args.args = args; |
| 1718 | CreateRoundRobinPolicyLocked(lb_policy_args); |
| 1719 | if (grpc_lb_glb_trace.enabled()) { |
Mark D. Roth | 48854d2 | 2018-04-25 13:05:26 -0700 | [diff] [blame] | 1720 | gpr_log(GPR_INFO, "[grpclb %p] Created new RR policy %p", this, |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1721 | rr_policy_.get()); |
| 1722 | } |
| 1723 | } |
| 1724 | grpc_channel_args_destroy(args); |
| 1725 | } |
| 1726 | |
| 1727 | void GrpcLb::OnRoundRobinRequestReresolutionLocked(void* arg, |
| 1728 | grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 1729 | GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1730 | if (grpclb_policy->shutting_down_ || error != GRPC_ERROR_NONE) { |
| 1731 | grpclb_policy->Unref(DEBUG_LOCATION, "on_rr_reresolution_requested"); |
| 1732 | return; |
| 1733 | } |
| 1734 | if (grpc_lb_glb_trace.enabled()) { |
| 1735 | gpr_log( |
Mark D. Roth | 48854d2 | 2018-04-25 13:05:26 -0700 | [diff] [blame] | 1736 | GPR_INFO, |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1737 | "[grpclb %p] Re-resolution requested from the internal RR policy (%p).", |
| 1738 | grpclb_policy, grpclb_policy->rr_policy_.get()); |
| 1739 | } |
| 1740 | // If we are talking to a balancer, we expect to get updated addresses form |
| 1741 | // the balancer, so we can ignore the re-resolution request from the RR |
| 1742 | // policy. Otherwise, handle the re-resolution request using the |
| 1743 | // grpclb policy's original re-resolution closure. |
| 1744 | if (grpclb_policy->lb_calld_ == nullptr || |
| 1745 | !grpclb_policy->lb_calld_->seen_initial_response()) { |
| 1746 | grpclb_policy->TryReresolutionLocked(&grpc_lb_glb_trace, GRPC_ERROR_NONE); |
| 1747 | } |
| 1748 | // Give back the wrapper closure to the RR policy. |
| 1749 | grpclb_policy->rr_policy_->SetReresolutionClosureLocked( |
| 1750 | &grpclb_policy->on_rr_request_reresolution_); |
| 1751 | } |
| 1752 | |
| 1753 | void GrpcLb::UpdateConnectivityStateFromRoundRobinPolicyLocked( |
| 1754 | grpc_error* rr_state_error) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1755 | const grpc_connectivity_state curr_glb_state = |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1756 | grpc_connectivity_state_check(&state_tracker_); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1757 | /* The new connectivity status is a function of the previous one and the new |
| 1758 | * input coming from the status of the RR policy. |
| 1759 | * |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 1760 | * current state (grpclb's) |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1761 | * | |
| 1762 | * v || I | C | R | TF | SD | <- new state (RR's) |
| 1763 | * ===++====+=====+=====+======+======+ |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 1764 | * I || I | C | R | [I] | [I] | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1765 | * ---++----+-----+-----+------+------+ |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 1766 | * C || I | C | R | [C] | [C] | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1767 | * ---++----+-----+-----+------+------+ |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 1768 | * R || I | C | R | [R] | [R] | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1769 | * ---++----+-----+-----+------+------+ |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 1770 | * TF || I | C | R | [TF] | [TF] | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1771 | * ---++----+-----+-----+------+------+ |
| 1772 | * SD || NA | NA | NA | NA | NA | (*) |
| 1773 | * ---++----+-----+-----+------+------+ |
| 1774 | * |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 1775 | * A [STATE] indicates that the old RR policy is kept. In those cases, STATE |
| 1776 | * is the current state of grpclb, which is left untouched. |
| 1777 | * |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1778 | * In summary, if the new state is TRANSIENT_FAILURE or SHUTDOWN, stick to |
| 1779 | * the previous RR instance. |
| 1780 | * |
| 1781 | * Note that the status is never updated to SHUTDOWN as a result of calling |
| 1782 | * this function. Only glb_shutdown() has the power to set that state. |
| 1783 | * |
| 1784 | * (*) This function mustn't be called during shutting down. */ |
| 1785 | GPR_ASSERT(curr_glb_state != GRPC_CHANNEL_SHUTDOWN); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1786 | switch (rr_connectivity_state_) { |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1787 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
| 1788 | case GRPC_CHANNEL_SHUTDOWN: |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1789 | GPR_ASSERT(rr_state_error != GRPC_ERROR_NONE); |
| 1790 | break; |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1791 | case GRPC_CHANNEL_IDLE: |
| 1792 | case GRPC_CHANNEL_CONNECTING: |
| 1793 | case GRPC_CHANNEL_READY: |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1794 | GPR_ASSERT(rr_state_error == GRPC_ERROR_NONE); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1795 | } |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1796 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1797 | gpr_log( |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1798 | GPR_INFO, |
| 1799 | "[grpclb %p] Setting grpclb's state to %s from new RR policy %p state.", |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1800 | this, grpc_connectivity_state_name(rr_connectivity_state_), |
| 1801 | rr_policy_.get()); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1802 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1803 | grpc_connectivity_state_set(&state_tracker_, rr_connectivity_state_, |
| 1804 | rr_state_error, |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1805 | "update_lb_connectivity_status_locked"); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 1806 | } |
| 1807 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1808 | void GrpcLb::OnRoundRobinConnectivityChangedLocked(void* arg, |
| 1809 | grpc_error* error) { |
Vijay Pai | 7fed69b | 2018-03-05 09:58:05 -0800 | [diff] [blame] | 1810 | GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1811 | if (grpclb_policy->shutting_down_) { |
| 1812 | grpclb_policy->Unref(DEBUG_LOCATION, "on_rr_connectivity_changed"); |
Juanli Shen | 87c6504 | 2018-02-15 09:42:45 -0800 | [diff] [blame] | 1813 | return; |
| 1814 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1815 | grpclb_policy->UpdateConnectivityStateFromRoundRobinPolicyLocked( |
| 1816 | GRPC_ERROR_REF(error)); |
| 1817 | // Resubscribe. Reuse the "on_rr_connectivity_changed" ref. |
| 1818 | grpclb_policy->rr_policy_->NotifyOnStateChangeLocked( |
| 1819 | &grpclb_policy->rr_connectivity_state_, |
| 1820 | &grpclb_policy->on_rr_connectivity_changed_); |
Juanli Shen | 87c6504 | 2018-02-15 09:42:45 -0800 | [diff] [blame] | 1821 | } |
| 1822 | |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1823 | // |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1824 | // factory |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1825 | // |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1826 | |
| 1827 | class GrpcLbFactory : public LoadBalancingPolicyFactory { |
| 1828 | public: |
| 1829 | OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
| 1830 | const LoadBalancingPolicy::Args& args) const override { |
| 1831 | /* Count the number of gRPC-LB addresses. There must be at least one. */ |
Noah Eisen | 7ea8a60 | 2018-06-14 11:43:18 -0400 | [diff] [blame] | 1832 | const grpc_arg* arg = |
| 1833 | grpc_channel_args_find(args.args, GRPC_ARG_LB_ADDRESSES); |
| 1834 | if (arg == nullptr || arg->type != GRPC_ARG_POINTER) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1835 | return nullptr; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1836 | } |
Noah Eisen | 7ea8a60 | 2018-06-14 11:43:18 -0400 | [diff] [blame] | 1837 | grpc_lb_addresses* addresses = |
| 1838 | static_cast<grpc_lb_addresses*>(arg->value.pointer.p); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1839 | size_t num_grpclb_addrs = 0; |
| 1840 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 1841 | if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1842 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1843 | if (num_grpclb_addrs == 0) return nullptr; |
| 1844 | return OrphanablePtr<LoadBalancingPolicy>(New<GrpcLb>(addresses, args)); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1845 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1846 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1847 | const char* name() const override { return "grpclb"; } |
| 1848 | }; |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1849 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1850 | } // namespace |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1851 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1852 | } // namespace grpc_core |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1853 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1854 | // |
| 1855 | // Plugin registration |
| 1856 | // |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1857 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1858 | namespace { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1859 | |
| 1860 | // Only add client_load_reporting filter if the grpclb LB policy is used. |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1861 | bool maybe_add_client_load_reporting_filter(grpc_channel_stack_builder* builder, |
| 1862 | void* arg) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1863 | const grpc_channel_args* args = |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1864 | grpc_channel_stack_builder_get_channel_arguments(builder); |
Noah Eisen | 7ea8a60 | 2018-06-14 11:43:18 -0400 | [diff] [blame] | 1865 | const grpc_arg* channel_arg = |
| 1866 | grpc_channel_args_find(args, GRPC_ARG_LB_POLICY_NAME); |
| 1867 | if (channel_arg != nullptr && channel_arg->type == GRPC_ARG_STRING && |
| 1868 | strcmp(channel_arg->value.string, "grpclb") == 0) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1869 | return grpc_channel_stack_builder_append_filter( |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1870 | builder, (const grpc_channel_filter*)arg, nullptr, nullptr); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1871 | } |
| 1872 | return true; |
| 1873 | } |
| 1874 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1875 | } // namespace |
| 1876 | |
ncteisen | adbfbd5 | 2017-11-16 15:35:45 -0800 | [diff] [blame] | 1877 | void grpc_lb_policy_grpclb_init() { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 1878 | grpc_core::LoadBalancingPolicyRegistry::Builder:: |
| 1879 | RegisterLoadBalancingPolicyFactory( |
| 1880 | grpc_core::UniquePtr<grpc_core::LoadBalancingPolicyFactory>( |
| 1881 | grpc_core::New<grpc_core::GrpcLbFactory>())); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1882 | grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL, |
| 1883 | GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, |
| 1884 | maybe_add_client_load_reporting_filter, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1885 | (void*)&grpc_client_load_reporting_filter); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
ncteisen | adbfbd5 | 2017-11-16 15:35:45 -0800 | [diff] [blame] | 1888 | void grpc_lb_policy_grpclb_shutdown() {} |