blob: bef3d387fc9ec6301bc4f81bb55950cf217bdba7 [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"
Masood Malekghassemi76c3d742015-08-19 18:22:53 -070041#include "src/core/surface/api_trace.h"
Craig Tiller48cb07c2015-07-15 16:16:15 -070042#include "src/core/surface/completion_queue.h"
43
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(
52 "grpc_channel_check_connectivity_state(channel=%p, try_to_connect=%d)",
53 2, (channel, try_to_connect));
Craig Tillera82950e2015-09-22 12:33:20 -070054 if (client_channel_elem->filter != &grpc_client_channel_filter) {
55 gpr_log(GPR_ERROR,
56 "grpc_channel_check_connectivity_state called on something that is "
57 "not a client channel, but '%s'",
58 client_channel_elem->filter->name);
Craig Tiller63010382015-09-24 15:00:58 -070059 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -070060 return GRPC_CHANNEL_FATAL_FAILURE;
61 }
62 state = grpc_client_channel_check_connectivity_state(
63 &exec_ctx, client_channel_elem, try_to_connect);
64 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller000cd8f2015-09-18 07:20:29 -070065 return state;
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;
78 int success;
yang-ga63fe4e2015-09-18 00:02:59 -070079 int removed;
Craig Tiller33825112015-09-18 07:44:19 -070080 grpc_closure on_complete;
Craig Tiller48cb07c2015-07-15 16:16:15 -070081 grpc_alarm alarm;
82 grpc_connectivity_state state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070083 grpc_completion_queue *cq;
84 grpc_cq_completion completion_storage;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070085 grpc_channel *channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -070086 void *tag;
87} state_watcher;
88
Craig Tillera82950e2015-09-22 12:33:20 -070089static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) {
90 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel, "watch_connectivity");
91 gpr_mu_destroy(&w->mu);
92 gpr_free(w);
Craig Tiller48cb07c2015-07-15 16:16:15 -070093}
94
Craig Tillera82950e2015-09-22 12:33:20 -070095static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw,
96 grpc_cq_completion *ignored) {
Craig Tiller48cb07c2015-07-15 16:16:15 -070097 int delete = 0;
98 state_watcher *w = pw;
Craig Tillera82950e2015-09-22 12:33:20 -070099 gpr_mu_lock(&w->mu);
100 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700101 case WAITING:
102 case CALLED_BACK:
Craig Tillera82950e2015-09-22 12:33:20 -0700103 gpr_log(GPR_ERROR, "should never reach here");
104 abort();
Craig Tiller48cb07c2015-07-15 16:16:15 -0700105 break;
106 case CALLING_BACK:
107 w->phase = CALLED_BACK;
108 break;
109 case CALLING_BACK_AND_FINISHED:
110 delete = 1;
111 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700112 }
113 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700114
Craig Tillera82950e2015-09-22 12:33:20 -0700115 if (delete) {
116 delete_state_watcher(exec_ctx, w);
117 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700118}
119
Craig Tillera82950e2015-09-22 12:33:20 -0700120static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
121 int due_to_completion) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700122 int delete = 0;
yang-ga63fe4e2015-09-18 00:02:59 -0700123 grpc_channel_element *client_channel_elem = NULL;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700124
Craig Tillera82950e2015-09-22 12:33:20 -0700125 gpr_mu_lock(&w->mu);
126 if (w->removed == 0) {
127 w->removed = 1;
128 client_channel_elem = grpc_channel_stack_last_element(
129 grpc_channel_get_channel_stack(w->channel));
130 grpc_client_channel_del_interested_party(exec_ctx, client_channel_elem,
131 grpc_cq_pollset(w->cq));
132 }
133 gpr_mu_unlock(&w->mu);
134 if (due_to_completion) {
135 gpr_mu_lock(&w->mu);
136 w->success = 1;
137 gpr_mu_unlock(&w->mu);
138 grpc_alarm_cancel(exec_ctx, &w->alarm);
139 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700140
Craig Tillera82950e2015-09-22 12:33:20 -0700141 gpr_mu_lock(&w->mu);
142 switch (w->phase) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700143 case WAITING:
144 w->phase = CALLING_BACK;
Craig Tillera82950e2015-09-22 12:33:20 -0700145 grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->success, finished_completion,
146 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:
Craig Tillera82950e2015-09-22 12:33:20 -0700152 gpr_log(GPR_ERROR, "should never reach here");
153 abort();
Craig Tiller48cb07c2015-07-15 16:16:15 -0700154 break;
155 case CALLED_BACK:
156 delete = 1;
157 break;
Craig Tillera82950e2015-09-22 12:33:20 -0700158 }
159 gpr_mu_unlock(&w->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700160
Craig Tillera82950e2015-09-22 12:33:20 -0700161 if (delete) {
162 delete_state_watcher(exec_ctx, w);
163 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700164}
165
Craig Tillera82950e2015-09-22 12:33:20 -0700166static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
167 partly_done(exec_ctx, pw, 1);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700168}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700169
Craig Tillera82950e2015-09-22 12:33:20 -0700170static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
171 partly_done(exec_ctx, pw, 0);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700172}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700173
Craig Tillera82950e2015-09-22 12:33:20 -0700174void grpc_channel_watch_connectivity_state(
175 grpc_channel *channel, grpc_connectivity_state last_observed_state,
176 gpr_timespec deadline, grpc_completion_queue *cq, void *tag) {
177 grpc_channel_element *client_channel_elem =
178 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tillerf5768a62015-09-22 10:54:34 -0700179 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -0700180 state_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700181
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700182 GRPC_API_TRACE(
183 "grpc_channel_watch_connectivity_state("
184 "channel=%p, last_observed_state=%d, "
185 "deadline=gpr_timespec { tv_sec: %ld, tv_nsec: %d, clock_type: %d }, "
186 "cq=%p, tag=%p)",
187 7, (channel, (int)last_observed_state, (long)deadline.tv_sec,
188 deadline.tv_nsec, (int)deadline.clock_type, cq, tag));
189
Craig Tillera82950e2015-09-22 12:33:20 -0700190 grpc_cq_begin_op(cq);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700191
Craig Tillera82950e2015-09-22 12:33:20 -0700192 gpr_mu_init(&w->mu);
193 grpc_closure_init(&w->on_complete, watch_complete, w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700194 w->phase = WAITING;
195 w->state = last_observed_state;
196 w->success = 0;
yang-ga63fe4e2015-09-18 00:02:59 -0700197 w->removed = 0;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700198 w->cq = cq;
199 w->tag = tag;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700200 w->channel = channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700201
Craig Tillera82950e2015-09-22 12:33:20 -0700202 grpc_alarm_init(&exec_ctx, &w->alarm,
203 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
204 timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700205
Craig Tillera82950e2015-09-22 12:33:20 -0700206 if (client_channel_elem->filter != &grpc_client_channel_filter) {
207 gpr_log(GPR_ERROR,
208 "grpc_channel_watch_connectivity_state called on something that is "
209 "not a client channel, but '%s'",
210 client_channel_elem->filter->name);
211 grpc_exec_ctx_enqueue(&exec_ctx, &w->on_complete, 1);
212 } else {
213 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_connectivity");
214 grpc_client_channel_add_interested_party(&exec_ctx, client_channel_elem,
215 grpc_cq_pollset(cq));
216 grpc_client_channel_watch_connectivity_state(&exec_ctx, client_channel_elem,
217 &w->state, &w->on_complete);
218 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700219
Craig Tillera82950e2015-09-22 12:33:20 -0700220 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700221}