3 * Copyright 2015 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 #include <grpc/support/port_platform.h>
25 #include "absl/strings/str_cat.h"
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/string_util.h>
29 #include <grpc/support/time.h>
31 #include "src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
32 #include "src/core/ext/filters/client_channel/resolver_registry.h"
33 #include "src/core/ext/filters/client_channel/server_address.h"
34 #include "src/core/lib/backoff/backoff.h"
35 #include "src/core/lib/channel/channel_args.h"
36 #include "src/core/lib/gpr/string.h"
37 #include "src/core/lib/gprpp/manual_constructor.h"
38 #include "src/core/lib/iomgr/resolve_address.h"
39 #include "src/core/lib/iomgr/timer.h"
40 #include "src/core/lib/iomgr/work_serializer.h"
42 #define GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS 1
43 #define GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER 1.6
44 #define GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS 120
45 #define GRPC_DNS_RECONNECT_JITTER 0.2
51 const char kDefaultPort[] = "https";
53 class NativeDnsResolver : public Resolver {
55 explicit NativeDnsResolver(ResolverArgs args);
57 void StartLocked() override;
59 void RequestReresolutionLocked() override;
61 void ResetBackoffLocked() override;
63 void ShutdownLocked() override;
66 virtual ~NativeDnsResolver();
68 void MaybeStartResolvingLocked();
69 void StartResolvingLocked();
71 static void OnNextResolution(void* arg, grpc_error* error);
72 void OnNextResolutionLocked(grpc_error* error);
73 static void OnResolved(void* arg, grpc_error* error);
74 void OnResolvedLocked(grpc_error* error);
77 char* name_to_resolve_ = nullptr;
79 grpc_channel_args* channel_args_ = nullptr;
80 /// pollset_set to drive the name resolution process
81 grpc_pollset_set* interested_parties_ = nullptr;
82 /// are we shutting down?
83 bool shutdown_ = false;
84 /// are we currently resolving?
85 bool resolving_ = false;
86 grpc_closure on_resolved_;
87 /// next resolution timer
88 bool have_next_resolution_timer_ = false;
89 grpc_timer next_resolution_timer_;
90 grpc_closure on_next_resolution_;
91 /// min time between DNS requests
92 grpc_millis min_time_between_resolutions_;
93 /// timestamp of last DNS request
94 grpc_millis last_resolution_timestamp_ = -1;
95 /// retry backoff state
97 /// currently resolving addresses
98 grpc_resolved_addresses* addresses_ = nullptr;
101 NativeDnsResolver::NativeDnsResolver(ResolverArgs args)
102 : Resolver(std::move(args.work_serializer), std::move(args.result_handler)),
105 .set_initial_backoff(GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS *
107 .set_multiplier(GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER)
108 .set_jitter(GRPC_DNS_RECONNECT_JITTER)
109 .set_max_backoff(GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS * 1000)) {
110 char* path = args.uri->path;
111 if (path[0] == '/') ++path;
112 name_to_resolve_ = gpr_strdup(path);
113 channel_args_ = grpc_channel_args_copy(args.args);
114 const grpc_arg* arg = grpc_channel_args_find(
115 args.args, GRPC_ARG_DNS_MIN_TIME_BETWEEN_RESOLUTIONS_MS);
116 min_time_between_resolutions_ =
117 grpc_channel_arg_get_integer(arg, {1000 * 30, 0, INT_MAX});
118 interested_parties_ = grpc_pollset_set_create();
119 if (args.pollset_set != nullptr) {
120 grpc_pollset_set_add_pollset_set(interested_parties_, args.pollset_set);
124 NativeDnsResolver::~NativeDnsResolver() {
125 grpc_channel_args_destroy(channel_args_);
126 grpc_pollset_set_destroy(interested_parties_);
127 gpr_free(name_to_resolve_);
130 void NativeDnsResolver::StartLocked() { MaybeStartResolvingLocked(); }
132 void NativeDnsResolver::RequestReresolutionLocked() {
134 MaybeStartResolvingLocked();
138 void NativeDnsResolver::ResetBackoffLocked() {
139 if (have_next_resolution_timer_) {
140 grpc_timer_cancel(&next_resolution_timer_);
145 void NativeDnsResolver::ShutdownLocked() {
147 if (have_next_resolution_timer_) {
148 grpc_timer_cancel(&next_resolution_timer_);
152 void NativeDnsResolver::OnNextResolution(void* arg, grpc_error* error) {
153 NativeDnsResolver* r = static_cast<NativeDnsResolver*>(arg);
154 GRPC_ERROR_REF(error); // ref owned by lambda
155 r->work_serializer()->Run([r, error]() { r->OnNextResolutionLocked(error); },
159 void NativeDnsResolver::OnNextResolutionLocked(grpc_error* error) {
160 have_next_resolution_timer_ = false;
161 if (error == GRPC_ERROR_NONE && !resolving_) {
162 StartResolvingLocked();
164 Unref(DEBUG_LOCATION, "retry-timer");
165 GRPC_ERROR_UNREF(error);
168 void NativeDnsResolver::OnResolved(void* arg, grpc_error* error) {
169 NativeDnsResolver* r = static_cast<NativeDnsResolver*>(arg);
170 GRPC_ERROR_REF(error); // owned by lambda
171 r->work_serializer()->Run([r, error]() { r->OnResolvedLocked(error); },
175 void NativeDnsResolver::OnResolvedLocked(grpc_error* error) {
176 GPR_ASSERT(resolving_);
179 Unref(DEBUG_LOCATION, "dns-resolving");
180 GRPC_ERROR_UNREF(error);
183 if (addresses_ != nullptr) {
185 for (size_t i = 0; i < addresses_->naddrs; ++i) {
186 result.addresses.emplace_back(&addresses_->addrs[i].addr,
187 addresses_->addrs[i].len,
190 grpc_resolved_addresses_destroy(addresses_);
191 result.args = grpc_channel_args_copy(channel_args_);
192 result_handler()->ReturnResult(std::move(result));
193 // Reset backoff state so that we start from the beginning when the
194 // next request gets triggered.
197 gpr_log(GPR_INFO, "dns resolution failed (will retry): %s",
198 grpc_error_string(error));
199 // Return transient error.
200 std::string error_message =
201 absl::StrCat("DNS resolution failed for service: ", name_to_resolve_);
202 result_handler()->ReturnError(grpc_error_set_int(
203 GRPC_ERROR_CREATE_REFERENCING_FROM_COPIED_STRING(error_message.c_str(),
205 GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_UNAVAILABLE));
207 grpc_millis next_try = backoff_.NextAttemptTime();
208 grpc_millis timeout = next_try - ExecCtx::Get()->Now();
209 GPR_ASSERT(!have_next_resolution_timer_);
210 have_next_resolution_timer_ = true;
211 // TODO(roth): We currently deal with this ref manually. Once the
212 // new closure API is done, find a way to track this ref with the timer
213 // callback as part of the type system.
214 Ref(DEBUG_LOCATION, "next_resolution_timer").release();
216 gpr_log(GPR_DEBUG, "retrying in %" PRId64 " milliseconds", timeout);
218 gpr_log(GPR_DEBUG, "retrying immediately");
220 GRPC_CLOSURE_INIT(&on_next_resolution_, NativeDnsResolver::OnNextResolution,
221 this, grpc_schedule_on_exec_ctx);
222 grpc_timer_init(&next_resolution_timer_, next_try, &on_next_resolution_);
224 Unref(DEBUG_LOCATION, "dns-resolving");
225 GRPC_ERROR_UNREF(error);
228 void NativeDnsResolver::MaybeStartResolvingLocked() {
229 // If there is an existing timer, the time it fires is the earliest time we
230 // can start the next resolution.
231 if (have_next_resolution_timer_) return;
232 if (last_resolution_timestamp_ >= 0) {
233 const grpc_millis earliest_next_resolution =
234 last_resolution_timestamp_ + min_time_between_resolutions_;
235 const grpc_millis ms_until_next_resolution =
236 earliest_next_resolution - grpc_core::ExecCtx::Get()->Now();
237 if (ms_until_next_resolution > 0) {
238 const grpc_millis last_resolution_ago =
239 grpc_core::ExecCtx::Get()->Now() - last_resolution_timestamp_;
241 "In cooldown from last resolution (from %" PRId64
242 " ms ago). Will resolve again in %" PRId64 " ms",
243 last_resolution_ago, ms_until_next_resolution);
244 have_next_resolution_timer_ = true;
245 // TODO(roth): We currently deal with this ref manually. Once the
246 // new closure API is done, find a way to track this ref with the timer
247 // callback as part of the type system.
248 Ref(DEBUG_LOCATION, "next_resolution_timer_cooldown").release();
249 GRPC_CLOSURE_INIT(&on_next_resolution_,
250 NativeDnsResolver::OnNextResolution, this,
251 grpc_schedule_on_exec_ctx);
252 grpc_timer_init(&next_resolution_timer_,
253 ExecCtx::Get()->Now() + ms_until_next_resolution,
254 &on_next_resolution_);
258 StartResolvingLocked();
261 void NativeDnsResolver::StartResolvingLocked() {
262 gpr_log(GPR_DEBUG, "Start resolving.");
263 // TODO(roth): We currently deal with this ref manually. Once the
264 // new closure API is done, find a way to track this ref with the timer
265 // callback as part of the type system.
266 Ref(DEBUG_LOCATION, "dns-resolving").release();
267 GPR_ASSERT(!resolving_);
269 addresses_ = nullptr;
270 GRPC_CLOSURE_INIT(&on_resolved_, NativeDnsResolver::OnResolved, this,
271 grpc_schedule_on_exec_ctx);
272 grpc_resolve_address(name_to_resolve_, kDefaultPort, interested_parties_,
273 &on_resolved_, &addresses_);
274 last_resolution_timestamp_ = grpc_core::ExecCtx::Get()->Now();
281 class NativeDnsResolverFactory : public ResolverFactory {
283 bool IsValidUri(const grpc_uri* uri) const override {
284 if (GPR_UNLIKELY(0 != strcmp(uri->authority, ""))) {
285 gpr_log(GPR_ERROR, "authority based dns uri's not supported");
291 OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
292 if (!IsValidUri(args.uri)) return nullptr;
293 return MakeOrphanable<NativeDnsResolver>(std::move(args));
296 const char* scheme() const override { return "dns"; }
301 } // namespace grpc_core
303 void grpc_resolver_dns_native_init() {
304 grpc_core::UniquePtr<char> resolver =
305 GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
306 if (gpr_stricmp(resolver.get(), "native") == 0) {
307 gpr_log(GPR_DEBUG, "Using native dns resolver");
308 grpc_core::ResolverRegistry::Builder::RegisterResolverFactory(
309 absl::make_unique<grpc_core::NativeDnsResolverFactory>());
311 grpc_core::ResolverRegistry::Builder::InitRegistry();
312 grpc_core::ResolverFactory* existing_factory =
313 grpc_core::ResolverRegistry::LookupResolverFactory("dns");
314 if (existing_factory == nullptr) {
315 gpr_log(GPR_DEBUG, "Using native dns resolver");
316 grpc_core::ResolverRegistry::Builder::RegisterResolverFactory(
317 absl::make_unique<grpc_core::NativeDnsResolverFactory>());
322 void grpc_resolver_dns_native_shutdown() {}