blob: 49918e11b70187774d1d6c77166c99001b2e1f9a [file] [log] [blame]
David Garcia Quintas3fb8f732016-06-15 22:53:08 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
David Garcia Quintas3fb8f732016-06-15 22:53:08 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Quintas3fb8f732016-06-15 22:53:08 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Quintas3fb8f732016-06-15 22:53:08 -070016 *
17 */
18
Mark D. Rothc8875492018-02-20 08:33:48 -080019/// 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 Quintas8b3b97f2016-07-15 07:46:47 -070056
Mark D. Rothc8875492018-02-20 08:33:48 -080057// 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 Polcyndb3e8982018-02-21 16:59:24 -080061#include <grpc/support/port_platform.h>
62
murgatroid997871f732016-09-23 13:49:05 -070063#include "src/core/lib/iomgr/sockaddr.h"
64
Yash Tibrewalfcd26bc2017-09-25 15:08:28 -070065#include <inttypes.h>
Mark D. Roth64d922a2017-05-03 12:52:04 -070066#include <limits.h>
David Garcia Quintas22e8f1d2016-06-15 23:53:00 -070067#include <string.h>
68
69#include <grpc/byte_buffer_reader.h>
70#include <grpc/grpc.h>
71#include <grpc/support/alloc.h>
David Garcia Quintas22e8f1d2016-06-15 23:53:00 -070072#include <grpc/support/string_util.h>
David Garcia Quintas69099222016-10-03 11:28:37 -070073#include <grpc/support/time.h>
David Garcia Quintas22e8f1d2016-06-15 23:53:00 -070074
Craig Tiller9eb0fde2017-03-31 16:59:30 -070075#include "src/core/ext/filters/client_channel/client_channel.h"
76#include "src/core/ext/filters/client_channel/client_channel_factory.h"
Mark D. Roth09e458c2017-05-02 08:13:26 -070077#include "src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.h"
Craig Tiller9eb0fde2017-03-31 16:59:30 -070078#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h"
Mark D. Roth09e458c2017-05-02 08:13:26 -070079#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h"
Craig Tiller9eb0fde2017-03-31 16:59:30 -070080#include "src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h"
Craig Tillerd52e22f2017-04-02 16:22:52 -070081#include "src/core/ext/filters/client_channel/lb_policy_factory.h"
82#include "src/core/ext/filters/client_channel/lb_policy_registry.h"
83#include "src/core/ext/filters/client_channel/parse_address.h"
David Garcia Quintas87d5a312017-06-06 19:45:58 -070084#include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
Juanli Shen6502ecc2017-09-13 13:10:54 -070085#include "src/core/ext/filters/client_channel/subchannel_index.h"
Craig Tillerc0df1c02017-07-17 16:12:33 -070086#include "src/core/lib/backoff/backoff.h"
Mark D. Roth046cf762016-09-26 11:13:51 -070087#include "src/core/lib/channel/channel_args.h"
Mark D. Roth09e458c2017-05-02 08:13:26 -070088#include "src/core/lib/channel/channel_stack.h"
Vijay Paiae376bf2018-01-25 22:54:02 -080089#include "src/core/lib/gpr/host_port.h"
Mark D. Rothdbdf4952018-01-18 11:21:12 -080090#include "src/core/lib/gpr/string.h"
Mark D. Roth4f2b0fd2018-01-19 12:12:23 -080091#include "src/core/lib/gprpp/manual_constructor.h"
Mark D. Rothc8875492018-02-20 08:33:48 -080092#include "src/core/lib/gprpp/memory.h"
93#include "src/core/lib/gprpp/orphanable.h"
Mark D. Roth209f6442018-02-08 10:26:46 -080094#include "src/core/lib/gprpp/ref_counted_ptr.h"
Craig Tiller2400bf52017-02-09 16:25:19 -080095#include "src/core/lib/iomgr/combiner.h"
David Garcia Quintas98da61b2016-10-29 08:46:31 +020096#include "src/core/lib/iomgr/sockaddr.h"
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070097#include "src/core/lib/iomgr/sockaddr_utils.h"
David Garcia Quintas98da61b2016-10-29 08:46:31 +020098#include "src/core/lib/iomgr/timer.h"
David Garcia Quintas01291502017-02-07 13:26:41 -080099#include "src/core/lib/slice/slice_hash_table.h"
Craig Tiller18b4ba32016-11-09 15:23:42 -0800100#include "src/core/lib/slice/slice_internal.h"
Craig Tiller0f310802016-10-26 16:25:56 -0700101#include "src/core/lib/slice/slice_string_helpers.h"
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700102#include "src/core/lib/surface/call.h"
103#include "src/core/lib/surface/channel.h"
Mark D. Roth09e458c2017-05-02 08:13:26 -0700104#include "src/core/lib/surface/channel_init.h"
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700105#include "src/core/lib/transport/static_metadata.h"
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700106
David Garcia Quintas1edfb952016-11-22 17:15:34 -0800107#define GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS 1
108#define GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER 1.6
109#define GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS 120
110#define GRPC_GRPCLB_RECONNECT_JITTER 0.2
Juanli Shenfe408152017-09-27 12:27:20 -0700111#define GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS 10000
David Garcia Quintas98da61b2016-10-29 08:46:31 +0200112
Mark D. Rothc8875492018-02-20 08:33:48 -0800113namespace grpc_core {
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700114
Mark D. Rothc8875492018-02-20 08:33:48 -0800115TraceFlag grpc_lb_glb_trace(false, "glb");
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700116
Vijay Pai849bd732018-01-02 23:30:47 +0000117namespace {
Mark D. Rothc0febd32018-01-09 10:25:24 -0800118
Mark D. Rothc8875492018-02-20 08:33:48 -0800119class GrpcLb : public LoadBalancingPolicy {
120 public:
121 GrpcLb(const grpc_lb_addresses* addresses, const Args& args);
122
123 void UpdateLocked(const grpc_channel_args& args) override;
124 bool PickLocked(PickState* pick) override;
125 void CancelPickLocked(PickState* pick, grpc_error* error) override;
126 void CancelMatchingPicksLocked(uint32_t initial_metadata_flags_mask,
127 uint32_t initial_metadata_flags_eq,
128 grpc_error* error) override;
129 void NotifyOnStateChangeLocked(grpc_connectivity_state* state,
130 grpc_closure* closure) override;
131 grpc_connectivity_state CheckConnectivityLocked(
132 grpc_error** connectivity_error) override;
133 void HandOffPendingPicksLocked(LoadBalancingPolicy* new_policy) override;
134 void PingOneLocked(grpc_closure* on_initiate, grpc_closure* on_ack) override;
135 void ExitIdleLocked() override;
136
137 private:
138 /// Linked list of pending pick requests. It stores all information needed to
139 /// eventually call (Round Robin's) pick() on them. They mainly stay pending
140 /// waiting for the RR policy to be created.
141 ///
142 /// Note that when a pick is sent to the RR policy, we inject our own
143 /// on_complete callback, so that we can intercept the result before
144 /// invoking the original on_complete callback. This allows us to set the
145 /// LB token metadata and add client_stats to the call context.
146 /// See \a pending_pick_complete() for details.
147 struct PendingPick {
148 // The grpclb instance that created the wrapping. This instance is not
149 // owned; reference counts are untouched. It's used only for logging
150 // purposes.
151 GrpcLb* grpclb_policy;
152 // The original pick.
153 PickState* pick;
154 // Our on_complete closure and the original one.
155 grpc_closure on_complete;
156 grpc_closure* original_on_complete;
157 // The LB token associated with the pick. This is set via user_data in
158 // the pick.
159 grpc_mdelem lb_token;
160 // Stats for client-side load reporting. Note that this holds a
161 // reference, which must be either passed on via context or unreffed.
162 grpc_grpclb_client_stats* client_stats = nullptr;
163 // Next pending pick.
164 PendingPick* next = nullptr;
165 };
166
167 /// A linked list of pending pings waiting for the RR policy to be created.
168 struct PendingPing {
169 grpc_closure* on_initiate;
170 grpc_closure* on_ack;
171 PendingPing* next = nullptr;
172 };
173
174 /// Contains a call to the LB server and all the data related to the call.
175 class BalancerCallState
176 : public InternallyRefCountedWithTracing<BalancerCallState> {
177 public:
178 explicit BalancerCallState(
179 RefCountedPtr<LoadBalancingPolicy> parent_grpclb_policy);
180
181 // It's the caller's responsibility to ensure that Orphan() is called from
182 // inside the combiner.
183 void Orphan() override;
184
185 void StartQuery();
186
187 grpc_grpclb_client_stats* client_stats() const { return client_stats_; }
188 bool seen_initial_response() const { return seen_initial_response_; }
189
190 private:
191 ~BalancerCallState();
192
193 GrpcLb* grpclb_policy() const {
Vijay Pai7fed69b2018-03-05 09:58:05 -0800194 return static_cast<GrpcLb*>(grpclb_policy_.get());
Mark D. Rothc8875492018-02-20 08:33:48 -0800195 }
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.
234 grpc_grpclb_client_stats* client_stats_ = nullptr;
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
267 // Pending ping methods.
268 void AddPendingPing(grpc_closure* on_initiate, grpc_closure* on_ack);
269
270 // Methods for dealing with the RR policy.
271 void CreateOrUpdateRoundRobinPolicyLocked();
272 grpc_channel_args* CreateRoundRobinPolicyArgsLocked();
273 void CreateRoundRobinPolicyLocked(const Args& args);
274 bool PickFromRoundRobinPolicyLocked(bool force_async, PendingPick* pp);
275 void UpdateConnectivityStateFromRoundRobinPolicyLocked(
276 grpc_error* rr_state_error);
277 static void OnRoundRobinConnectivityChangedLocked(void* arg,
278 grpc_error* error);
279 static void OnRoundRobinRequestReresolutionLocked(void* arg,
280 grpc_error* error);
281
282 // Who the client is trying to communicate with.
283 const char* server_name_ = nullptr;
284
285 // Current channel args from the resolver.
286 grpc_channel_args* args_ = nullptr;
287
288 // Internal state.
289 bool started_picking_ = false;
290 bool shutting_down_ = false;
291 grpc_connectivity_state_tracker state_tracker_;
292
293 // The channel for communicating with the LB server.
294 grpc_channel* lb_channel_ = nullptr;
295 grpc_connectivity_state lb_channel_connectivity_;
296 grpc_closure lb_channel_on_connectivity_changed_;
297 // Are we already watching the LB channel's connectivity?
298 bool watching_lb_channel_ = false;
299 // Response generator to inject address updates into lb_channel_.
300 RefCountedPtr<FakeResolverResponseGenerator> response_generator_;
301
302 // The data associated with the current LB call. It holds a ref to this LB
303 // policy. It's initialized every time we query for backends. It's reset to
304 // NULL whenever the current LB call is no longer needed (e.g., the LB policy
305 // is shutting down, or the LB call has ended). A non-NULL lb_calld_ always
306 // contains a non-NULL lb_call_.
307 OrphanablePtr<BalancerCallState> lb_calld_;
308 // Timeout in milliseconds for the LB call. 0 means no deadline.
309 int lb_call_timeout_ms_ = 0;
310 // Balancer call retry state.
311 BackOff lb_call_backoff_;
312 bool retry_timer_callback_pending_ = false;
313 grpc_timer lb_call_retry_timer_;
314 grpc_closure lb_on_call_retry_;
315
316 // The deserialized response from the balancer. May be nullptr until one
317 // such response has arrived.
318 grpc_grpclb_serverlist* serverlist_ = nullptr;
319 // Index into serverlist for next pick.
320 // If the server at this index is a drop, we return a drop.
321 // Otherwise, we delegate to the RR policy.
322 size_t serverlist_index_ = 0;
323
324 // Timeout in milliseconds for before using fallback backend addresses.
325 // 0 means not using fallback.
326 int lb_fallback_timeout_ms_ = 0;
327 // The backend addresses from the resolver.
328 grpc_lb_addresses* fallback_backend_addresses_ = nullptr;
329 // Fallback timer.
330 bool fallback_timer_callback_pending_ = false;
331 grpc_timer lb_fallback_timer_;
332 grpc_closure lb_on_fallback_;
333
334 // Pending picks and pings that are waiting on the RR policy's connectivity.
335 PendingPick* pending_picks_ = nullptr;
336 PendingPing* pending_pings_ = nullptr;
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 Pai849bd732018-01-02 23:30:47 +0000343};
Mark D. Rothc0febd32018-01-09 10:25:24 -0800344
Mark D. Rothc8875492018-02-20 08:33:48 -0800345//
346// serverlist parsing code
347//
Mark D. Rothc0febd32018-01-09 10:25:24 -0800348
Mark D. Rothc8875492018-02-20 08:33:48 -0800349// vtable for LB tokens in grpc_lb_addresses
350void* lb_token_copy(void* token) {
351 return token == nullptr
352 ? nullptr
353 : (void*)GRPC_MDELEM_REF(grpc_mdelem{(uintptr_t)token}).payload;
354}
355void lb_token_destroy(void* token) {
356 if (token != nullptr) {
357 GRPC_MDELEM_UNREF(grpc_mdelem{(uintptr_t)token});
Juanli Shen8e4c9d32018-01-23 16:26:39 -0800358 }
359}
Mark D. Rothc8875492018-02-20 08:33:48 -0800360int lb_token_cmp(void* token1, void* token2) {
361 if (token1 > token2) return 1;
362 if (token1 < token2) return -1;
363 return 0;
Juanli Shen8e4c9d32018-01-23 16:26:39 -0800364}
Mark D. Rothc8875492018-02-20 08:33:48 -0800365const grpc_lb_user_data_vtable lb_token_vtable = {
366 lb_token_copy, lb_token_destroy, lb_token_cmp};
Juanli Shen8e4c9d32018-01-23 16:26:39 -0800367
Mark D. Rothc8875492018-02-20 08:33:48 -0800368// Returns the backend addresses extracted from the given addresses.
369grpc_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. Roth83d5cd62018-01-11 08:56:53 -0800375 }
Mark D. Rothc0febd32018-01-09 10:25:24 -0800376 }
Mark D. Rothc8875492018-02-20 08:33:48 -0800377 // 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. Rothc0febd32018-01-09 10:25:24 -0800391}
392
Mark D. Rothc8875492018-02-20 08:33:48 -0800393bool IsServerValid(const grpc_grpclb_server* server, size_t idx, bool log) {
Mark D. Rothe7751802017-07-27 12:31:45 -0700394 if (server->drop) return false;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700395 const grpc_grpclb_ip_address* ip = &server->ip_address;
David Garcia Quintas35c2aba2016-09-13 15:28:09 -0700396 if (server->port >> 16 != 0) {
397 if (log) {
398 gpr_log(GPR_ERROR,
Jan Tattermusch2b398082016-10-07 14:40:30 +0200399 "Invalid port '%d' at index %lu of serverlist. Ignoring.",
Mark D. Rothc8875492018-02-20 08:33:48 -0800400 server->port, (unsigned long)idx);
David Garcia Quintas35c2aba2016-09-13 15:28:09 -0700401 }
402 return false;
403 }
David Garcia Quintas35c2aba2016-09-13 15:28:09 -0700404 if (ip->size != 4 && ip->size != 16) {
405 if (log) {
406 gpr_log(GPR_ERROR,
Jan Tattermusch2b398082016-10-07 14:40:30 +0200407 "Expected IP to be 4 or 16 bytes, got %d at index %lu of "
David Garcia Quintas35c2aba2016-09-13 15:28:09 -0700408 "serverlist. Ignoring",
Mark D. Rothc8875492018-02-20 08:33:48 -0800409 ip->size, (unsigned long)idx);
David Garcia Quintas35c2aba2016-09-13 15:28:09 -0700410 }
411 return false;
412 }
413 return true;
414}
415
Mark D. Rothc8875492018-02-20 08:33:48 -0800416void ParseServer(const grpc_grpclb_server* server,
417 grpc_resolved_address* addr) {
Mark D. Rothd7389b42017-05-17 12:22:17 -0700418 memset(addr, 0, sizeof(*addr));
Mark D. Rothe7751802017-07-27 12:31:45 -0700419 if (server->drop) return;
Mark D. Rothc8875492018-02-20 08:33:48 -0800420 const uint16_t netorder_port = htons((uint16_t)server->port);
David Garcia Quintas7ec29132016-11-01 04:09:05 +0100421 /* the addresses are given in binary format (a in(6)_addr struct) in
422 * server->ip_address.bytes. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700423 const grpc_grpclb_ip_address* ip = &server->ip_address;
David Garcia Quintas7ec29132016-11-01 04:09:05 +0100424 if (ip->size == 4) {
425 addr->len = sizeof(struct sockaddr_in);
Mark D. Rothc8875492018-02-20 08:33:48 -0800426 struct sockaddr_in* addr4 = (struct sockaddr_in*)&addr->addr;
David Garcia Quintas7ec29132016-11-01 04:09:05 +0100427 addr4->sin_family = AF_INET;
428 memcpy(&addr4->sin_addr, ip->bytes, ip->size);
429 addr4->sin_port = netorder_port;
430 } else if (ip->size == 16) {
431 addr->len = sizeof(struct sockaddr_in6);
Mark D. Rothc8875492018-02-20 08:33:48 -0800432 struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&addr->addr;
David Garcia Quintas107ca162016-11-02 18:17:03 -0700433 addr6->sin6_family = AF_INET6;
David Garcia Quintas7ec29132016-11-01 04:09:05 +0100434 memcpy(&addr6->sin6_addr, ip->bytes, ip->size);
435 addr6->sin6_port = netorder_port;
436 }
437}
438
Mark D. Rothc8875492018-02-20 08:33:48 -0800439// Returns addresses extracted from \a serverlist.
440grpc_lb_addresses* ProcessServerlist(const grpc_grpclb_serverlist* serverlist) {
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700441 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. Rothc8875492018-02-20 08:33:48 -0800445 if (IsServerValid(serverlist->servers[i], i, true)) ++num_valid;
David Garcia Quintasb8b384a2016-08-23 21:10:29 -0700446 }
Craig Tillerbaa14a92017-11-03 09:09:36 -0700447 grpc_lb_addresses* lb_addresses =
Mark D. Roth16883a32016-10-21 10:30:58 -0700448 grpc_lb_addresses_create(num_valid, &lb_token_vtable);
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700449 /* second pass: actually populate the addresses and LB tokens (aka user data
David Garcia Quintas35c2aba2016-09-13 15:28:09 -0700450 * 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 Quintasf47d6fb2016-09-14 12:59:17 -0700454 size_t addr_idx = 0;
455 for (size_t sl_idx = 0; sl_idx < serverlist->num_servers; ++sl_idx) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700456 const grpc_grpclb_server* server = serverlist->servers[sl_idx];
Mark D. Rothc8875492018-02-20 08:33:48 -0800457 if (!IsServerValid(serverlist->servers[sl_idx], sl_idx, false)) continue;
David Garcia Quintasc22c65b2017-07-25 14:22:20 -0700458 GPR_ASSERT(addr_idx < num_valid);
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700459 /* address processing */
Mark D. Rothc5c38782016-09-16 08:51:01 -0700460 grpc_resolved_address addr;
Mark D. Rothc8875492018-02-20 08:33:48 -0800461 ParseServer(server, &addr);
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700462 /* lb token processing */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700463 void* user_data;
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700464 if (server->has_load_balance_token) {
David Garcia Quintas0baf1dc2016-10-28 04:44:01 +0200465 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 Tiller7c70b6c2017-01-23 07:48:42 -0800469 grpc_slice lb_token_mdstr = grpc_slice_from_copied_buffer(
470 server->load_balance_token, lb_token_length);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800471 user_data =
472 (void*)grpc_mdelem_from_slices(GRPC_MDSTR_LB_TOKEN, lb_token_mdstr)
473 .payload;
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700474 } else {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700475 char* uri = grpc_sockaddr_to_uri(&addr);
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800476 gpr_log(GPR_INFO,
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700477 "Missing LB token for backend address '%s'. The empty token will "
478 "be used instead",
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800479 uri);
480 gpr_free(uri);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700481 user_data = (void*)GRPC_MDELEM_LB_TOKEN_EMPTY.payload;
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700482 }
Mark D. Roth64f1f8d2016-09-16 09:00:09 -0700483 grpc_lb_addresses_set_address(lb_addresses, addr_idx, &addr.addr, addr.len,
484 false /* is_balancer */,
Noah Eisen882dfed2017-11-14 14:58:20 -0800485 nullptr /* balancer_name */, user_data);
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700486 ++addr_idx;
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700487 }
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700488 GPR_ASSERT(addr_idx == num_valid);
Mark D. Rothc5c38782016-09-16 08:51:01 -0700489 return lb_addresses;
490}
491
Mark D. Rothc8875492018-02-20 08:33:48 -0800492//
493// GrpcLb::BalancerCallState
494//
495
496GrpcLb::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 Quintasc7c0d692018-03-10 17:27:15 -0800507 const grpc_millis deadline =
Mark D. Rothc8875492018-02-20 08:33:48 -0800508 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 Quintasc7c0d692018-03-10 17:27:15 -0800515 nullptr, deadline, nullptr);
Mark D. Rothc8875492018-02-20 08:33:48 -0800516 // 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 Shenfe408152017-09-27 12:27:20 -0700535}
536
Mark D. Rothc8875492018-02-20 08:33:48 -0800537GrpcLb::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_);
545 if (client_stats_ != nullptr) {
546 grpc_grpclb_client_stats_unref(client_stats_);
547 }
548}
549
550void GrpcLb::BalancerCallState::Orphan() {
551 GPR_ASSERT(lb_call_ != nullptr);
552 // If we are here because grpclb_policy wants to cancel the call,
553 // lb_on_balancer_status_received_ will complete the cancellation and clean
554 // up. Otherwise, we are here because grpclb_policy has to orphan a failed
555 // call, then the following cancellation will be a no-op.
556 grpc_call_cancel(lb_call_, nullptr);
557 if (client_load_report_timer_callback_pending_) {
558 grpc_timer_cancel(&client_load_report_timer_);
559 }
560 // Note that the initial ref is hold by lb_on_balancer_status_received_
561 // instead of the caller of this function. So the corresponding unref happens
562 // in lb_on_balancer_status_received_ instead of here.
563}
564
565void GrpcLb::BalancerCallState::StartQuery() {
566 GPR_ASSERT(lb_call_ != nullptr);
567 if (grpc_lb_glb_trace.enabled()) {
568 gpr_log(GPR_INFO,
569 "[grpclb %p] Starting LB call (lb_calld: %p, lb_call: %p)",
570 grpclb_policy_.get(), this, lb_call_);
571 }
572 // Create the ops.
573 grpc_call_error call_error;
574 grpc_op ops[3];
575 memset(ops, 0, sizeof(ops));
576 // Op: send initial metadata.
577 grpc_op* op = ops;
578 op->op = GRPC_OP_SEND_INITIAL_METADATA;
579 op->data.send_initial_metadata.count = 0;
580 op->flags = 0;
581 op->reserved = nullptr;
582 op++;
583 // Op: send request message.
584 GPR_ASSERT(send_message_payload_ != nullptr);
585 op->op = GRPC_OP_SEND_MESSAGE;
586 op->data.send_message.send_message = send_message_payload_;
587 op->flags = 0;
588 op->reserved = nullptr;
589 op++;
590 // TODO(roth): We currently track this ref manually. Once the
591 // ClosureRef API is ready, we should pass the RefCountedPtr<> along
592 // with the callback.
593 auto self = Ref(DEBUG_LOCATION, "on_initial_request_sent");
594 self.release();
595 call_error = grpc_call_start_batch_and_execute(
596 lb_call_, ops, (size_t)(op - ops), &lb_on_initial_request_sent_);
597 GPR_ASSERT(GRPC_CALL_OK == call_error);
598 // Op: recv initial metadata.
599 op = ops;
600 op->op = GRPC_OP_RECV_INITIAL_METADATA;
601 op->data.recv_initial_metadata.recv_initial_metadata =
602 &lb_initial_metadata_recv_;
603 op->flags = 0;
604 op->reserved = nullptr;
605 op++;
606 // Op: recv response.
607 op->op = GRPC_OP_RECV_MESSAGE;
608 op->data.recv_message.recv_message = &recv_message_payload_;
609 op->flags = 0;
610 op->reserved = nullptr;
611 op++;
612 // TODO(roth): We currently track this ref manually. Once the
613 // ClosureRef API is ready, we should pass the RefCountedPtr<> along
614 // with the callback.
615 self = Ref(DEBUG_LOCATION, "on_message_received");
616 self.release();
617 call_error = grpc_call_start_batch_and_execute(
618 lb_call_, ops, (size_t)(op - ops), &lb_on_balancer_message_received_);
619 GPR_ASSERT(GRPC_CALL_OK == call_error);
620 // Op: recv server status.
621 op = ops;
622 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
623 op->data.recv_status_on_client.trailing_metadata =
624 &lb_trailing_metadata_recv_;
625 op->data.recv_status_on_client.status = &lb_call_status_;
626 op->data.recv_status_on_client.status_details = &lb_call_status_details_;
627 op->flags = 0;
628 op->reserved = nullptr;
629 op++;
630 // This callback signals the end of the LB call, so it relies on the initial
631 // ref instead of a new ref. When it's invoked, it's the initial ref that is
632 // unreffed.
633 call_error = grpc_call_start_batch_and_execute(
634 lb_call_, ops, (size_t)(op - ops), &lb_on_balancer_status_received_);
635 GPR_ASSERT(GRPC_CALL_OK == call_error);
636};
637
638void GrpcLb::BalancerCallState::ScheduleNextClientLoadReportLocked() {
639 const grpc_millis next_client_load_report_time =
640 ExecCtx::Get()->Now() + client_stats_report_interval_;
641 GRPC_CLOSURE_INIT(&client_load_report_closure_,
642 MaybeSendClientLoadReportLocked, this,
643 grpc_combiner_scheduler(grpclb_policy()->combiner()));
644 grpc_timer_init(&client_load_report_timer_, next_client_load_report_time,
645 &client_load_report_closure_);
646 client_load_report_timer_callback_pending_ = true;
647}
648
649void GrpcLb::BalancerCallState::MaybeSendClientLoadReportLocked(
650 void* arg, grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -0800651 BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -0800652 GrpcLb* grpclb_policy = lb_calld->grpclb_policy();
653 lb_calld->client_load_report_timer_callback_pending_ = false;
654 if (error != GRPC_ERROR_NONE || lb_calld != grpclb_policy->lb_calld_.get()) {
655 lb_calld->Unref(DEBUG_LOCATION, "client_load_report");
656 return;
657 }
658 // If we've already sent the initial request, then we can go ahead and send
659 // the load report. Otherwise, we need to wait until the initial request has
660 // been sent to send this (see OnInitialRequestSentLocked()).
661 if (lb_calld->send_message_payload_ == nullptr) {
662 lb_calld->SendClientLoadReportLocked();
663 } else {
664 lb_calld->client_load_report_is_due_ = true;
665 }
666}
667
668bool GrpcLb::BalancerCallState::LoadReportCountersAreZero(
669 grpc_grpclb_request* request) {
670 grpc_grpclb_dropped_call_counts* drop_entries =
671 static_cast<grpc_grpclb_dropped_call_counts*>(
672 request->client_stats.calls_finished_with_drop.arg);
673 return request->client_stats.num_calls_started == 0 &&
674 request->client_stats.num_calls_finished == 0 &&
675 request->client_stats.num_calls_finished_with_client_failed_to_send ==
676 0 &&
677 request->client_stats.num_calls_finished_known_received == 0 &&
678 (drop_entries == nullptr || drop_entries->num_entries == 0);
679}
680
681void GrpcLb::BalancerCallState::SendClientLoadReportLocked() {
682 // Construct message payload.
683 GPR_ASSERT(send_message_payload_ == nullptr);
684 grpc_grpclb_request* request =
685 grpc_grpclb_load_report_request_create_locked(client_stats_);
686 // Skip client load report if the counters were all zero in the last
687 // report and they are still zero in this one.
688 if (LoadReportCountersAreZero(request)) {
689 if (last_client_load_report_counters_were_zero_) {
690 grpc_grpclb_request_destroy(request);
691 ScheduleNextClientLoadReportLocked();
692 return;
693 }
694 last_client_load_report_counters_were_zero_ = true;
695 } else {
696 last_client_load_report_counters_were_zero_ = false;
697 }
698 grpc_slice request_payload_slice = grpc_grpclb_request_encode(request);
699 send_message_payload_ =
700 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
701 grpc_slice_unref_internal(request_payload_slice);
702 grpc_grpclb_request_destroy(request);
703 // Send the report.
704 grpc_op op;
705 memset(&op, 0, sizeof(op));
706 op.op = GRPC_OP_SEND_MESSAGE;
707 op.data.send_message.send_message = send_message_payload_;
708 GRPC_CLOSURE_INIT(&client_load_report_closure_, ClientLoadReportDoneLocked,
709 this, grpc_combiner_scheduler(grpclb_policy()->combiner()));
710 grpc_call_error call_error = grpc_call_start_batch_and_execute(
711 lb_call_, &op, 1, &client_load_report_closure_);
712 if (call_error != GRPC_CALL_OK) {
713 gpr_log(GPR_ERROR, "[grpclb %p] call_error=%d", grpclb_policy_.get(),
714 call_error);
715 GPR_ASSERT(GRPC_CALL_OK == call_error);
716 }
717}
718
719void GrpcLb::BalancerCallState::ClientLoadReportDoneLocked(void* arg,
720 grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -0800721 BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -0800722 GrpcLb* grpclb_policy = lb_calld->grpclb_policy();
723 grpc_byte_buffer_destroy(lb_calld->send_message_payload_);
724 lb_calld->send_message_payload_ = nullptr;
725 if (error != GRPC_ERROR_NONE || lb_calld != grpclb_policy->lb_calld_.get()) {
726 lb_calld->Unref(DEBUG_LOCATION, "client_load_report");
727 return;
728 }
729 lb_calld->ScheduleNextClientLoadReportLocked();
730}
731
732void GrpcLb::BalancerCallState::OnInitialRequestSentLocked(void* arg,
733 grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -0800734 BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -0800735 grpc_byte_buffer_destroy(lb_calld->send_message_payload_);
736 lb_calld->send_message_payload_ = nullptr;
737 // If we attempted to send a client load report before the initial request was
738 // sent (and this lb_calld is still in use), send the load report now.
739 if (lb_calld->client_load_report_is_due_ &&
740 lb_calld == lb_calld->grpclb_policy()->lb_calld_.get()) {
741 lb_calld->SendClientLoadReportLocked();
742 lb_calld->client_load_report_is_due_ = false;
743 }
744 lb_calld->Unref(DEBUG_LOCATION, "on_initial_request_sent");
745}
746
747void GrpcLb::BalancerCallState::OnBalancerMessageReceivedLocked(
748 void* arg, grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -0800749 BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -0800750 GrpcLb* grpclb_policy = lb_calld->grpclb_policy();
751 // Empty payload means the LB call was cancelled.
752 if (lb_calld != grpclb_policy->lb_calld_.get() ||
753 lb_calld->recv_message_payload_ == nullptr) {
754 lb_calld->Unref(DEBUG_LOCATION, "on_message_received");
755 return;
756 }
757 grpc_byte_buffer_reader bbr;
758 grpc_byte_buffer_reader_init(&bbr, lb_calld->recv_message_payload_);
759 grpc_slice response_slice = grpc_byte_buffer_reader_readall(&bbr);
760 grpc_byte_buffer_reader_destroy(&bbr);
761 grpc_byte_buffer_destroy(lb_calld->recv_message_payload_);
762 lb_calld->recv_message_payload_ = nullptr;
763 grpc_grpclb_initial_response* initial_response;
764 grpc_grpclb_serverlist* serverlist;
765 if (!lb_calld->seen_initial_response_ &&
766 (initial_response = grpc_grpclb_initial_response_parse(response_slice)) !=
767 nullptr) {
768 // Have NOT seen initial response, look for initial response.
769 if (initial_response->has_client_stats_report_interval) {
770 lb_calld->client_stats_report_interval_ = GPR_MAX(
771 GPR_MS_PER_SEC, grpc_grpclb_duration_to_millis(
772 &initial_response->client_stats_report_interval));
773 if (grpc_lb_glb_trace.enabled()) {
774 gpr_log(GPR_INFO,
775 "[grpclb %p] Received initial LB response message; "
776 "client load reporting interval = %" PRIdPTR " milliseconds",
777 grpclb_policy, lb_calld->client_stats_report_interval_);
778 }
779 } else if (grpc_lb_glb_trace.enabled()) {
780 gpr_log(GPR_INFO,
781 "[grpclb %p] Received initial LB response message; client load "
782 "reporting NOT enabled",
783 grpclb_policy);
784 }
785 grpc_grpclb_initial_response_destroy(initial_response);
786 lb_calld->seen_initial_response_ = true;
787 } else if ((serverlist = grpc_grpclb_response_parse_serverlist(
788 response_slice)) != nullptr) {
789 // Have seen initial response, look for serverlist.
790 GPR_ASSERT(lb_calld->lb_call_ != nullptr);
791 if (grpc_lb_glb_trace.enabled()) {
792 gpr_log(GPR_INFO,
793 "[grpclb %p] Serverlist with %" PRIuPTR " servers received",
794 grpclb_policy, serverlist->num_servers);
795 for (size_t i = 0; i < serverlist->num_servers; ++i) {
796 grpc_resolved_address addr;
797 ParseServer(serverlist->servers[i], &addr);
798 char* ipport;
799 grpc_sockaddr_to_string(&ipport, &addr, false);
800 gpr_log(GPR_INFO, "[grpclb %p] Serverlist[%" PRIuPTR "]: %s",
801 grpclb_policy, i, ipport);
802 gpr_free(ipport);
803 }
804 }
805 /* update serverlist */
806 if (serverlist->num_servers > 0) {
807 // Start sending client load report only after we start using the
808 // serverlist returned from the current LB call.
809 if (lb_calld->client_stats_report_interval_ > 0 &&
810 lb_calld->client_stats_ == nullptr) {
811 lb_calld->client_stats_ = grpc_grpclb_client_stats_create();
812 // TODO(roth): We currently track this ref manually. Once the
813 // ClosureRef API is ready, we should pass the RefCountedPtr<> along
814 // with the callback.
815 auto self = lb_calld->Ref(DEBUG_LOCATION, "client_load_report");
816 self.release();
817 lb_calld->ScheduleNextClientLoadReportLocked();
818 }
819 if (grpc_grpclb_serverlist_equals(grpclb_policy->serverlist_,
820 serverlist)) {
821 if (grpc_lb_glb_trace.enabled()) {
822 gpr_log(GPR_INFO,
823 "[grpclb %p] Incoming server list identical to current, "
824 "ignoring.",
825 grpclb_policy);
826 }
827 grpc_grpclb_destroy_serverlist(serverlist);
828 } else { /* new serverlist */
829 if (grpclb_policy->serverlist_ != nullptr) {
830 /* dispose of the old serverlist */
831 grpc_grpclb_destroy_serverlist(grpclb_policy->serverlist_);
832 } else {
833 /* or dispose of the fallback */
834 grpc_lb_addresses_destroy(grpclb_policy->fallback_backend_addresses_);
835 grpclb_policy->fallback_backend_addresses_ = nullptr;
836 if (grpclb_policy->fallback_timer_callback_pending_) {
837 grpc_timer_cancel(&grpclb_policy->lb_fallback_timer_);
838 }
839 }
840 // and update the copy in the GrpcLb instance. This
841 // serverlist instance will be destroyed either upon the next
842 // update or when the GrpcLb instance is destroyed.
843 grpclb_policy->serverlist_ = serverlist;
844 grpclb_policy->serverlist_index_ = 0;
845 grpclb_policy->CreateOrUpdateRoundRobinPolicyLocked();
846 }
847 } else {
848 if (grpc_lb_glb_trace.enabled()) {
849 gpr_log(GPR_INFO, "[grpclb %p] Received empty server list, ignoring.",
850 grpclb_policy);
851 }
852 grpc_grpclb_destroy_serverlist(serverlist);
853 }
854 } else {
855 // No valid initial response or serverlist found.
856 gpr_log(GPR_ERROR,
857 "[grpclb %p] Invalid LB response received: '%s'. Ignoring.",
858 grpclb_policy,
859 grpc_dump_slice(response_slice, GPR_DUMP_ASCII | GPR_DUMP_HEX));
860 }
861 grpc_slice_unref_internal(response_slice);
862 if (!grpclb_policy->shutting_down_) {
863 // Keep listening for serverlist updates.
864 grpc_op op;
865 memset(&op, 0, sizeof(op));
866 op.op = GRPC_OP_RECV_MESSAGE;
867 op.data.recv_message.recv_message = &lb_calld->recv_message_payload_;
868 op.flags = 0;
869 op.reserved = nullptr;
870 // Reuse the "OnBalancerMessageReceivedLocked" ref taken in StartQuery().
871 const grpc_call_error call_error = grpc_call_start_batch_and_execute(
872 lb_calld->lb_call_, &op, 1,
873 &lb_calld->lb_on_balancer_message_received_);
874 GPR_ASSERT(GRPC_CALL_OK == call_error);
875 } else {
876 lb_calld->Unref(DEBUG_LOCATION, "on_message_received+grpclb_shutdown");
877 }
878}
879
880void GrpcLb::BalancerCallState::OnBalancerStatusReceivedLocked(
881 void* arg, grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -0800882 BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -0800883 GrpcLb* grpclb_policy = lb_calld->grpclb_policy();
884 GPR_ASSERT(lb_calld->lb_call_ != nullptr);
885 if (grpc_lb_glb_trace.enabled()) {
886 char* status_details =
887 grpc_slice_to_c_string(lb_calld->lb_call_status_details_);
888 gpr_log(GPR_INFO,
889 "[grpclb %p] Status from LB server received. Status = %d, details "
890 "= '%s', (lb_calld: %p, lb_call: %p), error '%s'",
891 grpclb_policy, lb_calld->lb_call_status_, status_details, lb_calld,
892 lb_calld->lb_call_, grpc_error_string(error));
893 gpr_free(status_details);
894 }
895 grpclb_policy->TryReresolutionLocked(&grpc_lb_glb_trace, GRPC_ERROR_NONE);
896 // If this lb_calld is still in use, this call ended because of a failure so
897 // we want to retry connecting. Otherwise, we have deliberately ended this
898 // call and no further action is required.
899 if (lb_calld == grpclb_policy->lb_calld_.get()) {
900 grpclb_policy->lb_calld_.reset();
901 GPR_ASSERT(!grpclb_policy->shutting_down_);
902 if (lb_calld->seen_initial_response_) {
903 // If we lose connection to the LB server, reset the backoff and restart
904 // the LB call immediately.
905 grpclb_policy->lb_call_backoff_.Reset();
906 grpclb_policy->StartBalancerCallLocked();
907 } else {
908 // If this LB call fails establishing any connection to the LB server,
909 // retry later.
910 grpclb_policy->StartBalancerCallRetryTimerLocked();
911 }
912 }
913 lb_calld->Unref(DEBUG_LOCATION, "lb_call_ended");
914}
915
916//
917// helper code for creating balancer channel
918//
919
Mark D. Rothbd0f1512018-02-20 10:28:22 -0800920grpc_lb_addresses* ExtractBalancerAddresses(
921 const grpc_lb_addresses* addresses) {
922 size_t num_grpclb_addrs = 0;
923 for (size_t i = 0; i < addresses->num_addresses; ++i) {
924 if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs;
925 }
926 // There must be at least one balancer address, or else the
927 // client_channel would not have chosen this LB policy.
928 GPR_ASSERT(num_grpclb_addrs > 0);
929 grpc_lb_addresses* lb_addresses =
930 grpc_lb_addresses_create(num_grpclb_addrs, nullptr);
931 size_t lb_addresses_idx = 0;
932 for (size_t i = 0; i < addresses->num_addresses; ++i) {
933 if (!addresses->addresses[i].is_balancer) continue;
934 if (addresses->addresses[i].user_data != nullptr) {
935 gpr_log(GPR_ERROR,
936 "This LB policy doesn't support user data. It will be ignored");
937 }
938 grpc_lb_addresses_set_address(
939 lb_addresses, lb_addresses_idx++, addresses->addresses[i].address.addr,
940 addresses->addresses[i].address.len, false /* is balancer */,
941 addresses->addresses[i].balancer_name, nullptr /* user data */);
942 }
943 GPR_ASSERT(num_grpclb_addrs == lb_addresses_idx);
944 return lb_addresses;
Mark D. Rothc8875492018-02-20 08:33:48 -0800945}
946
947/* Returns the channel args for the LB channel, used to create a bidirectional
948 * stream for the reception of load balancing updates.
949 *
950 * Inputs:
951 * - \a addresses: corresponding to the balancers.
952 * - \a response_generator: in order to propagate updates from the resolver
953 * above the grpclb policy.
954 * - \a args: other args inherited from the grpclb policy. */
955grpc_channel_args* BuildBalancerChannelArgs(
956 const grpc_lb_addresses* addresses,
957 FakeResolverResponseGenerator* response_generator,
958 const grpc_channel_args* args) {
Mark D. Rothbd0f1512018-02-20 10:28:22 -0800959 grpc_lb_addresses* lb_addresses = ExtractBalancerAddresses(addresses);
960 // Channel args to remove.
961 static const char* args_to_remove[] = {
962 // LB policy name, since we want to use the default (pick_first) in
963 // the LB channel.
964 GRPC_ARG_LB_POLICY_NAME,
965 // The channel arg for the server URI, since that will be different for
966 // the LB channel than for the parent channel. The client channel
967 // factory will re-add this arg with the right value.
968 GRPC_ARG_SERVER_URI,
969 // The resolved addresses, which will be generated by the name resolver
970 // used in the LB channel. Note that the LB channel will use the fake
971 // resolver, so this won't actually generate a query to DNS (or some
972 // other name service). However, the addresses returned by the fake
973 // resolver will have is_balancer=false, whereas our own addresses have
974 // is_balancer=true. We need the LB channel to return addresses with
975 // is_balancer=false so that it does not wind up recursively using the
976 // grpclb LB policy, as per the special case logic in client_channel.c.
977 GRPC_ARG_LB_ADDRESSES,
978 // The fake resolver response generator, because we are replacing it
979 // with the one from the grpclb policy, used to propagate updates to
980 // the LB channel.
981 GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
David Garcia Quintasc7c0d692018-03-10 17:27:15 -0800982 // The LB channel should use the authority indicated by the target
983 // authority table (see \a grpc_lb_policy_grpclb_modify_lb_channel_args),
984 // as opposed to the authority from the parent channel.
985 GRPC_ARG_DEFAULT_AUTHORITY,
Mark D. Rothbd0f1512018-02-20 10:28:22 -0800986 };
987 // Channel args to add.
988 const grpc_arg args_to_add[] = {
989 // New LB addresses.
990 // Note that we pass these in both when creating the LB channel
991 // and via the fake resolver. The latter is what actually gets used.
992 grpc_lb_addresses_create_channel_arg(lb_addresses),
993 // The fake resolver response generator, which we use to inject
994 // address updates into the LB channel.
995 grpc_core::FakeResolverResponseGenerator::MakeChannelArg(
996 response_generator),
997 };
998 // Construct channel args.
999 grpc_channel_args* new_args = grpc_channel_args_copy_and_add_and_remove(
1000 args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove), args_to_add,
1001 GPR_ARRAY_SIZE(args_to_add));
1002 // Make any necessary modifications for security.
1003 new_args = grpc_lb_policy_grpclb_modify_lb_channel_args(new_args);
1004 // Clean up.
Mark D. Rothc8875492018-02-20 08:33:48 -08001005 grpc_lb_addresses_destroy(lb_addresses);
Mark D. Rothbd0f1512018-02-20 10:28:22 -08001006 return new_args;
Mark D. Rothc8875492018-02-20 08:33:48 -08001007}
1008
1009//
1010// ctor and dtor
1011//
1012
1013GrpcLb::GrpcLb(const grpc_lb_addresses* addresses,
1014 const LoadBalancingPolicy::Args& args)
1015 : LoadBalancingPolicy(args),
1016 response_generator_(MakeRefCounted<FakeResolverResponseGenerator>()),
1017 lb_call_backoff_(
1018 BackOff::Options()
1019 .set_initial_backoff(GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS *
1020 1000)
1021 .set_multiplier(GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER)
1022 .set_jitter(GRPC_GRPCLB_RECONNECT_JITTER)
1023 .set_max_backoff(GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS *
1024 1000)) {
1025 // Initialization.
1026 grpc_subchannel_index_ref();
1027 GRPC_CLOSURE_INIT(&lb_channel_on_connectivity_changed_,
1028 &GrpcLb::OnBalancerChannelConnectivityChangedLocked, this,
1029 grpc_combiner_scheduler(args.combiner));
1030 GRPC_CLOSURE_INIT(&on_rr_connectivity_changed_,
1031 &GrpcLb::OnRoundRobinConnectivityChangedLocked, this,
1032 grpc_combiner_scheduler(args.combiner));
1033 GRPC_CLOSURE_INIT(&on_rr_request_reresolution_,
1034 &GrpcLb::OnRoundRobinRequestReresolutionLocked, this,
1035 grpc_combiner_scheduler(args.combiner));
1036 grpc_connectivity_state_init(&state_tracker_, GRPC_CHANNEL_IDLE, "grpclb");
1037 // Record server name.
1038 const grpc_arg* arg = grpc_channel_args_find(args.args, GRPC_ARG_SERVER_URI);
1039 const char* server_uri = grpc_channel_arg_get_string(arg);
1040 GPR_ASSERT(server_uri != nullptr);
1041 grpc_uri* uri = grpc_uri_parse(server_uri, true);
1042 GPR_ASSERT(uri->path[0] != '\0');
1043 server_name_ = gpr_strdup(uri->path[0] == '/' ? uri->path + 1 : uri->path);
1044 if (grpc_lb_glb_trace.enabled()) {
1045 gpr_log(GPR_INFO,
1046 "[grpclb %p] Will use '%s' as the server name for LB request.",
1047 this, server_name_);
1048 }
1049 grpc_uri_destroy(uri);
1050 // Record LB call timeout.
1051 arg = grpc_channel_args_find(args.args, GRPC_ARG_GRPCLB_CALL_TIMEOUT_MS);
1052 lb_call_timeout_ms_ = grpc_channel_arg_get_integer(arg, {0, 0, INT_MAX});
1053 // Record fallback timeout.
1054 arg = grpc_channel_args_find(args.args, GRPC_ARG_GRPCLB_FALLBACK_TIMEOUT_MS);
1055 lb_fallback_timeout_ms_ = grpc_channel_arg_get_integer(
1056 arg, {GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS, 0, INT_MAX});
1057 // Process channel args.
1058 ProcessChannelArgsLocked(*args.args);
1059}
1060
1061GrpcLb::~GrpcLb() {
1062 GPR_ASSERT(pending_picks_ == nullptr);
1063 GPR_ASSERT(pending_pings_ == nullptr);
1064 gpr_free((void*)server_name_);
1065 grpc_channel_args_destroy(args_);
1066 grpc_connectivity_state_destroy(&state_tracker_);
1067 if (serverlist_ != nullptr) {
1068 grpc_grpclb_destroy_serverlist(serverlist_);
1069 }
1070 if (fallback_backend_addresses_ != nullptr) {
1071 grpc_lb_addresses_destroy(fallback_backend_addresses_);
1072 }
1073 grpc_subchannel_index_unref();
1074}
1075
1076void GrpcLb::ShutdownLocked() {
1077 grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel shutdown");
1078 shutting_down_ = true;
1079 lb_calld_.reset();
1080 if (retry_timer_callback_pending_) {
1081 grpc_timer_cancel(&lb_call_retry_timer_);
1082 }
1083 if (fallback_timer_callback_pending_) {
1084 grpc_timer_cancel(&lb_fallback_timer_);
1085 }
1086 rr_policy_.reset();
1087 TryReresolutionLocked(&grpc_lb_glb_trace, GRPC_ERROR_CANCELLED);
1088 // We destroy the LB channel here instead of in our destructor because
1089 // destroying the channel triggers a last callback to
1090 // OnBalancerChannelConnectivityChangedLocked(), and we need to be
1091 // alive when that callback is invoked.
1092 if (lb_channel_ != nullptr) {
1093 grpc_channel_destroy(lb_channel_);
1094 lb_channel_ = nullptr;
1095 }
1096 grpc_connectivity_state_set(&state_tracker_, GRPC_CHANNEL_SHUTDOWN,
1097 GRPC_ERROR_REF(error), "grpclb_shutdown");
1098 // Clear pending picks.
1099 PendingPick* pp;
1100 while ((pp = pending_picks_) != nullptr) {
1101 pending_picks_ = pp->next;
1102 pp->pick->connected_subchannel.reset();
1103 // Note: pp is deleted in this callback.
1104 GRPC_CLOSURE_SCHED(&pp->on_complete, GRPC_ERROR_REF(error));
1105 }
1106 // Clear pending pings.
1107 PendingPing* pping;
1108 while ((pping = pending_pings_) != nullptr) {
1109 pending_pings_ = pping->next;
1110 GRPC_CLOSURE_SCHED(pping->on_initiate, GRPC_ERROR_REF(error));
1111 GRPC_CLOSURE_SCHED(pping->on_ack, GRPC_ERROR_REF(error));
1112 Delete(pping);
1113 }
1114 GRPC_ERROR_UNREF(error);
1115}
1116
1117//
1118// public methods
1119//
1120
1121void GrpcLb::HandOffPendingPicksLocked(LoadBalancingPolicy* new_policy) {
1122 PendingPick* pp;
1123 while ((pp = pending_picks_) != nullptr) {
1124 pending_picks_ = pp->next;
1125 pp->pick->on_complete = pp->original_on_complete;
1126 pp->pick->user_data = nullptr;
1127 if (new_policy->PickLocked(pp->pick)) {
1128 // Synchronous return; schedule closure.
1129 GRPC_CLOSURE_SCHED(pp->pick->on_complete, GRPC_ERROR_NONE);
1130 }
1131 Delete(pp);
1132 }
1133}
1134
1135// Cancel a specific pending pick.
1136//
1137// A grpclb pick progresses as follows:
1138// - If there's a Round Robin policy (rr_policy_) available, it'll be
1139// handed over to the RR policy (in CreateRoundRobinPolicyLocked()). From
1140// that point onwards, it'll be RR's responsibility. For cancellations, that
1141// implies the pick needs also be cancelled by the RR instance.
1142// - Otherwise, without an RR instance, picks stay pending at this policy's
1143// level (grpclb), inside the pending_picks_ list. To cancel these,
1144// we invoke the completion closure and set the pick's connected
1145// subchannel to nullptr right here.
1146void GrpcLb::CancelPickLocked(PickState* pick, grpc_error* error) {
1147 PendingPick* pp = pending_picks_;
1148 pending_picks_ = nullptr;
1149 while (pp != nullptr) {
1150 PendingPick* next = pp->next;
1151 if (pp->pick == pick) {
1152 pick->connected_subchannel.reset();
1153 // Note: pp is deleted in this callback.
1154 GRPC_CLOSURE_SCHED(&pp->on_complete,
1155 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
1156 "Pick Cancelled", &error, 1));
1157 } else {
1158 pp->next = pending_picks_;
1159 pending_picks_ = pp;
1160 }
1161 pp = next;
1162 }
1163 if (rr_policy_ != nullptr) {
1164 rr_policy_->CancelPickLocked(pick, GRPC_ERROR_REF(error));
1165 }
1166 GRPC_ERROR_UNREF(error);
1167}
1168
1169// Cancel all pending picks.
1170//
1171// A grpclb pick progresses as follows:
1172// - If there's a Round Robin policy (rr_policy_) available, it'll be
1173// handed over to the RR policy (in CreateRoundRobinPolicyLocked()). From
1174// that point onwards, it'll be RR's responsibility. For cancellations, that
1175// implies the pick needs also be cancelled by the RR instance.
1176// - Otherwise, without an RR instance, picks stay pending at this policy's
1177// level (grpclb), inside the pending_picks_ list. To cancel these,
1178// we invoke the completion closure and set the pick's connected
1179// subchannel to nullptr right here.
1180void GrpcLb::CancelMatchingPicksLocked(uint32_t initial_metadata_flags_mask,
1181 uint32_t initial_metadata_flags_eq,
1182 grpc_error* error) {
1183 PendingPick* pp = pending_picks_;
1184 pending_picks_ = nullptr;
1185 while (pp != nullptr) {
1186 PendingPick* next = pp->next;
1187 if ((pp->pick->initial_metadata_flags & initial_metadata_flags_mask) ==
1188 initial_metadata_flags_eq) {
1189 // Note: pp is deleted in this callback.
1190 GRPC_CLOSURE_SCHED(&pp->on_complete,
1191 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
1192 "Pick Cancelled", &error, 1));
1193 } else {
1194 pp->next = pending_picks_;
1195 pending_picks_ = pp;
1196 }
1197 pp = next;
1198 }
1199 if (rr_policy_ != nullptr) {
1200 rr_policy_->CancelMatchingPicksLocked(initial_metadata_flags_mask,
1201 initial_metadata_flags_eq,
1202 GRPC_ERROR_REF(error));
1203 }
1204 GRPC_ERROR_UNREF(error);
1205}
1206
1207void GrpcLb::ExitIdleLocked() {
1208 if (!started_picking_) {
1209 StartPickingLocked();
1210 }
1211}
1212
1213bool GrpcLb::PickLocked(PickState* pick) {
1214 PendingPick* pp = PendingPickCreate(pick);
1215 bool pick_done = false;
1216 if (rr_policy_ != nullptr) {
1217 const grpc_connectivity_state rr_connectivity_state =
1218 rr_policy_->CheckConnectivityLocked(nullptr);
1219 // The RR policy may have transitioned to SHUTDOWN but the callback
1220 // registered to capture this event (on_rr_connectivity_changed_) may not
1221 // have been invoked yet. We need to make sure we aren't trying to pick
1222 // from an RR policy instance that's in shutdown.
1223 if (rr_connectivity_state == GRPC_CHANNEL_SHUTDOWN) {
1224 if (grpc_lb_glb_trace.enabled()) {
1225 gpr_log(GPR_INFO,
1226 "[grpclb %p] NOT picking from from RR %p: RR conn state=%s",
1227 this, rr_policy_.get(),
1228 grpc_connectivity_state_name(rr_connectivity_state));
1229 }
1230 AddPendingPick(pp);
1231 pick_done = false;
1232 } else { // RR not in shutdown
1233 if (grpc_lb_glb_trace.enabled()) {
1234 gpr_log(GPR_INFO, "[grpclb %p] about to PICK from RR %p", this,
1235 rr_policy_.get());
1236 }
1237 pick_done = PickFromRoundRobinPolicyLocked(false /* force_async */, pp);
1238 }
1239 } else { // rr_policy_ == NULL
1240 if (grpc_lb_glb_trace.enabled()) {
1241 gpr_log(GPR_DEBUG,
1242 "[grpclb %p] No RR policy. Adding to grpclb's pending picks",
1243 this);
1244 }
1245 AddPendingPick(pp);
1246 if (!started_picking_) {
1247 StartPickingLocked();
1248 }
1249 pick_done = false;
1250 }
1251 return pick_done;
1252}
1253
1254void GrpcLb::PingOneLocked(grpc_closure* on_initiate, grpc_closure* on_ack) {
1255 if (rr_policy_ != nullptr) {
1256 rr_policy_->PingOneLocked(on_initiate, on_ack);
1257 } else {
1258 AddPendingPing(on_initiate, on_ack);
1259 if (!started_picking_) {
1260 StartPickingLocked();
1261 }
1262 }
1263}
1264
1265grpc_connectivity_state GrpcLb::CheckConnectivityLocked(
1266 grpc_error** connectivity_error) {
1267 return grpc_connectivity_state_get(&state_tracker_, connectivity_error);
1268}
1269
1270void GrpcLb::NotifyOnStateChangeLocked(grpc_connectivity_state* current,
1271 grpc_closure* notify) {
1272 grpc_connectivity_state_notify_on_state_change(&state_tracker_, current,
1273 notify);
1274}
1275
1276void GrpcLb::ProcessChannelArgsLocked(const grpc_channel_args& args) {
1277 const grpc_arg* arg = grpc_channel_args_find(&args, GRPC_ARG_LB_ADDRESSES);
1278 if (arg == nullptr || arg->type != GRPC_ARG_POINTER) {
1279 // Ignore this update.
1280 gpr_log(
1281 GPR_ERROR,
1282 "[grpclb %p] No valid LB addresses channel arg in update, ignoring.",
1283 this);
1284 return;
1285 }
1286 const grpc_lb_addresses* addresses =
Vijay Pai7fed69b2018-03-05 09:58:05 -08001287 static_cast<const grpc_lb_addresses*>(arg->value.pointer.p);
Mark D. Rothc8875492018-02-20 08:33:48 -08001288 // Update fallback address list.
1289 if (fallback_backend_addresses_ != nullptr) {
1290 grpc_lb_addresses_destroy(fallback_backend_addresses_);
1291 }
1292 fallback_backend_addresses_ = ExtractBackendAddresses(addresses);
1293 // Make sure that GRPC_ARG_LB_POLICY_NAME is set in channel args,
1294 // since we use this to trigger the client_load_reporting filter.
1295 static const char* args_to_remove[] = {GRPC_ARG_LB_POLICY_NAME};
1296 grpc_arg new_arg = grpc_channel_arg_string_create(
1297 (char*)GRPC_ARG_LB_POLICY_NAME, (char*)"grpclb");
1298 grpc_channel_args_destroy(args_);
1299 args_ = grpc_channel_args_copy_and_add_and_remove(
1300 &args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove), &new_arg, 1);
1301 // Construct args for balancer channel.
1302 grpc_channel_args* lb_channel_args =
1303 BuildBalancerChannelArgs(addresses, response_generator_.get(), &args);
1304 // Create balancer channel if needed.
1305 if (lb_channel_ == nullptr) {
1306 char* uri_str;
1307 gpr_asprintf(&uri_str, "fake:///%s", server_name_);
Mark D. Rothbd0f1512018-02-20 10:28:22 -08001308 lb_channel_ = grpc_client_channel_factory_create_channel(
1309 client_channel_factory(), uri_str,
1310 GRPC_CLIENT_CHANNEL_TYPE_LOAD_BALANCING, lb_channel_args);
Mark D. Rothc8875492018-02-20 08:33:48 -08001311 GPR_ASSERT(lb_channel_ != nullptr);
1312 gpr_free(uri_str);
1313 }
1314 // Propagate updates to the LB channel (pick_first) through the fake
1315 // resolver.
1316 response_generator_->SetResponse(lb_channel_args);
1317 grpc_channel_args_destroy(lb_channel_args);
1318}
1319
1320void GrpcLb::UpdateLocked(const grpc_channel_args& args) {
1321 ProcessChannelArgsLocked(args);
1322 // If fallback is configured and the RR policy already exists, update
1323 // it with the new fallback addresses.
1324 if (lb_fallback_timeout_ms_ > 0 && rr_policy_ != nullptr) {
1325 CreateOrUpdateRoundRobinPolicyLocked();
1326 }
1327 // Start watching the LB channel connectivity for connection, if not
1328 // already doing so.
1329 if (!watching_lb_channel_) {
1330 lb_channel_connectivity_ = grpc_channel_check_connectivity_state(
1331 lb_channel_, true /* try to connect */);
1332 grpc_channel_element* client_channel_elem = grpc_channel_stack_last_element(
1333 grpc_channel_get_channel_stack(lb_channel_));
1334 GPR_ASSERT(client_channel_elem->filter == &grpc_client_channel_filter);
1335 watching_lb_channel_ = true;
1336 // TODO(roth): We currently track this ref manually. Once the
1337 // ClosureRef API is ready, we should pass the RefCountedPtr<> along
1338 // with the callback.
1339 auto self = Ref(DEBUG_LOCATION, "watch_lb_channel_connectivity");
1340 self.release();
1341 grpc_client_channel_watch_connectivity_state(
1342 client_channel_elem,
1343 grpc_polling_entity_create_from_pollset_set(interested_parties()),
1344 &lb_channel_connectivity_, &lb_channel_on_connectivity_changed_,
1345 nullptr);
1346 }
1347}
1348
1349//
1350// code for balancer channel and call
1351//
1352
1353void GrpcLb::StartPickingLocked() {
1354 // Start a timer to fall back.
1355 if (lb_fallback_timeout_ms_ > 0 && serverlist_ == nullptr &&
1356 !fallback_timer_callback_pending_) {
1357 grpc_millis deadline = ExecCtx::Get()->Now() + lb_fallback_timeout_ms_;
1358 // TODO(roth): We currently track this ref manually. Once the
1359 // ClosureRef API is ready, we should pass the RefCountedPtr<> along
1360 // with the callback.
1361 auto self = Ref(DEBUG_LOCATION, "on_fallback_timer");
1362 self.release();
1363 GRPC_CLOSURE_INIT(&lb_on_fallback_, &GrpcLb::OnFallbackTimerLocked, this,
1364 grpc_combiner_scheduler(combiner()));
1365 fallback_timer_callback_pending_ = true;
1366 grpc_timer_init(&lb_fallback_timer_, deadline, &lb_on_fallback_);
1367 }
1368 started_picking_ = true;
1369 StartBalancerCallLocked();
1370}
1371
1372void GrpcLb::StartBalancerCallLocked() {
1373 GPR_ASSERT(lb_channel_ != nullptr);
1374 if (shutting_down_) return;
1375 // Init the LB call data.
1376 GPR_ASSERT(lb_calld_ == nullptr);
1377 lb_calld_ = MakeOrphanable<BalancerCallState>(Ref());
1378 if (grpc_lb_glb_trace.enabled()) {
1379 gpr_log(GPR_INFO,
1380 "[grpclb %p] Query for backends (lb_channel: %p, lb_calld: %p)",
1381 this, lb_channel_, lb_calld_.get());
1382 }
1383 lb_calld_->StartQuery();
1384}
1385
1386void GrpcLb::OnFallbackTimerLocked(void* arg, grpc_error* error) {
1387 GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg);
1388 grpclb_policy->fallback_timer_callback_pending_ = false;
1389 // If we receive a serverlist after the timer fires but before this callback
1390 // actually runs, don't fall back.
1391 if (grpclb_policy->serverlist_ == nullptr && !grpclb_policy->shutting_down_ &&
1392 error == GRPC_ERROR_NONE) {
1393 if (grpc_lb_glb_trace.enabled()) {
1394 gpr_log(GPR_INFO,
1395 "[grpclb %p] Falling back to use backends from resolver",
1396 grpclb_policy);
1397 }
1398 GPR_ASSERT(grpclb_policy->fallback_backend_addresses_ != nullptr);
1399 grpclb_policy->CreateOrUpdateRoundRobinPolicyLocked();
1400 }
1401 grpclb_policy->Unref(DEBUG_LOCATION, "on_fallback_timer");
1402}
1403
1404void GrpcLb::StartBalancerCallRetryTimerLocked() {
1405 grpc_millis next_try = lb_call_backoff_.NextAttemptTime();
1406 if (grpc_lb_glb_trace.enabled()) {
1407 gpr_log(GPR_DEBUG, "[grpclb %p] Connection to LB server lost...", this);
1408 grpc_millis timeout = next_try - ExecCtx::Get()->Now();
1409 if (timeout > 0) {
1410 gpr_log(GPR_DEBUG,
1411 "[grpclb %p] ... retry_timer_active in %" PRIuPTR "ms.", this,
1412 timeout);
1413 } else {
1414 gpr_log(GPR_DEBUG, "[grpclb %p] ... retry_timer_active immediately.",
1415 this);
1416 }
1417 }
1418 // TODO(roth): We currently track this ref manually. Once the
1419 // ClosureRef API is ready, we should pass the RefCountedPtr<> along
1420 // with the callback.
1421 auto self = Ref(DEBUG_LOCATION, "on_balancer_call_retry_timer");
1422 self.release();
1423 GRPC_CLOSURE_INIT(&lb_on_call_retry_, &GrpcLb::OnBalancerCallRetryTimerLocked,
1424 this, grpc_combiner_scheduler(combiner()));
1425 retry_timer_callback_pending_ = true;
1426 grpc_timer_init(&lb_call_retry_timer_, next_try, &lb_on_call_retry_);
1427}
1428
1429void GrpcLb::OnBalancerCallRetryTimerLocked(void* arg, grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -08001430 GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -08001431 grpclb_policy->retry_timer_callback_pending_ = false;
1432 if (!grpclb_policy->shutting_down_ && error == GRPC_ERROR_NONE &&
1433 grpclb_policy->lb_calld_ == nullptr) {
1434 if (grpc_lb_glb_trace.enabled()) {
1435 gpr_log(GPR_INFO, "[grpclb %p] Restarting call to LB server",
1436 grpclb_policy);
1437 }
1438 grpclb_policy->StartBalancerCallLocked();
1439 }
1440 grpclb_policy->Unref(DEBUG_LOCATION, "on_balancer_call_retry_timer");
1441}
1442
1443// Invoked as part of the update process. It continues watching the LB channel
1444// until it shuts down or becomes READY. It's invoked even if the LB channel
1445// stayed READY throughout the update (for example if the update is identical).
1446void GrpcLb::OnBalancerChannelConnectivityChangedLocked(void* arg,
1447 grpc_error* error) {
1448 GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg);
1449 if (grpclb_policy->shutting_down_) goto done;
1450 // Re-initialize the lb_call. This should also take care of updating the
1451 // embedded RR policy. Note that the current RR policy, if any, will stay in
1452 // effect until an update from the new lb_call is received.
1453 switch (grpclb_policy->lb_channel_connectivity_) {
1454 case GRPC_CHANNEL_CONNECTING:
1455 case GRPC_CHANNEL_TRANSIENT_FAILURE: {
1456 // Keep watching the LB channel.
1457 grpc_channel_element* client_channel_elem =
1458 grpc_channel_stack_last_element(
1459 grpc_channel_get_channel_stack(grpclb_policy->lb_channel_));
1460 GPR_ASSERT(client_channel_elem->filter == &grpc_client_channel_filter);
1461 grpc_client_channel_watch_connectivity_state(
1462 client_channel_elem,
1463 grpc_polling_entity_create_from_pollset_set(
1464 grpclb_policy->interested_parties()),
1465 &grpclb_policy->lb_channel_connectivity_,
1466 &grpclb_policy->lb_channel_on_connectivity_changed_, nullptr);
1467 break;
1468 }
1469 // The LB channel may be IDLE because it's shut down before the update.
1470 // Restart the LB call to kick the LB channel into gear.
1471 case GRPC_CHANNEL_IDLE:
1472 case GRPC_CHANNEL_READY:
1473 grpclb_policy->lb_calld_.reset();
1474 if (grpclb_policy->started_picking_) {
1475 if (grpclb_policy->retry_timer_callback_pending_) {
1476 grpc_timer_cancel(&grpclb_policy->lb_call_retry_timer_);
1477 }
1478 grpclb_policy->lb_call_backoff_.Reset();
1479 grpclb_policy->StartBalancerCallLocked();
1480 }
1481 // Fall through.
1482 case GRPC_CHANNEL_SHUTDOWN:
1483 done:
1484 grpclb_policy->watching_lb_channel_ = false;
1485 grpclb_policy->Unref(DEBUG_LOCATION,
1486 "watch_lb_channel_connectivity_cb_shutdown");
1487 }
1488}
1489
1490//
1491// PendingPick
1492//
1493
1494// Adds lb_token of selected subchannel (address) to the call's initial
1495// metadata.
1496grpc_error* AddLbTokenToInitialMetadata(
1497 grpc_mdelem lb_token, grpc_linked_mdelem* lb_token_mdelem_storage,
1498 grpc_metadata_batch* initial_metadata) {
1499 GPR_ASSERT(lb_token_mdelem_storage != nullptr);
1500 GPR_ASSERT(!GRPC_MDISNULL(lb_token));
1501 return grpc_metadata_batch_add_tail(initial_metadata, lb_token_mdelem_storage,
1502 lb_token);
1503}
1504
1505// Destroy function used when embedding client stats in call context.
1506void DestroyClientStats(void* arg) {
Vijay Pai7fed69b2018-03-05 09:58:05 -08001507 grpc_grpclb_client_stats_unref(static_cast<grpc_grpclb_client_stats*>(arg));
Mark D. Rothc8875492018-02-20 08:33:48 -08001508}
1509
1510void GrpcLb::PendingPickSetMetadataAndContext(PendingPick* pp) {
1511 /* if connected_subchannel is nullptr, no pick has been made by the RR
1512 * policy (e.g., all addresses failed to connect). There won't be any
1513 * user_data/token available */
1514 if (pp->pick->connected_subchannel != nullptr) {
1515 if (!GRPC_MDISNULL(pp->lb_token)) {
1516 AddLbTokenToInitialMetadata(GRPC_MDELEM_REF(pp->lb_token),
1517 &pp->pick->lb_token_mdelem_storage,
1518 pp->pick->initial_metadata);
1519 } else {
1520 gpr_log(GPR_ERROR,
1521 "[grpclb %p] No LB token for connected subchannel pick %p",
1522 pp->grpclb_policy, pp->pick);
1523 abort();
1524 }
1525 // Pass on client stats via context. Passes ownership of the reference.
1526 if (pp->client_stats != nullptr) {
1527 pp->pick->subchannel_call_context[GRPC_GRPCLB_CLIENT_STATS].value =
1528 pp->client_stats;
1529 pp->pick->subchannel_call_context[GRPC_GRPCLB_CLIENT_STATS].destroy =
1530 DestroyClientStats;
1531 }
1532 } else {
1533 if (pp->client_stats != nullptr) {
1534 grpc_grpclb_client_stats_unref(pp->client_stats);
1535 }
1536 }
1537}
1538
1539/* The \a on_complete closure passed as part of the pick requires keeping a
1540 * reference to its associated round robin instance. We wrap this closure in
1541 * order to unref the round robin instance upon its invocation */
1542void GrpcLb::OnPendingPickComplete(void* arg, grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -08001543 PendingPick* pp = static_cast<PendingPick*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -08001544 PendingPickSetMetadataAndContext(pp);
1545 GRPC_CLOSURE_SCHED(pp->original_on_complete, GRPC_ERROR_REF(error));
1546 Delete(pp);
1547}
1548
1549GrpcLb::PendingPick* GrpcLb::PendingPickCreate(PickState* pick) {
1550 PendingPick* pp = New<PendingPick>();
1551 pp->grpclb_policy = this;
1552 pp->pick = pick;
1553 GRPC_CLOSURE_INIT(&pp->on_complete, &GrpcLb::OnPendingPickComplete, pp,
1554 grpc_schedule_on_exec_ctx);
1555 pp->original_on_complete = pick->on_complete;
1556 pick->on_complete = &pp->on_complete;
1557 return pp;
1558}
1559
1560void GrpcLb::AddPendingPick(PendingPick* pp) {
1561 pp->next = pending_picks_;
1562 pending_picks_ = pp;
1563}
1564
1565//
1566// PendingPing
1567//
1568
1569void GrpcLb::AddPendingPing(grpc_closure* on_initiate, grpc_closure* on_ack) {
1570 PendingPing* pping = New<PendingPing>();
1571 pping->on_initiate = on_initiate;
1572 pping->on_ack = on_ack;
1573 pping->next = pending_pings_;
1574 pending_pings_ = pping;
1575}
1576
1577//
1578// code for interacting with the RR policy
1579//
1580
1581// Performs a pick over \a rr_policy_. Given that a pick can return
1582// immediately (ignoring its completion callback), we need to perform the
1583// cleanups this callback would otherwise be responsible for.
1584// If \a force_async is true, then we will manually schedule the
1585// completion callback even if the pick is available immediately.
1586bool GrpcLb::PickFromRoundRobinPolicyLocked(bool force_async, PendingPick* pp) {
1587 // Check for drops if we are not using fallback backend addresses.
1588 if (serverlist_ != nullptr) {
1589 // Look at the index into the serverlist to see if we should drop this call.
1590 grpc_grpclb_server* server = serverlist_->servers[serverlist_index_++];
1591 if (serverlist_index_ == serverlist_->num_servers) {
1592 serverlist_index_ = 0; // Wrap-around.
1593 }
1594 if (server->drop) {
1595 // Update client load reporting stats to indicate the number of
1596 // dropped calls. Note that we have to do this here instead of in
1597 // the client_load_reporting filter, because we do not create a
1598 // subchannel call (and therefore no client_load_reporting filter)
1599 // for dropped calls.
1600 if (lb_calld_ != nullptr && lb_calld_->client_stats() != nullptr) {
1601 grpc_grpclb_client_stats_add_call_dropped_locked(
1602 server->load_balance_token, lb_calld_->client_stats());
1603 }
1604 if (force_async) {
1605 GRPC_CLOSURE_SCHED(pp->original_on_complete, GRPC_ERROR_NONE);
1606 Delete(pp);
1607 return false;
1608 }
1609 Delete(pp);
1610 return true;
1611 }
1612 }
1613 // Set client_stats and user_data.
1614 if (lb_calld_ != nullptr && lb_calld_->client_stats() != nullptr) {
1615 pp->client_stats = grpc_grpclb_client_stats_ref(lb_calld_->client_stats());
1616 }
1617 GPR_ASSERT(pp->pick->user_data == nullptr);
1618 pp->pick->user_data = (void**)&pp->lb_token;
1619 // Pick via the RR policy.
1620 bool pick_done = rr_policy_->PickLocked(pp->pick);
1621 if (pick_done) {
1622 PendingPickSetMetadataAndContext(pp);
1623 if (force_async) {
1624 GRPC_CLOSURE_SCHED(pp->original_on_complete, GRPC_ERROR_NONE);
1625 pick_done = false;
1626 }
1627 Delete(pp);
1628 }
1629 // else, the pending pick will be registered and taken care of by the
1630 // pending pick list inside the RR policy. Eventually,
1631 // OnPendingPickComplete() will be called, which will (among other
1632 // things) add the LB token to the call's initial metadata.
1633 return pick_done;
1634}
1635
1636void GrpcLb::CreateRoundRobinPolicyLocked(const Args& args) {
1637 GPR_ASSERT(rr_policy_ == nullptr);
1638 rr_policy_ = LoadBalancingPolicyRegistry::CreateLoadBalancingPolicy(
1639 "round_robin", args);
1640 if (rr_policy_ == nullptr) {
1641 gpr_log(GPR_ERROR, "[grpclb %p] Failure creating a RoundRobin policy",
1642 this);
1643 return;
1644 }
1645 // TODO(roth): We currently track this ref manually. Once the new
1646 // ClosureRef API is done, pass the RefCountedPtr<> along with the closure.
1647 auto self = Ref(DEBUG_LOCATION, "on_rr_reresolution_requested");
1648 self.release();
1649 rr_policy_->SetReresolutionClosureLocked(&on_rr_request_reresolution_);
1650 grpc_error* rr_state_error = nullptr;
1651 rr_connectivity_state_ = rr_policy_->CheckConnectivityLocked(&rr_state_error);
1652 // Connectivity state is a function of the RR policy updated/created.
1653 UpdateConnectivityStateFromRoundRobinPolicyLocked(rr_state_error);
1654 // Add the gRPC LB's interested_parties pollset_set to that of the newly
1655 // created RR policy. This will make the RR policy progress upon activity on
1656 // gRPC LB, which in turn is tied to the application's call.
1657 grpc_pollset_set_add_pollset_set(rr_policy_->interested_parties(),
1658 interested_parties());
1659 // Subscribe to changes to the connectivity of the new RR.
1660 // TODO(roth): We currently track this ref manually. Once the new
1661 // ClosureRef API is done, pass the RefCountedPtr<> along with the closure.
1662 self = Ref(DEBUG_LOCATION, "on_rr_connectivity_changed");
1663 self.release();
1664 rr_policy_->NotifyOnStateChangeLocked(&rr_connectivity_state_,
1665 &on_rr_connectivity_changed_);
1666 rr_policy_->ExitIdleLocked();
1667 // Send pending picks to RR policy.
1668 PendingPick* pp;
1669 while ((pp = pending_picks_)) {
1670 pending_picks_ = pp->next;
1671 if (grpc_lb_glb_trace.enabled()) {
1672 gpr_log(GPR_INFO,
1673 "[grpclb %p] Pending pick about to (async) PICK from RR %p", this,
1674 rr_policy_.get());
1675 }
1676 PickFromRoundRobinPolicyLocked(true /* force_async */, pp);
1677 }
1678 // Send pending pings to RR policy.
1679 PendingPing* pping;
1680 while ((pping = pending_pings_)) {
1681 pending_pings_ = pping->next;
1682 if (grpc_lb_glb_trace.enabled()) {
1683 gpr_log(GPR_INFO, "[grpclb %p] Pending ping about to PING from RR %p",
1684 this, rr_policy_.get());
1685 }
1686 rr_policy_->PingOneLocked(pping->on_initiate, pping->on_ack);
1687 Delete(pping);
1688 }
1689}
1690
1691grpc_channel_args* GrpcLb::CreateRoundRobinPolicyArgsLocked() {
1692 grpc_lb_addresses* addresses;
1693 if (serverlist_ != nullptr) {
1694 GPR_ASSERT(serverlist_->num_servers > 0);
1695 addresses = ProcessServerlist(serverlist_);
1696 } else {
1697 // If CreateOrUpdateRoundRobinPolicyLocked() is invoked when we haven't
1698 // received any serverlist from the balancer, we use the fallback backends
1699 // returned by the resolver. Note that the fallback backend list may be
1700 // empty, in which case the new round_robin policy will keep the requested
1701 // picks pending.
1702 GPR_ASSERT(fallback_backend_addresses_ != nullptr);
1703 addresses = grpc_lb_addresses_copy(fallback_backend_addresses_);
1704 }
1705 GPR_ASSERT(addresses != nullptr);
1706 // Replace the LB addresses in the channel args that we pass down to
1707 // the subchannel.
1708 static const char* keys_to_remove[] = {GRPC_ARG_LB_ADDRESSES};
1709 const grpc_arg arg = grpc_lb_addresses_create_channel_arg(addresses);
1710 grpc_channel_args* args = grpc_channel_args_copy_and_add_and_remove(
1711 args_, keys_to_remove, GPR_ARRAY_SIZE(keys_to_remove), &arg, 1);
1712 grpc_lb_addresses_destroy(addresses);
1713 return args;
1714}
1715
1716void GrpcLb::CreateOrUpdateRoundRobinPolicyLocked() {
1717 if (shutting_down_) return;
1718 grpc_channel_args* args = CreateRoundRobinPolicyArgsLocked();
1719 GPR_ASSERT(args != nullptr);
1720 if (rr_policy_ != nullptr) {
1721 if (grpc_lb_glb_trace.enabled()) {
1722 gpr_log(GPR_DEBUG, "[grpclb %p] Updating RR policy %p", this,
1723 rr_policy_.get());
1724 }
1725 rr_policy_->UpdateLocked(*args);
1726 } else {
1727 LoadBalancingPolicy::Args lb_policy_args;
1728 lb_policy_args.combiner = combiner();
1729 lb_policy_args.client_channel_factory = client_channel_factory();
1730 lb_policy_args.args = args;
1731 CreateRoundRobinPolicyLocked(lb_policy_args);
1732 if (grpc_lb_glb_trace.enabled()) {
1733 gpr_log(GPR_DEBUG, "[grpclb %p] Created new RR policy %p", this,
1734 rr_policy_.get());
1735 }
1736 }
1737 grpc_channel_args_destroy(args);
1738}
1739
1740void GrpcLb::OnRoundRobinRequestReresolutionLocked(void* arg,
1741 grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -08001742 GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -08001743 if (grpclb_policy->shutting_down_ || error != GRPC_ERROR_NONE) {
1744 grpclb_policy->Unref(DEBUG_LOCATION, "on_rr_reresolution_requested");
1745 return;
1746 }
1747 if (grpc_lb_glb_trace.enabled()) {
1748 gpr_log(
1749 GPR_DEBUG,
1750 "[grpclb %p] Re-resolution requested from the internal RR policy (%p).",
1751 grpclb_policy, grpclb_policy->rr_policy_.get());
1752 }
1753 // If we are talking to a balancer, we expect to get updated addresses form
1754 // the balancer, so we can ignore the re-resolution request from the RR
1755 // policy. Otherwise, handle the re-resolution request using the
1756 // grpclb policy's original re-resolution closure.
1757 if (grpclb_policy->lb_calld_ == nullptr ||
1758 !grpclb_policy->lb_calld_->seen_initial_response()) {
1759 grpclb_policy->TryReresolutionLocked(&grpc_lb_glb_trace, GRPC_ERROR_NONE);
1760 }
1761 // Give back the wrapper closure to the RR policy.
1762 grpclb_policy->rr_policy_->SetReresolutionClosureLocked(
1763 &grpclb_policy->on_rr_request_reresolution_);
1764}
1765
1766void GrpcLb::UpdateConnectivityStateFromRoundRobinPolicyLocked(
1767 grpc_error* rr_state_error) {
Craig Tiller613dafa2017-02-09 12:00:43 -08001768 const grpc_connectivity_state curr_glb_state =
Mark D. Rothc8875492018-02-20 08:33:48 -08001769 grpc_connectivity_state_check(&state_tracker_);
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001770 /* The new connectivity status is a function of the previous one and the new
1771 * input coming from the status of the RR policy.
1772 *
David Garcia Quintas4283a262016-11-18 10:43:56 -08001773 * current state (grpclb's)
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001774 * |
1775 * v || I | C | R | TF | SD | <- new state (RR's)
1776 * ===++====+=====+=====+======+======+
David Garcia Quintas4283a262016-11-18 10:43:56 -08001777 * I || I | C | R | [I] | [I] |
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001778 * ---++----+-----+-----+------+------+
David Garcia Quintas4283a262016-11-18 10:43:56 -08001779 * C || I | C | R | [C] | [C] |
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001780 * ---++----+-----+-----+------+------+
David Garcia Quintas4283a262016-11-18 10:43:56 -08001781 * R || I | C | R | [R] | [R] |
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001782 * ---++----+-----+-----+------+------+
David Garcia Quintas4283a262016-11-18 10:43:56 -08001783 * TF || I | C | R | [TF] | [TF] |
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001784 * ---++----+-----+-----+------+------+
1785 * SD || NA | NA | NA | NA | NA | (*)
1786 * ---++----+-----+-----+------+------+
1787 *
David Garcia Quintas4283a262016-11-18 10:43:56 -08001788 * A [STATE] indicates that the old RR policy is kept. In those cases, STATE
1789 * is the current state of grpclb, which is left untouched.
1790 *
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001791 * In summary, if the new state is TRANSIENT_FAILURE or SHUTDOWN, stick to
1792 * the previous RR instance.
1793 *
1794 * Note that the status is never updated to SHUTDOWN as a result of calling
1795 * this function. Only glb_shutdown() has the power to set that state.
1796 *
1797 * (*) This function mustn't be called during shutting down. */
1798 GPR_ASSERT(curr_glb_state != GRPC_CHANNEL_SHUTDOWN);
Mark D. Rothc8875492018-02-20 08:33:48 -08001799 switch (rr_connectivity_state_) {
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001800 case GRPC_CHANNEL_TRANSIENT_FAILURE:
1801 case GRPC_CHANNEL_SHUTDOWN:
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001802 GPR_ASSERT(rr_state_error != GRPC_ERROR_NONE);
1803 break;
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001804 case GRPC_CHANNEL_IDLE:
1805 case GRPC_CHANNEL_CONNECTING:
1806 case GRPC_CHANNEL_READY:
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001807 GPR_ASSERT(rr_state_error == GRPC_ERROR_NONE);
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001808 }
Craig Tiller6014e8a2017-10-16 13:50:29 -07001809 if (grpc_lb_glb_trace.enabled()) {
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001810 gpr_log(
David Garcia Quintasa1c65902017-11-09 10:37:35 -08001811 GPR_INFO,
1812 "[grpclb %p] Setting grpclb's state to %s from new RR policy %p state.",
Mark D. Rothc8875492018-02-20 08:33:48 -08001813 this, grpc_connectivity_state_name(rr_connectivity_state_),
1814 rr_policy_.get());
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001815 }
Mark D. Rothc8875492018-02-20 08:33:48 -08001816 grpc_connectivity_state_set(&state_tracker_, rr_connectivity_state_,
1817 rr_state_error,
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001818 "update_lb_connectivity_status_locked");
David Garcia Quintas149f09d2016-11-17 20:43:10 -08001819}
1820
Mark D. Rothc8875492018-02-20 08:33:48 -08001821void GrpcLb::OnRoundRobinConnectivityChangedLocked(void* arg,
1822 grpc_error* error) {
Vijay Pai7fed69b2018-03-05 09:58:05 -08001823 GrpcLb* grpclb_policy = static_cast<GrpcLb*>(arg);
Mark D. Rothc8875492018-02-20 08:33:48 -08001824 if (grpclb_policy->shutting_down_) {
1825 grpclb_policy->Unref(DEBUG_LOCATION, "on_rr_connectivity_changed");
Juanli Shen87c65042018-02-15 09:42:45 -08001826 return;
1827 }
Mark D. Rothc8875492018-02-20 08:33:48 -08001828 grpclb_policy->UpdateConnectivityStateFromRoundRobinPolicyLocked(
1829 GRPC_ERROR_REF(error));
1830 // Resubscribe. Reuse the "on_rr_connectivity_changed" ref.
1831 grpclb_policy->rr_policy_->NotifyOnStateChangeLocked(
1832 &grpclb_policy->rr_connectivity_state_,
1833 &grpclb_policy->on_rr_connectivity_changed_);
Juanli Shen87c65042018-02-15 09:42:45 -08001834}
1835
David Garcia Quintasc22c65b2017-07-25 14:22:20 -07001836//
Mark D. Rothc8875492018-02-20 08:33:48 -08001837// factory
David Garcia Quintasc22c65b2017-07-25 14:22:20 -07001838//
Mark D. Rothc8875492018-02-20 08:33:48 -08001839
1840class GrpcLbFactory : public LoadBalancingPolicyFactory {
1841 public:
1842 OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy(
1843 const LoadBalancingPolicy::Args& args) const override {
1844 /* Count the number of gRPC-LB addresses. There must be at least one. */
1845 const grpc_arg* arg =
1846 grpc_channel_args_find(args.args, GRPC_ARG_LB_ADDRESSES);
1847 if (arg == nullptr || arg->type != GRPC_ARG_POINTER) {
1848 return nullptr;
David Garcia Quintas65318262016-07-29 13:43:38 -07001849 }
Mark D. Rothc8875492018-02-20 08:33:48 -08001850 grpc_lb_addresses* addresses =
Vijay Pai7fed69b2018-03-05 09:58:05 -08001851 static_cast<grpc_lb_addresses*>(arg->value.pointer.p);
Mark D. Rothc8875492018-02-20 08:33:48 -08001852 size_t num_grpclb_addrs = 0;
1853 for (size_t i = 0; i < addresses->num_addresses; ++i) {
1854 if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs;
David Garcia Quintas65318262016-07-29 13:43:38 -07001855 }
Mark D. Rothc8875492018-02-20 08:33:48 -08001856 if (num_grpclb_addrs == 0) return nullptr;
1857 return OrphanablePtr<LoadBalancingPolicy>(New<GrpcLb>(addresses, args));
David Garcia Quintas65318262016-07-29 13:43:38 -07001858 }
David Garcia Quintas8d489112016-07-29 15:20:42 -07001859
Mark D. Rothc8875492018-02-20 08:33:48 -08001860 const char* name() const override { return "grpclb"; }
1861};
David Garcia Quintas8d489112016-07-29 15:20:42 -07001862
Mark D. Rothc8875492018-02-20 08:33:48 -08001863} // namespace
David Garcia Quintas8d489112016-07-29 15:20:42 -07001864
Mark D. Rothc8875492018-02-20 08:33:48 -08001865} // namespace grpc_core
David Garcia Quintas65318262016-07-29 13:43:38 -07001866
Mark D. Rothc8875492018-02-20 08:33:48 -08001867//
1868// Plugin registration
1869//
Mark D. Rotha4792f52017-09-26 09:06:35 -07001870
Mark D. Rothc8875492018-02-20 08:33:48 -08001871namespace {
Mark D. Roth09e458c2017-05-02 08:13:26 -07001872
1873// Only add client_load_reporting filter if the grpclb LB policy is used.
Mark D. Rothc8875492018-02-20 08:33:48 -08001874bool maybe_add_client_load_reporting_filter(grpc_channel_stack_builder* builder,
1875 void* arg) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001876 const grpc_channel_args* args =
Mark D. Roth09e458c2017-05-02 08:13:26 -07001877 grpc_channel_stack_builder_get_channel_arguments(builder);
Craig Tillerbaa14a92017-11-03 09:09:36 -07001878 const grpc_arg* channel_arg =
Mark D. Roth09e458c2017-05-02 08:13:26 -07001879 grpc_channel_args_find(args, GRPC_ARG_LB_POLICY_NAME);
Noah Eisen882dfed2017-11-14 14:58:20 -08001880 if (channel_arg != nullptr && channel_arg->type == GRPC_ARG_STRING &&
Mark D. Roth09e458c2017-05-02 08:13:26 -07001881 strcmp(channel_arg->value.string, "grpclb") == 0) {
1882 return grpc_channel_stack_builder_append_filter(
Mark D. Rothc8875492018-02-20 08:33:48 -08001883 builder, (const grpc_channel_filter*)arg, nullptr, nullptr);
Mark D. Roth09e458c2017-05-02 08:13:26 -07001884 }
1885 return true;
1886}
1887
Mark D. Rothc8875492018-02-20 08:33:48 -08001888} // namespace
1889
ncteisenadbfbd52017-11-16 15:35:45 -08001890void grpc_lb_policy_grpclb_init() {
Mark D. Rothc8875492018-02-20 08:33:48 -08001891 grpc_core::LoadBalancingPolicyRegistry::Builder::
1892 RegisterLoadBalancingPolicyFactory(
1893 grpc_core::UniquePtr<grpc_core::LoadBalancingPolicyFactory>(
1894 grpc_core::New<grpc_core::GrpcLbFactory>()));
Mark D. Roth09e458c2017-05-02 08:13:26 -07001895 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
1896 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
1897 maybe_add_client_load_reporting_filter,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001898 (void*)&grpc_client_load_reporting_filter);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -07001899}
1900
ncteisenadbfbd52017-11-16 15:35:45 -08001901void grpc_lb_policy_grpclb_shutdown() {}