GRPC Core  9.0.0
health_check_client.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_HEALTH_HEALTH_CHECK_CLIENT_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_HEALTH_HEALTH_CHECK_CLIENT_H
21 
23 
24 #include <grpc/grpc.h>
25 #include <grpc/support/sync.h>
26 
42 
43 namespace grpc_core {
44 
45 class HealthCheckClient : public InternallyRefCounted<HealthCheckClient> {
46  public:
47  HealthCheckClient(const char* service_name,
48  RefCountedPtr<ConnectedSubchannel> connected_subchannel,
49  grpc_pollset_set* interested_parties,
52 
54 
55  void Orphan() override;
56 
57  private:
58  // Contains a call to the backend and all the data related to the call.
59  class CallState : public Orphanable {
60  public:
61  CallState(RefCountedPtr<HealthCheckClient> health_check_client,
62  grpc_pollset_set* interested_parties_);
63  ~CallState();
64 
65  void Orphan() override;
66 
67  void StartCall();
68 
69  private:
70  void Cancel();
71 
72  void StartBatch(grpc_transport_stream_op_batch* batch);
73  static void StartBatchInCallCombiner(void* arg, grpc_error* error);
74 
75  static void CallEndedRetry(void* arg, grpc_error* error);
76  void CallEnded(bool retry);
77 
78  static void OnComplete(void* arg, grpc_error* error);
79  static void RecvInitialMetadataReady(void* arg, grpc_error* error);
80  static void RecvMessageReady(void* arg, grpc_error* error);
81  static void RecvTrailingMetadataReady(void* arg, grpc_error* error);
82  static void StartCancel(void* arg, grpc_error* error);
83  static void OnCancelComplete(void* arg, grpc_error* error);
84 
85  static void OnByteStreamNext(void* arg, grpc_error* error);
86  void ContinueReadingRecvMessage();
87  grpc_error* PullSliceFromRecvMessage();
88  void DoneReadingRecvMessage(grpc_error* error);
89 
90  static void AfterCallStackDestruction(void* arg, grpc_error* error);
91 
92  RefCountedPtr<HealthCheckClient> health_check_client_;
93  grpc_polling_entity pollent_;
94 
95  Arena* arena_;
96  grpc_core::CallCombiner call_combiner_;
98 
99  // The streaming call to the backend. Always non-null.
100  // Refs are tracked manually; when the last ref is released, the
101  // CallState object will be automatically destroyed.
102  SubchannelCall* call_;
103 
106  grpc_transport_stream_op_batch recv_message_batch_;
107  grpc_transport_stream_op_batch recv_trailing_metadata_batch_;
108 
109  grpc_closure on_complete_;
110 
111  // send_initial_metadata
112  grpc_metadata_batch send_initial_metadata_;
113  grpc_linked_mdelem path_metadata_storage_;
114 
115  // send_message
117 
118  // send_trailing_metadata
119  grpc_metadata_batch send_trailing_metadata_;
120 
121  // recv_initial_metadata
122  grpc_metadata_batch recv_initial_metadata_;
123  grpc_closure recv_initial_metadata_ready_;
124 
125  // recv_message
126  OrphanablePtr<ByteStream> recv_message_;
127  grpc_closure recv_message_ready_;
128  grpc_slice_buffer recv_message_buffer_;
129  Atomic<bool> seen_response_{false};
130 
131  // recv_trailing_metadata
132  grpc_metadata_batch recv_trailing_metadata_;
133  grpc_transport_stream_stats collect_stats_;
134  grpc_closure recv_trailing_metadata_ready_;
135 
136  // True if the cancel_stream batch has been started.
137  Atomic<bool> cancelled_{false};
138 
139  // Closure for call stack destruction.
140  grpc_closure after_call_stack_destruction_;
141  };
142 
143  void StartCall();
144  void StartCallLocked(); // Requires holding mu_.
145 
146  void StartRetryTimer();
147  static void OnRetryTimer(void* arg, grpc_error* error);
148 
149  void SetHealthStatus(grpc_connectivity_state state, const char* reason);
150  void SetHealthStatusLocked(grpc_connectivity_state state,
151  const char* reason); // Requires holding mu_.
152 
153  const char* service_name_; // Do not own.
154  RefCountedPtr<ConnectedSubchannel> connected_subchannel_;
155  grpc_pollset_set* interested_parties_; // Do not own.
157 
158  Mutex mu_;
160  bool shutting_down_ = false;
161 
162  // The data associated with the current health check call. It holds a ref
163  // to this HealthCheckClient object.
164  OrphanablePtr<CallState> call_state_;
165 
166  // Call retry state.
167  BackOff retry_backoff_;
168  grpc_timer retry_timer_;
169  grpc_closure retry_timer_callback_;
170  bool retry_timer_callback_pending_ = false;
171 };
172 
173 } // namespace grpc_core
174 
175 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_HEALTH_HEALTH_CHECK_CLIENT_H */
Definition: arena.h:44
grpc_transport_stream_op_batch batch
Definition: client_channel.cc:471
Definition: metadata_batch.h:49
Definition: transport.h:220
Definition: call_combiner.h:49
Definition: error_internal.h:39
Implementation of the backoff mechanism described in doc/connection-backoff.md.
Definition: backoff.h:30
grpc_connectivity_state
Connectivity state of a channel.
Definition: connectivity_state.h:27
Definition: health_check_client.h:45
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:68
Represents an expandable array of slices, to be interpreted as a single item.
Definition: slice.h:78
Definition: orphanable.h:43
Definition: transport.h:128
Definition: context.h:41
void Orphan() override
Definition: health_check_client.cc:95
Definition: orphanable.h:77
Definition: polling_entity.h:37
Round Robin Policy.
Definition: backend_metric.cc:24
Definition: transport.h:163
Definition: ref_counted_ptr.h:35
HealthCheckClient(const char *service_name, RefCountedPtr< ConnectedSubchannel > connected_subchannel, grpc_pollset_set *interested_parties, RefCountedPtr< channelz::SubchannelNode > channelz_node, RefCountedPtr< ConnectivityStateWatcherInterface > watcher)
Definition: health_check_client.cc:46
Definition: subchannel.h:101
Definition: context.h:44
struct grpc_pollset_set grpc_pollset_set
Definition: pollset_set.h:31
Definition: metadata_batch.h:33
Definition: manual_constructor.h:169
Definition: timer.h:30
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
Definition: sync.h:40
~HealthCheckClient()
Definition: health_check_client.cc:74