Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -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 |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -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 |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -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. |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
| 21 | #include <grpc/support/alloc.h> |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 22 | |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 23 | #include "src/core/ext/filters/client_channel/lb_policy/subchannel_list.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 24 | #include "src/core/ext/filters/client_channel/lb_policy_registry.h" |
| 25 | #include "src/core/ext/filters/client_channel/subchannel.h" |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 26 | #include "src/core/ext/filters/client_channel/subchannel_index.h" |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 27 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 28 | #include "src/core/lib/iomgr/combiner.h" |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 29 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 30 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 31 | |
ncteisen | 7712c7c | 2017-07-12 23:11:27 -0700 | [diff] [blame] | 32 | grpc_tracer_flag grpc_lb_pick_first_trace = |
| 33 | GRPC_TRACER_INITIALIZER(false, "pick_first"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 34 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 35 | typedef struct pending_pick { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 36 | struct pending_pick* next; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 37 | uint32_t initial_metadata_flags; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 38 | grpc_connected_subchannel** target; |
| 39 | grpc_closure* on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 40 | } pending_pick; |
| 41 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 42 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 43 | /** base policy: must be first */ |
| 44 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 45 | /** all our subchannels */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 46 | grpc_lb_subchannel_list* subchannel_list; |
Mark D. Roth | 5132d0e | 2017-10-13 13:20:52 -0700 | [diff] [blame] | 47 | /** latest pending subchannel list */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 48 | grpc_lb_subchannel_list* latest_pending_subchannel_list; |
Mark D. Roth | 5132d0e | 2017-10-13 13:20:52 -0700 | [diff] [blame] | 49 | /** selected subchannel in \a subchannel_list */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 50 | grpc_lb_subchannel_data* selected; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 51 | /** have we started picking? */ |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 52 | bool started_picking; |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 53 | /** are we shut down? */ |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 54 | bool shutdown; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 55 | /** list of picks that are waiting on connectivity */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 56 | pending_pick* pending_picks; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 57 | /** our connectivity state tracker */ |
| 58 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 59 | } pick_first_lb_policy; |
| 60 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 61 | static void pf_destroy(grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol) { |
| 62 | pick_first_lb_policy* p = (pick_first_lb_policy*)pol; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 63 | GPR_ASSERT(p->subchannel_list == nullptr); |
| 64 | GPR_ASSERT(p->latest_pending_subchannel_list == nullptr); |
| 65 | GPR_ASSERT(p->pending_picks == nullptr); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 66 | grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 67 | gpr_free(p); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 68 | grpc_subchannel_index_unref(); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 69 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 70 | gpr_log(GPR_DEBUG, "Pick First %p destroyed.", (void*)p); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 71 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 74 | static void shutdown_locked(grpc_exec_ctx* exec_ctx, pick_first_lb_policy* p, |
| 75 | grpc_error* error) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 76 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 77 | gpr_log(GPR_DEBUG, "Pick First %p Shutting down", p); |
| 78 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 79 | p->shutdown = true; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 80 | pending_pick* pp; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 81 | while ((pp = p->pending_picks) != nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 82 | p->pending_picks = pp->next; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 83 | *pp->target = nullptr; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 84 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, GRPC_ERROR_REF(error)); |
| 85 | gpr_free(pp); |
| 86 | } |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 87 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 88 | GRPC_CHANNEL_SHUTDOWN, GRPC_ERROR_REF(error), |
| 89 | "shutdown"); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 90 | if (p->subchannel_list != nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 91 | grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list, |
| 92 | "pf_shutdown"); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 93 | p->subchannel_list = nullptr; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 94 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 95 | if (p->latest_pending_subchannel_list != nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 96 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 97 | exec_ctx, p->latest_pending_subchannel_list, "pf_shutdown"); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 98 | p->latest_pending_subchannel_list = nullptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 100 | GRPC_ERROR_UNREF(error); |
| 101 | } |
| 102 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 103 | static void pf_shutdown_locked(grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol) { |
| 104 | shutdown_locked(exec_ctx, (pick_first_lb_policy*)pol, |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 105 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel shutdown")); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 108 | static void pf_cancel_pick_locked(grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol, |
| 109 | grpc_connected_subchannel** target, |
| 110 | grpc_error* error) { |
| 111 | pick_first_lb_policy* p = (pick_first_lb_policy*)pol; |
| 112 | pending_pick* pp = p->pending_picks; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 113 | p->pending_picks = nullptr; |
| 114 | while (pp != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 115 | pending_pick* next = pp->next; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 116 | if (pp->target == target) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 117 | *target = nullptr; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 118 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 119 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 120 | "Pick Cancelled", &error, 1)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 121 | gpr_free(pp); |
| 122 | } else { |
| 123 | pp->next = p->pending_picks; |
| 124 | p->pending_picks = pp; |
| 125 | } |
| 126 | pp = next; |
| 127 | } |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 128 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 131 | static void pf_cancel_picks_locked(grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 132 | uint32_t initial_metadata_flags_mask, |
| 133 | uint32_t initial_metadata_flags_eq, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 134 | grpc_error* error) { |
| 135 | pick_first_lb_policy* p = (pick_first_lb_policy*)pol; |
| 136 | pending_pick* pp = p->pending_picks; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 137 | p->pending_picks = nullptr; |
| 138 | while (pp != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 139 | pending_pick* next = pp->next; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 140 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 141 | initial_metadata_flags_eq) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 142 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 143 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 144 | "Pick Cancelled", &error, 1)); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 145 | gpr_free(pp); |
| 146 | } else { |
| 147 | pp->next = p->pending_picks; |
| 148 | p->pending_picks = pp; |
| 149 | } |
| 150 | pp = next; |
| 151 | } |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 152 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 155 | static void start_picking_locked(grpc_exec_ctx* exec_ctx, |
| 156 | pick_first_lb_policy* p) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 157 | p->started_picking = true; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 158 | if (p->subchannel_list != nullptr && p->subchannel_list->num_subchannels > 0) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 159 | p->subchannel_list->checking_subchannel = 0; |
| 160 | grpc_lb_subchannel_list_ref_for_connectivity_watch( |
| 161 | p->subchannel_list, "connectivity_watch+start_picking"); |
| 162 | grpc_lb_subchannel_data_start_connectivity_watch( |
| 163 | exec_ctx, &p->subchannel_list->subchannels[0]); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 164 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 167 | static void pf_exit_idle_locked(grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol) { |
| 168 | pick_first_lb_policy* p = (pick_first_lb_policy*)pol; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 169 | if (!p->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 170 | start_picking_locked(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 174 | static int pf_pick_locked(grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol, |
| 175 | const grpc_lb_policy_pick_args* pick_args, |
| 176 | grpc_connected_subchannel** target, |
| 177 | grpc_call_context_element* context, void** user_data, |
| 178 | grpc_closure* on_complete) { |
| 179 | pick_first_lb_policy* p = (pick_first_lb_policy*)pol; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 180 | // If we have a selected subchannel already, return synchronously. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 181 | if (p->selected != nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 182 | *target = GRPC_CONNECTED_SUBCHANNEL_REF(p->selected->connected_subchannel, |
| 183 | "picked"); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 184 | return 1; |
| 185 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 186 | // No subchannel selected yet, so handle asynchronously. |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 187 | if (!p->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 188 | start_picking_locked(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 190 | pending_pick* pp = (pending_pick*)gpr_malloc(sizeof(*pp)); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 191 | pp->next = p->pending_picks; |
| 192 | pp->target = target; |
| 193 | pp->initial_metadata_flags = pick_args->initial_metadata_flags; |
| 194 | pp->on_complete = on_complete; |
| 195 | p->pending_picks = pp; |
| 196 | return 0; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 199 | static void destroy_unselected_subchannels_locked(grpc_exec_ctx* exec_ctx, |
| 200 | pick_first_lb_policy* p) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 201 | for (size_t i = 0; i < p->subchannel_list->num_subchannels; ++i) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 202 | grpc_lb_subchannel_data* sd = &p->subchannel_list->subchannels[i]; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 203 | if (p->selected != sd) { |
| 204 | grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, |
| 205 | "selected_different_subchannel"); |
| 206 | } |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 207 | } |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 208 | } |
| 209 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 210 | static grpc_connectivity_state pf_check_connectivity_locked( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 211 | grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol, grpc_error** error) { |
| 212 | pick_first_lb_policy* p = (pick_first_lb_policy*)pol; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 213 | return grpc_connectivity_state_get(&p->state_tracker, error); |
| 214 | } |
| 215 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 216 | static void pf_notify_on_state_change_locked(grpc_exec_ctx* exec_ctx, |
| 217 | grpc_lb_policy* pol, |
| 218 | grpc_connectivity_state* current, |
| 219 | grpc_closure* notify) { |
| 220 | pick_first_lb_policy* p = (pick_first_lb_policy*)pol; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 221 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 222 | current, notify); |
| 223 | } |
| 224 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 225 | static void pf_ping_one_locked(grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol, |
| 226 | grpc_closure* closure) { |
| 227 | pick_first_lb_policy* p = (pick_first_lb_policy*)pol; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 228 | if (p->selected) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 229 | grpc_connected_subchannel_ping(exec_ctx, p->selected->connected_subchannel, |
| 230 | closure); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 231 | } else { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 232 | GRPC_CLOSURE_SCHED(exec_ctx, closure, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 233 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Not connected")); |
| 234 | } |
| 235 | } |
| 236 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 237 | static void pf_connectivity_changed_locked(grpc_exec_ctx* exec_ctx, void* arg, |
| 238 | grpc_error* error); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 239 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 240 | static void pf_update_locked(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy, |
| 241 | const grpc_lb_policy_args* args) { |
| 242 | pick_first_lb_policy* p = (pick_first_lb_policy*)policy; |
| 243 | const grpc_arg* arg = |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 244 | grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 245 | if (arg == nullptr || arg->type != GRPC_ARG_POINTER) { |
| 246 | if (p->subchannel_list == nullptr) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 247 | // If we don't have a current subchannel list, go into TRANSIENT FAILURE. |
| 248 | grpc_connectivity_state_set( |
| 249 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 250 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Missing update in args"), |
| 251 | "pf_update_missing"); |
| 252 | } else { |
| 253 | // otherwise, keep using the current subchannel list (ignore this update). |
| 254 | gpr_log(GPR_ERROR, |
| 255 | "No valid LB addresses channel arg for Pick First %p update, " |
| 256 | "ignoring.", |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 257 | (void*)p); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 258 | } |
| 259 | return; |
| 260 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 261 | const grpc_lb_addresses* addresses = |
| 262 | (const grpc_lb_addresses*)arg->value.pointer.p; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 263 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 264 | gpr_log(GPR_INFO, "Pick First %p received update with %lu addresses", |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 265 | (void*)p, (unsigned long)addresses->num_addresses); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 266 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 267 | grpc_lb_subchannel_list* subchannel_list = grpc_lb_subchannel_list_create( |
Mark D. Roth | 901bb4f | 2017-10-11 08:49:07 -0700 | [diff] [blame] | 268 | exec_ctx, &p->base, &grpc_lb_pick_first_trace, addresses, args, |
| 269 | pf_connectivity_changed_locked); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 270 | if (subchannel_list->num_subchannels == 0) { |
| 271 | // Empty update or no valid subchannels. Unsubscribe from all current |
| 272 | // subchannels and put the channel in TRANSIENT_FAILURE. |
| 273 | grpc_connectivity_state_set( |
| 274 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 275 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Empty update"), |
| 276 | "pf_update_empty"); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 277 | if (p->subchannel_list != nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 278 | grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list, |
| 279 | "sl_shutdown_empty_update"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 280 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 281 | p->subchannel_list = subchannel_list; // Empty list. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 282 | p->selected = nullptr; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 283 | return; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 284 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 285 | if (p->selected == nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 286 | // We don't yet have a selected subchannel, so replace the current |
| 287 | // subchannel list immediately. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 288 | if (p->subchannel_list != nullptr) { |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 289 | grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list, |
| 290 | "pf_update_before_selected"); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 291 | } |
| 292 | p->subchannel_list = subchannel_list; |
| 293 | } else { |
| 294 | // We do have a selected subchannel. |
| 295 | // Check if it's present in the new list. If so, we're done. |
| 296 | for (size_t i = 0; i < subchannel_list->num_subchannels; ++i) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 297 | grpc_lb_subchannel_data* sd = &subchannel_list->subchannels[i]; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 298 | if (sd->subchannel == p->selected->subchannel) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 299 | // The currently selected subchannel is in the update: we are done. |
| 300 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 301 | gpr_log(GPR_INFO, |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 302 | "Pick First %p found already selected subchannel %p " |
Mark D. Roth | e9b1083 | 2017-10-26 13:18:25 -0700 | [diff] [blame] | 303 | "at update index %" PRIuPTR " of %" PRIuPTR "; update done", |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 304 | p, p->selected->subchannel, i, |
| 305 | subchannel_list->num_subchannels); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 306 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 307 | grpc_lb_subchannel_list_ref_for_connectivity_watch( |
| 308 | subchannel_list, "connectivity_watch+replace_selected"); |
| 309 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 310 | if (p->subchannel_list != nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 311 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 312 | exec_ctx, p->subchannel_list, "pf_update_includes_selected"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 313 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 314 | p->subchannel_list = subchannel_list; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 315 | if (p->selected->connected_subchannel != nullptr) { |
Mark D. Roth | 6e5ce72 | 2017-10-27 09:37:41 -0700 | [diff] [blame] | 316 | sd->connected_subchannel = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 317 | p->selected->connected_subchannel, "pf_update_includes_selected"); |
| 318 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 319 | p->selected = sd; |
| 320 | destroy_unselected_subchannels_locked(exec_ctx, p); |
| 321 | // If there was a previously pending update (which may or may |
| 322 | // not have contained the currently selected subchannel), drop |
| 323 | // it, so that it doesn't override what we've done here. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 324 | if (p->latest_pending_subchannel_list != nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 325 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 326 | exec_ctx, p->latest_pending_subchannel_list, |
| 327 | "pf_update_includes_selected+outdated"); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 328 | p->latest_pending_subchannel_list = nullptr; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 329 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 333 | // Not keeping the previous selected subchannel, so set the latest |
| 334 | // pending subchannel list to the new subchannel list. We will wait |
| 335 | // for it to report READY before swapping it into the current |
| 336 | // subchannel list. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 337 | if (p->latest_pending_subchannel_list != nullptr) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 338 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 339 | gpr_log(GPR_DEBUG, |
| 340 | "Pick First %p Shutting down latest pending subchannel list " |
| 341 | "%p, about to be replaced by newer latest %p", |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 342 | (void*)p, (void*)p->latest_pending_subchannel_list, |
| 343 | (void*)subchannel_list); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 344 | } |
| 345 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 346 | exec_ctx, p->latest_pending_subchannel_list, |
| 347 | "sl_outdated_dont_smash"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 348 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 349 | p->latest_pending_subchannel_list = subchannel_list; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 350 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 351 | // If we've started picking, start trying to connect to the first |
| 352 | // subchannel in the new list. |
Mark D. Roth | 5132d0e | 2017-10-13 13:20:52 -0700 | [diff] [blame] | 353 | if (p->started_picking) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 354 | grpc_lb_subchannel_list_ref_for_connectivity_watch( |
| 355 | subchannel_list, "connectivity_watch+update"); |
| 356 | grpc_lb_subchannel_data_start_connectivity_watch( |
| 357 | exec_ctx, &subchannel_list->subchannels[0]); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 361 | static void pf_connectivity_changed_locked(grpc_exec_ctx* exec_ctx, void* arg, |
| 362 | grpc_error* error) { |
| 363 | grpc_lb_subchannel_data* sd = (grpc_lb_subchannel_data*)arg; |
| 364 | pick_first_lb_policy* p = (pick_first_lb_policy*)sd->subchannel_list->policy; |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 365 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 366 | gpr_log(GPR_DEBUG, |
Mark D. Roth | e9b1083 | 2017-10-26 13:18:25 -0700 | [diff] [blame] | 367 | "Pick First %p connectivity changed for subchannel %p (%" PRIuPTR |
| 368 | " of %" PRIuPTR |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 369 | "), subchannel_list %p: state=%s p->shutdown=%d " |
| 370 | "sd->subchannel_list->shutting_down=%d error=%s", |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 371 | (void*)p, (void*)sd->subchannel, |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 372 | sd->subchannel_list->checking_subchannel, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 373 | sd->subchannel_list->num_subchannels, (void*)sd->subchannel_list, |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 374 | grpc_connectivity_state_name(sd->pending_connectivity_state_unsafe), |
| 375 | p->shutdown, sd->subchannel_list->shutting_down, |
| 376 | grpc_error_string(error)); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 377 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 378 | // If the policy is shutting down, unref and return. |
| 379 | if (p->shutdown) { |
| 380 | grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd); |
| 381 | grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, "pf_shutdown"); |
| 382 | grpc_lb_subchannel_list_unref_for_connectivity_watch( |
| 383 | exec_ctx, sd->subchannel_list, "pf_shutdown"); |
| 384 | return; |
| 385 | } |
| 386 | // If the subchannel list is shutting down, stop watching. |
| 387 | if (sd->subchannel_list->shutting_down || error == GRPC_ERROR_CANCELLED) { |
| 388 | grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd); |
| 389 | grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, "pf_sl_shutdown"); |
| 390 | grpc_lb_subchannel_list_unref_for_connectivity_watch( |
| 391 | exec_ctx, sd->subchannel_list, "pf_sl_shutdown"); |
| 392 | return; |
| 393 | } |
| 394 | // If we're still here, the notification must be for a subchannel in |
| 395 | // either the current or latest pending subchannel lists. |
| 396 | GPR_ASSERT(sd->subchannel_list == p->subchannel_list || |
| 397 | sd->subchannel_list == p->latest_pending_subchannel_list); |
Mark D. Roth | 5132d0e | 2017-10-13 13:20:52 -0700 | [diff] [blame] | 398 | // Update state. |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 399 | sd->curr_connectivity_state = sd->pending_connectivity_state_unsafe; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 400 | // Handle updates for the currently selected subchannel. |
| 401 | if (p->selected == sd) { |
| 402 | // If the new state is anything other than READY and there is a |
| 403 | // pending update, switch to the pending update. |
| 404 | if (sd->curr_connectivity_state != GRPC_CHANNEL_READY && |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 405 | p->latest_pending_subchannel_list != nullptr) { |
| 406 | p->selected = nullptr; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 407 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 408 | exec_ctx, p->subchannel_list, "selected_not_ready+switch_to_update"); |
| 409 | p->subchannel_list = p->latest_pending_subchannel_list; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 410 | p->latest_pending_subchannel_list = nullptr; |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 411 | grpc_connectivity_state_set( |
Mark D. Roth | 99f54e1 | 2017-10-16 09:55:53 -0700 | [diff] [blame] | 412 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 413 | GRPC_ERROR_REF(error), "selected_not_ready+switch_to_update"); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 414 | } else { |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 415 | if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 416 | /* if the selected channel goes bad, we're done */ |
| 417 | sd->curr_connectivity_state = GRPC_CHANNEL_SHUTDOWN; |
| 418 | } |
| 419 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 420 | sd->curr_connectivity_state, |
| 421 | GRPC_ERROR_REF(error), "selected_changed"); |
| 422 | if (sd->curr_connectivity_state != GRPC_CHANNEL_SHUTDOWN) { |
| 423 | // Renew notification. |
| 424 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 425 | } else { |
| 426 | grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd); |
| 427 | grpc_lb_subchannel_list_unref_for_connectivity_watch( |
| 428 | exec_ctx, sd->subchannel_list, "pf_selected_shutdown"); |
Mark D. Roth | aadf9f4 | 2017-10-19 07:53:53 -0700 | [diff] [blame] | 429 | shutdown_locked(exec_ctx, p, GRPC_ERROR_REF(error)); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 430 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 431 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 434 | // If we get here, there are two possible cases: |
| 435 | // 1. We do not currently have a selected subchannel, and the update is |
| 436 | // for a subchannel in p->subchannel_list that we're trying to |
| 437 | // connect to. The goal here is to find a subchannel that we can |
| 438 | // select. |
| 439 | // 2. We do currently have a selected subchannel, and the update is |
| 440 | // for a subchannel in p->latest_pending_subchannel_list. The |
| 441 | // goal here is to find a subchannel from the update that we can |
| 442 | // select in place of the current one. |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 443 | if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 444 | sd->curr_connectivity_state == GRPC_CHANNEL_SHUTDOWN) { |
| 445 | grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd); |
| 446 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 447 | while (true) { |
| 448 | switch (sd->curr_connectivity_state) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 449 | case GRPC_CHANNEL_READY: { |
| 450 | // Case 2. Promote p->latest_pending_subchannel_list to |
| 451 | // p->subchannel_list. |
| 452 | if (sd->subchannel_list == p->latest_pending_subchannel_list) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 453 | GPR_ASSERT(p->subchannel_list != nullptr); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 454 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 455 | exec_ctx, p->subchannel_list, "finish_update"); |
| 456 | p->subchannel_list = p->latest_pending_subchannel_list; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 457 | p->latest_pending_subchannel_list = nullptr; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 458 | } |
| 459 | // Cases 1 and 2. |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 460 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 461 | GRPC_CHANNEL_READY, GRPC_ERROR_NONE, |
| 462 | "connecting_ready"); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 463 | sd->connected_subchannel = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 464 | grpc_subchannel_get_connected_subchannel(sd->subchannel), |
| 465 | "connected"); |
| 466 | p->selected = sd; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 467 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 468 | gpr_log(GPR_INFO, "Pick First %p selected subchannel %p", (void*)p, |
| 469 | (void*)sd->subchannel); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 470 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 471 | // Drop all other subchannels, since we are now connected. |
| 472 | destroy_unselected_subchannels_locked(exec_ctx, p); |
| 473 | // Update any calls that were waiting for a pick. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 474 | pending_pick* pp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 475 | while ((pp = p->pending_picks)) { |
| 476 | p->pending_picks = pp->next; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 477 | *pp->target = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 478 | p->selected->connected_subchannel, "picked"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 479 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 480 | gpr_log(GPR_INFO, |
| 481 | "Servicing pending pick with selected subchannel %p", |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 482 | (void*)p->selected); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 483 | } |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 484 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 485 | gpr_free(pp); |
| 486 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 487 | // Renew notification. |
| 488 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 489 | return; |
| 490 | } |
| 491 | case GRPC_CHANNEL_TRANSIENT_FAILURE: { |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 492 | do { |
| 493 | sd->subchannel_list->checking_subchannel = |
| 494 | (sd->subchannel_list->checking_subchannel + 1) % |
| 495 | sd->subchannel_list->num_subchannels; |
| 496 | sd = &sd->subchannel_list |
| 497 | ->subchannels[sd->subchannel_list->checking_subchannel]; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 498 | } while (sd->subchannel == nullptr); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 499 | // Case 1: Only set state to TRANSIENT_FAILURE if we've tried |
| 500 | // all subchannels. |
| 501 | if (sd->subchannel_list->checking_subchannel == 0 && |
| 502 | sd->subchannel_list == p->subchannel_list) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 503 | grpc_connectivity_state_set( |
| 504 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 505 | GRPC_ERROR_REF(error), "connecting_transient_failure"); |
Craig Tiller | 131b6de | 2016-03-31 17:05:28 -0700 | [diff] [blame] | 506 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 507 | sd->curr_connectivity_state = |
| 508 | grpc_subchannel_check_connectivity(sd->subchannel, &error); |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 509 | GRPC_ERROR_UNREF(error); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 510 | if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 511 | // Reuses the connectivity refs from the previous watch. |
| 512 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 513 | return; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 514 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 515 | break; // Go back to top of loop. |
| 516 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 517 | case GRPC_CHANNEL_CONNECTING: |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 518 | case GRPC_CHANNEL_IDLE: { |
| 519 | // Only update connectivity state in case 1. |
| 520 | if (sd->subchannel_list == p->subchannel_list) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 521 | grpc_connectivity_state_set( |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 522 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING, |
| 523 | GRPC_ERROR_REF(error), "connecting_changed"); |
| 524 | } |
| 525 | // Renew notification. |
| 526 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 527 | return; |
| 528 | } |
| 529 | case GRPC_CHANNEL_SHUTDOWN: { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 530 | grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, |
| 531 | "pf_candidate_shutdown"); |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 532 | // Advance to next subchannel and check its state. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 533 | grpc_lb_subchannel_data* original_sd = sd; |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 534 | do { |
| 535 | sd->subchannel_list->checking_subchannel = |
| 536 | (sd->subchannel_list->checking_subchannel + 1) % |
| 537 | sd->subchannel_list->num_subchannels; |
| 538 | sd = &sd->subchannel_list |
| 539 | ->subchannels[sd->subchannel_list->checking_subchannel]; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 540 | } while (sd->subchannel == nullptr && sd != original_sd); |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 541 | if (sd == original_sd) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 542 | grpc_lb_subchannel_list_unref_for_connectivity_watch( |
| 543 | exec_ctx, sd->subchannel_list, "pf_candidate_shutdown"); |
| 544 | shutdown_locked(exec_ctx, p, |
| 545 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 546 | "Pick first exhausted channels", &error, 1)); |
| 547 | return; |
| 548 | } |
| 549 | if (sd->subchannel_list == p->subchannel_list) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 550 | grpc_connectivity_state_set( |
| 551 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 552 | GRPC_ERROR_REF(error), "subchannel_failed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 553 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 554 | sd->curr_connectivity_state = |
| 555 | grpc_subchannel_check_connectivity(sd->subchannel, &error); |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 556 | GRPC_ERROR_UNREF(error); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 557 | if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 558 | // Reuses the connectivity refs from the previous watch. |
| 559 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 560 | return; |
| 561 | } |
| 562 | // For any other state, go back to top of loop. |
| 563 | // We will reuse the connectivity refs from the previous watch. |
| 564 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 565 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 566 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 570 | pf_destroy, |
| 571 | pf_shutdown_locked, |
| 572 | pf_pick_locked, |
| 573 | pf_cancel_pick_locked, |
| 574 | pf_cancel_picks_locked, |
| 575 | pf_ping_one_locked, |
| 576 | pf_exit_idle_locked, |
| 577 | pf_check_connectivity_locked, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 578 | pf_notify_on_state_change_locked, |
| 579 | pf_update_locked}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 580 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 581 | static void pick_first_factory_ref(grpc_lb_policy_factory* factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 582 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 583 | static void pick_first_factory_unref(grpc_lb_policy_factory* factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 584 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 585 | static grpc_lb_policy* create_pick_first(grpc_exec_ctx* exec_ctx, |
| 586 | grpc_lb_policy_factory* factory, |
| 587 | grpc_lb_policy_args* args) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 588 | GPR_ASSERT(args->client_channel_factory != nullptr); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 589 | pick_first_lb_policy* p = (pick_first_lb_policy*)gpr_zalloc(sizeof(*p)); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 590 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 591 | gpr_log(GPR_DEBUG, "Pick First %p created.", (void*)p); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 592 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 593 | pf_update_locked(exec_ctx, &p->base, args); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 594 | grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable, args->combiner); |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 595 | grpc_subchannel_index_ref(); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 596 | return &p->base; |
| 597 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 598 | |
| 599 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 600 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 601 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 602 | |
| 603 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 604 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 605 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 606 | static grpc_lb_policy_factory* pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 607 | return &pick_first_lb_policy_factory; |
| 608 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 609 | |
| 610 | /* Plugin registration */ |
| 611 | |
Yash Tibrewal | 8306284 | 2017-09-21 18:56:08 -0700 | [diff] [blame] | 612 | extern "C" void grpc_lb_policy_pick_first_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 613 | grpc_register_lb_policy(pick_first_lb_factory_create()); |
ncteisen | 06bce6e | 2017-07-10 07:58:49 -0700 | [diff] [blame] | 614 | grpc_register_tracer(&grpc_lb_pick_first_trace); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Yash Tibrewal | 8306284 | 2017-09-21 18:56:08 -0700 | [diff] [blame] | 617 | extern "C" void grpc_lb_policy_pick_first_shutdown() {} |