blob: bef43d84ca0faf2ee7351379812703961ad816be [file] [log] [blame]
Craig Tiller48cb07c2015-07-15 16:16:15 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller48cb07c2015-07-15 16:16:15 -07004 * 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
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/surface/channel.h"
Craig Tiller48cb07c2015-07-15 16:16:15 -070035
36#include <grpc/support/alloc.h>
37#include <grpc/support/log.h>
38
Craig Tillerd4c98332016-03-31 13:45:47 -070039#include "src/core/ext/client_config/client_channel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/iomgr/timer.h"
41#include "src/core/lib/surface/api_trace.h"
42#include "src/core/lib/surface/completion_queue.h"
Craig Tiller48cb07c2015-07-15 16:16:15 -070043
Craig Tillera82950e2015-09-22 12:33:20 -070044grpc_connectivity_state grpc_channel_check_connectivity_state(
45 grpc_channel *channel, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070046 /* forward through to the underlying client channel */
Craig Tillera82950e2015-09-22 12:33:20 -070047 grpc_channel_element *client_channel_elem =
48 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -070049 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller000cd8f2015-09-18 07:20:29 -070050 grpc_connectivity_state state;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -070051 GRPC_API_TRACE(
Craig Tiller4de3e4f2015-10-05 08:55:50 -070052 "grpc_channel_check_connectivity_state(channel=%p, try_to_connect=%d)", 2,
53 (channel, try_to_connect));
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070054 if (client_channel_elem->filter == &grpc_client_channel_filter) {
55 state = grpc_client_channel_check_connectivity_state(
56 &exec_ctx, client_channel_elem, try_to_connect);
Craig Tiller63010382015-09-24 15:00:58 -070057 grpc_exec_ctx_finish(&exec_ctx);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070058 return state;
Craig Tillera82950e2015-09-22 12:33:20 -070059 }
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070060 gpr_log(GPR_ERROR,
61 "grpc_channel_check_connectivity_state called on something that is "
62 "not a (u)client channel, but '%s'",
63 client_channel_elem->filter->name);
Craig Tillera82950e2015-09-22 12:33:20 -070064 grpc_exec_ctx_finish(&exec_ctx);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070065 return GRPC_CHANNEL_FATAL_FAILURE;
Craig Tiller48cb07c2015-07-15 16:16:15 -070066}
67
Craig Tillera82950e2015-09-22 12:33:20 -070068typedef enum {
Craig Tiller48cb07c2015-07-15 16:16:15 -070069 WAITING,
70 CALLING_BACK,
71 CALLING_BACK_AND_FINISHED,
72 CALLED_BACK
73} callback_phase;
74
Craig Tillera82950e2015-09-22 12:33:20 -070075typedef struct {
Craig Tiller48cb07c2015-07-15 16:16:15 -070076 gpr_mu mu;
77 callback_phase phase;
Craig Tiller804ff712016-05-05 16:25:40 -070078 grpc_error *error;
Craig Tiller33825112015-09-18 07:44:19 -070079 grpc_closure on_complete;
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070080 grpc_timer alarm;
Craig Tiller48cb07c2015-07-15 16:16:15 -070081 grpc_connectivity_state state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070082 grpc_completion_queue *cq;
83 grpc_cq_completion completion_storage;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070084 grpc_channel *channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -070085 void *tag;
86} state_watcher;
87
Craig Tillera82950e2015-09-22 12:33:20 -070088static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) {
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070089 grpc_channel_element *client_channel_elem = grpc_channel_stack_last_element(
90 grpc_channel_get_channel_stack(w->channel));
91 if (client_channel_elem->filter == &grpc_client_channel_filter) {
92 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel,
93 "watch_channel_connectivity");
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070094 } else {
95 abort();
96 }
Craig Tillera82950e2015-09-22 12:33:20 -070097 gpr_mu_destroy(&w->mu);
Craig Tillerd7faf552016-05-08 09:55:28 -070098 GRPC_ERROR_UNREF(w->error);
Craig Tillera82950e2015-09-22 12:33:20 -070099 gpr_free(w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700100}
101
Craig Tillera82950e2015-09-22 12:33:20 -0700102static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw,
103 grpc_cq_completion *ignored) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700104 int delete = 0;
105 state_watcher *w = pw;
Craig Tillera82950e2015-09-22 12:33:20 -0700106 gpr_mu_lock(&w->mu);
107 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700108 case WAITING:
109 case CALLED_BACK:
yang-gb063c872015-10-07 11:40:13 -0700110 GPR_UNREACHABLE_CODE(return );
Craig Tiller48cb07c2015-07-15 16:16:15 -0700111 case CALLING_BACK:
112 w->phase = CALLED_BACK;
113 break;
114 case CALLING_BACK_AND_FINISHED:
115 delete = 1;
116 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700117 }
118 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700119
Craig Tillera82950e2015-09-22 12:33:20 -0700120 if (delete) {
121 delete_state_watcher(exec_ctx, w);
122 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700123}
124
Craig Tillera82950e2015-09-22 12:33:20 -0700125static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
Craig Tiller804ff712016-05-05 16:25:40 -0700126 bool due_to_completion, grpc_error *error) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700127 int delete = 0;
128
Craig Tillera82950e2015-09-22 12:33:20 -0700129 if (due_to_completion) {
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700130 grpc_timer_cancel(exec_ctx, &w->alarm);
Craig Tillera82950e2015-09-22 12:33:20 -0700131 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700132
Craig Tillera82950e2015-09-22 12:33:20 -0700133 gpr_mu_lock(&w->mu);
Craig Tillercfc8ae12016-05-10 20:49:54 -0700134 const char *msg = grpc_error_string(error);
135 gpr_log(GPR_DEBUG, "partly_done: d2c=%d phs=%d err=%s", due_to_completion, w->phase, msg);
136 grpc_error_free_string(msg);
137
Craig Tiller7b435612015-11-24 08:15:05 -0800138 if (due_to_completion) {
Craig Tillerf707d622016-05-06 14:26:12 -0700139 GRPC_ERROR_UNREF(w->error);
Craig Tiller804ff712016-05-05 16:25:40 -0700140 w->error = GRPC_ERROR_NONE;
Craig Tiller7b435612015-11-24 08:15:05 -0800141 }
Craig Tillera82950e2015-09-22 12:33:20 -0700142 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700143 case WAITING:
144 w->phase = CALLING_BACK;
Craig Tillerf707d622016-05-06 14:26:12 -0700145 grpc_cq_end_op(exec_ctx, w->cq, w->tag, GRPC_ERROR_REF(w->error),
Craig Tiller804ff712016-05-05 16:25:40 -0700146 finished_completion, w, &w->completion_storage);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700147 break;
148 case CALLING_BACK:
149 w->phase = CALLING_BACK_AND_FINISHED;
150 break;
151 case CALLING_BACK_AND_FINISHED:
yang-gb063c872015-10-07 11:40:13 -0700152 GPR_UNREACHABLE_CODE(return );
Craig Tiller48cb07c2015-07-15 16:16:15 -0700153 case CALLED_BACK:
154 delete = 1;
155 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700156 }
157 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700158
Craig Tillera82950e2015-09-22 12:33:20 -0700159 if (delete) {
160 delete_state_watcher(exec_ctx, w);
161 }
Craig Tillercfc8ae12016-05-10 20:49:54 -0700162
163 GRPC_ERROR_UNREF(error);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700164}
165
Craig Tiller804ff712016-05-05 16:25:40 -0700166static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw,
167 grpc_error *error) {
Craig Tillercfc8ae12016-05-10 20:49:54 -0700168 partly_done(exec_ctx, pw, true, GRPC_ERROR_REF(error));
Craig Tillerdfff1b82015-09-21 14:39:57 -0700169}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700170
Craig Tiller804ff712016-05-05 16:25:40 -0700171static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw,
172 grpc_error *error) {
Craig Tillercfc8ae12016-05-10 20:49:54 -0700173 partly_done(exec_ctx, pw, false, GRPC_ERROR_REF(error));
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;
Craig Tillercfc8ae12016-05-10 20:49:54 -0700198 w->error = GRPC_ERROR_CREATE("Timeout waiting for channel state");
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 Quintasfb349b92016-03-21 15:37:20 -0700212 } else {
213 abort();
Craig Tillera82950e2015-09-22 12:33:20 -0700214 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700215
Craig Tillera82950e2015-09-22 12:33:20 -0700216 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700217}