1 // 2 // Copyright 2015 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef GRPC_SRC_CORE_CLIENT_CHANNEL_CLIENT_CHANNEL_H 18 #define GRPC_SRC_CORE_CLIENT_CHANNEL_CLIENT_CHANNEL_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include "absl/status/status.h" 23 #include "absl/status/statusor.h" 24 #include "absl/strings/string_view.h" 25 #include "src/core/client_channel/client_channel_factory.h" 26 #include "src/core/client_channel/config_selector.h" 27 #include "src/core/client_channel/subchannel.h" 28 #include "src/core/ext/filters/channel_idle/idle_filter_state.h" 29 #include "src/core/filter/blackboard.h" 30 #include "src/core/lib/promise/observable.h" 31 #include "src/core/lib/surface/channel.h" 32 #include "src/core/lib/transport/metadata.h" 33 #include "src/core/load_balancing/lb_policy.h" 34 #include "src/core/resolver/resolver.h" 35 #include "src/core/service_config/service_config.h" 36 #include "src/core/util/single_set_ptr.h" 37 38 namespace grpc_core { 39 40 class ClientChannel : public Channel { 41 public: 42 using PickerObservable = 43 Observable<RefCountedPtr<LoadBalancingPolicy::SubchannelPicker>>; 44 45 class CallDestinationFactory { 46 public: 47 struct RawPointerChannelArgTag {}; 48 ChannelArgName()49 static absl::string_view ChannelArgName() { 50 return "grpc.internal.client_channel_call_destination"; 51 } 52 53 virtual RefCountedPtr<UnstartedCallDestination> CreateCallDestination( 54 PickerObservable) = 0; 55 56 protected: 57 ~CallDestinationFactory() = default; 58 }; 59 60 static absl::StatusOr<RefCountedPtr<Channel>> Create( 61 std::string target, ChannelArgs channel_args); 62 63 // Do not instantiate directly -- use Create() instead. 64 ClientChannel(std::string target_uri, ChannelArgs args, 65 std::string uri_to_resolve, 66 RefCountedPtr<ServiceConfig> default_service_config, 67 ClientChannelFactory* client_channel_factory, 68 CallDestinationFactory* call_destination_factory); 69 70 ~ClientChannel() override; 71 72 void Orphaned() override; 73 74 grpc_call* CreateCall(grpc_call* parent_call, uint32_t propagation_mask, 75 grpc_completion_queue* cq, 76 grpc_pollset_set* /*pollset_set_alternative*/, 77 Slice path, absl::optional<Slice> authority, 78 Timestamp deadline, bool registered_method) override; 79 80 void StartCall(UnstartedCallHandler unstarted_handler) override; 81 event_engine()82 grpc_event_engine::experimental::EventEngine* event_engine() const override { 83 return event_engine_.get(); 84 } 85 86 // TODO(ctiller): lame channels IsLame()87 bool IsLame() const override { return false; } 88 SupportsConnectivityWatcher()89 bool SupportsConnectivityWatcher() const override { return true; } 90 91 // Returns the current connectivity state. If try_to_connect is true, 92 // triggers a connection attempt if not already connected. 93 grpc_connectivity_state CheckConnectivityState(bool try_to_connect) override; 94 95 void WatchConnectivityState(grpc_connectivity_state last_observed_state, 96 Timestamp deadline, grpc_completion_queue* cq, 97 void* tag) override; 98 99 // Starts and stops a connectivity watch. The watcher will be initially 100 // notified as soon as the state changes from initial_state and then on 101 // every subsequent state change until either the watch is stopped or 102 // it is notified that the state has changed to SHUTDOWN. 103 // 104 // This is intended to be used when starting watches from code inside of 105 // C-core (e.g., for a nested control plane channel for things like xds). 106 void AddConnectivityWatcher( 107 grpc_connectivity_state initial_state, 108 OrphanablePtr<AsyncConnectivityStateWatcherInterface> watcher) override; 109 void RemoveConnectivityWatcher( 110 AsyncConnectivityStateWatcherInterface* watcher) override; 111 112 void GetInfo(const grpc_channel_info* channel_info) override; 113 114 void ResetConnectionBackoff() override; 115 116 void Ping(grpc_completion_queue* cq, void* tag) override; 117 118 // Flag that this object gets stored in channel args as a raw pointer. 119 struct RawPointerChannelArgTag {}; ChannelArgName()120 static absl::string_view ChannelArgName() { 121 return "grpc.internal.client_channel"; 122 } 123 124 private: 125 class ClientChannelControlHelper; 126 class ResolverResultHandler; 127 class SubchannelWrapper; 128 129 void CreateResolverLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 130 void DestroyResolverAndLbPolicyLocked() 131 ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 132 133 void TryToConnectLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 134 135 void OnResolverResultChangedLocked(Resolver::Result result) 136 ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 137 void OnResolverErrorLocked(absl::Status status) 138 ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 139 140 absl::Status CreateOrUpdateLbPolicyLocked( 141 RefCountedPtr<LoadBalancingPolicy::Config> lb_policy_config, 142 const absl::optional<std::string>& health_check_service_name, 143 Resolver::Result result) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 144 OrphanablePtr<LoadBalancingPolicy> CreateLbPolicyLocked( 145 const ChannelArgs& args) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 146 147 void UpdateServiceConfigInControlPlaneLocked( 148 RefCountedPtr<ServiceConfig> service_config, 149 RefCountedPtr<ConfigSelector> config_selector, std::string lb_policy_name) 150 ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 151 152 void UpdateServiceConfigInDataPlaneLocked(const ChannelArgs& args) 153 ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 154 155 void UpdateStateLocked(grpc_connectivity_state state, 156 const absl::Status& status, const char* reason) 157 ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 158 159 void UpdateStateAndPickerLocked( 160 grpc_connectivity_state state, const absl::Status& status, 161 const char* reason, 162 RefCountedPtr<LoadBalancingPolicy::SubchannelPicker> picker) 163 ABSL_EXCLUSIVE_LOCKS_REQUIRED(*work_serializer_); 164 165 void StartIdleTimer(); 166 167 // Applies service config settings from config_selector to the call. 168 // May modify call context and client_initial_metadata. 169 absl::Status ApplyServiceConfigToCall( 170 ConfigSelector& config_selector, 171 ClientMetadata& client_initial_metadata) const; 172 173 const ChannelArgs channel_args_; 174 const std::shared_ptr<grpc_event_engine::experimental::EventEngine> 175 event_engine_; 176 const std::string uri_to_resolve_; 177 const size_t service_config_parser_index_; 178 const RefCountedPtr<ServiceConfig> default_service_config_; 179 ClientChannelFactory* const client_channel_factory_; 180 const std::string default_authority_; 181 channelz::ChannelNode* const channelz_node_; 182 GlobalStatsPluginRegistry::StatsPluginGroup stats_plugin_group_; 183 184 // 185 // Idleness state. 186 // 187 const Duration idle_timeout_; 188 IdleFilterState idle_state_{false}; 189 SingleSetPtr<Activity, typename ActivityPtr::deleter_type> idle_activity_; 190 191 // 192 // Fields related to name resolution. 193 // 194 struct ResolverDataForCalls { 195 RefCountedPtr<ConfigSelector> config_selector; 196 RefCountedPtr<UnstartedCallDestination> call_destination; 197 }; 198 Observable<absl::StatusOr<ResolverDataForCalls>> resolver_data_for_calls_; 199 200 // 201 // Fields related to LB picks. 202 // 203 PickerObservable picker_; 204 const RefCountedPtr<UnstartedCallDestination> call_destination_; 205 206 // 207 // Fields used in the control plane. Guarded by work_serializer. 208 // 209 std::shared_ptr<WorkSerializer> work_serializer_; 210 ConnectivityStateTracker state_tracker_ ABSL_GUARDED_BY(*work_serializer_); 211 OrphanablePtr<Resolver> resolver_ ABSL_GUARDED_BY(*work_serializer_); 212 bool previous_resolution_contained_addresses_ 213 ABSL_GUARDED_BY(*work_serializer_) = false; 214 RefCountedPtr<ServiceConfig> saved_service_config_ 215 ABSL_GUARDED_BY(*work_serializer_); 216 RefCountedPtr<ConfigSelector> saved_config_selector_ 217 ABSL_GUARDED_BY(*work_serializer_); 218 RefCountedPtr<const Blackboard> blackboard_ 219 ABSL_GUARDED_BY(*work_serializer_); 220 OrphanablePtr<LoadBalancingPolicy> lb_policy_ 221 ABSL_GUARDED_BY(*work_serializer_); 222 RefCountedPtr<SubchannelPoolInterface> subchannel_pool_ 223 ABSL_GUARDED_BY(*work_serializer_); 224 // The number of SubchannelWrapper instances referencing a given Subchannel. 225 std::map<Subchannel*, int> subchannel_refcount_map_ 226 ABSL_GUARDED_BY(*work_serializer_); 227 // The set of SubchannelWrappers that currently exist. 228 // No need to hold a ref, since the set is updated in the control-plane 229 // work_serializer when the SubchannelWrappers are created and destroyed. 230 absl::flat_hash_set<SubchannelWrapper*> subchannel_wrappers_ 231 ABSL_GUARDED_BY(*work_serializer_); 232 int keepalive_time_ ABSL_GUARDED_BY(*work_serializer_) = -1; 233 absl::Status disconnect_error_ ABSL_GUARDED_BY(*work_serializer_); 234 235 // 236 // Fields accessed via GetChannelInfo(). 237 // 238 Mutex info_mu_; 239 std::string info_lb_policy_name_ ABSL_GUARDED_BY(info_mu_); 240 std::string info_service_config_json_ ABSL_GUARDED_BY(info_mu_); 241 }; 242 243 } // namespace grpc_core 244 245 #endif // GRPC_SRC_CORE_CLIENT_CHANNEL_CLIENT_CHANNEL_H 246