3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
22 #include <grpc/support/port_platform.h>
24 #include <grpc/grpc.h>
25 #include <grpc/support/atm.h>
26 #include <grpc/support/sync.h>
28 #include "src/core/ext/filters/client_channel/client_channel_channelz.h"
29 #include "src/core/ext/filters/client_channel/subchannel.h"
30 #include "src/core/lib/backoff/backoff.h"
31 #include "src/core/lib/gpr/arena.h"
32 #include "src/core/lib/gprpp/orphanable.h"
33 #include "src/core/lib/gprpp/ref_counted_ptr.h"
34 #include "src/core/lib/iomgr/call_combiner.h"
35 #include "src/core/lib/iomgr/closure.h"
36 #include "src/core/lib/iomgr/polling_entity.h"
37 #include "src/core/lib/iomgr/timer.h"
38 #include "src/core/lib/transport/byte_stream.h"
39 #include "src/core/lib/transport/metadata_batch.h"
40 #include "src/core/lib/transport/transport.h"
44 class HealthCheckClient : public InternallyRefCounted<HealthCheckClient> {
46 HealthCheckClient(const char* service_name,
47 RefCountedPtr<ConnectedSubchannel> connected_subchannel,
48 grpc_pollset_set* interested_parties,
49 RefCountedPtr<channelz::SubchannelNode> channelz_node);
53 // When the health state changes from *state, sets *state to the new
54 // value and schedules closure.
55 // Only one closure can be outstanding at a time.
56 void NotifyOnHealthChange(grpc_connectivity_state* state,
57 grpc_closure* closure);
59 void Orphan() override;
62 // Contains a call to the backend and all the data related to the call.
63 class CallState : public InternallyRefCounted<CallState> {
65 CallState(RefCountedPtr<HealthCheckClient> health_check_client,
66 grpc_pollset_set* interested_parties_);
69 void Orphan() override;
76 void StartBatch(grpc_transport_stream_op_batch* batch);
77 static void StartBatchInCallCombiner(void* arg, grpc_error* error);
79 static void CallEndedRetry(void* arg, grpc_error* error);
80 void CallEnded(bool retry);
82 static void OnComplete(void* arg, grpc_error* error);
83 static void RecvInitialMetadataReady(void* arg, grpc_error* error);
84 static void RecvMessageReady(void* arg, grpc_error* error);
85 static void RecvTrailingMetadataReady(void* arg, grpc_error* error);
86 static void StartCancel(void* arg, grpc_error* error);
87 static void OnCancelComplete(void* arg, grpc_error* error);
89 static void OnByteStreamNext(void* arg, grpc_error* error);
90 void ContinueReadingRecvMessage();
91 grpc_error* PullSliceFromRecvMessage();
92 void DoneReadingRecvMessage(grpc_error* error);
94 RefCountedPtr<HealthCheckClient> health_check_client_;
95 grpc_polling_entity pollent_;
98 grpc_call_combiner call_combiner_;
99 grpc_call_context_element context_[GRPC_CONTEXT_COUNT] = {};
101 // The streaming call to the backend. Always non-NULL.
102 RefCountedPtr<SubchannelCall> call_;
104 grpc_transport_stream_op_batch_payload payload_;
105 grpc_transport_stream_op_batch batch_;
106 grpc_transport_stream_op_batch recv_message_batch_;
107 grpc_transport_stream_op_batch recv_trailing_metadata_batch_;
109 grpc_closure on_complete_;
111 // send_initial_metadata
112 grpc_metadata_batch send_initial_metadata_;
113 grpc_linked_mdelem path_metadata_storage_;
116 ManualConstructor<SliceBufferByteStream> send_message_;
118 // send_trailing_metadata
119 grpc_metadata_batch send_trailing_metadata_;
121 // recv_initial_metadata
122 grpc_metadata_batch recv_initial_metadata_;
123 grpc_closure recv_initial_metadata_ready_;
126 OrphanablePtr<ByteStream> recv_message_;
127 grpc_closure recv_message_ready_;
128 grpc_slice_buffer recv_message_buffer_;
129 gpr_atm seen_response_;
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_;
138 void StartCallLocked(); // Requires holding mu_.
140 void StartRetryTimer();
141 static void OnRetryTimer(void* arg, grpc_error* error);
143 void SetHealthStatus(grpc_connectivity_state state, grpc_error* error);
144 void SetHealthStatusLocked(grpc_connectivity_state state,
145 grpc_error* error); // Requires holding mu_.
147 const char* service_name_; // Do not own.
148 RefCountedPtr<ConnectedSubchannel> connected_subchannel_;
149 grpc_pollset_set* interested_parties_; // Do not own.
150 RefCountedPtr<channelz::SubchannelNode> channelz_node_;
153 grpc_connectivity_state state_ = GRPC_CHANNEL_CONNECTING;
154 grpc_error* error_ = GRPC_ERROR_NONE;
155 grpc_connectivity_state* notify_state_ = nullptr;
156 grpc_closure* on_health_changed_ = nullptr;
157 bool shutting_down_ = false;
159 // The data associated with the current health check call. It holds a ref
160 // to this HealthCheckClient object.
161 OrphanablePtr<CallState> call_state_;
164 BackOff retry_backoff_;
165 grpc_timer retry_timer_;
166 grpc_closure retry_timer_callback_;
167 bool retry_timer_callback_pending_ = false;
170 } // namespace grpc_core
172 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_HEALTH_HEALTH_CHECK_CLIENT_H */