blob: cee1ddb8b9efbe96ff8f269b552d79243ceeba1b [file] [log] [blame]
Craig Tiller48cb07c2015-07-15 16:16:15 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/surface/channel.h"
35
36#include <grpc/support/alloc.h>
37#include <grpc/support/log.h>
38
39#include "src/core/channel/client_channel.h"
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070040#include "src/core/channel/client_uchannel.h"
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070041#include "src/core/iomgr/timer.h"
Masood Malekghassemi76c3d742015-08-19 18:22:53 -070042#include "src/core/surface/api_trace.h"
Craig Tiller48cb07c2015-07-15 16:16:15 -070043#include "src/core/surface/completion_queue.h"
44
Craig Tillera82950e2015-09-22 12:33:20 -070045grpc_connectivity_state grpc_channel_check_connectivity_state(
46 grpc_channel *channel, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070047 /* forward through to the underlying client channel */
Craig Tillera82950e2015-09-22 12:33:20 -070048 grpc_channel_element *client_channel_elem =
49 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -070050 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller000cd8f2015-09-18 07:20:29 -070051 grpc_connectivity_state state;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -070052 GRPC_API_TRACE(
Craig Tiller4de3e4f2015-10-05 08:55:50 -070053 "grpc_channel_check_connectivity_state(channel=%p, try_to_connect=%d)", 2,
54 (channel, try_to_connect));
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070055 if (client_channel_elem->filter == &grpc_client_channel_filter) {
56 state = grpc_client_channel_check_connectivity_state(
57 &exec_ctx, client_channel_elem, try_to_connect);
Craig Tiller63010382015-09-24 15:00:58 -070058 grpc_exec_ctx_finish(&exec_ctx);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070059 return state;
Craig Tillera82950e2015-09-22 12:33:20 -070060 }
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070061 if (client_channel_elem->filter == &grpc_client_uchannel_filter) {
62 state = grpc_client_uchannel_check_connectivity_state(
63 &exec_ctx, client_channel_elem, try_to_connect);
64 grpc_exec_ctx_finish(&exec_ctx);
65 return state;
66 }
67 gpr_log(GPR_ERROR,
68 "grpc_channel_check_connectivity_state called on something that is "
69 "not a (u)client channel, but '%s'",
70 client_channel_elem->filter->name);
Craig Tillera82950e2015-09-22 12:33:20 -070071 grpc_exec_ctx_finish(&exec_ctx);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070072 return GRPC_CHANNEL_FATAL_FAILURE;
Craig Tiller48cb07c2015-07-15 16:16:15 -070073}
74
Craig Tillera82950e2015-09-22 12:33:20 -070075typedef enum {
Craig Tiller48cb07c2015-07-15 16:16:15 -070076 WAITING,
77 CALLING_BACK,
78 CALLING_BACK_AND_FINISHED,
79 CALLED_BACK
80} callback_phase;
81
Craig Tillera82950e2015-09-22 12:33:20 -070082typedef struct {
Craig Tiller48cb07c2015-07-15 16:16:15 -070083 gpr_mu mu;
84 callback_phase phase;
85 int success;
yang-ga63fe4e2015-09-18 00:02:59 -070086 int removed;
Craig Tiller33825112015-09-18 07:44:19 -070087 grpc_closure on_complete;
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070088 grpc_timer alarm;
Craig Tiller48cb07c2015-07-15 16:16:15 -070089 grpc_connectivity_state state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070090 grpc_completion_queue *cq;
91 grpc_cq_completion completion_storage;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070092 grpc_channel *channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -070093 void *tag;
94} state_watcher;
95
Craig Tillera82950e2015-09-22 12:33:20 -070096static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) {
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070097 grpc_channel_element *client_channel_elem = grpc_channel_stack_last_element(
98 grpc_channel_get_channel_stack(w->channel));
99 if (client_channel_elem->filter == &grpc_client_channel_filter) {
100 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel,
101 "watch_channel_connectivity");
102 } else if (client_channel_elem->filter == &grpc_client_uchannel_filter) {
103 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel,
104 "watch_uchannel_connectivity");
105 } else {
106 abort();
107 }
Craig Tillera82950e2015-09-22 12:33:20 -0700108 gpr_mu_destroy(&w->mu);
109 gpr_free(w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700110}
111
Craig Tillera82950e2015-09-22 12:33:20 -0700112static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw,
113 grpc_cq_completion *ignored) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700114 int delete = 0;
115 state_watcher *w = pw;
Craig Tillera82950e2015-09-22 12:33:20 -0700116 gpr_mu_lock(&w->mu);
117 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700118 case WAITING:
119 case CALLED_BACK:
yang-gb063c872015-10-07 11:40:13 -0700120 GPR_UNREACHABLE_CODE(return );
Craig Tiller48cb07c2015-07-15 16:16:15 -0700121 case CALLING_BACK:
122 w->phase = CALLED_BACK;
123 break;
124 case CALLING_BACK_AND_FINISHED:
125 delete = 1;
126 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700127 }
128 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700129
Craig Tillera82950e2015-09-22 12:33:20 -0700130 if (delete) {
131 delete_state_watcher(exec_ctx, w);
132 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700133}
134
Craig Tillera82950e2015-09-22 12:33:20 -0700135static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
136 int due_to_completion) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700137 int delete = 0;
yang-ga63fe4e2015-09-18 00:02:59 -0700138 grpc_channel_element *client_channel_elem = NULL;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700139
Craig Tillera82950e2015-09-22 12:33:20 -0700140 gpr_mu_lock(&w->mu);
141 if (w->removed == 0) {
142 w->removed = 1;
143 client_channel_elem = grpc_channel_stack_last_element(
144 grpc_channel_get_channel_stack(w->channel));
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700145 if (client_channel_elem->filter == &grpc_client_channel_filter) {
146 grpc_client_channel_del_interested_party(exec_ctx, client_channel_elem,
147 grpc_cq_pollset(w->cq));
148 } else {
149 grpc_client_uchannel_del_interested_party(exec_ctx, client_channel_elem,
150 grpc_cq_pollset(w->cq));
151 }
Craig Tillera82950e2015-09-22 12:33:20 -0700152 }
153 gpr_mu_unlock(&w->mu);
154 if (due_to_completion) {
155 gpr_mu_lock(&w->mu);
156 w->success = 1;
157 gpr_mu_unlock(&w->mu);
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700158 grpc_timer_cancel(exec_ctx, &w->alarm);
Craig Tillera82950e2015-09-22 12:33:20 -0700159 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700160
Craig Tillera82950e2015-09-22 12:33:20 -0700161 gpr_mu_lock(&w->mu);
162 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700163 case WAITING:
164 w->phase = CALLING_BACK;
Craig Tillera82950e2015-09-22 12:33:20 -0700165 grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->success, finished_completion,
166 w, &w->completion_storage);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700167 break;
168 case CALLING_BACK:
169 w->phase = CALLING_BACK_AND_FINISHED;
170 break;
171 case CALLING_BACK_AND_FINISHED:
yang-gb063c872015-10-07 11:40:13 -0700172 GPR_UNREACHABLE_CODE(return );
Craig Tiller48cb07c2015-07-15 16:16:15 -0700173 case CALLED_BACK:
174 delete = 1;
175 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700176 }
177 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700178
Craig Tillera82950e2015-09-22 12:33:20 -0700179 if (delete) {
180 delete_state_watcher(exec_ctx, w);
181 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700182}
183
Craig Tillera82950e2015-09-22 12:33:20 -0700184static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
185 partly_done(exec_ctx, pw, 1);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700186}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700187
Craig Tillera82950e2015-09-22 12:33:20 -0700188static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
189 partly_done(exec_ctx, pw, 0);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700190}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700191
Craig Tillera82950e2015-09-22 12:33:20 -0700192void grpc_channel_watch_connectivity_state(
193 grpc_channel *channel, grpc_connectivity_state last_observed_state,
194 gpr_timespec deadline, grpc_completion_queue *cq, void *tag) {
195 grpc_channel_element *client_channel_elem =
196 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -0700197 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -0700198 state_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700199
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700200 GRPC_API_TRACE(
201 "grpc_channel_watch_connectivity_state("
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700202 "channel=%p, last_observed_state=%d, "
Jan Tattermusch88086372015-12-10 10:54:12 -0800203 "deadline=gpr_timespec { tv_sec: %lld, tv_nsec: %d, clock_type: %d }, "
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700204 "cq=%p, tag=%p)",
Jan Tattermusch88086372015-12-10 10:54:12 -0800205 7, (channel, (int)last_observed_state, (long long)deadline.tv_sec,
206 (int)deadline.tv_nsec, (int)deadline.clock_type, cq, tag));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700207
Craig Tillera82950e2015-09-22 12:33:20 -0700208 grpc_cq_begin_op(cq);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700209
Craig Tillera82950e2015-09-22 12:33:20 -0700210 gpr_mu_init(&w->mu);
211 grpc_closure_init(&w->on_complete, watch_complete, w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700212 w->phase = WAITING;
213 w->state = last_observed_state;
214 w->success = 0;
yang-ga63fe4e2015-09-18 00:02:59 -0700215 w->removed = 0;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700216 w->cq = cq;
217 w->tag = tag;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700218 w->channel = channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700219
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700220 grpc_timer_init(&exec_ctx, &w->alarm,
Craig Tillera82950e2015-09-22 12:33:20 -0700221 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
222 timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700223
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700224 if (client_channel_elem->filter == &grpc_client_channel_filter) {
225 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_channel_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700226 grpc_client_channel_add_interested_party(&exec_ctx, client_channel_elem,
227 grpc_cq_pollset(cq));
228 grpc_client_channel_watch_connectivity_state(&exec_ctx, client_channel_elem,
229 &w->state, &w->on_complete);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700230 } else if (client_channel_elem->filter == &grpc_client_uchannel_filter) {
231 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_uchannel_connectivity");
232 grpc_client_uchannel_add_interested_party(&exec_ctx, client_channel_elem,
233 grpc_cq_pollset(cq));
234 grpc_client_uchannel_watch_connectivity_state(
235 &exec_ctx, client_channel_elem, &w->state, &w->on_complete);
Craig Tillera82950e2015-09-22 12:33:20 -0700236 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700237
Craig Tillera82950e2015-09-22 12:33:20 -0700238 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700239}