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 <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
27 #include <grpc/support/time.h>
29 #include "src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
30 #include "src/core/ext/filters/client_channel/resolver_registry.h"
31 #include "src/core/ext/filters/client_channel/server_address.h"
32 #include "src/core/lib/backoff/backoff.h"
33 #include "src/core/lib/channel/channel_args.h"
34 #include "src/core/lib/gpr/string.h"
35 #include "src/core/lib/gprpp/manual_constructor.h"
36 #include "src/core/lib/iomgr/combiner.h"
37 #include "src/core/lib/iomgr/resolve_address.h"
38 #include "src/core/lib/iomgr/timer.h"
40 #define GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS 1
41 #define GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER 1.6
42 #define GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS 120
43 #define GRPC_DNS_RECONNECT_JITTER 0.2
49 const char kDefaultPort[] = "https";
51 class NativeDnsResolver : public Resolver {
53 explicit NativeDnsResolver(ResolverArgs args);
55 void StartLocked() override;
57 void RequestReresolutionLocked() override;
59 void ResetBackoffLocked() override;
61 void ShutdownLocked() override;
64 virtual ~NativeDnsResolver();
66 void MaybeStartResolvingLocked();
67 void StartResolvingLocked();
69 static void OnNextResolutionLocked(void* arg, grpc_error* error);
70 static void OnResolvedLocked(void* arg, grpc_error* error);
73 char* name_to_resolve_ = nullptr;
75 grpc_channel_args* channel_args_ = nullptr;
76 /// pollset_set to drive the name resolution process
77 grpc_pollset_set* interested_parties_ = nullptr;
78 /// are we shutting down?
79 bool shutdown_ = false;
80 /// are we currently resolving?
81 bool resolving_ = false;
82 grpc_closure on_resolved_;
83 /// next resolution timer
84 bool have_next_resolution_timer_ = false;
85 grpc_timer next_resolution_timer_;
86 grpc_closure on_next_resolution_;
87 /// min time between DNS requests
88 grpc_millis min_time_between_resolutions_;
89 /// timestamp of last DNS request
90 grpc_millis last_resolution_timestamp_ = -1;
91 /// retry backoff state
93 /// currently resolving addresses
94 grpc_resolved_addresses* addresses_ = nullptr;
97 NativeDnsResolver::NativeDnsResolver(ResolverArgs args)
98 : Resolver(args.combiner, std::move(args.result_handler)),
101 .set_initial_backoff(GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS *
103 .set_multiplier(GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER)
104 .set_jitter(GRPC_DNS_RECONNECT_JITTER)
105 .set_max_backoff(GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS * 1000)) {
106 char* path = args.uri->path;
107 if (path[0] == '/') ++path;
108 name_to_resolve_ = gpr_strdup(path);
109 channel_args_ = grpc_channel_args_copy(args.args);
110 const grpc_arg* arg = grpc_channel_args_find(
111 args.args, GRPC_ARG_DNS_MIN_TIME_BETWEEN_RESOLUTIONS_MS);
112 min_time_between_resolutions_ =
113 grpc_channel_arg_get_integer(arg, {1000 * 30, 0, INT_MAX});
114 interested_parties_ = grpc_pollset_set_create();
115 if (args.pollset_set != nullptr) {
116 grpc_pollset_set_add_pollset_set(interested_parties_, args.pollset_set);
118 GRPC_CLOSURE_INIT(&on_next_resolution_,
119 NativeDnsResolver::OnNextResolutionLocked, this,
120 grpc_combiner_scheduler(args.combiner));
121 GRPC_CLOSURE_INIT(&on_resolved_, NativeDnsResolver::OnResolvedLocked, this,
122 grpc_combiner_scheduler(args.combiner));
125 NativeDnsResolver::~NativeDnsResolver() {
126 grpc_channel_args_destroy(channel_args_);
127 grpc_pollset_set_destroy(interested_parties_);
128 gpr_free(name_to_resolve_);
131 void NativeDnsResolver::StartLocked() { MaybeStartResolvingLocked(); }
133 void NativeDnsResolver::RequestReresolutionLocked() {
135 MaybeStartResolvingLocked();
139 void NativeDnsResolver::ResetBackoffLocked() {
140 if (have_next_resolution_timer_) {
141 grpc_timer_cancel(&next_resolution_timer_);
146 void NativeDnsResolver::ShutdownLocked() {
148 if (have_next_resolution_timer_) {
149 grpc_timer_cancel(&next_resolution_timer_);
153 void NativeDnsResolver::OnNextResolutionLocked(void* arg, grpc_error* error) {
154 NativeDnsResolver* r = static_cast<NativeDnsResolver*>(arg);
155 r->have_next_resolution_timer_ = false;
156 if (error == GRPC_ERROR_NONE && !r->resolving_) {
157 r->StartResolvingLocked();
159 r->Unref(DEBUG_LOCATION, "retry-timer");
162 void NativeDnsResolver::OnResolvedLocked(void* arg, grpc_error* error) {
163 NativeDnsResolver* r = static_cast<NativeDnsResolver*>(arg);
164 GPR_ASSERT(r->resolving_);
165 r->resolving_ = false;
167 r->Unref(DEBUG_LOCATION, "dns-resolving");
170 if (r->addresses_ != nullptr) {
172 for (size_t i = 0; i < r->addresses_->naddrs; ++i) {
173 result.addresses.emplace_back(&r->addresses_->addrs[i].addr,
174 r->addresses_->addrs[i].len,
177 grpc_resolved_addresses_destroy(r->addresses_);
178 result.args = grpc_channel_args_copy(r->channel_args_);
179 r->result_handler()->ReturnResult(std::move(result));
180 // Reset backoff state so that we start from the beginning when the
181 // next request gets triggered.
184 gpr_log(GPR_INFO, "dns resolution failed (will retry): %s",
185 grpc_error_string(error));
186 // Return transient error.
187 r->result_handler()->ReturnError(grpc_error_set_int(
188 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
189 "DNS resolution failed", &error, 1),
190 GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_UNAVAILABLE));
192 grpc_millis next_try = r->backoff_.NextAttemptTime();
193 grpc_millis timeout = next_try - ExecCtx::Get()->Now();
194 GPR_ASSERT(!r->have_next_resolution_timer_);
195 r->have_next_resolution_timer_ = true;
196 // TODO(roth): We currently deal with this ref manually. Once the
197 // new closure API is done, find a way to track this ref with the timer
198 // callback as part of the type system.
199 r->Ref(DEBUG_LOCATION, "next_resolution_timer").release();
201 gpr_log(GPR_DEBUG, "retrying in %" PRId64 " milliseconds", timeout);
203 gpr_log(GPR_DEBUG, "retrying immediately");
205 grpc_timer_init(&r->next_resolution_timer_, next_try,
206 &r->on_next_resolution_);
208 r->Unref(DEBUG_LOCATION, "dns-resolving");
211 void NativeDnsResolver::MaybeStartResolvingLocked() {
212 // If there is an existing timer, the time it fires is the earliest time we
213 // can start the next resolution.
214 if (have_next_resolution_timer_) return;
215 if (last_resolution_timestamp_ >= 0) {
216 const grpc_millis earliest_next_resolution =
217 last_resolution_timestamp_ + min_time_between_resolutions_;
218 const grpc_millis ms_until_next_resolution =
219 earliest_next_resolution - grpc_core::ExecCtx::Get()->Now();
220 if (ms_until_next_resolution > 0) {
221 const grpc_millis last_resolution_ago =
222 grpc_core::ExecCtx::Get()->Now() - last_resolution_timestamp_;
224 "In cooldown from last resolution (from %" PRId64
225 " ms ago). Will resolve again in %" PRId64 " ms",
226 last_resolution_ago, ms_until_next_resolution);
227 have_next_resolution_timer_ = true;
228 // TODO(roth): We currently deal with this ref manually. Once the
229 // new closure API is done, find a way to track this ref with the timer
230 // callback as part of the type system.
231 Ref(DEBUG_LOCATION, "next_resolution_timer_cooldown").release();
232 grpc_timer_init(&next_resolution_timer_,
233 ExecCtx::Get()->Now() + ms_until_next_resolution,
234 &on_next_resolution_);
238 StartResolvingLocked();
241 void NativeDnsResolver::StartResolvingLocked() {
242 gpr_log(GPR_DEBUG, "Start resolving.");
243 // TODO(roth): We currently deal with this ref manually. Once the
244 // new closure API is done, find a way to track this ref with the timer
245 // callback as part of the type system.
246 Ref(DEBUG_LOCATION, "dns-resolving").release();
247 GPR_ASSERT(!resolving_);
249 addresses_ = nullptr;
250 grpc_resolve_address(name_to_resolve_, kDefaultPort, interested_parties_,
251 &on_resolved_, &addresses_);
252 last_resolution_timestamp_ = grpc_core::ExecCtx::Get()->Now();
259 class NativeDnsResolverFactory : public ResolverFactory {
261 bool IsValidUri(const grpc_uri* uri) const override {
262 if (GPR_UNLIKELY(0 != strcmp(uri->authority, ""))) {
263 gpr_log(GPR_ERROR, "authority based dns uri's not supported");
269 OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
270 if (!IsValidUri(args.uri)) return nullptr;
271 return OrphanablePtr<Resolver>(New<NativeDnsResolver>(std::move(args)));
274 const char* scheme() const override { return "dns"; }
279 } // namespace grpc_core
281 void grpc_resolver_dns_native_init() {
282 grpc_core::UniquePtr<char> resolver =
283 GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
284 if (gpr_stricmp(resolver.get(), "native") == 0) {
285 gpr_log(GPR_DEBUG, "Using native dns resolver");
286 grpc_core::ResolverRegistry::Builder::RegisterResolverFactory(
287 grpc_core::UniquePtr<grpc_core::ResolverFactory>(
288 grpc_core::New<grpc_core::NativeDnsResolverFactory>()));
290 grpc_core::ResolverRegistry::Builder::InitRegistry();
291 grpc_core::ResolverFactory* existing_factory =
292 grpc_core::ResolverRegistry::LookupResolverFactory("dns");
293 if (existing_factory == nullptr) {
294 gpr_log(GPR_DEBUG, "Using native dns resolver");
295 grpc_core::ResolverRegistry::Builder::RegisterResolverFactory(
296 grpc_core::UniquePtr<grpc_core::ResolverFactory>(
297 grpc_core::New<grpc_core::NativeDnsResolverFactory>()));
302 void grpc_resolver_dns_native_shutdown() {}