retry_streaming.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 <grpc/byte_buffer.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/string_util.h>
  25. #include <grpc/support/time.h>
  26. #include "src/core/lib/channel/channel_args.h"
  27. #include "src/core/lib/gpr/string.h"
  28. #include "src/core/lib/gpr/useful.h"
  29. #include "src/core/lib/iomgr/exec_ctx.h"
  30. #include "src/core/lib/surface/channel.h"
  31. #include "src/core/lib/surface/server.h"
  32. #include "test/core/end2end/cq_verifier.h"
  33. #include "test/core/end2end/end2end_tests.h"
  34. #include "test/core/end2end/tests/cancel_test_helpers.h"
  35. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  36. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  37. const char* test_name,
  38. grpc_channel_args* client_args,
  39. grpc_channel_args* server_args) {
  40. grpc_end2end_test_fixture f;
  41. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  42. f = config.create_fixture(client_args, server_args);
  43. config.init_server(&f, server_args);
  44. config.init_client(&f, client_args);
  45. return f;
  46. }
  47. static gpr_timespec n_seconds_from_now(int n) {
  48. return grpc_timeout_seconds_to_deadline(n);
  49. }
  50. static gpr_timespec five_seconds_from_now(void) {
  51. return n_seconds_from_now(5);
  52. }
  53. static void drain_cq(grpc_completion_queue* cq) {
  54. grpc_event ev;
  55. do {
  56. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  57. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  58. }
  59. static void shutdown_server(grpc_end2end_test_fixture* f) {
  60. if (!f->server) return;
  61. grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  62. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  63. grpc_timeout_seconds_to_deadline(5),
  64. nullptr)
  65. .type == GRPC_OP_COMPLETE);
  66. grpc_server_destroy(f->server);
  67. f->server = nullptr;
  68. }
  69. static void shutdown_client(grpc_end2end_test_fixture* f) {
  70. if (!f->client) return;
  71. grpc_channel_destroy(f->client);
  72. f->client = nullptr;
  73. }
  74. static void end_test(grpc_end2end_test_fixture* f) {
  75. shutdown_server(f);
  76. shutdown_client(f);
  77. grpc_completion_queue_shutdown(f->cq);
  78. drain_cq(f->cq);
  79. grpc_completion_queue_destroy(f->cq);
  80. grpc_completion_queue_destroy(f->shutdown_cq);
  81. }
  82. // Tests retrying a streaming RPC. This is the same as
  83. // the basic retry test, except that the client sends two messages on the
  84. // call before the initial attempt fails.
  85. // FIXME: We should also test the case where the retry is committed after
  86. // replaying 1 of 2 previously-completed send_message ops. However,
  87. // there's no way to trigger that from an end2end test, because the
  88. // replayed ops happen under the hood -- they are not surfaced to the
  89. // C-core API, and therefore we have no way to inject the commit at the
  90. // right point.
  91. static void test_retry_streaming(grpc_end2end_test_config config) {
  92. grpc_call* c;
  93. grpc_call* s;
  94. grpc_op ops[6];
  95. grpc_op* op;
  96. grpc_metadata_array initial_metadata_recv;
  97. grpc_metadata_array trailing_metadata_recv;
  98. grpc_metadata_array request_metadata_recv;
  99. grpc_call_details call_details;
  100. grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
  101. grpc_slice request2_payload_slice = grpc_slice_from_static_string("bar");
  102. grpc_slice request3_payload_slice = grpc_slice_from_static_string("baz");
  103. grpc_slice response_payload_slice = grpc_slice_from_static_string("quux");
  104. grpc_byte_buffer* request_payload =
  105. grpc_raw_byte_buffer_create(&request_payload_slice, 1);
  106. grpc_byte_buffer* request2_payload =
  107. grpc_raw_byte_buffer_create(&request2_payload_slice, 1);
  108. grpc_byte_buffer* request3_payload =
  109. grpc_raw_byte_buffer_create(&request3_payload_slice, 1);
  110. grpc_byte_buffer* response_payload =
  111. grpc_raw_byte_buffer_create(&response_payload_slice, 1);
  112. grpc_byte_buffer* request_payload_recv = nullptr;
  113. grpc_byte_buffer* request2_payload_recv = nullptr;
  114. grpc_byte_buffer* request3_payload_recv = nullptr;
  115. grpc_byte_buffer* response_payload_recv = nullptr;
  116. grpc_status_code status;
  117. grpc_call_error error;
  118. grpc_slice details;
  119. int was_cancelled = 2;
  120. char* peer;
  121. grpc_arg args[] = {
  122. grpc_channel_arg_integer_create(
  123. const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE),
  124. 1024 * 8),
  125. grpc_channel_arg_integer_create(
  126. const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true),
  127. grpc_channel_arg_string_create(
  128. const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
  129. const_cast<char*>(
  130. "{\n"
  131. " \"methodConfig\": [ {\n"
  132. " \"name\": [\n"
  133. " { \"service\": \"service\", \"method\": \"method\" }\n"
  134. " ],\n"
  135. " \"retryPolicy\": {\n"
  136. " \"maxAttempts\": 3,\n"
  137. " \"initialBackoff\": \"1s\",\n"
  138. " \"maxBackoff\": \"120s\",\n"
  139. " \"backoffMultiplier\": 1.6,\n"
  140. " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
  141. " }\n"
  142. " } ]\n"
  143. "}"))};
  144. grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
  145. grpc_end2end_test_fixture f =
  146. begin_test(config, "retry_streaming", &client_args, nullptr);
  147. cq_verifier* cqv = cq_verifier_create(f.cq);
  148. gpr_timespec deadline = five_seconds_from_now();
  149. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  150. grpc_slice_from_static_string("/service/method"),
  151. nullptr, deadline, nullptr);
  152. grpc_core::channelz::ChannelNode* channelz_channel =
  153. grpc_channel_get_channelz_node(f.client);
  154. GPR_ASSERT(c);
  155. peer = grpc_call_get_peer(c);
  156. GPR_ASSERT(peer != nullptr);
  157. gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
  158. gpr_free(peer);
  159. grpc_metadata_array_init(&initial_metadata_recv);
  160. grpc_metadata_array_init(&trailing_metadata_recv);
  161. grpc_metadata_array_init(&request_metadata_recv);
  162. grpc_call_details_init(&call_details);
  163. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  164. // Client starts a batch for receiving initial metadata, a message,
  165. // and trailing metadata.
  166. memset(ops, 0, sizeof(ops));
  167. op = ops;
  168. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  169. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  170. op++;
  171. op->op = GRPC_OP_RECV_MESSAGE;
  172. op->data.recv_message.recv_message = &response_payload_recv;
  173. op++;
  174. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  175. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  176. op->data.recv_status_on_client.status = &status;
  177. op->data.recv_status_on_client.status_details = &details;
  178. op++;
  179. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  180. nullptr);
  181. GPR_ASSERT(GRPC_CALL_OK == error);
  182. // Client sends initial metadata and a message.
  183. memset(ops, 0, sizeof(ops));
  184. op = ops;
  185. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  186. op->data.send_initial_metadata.count = 0;
  187. op++;
  188. op->op = GRPC_OP_SEND_MESSAGE;
  189. op->data.send_message.send_message = request_payload;
  190. op++;
  191. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
  192. nullptr);
  193. GPR_ASSERT(GRPC_CALL_OK == error);
  194. CQ_EXPECT_COMPLETION(cqv, tag(2), true);
  195. cq_verify(cqv);
  196. // Server gets a call with received initial metadata.
  197. error =
  198. grpc_server_request_call(f.server, &s, &call_details,
  199. &request_metadata_recv, f.cq, f.cq, tag(101));
  200. GPR_ASSERT(GRPC_CALL_OK == error);
  201. CQ_EXPECT_COMPLETION(cqv, tag(101), true);
  202. cq_verify(cqv);
  203. peer = grpc_call_get_peer(s);
  204. GPR_ASSERT(peer != nullptr);
  205. gpr_log(GPR_DEBUG, "server_peer=%s", peer);
  206. gpr_free(peer);
  207. peer = grpc_call_get_peer(c);
  208. GPR_ASSERT(peer != nullptr);
  209. gpr_log(GPR_DEBUG, "client_peer=%s", peer);
  210. gpr_free(peer);
  211. // Server receives a message.
  212. memset(ops, 0, sizeof(ops));
  213. op = ops;
  214. op->op = GRPC_OP_RECV_MESSAGE;
  215. op->data.recv_message.recv_message = &request_payload_recv;
  216. op++;
  217. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  218. nullptr);
  219. GPR_ASSERT(GRPC_CALL_OK == error);
  220. CQ_EXPECT_COMPLETION(cqv, tag(102), true);
  221. cq_verify(cqv);
  222. // Client sends a second message.
  223. memset(ops, 0, sizeof(ops));
  224. op = ops;
  225. op->op = GRPC_OP_SEND_MESSAGE;
  226. op->data.send_message.send_message = request2_payload;
  227. op++;
  228. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
  229. nullptr);
  230. GPR_ASSERT(GRPC_CALL_OK == error);
  231. CQ_EXPECT_COMPLETION(cqv, tag(3), true);
  232. cq_verify(cqv);
  233. // Server receives the second message.
  234. memset(ops, 0, sizeof(ops));
  235. op = ops;
  236. op->op = GRPC_OP_RECV_MESSAGE;
  237. op->data.recv_message.recv_message = &request2_payload_recv;
  238. op++;
  239. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
  240. nullptr);
  241. GPR_ASSERT(GRPC_CALL_OK == error);
  242. CQ_EXPECT_COMPLETION(cqv, tag(103), true);
  243. cq_verify(cqv);
  244. // Server sends both initial and trailing metadata.
  245. memset(ops, 0, sizeof(ops));
  246. op = ops;
  247. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  248. op->data.recv_close_on_server.cancelled = &was_cancelled;
  249. op++;
  250. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  251. op->data.send_initial_metadata.count = 0;
  252. op++;
  253. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  254. op->data.send_status_from_server.trailing_metadata_count = 0;
  255. op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
  256. op->data.send_status_from_server.status_details = &status_details;
  257. op++;
  258. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
  259. nullptr);
  260. GPR_ASSERT(GRPC_CALL_OK == error);
  261. CQ_EXPECT_COMPLETION(cqv, tag(104), true);
  262. cq_verify(cqv);
  263. // Clean up from first attempt.
  264. grpc_call_unref(s);
  265. grpc_metadata_array_destroy(&request_metadata_recv);
  266. grpc_metadata_array_init(&request_metadata_recv);
  267. grpc_call_details_destroy(&call_details);
  268. grpc_call_details_init(&call_details);
  269. GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice));
  270. grpc_byte_buffer_destroy(request_payload_recv);
  271. request_payload_recv = nullptr;
  272. GPR_ASSERT(
  273. byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice));
  274. grpc_byte_buffer_destroy(request2_payload_recv);
  275. request2_payload_recv = nullptr;
  276. // Server gets a second call (the retry).
  277. error =
  278. grpc_server_request_call(f.server, &s, &call_details,
  279. &request_metadata_recv, f.cq, f.cq, tag(201));
  280. GPR_ASSERT(GRPC_CALL_OK == error);
  281. CQ_EXPECT_COMPLETION(cqv, tag(201), true);
  282. cq_verify(cqv);
  283. peer = grpc_call_get_peer(s);
  284. GPR_ASSERT(peer != nullptr);
  285. gpr_log(GPR_DEBUG, "server_peer=%s", peer);
  286. gpr_free(peer);
  287. peer = grpc_call_get_peer(c);
  288. GPR_ASSERT(peer != nullptr);
  289. gpr_log(GPR_DEBUG, "client_peer=%s", peer);
  290. gpr_free(peer);
  291. // Server receives a message.
  292. memset(ops, 0, sizeof(ops));
  293. op = ops;
  294. op->op = GRPC_OP_RECV_MESSAGE;
  295. op->data.recv_message.recv_message = &request_payload_recv;
  296. op++;
  297. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(202),
  298. nullptr);
  299. GPR_ASSERT(GRPC_CALL_OK == error);
  300. CQ_EXPECT_COMPLETION(cqv, tag(202), true);
  301. cq_verify(cqv);
  302. // Server receives a second message.
  303. memset(ops, 0, sizeof(ops));
  304. op = ops;
  305. op->op = GRPC_OP_RECV_MESSAGE;
  306. op->data.recv_message.recv_message = &request2_payload_recv;
  307. op++;
  308. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(203),
  309. nullptr);
  310. GPR_ASSERT(GRPC_CALL_OK == error);
  311. CQ_EXPECT_COMPLETION(cqv, tag(203), true);
  312. cq_verify(cqv);
  313. // Client sends a third message and a close.
  314. memset(ops, 0, sizeof(ops));
  315. op = ops;
  316. op->op = GRPC_OP_SEND_MESSAGE;
  317. op->data.send_message.send_message = request3_payload;
  318. op++;
  319. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  320. op++;
  321. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(4),
  322. nullptr);
  323. GPR_ASSERT(GRPC_CALL_OK == error);
  324. CQ_EXPECT_COMPLETION(cqv, tag(4), true);
  325. cq_verify(cqv);
  326. // Server receives a third message.
  327. memset(ops, 0, sizeof(ops));
  328. op = ops;
  329. op->op = GRPC_OP_RECV_MESSAGE;
  330. op->data.recv_message.recv_message = &request3_payload_recv;
  331. op++;
  332. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(204),
  333. nullptr);
  334. GPR_ASSERT(GRPC_CALL_OK == error);
  335. CQ_EXPECT_COMPLETION(cqv, tag(204), true);
  336. cq_verify(cqv);
  337. // Server receives a close and sends initial metadata, a message, and
  338. // trailing metadata.
  339. memset(ops, 0, sizeof(ops));
  340. op = ops;
  341. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  342. op->data.recv_close_on_server.cancelled = &was_cancelled;
  343. op++;
  344. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  345. op->data.send_initial_metadata.count = 0;
  346. op++;
  347. op->op = GRPC_OP_SEND_MESSAGE;
  348. op->data.send_message.send_message = response_payload;
  349. op++;
  350. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  351. op->data.send_status_from_server.trailing_metadata_count = 0;
  352. // Returning a retriable code, but because we are also sending a
  353. // message, the client will commit instead of retrying again.
  354. op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
  355. op->data.send_status_from_server.status_details = &status_details;
  356. op++;
  357. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(205),
  358. nullptr);
  359. GPR_ASSERT(GRPC_CALL_OK == error);
  360. CQ_EXPECT_COMPLETION(cqv, tag(205), true);
  361. CQ_EXPECT_COMPLETION(cqv, tag(1), true);
  362. cq_verify(cqv);
  363. GPR_ASSERT(status == GRPC_STATUS_ABORTED);
  364. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  365. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
  366. GPR_ASSERT(0 == call_details.flags);
  367. GPR_ASSERT(was_cancelled == 0);
  368. GPR_ASSERT(channelz_channel != nullptr);
  369. std::string json = channelz_channel->RenderJsonString();
  370. gpr_log(GPR_INFO, "%s", json.c_str());
  371. GPR_ASSERT(json.find("\"trace\"") != json.npos);
  372. GPR_ASSERT(json.find("\"description\":\"Channel created\"") != json.npos);
  373. GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") != json.npos);
  374. GPR_ASSERT(json.find("Resolution event") != json.npos);
  375. GPR_ASSERT(json.find("Created new LB policy") != json.npos);
  376. GPR_ASSERT(json.find("Service config changed") != json.npos);
  377. GPR_ASSERT(json.find("Address list became non-empty") != json.npos);
  378. GPR_ASSERT(json.find("Channel state change to CONNECTING") != json.npos);
  379. grpc_slice_unref(details);
  380. grpc_metadata_array_destroy(&initial_metadata_recv);
  381. grpc_metadata_array_destroy(&trailing_metadata_recv);
  382. grpc_metadata_array_destroy(&request_metadata_recv);
  383. grpc_call_details_destroy(&call_details);
  384. grpc_byte_buffer_destroy(request_payload);
  385. grpc_byte_buffer_destroy(request2_payload);
  386. grpc_byte_buffer_destroy(request3_payload);
  387. grpc_byte_buffer_destroy(response_payload);
  388. GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice));
  389. grpc_byte_buffer_destroy(request_payload_recv);
  390. GPR_ASSERT(
  391. byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice));
  392. grpc_byte_buffer_destroy(request2_payload_recv);
  393. GPR_ASSERT(
  394. byte_buffer_eq_slice(request3_payload_recv, request3_payload_slice));
  395. grpc_byte_buffer_destroy(request3_payload_recv);
  396. grpc_byte_buffer_destroy(response_payload_recv);
  397. grpc_call_unref(c);
  398. grpc_call_unref(s);
  399. cq_verifier_destroy(cqv);
  400. end_test(&f);
  401. config.tear_down_data(&f);
  402. }
  403. void retry_streaming(grpc_end2end_test_config config) {
  404. GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
  405. test_retry_streaming(config);
  406. }
  407. void retry_streaming_pre_init(void) {}