connectivity_state.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 
20 
22 
23 #include <string>
24 #include <type_traits>
25 
26 #include <grpc/support/log.h>
27 
33 
34 namespace grpc_core {
35 
36 TraceFlag grpc_connectivity_state_trace(false, "connectivity_state");
37 
39  switch (state) {
40  case GRPC_CHANNEL_IDLE:
41  return "IDLE";
43  return "CONNECTING";
44  case GRPC_CHANNEL_READY:
45  return "READY";
47  return "TRANSIENT_FAILURE";
49  return "SHUTDOWN";
50  }
51  GPR_UNREACHABLE_CODE(return "UNKNOWN");
52 }
53 
54 //
55 // AsyncConnectivityStateWatcherInterface
56 //
57 
58 // A fire-and-forget class to asynchronously deliver a connectivity
59 // state notification to a watcher.
61  public:
64  const std::shared_ptr<WorkSerializer>& work_serializer)
66  if (work_serializer != nullptr) {
67  work_serializer->Run(
68  [this]() { SendNotification(this, GRPC_ERROR_NONE); },
70  } else {
72  grpc_schedule_on_exec_ctx);
74  }
75  }
76 
77  private:
78  static void SendNotification(void* arg, grpc_error_handle /*ignored*/) {
79  Notifier* self = static_cast<Notifier*>(arg);
81  gpr_log(GPR_INFO, "watcher %p: delivering async notification for %s (%s)",
82  self->watcher_.get(), ConnectivityStateName(self->state_),
83  self->status_.ToString().c_str());
84  }
85  self->watcher_->OnConnectivityStateChange(self->state_, self->status_);
86  delete self;
87  }
88 
93 };
94 
97  new Notifier(Ref(), state, status,
98  work_serializer_); // Deletes itself when done.
99 }
100 
101 //
102 // ConnectivityStateTracker
103 //
104 
106  grpc_connectivity_state current_state =
107  state_.load(std::memory_order_relaxed);
108  if (current_state == GRPC_CHANNEL_SHUTDOWN) return;
109  for (const auto& p : watchers_) {
112  "ConnectivityStateTracker %s[%p]: notifying watcher %p: %s -> %s",
113  name_, this, p.first, ConnectivityStateName(current_state),
115  }
116  p.second->Notify(GRPC_CHANNEL_SHUTDOWN, absl::Status());
117  }
118 }
119 
121  grpc_connectivity_state initial_state,
124  gpr_log(GPR_INFO, "ConnectivityStateTracker %s[%p]: add watcher %p", name_,
125  this, watcher.get());
126  }
127  grpc_connectivity_state current_state =
128  state_.load(std::memory_order_relaxed);
129  if (initial_state != current_state) {
132  "ConnectivityStateTracker %s[%p]: notifying watcher %p: %s -> %s",
133  name_, this, watcher.get(), ConnectivityStateName(initial_state),
134  ConnectivityStateName(current_state));
135  }
136  watcher->Notify(current_state, status_);
137  }
138  // If we're in state SHUTDOWN, don't add the watcher, so that it will
139  // be orphaned immediately.
140  if (current_state != GRPC_CHANNEL_SHUTDOWN) {
141  watchers_.insert(std::make_pair(watcher.get(), std::move(watcher)));
142  }
143 }
144 
148  gpr_log(GPR_INFO, "ConnectivityStateTracker %s[%p]: remove watcher %p",
149  name_, this, watcher);
150  }
151  watchers_.erase(watcher);
152 }
153 
155  const absl::Status& status,
156  const char* reason) {
157  grpc_connectivity_state current_state =
158  state_.load(std::memory_order_relaxed);
159  if (state == current_state) return;
161  gpr_log(GPR_INFO, "ConnectivityStateTracker %s[%p]: %s -> %s (%s, %s)",
162  name_, this, ConnectivityStateName(current_state),
163  ConnectivityStateName(state), reason, status.ToString().c_str());
164  }
165  state_.store(state, std::memory_order_relaxed);
166  status_ = status;
167  for (const auto& p : watchers_) {
170  "ConnectivityStateTracker %s[%p]: notifying watcher %p: %s -> %s",
171  name_, this, p.first, ConnectivityStateName(current_state),
173  }
174  p.second->Notify(state, status);
175  }
176  // If the new state is SHUTDOWN, orphan all of the watchers. This
177  // avoids the need for the callers to explicitly cancel them.
178  if (state == GRPC_CHANNEL_SHUTDOWN) watchers_.clear();
179 }
180 
182  grpc_connectivity_state state = state_.load(std::memory_order_relaxed);
184  gpr_log(GPR_INFO, "ConnectivityStateTracker %s[%p]: get current state: %s",
186  }
187  return state;
188 }
189 
190 } // namespace grpc_core
GRPC_CLOSURE_INIT
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
Definition: closure.h:115
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_core::ConnectivityStateTracker::RemoveWatcher
void RemoveWatcher(ConnectivityStateWatcherInterface *watcher)
Definition: connectivity_state.cc:145
GRPC_CHANNEL_READY
@ GRPC_CHANNEL_READY
Definition: include/grpc/impl/codegen/connectivity_state.h:36
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
grpc_core::ConnectivityStateWatcherInterface
Definition: src/core/lib/transport/connectivity_state.h:49
grpc_core::ConnectivityStateTracker::status
absl::Status status() const
Definition: src/core/lib/transport/connectivity_state.h:129
absl::Status::ToString
std::string ToString(StatusToStringMode mode=StatusToStringMode::kDefault) const
Definition: third_party/abseil-cpp/absl/status/status.h:821
grpc_core::AsyncConnectivityStateWatcherInterface::Notifier::closure_
grpc_closure closure_
Definition: connectivity_state.cc:92
connectivity_state.h
grpc_core::AsyncConnectivityStateWatcherInterface::Notifier
Definition: connectivity_state.cc:60
grpc_core::AsyncConnectivityStateWatcherInterface::Notify
void Notify(grpc_connectivity_state new_state, const absl::Status &status) final
Definition: connectivity_state.cc:95
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::AsyncConnectivityStateWatcherInterface::work_serializer_
std::shared_ptr< WorkSerializer > work_serializer_
Definition: src/core/lib/transport/connectivity_state.h:88
GRPC_CHANNEL_TRANSIENT_FAILURE
@ GRPC_CHANNEL_TRANSIENT_FAILURE
Definition: include/grpc/impl/codegen/connectivity_state.h:38
closure.h
grpc_core::ConnectivityStateTracker::AddWatcher
void AddWatcher(grpc_connectivity_state initial_state, OrphanablePtr< ConnectivityStateWatcherInterface > watcher)
Definition: connectivity_state.cc:120
status
absl::Status status
Definition: rls.cc:251
grpc_core::grpc_connectivity_state_trace
TraceFlag grpc_connectivity_state_trace(false, "connectivity_state")
Definition: src/core/lib/transport/connectivity_state.h:39
grpc_core::ConnectivityStateTracker::watchers_
std::map< ConnectivityStateWatcherInterface *, OrphanablePtr< ConnectivityStateWatcherInterface > > watchers_
Definition: src/core/lib/transport/connectivity_state.h:139
GRPC_TRACE_FLAG_ENABLED
#define GRPC_TRACE_FLAG_ENABLED(f)
Definition: debug/trace.h:114
grpc_connectivity_state
grpc_connectivity_state
Definition: include/grpc/impl/codegen/connectivity_state.h:30
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_core::ConnectivityStateTracker::state
grpc_connectivity_state state() const
Definition: connectivity_state.cc:181
grpc_core::ConnectivityStateTracker::SetState
void SetState(grpc_connectivity_state state, const absl::Status &status, const char *reason)
Definition: connectivity_state.cc:154
grpc_core::ConnectivityStateTracker::state_
std::atomic< grpc_connectivity_state > state_
Definition: src/core/lib/transport/connectivity_state.h:133
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::InternallyRefCounted< ConnectivityStateWatcherInterface >::Ref
RefCountedPtr< ConnectivityStateWatcherInterface > Ref() GRPC_MUST_USE_RESULT
Definition: orphanable.h:90
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
GRPC_CHANNEL_IDLE
@ GRPC_CHANNEL_IDLE
Definition: include/grpc/impl/codegen/connectivity_state.h:32
arg
Definition: cmdline.cc:40
error.h
grpc_core::AsyncConnectivityStateWatcherInterface::Notifier::SendNotification
static void SendNotification(void *arg, grpc_error_handle)
Definition: connectivity_state.cc:78
GPR_UNREACHABLE_CODE
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: impl/codegen/port_platform.h:652
grpc_core::AsyncConnectivityStateWatcherInterface::Notifier::watcher_
RefCountedPtr< AsyncConnectivityStateWatcherInterface > watcher_
Definition: connectivity_state.cc:89
GRPC_CHANNEL_CONNECTING
@ GRPC_CHANNEL_CONNECTING
Definition: include/grpc/impl/codegen/connectivity_state.h:34
grpc_core::ConnectivityStateName
const char * ConnectivityStateName(grpc_connectivity_state state)
Definition: connectivity_state.cc:38
debug_location.h
grpc_core::ConnectivityStateTracker::status_
absl::Status status_
Definition: src/core/lib/transport/connectivity_state.h:134
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
grpc_core::OrphanablePtr
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:64
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
arg
struct arg arg
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
exec_ctx.h
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
grpc_core::AsyncConnectivityStateWatcherInterface::Notifier::status_
const absl::Status status_
Definition: connectivity_state.cc:91
ref_counted_ptr.h
watcher
ClusterWatcher * watcher
Definition: cds.cc:148
grpc_core::AsyncConnectivityStateWatcherInterface::Notifier::Notifier
Notifier(RefCountedPtr< AsyncConnectivityStateWatcherInterface > watcher, grpc_connectivity_state state, const absl::Status &status, const std::shared_ptr< WorkSerializer > &work_serializer)
Definition: connectivity_state.cc:62
GRPC_CHANNEL_SHUTDOWN
@ GRPC_CHANNEL_SHUTDOWN
Definition: include/grpc/impl/codegen/connectivity_state.h:40
grpc_core::ConnectivityStateTracker::name_
const char * name_
Definition: src/core/lib/transport/connectivity_state.h:132
grpc_core::AsyncConnectivityStateWatcherInterface::Notifier::state_
const grpc_connectivity_state state_
Definition: connectivity_state.cc:90
grpc_error
Definition: error_internal.h:42
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
grpc_closure
Definition: closure.h:56
grpc_core::ConnectivityStateTracker::~ConnectivityStateTracker
~ConnectivityStateTracker()
Definition: connectivity_state.cc:105
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:01