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