polling_resolver.cc
Go to the documentation of this file.
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 
18 
20 
21 #include <inttypes.h>
22 
23 #include <utility>
24 
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/string_view.h"
28 #include "absl/strings/strip.h"
29 
30 #include <grpc/support/log.h>
31 
40 
41 namespace grpc_core {
42 
44  const grpc_channel_args* channel_args,
45  Duration min_time_between_resolutions,
46  BackOff::Options backoff_options,
47  TraceFlag* tracer)
48  : authority_(args.uri.authority()),
49  name_to_resolve_(absl::StripPrefix(args.uri.path(), "/")),
51  work_serializer_(std::move(args.work_serializer)),
52  result_handler_(std::move(args.result_handler)),
53  tracer_(tracer),
54  interested_parties_(args.pollset_set),
55  min_time_between_resolutions_(min_time_between_resolutions),
56  backoff_(backoff_options) {
57  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
58  gpr_log(GPR_INFO, "[polling resolver %p] created", this);
59  }
60 }
61 
63  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
64  gpr_log(GPR_INFO, "[polling resolver %p] destroying", this);
65  }
67 }
68 
70 
72  if (request_ == nullptr) {
74  }
75 }
76 
80  }
81  backoff_.Reset();
82 }
83 
85  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
86  gpr_log(GPR_INFO, "[polling resolver %p] shutting down", this);
87  }
88  shutdown_ = true;
91  }
92  request_.reset();
93 }
94 
96  auto* self = static_cast<PollingResolver*>(arg);
97  (void)GRPC_ERROR_REF(error); // ref owned by lambda
98  self->work_serializer_->Run(
99  [self, error]() { self->OnNextResolutionLocked(error); }, DEBUG_LOCATION);
100 }
101 
103  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
105  "[polling resolver %p] re-resolution timer fired: error=\"%s\", "
106  "shutdown_=%d",
108  }
112  }
113  Unref(DEBUG_LOCATION, "retry-timer");
115 }
116 
118  Ref(DEBUG_LOCATION, "OnRequestComplete").release();
119  work_serializer_->Run(
120  [this, result]() mutable { OnRequestCompleteLocked(std::move(result)); },
122 }
123 
125  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
126  gpr_log(GPR_INFO, "[polling resolver %p] request complete", this);
127  }
128  request_.reset();
129  if (!shutdown_) {
130  if (result.service_config.ok() && result.addresses.ok()) {
131  // Reset backoff state so that we start from the beginning when the
132  // next request gets triggered.
133  backoff_.Reset();
134  } else {
135  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
137  "[polling resolver %p] resolution failed (will retry): "
138  "address status \"%s\"; service config status \"%s\"",
139  this, result.addresses.status().ToString().c_str(),
140  result.service_config.status().ToString().c_str());
141  }
142  // Set up for retry.
143  // InvalidateNow to avoid getting stuck re-initializing this timer
144  // in a loop while draining the currently-held WorkSerializer.
145  // Also see https://github.com/grpc/grpc/issues/26079.
147  Timestamp next_try = backoff_.NextAttemptTime();
148  Duration timeout = next_try - ExecCtx::Get()->Now();
151  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
152  if (timeout > Duration::Zero()) {
153  gpr_log(GPR_INFO, "[polling resolver %p] retrying in %" PRId64 " ms",
154  this, timeout.millis());
155  } else {
156  gpr_log(GPR_INFO, "[polling resolver %p] retrying immediately", this);
157  }
158  }
159  Ref(DEBUG_LOCATION, "next_resolution_timer").release();
162  }
163  result_handler_->ReportResult(std::move(result));
164  }
165  Unref(DEBUG_LOCATION, "OnRequestComplete");
166 }
167 
169  // If there is an existing timer, the time it fires is the earliest time we
170  // can start the next resolution.
171  if (have_next_resolution_timer_) return;
172  if (last_resolution_timestamp_.has_value()) {
173  // InvalidateNow to avoid getting stuck re-initializing this timer
174  // in a loop while draining the currently-held WorkSerializer.
175  // Also see https://github.com/grpc/grpc/issues/26079.
177  const Timestamp earliest_next_resolution =
179  const Duration time_until_next_resolution =
180  earliest_next_resolution - ExecCtx::Get()->Now();
181  if (time_until_next_resolution > Duration::Zero()) {
182  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
183  const Duration last_resolution_ago =
186  "[polling resolver %p] in cooldown from last resolution "
187  "(from %" PRId64 " ms ago); will resolve again in %" PRId64
188  " ms",
189  this, last_resolution_ago.millis(),
190  time_until_next_resolution.millis());
191  }
193  Ref(DEBUG_LOCATION, "next_resolution_timer_cooldown").release();
196  ExecCtx::Get()->Now() + time_until_next_resolution,
198  return;
199  }
200  }
202 }
203 
207  if (GPR_UNLIKELY(tracer_ != nullptr && tracer_->enabled())) {
208  gpr_log(GPR_INFO, "[polling resolver %p] starting resolution, request_=%p",
209  this, request_.get());
210  }
211 }
212 
213 } // namespace grpc_core
GRPC_CLOSURE_INIT
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
Definition: closure.h:115
grpc_core::PollingResolver::channel_args_
const grpc_channel_args * channel_args_
channel args
Definition: polling_resolver.h:89
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
backoff.h
grpc_core::PollingResolver::StartLocked
void StartLocked() override
Starts resolving.
Definition: polling_resolver.cc:69
grpc_core::PollingResolver::next_resolution_timer_
grpc_timer next_resolution_timer_
Definition: polling_resolver.h:101
grpc_core::BackOff::NextAttemptTime
Timestamp NextAttemptTime()
Returns the time at which the next attempt should start.
Definition: backoff.cc:31
grpc_core::InternallyRefCounted< Resolver >::Unref
void Unref()
Definition: orphanable.h:100
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::PollingResolver::work_serializer_
std::shared_ptr< WorkSerializer > work_serializer_
Definition: polling_resolver.h:90
grpc_core::PollingResolver::request_
OrphanablePtr< Orphanable > request_
are we currently resolving?
Definition: polling_resolver.h:98
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
error
grpc_error_handle error
Definition: retry_filter.cc:499
check_documentation.path
path
Definition: check_documentation.py:57
grpc_channel_args
Definition: grpc_types.h:132
grpc_core::PollingResolver::OnNextResolutionLocked
void OnNextResolutionLocked(grpc_error_handle error)
Definition: polling_resolver.cc:102
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_core::PollingResolver::OnNextResolution
static void OnNextResolution(void *arg, grpc_error_handle error)
Definition: polling_resolver.cc:95
grpc_core::PollingResolver::StartRequest
virtual OrphanablePtr< Orphanable > StartRequest()=0
grpc_core::PollingResolver::PollingResolver
PollingResolver(ResolverArgs args, const grpc_channel_args *channel_args, Duration min_time_between_resolutions, BackOff::Options backoff_options, TraceFlag *tracer)
Definition: polling_resolver.cc:43
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::PollingResolver::RequestReresolutionLocked
void RequestReresolutionLocked() override
Definition: polling_resolver.cc:71
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
grpc_core::InternallyRefCounted< Resolver >::Ref
RefCountedPtr< Resolver > 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
work_serializer.h
channel_args_
const grpc_channel_args * channel_args_
Definition: rls.cc:710
GPR_UNLIKELY
#define GPR_UNLIKELY(x)
Definition: impl/codegen/port_platform.h:770
grpc_core::Resolver::Result
Results returned by the resolver.
Definition: resolver/resolver.h:56
grpc_core::PollingResolver::backoff_
BackOff backoff_
retry backoff state
Definition: polling_resolver.h:108
grpc_core::ResolverArgs
Definition: resolver_factory.h:41
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
arg
Definition: cmdline.cc:40
grpc_core::PollingResolver::MaybeStartResolvingLocked
void MaybeStartResolvingLocked()
Definition: polling_resolver.cc:168
grpc_channel_args_copy
grpc_channel_args * grpc_channel_args_copy(const grpc_channel_args *src)
Definition: channel_args.cc:285
grpc_core::PollingResolver::OnRequestComplete
void OnRequestComplete(Result result)
Definition: polling_resolver.cc:117
result_handler_
std::unique_ptr< ResultHandler > result_handler_
Definition: sockaddr_resolver.cc:59
grpc_core::PollingResolver
Definition: polling_resolver.h:46
grpc_core::PollingResolver::min_time_between_resolutions_
Duration min_time_between_resolutions_
min time between DNS requests
Definition: polling_resolver.h:104
grpc_core::TraceFlag
Definition: debug/trace.h:63
grpc_core::PollingResolver::ResetBackoffLocked
void ResetBackoffLocked() override
Definition: polling_resolver.cc:77
grpc_core::TraceFlag::enabled
bool enabled()
Definition: debug/trace.h:82
grpc_core::Duration::Zero
static constexpr Duration Zero()
Definition: src/core/lib/gprpp/time.h:130
grpc_timer_cancel
void grpc_timer_cancel(grpc_timer *timer)
Definition: iomgr/timer.cc:36
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
grpc_core::Duration::millis
constexpr int64_t millis() const
Definition: src/core/lib/gprpp/time.h:208
GRPC_ERROR_REF
#define GRPC_ERROR_REF(err)
Definition: error.h:261
debug_location.h
grpc_core::PollingResolver::result_handler_
std::unique_ptr< ResultHandler > result_handler_
Definition: polling_resolver.h:91
polling_resolver.h
grpc_core::PollingResolver::StartResolvingLocked
void StartResolvingLocked()
Definition: polling_resolver.cc:204
grpc_core::PollingResolver::~PollingResolver
~PollingResolver() override
Definition: polling_resolver.cc:62
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
grpc_core::PollingResolver::on_next_resolution_
grpc_closure on_next_resolution_
Definition: polling_resolver.h:102
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::BackOff::Reset
void Reset()
Definition: backoff.cc:44
arg
struct arg arg
exec_ctx.h
grpc_timer_init
void grpc_timer_init(grpc_timer *timer, grpc_core::Timestamp deadline, grpc_closure *closure)
Definition: iomgr/timer.cc:31
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::PollingResolver::last_resolution_timestamp_
absl::optional< Timestamp > last_resolution_timestamp_
timestamp of last DNS request
Definition: polling_resolver.h:106
ref_counted_ptr.h
grpc_core::PollingResolver::ShutdownLocked
void ShutdownLocked() override
Shuts down the resolver.
Definition: polling_resolver.cc:84
channel_args.h
timer.h
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
work_serializer_
std::shared_ptr< WorkSerializer > work_serializer_
Definition: google_c2p_resolver.cc:134
uri_parser.h
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_error
Definition: error_internal.h:42
grpc_core::PollingResolver::OnRequestCompleteLocked
void OnRequestCompleteLocked(Result result)
Definition: polling_resolver.cc:124
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
grpc_core::BackOff::Options
Definition: backoff.h:46
grpc_core::PollingResolver::shutdown_
bool shutdown_
are we shutting down?
Definition: polling_resolver.h:96
grpc_core::PollingResolver::have_next_resolution_timer_
bool have_next_resolution_timer_
next resolution timer
Definition: polling_resolver.h:100
grpc_core::PollingResolver::tracer_
TraceFlag * tracer_
Definition: polling_resolver.h:92
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
interested_parties_
grpc_pollset_set * interested_parties_
Definition: oob_backend_metric.cc:169
grpc_core::ExecCtx::InvalidateNow
void InvalidateNow()
Definition: exec_ctx.h:188
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
grpc_generator::StripPrefix
bool StripPrefix(std::string *name, const std::string &prefix)
Definition: generator_helpers.h:44
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:44