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