retry_cancellation.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. *
  3. * Copyright 2017 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 "absl/strings/str_cat.h"
  22. #include <grpc/byte_buffer.h>
  23. #include <grpc/grpc.h>
  24. #include <grpc/support/alloc.h>
  25. #include <grpc/support/log.h>
  26. #include <grpc/support/time.h>
  27. #include "src/core/lib/channel/channel_args.h"
  28. #include "src/core/lib/gpr/string.h"
  29. #include "src/core/lib/gpr/useful.h"
  30. #include "src/core/lib/iomgr/exec_ctx.h"
  31. #include "test/core/end2end/cq_verifier.h"
  32. #include "test/core/end2end/end2end_tests.h"
  33. #include "test/core/end2end/tests/cancel_test_helpers.h"
  34. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  35. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  36. const char* test_name,
  37. grpc_channel_args* client_args,
  38. grpc_channel_args* server_args) {
  39. grpc_end2end_test_fixture f;
  40. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  41. f = config.create_fixture(client_args, server_args);
  42. config.init_server(&f, server_args);
  43. config.init_client(&f, client_args);
  44. return f;
  45. }
  46. static gpr_timespec n_seconds_from_now(int n) {
  47. return grpc_timeout_seconds_to_deadline(n);
  48. }
  49. static gpr_timespec five_seconds_from_now(void) {
  50. return n_seconds_from_now(5);
  51. }
  52. static void drain_cq(grpc_completion_queue* cq) {
  53. grpc_event ev;
  54. do {
  55. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  56. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  57. }
  58. static void shutdown_server(grpc_end2end_test_fixture* f) {
  59. if (!f->server) return;
  60. grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  61. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  62. grpc_timeout_seconds_to_deadline(5),
  63. nullptr)
  64. .type == GRPC_OP_COMPLETE);
  65. grpc_server_destroy(f->server);
  66. f->server = nullptr;
  67. }
  68. static void shutdown_client(grpc_end2end_test_fixture* f) {
  69. if (!f->client) return;
  70. grpc_channel_destroy(f->client);
  71. f->client = nullptr;
  72. }
  73. static void end_test(grpc_end2end_test_fixture* f) {
  74. shutdown_server(f);
  75. shutdown_client(f);
  76. grpc_completion_queue_shutdown(f->cq);
  77. drain_cq(f->cq);
  78. grpc_completion_queue_destroy(f->cq);
  79. grpc_completion_queue_destroy(f->shutdown_cq);
  80. }
  81. // Tests retry cancellation.
  82. static void test_retry_cancellation(grpc_end2end_test_config config,
  83. cancellation_mode mode) {
  84. grpc_call* c;
  85. grpc_call* s;
  86. grpc_op ops[6];
  87. grpc_op* op;
  88. grpc_metadata_array initial_metadata_recv;
  89. grpc_metadata_array trailing_metadata_recv;
  90. grpc_metadata_array request_metadata_recv;
  91. grpc_call_details call_details;
  92. grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
  93. grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
  94. grpc_byte_buffer* request_payload =
  95. grpc_raw_byte_buffer_create(&request_payload_slice, 1);
  96. grpc_byte_buffer* response_payload =
  97. grpc_raw_byte_buffer_create(&response_payload_slice, 1);
  98. grpc_byte_buffer* request_payload_recv = nullptr;
  99. grpc_byte_buffer* response_payload_recv = nullptr;
  100. grpc_status_code status;
  101. grpc_call_error error;
  102. grpc_slice details;
  103. int was_cancelled = 2;
  104. char* peer;
  105. grpc_arg args[] = {
  106. grpc_channel_arg_string_create(
  107. const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
  108. const_cast<char*>(
  109. "{\n"
  110. " \"methodConfig\": [ {\n"
  111. " \"name\": [\n"
  112. " { \"service\": \"service\", \"method\": \"method\" }\n"
  113. " ],\n"
  114. " \"retryPolicy\": {\n"
  115. " \"maxAttempts\": 3,\n"
  116. " \"initialBackoff\": \"1s\",\n"
  117. " \"maxBackoff\": \"120s\",\n"
  118. " \"backoffMultiplier\": 1.6,\n"
  119. " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
  120. " },\n"
  121. " \"timeout\": \"5s\"\n"
  122. " } ]\n"
  123. "}")),
  124. };
  125. grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
  126. std::string name = absl::StrCat("retry_cancellation/", mode.name);
  127. grpc_end2end_test_fixture f =
  128. begin_test(config, name.c_str(), &client_args, nullptr);
  129. cq_verifier* cqv = cq_verifier_create(f.cq);
  130. gpr_timespec deadline = five_seconds_from_now();
  131. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  132. grpc_slice_from_static_string("/service/method"),
  133. nullptr, deadline, nullptr);
  134. GPR_ASSERT(c);
  135. peer = grpc_call_get_peer(c);
  136. GPR_ASSERT(peer != nullptr);
  137. gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
  138. gpr_free(peer);
  139. grpc_metadata_array_init(&initial_metadata_recv);
  140. grpc_metadata_array_init(&trailing_metadata_recv);
  141. grpc_metadata_array_init(&request_metadata_recv);
  142. grpc_call_details_init(&call_details);
  143. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  144. // Client starts a batch with all 6 ops.
  145. memset(ops, 0, sizeof(ops));
  146. op = ops;
  147. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  148. op->data.send_initial_metadata.count = 0;
  149. op++;
  150. op->op = GRPC_OP_SEND_MESSAGE;
  151. op->data.send_message.send_message = request_payload;
  152. op++;
  153. op->op = GRPC_OP_RECV_MESSAGE;
  154. op->data.recv_message.recv_message = &response_payload_recv;
  155. op++;
  156. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  157. op++;
  158. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  159. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  160. op++;
  161. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  162. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  163. op->data.recv_status_on_client.status = &status;
  164. op->data.recv_status_on_client.status_details = &details;
  165. op++;
  166. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  167. nullptr);
  168. GPR_ASSERT(GRPC_CALL_OK == error);
  169. // Server gets a call and fails with retryable status.
  170. error =
  171. grpc_server_request_call(f.server, &s, &call_details,
  172. &request_metadata_recv, f.cq, f.cq, tag(101));
  173. GPR_ASSERT(GRPC_CALL_OK == error);
  174. CQ_EXPECT_COMPLETION(cqv, tag(101), true);
  175. cq_verify(cqv);
  176. peer = grpc_call_get_peer(s);
  177. GPR_ASSERT(peer != nullptr);
  178. gpr_log(GPR_DEBUG, "server_peer=%s", peer);
  179. gpr_free(peer);
  180. peer = grpc_call_get_peer(c);
  181. GPR_ASSERT(peer != nullptr);
  182. gpr_log(GPR_DEBUG, "client_peer=%s", peer);
  183. gpr_free(peer);
  184. memset(ops, 0, sizeof(ops));
  185. op = ops;
  186. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  187. op->data.send_initial_metadata.count = 0;
  188. op++;
  189. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  190. op->data.send_status_from_server.trailing_metadata_count = 0;
  191. op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
  192. op->data.send_status_from_server.status_details = &status_details;
  193. op++;
  194. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  195. op->data.recv_close_on_server.cancelled = &was_cancelled;
  196. op++;
  197. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  198. nullptr);
  199. GPR_ASSERT(GRPC_CALL_OK == error);
  200. CQ_EXPECT_COMPLETION(cqv, tag(102), true);
  201. cq_verify(cqv);
  202. grpc_call_unref(s);
  203. grpc_metadata_array_destroy(&request_metadata_recv);
  204. grpc_metadata_array_init(&request_metadata_recv);
  205. grpc_call_details_destroy(&call_details);
  206. grpc_call_details_init(&call_details);
  207. // Server gets a second call (the retry).
  208. error =
  209. grpc_server_request_call(f.server, &s, &call_details,
  210. &request_metadata_recv, f.cq, f.cq, tag(201));
  211. GPR_ASSERT(GRPC_CALL_OK == error);
  212. CQ_EXPECT_COMPLETION(cqv, tag(201), true);
  213. cq_verify(cqv);
  214. // Initiate cancellation.
  215. GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
  216. CQ_EXPECT_COMPLETION(cqv, tag(1), true);
  217. cq_verify(cqv);
  218. GPR_ASSERT(status == mode.expect_status);
  219. GPR_ASSERT(was_cancelled == 0);
  220. grpc_slice_unref(details);
  221. grpc_metadata_array_destroy(&initial_metadata_recv);
  222. grpc_metadata_array_destroy(&trailing_metadata_recv);
  223. grpc_metadata_array_destroy(&request_metadata_recv);
  224. grpc_call_details_destroy(&call_details);
  225. grpc_byte_buffer_destroy(request_payload);
  226. grpc_byte_buffer_destroy(response_payload);
  227. grpc_byte_buffer_destroy(request_payload_recv);
  228. grpc_byte_buffer_destroy(response_payload_recv);
  229. grpc_call_unref(c);
  230. grpc_call_unref(s);
  231. cq_verifier_destroy(cqv);
  232. end_test(&f);
  233. config.tear_down_data(&f);
  234. }
  235. void retry_cancellation(grpc_end2end_test_config config) {
  236. GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
  237. for (size_t i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); ++i) {
  238. test_retry_cancellation(config, cancellation_modes[i]);
  239. }
  240. }
  241. void retry_cancellation_pre_init(void) {}