blob: a685a99eacd329164ee0d2393430cb6413d87039 [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"
40#include "src/core/iomgr/alarm.h"
41#include "src/core/surface/completion_queue.h"
42
Craig Tillera82950e2015-09-22 12:33:20 -070043grpc_connectivity_state grpc_channel_check_connectivity_state(
44 grpc_channel *channel, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070045 /* forward through to the underlying client channel */
Craig Tillera82950e2015-09-22 12:33:20 -070046 grpc_channel_element *client_channel_elem =
47 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -070048 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller000cd8f2015-09-18 07:20:29 -070049 grpc_connectivity_state state;
Craig Tillera82950e2015-09-22 12:33:20 -070050 if (client_channel_elem->filter != &grpc_client_channel_filter) {
51 gpr_log(GPR_ERROR,
52 "grpc_channel_check_connectivity_state called on something that is "
53 "not a client channel, but '%s'",
54 client_channel_elem->filter->name);
55 return GRPC_CHANNEL_FATAL_FAILURE;
56 }
57 state = grpc_client_channel_check_connectivity_state(
58 &exec_ctx, client_channel_elem, try_to_connect);
59 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller000cd8f2015-09-18 07:20:29 -070060 return state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070061}
62
Craig Tillera82950e2015-09-22 12:33:20 -070063typedef enum {
Craig Tiller48cb07c2015-07-15 16:16:15 -070064 WAITING,
65 CALLING_BACK,
66 CALLING_BACK_AND_FINISHED,
67 CALLED_BACK
68} callback_phase;
69
Craig Tillera82950e2015-09-22 12:33:20 -070070typedef struct {
Craig Tiller48cb07c2015-07-15 16:16:15 -070071 gpr_mu mu;
72 callback_phase phase;
73 int success;
yang-ga63fe4e2015-09-18 00:02:59 -070074 int removed;
Craig Tiller33825112015-09-18 07:44:19 -070075 grpc_closure on_complete;
Craig Tiller48cb07c2015-07-15 16:16:15 -070076 grpc_alarm alarm;
77 grpc_connectivity_state state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070078 grpc_completion_queue *cq;
79 grpc_cq_completion completion_storage;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070080 grpc_channel *channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -070081 void *tag;
82} state_watcher;
83
Craig Tillera82950e2015-09-22 12:33:20 -070084static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) {
85 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel, "watch_connectivity");
86 gpr_mu_destroy(&w->mu);
87 gpr_free(w);
Craig Tiller48cb07c2015-07-15 16:16:15 -070088}
89
Craig Tillera82950e2015-09-22 12:33:20 -070090static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw,
91 grpc_cq_completion *ignored) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070092 int delete = 0;
93 state_watcher *w = pw;
Craig Tillera82950e2015-09-22 12:33:20 -070094 gpr_mu_lock(&w->mu);
95 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070096 case WAITING:
97 case CALLED_BACK:
Craig Tillera82950e2015-09-22 12:33:20 -070098 gpr_log(GPR_ERROR, "should never reach here");
99 abort();
Craig Tiller48cb07c2015-07-15 16:16:15 -0700100 break;
101 case CALLING_BACK:
102 w->phase = CALLED_BACK;
103 break;
104 case CALLING_BACK_AND_FINISHED:
105 delete = 1;
106 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700107 }
108 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700109
Craig Tillera82950e2015-09-22 12:33:20 -0700110 if (delete) {
111 delete_state_watcher(exec_ctx, w);
112 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700113}
114
Craig Tillera82950e2015-09-22 12:33:20 -0700115static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
116 int due_to_completion) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700117 int delete = 0;
yang-ga63fe4e2015-09-18 00:02:59 -0700118 grpc_channel_element *client_channel_elem = NULL;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700119
Craig Tillera82950e2015-09-22 12:33:20 -0700120 gpr_mu_lock(&w->mu);
121 if (w->removed == 0) {
122 w->removed = 1;
123 client_channel_elem = grpc_channel_stack_last_element(
124 grpc_channel_get_channel_stack(w->channel));
125 grpc_client_channel_del_interested_party(exec_ctx, client_channel_elem,
126 grpc_cq_pollset(w->cq));
127 }
128 gpr_mu_unlock(&w->mu);
129 if (due_to_completion) {
130 gpr_mu_lock(&w->mu);
131 w->success = 1;
132 gpr_mu_unlock(&w->mu);
133 grpc_alarm_cancel(exec_ctx, &w->alarm);
134 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700135
Craig Tillera82950e2015-09-22 12:33:20 -0700136 gpr_mu_lock(&w->mu);
137 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700138 case WAITING:
139 w->phase = CALLING_BACK;
Craig Tillera82950e2015-09-22 12:33:20 -0700140 grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->success, finished_completion,
141 w, &w->completion_storage);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700142 break;
143 case CALLING_BACK:
144 w->phase = CALLING_BACK_AND_FINISHED;
145 break;
146 case CALLING_BACK_AND_FINISHED:
Craig Tillera82950e2015-09-22 12:33:20 -0700147 gpr_log(GPR_ERROR, "should never reach here");
148 abort();
Craig Tiller48cb07c2015-07-15 16:16:15 -0700149 break;
150 case CALLED_BACK:
151 delete = 1;
152 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700153 }
154 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700155
Craig Tillera82950e2015-09-22 12:33:20 -0700156 if (delete) {
157 delete_state_watcher(exec_ctx, w);
158 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700159}
160
Craig Tillera82950e2015-09-22 12:33:20 -0700161static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
162 partly_done(exec_ctx, pw, 1);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700163}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700164
Craig Tillera82950e2015-09-22 12:33:20 -0700165static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
166 partly_done(exec_ctx, pw, 0);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700167}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700168
Craig Tillera82950e2015-09-22 12:33:20 -0700169void grpc_channel_watch_connectivity_state(
170 grpc_channel *channel, grpc_connectivity_state last_observed_state,
171 gpr_timespec deadline, grpc_completion_queue *cq, void *tag) {
172 grpc_channel_element *client_channel_elem =
173 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -0700174 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -0700175 state_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700176
Craig Tillera82950e2015-09-22 12:33:20 -0700177 grpc_cq_begin_op(cq);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700178
Craig Tillera82950e2015-09-22 12:33:20 -0700179 gpr_mu_init(&w->mu);
180 grpc_closure_init(&w->on_complete, watch_complete, w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700181 w->phase = WAITING;
182 w->state = last_observed_state;
183 w->success = 0;
yang-ga63fe4e2015-09-18 00:02:59 -0700184 w->removed = 0;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700185 w->cq = cq;
186 w->tag = tag;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700187 w->channel = channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700188
Craig Tillera82950e2015-09-22 12:33:20 -0700189 grpc_alarm_init(&exec_ctx, &w->alarm,
190 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
191 timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700192
Craig Tillera82950e2015-09-22 12:33:20 -0700193 if (client_channel_elem->filter != &grpc_client_channel_filter) {
194 gpr_log(GPR_ERROR,
195 "grpc_channel_watch_connectivity_state called on something that is "
196 "not a client channel, but '%s'",
197 client_channel_elem->filter->name);
198 grpc_exec_ctx_enqueue(&exec_ctx, &w->on_complete, 1);
199 } else {
200 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_connectivity");
201 grpc_client_channel_add_interested_party(&exec_ctx, client_channel_elem,
202 grpc_cq_pollset(cq));
203 grpc_client_channel_watch_connectivity_state(&exec_ctx, client_channel_elem,
204 &w->state, &w->on_complete);
205 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700206
Craig Tillera82950e2015-09-22 12:33:20 -0700207 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700208}