1 //
2 // Copyright 2017 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <stdio.h>
18 #include <string.h>
19 
20 #include <string>
21 
22 #include "absl/strings/str_cat.h"
23 
24 #include <grpc/byte_buffer.h>
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29 
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/gpr/useful.h"
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "src/core/lib/transport/static_metadata.h"
35 #include "test/core/end2end/cq_verifier.h"
36 #include "test/core/end2end/end2end_tests.h"
37 #include "test/core/end2end/tests/cancel_test_helpers.h"
38 
tag(intptr_t t)39 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
40 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)41 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
42                                             const char* test_name,
43                                             grpc_channel_args* client_args,
44                                             grpc_channel_args* server_args) {
45   grpc_end2end_test_fixture f;
46   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
47   f = config.create_fixture(client_args, server_args);
48   config.init_server(&f, server_args);
49   config.init_client(&f, client_args);
50   return f;
51 }
52 
n_seconds_from_now(int n)53 static gpr_timespec n_seconds_from_now(int n) {
54   return grpc_timeout_seconds_to_deadline(n);
55 }
56 
five_seconds_from_now(void)57 static gpr_timespec five_seconds_from_now(void) {
58   return n_seconds_from_now(5);
59 }
60 
drain_cq(grpc_completion_queue * cq)61 static void drain_cq(grpc_completion_queue* cq) {
62   grpc_event ev;
63   do {
64     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
65   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
66 }
67 
shutdown_server(grpc_end2end_test_fixture * f)68 static void shutdown_server(grpc_end2end_test_fixture* f) {
69   if (!f->server) return;
70   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
71   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
72                                          grpc_timeout_seconds_to_deadline(5),
73                                          nullptr)
74                  .type == GRPC_OP_COMPLETE);
75   grpc_server_destroy(f->server);
76   f->server = nullptr;
77 }
78 
shutdown_client(grpc_end2end_test_fixture * f)79 static void shutdown_client(grpc_end2end_test_fixture* f) {
80   if (!f->client) return;
81   grpc_channel_destroy(f->client);
82   f->client = nullptr;
83 }
84 
end_test(grpc_end2end_test_fixture * f)85 static void end_test(grpc_end2end_test_fixture* f) {
86   shutdown_server(f);
87   shutdown_client(f);
88 
89   grpc_completion_queue_shutdown(f->cq);
90   drain_cq(f->cq);
91   grpc_completion_queue_destroy(f->cq);
92   grpc_completion_queue_destroy(f->shutdown_cq);
93 }
94 
95 // Tests retry cancellation during backoff.
test_retry_cancel_during_delay(grpc_end2end_test_config config,cancellation_mode mode)96 static void test_retry_cancel_during_delay(grpc_end2end_test_config config,
97                                            cancellation_mode mode) {
98   grpc_call* c;
99   grpc_call* s;
100   grpc_op ops[6];
101   grpc_op* op;
102   grpc_metadata_array initial_metadata_recv;
103   grpc_metadata_array trailing_metadata_recv;
104   grpc_metadata_array request_metadata_recv;
105   grpc_call_details call_details;
106   grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
107   grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
108   grpc_byte_buffer* request_payload =
109       grpc_raw_byte_buffer_create(&request_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* response_payload_recv = nullptr;
114   grpc_status_code status;
115   grpc_call_error error;
116   grpc_slice details;
117   int was_cancelled = 2;
118   char* peer;
119 
120   grpc_arg args[] = {
121       grpc_channel_arg_string_create(
122           const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
123           const_cast<char*>(
124               "{\n"
125               "  \"methodConfig\": [ {\n"
126               "    \"name\": [\n"
127               "      { \"service\": \"service\", \"method\": \"method\" }\n"
128               "    ],\n"
129               "    \"retryPolicy\": {\n"
130               "      \"maxAttempts\": 3,\n"
131               "      \"initialBackoff\": \"10s\",\n"
132               "      \"maxBackoff\": \"120s\",\n"
133               "      \"backoffMultiplier\": 1.6,\n"
134               "      \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
135               "    },\n"
136               "    \"timeout\": \"5s\"\n"
137               "  } ]\n"
138               "}")),
139   };
140   grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
141   std::string name = absl::StrCat("retry_cancel_during_delay/", mode.name);
142   grpc_end2end_test_fixture f =
143       begin_test(config, name.c_str(), &client_args, nullptr);
144 
145   cq_verifier* cqv = cq_verifier_create(f.cq);
146 
147   gpr_timespec expect_finish_before = n_seconds_from_now(10);
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   GPR_ASSERT(c);
153 
154   peer = grpc_call_get_peer(c);
155   GPR_ASSERT(peer != nullptr);
156   gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
157   gpr_free(peer);
158 
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 
165   // Client starts a batch with all 6 ops.
166   memset(ops, 0, sizeof(ops));
167   op = ops;
168   op->op = GRPC_OP_SEND_INITIAL_METADATA;
169   op->data.send_initial_metadata.count = 0;
170   op++;
171   op->op = GRPC_OP_SEND_MESSAGE;
172   op->data.send_message.send_message = request_payload;
173   op++;
174   op->op = GRPC_OP_RECV_MESSAGE;
175   op->data.recv_message.recv_message = &response_payload_recv;
176   op++;
177   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
178   op++;
179   op->op = GRPC_OP_RECV_INITIAL_METADATA;
180   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
181   op++;
182   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
183   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
184   op->data.recv_status_on_client.status = &status;
185   op->data.recv_status_on_client.status_details = &details;
186   op++;
187   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
188                                 nullptr);
189   GPR_ASSERT(GRPC_CALL_OK == error);
190 
191   // Server gets a call and fails with retryable status.
192   error =
193       grpc_server_request_call(f.server, &s, &call_details,
194                                &request_metadata_recv, f.cq, f.cq, tag(101));
195   GPR_ASSERT(GRPC_CALL_OK == error);
196   CQ_EXPECT_COMPLETION(cqv, tag(101), true);
197   cq_verify(cqv);
198 
199   peer = grpc_call_get_peer(s);
200   GPR_ASSERT(peer != nullptr);
201   gpr_log(GPR_DEBUG, "server_peer=%s", peer);
202   gpr_free(peer);
203   peer = grpc_call_get_peer(c);
204   GPR_ASSERT(peer != nullptr);
205   gpr_log(GPR_DEBUG, "client_peer=%s", peer);
206   gpr_free(peer);
207 
208   memset(ops, 0, sizeof(ops));
209   op = ops;
210   op->op = GRPC_OP_SEND_INITIAL_METADATA;
211   op->data.send_initial_metadata.count = 0;
212   op++;
213   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
214   op->data.send_status_from_server.trailing_metadata_count = 0;
215   op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
216   op->data.send_status_from_server.status_details = &status_details;
217   op++;
218   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
219   op->data.recv_close_on_server.cancelled = &was_cancelled;
220   op++;
221   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
222                                 nullptr);
223   GPR_ASSERT(GRPC_CALL_OK == error);
224 
225   CQ_EXPECT_COMPLETION(cqv, tag(102), true);
226   cq_verify(cqv);
227 
228   grpc_call_unref(s);
229   grpc_metadata_array_destroy(&request_metadata_recv);
230   grpc_metadata_array_init(&request_metadata_recv);
231   grpc_call_details_destroy(&call_details);
232   grpc_call_details_init(&call_details);
233 
234   // Server should never get a second call, because the initial retry
235   // delay is longer than the call's deadline.
236   error =
237       grpc_server_request_call(f.server, &s, &call_details,
238                                &request_metadata_recv, f.cq, f.cq, tag(201));
239   GPR_ASSERT(GRPC_CALL_OK == error);
240   cq_verify_empty(cqv);
241 
242   // Initiate cancellation.
243   GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
244 
245   CQ_EXPECT_COMPLETION(cqv, tag(1), true);
246   cq_verify(cqv);
247 
248   // Make sure we didn't wait the full deadline before failing.
249   gpr_log(
250       GPR_INFO, "Expect completion before: %s",
251       absl::FormatTime(grpc_core::ToAbslTime(expect_finish_before)).c_str());
252   GPR_ASSERT(gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), expect_finish_before) <
253              0);
254 
255   gpr_log(GPR_INFO, "status=%d expected=%d", status, mode.expect_status);
256   GPR_ASSERT(status == mode.expect_status);
257   GPR_ASSERT(was_cancelled == 0);
258 
259   grpc_slice_unref(details);
260   grpc_metadata_array_destroy(&initial_metadata_recv);
261   grpc_metadata_array_destroy(&trailing_metadata_recv);
262   grpc_metadata_array_destroy(&request_metadata_recv);
263   grpc_call_details_destroy(&call_details);
264   grpc_byte_buffer_destroy(request_payload);
265   grpc_byte_buffer_destroy(response_payload);
266   grpc_byte_buffer_destroy(request_payload_recv);
267   grpc_byte_buffer_destroy(response_payload_recv);
268 
269   grpc_call_unref(c);
270 
271   cq_verifier_destroy(cqv);
272 
273   end_test(&f);
274   config.tear_down_data(&f);
275 }
276 
retry_cancel_during_delay(grpc_end2end_test_config config)277 void retry_cancel_during_delay(grpc_end2end_test_config config) {
278   GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
279   for (size_t i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); ++i) {
280     test_retry_cancel_during_delay(config, cancellation_modes[i]);
281   }
282 }
283 
retry_cancel_during_delay_pre_init(void)284 void retry_cancel_during_delay_pre_init(void) {}
285