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/support/log.h>
22
23 #include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
24 #include "src/core/ext/filters/client_channel/resolver_registry.h"
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/gprpp/memory.h"
27 #include "src/core/lib/iomgr/combiner.h"
28 #include "src/core/lib/iomgr/sockaddr_utils.h"
29 #include "test/core/util/test_config.h"
30
31 constexpr int kMinResolutionPeriodMs = 1000;
32 // Provide some slack when checking intervals, to allow for test timing issues.
33 constexpr int kMinResolutionPeriodForCheckMs = 900;
34
35 extern grpc_address_resolver_vtable* grpc_resolve_address_impl;
36 static grpc_address_resolver_vtable* default_resolve_address;
37
38 static grpc_combiner* g_combiner;
39
40 static grpc_ares_request* (*g_default_dns_lookup_ares_locked)(
41 const char* dns_server, const char* name, const char* default_port,
42 grpc_pollset_set* interested_parties, grpc_closure* on_done,
43 grpc_lb_addresses** addrs, bool check_grpclb, char** service_config_json,
44 grpc_combiner* combiner);
45
46 // Counter incremented by test_resolve_address_impl indicating the number of
47 // times a system-level resolution has happened.
48 static int g_resolution_count;
49
50 static struct iomgr_args {
51 gpr_event ev;
52 gpr_atm done_atm;
53 gpr_mu* mu;
54 grpc_pollset* pollset;
55 grpc_pollset_set* pollset_set;
56 } g_iomgr_args;
57
58 // Wrapper around default resolve_address in order to count the number of
59 // times we incur in a system-level name resolution.
test_resolve_address_impl(const char * name,const char * default_port,grpc_pollset_set * interested_parties,grpc_closure * on_done,grpc_resolved_addresses ** addrs)60 static void test_resolve_address_impl(const char* name,
61 const char* default_port,
62 grpc_pollset_set* interested_parties,
63 grpc_closure* on_done,
64 grpc_resolved_addresses** addrs) {
65 default_resolve_address->resolve_address(
66 name, default_port, g_iomgr_args.pollset_set, on_done, addrs);
67 ++g_resolution_count;
68 static grpc_millis last_resolution_time = 0;
69 if (last_resolution_time == 0) {
70 last_resolution_time =
71 grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
72 } else {
73 grpc_millis now =
74 grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
75 GPR_ASSERT(now - last_resolution_time >= kMinResolutionPeriodForCheckMs);
76 last_resolution_time = now;
77 }
78 }
79
test_blocking_resolve_address_impl(const char * name,const char * default_port,grpc_resolved_addresses ** addresses)80 static grpc_error* test_blocking_resolve_address_impl(
81 const char* name, const char* default_port,
82 grpc_resolved_addresses** addresses) {
83 return default_resolve_address->blocking_resolve_address(name, default_port,
84 addresses);
85 }
86
87 static grpc_address_resolver_vtable test_resolver = {
88 test_resolve_address_impl, test_blocking_resolve_address_impl};
89
test_dns_lookup_ares_locked(const char * dns_server,const char * name,const char * default_port,grpc_pollset_set * interested_parties,grpc_closure * on_done,grpc_lb_addresses ** addrs,bool check_grpclb,char ** service_config_json,grpc_combiner * combiner)90 static grpc_ares_request* test_dns_lookup_ares_locked(
91 const char* dns_server, const char* name, const char* default_port,
92 grpc_pollset_set* interested_parties, grpc_closure* on_done,
93 grpc_lb_addresses** addrs, bool check_grpclb, char** service_config_json,
94 grpc_combiner* combiner) {
95 grpc_ares_request* result = g_default_dns_lookup_ares_locked(
96 dns_server, name, default_port, g_iomgr_args.pollset_set, on_done, addrs,
97 check_grpclb, service_config_json, combiner);
98 ++g_resolution_count;
99 static grpc_millis last_resolution_time = 0;
100 if (last_resolution_time == 0) {
101 last_resolution_time =
102 grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
103 } else {
104 grpc_millis now =
105 grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
106 GPR_ASSERT(now - last_resolution_time >= kMinResolutionPeriodForCheckMs);
107 last_resolution_time = now;
108 }
109 return result;
110 }
111
test_deadline(void)112 static gpr_timespec test_deadline(void) {
113 return grpc_timeout_seconds_to_deadline(100);
114 }
115
do_nothing(void * arg,grpc_error * error)116 static void do_nothing(void* arg, grpc_error* error) {}
117
iomgr_args_init(iomgr_args * args)118 static void iomgr_args_init(iomgr_args* args) {
119 gpr_event_init(&args->ev);
120 args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
121 grpc_pollset_init(args->pollset, &args->mu);
122 args->pollset_set = grpc_pollset_set_create();
123 grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
124 gpr_atm_rel_store(&args->done_atm, 0);
125 }
126
iomgr_args_finish(iomgr_args * args)127 static void iomgr_args_finish(iomgr_args* args) {
128 GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
129 grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
130 grpc_pollset_set_destroy(args->pollset_set);
131 grpc_closure do_nothing_cb;
132 GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
133 grpc_schedule_on_exec_ctx);
134 gpr_mu_lock(args->mu);
135 grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
136 gpr_mu_unlock(args->mu);
137 // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
138 grpc_core::ExecCtx::Get()->Flush();
139 grpc_pollset_destroy(args->pollset);
140 gpr_free(args->pollset);
141 }
142
n_sec_deadline(int seconds)143 static grpc_millis n_sec_deadline(int seconds) {
144 return grpc_timespec_to_millis_round_up(
145 grpc_timeout_seconds_to_deadline(seconds));
146 }
147
poll_pollset_until_request_done(iomgr_args * args)148 static void poll_pollset_until_request_done(iomgr_args* args) {
149 grpc_core::ExecCtx exec_ctx;
150 grpc_millis deadline = n_sec_deadline(10);
151 while (true) {
152 bool done = gpr_atm_acq_load(&args->done_atm) != 0;
153 if (done) {
154 break;
155 }
156 grpc_millis time_left = deadline - grpc_core::ExecCtx::Get()->Now();
157 gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, done, time_left);
158 GPR_ASSERT(time_left >= 0);
159 grpc_pollset_worker* worker = nullptr;
160 gpr_mu_lock(args->mu);
161 GRPC_LOG_IF_ERROR("pollset_work", grpc_pollset_work(args->pollset, &worker,
162 n_sec_deadline(1)));
163 gpr_mu_unlock(args->mu);
164 grpc_core::ExecCtx::Get()->Flush();
165 }
166 gpr_event_set(&args->ev, (void*)1);
167 }
168
169 struct OnResolutionCallbackArg {
170 const char* uri_str = nullptr;
171 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver;
172 grpc_channel_args* result = nullptr;
173 };
174
175 // Set to true by the last callback in the resolution chain.
176 static bool g_all_callbacks_invoked;
177
on_second_resolution(void * arg,grpc_error * error)178 static void on_second_resolution(void* arg, grpc_error* error) {
179 OnResolutionCallbackArg* cb_arg = static_cast<OnResolutionCallbackArg*>(arg);
180 grpc_channel_args_destroy(cb_arg->result);
181 GPR_ASSERT(error == GRPC_ERROR_NONE);
182 gpr_log(GPR_INFO, "2nd: g_resolution_count: %d", g_resolution_count);
183 // The resolution callback was not invoked until new data was
184 // available, which was delayed until after the cooldown period.
185 GPR_ASSERT(g_resolution_count == 2);
186 cb_arg->resolver.reset();
187 gpr_atm_rel_store(&g_iomgr_args.done_atm, 1);
188 gpr_mu_lock(g_iomgr_args.mu);
189 GRPC_LOG_IF_ERROR("pollset_kick",
190 grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
191 gpr_mu_unlock(g_iomgr_args.mu);
192 grpc_core::Delete(cb_arg);
193 g_all_callbacks_invoked = true;
194 }
195
on_first_resolution(void * arg,grpc_error * error)196 static void on_first_resolution(void* arg, grpc_error* error) {
197 OnResolutionCallbackArg* cb_arg = static_cast<OnResolutionCallbackArg*>(arg);
198 grpc_channel_args_destroy(cb_arg->result);
199 GPR_ASSERT(error == GRPC_ERROR_NONE);
200 gpr_log(GPR_INFO, "1st: g_resolution_count: %d", g_resolution_count);
201 // There's one initial system-level resolution and one invocation of a
202 // notification callback (the current function).
203 GPR_ASSERT(g_resolution_count == 1);
204 cb_arg->resolver->NextLocked(
205 &cb_arg->result,
206 GRPC_CLOSURE_CREATE(on_second_resolution, arg,
207 grpc_combiner_scheduler(g_combiner)));
208 cb_arg->resolver->RequestReresolutionLocked();
209 gpr_mu_lock(g_iomgr_args.mu);
210 GRPC_LOG_IF_ERROR("pollset_kick",
211 grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
212 gpr_mu_unlock(g_iomgr_args.mu);
213 }
214
start_test_under_combiner(void * arg,grpc_error * error)215 static void start_test_under_combiner(void* arg, grpc_error* error) {
216 OnResolutionCallbackArg* res_cb_arg =
217 static_cast<OnResolutionCallbackArg*>(arg);
218
219 grpc_core::ResolverFactory* factory =
220 grpc_core::ResolverRegistry::LookupResolverFactory("dns");
221 grpc_uri* uri = grpc_uri_parse(res_cb_arg->uri_str, 0);
222 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", res_cb_arg->uri_str,
223 factory->scheme());
224 GPR_ASSERT(uri != nullptr);
225 grpc_core::ResolverArgs args;
226 args.uri = uri;
227 args.combiner = g_combiner;
228 g_resolution_count = 0;
229
230 grpc_arg cooldown_arg;
231 cooldown_arg.key =
232 const_cast<char*>(GRPC_ARG_DNS_MIN_TIME_BETWEEN_RESOLUTIONS_MS);
233 cooldown_arg.type = GRPC_ARG_INTEGER;
234 cooldown_arg.value.integer = kMinResolutionPeriodMs;
235 auto* cooldown_channel_args =
236 grpc_channel_args_copy_and_add(nullptr, &cooldown_arg, 1);
237 args.args = cooldown_channel_args;
238 res_cb_arg->resolver = factory->CreateResolver(args);
239 grpc_channel_args_destroy(cooldown_channel_args);
240 GPR_ASSERT(res_cb_arg->resolver != nullptr);
241 // First resolution, would incur in system-level resolution.
242 res_cb_arg->resolver->NextLocked(
243 &res_cb_arg->result,
244 GRPC_CLOSURE_CREATE(on_first_resolution, res_cb_arg,
245 grpc_combiner_scheduler(g_combiner)));
246 grpc_uri_destroy(uri);
247 }
248
test_cooldown()249 static void test_cooldown() {
250 grpc_core::ExecCtx exec_ctx;
251 iomgr_args_init(&g_iomgr_args);
252 OnResolutionCallbackArg* res_cb_arg =
253 grpc_core::New<OnResolutionCallbackArg>();
254 res_cb_arg->uri_str = "dns:127.0.0.1";
255
256 GRPC_CLOSURE_SCHED(GRPC_CLOSURE_CREATE(start_test_under_combiner, res_cb_arg,
257 grpc_combiner_scheduler(g_combiner)),
258 GRPC_ERROR_NONE);
259 grpc_core::ExecCtx::Get()->Flush();
260 poll_pollset_until_request_done(&g_iomgr_args);
261 iomgr_args_finish(&g_iomgr_args);
262 }
263
main(int argc,char ** argv)264 int main(int argc, char** argv) {
265 grpc_test_init(argc, argv);
266 grpc_init();
267
268 g_combiner = grpc_combiner_create();
269
270 g_default_dns_lookup_ares_locked = grpc_dns_lookup_ares_locked;
271 grpc_dns_lookup_ares_locked = test_dns_lookup_ares_locked;
272 default_resolve_address = grpc_resolve_address_impl;
273 grpc_set_resolver_impl(&test_resolver);
274
275 test_cooldown();
276
277 {
278 grpc_core::ExecCtx exec_ctx;
279 GRPC_COMBINER_UNREF(g_combiner, "test");
280 }
281 grpc_shutdown();
282 GPR_ASSERT(g_all_callbacks_invoked);
283 return 0;
284 }
285