blob: 47cbab154f8a3b3db038703edb015aa25af7bc08 [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);
Craig Tiller63010382015-09-24 15:00:58 -070055 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -070056 return GRPC_CHANNEL_FATAL_FAILURE;
57 }
58 state = grpc_client_channel_check_connectivity_state(
59 &exec_ctx, client_channel_elem, try_to_connect);
60 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller000cd8f2015-09-18 07:20:29 -070061 return state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070062}
63
Craig Tillera82950e2015-09-22 12:33:20 -070064typedef enum {
Craig Tiller48cb07c2015-07-15 16:16:15 -070065 WAITING,
66 CALLING_BACK,
67 CALLING_BACK_AND_FINISHED,
68 CALLED_BACK
69} callback_phase;
70
Craig Tillera82950e2015-09-22 12:33:20 -070071typedef struct {
Craig Tiller48cb07c2015-07-15 16:16:15 -070072 gpr_mu mu;
73 callback_phase phase;
74 int success;
yang-ga63fe4e2015-09-18 00:02:59 -070075 int removed;
Craig Tiller33825112015-09-18 07:44:19 -070076 grpc_closure on_complete;
Craig Tiller48cb07c2015-07-15 16:16:15 -070077 grpc_alarm alarm;
78 grpc_connectivity_state state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070079 grpc_completion_queue *cq;
80 grpc_cq_completion completion_storage;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070081 grpc_channel *channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -070082 void *tag;
83} state_watcher;
84
Craig Tillera82950e2015-09-22 12:33:20 -070085static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) {
86 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel, "watch_connectivity");
87 gpr_mu_destroy(&w->mu);
88 gpr_free(w);
Craig Tiller48cb07c2015-07-15 16:16:15 -070089}
90
Craig Tillera82950e2015-09-22 12:33:20 -070091static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw,
92 grpc_cq_completion *ignored) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070093 int delete = 0;
94 state_watcher *w = pw;
Craig Tillera82950e2015-09-22 12:33:20 -070095 gpr_mu_lock(&w->mu);
96 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070097 case WAITING:
98 case CALLED_BACK:
Craig Tillera82950e2015-09-22 12:33:20 -070099 gpr_log(GPR_ERROR, "should never reach here");
100 abort();
Craig Tiller48cb07c2015-07-15 16:16:15 -0700101 break;
102 case CALLING_BACK:
103 w->phase = CALLED_BACK;
104 break;
105 case CALLING_BACK_AND_FINISHED:
106 delete = 1;
107 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700108 }
109 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700110
Craig Tillera82950e2015-09-22 12:33:20 -0700111 if (delete) {
112 delete_state_watcher(exec_ctx, w);
113 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700114}
115
Craig Tillera82950e2015-09-22 12:33:20 -0700116static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
117 int due_to_completion) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700118 int delete = 0;
yang-ga63fe4e2015-09-18 00:02:59 -0700119 grpc_channel_element *client_channel_elem = NULL;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700120
Craig Tillera82950e2015-09-22 12:33:20 -0700121 gpr_mu_lock(&w->mu);
122 if (w->removed == 0) {
123 w->removed = 1;
124 client_channel_elem = grpc_channel_stack_last_element(
125 grpc_channel_get_channel_stack(w->channel));
126 grpc_client_channel_del_interested_party(exec_ctx, client_channel_elem,
127 grpc_cq_pollset(w->cq));
128 }
129 gpr_mu_unlock(&w->mu);
130 if (due_to_completion) {
131 gpr_mu_lock(&w->mu);
132 w->success = 1;
133 gpr_mu_unlock(&w->mu);
134 grpc_alarm_cancel(exec_ctx, &w->alarm);
135 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700136
Craig Tillera82950e2015-09-22 12:33:20 -0700137 gpr_mu_lock(&w->mu);
138 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700139 case WAITING:
140 w->phase = CALLING_BACK;
Craig Tillera82950e2015-09-22 12:33:20 -0700141 grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->success, finished_completion,
142 w, &w->completion_storage);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700143 break;
144 case CALLING_BACK:
145 w->phase = CALLING_BACK_AND_FINISHED;
146 break;
147 case CALLING_BACK_AND_FINISHED:
Craig Tillera82950e2015-09-22 12:33:20 -0700148 gpr_log(GPR_ERROR, "should never reach here");
149 abort();
Craig Tiller48cb07c2015-07-15 16:16:15 -0700150 break;
151 case CALLED_BACK:
152 delete = 1;
153 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700154 }
155 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700156
Craig Tillera82950e2015-09-22 12:33:20 -0700157 if (delete) {
158 delete_state_watcher(exec_ctx, w);
159 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700160}
161
Craig Tillera82950e2015-09-22 12:33:20 -0700162static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
163 partly_done(exec_ctx, pw, 1);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700164}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700165
Craig Tillera82950e2015-09-22 12:33:20 -0700166static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
167 partly_done(exec_ctx, pw, 0);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700168}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700169
Craig Tillera82950e2015-09-22 12:33:20 -0700170void grpc_channel_watch_connectivity_state(
171 grpc_channel *channel, grpc_connectivity_state last_observed_state,
172 gpr_timespec deadline, grpc_completion_queue *cq, void *tag) {
173 grpc_channel_element *client_channel_elem =
174 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -0700175 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -0700176 state_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700177
Craig Tillera82950e2015-09-22 12:33:20 -0700178 grpc_cq_begin_op(cq);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700179
Craig Tillera82950e2015-09-22 12:33:20 -0700180 gpr_mu_init(&w->mu);
181 grpc_closure_init(&w->on_complete, watch_complete, w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700182 w->phase = WAITING;
183 w->state = last_observed_state;
184 w->success = 0;
yang-ga63fe4e2015-09-18 00:02:59 -0700185 w->removed = 0;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700186 w->cq = cq;
187 w->tag = tag;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700188 w->channel = channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700189
Craig Tillera82950e2015-09-22 12:33:20 -0700190 grpc_alarm_init(&exec_ctx, &w->alarm,
191 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
192 timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700193
Craig Tillera82950e2015-09-22 12:33:20 -0700194 if (client_channel_elem->filter != &grpc_client_channel_filter) {
195 gpr_log(GPR_ERROR,
196 "grpc_channel_watch_connectivity_state called on something that is "
197 "not a client channel, but '%s'",
198 client_channel_elem->filter->name);
199 grpc_exec_ctx_enqueue(&exec_ctx, &w->on_complete, 1);
200 } else {
201 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_connectivity");
202 grpc_client_channel_add_interested_party(&exec_ctx, client_channel_elem,
203 grpc_cq_pollset(cq));
204 grpc_client_channel_watch_connectivity_state(&exec_ctx, client_channel_elem,
205 &w->state, &w->on_complete);
206 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700207
Craig Tillera82950e2015-09-22 12:33:20 -0700208 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700209}