LCOV - code coverage report
Current view: top level - src/core/surface - channel_connectivity.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 83 92 90.2 %
Date: 2015-10-10 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       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/api_trace.h"
      42             : #include "src/core/surface/completion_queue.h"
      43             : 
      44         125 : grpc_connectivity_state grpc_channel_check_connectivity_state(
      45             :     grpc_channel *channel, int try_to_connect) {
      46             :   /* forward through to the underlying client channel */
      47         125 :   grpc_channel_element *client_channel_elem =
      48         125 :       grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
      49         125 :   grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
      50             :   grpc_connectivity_state state;
      51         125 :   GRPC_API_TRACE(
      52             :       "grpc_channel_check_connectivity_state(channel=%p, try_to_connect=%d)", 2,
      53             :       (channel, try_to_connect));
      54         125 :   if (client_channel_elem->filter != &grpc_client_channel_filter) {
      55           0 :     gpr_log(GPR_ERROR,
      56             :             "grpc_channel_check_connectivity_state called on something that is "
      57             :             "not a client channel, but '%s'",
      58           0 :             client_channel_elem->filter->name);
      59           0 :     grpc_exec_ctx_finish(&exec_ctx);
      60           0 :     return GRPC_CHANNEL_FATAL_FAILURE;
      61             :   }
      62         125 :   state = grpc_client_channel_check_connectivity_state(
      63             :       &exec_ctx, client_channel_elem, try_to_connect);
      64         125 :   grpc_exec_ctx_finish(&exec_ctx);
      65         125 :   return state;
      66             : }
      67             : 
      68             : typedef enum {
      69             :   WAITING,
      70             :   CALLING_BACK,
      71             :   CALLING_BACK_AND_FINISHED,
      72             :   CALLED_BACK
      73             : } callback_phase;
      74             : 
      75             : typedef struct {
      76             :   gpr_mu mu;
      77             :   callback_phase phase;
      78             :   int success;
      79             :   int removed;
      80             :   grpc_closure on_complete;
      81             :   grpc_alarm alarm;
      82             :   grpc_connectivity_state state;
      83             :   grpc_completion_queue *cq;
      84             :   grpc_cq_completion completion_storage;
      85             :   grpc_channel *channel;
      86             :   void *tag;
      87             : } state_watcher;
      88             : 
      89          94 : static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) {
      90          94 :   GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel, "watch_connectivity");
      91          94 :   gpr_mu_destroy(&w->mu);
      92          94 :   gpr_free(w);
      93          94 : }
      94             : 
      95          94 : static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw,
      96             :                                 grpc_cq_completion *ignored) {
      97          94 :   int delete = 0;
      98          94 :   state_watcher *w = pw;
      99          94 :   gpr_mu_lock(&w->mu);
     100          94 :   switch (w->phase) {
     101             :     case WAITING:
     102             :     case CALLED_BACK:
     103           0 :       GPR_UNREACHABLE_CODE(return );
     104             :     case CALLING_BACK:
     105          22 :       w->phase = CALLED_BACK;
     106          22 :       break;
     107             :     case CALLING_BACK_AND_FINISHED:
     108          72 :       delete = 1;
     109          72 :       break;
     110             :   }
     111          94 :   gpr_mu_unlock(&w->mu);
     112             : 
     113          94 :   if (delete) {
     114          72 :     delete_state_watcher(exec_ctx, w);
     115             :   }
     116          94 : }
     117             : 
     118         188 : static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
     119             :                         int due_to_completion) {
     120         188 :   int delete = 0;
     121         188 :   grpc_channel_element *client_channel_elem = NULL;
     122             : 
     123         188 :   gpr_mu_lock(&w->mu);
     124         188 :   if (w->removed == 0) {
     125          94 :     w->removed = 1;
     126          94 :     client_channel_elem = grpc_channel_stack_last_element(
     127             :         grpc_channel_get_channel_stack(w->channel));
     128          94 :     grpc_client_channel_del_interested_party(exec_ctx, client_channel_elem,
     129             :                                              grpc_cq_pollset(w->cq));
     130             :   }
     131         188 :   gpr_mu_unlock(&w->mu);
     132         188 :   if (due_to_completion) {
     133          94 :     gpr_mu_lock(&w->mu);
     134          94 :     w->success = 1;
     135          94 :     gpr_mu_unlock(&w->mu);
     136          94 :     grpc_alarm_cancel(exec_ctx, &w->alarm);
     137             :   }
     138             : 
     139         188 :   gpr_mu_lock(&w->mu);
     140         188 :   switch (w->phase) {
     141             :     case WAITING:
     142          94 :       w->phase = CALLING_BACK;
     143          94 :       grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->success, finished_completion,
     144             :                      w, &w->completion_storage);
     145          94 :       break;
     146             :     case CALLING_BACK:
     147          72 :       w->phase = CALLING_BACK_AND_FINISHED;
     148          72 :       break;
     149             :     case CALLING_BACK_AND_FINISHED:
     150           0 :       GPR_UNREACHABLE_CODE(return );
     151             :     case CALLED_BACK:
     152          22 :       delete = 1;
     153          22 :       break;
     154             :   }
     155         188 :   gpr_mu_unlock(&w->mu);
     156             : 
     157         188 :   if (delete) {
     158          22 :     delete_state_watcher(exec_ctx, w);
     159             :   }
     160         188 : }
     161             : 
     162          94 : static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
     163          94 :   partly_done(exec_ctx, pw, 1);
     164          94 : }
     165             : 
     166          94 : static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
     167          94 :   partly_done(exec_ctx, pw, 0);
     168          94 : }
     169             : 
     170          94 : void 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          94 :   grpc_channel_element *client_channel_elem =
     174          94 :       grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
     175          94 :   grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
     176          94 :   state_watcher *w = gpr_malloc(sizeof(*w));
     177             : 
     178          94 :   GRPC_API_TRACE(
     179             :       "grpc_channel_watch_connectivity_state("
     180             :       "channel=%p, last_observed_state=%d, "
     181             :       "deadline=gpr_timespec { tv_sec: %ld, tv_nsec: %d, clock_type: %d }, "
     182             :       "cq=%p, tag=%p)",
     183             :       7, (channel, (int)last_observed_state, (long)deadline.tv_sec,
     184             :           deadline.tv_nsec, (int)deadline.clock_type, cq, tag));
     185             : 
     186          94 :   grpc_cq_begin_op(cq);
     187             : 
     188          94 :   gpr_mu_init(&w->mu);
     189          94 :   grpc_closure_init(&w->on_complete, watch_complete, w);
     190          94 :   w->phase = WAITING;
     191          94 :   w->state = last_observed_state;
     192          94 :   w->success = 0;
     193          94 :   w->removed = 0;
     194          94 :   w->cq = cq;
     195          94 :   w->tag = tag;
     196          94 :   w->channel = channel;
     197             : 
     198          94 :   grpc_alarm_init(&exec_ctx, &w->alarm,
     199             :                   gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
     200             :                   timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
     201             : 
     202          94 :   if (client_channel_elem->filter != &grpc_client_channel_filter) {
     203           0 :     gpr_log(GPR_ERROR,
     204             :             "grpc_channel_watch_connectivity_state called on something that is "
     205             :             "not a client channel, but '%s'",
     206           0 :             client_channel_elem->filter->name);
     207           0 :     grpc_exec_ctx_enqueue(&exec_ctx, &w->on_complete, 1);
     208             :   } else {
     209          94 :     GRPC_CHANNEL_INTERNAL_REF(channel, "watch_connectivity");
     210          94 :     grpc_client_channel_add_interested_party(&exec_ctx, client_channel_elem,
     211             :                                              grpc_cq_pollset(cq));
     212          94 :     grpc_client_channel_watch_connectivity_state(&exec_ctx, client_channel_elem,
     213             :                                                  &w->state, &w->on_complete);
     214             :   }
     215             : 
     216          94 :   grpc_exec_ctx_finish(&exec_ctx);
     217          94 : }

Generated by: LCOV version 1.10