blob: d3627a237fb3d44d072b1b9a8d7efc2f133d5e65 [file] [log] [blame]
Craig Tiller48cb07c2015-07-15 16:16:15 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller48cb07c2015-07-15 16:16:15 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Craig Tiller48cb07c2015-07-15 16:16:15 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller48cb07c2015-07-15 16:16:15 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Craig Tiller48cb07c2015-07-15 16:16:15 -070016 *
17 */
18
Craig Tiller9533d042016-03-25 17:11:06 -070019#include "src/core/lib/surface/channel.h"
Craig Tiller48cb07c2015-07-15 16:16:15 -070020
Yash Tibrewalfcd26bc2017-09-25 15:08:28 -070021#include <inttypes.h>
22
Craig Tiller48cb07c2015-07-15 16:16:15 -070023#include <grpc/support/alloc.h>
24#include <grpc/support/log.h>
25
Craig Tiller9eb0fde2017-03-31 16:59:30 -070026#include "src/core/ext/filters/client_channel/client_channel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070027#include "src/core/lib/iomgr/timer.h"
28#include "src/core/lib/surface/api_trace.h"
29#include "src/core/lib/surface/completion_queue.h"
Craig Tiller48cb07c2015-07-15 16:16:15 -070030
Craig Tillera82950e2015-09-22 12:33:20 -070031grpc_connectivity_state grpc_channel_check_connectivity_state(
Craig Tillerbaa14a92017-11-03 09:09:36 -070032 grpc_channel* channel, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070033 /* forward through to the underlying client channel */
Craig Tillerbaa14a92017-11-03 09:09:36 -070034 grpc_channel_element* client_channel_elem =
Craig Tillera82950e2015-09-22 12:33:20 -070035 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -070036 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller000cd8f2015-09-18 07:20:29 -070037 grpc_connectivity_state state;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -070038 GRPC_API_TRACE(
Craig Tiller4de3e4f2015-10-05 08:55:50 -070039 "grpc_channel_check_connectivity_state(channel=%p, try_to_connect=%d)", 2,
40 (channel, try_to_connect));
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070041 if (client_channel_elem->filter == &grpc_client_channel_filter) {
42 state = grpc_client_channel_check_connectivity_state(
43 &exec_ctx, client_channel_elem, try_to_connect);
Craig Tiller63010382015-09-24 15:00:58 -070044 grpc_exec_ctx_finish(&exec_ctx);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070045 return state;
Craig Tillera82950e2015-09-22 12:33:20 -070046 }
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070047 gpr_log(GPR_ERROR,
48 "grpc_channel_check_connectivity_state called on something that is "
Nicolas "Pixel" Noble20b83332016-07-21 19:28:38 +020049 "not a client channel, but '%s'",
50 client_channel_elem->filter->name);
Craig Tillera82950e2015-09-22 12:33:20 -070051 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller48ed92e2016-06-02 11:07:12 -070052 return GRPC_CHANNEL_SHUTDOWN;
Craig Tiller48cb07c2015-07-15 16:16:15 -070053}
54
Craig Tillera82950e2015-09-22 12:33:20 -070055typedef enum {
Craig Tiller48cb07c2015-07-15 16:16:15 -070056 WAITING,
Alexander Polcync3b1f182017-04-18 13:51:36 -070057 READY_TO_CALL_BACK,
Craig Tiller48cb07c2015-07-15 16:16:15 -070058 CALLING_BACK_AND_FINISHED,
Craig Tiller48cb07c2015-07-15 16:16:15 -070059} callback_phase;
60
Craig Tillera82950e2015-09-22 12:33:20 -070061typedef struct {
Craig Tiller48cb07c2015-07-15 16:16:15 -070062 gpr_mu mu;
63 callback_phase phase;
Craig Tiller33825112015-09-18 07:44:19 -070064 grpc_closure on_complete;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080065 grpc_closure on_timeout;
Alexander Polcync3b1f182017-04-18 13:51:36 -070066 grpc_closure watcher_timer_init;
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070067 grpc_timer alarm;
Craig Tiller48cb07c2015-07-15 16:16:15 -070068 grpc_connectivity_state state;
Craig Tillerbaa14a92017-11-03 09:09:36 -070069 grpc_completion_queue* cq;
Craig Tiller48cb07c2015-07-15 16:16:15 -070070 grpc_cq_completion completion_storage;
Craig Tillerbaa14a92017-11-03 09:09:36 -070071 grpc_channel* channel;
72 grpc_error* error;
73 void* tag;
Craig Tiller48cb07c2015-07-15 16:16:15 -070074} state_watcher;
75
Craig Tillerbaa14a92017-11-03 09:09:36 -070076static void delete_state_watcher(grpc_exec_ctx* exec_ctx, state_watcher* w) {
77 grpc_channel_element* client_channel_elem = grpc_channel_stack_last_element(
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070078 grpc_channel_get_channel_stack(w->channel));
79 if (client_channel_elem->filter == &grpc_client_channel_filter) {
80 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel,
81 "watch_channel_connectivity");
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070082 } else {
83 abort();
84 }
Craig Tillera82950e2015-09-22 12:33:20 -070085 gpr_mu_destroy(&w->mu);
86 gpr_free(w);
Craig Tiller48cb07c2015-07-15 16:16:15 -070087}
88
Craig Tillerbaa14a92017-11-03 09:09:36 -070089static void finished_completion(grpc_exec_ctx* exec_ctx, void* pw,
90 grpc_cq_completion* ignored) {
Yash Tibrewalbc130da2017-09-12 22:44:08 -070091 bool should_delete = false;
Craig Tillerbaa14a92017-11-03 09:09:36 -070092 state_watcher* w = (state_watcher*)pw;
Craig Tillera82950e2015-09-22 12:33:20 -070093 gpr_mu_lock(&w->mu);
94 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070095 case WAITING:
Alexander Polcync3b1f182017-04-18 13:51:36 -070096 case READY_TO_CALL_BACK:
yang-gb063c872015-10-07 11:40:13 -070097 GPR_UNREACHABLE_CODE(return );
Craig Tiller48cb07c2015-07-15 16:16:15 -070098 case CALLING_BACK_AND_FINISHED:
Yash Tibrewalbc130da2017-09-12 22:44:08 -070099 should_delete = true;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700100 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700101 }
102 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700103
Yash Tibrewalbc130da2017-09-12 22:44:08 -0700104 if (should_delete) {
Craig Tillera82950e2015-09-22 12:33:20 -0700105 delete_state_watcher(exec_ctx, w);
106 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700107}
108
Craig Tillerbaa14a92017-11-03 09:09:36 -0700109static void partly_done(grpc_exec_ctx* exec_ctx, state_watcher* w,
110 bool due_to_completion, grpc_error* error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700111 if (due_to_completion) {
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700112 grpc_timer_cancel(exec_ctx, &w->alarm);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700113 } else {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700114 grpc_channel_element* client_channel_elem = grpc_channel_stack_last_element(
Alexander Polcync3b1f182017-04-18 13:51:36 -0700115 grpc_channel_get_channel_stack(w->channel));
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700116 grpc_client_channel_watch_connectivity_state(
117 exec_ctx, client_channel_elem,
Craig Tillerbe98d242017-11-10 15:26:57 -0800118 grpc_polling_entity_create_from_pollset(grpc_cq_pollset(w->cq)),
119 nullptr, &w->on_complete, nullptr);
Craig Tillera82950e2015-09-22 12:33:20 -0700120 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700121
Craig Tillera82950e2015-09-22 12:33:20 -0700122 gpr_mu_lock(&w->mu);
Craig Tillercfc8ae12016-05-10 20:49:54 -0700123
Craig Tiller7b435612015-11-24 08:15:05 -0800124 if (due_to_completion) {
Craig Tiller84f75d42017-05-03 13:06:35 -0700125 if (GRPC_TRACER_ON(grpc_trace_operation_failures)) {
Craig Tiller89af61e2016-06-13 15:29:40 -0700126 GRPC_LOG_IF_ERROR("watch_completion_error", GRPC_ERROR_REF(error));
127 }
128 GRPC_ERROR_UNREF(error);
129 error = GRPC_ERROR_NONE;
130 } else {
131 if (error == GRPC_ERROR_NONE) {
ncteisen4b36a3d2017-03-13 19:08:06 -0700132 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
133 "Timed out waiting for connection state change");
Craig Tiller89af61e2016-06-13 15:29:40 -0700134 } else if (error == GRPC_ERROR_CANCELLED) {
135 error = GRPC_ERROR_NONE;
136 }
Craig Tiller7b435612015-11-24 08:15:05 -0800137 }
Craig Tillera82950e2015-09-22 12:33:20 -0700138 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700139 case WAITING:
Alexander Polcync3b1f182017-04-18 13:51:36 -0700140 GRPC_ERROR_REF(error);
141 w->error = error;
142 w->phase = READY_TO_CALL_BACK;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700143 break;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700144 case READY_TO_CALL_BACK:
145 if (error != GRPC_ERROR_NONE) {
146 GPR_ASSERT(!due_to_completion);
147 GRPC_ERROR_UNREF(w->error);
148 GRPC_ERROR_REF(error);
149 w->error = error;
150 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700151 w->phase = CALLING_BACK_AND_FINISHED;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700152 grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->error, finished_completion, w,
153 &w->completion_storage);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700154 break;
155 case CALLING_BACK_AND_FINISHED:
yang-gb063c872015-10-07 11:40:13 -0700156 GPR_UNREACHABLE_CODE(return );
Craig Tiller48cb07c2015-07-15 16:16:15 -0700157 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700158 }
159 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700160
Craig Tillercfc8ae12016-05-10 20:49:54 -0700161 GRPC_ERROR_UNREF(error);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700162}
163
Craig Tillerbaa14a92017-11-03 09:09:36 -0700164static void watch_complete(grpc_exec_ctx* exec_ctx, void* pw,
165 grpc_error* error) {
166 partly_done(exec_ctx, (state_watcher*)pw, true, GRPC_ERROR_REF(error));
Craig Tillerdfff1b82015-09-21 14:39:57 -0700167}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700168
Craig Tillerbaa14a92017-11-03 09:09:36 -0700169static void timeout_complete(grpc_exec_ctx* exec_ctx, void* pw,
170 grpc_error* error) {
171 partly_done(exec_ctx, (state_watcher*)pw, false, GRPC_ERROR_REF(error));
Craig Tillerdfff1b82015-09-21 14:39:57 -0700172}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700173
Craig Tillerbaa14a92017-11-03 09:09:36 -0700174int grpc_channel_num_external_connectivity_watchers(grpc_channel* channel) {
175 grpc_channel_element* client_channel_elem =
Alexander Polcync3b1f182017-04-18 13:51:36 -0700176 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
177 return grpc_client_channel_num_external_connectivity_watchers(
178 client_channel_elem);
179}
180
181typedef struct watcher_timer_init_arg {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700182 state_watcher* w;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700183 gpr_timespec deadline;
184} watcher_timer_init_arg;
185
Craig Tillerbaa14a92017-11-03 09:09:36 -0700186static void watcher_timer_init(grpc_exec_ctx* exec_ctx, void* arg,
187 grpc_error* error_ignored) {
188 watcher_timer_init_arg* wa = (watcher_timer_init_arg*)arg;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700189
190 grpc_timer_init(exec_ctx, &wa->w->alarm,
Craig Tiller9a8c3f32017-07-21 13:14:14 -0700191 grpc_timespec_to_millis_round_up(wa->deadline),
192 &wa->w->on_timeout);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700193 gpr_free(wa);
194}
195
Craig Tillerbaa14a92017-11-03 09:09:36 -0700196int grpc_channel_support_connectivity_watcher(grpc_channel* channel) {
197 grpc_channel_element* client_channel_elem =
Yuchen Zeng6a6d6182017-08-22 13:43:38 -0700198 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
199 return client_channel_elem->filter != &grpc_client_channel_filter ? 0 : 1;
200}
201
Craig Tillera82950e2015-09-22 12:33:20 -0700202void grpc_channel_watch_connectivity_state(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700203 grpc_channel* channel, grpc_connectivity_state last_observed_state,
204 gpr_timespec deadline, grpc_completion_queue* cq, void* tag) {
205 grpc_channel_element* client_channel_elem =
Craig Tillera82950e2015-09-22 12:33:20 -0700206 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -0700207 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700208 state_watcher* w = (state_watcher*)gpr_malloc(sizeof(*w));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700209
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700210 GRPC_API_TRACE(
211 "grpc_channel_watch_connectivity_state("
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700212 "channel=%p, last_observed_state=%d, "
Ken Payson62a6c922016-06-24 11:53:54 -0700213 "deadline=gpr_timespec { tv_sec: %" PRId64
214 ", tv_nsec: %d, clock_type: %d }, "
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700215 "cq=%p, tag=%p)",
Craig Tillerbaa14a92017-11-03 09:09:36 -0700216 7,
217 (channel, (int)last_observed_state, deadline.tv_sec, deadline.tv_nsec,
218 (int)deadline.clock_type, cq, tag));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700219
yang-g7d6b9142017-07-13 11:48:56 -0700220 GPR_ASSERT(grpc_cq_begin_op(cq, tag));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700221
Craig Tillera82950e2015-09-22 12:33:20 -0700222 gpr_mu_init(&w->mu);
ncteisen274bbbe2017-06-08 14:57:11 -0700223 GRPC_CLOSURE_INIT(&w->on_complete, watch_complete, w,
Craig Tillerd4654562017-01-03 08:45:56 -0800224 grpc_schedule_on_exec_ctx);
ncteisen274bbbe2017-06-08 14:57:11 -0700225 GRPC_CLOSURE_INIT(&w->on_timeout, timeout_complete, w,
Masood Malekghassemib5b43722017-01-05 15:07:26 -0800226 grpc_schedule_on_exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700227 w->phase = WAITING;
228 w->state = last_observed_state;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700229 w->cq = cq;
230 w->tag = tag;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700231 w->channel = channel;
Craig Tiller4782d922017-11-10 09:53:21 -0800232 w->error = nullptr;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700233
Craig Tillerbaa14a92017-11-03 09:09:36 -0700234 watcher_timer_init_arg* wa =
235 (watcher_timer_init_arg*)gpr_malloc(sizeof(watcher_timer_init_arg));
Alexander Polcync3b1f182017-04-18 13:51:36 -0700236 wa->w = w;
237 wa->deadline = deadline;
ncteisen274bbbe2017-06-08 14:57:11 -0700238 GRPC_CLOSURE_INIT(&w->watcher_timer_init, watcher_timer_init, wa,
Alexander Polcync3b1f182017-04-18 13:51:36 -0700239 grpc_schedule_on_exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700240
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700241 if (client_channel_elem->filter == &grpc_client_channel_filter) {
242 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_channel_connectivity");
Alexander Polcync3b1f182017-04-18 13:51:36 -0700243 grpc_client_channel_watch_connectivity_state(
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700244 &exec_ctx, client_channel_elem,
245 grpc_polling_entity_create_from_pollset(grpc_cq_pollset(cq)), &w->state,
Alexander Polcync3b1f182017-04-18 13:51:36 -0700246 &w->on_complete, &w->watcher_timer_init);
David Garcia Quintasfb349b92016-03-21 15:37:20 -0700247 } else {
248 abort();
Craig Tillera82950e2015-09-22 12:33:20 -0700249 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700250
Craig Tillera82950e2015-09-22 12:33:20 -0700251 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700252}