blob: e2973bba6e7c5bddc67414574b7088b0903654c1 [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;
Craig Tiller33825112015-09-18 07:44:19 -070086 grpc_closure on_complete;
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070087 grpc_timer alarm;
Craig Tiller48cb07c2015-07-15 16:16:15 -070088 grpc_connectivity_state state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070089 grpc_completion_queue *cq;
90 grpc_cq_completion completion_storage;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070091 grpc_channel *channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -070092 void *tag;
93} state_watcher;
94
Craig Tillera82950e2015-09-22 12:33:20 -070095static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) {
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070096 grpc_channel_element *client_channel_elem = grpc_channel_stack_last_element(
97 grpc_channel_get_channel_stack(w->channel));
98 if (client_channel_elem->filter == &grpc_client_channel_filter) {
99 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel,
100 "watch_channel_connectivity");
101 } else if (client_channel_elem->filter == &grpc_client_uchannel_filter) {
102 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel,
103 "watch_uchannel_connectivity");
104 } else {
105 abort();
106 }
Craig Tillera82950e2015-09-22 12:33:20 -0700107 gpr_mu_destroy(&w->mu);
108 gpr_free(w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700109}
110
Craig Tillera82950e2015-09-22 12:33:20 -0700111static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw,
112 grpc_cq_completion *ignored) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700113 int delete = 0;
114 state_watcher *w = pw;
Craig Tillera82950e2015-09-22 12:33:20 -0700115 gpr_mu_lock(&w->mu);
116 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700117 case WAITING:
118 case CALLED_BACK:
yang-gb063c872015-10-07 11:40:13 -0700119 GPR_UNREACHABLE_CODE(return );
Craig Tiller48cb07c2015-07-15 16:16:15 -0700120 case CALLING_BACK:
121 w->phase = CALLED_BACK;
122 break;
123 case CALLING_BACK_AND_FINISHED:
124 delete = 1;
125 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700126 }
127 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700128
Craig Tillera82950e2015-09-22 12:33:20 -0700129 if (delete) {
130 delete_state_watcher(exec_ctx, w);
131 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700132}
133
Craig Tillera82950e2015-09-22 12:33:20 -0700134static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
135 int due_to_completion) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700136 int delete = 0;
137
Craig Tillera82950e2015-09-22 12:33:20 -0700138 if (due_to_completion) {
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700139 grpc_timer_cancel(exec_ctx, &w->alarm);
Craig Tillera82950e2015-09-22 12:33:20 -0700140 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700141
Craig Tillera82950e2015-09-22 12:33:20 -0700142 gpr_mu_lock(&w->mu);
Craig Tiller7b435612015-11-24 08:15:05 -0800143 if (due_to_completion) {
144 w->success = 1;
145 }
Craig Tillera82950e2015-09-22 12:33:20 -0700146 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700147 case WAITING:
148 w->phase = CALLING_BACK;
Craig Tillera82950e2015-09-22 12:33:20 -0700149 grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->success, finished_completion,
150 w, &w->completion_storage);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700151 break;
152 case CALLING_BACK:
153 w->phase = CALLING_BACK_AND_FINISHED;
154 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 case CALLED_BACK:
158 delete = 1;
159 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700160 }
161 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700162
Craig Tillera82950e2015-09-22 12:33:20 -0700163 if (delete) {
164 delete_state_watcher(exec_ctx, w);
165 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700166}
167
Craig Tiller6c396862016-01-28 13:53:40 -0800168static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw, bool success) {
Craig Tillera82950e2015-09-22 12:33:20 -0700169 partly_done(exec_ctx, pw, 1);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700170}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700171
Craig Tiller6c396862016-01-28 13:53:40 -0800172static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw, bool success) {
Craig Tillera82950e2015-09-22 12:33:20 -0700173 partly_done(exec_ctx, pw, 0);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700174}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700175
Craig Tillera82950e2015-09-22 12:33:20 -0700176void grpc_channel_watch_connectivity_state(
177 grpc_channel *channel, grpc_connectivity_state last_observed_state,
178 gpr_timespec deadline, grpc_completion_queue *cq, void *tag) {
179 grpc_channel_element *client_channel_elem =
180 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -0700181 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -0700182 state_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700183
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700184 GRPC_API_TRACE(
185 "grpc_channel_watch_connectivity_state("
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700186 "channel=%p, last_observed_state=%d, "
Jan Tattermusch88086372015-12-10 10:54:12 -0800187 "deadline=gpr_timespec { tv_sec: %lld, tv_nsec: %d, clock_type: %d }, "
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700188 "cq=%p, tag=%p)",
Jan Tattermusch88086372015-12-10 10:54:12 -0800189 7, (channel, (int)last_observed_state, (long long)deadline.tv_sec,
190 (int)deadline.tv_nsec, (int)deadline.clock_type, cq, tag));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700191
Craig Tiller4bf29282015-12-14 11:25:48 -0800192 grpc_cq_begin_op(cq, tag);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700193
Craig Tillera82950e2015-09-22 12:33:20 -0700194 gpr_mu_init(&w->mu);
195 grpc_closure_init(&w->on_complete, watch_complete, w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700196 w->phase = WAITING;
197 w->state = last_observed_state;
198 w->success = 0;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700199 w->cq = cq;
200 w->tag = tag;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700201 w->channel = channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700202
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700203 grpc_timer_init(&exec_ctx, &w->alarm,
Craig Tillera82950e2015-09-22 12:33:20 -0700204 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
205 timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700206
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700207 if (client_channel_elem->filter == &grpc_client_channel_filter) {
208 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_channel_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700209 grpc_client_channel_watch_connectivity_state(&exec_ctx, client_channel_elem,
Craig Tiller7b435612015-11-24 08:15:05 -0800210 grpc_cq_pollset(cq), &w->state,
211 &w->on_complete);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700212 } else if (client_channel_elem->filter == &grpc_client_uchannel_filter) {
213 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_uchannel_connectivity");
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700214 grpc_client_uchannel_watch_connectivity_state(
Craig Tiller7b435612015-11-24 08:15:05 -0800215 &exec_ctx, client_channel_elem, grpc_cq_pollset(cq), &w->state,
216 &w->on_complete);
Craig Tillera82950e2015-09-22 12:33:20 -0700217 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700218
Craig Tillera82950e2015-09-22 12:33:20 -0700219 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700220}