Debian Codegraph @

test/core/end2end/tests/retry_cancel_during_delay.cc

raw
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 <stdint.h>
18 #include <string.h>
19
20 #include <string>
21
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/str_format.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/time/time.h"
26
27 #include <grpc/byte_buffer.h>
28 #include <grpc/grpc.h>
29 #include <grpc/impl/codegen/propagation_bits.h>
30 #include <grpc/slice.h>
31 #include <grpc/status.h>
32 #include <grpc/support/alloc.h>
33 #include <grpc/support/log.h>
34 #include <grpc/support/time.h>
35
36 #include "src/core/lib/channel/channel_args.h"
37 #include "src/core/lib/gpr/useful.h"
38 #include "src/core/lib/gprpp/time_util.h"
39 #include "src/core/lib/slice/slice_internal.h"
40 #include "test/core/end2end/cq_verifier.h"
41 #include "test/core/end2end/end2end_tests.h"
42 #include "test/core/end2end/tests/cancel_test_helpers.h"
43 #include "test/core/util/test_config.h"
44
45 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
46
47 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
48 const char* test_name,
49 grpc_channel_args* client_args,
50 grpc_channel_args* server_args) {
51 grpc_end2end_test_fixture f;
52 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
53 f = config.create_fixture(client_args, server_args);
54 config.init_server(&f, server_args);
55 config.init_client(&f, client_args);
56 return f;
57 }
58
59 static gpr_timespec n_seconds_from_now(int n) {
60 return grpc_timeout_seconds_to_deadline(n);
61 }
62
63 static gpr_timespec five_seconds_from_now(void) {
64 return n_seconds_from_now(5);
65 }
66
67 static void drain_cq(grpc_completion_queue* cq) {
68 grpc_event ev;
69 do {
70 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
71 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
72 }
73
74 static void shutdown_server(grpc_end2end_test_fixture* f) {
75 if (!f->server) return;
76 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
77 grpc_event ev;
78 do {
79 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
80 nullptr);
81 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
82 grpc_server_destroy(f->server);
83 f->server = nullptr;
84 }
85
86 static void shutdown_client(grpc_end2end_test_fixture* f) {
87 if (!f->client) return;
88 grpc_channel_destroy(f->client);
89 f->client = nullptr;
90 }
91
92 static void end_test(grpc_end2end_test_fixture* f) {
93 shutdown_server(f);
94 shutdown_client(f);
95
96 grpc_completion_queue_shutdown(f->cq);
97 drain_cq(f->cq);
98 grpc_completion_queue_destroy(f->cq);
99 }
100
101 // Tests retry cancellation during backoff.
102 static void test_retry_cancel_during_delay(grpc_end2end_test_config config,
103 cancellation_mode mode) {
104 grpc_call* c;
105 grpc_call* s;
106 grpc_op ops[6];
107 grpc_op* op;
108 grpc_metadata_array initial_metadata_recv;
109 grpc_metadata_array trailing_metadata_recv;
110 grpc_metadata_array request_metadata_recv;
111 grpc_call_details call_details;
112 grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
113 grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
114 grpc_byte_buffer* request_payload =
115 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
116 grpc_byte_buffer* response_payload =
117 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
118 grpc_byte_buffer* request_payload_recv = nullptr;
119 grpc_byte_buffer* response_payload_recv = nullptr;
120 grpc_status_code status;
121 grpc_call_error error;
122 grpc_slice details;
123 int was_cancelled = 2;
124 char* peer;
125
126 std::string service_config = absl::StrFormat(
127 "{\n"
128 " \"methodConfig\": [ {\n"
129 " \"name\": [\n"
130 " { \"service\": \"service\", \"method\": \"method\" }\n"
131 " ],\n"
132 " \"retryPolicy\": {\n"
133 " \"maxAttempts\": 3,\n"
134 " \"initialBackoff\": \"%ds\",\n"
135 " \"maxBackoff\": \"120s\",\n"
136 " \"backoffMultiplier\": 1.6,\n"
137 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
138 " }\n"
139 " } ]\n"
140 "}",
141 10 * grpc_test_slowdown_factor());
142
143 grpc_arg args[] = {
144 grpc_channel_arg_string_create(const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
145 const_cast<char*>(service_config.c_str())),
146 };
147 grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
148 std::string name = absl::StrCat("retry_cancel_during_delay/", mode.name);
149 grpc_end2end_test_fixture f =
150 begin_test(config, name.c_str(), &client_args, nullptr);
151
152 grpc_core::CqVerifier cqv(f.cq);
153
154 gpr_timespec expect_finish_before = n_seconds_from_now(10);
155 gpr_timespec deadline = five_seconds_from_now();
156 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
157 grpc_slice_from_static_string("/service/method"),
158 nullptr, deadline, nullptr);
159 GPR_ASSERT(c);
160
161 peer = grpc_call_get_peer(c);
162 GPR_ASSERT(peer != nullptr);
163 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
164 gpr_free(peer);
165
166 grpc_metadata_array_init(&initial_metadata_recv);
167 grpc_metadata_array_init(&trailing_metadata_recv);
168 grpc_metadata_array_init(&request_metadata_recv);
169 grpc_call_details_init(&call_details);
170 grpc_slice status_details = grpc_slice_from_static_string("xyz");
171
172 // Client starts a batch with all 6 ops.
173 memset(ops, 0, sizeof(ops));
174 op = ops;
175 op->op = GRPC_OP_SEND_INITIAL_METADATA;
176 op->data.send_initial_metadata.count = 0;
177 op++;
178 op->op = GRPC_OP_SEND_MESSAGE;
179 op->data.send_message.send_message = request_payload;
180 op++;
181 op->op = GRPC_OP_RECV_MESSAGE;
182 op->data.recv_message.recv_message = &response_payload_recv;
183 op++;
184 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
185 op++;
186 op->op = GRPC_OP_RECV_INITIAL_METADATA;
187 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
188 op++;
189 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
190 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
191 op->data.recv_status_on_client.status = &status;
192 op->data.recv_status_on_client.status_details = &details;
193 op++;
194 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
195 nullptr);
196 GPR_ASSERT(GRPC_CALL_OK == error);
197
198 // Server gets a call and fails with retryable status.
199 error =
200 grpc_server_request_call(f.server, &s, &call_details,
201 &request_metadata_recv, f.cq, f.cq, tag(101));
202 GPR_ASSERT(GRPC_CALL_OK == error);
203 cqv.Expect(tag(101), true);
204 cqv.Verify();
205
206 peer = grpc_call_get_peer(s);
207 GPR_ASSERT(peer != nullptr);
208 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
209 gpr_free(peer);
210 peer = grpc_call_get_peer(c);
211 GPR_ASSERT(peer != nullptr);
212 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
213 gpr_free(peer);
214
215 memset(ops, 0, sizeof(ops));
216 op = ops;
217 op->op = GRPC_OP_SEND_INITIAL_METADATA;
218 op->data.send_initial_metadata.count = 0;
219 op++;
220 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
221 op->data.send_status_from_server.trailing_metadata_count = 0;
222 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
223 op->data.send_status_from_server.status_details = &status_details;
224 op++;
225 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
226 op->data.recv_close_on_server.cancelled = &was_cancelled;
227 op++;
228 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
229 nullptr);
230 GPR_ASSERT(GRPC_CALL_OK == error);
231
232 cqv.Expect(tag(102), true);
233 cqv.Verify();
234
235 grpc_call_unref(s);
236 grpc_metadata_array_destroy(&request_metadata_recv);
237 grpc_metadata_array_init(&request_metadata_recv);
238 grpc_call_details_destroy(&call_details);
239 grpc_call_details_init(&call_details);
240
241 // Server should never get a second call, because the initial retry
242 // delay is longer than the call's deadline.
243 error =
244 grpc_server_request_call(f.server, &s, &call_details,
245 &request_metadata_recv, f.cq, f.cq, tag(201));
246 GPR_ASSERT(GRPC_CALL_OK == error);
247
248 // Initiate cancellation.
249 GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
250
251 cqv.Expect(tag(1), true);
252 cqv.Verify();
253
254 gpr_timespec finish_time = gpr_now(GPR_CLOCK_MONOTONIC);
255
256 gpr_log(GPR_INFO, "status=%d expected=%d", status, mode.expect_status);
257 gpr_log(GPR_INFO, "message=\"%s\"",
258 std::string(grpc_core::StringViewFromSlice(details)).c_str());
259 GPR_ASSERT(status == mode.expect_status);
260 GPR_ASSERT(was_cancelled == 0);
261
262 // Make sure we didn't wait the full deadline before failing.
263 gpr_log(
264 GPR_INFO, "Expect completion before: %s",
265 absl::FormatTime(grpc_core::ToAbslTime(expect_finish_before)).c_str());
266 GPR_ASSERT(gpr_time_cmp(finish_time, expect_finish_before) < 0);
267
268 grpc_slice_unref(details);
269 grpc_metadata_array_destroy(&initial_metadata_recv);
270 grpc_metadata_array_destroy(&trailing_metadata_recv);
271 grpc_metadata_array_destroy(&request_metadata_recv);
272 grpc_call_details_destroy(&call_details);
273 grpc_byte_buffer_destroy(request_payload);
274 grpc_byte_buffer_destroy(response_payload);
275 grpc_byte_buffer_destroy(request_payload_recv);
276 grpc_byte_buffer_destroy(response_payload_recv);
277
278 grpc_call_unref(c);
279
280 end_test(&f);
281 config.tear_down_data(&f);
282 }
283
284 void retry_cancel_during_delay(grpc_end2end_test_config config) {
285 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
286 for (size_t i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); ++i) {
287 test_retry_cancel_during_delay(config, cancellation_modes[i]);
288 }
289 }
290
291 void retry_cancel_during_delay_pre_init(void) {}