Debian Codegraph @

test/core/end2end/tests/max_connection_age.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 <limits.h>
20 #include <stdint.h>
21 #include <string.h>
22
23 #include <memory>
24
25 #include <grpc/grpc.h>
26 #include <grpc/impl/codegen/propagation_bits.h>
27 #include <grpc/slice.h>
28 #include <grpc/status.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
31
32 #include "src/core/lib/gpr/useful.h"
33 #include "src/core/lib/gprpp/time.h"
34 #include "test/core/end2end/cq_verifier.h"
35 #include "test/core/end2end/end2end_tests.h"
36 #include "test/core/util/test_config.h"
37
38 #define MAX_CONNECTION_AGE_MS 500
39 #define MAX_CONNECTION_AGE_GRACE_MS 2000
40 #define MAX_CONNECTION_IDLE_MS 9999
41
42 #define MAX_CONNECTION_AGE_JITTER_MULTIPLIER 1.1
43 #define CALL_DEADLINE_S 30
44 /* The amount of time we wait for the connection to time out, but after it the
45 connection should not use up its grace period. It should be a number between
46 MAX_CONNECTION_AGE_MS and MAX_CONNECTION_AGE_MS +
47 MAX_CONNECTION_AGE_GRACE_MS */
48 #define CQ_MAX_CONNECTION_AGE_WAIT_TIME_S 1
49 /* The amount of time we wait after the connection reaches its max age, it
50 should be shorter than CALL_DEADLINE_S - CQ_MAX_CONNECTION_AGE_WAIT_TIME_S */
51 #define CQ_MAX_CONNECTION_AGE_GRACE_WAIT_TIME_S 2
52 /* The grace period for the test to observe the channel shutdown process */
53 #define IMMEDIATE_SHUTDOWN_GRACE_TIME_MS 3000
54
55 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
56
57 static void drain_cq(grpc_completion_queue* cq) {
58 grpc_event ev;
59 do {
60 ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
61 nullptr);
62 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
63 }
64
65 static void shutdown_server(grpc_end2end_test_fixture* f) {
66 if (!f->server) return;
67 grpc_server_destroy(f->server);
68 f->server = nullptr;
69 }
70
71 static void shutdown_client(grpc_end2end_test_fixture* f) {
72 if (!f->client) return;
73 grpc_channel_destroy(f->client);
74 f->client = nullptr;
75 }
76
77 static void end_test(grpc_end2end_test_fixture* f) {
78 shutdown_server(f);
79 shutdown_client(f);
80
81 grpc_completion_queue_shutdown(f->cq);
82 drain_cq(f->cq);
83 grpc_completion_queue_destroy(f->cq);
84 }
85
86 static void test_max_age_forcibly_close(grpc_end2end_test_config config) {
87 grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
88 auto cqv = std::make_unique<grpc_core::CqVerifier>(f.cq);
89 grpc_arg server_a[3];
90 server_a[0].type = GRPC_ARG_INTEGER;
91 server_a[0].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_AGE_MS);
92 server_a[0].value.integer = MAX_CONNECTION_AGE_MS;
93 server_a[1].type = GRPC_ARG_INTEGER;
94 server_a[1].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_AGE_GRACE_MS);
95 server_a[1].value.integer = MAX_CONNECTION_AGE_GRACE_MS;
96 server_a[2].type = GRPC_ARG_INTEGER;
97 server_a[2].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_IDLE_MS);
98 server_a[2].value.integer = MAX_CONNECTION_IDLE_MS;
99 grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
100
101 config.init_client(&f, nullptr);
102 config.init_server(&f, &server_args);
103
104 grpc_call* c;
105 grpc_call* s;
106 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(CALL_DEADLINE_S);
107 grpc_op ops[6];
108 grpc_op* op;
109 grpc_metadata_array initial_metadata_recv;
110 grpc_metadata_array trailing_metadata_recv;
111 grpc_metadata_array request_metadata_recv;
112 grpc_call_details call_details;
113 grpc_status_code status;
114 grpc_call_error error;
115 grpc_slice details;
116 int was_cancelled = 2;
117
118 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
119 grpc_slice_from_static_string("/foo"), nullptr,
120 deadline, nullptr);
121 GPR_ASSERT(c);
122
123 grpc_metadata_array_init(&initial_metadata_recv);
124 grpc_metadata_array_init(&trailing_metadata_recv);
125 grpc_metadata_array_init(&request_metadata_recv);
126 grpc_call_details_init(&call_details);
127
128 memset(ops, 0, sizeof(ops));
129 op = ops;
130 op->op = GRPC_OP_SEND_INITIAL_METADATA;
131 op->data.send_initial_metadata.count = 0;
132 op->data.send_initial_metadata.metadata = nullptr;
133 op->flags = 0;
134 op->reserved = nullptr;
135 op++;
136 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
137 op->flags = 0;
138 op->reserved = nullptr;
139 op++;
140 op->op = GRPC_OP_RECV_INITIAL_METADATA;
141 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
142 op->flags = 0;
143 op->reserved = nullptr;
144 op++;
145 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
146 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
147 op->data.recv_status_on_client.status = &status;
148 op->data.recv_status_on_client.status_details = &details;
149 op->flags = 0;
150 op->reserved = nullptr;
151 op++;
152 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
153 nullptr);
154 GPR_ASSERT(GRPC_CALL_OK == error);
155
156 error =
157 grpc_server_request_call(f.server, &s, &call_details,
158 &request_metadata_recv, f.cq, f.cq, tag(101));
159 GPR_ASSERT(GRPC_CALL_OK == error);
160 cqv->Expect(tag(101), true);
161 cqv->Verify();
162
163 gpr_timespec expect_shutdown_time = grpc_timeout_milliseconds_to_deadline(
164 static_cast<int>(MAX_CONNECTION_AGE_MS *
165 MAX_CONNECTION_AGE_JITTER_MULTIPLIER) +
166 MAX_CONNECTION_AGE_GRACE_MS + IMMEDIATE_SHUTDOWN_GRACE_TIME_MS);
167
168 /* Wait for the channel to reach its max age */
169 cqv->VerifyEmpty(
170 grpc_core::Duration::Seconds(CQ_MAX_CONNECTION_AGE_WAIT_TIME_S));
171
172 /* After the channel reaches its max age, we still do nothing here. And wait
173 for it to use up its max age grace period. */
174 cqv->Expect(tag(1), true);
175 cqv->Verify();
176
177 gpr_timespec channel_shutdown_time = gpr_now(GPR_CLOCK_MONOTONIC);
178 GPR_ASSERT(gpr_time_cmp(channel_shutdown_time, expect_shutdown_time) < 0);
179
180 memset(ops, 0, sizeof(ops));
181 op = ops;
182 op->op = GRPC_OP_SEND_INITIAL_METADATA;
183 op->data.send_initial_metadata.count = 0;
184 op->flags = 0;
185 op->reserved = nullptr;
186 op++;
187 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
188 op->data.send_status_from_server.trailing_metadata_count = 0;
189 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
190 grpc_slice status_details = grpc_slice_from_static_string("xyz");
191 op->data.send_status_from_server.status_details = &status_details;
192 op->flags = 0;
193 op->reserved = nullptr;
194 op++;
195 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
196 op->data.recv_close_on_server.cancelled = &was_cancelled;
197 op->flags = 0;
198 op->reserved = nullptr;
199 op++;
200 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
201 nullptr);
202 GPR_ASSERT(GRPC_CALL_OK == error);
203 cqv->Expect(tag(102), true);
204 cqv->Verify();
205
206 grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
207 cqv->Expect(tag(0xdead), true);
208 cqv->Verify();
209
210 grpc_call_unref(s);
211
212 /* The connection should be closed immediately after the max age grace period,
213 the in-progress RPC should fail. */
214 GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
215 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
216 GPR_ASSERT(was_cancelled == 1);
217
218 grpc_slice_unref(details);
219 grpc_metadata_array_destroy(&initial_metadata_recv);
220 grpc_metadata_array_destroy(&trailing_metadata_recv);
221 grpc_metadata_array_destroy(&request_metadata_recv);
222 grpc_call_details_destroy(&call_details);
223 grpc_call_unref(c);
224 cqv.reset();
225 end_test(&f);
226 config.tear_down_data(&f);
227 }
228
229 static void test_max_age_gracefully_close(grpc_end2end_test_config config) {
230 grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
231 auto cqv = std::make_unique<grpc_core::CqVerifier>(f.cq);
232 grpc_arg server_a[3];
233 server_a[0].type = GRPC_ARG_INTEGER;
234 server_a[0].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_AGE_MS);
235 server_a[0].value.integer = MAX_CONNECTION_AGE_MS;
236 server_a[1].type = GRPC_ARG_INTEGER;
237 server_a[1].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_AGE_GRACE_MS);
238 server_a[1].value.integer = INT_MAX;
239 server_a[2].type = GRPC_ARG_INTEGER;
240 server_a[2].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_IDLE_MS);
241 server_a[2].value.integer = MAX_CONNECTION_IDLE_MS;
242 grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
243
244 config.init_client(&f, nullptr);
245 config.init_server(&f, &server_args);
246
247 grpc_call* c;
248 grpc_call* s;
249 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(CALL_DEADLINE_S);
250 grpc_op ops[6];
251 grpc_op* op;
252 grpc_metadata_array initial_metadata_recv;
253 grpc_metadata_array trailing_metadata_recv;
254 grpc_metadata_array request_metadata_recv;
255 grpc_call_details call_details;
256 grpc_status_code status;
257 grpc_call_error error;
258 grpc_slice details;
259 int was_cancelled = 2;
260
261 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
262 grpc_slice_from_static_string("/foo"), nullptr,
263 deadline, nullptr);
264 GPR_ASSERT(c);
265
266 grpc_metadata_array_init(&initial_metadata_recv);
267 grpc_metadata_array_init(&trailing_metadata_recv);
268 grpc_metadata_array_init(&request_metadata_recv);
269 grpc_call_details_init(&call_details);
270
271 memset(ops, 0, sizeof(ops));
272 op = ops;
273 op->op = GRPC_OP_SEND_INITIAL_METADATA;
274 op->data.send_initial_metadata.count = 0;
275 op->data.send_initial_metadata.metadata = nullptr;
276 op->flags = 0;
277 op->reserved = nullptr;
278 op++;
279 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
280 op->flags = 0;
281 op->reserved = nullptr;
282 op++;
283 op->op = GRPC_OP_RECV_INITIAL_METADATA;
284 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
285 op->flags = 0;
286 op->reserved = nullptr;
287 op++;
288 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
289 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
290 op->data.recv_status_on_client.status = &status;
291 op->data.recv_status_on_client.status_details = &details;
292 op->flags = 0;
293 op->reserved = nullptr;
294 op++;
295 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
296 nullptr);
297 GPR_ASSERT(GRPC_CALL_OK == error);
298
299 error =
300 grpc_server_request_call(f.server, &s, &call_details,
301 &request_metadata_recv, f.cq, f.cq, tag(101));
302 GPR_ASSERT(GRPC_CALL_OK == error);
303 cqv->Expect(tag(101), true);
304 cqv->Verify();
305
306 /* Wait for the channel to reach its max age */
307 cqv->VerifyEmpty(
308 grpc_core::Duration::Seconds(CQ_MAX_CONNECTION_AGE_WAIT_TIME_S));
309
310 /* The connection is shutting down gracefully. In-progress rpc should not be
311 closed, hence the completion queue should see nothing here. */
312 cqv->VerifyEmpty(
313 grpc_core::Duration::Seconds(CQ_MAX_CONNECTION_AGE_GRACE_WAIT_TIME_S));
314
315 memset(ops, 0, sizeof(ops));
316 op = ops;
317 op->op = GRPC_OP_SEND_INITIAL_METADATA;
318 op->data.send_initial_metadata.count = 0;
319 op->flags = 0;
320 op->reserved = nullptr;
321 op++;
322 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
323 op->data.send_status_from_server.trailing_metadata_count = 0;
324 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
325 grpc_slice status_details = grpc_slice_from_static_string("xyz");
326 op->data.send_status_from_server.status_details = &status_details;
327 op->flags = 0;
328 op->reserved = nullptr;
329 op++;
330 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
331 op->data.recv_close_on_server.cancelled = &was_cancelled;
332 op->flags = 0;
333 op->reserved = nullptr;
334 op++;
335 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
336 nullptr);
337 GPR_ASSERT(GRPC_CALL_OK == error);
338
339 cqv->Expect(tag(102), true);
340 cqv->Expect(tag(1), true);
341 cqv->Verify();
342
343 grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
344 cqv->Expect(tag(0xdead), true);
345 cqv->Verify();
346
347 grpc_call_unref(s);
348
349 /* The connection is closed gracefully with goaway, the rpc should still be
350 completed. */
351 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
352 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
353 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
354 GPR_ASSERT(was_cancelled == 0);
355
356 grpc_slice_unref(details);
357 grpc_metadata_array_destroy(&initial_metadata_recv);
358 grpc_metadata_array_destroy(&trailing_metadata_recv);
359 grpc_metadata_array_destroy(&request_metadata_recv);
360 grpc_call_details_destroy(&call_details);
361 grpc_call_unref(c);
362 cqv.reset();
363 end_test(&f);
364 config.tear_down_data(&f);
365 }
366
367 void max_connection_age(grpc_end2end_test_config config) {
368 test_max_age_forcibly_close(config);
369 test_max_age_gracefully_close(config);
370 }
371
372 void max_connection_age_pre_init(void) {}