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 | |
David Garcia Quintas | 8b3b97f | 2016-07-15 07:46:47 -0700 | [diff] [blame] | 19 | /** Implementation of the gRPC LB policy. |
| 20 | * |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 21 | * This policy takes as input a set of resolved addresses {a1..an} for which the |
| 22 | * LB set was set (it's the resolver's responsibility to ensure this). That is |
| 23 | * to say, {a1..an} represent a collection of LB servers. |
| 24 | * |
| 25 | * An internal channel (\a glb_lb_policy.lb_channel) is created over {a1..an}. |
| 26 | * This channel behaves just like a regular channel. In particular, the |
| 27 | * constructed URI over the addresses a1..an will use the default pick first |
| 28 | * policy to select from this list of LB server backends. |
| 29 | * |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 30 | * The first time the policy gets a request for a pick, a ping, or to exit the |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 31 | * idle state, \a query_for_backends_locked() is called. This function sets up |
| 32 | * and initiates the internal communication with the LB server. In particular, |
| 33 | * it's responsible for instantiating the internal *streaming* call to the LB |
| 34 | * server (whichever address from {a1..an} pick-first chose). This call is |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 35 | * serviced by two callbacks, \a lb_on_server_status_received and \a |
| 36 | * lb_on_response_received. The former will be called when the call to the LB |
| 37 | * server completes. This can happen if the LB server closes the connection or |
| 38 | * if this policy itself cancels the call (for example because it's shutting |
David Garcia Quintas | 246c564 | 2016-11-01 11:16:52 -0700 | [diff] [blame] | 39 | * down). If the internal call times out, the usual behavior of pick-first |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 40 | * applies, continuing to pick from the list {a1..an}. |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 41 | * |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 42 | * Upon sucesss, the incoming \a LoadBalancingResponse is processed by \a |
| 43 | * res_recv. An invalid one results in the termination of the streaming call. A |
| 44 | * new streaming call should be created if possible, failing the original call |
| 45 | * otherwise. For a valid \a LoadBalancingResponse, the server list of actual |
| 46 | * backends is extracted. A Round Robin policy will be created from this list. |
| 47 | * There are two possible scenarios: |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 48 | * |
| 49 | * 1. This is the first server list received. There was no previous instance of |
David Garcia Quintas | 90712d5 | 2016-10-13 19:33:04 -0700 | [diff] [blame] | 50 | * the Round Robin policy. \a rr_handover_locked() will instantiate the RR |
| 51 | * policy and perform all the pending operations over it. |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 52 | * 2. There's already a RR policy instance active. We need to introduce the new |
| 53 | * one build from the new serverlist, but taking care not to disrupt the |
| 54 | * operations in progress over the old RR instance. This is done by |
| 55 | * decreasing the reference count on the old policy. The moment no more |
| 56 | * references are held on the old RR policy, it'll be destroyed and \a |
David Garcia Quintas | 348cfdb | 2016-08-19 12:19:43 -0700 | [diff] [blame] | 57 | * glb_rr_connectivity_changed notified with a \a GRPC_CHANNEL_SHUTDOWN |
| 58 | * state. At this point we can transition to a new RR instance safely, which |
David Garcia Quintas | 90712d5 | 2016-10-13 19:33:04 -0700 | [diff] [blame] | 59 | * is done once again via \a rr_handover_locked(). |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 60 | * |
| 61 | * |
| 62 | * Once a RR policy instance is in place (and getting updated as described), |
| 63 | * calls to for a pick, a ping or a cancellation will be serviced right away by |
| 64 | * forwarding them to the RR instance. Any time there's no RR policy available |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 65 | * (ie, right after the creation of the gRPCLB policy, if an empty serverlist is |
| 66 | * received, etc), pick/ping requests are added to a list of pending picks/pings |
| 67 | * to be flushed and serviced as part of \a rr_handover_locked() the moment the |
| 68 | * RR policy instance becomes available. |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 69 | * |
| 70 | * \see https://github.com/grpc/grpc/blob/master/doc/load-balancing.md for the |
| 71 | * high level design and details. */ |
David Garcia Quintas | 8b3b97f | 2016-07-15 07:46:47 -0700 | [diff] [blame] | 72 | |
| 73 | /* TODO(dgq): |
| 74 | * - Implement LB service forwarding (point 2c. in the doc's diagram). |
| 75 | */ |
| 76 | |
murgatroid99 | 085f9af | 2016-10-24 09:55:44 -0700 | [diff] [blame] | 77 | /* With the addition of a libuv endpoint, sockaddr.h now includes uv.h when |
| 78 | using that endpoint. Because of various transitive includes in uv.h, |
| 79 | including windows.h on Windows, uv.h must be included before other system |
| 80 | headers. Therefore, sockaddr.h must always be included first */ |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 81 | #include "src/core/lib/iomgr/sockaddr.h" |
| 82 | |
Yash Tibrewal | fcd26bc | 2017-09-25 15:08:28 -0700 | [diff] [blame] | 83 | #include <inttypes.h> |
Mark D. Roth | 64d922a | 2017-05-03 12:52:04 -0700 | [diff] [blame] | 84 | #include <limits.h> |
David Garcia Quintas | 22e8f1d | 2016-06-15 23:53:00 -0700 | [diff] [blame] | 85 | #include <string.h> |
| 86 | |
| 87 | #include <grpc/byte_buffer_reader.h> |
| 88 | #include <grpc/grpc.h> |
| 89 | #include <grpc/support/alloc.h> |
| 90 | #include <grpc/support/host_port.h> |
| 91 | #include <grpc/support/string_util.h> |
David Garcia Quintas | 6909922 | 2016-10-03 11:28:37 -0700 | [diff] [blame] | 92 | #include <grpc/support/time.h> |
David Garcia Quintas | 22e8f1d | 2016-06-15 23:53:00 -0700 | [diff] [blame] | 93 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 94 | #include "src/core/ext/filters/client_channel/client_channel.h" |
| 95 | #include "src/core/ext/filters/client_channel/client_channel_factory.h" |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 96 | #include "src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 97 | #include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h" |
| 98 | #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] | 99 | #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] | 100 | #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] | 101 | #include "src/core/ext/filters/client_channel/lb_policy_factory.h" |
| 102 | #include "src/core/ext/filters/client_channel/lb_policy_registry.h" |
| 103 | #include "src/core/ext/filters/client_channel/parse_address.h" |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 104 | #include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h" |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 105 | #include "src/core/ext/filters/client_channel/subchannel_index.h" |
Craig Tiller | c0df1c0 | 2017-07-17 16:12:33 -0700 | [diff] [blame] | 106 | #include "src/core/lib/backoff/backoff.h" |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 107 | #include "src/core/lib/channel/channel_args.h" |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 108 | #include "src/core/lib/channel/channel_stack.h" |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 109 | #include "src/core/lib/iomgr/combiner.h" |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 110 | #include "src/core/lib/iomgr/sockaddr.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 111 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 112 | #include "src/core/lib/iomgr/timer.h" |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 113 | #include "src/core/lib/slice/slice_hash_table.h" |
Craig Tiller | 18b4ba3 | 2016-11-09 15:23:42 -0800 | [diff] [blame] | 114 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 115 | #include "src/core/lib/slice/slice_string_helpers.h" |
David Garcia Quintas | d27e242 | 2017-11-27 12:53:14 -0800 | [diff] [blame] | 116 | #include "src/core/lib/support/manual_constructor.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 117 | #include "src/core/lib/support/string.h" |
| 118 | #include "src/core/lib/surface/call.h" |
| 119 | #include "src/core/lib/surface/channel.h" |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 120 | #include "src/core/lib/surface/channel_init.h" |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 121 | #include "src/core/lib/transport/static_metadata.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 122 | |
David Garcia Quintas | 1edfb95 | 2016-11-22 17:15:34 -0800 | [diff] [blame] | 123 | #define GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS 1 |
| 124 | #define GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER 1.6 |
| 125 | #define GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS 120 |
| 126 | #define GRPC_GRPCLB_RECONNECT_JITTER 0.2 |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 127 | #define GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS 10000 |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 128 | |
Craig Tiller | 694580f | 2017-10-18 14:48:14 -0700 | [diff] [blame] | 129 | grpc_core::TraceFlag grpc_lb_glb_trace(false, "glb"); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 130 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 131 | /* add lb_token of selected subchannel (address) to the call's initial |
| 132 | * metadata */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 133 | static grpc_error* initial_metadata_add_lb_token( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 134 | grpc_metadata_batch* initial_metadata, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 135 | grpc_linked_mdelem* lb_token_mdelem_storage, grpc_mdelem lb_token) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 136 | GPR_ASSERT(lb_token_mdelem_storage != nullptr); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 137 | GPR_ASSERT(!GRPC_MDISNULL(lb_token)); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 138 | return grpc_metadata_batch_add_tail(initial_metadata, lb_token_mdelem_storage, |
| 139 | lb_token); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 142 | static void destroy_client_stats(void* arg) { |
| 143 | grpc_grpclb_client_stats_unref((grpc_grpclb_client_stats*)arg); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 144 | } |
| 145 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 146 | typedef struct wrapped_rr_closure_arg { |
David Garcia Quintas | 97ba642 | 2016-10-14 13:06:45 -0700 | [diff] [blame] | 147 | /* the closure instance using this struct as argument */ |
| 148 | grpc_closure wrapper_closure; |
| 149 | |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 150 | /* the original closure. Usually a on_complete/notify cb for pick() and ping() |
| 151 | * calls against the internal RR instance, respectively. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 152 | grpc_closure* wrapped_closure; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 153 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 154 | /* the pick's initial metadata, kept in order to append the LB token for the |
| 155 | * pick */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 156 | grpc_metadata_batch* initial_metadata; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 157 | |
| 158 | /* the picked target, used to determine which LB token to add to the pick's |
| 159 | * initial metadata */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 160 | grpc_connected_subchannel** target; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 161 | |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 162 | /* the context to be populated for the subchannel call */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 163 | grpc_call_context_element* context; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 164 | |
| 165 | /* Stats for client-side load reporting. Note that this holds a |
| 166 | * reference, which must be either passed on via context or unreffed. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 167 | grpc_grpclb_client_stats* client_stats; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 168 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 169 | /* the LB token associated with the pick */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 170 | grpc_mdelem lb_token; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 171 | |
| 172 | /* storage for the lb token initial metadata mdelem */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 173 | grpc_linked_mdelem* lb_token_mdelem_storage; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 174 | |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 175 | /* The RR instance related to the closure */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 176 | grpc_lb_policy* rr_policy; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 177 | |
David Garcia Quintas | 2b372e0 | 2017-11-09 14:15:59 -0800 | [diff] [blame] | 178 | /* The grpclb instance that created the wrapping. This instance is not owned, |
David Garcia Quintas | 5960790 | 2017-11-09 14:39:59 -0800 | [diff] [blame] | 179 | * reference counts are untouched. It's used only for logging purposes. */ |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 180 | grpc_lb_policy* glb_policy; |
| 181 | |
David Garcia Quintas | 97ba642 | 2016-10-14 13:06:45 -0700 | [diff] [blame] | 182 | /* heap memory to be freed upon closure execution. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 183 | void* free_when_done; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 184 | } wrapped_rr_closure_arg; |
| 185 | |
| 186 | /* The \a on_complete closure passed as part of the pick requires keeping a |
| 187 | * reference to its associated round robin instance. We wrap this closure in |
| 188 | * order to unref the round robin instance upon its invocation */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 189 | static void wrapped_rr_closure(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 190 | wrapped_rr_closure_arg* wc_arg = (wrapped_rr_closure_arg*)arg; |
David Garcia Quintas | 5bb7b9c | 2016-09-15 23:46:32 -0700 | [diff] [blame] | 191 | |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 192 | GPR_ASSERT(wc_arg->wrapped_closure != nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 193 | GRPC_CLOSURE_SCHED(wc_arg->wrapped_closure, GRPC_ERROR_REF(error)); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 194 | |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 195 | if (wc_arg->rr_policy != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 196 | /* if *target is nullptr, no pick has been made by the RR policy (eg, all |
David Garcia Quintas | 5bb7b9c | 2016-09-15 23:46:32 -0700 | [diff] [blame] | 197 | * addresses failed to connect). There won't be any user_data/token |
| 198 | * available */ |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 199 | if (*wc_arg->target != nullptr) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 200 | if (!GRPC_MDISNULL(wc_arg->lb_token)) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 201 | initial_metadata_add_lb_token(wc_arg->initial_metadata, |
David Garcia Quintas | 850cbaa | 2016-11-15 15:13:35 -0800 | [diff] [blame] | 202 | wc_arg->lb_token_mdelem_storage, |
| 203 | GRPC_MDELEM_REF(wc_arg->lb_token)); |
| 204 | } else { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 205 | gpr_log( |
| 206 | GPR_ERROR, |
| 207 | "[grpclb %p] No LB token for connected subchannel pick %p (from RR " |
| 208 | "instance %p).", |
| 209 | wc_arg->glb_policy, *wc_arg->target, wc_arg->rr_policy); |
David Garcia Quintas | 850cbaa | 2016-11-15 15:13:35 -0800 | [diff] [blame] | 210 | abort(); |
| 211 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 212 | // Pass on client stats via context. Passes ownership of the reference. |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 213 | GPR_ASSERT(wc_arg->client_stats != nullptr); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 214 | wc_arg->context[GRPC_GRPCLB_CLIENT_STATS].value = wc_arg->client_stats; |
| 215 | wc_arg->context[GRPC_GRPCLB_CLIENT_STATS].destroy = destroy_client_stats; |
| 216 | } else { |
| 217 | grpc_grpclb_client_stats_unref(wc_arg->client_stats); |
David Garcia Quintas | 5bb7b9c | 2016-09-15 23:46:32 -0700 | [diff] [blame] | 218 | } |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 219 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 220 | gpr_log(GPR_INFO, "[grpclb %p] Unreffing RR %p", wc_arg->glb_policy, |
| 221 | wc_arg->rr_policy); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 222 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 223 | GRPC_LB_POLICY_UNREF(wc_arg->rr_policy, "wrapped_rr_closure"); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 224 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 225 | GPR_ASSERT(wc_arg->free_when_done != nullptr); |
David Garcia Quintas | 97ba642 | 2016-10-14 13:06:45 -0700 | [diff] [blame] | 226 | gpr_free(wc_arg->free_when_done); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Vijay Pai | 849bd73 | 2018-01-02 23:30:47 +0000 | [diff] [blame^] | 229 | namespace { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 230 | /* Linked list of pending pick requests. It stores all information needed to |
| 231 | * eventually call (Round Robin's) pick() on them. They mainly stay pending |
| 232 | * waiting for the RR policy to be created/updated. |
| 233 | * |
| 234 | * One particularity is the wrapping of the user-provided \a on_complete closure |
| 235 | * (in \a wrapped_on_complete and \a wrapped_on_complete_arg). This is needed in |
| 236 | * order to correctly unref the RR policy instance upon completion of the pick. |
| 237 | * See \a wrapped_rr_closure for details. */ |
Vijay Pai | 849bd73 | 2018-01-02 23:30:47 +0000 | [diff] [blame^] | 238 | struct pending_pick { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 239 | struct pending_pick* next; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 240 | |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 241 | /* original pick()'s arguments */ |
| 242 | grpc_lb_policy_pick_args pick_args; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 243 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 244 | /* output argument where to store the pick()ed connected subchannel, or |
| 245 | * nullptr upon error. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 246 | grpc_connected_subchannel** target; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 247 | |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 248 | /* args for wrapped_on_complete */ |
| 249 | wrapped_rr_closure_arg wrapped_on_complete_arg; |
Vijay Pai | 849bd73 | 2018-01-02 23:30:47 +0000 | [diff] [blame^] | 250 | }; |
| 251 | } // namespace |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 252 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 253 | static void add_pending_pick(pending_pick** root, |
| 254 | const grpc_lb_policy_pick_args* pick_args, |
| 255 | grpc_connected_subchannel** target, |
| 256 | grpc_call_context_element* context, |
| 257 | grpc_closure* on_complete) { |
| 258 | pending_pick* pp = (pending_pick*)gpr_zalloc(sizeof(*pp)); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 259 | pp->next = *root; |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 260 | pp->pick_args = *pick_args; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 261 | pp->target = target; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 262 | pp->wrapped_on_complete_arg.wrapped_closure = on_complete; |
David Garcia Quintas | 5bb7b9c | 2016-09-15 23:46:32 -0700 | [diff] [blame] | 263 | pp->wrapped_on_complete_arg.target = target; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 264 | pp->wrapped_on_complete_arg.context = context; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 265 | pp->wrapped_on_complete_arg.initial_metadata = pick_args->initial_metadata; |
| 266 | pp->wrapped_on_complete_arg.lb_token_mdelem_storage = |
| 267 | pick_args->lb_token_mdelem_storage; |
David Garcia Quintas | b39330d | 2016-10-14 13:35:56 -0700 | [diff] [blame] | 268 | pp->wrapped_on_complete_arg.free_when_done = pp; |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 269 | GRPC_CLOSURE_INIT(&pp->wrapped_on_complete_arg.wrapper_closure, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 270 | wrapped_rr_closure, &pp->wrapped_on_complete_arg, |
| 271 | grpc_schedule_on_exec_ctx); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 272 | *root = pp; |
| 273 | } |
| 274 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 275 | /* Same as the \a pending_pick struct but for ping operations */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 276 | typedef struct pending_ping { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 277 | struct pending_ping* next; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 278 | |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 279 | /* args for sending the ping */ |
Yuchen Zeng | 625a5c0 | 2017-12-06 13:24:27 -0800 | [diff] [blame] | 280 | wrapped_rr_closure_arg* on_initiate; |
| 281 | wrapped_rr_closure_arg* on_ack; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 282 | } pending_ping; |
| 283 | |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 284 | static void add_pending_ping(pending_ping** root, grpc_closure* on_initiate, |
| 285 | grpc_closure* on_ack) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 286 | pending_ping* pping = (pending_ping*)gpr_zalloc(sizeof(*pping)); |
Yuchen Zeng | 625a5c0 | 2017-12-06 13:24:27 -0800 | [diff] [blame] | 287 | if (on_initiate != nullptr) { |
| 288 | pping->on_initiate = |
| 289 | (wrapped_rr_closure_arg*)gpr_zalloc(sizeof(*pping->on_initiate)); |
| 290 | pping->on_initiate->wrapped_closure = on_initiate; |
| 291 | pping->on_initiate->free_when_done = pping->on_initiate; |
| 292 | GRPC_CLOSURE_INIT(&pping->on_initiate->wrapper_closure, wrapped_rr_closure, |
| 293 | &pping->on_initiate, grpc_schedule_on_exec_ctx); |
| 294 | } |
| 295 | if (on_ack != nullptr) { |
| 296 | pping->on_ack = (wrapped_rr_closure_arg*)gpr_zalloc(sizeof(*pping->on_ack)); |
| 297 | pping->on_ack->wrapped_closure = on_ack; |
| 298 | pping->on_ack->free_when_done = pping->on_ack; |
| 299 | GRPC_CLOSURE_INIT(&pping->on_ack->wrapper_closure, wrapped_rr_closure, |
| 300 | &pping->on_ack, grpc_schedule_on_exec_ctx); |
| 301 | } |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 302 | pping->next = *root; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 303 | *root = pping; |
| 304 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 305 | |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 306 | /* |
| 307 | * glb_lb_policy |
| 308 | */ |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 309 | typedef struct rr_connectivity_data rr_connectivity_data; |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 310 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 311 | typedef struct glb_lb_policy { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 312 | /** base policy: must be first */ |
| 313 | grpc_lb_policy base; |
| 314 | |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 315 | /** who the client is trying to communicate with */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 316 | const char* server_name; |
| 317 | grpc_client_channel_factory* cc_factory; |
| 318 | grpc_channel_args* args; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 319 | |
Mark D. Roth | 64d922a | 2017-05-03 12:52:04 -0700 | [diff] [blame] | 320 | /** timeout in milliseconds for the LB call. 0 means no deadline. */ |
| 321 | int lb_call_timeout_ms; |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 322 | |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 323 | /** timeout in milliseconds for before using fallback backend addresses. |
| 324 | * 0 means not using fallback. */ |
| 325 | int lb_fallback_timeout_ms; |
| 326 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 327 | /** for communicating with the LB server */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 328 | grpc_channel* lb_channel; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 329 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 330 | /** response generator to inject address updates into \a lb_channel */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 331 | grpc_fake_resolver_response_generator* response_generator; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 332 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 333 | /** the RR policy to use of the backend servers returned by the LB server */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 334 | grpc_lb_policy* rr_policy; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 335 | |
| 336 | bool started_picking; |
| 337 | |
| 338 | /** our connectivity state tracker */ |
| 339 | grpc_connectivity_state_tracker state_tracker; |
| 340 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 341 | /** connectivity state of the LB channel */ |
| 342 | grpc_connectivity_state lb_channel_connectivity; |
| 343 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 344 | /** stores the deserialized response from the LB. May be nullptr until one |
| 345 | * such response has arrived. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 346 | grpc_grpclb_serverlist* serverlist; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 347 | |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 348 | /** Index into serverlist for next pick. |
| 349 | * If the server at this index is a drop, we return a drop. |
| 350 | * Otherwise, we delegate to the RR policy. */ |
| 351 | size_t serverlist_index; |
| 352 | |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 353 | /** stores the backend addresses from the resolver */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 354 | grpc_lb_addresses* fallback_backend_addresses; |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 355 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 356 | /** list of picks that are waiting on RR's policy connectivity */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 357 | pending_pick* pending_picks; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 358 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 359 | /** list of pings that are waiting on RR's policy connectivity */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 360 | pending_ping* pending_pings; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 361 | |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 362 | bool shutting_down; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 363 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 364 | /** are we currently updating lb_call? */ |
| 365 | bool updating_lb_call; |
| 366 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 367 | /** are we already watching the LB channel's connectivity? */ |
| 368 | bool watching_lb_channel; |
| 369 | |
| 370 | /** is \a lb_call_retry_timer active? */ |
| 371 | bool retry_timer_active; |
| 372 | |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 373 | /** is \a lb_fallback_timer active? */ |
| 374 | bool fallback_timer_active; |
| 375 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 376 | /** called upon changes to the LB channel's connectivity. */ |
| 377 | grpc_closure lb_channel_on_connectivity_changed; |
| 378 | |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 379 | /************************************************************/ |
| 380 | /* client data associated with the LB server communication */ |
| 381 | /************************************************************/ |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 382 | /* Status from the LB server has been received. This signals the end of the LB |
| 383 | * call. */ |
| 384 | grpc_closure lb_on_server_status_received; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 385 | |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 386 | /* A response from the LB server has been received. Process it */ |
| 387 | grpc_closure lb_on_response_received; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 388 | |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 389 | /* LB call retry timer callback. */ |
| 390 | grpc_closure lb_on_call_retry; |
| 391 | |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 392 | /* LB fallback timer callback. */ |
| 393 | grpc_closure lb_on_fallback; |
| 394 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 395 | grpc_call* lb_call; /* streaming call to the LB server, */ |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 396 | |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 397 | grpc_metadata_array lb_initial_metadata_recv; /* initial MD from LB server */ |
| 398 | grpc_metadata_array |
| 399 | lb_trailing_metadata_recv; /* trailing MD from LB server */ |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 400 | |
| 401 | /* what's being sent to the LB server. Note that its value may vary if the LB |
| 402 | * server indicates a redirect. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 403 | grpc_byte_buffer* lb_request_payload; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 404 | |
David Garcia Quintas | 246c564 | 2016-11-01 11:16:52 -0700 | [diff] [blame] | 405 | /* response the LB server, if any. Processed in lb_on_response_received() */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 406 | grpc_byte_buffer* lb_response_payload; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 407 | |
David Garcia Quintas | 246c564 | 2016-11-01 11:16:52 -0700 | [diff] [blame] | 408 | /* call status code and details, set in lb_on_server_status_received() */ |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 409 | grpc_status_code lb_call_status; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 410 | grpc_slice lb_call_status_details; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 411 | |
| 412 | /** LB call retry backoff state */ |
David Garcia Quintas | 0f91e51 | 2017-12-04 16:12:54 -0800 | [diff] [blame] | 413 | grpc_core::ManualConstructor<grpc_core::BackOff> lb_call_backoff; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 414 | |
| 415 | /** LB call retry timer */ |
| 416 | grpc_timer lb_call_retry_timer; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 417 | |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 418 | /** LB fallback timer */ |
| 419 | grpc_timer lb_fallback_timer; |
| 420 | |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 421 | bool seen_initial_response; |
| 422 | |
| 423 | /* Stats for client-side load reporting. Should be unreffed and |
| 424 | * recreated whenever lb_call is replaced. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 425 | grpc_grpclb_client_stats* client_stats; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 426 | /* Interval and timer for next client load report. */ |
Craig Tiller | c0df1c0 | 2017-07-17 16:12:33 -0700 | [diff] [blame] | 427 | grpc_millis client_stats_report_interval; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 428 | grpc_timer client_load_report_timer; |
| 429 | bool client_load_report_timer_pending; |
| 430 | bool last_client_load_report_counters_were_zero; |
| 431 | /* Closure used for either the load report timer or the callback for |
| 432 | * completion of sending the load report. */ |
| 433 | grpc_closure client_load_report_closure; |
| 434 | /* Client load report message payload. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 435 | grpc_byte_buffer* client_load_report_payload; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 436 | } glb_lb_policy; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 437 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 438 | /* Keeps track and reacts to changes in connectivity of the RR instance */ |
| 439 | struct rr_connectivity_data { |
| 440 | grpc_closure on_change; |
| 441 | grpc_connectivity_state state; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 442 | glb_lb_policy* glb_policy; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 443 | }; |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 444 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 445 | static bool is_server_valid(const grpc_grpclb_server* server, size_t idx, |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 446 | bool log) { |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 447 | if (server->drop) return false; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 448 | const grpc_grpclb_ip_address* ip = &server->ip_address; |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 449 | if (server->port >> 16 != 0) { |
| 450 | if (log) { |
| 451 | gpr_log(GPR_ERROR, |
Jan Tattermusch | 2b39808 | 2016-10-07 14:40:30 +0200 | [diff] [blame] | 452 | "Invalid port '%d' at index %lu of serverlist. Ignoring.", |
| 453 | server->port, (unsigned long)idx); |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 454 | } |
| 455 | return false; |
| 456 | } |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 457 | if (ip->size != 4 && ip->size != 16) { |
| 458 | if (log) { |
| 459 | gpr_log(GPR_ERROR, |
Jan Tattermusch | 2b39808 | 2016-10-07 14:40:30 +0200 | [diff] [blame] | 460 | "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] | 461 | "serverlist. Ignoring", |
Jan Tattermusch | 2b39808 | 2016-10-07 14:40:30 +0200 | [diff] [blame] | 462 | ip->size, (unsigned long)idx); |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 469 | /* vtable for LB tokens in grpc_lb_addresses. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 470 | static void* lb_token_copy(void* token) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 471 | return token == nullptr |
| 472 | ? nullptr |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 473 | : (void*)GRPC_MDELEM_REF(grpc_mdelem{(uintptr_t)token}).payload; |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 474 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 475 | static void lb_token_destroy(void* token) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 476 | if (token != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 477 | GRPC_MDELEM_UNREF(grpc_mdelem{(uintptr_t)token}); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 478 | } |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 479 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 480 | static int lb_token_cmp(void* token1, void* token2) { |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 481 | if (token1 > token2) return 1; |
| 482 | if (token1 < token2) return -1; |
| 483 | return 0; |
| 484 | } |
| 485 | static const grpc_lb_user_data_vtable lb_token_vtable = { |
| 486 | lb_token_copy, lb_token_destroy, lb_token_cmp}; |
| 487 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 488 | static void parse_server(const grpc_grpclb_server* server, |
| 489 | grpc_resolved_address* addr) { |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 490 | memset(addr, 0, sizeof(*addr)); |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 491 | if (server->drop) return; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 492 | const uint16_t netorder_port = htons((uint16_t)server->port); |
| 493 | /* the addresses are given in binary format (a in(6)_addr struct) in |
| 494 | * server->ip_address.bytes. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 495 | const grpc_grpclb_ip_address* ip = &server->ip_address; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 496 | if (ip->size == 4) { |
| 497 | addr->len = sizeof(struct sockaddr_in); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 498 | struct sockaddr_in* addr4 = (struct sockaddr_in*)&addr->addr; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 499 | addr4->sin_family = AF_INET; |
| 500 | memcpy(&addr4->sin_addr, ip->bytes, ip->size); |
| 501 | addr4->sin_port = netorder_port; |
| 502 | } else if (ip->size == 16) { |
| 503 | addr->len = sizeof(struct sockaddr_in6); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 504 | struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&addr->addr; |
David Garcia Quintas | 107ca16 | 2016-11-02 18:17:03 -0700 | [diff] [blame] | 505 | addr6->sin6_family = AF_INET6; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 506 | memcpy(&addr6->sin6_addr, ip->bytes, ip->size); |
| 507 | addr6->sin6_port = netorder_port; |
| 508 | } |
| 509 | } |
| 510 | |
Mark D. Roth | 7ce14d2 | 2016-09-16 13:03:46 -0700 | [diff] [blame] | 511 | /* Returns addresses extracted from \a serverlist. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 512 | static grpc_lb_addresses* process_serverlist_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 513 | const grpc_grpclb_serverlist* serverlist) { |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 514 | size_t num_valid = 0; |
| 515 | /* first pass: count how many are valid in order to allocate the necessary |
| 516 | * memory in a single block */ |
| 517 | for (size_t i = 0; i < serverlist->num_servers; ++i) { |
David Garcia Quintas | 35c2aba | 2016-09-13 15:28:09 -0700 | [diff] [blame] | 518 | if (is_server_valid(serverlist->servers[i], i, true)) ++num_valid; |
David Garcia Quintas | b8b384a | 2016-08-23 21:10:29 -0700 | [diff] [blame] | 519 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 520 | grpc_lb_addresses* lb_addresses = |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 521 | grpc_lb_addresses_create(num_valid, &lb_token_vtable); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 522 | /* 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] | 523 | * to the outside world) to be read by the RR policy during its creation. |
| 524 | * Given that the validity tests are very cheap, they are performed again |
| 525 | * instead of marking the valid ones during the first pass, as this would |
| 526 | * incurr in an allocation due to the arbitrary number of server */ |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 527 | size_t addr_idx = 0; |
| 528 | 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] | 529 | const grpc_grpclb_server* server = serverlist->servers[sl_idx]; |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 530 | if (!is_server_valid(serverlist->servers[sl_idx], sl_idx, false)) continue; |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 531 | GPR_ASSERT(addr_idx < num_valid); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 532 | /* address processing */ |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 533 | grpc_resolved_address addr; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 534 | parse_server(server, &addr); |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 535 | /* lb token processing */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 536 | void* user_data; |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 537 | if (server->has_load_balance_token) { |
David Garcia Quintas | 0baf1dc | 2016-10-28 04:44:01 +0200 | [diff] [blame] | 538 | const size_t lb_token_max_length = |
| 539 | GPR_ARRAY_SIZE(server->load_balance_token); |
| 540 | const size_t lb_token_length = |
| 541 | strnlen(server->load_balance_token, lb_token_max_length); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 542 | grpc_slice lb_token_mdstr = grpc_slice_from_copied_buffer( |
| 543 | server->load_balance_token, lb_token_length); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 544 | user_data = |
| 545 | (void*)grpc_mdelem_from_slices(GRPC_MDSTR_LB_TOKEN, lb_token_mdstr) |
| 546 | .payload; |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 547 | } else { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 548 | char* uri = grpc_sockaddr_to_uri(&addr); |
David Garcia Quintas | 850cbaa | 2016-11-15 15:13:35 -0800 | [diff] [blame] | 549 | gpr_log(GPR_INFO, |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 550 | "Missing LB token for backend address '%s'. The empty token will " |
| 551 | "be used instead", |
David Garcia Quintas | 850cbaa | 2016-11-15 15:13:35 -0800 | [diff] [blame] | 552 | uri); |
| 553 | gpr_free(uri); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 554 | user_data = (void*)GRPC_MDELEM_LB_TOKEN_EMPTY.payload; |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 555 | } |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 556 | |
Mark D. Roth | 64f1f8d | 2016-09-16 09:00:09 -0700 | [diff] [blame] | 557 | grpc_lb_addresses_set_address(lb_addresses, addr_idx, &addr.addr, addr.len, |
| 558 | false /* is_balancer */, |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 559 | nullptr /* balancer_name */, user_data); |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 560 | ++addr_idx; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 561 | } |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 562 | GPR_ASSERT(addr_idx == num_valid); |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 563 | return lb_addresses; |
| 564 | } |
| 565 | |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 566 | /* Returns the backend addresses extracted from the given addresses */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 567 | static grpc_lb_addresses* extract_backend_addresses_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 568 | const grpc_lb_addresses* addresses) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 569 | /* first pass: count the number of backend addresses */ |
| 570 | size_t num_backends = 0; |
| 571 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 572 | if (!addresses->addresses[i].is_balancer) { |
| 573 | ++num_backends; |
| 574 | } |
| 575 | } |
| 576 | /* second pass: actually populate the addresses and (empty) LB tokens */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 577 | grpc_lb_addresses* backend_addresses = |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 578 | grpc_lb_addresses_create(num_backends, &lb_token_vtable); |
| 579 | size_t num_copied = 0; |
| 580 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 581 | if (addresses->addresses[i].is_balancer) continue; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 582 | const grpc_resolved_address* addr = &addresses->addresses[i].address; |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 583 | grpc_lb_addresses_set_address(backend_addresses, num_copied, &addr->addr, |
| 584 | addr->len, false /* is_balancer */, |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 585 | nullptr /* balancer_name */, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 586 | (void*)GRPC_MDELEM_LB_TOKEN_EMPTY.payload); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 587 | ++num_copied; |
| 588 | } |
| 589 | return backend_addresses; |
| 590 | } |
| 591 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 592 | static void update_lb_connectivity_status_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 593 | glb_lb_policy* glb_policy, grpc_connectivity_state rr_state, |
| 594 | grpc_error* rr_state_error) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 595 | const grpc_connectivity_state curr_glb_state = |
| 596 | grpc_connectivity_state_check(&glb_policy->state_tracker); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 597 | |
| 598 | /* The new connectivity status is a function of the previous one and the new |
| 599 | * input coming from the status of the RR policy. |
| 600 | * |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 601 | * current state (grpclb's) |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 602 | * | |
| 603 | * v || I | C | R | TF | SD | <- new state (RR's) |
| 604 | * ===++====+=====+=====+======+======+ |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 605 | * I || I | C | R | [I] | [I] | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 606 | * ---++----+-----+-----+------+------+ |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 607 | * C || I | C | R | [C] | [C] | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 608 | * ---++----+-----+-----+------+------+ |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 609 | * R || I | C | R | [R] | [R] | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 610 | * ---++----+-----+-----+------+------+ |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 611 | * TF || I | C | R | [TF] | [TF] | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 612 | * ---++----+-----+-----+------+------+ |
| 613 | * SD || NA | NA | NA | NA | NA | (*) |
| 614 | * ---++----+-----+-----+------+------+ |
| 615 | * |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 616 | * A [STATE] indicates that the old RR policy is kept. In those cases, STATE |
| 617 | * is the current state of grpclb, which is left untouched. |
| 618 | * |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 619 | * In summary, if the new state is TRANSIENT_FAILURE or SHUTDOWN, stick to |
| 620 | * the previous RR instance. |
| 621 | * |
| 622 | * Note that the status is never updated to SHUTDOWN as a result of calling |
| 623 | * this function. Only glb_shutdown() has the power to set that state. |
| 624 | * |
| 625 | * (*) This function mustn't be called during shutting down. */ |
| 626 | GPR_ASSERT(curr_glb_state != GRPC_CHANNEL_SHUTDOWN); |
| 627 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 628 | switch (rr_state) { |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 629 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
| 630 | case GRPC_CHANNEL_SHUTDOWN: |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 631 | GPR_ASSERT(rr_state_error != GRPC_ERROR_NONE); |
| 632 | break; |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 633 | case GRPC_CHANNEL_IDLE: |
| 634 | case GRPC_CHANNEL_CONNECTING: |
| 635 | case GRPC_CHANNEL_READY: |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 636 | GPR_ASSERT(rr_state_error == GRPC_ERROR_NONE); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 639 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 640 | gpr_log( |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 641 | GPR_INFO, |
| 642 | "[grpclb %p] Setting grpclb's state to %s from new RR policy %p state.", |
| 643 | glb_policy, grpc_connectivity_state_name(rr_state), |
| 644 | glb_policy->rr_policy); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 645 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 646 | grpc_connectivity_state_set(&glb_policy->state_tracker, rr_state, |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 647 | rr_state_error, |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 648 | "update_lb_connectivity_status_locked"); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 651 | /* Perform a pick over \a glb_policy->rr_policy. Given that a pick can return |
| 652 | * immediately (ignoring its completion callback), we need to perform the |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 653 | * cleanups this callback would otherwise be responsible for. |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 654 | * If \a force_async is true, then we will manually schedule the |
| 655 | * completion callback even if the pick is available immediately. */ |
David Garcia Quintas | 2035906 | 2016-10-15 15:22:51 -0700 | [diff] [blame] | 656 | static bool pick_from_internal_rr_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 657 | glb_lb_policy* glb_policy, const grpc_lb_policy_pick_args* pick_args, |
| 658 | bool force_async, grpc_connected_subchannel** target, |
| 659 | wrapped_rr_closure_arg* wc_arg) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 660 | // Check for drops if we are not using fallback backend addresses. |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 661 | if (glb_policy->serverlist != nullptr) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 662 | // Look at the index into the serverlist to see if we should drop this call. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 663 | grpc_grpclb_server* server = |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 664 | glb_policy->serverlist->servers[glb_policy->serverlist_index++]; |
| 665 | if (glb_policy->serverlist_index == glb_policy->serverlist->num_servers) { |
| 666 | glb_policy->serverlist_index = 0; // Wrap-around. |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 667 | } |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 668 | if (server->drop) { |
| 669 | // Not using the RR policy, so unref it. |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 670 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 671 | gpr_log(GPR_INFO, "[grpclb %p] Unreffing RR %p for drop", glb_policy, |
David Garcia Quintas | 2b372e0 | 2017-11-09 14:15:59 -0800 | [diff] [blame] | 672 | wc_arg->rr_policy); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 673 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 674 | GRPC_LB_POLICY_UNREF(wc_arg->rr_policy, "glb_pick_sync"); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 675 | // Update client load reporting stats to indicate the number of |
| 676 | // dropped calls. Note that we have to do this here instead of in |
| 677 | // the client_load_reporting filter, because we do not create a |
| 678 | // subchannel call (and therefore no client_load_reporting filter) |
| 679 | // for dropped calls. |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 680 | GPR_ASSERT(wc_arg->client_stats != nullptr); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 681 | grpc_grpclb_client_stats_add_call_dropped_locked( |
| 682 | server->load_balance_token, wc_arg->client_stats); |
| 683 | grpc_grpclb_client_stats_unref(wc_arg->client_stats); |
| 684 | if (force_async) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 685 | GPR_ASSERT(wc_arg->wrapped_closure != nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 686 | GRPC_CLOSURE_SCHED(wc_arg->wrapped_closure, GRPC_ERROR_NONE); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 687 | gpr_free(wc_arg->free_when_done); |
| 688 | return false; |
| 689 | } |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 690 | gpr_free(wc_arg->free_when_done); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 691 | return true; |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 692 | } |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 693 | } |
| 694 | // Pick via the RR policy. |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 695 | const bool pick_done = grpc_lb_policy_pick_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 696 | wc_arg->rr_policy, pick_args, target, wc_arg->context, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 697 | (void**)&wc_arg->lb_token, &wc_arg->wrapper_closure); |
David Garcia Quintas | 2035906 | 2016-10-15 15:22:51 -0700 | [diff] [blame] | 698 | if (pick_done) { |
| 699 | /* synchronous grpc_lb_policy_pick call. Unref the RR policy. */ |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 700 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 701 | gpr_log(GPR_INFO, "[grpclb %p] Unreffing RR %p", glb_policy, |
| 702 | wc_arg->rr_policy); |
David Garcia Quintas | 58c18e7 | 2016-10-14 15:23:45 -0700 | [diff] [blame] | 703 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 704 | GRPC_LB_POLICY_UNREF(wc_arg->rr_policy, "glb_pick_sync"); |
David Garcia Quintas | 2035906 | 2016-10-15 15:22:51 -0700 | [diff] [blame] | 705 | /* add the load reporting initial metadata */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 706 | initial_metadata_add_lb_token(pick_args->initial_metadata, |
David Garcia Quintas | 2035906 | 2016-10-15 15:22:51 -0700 | [diff] [blame] | 707 | pick_args->lb_token_mdelem_storage, |
| 708 | GRPC_MDELEM_REF(wc_arg->lb_token)); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 709 | // Pass on client stats via context. Passes ownership of the reference. |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 710 | GPR_ASSERT(wc_arg->client_stats != nullptr); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 711 | wc_arg->context[GRPC_GRPCLB_CLIENT_STATS].value = wc_arg->client_stats; |
| 712 | wc_arg->context[GRPC_GRPCLB_CLIENT_STATS].destroy = destroy_client_stats; |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 713 | if (force_async) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 714 | GPR_ASSERT(wc_arg->wrapped_closure != nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 715 | GRPC_CLOSURE_SCHED(wc_arg->wrapped_closure, GRPC_ERROR_NONE); |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 716 | gpr_free(wc_arg->free_when_done); |
| 717 | return false; |
| 718 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 719 | gpr_free(wc_arg->free_when_done); |
David Garcia Quintas | 2035906 | 2016-10-15 15:22:51 -0700 | [diff] [blame] | 720 | } |
| 721 | /* else, the pending pick will be registered and taken care of by the |
| 722 | * pending pick list inside the RR policy (glb_policy->rr_policy). |
| 723 | * Eventually, wrapped_on_complete will be called, which will -among other |
| 724 | * things- add the LB token to the call's initial metadata */ |
David Garcia Quintas | 2035906 | 2016-10-15 15:22:51 -0700 | [diff] [blame] | 725 | return pick_done; |
David Garcia Quintas | 58c18e7 | 2016-10-14 15:23:45 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 728 | static grpc_lb_policy_args* lb_policy_args_create(glb_lb_policy* glb_policy) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 729 | grpc_lb_addresses* addresses; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 730 | if (glb_policy->serverlist != nullptr) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 731 | GPR_ASSERT(glb_policy->serverlist->num_servers > 0); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 732 | addresses = process_serverlist_locked(glb_policy->serverlist); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 733 | } else { |
| 734 | // If rr_handover_locked() is invoked when we haven't received any |
| 735 | // serverlist from the balancer, we use the fallback backends returned by |
| 736 | // the resolver. Note that the fallback backend list may be empty, in which |
| 737 | // case the new round_robin policy will keep the requested picks pending. |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 738 | GPR_ASSERT(glb_policy->fallback_backend_addresses != nullptr); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 739 | addresses = grpc_lb_addresses_copy(glb_policy->fallback_backend_addresses); |
| 740 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 741 | GPR_ASSERT(addresses != nullptr); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 742 | grpc_lb_policy_args* args = (grpc_lb_policy_args*)gpr_zalloc(sizeof(*args)); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 743 | args->client_channel_factory = glb_policy->cc_factory; |
| 744 | args->combiner = glb_policy->base.combiner; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 745 | // Replace the LB addresses in the channel args that we pass down to |
| 746 | // the subchannel. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 747 | static const char* keys_to_remove[] = {GRPC_ARG_LB_ADDRESSES}; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 748 | const grpc_arg arg = grpc_lb_addresses_create_channel_arg(addresses); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 749 | args->args = grpc_channel_args_copy_and_add_and_remove( |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 750 | glb_policy->args, keys_to_remove, GPR_ARRAY_SIZE(keys_to_remove), &arg, |
| 751 | 1); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 752 | grpc_lb_addresses_destroy(addresses); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 753 | return args; |
| 754 | } |
| 755 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 756 | static void lb_policy_args_destroy(grpc_lb_policy_args* args) { |
| 757 | grpc_channel_args_destroy(args->args); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 758 | gpr_free(args); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 759 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 760 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 761 | static void glb_rr_connectivity_changed_locked(void* arg, grpc_error* error); |
| 762 | static void create_rr_locked(glb_lb_policy* glb_policy, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 763 | grpc_lb_policy_args* args) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 764 | GPR_ASSERT(glb_policy->rr_policy == nullptr); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 765 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 766 | grpc_lb_policy* new_rr_policy = grpc_lb_policy_create("round_robin", args); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 767 | if (new_rr_policy == nullptr) { |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 768 | gpr_log(GPR_ERROR, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 769 | "[grpclb %p] Failure creating a RoundRobin policy for serverlist " |
David Garcia Quintas | 2b372e0 | 2017-11-09 14:15:59 -0800 | [diff] [blame] | 770 | "update with %" PRIuPTR |
| 771 | " entries. The previous RR instance (%p), if any, will continue to " |
| 772 | "be used. Future updates from the LB will attempt to create new " |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 773 | "instances.", |
David Garcia Quintas | 2b372e0 | 2017-11-09 14:15:59 -0800 | [diff] [blame] | 774 | glb_policy, glb_policy->serverlist->num_servers, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 775 | glb_policy->rr_policy); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 776 | return; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 777 | } |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 778 | grpc_lb_policy_set_reresolve_closure_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 779 | new_rr_policy, glb_policy->base.request_reresolution); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 780 | glb_policy->base.request_reresolution = nullptr; |
David Garcia Quintas | 4283a26 | 2016-11-18 10:43:56 -0800 | [diff] [blame] | 781 | glb_policy->rr_policy = new_rr_policy; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 782 | grpc_error* rr_state_error = nullptr; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 783 | const grpc_connectivity_state rr_state = |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 784 | grpc_lb_policy_check_connectivity_locked(glb_policy->rr_policy, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 785 | &rr_state_error); |
| 786 | /* Connectivity state is a function of the RR policy updated/created */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 787 | update_lb_connectivity_status_locked(glb_policy, rr_state, rr_state_error); |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 788 | /* Add the gRPC LB's interested_parties pollset_set to that of the newly |
| 789 | * created RR policy. This will make the RR policy progress upon activity on |
| 790 | * gRPC LB, which in turn is tied to the application's call */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 791 | grpc_pollset_set_add_pollset_set(glb_policy->rr_policy->interested_parties, |
Yuchen Zeng | b429164 | 2016-09-01 19:17:14 -0700 | [diff] [blame] | 792 | glb_policy->base.interested_parties); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 793 | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 794 | /* Allocate the data for the tracking of the new RR policy's connectivity. |
| 795 | * It'll be deallocated in glb_rr_connectivity_changed() */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 796 | rr_connectivity_data* rr_connectivity = |
| 797 | (rr_connectivity_data*)gpr_zalloc(sizeof(rr_connectivity_data)); |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 798 | GRPC_CLOSURE_INIT(&rr_connectivity->on_change, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 799 | glb_rr_connectivity_changed_locked, rr_connectivity, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 800 | grpc_combiner_scheduler(glb_policy->base.combiner)); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 801 | rr_connectivity->glb_policy = glb_policy; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 802 | rr_connectivity->state = rr_state; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 803 | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 804 | /* Subscribe to changes to the connectivity of the new RR */ |
David Garcia Quintas | fc950fb | 2017-07-27 19:41:12 -0700 | [diff] [blame] | 805 | GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "glb_rr_connectivity_cb"); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 806 | grpc_lb_policy_notify_on_state_change_locked(glb_policy->rr_policy, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 807 | &rr_connectivity->state, |
| 808 | &rr_connectivity->on_change); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 809 | grpc_lb_policy_exit_idle_locked(glb_policy->rr_policy); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 810 | |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 811 | /* Update picks and pings in wait */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 812 | pending_pick* pp; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 813 | while ((pp = glb_policy->pending_picks)) { |
| 814 | glb_policy->pending_picks = pp->next; |
| 815 | GRPC_LB_POLICY_REF(glb_policy->rr_policy, "rr_handover_pending_pick"); |
| 816 | pp->wrapped_on_complete_arg.rr_policy = glb_policy->rr_policy; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 817 | pp->wrapped_on_complete_arg.client_stats = |
| 818 | grpc_grpclb_client_stats_ref(glb_policy->client_stats); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 819 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 820 | gpr_log(GPR_INFO, |
| 821 | "[grpclb %p] Pending pick about to (async) PICK from RR %p", |
| 822 | glb_policy, glb_policy->rr_policy); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 823 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 824 | pick_from_internal_rr_locked(glb_policy, &pp->pick_args, |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 825 | true /* force_async */, pp->target, |
David Garcia Quintas | 58c18e7 | 2016-10-14 15:23:45 -0700 | [diff] [blame] | 826 | &pp->wrapped_on_complete_arg); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 829 | pending_ping* pping; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 830 | while ((pping = glb_policy->pending_pings)) { |
| 831 | glb_policy->pending_pings = pping->next; |
Yuchen Zeng | 625a5c0 | 2017-12-06 13:24:27 -0800 | [diff] [blame] | 832 | grpc_closure* on_initiate = nullptr; |
| 833 | grpc_closure* on_ack = nullptr; |
| 834 | if (pping->on_initiate != nullptr) { |
| 835 | GRPC_LB_POLICY_REF(glb_policy->rr_policy, "rr_handover_pending_ping"); |
| 836 | pping->on_initiate->rr_policy = glb_policy->rr_policy; |
| 837 | on_initiate = &pping->on_initiate->wrapper_closure; |
| 838 | } |
| 839 | if (pping->on_ack != nullptr) { |
| 840 | GRPC_LB_POLICY_REF(glb_policy->rr_policy, "rr_handover_pending_ping"); |
| 841 | pping->on_ack->rr_policy = glb_policy->rr_policy; |
| 842 | on_ack = &pping->on_ack->wrapper_closure; |
| 843 | } |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 844 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 845 | gpr_log(GPR_INFO, "[grpclb %p] Pending ping about to PING from RR %p", |
| 846 | glb_policy, glb_policy->rr_policy); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 847 | } |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 848 | grpc_lb_policy_ping_one_locked(glb_policy->rr_policy, on_initiate, on_ack); |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 849 | gpr_free(pping); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 850 | } |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 851 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 852 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 853 | /* glb_policy->rr_policy may be nullptr (initial handover) */ |
| 854 | static void rr_handover_locked(glb_lb_policy* glb_policy) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 855 | if (glb_policy->shutting_down) return; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 856 | grpc_lb_policy_args* args = lb_policy_args_create(glb_policy); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 857 | GPR_ASSERT(args != nullptr); |
| 858 | if (glb_policy->rr_policy != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 859 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 860 | gpr_log(GPR_DEBUG, "[grpclb %p] Updating RR policy %p", glb_policy, |
| 861 | glb_policy->rr_policy); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 862 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 863 | grpc_lb_policy_update_locked(glb_policy->rr_policy, args); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 864 | } else { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 865 | create_rr_locked(glb_policy, args); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 866 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 867 | gpr_log(GPR_DEBUG, "[grpclb %p] Created new RR policy %p", glb_policy, |
| 868 | glb_policy->rr_policy); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 871 | lb_policy_args_destroy(args); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 874 | static void glb_rr_connectivity_changed_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 875 | rr_connectivity_data* rr_connectivity = (rr_connectivity_data*)arg; |
| 876 | glb_lb_policy* glb_policy = rr_connectivity->glb_policy; |
David Garcia Quintas | fc950fb | 2017-07-27 19:41:12 -0700 | [diff] [blame] | 877 | if (glb_policy->shutting_down) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 878 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, "glb_rr_connectivity_cb"); |
David Garcia Quintas | fc950fb | 2017-07-27 19:41:12 -0700 | [diff] [blame] | 879 | gpr_free(rr_connectivity); |
| 880 | return; |
David Garcia Quintas | 149f09d | 2016-11-17 20:43:10 -0800 | [diff] [blame] | 881 | } |
David Garcia Quintas | fc950fb | 2017-07-27 19:41:12 -0700 | [diff] [blame] | 882 | if (rr_connectivity->state == GRPC_CHANNEL_SHUTDOWN) { |
| 883 | /* An RR policy that has transitioned into the SHUTDOWN connectivity state |
| 884 | * should not be considered for picks or updates: the SHUTDOWN state is a |
| 885 | * sink, policies can't transition back from it. .*/ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 886 | GRPC_LB_POLICY_UNREF(glb_policy->rr_policy, "rr_connectivity_shutdown"); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 887 | glb_policy->rr_policy = nullptr; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 888 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, "glb_rr_connectivity_cb"); |
David Garcia Quintas | fc950fb | 2017-07-27 19:41:12 -0700 | [diff] [blame] | 889 | gpr_free(rr_connectivity); |
| 890 | return; |
| 891 | } |
| 892 | /* rr state != SHUTDOWN && !glb_policy->shutting down: biz as usual */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 893 | update_lb_connectivity_status_locked(glb_policy, rr_connectivity->state, |
| 894 | GRPC_ERROR_REF(error)); |
David Garcia Quintas | fc950fb | 2017-07-27 19:41:12 -0700 | [diff] [blame] | 895 | /* Resubscribe. Reuse the "glb_rr_connectivity_cb" weak ref. */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 896 | grpc_lb_policy_notify_on_state_change_locked(glb_policy->rr_policy, |
David Garcia Quintas | fc950fb | 2017-07-27 19:41:12 -0700 | [diff] [blame] | 897 | &rr_connectivity->state, |
| 898 | &rr_connectivity->on_change); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 901 | static void destroy_balancer_name(void* balancer_name) { |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 902 | gpr_free(balancer_name); |
| 903 | } |
| 904 | |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 905 | static grpc_slice_hash_table_entry targets_info_entry_create( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 906 | const char* address, const char* balancer_name) { |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 907 | grpc_slice_hash_table_entry entry; |
| 908 | entry.key = grpc_slice_from_copied_string(address); |
Mark D. Roth | e300670 | 2017-04-19 07:43:56 -0700 | [diff] [blame] | 909 | entry.value = gpr_strdup(balancer_name); |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 910 | return entry; |
| 911 | } |
| 912 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 913 | static int balancer_name_cmp_fn(void* a, void* b) { |
| 914 | const char* a_str = (const char*)a; |
| 915 | const char* b_str = (const char*)b; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 916 | return strcmp(a_str, b_str); |
| 917 | } |
| 918 | |
| 919 | /* Returns the channel args for the LB channel, used to create a bidirectional |
| 920 | * stream for the reception of load balancing updates. |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 921 | * |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 922 | * Inputs: |
| 923 | * - \a addresses: corresponding to the balancers. |
| 924 | * - \a response_generator: in order to propagate updates from the resolver |
| 925 | * above the grpclb policy. |
| 926 | * - \a args: other args inherited from the grpclb policy. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 927 | static grpc_channel_args* build_lb_channel_args( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 928 | const grpc_lb_addresses* addresses, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 929 | grpc_fake_resolver_response_generator* response_generator, |
| 930 | const grpc_channel_args* args) { |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 931 | size_t num_grpclb_addrs = 0; |
| 932 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 933 | if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs; |
| 934 | } |
| 935 | /* All input addresses come from a resolver that claims they are LB services. |
| 936 | * It's the resolver's responsibility to make sure this policy is only |
| 937 | * instantiated and used in that case. Otherwise, something has gone wrong. */ |
| 938 | GPR_ASSERT(num_grpclb_addrs > 0); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 939 | grpc_lb_addresses* lb_addresses = |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 940 | grpc_lb_addresses_create(num_grpclb_addrs, nullptr); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 941 | grpc_slice_hash_table_entry* targets_info_entries = |
| 942 | (grpc_slice_hash_table_entry*)gpr_zalloc(sizeof(*targets_info_entries) * |
| 943 | num_grpclb_addrs); |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 944 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 945 | size_t lb_addresses_idx = 0; |
| 946 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 947 | if (!addresses->addresses[i].is_balancer) continue; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 948 | if (addresses->addresses[i].user_data != nullptr) { |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 949 | gpr_log(GPR_ERROR, |
| 950 | "This LB policy doesn't support user data. It will be ignored"); |
| 951 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 952 | char* addr_str; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 953 | GPR_ASSERT(grpc_sockaddr_to_string( |
| 954 | &addr_str, &addresses->addresses[i].address, true) > 0); |
| 955 | targets_info_entries[lb_addresses_idx] = targets_info_entry_create( |
| 956 | addr_str, addresses->addresses[i].balancer_name); |
| 957 | gpr_free(addr_str); |
| 958 | |
| 959 | grpc_lb_addresses_set_address( |
| 960 | lb_addresses, lb_addresses_idx++, addresses->addresses[i].address.addr, |
| 961 | addresses->addresses[i].address.len, false /* is balancer */, |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 962 | addresses->addresses[i].balancer_name, nullptr /* user data */); |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 963 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 964 | GPR_ASSERT(num_grpclb_addrs == lb_addresses_idx); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 965 | grpc_slice_hash_table* targets_info = |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 966 | grpc_slice_hash_table_create(num_grpclb_addrs, targets_info_entries, |
| 967 | destroy_balancer_name, balancer_name_cmp_fn); |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 968 | gpr_free(targets_info_entries); |
| 969 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 970 | grpc_channel_args* lb_channel_args = |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 971 | grpc_lb_policy_grpclb_build_lb_channel_args(targets_info, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 972 | response_generator, args); |
| 973 | |
| 974 | grpc_arg lb_channel_addresses_arg = |
| 975 | grpc_lb_addresses_create_channel_arg(lb_addresses); |
| 976 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 977 | grpc_channel_args* result = grpc_channel_args_copy_and_add( |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 978 | lb_channel_args, &lb_channel_addresses_arg, 1); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 979 | grpc_slice_hash_table_unref(targets_info); |
| 980 | grpc_channel_args_destroy(lb_channel_args); |
| 981 | grpc_lb_addresses_destroy(lb_addresses); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 982 | return result; |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 983 | } |
| 984 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 985 | static void glb_destroy(grpc_lb_policy* pol) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 986 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 987 | GPR_ASSERT(glb_policy->pending_picks == nullptr); |
| 988 | GPR_ASSERT(glb_policy->pending_pings == nullptr); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 989 | gpr_free((void*)glb_policy->server_name); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 990 | grpc_channel_args_destroy(glb_policy->args); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 991 | if (glb_policy->client_stats != nullptr) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 992 | grpc_grpclb_client_stats_unref(glb_policy->client_stats); |
| 993 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 994 | grpc_connectivity_state_destroy(&glb_policy->state_tracker); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 995 | if (glb_policy->serverlist != nullptr) { |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 996 | grpc_grpclb_destroy_serverlist(glb_policy->serverlist); |
| 997 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 998 | if (glb_policy->fallback_backend_addresses != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 999 | grpc_lb_addresses_destroy(glb_policy->fallback_backend_addresses); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1000 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1001 | grpc_fake_resolver_response_generator_unref(glb_policy->response_generator); |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 1002 | grpc_subchannel_index_unref(); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1003 | gpr_free(glb_policy); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1006 | static void glb_shutdown_locked(grpc_lb_policy* pol) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1007 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1008 | grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel shutdown"); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1009 | glb_policy->shutting_down = true; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1010 | |
David Garcia Quintas | a74b246 | 2016-11-11 14:07:27 -0800 | [diff] [blame] | 1011 | /* We need a copy of the lb_call pointer because we can't cancell the call |
| 1012 | * while holding glb_policy->mu: lb_on_server_status_received, invoked due to |
| 1013 | * the cancel, needs to acquire that same lock */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1014 | grpc_call* lb_call = glb_policy->lb_call; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1015 | |
David Garcia Quintas | a74b246 | 2016-11-11 14:07:27 -0800 | [diff] [blame] | 1016 | /* glb_policy->lb_call and this local lb_call must be consistent at this point |
| 1017 | * because glb_policy->lb_call is only assigned in lb_call_init_locked as part |
| 1018 | * of query_for_backends_locked, which can only be invoked while |
| 1019 | * glb_policy->shutting_down is false. */ |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1020 | if (lb_call != nullptr) { |
| 1021 | grpc_call_cancel(lb_call, nullptr); |
David Garcia Quintas | a74b246 | 2016-11-11 14:07:27 -0800 | [diff] [blame] | 1022 | /* lb_on_server_status_received will pick up the cancel and clean up */ |
| 1023 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1024 | if (glb_policy->retry_timer_active) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1025 | grpc_timer_cancel(&glb_policy->lb_call_retry_timer); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1026 | glb_policy->retry_timer_active = false; |
| 1027 | } |
Juanli Shen | 663f50c | 2017-10-05 14:36:13 -0700 | [diff] [blame] | 1028 | if (glb_policy->fallback_timer_active) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1029 | grpc_timer_cancel(&glb_policy->lb_fallback_timer); |
Juanli Shen | 663f50c | 2017-10-05 14:36:13 -0700 | [diff] [blame] | 1030 | glb_policy->fallback_timer_active = false; |
| 1031 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1032 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1033 | pending_pick* pp = glb_policy->pending_picks; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1034 | glb_policy->pending_picks = nullptr; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1035 | pending_ping* pping = glb_policy->pending_pings; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1036 | glb_policy->pending_pings = nullptr; |
| 1037 | if (glb_policy->rr_policy != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1038 | GRPC_LB_POLICY_UNREF(glb_policy->rr_policy, "glb_shutdown"); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1039 | } else { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1040 | grpc_lb_policy_try_reresolve(pol, &grpc_lb_glb_trace, GRPC_ERROR_CANCELLED); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1041 | } |
| 1042 | // We destroy the LB channel here because |
| 1043 | // glb_lb_channel_on_connectivity_changed_cb needs a valid glb_policy |
| 1044 | // instance. Destroying the lb channel in glb_destroy would likely result in |
| 1045 | // a callback invocation without a valid glb_policy arg. |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1046 | if (glb_policy->lb_channel != nullptr) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1047 | grpc_channel_destroy(glb_policy->lb_channel); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1048 | glb_policy->lb_channel = nullptr; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1049 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1050 | grpc_connectivity_state_set(&glb_policy->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
| 1051 | GRPC_ERROR_REF(error), "glb_shutdown"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1052 | |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1053 | while (pp != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1054 | pending_pick* next = pp->next; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1055 | *pp->target = nullptr; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1056 | GRPC_CLOSURE_SCHED(&pp->wrapped_on_complete_arg.wrapper_closure, |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1057 | GRPC_ERROR_REF(error)); |
Mark D. Roth | 7a2db96 | 2017-10-06 15:06:12 -0700 | [diff] [blame] | 1058 | gpr_free(pp); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1059 | pp = next; |
| 1060 | } |
| 1061 | |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1062 | while (pping != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1063 | pending_ping* next = pping->next; |
Yuchen Zeng | 625a5c0 | 2017-12-06 13:24:27 -0800 | [diff] [blame] | 1064 | if (pping->on_initiate != nullptr) { |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 1065 | GRPC_CLOSURE_SCHED(&pping->on_initiate->wrapper_closure, |
Yuchen Zeng | 625a5c0 | 2017-12-06 13:24:27 -0800 | [diff] [blame] | 1066 | GRPC_ERROR_REF(error)); |
| 1067 | gpr_free(pping->on_initiate); |
| 1068 | } |
| 1069 | if (pping->on_ack != nullptr) { |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 1070 | GRPC_CLOSURE_SCHED(&pping->on_ack->wrapper_closure, |
Yuchen Zeng | 625a5c0 | 2017-12-06 13:24:27 -0800 | [diff] [blame] | 1071 | GRPC_ERROR_REF(error)); |
| 1072 | gpr_free(pping->on_ack); |
| 1073 | } |
Mark D. Roth | 7a2db96 | 2017-10-06 15:06:12 -0700 | [diff] [blame] | 1074 | gpr_free(pping); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1075 | pping = next; |
| 1076 | } |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1077 | GRPC_ERROR_UNREF(error); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1080 | // Cancel a specific pending pick. |
| 1081 | // |
| 1082 | // A grpclb pick progresses as follows: |
| 1083 | // - If there's a Round Robin policy (glb_policy->rr_policy) available, it'll be |
| 1084 | // handed over to the RR policy (in create_rr_locked()). From that point |
| 1085 | // onwards, it'll be RR's responsibility. For cancellations, that implies the |
| 1086 | // pick needs also be cancelled by the RR instance. |
| 1087 | // - Otherwise, without an RR instance, picks stay pending at this policy's |
| 1088 | // level (grpclb), inside the glb_policy->pending_picks list. To cancel these, |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1089 | // we invoke the completion closure and set *target to nullptr right here. |
| 1090 | static void glb_cancel_pick_locked(grpc_lb_policy* pol, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1091 | grpc_connected_subchannel** target, |
| 1092 | grpc_error* error) { |
| 1093 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
| 1094 | pending_pick* pp = glb_policy->pending_picks; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1095 | glb_policy->pending_picks = nullptr; |
| 1096 | while (pp != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1097 | pending_pick* next = pp->next; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1098 | if (pp->target == target) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1099 | *target = nullptr; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1100 | GRPC_CLOSURE_SCHED(&pp->wrapped_on_complete_arg.wrapper_closure, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 1101 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1102 | "Pick Cancelled", &error, 1)); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1103 | } else { |
| 1104 | pp->next = glb_policy->pending_picks; |
| 1105 | glb_policy->pending_picks = pp; |
| 1106 | } |
| 1107 | pp = next; |
| 1108 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1109 | if (glb_policy->rr_policy != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1110 | grpc_lb_policy_cancel_pick_locked(glb_policy->rr_policy, target, |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1111 | GRPC_ERROR_REF(error)); |
| 1112 | } |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 1113 | GRPC_ERROR_UNREF(error); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1116 | // Cancel all pending picks. |
| 1117 | // |
| 1118 | // A grpclb pick progresses as follows: |
| 1119 | // - If there's a Round Robin policy (glb_policy->rr_policy) available, it'll be |
| 1120 | // handed over to the RR policy (in create_rr_locked()). From that point |
| 1121 | // onwards, it'll be RR's responsibility. For cancellations, that implies the |
| 1122 | // pick needs also be cancelled by the RR instance. |
| 1123 | // - Otherwise, without an RR instance, picks stay pending at this policy's |
| 1124 | // level (grpclb), inside the glb_policy->pending_picks list. To cancel these, |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1125 | // we invoke the completion closure and set *target to nullptr right here. |
| 1126 | static void glb_cancel_picks_locked(grpc_lb_policy* pol, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 1127 | uint32_t initial_metadata_flags_mask, |
| 1128 | uint32_t initial_metadata_flags_eq, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1129 | grpc_error* error) { |
| 1130 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
| 1131 | pending_pick* pp = glb_policy->pending_picks; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1132 | glb_policy->pending_picks = nullptr; |
| 1133 | while (pp != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1134 | pending_pick* next = pp->next; |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 1135 | if ((pp->pick_args.initial_metadata_flags & initial_metadata_flags_mask) == |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1136 | initial_metadata_flags_eq) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1137 | GRPC_CLOSURE_SCHED(&pp->wrapped_on_complete_arg.wrapper_closure, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 1138 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1139 | "Pick Cancelled", &error, 1)); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1140 | } else { |
| 1141 | pp->next = glb_policy->pending_picks; |
| 1142 | glb_policy->pending_picks = pp; |
| 1143 | } |
| 1144 | pp = next; |
| 1145 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1146 | if (glb_policy->rr_policy != nullptr) { |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1147 | grpc_lb_policy_cancel_picks_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1148 | glb_policy->rr_policy, initial_metadata_flags_mask, |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1149 | initial_metadata_flags_eq, GRPC_ERROR_REF(error)); |
| 1150 | } |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 1151 | GRPC_ERROR_UNREF(error); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1152 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1153 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1154 | static void lb_on_fallback_timer_locked(void* arg, grpc_error* error); |
| 1155 | static void query_for_backends_locked(glb_lb_policy* glb_policy); |
| 1156 | static void start_picking_locked(glb_lb_policy* glb_policy) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1157 | /* start a timer to fall back */ |
| 1158 | if (glb_policy->lb_fallback_timeout_ms > 0 && |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1159 | glb_policy->serverlist == nullptr && !glb_policy->fallback_timer_active) { |
Craig Tiller | 1e868f0 | 2017-09-29 11:18:26 -0700 | [diff] [blame] | 1160 | grpc_millis deadline = |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1161 | grpc_core::ExecCtx::Get()->Now() + glb_policy->lb_fallback_timeout_ms; |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1162 | GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "grpclb_fallback_timer"); |
| 1163 | GRPC_CLOSURE_INIT(&glb_policy->lb_on_fallback, lb_on_fallback_timer_locked, |
| 1164 | glb_policy, |
| 1165 | grpc_combiner_scheduler(glb_policy->base.combiner)); |
| 1166 | glb_policy->fallback_timer_active = true; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1167 | grpc_timer_init(&glb_policy->lb_fallback_timer, deadline, |
Craig Tiller | 1e868f0 | 2017-09-29 11:18:26 -0700 | [diff] [blame] | 1168 | &glb_policy->lb_on_fallback); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1171 | glb_policy->started_picking = true; |
David Garcia Quintas | dde6afc | 2017-11-22 16:31:01 -0800 | [diff] [blame] | 1172 | glb_policy->lb_call_backoff->Reset(); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1173 | query_for_backends_locked(glb_policy); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1174 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1175 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1176 | static void glb_exit_idle_locked(grpc_lb_policy* pol) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1177 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1178 | if (!glb_policy->started_picking) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1179 | start_picking_locked(glb_policy); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1180 | } |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1181 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1182 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1183 | static int glb_pick_locked(grpc_lb_policy* pol, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1184 | const grpc_lb_policy_pick_args* pick_args, |
| 1185 | grpc_connected_subchannel** target, |
| 1186 | grpc_call_context_element* context, void** user_data, |
| 1187 | grpc_closure* on_complete) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1188 | if (pick_args->lb_token_mdelem_storage == nullptr) { |
| 1189 | *target = nullptr; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1190 | GRPC_CLOSURE_SCHED(on_complete, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 1191 | GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 1192 | "No mdelem storage for the LB token. Load reporting " |
| 1193 | "won't work without it. Failing")); |
Mark D. Roth | 1e5f6af | 2016-10-07 08:32:58 -0700 | [diff] [blame] | 1194 | return 0; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 1195 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1196 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1197 | bool pick_done = false; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1198 | if (glb_policy->rr_policy != nullptr) { |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1199 | const grpc_connectivity_state rr_connectivity_state = |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1200 | grpc_lb_policy_check_connectivity_locked(glb_policy->rr_policy, |
| 1201 | nullptr); |
David Garcia Quintas | f6c6b92 | 2017-11-03 07:48:16 -0700 | [diff] [blame] | 1202 | // The glb_policy->rr_policy may have transitioned to SHUTDOWN but the |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1203 | // callback registered to capture this event |
| 1204 | // (glb_rr_connectivity_changed_locked) may not have been invoked yet. We |
| 1205 | // need to make sure we aren't trying to pick from a RR policy instance |
| 1206 | // that's in shutdown. |
| 1207 | if (rr_connectivity_state == GRPC_CHANNEL_SHUTDOWN) { |
ncteisen | 72afb76 | 2017-11-10 12:23:12 -0800 | [diff] [blame] | 1208 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1209 | gpr_log(GPR_INFO, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1210 | "[grpclb %p] NOT picking from from RR %p: RR conn state=%s", |
| 1211 | glb_policy, glb_policy->rr_policy, |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1212 | grpc_connectivity_state_name(rr_connectivity_state)); |
| 1213 | } |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1214 | add_pending_pick(&glb_policy->pending_picks, pick_args, target, context, |
| 1215 | on_complete); |
| 1216 | pick_done = false; |
| 1217 | } else { // RR not in shutdown |
ncteisen | 72afb76 | 2017-11-10 12:23:12 -0800 | [diff] [blame] | 1218 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1219 | gpr_log(GPR_INFO, "[grpclb %p] about to PICK from RR %p", glb_policy, |
| 1220 | glb_policy->rr_policy); |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1221 | } |
| 1222 | GRPC_LB_POLICY_REF(glb_policy->rr_policy, "glb_pick"); |
Craig Tiller | 34992a6 | 2017-11-06 12:33:42 -0800 | [diff] [blame] | 1223 | wrapped_rr_closure_arg* wc_arg = |
| 1224 | (wrapped_rr_closure_arg*)gpr_zalloc(sizeof(wrapped_rr_closure_arg)); |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1225 | GRPC_CLOSURE_INIT(&wc_arg->wrapper_closure, wrapped_rr_closure, wc_arg, |
| 1226 | grpc_schedule_on_exec_ctx); |
| 1227 | wc_arg->rr_policy = glb_policy->rr_policy; |
| 1228 | wc_arg->target = target; |
| 1229 | wc_arg->context = context; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1230 | GPR_ASSERT(glb_policy->client_stats != nullptr); |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1231 | wc_arg->client_stats = |
| 1232 | grpc_grpclb_client_stats_ref(glb_policy->client_stats); |
| 1233 | wc_arg->wrapped_closure = on_complete; |
| 1234 | wc_arg->lb_token_mdelem_storage = pick_args->lb_token_mdelem_storage; |
| 1235 | wc_arg->initial_metadata = pick_args->initial_metadata; |
| 1236 | wc_arg->free_when_done = wc_arg; |
David Garcia Quintas | 6712a75 | 2017-11-10 12:09:25 -0800 | [diff] [blame] | 1237 | wc_arg->glb_policy = pol; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1238 | pick_done = pick_from_internal_rr_locked( |
| 1239 | glb_policy, pick_args, false /* force_async */, target, wc_arg); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1240 | } |
David Garcia Quintas | 2a95bf4 | 2017-09-07 11:26:34 -0700 | [diff] [blame] | 1241 | } else { // glb_policy->rr_policy == NULL |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1242 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1243 | gpr_log(GPR_DEBUG, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1244 | "[grpclb %p] No RR policy. Adding to grpclb's pending picks", |
| 1245 | glb_policy); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1246 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1247 | add_pending_pick(&glb_policy->pending_picks, pick_args, target, context, |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 1248 | on_complete); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1249 | if (!glb_policy->started_picking) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1250 | start_picking_locked(glb_policy); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1251 | } |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 1252 | pick_done = false; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1253 | } |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 1254 | return pick_done; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1255 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1256 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 1257 | static grpc_connectivity_state glb_check_connectivity_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1258 | grpc_lb_policy* pol, grpc_error** connectivity_error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1259 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 1260 | return grpc_connectivity_state_get(&glb_policy->state_tracker, |
| 1261 | connectivity_error); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1262 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1263 | |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 1264 | static void glb_ping_one_locked(grpc_lb_policy* pol, grpc_closure* on_initiate, |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 1265 | grpc_closure* on_ack) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1266 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1267 | if (glb_policy->rr_policy) { |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 1268 | grpc_lb_policy_ping_one_locked(glb_policy->rr_policy, on_initiate, on_ack); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1269 | } else { |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 1270 | add_pending_ping(&glb_policy->pending_pings, on_initiate, on_ack); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1271 | if (!glb_policy->started_picking) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1272 | start_picking_locked(glb_policy); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1275 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1276 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1277 | static void glb_notify_on_state_change_locked(grpc_lb_policy* pol, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1278 | grpc_connectivity_state* current, |
| 1279 | grpc_closure* notify) { |
| 1280 | glb_lb_policy* glb_policy = (glb_lb_policy*)pol; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1281 | grpc_connectivity_state_notify_on_state_change(&glb_policy->state_tracker, |
| 1282 | current, notify); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1285 | static void lb_call_on_retry_timer_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1286 | glb_lb_policy* glb_policy = (glb_lb_policy*)arg; |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1287 | glb_policy->retry_timer_active = false; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1288 | if (!glb_policy->shutting_down && glb_policy->lb_call == nullptr && |
David Garcia Quintas | b90cb3f | 2017-11-09 13:58:00 -0800 | [diff] [blame] | 1289 | error == GRPC_ERROR_NONE) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1290 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | 2b372e0 | 2017-11-09 14:15:59 -0800 | [diff] [blame] | 1291 | gpr_log(GPR_INFO, "[grpclb %p] Restarting call to LB server", glb_policy); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1292 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1293 | query_for_backends_locked(glb_policy); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1294 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1295 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, "grpclb_retry_timer"); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1298 | static void maybe_restart_lb_call(glb_lb_policy* glb_policy) { |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1299 | if (glb_policy->started_picking && glb_policy->updating_lb_call) { |
| 1300 | if (glb_policy->retry_timer_active) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1301 | grpc_timer_cancel(&glb_policy->lb_call_retry_timer); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1302 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1303 | if (!glb_policy->shutting_down) start_picking_locked(glb_policy); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1304 | glb_policy->updating_lb_call = false; |
| 1305 | } else if (!glb_policy->shutting_down) { |
| 1306 | /* if we aren't shutting down, restart the LB client call after some time */ |
David Garcia Quintas | 54d699d | 2017-12-13 14:44:29 -0800 | [diff] [blame] | 1307 | grpc_millis next_try = glb_policy->lb_call_backoff->Step(); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1308 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1309 | gpr_log(GPR_DEBUG, "[grpclb %p] Connection to LB server lost...", |
| 1310 | glb_policy); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1311 | grpc_millis timeout = next_try - grpc_core::ExecCtx::Get()->Now(); |
Craig Tiller | 1e868f0 | 2017-09-29 11:18:26 -0700 | [diff] [blame] | 1312 | if (timeout > 0) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1313 | gpr_log(GPR_DEBUG, |
| 1314 | "[grpclb %p] ... retry_timer_active in %" PRIuPTR "ms.", |
| 1315 | glb_policy, timeout); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1316 | } else { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1317 | gpr_log(GPR_DEBUG, "[grpclb %p] ... retry_timer_active immediately.", |
| 1318 | glb_policy); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "grpclb_retry_timer"); |
| 1322 | GRPC_CLOSURE_INIT(&glb_policy->lb_on_call_retry, |
| 1323 | lb_call_on_retry_timer_locked, glb_policy, |
| 1324 | grpc_combiner_scheduler(glb_policy->base.combiner)); |
| 1325 | glb_policy->retry_timer_active = true; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1326 | grpc_timer_init(&glb_policy->lb_call_retry_timer, next_try, |
Craig Tiller | 1e868f0 | 2017-09-29 11:18:26 -0700 | [diff] [blame] | 1327 | &glb_policy->lb_on_call_retry); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1328 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1329 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1330 | "lb_on_server_status_received_locked"); |
| 1331 | } |
| 1332 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1333 | static void send_client_load_report_locked(void* arg, grpc_error* error); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1334 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1335 | static void schedule_next_client_load_report(glb_lb_policy* glb_policy) { |
Craig Tiller | c0df1c0 | 2017-07-17 16:12:33 -0700 | [diff] [blame] | 1336 | const grpc_millis next_client_load_report_time = |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1337 | grpc_core::ExecCtx::Get()->Now() + |
| 1338 | glb_policy->client_stats_report_interval; |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1339 | GRPC_CLOSURE_INIT(&glb_policy->client_load_report_closure, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1340 | send_client_load_report_locked, glb_policy, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 1341 | grpc_combiner_scheduler(glb_policy->base.combiner)); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1342 | grpc_timer_init(&glb_policy->client_load_report_timer, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1343 | next_client_load_report_time, |
Craig Tiller | c0df1c0 | 2017-07-17 16:12:33 -0700 | [diff] [blame] | 1344 | &glb_policy->client_load_report_closure); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1347 | static void client_load_report_done_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1348 | glb_lb_policy* glb_policy = (glb_lb_policy*)arg; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1349 | grpc_byte_buffer_destroy(glb_policy->client_load_report_payload); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1350 | glb_policy->client_load_report_payload = nullptr; |
| 1351 | if (error != GRPC_ERROR_NONE || glb_policy->lb_call == nullptr) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1352 | glb_policy->client_load_report_timer_pending = false; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1353 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, "client_load_report"); |
Mark D. Roth | 6c8ee58 | 2017-11-16 08:35:25 -0800 | [diff] [blame] | 1354 | if (glb_policy->lb_call == nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1355 | maybe_restart_lb_call(glb_policy); |
Mark D. Roth | 6c8ee58 | 2017-11-16 08:35:25 -0800 | [diff] [blame] | 1356 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1357 | return; |
| 1358 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1359 | schedule_next_client_load_report(glb_policy); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1362 | static bool load_report_counters_are_zero(grpc_grpclb_request* request) { |
| 1363 | grpc_grpclb_dropped_call_counts* drop_entries = |
| 1364 | (grpc_grpclb_dropped_call_counts*) |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 1365 | request->client_stats.calls_finished_with_drop.arg; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1366 | return request->client_stats.num_calls_started == 0 && |
| 1367 | request->client_stats.num_calls_finished == 0 && |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1368 | request->client_stats.num_calls_finished_with_client_failed_to_send == |
| 1369 | 0 && |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 1370 | request->client_stats.num_calls_finished_known_received == 0 && |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1371 | (drop_entries == nullptr || drop_entries->num_entries == 0); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1374 | static void send_client_load_report_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1375 | glb_lb_policy* glb_policy = (glb_lb_policy*)arg; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1376 | if (error == GRPC_ERROR_CANCELLED || glb_policy->lb_call == nullptr) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1377 | glb_policy->client_load_report_timer_pending = false; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1378 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, "client_load_report"); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1379 | if (glb_policy->lb_call == nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1380 | maybe_restart_lb_call(glb_policy); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1381 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1382 | return; |
| 1383 | } |
| 1384 | // Construct message payload. |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1385 | GPR_ASSERT(glb_policy->client_load_report_payload == nullptr); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1386 | grpc_grpclb_request* request = |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 1387 | grpc_grpclb_load_report_request_create_locked(glb_policy->client_stats); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1388 | // Skip client load report if the counters were all zero in the last |
| 1389 | // report and they are still zero in this one. |
| 1390 | if (load_report_counters_are_zero(request)) { |
| 1391 | if (glb_policy->last_client_load_report_counters_were_zero) { |
| 1392 | grpc_grpclb_request_destroy(request); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1393 | schedule_next_client_load_report(glb_policy); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1394 | return; |
| 1395 | } |
| 1396 | glb_policy->last_client_load_report_counters_were_zero = true; |
| 1397 | } else { |
| 1398 | glb_policy->last_client_load_report_counters_were_zero = false; |
| 1399 | } |
| 1400 | grpc_slice request_payload_slice = grpc_grpclb_request_encode(request); |
| 1401 | glb_policy->client_load_report_payload = |
| 1402 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1403 | grpc_slice_unref_internal(request_payload_slice); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1404 | grpc_grpclb_request_destroy(request); |
Mark D. Roth | 2de36a8 | 2017-09-25 14:54:44 -0700 | [diff] [blame] | 1405 | // Send load report message. |
| 1406 | grpc_op op; |
| 1407 | memset(&op, 0, sizeof(op)); |
| 1408 | op.op = GRPC_OP_SEND_MESSAGE; |
| 1409 | op.data.send_message.send_message = glb_policy->client_load_report_payload; |
| 1410 | GRPC_CLOSURE_INIT(&glb_policy->client_load_report_closure, |
| 1411 | client_load_report_done_locked, glb_policy, |
| 1412 | grpc_combiner_scheduler(glb_policy->base.combiner)); |
| 1413 | grpc_call_error call_error = grpc_call_start_batch_and_execute( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1414 | glb_policy->lb_call, &op, 1, &glb_policy->client_load_report_closure); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1415 | if (call_error != GRPC_CALL_OK) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1416 | gpr_log(GPR_ERROR, "[grpclb %p] call_error=%d", glb_policy, call_error); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1417 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1418 | } |
| 1419 | } |
| 1420 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1421 | static void lb_on_server_status_received_locked(void* arg, grpc_error* error); |
| 1422 | static void lb_on_response_received_locked(void* arg, grpc_error* error); |
| 1423 | static void lb_call_init_locked(glb_lb_policy* glb_policy) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1424 | GPR_ASSERT(glb_policy->server_name != nullptr); |
David Garcia Quintas | 55ba14a | 2016-09-27 18:45:30 -0700 | [diff] [blame] | 1425 | GPR_ASSERT(glb_policy->server_name[0] != '\0'); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1426 | GPR_ASSERT(glb_policy->lb_call == nullptr); |
David Garcia Quintas | a74b246 | 2016-11-11 14:07:27 -0800 | [diff] [blame] | 1427 | GPR_ASSERT(!glb_policy->shutting_down); |
David Garcia Quintas | 55ba14a | 2016-09-27 18:45:30 -0700 | [diff] [blame] | 1428 | |
David Garcia Quintas | 15eba13 | 2016-08-09 15:20:48 -0700 | [diff] [blame] | 1429 | /* Note the following LB call progresses every time there's activity in \a |
| 1430 | * glb_policy->base.interested_parties, which is comprised of the polling |
Yuchen Zeng | f7c45ae | 2016-09-15 13:40:32 -0700 | [diff] [blame] | 1431 | * entities from \a client_channel. */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1432 | grpc_slice host = grpc_slice_from_copied_string(glb_policy->server_name); |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 1433 | grpc_millis deadline = |
Mark D. Roth | 64d922a | 2017-05-03 12:52:04 -0700 | [diff] [blame] | 1434 | glb_policy->lb_call_timeout_ms == 0 |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 1435 | ? GRPC_MILLIS_INF_FUTURE |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1436 | : grpc_core::ExecCtx::Get()->Now() + glb_policy->lb_call_timeout_ms; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1437 | glb_policy->lb_call = grpc_channel_create_pollset_set_call( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1438 | glb_policy->lb_channel, nullptr, GRPC_PROPAGATE_DEFAULTS, |
David Garcia Quintas | 4543e5c | 2016-09-22 15:09:34 -0700 | [diff] [blame] | 1439 | glb_policy->base.interested_parties, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1440 | GRPC_MDSTR_SLASH_GRPC_DOT_LB_DOT_V1_DOT_LOADBALANCER_SLASH_BALANCELOAD, |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1441 | &host, deadline, nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1442 | grpc_slice_unref_internal(host); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1443 | |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1444 | if (glb_policy->client_stats != nullptr) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1445 | grpc_grpclb_client_stats_unref(glb_policy->client_stats); |
| 1446 | } |
| 1447 | glb_policy->client_stats = grpc_grpclb_client_stats_create(); |
| 1448 | |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1449 | grpc_metadata_array_init(&glb_policy->lb_initial_metadata_recv); |
| 1450 | grpc_metadata_array_init(&glb_policy->lb_trailing_metadata_recv); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1451 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1452 | grpc_grpclb_request* request = |
David Garcia Quintas | 55ba14a | 2016-09-27 18:45:30 -0700 | [diff] [blame] | 1453 | grpc_grpclb_request_create(glb_policy->server_name); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 1454 | grpc_slice request_payload_slice = grpc_grpclb_request_encode(request); |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1455 | glb_policy->lb_request_payload = |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1456 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1457 | grpc_slice_unref_internal(request_payload_slice); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1458 | grpc_grpclb_request_destroy(request); |
| 1459 | |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1460 | GRPC_CLOSURE_INIT(&glb_policy->lb_on_server_status_received, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 1461 | lb_on_server_status_received_locked, glb_policy, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 1462 | grpc_combiner_scheduler(glb_policy->base.combiner)); |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1463 | GRPC_CLOSURE_INIT(&glb_policy->lb_on_response_received, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 1464 | lb_on_response_received_locked, glb_policy, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 1465 | grpc_combiner_scheduler(glb_policy->base.combiner)); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1466 | |
David Garcia Quintas | 0f91e51 | 2017-12-04 16:12:54 -0800 | [diff] [blame] | 1467 | grpc_core::BackOff::Options backoff_options; |
David Garcia Quintas | dde6afc | 2017-11-22 16:31:01 -0800 | [diff] [blame] | 1468 | backoff_options |
| 1469 | .set_initial_backoff(GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS * 1000) |
| 1470 | .set_multiplier(GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER) |
| 1471 | .set_jitter(GRPC_GRPCLB_RECONNECT_JITTER) |
David Garcia Quintas | dde6afc | 2017-11-22 16:31:01 -0800 | [diff] [blame] | 1472 | .set_max_backoff(GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS * 1000); |
David Garcia Quintas | d27e242 | 2017-11-27 12:53:14 -0800 | [diff] [blame] | 1473 | |
| 1474 | glb_policy->lb_call_backoff.Init(backoff_options); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1475 | |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1476 | glb_policy->seen_initial_response = false; |
| 1477 | glb_policy->last_client_load_report_counters_were_zero = false; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1478 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1479 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1480 | static void lb_call_destroy_locked(glb_lb_policy* glb_policy) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1481 | GPR_ASSERT(glb_policy->lb_call != nullptr); |
Craig Tiller | dd36b15 | 2017-03-31 08:27:28 -0700 | [diff] [blame] | 1482 | grpc_call_unref(glb_policy->lb_call); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1483 | glb_policy->lb_call = nullptr; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1484 | |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1485 | grpc_metadata_array_destroy(&glb_policy->lb_initial_metadata_recv); |
| 1486 | grpc_metadata_array_destroy(&glb_policy->lb_trailing_metadata_recv); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1487 | |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1488 | grpc_byte_buffer_destroy(glb_policy->lb_request_payload); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1489 | grpc_slice_unref_internal(glb_policy->lb_call_status_details); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1490 | |
Mark D. Roth | 9247ad5 | 2017-09-25 13:35:48 -0700 | [diff] [blame] | 1491 | if (glb_policy->client_load_report_timer_pending) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1492 | grpc_timer_cancel(&glb_policy->client_load_report_timer); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1493 | } |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1496 | /* |
| 1497 | * Auxiliary functions and LB client callbacks. |
| 1498 | */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1499 | static void query_for_backends_locked(glb_lb_policy* glb_policy) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1500 | GPR_ASSERT(glb_policy->lb_channel != nullptr); |
David Garcia Quintas | a74b246 | 2016-11-11 14:07:27 -0800 | [diff] [blame] | 1501 | if (glb_policy->shutting_down) return; |
| 1502 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1503 | lb_call_init_locked(glb_policy); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1504 | |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1505 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1506 | gpr_log(GPR_INFO, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1507 | "[grpclb %p] Query for backends (lb_channel: %p, lb_call: %p)", |
| 1508 | glb_policy, glb_policy->lb_channel, glb_policy->lb_call); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1509 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1510 | GPR_ASSERT(glb_policy->lb_call != nullptr); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1511 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1512 | grpc_call_error call_error; |
Mark D. Roth | 2de36a8 | 2017-09-25 14:54:44 -0700 | [diff] [blame] | 1513 | grpc_op ops[3]; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1514 | memset(ops, 0, sizeof(ops)); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1515 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1516 | grpc_op* op = ops; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1517 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 1518 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1519 | op->flags = 0; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1520 | op->reserved = nullptr; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1521 | op++; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1522 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1523 | op->data.recv_initial_metadata.recv_initial_metadata = |
| 1524 | &glb_policy->lb_initial_metadata_recv; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1525 | op->flags = 0; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1526 | op->reserved = nullptr; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1527 | op++; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1528 | GPR_ASSERT(glb_policy->lb_request_payload != nullptr); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1529 | op->op = GRPC_OP_SEND_MESSAGE; |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1530 | op->data.send_message.send_message = glb_policy->lb_request_payload; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1531 | op->flags = 0; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1532 | op->reserved = nullptr; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1533 | op++; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1534 | call_error = grpc_call_start_batch_and_execute(glb_policy->lb_call, ops, |
| 1535 | (size_t)(op - ops), nullptr); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1536 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1537 | |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1538 | op = ops; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1539 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 1540 | op->data.recv_status_on_client.trailing_metadata = |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1541 | &glb_policy->lb_trailing_metadata_recv; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1542 | op->data.recv_status_on_client.status = &glb_policy->lb_call_status; |
| 1543 | op->data.recv_status_on_client.status_details = |
| 1544 | &glb_policy->lb_call_status_details; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1545 | op->flags = 0; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1546 | op->reserved = nullptr; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1547 | op++; |
David Garcia Quintas | e224a76 | 2016-11-01 13:00:58 -0700 | [diff] [blame] | 1548 | /* take a weak ref (won't prevent calling of \a glb_shutdown if the strong ref |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1549 | * count goes to zero) to be unref'd in lb_on_server_status_received_locked */ |
| 1550 | GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, |
| 1551 | "lb_on_server_status_received_locked"); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1552 | call_error = grpc_call_start_batch_and_execute( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1553 | glb_policy->lb_call, ops, (size_t)(op - ops), |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1554 | &glb_policy->lb_on_server_status_received); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1555 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 1556 | |
| 1557 | op = ops; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1558 | op->op = GRPC_OP_RECV_MESSAGE; |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1559 | op->data.recv_message.recv_message = &glb_policy->lb_response_payload; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1560 | op->flags = 0; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1561 | op->reserved = nullptr; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1562 | op++; |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1563 | /* take another weak ref to be unref'd/reused in |
| 1564 | * lb_on_response_received_locked */ |
| 1565 | GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "lb_on_response_received_locked"); |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1566 | call_error = grpc_call_start_batch_and_execute( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1567 | glb_policy->lb_call, ops, (size_t)(op - ops), |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1568 | &glb_policy->lb_on_response_received); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 1569 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1572 | static void lb_on_response_received_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1573 | glb_lb_policy* glb_policy = (glb_lb_policy*)arg; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1574 | grpc_op ops[2]; |
| 1575 | memset(ops, 0, sizeof(ops)); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1576 | grpc_op* op = ops; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1577 | if (glb_policy->lb_response_payload != nullptr) { |
David Garcia Quintas | dde6afc | 2017-11-22 16:31:01 -0800 | [diff] [blame] | 1578 | glb_policy->lb_call_backoff->Reset(); |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1579 | /* Received data from the LB server. Look inside |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1580 | * glb_policy->lb_response_payload, for a serverlist. */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1581 | grpc_byte_buffer_reader bbr; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1582 | grpc_byte_buffer_reader_init(&bbr, glb_policy->lb_response_payload); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 1583 | grpc_slice response_slice = grpc_byte_buffer_reader_readall(&bbr); |
David Garcia Quintas | 97e1785 | 2017-08-14 14:55:02 -0700 | [diff] [blame] | 1584 | grpc_byte_buffer_reader_destroy(&bbr); |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 1585 | grpc_byte_buffer_destroy(glb_policy->lb_response_payload); |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1586 | |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1587 | grpc_grpclb_initial_response* response = nullptr; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1588 | if (!glb_policy->seen_initial_response && |
| 1589 | (response = grpc_grpclb_initial_response_parse(response_slice)) != |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1590 | nullptr) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1591 | if (response->has_client_stats_report_interval) { |
Craig Tiller | c0df1c0 | 2017-07-17 16:12:33 -0700 | [diff] [blame] | 1592 | glb_policy->client_stats_report_interval = GPR_MAX( |
| 1593 | GPR_MS_PER_SEC, grpc_grpclb_duration_to_millis( |
| 1594 | &response->client_stats_report_interval)); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1595 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1596 | gpr_log(GPR_INFO, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1597 | "[grpclb %p] Received initial LB response message; " |
Craig Tiller | c0df1c0 | 2017-07-17 16:12:33 -0700 | [diff] [blame] | 1598 | "client load reporting interval = %" PRIdPTR " milliseconds", |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1599 | glb_policy, glb_policy->client_stats_report_interval); |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1600 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1601 | /* take a weak ref (won't prevent calling of \a glb_shutdown() if the |
| 1602 | * strong ref count goes to zero) to be unref'd in |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1603 | * send_client_load_report_locked() */ |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1604 | glb_policy->client_load_report_timer_pending = true; |
| 1605 | GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "client_load_report"); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1606 | schedule_next_client_load_report(glb_policy); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1607 | } else if (grpc_lb_glb_trace.enabled()) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1608 | gpr_log(GPR_INFO, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1609 | "[grpclb %p] Received initial LB response message; client load " |
| 1610 | "reporting NOT enabled", |
| 1611 | glb_policy); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1612 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1613 | grpc_grpclb_initial_response_destroy(response); |
| 1614 | glb_policy->seen_initial_response = true; |
| 1615 | } else { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1616 | grpc_grpclb_serverlist* serverlist = |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1617 | grpc_grpclb_response_parse_serverlist(response_slice); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1618 | if (serverlist != nullptr) { |
| 1619 | GPR_ASSERT(glb_policy->lb_call != nullptr); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1620 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1621 | gpr_log(GPR_INFO, |
| 1622 | "[grpclb %p] Serverlist with %" PRIuPTR " servers received", |
| 1623 | glb_policy, serverlist->num_servers); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1624 | for (size_t i = 0; i < serverlist->num_servers; ++i) { |
| 1625 | grpc_resolved_address addr; |
| 1626 | parse_server(serverlist->servers[i], &addr); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1627 | char* ipport; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1628 | grpc_sockaddr_to_string(&ipport, &addr, false); |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1629 | gpr_log(GPR_INFO, "[grpclb %p] Serverlist[%" PRIuPTR "]: %s", |
| 1630 | glb_policy, i, ipport); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1631 | gpr_free(ipport); |
| 1632 | } |
| 1633 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1634 | /* update serverlist */ |
| 1635 | if (serverlist->num_servers > 0) { |
| 1636 | if (grpc_grpclb_serverlist_equals(glb_policy->serverlist, |
| 1637 | serverlist)) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1638 | if (grpc_lb_glb_trace.enabled()) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1639 | gpr_log(GPR_INFO, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1640 | "[grpclb %p] Incoming server list identical to current, " |
| 1641 | "ignoring.", |
| 1642 | glb_policy); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1643 | } |
| 1644 | grpc_grpclb_destroy_serverlist(serverlist); |
| 1645 | } else { /* new serverlist */ |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1646 | if (glb_policy->serverlist != nullptr) { |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1647 | /* dispose of the old serverlist */ |
| 1648 | grpc_grpclb_destroy_serverlist(glb_policy->serverlist); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1649 | } else { |
| 1650 | /* or dispose of the fallback */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1651 | grpc_lb_addresses_destroy(glb_policy->fallback_backend_addresses); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1652 | glb_policy->fallback_backend_addresses = nullptr; |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1653 | if (glb_policy->fallback_timer_active) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1654 | grpc_timer_cancel(&glb_policy->lb_fallback_timer); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1655 | glb_policy->fallback_timer_active = false; |
| 1656 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1657 | } |
| 1658 | /* and update the copy in the glb_lb_policy instance. This |
| 1659 | * serverlist instance will be destroyed either upon the next |
| 1660 | * update or in glb_destroy() */ |
| 1661 | glb_policy->serverlist = serverlist; |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 1662 | glb_policy->serverlist_index = 0; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1663 | rr_handover_locked(glb_policy); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1664 | } |
| 1665 | } else { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1666 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1667 | gpr_log(GPR_INFO, |
| 1668 | "[grpclb %p] Received empty server list, ignoring.", |
| 1669 | glb_policy); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1670 | } |
| 1671 | grpc_grpclb_destroy_serverlist(serverlist); |
| 1672 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1673 | } else { /* serverlist == nullptr */ |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1674 | gpr_log(GPR_ERROR, |
| 1675 | "[grpclb %p] Invalid LB response received: '%s'. Ignoring.", |
| 1676 | glb_policy, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1677 | grpc_dump_slice(response_slice, GPR_DUMP_ASCII | GPR_DUMP_HEX)); |
| 1678 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1679 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1680 | grpc_slice_unref_internal(response_slice); |
David Garcia Quintas | 246c564 | 2016-11-01 11:16:52 -0700 | [diff] [blame] | 1681 | if (!glb_policy->shutting_down) { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1682 | /* keep listening for serverlist updates */ |
| 1683 | op->op = GRPC_OP_RECV_MESSAGE; |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1684 | op->data.recv_message.recv_message = &glb_policy->lb_response_payload; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1685 | op->flags = 0; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1686 | op->reserved = nullptr; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1687 | op++; |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1688 | /* reuse the "lb_on_response_received_locked" weak ref taken in |
David Garcia Quintas | e224a76 | 2016-11-01 13:00:58 -0700 | [diff] [blame] | 1689 | * query_for_backends_locked() */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1690 | const grpc_call_error call_error = grpc_call_start_batch_and_execute( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1691 | glb_policy->lb_call, ops, (size_t)(op - ops), |
David Garcia Quintas | 246c564 | 2016-11-01 11:16:52 -0700 | [diff] [blame] | 1692 | &glb_policy->lb_on_response_received); /* loop */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1693 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
David Garcia Quintas | 998bd2c | 2017-09-18 12:41:07 -0700 | [diff] [blame] | 1694 | } else { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1695 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, |
David Garcia Quintas | 998bd2c | 2017-09-18 12:41:07 -0700 | [diff] [blame] | 1696 | "lb_on_response_received_locked_shutdown"); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1697 | } |
David Garcia Quintas | e224a76 | 2016-11-01 13:00:58 -0700 | [diff] [blame] | 1698 | } else { /* empty payload: call cancelled. */ |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1699 | /* dispose of the "lb_on_response_received_locked" weak ref taken in |
David Garcia Quintas | e224a76 | 2016-11-01 13:00:58 -0700 | [diff] [blame] | 1700 | * query_for_backends_locked() and reused in every reception loop */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1701 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, |
David Garcia Quintas | c22c65b | 2017-07-25 14:22:20 -0700 | [diff] [blame] | 1702 | "lb_on_response_received_locked_empty_payload"); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1703 | } |
| 1704 | } |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1705 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1706 | static void lb_on_fallback_timer_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1707 | glb_lb_policy* glb_policy = (glb_lb_policy*)arg; |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1708 | glb_policy->fallback_timer_active = false; |
| 1709 | /* If we receive a serverlist after the timer fires but before this callback |
| 1710 | * actually runs, don't fall back. */ |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1711 | if (glb_policy->serverlist == nullptr) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1712 | if (!glb_policy->shutting_down && error == GRPC_ERROR_NONE) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1713 | if (grpc_lb_glb_trace.enabled()) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1714 | gpr_log(GPR_INFO, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1715 | "[grpclb %p] Falling back to use backends from resolver", |
| 1716 | glb_policy); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1717 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1718 | GPR_ASSERT(glb_policy->fallback_backend_addresses != nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1719 | rr_handover_locked(glb_policy); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1720 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1721 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1722 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, "grpclb_fallback_timer"); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1725 | static void lb_on_server_status_received_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1726 | glb_lb_policy* glb_policy = (glb_lb_policy*)arg; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1727 | GPR_ASSERT(glb_policy->lb_call != nullptr); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1728 | if (grpc_lb_glb_trace.enabled()) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1729 | char* status_details = |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1730 | grpc_slice_to_c_string(glb_policy->lb_call_status_details); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1731 | gpr_log(GPR_INFO, |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1732 | "[grpclb %p] Status from LB server received. Status = %d, Details " |
David Garcia Quintas | 2b372e0 | 2017-11-09 14:15:59 -0800 | [diff] [blame] | 1733 | "= '%s', (call: %p), error '%s'", |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1734 | glb_policy, glb_policy->lb_call_status, status_details, |
David Garcia Quintas | 2b372e0 | 2017-11-09 14:15:59 -0800 | [diff] [blame] | 1735 | glb_policy->lb_call, grpc_error_string(error)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1736 | gpr_free(status_details); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1737 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1738 | /* We need to perform cleanups no matter what. */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1739 | lb_call_destroy_locked(glb_policy); |
Mark D. Roth | a4792f5 | 2017-09-26 09:06:35 -0700 | [diff] [blame] | 1740 | // If the load report timer is still pending, we wait for it to be |
| 1741 | // called before restarting the call. Otherwise, we restart the call |
| 1742 | // here. |
| 1743 | if (!glb_policy->client_load_report_timer_pending) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1744 | maybe_restart_lb_call(glb_policy); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 1745 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1748 | static void fallback_update_locked(glb_lb_policy* glb_policy, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1749 | const grpc_lb_addresses* addresses) { |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1750 | GPR_ASSERT(glb_policy->fallback_backend_addresses != nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1751 | grpc_lb_addresses_destroy(glb_policy->fallback_backend_addresses); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1752 | glb_policy->fallback_backend_addresses = |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1753 | extract_backend_addresses_locked(addresses); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1754 | if (glb_policy->lb_fallback_timeout_ms > 0 && |
| 1755 | glb_policy->rr_policy != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1756 | rr_handover_locked(glb_policy); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1757 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1760 | static void glb_update_locked(grpc_lb_policy* policy, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1761 | const grpc_lb_policy_args* args) { |
| 1762 | glb_lb_policy* glb_policy = (glb_lb_policy*)policy; |
| 1763 | const grpc_arg* arg = |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1764 | grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1765 | if (arg == nullptr || arg->type != GRPC_ARG_POINTER) { |
| 1766 | if (glb_policy->lb_channel == nullptr) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1767 | // If we don't have a current channel to the LB, go into TRANSIENT |
| 1768 | // FAILURE. |
| 1769 | grpc_connectivity_state_set( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1770 | &glb_policy->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1771 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Missing update in args"), |
| 1772 | "glb_update_missing"); |
| 1773 | } else { |
| 1774 | // otherwise, keep using the current LB channel (ignore this update). |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1775 | gpr_log( |
| 1776 | GPR_ERROR, |
| 1777 | "[grpclb %p] No valid LB addresses channel arg in update, ignoring.", |
| 1778 | glb_policy); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1779 | } |
| 1780 | return; |
| 1781 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1782 | const grpc_lb_addresses* addresses = |
| 1783 | (const grpc_lb_addresses*)arg->value.pointer.p; |
Mark D. Roth | 97b6e5d | 2017-10-09 08:31:41 -0700 | [diff] [blame] | 1784 | // If a non-empty serverlist hasn't been received from the balancer, |
| 1785 | // propagate the update to fallback_backend_addresses. |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1786 | if (glb_policy->serverlist == nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1787 | fallback_update_locked(glb_policy, addresses); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1788 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1789 | GPR_ASSERT(glb_policy->lb_channel != nullptr); |
Mark D. Roth | 97b6e5d | 2017-10-09 08:31:41 -0700 | [diff] [blame] | 1790 | // Propagate updates to the LB channel (pick_first) through the fake |
| 1791 | // resolver. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1792 | grpc_channel_args* lb_channel_args = build_lb_channel_args( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1793 | addresses, glb_policy->response_generator, args->args); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1794 | grpc_fake_resolver_response_generator_set_response( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1795 | glb_policy->response_generator, lb_channel_args); |
| 1796 | grpc_channel_args_destroy(lb_channel_args); |
Mark D. Roth | 97b6e5d | 2017-10-09 08:31:41 -0700 | [diff] [blame] | 1797 | // Start watching the LB channel connectivity for connection, if not |
| 1798 | // already doing so. |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1799 | if (!glb_policy->watching_lb_channel) { |
David Garcia Quintas | 6a7935e | 2017-07-27 19:24:52 -0700 | [diff] [blame] | 1800 | glb_policy->lb_channel_connectivity = grpc_channel_check_connectivity_state( |
| 1801 | glb_policy->lb_channel, true /* try to connect */); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1802 | grpc_channel_element* client_channel_elem = grpc_channel_stack_last_element( |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1803 | grpc_channel_get_channel_stack(glb_policy->lb_channel)); |
| 1804 | GPR_ASSERT(client_channel_elem->filter == &grpc_client_channel_filter); |
| 1805 | glb_policy->watching_lb_channel = true; |
| 1806 | GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "watch_lb_channel_connectivity"); |
| 1807 | grpc_client_channel_watch_connectivity_state( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1808 | client_channel_elem, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1809 | grpc_polling_entity_create_from_pollset_set( |
| 1810 | glb_policy->base.interested_parties), |
| 1811 | &glb_policy->lb_channel_connectivity, |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1812 | &glb_policy->lb_channel_on_connectivity_changed, nullptr); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | // Invoked as part of the update process. It continues watching the LB channel |
| 1817 | // until it shuts down or becomes READY. It's invoked even if the LB channel |
| 1818 | // stayed READY throughout the update (for example if the update is identical). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1819 | static void glb_lb_channel_on_connectivity_changed_cb(void* arg, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1820 | grpc_error* error) { |
| 1821 | glb_lb_policy* glb_policy = (glb_lb_policy*)arg; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1822 | if (glb_policy->shutting_down) goto done; |
| 1823 | // Re-initialize the lb_call. This should also take care of updating the |
| 1824 | // embedded RR policy. Note that the current RR policy, if any, will stay in |
| 1825 | // effect until an update from the new lb_call is received. |
| 1826 | switch (glb_policy->lb_channel_connectivity) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1827 | case GRPC_CHANNEL_CONNECTING: |
| 1828 | case GRPC_CHANNEL_TRANSIENT_FAILURE: { |
| 1829 | /* resub. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1830 | grpc_channel_element* client_channel_elem = |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1831 | grpc_channel_stack_last_element( |
| 1832 | grpc_channel_get_channel_stack(glb_policy->lb_channel)); |
| 1833 | GPR_ASSERT(client_channel_elem->filter == &grpc_client_channel_filter); |
| 1834 | grpc_client_channel_watch_connectivity_state( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1835 | client_channel_elem, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1836 | grpc_polling_entity_create_from_pollset_set( |
| 1837 | glb_policy->base.interested_parties), |
| 1838 | &glb_policy->lb_channel_connectivity, |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1839 | &glb_policy->lb_channel_on_connectivity_changed, nullptr); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1840 | break; |
| 1841 | } |
| 1842 | case GRPC_CHANNEL_IDLE: |
David Garcia Quintas | 2b217d4 | 2017-10-20 15:56:30 -0700 | [diff] [blame] | 1843 | // lb channel inactive (probably shutdown prior to update). Restart lb |
| 1844 | // call to kick the lb channel into gear. |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1845 | /* fallthrough */ |
| 1846 | case GRPC_CHANNEL_READY: |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1847 | if (glb_policy->lb_call != nullptr) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1848 | glb_policy->updating_lb_call = true; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1849 | grpc_call_cancel(glb_policy->lb_call, nullptr); |
Mark D. Roth | 97b6e5d | 2017-10-09 08:31:41 -0700 | [diff] [blame] | 1850 | // lb_on_server_status_received() will pick up the cancel and reinit |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1851 | // lb_call. |
Juanli Shen | d7ccea8 | 2017-12-04 18:33:41 -0800 | [diff] [blame] | 1852 | } else if (glb_policy->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1853 | if (glb_policy->retry_timer_active) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1854 | grpc_timer_cancel(&glb_policy->lb_call_retry_timer); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1855 | glb_policy->retry_timer_active = false; |
| 1856 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1857 | start_picking_locked(glb_policy); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1858 | } |
| 1859 | /* fallthrough */ |
| 1860 | case GRPC_CHANNEL_SHUTDOWN: |
| 1861 | done: |
| 1862 | glb_policy->watching_lb_channel = false; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1863 | GRPC_LB_POLICY_WEAK_UNREF(&glb_policy->base, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1864 | "watch_lb_channel_connectivity_cb_shutdown"); |
| 1865 | break; |
| 1866 | } |
| 1867 | } |
| 1868 | |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1869 | static void glb_set_reresolve_closure_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1870 | grpc_lb_policy* policy, grpc_closure* request_reresolution) { |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1871 | glb_lb_policy* glb_policy = (glb_lb_policy*)policy; |
| 1872 | GPR_ASSERT(!glb_policy->shutting_down); |
| 1873 | GPR_ASSERT(glb_policy->base.request_reresolution == nullptr); |
| 1874 | if (glb_policy->rr_policy != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1875 | grpc_lb_policy_set_reresolve_closure_locked(glb_policy->rr_policy, |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1876 | request_reresolution); |
| 1877 | } else { |
| 1878 | glb_policy->base.request_reresolution = request_reresolution; |
| 1879 | } |
| 1880 | } |
| 1881 | |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1882 | /* Code wiring the policy with the rest of the core */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1883 | static const grpc_lb_policy_vtable glb_lb_policy_vtable = { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 1884 | glb_destroy, |
| 1885 | glb_shutdown_locked, |
| 1886 | glb_pick_locked, |
| 1887 | glb_cancel_pick_locked, |
| 1888 | glb_cancel_picks_locked, |
| 1889 | glb_ping_one_locked, |
| 1890 | glb_exit_idle_locked, |
| 1891 | glb_check_connectivity_locked, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1892 | glb_notify_on_state_change_locked, |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 1893 | glb_update_locked, |
| 1894 | glb_set_reresolve_closure_locked}; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1895 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1896 | static grpc_lb_policy* glb_create(grpc_lb_policy_factory* factory, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1897 | grpc_lb_policy_args* args) { |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1898 | /* Count the number of gRPC-LB addresses. There must be at least one. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1899 | const grpc_arg* arg = |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1900 | grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1901 | if (arg == nullptr || arg->type != GRPC_ARG_POINTER) { |
| 1902 | return nullptr; |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1903 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1904 | grpc_lb_addresses* addresses = (grpc_lb_addresses*)arg->value.pointer.p; |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1905 | size_t num_grpclb_addrs = 0; |
| 1906 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 1907 | if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs; |
| 1908 | } |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1909 | if (num_grpclb_addrs == 0) return nullptr; |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1910 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1911 | glb_lb_policy* glb_policy = (glb_lb_policy*)gpr_zalloc(sizeof(*glb_policy)); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1912 | |
| 1913 | /* Get server name. */ |
| 1914 | arg = grpc_channel_args_find(args->args, GRPC_ARG_SERVER_URI); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1915 | GPR_ASSERT(arg != nullptr); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1916 | GPR_ASSERT(arg->type == GRPC_ARG_STRING); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1917 | grpc_uri* uri = grpc_uri_parse(arg->value.string, true); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1918 | GPR_ASSERT(uri->path[0] != '\0'); |
| 1919 | glb_policy->server_name = |
| 1920 | gpr_strdup(uri->path[0] == '/' ? uri->path + 1 : uri->path); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1921 | if (grpc_lb_glb_trace.enabled()) { |
David Garcia Quintas | a1c6590 | 2017-11-09 10:37:35 -0800 | [diff] [blame] | 1922 | gpr_log(GPR_INFO, |
| 1923 | "[grpclb %p] Will use '%s' as the server name for LB request.", |
| 1924 | glb_policy, glb_policy->server_name); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1925 | } |
| 1926 | grpc_uri_destroy(uri); |
| 1927 | |
| 1928 | glb_policy->cc_factory = args->client_channel_factory; |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1929 | GPR_ASSERT(glb_policy->cc_factory != nullptr); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1930 | |
| 1931 | arg = grpc_channel_args_find(args->args, GRPC_ARG_GRPCLB_CALL_TIMEOUT_MS); |
| 1932 | glb_policy->lb_call_timeout_ms = |
Yash Tibrewal | d8b84a2 | 2017-09-25 13:38:03 -0700 | [diff] [blame] | 1933 | grpc_channel_arg_get_integer(arg, {0, 0, INT_MAX}); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1934 | |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1935 | arg = grpc_channel_args_find(args->args, GRPC_ARG_GRPCLB_FALLBACK_TIMEOUT_MS); |
| 1936 | glb_policy->lb_fallback_timeout_ms = grpc_channel_arg_get_integer( |
Yash Tibrewal | 1150bfb | 2017-09-28 14:43:41 -0700 | [diff] [blame] | 1937 | arg, {GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS, 0, INT_MAX}); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1938 | |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1939 | // Make sure that GRPC_ARG_LB_POLICY_NAME is set in channel args, |
| 1940 | // since we use this to trigger the client_load_reporting filter. |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 1941 | grpc_arg new_arg = grpc_channel_arg_string_create( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1942 | (char*)GRPC_ARG_LB_POLICY_NAME, (char*)"grpclb"); |
| 1943 | static const char* args_to_remove[] = {GRPC_ARG_LB_POLICY_NAME}; |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1944 | glb_policy->args = grpc_channel_args_copy_and_add_and_remove( |
| 1945 | args->args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove), &new_arg, 1); |
| 1946 | |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1947 | /* Extract the backend addresses (may be empty) from the resolver for |
| 1948 | * fallback. */ |
| 1949 | glb_policy->fallback_backend_addresses = |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1950 | extract_backend_addresses_locked(addresses); |
Juanli Shen | fe40815 | 2017-09-27 12:27:20 -0700 | [diff] [blame] | 1951 | |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1952 | /* Create a client channel over them to communicate with a LB service */ |
| 1953 | glb_policy->response_generator = |
| 1954 | grpc_fake_resolver_response_generator_create(); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1955 | grpc_channel_args* lb_channel_args = build_lb_channel_args( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1956 | addresses, glb_policy->response_generator, args->args); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1957 | char* uri_str; |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1958 | gpr_asprintf(&uri_str, "fake:///%s", glb_policy->server_name); |
| 1959 | glb_policy->lb_channel = grpc_lb_policy_grpclb_create_lb_channel( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1960 | uri_str, args->client_channel_factory, lb_channel_args); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1961 | |
| 1962 | /* Propagate initial resolution */ |
| 1963 | grpc_fake_resolver_response_generator_set_response( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1964 | glb_policy->response_generator, lb_channel_args); |
| 1965 | grpc_channel_args_destroy(lb_channel_args); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1966 | gpr_free(uri_str); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1967 | if (glb_policy->lb_channel == nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1968 | gpr_free((void*)glb_policy->server_name); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1969 | grpc_channel_args_destroy(glb_policy->args); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1970 | gpr_free(glb_policy); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 1971 | return nullptr; |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1972 | } |
Ken Payson | 9fa10cc | 2017-09-14 11:49:52 -0700 | [diff] [blame] | 1973 | grpc_subchannel_index_ref(); |
Yash Tibrewal | a495220 | 2017-09-13 10:53:28 -0700 | [diff] [blame] | 1974 | GRPC_CLOSURE_INIT(&glb_policy->lb_channel_on_connectivity_changed, |
| 1975 | glb_lb_channel_on_connectivity_changed_cb, glb_policy, |
| 1976 | grpc_combiner_scheduler(args->combiner)); |
| 1977 | grpc_lb_policy_init(&glb_policy->base, &glb_lb_policy_vtable, args->combiner); |
| 1978 | grpc_connectivity_state_init(&glb_policy->state_tracker, GRPC_CHANNEL_IDLE, |
| 1979 | "grpclb"); |
| 1980 | return &glb_policy->base; |
| 1981 | } |
| 1982 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1983 | static void glb_factory_ref(grpc_lb_policy_factory* factory) {} |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1984 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1985 | static void glb_factory_unref(grpc_lb_policy_factory* factory) {} |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1986 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1987 | static const grpc_lb_policy_factory_vtable glb_factory_vtable = { |
| 1988 | glb_factory_ref, glb_factory_unref, glb_create, "grpclb"}; |
| 1989 | |
| 1990 | static grpc_lb_policy_factory glb_lb_policy_factory = {&glb_factory_vtable}; |
| 1991 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1992 | grpc_lb_policy_factory* grpc_glb_lb_factory_create() { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1993 | return &glb_lb_policy_factory; |
| 1994 | } |
| 1995 | |
| 1996 | /* Plugin registration */ |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1997 | |
| 1998 | // Only add client_load_reporting filter if the grpclb LB policy is used. |
| 1999 | static bool maybe_add_client_load_reporting_filter( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2000 | grpc_channel_stack_builder* builder, void* arg) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2001 | const grpc_channel_args* args = |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 2002 | grpc_channel_stack_builder_get_channel_arguments(builder); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2003 | const grpc_arg* channel_arg = |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 2004 | grpc_channel_args_find(args, GRPC_ARG_LB_POLICY_NAME); |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 2005 | if (channel_arg != nullptr && channel_arg->type == GRPC_ARG_STRING && |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 2006 | strcmp(channel_arg->value.string, "grpclb") == 0) { |
| 2007 | return grpc_channel_stack_builder_append_filter( |
Noah Eisen | 882dfed | 2017-11-14 14:58:20 -0800 | [diff] [blame] | 2008 | builder, (const grpc_channel_filter*)arg, nullptr, nullptr); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 2009 | } |
| 2010 | return true; |
| 2011 | } |
| 2012 | |
ncteisen | adbfbd5 | 2017-11-16 15:35:45 -0800 | [diff] [blame] | 2013 | void grpc_lb_policy_grpclb_init() { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 2014 | grpc_register_lb_policy(grpc_glb_lb_factory_create()); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 2015 | grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL, |
| 2016 | GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, |
| 2017 | maybe_add_client_load_reporting_filter, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2018 | (void*)&grpc_client_load_reporting_filter); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
ncteisen | adbfbd5 | 2017-11-16 15:35:45 -0800 | [diff] [blame] | 2021 | void grpc_lb_policy_grpclb_shutdown() {} |