• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test/core/end2end/end2end_tests.h"
20 
21 #include <stdio.h>
22 #include <string.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/string_util.h>
29 #include <grpc/support/time.h>
30 
31 #include "src/core/lib/channel/channel_args.h"
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/gpr/useful.h"
34 #include "src/core/lib/iomgr/exec_ctx.h"
35 #include "src/core/lib/transport/static_metadata.h"
36 
37 #include "test/core/end2end/cq_verifier.h"
38 #include "test/core/end2end/tests/cancel_test_helpers.h"
39 
tag(intptr_t t)40 static void* tag(intptr_t t) { return (void*)t; }
41 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)42 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
43                                             const char* test_name,
44                                             grpc_channel_args* client_args,
45                                             grpc_channel_args* server_args) {
46   grpc_end2end_test_fixture f;
47   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
48   f = config.create_fixture(client_args, server_args);
49   config.init_server(&f, server_args);
50   config.init_client(&f, client_args);
51   return f;
52 }
53 
n_seconds_from_now(int n)54 static gpr_timespec n_seconds_from_now(int n) {
55   return grpc_timeout_seconds_to_deadline(n);
56 }
57 
five_seconds_from_now(void)58 static gpr_timespec five_seconds_from_now(void) {
59   return n_seconds_from_now(5);
60 }
61 
drain_cq(grpc_completion_queue * cq)62 static void drain_cq(grpc_completion_queue* cq) {
63   grpc_event ev;
64   do {
65     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
66   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
67 }
68 
shutdown_server(grpc_end2end_test_fixture * f)69 static void shutdown_server(grpc_end2end_test_fixture* f) {
70   if (!f->server) return;
71   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
72   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
73                                          grpc_timeout_seconds_to_deadline(5),
74                                          nullptr)
75                  .type == GRPC_OP_COMPLETE);
76   grpc_server_destroy(f->server);
77   f->server = nullptr;
78 }
79 
shutdown_client(grpc_end2end_test_fixture * f)80 static void shutdown_client(grpc_end2end_test_fixture* f) {
81   if (!f->client) return;
82   grpc_channel_destroy(f->client);
83   f->client = nullptr;
84 }
85 
end_test(grpc_end2end_test_fixture * f)86 static void end_test(grpc_end2end_test_fixture* f) {
87   shutdown_server(f);
88   shutdown_client(f);
89 
90   grpc_completion_queue_shutdown(f->cq);
91   drain_cq(f->cq);
92   grpc_completion_queue_destroy(f->cq);
93   grpc_completion_queue_destroy(f->shutdown_cq);
94 }
95 
96 // Tests that we don't retry when disabled by server push-back.
97 // - 2 retries allowed for ABORTED status
98 // - first attempt gets ABORTED
99 // - second attempt gets ABORTED but server push back disables retrying
test_retry_server_pushback_disabled(grpc_end2end_test_config config)100 static void test_retry_server_pushback_disabled(
101     grpc_end2end_test_config config) {
102   grpc_call* c;
103   grpc_call* s;
104   grpc_op ops[6];
105   grpc_op* op;
106   grpc_metadata_array initial_metadata_recv;
107   grpc_metadata_array trailing_metadata_recv;
108   grpc_metadata_array request_metadata_recv;
109   grpc_call_details call_details;
110   grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
111   grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
112   grpc_byte_buffer* request_payload =
113       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
114   grpc_byte_buffer* response_payload =
115       grpc_raw_byte_buffer_create(&response_payload_slice, 1);
116   grpc_byte_buffer* request_payload_recv = nullptr;
117   grpc_byte_buffer* response_payload_recv = nullptr;
118   grpc_status_code status;
119   grpc_call_error error;
120   grpc_slice details;
121   int was_cancelled = 2;
122   char* peer;
123 
124   grpc_metadata pushback_md;
125   memset(&pushback_md, 0, sizeof(pushback_md));
126   pushback_md.key = GRPC_MDSTR_GRPC_RETRY_PUSHBACK_MS;
127   pushback_md.value = grpc_slice_from_static_string("-1");
128 
129   grpc_arg arg;
130   arg.type = GRPC_ARG_STRING;
131   arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
132   arg.value.string = const_cast<char*>(
133       "{\n"
134       "  \"methodConfig\": [ {\n"
135       "    \"name\": [\n"
136       "      { \"service\": \"service\", \"method\": \"method\" }\n"
137       "    ],\n"
138       "    \"retryPolicy\": {\n"
139       "      \"maxAttempts\": 3,\n"
140       "      \"initialBackoff\": \"1s\",\n"
141       "      \"maxBackoff\": \"120s\",\n"
142       "      \"backoffMultiplier\": 1.6,\n"
143       "      \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
144       "    }\n"
145       "  } ]\n"
146       "}");
147   grpc_channel_args client_args = {1, &arg};
148   grpc_end2end_test_fixture f = begin_test(
149       config, "retry_server_pushback_disabled", &client_args, nullptr);
150 
151   cq_verifier* cqv = cq_verifier_create(f.cq);
152 
153   gpr_timespec deadline = five_seconds_from_now();
154   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
155                                grpc_slice_from_static_string("/service/method"),
156                                nullptr, deadline, nullptr);
157   GPR_ASSERT(c);
158 
159   peer = grpc_call_get_peer(c);
160   GPR_ASSERT(peer != nullptr);
161   gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
162   gpr_free(peer);
163 
164   grpc_metadata_array_init(&initial_metadata_recv);
165   grpc_metadata_array_init(&trailing_metadata_recv);
166   grpc_metadata_array_init(&request_metadata_recv);
167   grpc_call_details_init(&call_details);
168   grpc_slice status_details = grpc_slice_from_static_string("xyz");
169 
170   memset(ops, 0, sizeof(ops));
171   op = ops;
172   op->op = GRPC_OP_SEND_INITIAL_METADATA;
173   op->data.send_initial_metadata.count = 0;
174   op++;
175   op->op = GRPC_OP_SEND_MESSAGE;
176   op->data.send_message.send_message = request_payload;
177   op++;
178   op->op = GRPC_OP_RECV_MESSAGE;
179   op->data.recv_message.recv_message = &response_payload_recv;
180   op++;
181   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
182   op++;
183   op->op = GRPC_OP_RECV_INITIAL_METADATA;
184   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
185   op++;
186   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
187   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
188   op->data.recv_status_on_client.status = &status;
189   op->data.recv_status_on_client.status_details = &details;
190   op++;
191   error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
192   GPR_ASSERT(GRPC_CALL_OK == error);
193 
194   error =
195       grpc_server_request_call(f.server, &s, &call_details,
196                                &request_metadata_recv, f.cq, f.cq, tag(101));
197   GPR_ASSERT(GRPC_CALL_OK == error);
198   CQ_EXPECT_COMPLETION(cqv, tag(101), true);
199   cq_verify(cqv);
200 
201   peer = grpc_call_get_peer(s);
202   GPR_ASSERT(peer != nullptr);
203   gpr_log(GPR_DEBUG, "server_peer=%s", peer);
204   gpr_free(peer);
205   peer = grpc_call_get_peer(c);
206   GPR_ASSERT(peer != nullptr);
207   gpr_log(GPR_DEBUG, "client_peer=%s", peer);
208   gpr_free(peer);
209 
210   memset(ops, 0, sizeof(ops));
211   op = ops;
212   op->op = GRPC_OP_SEND_INITIAL_METADATA;
213   op->data.send_initial_metadata.count = 0;
214   op++;
215   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
216   op->data.send_status_from_server.trailing_metadata_count = 0;
217   op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
218   op->data.send_status_from_server.status_details = &status_details;
219   op++;
220   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
221   op->data.recv_close_on_server.cancelled = &was_cancelled;
222   op++;
223   error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
224   GPR_ASSERT(GRPC_CALL_OK == error);
225 
226   CQ_EXPECT_COMPLETION(cqv, tag(102), true);
227   cq_verify(cqv);
228 
229   grpc_call_unref(s);
230   grpc_metadata_array_destroy(&request_metadata_recv);
231   grpc_metadata_array_init(&request_metadata_recv);
232   grpc_call_details_destroy(&call_details);
233   grpc_call_details_init(&call_details);
234 
235   error =
236       grpc_server_request_call(f.server, &s, &call_details,
237                                &request_metadata_recv, f.cq, f.cq, tag(201));
238   GPR_ASSERT(GRPC_CALL_OK == error);
239   CQ_EXPECT_COMPLETION(cqv, tag(201), true);
240   cq_verify(cqv);
241 
242   peer = grpc_call_get_peer(s);
243   GPR_ASSERT(peer != nullptr);
244   gpr_log(GPR_DEBUG, "server_peer=%s", peer);
245   gpr_free(peer);
246   peer = grpc_call_get_peer(c);
247   GPR_ASSERT(peer != nullptr);
248   gpr_log(GPR_DEBUG, "client_peer=%s", peer);
249   gpr_free(peer);
250 
251   memset(ops, 0, sizeof(ops));
252   op = ops;
253   op->op = GRPC_OP_SEND_INITIAL_METADATA;
254   op->data.send_initial_metadata.count = 0;
255   op++;
256   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
257   op->data.send_status_from_server.trailing_metadata_count = 1;
258   op->data.send_status_from_server.trailing_metadata = &pushback_md;
259   op->data.send_status_from_server.status = GRPC_STATUS_ABORTED;
260   op->data.send_status_from_server.status_details = &status_details;
261   op++;
262   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
263   op->data.recv_close_on_server.cancelled = &was_cancelled;
264   op++;
265   error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr);
266   GPR_ASSERT(GRPC_CALL_OK == error);
267 
268   CQ_EXPECT_COMPLETION(cqv, tag(202), true);
269   CQ_EXPECT_COMPLETION(cqv, tag(1), true);
270   cq_verify(cqv);
271 
272   GPR_ASSERT(status == GRPC_STATUS_ABORTED);
273   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
274   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
275   GPR_ASSERT(0 == call_details.flags);
276   GPR_ASSERT(was_cancelled == 1);
277 
278   grpc_slice_unref(details);
279   grpc_metadata_array_destroy(&initial_metadata_recv);
280   grpc_metadata_array_destroy(&trailing_metadata_recv);
281   grpc_metadata_array_destroy(&request_metadata_recv);
282   grpc_call_details_destroy(&call_details);
283   grpc_byte_buffer_destroy(request_payload);
284   grpc_byte_buffer_destroy(response_payload);
285   grpc_byte_buffer_destroy(request_payload_recv);
286   grpc_byte_buffer_destroy(response_payload_recv);
287 
288   grpc_call_unref(c);
289   grpc_call_unref(s);
290 
291   cq_verifier_destroy(cqv);
292 
293   end_test(&f);
294   config.tear_down_data(&f);
295 }
296 
retry_server_pushback_disabled(grpc_end2end_test_config config)297 void retry_server_pushback_disabled(grpc_end2end_test_config config) {
298   GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
299   test_retry_server_pushback_disabled(config);
300 }
301 
retry_server_pushback_disabled_pre_init(void)302 void retry_server_pushback_disabled_pre_init(void) {}
303