Debian Codegraph @

test/core/end2end/tests/retry_recv_message_replay.cc

raw
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
19 #include <stdint.h>
20 #include <string.h>
21
22 #include <new>
23
24 #include "absl/status/status.h"
25 #include "absl/types/optional.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
35 #include "src/core/lib/channel/channel_args.h"
36 #include "src/core/lib/channel/channel_fwd.h"
37 #include "src/core/lib/channel/channel_stack.h"
38 #include "src/core/lib/channel/channel_stack_builder.h"
39 #include "src/core/lib/config/core_configuration.h"
40 #include "src/core/lib/gpr/useful.h"
41 #include "src/core/lib/gprpp/status_helper.h"
42 #include "src/core/lib/iomgr/call_combiner.h"
43 #include "src/core/lib/iomgr/closure.h"
44 #include "src/core/lib/iomgr/error.h"
45 #include "src/core/lib/surface/channel_stack_type.h"
46 #include "src/core/lib/transport/transport.h"
47 #include "test/core/end2end/cq_verifier.h"
48 #include "test/core/end2end/end2end_tests.h"
49 #include "test/core/util/test_config.h"
50
51 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
52
53 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
54 const char* test_name,
55 grpc_channel_args* client_args,
56 grpc_channel_args* server_args) {
57 grpc_end2end_test_fixture f;
58 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
59 f = config.create_fixture(client_args, server_args);
60 config.init_server(&f, server_args);
61 config.init_client(&f, client_args);
62 return f;
63 }
64
65 static gpr_timespec n_seconds_from_now(int n) {
66 return grpc_timeout_seconds_to_deadline(n);
67 }
68
69 static gpr_timespec five_seconds_from_now(void) {
70 return n_seconds_from_now(5);
71 }
72
73 static void drain_cq(grpc_completion_queue* cq) {
74 grpc_event ev;
75 do {
76 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
77 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
78 }
79
80 static void shutdown_server(grpc_end2end_test_fixture* f) {
81 if (!f->server) return;
82 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
83 grpc_event ev;
84 do {
85 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
86 nullptr);
87 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
88 grpc_server_destroy(f->server);
89 f->server = nullptr;
90 }
91
92 static void shutdown_client(grpc_end2end_test_fixture* f) {
93 if (!f->client) return;
94 grpc_channel_destroy(f->client);
95 f->client = nullptr;
96 }
97
98 static void end_test(grpc_end2end_test_fixture* f) {
99 shutdown_server(f);
100 shutdown_client(f);
101
102 grpc_completion_queue_shutdown(f->cq);
103 drain_cq(f->cq);
104 grpc_completion_queue_destroy(f->cq);
105 }
106
107 // Tests the fix for a bug found in real-world code where recv_message
108 // was incorrectly replayed on a call attempt that it was already sent
109 // to when the recv_message completion had already been returned but was
110 // deferred at the point where recv_trailing_metadata was started from
111 // the surface. This resulted in ASAN failures caused by not unreffing
112 // a grpc_error.
113 static void test_retry_recv_message_replay(grpc_end2end_test_config config) {
114 grpc_call* c;
115 grpc_call* s;
116 grpc_op ops[6];
117 grpc_op* op;
118 grpc_metadata_array initial_metadata_recv;
119 grpc_metadata_array trailing_metadata_recv;
120 grpc_metadata_array request_metadata_recv;
121 grpc_call_details call_details;
122 grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
123 grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
124 grpc_byte_buffer* request_payload =
125 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
126 grpc_byte_buffer* response_payload =
127 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
128 grpc_byte_buffer* request_payload_recv = nullptr;
129 grpc_byte_buffer* response_payload_recv = nullptr;
130 grpc_status_code status;
131 grpc_call_error error;
132 grpc_slice details;
133 int was_cancelled = 2;
134 char* peer;
135
136 grpc_arg args[] = {
137 grpc_channel_arg_string_create(
138 const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
139 const_cast<char*>(
140 "{\n"
141 " \"methodConfig\": [ {\n"
142 " \"name\": [\n"
143 " { \"service\": \"service\", \"method\": \"method\" }\n"
144 " ],\n"
145 " \"retryPolicy\": {\n"
146 " \"maxAttempts\": 2,\n"
147 " \"initialBackoff\": \"1s\",\n"
148 " \"maxBackoff\": \"120s\",\n"
149 " \"backoffMultiplier\": 1.6,\n"
150 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
151 " }\n"
152 " } ]\n"
153 "}")),
154 };
155 grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
156 grpc_end2end_test_fixture f =
157 begin_test(config, "retry_recv_message_replay", &client_args, nullptr);
158
159 grpc_core::CqVerifier cqv(f.cq);
160
161 gpr_timespec deadline = five_seconds_from_now();
162 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
163 grpc_slice_from_static_string("/service/method"),
164 nullptr, deadline, nullptr);
165 GPR_ASSERT(c);
166
167 peer = grpc_call_get_peer(c);
168 GPR_ASSERT(peer != nullptr);
169 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
170 gpr_free(peer);
171
172 grpc_metadata_array_init(&initial_metadata_recv);
173 grpc_metadata_array_init(&trailing_metadata_recv);
174 grpc_metadata_array_init(&request_metadata_recv);
175 grpc_call_details_init(&call_details);
176 grpc_slice status_details = grpc_slice_from_static_string("xyz");
177
178 // Start a batch containing send_initial_metadata and recv_initial_metadata.
179 memset(ops, 0, sizeof(ops));
180 op = ops;
181 op->op = GRPC_OP_SEND_INITIAL_METADATA;
182 op->data.send_initial_metadata.count = 0;
183 op++;
184 op->op = GRPC_OP_RECV_INITIAL_METADATA;
185 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
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 // Start a batch containing recv_message.
192 memset(ops, 0, sizeof(ops));
193 op = ops;
194 op->op = GRPC_OP_RECV_MESSAGE;
195 op->data.recv_message.recv_message = &response_payload_recv;
196 op++;
197 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
198 nullptr);
199 GPR_ASSERT(GRPC_CALL_OK == error);
200
201 // Start a batch containing recv_trailing_metadata.
202 memset(ops, 0, sizeof(ops));
203 op = ops;
204 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
205 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
206 op->data.recv_status_on_client.status = &status;
207 op->data.recv_status_on_client.status_details = &details;
208 op++;
209 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
210 nullptr);
211 GPR_ASSERT(GRPC_CALL_OK == error);
212
213 // Server should get a call.
214 error =
215 grpc_server_request_call(f.server, &s, &call_details,
216 &request_metadata_recv, f.cq, f.cq, tag(101));
217 GPR_ASSERT(GRPC_CALL_OK == error);
218 cqv.Expect(tag(101), true);
219 cqv.Verify();
220
221 // Server fails with status ABORTED.
222 memset(ops, 0, sizeof(ops));
223 op = ops;
224 op->op = GRPC_OP_SEND_INITIAL_METADATA;
225 op->data.send_initial_metadata.count = 0;
226 op++;
227 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
228 op->data.send_status_from_server.trailing_metadata_count = 0;
229 op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
230 op->data.send_status_from_server.status_details = &status_details;
231 op++;
232 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
233 op->data.recv_close_on_server.cancelled = &was_cancelled;
234 op++;
235 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
236 nullptr);
237 GPR_ASSERT(GRPC_CALL_OK == error);
238
239 // In principle, the server batch should complete before the client
240 // batches, but in the proxy fixtures, there are multiple threads
241 // involved, so the completion order tends to be a little racy.
242 cqv.Expect(tag(102), true);
243 cqv.Expect(tag(1), true);
244 cqv.Expect(tag(2), true);
245 cqv.Expect(tag(3), true);
246 cqv.Verify();
247
248 GPR_ASSERT(status == GRPC_STATUS_ABORTED);
249 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
250 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
251 GPR_ASSERT(was_cancelled == 0);
252
253 grpc_slice_unref(details);
254 grpc_metadata_array_destroy(&initial_metadata_recv);
255 grpc_metadata_array_destroy(&trailing_metadata_recv);
256 grpc_metadata_array_destroy(&request_metadata_recv);
257 grpc_call_details_destroy(&call_details);
258 grpc_byte_buffer_destroy(request_payload);
259 grpc_byte_buffer_destroy(response_payload);
260 grpc_byte_buffer_destroy(request_payload_recv);
261 grpc_byte_buffer_destroy(response_payload_recv);
262
263 grpc_call_unref(c);
264 grpc_call_unref(s);
265
266 end_test(&f);
267 config.tear_down_data(&f);
268 }
269
270 namespace {
271
272 // A filter that, for the first call it sees, will fail the batch
273 // containing send_initial_metadata and then fail the call with status
274 // ABORTED. All subsequent calls are allowed through without failures.
275 class FailFirstSendOpFilter {
276 public:
277 static grpc_channel_filter kFilterVtable;
278
279 public:
280 class CallData {
281 public:
282 static grpc_error_handle Init(grpc_call_element* elem,
283 const grpc_call_element_args* args) {
284 new (elem->call_data) CallData(args);
285 return absl::OkStatus();
286 }
287
288 static void Destroy(grpc_call_element* elem,
289 const grpc_call_final_info* /*final_info*/,
290 grpc_closure* /*ignored*/) {
291 auto* calld = static_cast<CallData*>(elem->call_data);
292 calld->~CallData();
293 }
294
295 static void StartTransportStreamOpBatch(
296 grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
297 auto* chand = static_cast<FailFirstSendOpFilter*>(elem->channel_data);
298 auto* calld = static_cast<CallData*>(elem->call_data);
299 if (!chand->seen_first_) {
300 chand->seen_first_ = true;
301 calld->fail_ = true;
302 }
303 if (calld->fail_ && !batch->cancel_stream) {
304 grpc_transport_stream_op_batch_finish_with_failure(
305 batch,
306 grpc_error_set_int(
307 GRPC_ERROR_CREATE("FailFirstSendOpFilter failing batch"),
308 grpc_core::StatusIntProperty::kRpcStatus, GRPC_STATUS_ABORTED),
309 calld->call_combiner_);
310 return;
311 }
312 grpc_call_next_op(elem, batch);
313 }
314
315 private:
316 explicit CallData(const grpc_call_element_args* args)
317 : call_combiner_(args->call_combiner) {}
318
319 grpc_core::CallCombiner* call_combiner_;
320 bool fail_ = false;
321 };
322
323 static grpc_error_handle Init(grpc_channel_element* elem,
324 grpc_channel_element_args* /*args*/) {
325 new (elem->channel_data) FailFirstSendOpFilter();
326 return absl::OkStatus();
327 }
328
329 static void Destroy(grpc_channel_element* elem) {
330 auto* chand = static_cast<FailFirstSendOpFilter*>(elem->channel_data);
331 chand->~FailFirstSendOpFilter();
332 }
333
334 bool seen_first_ = false;
335 };
336
337 grpc_channel_filter FailFirstSendOpFilter::kFilterVtable = {
338 CallData::StartTransportStreamOpBatch,
339 nullptr,
340 grpc_channel_next_op,
341 sizeof(CallData),
342 CallData::Init,
343 grpc_call_stack_ignore_set_pollset_or_pollset_set,
344 CallData::Destroy,
345 sizeof(FailFirstSendOpFilter),
346 Init,
347 grpc_channel_stack_no_post_init,
348 Destroy,
349 grpc_channel_next_get_info,
350 "FailFirstSendOpFilter",
351 };
352
353 } // namespace
354
355 void retry_recv_message_replay(grpc_end2end_test_config config) {
356 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
357 grpc_core::CoreConfiguration::RunWithSpecialConfiguration(
358 [](grpc_core::CoreConfiguration::Builder* builder) {
359 grpc_core::BuildCoreConfiguration(builder);
360 builder->channel_init()->RegisterStage(
361 GRPC_CLIENT_SUBCHANNEL, 0,
362 [](grpc_core::ChannelStackBuilder* builder) {
363 // Skip on proxy (which explicitly disables retries).
364 if (!builder->channel_args()
365 .GetBool(GRPC_ARG_ENABLE_RETRIES)
366 .value_or(true)) {
367 return true;
368 }
369 // Install filter.
370 builder->PrependFilter(&FailFirstSendOpFilter::kFilterVtable);
371 return true;
372 });
373 },
374 [config] { test_retry_recv_message_replay(config); });
375 }
376
377 void retry_recv_message_replay_pre_init(void) {}