cancel_ares_query_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #include <stdio.h>
  19. #include <string.h>
  20. #include <string>
  21. #include <gmock/gmock.h>
  22. #include "absl/strings/str_cat.h"
  23. #include "absl/strings/str_format.h"
  24. #include <grpc/byte_buffer.h>
  25. #include <grpc/grpc.h>
  26. #include <grpc/grpc_security.h>
  27. #include <grpc/support/alloc.h>
  28. #include <grpc/support/log.h>
  29. #include <grpc/support/time.h>
  30. #include "src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
  31. #include "src/core/lib/channel/channel_args.h"
  32. #include "src/core/lib/config/core_configuration.h"
  33. #include "src/core/lib/debug/stats.h"
  34. #include "src/core/lib/gpr/string.h"
  35. #include "src/core/lib/gprpp/orphanable.h"
  36. #include "src/core/lib/gprpp/thd.h"
  37. #include "src/core/lib/iomgr/iomgr.h"
  38. #include "src/core/lib/iomgr/pollset.h"
  39. #include "src/core/lib/iomgr/pollset_set.h"
  40. #include "src/core/lib/iomgr/work_serializer.h"
  41. #include "src/core/lib/resolver/resolver.h"
  42. #include "src/core/lib/resolver/resolver_registry.h"
  43. #include "test/core/end2end/cq_verifier.h"
  44. #include "test/core/util/cmdline.h"
  45. #include "test/core/util/fake_udp_and_tcp_server.h"
  46. #include "test/core/util/port.h"
  47. #include "test/core/util/test_config.h"
  48. #ifdef GPR_WINDOWS
  49. #include "src/core/lib/iomgr/sockaddr_windows.h"
  50. #include "src/core/lib/iomgr/socket_windows.h"
  51. #define BAD_SOCKET_RETURN_VAL INVALID_SOCKET
  52. #else
  53. #include "src/core/lib/iomgr/sockaddr_posix.h"
  54. #define BAD_SOCKET_RETURN_VAL (-1)
  55. #endif
  56. namespace {
  57. void* Tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  58. gpr_timespec FiveSecondsFromNow(void) {
  59. return grpc_timeout_seconds_to_deadline(5);
  60. }
  61. void DrainCq(grpc_completion_queue* cq) {
  62. grpc_event ev;
  63. do {
  64. ev = grpc_completion_queue_next(cq, FiveSecondsFromNow(), nullptr);
  65. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  66. }
  67. void EndTest(grpc_channel* client, grpc_completion_queue* cq) {
  68. grpc_channel_destroy(client);
  69. grpc_completion_queue_shutdown(cq);
  70. DrainCq(cq);
  71. grpc_completion_queue_destroy(cq);
  72. }
  73. struct ArgsStruct {
  74. gpr_atm done_atm;
  75. gpr_mu* mu;
  76. grpc_pollset* pollset;
  77. grpc_pollset_set* pollset_set;
  78. std::shared_ptr<grpc_core::WorkSerializer> lock;
  79. grpc_channel_args* channel_args;
  80. };
  81. void ArgsInit(ArgsStruct* args) {
  82. args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
  83. grpc_pollset_init(args->pollset, &args->mu);
  84. args->pollset_set = grpc_pollset_set_create();
  85. grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
  86. args->lock = std::make_shared<grpc_core::WorkSerializer>();
  87. gpr_atm_rel_store(&args->done_atm, 0);
  88. args->channel_args = nullptr;
  89. }
  90. void DoNothing(void* /*arg*/, grpc_error_handle /*error*/) {}
  91. void ArgsFinish(ArgsStruct* args) {
  92. grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
  93. grpc_pollset_set_destroy(args->pollset_set);
  94. grpc_closure DoNothing_cb;
  95. GRPC_CLOSURE_INIT(&DoNothing_cb, DoNothing, nullptr,
  96. grpc_schedule_on_exec_ctx);
  97. grpc_pollset_shutdown(args->pollset, &DoNothing_cb);
  98. // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
  99. grpc_channel_args_destroy(args->channel_args);
  100. grpc_core::ExecCtx::Get()->Flush();
  101. grpc_pollset_destroy(args->pollset);
  102. gpr_free(args->pollset);
  103. }
  104. void PollPollsetUntilRequestDone(ArgsStruct* args) {
  105. while (true) {
  106. bool done = gpr_atm_acq_load(&args->done_atm) != 0;
  107. if (done) {
  108. break;
  109. }
  110. grpc_pollset_worker* worker = nullptr;
  111. grpc_core::ExecCtx exec_ctx;
  112. gpr_mu_lock(args->mu);
  113. GRPC_LOG_IF_ERROR("pollset_work",
  114. grpc_pollset_work(args->pollset, &worker,
  115. grpc_core::Timestamp::InfFuture()));
  116. gpr_mu_unlock(args->mu);
  117. }
  118. }
  119. class AssertFailureResultHandler : public grpc_core::Resolver::ResultHandler {
  120. public:
  121. explicit AssertFailureResultHandler(ArgsStruct* args) : args_(args) {}
  122. ~AssertFailureResultHandler() override {
  123. gpr_atm_rel_store(&args_->done_atm, 1);
  124. gpr_mu_lock(args_->mu);
  125. GRPC_LOG_IF_ERROR("pollset_kick",
  126. grpc_pollset_kick(args_->pollset, nullptr));
  127. gpr_mu_unlock(args_->mu);
  128. }
  129. void ReportResult(grpc_core::Resolver::Result /*result*/) override {
  130. GPR_ASSERT(false);
  131. }
  132. private:
  133. ArgsStruct* args_;
  134. };
  135. void TestCancelActiveDNSQuery(ArgsStruct* args) {
  136. grpc_core::testing::FakeUdpAndTcpServer fake_dns_server(
  137. grpc_core::testing::FakeUdpAndTcpServer::AcceptMode::
  138. kWaitForClientToSendFirstBytes,
  139. grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer);
  140. std::string client_target = absl::StrFormat(
  141. "dns://[::1]:%d/dont-care-since-wont-be-resolved.test.com:1234",
  142. fake_dns_server.port());
  143. // create resolver and resolve
  144. grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
  145. grpc_core::CoreConfiguration::Get().resolver_registry().CreateResolver(
  146. client_target.c_str(), nullptr, args->pollset_set, args->lock,
  147. std::unique_ptr<grpc_core::Resolver::ResultHandler>(
  148. new AssertFailureResultHandler(args)));
  149. resolver->StartLocked();
  150. // Without resetting and causing resolver shutdown, the
  151. // PollPollsetUntilRequestDone call should never finish.
  152. resolver.reset();
  153. grpc_core::ExecCtx::Get()->Flush();
  154. PollPollsetUntilRequestDone(args);
  155. ArgsFinish(args);
  156. }
  157. class CancelDuringAresQuery : public ::testing::Test {
  158. protected:
  159. static void SetUpTestCase() {
  160. GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "ares");
  161. // Sanity check the time that it takes to run the test
  162. // including the teardown time (the teardown
  163. // part of the test involves cancelling the DNS query,
  164. // which is the main point of interest for this test).
  165. overall_deadline = grpc_timeout_seconds_to_deadline(4);
  166. grpc_init();
  167. }
  168. static void TearDownTestCase() {
  169. grpc_shutdown();
  170. if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), overall_deadline) > 0) {
  171. gpr_log(GPR_ERROR, "Test took too long");
  172. abort();
  173. }
  174. }
  175. private:
  176. static gpr_timespec overall_deadline;
  177. };
  178. gpr_timespec CancelDuringAresQuery::overall_deadline;
  179. TEST_F(CancelDuringAresQuery, TestCancelActiveDNSQuery) {
  180. grpc_core::ExecCtx exec_ctx;
  181. ArgsStruct args;
  182. ArgsInit(&args);
  183. TestCancelActiveDNSQuery(&args);
  184. }
  185. #ifdef GPR_WINDOWS
  186. void MaybePollArbitraryPollsetTwice() {
  187. grpc_pollset* pollset = (grpc_pollset*)gpr_zalloc(grpc_pollset_size());
  188. gpr_mu* mu;
  189. grpc_pollset_init(pollset, &mu);
  190. grpc_pollset_worker* worker = nullptr;
  191. // Make a zero timeout poll
  192. gpr_mu_lock(mu);
  193. GRPC_LOG_IF_ERROR(
  194. "pollset_work",
  195. grpc_pollset_work(pollset, &worker, grpc_core::ExecCtx::Get()->Now()));
  196. gpr_mu_unlock(mu);
  197. grpc_core::ExecCtx::Get()->Flush();
  198. // Make a second zero-timeout poll (in case the first one
  199. // short-circuited by picking up a previous "kick")
  200. gpr_mu_lock(mu);
  201. GRPC_LOG_IF_ERROR(
  202. "pollset_work",
  203. grpc_pollset_work(pollset, &worker, grpc_core::ExecCtx::Get()->Now()));
  204. gpr_mu_unlock(mu);
  205. grpc_core::ExecCtx::Get()->Flush();
  206. grpc_pollset_destroy(pollset);
  207. gpr_free(pollset);
  208. }
  209. #else
  210. void MaybePollArbitraryPollsetTwice() {}
  211. #endif
  212. TEST_F(CancelDuringAresQuery, TestFdsAreDeletedFromPollsetSet) {
  213. grpc_core::ExecCtx exec_ctx;
  214. ArgsStruct args;
  215. ArgsInit(&args);
  216. // Add fake_other_pollset_set into the mix to test
  217. // that we're explicitly deleting fd's from their pollset.
  218. // If we aren't doing so, then the remaining presence of
  219. // "fake_other_pollset_set" after the request is done and the resolver
  220. // pollset set is destroyed should keep the resolver's fd alive and
  221. // fail the test.
  222. grpc_pollset_set* fake_other_pollset_set = grpc_pollset_set_create();
  223. grpc_pollset_set_add_pollset_set(fake_other_pollset_set, args.pollset_set);
  224. // Note that running the cancellation c-ares test is somewhat irrelevant for
  225. // this test. This test only cares about what happens to fd's that c-ares
  226. // opens.
  227. TestCancelActiveDNSQuery(&args);
  228. // This test relies on the assumption that cancelling a c-ares query
  229. // will flush out all callbacks on the current exec ctx, which is true
  230. // on posix platforms but not on Windows, because fd shutdown on Windows
  231. // requires a trip through the polling loop to schedule the callback.
  232. // So we need to do extra polling work on Windows to free things up.
  233. MaybePollArbitraryPollsetTwice();
  234. EXPECT_EQ(grpc_iomgr_count_objects_for_testing(), 0u);
  235. grpc_pollset_set_destroy(fake_other_pollset_set);
  236. }
  237. // Settings for TestCancelDuringActiveQuery test
  238. typedef enum {
  239. NONE,
  240. SHORT,
  241. ZERO,
  242. } cancellation_test_query_timeout_setting;
  243. void TestCancelDuringActiveQuery(
  244. cancellation_test_query_timeout_setting query_timeout_setting) {
  245. // Start up fake non responsive DNS server
  246. grpc_core::testing::FakeUdpAndTcpServer fake_dns_server(
  247. grpc_core::testing::FakeUdpAndTcpServer::AcceptMode::
  248. kWaitForClientToSendFirstBytes,
  249. grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer);
  250. // Create a call that will try to use the fake DNS server
  251. std::string name = "dont-care-since-wont-be-resolved.test.com:1234";
  252. std::string client_target =
  253. absl::StrFormat("dns://[::1]:%d/%s", fake_dns_server.port(), name);
  254. gpr_log(GPR_DEBUG, "TestCancelActiveDNSQuery. query timeout setting: %d",
  255. query_timeout_setting);
  256. grpc_channel_args* client_args = nullptr;
  257. grpc_status_code expected_status_code = GRPC_STATUS_OK;
  258. std::string expected_error_message_substring;
  259. gpr_timespec rpc_deadline;
  260. if (query_timeout_setting == NONE) {
  261. // The RPC deadline should go off well before the DNS resolution
  262. // timeout fires.
  263. expected_status_code = GRPC_STATUS_DEADLINE_EXCEEDED;
  264. // use default DNS resolution timeout (which is over one minute).
  265. client_args = nullptr;
  266. rpc_deadline = grpc_timeout_milliseconds_to_deadline(100);
  267. } else if (query_timeout_setting == SHORT) {
  268. // The DNS resolution timeout should fire well before the
  269. // RPC's deadline expires.
  270. expected_status_code = GRPC_STATUS_UNAVAILABLE;
  271. expected_error_message_substring =
  272. absl::StrCat("DNS resolution failed for ", name);
  273. grpc_arg arg;
  274. arg.type = GRPC_ARG_INTEGER;
  275. arg.key = const_cast<char*>(GRPC_ARG_DNS_ARES_QUERY_TIMEOUT_MS);
  276. arg.value.integer =
  277. 1; // Set this shorter than the call deadline so that it goes off.
  278. client_args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
  279. // Set the deadline high enough such that if we hit this and get
  280. // a deadline exceeded status code, then we are confident that there's
  281. // a bug causing cancellation of DNS resolutions to not happen in a timely
  282. // manner.
  283. rpc_deadline = grpc_timeout_seconds_to_deadline(10);
  284. } else if (query_timeout_setting == ZERO) {
  285. // The RPC deadline should go off well before the DNS resolution
  286. // timeout fires.
  287. expected_status_code = GRPC_STATUS_DEADLINE_EXCEEDED;
  288. grpc_arg arg;
  289. arg.type = GRPC_ARG_INTEGER;
  290. arg.key = const_cast<char*>(GRPC_ARG_DNS_ARES_QUERY_TIMEOUT_MS);
  291. arg.value.integer = 0; // Set this to zero to disable query timeouts.
  292. client_args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
  293. rpc_deadline = grpc_timeout_milliseconds_to_deadline(100);
  294. } else {
  295. abort();
  296. }
  297. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  298. grpc_channel* client =
  299. grpc_channel_create(client_target.c_str(), creds, client_args);
  300. grpc_channel_credentials_release(creds);
  301. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  302. cq_verifier* cqv = cq_verifier_create(cq);
  303. grpc_call* call = grpc_channel_create_call(
  304. client, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
  305. grpc_slice_from_static_string("/foo"), nullptr, rpc_deadline, nullptr);
  306. GPR_ASSERT(call);
  307. grpc_metadata_array initial_metadata_recv;
  308. grpc_metadata_array trailing_metadata_recv;
  309. grpc_metadata_array request_metadata_recv;
  310. grpc_metadata_array_init(&initial_metadata_recv);
  311. grpc_metadata_array_init(&trailing_metadata_recv);
  312. grpc_metadata_array_init(&request_metadata_recv);
  313. grpc_call_details call_details;
  314. grpc_call_details_init(&call_details);
  315. grpc_status_code status;
  316. const char* error_string;
  317. grpc_slice details;
  318. // Set ops for client the request
  319. grpc_op ops_base[6];
  320. memset(ops_base, 0, sizeof(ops_base));
  321. grpc_op* op = ops_base;
  322. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  323. op->data.send_initial_metadata.count = 0;
  324. op->flags = 0;
  325. op->reserved = nullptr;
  326. op++;
  327. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  328. op->flags = 0;
  329. op->reserved = nullptr;
  330. op++;
  331. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  332. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  333. op->flags = 0;
  334. op->reserved = nullptr;
  335. op++;
  336. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  337. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  338. op->data.recv_status_on_client.status = &status;
  339. op->data.recv_status_on_client.status_details = &details;
  340. op->data.recv_status_on_client.error_string = &error_string;
  341. op->flags = 0;
  342. op->reserved = nullptr;
  343. op++;
  344. // Run the call and sanity check it failed as expected
  345. grpc_call_error error = grpc_call_start_batch(
  346. call, ops_base, static_cast<size_t>(op - ops_base), Tag(1), nullptr);
  347. EXPECT_EQ(GRPC_CALL_OK, error);
  348. CQ_EXPECT_COMPLETION(cqv, Tag(1), 1);
  349. cq_verify(cqv);
  350. EXPECT_EQ(status, expected_status_code);
  351. EXPECT_THAT(std::string(error_string),
  352. testing::HasSubstr(expected_error_message_substring));
  353. // Teardown
  354. grpc_channel_args_destroy(client_args);
  355. grpc_slice_unref(details);
  356. gpr_free(const_cast<char*>(error_string));
  357. grpc_metadata_array_destroy(&initial_metadata_recv);
  358. grpc_metadata_array_destroy(&trailing_metadata_recv);
  359. grpc_metadata_array_destroy(&request_metadata_recv);
  360. grpc_call_details_destroy(&call_details);
  361. grpc_call_unref(call);
  362. cq_verifier_destroy(cqv);
  363. EndTest(client, cq);
  364. }
  365. TEST_F(CancelDuringAresQuery,
  366. TestHitDeadlineAndDestroyChannelDuringAresResolutionIsGraceful) {
  367. TestCancelDuringActiveQuery(NONE /* don't set query timeouts */);
  368. }
  369. TEST_F(
  370. CancelDuringAresQuery,
  371. TestHitDeadlineAndDestroyChannelDuringAresResolutionWithQueryTimeoutIsGraceful) {
  372. TestCancelDuringActiveQuery(SHORT /* set short query timeout */);
  373. }
  374. TEST_F(
  375. CancelDuringAresQuery,
  376. TestHitDeadlineAndDestroyChannelDuringAresResolutionWithZeroQueryTimeoutIsGraceful) {
  377. TestCancelDuringActiveQuery(ZERO /* disable query timeouts */);
  378. }
  379. } // namespace
  380. int main(int argc, char** argv) {
  381. grpc::testing::TestEnvironment env(argc, argv);
  382. ::testing::InitGoogleTest(&argc, argv);
  383. auto result = RUN_ALL_TESTS();
  384. return result;
  385. }