Imported Upstream version 1.26.0
[platform/upstream/grpc.git] / test / core / client_channel / resolvers / dns_resolver_cooldown_test.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 <cstring>
20
21 #include <grpc/grpc.h>
22 #include <grpc/support/log.h>
23
24 #include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
25 #include "src/core/ext/filters/client_channel/resolver_registry.h"
26 #include "src/core/ext/filters/client_channel/server_address.h"
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gprpp/memory.h"
29 #include "src/core/lib/iomgr/combiner.h"
30 #include "src/core/lib/iomgr/sockaddr_utils.h"
31 #include "test/core/util/test_config.h"
32
33 constexpr int kMinResolutionPeriodMs = 1000;
34 // Provide some slack when checking intervals, to allow for test timing issues.
35 constexpr int kMinResolutionPeriodForCheckMs = 900;
36
37 extern grpc_address_resolver_vtable* grpc_resolve_address_impl;
38 static grpc_address_resolver_vtable* default_resolve_address;
39
40 static grpc_core::Combiner* g_combiner;
41
42 static grpc_ares_request* (*g_default_dns_lookup_ares_locked)(
43     const char* dns_server, const char* name, const char* default_port,
44     grpc_pollset_set* interested_parties, grpc_closure* on_done,
45     std::unique_ptr<grpc_core::ServerAddressList>* addresses, bool check_grpclb,
46     char** service_config_json, int query_timeout_ms,
47     grpc_core::Combiner* combiner);
48
49 // Counter incremented by test_resolve_address_impl indicating the number of
50 // times a system-level resolution has happened.
51 static int g_resolution_count;
52
53 static struct iomgr_args {
54   gpr_event ev;
55   gpr_atm done_atm;
56   gpr_mu* mu;
57   grpc_pollset* pollset;
58   grpc_pollset_set* pollset_set;
59 } g_iomgr_args;
60
61 // Wrapper around default resolve_address in order to count the number of
62 // times we incur in a system-level name resolution.
63 static void test_resolve_address_impl(const char* name,
64                                       const char* default_port,
65                                       grpc_pollset_set* /*interested_parties*/,
66                                       grpc_closure* on_done,
67                                       grpc_resolved_addresses** addrs) {
68   default_resolve_address->resolve_address(
69       name, default_port, g_iomgr_args.pollset_set, on_done, addrs);
70   ++g_resolution_count;
71   static grpc_millis last_resolution_time = 0;
72   if (last_resolution_time == 0) {
73     last_resolution_time =
74         grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
75   } else {
76     grpc_millis now =
77         grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
78     GPR_ASSERT(now - last_resolution_time >= kMinResolutionPeriodForCheckMs);
79     last_resolution_time = now;
80   }
81 }
82
83 static grpc_error* test_blocking_resolve_address_impl(
84     const char* name, const char* default_port,
85     grpc_resolved_addresses** addresses) {
86   return default_resolve_address->blocking_resolve_address(name, default_port,
87                                                            addresses);
88 }
89
90 static grpc_address_resolver_vtable test_resolver = {
91     test_resolve_address_impl, test_blocking_resolve_address_impl};
92
93 static grpc_ares_request* test_dns_lookup_ares_locked(
94     const char* dns_server, const char* name, const char* default_port,
95     grpc_pollset_set* /*interested_parties*/, grpc_closure* on_done,
96     std::unique_ptr<grpc_core::ServerAddressList>* addresses, bool check_grpclb,
97     char** service_config_json, int query_timeout_ms,
98     grpc_core::Combiner* combiner) {
99   grpc_ares_request* result = g_default_dns_lookup_ares_locked(
100       dns_server, name, default_port, g_iomgr_args.pollset_set, on_done,
101       addresses, check_grpclb, service_config_json, query_timeout_ms, combiner);
102   ++g_resolution_count;
103   static grpc_millis last_resolution_time = 0;
104   grpc_millis now =
105       grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
106   gpr_log(GPR_DEBUG,
107           "last_resolution_time:%" PRId64 " now:%" PRId64
108           " min_time_between:%d",
109           last_resolution_time, now, kMinResolutionPeriodForCheckMs);
110   if (last_resolution_time == 0) {
111     last_resolution_time =
112         grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
113   } else {
114     GPR_ASSERT(now - last_resolution_time >= kMinResolutionPeriodForCheckMs);
115     last_resolution_time = now;
116   }
117   return result;
118 }
119
120 static gpr_timespec test_deadline(void) {
121   return grpc_timeout_seconds_to_deadline(100);
122 }
123
124 static void do_nothing(void* /*arg*/, grpc_error* /*error*/) {}
125
126 static void iomgr_args_init(iomgr_args* args) {
127   gpr_event_init(&args->ev);
128   args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
129   grpc_pollset_init(args->pollset, &args->mu);
130   args->pollset_set = grpc_pollset_set_create();
131   grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
132   gpr_atm_rel_store(&args->done_atm, 0);
133 }
134
135 static void iomgr_args_finish(iomgr_args* args) {
136   GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
137   grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
138   grpc_pollset_set_destroy(args->pollset_set);
139   grpc_closure do_nothing_cb;
140   GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
141                     grpc_schedule_on_exec_ctx);
142   gpr_mu_lock(args->mu);
143   grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
144   gpr_mu_unlock(args->mu);
145   // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
146   grpc_core::ExecCtx::Get()->Flush();
147   grpc_pollset_destroy(args->pollset);
148   gpr_free(args->pollset);
149 }
150
151 static grpc_millis n_sec_deadline(int seconds) {
152   return grpc_timespec_to_millis_round_up(
153       grpc_timeout_seconds_to_deadline(seconds));
154 }
155
156 static void poll_pollset_until_request_done(iomgr_args* args) {
157   grpc_core::ExecCtx exec_ctx;
158   grpc_millis deadline = n_sec_deadline(10);
159   while (true) {
160     bool done = gpr_atm_acq_load(&args->done_atm) != 0;
161     if (done) {
162       break;
163     }
164     grpc_millis time_left = deadline - grpc_core::ExecCtx::Get()->Now();
165     gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, done, time_left);
166     GPR_ASSERT(time_left >= 0);
167     grpc_pollset_worker* worker = nullptr;
168     gpr_mu_lock(args->mu);
169     GRPC_LOG_IF_ERROR("pollset_work", grpc_pollset_work(args->pollset, &worker,
170                                                         n_sec_deadline(1)));
171     gpr_mu_unlock(args->mu);
172     grpc_core::ExecCtx::Get()->Flush();
173   }
174   gpr_event_set(&args->ev, (void*)1);
175 }
176
177 struct OnResolutionCallbackArg;
178
179 class ResultHandler : public grpc_core::Resolver::ResultHandler {
180  public:
181   using ResultCallback = void (*)(OnResolutionCallbackArg* state);
182
183   void SetCallback(ResultCallback result_cb, OnResolutionCallbackArg* state) {
184     GPR_ASSERT(result_cb_ == nullptr);
185     result_cb_ = result_cb;
186     GPR_ASSERT(state_ == nullptr);
187     state_ = state;
188   }
189
190   void ReturnResult(grpc_core::Resolver::Result /*result*/) override {
191     GPR_ASSERT(result_cb_ != nullptr);
192     GPR_ASSERT(state_ != nullptr);
193     ResultCallback cb = result_cb_;
194     OnResolutionCallbackArg* state = state_;
195     result_cb_ = nullptr;
196     state_ = nullptr;
197     cb(state);
198   }
199
200   void ReturnError(grpc_error* error) override {
201     gpr_log(GPR_ERROR, "resolver returned error: %s", grpc_error_string(error));
202     GPR_ASSERT(false);
203   }
204
205  private:
206   ResultCallback result_cb_ = nullptr;
207   OnResolutionCallbackArg* state_ = nullptr;
208 };
209
210 struct OnResolutionCallbackArg {
211   const char* uri_str = nullptr;
212   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver;
213   ResultHandler* result_handler;
214 };
215
216 // Set to true by the last callback in the resolution chain.
217 static bool g_all_callbacks_invoked;
218
219 // It's interesting to run a few rounds of this test because as
220 // we run more rounds, the base starting time
221 // (i.e. ExecCtx g_start_time) gets further and further away
222 // from "Now()". Thus the more rounds ran, the more highlighted the
223 // difference is between absolute and relative times values.
224 static void on_fourth_resolution(OnResolutionCallbackArg* cb_arg) {
225   gpr_log(GPR_INFO, "4th: g_resolution_count: %d", g_resolution_count);
226   GPR_ASSERT(g_resolution_count == 4);
227   cb_arg->resolver.reset();
228   gpr_atm_rel_store(&g_iomgr_args.done_atm, 1);
229   gpr_mu_lock(g_iomgr_args.mu);
230   GRPC_LOG_IF_ERROR("pollset_kick",
231                     grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
232   gpr_mu_unlock(g_iomgr_args.mu);
233   delete cb_arg;
234   g_all_callbacks_invoked = true;
235 }
236
237 static void on_third_resolution(OnResolutionCallbackArg* cb_arg) {
238   gpr_log(GPR_INFO, "3rd: g_resolution_count: %d", g_resolution_count);
239   GPR_ASSERT(g_resolution_count == 3);
240   cb_arg->result_handler->SetCallback(on_fourth_resolution, cb_arg);
241   cb_arg->resolver->RequestReresolutionLocked();
242   gpr_mu_lock(g_iomgr_args.mu);
243   GRPC_LOG_IF_ERROR("pollset_kick",
244                     grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
245   gpr_mu_unlock(g_iomgr_args.mu);
246 }
247
248 static void on_second_resolution(OnResolutionCallbackArg* cb_arg) {
249   gpr_log(GPR_INFO, "2nd: g_resolution_count: %d", g_resolution_count);
250   // The resolution callback was not invoked until new data was
251   // available, which was delayed until after the cooldown period.
252   GPR_ASSERT(g_resolution_count == 2);
253   cb_arg->result_handler->SetCallback(on_third_resolution, cb_arg);
254   cb_arg->resolver->RequestReresolutionLocked();
255   gpr_mu_lock(g_iomgr_args.mu);
256   GRPC_LOG_IF_ERROR("pollset_kick",
257                     grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
258   gpr_mu_unlock(g_iomgr_args.mu);
259 }
260
261 static void on_first_resolution(OnResolutionCallbackArg* cb_arg) {
262   gpr_log(GPR_INFO, "1st: g_resolution_count: %d", g_resolution_count);
263   // There's one initial system-level resolution and one invocation of a
264   // notification callback (the current function).
265   GPR_ASSERT(g_resolution_count == 1);
266   cb_arg->result_handler->SetCallback(on_second_resolution, cb_arg);
267   cb_arg->resolver->RequestReresolutionLocked();
268   gpr_mu_lock(g_iomgr_args.mu);
269   GRPC_LOG_IF_ERROR("pollset_kick",
270                     grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
271   gpr_mu_unlock(g_iomgr_args.mu);
272 }
273
274 static void start_test_under_combiner(void* arg, grpc_error* /*error*/) {
275   OnResolutionCallbackArg* res_cb_arg =
276       static_cast<OnResolutionCallbackArg*>(arg);
277   res_cb_arg->result_handler = new ResultHandler();
278   grpc_core::ResolverFactory* factory =
279       grpc_core::ResolverRegistry::LookupResolverFactory("dns");
280   grpc_uri* uri = grpc_uri_parse(res_cb_arg->uri_str, 0);
281   gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", res_cb_arg->uri_str,
282           factory->scheme());
283   GPR_ASSERT(uri != nullptr);
284   grpc_core::ResolverArgs args;
285   args.uri = uri;
286   args.combiner = g_combiner;
287   args.result_handler = std::unique_ptr<grpc_core::Resolver::ResultHandler>(
288       res_cb_arg->result_handler);
289   g_resolution_count = 0;
290
291   grpc_arg cooldown_arg = grpc_channel_arg_integer_create(
292       const_cast<char*>(GRPC_ARG_DNS_MIN_TIME_BETWEEN_RESOLUTIONS_MS),
293       kMinResolutionPeriodMs);
294   grpc_channel_args cooldown_args = {1, &cooldown_arg};
295   args.args = &cooldown_args;
296   res_cb_arg->resolver = factory->CreateResolver(std::move(args));
297   GPR_ASSERT(res_cb_arg->resolver != nullptr);
298   grpc_uri_destroy(uri);
299   // First resolution, would incur in system-level resolution.
300   res_cb_arg->result_handler->SetCallback(on_first_resolution, res_cb_arg);
301   res_cb_arg->resolver->StartLocked();
302 }
303
304 static void test_cooldown() {
305   grpc_core::ExecCtx exec_ctx;
306   iomgr_args_init(&g_iomgr_args);
307   OnResolutionCallbackArg* res_cb_arg = new OnResolutionCallbackArg();
308   res_cb_arg->uri_str = "dns:127.0.0.1";
309
310   g_combiner->Run(
311       GRPC_CLOSURE_CREATE(start_test_under_combiner, res_cb_arg, nullptr),
312       GRPC_ERROR_NONE);
313   grpc_core::ExecCtx::Get()->Flush();
314   poll_pollset_until_request_done(&g_iomgr_args);
315   iomgr_args_finish(&g_iomgr_args);
316 }
317
318 int main(int argc, char** argv) {
319   grpc::testing::TestEnvironment env(argc, argv);
320   grpc_init();
321
322   g_combiner = grpc_combiner_create();
323
324   g_default_dns_lookup_ares_locked = grpc_dns_lookup_ares_locked;
325   grpc_dns_lookup_ares_locked = test_dns_lookup_ares_locked;
326   default_resolve_address = grpc_resolve_address_impl;
327   grpc_set_resolver_impl(&test_resolver);
328
329   test_cooldown();
330   {
331     grpc_core::ExecCtx exec_ctx;
332     GRPC_COMBINER_UNREF(g_combiner, "test");
333   }
334   grpc_shutdown_blocking();
335   GPR_ASSERT(g_all_callbacks_invoked);
336   return 0;
337 }