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