Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Yash Tibrewal | 37fdb73 | 2017-09-25 16:45:02 -0700 | [diff] [blame] | 19 | #include <grpc/support/port_platform.h> |
| 20 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 21 | #include "src/core/ext/filters/client_channel/client_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 22 | |
Yash Tibrewal | fcd26bc | 2017-09-25 15:08:28 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 26 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 27 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 28 | #include <grpc/support/alloc.h> |
| 29 | #include <grpc/support/log.h> |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 30 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 31 | #include <grpc/support/sync.h> |
| 32 | #include <grpc/support/useful.h> |
| 33 | |
Yuchen Zeng | 0bad30a | 2017-10-05 21:47:39 -0700 | [diff] [blame] | 34 | #include "src/core/ext/filters/client_channel/backup_poller.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 35 | #include "src/core/ext/filters/client_channel/http_connect_handshaker.h" |
| 36 | #include "src/core/ext/filters/client_channel/lb_policy_registry.h" |
| 37 | #include "src/core/ext/filters/client_channel/proxy_mapper_registry.h" |
| 38 | #include "src/core/ext/filters/client_channel/resolver_registry.h" |
| 39 | #include "src/core/ext/filters/client_channel/retry_throttle.h" |
| 40 | #include "src/core/ext/filters/client_channel/subchannel.h" |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 41 | #include "src/core/ext/filters/deadline/deadline_filter.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 42 | #include "src/core/lib/channel/channel_args.h" |
| 43 | #include "src/core/lib/channel/connected_channel.h" |
Mark D. Roth | dbdf495 | 2018-01-18 11:21:12 -0800 | [diff] [blame] | 44 | #include "src/core/lib/gpr/string.h" |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 45 | #include "src/core/lib/iomgr/combiner.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 46 | #include "src/core/lib/iomgr/iomgr.h" |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 47 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 48 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 49 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 50 | #include "src/core/lib/surface/channel.h" |
| 51 | #include "src/core/lib/transport/connectivity_state.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 52 | #include "src/core/lib/transport/metadata.h" |
| 53 | #include "src/core/lib/transport/metadata_batch.h" |
Mark D. Roth | ea846a0 | 2016-11-03 11:32:54 -0700 | [diff] [blame] | 54 | #include "src/core/lib/transport/service_config.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 55 | #include "src/core/lib/transport/static_metadata.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 56 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | /* Client channel implementation */ |
| 58 | |
Craig Tiller | 694580f | 2017-10-18 14:48:14 -0700 | [diff] [blame] | 59 | grpc_core::TraceFlag grpc_client_channel_trace(false, "client_channel"); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 60 | |
Mark D. Roth | 26b7be4 | 2016-10-24 10:08:07 -0700 | [diff] [blame] | 61 | /************************************************************************* |
| 62 | * METHOD-CONFIG TABLE |
| 63 | */ |
| 64 | |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 65 | typedef enum { |
Craig Tiller | 7acc37e | 2017-02-28 10:01:37 -0800 | [diff] [blame] | 66 | /* zero so it can be default initialized */ |
| 67 | WAIT_FOR_READY_UNSET = 0, |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 68 | WAIT_FOR_READY_FALSE, |
| 69 | WAIT_FOR_READY_TRUE |
| 70 | } wait_for_ready_value; |
| 71 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 72 | typedef struct { |
| 73 | gpr_refcount refs; |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 74 | grpc_millis timeout; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 75 | wait_for_ready_value wait_for_ready; |
| 76 | } method_parameters; |
| 77 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 78 | static method_parameters* method_parameters_ref( |
| 79 | method_parameters* method_params) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 80 | gpr_ref(&method_params->refs); |
| 81 | return method_params; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 84 | static void method_parameters_unref(method_parameters* method_params) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 85 | if (gpr_unref(&method_params->refs)) { |
| 86 | gpr_free(method_params); |
| 87 | } |
| 88 | } |
| 89 | |
Mark D. Roth | 76d0ec4 | 2017-10-26 11:08:14 -0700 | [diff] [blame] | 90 | // Wrappers to pass to grpc_service_config_create_method_config_table(). |
Craig Tiller | a64b2b1 | 2017-11-03 15:23:13 -0700 | [diff] [blame] | 91 | static void* method_parameters_ref_wrapper(void* value) { |
| 92 | return method_parameters_ref((method_parameters*)value); |
Mark D. Roth | 76d0ec4 | 2017-10-26 11:08:14 -0700 | [diff] [blame] | 93 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 94 | static void method_parameters_unref_wrapper(void* value) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 95 | method_parameters_unref((method_parameters*)value); |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 98 | static bool parse_wait_for_ready(grpc_json* field, |
| 99 | wait_for_ready_value* wait_for_ready) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 100 | if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) { |
| 101 | return false; |
| 102 | } |
| 103 | *wait_for_ready = field->type == GRPC_JSON_TRUE ? WAIT_FOR_READY_TRUE |
| 104 | : WAIT_FOR_READY_FALSE; |
| 105 | return true; |
| 106 | } |
| 107 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 108 | static bool parse_timeout(grpc_json* field, grpc_millis* timeout) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 109 | if (field->type != GRPC_JSON_STRING) return false; |
| 110 | size_t len = strlen(field->value); |
| 111 | if (field->value[len - 1] != 's') return false; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 112 | char* buf = gpr_strdup(field->value); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 113 | buf[len - 1] = '\0'; // Remove trailing 's'. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 114 | char* decimal_point = strchr(buf, '.'); |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 115 | int nanos = 0; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 116 | if (decimal_point != nullptr) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 117 | *decimal_point = '\0'; |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 118 | nanos = gpr_parse_nonnegative_int(decimal_point + 1); |
| 119 | if (nanos == -1) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 120 | gpr_free(buf); |
| 121 | return false; |
| 122 | } |
Mark D. Roth | a282146 | 2017-10-26 11:31:58 -0700 | [diff] [blame] | 123 | int num_digits = (int)strlen(decimal_point + 1); |
| 124 | if (num_digits > 9) { // We don't accept greater precision than nanos. |
| 125 | gpr_free(buf); |
| 126 | return false; |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 127 | } |
Mark D. Roth | a282146 | 2017-10-26 11:31:58 -0700 | [diff] [blame] | 128 | for (int i = 0; i < (9 - num_digits); ++i) { |
| 129 | nanos *= 10; |
| 130 | } |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 131 | } |
Mark D. Roth | a282146 | 2017-10-26 11:31:58 -0700 | [diff] [blame] | 132 | int seconds = decimal_point == buf ? 0 : gpr_parse_nonnegative_int(buf); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 133 | gpr_free(buf); |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 134 | if (seconds == -1) return false; |
| 135 | *timeout = seconds * GPR_MS_PER_SEC + nanos / GPR_NS_PER_MS; |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 139 | static void* method_parameters_create_from_json(const grpc_json* json) { |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 140 | wait_for_ready_value wait_for_ready = WAIT_FOR_READY_UNSET; |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 141 | grpc_millis timeout = 0; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 142 | for (grpc_json* field = json->child; field != nullptr; field = field->next) { |
| 143 | if (field->key == nullptr) continue; |
Mark D. Roth | 84c8a02 | 2016-11-10 09:39:34 -0800 | [diff] [blame] | 144 | if (strcmp(field->key, "waitForReady") == 0) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 145 | if (wait_for_ready != WAIT_FOR_READY_UNSET) return nullptr; // Duplicate. |
| 146 | if (!parse_wait_for_ready(field, &wait_for_ready)) return nullptr; |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 147 | } else if (strcmp(field->key, "timeout") == 0) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 148 | if (timeout > 0) return nullptr; // Duplicate. |
| 149 | if (!parse_timeout(field, &timeout)) return nullptr; |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 152 | method_parameters* value = |
| 153 | (method_parameters*)gpr_malloc(sizeof(method_parameters)); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 154 | gpr_ref_init(&value->refs, 1); |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 155 | value->timeout = timeout; |
| 156 | value->wait_for_ready = wait_for_ready; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 157 | return value; |
| 158 | } |
| 159 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 160 | struct external_connectivity_watcher; |
| 161 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 162 | /************************************************************************* |
| 163 | * CHANNEL-WIDE FUNCTIONS |
| 164 | */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 165 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 166 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 167 | /** resolver for this channel */ |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 168 | grpc_core::OrphanablePtr<grpc_core::Resolver> resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 169 | /** have we started resolving this channel */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 170 | bool started_resolving; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 171 | /** is deadline checking enabled? */ |
| 172 | bool deadline_checking_enabled; |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 173 | /** client channel factory */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 174 | grpc_client_channel_factory* client_channel_factory; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 175 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 176 | /** combiner protecting all variables below in this data structure */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 177 | grpc_combiner* combiner; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 178 | /** currently active load balancer */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 179 | grpc_lb_policy* lb_policy; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 180 | /** retry throttle data */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 181 | grpc_server_retry_throttle_data* retry_throttle_data; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 182 | /** maps method names to method_parameters structs */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 183 | grpc_slice_hash_table* method_params_table; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 184 | /** incoming resolver result - set by resolver.next() */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 185 | grpc_channel_args* resolver_result; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 186 | /** a list of closures that are all waiting for resolver result to come in */ |
| 187 | grpc_closure_list waiting_for_resolver_result_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 188 | /** resolver callback */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 189 | grpc_closure on_resolver_result_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 190 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 191 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 192 | /** when an lb_policy arrives, should we try to exit idle */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 193 | bool exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 194 | /** owning stack */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 195 | grpc_channel_stack* owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 196 | /** interested parties (owned) */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 197 | grpc_pollset_set* interested_parties; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 198 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 199 | /* external_connectivity_watcher_list head is guarded by its own mutex, since |
| 200 | * counts need to be grabbed immediately without polling on a cq */ |
| 201 | gpr_mu external_connectivity_watcher_list_mu; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 202 | struct external_connectivity_watcher* external_connectivity_watcher_list_head; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 203 | |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 204 | /* the following properties are guarded by a mutex since API's require them |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 205 | to be instantaneously available */ |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 206 | gpr_mu info_mu; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 207 | char* info_lb_policy_name; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 208 | /** service config in JSON form */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 209 | char* info_service_config_json; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 210 | } channel_data; |
| 211 | |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 212 | typedef struct { |
| 213 | channel_data* chand; |
| 214 | /** used as an identifier, don't dereference it because the LB policy may be |
| 215 | * non-existing when the callback is run */ |
| 216 | grpc_lb_policy* lb_policy; |
| 217 | grpc_closure closure; |
| 218 | } reresolution_request_args; |
| 219 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 220 | /** We create one watcher for each new lb_policy that is returned from a |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 221 | resolver, to watch for state changes from the lb_policy. When a state |
| 222 | change is seen, we update the channel, and create a new watcher. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | typedef struct { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 224 | channel_data* chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 225 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 226 | grpc_connectivity_state state; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 227 | grpc_lb_policy* lb_policy; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 228 | } lb_policy_connectivity_watcher; |
| 229 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 230 | static void watch_lb_policy_locked(channel_data* chand, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 231 | grpc_lb_policy* lb_policy, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 232 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 233 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 234 | static void set_channel_connectivity_state_locked(channel_data* chand, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 235 | grpc_connectivity_state state, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 236 | grpc_error* error, |
| 237 | const char* reason) { |
David Garcia Quintas | 3725128 | 2017-04-14 13:46:03 -0700 | [diff] [blame] | 238 | /* TODO: Improve failure handling: |
| 239 | * - Make it possible for policies to return GRPC_CHANNEL_TRANSIENT_FAILURE. |
| 240 | * - Hand over pending picks from old policies during the switch that happens |
| 241 | * when resolver provides an update. */ |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 242 | if (chand->lb_policy != nullptr) { |
David Garcia Quintas | 956f700 | 2017-04-13 15:40:06 -0700 | [diff] [blame] | 243 | if (state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 244 | /* cancel picks with wait_for_ready=false */ |
| 245 | grpc_lb_policy_cancel_picks_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 246 | chand->lb_policy, |
David Garcia Quintas | 956f700 | 2017-04-13 15:40:06 -0700 | [diff] [blame] | 247 | /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY, |
| 248 | /* check= */ 0, GRPC_ERROR_REF(error)); |
| 249 | } else if (state == GRPC_CHANNEL_SHUTDOWN) { |
| 250 | /* cancel all picks */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 251 | grpc_lb_policy_cancel_picks_locked(chand->lb_policy, |
David Garcia Quintas | 956f700 | 2017-04-13 15:40:06 -0700 | [diff] [blame] | 252 | /* mask= */ 0, /* check= */ 0, |
| 253 | GRPC_ERROR_REF(error)); |
| 254 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 255 | } |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 256 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 257 | gpr_log(GPR_DEBUG, "chand=%p: setting connectivity state to %s", chand, |
| 258 | grpc_connectivity_state_name(state)); |
| 259 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 260 | grpc_connectivity_state_set(&chand->state_tracker, state, error, reason); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 263 | static void on_lb_policy_state_changed_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 264 | lb_policy_connectivity_watcher* w = (lb_policy_connectivity_watcher*)arg; |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 265 | /* check if the notification is for the latest policy */ |
| 266 | if (w->lb_policy == w->chand->lb_policy) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 267 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 268 | gpr_log(GPR_DEBUG, "chand=%p: lb_policy=%p state changed to %s", w->chand, |
| 269 | w->lb_policy, grpc_connectivity_state_name(w->state)); |
| 270 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 271 | set_channel_connectivity_state_locked(w->chand, w->state, |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 272 | GRPC_ERROR_REF(error), "lb_changed"); |
| 273 | if (w->state != GRPC_CHANNEL_SHUTDOWN) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 274 | watch_lb_policy_locked(w->chand, w->lb_policy, w->state); |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 275 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 276 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 277 | GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 278 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 281 | static void watch_lb_policy_locked(channel_data* chand, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 282 | grpc_lb_policy* lb_policy, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 283 | grpc_connectivity_state current_state) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 284 | lb_policy_connectivity_watcher* w = |
| 285 | (lb_policy_connectivity_watcher*)gpr_malloc(sizeof(*w)); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 286 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 287 | w->chand = chand; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 288 | GRPC_CLOSURE_INIT(&w->on_changed, on_lb_policy_state_changed_locked, w, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 289 | grpc_combiner_scheduler(chand->combiner)); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 290 | w->state = current_state; |
| 291 | w->lb_policy = lb_policy; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 292 | grpc_lb_policy_notify_on_state_change_locked(lb_policy, &w->state, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 293 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 296 | static void start_resolving_locked(channel_data* chand) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 297 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 298 | gpr_log(GPR_DEBUG, "chand=%p: starting name resolution", chand); |
| 299 | } |
| 300 | GPR_ASSERT(!chand->started_resolving); |
| 301 | chand->started_resolving = true; |
| 302 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 303 | chand->resolver->NextLocked(&chand->resolver_result, |
| 304 | &chand->on_resolver_result_changed); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 307 | typedef struct { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 308 | char* server_name; |
| 309 | grpc_server_retry_throttle_data* retry_throttle_data; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 310 | } service_config_parsing_state; |
| 311 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 312 | static void parse_retry_throttle_params(const grpc_json* field, void* arg) { |
| 313 | service_config_parsing_state* parsing_state = |
| 314 | (service_config_parsing_state*)arg; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 315 | if (strcmp(field->key, "retryThrottling") == 0) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 316 | if (parsing_state->retry_throttle_data != nullptr) return; // Duplicate. |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 317 | if (field->type != GRPC_JSON_OBJECT) return; |
| 318 | int max_milli_tokens = 0; |
| 319 | int milli_token_ratio = 0; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 320 | for (grpc_json* sub_field = field->child; sub_field != nullptr; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 321 | sub_field = sub_field->next) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 322 | if (sub_field->key == nullptr) return; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 323 | if (strcmp(sub_field->key, "maxTokens") == 0) { |
| 324 | if (max_milli_tokens != 0) return; // Duplicate. |
| 325 | if (sub_field->type != GRPC_JSON_NUMBER) return; |
| 326 | max_milli_tokens = gpr_parse_nonnegative_int(sub_field->value); |
| 327 | if (max_milli_tokens == -1) return; |
| 328 | max_milli_tokens *= 1000; |
| 329 | } else if (strcmp(sub_field->key, "tokenRatio") == 0) { |
| 330 | if (milli_token_ratio != 0) return; // Duplicate. |
| 331 | if (sub_field->type != GRPC_JSON_NUMBER) return; |
| 332 | // We support up to 3 decimal digits. |
| 333 | size_t whole_len = strlen(sub_field->value); |
| 334 | uint32_t multiplier = 1; |
| 335 | uint32_t decimal_value = 0; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 336 | const char* decimal_point = strchr(sub_field->value, '.'); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 337 | if (decimal_point != nullptr) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 338 | whole_len = (size_t)(decimal_point - sub_field->value); |
| 339 | multiplier = 1000; |
| 340 | size_t decimal_len = strlen(decimal_point + 1); |
| 341 | if (decimal_len > 3) decimal_len = 3; |
| 342 | if (!gpr_parse_bytes_to_uint32(decimal_point + 1, decimal_len, |
| 343 | &decimal_value)) { |
| 344 | return; |
| 345 | } |
| 346 | uint32_t decimal_multiplier = 1; |
| 347 | for (size_t i = 0; i < (3 - decimal_len); ++i) { |
| 348 | decimal_multiplier *= 10; |
| 349 | } |
| 350 | decimal_value *= decimal_multiplier; |
| 351 | } |
| 352 | uint32_t whole_value; |
| 353 | if (!gpr_parse_bytes_to_uint32(sub_field->value, whole_len, |
| 354 | &whole_value)) { |
| 355 | return; |
| 356 | } |
| 357 | milli_token_ratio = (int)((whole_value * multiplier) + decimal_value); |
Mark D. Roth | b332256 | 2017-02-23 14:38:02 -0800 | [diff] [blame] | 358 | if (milli_token_ratio <= 0) return; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | parsing_state->retry_throttle_data = |
| 362 | grpc_retry_throttle_map_get_data_for_server( |
| 363 | parsing_state->server_name, max_milli_tokens, milli_token_ratio); |
| 364 | } |
| 365 | } |
| 366 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 367 | static void request_reresolution_locked(void* arg, grpc_error* error) { |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 368 | reresolution_request_args* args = (reresolution_request_args*)arg; |
| 369 | channel_data* chand = args->chand; |
| 370 | // If this invocation is for a stale LB policy, treat it as an LB shutdown |
| 371 | // signal. |
| 372 | if (args->lb_policy != chand->lb_policy || error != GRPC_ERROR_NONE || |
| 373 | chand->resolver == nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 374 | GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "re-resolution"); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 375 | gpr_free(args); |
| 376 | return; |
| 377 | } |
| 378 | if (grpc_client_channel_trace.enabled()) { |
| 379 | gpr_log(GPR_DEBUG, "chand=%p: started name re-resolving", chand); |
| 380 | } |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 381 | chand->resolver->RequestReresolutionLocked(); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 382 | // Give back the closure to the LB policy. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 383 | grpc_lb_policy_set_reresolve_closure_locked(chand->lb_policy, &args->closure); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 386 | static void on_resolver_result_changed_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 387 | channel_data* chand = (channel_data*)arg; |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 388 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 389 | gpr_log(GPR_DEBUG, "chand=%p: got resolver result: error=%s", chand, |
| 390 | grpc_error_string(error)); |
| 391 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 392 | // Extract the following fields from the resolver result, if non-NULL. |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 393 | bool lb_policy_updated = false; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 394 | char* lb_policy_name_dup = nullptr; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 395 | bool lb_policy_name_changed = false; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 396 | grpc_lb_policy* new_lb_policy = nullptr; |
| 397 | char* service_config_json = nullptr; |
| 398 | grpc_server_retry_throttle_data* retry_throttle_data = nullptr; |
| 399 | grpc_slice_hash_table* method_params_table = nullptr; |
| 400 | if (chand->resolver_result != nullptr) { |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 401 | if (chand->resolver != nullptr) { |
| 402 | // Find LB policy name. |
| 403 | const char* lb_policy_name = nullptr; |
| 404 | const grpc_arg* channel_arg = grpc_channel_args_find( |
| 405 | chand->resolver_result, GRPC_ARG_LB_POLICY_NAME); |
| 406 | if (channel_arg != nullptr) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 407 | GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 408 | lb_policy_name = channel_arg->value.string; |
Mark D. Roth | bdc58b2 | 2016-11-04 09:25:57 -0700 | [diff] [blame] | 409 | } |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 410 | // Special case: If at least one balancer address is present, we use |
| 411 | // the grpclb policy, regardless of what the resolver actually specified. |
| 412 | channel_arg = |
| 413 | grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES); |
| 414 | if (channel_arg != nullptr && channel_arg->type == GRPC_ARG_POINTER) { |
| 415 | grpc_lb_addresses* addresses = |
| 416 | (grpc_lb_addresses*)channel_arg->value.pointer.p; |
| 417 | bool found_balancer_address = false; |
| 418 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 419 | if (addresses->addresses[i].is_balancer) { |
| 420 | found_balancer_address = true; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | if (found_balancer_address) { |
| 425 | if (lb_policy_name != nullptr && |
| 426 | strcmp(lb_policy_name, "grpclb") != 0) { |
| 427 | gpr_log(GPR_INFO, |
| 428 | "resolver requested LB policy %s but provided at least one " |
| 429 | "balancer address -- forcing use of grpclb LB policy", |
| 430 | lb_policy_name); |
| 431 | } |
| 432 | lb_policy_name = "grpclb"; |
| 433 | } |
| 434 | } |
| 435 | // Use pick_first if nothing was specified and we didn't select grpclb |
| 436 | // above. |
| 437 | if (lb_policy_name == nullptr) lb_policy_name = "pick_first"; |
| 438 | grpc_lb_policy_args lb_policy_args; |
| 439 | lb_policy_args.args = chand->resolver_result; |
| 440 | lb_policy_args.client_channel_factory = chand->client_channel_factory; |
| 441 | lb_policy_args.combiner = chand->combiner; |
| 442 | // Check to see if we're already using the right LB policy. |
| 443 | // Note: It's safe to use chand->info_lb_policy_name here without |
| 444 | // taking a lock on chand->info_mu, because this function is the |
| 445 | // only thing that modifies its value, and it can only be invoked |
| 446 | // once at any given time. |
| 447 | lb_policy_name_changed = |
| 448 | chand->info_lb_policy_name == nullptr || |
| 449 | gpr_stricmp(chand->info_lb_policy_name, lb_policy_name) != 0; |
| 450 | if (chand->lb_policy != nullptr && !lb_policy_name_changed) { |
| 451 | // Continue using the same LB policy. Update with new addresses. |
| 452 | lb_policy_updated = true; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 453 | grpc_lb_policy_update_locked(chand->lb_policy, &lb_policy_args); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 454 | } else { |
| 455 | // Instantiate new LB policy. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 456 | new_lb_policy = grpc_lb_policy_create(lb_policy_name, &lb_policy_args); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 457 | if (new_lb_policy == nullptr) { |
| 458 | gpr_log(GPR_ERROR, "could not create LB policy \"%s\"", |
| 459 | lb_policy_name); |
| 460 | } else { |
| 461 | reresolution_request_args* args = |
| 462 | (reresolution_request_args*)gpr_zalloc(sizeof(*args)); |
| 463 | args->chand = chand; |
| 464 | args->lb_policy = new_lb_policy; |
| 465 | GRPC_CLOSURE_INIT(&args->closure, request_reresolution_locked, args, |
| 466 | grpc_combiner_scheduler(chand->combiner)); |
| 467 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "re-resolution"); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 468 | grpc_lb_policy_set_reresolve_closure_locked(new_lb_policy, |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 469 | &args->closure); |
| 470 | } |
| 471 | } |
| 472 | // Find service config. |
| 473 | channel_arg = grpc_channel_args_find(chand->resolver_result, |
| 474 | GRPC_ARG_SERVICE_CONFIG); |
| 475 | if (channel_arg != nullptr) { |
| 476 | GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
| 477 | service_config_json = gpr_strdup(channel_arg->value.string); |
| 478 | grpc_service_config* service_config = |
| 479 | grpc_service_config_create(service_config_json); |
| 480 | if (service_config != nullptr) { |
| 481 | channel_arg = grpc_channel_args_find(chand->resolver_result, |
| 482 | GRPC_ARG_SERVER_URI); |
| 483 | GPR_ASSERT(channel_arg != nullptr); |
| 484 | GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 485 | grpc_uri* uri = grpc_uri_parse(channel_arg->value.string, true); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 486 | GPR_ASSERT(uri->path[0] != '\0'); |
| 487 | service_config_parsing_state parsing_state; |
| 488 | memset(&parsing_state, 0, sizeof(parsing_state)); |
| 489 | parsing_state.server_name = |
| 490 | uri->path[0] == '/' ? uri->path + 1 : uri->path; |
| 491 | grpc_service_config_parse_global_params( |
| 492 | service_config, parse_retry_throttle_params, &parsing_state); |
| 493 | grpc_uri_destroy(uri); |
| 494 | retry_throttle_data = parsing_state.retry_throttle_data; |
| 495 | method_params_table = grpc_service_config_create_method_config_table( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 496 | service_config, method_parameters_create_from_json, |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 497 | method_parameters_ref_wrapper, method_parameters_unref_wrapper); |
| 498 | grpc_service_config_destroy(service_config); |
| 499 | } |
| 500 | } |
| 501 | // Before we clean up, save a copy of lb_policy_name, since it might |
| 502 | // be pointing to data inside chand->resolver_result. |
| 503 | // The copy will be saved in chand->lb_policy_name below. |
| 504 | lb_policy_name_dup = gpr_strdup(lb_policy_name); |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 505 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 506 | grpc_channel_args_destroy(chand->resolver_result); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 507 | chand->resolver_result = nullptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 508 | } |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 509 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 510 | gpr_log(GPR_DEBUG, |
| 511 | "chand=%p: resolver result: lb_policy_name=\"%s\"%s, " |
| 512 | "service_config=\"%s\"", |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 513 | chand, lb_policy_name_dup, |
| 514 | lb_policy_name_changed ? " (changed)" : "", service_config_json); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 515 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 516 | // Now swap out fields in chand. Note that the new values may still |
| 517 | // be NULL if (e.g.) the resolver failed to return results or the |
| 518 | // results did not contain the necessary data. |
| 519 | // |
| 520 | // First, swap out the data used by cc_get_channel_info(). |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 521 | gpr_mu_lock(&chand->info_mu); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 522 | if (lb_policy_name_dup != nullptr) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 523 | gpr_free(chand->info_lb_policy_name); |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 524 | chand->info_lb_policy_name = lb_policy_name_dup; |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 525 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 526 | if (service_config_json != nullptr) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 527 | gpr_free(chand->info_service_config_json); |
| 528 | chand->info_service_config_json = service_config_json; |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 529 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 530 | gpr_mu_unlock(&chand->info_mu); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 531 | // Swap out the retry throttle data. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 532 | if (chand->retry_throttle_data != nullptr) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 533 | grpc_server_retry_throttle_data_unref(chand->retry_throttle_data); |
| 534 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 535 | chand->retry_throttle_data = retry_throttle_data; |
| 536 | // Swap out the method params table. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 537 | if (chand->method_params_table != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 538 | grpc_slice_hash_table_unref(chand->method_params_table); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 539 | } |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 540 | chand->method_params_table = method_params_table; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 541 | // If we have a new LB policy or are shutting down (in which case |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 542 | // new_lb_policy will be NULL), swap out the LB policy, unreffing the old one |
| 543 | // and removing its fds from chand->interested_parties. Note that we do NOT do |
| 544 | // this if either (a) we updated the existing LB policy above or (b) we failed |
| 545 | // to create the new LB policy (in which case we want to continue using the |
| 546 | // most recent one we had). |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 547 | if (new_lb_policy != nullptr || error != GRPC_ERROR_NONE || |
| 548 | chand->resolver == nullptr) { |
| 549 | if (chand->lb_policy != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 550 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 551 | gpr_log(GPR_DEBUG, "chand=%p: unreffing lb_policy=%p", chand, |
| 552 | chand->lb_policy); |
| 553 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 554 | grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties, |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 555 | chand->interested_parties); |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 556 | grpc_lb_policy_shutdown_locked(chand->lb_policy, new_lb_policy); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 557 | GRPC_LB_POLICY_UNREF(chand->lb_policy, "channel"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 558 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 559 | chand->lb_policy = new_lb_policy; |
| 560 | } |
| 561 | // Now that we've swapped out the relevant fields of chand, check for |
| 562 | // error or shutdown. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 563 | if (error != GRPC_ERROR_NONE || chand->resolver == nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 564 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 565 | gpr_log(GPR_DEBUG, "chand=%p: shutting down", chand); |
| 566 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 567 | if (chand->resolver != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 568 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 569 | gpr_log(GPR_DEBUG, "chand=%p: shutting down resolver", chand); |
| 570 | } |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 571 | chand->resolver.reset(); |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 572 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 573 | set_channel_connectivity_state_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 574 | chand, GRPC_CHANNEL_SHUTDOWN, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 575 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 576 | "Got resolver result after disconnection", &error, 1), |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 577 | "resolver_gone"); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 578 | GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "resolver"); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 579 | grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures, |
| 580 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 581 | "Channel disconnected", &error, 1)); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 582 | GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 583 | } else { // Not shutting down. |
| 584 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 585 | grpc_error* state_error = |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 586 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("No load balancing policy"); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 587 | if (new_lb_policy != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 588 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 589 | gpr_log(GPR_DEBUG, "chand=%p: initializing new LB policy", chand); |
| 590 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 591 | GRPC_ERROR_UNREF(state_error); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 592 | state = |
| 593 | grpc_lb_policy_check_connectivity_locked(new_lb_policy, &state_error); |
| 594 | grpc_pollset_set_add_pollset_set(new_lb_policy->interested_parties, |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 595 | chand->interested_parties); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 596 | GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 597 | if (chand->exit_idle_when_lb_policy_arrives) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 598 | grpc_lb_policy_exit_idle_locked(new_lb_policy); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 599 | chand->exit_idle_when_lb_policy_arrives = false; |
| 600 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 601 | watch_lb_policy_locked(chand, new_lb_policy, state); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 602 | } |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 603 | if (!lb_policy_updated) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 604 | set_channel_connectivity_state_locked( |
| 605 | chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver"); |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 606 | } |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 607 | chand->resolver->NextLocked(&chand->resolver_result, |
| 608 | &chand->on_resolver_result_changed); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 609 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 610 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 613 | static void start_transport_op_locked(void* arg, grpc_error* error_ignored) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 614 | grpc_transport_op* op = (grpc_transport_op*)arg; |
| 615 | grpc_channel_element* elem = |
| 616 | (grpc_channel_element*)op->handler_private.extra_arg; |
| 617 | channel_data* chand = (channel_data*)elem->channel_data; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 618 | |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 619 | if (op->on_connectivity_state_change != nullptr) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 620 | grpc_connectivity_state_notify_on_state_change( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 621 | &chand->state_tracker, op->connectivity_state, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 622 | op->on_connectivity_state_change); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 623 | op->on_connectivity_state_change = nullptr; |
| 624 | op->connectivity_state = nullptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 627 | if (op->send_ping.on_initiate != nullptr || op->send_ping.on_ack != nullptr) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 628 | if (chand->lb_policy == nullptr) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 629 | GRPC_CLOSURE_SCHED( |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 630 | op->send_ping.on_initiate, |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 631 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing")); |
| 632 | GRPC_CLOSURE_SCHED( |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 633 | op->send_ping.on_ack, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 634 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing")); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 635 | } else { |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 636 | grpc_lb_policy_ping_one_locked( |
| 637 | chand->lb_policy, op->send_ping.on_initiate, op->send_ping.on_ack); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 638 | op->bind_pollset = nullptr; |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 639 | } |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 640 | op->send_ping.on_initiate = nullptr; |
| 641 | op->send_ping.on_ack = nullptr; |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 644 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 645 | if (chand->resolver != nullptr) { |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 646 | set_channel_connectivity_state_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 647 | chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 648 | GRPC_ERROR_REF(op->disconnect_with_error), "disconnect"); |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 649 | chand->resolver.reset(); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 650 | if (!chand->started_resolving) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 651 | grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 652 | GRPC_ERROR_REF(op->disconnect_with_error)); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 653 | GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 654 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 655 | if (chand->lb_policy != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 656 | grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 657 | chand->interested_parties); |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 658 | grpc_lb_policy_shutdown_locked(chand->lb_policy, nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 659 | GRPC_LB_POLICY_UNREF(chand->lb_policy, "channel"); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 660 | chand->lb_policy = nullptr; |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 661 | } |
Craig Tiller | b12d22a | 2016-04-23 12:50:21 -0700 | [diff] [blame] | 662 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 663 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 664 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 665 | GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "start_transport_op"); |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 666 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 667 | GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 670 | static void cc_start_transport_op(grpc_channel_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 671 | grpc_transport_op* op) { |
| 672 | channel_data* chand = (channel_data*)elem->channel_data; |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 673 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 674 | GPR_ASSERT(op->set_accept_stream == false); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 675 | if (op->bind_pollset != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 676 | grpc_pollset_set_add_pollset(chand->interested_parties, op->bind_pollset); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Craig Tiller | c55c102 | 2017-03-10 10:26:42 -0800 | [diff] [blame] | 679 | op->handler_private.extra_arg = elem; |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 680 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "start_transport_op"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 681 | GRPC_CLOSURE_SCHED( |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 682 | GRPC_CLOSURE_INIT(&op->handler_private.closure, start_transport_op_locked, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 683 | op, grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 684 | GRPC_ERROR_NONE); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 685 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 686 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 687 | static void cc_get_channel_info(grpc_channel_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 688 | const grpc_channel_info* info) { |
| 689 | channel_data* chand = (channel_data*)elem->channel_data; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 690 | gpr_mu_lock(&chand->info_mu); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 691 | if (info->lb_policy_name != nullptr) { |
| 692 | *info->lb_policy_name = chand->info_lb_policy_name == nullptr |
| 693 | ? nullptr |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 694 | : gpr_strdup(chand->info_lb_policy_name); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 695 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 696 | if (info->service_config_json != nullptr) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 697 | *info->service_config_json = |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 698 | chand->info_service_config_json == nullptr |
| 699 | ? nullptr |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 700 | : gpr_strdup(chand->info_service_config_json); |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 701 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 702 | gpr_mu_unlock(&chand->info_mu); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 705 | /* Constructor for channel_data */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 706 | static grpc_error* cc_init_channel_elem(grpc_channel_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 707 | grpc_channel_element_args* args) { |
| 708 | channel_data* chand = (channel_data*)elem->channel_data; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 709 | GPR_ASSERT(args->is_last); |
| 710 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 711 | // Initialize data members. |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 712 | chand->combiner = grpc_combiner_create(); |
Craig Tiller | d8547751 | 2017-02-09 12:02:39 -0800 | [diff] [blame] | 713 | gpr_mu_init(&chand->info_mu); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 714 | gpr_mu_init(&chand->external_connectivity_watcher_list_mu); |
| 715 | |
| 716 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 717 | chand->external_connectivity_watcher_list_head = nullptr; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 718 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 719 | |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 720 | chand->owning_stack = args->channel_stack; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 721 | GRPC_CLOSURE_INIT(&chand->on_resolver_result_changed, |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 722 | on_resolver_result_changed_locked, chand, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 723 | grpc_combiner_scheduler(chand->combiner)); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 724 | chand->interested_parties = grpc_pollset_set_create(); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 725 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 726 | "client_channel"); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 727 | grpc_client_channel_start_backup_polling(chand->interested_parties); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 728 | // Record client channel factory. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 729 | const grpc_arg* arg = grpc_channel_args_find(args->channel_args, |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 730 | GRPC_ARG_CLIENT_CHANNEL_FACTORY); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 731 | if (arg == nullptr) { |
David Garcia Quintas | 228a514 | 2017-03-30 19:43:00 -0700 | [diff] [blame] | 732 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 733 | "Missing client channel factory in args for client channel filter"); |
| 734 | } |
| 735 | if (arg->type != GRPC_ARG_POINTER) { |
| 736 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 737 | "client channel factory arg must be a pointer"); |
| 738 | } |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 739 | grpc_client_channel_factory_ref( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 740 | (grpc_client_channel_factory*)arg->value.pointer.p); |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 741 | chand->client_channel_factory = |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 742 | (grpc_client_channel_factory*)arg->value.pointer.p; |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 743 | // Get server name to resolve, using proxy mapper if needed. |
Mark D. Roth | 86e9059 | 2016-11-18 09:56:40 -0800 | [diff] [blame] | 744 | arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 745 | if (arg == nullptr) { |
David Garcia Quintas | 228a514 | 2017-03-30 19:43:00 -0700 | [diff] [blame] | 746 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 747 | "Missing server uri in args for client channel filter"); |
| 748 | } |
| 749 | if (arg->type != GRPC_ARG_STRING) { |
| 750 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 751 | "server uri arg must be a string"); |
| 752 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 753 | char* proxy_name = nullptr; |
| 754 | grpc_channel_args* new_args = nullptr; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 755 | grpc_proxy_mappers_map_name(arg->value.string, args->channel_args, |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 756 | &proxy_name, &new_args); |
| 757 | // Instantiate resolver. |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 758 | chand->resolver = grpc_core::ResolverRegistry::CreateResolver( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 759 | proxy_name != nullptr ? proxy_name : arg->value.string, |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 760 | new_args != nullptr ? new_args : args->channel_args, |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 761 | chand->interested_parties, chand->combiner); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 762 | if (proxy_name != nullptr) gpr_free(proxy_name); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 763 | if (new_args != nullptr) grpc_channel_args_destroy(new_args); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 764 | if (chand->resolver == nullptr) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 765 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING("resolver creation failed"); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 766 | } |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 767 | chand->deadline_checking_enabled = |
| 768 | grpc_deadline_checking_enabled(args->channel_args); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 769 | return GRPC_ERROR_NONE; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 772 | static void shutdown_resolver_locked(void* arg, grpc_error* error) { |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 773 | grpc_core::Resolver* resolver = static_cast<grpc_core::Resolver*>(arg); |
| 774 | resolver->Orphan(); |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 777 | /* Destructor for channel_data */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 778 | static void cc_destroy_channel_elem(grpc_channel_element* elem) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 779 | channel_data* chand = (channel_data*)elem->channel_data; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 780 | if (chand->resolver != nullptr) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 781 | GRPC_CLOSURE_SCHED( |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame^] | 782 | GRPC_CLOSURE_CREATE(shutdown_resolver_locked, chand->resolver.release(), |
Yash Tibrewal | 0ee7574 | 2017-10-13 16:07:13 -0700 | [diff] [blame] | 783 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 784 | GRPC_ERROR_NONE); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 785 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 786 | if (chand->client_channel_factory != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 787 | grpc_client_channel_factory_unref(chand->client_channel_factory); |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 788 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 789 | if (chand->lb_policy != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 790 | grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties, |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 791 | chand->interested_parties); |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 792 | grpc_lb_policy_shutdown_locked(chand->lb_policy, nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 793 | GRPC_LB_POLICY_UNREF(chand->lb_policy, "channel"); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 794 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 795 | gpr_free(chand->info_lb_policy_name); |
| 796 | gpr_free(chand->info_service_config_json); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 797 | if (chand->retry_throttle_data != nullptr) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 798 | grpc_server_retry_throttle_data_unref(chand->retry_throttle_data); |
| 799 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 800 | if (chand->method_params_table != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 801 | grpc_slice_hash_table_unref(chand->method_params_table); |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 802 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 803 | grpc_client_channel_stop_backup_polling(chand->interested_parties); |
| 804 | grpc_connectivity_state_destroy(&chand->state_tracker); |
| 805 | grpc_pollset_set_destroy(chand->interested_parties); |
| 806 | GRPC_COMBINER_UNREF(chand->combiner, "client_channel"); |
Craig Tiller | d8547751 | 2017-02-09 12:02:39 -0800 | [diff] [blame] | 807 | gpr_mu_destroy(&chand->info_mu); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 808 | gpr_mu_destroy(&chand->external_connectivity_watcher_list_mu); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /************************************************************************* |
| 812 | * PER-CALL FUNCTIONS |
| 813 | */ |
| 814 | |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 815 | // Max number of batches that can be pending on a call at any given |
| 816 | // time. This includes: |
| 817 | // recv_initial_metadata |
| 818 | // send_initial_metadata |
| 819 | // recv_message |
| 820 | // send_message |
| 821 | // recv_trailing_metadata |
| 822 | // send_trailing_metadata |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 823 | // We also add room for a single cancel_stream batch. |
| 824 | #define MAX_WAITING_BATCHES 7 |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 825 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 826 | /** Call data. Holds a pointer to grpc_subchannel_call and the |
| 827 | associated machinery to create such a pointer. |
| 828 | Handles queueing of stream ops until a call object is ready, waiting |
| 829 | for initial metadata before trying to create a call object, |
| 830 | and handling cancellation gracefully. */ |
| 831 | typedef struct client_channel_call_data { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 832 | // State for handling deadlines. |
| 833 | // The code in deadline_filter.c requires this to be the first field. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 834 | // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 835 | // and this struct both independently store pointers to the call stack |
| 836 | // and call combiner. If/when we have time, find a way to avoid this |
| 837 | // without breaking the grpc_deadline_state abstraction. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 838 | grpc_deadline_state deadline_state; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 839 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 840 | grpc_slice path; // Request path. |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 841 | gpr_timespec call_start_time; |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 842 | grpc_millis deadline; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 843 | gpr_arena* arena; |
| 844 | grpc_call_stack* owning_call; |
| 845 | grpc_call_combiner* call_combiner; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 846 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 847 | grpc_server_retry_throttle_data* retry_throttle_data; |
| 848 | method_parameters* method_params; |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 849 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 850 | grpc_subchannel_call* subchannel_call; |
| 851 | grpc_error* error; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 852 | |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 853 | grpc_lb_policy_pick_state pick; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 854 | grpc_closure lb_pick_closure; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 855 | grpc_closure lb_pick_cancel_closure; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 856 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 857 | grpc_polling_entity* pollent; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 858 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 859 | grpc_transport_stream_op_batch* waiting_for_pick_batches[MAX_WAITING_BATCHES]; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 860 | size_t waiting_for_pick_batches_count; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 861 | grpc_closure handle_pending_batch_in_call_combiner[MAX_WAITING_BATCHES]; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 862 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 863 | grpc_transport_stream_op_batch* initial_metadata_batch; |
David Garcia Quintas | d1a47f1 | 2016-09-02 12:46:44 +0200 | [diff] [blame] | 864 | |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 865 | grpc_closure on_complete; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 866 | grpc_closure* original_on_complete; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 867 | } call_data; |
| 868 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 869 | grpc_subchannel_call* grpc_client_channel_get_subchannel_call( |
| 870 | grpc_call_element* elem) { |
| 871 | call_data* calld = (call_data*)elem->call_data; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 872 | return calld->subchannel_call; |
Craig Tiller | 8b1d59c | 2016-12-27 15:15:30 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 875 | // This is called via the call combiner, so access to calld is synchronized. |
| 876 | static void waiting_for_pick_batches_add( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 877 | call_data* calld, grpc_transport_stream_op_batch* batch) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 878 | if (batch->send_initial_metadata) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 879 | GPR_ASSERT(calld->initial_metadata_batch == nullptr); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 880 | calld->initial_metadata_batch = batch; |
| 881 | } else { |
| 882 | GPR_ASSERT(calld->waiting_for_pick_batches_count < MAX_WAITING_BATCHES); |
| 883 | calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count++] = |
| 884 | batch; |
| 885 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 888 | // This is called via the call combiner, so access to calld is synchronized. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 889 | static void fail_pending_batch_in_call_combiner(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 890 | call_data* calld = (call_data*)arg; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 891 | if (calld->waiting_for_pick_batches_count > 0) { |
| 892 | --calld->waiting_for_pick_batches_count; |
| 893 | grpc_transport_stream_op_batch_finish_with_failure( |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 894 | calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count], |
| 895 | GRPC_ERROR_REF(error), calld->call_combiner); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | // This is called via the call combiner, so access to calld is synchronized. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 900 | static void waiting_for_pick_batches_fail(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 901 | grpc_error* error) { |
| 902 | call_data* calld = (call_data*)elem->call_data; |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 903 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 904 | gpr_log(GPR_DEBUG, |
Mark D. Roth | e9b1083 | 2017-10-26 13:18:25 -0700 | [diff] [blame] | 905 | "chand=%p calld=%p: failing %" PRIuPTR " pending batches: %s", |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 906 | elem->channel_data, calld, calld->waiting_for_pick_batches_count, |
| 907 | grpc_error_string(error)); |
| 908 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 909 | for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 910 | GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i], |
| 911 | fail_pending_batch_in_call_combiner, calld, |
| 912 | grpc_schedule_on_exec_ctx); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 913 | GRPC_CALL_COMBINER_START( |
| 914 | calld->call_combiner, &calld->handle_pending_batch_in_call_combiner[i], |
| 915 | GRPC_ERROR_REF(error), "waiting_for_pick_batches_fail"); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 916 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 917 | if (calld->initial_metadata_batch != nullptr) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 918 | grpc_transport_stream_op_batch_finish_with_failure( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 919 | calld->initial_metadata_batch, GRPC_ERROR_REF(error), |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 920 | calld->call_combiner); |
| 921 | } else { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 922 | GRPC_CALL_COMBINER_STOP(calld->call_combiner, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 923 | "waiting_for_pick_batches_fail"); |
| 924 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 925 | GRPC_ERROR_UNREF(error); |
| 926 | } |
| 927 | |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 928 | // This is called via the call combiner, so access to calld is synchronized. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 929 | static void run_pending_batch_in_call_combiner(void* arg, grpc_error* ignored) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 930 | call_data* calld = (call_data*)arg; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 931 | if (calld->waiting_for_pick_batches_count > 0) { |
| 932 | --calld->waiting_for_pick_batches_count; |
| 933 | grpc_subchannel_call_process_op( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 934 | calld->subchannel_call, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 935 | calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count]); |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 936 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | // This is called via the call combiner, so access to calld is synchronized. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 940 | static void waiting_for_pick_batches_resume(grpc_call_element* elem) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 941 | channel_data* chand = (channel_data*)elem->channel_data; |
| 942 | call_data* calld = (call_data*)elem->call_data; |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 943 | if (grpc_client_channel_trace.enabled()) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 944 | gpr_log(GPR_DEBUG, |
| 945 | "chand=%p calld=%p: sending %" PRIuPTR |
| 946 | " pending batches to subchannel_call=%p", |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 947 | chand, calld, calld->waiting_for_pick_batches_count, |
| 948 | calld->subchannel_call); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 949 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 950 | for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 951 | GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i], |
| 952 | run_pending_batch_in_call_combiner, calld, |
| 953 | grpc_schedule_on_exec_ctx); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 954 | GRPC_CALL_COMBINER_START( |
| 955 | calld->call_combiner, &calld->handle_pending_batch_in_call_combiner[i], |
| 956 | GRPC_ERROR_NONE, "waiting_for_pick_batches_resume"); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 957 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 958 | GPR_ASSERT(calld->initial_metadata_batch != nullptr); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 959 | grpc_subchannel_call_process_op(calld->subchannel_call, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 960 | calld->initial_metadata_batch); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 963 | // Applies service config to the call. Must be invoked once we know |
| 964 | // that the resolver has returned results to the channel. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 965 | static void apply_service_config_to_call_locked(grpc_call_element* elem) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 966 | channel_data* chand = (channel_data*)elem->channel_data; |
| 967 | call_data* calld = (call_data*)elem->call_data; |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 968 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 969 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: applying service config to call", |
| 970 | chand, calld); |
| 971 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 972 | if (chand->retry_throttle_data != nullptr) { |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 973 | calld->retry_throttle_data = |
| 974 | grpc_server_retry_throttle_data_ref(chand->retry_throttle_data); |
| 975 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 976 | if (chand->method_params_table != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 977 | calld->method_params = (method_parameters*)grpc_method_config_table_get( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 978 | chand->method_params_table, calld->path); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 979 | if (calld->method_params != nullptr) { |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 980 | method_parameters_ref(calld->method_params); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 981 | // If the deadline from the service config is shorter than the one |
| 982 | // from the client API, reset the deadline timer. |
| 983 | if (chand->deadline_checking_enabled && |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 984 | calld->method_params->timeout != 0) { |
| 985 | const grpc_millis per_method_deadline = |
Craig Tiller | 9a8c3f3 | 2017-07-21 13:14:14 -0700 | [diff] [blame] | 986 | grpc_timespec_to_millis_round_up(calld->call_start_time) + |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 987 | calld->method_params->timeout; |
| 988 | if (per_method_deadline < calld->deadline) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 989 | calld->deadline = per_method_deadline; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 990 | grpc_deadline_state_reset(elem, calld->deadline); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 991 | } |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | } |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 995 | } |
Craig Tiller | ea4a4f1 | 2017-03-13 13:36:52 -0700 | [diff] [blame] | 996 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 997 | static void create_subchannel_call_locked(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 998 | grpc_error* error) { |
| 999 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1000 | call_data* calld = (call_data*)elem->call_data; |
David Garcia Quintas | baf1ac7 | 2018-01-09 14:24:32 -0800 | [diff] [blame] | 1001 | const grpc_core::ConnectedSubchannel::CallArgs call_args = { |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1002 | calld->pollent, // pollent |
| 1003 | calld->path, // path |
| 1004 | calld->call_start_time, // start_time |
| 1005 | calld->deadline, // deadline |
| 1006 | calld->arena, // arena |
| 1007 | calld->pick.subchannel_call_context, // context |
| 1008 | calld->call_combiner // call_combiner |
Yash Tibrewal | d8b84a2 | 2017-09-25 13:38:03 -0700 | [diff] [blame] | 1009 | }; |
David Garcia Quintas | 70fbe62 | 2018-01-09 19:27:46 -0800 | [diff] [blame] | 1010 | grpc_error* new_error = calld->pick.connected_subchannel->CreateCall( |
David Garcia Quintas | baf1ac7 | 2018-01-09 14:24:32 -0800 | [diff] [blame] | 1011 | call_args, &calld->subchannel_call); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1012 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1013 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: create subchannel_call=%p: error=%s", |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1014 | chand, calld, calld->subchannel_call, grpc_error_string(new_error)); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1015 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1016 | if (new_error != GRPC_ERROR_NONE) { |
| 1017 | new_error = grpc_error_add_child(new_error, error); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1018 | waiting_for_pick_batches_fail(elem, new_error); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1019 | } else { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1020 | waiting_for_pick_batches_resume(elem); |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 1021 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1022 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1025 | // Invoked when a pick is completed, on both success or failure. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1026 | static void pick_done_locked(grpc_call_element* elem, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1027 | call_data* calld = (call_data*)elem->call_data; |
| 1028 | channel_data* chand = (channel_data*)elem->channel_data; |
David Garcia Quintas | be1b7f9 | 2018-01-12 14:01:38 -0800 | [diff] [blame] | 1029 | if (calld->pick.connected_subchannel == nullptr) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1030 | // Failed to create subchannel. |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1031 | GRPC_ERROR_UNREF(calld->error); |
| 1032 | calld->error = error == GRPC_ERROR_NONE |
| 1033 | ? GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 1034 | "Call dropped by load balancing policy") |
| 1035 | : GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1036 | "Failed to create subchannel", &error, 1); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1037 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1038 | gpr_log(GPR_DEBUG, |
| 1039 | "chand=%p calld=%p: failed to create subchannel: error=%s", chand, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1040 | calld, grpc_error_string(calld->error)); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1041 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1042 | waiting_for_pick_batches_fail(elem, GRPC_ERROR_REF(calld->error)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1043 | } else { |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 1044 | /* Create call on subchannel. */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1045 | create_subchannel_call_locked(elem, GRPC_ERROR_REF(error)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1046 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1047 | GRPC_ERROR_UNREF(error); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1050 | // A wrapper around pick_done_locked() that is used in cases where |
| 1051 | // either (a) the pick was deferred pending a resolver result or (b) the |
| 1052 | // pick was done asynchronously. Removes the call's polling entity from |
| 1053 | // chand->interested_parties before invoking pick_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1054 | static void async_pick_done_locked(grpc_call_element* elem, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1055 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1056 | call_data* calld = (call_data*)elem->call_data; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1057 | grpc_polling_entity_del_from_pollset_set(calld->pollent, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1058 | chand->interested_parties); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1059 | pick_done_locked(elem, error); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | // Note: This runs under the client_channel combiner, but will NOT be |
| 1063 | // holding the call combiner. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1064 | static void pick_callback_cancel_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1065 | grpc_call_element* elem = (grpc_call_element*)arg; |
| 1066 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1067 | call_data* calld = (call_data*)elem->call_data; |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1068 | // Note: chand->lb_policy may have changed since we started our pick, |
| 1069 | // in which case we will be cancelling the pick on a policy other than |
| 1070 | // the one we started it on. However, this will just be a no-op. |
| 1071 | if (error != GRPC_ERROR_NONE && chand->lb_policy != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1072 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1073 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: cancelling pick from LB policy %p", |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1074 | chand, calld, chand->lb_policy); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1075 | } |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1076 | grpc_lb_policy_cancel_pick_locked(chand->lb_policy, &calld->pick, |
| 1077 | GRPC_ERROR_REF(error)); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1078 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1079 | GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback_cancel"); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | // Callback invoked by grpc_lb_policy_pick_locked() for async picks. |
| 1083 | // Unrefs the LB policy and invokes async_pick_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1084 | static void pick_callback_done_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1085 | grpc_call_element* elem = (grpc_call_element*)arg; |
| 1086 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1087 | call_data* calld = (call_data*)elem->call_data; |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1088 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1089 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed asynchronously", |
| 1090 | chand, calld); |
| 1091 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1092 | async_pick_done_locked(elem, GRPC_ERROR_REF(error)); |
Ken Payson | f069dd4 | 2018-02-05 09:15:05 -0800 | [diff] [blame] | 1093 | GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback"); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | // Takes a ref to chand->lb_policy and calls grpc_lb_policy_pick_locked(). |
| 1097 | // If the pick was completed synchronously, unrefs the LB policy and |
| 1098 | // returns true. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1099 | static bool pick_callback_start_locked(grpc_call_element* elem) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1100 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1101 | call_data* calld = (call_data*)elem->call_data; |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1102 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1103 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: starting pick on lb_policy=%p", |
| 1104 | chand, calld, chand->lb_policy); |
| 1105 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1106 | apply_service_config_to_call_locked(elem); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1107 | // If the application explicitly set wait_for_ready, use that. |
| 1108 | // Otherwise, if the service config specified a value for this |
| 1109 | // method, use that. |
| 1110 | uint32_t initial_metadata_flags = |
| 1111 | calld->initial_metadata_batch->payload->send_initial_metadata |
| 1112 | .send_initial_metadata_flags; |
| 1113 | const bool wait_for_ready_set_from_api = |
| 1114 | initial_metadata_flags & |
| 1115 | GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET; |
| 1116 | const bool wait_for_ready_set_from_service_config = |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1117 | calld->method_params != nullptr && |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1118 | calld->method_params->wait_for_ready != WAIT_FOR_READY_UNSET; |
| 1119 | if (!wait_for_ready_set_from_api && wait_for_ready_set_from_service_config) { |
| 1120 | if (calld->method_params->wait_for_ready == WAIT_FOR_READY_TRUE) { |
| 1121 | initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 1122 | } else { |
| 1123 | initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 1124 | } |
| 1125 | } |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1126 | calld->pick.initial_metadata = |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1127 | calld->initial_metadata_batch->payload->send_initial_metadata |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1128 | .send_initial_metadata; |
| 1129 | calld->pick.initial_metadata_flags = initial_metadata_flags; |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1130 | GRPC_CLOSURE_INIT(&calld->lb_pick_closure, pick_callback_done_locked, elem, |
| 1131 | grpc_combiner_scheduler(chand->combiner)); |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1132 | calld->pick.on_complete = &calld->lb_pick_closure; |
Ken Payson | f069dd4 | 2018-02-05 09:15:05 -0800 | [diff] [blame] | 1133 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback"); |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1134 | const bool pick_done = |
| 1135 | grpc_lb_policy_pick_locked(chand->lb_policy, &calld->pick); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1136 | if (pick_done) { |
| 1137 | /* synchronous grpc_lb_policy_pick call. Unref the LB policy. */ |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1138 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1139 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed synchronously", |
| 1140 | chand, calld); |
| 1141 | } |
Ken Payson | f069dd4 | 2018-02-05 09:15:05 -0800 | [diff] [blame] | 1142 | GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback"); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1143 | } else { |
| 1144 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback_cancel"); |
| 1145 | grpc_call_combiner_set_notify_on_cancel( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1146 | calld->call_combiner, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1147 | GRPC_CLOSURE_INIT(&calld->lb_pick_cancel_closure, |
| 1148 | pick_callback_cancel_locked, elem, |
| 1149 | grpc_combiner_scheduler(chand->combiner))); |
| 1150 | } |
| 1151 | return pick_done; |
| 1152 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1153 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1154 | typedef struct { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1155 | grpc_call_element* elem; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1156 | bool finished; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1157 | grpc_closure closure; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1158 | grpc_closure cancel_closure; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1159 | } pick_after_resolver_result_args; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1160 | |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1161 | // Note: This runs under the client_channel combiner, but will NOT be |
| 1162 | // holding the call combiner. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1163 | static void pick_after_resolver_result_cancel_locked(void* arg, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1164 | grpc_error* error) { |
| 1165 | pick_after_resolver_result_args* args = (pick_after_resolver_result_args*)arg; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1166 | if (args->finished) { |
| 1167 | gpr_free(args); |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1168 | return; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1169 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1170 | // If we don't yet have a resolver result, then a closure for |
| 1171 | // pick_after_resolver_result_done_locked() will have been added to |
| 1172 | // chand->waiting_for_resolver_result_closures, and it may not be invoked |
| 1173 | // until after this call has been destroyed. We mark the operation as |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1174 | // finished, so that when pick_after_resolver_result_done_locked() |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1175 | // is called, it will be a no-op. We also immediately invoke |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1176 | // async_pick_done_locked() to propagate the error back to the caller. |
| 1177 | args->finished = true; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1178 | grpc_call_element* elem = args->elem; |
| 1179 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1180 | call_data* calld = (call_data*)elem->call_data; |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1181 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1182 | gpr_log(GPR_DEBUG, |
| 1183 | "chand=%p calld=%p: cancelling pick waiting for resolver result", |
| 1184 | chand, calld); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1185 | } |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1186 | // Note: Although we are not in the call combiner here, we are |
| 1187 | // basically stealing the call combiner from the pending pick, so |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1188 | // it's safe to call async_pick_done_locked() here -- we are |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1189 | // essentially calling it here instead of calling it in |
| 1190 | // pick_after_resolver_result_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1191 | async_pick_done_locked(elem, GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1192 | "Pick cancelled", &error, 1)); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1195 | static void pick_after_resolver_result_start_locked(grpc_call_element* elem); |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 1196 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1197 | static void pick_after_resolver_result_done_locked(void* arg, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1198 | grpc_error* error) { |
| 1199 | pick_after_resolver_result_args* args = (pick_after_resolver_result_args*)arg; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1200 | if (args->finished) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1201 | /* cancelled, do nothing */ |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1202 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1203 | gpr_log(GPR_DEBUG, "call cancelled before resolver result"); |
| 1204 | } |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1205 | gpr_free(args); |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1206 | return; |
| 1207 | } |
| 1208 | args->finished = true; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1209 | grpc_call_element* elem = args->elem; |
| 1210 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1211 | call_data* calld = (call_data*)elem->call_data; |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1212 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1213 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1214 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver failed to return data", |
| 1215 | chand, calld); |
| 1216 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1217 | async_pick_done_locked(elem, GRPC_ERROR_REF(error)); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1218 | } else if (chand->lb_policy != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1219 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1220 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver returned, doing pick", |
| 1221 | chand, calld); |
| 1222 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1223 | if (pick_callback_start_locked(elem)) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1224 | // Even if the LB policy returns a result synchronously, we have |
| 1225 | // already added our polling entity to chand->interested_parties |
| 1226 | // in order to wait for the resolver result, so we need to |
| 1227 | // remove it here. Therefore, we call async_pick_done_locked() |
| 1228 | // instead of pick_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1229 | async_pick_done_locked(elem, GRPC_ERROR_NONE); |
Mark D. Roth | 9dab7d5 | 2016-10-07 07:48:03 -0700 | [diff] [blame] | 1230 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1231 | } |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 1232 | // TODO(roth): It should be impossible for chand->lb_policy to be NULL |
| 1233 | // here, so the rest of this code should never actually be executed. |
| 1234 | // However, we have reports of a crash on iOS that triggers this case, |
| 1235 | // so we are temporarily adding this to restore branches that were |
| 1236 | // removed in https://github.com/grpc/grpc/pull/12297. Need to figure |
| 1237 | // out what is actually causing this to occur and then figure out the |
| 1238 | // right way to deal with it. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1239 | else if (chand->resolver != nullptr) { |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 1240 | // No LB policy, so try again. |
ncteisen | 72afb76 | 2017-11-10 12:23:12 -0800 | [diff] [blame] | 1241 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 1242 | gpr_log(GPR_DEBUG, |
| 1243 | "chand=%p calld=%p: resolver returned but no LB policy, " |
| 1244 | "trying again", |
| 1245 | chand, calld); |
| 1246 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1247 | pick_after_resolver_result_start_locked(elem); |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 1248 | } else { |
ncteisen | 72afb76 | 2017-11-10 12:23:12 -0800 | [diff] [blame] | 1249 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 1250 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver disconnected", chand, |
| 1251 | calld); |
| 1252 | } |
| 1253 | async_pick_done_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1254 | elem, GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected")); |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 1255 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1258 | static void pick_after_resolver_result_start_locked(grpc_call_element* elem) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1259 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1260 | call_data* calld = (call_data*)elem->call_data; |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1261 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1262 | gpr_log(GPR_DEBUG, |
| 1263 | "chand=%p calld=%p: deferring pick pending resolver result", chand, |
| 1264 | calld); |
Mark D. Roth | 64a317c | 2017-05-02 08:27:08 -0700 | [diff] [blame] | 1265 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1266 | pick_after_resolver_result_args* args = |
| 1267 | (pick_after_resolver_result_args*)gpr_zalloc(sizeof(*args)); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1268 | args->elem = elem; |
| 1269 | GRPC_CLOSURE_INIT(&args->closure, pick_after_resolver_result_done_locked, |
| 1270 | args, grpc_combiner_scheduler(chand->combiner)); |
| 1271 | grpc_closure_list_append(&chand->waiting_for_resolver_result_closures, |
| 1272 | &args->closure, GRPC_ERROR_NONE); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1273 | grpc_call_combiner_set_notify_on_cancel( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1274 | calld->call_combiner, |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1275 | GRPC_CLOSURE_INIT(&args->cancel_closure, |
| 1276 | pick_after_resolver_result_cancel_locked, args, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1277 | grpc_combiner_scheduler(chand->combiner))); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1280 | static void start_pick_locked(void* arg, grpc_error* ignored) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1281 | grpc_call_element* elem = (grpc_call_element*)arg; |
| 1282 | call_data* calld = (call_data*)elem->call_data; |
| 1283 | channel_data* chand = (channel_data*)elem->channel_data; |
David Garcia Quintas | be1b7f9 | 2018-01-12 14:01:38 -0800 | [diff] [blame] | 1284 | GPR_ASSERT(calld->pick.connected_subchannel == nullptr); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1285 | if (chand->lb_policy != nullptr) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1286 | // We already have an LB policy, so ask it for a pick. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1287 | if (pick_callback_start_locked(elem)) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1288 | // Pick completed synchronously. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1289 | pick_done_locked(elem, GRPC_ERROR_NONE); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1290 | return; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1291 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1292 | } else { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1293 | // We do not yet have an LB policy, so wait for a resolver result. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1294 | if (chand->resolver == nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1295 | pick_done_locked(elem, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1296 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected")); |
| 1297 | return; |
| 1298 | } |
| 1299 | if (!chand->started_resolving) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1300 | start_resolving_locked(chand); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1301 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1302 | pick_after_resolver_result_start_locked(elem); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1303 | } |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1304 | // We need to wait for either a resolver result or for an async result |
| 1305 | // from the LB policy. Add the polling entity from call_data to the |
| 1306 | // channel_data's interested_parties, so that the I/O of the LB policy |
| 1307 | // and resolver can be done under it. The polling entity will be |
| 1308 | // removed in async_pick_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1309 | grpc_polling_entity_add_to_pollset_set(calld->pollent, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1310 | chand->interested_parties); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1313 | static void on_complete(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1314 | grpc_call_element* elem = (grpc_call_element*)arg; |
| 1315 | call_data* calld = (call_data*)elem->call_data; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1316 | if (calld->retry_throttle_data != nullptr) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1317 | if (error == GRPC_ERROR_NONE) { |
| 1318 | grpc_server_retry_throttle_data_record_success( |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 1319 | calld->retry_throttle_data); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1320 | } else { |
| 1321 | // TODO(roth): In a subsequent PR, check the return value here and |
Mark D. Roth | b332256 | 2017-02-23 14:38:02 -0800 | [diff] [blame] | 1322 | // decide whether or not to retry. Note that we should only |
| 1323 | // record failures whose statuses match the configured retryable |
| 1324 | // or non-fatal status codes. |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1325 | grpc_server_retry_throttle_data_record_failure( |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 1326 | calld->retry_throttle_data); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1327 | } |
| 1328 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1329 | GRPC_CLOSURE_RUN(calld->original_on_complete, GRPC_ERROR_REF(error)); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1330 | } |
| 1331 | |
Craig Tiller | e1b51da | 2017-03-31 15:44:33 -0700 | [diff] [blame] | 1332 | static void cc_start_transport_stream_op_batch( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1333 | grpc_call_element* elem, grpc_transport_stream_op_batch* batch) { |
yang-g | ce1cfea | 2018-01-31 15:59:50 -0800 | [diff] [blame] | 1334 | GPR_TIMER_SCOPE("cc_start_transport_stream_op_batch", 0); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1335 | call_data* calld = (call_data*)elem->call_data; |
| 1336 | channel_data* chand = (channel_data*)elem->channel_data; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1337 | if (chand->deadline_checking_enabled) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1338 | grpc_deadline_state_client_start_transport_stream_op_batch(elem, batch); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1339 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1340 | // If we've previously been cancelled, immediately fail any new batches. |
| 1341 | if (calld->error != GRPC_ERROR_NONE) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1342 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1343 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: failing batch with error: %s", |
| 1344 | chand, calld, grpc_error_string(calld->error)); |
| 1345 | } |
| 1346 | grpc_transport_stream_op_batch_finish_with_failure( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1347 | batch, GRPC_ERROR_REF(calld->error), calld->call_combiner); |
yang-g | ce1cfea | 2018-01-31 15:59:50 -0800 | [diff] [blame] | 1348 | return; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1349 | } |
| 1350 | if (batch->cancel_stream) { |
| 1351 | // Stash a copy of cancel_error in our call data, so that we can use |
| 1352 | // it for subsequent operations. This ensures that if the call is |
| 1353 | // cancelled before any batches are passed down (e.g., if the deadline |
| 1354 | // is in the past when the call starts), we can return the right |
| 1355 | // error to the caller when the first batch does get passed down. |
| 1356 | GRPC_ERROR_UNREF(calld->error); |
| 1357 | calld->error = GRPC_ERROR_REF(batch->payload->cancel_stream.cancel_error); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1358 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1359 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: recording cancel_error=%s", chand, |
| 1360 | calld, grpc_error_string(calld->error)); |
| 1361 | } |
| 1362 | // If we have a subchannel call, send the cancellation batch down. |
| 1363 | // Otherwise, fail all pending batches. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1364 | if (calld->subchannel_call != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1365 | grpc_subchannel_call_process_op(calld->subchannel_call, batch); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1366 | } else { |
| 1367 | waiting_for_pick_batches_add(calld, batch); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1368 | waiting_for_pick_batches_fail(elem, GRPC_ERROR_REF(calld->error)); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1369 | } |
yang-g | ce1cfea | 2018-01-31 15:59:50 -0800 | [diff] [blame] | 1370 | return; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1371 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1372 | // Intercept on_complete for recv_trailing_metadata so that we can |
| 1373 | // check retry throttle status. |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1374 | if (batch->recv_trailing_metadata) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1375 | GPR_ASSERT(batch->on_complete != nullptr); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1376 | calld->original_on_complete = batch->on_complete; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1377 | GRPC_CLOSURE_INIT(&calld->on_complete, on_complete, elem, |
| 1378 | grpc_schedule_on_exec_ctx); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1379 | batch->on_complete = &calld->on_complete; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1380 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1381 | // Check if we've already gotten a subchannel call. |
| 1382 | // Note that once we have completed the pick, we do not need to enter |
| 1383 | // the channel combiner, which is more efficient (especially for |
| 1384 | // streaming calls). |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1385 | if (calld->subchannel_call != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1386 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1387 | gpr_log(GPR_DEBUG, |
| 1388 | "chand=%p calld=%p: sending batch to subchannel_call=%p", chand, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1389 | calld, calld->subchannel_call); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1390 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1391 | grpc_subchannel_call_process_op(calld->subchannel_call, batch); |
yang-g | ce1cfea | 2018-01-31 15:59:50 -0800 | [diff] [blame] | 1392 | return; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1393 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1394 | // We do not yet have a subchannel call. |
| 1395 | // Add the batch to the waiting-for-pick list. |
| 1396 | waiting_for_pick_batches_add(calld, batch); |
| 1397 | // For batches containing a send_initial_metadata op, enter the channel |
| 1398 | // combiner to start a pick. |
| 1399 | if (batch->send_initial_metadata) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1400 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1401 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: entering client_channel combiner", |
| 1402 | chand, calld); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1403 | } |
| 1404 | GRPC_CLOSURE_SCHED( |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1405 | GRPC_CLOSURE_INIT(&batch->handler_private.closure, start_pick_locked, |
| 1406 | elem, grpc_combiner_scheduler(chand->combiner)), |
| 1407 | GRPC_ERROR_NONE); |
| 1408 | } else { |
| 1409 | // For all other batches, release the call combiner. |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1410 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1411 | gpr_log(GPR_DEBUG, |
| 1412 | "chand=%p calld=%p: saved batch, yeilding call combiner", chand, |
| 1413 | calld); |
| 1414 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1415 | GRPC_CALL_COMBINER_STOP(calld->call_combiner, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1416 | "batch does not include send_initial_metadata"); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1417 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1420 | /* Constructor for call_data */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1421 | static grpc_error* cc_init_call_elem(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1422 | const grpc_call_element_args* args) { |
| 1423 | call_data* calld = (call_data*)elem->call_data; |
| 1424 | channel_data* chand = (channel_data*)elem->channel_data; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1425 | // Initialize data members. |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1426 | calld->path = grpc_slice_ref_internal(args->path); |
Mark D. Roth | ff08f33 | 2016-10-14 13:01:01 -0700 | [diff] [blame] | 1427 | calld->call_start_time = args->start_time; |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 1428 | calld->deadline = args->deadline; |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame] | 1429 | calld->arena = args->arena; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1430 | calld->owning_call = args->call_stack; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1431 | calld->call_combiner = args->call_combiner; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1432 | if (chand->deadline_checking_enabled) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1433 | grpc_deadline_state_init(elem, args->call_stack, args->call_combiner, |
| 1434 | calld->deadline); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1435 | } |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 1436 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /* Destructor for call_data */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1440 | static void cc_destroy_call_elem(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1441 | const grpc_call_final_info* final_info, |
| 1442 | grpc_closure* then_schedule_closure) { |
| 1443 | call_data* calld = (call_data*)elem->call_data; |
| 1444 | channel_data* chand = (channel_data*)elem->channel_data; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1445 | if (chand->deadline_checking_enabled) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1446 | grpc_deadline_state_destroy(elem); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1447 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1448 | grpc_slice_unref_internal(calld->path); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1449 | if (calld->method_params != nullptr) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1450 | method_parameters_unref(calld->method_params); |
| 1451 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1452 | GRPC_ERROR_UNREF(calld->error); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1453 | if (calld->subchannel_call != nullptr) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 1454 | grpc_subchannel_call_set_cleanup_closure(calld->subchannel_call, |
Craig Tiller | f7c8c9f | 2017-05-17 15:22:05 -0700 | [diff] [blame] | 1455 | then_schedule_closure); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1456 | then_schedule_closure = nullptr; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1457 | GRPC_SUBCHANNEL_CALL_UNREF(calld->subchannel_call, |
Craig Tiller | f7c8c9f | 2017-05-17 15:22:05 -0700 | [diff] [blame] | 1458 | "client_channel_destroy_call"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1459 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1460 | GPR_ASSERT(calld->waiting_for_pick_batches_count == 0); |
David Garcia Quintas | be1b7f9 | 2018-01-12 14:01:38 -0800 | [diff] [blame] | 1461 | if (calld->pick.connected_subchannel != nullptr) { |
David Garcia Quintas | dfa2851 | 2018-01-11 18:31:13 -0800 | [diff] [blame] | 1462 | calld->pick.connected_subchannel.reset(); |
Craig Tiller | 693d394 | 2016-10-27 16:51:25 -0700 | [diff] [blame] | 1463 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1464 | for (size_t i = 0; i < GRPC_CONTEXT_COUNT; ++i) { |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 1465 | if (calld->pick.subchannel_call_context[i].value != nullptr) { |
| 1466 | calld->pick.subchannel_call_context[i].destroy( |
| 1467 | calld->pick.subchannel_call_context[i].value); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1468 | } |
| 1469 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1470 | GRPC_CLOSURE_SCHED(then_schedule_closure, GRPC_ERROR_NONE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1473 | static void cc_set_pollset_or_pollset_set(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1474 | grpc_polling_entity* pollent) { |
| 1475 | call_data* calld = (call_data*)elem->call_data; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 1476 | calld->pollent = pollent; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1479 | /************************************************************************* |
| 1480 | * EXPORTED SYMBOLS |
| 1481 | */ |
| 1482 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1483 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 1484 | cc_start_transport_stream_op_batch, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1485 | cc_start_transport_op, |
| 1486 | sizeof(call_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1487 | cc_init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 1488 | cc_set_pollset_or_pollset_set, |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1489 | cc_destroy_call_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1490 | sizeof(channel_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1491 | cc_init_channel_elem, |
| 1492 | cc_destroy_channel_elem, |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 1493 | cc_get_channel_info, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1494 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 1495 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1496 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1497 | static void try_to_connect_locked(void* arg, grpc_error* error_ignored) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1498 | channel_data* chand = (channel_data*)arg; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1499 | if (chand->lb_policy != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1500 | grpc_lb_policy_exit_idle_locked(chand->lb_policy); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1501 | } else { |
| 1502 | chand->exit_idle_when_lb_policy_arrives = true; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1503 | if (!chand->started_resolving && chand->resolver != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1504 | start_resolving_locked(chand); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1505 | } |
| 1506 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1507 | GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "try_to_connect"); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1508 | } |
| 1509 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1510 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1511 | grpc_channel_element* elem, int try_to_connect) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1512 | channel_data* chand = (channel_data*)elem->channel_data; |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 1513 | grpc_connectivity_state out = |
| 1514 | grpc_connectivity_state_check(&chand->state_tracker); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1515 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1516 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "try_to_connect"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1517 | GRPC_CLOSURE_SCHED( |
Yash Tibrewal | 0ee7574 | 2017-10-13 16:07:13 -0700 | [diff] [blame] | 1518 | GRPC_CLOSURE_CREATE(try_to_connect_locked, chand, |
| 1519 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1520 | GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1521 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1522 | return out; |
| 1523 | } |
| 1524 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1525 | typedef struct external_connectivity_watcher { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1526 | channel_data* chand; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1527 | grpc_polling_entity pollent; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1528 | grpc_closure* on_complete; |
| 1529 | grpc_closure* watcher_timer_init; |
| 1530 | grpc_connectivity_state* state; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1531 | grpc_closure my_closure; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1532 | struct external_connectivity_watcher* next; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1533 | } external_connectivity_watcher; |
| 1534 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1535 | static external_connectivity_watcher* lookup_external_connectivity_watcher( |
| 1536 | channel_data* chand, grpc_closure* on_complete) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1537 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1538 | external_connectivity_watcher* w = |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1539 | chand->external_connectivity_watcher_list_head; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1540 | while (w != nullptr && w->on_complete != on_complete) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1541 | w = w->next; |
| 1542 | } |
| 1543 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 1544 | return w; |
| 1545 | } |
| 1546 | |
| 1547 | static void external_connectivity_watcher_list_append( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1548 | channel_data* chand, external_connectivity_watcher* w) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1549 | GPR_ASSERT(!lookup_external_connectivity_watcher(chand, w->on_complete)); |
| 1550 | |
| 1551 | gpr_mu_lock(&w->chand->external_connectivity_watcher_list_mu); |
| 1552 | GPR_ASSERT(!w->next); |
| 1553 | w->next = chand->external_connectivity_watcher_list_head; |
| 1554 | chand->external_connectivity_watcher_list_head = w; |
| 1555 | gpr_mu_unlock(&w->chand->external_connectivity_watcher_list_mu); |
| 1556 | } |
| 1557 | |
| 1558 | static void external_connectivity_watcher_list_remove( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1559 | channel_data* chand, external_connectivity_watcher* too_remove) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1560 | GPR_ASSERT( |
| 1561 | lookup_external_connectivity_watcher(chand, too_remove->on_complete)); |
| 1562 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
| 1563 | if (too_remove == chand->external_connectivity_watcher_list_head) { |
| 1564 | chand->external_connectivity_watcher_list_head = too_remove->next; |
| 1565 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 1566 | return; |
| 1567 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1568 | external_connectivity_watcher* w = |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1569 | chand->external_connectivity_watcher_list_head; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1570 | while (w != nullptr) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1571 | if (w->next == too_remove) { |
| 1572 | w->next = w->next->next; |
| 1573 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 1574 | return; |
| 1575 | } |
| 1576 | w = w->next; |
| 1577 | } |
| 1578 | GPR_UNREACHABLE_CODE(return ); |
| 1579 | } |
| 1580 | |
| 1581 | int grpc_client_channel_num_external_connectivity_watchers( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1582 | grpc_channel_element* elem) { |
| 1583 | channel_data* chand = (channel_data*)elem->channel_data; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1584 | int count = 0; |
| 1585 | |
| 1586 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1587 | external_connectivity_watcher* w = |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1588 | chand->external_connectivity_watcher_list_head; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1589 | while (w != nullptr) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1590 | count++; |
| 1591 | w = w->next; |
| 1592 | } |
| 1593 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 1594 | |
| 1595 | return count; |
| 1596 | } |
| 1597 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1598 | static void on_external_watch_complete_locked(void* arg, grpc_error* error) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1599 | external_connectivity_watcher* w = (external_connectivity_watcher*)arg; |
| 1600 | grpc_closure* follow_up = w->on_complete; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1601 | grpc_polling_entity_del_from_pollset_set(&w->pollent, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1602 | w->chand->interested_parties); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1603 | GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 1604 | "external_connectivity_watcher"); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1605 | external_connectivity_watcher_list_remove(w->chand, w); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1606 | gpr_free(w); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1607 | GRPC_CLOSURE_RUN(follow_up, GRPC_ERROR_REF(error)); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1610 | static void watch_connectivity_state_locked(void* arg, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1611 | grpc_error* error_ignored) { |
| 1612 | external_connectivity_watcher* w = (external_connectivity_watcher*)arg; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1613 | external_connectivity_watcher* found = nullptr; |
| 1614 | if (w->state != nullptr) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1615 | external_connectivity_watcher_list_append(w->chand, w); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1616 | GRPC_CLOSURE_RUN(w->watcher_timer_init, GRPC_ERROR_NONE); |
Alexander Polcyn | 2004e39 | 2017-10-16 15:14:46 -0700 | [diff] [blame] | 1617 | GRPC_CLOSURE_INIT(&w->my_closure, on_external_watch_complete_locked, w, |
| 1618 | grpc_combiner_scheduler(w->chand->combiner)); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1619 | grpc_connectivity_state_notify_on_state_change(&w->chand->state_tracker, |
| 1620 | w->state, &w->my_closure); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1621 | } else { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1622 | GPR_ASSERT(w->watcher_timer_init == nullptr); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1623 | found = lookup_external_connectivity_watcher(w->chand, w->on_complete); |
| 1624 | if (found) { |
| 1625 | GPR_ASSERT(found->on_complete == w->on_complete); |
| 1626 | grpc_connectivity_state_notify_on_state_change( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1627 | &found->chand->state_tracker, nullptr, &found->my_closure); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1628 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1629 | grpc_polling_entity_del_from_pollset_set(&w->pollent, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1630 | w->chand->interested_parties); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1631 | GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack, |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1632 | "external_connectivity_watcher"); |
| 1633 | gpr_free(w); |
| 1634 | } |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1635 | } |
| 1636 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1637 | void grpc_client_channel_watch_connectivity_state( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1638 | grpc_channel_element* elem, grpc_polling_entity pollent, |
| 1639 | grpc_connectivity_state* state, grpc_closure* closure, |
| 1640 | grpc_closure* watcher_timer_init) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1641 | channel_data* chand = (channel_data*)elem->channel_data; |
| 1642 | external_connectivity_watcher* w = |
| 1643 | (external_connectivity_watcher*)gpr_zalloc(sizeof(*w)); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1644 | w->chand = chand; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1645 | w->pollent = pollent; |
Mark D. Roth | 9221083 | 2017-05-02 15:04:39 -0700 | [diff] [blame] | 1646 | w->on_complete = closure; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1647 | w->state = state; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1648 | w->watcher_timer_init = watcher_timer_init; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 1649 | grpc_polling_entity_add_to_pollset_set(&w->pollent, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1650 | chand->interested_parties); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 1651 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 1652 | "external_connectivity_watcher"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1653 | GRPC_CLOSURE_SCHED( |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1654 | GRPC_CLOSURE_INIT(&w->my_closure, watch_connectivity_state_locked, w, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 1655 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1656 | GRPC_ERROR_NONE); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1657 | } |