blob: 4d0cf1ed8bffdaa3cb9e4f54fa4ec30551b6dcb6 [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
43grpc_connectivity_state grpc_channel_check_connectivity_state(
44 grpc_channel *channel, int try_to_connect) {
45 /* forward through to the underlying client channel */
46 grpc_channel_element *client_channel_elem =
47 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tiller33825112015-09-18 07:44:19 -070048 grpc_call_list call_list = GRPC_CALL_LIST_INIT;
Craig Tiller000cd8f2015-09-18 07:20:29 -070049 grpc_connectivity_state state;
Craig Tiller48cb07c2015-07-15 16:16:15 -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 }
Craig Tiller000cd8f2015-09-18 07:20:29 -070057 state = grpc_client_channel_check_connectivity_state(
58 client_channel_elem, try_to_connect, &call_list);
Craig Tiller33825112015-09-18 07:44:19 -070059 grpc_call_list_run(call_list);
Craig Tiller000cd8f2015-09-18 07:20:29 -070060 return state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070061}
62
63typedef enum {
64 WAITING,
65 CALLING_BACK,
66 CALLING_BACK_AND_FINISHED,
67 CALLED_BACK
68} callback_phase;
69
70typedef struct {
71 gpr_mu mu;
72 callback_phase phase;
73 int success;
Craig Tiller33825112015-09-18 07:44:19 -070074 grpc_closure on_complete;
Craig Tiller48cb07c2015-07-15 16:16:15 -070075 grpc_alarm alarm;
76 grpc_connectivity_state state;
Craig Tiller48cb07c2015-07-15 16:16:15 -070077 grpc_completion_queue *cq;
78 grpc_cq_completion completion_storage;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070079 grpc_channel *channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -070080 void *tag;
81} state_watcher;
82
83static void delete_state_watcher(state_watcher *w) {
Craig Tillerd6c98df2015-08-18 09:33:44 -070084 grpc_channel_element *client_channel_elem = grpc_channel_stack_last_element(
85 grpc_channel_get_channel_stack(w->channel));
86 grpc_client_channel_del_interested_party(client_channel_elem,
87 grpc_cq_pollset(w->cq));
Craig Tiller1ada6ad2015-07-16 16:19:14 -070088 GRPC_CHANNEL_INTERNAL_UNREF(w->channel, "watch_connectivity");
Craig Tiller48cb07c2015-07-15 16:16:15 -070089 gpr_mu_destroy(&w->mu);
90 gpr_free(w);
91}
92
93static void finished_completion(void *pw, grpc_cq_completion *ignored) {
94 int delete = 0;
95 state_watcher *w = pw;
96 gpr_mu_lock(&w->mu);
97 switch (w->phase) {
98 case WAITING:
99 case CALLED_BACK:
100 gpr_log(GPR_ERROR, "should never reach here");
101 abort();
102 break;
103 case CALLING_BACK:
104 w->phase = CALLED_BACK;
105 break;
106 case CALLING_BACK_AND_FINISHED:
107 delete = 1;
108 break;
109 }
110 gpr_mu_unlock(&w->mu);
111
112 if (delete) {
113 delete_state_watcher(w);
114 }
115}
116
117static void partly_done(state_watcher *w, int due_to_completion) {
118 int delete = 0;
119
120 if (due_to_completion) {
121 gpr_mu_lock(&w->mu);
122 w->success = 1;
123 gpr_mu_unlock(&w->mu);
124 grpc_alarm_cancel(&w->alarm);
125 }
126
127 gpr_mu_lock(&w->mu);
128 switch (w->phase) {
129 case WAITING:
130 w->phase = CALLING_BACK;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700131 grpc_cq_end_op(w->cq, w->tag, w->success, finished_completion, w,
132 &w->completion_storage);
133 break;
134 case CALLING_BACK:
135 w->phase = CALLING_BACK_AND_FINISHED;
136 break;
137 case CALLING_BACK_AND_FINISHED:
138 gpr_log(GPR_ERROR, "should never reach here");
139 abort();
140 break;
141 case CALLED_BACK:
142 delete = 1;
143 break;
144 }
145 gpr_mu_unlock(&w->mu);
146
147 if (delete) {
148 delete_state_watcher(w);
149 }
150}
151
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700152static void watch_complete(void *pw, int success) { partly_done(pw, 1); }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700153
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700154static void timeout_complete(void *pw, int success) { partly_done(pw, 0); }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700155
156void grpc_channel_watch_connectivity_state(
157 grpc_channel *channel, grpc_connectivity_state last_observed_state,
Craig Tiller2cd9dd92015-07-31 16:34:37 -0700158 gpr_timespec deadline, grpc_completion_queue *cq, void *tag) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700159 grpc_channel_element *client_channel_elem =
160 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
Craig Tiller33825112015-09-18 07:44:19 -0700161 grpc_call_list call_list = GRPC_CALL_LIST_INIT;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700162 state_watcher *w = gpr_malloc(sizeof(*w));
163
164 grpc_cq_begin_op(cq);
165
166 gpr_mu_init(&w->mu);
Craig Tiller33825112015-09-18 07:44:19 -0700167 grpc_closure_init(&w->on_complete, watch_complete, w);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700168 w->phase = WAITING;
169 w->state = last_observed_state;
170 w->success = 0;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700171 w->cq = cq;
172 w->tag = tag;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700173 w->channel = channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700174
Craig Tillerd6c98df2015-08-18 09:33:44 -0700175 grpc_alarm_init(&w->alarm,
176 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
177 timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700178
179 if (client_channel_elem->filter != &grpc_client_channel_filter) {
180 gpr_log(GPR_ERROR,
181 "grpc_channel_watch_connectivity_state called on something that is "
182 "not a client channel, but '%s'",
183 client_channel_elem->filter->name);
Craig Tiller33825112015-09-18 07:44:19 -0700184 grpc_call_list_add(&call_list, &w->on_complete, 1);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700185 } else {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700186 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_connectivity");
Craig Tillerd6c98df2015-08-18 09:33:44 -0700187 grpc_client_channel_add_interested_party(client_channel_elem,
188 grpc_cq_pollset(cq));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700189 grpc_client_channel_watch_connectivity_state(client_channel_elem, &w->state,
Craig Tiller000cd8f2015-09-18 07:20:29 -0700190 &w->on_complete, &call_list);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700191 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700192
Craig Tiller33825112015-09-18 07:44:19 -0700193 grpc_call_list_run(call_list);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700194}