Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/surface/channel.h" |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 35 | |
| 36 | #include <grpc/support/alloc.h> |
| 37 | #include <grpc/support/log.h> |
| 38 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 39 | #include "src/core/ext/client_config/client_channel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 40 | #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 Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 43 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 44 | grpc_connectivity_state grpc_channel_check_connectivity_state( |
| 45 | grpc_channel *channel, int try_to_connect) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 46 | /* forward through to the underlying client channel */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 47 | grpc_channel_element *client_channel_elem = |
| 48 | grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel)); |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 49 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 50 | grpc_connectivity_state state; |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 51 | GRPC_API_TRACE( |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 52 | "grpc_channel_check_connectivity_state(channel=%p, try_to_connect=%d)", 2, |
| 53 | (channel, try_to_connect)); |
David Garcia Quintas | 7b1bd2c | 2015-10-05 18:22:10 -0700 | [diff] [blame] | 54 | 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 Tiller | 6301038 | 2015-09-24 15:00:58 -0700 | [diff] [blame] | 57 | grpc_exec_ctx_finish(&exec_ctx); |
David Garcia Quintas | 7b1bd2c | 2015-10-05 18:22:10 -0700 | [diff] [blame] | 58 | return state; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 59 | } |
David Garcia Quintas | 7b1bd2c | 2015-10-05 18:22:10 -0700 | [diff] [blame] | 60 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 64 | grpc_exec_ctx_finish(&exec_ctx); |
David Garcia Quintas | 7b1bd2c | 2015-10-05 18:22:10 -0700 | [diff] [blame] | 65 | return GRPC_CHANNEL_FATAL_FAILURE; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | typedef enum { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 69 | WAITING, |
| 70 | CALLING_BACK, |
| 71 | CALLING_BACK_AND_FINISHED, |
| 72 | CALLED_BACK |
| 73 | } callback_phase; |
| 74 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | typedef struct { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 76 | gpr_mu mu; |
| 77 | callback_phase phase; |
| 78 | int success; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 79 | grpc_closure on_complete; |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 80 | grpc_timer alarm; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 81 | grpc_connectivity_state state; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 82 | grpc_completion_queue *cq; |
| 83 | grpc_cq_completion completion_storage; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 84 | grpc_channel *channel; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 85 | void *tag; |
| 86 | } state_watcher; |
| 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) { |
David Garcia Quintas | 7b1bd2c | 2015-10-05 18:22:10 -0700 | [diff] [blame] | 89 | 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 Quintas | 7b1bd2c | 2015-10-05 18:22:10 -0700 | [diff] [blame] | 94 | } else { |
| 95 | abort(); |
| 96 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | gpr_mu_destroy(&w->mu); |
| 98 | gpr_free(w); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw, |
| 102 | grpc_cq_completion *ignored) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 103 | int delete = 0; |
| 104 | state_watcher *w = pw; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | gpr_mu_lock(&w->mu); |
| 106 | switch (w->phase) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 107 | case WAITING: |
| 108 | case CALLED_BACK: |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 109 | GPR_UNREACHABLE_CODE(return ); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 110 | case CALLING_BACK: |
| 111 | w->phase = CALLED_BACK; |
| 112 | break; |
| 113 | case CALLING_BACK_AND_FINISHED: |
| 114 | delete = 1; |
| 115 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | } |
| 117 | gpr_mu_unlock(&w->mu); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 118 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 119 | if (delete) { |
| 120 | delete_state_watcher(exec_ctx, w); |
| 121 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w, |
| 125 | int due_to_completion) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 126 | int delete = 0; |
| 127 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | if (due_to_completion) { |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 129 | grpc_timer_cancel(exec_ctx, &w->alarm); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 131 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | gpr_mu_lock(&w->mu); |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 133 | if (due_to_completion) { |
| 134 | w->success = 1; |
| 135 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | switch (w->phase) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 137 | case WAITING: |
| 138 | w->phase = CALLING_BACK; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->success, finished_completion, |
| 140 | w, &w->completion_storage); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 141 | break; |
| 142 | case CALLING_BACK: |
| 143 | w->phase = CALLING_BACK_AND_FINISHED; |
| 144 | break; |
| 145 | case CALLING_BACK_AND_FINISHED: |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 146 | GPR_UNREACHABLE_CODE(return ); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 147 | case CALLED_BACK: |
| 148 | delete = 1; |
| 149 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | } |
| 151 | gpr_mu_unlock(&w->mu); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 152 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | if (delete) { |
| 154 | delete_state_watcher(exec_ctx, w); |
| 155 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 158 | static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw, bool success) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | partly_done(exec_ctx, pw, 1); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 160 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 161 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 162 | static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw, bool success) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 163 | partly_done(exec_ctx, pw, 0); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 164 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 165 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 166 | void grpc_channel_watch_connectivity_state( |
| 167 | grpc_channel *channel, grpc_connectivity_state last_observed_state, |
| 168 | gpr_timespec deadline, grpc_completion_queue *cq, void *tag) { |
| 169 | grpc_channel_element *client_channel_elem = |
| 170 | grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel)); |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 171 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | state_watcher *w = gpr_malloc(sizeof(*w)); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 173 | |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 174 | GRPC_API_TRACE( |
| 175 | "grpc_channel_watch_connectivity_state(" |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 176 | "channel=%p, last_observed_state=%d, " |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 177 | "deadline=gpr_timespec { tv_sec: %lld, tv_nsec: %d, clock_type: %d }, " |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 178 | "cq=%p, tag=%p)", |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 179 | 7, (channel, (int)last_observed_state, (long long)deadline.tv_sec, |
| 180 | (int)deadline.tv_nsec, (int)deadline.clock_type, cq, tag)); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 181 | |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 182 | grpc_cq_begin_op(cq, tag); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 183 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 184 | gpr_mu_init(&w->mu); |
| 185 | grpc_closure_init(&w->on_complete, watch_complete, w); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 186 | w->phase = WAITING; |
| 187 | w->state = last_observed_state; |
| 188 | w->success = 0; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 189 | w->cq = cq; |
| 190 | w->tag = tag; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 191 | w->channel = channel; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 192 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 193 | grpc_timer_init(&exec_ctx, &w->alarm, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 194 | gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC), |
| 195 | timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 196 | |
David Garcia Quintas | 7b1bd2c | 2015-10-05 18:22:10 -0700 | [diff] [blame] | 197 | if (client_channel_elem->filter == &grpc_client_channel_filter) { |
| 198 | GRPC_CHANNEL_INTERNAL_REF(channel, "watch_channel_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | grpc_client_channel_watch_connectivity_state(&exec_ctx, client_channel_elem, |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 200 | grpc_cq_pollset(cq), &w->state, |
| 201 | &w->on_complete); |
David Garcia Quintas | fb349b9 | 2016-03-21 15:37:20 -0700 | [diff] [blame] | 202 | } else { |
| 203 | abort(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 205 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 207 | } |