7af88a54cfcbeaa91e02e2d0124d62ca52d46af0
[platform/upstream/grpc.git] / src / core / ext / filters / client_channel / health / health_check_client.h
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
22 #include <grpc/support/port_platform.h>
23
24 #include <grpc/grpc.h>
25 #include <grpc/support/atm.h>
26 #include <grpc/support/sync.h>
27
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"
41
42 namespace grpc_core {
43
44 class HealthCheckClient : public InternallyRefCounted<HealthCheckClient> {
45  public:
46   HealthCheckClient(const char* service_name,
47                     RefCountedPtr<ConnectedSubchannel> connected_subchannel,
48                     grpc_pollset_set* interested_parties,
49                     RefCountedPtr<channelz::SubchannelNode> channelz_node);
50
51   ~HealthCheckClient();
52
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);
58
59   void Orphan() override;
60
61  private:
62   // Contains a call to the backend and all the data related to the call.
63   class CallState : public InternallyRefCounted<CallState> {
64    public:
65     CallState(RefCountedPtr<HealthCheckClient> health_check_client,
66               grpc_pollset_set* interested_parties_);
67     ~CallState();
68
69     void Orphan() override;
70
71     void StartCall();
72
73    private:
74     void Cancel();
75
76     void StartBatch(grpc_transport_stream_op_batch* batch);
77     static void StartBatchInCallCombiner(void* arg, grpc_error* error);
78
79     static void CallEndedRetry(void* arg, grpc_error* error);
80     void CallEnded(bool retry);
81
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);
88
89     static void OnByteStreamNext(void* arg, grpc_error* error);
90     void ContinueReadingRecvMessage();
91     grpc_error* PullSliceFromRecvMessage();
92     void DoneReadingRecvMessage(grpc_error* error);
93
94     RefCountedPtr<HealthCheckClient> health_check_client_;
95     grpc_polling_entity pollent_;
96
97     gpr_arena* arena_;
98     grpc_call_combiner call_combiner_;
99     grpc_call_context_element context_[GRPC_CONTEXT_COUNT] = {};
100
101     // The streaming call to the backend. Always non-NULL.
102     RefCountedPtr<SubchannelCall> call_;
103
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_;
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
116     ManualConstructor<SliceBufferByteStream> 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     gpr_atm seen_response_;
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
137   void StartCall();
138   void StartCallLocked();  // Requires holding mu_.
139
140   void StartRetryTimer();
141   static void OnRetryTimer(void* arg, grpc_error* error);
142
143   void SetHealthStatus(grpc_connectivity_state state, grpc_error* error);
144   void SetHealthStatusLocked(grpc_connectivity_state state,
145                              grpc_error* error);  // Requires holding mu_.
146
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_;
151
152   gpr_mu mu_;
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;
158
159   // The data associated with the current health check call.  It holds a ref
160   // to this HealthCheckClient object.
161   OrphanablePtr<CallState> call_state_;
162
163   // Call retry state.
164   BackOff retry_backoff_;
165   grpc_timer retry_timer_;
166   grpc_closure retry_timer_callback_;
167   bool retry_timer_callback_pending_ = false;
168 };
169
170 }  // namespace grpc_core
171
172 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_HEALTH_HEALTH_CHECK_CLIENT_H */