Debian Codegraph @

test/core/transport/chttp2/too_many_pings_test.cc

raw
1 /*
2 *
3 * Copyright 2020 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 <grpc/support/port_platform.h>
20
21 #include <stdint.h>
22 #include <string.h>
23
24 #include <algorithm>
25 #include <map>
26 #include <string>
27 #include <utility>
28 #include <vector>
29
30 #include "absl/status/status.h"
31 #include "absl/status/statusor.h"
32 #include "absl/strings/str_cat.h"
33 #include "absl/time/time.h"
34 #include "gtest/gtest.h"
35
36 #include <grpc/byte_buffer.h>
37 #include <grpc/grpc.h>
38 #include <grpc/grpc_security.h>
39 #include <grpc/impl/codegen/propagation_bits.h>
40 #include <grpc/slice.h>
41 #include <grpc/status.h>
42 #include <grpc/support/log.h>
43 #include <grpc/support/time.h>
44
45 #include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
46 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
47 #include "src/core/lib/address_utils/parse_address.h"
48 #include "src/core/lib/channel/channel_args.h"
49 #include "src/core/lib/gpr/useful.h"
50 #include "src/core/lib/gprpp/host_port.h"
51 #include "src/core/lib/gprpp/ref_counted_ptr.h"
52 #include "src/core/lib/gprpp/sync.h"
53 #include "src/core/lib/gprpp/time.h"
54 #include "src/core/lib/iomgr/exec_ctx.h"
55 #include "src/core/lib/iomgr/resolved_address.h"
56 #include "src/core/lib/resolver/resolver.h"
57 #include "src/core/lib/resolver/server_address.h"
58 #include "src/core/lib/surface/channel.h"
59 #include "src/core/lib/uri/uri_parser.h"
60 #include "test/core/end2end/cq_verifier.h"
61 #include "test/core/util/port.h"
62 #include "test/core/util/test_config.h"
63
64 namespace {
65
66 class TransportCounter {
67 public:
68 static void CounterInitCallback() {
69 grpc_core::MutexLock lock(&mu());
70 ++count_;
71 }
72
73 static void CounterDestructCallback() {
74 grpc_core::MutexLock lock(&mu());
75 if (--count_ == 0) {
76 cv().SignalAll();
77 }
78 }
79
80 static void WaitForTransportsToBeDestroyed() {
81 grpc_core::MutexLock lock(&mu());
82 while (count_ != 0) {
83 ASSERT_FALSE(cv().WaitWithTimeout(&mu(), absl::Seconds(10)));
84 }
85 }
86
87 static int count() {
88 grpc_core::MutexLock lock(&mu());
89 return count_;
90 }
91
92 static grpc_core::Mutex& mu() {
93 static grpc_core::Mutex* mu = new grpc_core::Mutex();
94 return *mu;
95 }
96
97 static grpc_core::CondVar& cv() {
98 static grpc_core::CondVar* cv = new grpc_core::CondVar();
99 return *cv;
100 }
101
102 private:
103 static int count_;
104 };
105
106 int TransportCounter::count_ = 0;
107
108 void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
109
110 // Perform a simple RPC where the server cancels the request with
111 // grpc_call_cancel_with_status
112 grpc_status_code PerformCall(grpc_channel* channel, grpc_server* server,
113 grpc_completion_queue* cq) {
114 grpc_call* c;
115 grpc_call* s;
116 grpc_core::CqVerifier cqv(cq);
117 grpc_op ops[6];
118 grpc_op* op;
119 grpc_metadata_array initial_metadata_recv;
120 grpc_metadata_array trailing_metadata_recv;
121 grpc_metadata_array request_metadata_recv;
122 grpc_call_details call_details;
123 grpc_status_code status;
124 grpc_call_error error;
125 grpc_slice details;
126 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
127 // Start a call
128 c = grpc_channel_create_call(channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
129 grpc_slice_from_static_string("/foo"), nullptr,
130 deadline, nullptr);
131 GPR_ASSERT(c);
132 grpc_metadata_array_init(&initial_metadata_recv);
133 grpc_metadata_array_init(&trailing_metadata_recv);
134 grpc_metadata_array_init(&request_metadata_recv);
135 grpc_call_details_init(&call_details);
136 memset(ops, 0, sizeof(ops));
137 op = ops;
138 op->op = GRPC_OP_SEND_INITIAL_METADATA;
139 op->data.send_initial_metadata.count = 0;
140 op->flags = 0;
141 op->reserved = nullptr;
142 op++;
143 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
144 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
145 op->data.recv_status_on_client.status = &status;
146 op->data.recv_status_on_client.status_details = &details;
147 op->flags = 0;
148 op->reserved = nullptr;
149 op++;
150 op->op = GRPC_OP_RECV_INITIAL_METADATA;
151 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
152 op->flags = 0;
153 op->reserved = nullptr;
154 op++;
155 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
156 nullptr);
157 GPR_ASSERT(GRPC_CALL_OK == error);
158 // Request a call on the server
159 error = grpc_server_request_call(server, &s, &call_details,
160 &request_metadata_recv, cq, cq, tag(101));
161 GPR_ASSERT(GRPC_CALL_OK == error);
162 cqv.Expect(tag(101), true);
163 cqv.Verify();
164 grpc_call_cancel_with_status(s, GRPC_STATUS_PERMISSION_DENIED, "test status",
165 nullptr);
166 cqv.Expect(tag(1), true);
167 cqv.Verify();
168 // cleanup
169 grpc_slice_unref(details);
170 grpc_metadata_array_destroy(&initial_metadata_recv);
171 grpc_metadata_array_destroy(&trailing_metadata_recv);
172 grpc_metadata_array_destroy(&request_metadata_recv);
173 grpc_call_details_destroy(&call_details);
174 grpc_call_unref(c);
175 grpc_call_unref(s);
176 return status;
177 }
178
179 // Test that sending a lot of RPCs that are cancelled by the server doesn't
180 // result in too many pings due to the pings sent by BDP.
181 TEST(TooManyPings, TestLotsOfServerCancelledRpcsDoesntGiveTooManyPings) {
182 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
183 // create the server
184 grpc_server* server = grpc_server_create(nullptr, nullptr);
185 std::string server_address =
186 grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
187 grpc_server_register_completion_queue(server, cq, nullptr);
188 grpc_server_credentials* server_creds =
189 grpc_insecure_server_credentials_create();
190 GPR_ASSERT(
191 grpc_server_add_http2_port(server, server_address.c_str(), server_creds));
192 grpc_server_credentials_release(server_creds);
193 grpc_server_start(server);
194 // create the channel (bdp pings are enabled by default)
195 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
196 grpc_channel* channel = grpc_channel_create(server_address.c_str(), creds,
197 nullptr /* channel args */);
198 grpc_channel_credentials_release(creds);
199 std::map<grpc_status_code, int> statuses_and_counts;
200 const int kNumTotalRpcs = 1e5;
201 // perform an RPC
202 gpr_log(GPR_INFO,
203 "Performing %d total RPCs and expecting them all to receive status "
204 "PERMISSION_DENIED (%d)",
205 kNumTotalRpcs, GRPC_STATUS_PERMISSION_DENIED);
206 for (int i = 0; i < kNumTotalRpcs; i++) {
207 grpc_status_code status = PerformCall(channel, server, cq);
208 statuses_and_counts[status] += 1;
209 }
210 int num_not_cancelled = 0;
211 for (auto itr = statuses_and_counts.begin(); itr != statuses_and_counts.end();
212 itr++) {
213 if (itr->first != GRPC_STATUS_PERMISSION_DENIED) {
214 num_not_cancelled += itr->second;
215 }
216 gpr_log(GPR_INFO, "%d / %d RPCs received status code: %d", itr->second,
217 kNumTotalRpcs, itr->first);
218 }
219 if (num_not_cancelled > 0) {
220 gpr_log(GPR_ERROR,
221 "Expected all RPCs to receive status PERMISSION_DENIED (%d) but %d "
222 "received other status codes",
223 GRPC_STATUS_PERMISSION_DENIED, num_not_cancelled);
224 FAIL();
225 }
226 // shutdown and destroy the client and server
227 grpc_channel_destroy(channel);
228 grpc_server_shutdown_and_notify(server, cq, nullptr);
229 grpc_completion_queue_shutdown(cq);
230 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
231 nullptr)
232 .type != GRPC_QUEUE_SHUTDOWN) {
233 }
234 grpc_server_destroy(server);
235 grpc_completion_queue_destroy(cq);
236 }
237
238 // Perform a simple RPC where the client makes a request, and both the client
239 // and server continue reading so that gRPC can send and receive keepalive
240 // pings.
241 grpc_status_code PerformWaitingCall(grpc_channel* channel, grpc_server* server,
242 grpc_completion_queue* cq) {
243 grpc_call* c;
244 grpc_call* s;
245 grpc_core::CqVerifier cqv(cq);
246 grpc_op ops[6];
247 grpc_op* op;
248 grpc_metadata_array initial_metadata_recv;
249 grpc_metadata_array trailing_metadata_recv;
250 grpc_metadata_array request_metadata_recv;
251 grpc_call_details call_details;
252 grpc_status_code status;
253 grpc_call_error error;
254 grpc_slice details;
255 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(15);
256 // Start a call
257 c = grpc_channel_create_call(channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
258 grpc_slice_from_static_string("/foo"), nullptr,
259 deadline, nullptr);
260 GPR_ASSERT(c);
261 grpc_metadata_array_init(&initial_metadata_recv);
262 grpc_metadata_array_init(&trailing_metadata_recv);
263 grpc_metadata_array_init(&request_metadata_recv);
264 grpc_call_details_init(&call_details);
265 memset(ops, 0, sizeof(ops));
266 op = ops;
267 op->op = GRPC_OP_SEND_INITIAL_METADATA;
268 op->data.send_initial_metadata.count = 0;
269 op->flags = 0;
270 op->reserved = nullptr;
271 op++;
272 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
273 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
274 op->data.recv_status_on_client.status = &status;
275 op->data.recv_status_on_client.status_details = &details;
276 op->flags = 0;
277 op->reserved = nullptr;
278 op++;
279 op->op = GRPC_OP_RECV_INITIAL_METADATA;
280 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
281 op->flags = 0;
282 op->reserved = nullptr;
283 op++;
284 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
285 nullptr);
286 GPR_ASSERT(GRPC_CALL_OK == error);
287 // Request a call on the server
288 error = grpc_server_request_call(server, &s, &call_details,
289 &request_metadata_recv, cq, cq, tag(101));
290 GPR_ASSERT(GRPC_CALL_OK == error);
291 cqv.Expect(tag(101), true);
292 cqv.Verify();
293 // Since the server is configured to allow only a single ping strike, it would
294 // take 3 pings to trigger the GOAWAY frame with "too_many_pings" from the
295 // server. (The second ping from the client would be the first bad ping sent
296 // too quickly leading to a ping strike and the third ping would lead to the
297 // GOAWAY.) If the client settings match with the server's settings, there
298 // won't be a bad ping, and the call will end due to the deadline expiring
299 // instead.
300 cqv.Expect(tag(1), true);
301 // The call will end after this
302 cqv.Verify(grpc_core::Duration::Seconds(60));
303 // cleanup
304 grpc_slice_unref(details);
305 grpc_metadata_array_destroy(&initial_metadata_recv);
306 grpc_metadata_array_destroy(&trailing_metadata_recv);
307 grpc_metadata_array_destroy(&request_metadata_recv);
308 grpc_call_details_destroy(&call_details);
309 grpc_call_unref(c);
310 grpc_call_unref(s);
311 return status;
312 }
313
314 // Shuts down and destroys the server.
315 void ServerShutdownAndDestroy(grpc_server* server, grpc_completion_queue* cq) {
316 // Shutdown and destroy server
317 grpc_server_shutdown_and_notify(server, cq, reinterpret_cast<void*>(1000));
318 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
319 nullptr)
320 .tag != reinterpret_cast<void*>(1000)) {
321 }
322 grpc_server_destroy(server);
323 }
324
325 void VerifyChannelReady(grpc_channel* channel, grpc_completion_queue* cq) {
326 grpc_connectivity_state state =
327 grpc_channel_check_connectivity_state(channel, 1 /* try_to_connect */);
328 while (state != GRPC_CHANNEL_READY) {
329 grpc_channel_watch_connectivity_state(
330 channel, state, grpc_timeout_seconds_to_deadline(5), cq, nullptr);
331 grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
332 nullptr);
333 state = grpc_channel_check_connectivity_state(channel, 0);
334 }
335 }
336
337 void VerifyChannelDisconnected(grpc_channel* channel,
338 grpc_completion_queue* cq) {
339 // Verify channel gets disconnected. Use a ping to make sure that clients
340 // tries sending/receiving bytes if the channel is connected.
341 grpc_channel_ping(channel, cq, reinterpret_cast<void*>(2000), nullptr);
342 grpc_event ev = grpc_completion_queue_next(
343 cq, grpc_timeout_seconds_to_deadline(5), nullptr);
344 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
345 GPR_ASSERT(ev.tag == reinterpret_cast<void*>(2000));
346 GPR_ASSERT(ev.success == 0);
347 // We are intentionally not checking the connectivity state since it is
348 // propagated in an asynchronous manner which means that we might see an older
349 // state. We would eventually get the correct state, but since we have already
350 // verified that the ping has failed, checking the connectivity state is not
351 // necessary.
352 }
353
354 class KeepaliveThrottlingTest : public ::testing::Test {
355 protected:
356 // Starts the server and makes sure that the channel is able to get connected.
357 grpc_server* ServerStart(const char* addr, grpc_completion_queue* cq) {
358 // Set up server channel args to expect pings at an interval of 5 seconds
359 // and use a single ping strike
360 grpc_arg server_args[] = {
361 grpc_channel_arg_integer_create(
362 const_cast<char*>(
363 GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS),
364 5 * 1000),
365 grpc_channel_arg_integer_create(
366 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PING_STRIKES), 1)};
367 grpc_channel_args server_channel_args = {GPR_ARRAY_SIZE(server_args),
368 server_args};
369 // Create server
370 grpc_server* server = grpc_server_create(&server_channel_args, nullptr);
371 grpc_server_register_completion_queue(server, cq, nullptr);
372 grpc_server_credentials* server_creds =
373 grpc_insecure_server_credentials_create();
374 GPR_ASSERT(grpc_server_add_http2_port(server, addr, server_creds));
375 grpc_server_credentials_release(server_creds);
376 grpc_server_start(server);
377 return server;
378 }
379 };
380
381 TEST_F(KeepaliveThrottlingTest, KeepaliveThrottlingMultipleChannels) {
382 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
383 std::string server_address =
384 grpc_core::JoinHostPort("127.0.0.1", grpc_pick_unused_port_or_die());
385 grpc_server* server = ServerStart(server_address.c_str(), cq);
386 // create two channel with a keepalive ping interval of 1 second.
387 grpc_arg client_args[] = {
388 grpc_channel_arg_integer_create(
389 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), 0),
390 grpc_channel_arg_integer_create(
391 const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS), 1 * 1000),
392 grpc_channel_arg_integer_create(
393 const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE), 0)};
394 grpc_channel_args client_channel_args = {GPR_ARRAY_SIZE(client_args),
395 client_args};
396 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
397 grpc_channel* channel =
398 grpc_channel_create(server_address.c_str(), creds, &client_channel_args);
399 grpc_channel* channel_dup =
400 grpc_channel_create(server_address.c_str(), creds, &client_channel_args);
401 grpc_channel_credentials_release(creds);
402 int expected_keepalive_time_sec = 1;
403 // We need 3 GOAWAY frames to throttle the keepalive time from 1 second to 8
404 // seconds (> 5sec).
405 for (int i = 0; i < 3; i++) {
406 gpr_log(GPR_INFO, "Expected keepalive time : %d",
407 expected_keepalive_time_sec);
408 EXPECT_EQ(PerformWaitingCall(channel, server, cq), GRPC_STATUS_UNAVAILABLE);
409 expected_keepalive_time_sec *= 2;
410 }
411 gpr_log(
412 GPR_INFO,
413 "Client keepalive time %d should now be in sync with the server settings",
414 expected_keepalive_time_sec);
415 EXPECT_EQ(PerformWaitingCall(channel, server, cq),
416 GRPC_STATUS_DEADLINE_EXCEEDED);
417 // Since the subchannel is shared, the second channel should also have
418 // keepalive settings in sync with the server.
419 gpr_log(GPR_INFO, "Now testing second channel sharing the same subchannel");
420 EXPECT_EQ(PerformWaitingCall(channel_dup, server, cq),
421 GRPC_STATUS_DEADLINE_EXCEEDED);
422 // shutdown and destroy the client and server
423 grpc_channel_destroy(channel);
424 grpc_channel_destroy(channel_dup);
425 ServerShutdownAndDestroy(server, cq);
426 grpc_completion_queue_shutdown(cq);
427 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
428 nullptr)
429 .type != GRPC_QUEUE_SHUTDOWN) {
430 }
431 grpc_completion_queue_destroy(cq);
432 }
433
434 grpc_core::Resolver::Result BuildResolverResult(
435 const std::vector<std::string>& addresses) {
436 grpc_core::Resolver::Result result;
437 result.addresses = grpc_core::ServerAddressList();
438 for (const auto& address_str : addresses) {
439 absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(address_str);
440 if (!uri.ok()) {
441 gpr_log(GPR_ERROR, "Failed to parse uri. Error: %s",
442 uri.status().ToString().c_str());
443 GPR_ASSERT(uri.ok());
444 }
445 grpc_resolved_address address;
446 GPR_ASSERT(grpc_parse_uri(*uri, &address));
447 result.addresses->emplace_back(address.addr, address.len,
448 grpc_core::ChannelArgs());
449 }
450 return result;
451 }
452
453 // Tests that when new subchannels are created due to a change in resolved
454 // addresses, the new subchannels use the updated keepalive time.
455 TEST_F(KeepaliveThrottlingTest, NewSubchannelsUseUpdatedKeepaliveTime) {
456 grpc_core::ExecCtx exec_ctx;
457 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
458 std::string server_address1 =
459 grpc_core::JoinHostPort("127.0.0.1", grpc_pick_unused_port_or_die());
460 std::string server_address2 =
461 grpc_core::JoinHostPort("127.0.0.1", grpc_pick_unused_port_or_die());
462 grpc_server* server1 = ServerStart(server_address1.c_str(), cq);
463 grpc_server* server2 = ServerStart(server_address2.c_str(), cq);
464 // create a single channel with multiple subchannels with a keepalive ping
465 // interval of 1 second. To get finer control on subchannel connection times,
466 // we are using pick_first instead of round_robin and using the fake resolver
467 // response generator to switch between the two.
468 auto response_generator =
469 grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
470 grpc_arg client_args[] = {
471 grpc_channel_arg_integer_create(
472 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), 0),
473 grpc_channel_arg_integer_create(
474 const_cast<char*>(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS), 0),
475 grpc_channel_arg_integer_create(
476 const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS), 1 * 1000),
477 grpc_channel_arg_integer_create(
478 const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE), 0),
479 grpc_core::FakeResolverResponseGenerator::MakeChannelArg(
480 response_generator.get())};
481 grpc_channel_args client_channel_args = {GPR_ARRAY_SIZE(client_args),
482 client_args};
483 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
484 grpc_channel* channel =
485 grpc_channel_create("fake:///", creds, &client_channel_args);
486 grpc_channel_credentials_release(creds);
487 // For a single subchannel 3 GOAWAYs would be sufficient to increase the
488 // keepalive time from 1 second to beyond 5 seconds. Even though we are
489 // alternating between two subchannels, 3 GOAWAYs should still be enough since
490 // the channel should start all new transports with the new keepalive value
491 // (even those from a different subchannel).
492 int expected_keepalive_time_sec = 1;
493 for (int i = 0; i < 3; i++) {
494 gpr_log(GPR_INFO, "Expected keepalive time : %d",
495 expected_keepalive_time_sec);
496 response_generator->SetResponse(BuildResolverResult({absl::StrCat(
497 "ipv4:", i % 2 == 0 ? server_address1 : server_address2)}));
498 // ExecCtx::Flush() might not be enough to make sure that the resolver
499 // result has been propagated, so sleep for a bit.
500 grpc_core::ExecCtx::Get()->Flush();
501 gpr_sleep_until(grpc_timeout_seconds_to_deadline(1));
502 EXPECT_EQ(PerformWaitingCall(channel, i % 2 == 0 ? server1 : server2, cq),
503 GRPC_STATUS_UNAVAILABLE);
504 expected_keepalive_time_sec *= 2;
505 }
506 gpr_log(
507 GPR_INFO,
508 "Client keepalive time %d should now be in sync with the server settings",
509 expected_keepalive_time_sec);
510 response_generator->SetResponse(
511 BuildResolverResult({absl::StrCat("ipv4:", server_address2)}));
512 grpc_core::ExecCtx::Get()->Flush();
513 gpr_sleep_until(grpc_timeout_seconds_to_deadline(1));
514 EXPECT_EQ(PerformWaitingCall(channel, server2, cq),
515 GRPC_STATUS_DEADLINE_EXCEEDED);
516 // shutdown and destroy the client and server
517 grpc_channel_destroy(channel);
518 ServerShutdownAndDestroy(server1, cq);
519 ServerShutdownAndDestroy(server2, cq);
520 grpc_completion_queue_shutdown(cq);
521 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
522 nullptr)
523 .type != GRPC_QUEUE_SHUTDOWN) {
524 }
525 grpc_completion_queue_destroy(cq);
526 }
527
528 // Tests that when a channel has multiple subchannels and receives a GOAWAY with
529 // "too_many_pings" on one of them, all subchannels start any new transports
530 // with an updated keepalive time.
531 TEST_F(KeepaliveThrottlingTest,
532 ExistingSubchannelsUseNewKeepaliveTimeWhenReconnecting) {
533 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
534 std::string server_address1 =
535 grpc_core::JoinHostPort("127.0.0.1", grpc_pick_unused_port_or_die());
536 std::string server_address2 =
537 grpc_core::JoinHostPort("127.0.0.1", grpc_pick_unused_port_or_die());
538 // create a single channel with round robin load balancing policy.
539 auto response_generator =
540 grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
541 grpc_arg client_args[] = {
542 grpc_channel_arg_integer_create(
543 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), 0),
544 grpc_channel_arg_integer_create(
545 const_cast<char*>(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS), 0),
546 grpc_channel_arg_integer_create(
547 const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS), 1 * 1000),
548 grpc_channel_arg_integer_create(
549 const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE), 0),
550 grpc_core::FakeResolverResponseGenerator::MakeChannelArg(
551 response_generator.get())};
552 grpc_channel_args client_channel_args = {GPR_ARRAY_SIZE(client_args),
553 client_args};
554 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
555 grpc_channel* channel =
556 grpc_channel_create("fake:///", creds, &client_channel_args);
557 grpc_channel_credentials_release(creds);
558 response_generator->SetResponse(
559 BuildResolverResult({absl::StrCat("ipv4:", server_address1),
560 absl::StrCat("ipv4:", server_address2)}));
561 // For a single subchannel 3 GOAWAYs would be sufficient to increase the
562 // keepalive time from 1 second to beyond 5 seconds. Even though we are
563 // alternating between two subchannels, 3 GOAWAYs should still be enough since
564 // the channel should start all new transports with the new keepalive value
565 // (even those from a different subchannel).
566 int expected_keepalive_time_sec = 1;
567 for (int i = 0; i < 3; i++) {
568 gpr_log(GPR_ERROR, "Expected keepalive time : %d",
569 expected_keepalive_time_sec);
570 grpc_server* server = ServerStart(
571 i % 2 == 0 ? server_address1.c_str() : server_address2.c_str(), cq);
572 VerifyChannelReady(channel, cq);
573 EXPECT_EQ(PerformWaitingCall(channel, server, cq), GRPC_STATUS_UNAVAILABLE);
574 ServerShutdownAndDestroy(server, cq);
575 VerifyChannelDisconnected(channel, cq);
576 expected_keepalive_time_sec *= 2;
577 }
578 gpr_log(
579 GPR_INFO,
580 "Client keepalive time %d should now be in sync with the server settings",
581 expected_keepalive_time_sec);
582 grpc_server* server = ServerStart(server_address1.c_str(), cq);
583 VerifyChannelReady(channel, cq);
584 EXPECT_EQ(PerformWaitingCall(channel, server, cq),
585 GRPC_STATUS_DEADLINE_EXCEEDED);
586 ServerShutdownAndDestroy(server, cq);
587 // shutdown and destroy the client
588 grpc_channel_destroy(channel);
589 grpc_completion_queue_shutdown(cq);
590 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
591 nullptr)
592 .type != GRPC_QUEUE_SHUTDOWN) {
593 }
594 grpc_completion_queue_destroy(cq);
595 }
596
597 // Perform a simple RPC where the client makes a request expecting a response
598 // with payload.
599 void PerformCallWithResponsePayload(grpc_channel* channel, grpc_server* server,
600 grpc_completion_queue* cq) {
601 grpc_slice response_payload_slice = grpc_slice_from_static_string("hello");
602
603 grpc_call* c;
604 grpc_call* s;
605 grpc_byte_buffer* response_payload =
606 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
607 grpc_core::CqVerifier cqv(cq);
608 grpc_op ops[6];
609 grpc_op* op;
610 grpc_metadata_array initial_metadata_recv;
611 grpc_metadata_array trailing_metadata_recv;
612 grpc_metadata_array request_metadata_recv;
613 grpc_byte_buffer* response_payload_recv = nullptr;
614 grpc_call_details call_details;
615 grpc_status_code status;
616 grpc_call_error error;
617 grpc_slice details;
618 int was_cancelled = 2;
619
620 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(60);
621 c = grpc_channel_create_call(channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
622 grpc_slice_from_static_string("/foo"), nullptr,
623 deadline, nullptr);
624 GPR_ASSERT(c);
625
626 grpc_metadata_array_init(&initial_metadata_recv);
627 grpc_metadata_array_init(&trailing_metadata_recv);
628 grpc_metadata_array_init(&request_metadata_recv);
629 grpc_call_details_init(&call_details);
630
631 memset(ops, 0, sizeof(ops));
632 op = ops;
633 op->op = GRPC_OP_SEND_INITIAL_METADATA;
634 op->data.send_initial_metadata.count = 0;
635 op->flags = 0;
636 op->reserved = nullptr;
637 op++;
638 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
639 op->flags = 0;
640 op->reserved = nullptr;
641 op++;
642 op->op = GRPC_OP_RECV_INITIAL_METADATA;
643 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
644 op->flags = 0;
645 op->reserved = nullptr;
646 op++;
647 op->op = GRPC_OP_RECV_MESSAGE;
648 op->data.recv_message.recv_message = &response_payload_recv;
649 op->flags = 0;
650 op->reserved = nullptr;
651 op++;
652 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
653 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
654 op->data.recv_status_on_client.status = &status;
655 op->data.recv_status_on_client.status_details = &details;
656 op->flags = 0;
657 op->reserved = nullptr;
658 op++;
659 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
660 nullptr);
661 GPR_ASSERT(GRPC_CALL_OK == error);
662
663 error = grpc_server_request_call(server, &s, &call_details,
664 &request_metadata_recv, cq, cq, tag(101));
665 GPR_ASSERT(GRPC_CALL_OK == error);
666 cqv.Expect(tag(101), true);
667 cqv.Verify();
668
669 memset(ops, 0, sizeof(ops));
670 op = ops;
671 op->op = GRPC_OP_SEND_INITIAL_METADATA;
672 op->data.send_initial_metadata.count = 0;
673 op->flags = 0;
674 op->reserved = nullptr;
675 op++;
676 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
677 nullptr);
678 GPR_ASSERT(GRPC_CALL_OK == error);
679
680 cqv.Expect(tag(102), true);
681 cqv.Verify();
682
683 memset(ops, 0, sizeof(ops));
684 op = ops;
685 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
686 op->data.recv_close_on_server.cancelled = &was_cancelled;
687 op->flags = 0;
688 op->reserved = nullptr;
689 op++;
690 op->op = GRPC_OP_SEND_MESSAGE;
691 op->data.send_message.send_message = response_payload;
692 op->flags = 0;
693 op->reserved = nullptr;
694 op++;
695 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
696 op->data.send_status_from_server.trailing_metadata_count = 0;
697 op->data.send_status_from_server.status = GRPC_STATUS_OK;
698 grpc_slice status_details = grpc_slice_from_static_string("xyz");
699 op->data.send_status_from_server.status_details = &status_details;
700 op->flags = 0;
701 op->reserved = nullptr;
702 op++;
703 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
704 nullptr);
705 GPR_ASSERT(GRPC_CALL_OK == error);
706
707 cqv.Expect(tag(103), true);
708 cqv.Expect(tag(1), true);
709 cqv.Verify();
710
711 GPR_ASSERT(status == GRPC_STATUS_OK);
712 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
713 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
714 GPR_ASSERT(was_cancelled == 0);
715 GPR_ASSERT(
716 byte_buffer_eq_slice(response_payload_recv, response_payload_slice));
717
718 grpc_slice_unref(details);
719 grpc_metadata_array_destroy(&initial_metadata_recv);
720 grpc_metadata_array_destroy(&trailing_metadata_recv);
721 grpc_metadata_array_destroy(&request_metadata_recv);
722 grpc_call_details_destroy(&call_details);
723
724 grpc_call_unref(c);
725 grpc_call_unref(s);
726
727 grpc_byte_buffer_destroy(response_payload);
728 grpc_byte_buffer_destroy(response_payload_recv);
729 }
730
731 TEST(TooManyPings, BdpPingNotSentWithoutReceiveSideActivity) {
732 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
733 // create the server
734 std::string server_address =
735 grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
736 grpc_arg server_args[] = {
737 grpc_channel_arg_integer_create(
738 const_cast<char*>(
739 GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS),
740 60 * 1000),
741 grpc_channel_arg_integer_create(
742 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PING_STRIKES), 1)};
743 grpc_channel_args server_channel_args = {GPR_ARRAY_SIZE(server_args),
744 server_args};
745 grpc_server* server = grpc_server_create(&server_channel_args, nullptr);
746 grpc_server_register_completion_queue(server, cq, nullptr);
747 grpc_server_credentials* server_creds =
748 grpc_insecure_server_credentials_create();
749 GPR_ASSERT(
750 grpc_server_add_http2_port(server, server_address.c_str(), server_creds));
751 grpc_server_credentials_release(server_creds);
752 grpc_server_start(server);
753 // create the channel (bdp pings are enabled by default)
754 grpc_arg client_args[] = {
755 grpc_channel_arg_integer_create(
756 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), 0),
757 grpc_channel_arg_integer_create(
758 const_cast<char*>(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS), 1)};
759 grpc_channel_args client_channel_args = {GPR_ARRAY_SIZE(client_args),
760 client_args};
761 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
762 grpc_channel* channel =
763 grpc_channel_create(server_address.c_str(), creds, &client_channel_args);
764 grpc_channel_credentials_release(creds);
765 VerifyChannelReady(channel, cq);
766 EXPECT_EQ(TransportCounter::count(), 2 /* one each for server and client */);
767 grpc_core::CqVerifier cqv(cq);
768 // Channel should be able to send two pings without disconnect if there was no
769 // BDP sent.
770 grpc_channel_ping(channel, cq, tag(1), nullptr);
771 cqv.Expect(tag(1), true);
772 cqv.Verify(grpc_core::Duration::Seconds(5));
773 // Second ping
774 grpc_channel_ping(channel, cq, tag(2), nullptr);
775 cqv.Expect(tag(2), true);
776 cqv.Verify(grpc_core::Duration::Seconds(5));
777 ASSERT_EQ(grpc_channel_check_connectivity_state(channel, 0),
778 GRPC_CHANNEL_READY);
779 PerformCallWithResponsePayload(channel, server, cq);
780 // Wait a bit to make sure that the BDP ping goes out.
781 cqv.VerifyEmpty(grpc_core::Duration::Seconds(1));
782 // The call with a response payload should have triggered a BDP ping.
783 // Send two more pings to verify. The second ping should cause a disconnect.
784 // If BDP was not sent, the second ping would not cause a disconnect.
785 grpc_channel_ping(channel, cq, tag(3), nullptr);
786 cqv.Expect(tag(3), true);
787 cqv.Verify(grpc_core::Duration::Seconds(5));
788 // Second ping
789 grpc_channel_ping(channel, cq, tag(4), nullptr);
790 cqv.Expect(tag(4), true);
791 cqv.Verify(grpc_core::Duration::Seconds(5));
792 // Make sure that the transports have been destroyed
793 VerifyChannelDisconnected(channel, cq);
794 TransportCounter::WaitForTransportsToBeDestroyed();
795 // shutdown and destroy the client and server
796 ServerShutdownAndDestroy(server, cq);
797 grpc_channel_destroy(channel);
798 grpc_completion_queue_shutdown(cq);
799 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
800 nullptr)
801 .type != GRPC_QUEUE_SHUTDOWN) {
802 }
803 grpc_completion_queue_destroy(cq);
804 }
805
806 TEST(TooManyPings, TransportsGetCleanedUpOnDisconnect) {
807 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
808 // create the client and server
809 std::string server_address =
810 grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
811 grpc_arg server_args[] = {
812 grpc_channel_arg_integer_create(
813 const_cast<char*>(
814 GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS),
815 60 * 1000),
816 grpc_channel_arg_integer_create(
817 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PING_STRIKES), 1)};
818 grpc_channel_args server_channel_args = {GPR_ARRAY_SIZE(server_args),
819 server_args};
820 grpc_server* server = grpc_server_create(&server_channel_args, nullptr);
821 grpc_server_register_completion_queue(server, cq, nullptr);
822 grpc_server_credentials* server_creds =
823 grpc_insecure_server_credentials_create();
824 GPR_ASSERT(
825 grpc_server_add_http2_port(server, server_address.c_str(), server_creds));
826 grpc_server_credentials_release(server_creds);
827 grpc_server_start(server);
828 grpc_arg client_args[] = {
829 grpc_channel_arg_integer_create(
830 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), 0),
831 grpc_channel_arg_integer_create(
832 const_cast<char*>(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS), 1)};
833 grpc_channel_args client_channel_args = {GPR_ARRAY_SIZE(client_args),
834 client_args};
835 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
836 grpc_channel* channel =
837 grpc_channel_create(server_address.c_str(), creds, &client_channel_args);
838 grpc_channel_credentials_release(creds);
839 VerifyChannelReady(channel, cq);
840 EXPECT_EQ(TransportCounter::count(), 2 /* one each for server and client */);
841 grpc_core::CqVerifier cqv(cq);
842 // First ping
843 grpc_channel_ping(channel, cq, tag(1), nullptr);
844 cqv.Expect(tag(1), true);
845 cqv.Verify(grpc_core::Duration::Seconds(5));
846 // Second ping
847 grpc_channel_ping(channel, cq, tag(2), nullptr);
848 cqv.Expect(tag(2), true);
849 cqv.Verify(grpc_core::Duration::Seconds(5));
850 // Third ping caused disconnect
851 grpc_channel_ping(channel, cq, tag(2), nullptr);
852 cqv.Expect(tag(2), true);
853 cqv.Verify(grpc_core::Duration::Seconds(5));
854 // Make sure that the transports have been destroyed
855 VerifyChannelDisconnected(channel, cq);
856 TransportCounter::WaitForTransportsToBeDestroyed();
857 // shutdown and destroy the client and server
858 ServerShutdownAndDestroy(server, cq);
859 grpc_channel_destroy(channel);
860 grpc_completion_queue_shutdown(cq);
861 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
862 nullptr)
863 .type != GRPC_QUEUE_SHUTDOWN) {
864 }
865 grpc_completion_queue_destroy(cq);
866 }
867
868 } // namespace
869
870 int main(int argc, char** argv) {
871 ::testing::InitGoogleTest(&argc, argv);
872 grpc::testing::TestEnvironment env(&argc, argv);
873 grpc_core::TestOnlySetGlobalHttp2TransportInitCallback(
874 TransportCounter::CounterInitCallback);
875 grpc_core::TestOnlySetGlobalHttp2TransportDestructCallback(
876 TransportCounter::CounterDestructCallback);
877 grpc_init();
878 auto result = RUN_ALL_TESTS();
879 grpc_shutdown();
880 return result;
881 }