Imported Upstream version 1.33.1
[platform/upstream/grpc.git] / src / core / ext / filters / client_channel / resolver / dns / native / dns_resolver.cc
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
19 #include <grpc/support/port_platform.h>
20
21 #include <inttypes.h>
22 #include <climits>
23 #include <cstring>
24
25 #include "absl/strings/str_cat.h"
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/string_util.h>
29 #include <grpc/support/time.h>
30
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"
41
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
46
47 namespace grpc_core {
48
49 namespace {
50
51 const char kDefaultPort[] = "https";
52
53 class NativeDnsResolver : public Resolver {
54  public:
55   explicit NativeDnsResolver(ResolverArgs args);
56
57   void StartLocked() override;
58
59   void RequestReresolutionLocked() override;
60
61   void ResetBackoffLocked() override;
62
63   void ShutdownLocked() override;
64
65  private:
66   virtual ~NativeDnsResolver();
67
68   void MaybeStartResolvingLocked();
69   void StartResolvingLocked();
70
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);
75
76   /// name to resolve
77   char* name_to_resolve_ = nullptr;
78   /// channel args
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
96   BackOff backoff_;
97   /// currently resolving addresses
98   grpc_resolved_addresses* addresses_ = nullptr;
99 };
100
101 NativeDnsResolver::NativeDnsResolver(ResolverArgs args)
102     : Resolver(std::move(args.work_serializer), std::move(args.result_handler)),
103       backoff_(
104           BackOff::Options()
105               .set_initial_backoff(GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS *
106                                    1000)
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);
121   }
122 }
123
124 NativeDnsResolver::~NativeDnsResolver() {
125   grpc_channel_args_destroy(channel_args_);
126   grpc_pollset_set_destroy(interested_parties_);
127   gpr_free(name_to_resolve_);
128 }
129
130 void NativeDnsResolver::StartLocked() { MaybeStartResolvingLocked(); }
131
132 void NativeDnsResolver::RequestReresolutionLocked() {
133   if (!resolving_) {
134     MaybeStartResolvingLocked();
135   }
136 }
137
138 void NativeDnsResolver::ResetBackoffLocked() {
139   if (have_next_resolution_timer_) {
140     grpc_timer_cancel(&next_resolution_timer_);
141   }
142   backoff_.Reset();
143 }
144
145 void NativeDnsResolver::ShutdownLocked() {
146   shutdown_ = true;
147   if (have_next_resolution_timer_) {
148     grpc_timer_cancel(&next_resolution_timer_);
149   }
150 }
151
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); },
156                             DEBUG_LOCATION);
157 }
158
159 void NativeDnsResolver::OnNextResolutionLocked(grpc_error* error) {
160   have_next_resolution_timer_ = false;
161   if (error == GRPC_ERROR_NONE && !resolving_) {
162     StartResolvingLocked();
163   }
164   Unref(DEBUG_LOCATION, "retry-timer");
165   GRPC_ERROR_UNREF(error);
166 }
167
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); },
172                             DEBUG_LOCATION);
173 }
174
175 void NativeDnsResolver::OnResolvedLocked(grpc_error* error) {
176   GPR_ASSERT(resolving_);
177   resolving_ = false;
178   if (shutdown_) {
179     Unref(DEBUG_LOCATION, "dns-resolving");
180     GRPC_ERROR_UNREF(error);
181     return;
182   }
183   if (addresses_ != nullptr) {
184     Result result;
185     for (size_t i = 0; i < addresses_->naddrs; ++i) {
186       result.addresses.emplace_back(&addresses_->addrs[i].addr,
187                                     addresses_->addrs[i].len,
188                                     nullptr /* args */);
189     }
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.
195     backoff_.Reset();
196   } else {
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(),
204                                                          &error, 1),
205         GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_UNAVAILABLE));
206     // Set up for retry.
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();
215     if (timeout > 0) {
216       gpr_log(GPR_DEBUG, "retrying in %" PRId64 " milliseconds", timeout);
217     } else {
218       gpr_log(GPR_DEBUG, "retrying immediately");
219     }
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_);
223   }
224   Unref(DEBUG_LOCATION, "dns-resolving");
225   GRPC_ERROR_UNREF(error);
226 }
227
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_;
240       gpr_log(GPR_DEBUG,
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_);
255       return;
256     }
257   }
258   StartResolvingLocked();
259 }
260
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_);
268   resolving_ = true;
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();
275 }
276
277 //
278 // Factory
279 //
280
281 class NativeDnsResolverFactory : public ResolverFactory {
282  public:
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");
286       return false;
287     }
288     return true;
289   }
290
291   OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
292     if (!IsValidUri(args.uri)) return nullptr;
293     return MakeOrphanable<NativeDnsResolver>(std::move(args));
294   }
295
296   const char* scheme() const override { return "dns"; }
297 };
298
299 }  // namespace
300
301 }  // namespace grpc_core
302
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>());
310   } else {
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>());
318     }
319   }
320 }
321
322 void grpc_resolver_dns_native_shutdown() {}